bug fix
[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     marcrecorddone=>"( isbn char(40),
76                       issn char(40),
77                       lccn char(40),
78                       controlnumber char(40))",
79     uploadedmarc=>"( id int(11) NOT NULL auto_increment PRIMARY KEY,
80                     marc longblob,
81                     hidden smallint(6) default NULL,
82                     name varchar(255) default NULL)",
83     ethnicity=>"( code varchar(10) NOT NULL default '',
84                 name varchar(255) default NULL,
85                 PRIMARY KEY  (code)   )",
86     sessions=>"( sessionID varchar(255) NOT NULL default '',
87                 userid varchar(255) default NULL,
88                 ip varchar(16) default NULL,
89                 lasttime int,
90                 PRIMARY KEY (sessionID)   )",
91 );
92
93
94 my %requirefields=(
95     biblio=>{ 'abstract' => 'text' },
96     deletedbiblio=>{ 'abstract' => 'text' },
97     biblioitems=>{ 'lccn' => 'char(25)',
98                 'url' => 'varchar(255)',
99                 'marc' => 'text' },
100     deletedbiblioitems=>{ 'lccn' => 'char(25)',
101                 'url' => 'varchar(255)',
102                 'marc' => 'text' },
103     branchtransfers=>{ 'datearrived' => 'datetime' },
104     statistics=>{'borrowernumber' =>'int(11)'},
105     aqbooksellers=>{'invoicedisc' =>'float(6,4)',
106                      'nocalc' => 'int(11)'},
107 );
108
109 # Default system preferences
110 my %defaultprefs=(
111     'autoMemberNum'=> '1',
112     'acquisitions'=> 'simple',
113 );
114
115 #-------------------
116 # Initialize
117 my $dbh=C4Connect;
118
119 # Start checking
120
121 # Get version of MySQL database engine.
122 my $mysqlversion=`mysqld --version`;
123 $mysqlversion=~/Ver (\S*) /;
124 $mysqlversion=$1;
125 if ($mysqlversion ge '3.23') {
126     print "Could convert to MyISAM database tables...\n";
127 }
128
129 #---------------------------------
130 # Tables
131
132 # Collect all tables into a list
133 $sth=$dbh->prepare("show tables");
134 $sth->execute;
135 while (my ($table) = $sth->fetchrow) {
136     $existingtables{$table}=1;
137 }
138
139 # Now add any missing tables
140 foreach $table ( keys %requiretables ) {
141     print "Checking $table table...\n" if $debug;
142     unless ($existingtables{$table} ) {
143         print "Adding $table table...\n";
144         my $sth=$dbh->prepare(
145                 "create table $table $requiretables{$table}" );
146         $sth->execute;
147         if ($sth->err) {
148                 print "Error : $sth->errstr \n";
149                 $sth->finish;
150         } # if error
151     } # unless exists
152 } # foreach
153
154 unless ($existingtables{'z3950servers'}) {
155     print "Adding z3950servers table...\n";
156     my $sti=$dbh->prepare("create table z3950servers (
157         host char(255), 
158         port int, 
159         db char(255), 
160         userid char(255), 
161         password char(255), 
162         name text, 
163         id int, 
164         checked smallint, 
165         rank int)");
166     $sti->execute;
167     $sti=$dbh->prepare("insert into z3950servers 
168         values ('z3950.loc.gov', 
169         7090, 
170         'voyager', 
171         '', '', 
172         'Library of Congress', 
173         1, 1, 1)");
174     $sti->execute;
175 }
176
177 #---------------------------------
178 # Columns
179
180 foreach $table ( keys %requirefields ) {
181     print "Check table $table\n" if $debug;
182     $sth=$dbh->prepare("show columns from $table");
183     $sth->execute();
184     undef %types;
185     while ( ($column, $type, $null, $key, $default, $extra) 
186                 = $sth->fetchrow) {
187         $types{$column}=$type;
188     } # while 
189     foreach $column ( keys %{ $requirefields{$table} } )  {
190         print "  Check column $column\n" if $debug;
191         if ( ! $types{$column} ) {
192             # column doesn't exist
193             print "Adding $column field to $table table...\n";
194             $query="alter table $table
195                 add column $column " . $requirefields{$table}->{$column} ;
196             print "Execute: $query\n" if $debug;
197             my $sti=$dbh->prepare($query);
198             $sti->execute;
199             if ($sti->err) {
200                     print "**Error : $sti->errstr \n";
201                     $sti->finish;
202             } # if error
203         } # if column
204     } # foreach column
205 } # foreach table
206
207 # Get list of columns from items table
208 my %itemtypes;
209
210 my $sth=$dbh->prepare("show columns from items");
211 $sth->execute;
212 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
213     $itemtypes{$column}=$type;
214 }
215
216 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
217     $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
218     my $oldlength=$1;
219     if ($oldlength<20) {
220         print "Setting maximum barcode length to 20 (was $oldlength).\n";
221         my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
222         $sti->execute;
223     }
224 }
225
226 # extending the timestamp in branchtransfers...
227 my %branchtransfers;
228
229 my $sth=$dbh->prepare("show columns from branchtransfers");
230 $sth->execute;
231 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
232     $branchtransfers{$column}=$type;
233 }
234
235 unless ($branchtransfers{'datesent'} eq 'datetime') {
236     print "Setting type of datesent in branchtransfers to datetime.\n";
237     my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
238     $sti->execute;
239 }
240
241 unless ($branchtransfers{'datearrived'} eq 'datetime') {
242     print "Setting type of datearrived in branchtransfers to datetime.\n";
243     my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
244     $sti->execute;
245 }
246
247 # changing the branchcategories table around...
248 my %branchcategories;
249
250 my $sth=$dbh->prepare("show columns from branchcategories");
251 $sth->execute;
252 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
253     $branchcategories{$column}=$type;
254 }
255
256 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
257     print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
258     my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
259     $sti->execute;
260     $sti=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
261     $sti->execute;
262 }
263
264 unless ($branchcategories{'categoryname'} eq 'text') {
265     print "Changing branchcode in branchcategories to categoryname text.\n";
266     my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
267     $sth->execute;
268 }
269
270 unless ($branchcategories{'codedescription'} eq 'text') {
271     print "Replacing branchholding in branchcategories with codedescription text.\n";
272     my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
273     $sth->execute;
274 }
275
276
277 # Populate systempreferences if it is empty
278
279 foreach $prefitem ( keys %defaultprefs ) {
280     $sth=$dbh->prepare("select value 
281         from systempreferences 
282         where variable=?");
283     $sth->execute($prefitem);
284     unless ($sth->rows) {
285         print "Adding system preference item $prefitem with value " .
286                 $defaultprefs{$prefitem} ."\n";
287         $sti=$dbh->prepare("
288           insert into systempreferences (variable, value) 
289           values (?,?)");
290         $sti->execute($prefitem,$defaultprefs{$prefitem});
291     } # unless
292 } # foreach
293
294
295 $sth->finish;
296 $dbh->disconnect;
297
298 exit;
299
300 # $Log$
301 # Revision 1.13  2002/07/04 18:05:36  tonnesen
302 # bug fix
303 #
304 # Revision 1.12  2002/07/04 16:41:06  tonnesen
305 # Merged changes from rel-1-2.  Abstracted table structure changes by alan.
306 #