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.
22 use C4::Context("/etc/koha.conf.tmp");
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=> "( id bigint(20) NOT NULL auto_increment,
103 freelib char(255) NOT NULL default '',
104 stdlib char(255) NOT NULL default '',
105 category char(10) NOT NULL default '',
106 level tinyint(4) NOT NULL default '1',
107 hierarchy char(80) NOT NULL default '',
109 KEY freelib (freelib),
111 KEY category (category),
112 KEY hierarchy (hierarchy)
115 bibid bigint(20) unsigned NOT NULL auto_increment,
116 biblionumber int(11) NOT NULL default '0',
117 datecreated date NOT NULL default '0000-00-00',
118 datemodified date default NULL,
119 origincode char(20) default NULL,
121 KEY origincode (origincode),
122 KEY biblionumber (biblionumber)
124 marc_blob_subfield => "(
125 blobidlink bigint(20) NOT NULL auto_increment,
126 subfieldvalue longtext NOT NULL,
127 PRIMARY KEY (blobidlink)
129 marc_subfield_structure => "(
130 tagfield char(3) NOT NULL default '',
131 tagsubfield char(1) NOT NULL default '',
132 liblibrarian char(255) NOT NULL default '',
133 libopac char(255) NOT NULL default '',
134 repeatable tinyint(4) NOT NULL default '0',
135 mandatory tinyint(4) NOT NULL default '0',
136 kohafield char(40) default NULL,
137 tab tinyint(1) default NULL,
138 authorised_value char(10) default NULL,
139 thesaurus_category char(10) default NULL,
140 value_builder char(80) default NULL,
141 PRIMARY KEY (tagfield,tagsubfield),
142 KEY kohafield (kohafield),
145 marc_subfield_table => "(
146 subfieldid bigint(20) unsigned NOT NULL auto_increment,
147 bibid bigint(20) unsigned NOT NULL default '0',
148 tag char(3) NOT NULL default '',
149 tagorder tinyint(4) NOT NULL default '1',
150 tag_indicator char(2) NOT NULL default '',
151 subfieldcode char(1) NOT NULL default '',
152 subfieldorder tinyint(4) NOT NULL default '1',
153 subfieldvalue varchar(255) default NULL,
154 valuebloblink bigint(20) default NULL,
155 PRIMARY KEY (subfieldid),
158 KEY tag_indicator (tag_indicator),
159 KEY subfieldorder (subfieldorder),
160 KEY subfieldcode (subfieldcode),
161 KEY subfieldvalue (subfieldvalue),
162 KEY tagorder (tagorder)
164 marc_tag_structure => "(
165 tagfield char(3) NOT NULL default '',
166 liblibrarian char(255) NOT NULL default '',
167 libopac char(255) NOT NULL default '',
168 repeatable tinyint(4) NOT NULL default '0',
169 mandatory tinyint(4) NOT NULL default '0',
170 authorised_value char(10) default NULL,
171 PRIMARY KEY (tagfield)
174 bibid bigint(20) NOT NULL default '0',
175 tag char(3) NOT NULL default '',
176 tagorder tinyint(4) NOT NULL default '1',
177 subfieldid char(1) NOT NULL default '',
178 subfieldorder tinyint(4) NOT NULL default '1',
179 word varchar(255) NOT NULL default '',
180 sndx_word varchar(255) NOT NULL default '',
183 KEY tagorder (tagorder),
184 KEY subfieldid (subfieldid),
185 KEY subfieldorder (subfieldorder),
187 KEY sndx_word (sndx_word)
189 marc_breeding =>"( file varchar(80) NOT NULL default '',
190 isbn varchar(10) NOT NULL default '',
194 authorised_values => "(id int(11) NOT NULL auto_increment,
195 category char(10) NOT NULL default '',
196 authorised_value char(80) NOT NULL default '',
204 biblio=>{ 'abstract' => 'text' },
205 deletedbiblio=>{ 'abstract' => 'text' },
206 biblioitems=>{ 'lccn' => 'char(25)',
207 'url' => 'varchar(255)',
209 deletedbiblioitems=>{ 'lccn' => 'char(25)',
210 'url' => 'varchar(255)',
212 branchtransfers=>{ 'datearrived' => 'datetime' },
213 statistics=>{'borrowernumber' =>'int(11)'},
214 aqbooksellers=>{'invoicedisc' =>'float(6,4)',
215 'nocalc' => 'int(11)'},
216 borrowers=>{'userid' => 'char(30)',
217 'password' => 'char(30)',
218 'flags' => 'int(11)'},
219 aqorders=>{'budgetdate' => 'date'},
220 #added so that reference items are not available for reserves...
221 itemtypes=>{'notforloan' => 'smallint(6)'},
222 systempreferences =>{'explanation' => 'char(80)'},
226 classification =>'classification',
227 multipart =>'multipart',
228 multivolume =>'multivolume',
229 newitems =>'newitems',
230 procedures =>'procedures',
231 publisher =>'publisher',
232 searchstats =>'searchstats',
233 serialissues =>'serialissues',
237 # The tabledata hash contains data that should be in the tables.
238 # The uniquefieldrequired hash entry is used to determine which (if any) fields
239 # must not exist in the table for this row to be inserted. If the
240 # uniquefieldrequired entry is already in the table, the existing data is not
245 { uniquefieldrequired => 'bit', bit => 0, flag => 'superlibrarian', flagdesc => 'Access to all librarian functions', defaulton => 0 },
246 { uniquefieldrequired => 'bit', bit => 1, flag => 'circulate', flagdesc => 'Circulate books', defaulton => 0 },
247 { uniquefieldrequired => 'bit', bit => 2, flag => 'catalogue', flagdesc => 'View Catalogue (Librarian Interface)', defaulton => 0 },
248 { uniquefieldrequired => 'bit', bit => 3, flag => 'parameters', flagdesc => 'Set Koha system paramters', defaulton => 0 },
249 { uniquefieldrequired => 'bit', bit => 4, flag => 'borrowers', flagdesc => 'Add or modify borrowers', defaulton => 0 },
250 { uniquefieldrequired => 'bit', bit => 5, flag => 'permissions', flagdesc => 'Set user permissions', defaulton => 0 },
251 { uniquefieldrequired => 'bit', bit => 6, flag => 'reserveforothers', flagdesc => 'Reserve books for patrons', defaulton => 0 },
252 { uniquefieldrequired => 'bit', bit => 7, flag => 'borrow', flagdesc => 'Borrow books', defaulton => 1 },
253 { uniquefieldrequired => 'bit', bit => 8, flag => 'reserveforself', flagdesc => 'Reserve books for self', defaulton => 0 },
254 { uniquefieldrequired => 'bit', bit => 9, flag => 'editcatalogue', flagdesc => 'Edit Catalogue (Modify bibliographic/holdings data)', defaulton => 0 },
255 { uniquefieldrequired => 'bit', bit => 10, flag => 'updatecharges', flagdesc => 'Update borrower charges', defaulton => 0 },
257 systempreferences => [
258 { uniquefieldrequired => 'variable', variable => 'autoMemberNum', value => '1', explanation => '1 or else. If 1, Barcode is auto-calculated' },
259 { uniquefieldrequired => 'variable', variable => 'acquisitions', value => 'simple', explanation => 'normal or simple : will use acquisition system found in directory acqui.simple or acquisition' },
260 { uniquefieldrequired => 'variable', variable => 'dateformat', value => 'metric', explanation => 'metric or us' },
261 { uniquefieldrequired => 'variable', variable => 'template', value => 'default', explanation => 'template default name' },
262 { uniquefieldrequired => 'variable', variable => 'autoBarcode', value => '1', explanation => '1 or else. If 1, Barcode is auto-calculated' },
263 { uniquefieldrequired => 'variable', variable => 'insecure', value => 'NO', explanation => 'if YES, no auth at all is needed. Be careful if you set this to yes !' },
264 { uniquefieldrequired => 'variable', variable => 'authoritysep', value => '--', explanation => 'the separator used in authority/thesaurus. Usually --' },
265 { uniquefieldrequired => 'variable', variable => 'opaclanguages', value => 'en', explanation => 'languages' },
266 { uniquefieldrequired => 'variable', variable => 'opacthemes', value => 'default', explanation => 'theme' },
267 { uniquefieldrequired => 'variable', variable => 'timeout', value => '12000000', explanation => 'login timeout' },
273 my %fielddefinitions=(
275 { field => 'printername', type => 'char(40)', null => '', key => 'PRI', default => '' },
278 { field => 'bookfundid', type => 'char(5)', null => '', key => 'PRI', default => '' },
281 { field => 'id', type => 'int', null => '', key => 'PRI', default => '', extra => 'auto_increment' },
292 # Get version of MySQL database engine.
293 my $mysqlversion=`mysqld --version`;
294 $mysqlversion=~/Ver (\S*) /;
296 if ($mysqlversion ge '3.23') {
297 print "Could convert to MyISAM database tables...\n";
300 #---------------------------------
303 # Collect all tables into a list
304 $sth=$dbh->prepare("show tables");
306 while (my ($table) = $sth->fetchrow) {
307 $existingtables{$table}=1;
310 # Now add any missing tables
311 foreach $table ( keys %requiretables ) {
312 print "Checking $table table...\n" if $debug;
313 unless ($existingtables{$table} ) {
314 print "Adding $table table...\n";
315 my $sth=$dbh->prepare(
316 "create table $table $requiretables{$table}" );
319 print "Error : $sth->errstr \n";
325 # now drop useless tables
326 foreach $table ( keys %dropable_table) {
327 print "Dropping unused tables...\n" if $debug;
328 if ($existingtables{$table} ) {
329 $dbh->do("drop table $table");
331 print "Error : $dbh->errstr \n";
335 unless ($existingtables{'z3950servers'}) {
336 print "Adding z3950servers table...\n";
337 my $sti=$dbh->prepare("create table z3950servers (
348 $sti=$dbh->prepare("insert into z3950servers
349 values ('z3950.loc.gov',
353 'Library of Congress',
358 #---------------------------------
361 foreach $table ( keys %requirefields ) {
362 print "Check table $table\n" if $debug;
363 $sth=$dbh->prepare("show columns from $table");
366 while ( ($column, $type, $null, $key, $default, $extra)
368 $types{$column}=$type;
370 foreach $column ( keys %{ $requirefields{$table} } ) {
371 print " Check column $column\n" if $debug;
372 if ( ! $types{$column} ) {
373 # column doesn't exist
374 print "Adding $column field to $table table...\n";
375 $query="alter table $table
376 add column $column " . $requirefields{$table}->{$column} ;
377 print "Execute: $query\n" if $debug;
378 my $sti=$dbh->prepare($query);
381 print "**Error : $sti->errstr \n";
388 foreach $table ( keys %fielddefinitions ) {
389 print "Check table $table\n" if $debug;
390 $sth=$dbh->prepare("show columns from $table");
393 while ( ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
394 $definitions->{$column}->{type}=$type;
395 $definitions->{$column}->{null}=$null;
396 $definitions->{$column}->{key}=$key;
397 $definitions->{$column}->{default}=$default;
398 $definitions->{$column}->{extra}=$extra;
400 my $fieldrow=$fielddefinitions{$table};
401 foreach my $row ( @$fieldrow ) {
402 my $field = $row->{field};
403 my $type = $row->{type};
404 my $null = $row->{null};
405 my $key = $row->{key};
406 my $default = $row->{default};
407 my $extra = $row->{extra};
408 my $def=$definitions->{$field};
409 unless ($type eq $def->{type} && $null eq $def->{null} && $key eq $def->{key} && $default eq $def->{default} && $extra eq $def->{extra}) {
416 unless ($extra eq 'auto_increment') {
419 my $sth=$dbh->prepare("alter table $table change $field $field $type $null $key $extra default ?");
420 $sth->execute($default);
421 print " Alter $field in $table\n";
426 # Get list of columns from items table
429 $sth=$dbh->prepare("show columns from items");
431 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
432 $itemtypes{$column}=$type;
435 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
436 $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
439 print "Setting maximum barcode length to 20 (was $oldlength).\n";
440 my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
445 # extending the timestamp in branchtransfers...
448 $sth=$dbh->prepare("show columns from branchtransfers");
450 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
451 $branchtransfers{$column}=$type;
454 unless ($branchtransfers{'datesent'} eq 'datetime') {
455 print "Setting type of datesent in branchtransfers to datetime.\n";
456 my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
460 unless ($branchtransfers{'datearrived'} eq 'datetime') {
461 print "Setting type of datearrived in branchtransfers to datetime.\n";
462 my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
466 # changing the branchcategories table around...
467 my %branchcategories;
469 $sth=$dbh->prepare("show columns from branchcategories");
471 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
472 $branchcategories{$column}=$type;
475 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
476 print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
477 my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
479 $sti=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
483 unless ($branchcategories{'categoryname'} eq 'text') {
484 print "Changing branchcode in branchcategories to categoryname text.\n";
485 my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
489 unless ($branchcategories{'codedescription'} eq 'text') {
490 print "Replacing branchholding in branchcategories with codedescription text.\n";
491 my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
496 # Populate tables with required data
498 foreach my $table (keys %tabledata) {
499 print "Checking for data required in table $table...\n";
500 my $tablerows=$tabledata{$table};
501 foreach my $row (@$tablerows) {
502 my $uniquefieldrequired=$row->{uniquefieldrequired};
503 my $uniquevalue=$row->{$uniquefieldrequired};
504 my $sth=$dbh->prepare("select $uniquefieldrequired from $table where $uniquefieldrequired=?");
505 $sth->execute($uniquevalue);
506 unless ($sth->rows) {
507 print "Adding row to $table: ";
511 foreach my $field (keys %$row) {
512 (next) if ($field eq 'uniquefieldrequired');
513 my $value=$row->{$field};
514 push @values, $value;
515 print " $field => $value";
516 $fieldlist.="$field,";
521 $placeholders=~s/,$//;
522 my $sth=$dbh->prepare("insert into $table ($fieldlist) values ($placeholders)");
523 $sth->execute(@values);
533 # Revision 1.30 2003/01/06 13:11:45 tipaul
536 # Revision 1.29 2003/01/06 11:14:11 tipaul
537 # last bugfixes before 1.3.3 : systempref table correctly filled
539 # Revision 1.28 2002/12/10 13:27:47 tipaul
540 # bugfixes (davide mails in koha-dev)
542 # Revision 1.27 2002/11/26 15:04:54 tipaul
543 # road to 1.3.2. Updating db structure during installation
545 # Revision 1.26 2002/11/12 17:42:40 tonnesen
546 # Merged some features over from rel-1-2, including primary key checking.
548 # Revision 1.25 2002/11/12 16:44:38 tipaul
551 # * adding value_builder : you can map a subfield in the marc_subfield_structure to a sub stored in "value_builder" directory. In this directory you can create screen used to build values with any method. In this commit is a 1st draft of the builder for 100$a unimarc french subfield, which is composed of 35 digits, with 12 differents values (only the 4th first are provided for instance)
553 # Revision 1.24 2002/10/30 14:00:23 arensb
554 # (bug fix): Fixed typo.
556 # Revision 1.23 2002/10/25 10:55:46 tipaul
558 # * bugfixes and improvements
559 # * manage mandatory MARC subfields
560 # * new table : authorised_values. this table contains categories and authorised values for the category. On MARC management, you can map a subfield to a authorised_values category. If you do this, the subfield can only be filled with a authorised_value of the selected category.
561 # this submit contains everything needed :
564 # * "links" management
565 # * creation of a html-list if a subfield is mapped to an authorised value.
567 # Note this is different from authorities support, which will come soon.
568 # The authorised_values is supposed to contains a "small" number of authorised values for a category (less than 50-100). If you enter more authorised values than this, it should be hard to find what you want in a BIG list...
570 # Revision 1.22 2002/10/15 10:08:19 tipaul
571 # fixme corrected, re-indent and adding the marc_breeding table (see commit of marcimport.pl for more explanations about breeding)
573 # Revision 1.21 2002/10/14 11:48:59 tipaul
576 # Revision 1.20 2002/10/10 04:49:41 arensb
577 # Added some FIXME comments.
579 # Revision 1.19 2002/10/05 10:17:17 arensb
580 # Merged with arensb-context branch: use C4::Context->dbh instead of
581 # &C4Connect, and generally prefer C4::Context over C4::Database.
583 # Revision 1.18.2.2 2002/10/05 06:18:43 arensb
584 # Added a whole mess of FIXME comments.
586 # Revision 1.18.2.1 2002/10/04 02:46:00 arensb
587 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
590 # Revision 1.18 2002/09/24 13:50:55 tipaul
591 # long WAS the road to 1.3.0...
592 # coming VERY SOON NOW...
593 # modifying installer and buildrelease to update the DB
595 # Revision 1.17 2002/09/24 12:57:35 tipaul
596 # long WAS the road to 1.3.0...
597 # coming VERY SOON NOW...
598 # modifying installer and buildrelease to update the DB
600 # Revision 1.16 2002/07/31 02:34:27 finlayt
602 # added "notforloan" field to the itemtypes table.
604 # Revision 1.15 2002/07/20 22:30:06 rangi
605 # Making sure fix makes it into the main branch as well
608 # Revision 1.14 2002/07/08 16:20:26 tonnesen
609 # Added sessionqueries table and password/userid fields to borrowers table
611 # Revision 1.13 2002/07/04 18:05:36 tonnesen
614 # Revision 1.12 2002/07/04 16:41:06 tonnesen
615 # Merged changes from rel-1-2. Abstracted table structure changes by alan.