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 use MARC::File::XML ( BinaryEncoding => 'utf8' );
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
46 my $dbh = C4::Context->dbh;
47 print "connected to your DB. Checking & modifying it\n" unless $silent;
48 $|=1; # flushes output
53 # Tables to add if they don't exist
55 categorytable => "(categorycode char(5) NOT NULL default '',
56 description text default '',
57 itemtypecodes text default '',
58 PRIMARY KEY (categorycode)
60 subcategorytable => "(subcategorycode char(5) NOT NULL default '',
61 description text default '',
62 itemtypecodes text default '',
63 PRIMARY KEY (subcategorycode)
65 mediatypetable => "(mediatypecode char(5) NOT NULL default '',
66 description text default '',
67 itemtypecodes text default '',
68 PRIMARY KEY (mediatypecode)
71 `timestamp` TIMESTAMP NOT NULL ,
72 `user` INT( 11 ) NOT NULL ,
73 `module` TEXT default '',
74 `action` TEXT default '' ,
75 `object` INT(11) default '' ,
76 `info` TEXT default '' ,
77 PRIMARY KEY ( `timestamp` , `user` )
80 module varchar(20) NOT NULL default '',
81 code varchar(20) NOT NULL default '',
82 name varchar(100) NOT NULL default '',
83 title varchar(200) NOT NULL default '',
85 PRIMARY KEY (module,code)
88 alertid int(11) NOT NULL auto_increment,
89 borrowernumber int(11) NOT NULL default '0',
90 type varchar(10) NOT NULL default '',
91 externalid varchar(20) NOT NULL default '',
92 PRIMARY KEY (alertid),
93 KEY borrowernumber (borrowernumber),
94 KEY type (type,externalid)
97 `idnew` int(10) unsigned NOT NULL auto_increment,
98 `title` varchar(250) NOT NULL default '',
100 `lang` varchar(4) NOT NULL default '',
101 `timestamp` timestamp NOT NULL default CURRENT_TIMESTAMP,
102 PRIMARY KEY (`idnew`)
104 repeatable_holidays => "(
105 `id` int(11) NOT NULL auto_increment,
106 `branchcode` varchar(4) NOT NULL default '',
107 `weekday` smallint(6) default NULL,
108 `day` smallint(6) default NULL,
109 `month` smallint(6) default NULL,
110 `title` varchar(50) NOT NULL default '',
111 `description` text NOT NULL,
114 special_holidays => "(
115 `id` int(11) NOT NULL auto_increment,
116 `branchcode` varchar(4) NOT NULL default '',
117 `day` smallint(6) NOT NULL default '0',
118 `month` smallint(6) NOT NULL default '0',
119 `year` smallint(6) NOT NULL default '0',
120 `isexception` smallint(1) NOT NULL default '1',
121 `title` varchar(50) NOT NULL default '',
122 `description` text NOT NULL,
125 overduerules =>"(`branchcode` varchar(255) NOT NULL default '',
126 `categorycode` char(2) NOT NULL default '',
127 `delay1` int(4) default '0',
128 `letter1` varchar(20) default NULL,
129 `debarred1` char(1) default '0',
130 `delay2` int(4) default '0',
131 `debarred2` char(1) default '0',
132 `letter2` varchar(20) default NULL,
133 `delay3` int(4) default '0',
134 `letter3` varchar(20) default NULL,
135 `debarred3` int(1) default '0',
136 PRIMARY KEY (`branchcode`,`categorycode`)
140 my %requirefields = (
141 subscription => { 'letter' => 'char(20) NULL', 'distributedto' => 'text NULL'},
142 itemtypes => { 'imageurl' => 'char(200) NULL'},
143 aqbookfund => { 'branchcode' => 'varchar(4) NULL'},
144 aqbudget => { 'branchcode' => 'varchar(4) NULL'},
145 # tablename => { 'field' => 'fieldtype' },
148 my %dropable_table = (
149 sessionqueries => 'sessionqueries',
150 marcrecorddone => 'marcrecorddone',
152 itemsprices => 'itemsprices',
153 biblioanalysis => 'biblioanalysis',
155 # tablename => 'tablename',
158 my %uselessfields = (
159 # tablename => "field1,field2",
161 # the other hash contains other actions that can't be done elsewhere. they are done
162 # either BEFORE of AFTER everything else, depending on "when" entry (default => AFTER)
164 # The tabledata hash contains data that should be in the tables.
165 # The uniquefieldrequired hash entry is used to determine which (if any) fields
166 # must not exist in the table for this row to be inserted. If the
167 # uniquefieldrequired entry is already in the table, the existing data is not
168 # modified, unless the forceupdate hash entry is also set. Fields in the
169 # anonymous "forceupdate" hash will be forced to be updated to the default
170 # values given in the %tabledata hash.
174 # { uniquefielrequired => 'fieldname', # the primary key in the table
175 # fieldname => fieldvalue,
176 # fieldname2 => fieldvalue2,
179 systempreferences => [
181 uniquefieldrequired => 'variable',
182 variable => 'Activate_Log',
184 forceupdate => { 'explanation' => 1,
186 explanation => 'Turn Log Actions on DB On an Off',
190 uniquefieldrequired => 'variable',
191 variable => 'IndependantBranches',
193 forceupdate => { 'explanation' => 1,
195 explanation => 'Turn Branch independancy management On an Off',
199 uniquefieldrequired => 'variable',
200 variable => 'ReturnBeforeExpiry',
202 forceupdate => { 'explanation' => 1,
204 explanation => 'If Yes, Returndate on issuing can\'t be after borrower card expiry',
208 uniquefieldrequired => 'variable',
209 variable => 'opacstylesheet',
211 forceupdate => { 'explanation' => 1,
213 explanation => 'Enter a complete URL to use an alternate stylesheet in OPAC',
217 uniquefieldrequired => 'variable',
218 variable => 'opacsmallimage',
220 forceupdate => { 'explanation' => 1,
222 explanation => 'Enter a complete URL to an image, will be on top/left instead of the Koha logo',
226 uniquefieldrequired => 'variable',
227 variable => 'opaclargeimage',
229 forceupdate => { 'explanation' => 1,
231 explanation => 'Enter a complete URL to an image, will be on the main page, instead of the Koha logo',
235 uniquefieldrequired => 'variable',
236 variable => 'delimiter',
238 forceupdate => { 'explanation' => 1,
240 explanation => 'separator for reports exported to spreadsheet',
244 uniquefieldrequired => 'variable',
246 value => 'OPENOFFICE.ORG',
247 forceupdate => { 'explanation' => 1,
250 explanation => 'Define the default application for report exportations into files',
252 options => 'EXCEL|OPENOFFICE.ORG'
255 uniquefieldrequired => 'variable',
256 variable => 'Delimiter',
258 forceupdate => { 'explanation' => 1,
261 explanation => 'Define the default separator character for report exportations into files',
263 options => ';|tabulation|,|/|\|#'
266 uniquefieldrequired => 'variable',
267 variable => 'SubscriptionHistory',
269 forceupdate => { 'explanation' => 1,
272 explanation => 'Define the information level for serials history in OPAC',
274 options => 'simplified|full'
277 uniquefieldrequired => 'variable',
278 variable => 'hidelostitems',
280 forceupdate => { 'explanation' => 1,
282 explanation => 'show or hide "lost" items in OPAC.',
286 uniquefieldrequired => 'variable',
287 variable => 'IndependantBranches',
289 forceupdate => { 'explanation' => 1,
291 explanation => 'Turn Branch independancy management On an Off',
295 uniquefieldrequired => 'variable',
296 variable => 'ReturnBeforeExpiry',
298 forceupdate => { 'explanation' => 1,
300 explanation => 'If Yes, Returndate on issuing can\'t be after borrower card expiry',
304 uniquefieldrequired => 'variable',
305 variable => 'Disable_Dictionary',
307 forceupdate => { 'explanation' => 1,
309 explanation => 'Disables Dictionary buttons if set to yes',
313 uniquefieldrequired => 'variable',
314 variable => 'hide_marc',
316 forceupdate => { 'explanation' => 1,
318 explanation => 'hide marc specific datas like subfield code & indicators to library',
322 uniquefieldrequired => 'variable',
323 variable => 'NotifyBorrowerDeparture',
325 forceupdate => { 'explanation' => 1,
327 explanation => 'Delay before expiry where a notice is sent when issuing',
331 uniquefieldrequired => 'variable',
332 variable => 'OpacPasswordChange',
334 forceupdate => { 'explanation' => 1,
336 explanation => 'Enable/Disable password change in OPAC (disable it when using LDAP auth)',
340 uniquefieldrequired => 'variable',
341 variable => 'useDaysMode',
343 forceupdate => { 'explanation' => 1,
345 explanation => 'How to calculate return dates : Calendar means holidays will be controled, Days means the return date don\'t depend on holidays',
347 options => 'Calendar|Days'
353 my %fielddefinitions = (
355 # { field => 'fieldname',
356 # type => 'fieldtype',
374 field => 'booksellerid',
384 field => 'listprice',
385 type => 'varchar(10)',
392 field => 'invoiceprice',
393 type => 'varchar(10)',
402 field => 'borrowernumber',
404 null => 'NULL', # can be null when a borrower is deleted and the foreign key rule executed
410 field => 'itemnumber',
412 null => 'NULL', # can be null when a borrower is deleted and the foreign key rule executed
422 # { indexname => 'index detail'
426 { indexname => 'shelfnumber',
427 content => 'shelfnumber',
429 { indexname => 'itemnumber',
430 content => 'itemnumber',
434 { indexname => 'biblionumber',
435 content => 'biblionumber',
439 { indexname => 'homebranch',
440 content => 'homebranch',
442 { indexname => 'holdingbranch',
443 content => 'holdingbranch',
447 { indexname => 'PRIMARY',
453 { indexname => 'booksellerid',
454 content => 'booksellerid',
458 { indexname => 'basketno',
459 content => 'basketno',
462 aqorderbreakdown => [
463 { indexname => 'ordernumber',
464 content => 'ordernumber',
466 { indexname => 'bookfundid',
467 content => 'bookfundid',
471 { indexname => 'PRIMARY',
472 content => 'currency',
480 # { key => 'the key in table' (must be indexed)
481 # foreigntable => 'the foreigntable name', # (the parent)
482 # foreignkey => 'the foreign key column(s)' # (in the parent)
483 # onUpdate => 'CASCADE|SET NULL|NO ACTION| RESTRICT',
484 # onDelete => 'CASCADE|SET NULL|NO ACTION| RESTRICT',
488 { key => 'shelfnumber',
489 foreigntable => 'bookshelf',
490 foreignkey => 'shelfnumber',
491 onUpdate => 'CASCADE',
492 onDelete => 'CASCADE',
494 { key => 'itemnumber',
495 foreigntable => 'items',
496 foreignkey => 'itemnumber',
497 onUpdate => 'CASCADE',
498 onDelete => 'CASCADE',
501 # onDelete is RESTRICT on reference tables (branches, itemtype) as we don't want items to be
502 # easily deleted, but branches/itemtype not too easy to empty...
504 { key => 'biblionumber',
505 foreigntable => 'biblio',
506 foreignkey => 'biblionumber',
507 onUpdate => 'CASCADE',
508 onDelete => 'CASCADE',
511 foreigntable => 'itemtypes',
512 foreignkey => 'itemtype',
513 onUpdate => 'CASCADE',
514 onDelete => 'RESTRICT',
518 { key => 'biblioitemnumber',
519 foreigntable => 'biblioitems',
520 foreignkey => 'biblioitemnumber',
521 onUpdate => 'CASCADE',
522 onDelete => 'CASCADE',
524 { key => 'homebranch',
525 foreigntable => 'branches',
526 foreignkey => 'branchcode',
527 onUpdate => 'CASCADE',
528 onDelete => 'RESTRICT',
530 { key => 'holdingbranch',
531 foreigntable => 'branches',
532 foreignkey => 'branchcode',
533 onUpdate => 'CASCADE',
534 onDelete => 'RESTRICT',
537 additionalauthors => [
538 { key => 'biblionumber',
539 foreigntable => 'biblio',
540 foreignkey => 'biblionumber',
541 onUpdate => 'CASCADE',
542 onDelete => 'CASCADE',
546 { key => 'biblionumber',
547 foreigntable => 'biblio',
548 foreignkey => 'biblionumber',
549 onUpdate => 'CASCADE',
550 onDelete => 'CASCADE',
554 { key => 'booksellerid',
555 foreigntable => 'aqbooksellers',
557 onUpdate => 'CASCADE',
558 onDelete => 'RESTRICT',
563 foreigntable => 'aqbasket',
564 foreignkey => 'basketno',
565 onUpdate => 'CASCADE',
566 onDelete => 'CASCADE',
568 { key => 'biblionumber',
569 foreigntable => 'biblio',
570 foreignkey => 'biblionumber',
571 onUpdate => 'SET NULL',
572 onDelete => 'SET NULL',
576 { key => 'listprice',
577 foreigntable => 'currency',
578 foreignkey => 'currency',
579 onUpdate => 'CASCADE',
580 onDelete => 'CASCADE',
582 { key => 'invoiceprice',
583 foreigntable => 'currency',
584 foreignkey => 'currency',
585 onUpdate => 'CASCADE',
586 onDelete => 'CASCADE',
589 aqorderbreakdown => [
590 { key => 'ordernumber',
591 foreigntable => 'aqorders',
592 foreignkey => 'ordernumber',
593 onUpdate => 'CASCADE',
594 onDelete => 'CASCADE',
596 { key => 'bookfundid',
597 foreigntable => 'aqbookfund',
598 foreignkey => 'bookfundid',
599 onUpdate => 'CASCADE',
600 onDelete => 'CASCADE',
604 { key => 'frombranch',
605 foreigntable => 'branches',
606 foreignkey => 'branchcode',
607 onUpdate => 'CASCADE',
608 onDelete => 'CASCADE',
611 foreigntable => 'branches',
612 foreignkey => 'branchcode',
613 onUpdate => 'CASCADE',
614 onDelete => 'CASCADE',
616 { key => 'itemnumber',
617 foreigntable => 'items',
618 foreignkey => 'itemnumber',
619 onUpdate => 'CASCADE',
620 onDelete => 'CASCADE',
624 { key => 'categorycode',
625 foreigntable => 'categories',
626 foreignkey => 'categorycode',
627 onUpdate => 'CASCADE',
628 onDelete => 'CASCADE',
631 foreigntable => 'itemtypes',
632 foreignkey => 'itemtype',
633 onUpdate => 'CASCADE',
634 onDelete => 'CASCADE',
637 issues => [ # constraint is SET NULL : when a borrower or an item is deleted, we keep the issuing record
639 { key => 'borrowernumber',
640 foreigntable => 'borrowers',
641 foreignkey => 'borrowernumber',
642 onUpdate => 'SET NULL',
643 onDelete => 'SET NULL',
645 { key => 'itemnumber',
646 foreigntable => 'items',
647 foreignkey => 'itemnumber',
648 onUpdate => 'SET NULL',
649 onDelete => 'SET NULL',
653 { key => 'borrowernumber',
654 foreigntable => 'borrowers',
655 foreignkey => 'borrowernumber',
656 onUpdate => 'CASCADE',
657 onDelete => 'CASCADE',
659 { key => 'biblionumber',
660 foreigntable => 'biblio',
661 foreignkey => 'biblionumber',
662 onUpdate => 'CASCADE',
663 onDelete => 'CASCADE',
665 { key => 'itemnumber',
666 foreigntable => 'items',
667 foreignkey => 'itemnumber',
668 onUpdate => 'CASCADE',
669 onDelete => 'CASCADE',
671 { key => 'branchcode',
672 foreigntable => 'branches',
673 foreignkey => 'branchcode',
674 onUpdate => 'CASCADE',
675 onDelete => 'CASCADE',
678 borrowers => [ # foreign keys are RESTRICT as we don't want to delete borrowers when a branch is deleted
679 # but prevent deleting a branch as soon as it has 1 borrower !
680 { key => 'categorycode',
681 foreigntable => 'categories',
682 foreignkey => 'categorycode',
683 onUpdate => 'RESTRICT',
684 onDelete => 'RESTRICT',
686 { key => 'branchcode',
687 foreigntable => 'branches',
688 foreignkey => 'branchcode',
689 onUpdate => 'RESTRICT',
690 onDelete => 'RESTRICT',
694 { key => 'borrowernumber',
695 foreigntable => 'borrowers',
696 foreignkey => 'borrowernumber',
697 onUpdate => 'CASCADE',
698 onDelete => 'CASCADE',
700 { key => 'itemnumber',
701 foreigntable => 'items',
702 foreignkey => 'itemnumber',
703 onUpdate => 'SET NULL',
704 onDelete => 'SET NULL',
707 auth_tag_structure => [
708 { key => 'authtypecode',
709 foreigntable => 'auth_types',
710 foreignkey => 'authtypecode',
711 onUpdate => 'CASCADE',
712 onDelete => 'CASCADE',
715 # FIXME : don't constraint auth_*_table and auth_word, as they may be replaced by zebra
723 # Get version of MySQL database engine.
724 my $mysqlversion = `mysqld --version`;
725 $mysqlversion =~ /Ver (\S*) /;
727 if ( $mysqlversion ge '3.23' ) {
728 print "Could convert to MyISAM database tables...\n" unless $silent;
731 #---------------------------------
734 # Collect all tables into a list
735 $sth = $dbh->prepare("show tables");
737 while ( my ($table) = $sth->fetchrow ) {
738 $existingtables{$table} = 1;
742 # Now add any missing tables
743 foreach $table ( keys %requiretables ) {
744 unless ( $existingtables{$table} ) {
745 print "Adding $table table...\n" unless $silent;
746 my $sth = $dbh->prepare("create table $table $requiretables{$table}");
749 print "Error : $sth->errstr \n";
755 # now drop useless tables
756 foreach $table ( keys %dropable_table ) {
757 if ( $existingtables{$table} ) {
758 print "Dropping unused table $table\n" if $debug and not $silent;
759 $dbh->do("drop table $table");
761 print "Error : $dbh->errstr \n";
766 #---------------------------------
769 foreach $table ( keys %requirefields ) {
770 print "Check table $table\n" if $debug and not $silent;
771 $sth = $dbh->prepare("show columns from $table");
774 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
776 $types{$column} = $type;
778 foreach $column ( keys %{ $requirefields{$table} } ) {
779 print " Check column $column [$types{$column}]\n" if $debug and not $silent;
780 if ( !$types{$column} ) {
782 # column doesn't exist
783 print "Adding $column field to $table table...\n" unless $silent;
784 $query = "alter table $table
785 add column $column " . $requirefields{$table}->{$column};
786 print "Execute: $query\n" if $debug;
787 my $sti = $dbh->prepare($query);
790 print "**Error : $sti->errstr \n";
797 foreach $table ( keys %fielddefinitions ) {
798 print "Check table $table\n" if $debug;
799 $sth = $dbh->prepare("show columns from $table");
802 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
804 $definitions->{$column}->{type} = $type;
805 $definitions->{$column}->{null} = $null;
806 $definitions->{$column}->{null} = 'NULL' if $null eq 'YES';
807 $definitions->{$column}->{key} = $key;
808 $definitions->{$column}->{default} = $default;
809 $definitions->{$column}->{extra} = $extra;
811 my $fieldrow = $fielddefinitions{$table};
812 foreach my $row (@$fieldrow) {
813 my $field = $row->{field};
814 my $type = $row->{type};
815 my $null = $row->{null};
816 # $null = 'YES' if $row->{null} eq 'NULL';
817 my $key = $row->{key};
818 my $default = $row->{default};
819 my $null = $row->{null};
820 # $default="''" unless $default;
821 my $extra = $row->{extra};
822 my $def = $definitions->{$field};
824 unless ( $type eq $def->{type}
825 && $null eq $def->{null}
826 && $key eq $def->{key}
827 && $extra eq $def->{extra} )
832 if ( $key eq 'PRI' ) {
833 $key = 'PRIMARY KEY';
835 unless ( $extra eq 'auto_increment' ) {
839 # if it's a new column use "add", if it's an old one, use "change".
841 if ($definitions->{$field}->{type}) {
842 $action="change $field"
846 # if it's a primary key, drop the previous pk, before altering the table
848 if ($key ne 'PRIMARY KEY') {
849 $sth =$dbh->prepare("alter table $table $action $field $type $null $key $extra default ?");
851 $sth =$dbh->prepare("alter table $table drop primary key, $action $field $type $null $key $extra default ?");
853 $sth->execute($default);
854 print " Alter $field in $table\n" unless $silent;
860 # Populate tables with required data
863 # synch table and deletedtable.
864 foreach my $table (('borrowers','items','biblio','biblioitems')) {
865 my %deletedborrowers;
866 print "synch'ing $table\n";
867 $sth = $dbh->prepare("show columns from deleted$table");
869 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ) {
870 $deletedborrowers{$column}=1;
872 $sth = $dbh->prepare("show columns from $table");
875 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ) {
876 unless ($deletedborrowers{$column}) {
877 my $newcol="alter table deleted$table add $column $type";
878 if ($null eq 'YES') {
881 $newcol .= " NOT NULL ";
883 $newcol .= "default $default" if $default;
884 $newcol .= " after $previous" if $previous;
886 print "creating column $column\n";
892 foreach my $table ( keys %tabledata ) {
893 print "Checking for data required in table $table...\n" unless $silent;
894 my $tablerows = $tabledata{$table};
895 foreach my $row (@$tablerows) {
896 my $uniquefieldrequired = $row->{uniquefieldrequired};
897 my $uniquevalue = $row->{$uniquefieldrequired};
898 my $forceupdate = $row->{forceupdate};
901 "select $uniquefieldrequired from $table where $uniquefieldrequired=?"
903 $sth->execute($uniquevalue);
905 foreach my $field (keys %$forceupdate) {
906 if ($forceupdate->{$field}) {
907 my $sth=$dbh->prepare("update systempreferences set $field=? where $uniquefieldrequired=?");
908 $sth->execute($row->{$field}, $uniquevalue);
912 print "Adding row to $table: " unless $silent;
916 foreach my $field ( keys %$row ) {
917 next if $field eq 'uniquefieldrequired';
918 next if $field eq 'forceupdate';
919 my $value = $row->{$field};
920 push @values, $value;
921 print " $field => $value" unless $silent;
922 $fieldlist .= "$field,";
923 $placeholders .= "?,";
925 print "\n" unless $silent;
926 $fieldlist =~ s/,$//;
927 $placeholders =~ s/,$//;
930 "insert into $table ($fieldlist) values ($placeholders)");
931 $sth->execute(@values);
937 # check indexes and create them when needed
939 print "Checking for index required...\n" unless $silent;
940 foreach my $table ( keys %indexes ) {
942 # read all indexes from $table
944 $sth = $dbh->prepare("show index from $table");
947 while ( my ( $table, $non_unique, $key_name, $Seq_in_index, $Column_name, $Collation, $cardinality, $sub_part, $Packed, $comment ) = $sth->fetchrow ) {
948 $existingindexes{$key_name} = 1;
950 # read indexes to check
951 my $tablerows = $indexes{$table};
952 foreach my $row (@$tablerows) {
953 my $key_name=$row->{indexname};
954 if ($existingindexes{$key_name} eq 1) {
955 # print "$key_name existing";
957 print "\tCreating index $key_name in $table\n";
959 if ($row->{indexname} eq 'PRIMARY') {
960 $sql = "alter table $table ADD PRIMARY KEY ($row->{content})";
962 $sql = "alter table $table ADD INDEX $key_name ($row->{content}) $row->{type}";
965 print "Error $sql : $dbh->err \n" if $dbh->err;
971 # check foreign keys and create them when needed
973 print "Checking for foreign keys required...\n" unless $silent;
974 foreach my $table ( keys %foreign_keys ) {
976 # read all indexes from $table
978 $sth = $dbh->prepare("show table status like '$table'");
980 my $stat = $sth->fetchrow_hashref;
981 # read indexes to check
982 my $tablerows = $foreign_keys{$table};
983 foreach my $row (@$tablerows) {
984 my $foreign_table=$row->{foreigntable};
985 if ($stat->{'Comment'} =~/$foreign_table/) {
986 # print "$foreign_table existing\n";
988 print "\tCreating foreign key $foreign_table in $table\n";
989 # first, drop any orphan value in child table
990 if ($row->{onDelete} ne "RESTRICT") {
991 my $sql = "delete from $table where $row->{key} not in (select $row->{foreignkey} from $row->{foreigntable})";
993 print "SQL ERROR: $sql : $dbh->err \n" if $dbh->err;
995 my $sql="alter table $table ADD FOREIGN KEY $row->{key} ($row->{key}) REFERENCES $row->{foreigntable} ($row->{foreignkey})";
996 $sql .= " on update ".$row->{onUpdate} if $row->{onUpdate};
997 $sql .= " on delete ".$row->{onDelete} if $row->{onDelete};
1000 print "====================
1001 An error occured during :
1003 It probably means there is something wrong in your DB : a row ($table.$row->{key}) refers to a value in $row->{foreigntable}.$row->{foreignkey} that does not exist. solve the problem and run updater again (or just the previous SQL statement).
1004 You can find those values with select
1005 \t$table.* from $table where $row->{key} not in (select $row->{foreignkey} from $row->{foreigntable})
1006 ====================\n
1017 # create frameworkcode row in biblio table & fill it with marc_biblio.frameworkcode.
1020 # 1st, get how many biblio we will have to do...
1021 $sth = $dbh->prepare('select count(*) from marc_biblio');
1023 my ($totaltodo) = $sth->fetchrow;
1025 $sth = $dbh->prepare("show columns from biblio");
1028 my $bibliofwexist=0;
1029 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ){
1030 $bibliofwexist=1 if $column eq 'frameworkcode';
1032 unless ($bibliofwexist) {
1033 print "moving biblioframework to biblio table\n";
1034 $dbh->do('ALTER TABLE `biblio` ADD `frameworkcode` VARCHAR( 4 ) NOT NULL AFTER `biblionumber`');
1035 $sth = $dbh->prepare('select biblionumber,frameworkcode from marc_biblio');
1037 my $sth_update = $dbh->prepare('update biblio set frameworkcode=? where biblionumber=?');
1039 while (my ($biblionumber,$frameworkcode) = $sth->fetchrow) {
1040 $sth_update->execute($frameworkcode,$biblionumber);
1042 print "\r$totaldone / $totaltodo" unless ($totaldone % 100);
1048 # moving MARC data from marc_subfield_table to biblioitems.marc
1050 $sth = $dbh->prepare("show columns from biblioitems");
1054 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow ){
1055 $marcdone=1 if ($type eq 'blob' && $column eq 'marc') ;
1057 unless ($marcdone) {
1058 print "moving MARC record to biblioitems table\n";
1059 # changing marc field type
1060 $dbh->do('ALTER TABLE `biblioitems` CHANGE `marc` `marc` BLOB NULL DEFAULT NULL ');
1061 # adding marc xml, just for convenience
1062 $dbh->do('ALTER TABLE `biblioitems` ADD `marcxml` TEXT CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL ');
1063 # moving data from marc_subfield_value to biblio
1064 $sth = $dbh->prepare('select bibid,biblionumber from marc_biblio');
1066 my $sth_update = $dbh->prepare('update biblioitems set marc=?, marcxml=? where biblionumber=?');
1068 while (my ($bibid,$biblionumber) = $sth->fetchrow) {
1069 my $record = MARCgetbiblio($dbh,$bibid);
1070 print $record->as_formatted if ($biblionumber==3902);
1071 $sth_update->execute($record->as_usmarc(),$record->as_xml(),$biblionumber);
1073 print "\r$totaldone / $totaltodo" unless ($totaldone % 100);
1079 # at last, remove useless fields
1080 foreach $table ( keys %uselessfields ) {
1081 my @fields = split /,/,$uselessfields{$table};
1084 foreach my $fieldtodrop (@fields) {
1085 $fieldtodrop =~ s/\t//g;
1086 $fieldtodrop =~ s/\n//g;
1088 $sth = $dbh->prepare("show columns from $table");
1090 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1092 $exists =1 if ($column eq $fieldtodrop);
1095 print "deleting $fieldtodrop field in $table...\n" unless $silent;
1096 my $sth = $dbh->prepare("alter table $table drop $fieldtodrop");
1103 # MOVE all tables TO UTF-8 and innoDB
1104 $sth = $dbh->prepare("show table status");
1106 while ( my $table = $sth->fetchrow_hashref ) {
1107 # if ($table->{Engine} ne 'InnoDB') {
1108 # $dbh->do("ALTER TABLE $table->{Name} TYPE = innodb");
1109 # print "moving $table->{Name} to InnoDB\n";
1111 unless ($table->{Collation} =~ /^utf8/) {
1112 $dbh->do("ALTER TABLE $table->{Name} CONVERT TO CHARACTER SET utf8");
1113 $dbh->do("ALTER TABLE $table->{Name} DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci");
1114 # FIXME : maybe a ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8 would be better, def char set seems to work fine. If any problem encountered, let's try with convert !
1115 print "moving $table->{Name} to utf8\n";
1123 # those 2 subs are a copy of Biblio.pm, version 2.2.4
1124 # they are useful only once, for moving from 2.2 to 3.0
1125 # the MARCgetbiblio & MARCgetitem subs in Biblio.pm
1126 # are still here, but uses other tables
1127 # (the ones that are filled by updatedatabase !)
1132 # Returns MARC::Record of the biblio passed in parameter.
1133 my ( $dbh, $bibid ) = @_;
1134 my $record = MARC::Record->new();
1139 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
1140 from marc_subfield_table
1141 where bibid=? order by tag,tagorder,subfieldorder
1146 "select subfieldvalue from marc_blob_subfield where blobidlink=?");
1147 $sth->execute($bibid);
1148 my $prevtagorder = 1;
1149 my $prevtag = 'XXX';
1151 my $field; # for >=10 tags
1152 my $prevvalue; # for <10 tags
1153 while ( my $row = $sth->fetchrow_hashref ) {
1155 if ( $row->{'valuebloblink'} ) { #---- search blob if there is one
1156 $sth2->execute( $row->{'valuebloblink'} );
1157 my $row2 = $sth2->fetchrow_hashref;
1159 $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
1161 if ( $row->{tagorder} ne $prevtagorder || $row->{tag} ne $prevtag ) {
1162 $previndicator .= " ";
1163 if ( $prevtag < 10 ) {
1164 if ($prevtag ne '000') {
1165 $record->add_fields( ( sprintf "%03s", $prevtag ), $prevvalue ) unless $prevtag eq "XXX"; # ignore the 1st loop
1167 $record->leader(sprintf("%24s",$prevvalue));
1171 $record->add_fields($field) unless $prevtag eq "XXX";
1174 $prevtagorder = $row->{tagorder};
1175 $prevtag = $row->{tag};
1176 $previndicator = $row->{tag_indicator};
1177 if ( $row->{tag} < 10 ) {
1178 $prevvalue = $row->{subfieldvalue};
1181 $field = MARC::Field->new(
1182 ( sprintf "%03s", $prevtag ),
1183 substr( $row->{tag_indicator} . ' ', 0, 1 ),
1184 substr( $row->{tag_indicator} . ' ', 1, 1 ),
1185 $row->{'subfieldcode'},
1186 $row->{'subfieldvalue'}
1191 if ( $row->{tag} < 10 ) {
1192 $record->add_fields( ( sprintf "%03s", $row->{tag} ),
1193 $row->{'subfieldvalue'} );
1196 $field->add_subfields( $row->{'subfieldcode'},
1197 $row->{'subfieldvalue'} );
1199 $prevtag = $row->{tag};
1200 $previndicator = $row->{tag_indicator};
1204 # the last has not been included inside the loop... do it now !
1205 if ( $prevtag ne "XXX" )
1206 { # check that we have found something. Otherwise, prevtag is still XXX and we
1207 # must return an empty record, not make MARC::Record fail because we try to
1208 # create a record with XXX as field :-(
1209 if ( $prevtag < 10 ) {
1210 $record->add_fields( $prevtag, $prevvalue );
1214 # my $field = MARC::Field->new( $prevtag, "", "", %subfieldlist);
1215 $record->add_fields($field);
1223 # Returns MARC::Record of the biblio passed in parameter.
1224 my ( $dbh, $bibid, $itemnumber ) = @_;
1225 my $record = MARC::Record->new();
1227 # search MARC tagorder
1230 "select tagorder from marc_subfield_table,marc_subfield_structure where marc_subfield_table.tag=marc_subfield_structure.tagfield and marc_subfield_table.subfieldcode=marc_subfield_structure.tagsubfield and bibid=? and kohafield='items.itemnumber' and subfieldvalue=?"
1232 $sth2->execute( $bibid, $itemnumber );
1233 my ($tagorder) = $sth2->fetchrow_array();
1235 #---- TODO : the leader is missing
1238 "select bibid,subfieldid,tag,tagorder,tag_indicator,subfieldcode,subfieldorder,subfieldvalue,valuebloblink
1239 from marc_subfield_table
1240 where bibid=? and tagorder=? order by subfieldcode,subfieldorder
1245 "select subfieldvalue from marc_blob_subfield where blobidlink=?");
1246 $sth->execute( $bibid, $tagorder );
1247 while ( my $row = $sth->fetchrow_hashref ) {
1248 if ( $row->{'valuebloblink'} ) { #---- search blob if there is one
1249 $sth2->execute( $row->{'valuebloblink'} );
1250 my $row2 = $sth2->fetchrow_hashref;
1252 $row->{'subfieldvalue'} = $row2->{'subfieldvalue'};
1254 if ( $record->field( $row->{'tag'} ) ) {
1257 #--- this test must stay as this, because of strange behaviour of mySQL/Perl DBI with char var containing a number...
1258 #--- sometimes, eliminates 0 at beginning, sometimes no ;-\\\
1259 if ( length( $row->{'tag'} ) < 3 ) {
1260 $row->{'tag'} = "0" . $row->{'tag'};
1262 $field = $record->field( $row->{'tag'} );
1265 $field->add_subfields( $row->{'subfieldcode'},
1266 $row->{'subfieldvalue'} );
1267 $record->delete_field($field);
1268 $record->add_fields($field);
1272 if ( length( $row->{'tag'} ) < 3 ) {
1273 $row->{'tag'} = "0" . $row->{'tag'};
1276 MARC::Field->new( $row->{'tag'}, " ", " ",
1277 $row->{'subfieldcode'} => $row->{'subfieldvalue'} );
1278 $record->add_fields($temp);
1289 # Revision 1.133 2006/04/13 08:36:42 plg
1290 # new: function C4::Date::get_date_format_string_for_DHTMLcalendar based on
1291 # the system preference prefered date format.
1293 # improvement: book fund list and budget list screen redesigned. Filters on
1294 # each field. Columns are not sortable yet. Using DHTML Calendar to fill date
1295 # fields instead of manual filling. Pagination system. From the book fund
1296 # list, you can reach the budget list, filtered on a book fund, or not. A
1297 # budget can be added only from book fund list screen.
1299 # bug fixed: branchcode was missing in table aqbudget.
1301 # bug fixed: when setting a branchcode to a book fund, all associated budgets
1302 # move to this branchcode.
1304 # modification: when adding/modifying budget/fund, MySQL specific "REPLACE..."
1305 # statements replaced by standard SQL compliant statement.
1307 # bug fixed: when adding/modifying a budget, if the book fund is associated to
1308 # a branch, the branch selection is disabled and set to the book fund branch.
1310 # Revision 1.132 2006/04/06 12:37:05 hdl
1311 # Bugfixing : aqbookfund needed a field.
1313 # Revision 1.131 2006/03/03 17:02:22 tipaul
1314 # commit for holidays and news management.
1315 # (some forgotten files)
1317 # Revision 1.130 2006/03/03 16:35:21 tipaul
1318 # commit for holidays and news management.
1320 # Contrib from Tümer Garip (from Turkey) :
1322 # in /tools/ the holiday.pl script let you define holidays (days where the library is closed), branch by branch. You can define 3 types of holidays :
1323 # - single day : only this day is closed
1324 # - repet weekly (like "sunday") : the day is holiday every week
1325 # - repet yearly (like "July, 4") : this day is closed every year.
1327 # You can also put exception :
1328 # - sunday is holiday, but "2006 March, 5th" the library will be open
1330 # The holidays are used for return date calculation : the return date is set to the next date where the library is open. A systempreference (useDaysMode) set ON (Calendar) or OFF (Normal) the calendar calculation.
1332 # Revision 1.129 2006/02/27 18:19:33 hdl
1333 # New table used in overduerules.pl tools page.
1335 # Revision 1.128 2006/01/25 15:16:06 tipaul
1337 # * removing useless tables
1338 # * adding useful indexes
1339 # * altering some columns definitions
1340 # * The goal being to have updater working fine for foreign keys.
1342 # For me it's done, let me know if it works for you. You can see an updated schema of the DB (with constraints) on the wiki
1344 # Revision 1.127 2006/01/24 17:57:17 tipaul
1345 # DB improvements : adding foreign keys on some tables. partial stuff done.
1347 # Revision 1.126 2006/01/06 16:39:42 tipaul
1348 # synch'ing head and rel_2_2 (from 2.2.5, including npl templates)
1349 # Seems not to break too many things, but i'm probably wrong here.
1350 # at least, new features/bugfixes from 2.2.5 are here (tested on some features on my head local copy)
1352 # - removing useless directories (koha-html and koha-plucene)
1354 # Revision 1.125 2006/01/04 15:54:55 tipaul
1355 # utf8 is a : go for beta test in HEAD.
1356 # some explanations :
1357 # - updater/updatedatabase => will transform all tables in innoDB (not related to utf8, just to warn you) AND collate them in utf8 / utf8_general_ci. The SQL command is : ALTER TABLE tablename DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci.
1358 # - *-top.inc will show the pages in utf8
1359 # - THE HARD THING : for me, mysql-client and mysql-server were set up to communicate in iso8859-1, whatever the mysql collation ! Thus, pages were improperly shown, as datas were transmitted in iso8859-1 format ! After a full day of investigation, someone on usenet pointed "set NAMES 'utf8'" to explain that I wanted utf8. I could put this in my.cnf, but if I do that, ALL databases will "speak" in utf8, that's not what we want. Thus, I added a line in Context.pm : everytime a DB handle is opened, the communication is set to utf8.
1360 # - using marcxml field and no more the iso2709 raw marc biblioitems.marc field.
1362 # Revision 1.124 2005/10/27 12:09:05 tipaul
1363 # new features for serial module :
1364 # - the last 5 issues are now shown, and their status can be changed (but not reverted to "waited", as there can be only one "waited")
1365 # - the library can create a "distribution list". this paper contains a list of borrowers (selected from the borrower list, or manually entered), and print it for a given issue. once printed, the sheet can be put on the issue and distributed to every reader on the list (one by one).
1367 # Revision 1.123 2005/10/26 09:13:37 tipaul
1368 # big commit, still breaking things...
1370 # * synch with rel_2_2. Probably the last non manual synch, as rel_2_2 should not be modified deeply.
1371 # * code cleaning (cleaning warnings from perl -w) continued
1373 # Revision 1.122 2005/09/02 14:18:38 tipaul
1374 # new feature : image for itemtypes.
1376 # * run updater/updatedatabase to create imageurl field in itemtypes.
1377 # * go to Koha >> parameters >> itemtypes >> modify (or add) an itemtype. You will see around 20 nice images to choose between (thanks to owen). If you prefer your own image, you also can type a complete url (http://www.myserver.lib/path/to/my/image.gif)
1378 # * go to OPAC, and search something. In the result list, you now have the picture instead of the text itemtype.
1380 # Revision 1.121 2005/08/24 08:49:03 hdl
1381 # Adding a note field in serial table.
1382 # This will allow librarian to mention a note on a peculiar waiting serial number.
1384 # Revision 1.120 2005/08/09 14:10:32 tipaul
1385 # 1st commit to go to zebra.
1386 # don't update your cvs if you want to have a working head...
1388 # this commit contains :
1389 # * updater/updatedatabase : get rid with marc_* tables, but DON'T remove them. As a lot of things uses them, it would not be a good idea for instance to drop them. If you really want to play, you can rename them to test head without them but being still able to reintroduce them...
1390 # * Biblio.pm : modify MARCgetbiblio to find the raw marc record in biblioitems.marc field, not from marc_subfield_table, modify MARCfindframeworkcode to find frameworkcode in biblio.frameworkcode, modify some other subs to use biblio.biblionumber & get rid of bibid.
1391 # * other files : get rid of bibid and use biblionumber instead.
1394 # * does not do anything on zebra yet.
1395 # * if you rename marc_subfield_table, you can't search anymore.
1396 # * you can view a biblio & bibliodetails, go to MARC editor, but NOT save any modif.
1397 # * don't try to add a biblio, it would add data poorly... (don't try to delete either, it may work, but that would be a surprise ;-) )
1399 # IMPORTANT NOTE : you need MARC::XML package (http://search.cpan.org/~esummers/MARC-XML-0.7/lib/MARC/File/XML.pm), that requires a recent version of MARC::Record
1400 # Updatedatabase stores the iso2709 data in biblioitems.marc field & an xml version in biblioitems.marcxml Not sure we will keep it when releasing the stable version, but I think it's a good idea to have something readable in sql, at least for development stage.
1402 # Revision 1.119 2005/08/04 16:07:58 tipaul
1403 # Synch really broke this script...
1405 # Revision 1.118 2005/08/04 16:02:55 tipaul
1406 # oops... error in synch between 2.2 and head
1408 # Revision 1.117 2005/08/04 14:24:39 tipaul
1409 # synch'ing 2.2 and head
1411 # Revision 1.116 2005/08/04 08:55:54 tipaul
1412 # Letters / alert system, continuing...
1414 # * adding a package Letters.pm, that manages Letters & alerts.
1415 # * adding feature : it's now possible to define a "letter" for any subscription created. If a letter is defined, users in OPAC can put an alert on the subscription. When an issue is marked "arrived", all users in the alert will recieve a mail (as defined in the "letter"). This last part (= send the mail) is not yet developped. (Should be done this week)
1416 # * adding feature : it's now possible to "put to an alert" in OPAC, for any serial subscription. The alert is stored in a new table, called alert. An alert can be put only if the librarian has activated them in subscription (and they activate it just by choosing a "letter" to sent to borrowers on new issues)
1417 # * adding feature : librarian can see in borrower detail which alerts they have put, and a user can see in opac-detail which alert they have put too.
1419 # Note that the system should be generic enough to manage any type of alert.
1420 # I plan to extend it soon to virtual shelves : a borrower will be able to put an alert on a virtual shelf, to be warned when something is changed in the virtual shelf (mail being sent once a day by cron, or manually by the shelf owner. Anyway, a mail won't be sent on every change, users would be spammed by Koha ;-) )
1422 # Revision 1.115 2005/08/02 16:15:34 tipaul
1423 # adding 2 fields to letter system :
1424 # * module (acquisition, catalogue...) : it will be usefull to show the librarian only letters he may be interested by.
1425 # * title, that will be used as mail subject.
1427 # Revision 1.114 2005/07/28 15:10:13 tipaul
1428 # Introducing new "Letters" system : Letters will be used everytime you want to sent something to someone (through mail or paper). For example, sending a mail for overdues use letter that you can put as parameters. Sending a mail to a borrower when a suggestion is validated uses a letter too.
1429 # the letter table contains 3 fields :
1430 # * code => the code of the letter
1431 # * name => the complete name of the letter
1432 # * content => the complete text. It's a TEXT field type, so has no limits.
1434 # My next goal now is to work on point 2-I "serial issue alert"
1435 # With this feature, in serials, a user can subscribe the "issue alert". For every issue arrived/missing, a mail is sent to all subscribers of this list. The mail warns the user that the issue is arrive or missing. Will be in head.
1436 # (see mail on koha-devel, 2005/04/07)
1438 # The "serial issue alert" will be the 1st to use this letter system that probably needs some tweaking ;-)
1440 # Once it will be stabilised default letters (in any languages) could be added during installer to help the library begin with this new feature.
1442 # Revision 1.113 2005/07/28 08:38:41 tipaul
1443 # For instance, the return date does not rely on the borrower expiration date. A systempref will be added in Koha, to modify return date calculation schema :
1444 # * ReturnBeforeExpiry = yes => return date can't be after expiry date
1445 # * ReturnBeforeExpiry = no => return date can be after expiry date
1447 # Revision 1.112 2005/07/26 08:19:47 hdl
1448 # Adding IndependantBranches System preference variable in order to manage Branch independancy.
1450 # Revision 1.111 2005/07/25 15:35:38 tipaul
1451 # we have decided that moving to Koha 3.0 requires being already in Koha 2.2.x
1452 # So, the updatedatabase script can highly be cleaned (90% removed).
1453 # Let's play with the new Koha DB structure now ;-)