Modularize table creation, driven by hash instead of redundant code.
[koha.git] / updater / updatedatabase
1 #!/usr/bin/perl
2
3 # Database Updater
4 # This script checks for required updates to the database.  
5
6 # Part of the Koha Library Software www.koha.org
7 # Licensed under the GPL.
8
9 # Bugs/ToDo: 
10 # - Would also be a good idea to offer to do a backup at this time...
11
12 use strict;
13
14 # CPAN modules
15 use DBI;
16
17 # Koha modules
18 use C4::Database;
19
20 my %existingtables;     # tables already in database
21 my %types;
22 my $table;
23
24 #-------------------
25 # Defines
26
27 # Tables to add if they don't exist
28 my %requiretables=(
29     shelfcontents=>"( shelfnumber int not null, 
30         itemnumber int not null, 
31         flags int)",
32     bookshelf=>"( shelfnumber int auto_increment primary key, 
33         shelfname char(255))",
34     z3950queue=>"( id int auto_increment primary key, 
35         term text, 
36         type char(10), 
37         startdate int, 
38         enddate int, 
39         done smallint, 
40         results longblob, 
41         numrecords int, 
42         servers text, 
43         identifier char(30))",
44     z3950results=>"( id int auto_increment primary key, 
45         queryid int, 
46         server char(255), 
47         startdate int, 
48         enddate int, 
49         results longblob, 
50         numrecords int, 
51         numdownloaded int, 
52         highestseen int, 
53         active smallint)",
54     branchrelations=>"( branchcode varchar(4), 
55         categorycode varchar(4))",
56 );
57
58 #-------------------
59 # Initialize
60 my $dbh=C4Connect;
61
62 # Start checking
63
64 # Get version of MySQL database engine.
65 my $mysqlversion=`mysqld --version`;
66 $mysqlversion=~/Ver (\S*) /;
67 $mysqlversion=$1;
68 if ($mysqlversion ge '3.23') {
69     print "Could convert to MyISAM database tables...\n";
70 }
71
72 #---------------------------------
73 # Tables
74
75 # Collect all tables into a list
76 my $sth=$dbh->prepare("show tables");
77 $sth->execute;
78 while (my ($table) = $sth->fetchrow) {
79     $existingtables{$table}=1;
80 }
81
82 # Now add any missing tables
83 foreach $table ( keys %requiretables ) {
84     unless ($existingtables{$table} ) {
85         print "Adding $table table...\n";
86         my $sth=$dbh->prepare(
87                 "create table $table $requiretables{$table}" );
88         $sth->execute;
89         if ($sth->err) {
90                 print "Error : $sth->errstr \n";
91                 $sth->finish;
92         } # if error
93     } # unless exists
94 } # foreach
95 exit;
96
97 unless ($existingtables{'z3950servers'}) {
98     print "Adding z3950servers table...\n";
99     my $sti=$dbh->prepare("create table z3950servers (
100         host char(255), 
101         port int, 
102         db char(255), 
103         userid char(255), 
104         password char(255), 
105         name text, 
106         id int, 
107         checked smallint, 
108         rank int)");
109     $sti->execute;
110     $sti=$dbh->prepare("insert into z3950servers 
111         values ('z3950.loc.gov', 
112         7090, 
113         'voyager', 
114         '', '', 
115         'Library of Congress', 
116         1, 1, 1)");
117     $sti->execute;
118 }
119
120 #---------------------------------
121 # Columns
122
123
124 # Get list of columns from biblioitems table
125
126 my $sth=$dbh->prepare("show columns from biblioitems");
127 $sth->execute;
128 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
129     $types{$column}=$type;
130 }
131 unless ($types{'lccn'}) {
132     # Add LCCN field to biblioitems db
133     print "Adding lccn field to biblioitems table...\n";
134     my $sti=$dbh->prepare("alter table biblioitems 
135         add column lccn char(25)");
136     $sti->execute;
137 }
138 unless ($types{'marc'}) {
139     # Add MARC field to biblioitems db (not used anymore)
140     print "Adding marc field to biblioitems table...\n";
141     my $sti=$dbh->prepare("alter table biblioitems 
142         add column marc text");
143     $sti->execute;
144 }
145
146 # Get list of columns from biblioitems table
147 my %itemtypes;
148
149 my $sth=$dbh->prepare("show columns from items");
150 $sth->execute;
151 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
152     $itemtypes{$column}=$type;
153 }
154
155 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
156     $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
157     my $oldlength=$1;
158     if ($oldlength<20) {
159         print "Setting maximum barcode length to 20 (was $oldlength).\n";
160         my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
161         $sti->execute;
162     }
163 }
164
165 # extending the timestamp in branchtransfers...
166 my %branchtransfers;
167
168 my $sth=$dbh->prepare("show columns from branchtransfers");
169 $sth->execute;
170 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
171     $branchtransfers{$column}=$type;
172 }
173
174 unless ($branchtransfers{'datesent'} eq 'datetime') {
175     print "Setting type of datesent in branchtransfers to datetime.\n";
176     my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
177     $sti->execute;
178 }
179
180 unless ($branchtransfers{'datearrived'} eq 'datetime') {
181     print "Setting type of datearrived in branchtransfers to datetime.\n";
182     my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
183     $sti->execute;
184 }
185
186 # changing the branchcategories table around...
187 my %branchcategories;
188
189 my $sth=$dbh->prepare("show columns from branchcategories");
190 $sth->execute;
191 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
192     $branchcategories{$column}=$type;
193 }
194
195 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
196     print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
197     my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
198     $sth->execute;
199     $sth=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
200     $sth->execute;
201 }
202
203 unless ($branchcategories{'branchcode'} eq 'varchar(4)') {
204     print "Changing branchcode in branchcategories to categoryname text.\n";
205     my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
206     $sth->execute;
207 }
208
209 unless ($branchcategories{'codedescription'} eq 'text') {
210     print "Replacing branchholding in branchcategories with codedescription text.\n";
211     my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
212     $sth->execute;
213 }
214
215
216 $sth->finish;
217 $dbh->disconnect;