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