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.
24 # FIXME - The user might be installing a new database, so can't rely
25 # on /etc/koha.conf anyway.
32 %existingtables, # tables already in database
36 $type, $null, $key, $default, $extra,
37 $prefitem, # preference item in systempreferences table
40 my $dbh = C4::Context->dbh;
41 print "connected to your DB. Checking & modifying it\n";
46 # Tables to add if they don't exist
48 shelfcontents => "( shelfnumber int not null,
49 itemnumber int not null,
51 bookshelf => "( shelfnumber int auto_increment primary key,
52 shelfname char(255))",
53 z3950queue => "( id int auto_increment primary key,
62 identifier char(30))",
63 z3950results => "( id int auto_increment primary key,
73 branchrelations => "( branchcode varchar(4),
74 categorycode varchar(4))",
75 websites => "( websitenumber int(11) NOT NULL auto_increment,
76 biblionumber int(11) NOT NULL default '0',
80 PRIMARY KEY (websitenumber) )",
81 marcrecorddone => "( isbn char(40),
84 controlnumber char(40))",
85 uploadedmarc => "( id int(11) NOT NULL auto_increment PRIMARY KEY,
87 hidden smallint(6) default NULL,
88 name varchar(255) default NULL)",
89 ethnicity => "( code varchar(10) NOT NULL default '',
90 name varchar(255) default NULL,
91 PRIMARY KEY (code) )",
92 sessions => "( sessionID varchar(255) NOT NULL default '',
93 userid varchar(255) default NULL,
94 ip varchar(16) default NULL,
96 PRIMARY KEY (sessionID) )",
97 sessionqueries => "( sessionID varchar(255) NOT NULL default '',
98 userid char(100) NOT NULL default '',
99 ip char(18) NOT NULL default '',
100 url text NOT NULL default '' )",
101 bibliothesaurus => "( id bigint(20) NOT NULL auto_increment,
102 freelib char(255) NOT NULL default '',
103 stdlib char(255) NOT NULL default '',
104 category char(10) NOT NULL default '',
105 level tinyint(4) NOT NULL default '1',
106 hierarchy char(80) NOT NULL default '',
107 father 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 => "( id bigint(20) NOT NULL auto_increment,
190 file varchar(80) NOT NULL default '',
191 isbn varchar(10) NOT NULL default '',
192 title varchar(128) default NULL,
193 author varchar(80) default NULL,
195 encoding varchar(40) 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),
213 my %requirefields = (
214 biblio => { 'abstract' => 'text' },
215 deletedbiblio => { 'abstract' => 'text', 'marc' => 'blob' },
216 deleteditems => { 'marc' => 'blob', 'paidfor' => 'text' },
218 'lccn' => 'char(25)',
219 'url' => 'varchar(255)',
222 deletedbiblioitems => {
223 'lccn' => 'char(25)',
224 'url' => 'varchar(255)',
227 branchtransfers => { 'datearrived' => 'datetime' },
228 statistics => { 'borrowernumber' => 'int(11)' },
230 'invoicedisc' => 'float(6,4)',
231 'nocalc' => 'int(11)'
234 'userid' => 'char(30)',
235 'password' => 'char(30)',
236 'flags' => 'int(11)',
237 'textmessaging' => 'varchar(30)',
238 'zipcode' => 'varchar(25)',
239 'homezipcode' => 'varchar(25)',
241 aqorders => { 'budgetdate' => 'date' },
242 aqbudget => {'aqbudgetid' => 'tinyint(4) auto_increment primary key'},
243 items => {'paidfor' => 'text'},
245 #added so that reference items are not available for reserves...
246 itemtypes => { 'notforloan' => 'smallint(6)' },
247 systempreferences => { 'explanation' => 'char(80)',
248 'type' => 'char(20)',
249 'options' => 'text' },
250 z3950servers => { 'syntax' => 'char(80)' },
251 marc_subfield_structure =>{'seealso' => 'char(80)'},
252 bookshelf => {'owner' => 'char(80)',
253 'category' => 'char(1)',
257 my %dropable_table = (
258 classification => 'classification',
259 multipart => 'multipart',
260 multivolume => 'multivolume',
261 newitems => 'newitems',
262 procedures => 'procedures',
263 publisher => 'publisher',
264 searchstats => 'searchstats',
265 serialissues => 'serialissues',
268 # the other hash contains other actions that can't be done elsewhere. they are done
269 # either BEFORE of AFTER everything else, depending on "when" entry (default => AFTER)
271 # The tabledata hash contains data that should be in the tables.
272 # The uniquefieldrequired hash entry is used to determine which (if any) fields
273 # must not exist in the table for this row to be inserted. If the
274 # uniquefieldrequired entry is already in the table, the existing data is not
275 # modified, unless the forceupdate hash entry is also set. Fields in the
276 # anonymous "forceupdate" hash will be forced to be updated to the default
277 # values given in the %tabledata hash.
282 uniquefieldrequired => 'bit',
284 flag => 'superlibrarian',
285 flagdesc => 'Access to all librarian functions',
289 uniquefieldrequired => 'bit',
292 flagdesc => 'Circulate books',
296 uniquefieldrequired => 'bit',
299 flagdesc => 'View Catalogue (Librarian Interface)',
303 uniquefieldrequired => 'bit',
305 flag => 'parameters',
306 flagdesc => 'Set Koha system paramters',
310 uniquefieldrequired => 'bit',
313 flagdesc => 'Add or modify borrowers',
317 uniquefieldrequired => 'bit',
319 flag => 'permissions',
320 flagdesc => 'Set user permissions',
324 uniquefieldrequired => 'bit',
326 flag => 'reserveforothers',
327 flagdesc => 'Reserve books for patrons',
331 uniquefieldrequired => 'bit',
334 flagdesc => 'Borrow books',
338 uniquefieldrequired => 'bit',
340 flag => 'reserveforself',
341 flagdesc => 'Reserve books for self',
345 uniquefieldrequired => 'bit',
347 flag => 'editcatalogue',
348 flagdesc => 'Edit Catalogue (Modify bibliographic/holdings data)',
352 uniquefieldrequired => 'bit',
354 flag => 'updatecharges',
355 flagdesc => 'Update borrower charges',
359 systempreferences => [
361 uniquefieldrequired => 'variable',
362 forceupdate => { 'explanation' => 1,
364 variable => 'LibraryName',
365 value => '<i><b>Koha<br/>Free Software ILS<br/><br/></b>Koha : a gift, a contribution<br/> in Maori</i>',
366 explanation => 'Library name as shown on main opac page',
371 uniquefieldrequired => 'variable',
372 forceupdate => { 'explanation' => 1,
374 variable => 'autoMemberNum',
376 explanation => 'Member number is auto-calculated',
381 uniquefieldrequired => 'variable',
382 forceupdate => { 'explanation' => 1,
385 variable => 'acquisitions',
388 'Normal, budget-based acquisitions, or Simple bibliographic-data acquisitions',
390 options => 'simple|normal'
393 uniquefieldrequired => 'variable',
394 forceupdate => { 'explanation' => 1,
397 variable => 'dateformat',
400 'date format (us mm/dd/yyyy, metric dd/mm/yyy, ISO yyyy/mm/dd)',
402 options => 'metric|us|iso'
405 uniquefieldrequired => 'variable',
406 variable => 'template',
407 forceupdate => { 'explanation' => 1,
410 explanation => 'Preference order for intranet interface templates',
414 uniquefieldrequired => 'variable',
415 variable => 'autoBarcode',
416 forceupdate => { 'explanation' => 1,
419 explanation => 'Barcode is auto-calculated',
423 uniquefieldrequired => 'variable',
424 variable => 'insecure',
425 forceupdate => { 'explanation' => 1,
429 'If YES, no auth at all is needed. Be careful if you set this to yes!',
433 uniquefieldrequired => 'variable',
434 variable => 'authoritysep',
435 forceupdate => { 'explanation' => 1,
440 'the separator used in authority/thesaurus. Usually --',
445 uniquefieldrequired => 'variable',
446 variable => 'opaclanguages',
447 forceupdate => { 'explanation' => 1,
450 explanation => 'Set the preferred order for translations. The top language will be tried first.',
454 uniquefieldrequired => 'variable',
455 variable => 'opacthemes',
456 forceupdate => { 'explanation' => 1,
459 explanation => 'Set the preferred order for themes. The top theme will be tried first.',
463 uniquefieldrequired => 'variable',
464 variable => 'timeout',
465 forceupdate => { 'explanation' => 1,
468 explanation => 'Inactivity timeout for cookies authentication (in seconds)',
472 uniquefieldrequired => 'variable',
474 forceupdate => { 'explanation' => 1,
477 explanation => 'Turn on MARC support',
481 uniquefieldrequired => 'variable',
482 variable => 'marcflavour',
483 forceupdate => { 'explanation' => 1,
488 'your MARC flavor (MARC21 or UNIMARC) used for character encoding',
490 options => 'MARC21|UNIMARC'
493 uniquefieldrequired => 'variable',
494 variable => 'checkdigit',
496 forceupdate => { 'explanation' => 1,
499 explanation => 'Validity checks on membership number: none or "Katipo" style checks',
501 options => 'none|katipo'
504 uniquefieldrequired => 'variable',
505 variable => 'maxoutstanding',
506 forceupdate => { 'explanation' => 1,
510 'maximum amount withstanding to be able make reserves ',
514 uniquefieldrequired => 'variable',
515 variable => 'maxreserves',
516 forceupdate => { 'explanation' => 1,
520 'maximum number of reserves a member can make',
525 uniquefieldrequired => 'variable',
526 variable => 'noissuescharge',
527 forceupdate => { 'explanation' => 1,
531 'maximum amount withstanding to be able to check out an item',
536 uniquefieldrequired => 'variable',
537 variable => 'KohaAdminEmailAddress',
538 forceupdate => { 'explanation' => 1,
540 value => 'your.mail@here',
541 explanation => 'the email address where borrowers modifs are sent',
545 uniquefieldrequired => 'variable',
547 forceupdate => { 'explanation' => 1,
550 explanation => 'the gist rate. NOT in %, but in numeric form (0.12 for 12%)',
554 uniquefieldrequired => 'variable',
555 variable => 'ldapserver',
556 forceupdate => { 'explanation' => 1,
559 explanation => 'your ldap server',
563 uniquefieldrequired => 'variable',
564 variable => 'ldapinfos',
565 forceupdate => { 'explanation' => 1,
568 explanation => 'ldap info. The ldap will be used in dn : uid=xxx, <ldapinfos>',
575 my %fielddefinitions = (
578 field => 'printername',
587 field => 'bookfundid',
596 field => 'aqbudgetid',
597 type => 'tinyint(4)',
601 extra => 'auto_increment'
611 extra => 'auto_increment'
616 field => 'z3950random',
617 type => 'varchar(40)',
625 type => 'varchar(40)',
639 # Get version of MySQL database engine.
640 my $mysqlversion = `mysqld --version`;
641 $mysqlversion =~ /Ver (\S*) /;
643 if ( $mysqlversion ge '3.23' ) {
644 print "Could convert to MyISAM database tables...\n";
647 #---------------------------------
650 # Collect all tables into a list
651 $sth = $dbh->prepare("show tables");
653 while ( my ($table) = $sth->fetchrow ) {
654 $existingtables{$table} = 1;
658 # Now add any missing tables
659 foreach $table ( keys %requiretables ) {
660 unless ( $existingtables{$table} ) {
661 print "Adding $table table...\n";
662 my $sth = $dbh->prepare("create table $table $requiretables{$table}");
665 print "Error : $sth->errstr \n";
671 # now drop useless tables
672 foreach $table ( keys %dropable_table ) {
673 if ( $existingtables{$table} ) {
674 print "Dropping unused table $table\n" if $debug;
675 $dbh->do("drop table $table");
677 print "Error : $dbh->errstr \n";
681 unless ( $existingtables{'z3950servers'} ) {
682 #MJR: added syntax entries to close bug 624
683 print "Adding z3950servers table...\n";
684 my $sti = $dbh->prepare( "create table z3950servers (
697 $sti = $dbh->prepare( "insert into z3950servers
698 values ('z3950.loc.gov',
702 'Library of Congress',
708 #---------------------------------
711 foreach $table ( keys %requirefields ) {
712 print "Check table $table\n" if $debug;
713 $sth = $dbh->prepare("show columns from $table");
716 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
718 $types{$column} = $type;
720 foreach $column ( keys %{ $requirefields{$table} } ) {
721 print " Check column $column [$types{$column}]\n" if $debug;
722 if ( !$types{$column} ) {
724 # column doesn't exist
725 print "Adding $column field to $table table...\n";
726 $query = "alter table $table
727 add column $column " . $requirefields{$table}->{$column};
728 print "Execute: $query\n" if $debug;
729 my $sti = $dbh->prepare($query);
732 print "**Error : $sti->errstr \n";
739 foreach $table ( keys %fielddefinitions ) {
740 print "Check table $table\n" if $debug;
741 $sth = $dbh->prepare("show columns from $table");
744 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
746 $definitions->{$column}->{type} = $type;
747 $definitions->{$column}->{null} = $null;
748 $definitions->{$column}->{key} = $key;
749 $definitions->{$column}->{default} = $default;
750 $definitions->{$column}->{extra} = $extra;
752 my $fieldrow = $fielddefinitions{$table};
753 foreach my $row (@$fieldrow) {
754 my $field = $row->{field};
755 my $type = $row->{type};
756 my $null = $row->{null};
757 my $key = $row->{key};
758 my $default = $row->{default};
759 $default="''" unless $default;
760 my $extra = $row->{extra};
761 my $def = $definitions->{$field};
762 unless ( $type eq $def->{type}
763 && $null eq $def->{null}
764 && $key eq $def->{key}
765 && $default eq $def->{default}
766 && $extra eq $def->{extra} )
772 if ( $key eq 'PRI' ) {
773 $key = 'PRIMARY KEY';
775 unless ( $extra eq 'auto_increment' ) {
778 # if it's a new column use "add", if it's an old one, use "change".
780 if ($definitions->{$field}->{type}) {
781 $action="change $field"
785 # if it's a primary key, drop the previous pk, before altering the table
787 if ($key ne 'PRIMARY KEY') {
788 $sth =$dbh->prepare("alter table $table $action $field $type $null $key $extra default ?");
790 $sth =$dbh->prepare("alter table $table drop primary key, $action $field $type $null $key $extra default ?");
792 $sth->execute($default);
793 print " Alter $field in $table\n";
798 # Get list of columns from borrowers table
801 $sth = $dbh->prepare("show columns from borrowers");
803 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
805 $itemtypes{$column} = $type;
806 $nullenabled{$column} = $null;
809 unless ( $itemtypes{'cardnumber'} eq 'varchar(20)' ) {
810 $itemtypes{'cardnumber'} =~ /varchar\((\d+)\)/;
812 if ( $oldlength < 16 ) {
813 print "Setting maximum cardnumber length to 16 (was $oldlength) and marking unique.\n";
816 "alter table borrowers change cardnumber cardnumber varchar(16)");
821 "alter table borrowers drop index cardnumber");
826 "alter table borrowers add unique(cardnumber)");
832 # Get list of columns from items table
833 $sth = $dbh->prepare("show columns from items");
835 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
837 $itemtypes{$column} = $type;
838 $nullenabled{$column} = $null;
841 unless ( $itemtypes{'barcode'} eq 'varchar(20)' ) {
842 $itemtypes{'barcode'} =~ /varchar\((\d+)\)/;
844 if ( $oldlength < 20 ) {
845 print "Setting maximum barcode length to 20 (was $oldlength).\n";
848 "alter table items change barcode barcode varchar(20)");
853 # dropping unique barcode index & setting barcode to null allowed.
855 $sth = $dbh->prepare("show index from items");
857 while ( my ( $table, $non_unique, $key_name, $Seq_in_index, $Column_name, $Collation, $cardinality, $sub_part, $Packed, $comment ) = $sth->fetchrow )
859 if ($key_name eq 'barcode' && $non_unique eq 0) {
860 print "dropping BARCODE index to enable empty barcodes\n";
861 $dbh->do("ALTER TABLE `items` DROP INDEX `barcode`");
864 $dbh->do("ALTER TABLE `items` CHANGE `barcode` `barcode` VARCHAR( 20 )") unless ($nullenabled{barcode} eq 'YES');
867 # creating fulltext index in bibliothesaurus if needed
869 $sth = $dbh->prepare("show index from bibliothesaurus");
872 while ( my ( $table, $non_unique, $key_name, $Seq_in_index, $Column_name, $Collation, $cardinality, $sub_part, $Packed, $comment ) = $sth->fetchrow )
874 if ($key_name eq 'category_2') {
878 print "Creating fulltext index on bibliothesaurus\n" unless $exists;
879 $dbh->do('create fulltext index category_2 on bibliothesaurus (category,freelib)') unless $exists;
882 # creating index in z3950results if needed
884 $sth = $dbh->prepare("show index from z3950results");
887 while ( my ( $table, $non_unique, $key_name, $Seq_in_index, $Column_name, $Collation, $cardinality, $sub_part, $Packed, $comment ) = $sth->fetchrow )
889 if ($key_name eq 'query_server') {
893 print "Creating index on z3950results\n" unless $exists;
894 $dbh->do('create unique index query_server on z3950results (queryid,server)') unless $exists;
896 # changing z3950daemon field to NULL in marc_breeding
897 $dbh->do("ALTER TABLE `marc_breeding` CHANGE `z3950random` `z3950random` VARCHAR( 40 )");
899 # making borrowernumber an auto_increment field
900 $dbh->do("ALTER TABLE `borrowers` CHANGE `borrowernumber` `borrowernumber` INTEGER auto_increment");
902 # extending the timestamp in branchtransfers...
905 $sth = $dbh->prepare("show columns from branchtransfers");
907 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
909 $branchtransfers{$column} = $type;
912 unless ( $branchtransfers{'datesent'} eq 'datetime' ) {
913 print "Setting type of datesent in branchtransfers to datetime.\n";
916 "alter table branchtransfers change datesent datesent datetime");
920 unless ( $branchtransfers{'datearrived'} eq 'datetime' ) {
921 print "Setting type of datearrived in branchtransfers to datetime.\n";
924 "alter table branchtransfers change datearrived datearrived datetime");
928 # changing the branchcategories table around...
929 my %branchcategories;
931 $sth = $dbh->prepare("show columns from branchcategories");
933 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
935 $branchcategories{$column} = $type;
938 unless ( $branchcategories{'categorycode'} eq 'varchar(4)' ) {
940 "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
943 "alter table branchcategories change categorycode categorycode varchar(4) not null"
948 "alter table branchcategories add primary key (categorycode)");
952 unless ( $branchcategories{'categoryname'} eq 'text' ) {
953 print "Changing branchcode in branchcategories to categoryname text.\n";
956 "alter table branchcategories change branchcode categoryname text");
960 unless ( $branchcategories{'codedescription'} eq 'text' ) {
962 "Replacing branchholding in branchcategories with codedescription text.\n";
965 "alter table branchcategories change branchholding codedescription text"
970 # Populate tables with required data
972 foreach my $table ( keys %tabledata ) {
973 print "Checking for data required in table $table...\n";
974 my $tablerows = $tabledata{$table};
975 foreach my $row (@$tablerows) {
976 my $uniquefieldrequired = $row->{uniquefieldrequired};
977 my $uniquevalue = $row->{$uniquefieldrequired};
978 my $forceupdate = $row->{forceupdate};
981 "select $uniquefieldrequired from $table where $uniquefieldrequired=?"
983 $sth->execute($uniquevalue);
985 foreach my $field (keys %$forceupdate) {
986 if ($forceupdate->{$field}) {
987 my $sth=$dbh->prepare("update systempreferences set $field=? where $uniquefieldrequired=?");
988 $sth->execute($row->{$field}, $uniquevalue);
992 print "Adding row to $table: ";
996 foreach my $field ( keys %$row ) {
997 next if $field eq 'uniquefieldrequired';
998 next if $field eq 'forceupdate';
999 my $value = $row->{$field};
1000 push @values, $value;
1001 print " $field => $value";
1002 $fieldlist .= "$field,";
1003 $placeholders .= "?,";
1006 $fieldlist =~ s/,$//;
1007 $placeholders =~ s/,$//;
1010 "insert into $table ($fieldlist) values ($placeholders)");
1011 $sth->execute(@values);
1021 # Revision 1.74 2004/03/11 16:10:16 tipaul
1022 # *** empty log message ***
1024 # Revision 1.73 2004/03/06 20:26:13 tipaul
1025 # adding seealso feature in MARC searches