6 # This script checks for required updates to the database.
8 # Part of the Koha Library Software www.koha.org
9 # Licensed under the GPL.
12 # - Would also be a good idea to offer to do a backup at this time...
14 # NOTE: If you do something more than once in here, make it table driven.
23 # FIXME - /etc/koha.conf might not exist, so shouldn't use
26 # FIXME - The user might be installing a new database, so can't rely
27 # on /etc/koha.conf anyway.
34 %existingtables, # tables already in database
38 $type, $null, $key, $default, $extra,
39 $prefitem, # preference item in systempreferences table
42 my $dbh = C4::Context->dbh;
47 # Tables to add if they don't exist
49 shelfcontents=>"( shelfnumber int not null,
50 itemnumber int not null,
52 bookshelf=>"( shelfnumber int auto_increment primary key,
53 shelfname char(255))",
54 z3950queue=>"( id int auto_increment primary key,
63 identifier char(30))",
64 z3950results=>"( id int auto_increment primary key,
74 branchrelations=>"( branchcode varchar(4),
75 categorycode varchar(4))",
76 websites=>"( websitenumber int(11) NOT NULL auto_increment,
77 biblionumber int(11) NOT NULL default '0',
81 PRIMARY KEY (websitenumber) )",
82 marcrecorddone=>"( isbn char(40),
85 controlnumber char(40))",
86 uploadedmarc=>"( id int(11) NOT NULL auto_increment PRIMARY KEY,
88 hidden smallint(6) default NULL,
89 name varchar(255) default NULL)",
90 ethnicity=>"( code varchar(10) NOT NULL default '',
91 name varchar(255) default NULL,
92 PRIMARY KEY (code) )",
93 sessions=>"( sessionID varchar(255) NOT NULL default '',
94 userid varchar(255) default NULL,
95 ip varchar(16) default NULL,
97 PRIMARY KEY (sessionID) )",
98 sessionqueries=>"( sessionID varchar(255) NOT NULL default '',
99 userid char(100) NOT NULL default '',
100 ip char(18) NOT NULL default '',
101 url text NOT NULL default '' )",
102 bibliothesaurus=> "( code BIGINT not null AUTO_INCREMENT,
103 freelib CHAR (255) not null ,
104 stdlib CHAR (255) not null ,
105 type CHAR (80) not null ,
107 INDEX (freelib),index(stdlib),index(type) )",
109 bibid bigint(20) unsigned NOT NULL auto_increment,
110 biblionumber int(11) NOT NULL default '0',
111 datecreated date NOT NULL default '0000-00-00',
112 datemodified date default NULL,
113 origincode char(20) default NULL,
115 KEY origincode (origincode),
116 KEY biblionumber (biblionumber)
118 marc_blob_subfield => "(
119 blobidlink bigint(20) NOT NULL auto_increment,
120 subfieldvalue longtext NOT NULL,
121 PRIMARY KEY (blobidlink)
123 marc_subfield_structure => "(
124 tagfield char(3) NOT NULL default '',
125 tagsubfield char(1) NOT NULL default '',
126 liblibrarian char(255) NOT NULL default '',
127 libopac char(255) NOT NULL default '',
128 repeatable tinyint(4) NOT NULL default '0',
129 mandatory tinyint(4) NOT NULL default '0',
130 kohafield char(40) NOT NULL default '',
132 PRIMARY KEY (tagfield,tagsubfield),
133 KEY kohafield (kohafield),
136 marc_subfield_table => "(
137 subfieldid bigint(20) unsigned NOT NULL auto_increment,
138 bibid bigint(20) unsigned NOT NULL default '0',
139 tag char(3) NOT NULL default '',
140 tagorder tinyint(4) NOT NULL default '1',
141 tag_indicator char(2) NOT NULL default '',
142 subfieldcode char(1) NOT NULL default '',
143 subfieldorder tinyint(4) NOT NULL default '1',
144 subfieldvalue varchar(255) default NULL,
145 valuebloblink bigint(20) default NULL,
146 PRIMARY KEY (subfieldid),
149 KEY tag_indicator (tag_indicator),
150 KEY subfieldorder (subfieldorder),
151 KEY subfieldcode (subfieldcode),
152 KEY subfieldvalue (subfieldvalue),
153 KEY tagorder (tagorder)
155 marc_tag_structure => "(
156 tagfield char(3) NOT NULL default '',
157 liblibrarian char(255) NOT NULL default '',
158 libopac char(255) NOT NULL default '',
159 repeatable tinyint(4) NOT NULL default '0',
160 mandatory tinyint(4) NOT NULL default '0',
161 PRIMARY KEY (tagfield)
164 bibid bigint(20) NOT NULL default '0',
165 tag char(3) NOT NULL default '',
166 tagorder tinyint(4) NOT NULL default '1',
167 subfieldid char(1) NOT NULL default '',
168 subfieldorder tinyint(4) NOT NULL default '1',
169 word varchar(255) NOT NULL default '',
170 sndx_word varchar(255) NOT NULL default '',
173 KEY tagorder (tagorder),
174 KEY subfieldid (subfieldid),
175 KEY subfieldorder (subfieldorder),
177 KEY sndx_word (sndx_word)
179 marc_breeding =>"( file varchar(80) NOT NULL default '',
180 isbn varchar(10) NOT NULL default '',
188 biblio=>{ 'abstract' => 'text' },
189 deletedbiblio=>{ 'abstract' => 'text' },
190 biblioitems=>{ 'lccn' => 'char(25)',
191 'url' => 'varchar(255)',
193 deletedbiblioitems=>{ 'lccn' => 'char(25)',
194 'url' => 'varchar(255)',
196 branchtransfers=>{ 'datearrived' => 'datetime' },
197 statistics=>{'borrowernumber' =>'int(11)'},
198 aqbooksellers=>{'invoicedisc' =>'float(6,4)',
199 'nocalc' => 'int(11)'},
200 borrowers=>{'userid' => 'char(30)',
201 'password' => 'char(30)',},
202 aqorders=>{'budgetdate' => 'date'},
203 #added so that reference items are not available for reserves...
204 itemtypes=>{'notforloan' => 'smallint(6)'},
205 systempreferences =>{'explanation' => 'char(80)'},
209 classification =>'classification',
210 multipart =>'multipart',
211 multivolume =>'multivolume',
212 newitems =>'newitems',
213 procedures =>'procedures',
214 publisher =>'publisher',
215 searchstats =>'searchstats',
216 serialissues =>'serialissues',
219 # Default system preferences
221 'autoMemberNum'=> ['1','1 or else. If 1, Barcode is auto-calculated'],
222 'acquisitions'=> ['simple','normal or simple : will use acquisition system found in directory acqui.simple or acquisition'],
223 'template' => ['default','template default name'],
224 'autoBarcode' => ['0','1 or else. If 1, Barcode is auto-calculated'],
225 'insecure' => ['no','if YES, no auth at all is needed. Be careful if you set this to yes !']
231 # Get version of MySQL database engine.
232 my $mysqlversion=`mysqld --version`;
233 $mysqlversion=~/Ver (\S*) /;
235 if ($mysqlversion ge '3.23') {
236 print "Could convert to MyISAM database tables...\n";
239 #---------------------------------
242 # Collect all tables into a list
243 $sth=$dbh->prepare("show tables");
245 while (my ($table) = $sth->fetchrow) {
246 $existingtables{$table}=1;
249 # Now add any missing tables
250 foreach $table ( keys %requiretables ) {
251 print "Checking $table table...\n" if $debug;
252 unless ($existingtables{$table} ) {
253 print "Adding $table table...\n";
254 my $sth=$dbh->prepare(
255 "create table $table $requiretables{$table}" );
258 print "Error : $sth->errstr \n";
264 # now drop useless tables
265 foreach $table ( keys %dropable_table) {
266 print "Dropping unused tables...\n" if $debug;
267 if ($existingtables{$table} ) {
268 $dbh->do("drop table $table");
270 print "Error : $dbh->errstr \n";
274 unless ($existingtables{'z3950servers'}) {
275 print "Adding z3950servers table...\n";
276 my $sti=$dbh->prepare("create table z3950servers (
287 $sti=$dbh->prepare("insert into z3950servers
288 values ('z3950.loc.gov',
292 'Library of Congress',
297 #---------------------------------
300 foreach $table ( keys %requirefields ) {
301 print "Check table $table\n" if $debug;
302 $sth=$dbh->prepare("show columns from $table");
305 while ( ($column, $type, $null, $key, $default, $extra)
307 $types{$column}=$type;
309 foreach $column ( keys %{ $requirefields{$table} } ) {
310 print " Check column $column\n" if $debug;
311 if ( ! $types{$column} ) {
312 # column doesn't exist
313 print "Adding $column field to $table table...\n";
314 $query="alter table $table
315 add column $column " . $requirefields{$table}->{$column} ;
316 print "Execute: $query\n" if $debug;
317 my $sti=$dbh->prepare($query);
320 print "**Error : $sti->errstr \n";
327 # Get list of columns from items table
330 $sth=$dbh->prepare("show columns from items");
332 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
333 $itemtypes{$column}=$type;
336 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
337 $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
340 print "Setting maximum barcode length to 20 (was $oldlength).\n";
341 my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
346 # extending the timestamp in branchtransfers...
349 $sth=$dbh->prepare("show columns from branchtransfers");
351 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
352 $branchtransfers{$column}=$type;
355 unless ($branchtransfers{'datesent'} eq 'datetime') {
356 print "Setting type of datesent in branchtransfers to datetime.\n";
357 my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
361 unless ($branchtransfers{'datearrived'} eq 'datetime') {
362 print "Setting type of datearrived in branchtransfers to datetime.\n";
363 my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
367 # changing the branchcategories table around...
368 my %branchcategories;
370 $sth=$dbh->prepare("show columns from branchcategories");
372 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
373 $branchcategories{$column}=$type;
376 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
377 print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
378 my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
380 $sti=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
384 unless ($branchcategories{'categoryname'} eq 'text') {
385 print "Changing branchcode in branchcategories to categoryname text.\n";
386 my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
390 unless ($branchcategories{'codedescription'} eq 'text') {
391 print "Replacing branchholding in branchcategories with codedescription text.\n";
392 my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
397 # Populate systempreferences if it is empty
399 foreach $prefitem ( keys %defaultprefs ) {
400 $sth=$dbh->prepare("select value
401 from systempreferences
403 $sth->execute($prefitem);
404 unless ($sth->rows) {
405 print "Adding system preference item $prefitem with value " .$defaultprefs{$prefitem}[0] ."\n";
406 $sti=$dbh->prepare("insert into systempreferences (variable, value,explanation) values (?,?,?)");
407 $sti->execute($prefitem,$defaultprefs{$prefitem}[0],$defaultprefs{$prefitem}[1]);
417 # Revision 1.22 2002/10/15 10:08:19 tipaul
418 # fixme corrected, re-indent and adding the marc_breeding table (see commit of marcimport.pl for more explanations about breeding)
420 # Revision 1.21 2002/10/14 11:48:59 tipaul
423 # Revision 1.20 2002/10/10 04:49:41 arensb
424 # Added some FIXME comments.
426 # Revision 1.19 2002/10/05 10:17:17 arensb
427 # Merged with arensb-context branch: use C4::Context->dbh instead of
428 # &C4Connect, and generally prefer C4::Context over C4::Database.
430 # Revision 1.18.2.2 2002/10/05 06:18:43 arensb
431 # Added a whole mess of FIXME comments.
433 # Revision 1.18.2.1 2002/10/04 02:46:00 arensb
434 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
437 # Revision 1.18 2002/09/24 13:50:55 tipaul
438 # long WAS the road to 1.3.0...
439 # coming VERY SOON NOW...
440 # modifying installer and buildrelease to update the DB
442 # Revision 1.17 2002/09/24 12:57:35 tipaul
443 # long WAS the road to 1.3.0...
444 # coming VERY SOON NOW...
445 # modifying installer and buildrelease to update the DB
447 # Revision 1.16 2002/07/31 02:34:27 finlayt
449 # added "notforloan" field to the itemtypes table.
451 # Revision 1.15 2002/07/20 22:30:06 rangi
452 # Making sure fix makes it into the main branch as well
455 # Revision 1.14 2002/07/08 16:20:26 tonnesen
456 # Added sessionqueries table and password/userid fields to borrowers table
458 # Revision 1.13 2002/07/04 18:05:36 tonnesen
461 # Revision 1.12 2002/07/04 16:41:06 tonnesen
462 # Merged changes from rel-1-2. Abstracted table structure changes by alan.