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