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 '',
108 father bigint(20) NOT NULL default '',
110 KEY freelib (freelib),
112 KEY category (category),
113 KEY hierarchy (hierarchy)
116 bibid bigint(20) unsigned NOT NULL auto_increment,
117 biblionumber int(11) NOT NULL default '0',
118 datecreated date NOT NULL default '0000-00-00',
119 datemodified date default NULL,
120 origincode char(20) default NULL,
122 KEY origincode (origincode),
123 KEY biblionumber (biblionumber)
125 marc_blob_subfield => "(
126 blobidlink bigint(20) NOT NULL auto_increment,
127 subfieldvalue longtext NOT NULL,
128 PRIMARY KEY (blobidlink)
130 marc_subfield_structure => "(
131 tagfield char(3) NOT NULL default '',
132 tagsubfield char(1) NOT NULL default '',
133 liblibrarian char(255) NOT NULL default '',
134 libopac char(255) NOT NULL default '',
135 repeatable tinyint(4) NOT NULL default '0',
136 mandatory tinyint(4) NOT NULL default '0',
137 kohafield char(40) default NULL,
138 tab tinyint(1) default NULL,
139 authorised_value char(10) default NULL,
140 thesaurus_category char(10) default NULL,
141 value_builder char(80) default NULL,
142 PRIMARY KEY (tagfield,tagsubfield),
143 KEY kohafield (kohafield),
146 marc_subfield_table => "(
147 subfieldid bigint(20) unsigned NOT NULL auto_increment,
148 bibid bigint(20) unsigned NOT NULL default '0',
149 tag char(3) NOT NULL default '',
150 tagorder tinyint(4) NOT NULL default '1',
151 tag_indicator char(2) NOT NULL default '',
152 subfieldcode char(1) NOT NULL default '',
153 subfieldorder tinyint(4) NOT NULL default '1',
154 subfieldvalue varchar(255) default NULL,
155 valuebloblink bigint(20) default NULL,
156 PRIMARY KEY (subfieldid),
159 KEY tag_indicator (tag_indicator),
160 KEY subfieldorder (subfieldorder),
161 KEY subfieldcode (subfieldcode),
162 KEY subfieldvalue (subfieldvalue),
163 KEY tagorder (tagorder)
165 marc_tag_structure => "(
166 tagfield char(3) NOT NULL default '',
167 liblibrarian char(255) NOT NULL default '',
168 libopac char(255) NOT NULL default '',
169 repeatable tinyint(4) NOT NULL default '0',
170 mandatory tinyint(4) NOT NULL default '0',
171 authorised_value char(10) default NULL,
172 PRIMARY KEY (tagfield)
175 bibid bigint(20) NOT NULL default '0',
176 tag char(3) NOT NULL default '',
177 tagorder tinyint(4) NOT NULL default '1',
178 subfieldid char(1) NOT NULL default '',
179 subfieldorder tinyint(4) NOT NULL default '1',
180 word varchar(255) NOT NULL default '',
181 sndx_word varchar(255) NOT NULL default '',
184 KEY tagorder (tagorder),
185 KEY subfieldid (subfieldid),
186 KEY subfieldorder (subfieldorder),
188 KEY sndx_word (sndx_word)
190 marc_breeding =>"( id bigint(20) NOT NULL auto_increment,
191 file varchar(80) NOT NULL default '',
192 isbn varchar(10) NOT NULL default '',
193 title varchar(128) default NULL,
194 author varchar(80) default NULL,
200 authorised_values => "(id int(11) NOT NULL auto_increment,
201 category char(10) NOT NULL default '',
202 authorised_value char(80) NOT NULL default '',
207 userflags => "( bit int(11) NOT NULL default '0',
208 flag char(30), flagdesc char(255),
216 biblio=>{ 'abstract' => 'text' },
217 deletedbiblio=>{ 'abstract' => 'text' },
218 biblioitems=>{ 'lccn' => 'char(25)',
219 'url' => 'varchar(255)',
221 deletedbiblioitems=>{ 'lccn' => 'char(25)',
222 'url' => 'varchar(255)',
224 branchtransfers=>{ 'datearrived' => 'datetime' },
225 statistics=>{'borrowernumber' =>'int(11)'},
226 aqbooksellers=>{'invoicedisc' =>'float(6,4)',
227 'nocalc' => 'int(11)'},
228 borrowers=>{'userid' => 'char(30)',
229 'password' => 'char(30)',
230 'flags' => 'int(11)'},
231 aqorders=>{'budgetdate' => 'date'},
232 #added so that reference items are not available for reserves...
233 itemtypes=>{'notforloan' => 'smallint(6)'},
234 systempreferences =>{'explanation' => 'char(80)'},
235 z3950servers =>{'syntax' => 'char(80)'},
239 classification =>'classification',
240 multipart =>'multipart',
241 multivolume =>'multivolume',
242 newitems =>'newitems',
243 procedures =>'procedures',
244 publisher =>'publisher',
245 searchstats =>'searchstats',
246 serialissues =>'serialissues',
250 # The tabledata hash contains data that should be in the tables.
251 # The uniquefieldrequired hash entry is used to determine which (if any) fields
252 # must not exist in the table for this row to be inserted. If the
253 # uniquefieldrequired entry is already in the table, the existing data is not
258 { uniquefieldrequired => 'bit', bit => 0, flag => 'superlibrarian', flagdesc => 'Access to all librarian functions', defaulton => 0 },
259 { uniquefieldrequired => 'bit', bit => 1, flag => 'circulate', flagdesc => 'Circulate books', defaulton => 0 },
260 { uniquefieldrequired => 'bit', bit => 2, flag => 'catalogue', flagdesc => 'View Catalogue (Librarian Interface)', defaulton => 0 },
261 { uniquefieldrequired => 'bit', bit => 3, flag => 'parameters', flagdesc => 'Set Koha system paramters', defaulton => 0 },
262 { uniquefieldrequired => 'bit', bit => 4, flag => 'borrowers', flagdesc => 'Add or modify borrowers', defaulton => 0 },
263 { uniquefieldrequired => 'bit', bit => 5, flag => 'permissions', flagdesc => 'Set user permissions', defaulton => 0 },
264 { uniquefieldrequired => 'bit', bit => 6, flag => 'reserveforothers', flagdesc => 'Reserve books for patrons', defaulton => 0 },
265 { uniquefieldrequired => 'bit', bit => 7, flag => 'borrow', flagdesc => 'Borrow books', defaulton => 1 },
266 { uniquefieldrequired => 'bit', bit => 8, flag => 'reserveforself', flagdesc => 'Reserve books for self', defaulton => 0 },
267 { uniquefieldrequired => 'bit', bit => 9, flag => 'editcatalogue', flagdesc => 'Edit Catalogue (Modify bibliographic/holdings data)', defaulton => 0 },
268 { uniquefieldrequired => 'bit', bit => 10, flag => 'updatecharges', flagdesc => 'Update borrower charges', defaulton => 0 },
270 systempreferences => [
271 { uniquefieldrequired => 'variable', variable => 'autoMemberNum', value => '1', explanation => '1 or else. If 1, Barcode is auto-calculated' },
272 { uniquefieldrequired => 'variable', variable => 'acquisitions', value => 'simple', explanation => 'normal or simple : whether to use "acqui" or "acqui.simple" acquisition system' },
273 { uniquefieldrequired => 'variable', variable => 'dateformat', value => 'metric', explanation => 'metric, us, or iso' },
274 { uniquefieldrequired => 'variable', variable => 'template', value => 'default', explanation => 'template default name' },
275 { uniquefieldrequired => 'variable', variable => 'autoBarcode', value => '1', explanation => '1 or else. If 1, Barcode is auto-calculated' },
276 { uniquefieldrequired => 'variable', variable => 'insecure', value => 'NO', explanation => 'if YES, no auth at all is needed. Be careful if you set this to yes !' },
277 { uniquefieldrequired => 'variable', variable => 'authoritysep', value => '--', explanation => 'the separator used in authority/thesaurus. Usually --' },
278 { uniquefieldrequired => 'variable', variable => 'opaclanguages', value => 'en', explanation => 'languages' },
279 { uniquefieldrequired => 'variable', variable => 'opacthemes', value => 'default', explanation => 'theme' },
280 { uniquefieldrequired => 'variable', variable => 'timeout', value => '12000000', explanation => 'login timeout' },
281 { uniquefieldrequired => 'variable', variable => 'marc', value => 'ON', explanation => 'MARC support (ON or OFF)' },
282 { uniquefieldrequired => 'variable', variable => 'marcflavour', value => 'MARC21', explanation => 'your MARC flavor (MARC21 or UNIMARC) used for character encoding' },
283 { uniquefieldrequired => 'variable', variable => 'checkdigit', value => 'katipo', explanation => 'none= no check on member cardnumber. katipo= katipo check' },
284 { uniquefieldrequired => 'variable', variable => 'dateformat', value => 'ISO', explanation => 'date format (US mm/dd/yyyy, metric dd/mm/yyy, ISO yyyy/mm/dd) ' },
285 { uniquefieldrequired => 'variable', variable => 'KohaAdminEmailAddress', value => 'your.mail@here', explanation => 'the email adress where borrowers modifs are sent' },
291 my %fielddefinitions=(
293 { field => 'printername', type => 'char(40)', null => '', key => 'PRI', default => '' },
296 { field => 'bookfundid', type => 'char(5)', null => '', key => 'PRI', default => '' },
299 { field => 'id', type => 'int', null => '', key => 'PRI', default => '', extra => 'auto_increment' },
310 # Get version of MySQL database engine.
311 my $mysqlversion=`mysqld --version`;
312 $mysqlversion=~/Ver (\S*) /;
314 if ($mysqlversion ge '3.23') {
315 print "Could convert to MyISAM database tables...\n";
318 #---------------------------------
321 # Collect all tables into a list
322 $sth=$dbh->prepare("show tables");
324 while (my ($table) = $sth->fetchrow) {
325 $existingtables{$table}=1;
328 # Now add any missing tables
329 foreach $table ( keys %requiretables ) {
330 print "Checking $table table...\n" if $debug;
331 unless ($existingtables{$table} ) {
332 print "Adding $table table...\n";
333 my $sth=$dbh->prepare(
334 "create table $table $requiretables{$table}" );
337 print "Error : $sth->errstr \n";
343 # now drop useless tables
344 foreach $table ( keys %dropable_table) {
345 print "Dropping unused tables...\n" if $debug;
346 if ($existingtables{$table} ) {
347 $dbh->do("drop table $table");
349 print "Error : $dbh->errstr \n";
353 unless ($existingtables{'z3950servers'}) {
354 print "Adding z3950servers table...\n";
355 my $sti=$dbh->prepare("create table z3950servers (
366 $sti=$dbh->prepare("insert into z3950servers
367 values ('z3950.loc.gov',
371 'Library of Congress',
376 #---------------------------------
379 foreach $table ( keys %requirefields ) {
380 print "Check table $table\n" if $debug;
381 $sth=$dbh->prepare("show columns from $table");
384 while ( ($column, $type, $null, $key, $default, $extra)
386 $types{$column}=$type;
388 foreach $column ( keys %{ $requirefields{$table} } ) {
389 print " Check column $column\n" if $debug;
390 if ( ! $types{$column} ) {
391 # column doesn't exist
392 print "Adding $column field to $table table...\n";
393 $query="alter table $table
394 add column $column " . $requirefields{$table}->{$column} ;
395 print "Execute: $query\n" if $debug;
396 my $sti=$dbh->prepare($query);
399 print "**Error : $sti->errstr \n";
406 foreach $table ( keys %fielddefinitions ) {
407 print "Check table $table\n" if $debug;
408 $sth=$dbh->prepare("show columns from $table");
411 while ( ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
412 $definitions->{$column}->{type}=$type;
413 $definitions->{$column}->{null}=$null;
414 $definitions->{$column}->{key}=$key;
415 $definitions->{$column}->{default}=$default;
416 $definitions->{$column}->{extra}=$extra;
418 my $fieldrow=$fielddefinitions{$table};
419 foreach my $row ( @$fieldrow ) {
420 my $field = $row->{field};
421 my $type = $row->{type};
422 my $null = $row->{null};
423 my $key = $row->{key};
424 my $default = $row->{default};
425 my $extra = $row->{extra};
426 my $def=$definitions->{$field};
427 unless ($type eq $def->{type} && $null eq $def->{null} && $key eq $def->{key} && $default eq $def->{default} && $extra eq $def->{extra}) {
434 unless ($extra eq 'auto_increment') {
437 my $sth=$dbh->prepare("alter table $table change $field $field $type $null $key $extra default ?");
438 $sth->execute($default);
439 print " Alter $field in $table\n";
444 # Get list of columns from items table
447 $sth=$dbh->prepare("show columns from items");
449 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
450 $itemtypes{$column}=$type;
453 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
454 $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
457 print "Setting maximum barcode length to 20 (was $oldlength).\n";
458 my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
463 # extending the timestamp in branchtransfers...
466 $sth=$dbh->prepare("show columns from branchtransfers");
468 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
469 $branchtransfers{$column}=$type;
472 unless ($branchtransfers{'datesent'} eq 'datetime') {
473 print "Setting type of datesent in branchtransfers to datetime.\n";
474 my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
478 unless ($branchtransfers{'datearrived'} eq 'datetime') {
479 print "Setting type of datearrived in branchtransfers to datetime.\n";
480 my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
484 # changing the branchcategories table around...
485 my %branchcategories;
487 $sth=$dbh->prepare("show columns from branchcategories");
489 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
490 $branchcategories{$column}=$type;
493 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
494 print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
495 my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
497 $sti=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
501 unless ($branchcategories{'categoryname'} eq 'text') {
502 print "Changing branchcode in branchcategories to categoryname text.\n";
503 my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
507 unless ($branchcategories{'codedescription'} eq 'text') {
508 print "Replacing branchholding in branchcategories with codedescription text.\n";
509 my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
514 # Populate tables with required data
516 foreach my $table (keys %tabledata) {
517 print "Checking for data required in table $table...\n";
518 my $tablerows=$tabledata{$table};
519 foreach my $row (@$tablerows) {
520 my $uniquefieldrequired=$row->{uniquefieldrequired};
521 my $uniquevalue=$row->{$uniquefieldrequired};
522 my $sth=$dbh->prepare("select $uniquefieldrequired from $table where $uniquefieldrequired=?");
523 $sth->execute($uniquevalue);
524 unless ($sth->rows) {
525 print "Adding row to $table: ";
529 foreach my $field (keys %$row) {
530 (next) if ($field eq 'uniquefieldrequired');
531 my $value=$row->{$field};
532 push @values, $value;
533 print " $field => $value";
534 $fieldlist.="$field,";
539 $placeholders=~s/,$//;
540 my $sth=$dbh->prepare("insert into $table ($fieldlist) values ($placeholders)");
541 $sth->execute(@values);
551 # Revision 1.41 2003/04/29 08:09:44 tipaul
552 # z3950 support is coming...
553 # * adding a syntax column in z3950 table = this column will say wether the z3950 must be called with PerferedRecordsyntax => USMARC or PerferedRecordsyntax => UNIMARC. I tried some french UNIMARC z3950 servers, and some only send USMARC, some only UNIMARC, some can answer with both.
554 # Note this is a 1st draft. More to follow (today ? I hope).
556 # Revision 1.40 2003/04/22 10:48:27 wolfpac444
557 # Added "father" column to bibliothesaurus table
559 # Revision 1.39 2003/04/04 08:45:00 tipaul
560 # last commits before 1.9.1
562 # Revision 1.38 2003/03/18 10:58:19 tipaul
563 # adding checkdigit parameter that choose how to check the members cardnumber.
565 # * none = no checking
566 # * katipo = checked as before
568 # Revision 1.37 2003/01/30 01:47:48 acli
569 # Corrected syntax error reported by Benedict
571 # Made the indentation somewhat easier to read; the messiness probably caused
572 # the original syntax error.
574 # Revision 1.36 2003/01/28 15:13:30 tipaul
575 # userflag table now created in upgrade script (bugfix #171)
577 # Revision 1.35 2003/01/27 03:12:49 acli
578 # Reworded the description for "acquisitions" to make it fit on the screen
580 # Added "iso" to dateformat, since dateformat is not yet being used anyway
582 # Revision 1.34 2003/01/23 12:30:02 tipaul
583 # introducint marcflavour in systempref file : used for character decoding
585 # Revision 1.33 2003/01/21 09:03:27 tipaul
586 # bugfix (NOTE : this bugs makes installation of the 1.3.3 a little fuzzy. Please fix your DB if you installed 1.3.3)
588 # Revision 1.32 2003/01/16 10:29:45 tipaul
589 # adding a MARC parameter in systempref ( which is ON or OFF)
590 # the search will be a marc search if MARC=ON
591 # and a standard (v1.2) search if MARC=OFF
593 # Revision 1.31 2003/01/06 13:32:43 tipaul
594 # *** empty log message ***
596 # Revision 1.29 2003/01/06 11:14:11 tipaul
597 # last bugfixes before 1.3.3 : systempref table correctly filled
599 # Revision 1.28 2002/12/10 13:27:47 tipaul
600 # bugfixes (davide mails in koha-dev)
602 # Revision 1.27 2002/11/26 15:04:54 tipaul
603 # road to 1.3.2. Updating db structure during installation
605 # Revision 1.26 2002/11/12 17:42:40 tonnesen
606 # Merged some features over from rel-1-2, including primary key checking.
608 # Revision 1.25 2002/11/12 16:44:38 tipaul
611 # * 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)
613 # Revision 1.24 2002/10/30 14:00:23 arensb
614 # (bug fix): Fixed typo.
616 # Revision 1.23 2002/10/25 10:55:46 tipaul
618 # * bugfixes and improvements
619 # * manage mandatory MARC subfields
620 # * 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.
621 # this submit contains everything needed :
624 # * "links" management
625 # * creation of a html-list if a subfield is mapped to an authorised value.
627 # Note this is different from authorities support, which will come soon.
628 # 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...
630 # Revision 1.22 2002/10/15 10:08:19 tipaul
631 # fixme corrected, re-indent and adding the marc_breeding table (see commit of marcimport.pl for more explanations about breeding)
633 # Revision 1.21 2002/10/14 11:48:59 tipaul
636 # Revision 1.20 2002/10/10 04:49:41 arensb
637 # Added some FIXME comments.
639 # Revision 1.19 2002/10/05 10:17:17 arensb
640 # Merged with arensb-context branch: use C4::Context->dbh instead of
641 # &C4Connect, and generally prefer C4::Context over C4::Database.
643 # Revision 1.18.2.2 2002/10/05 06:18:43 arensb
644 # Added a whole mess of FIXME comments.
646 # Revision 1.18.2.1 2002/10/04 02:46:00 arensb
647 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
650 # Revision 1.18 2002/09/24 13:50:55 tipaul
651 # long WAS the road to 1.3.0...
652 # coming VERY SOON NOW...
653 # modifying installer and buildrelease to update the DB
655 # Revision 1.17 2002/09/24 12:57:35 tipaul
656 # long WAS the road to 1.3.0...
657 # coming VERY SOON NOW...
658 # modifying installer and buildrelease to update the DB
660 # Revision 1.16 2002/07/31 02:34:27 finlayt
662 # added "notforloan" field to the itemtypes table.
664 # Revision 1.15 2002/07/20 22:30:06 rangi
665 # Making sure fix makes it into the main branch as well
668 # Revision 1.14 2002/07/08 16:20:26 tonnesen
669 # Added sessionqueries table and password/userid fields to borrowers table
671 # Revision 1.13 2002/07/04 18:05:36 tonnesen
674 # Revision 1.12 2002/07/04 16:41:06 tonnesen
675 # Merged changes from rel-1-2. Abstracted table structure changes by alan.