Merged changes from rel-1-2. Abstracted table structure changes by alan.
[koha.git] / updater / updatedatabase
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Database Updater
6 # This script checks for required updates to the database.  
7
8 # Part of the Koha Library Software www.koha.org
9 # Licensed under the GPL.
10
11 # Bugs/ToDo: 
12 # - Would also be a good idea to offer to do a backup at this time...
13
14 # NOTE:  If you do something more than once in here, make it table driven.
15
16 use strict;
17
18 # CPAN modules
19 use DBI;
20
21 # Koha modules
22 use C4::Database;
23
24 my $debug=0;
25
26 my (
27         $sth, $sti,
28         $query,
29         %existingtables,        # tables already in database
30         %types,
31         $table,
32         $column,
33         $type, $null, $key, $default, $extra,
34         $prefitem,              # preference item in systempreferences table
35 );
36
37 #-------------------
38 # Defines
39
40 # Tables to add if they don't exist
41 my %requiretables=(
42     shelfcontents=>"( shelfnumber int not null, 
43         itemnumber int not null, 
44         flags int)",
45     bookshelf=>"( shelfnumber int auto_increment primary key, 
46         shelfname char(255))",
47     z3950queue=>"( id int auto_increment primary key, 
48         term text, 
49         type char(10), 
50         startdate int, 
51         enddate int, 
52         done smallint, 
53         results longblob, 
54         numrecords int, 
55         servers text, 
56         identifier char(30))",
57     z3950results=>"( id int auto_increment primary key, 
58         queryid int, 
59         server char(255), 
60         startdate int, 
61         enddate int, 
62         results longblob, 
63         numrecords int, 
64         numdownloaded int, 
65         highestseen int, 
66         active smallint)",
67     branchrelations=>"( branchcode varchar(4), 
68         categorycode varchar(4))",
69     websites=>"( websitenumber int(11) NOT NULL auto_increment,
70         biblionumber int(11) NOT NULL default '0',
71         title text,
72         description text,
73         url varchar(255),
74         PRIMARY KEY (websitenumber) )",
75 );
76     marcrecorddone=>"( isbn char(40),
77                       issn char(40),
78                       lccn char(40),
79                       controlnumber char(40))",
80     uploadedmarc=>"( id int(11) NOT NULL auto_increment PRIMARY KEY,
81                     marc longblob,
82                     hidden smallint(6) default NULL,
83                     name varchar(255) default NULL)",
84     ethnicity=>"( code varchar(10) NOT NULL default '',
85                 name varchar(255) default NULL,
86                 PRIMARY KEY  (code)   )",
87 );
88
89 my %requirefields=(
90     biblio=>{ 'abstract' => 'text' },
91     deletedbiblio=>{ 'abstract' => 'text' },
92     biblioitems=>{ 'lccn' => 'char(25)',
93                 'url' => 'varchar(255)',
94                 'marc' => 'text' },
95     deletedbiblioitems=>{ 'lccn' => 'char(25)',
96                 'url' => 'varchar(255)',
97                 'marc' => 'text' },
98     branchtransfers=>{ 'datearrived' => 'datetime' },
99     statistics=>{'borrowernumber' =>'int(11)'},
100     aqbooksellers=>{'invoicedisc' =>'float(6,4)',
101                      'nocalc' => 'int(11)'},
102 );
103
104 # Default system preferences
105 my %defaultprefs=(
106     'autoMemberNum'=> '1',
107     'acquisitions'=> 'simple',
108 );
109
110 #-------------------
111 # Initialize
112 my $dbh=C4Connect;
113
114 # Start checking
115
116 # Get version of MySQL database engine.
117 my $mysqlversion=`mysqld --version`;
118 $mysqlversion=~/Ver (\S*) /;
119 $mysqlversion=$1;
120 if ($mysqlversion ge '3.23') {
121     print "Could convert to MyISAM database tables...\n";
122 }
123
124 #---------------------------------
125 # Tables
126
127 # Collect all tables into a list
128 $sth=$dbh->prepare("show tables");
129 $sth->execute;
130 while (my ($table) = $sth->fetchrow) {
131     $existingtables{$table}=1;
132 }
133
134 # Now add any missing tables
135 foreach $table ( keys %requiretables ) {
136     print "Checking $table table...\n" if $debug;
137     unless ($existingtables{$table} ) {
138         print "Adding $table table...\n";
139         my $sth=$dbh->prepare(
140                 "create table $table $requiretables{$table}" );
141         $sth->execute;
142         if ($sth->err) {
143                 print "Error : $sth->errstr \n";
144                 $sth->finish;
145         } # if error
146     } # unless exists
147 } # foreach
148
149 unless ($existingtables{'z3950servers'}) {
150     print "Adding z3950servers table...\n";
151     my $sti=$dbh->prepare("create table z3950servers (
152         host char(255), 
153         port int, 
154         db char(255), 
155         userid char(255), 
156         password char(255), 
157         name text, 
158         id int, 
159         checked smallint, 
160         rank int)");
161     $sti->execute;
162     $sti=$dbh->prepare("insert into z3950servers 
163         values ('z3950.loc.gov', 
164         7090, 
165         'voyager', 
166         '', '', 
167         'Library of Congress', 
168         1, 1, 1)");
169     $sti->execute;
170 }
171
172 #---------------------------------
173 # Columns
174
175 foreach $table ( keys %requirefields ) {
176     print "Check table $table\n" if $debug;
177     $sth=$dbh->prepare("show columns from $table");
178     $sth->execute();
179     undef %types;
180     while ( ($column, $type, $null, $key, $default, $extra) 
181                 = $sth->fetchrow) {
182         $types{$column}=$type;
183     } # while 
184     foreach $column ( keys %{ $requirefields{$table} } )  {
185         print "  Check column $column\n" if $debug;
186         if ( ! $types{$column} ) {
187             # column doesn't exist
188             print "Adding $column field to $table table...\n";
189             $query="alter table $table
190                 add column $column " . $requirefields{$table}->{$column} ;
191             print "Execute: $query\n" if $debug;
192             my $sti=$dbh->prepare($query);
193             $sti->execute;
194             if ($sti->err) {
195                     print "**Error : $sti->errstr \n";
196                     $sti->finish;
197             } # if error
198         } # if column
199     } # foreach column
200 } # foreach table
201
202 # Get list of columns from items table
203 my %itemtypes;
204
205 my $sth=$dbh->prepare("show columns from items");
206 $sth->execute;
207 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
208     $itemtypes{$column}=$type;
209 }
210
211 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
212     $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
213     my $oldlength=$1;
214     if ($oldlength<20) {
215         print "Setting maximum barcode length to 20 (was $oldlength).\n";
216         my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
217         $sti->execute;
218     }
219 }
220
221 # extending the timestamp in branchtransfers...
222 my %branchtransfers;
223
224 my $sth=$dbh->prepare("show columns from branchtransfers");
225 $sth->execute;
226 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
227     $branchtransfers{$column}=$type;
228 }
229
230 unless ($branchtransfers{'datesent'} eq 'datetime') {
231     print "Setting type of datesent in branchtransfers to datetime.\n";
232     my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
233     $sti->execute;
234 }
235
236 unless ($branchtransfers{'datearrived'} eq 'datetime') {
237     print "Setting type of datearrived in branchtransfers to datetime.\n";
238     my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
239     $sti->execute;
240 }
241
242 # changing the branchcategories table around...
243 my %branchcategories;
244
245 my $sth=$dbh->prepare("show columns from branchcategories");
246 $sth->execute;
247 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
248     $branchcategories{$column}=$type;
249 }
250
251 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
252     print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
253     my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
254     $sti->execute;
255     $sti=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
256     $sti->execute;
257 }
258
259 unless ($branchcategories{'categoryname'} eq 'text') {
260     print "Changing branchcode in branchcategories to categoryname text.\n";
261     my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
262     $sth->execute;
263 }
264
265 unless ($branchcategories{'codedescription'} eq 'text') {
266     print "Replacing branchholding in branchcategories with codedescription text.\n";
267     my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
268     $sth->execute;
269 }
270
271
272 # Populate systempreferences if it is empty
273
274 foreach $prefitem ( keys %defaultprefs ) {
275     $sth=$dbh->prepare("select value 
276         from systempreferences 
277         where variable=?");
278     $sth->execute($prefitem);
279     unless ($sth->rows) {
280         print "Adding system preference item $prefitem with value " .
281                 $defaultprefs{$prefitem} ."\n";
282         $sti=$dbh->prepare("
283           insert into systempreferences (variable, value) 
284           values (?,?)");
285         $sti->execute($prefitem,$defaultprefs{$prefitem});
286     } # unless
287 } # foreach
288
289
290 $sth->finish;
291 $dbh->disconnect;
292
293 exit;
294
295 # $Log$
296 # Revision 1.12  2002/07/04 16:41:06  tonnesen
297 # Merged changes from rel-1-2.  Abstracted table structure changes by alan.
298 #