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
44 my $dbh = C4::Context->dbh;
45 print "connected to your DB. Checking & modifying it\n" unless $silent;
50 # Tables to add if they don't exist
52 shelfcontents => "( shelfnumber int not null,
53 itemnumber int not null,
55 bookshelf => "( shelfnumber int auto_increment primary key,
56 shelfname char(255))",
57 z3950queue => "( id int auto_increment primary key,
66 identifier char(30))",
67 z3950results => "( id int auto_increment primary key,
77 branchrelations => "( branchcode varchar(4),
78 categorycode varchar(4))",
79 websites => "( websitenumber int(11) NOT NULL auto_increment,
80 biblionumber int(11) NOT NULL default '0',
84 PRIMARY KEY (websitenumber) )",
85 marcrecorddone => "( isbn char(40),
88 controlnumber char(40))",
89 uploadedmarc => "( id int(11) NOT NULL auto_increment PRIMARY KEY,
91 hidden smallint(6) default NULL,
92 name varchar(255) default NULL)",
93 ethnicity => "( code varchar(10) NOT NULL default '',
94 name varchar(255) default NULL,
95 PRIMARY KEY (code) )",
96 sessions => "( sessionID varchar(255) NOT NULL default '',
97 userid varchar(255) default NULL,
98 ip varchar(16) default NULL,
100 PRIMARY KEY (sessionID) )",
101 sessionqueries => "( sessionID varchar(255) NOT NULL default '',
102 userid char(100) NOT NULL default '',
103 ip char(18) NOT NULL default '',
104 url text NOT NULL default '' )",
105 bibliothesaurus => "( id bigint(20) NOT NULL auto_increment,
106 freelib char(255) NOT NULL default '',
107 stdlib char(255) NOT NULL default '',
108 category char(10) NOT NULL default '',
109 level tinyint(4) NOT NULL default '1',
110 hierarchy char(80) NOT NULL default '',
111 father char(80) NOT NULL default '',
113 KEY freelib (freelib),
115 KEY category (category),
116 KEY hierarchy (hierarchy)
119 bibid bigint(20) unsigned NOT NULL auto_increment,
120 biblionumber int(11) NOT NULL default '0',
121 datecreated date NOT NULL default '0000-00-00',
122 datemodified date default NULL,
123 origincode char(20) default NULL,
125 KEY origincode (origincode),
126 KEY biblionumber (biblionumber)
128 marc_blob_subfield => "(
129 blobidlink bigint(20) NOT NULL auto_increment,
130 subfieldvalue longtext NOT NULL,
131 PRIMARY KEY (blobidlink)
133 marc_subfield_structure => "(
134 tagfield char(3) NOT NULL default '',
135 tagsubfield char(1) NOT NULL default '',
136 liblibrarian char(255) NOT NULL default '',
137 libopac char(255) NOT NULL default '',
138 repeatable tinyint(4) NOT NULL default '0',
139 mandatory tinyint(4) NOT NULL default '0',
140 kohafield char(40) default NULL,
141 tab tinyint(1) default NULL,
142 authorised_value char(10) default NULL,
143 thesaurus_category char(10) default NULL,
144 value_builder char(80) default NULL,
145 PRIMARY KEY (tagfield,tagsubfield),
146 KEY kohafield (kohafield),
149 marc_subfield_table => "(
150 subfieldid bigint(20) unsigned NOT NULL auto_increment,
151 bibid bigint(20) unsigned NOT NULL default '0',
152 tag char(3) NOT NULL default '',
153 tagorder tinyint(4) NOT NULL default '1',
154 tag_indicator char(2) NOT NULL default '',
155 subfieldcode char(1) NOT NULL default '',
156 subfieldorder tinyint(4) NOT NULL default '1',
157 subfieldvalue varchar(255) default NULL,
158 valuebloblink bigint(20) default NULL,
159 PRIMARY KEY (subfieldid),
162 KEY tag_indicator (tag_indicator),
163 KEY subfieldorder (subfieldorder),
164 KEY subfieldcode (subfieldcode),
165 KEY subfieldvalue (subfieldvalue),
166 KEY tagorder (tagorder)
168 marc_tag_structure => "(
169 tagfield char(3) NOT NULL default '',
170 liblibrarian char(255) NOT NULL default '',
171 libopac char(255) NOT NULL default '',
172 repeatable tinyint(4) NOT NULL default '0',
173 mandatory tinyint(4) NOT NULL default '0',
174 authorised_value char(10) default NULL,
175 PRIMARY KEY (tagfield)
178 bibid bigint(20) NOT NULL default '0',
179 tag char(3) NOT NULL default '',
180 tagorder tinyint(4) NOT NULL default '1',
181 subfieldid char(1) NOT NULL default '',
182 subfieldorder tinyint(4) NOT NULL default '1',
183 word varchar(255) NOT NULL default '',
184 sndx_word varchar(255) NOT NULL default '',
187 KEY tagorder (tagorder),
188 KEY subfieldid (subfieldid),
189 KEY subfieldorder (subfieldorder),
191 KEY sndx_word (sndx_word)
193 marc_breeding => "( id bigint(20) NOT NULL auto_increment,
194 file varchar(80) NOT NULL default '',
195 isbn varchar(10) NOT NULL default '',
196 title varchar(128) default NULL,
197 author varchar(80) default NULL,
199 encoding varchar(40) default NULL,
204 authorised_values => "(id int(11) NOT NULL auto_increment,
205 category char(10) NOT NULL default '',
206 authorised_value char(80) NOT NULL default '',
211 userflags => "( bit int(11) NOT NULL default '0',
212 flag char(30), flagdesc char(255),
216 authtypecode char(10) not NULL,
217 authtypetext char(255) not NULL,
218 auth_tag_to_report char(3) not NULL,
219 summary text not NULL,
220 PRIMARY KEY (authtypecode)
222 biblio_framework => "(
223 frameworkcode char(4) not NULL,
224 frameworktext char(255) not NULL,
225 PRIMARY KEY (frameworkcode)
227 auth_subfield_structure => "(
228 authtypecode char(10) NOT NULL default '',
229 tagfield char(3) NOT NULL default '',
230 tagsubfield char(1) NOT NULL default '',
231 liblibrarian char(255) NOT NULL default '',
232 libopac char(255) NOT NULL default '',
233 repeatable tinyint(4) NOT NULL default '0',
234 mandatory tinyint(4) NOT NULL default '0',
235 tab tinyint(1) default NULL,
236 authorised_value char(10) default NULL,
237 value_builder char(80) default NULL,
238 seealso char(255) default NULL,
239 PRIMARY KEY (authtypecode,tagfield,tagsubfield),
240 KEY tab (authtypecode,tab)
242 auth_tag_structure => "(
243 authtypecode char(10) NOT NULL default '',
244 tagfield char(3) NOT NULL default '',
245 liblibrarian char(255) NOT NULL default '',
246 libopac char(255) NOT NULL default '',
247 repeatable tinyint(4) NOT NULL default '0',
248 mandatory tinyint(4) NOT NULL default '0',
249 authorised_value char(10) default NULL,
250 PRIMARY KEY (authtypecode,tagfield)
253 authid bigint(20) unsigned NOT NULL auto_increment,
254 authtypecode char(10) NOT NULL default '',
255 datecreated date NOT NULL default '0000-00-00',
256 datemodified date default NULL,
257 origincode char(20) default NULL,
258 PRIMARY KEY (authid),
259 KEY origincode (origincode),
261 auth_subfield_table => "(
262 subfieldid bigint(20) unsigned NOT NULL auto_increment,
263 authid bigint(20) unsigned NOT NULL default '0',
264 tag char(3) NOT NULL default '',
265 tagorder tinyint(4) NOT NULL default '1',
266 tag_indicator char(2) NOT NULL default '',
267 subfieldcode char(1) NOT NULL default '',
268 subfieldorder tinyint(4) NOT NULL default '1',
269 subfieldvalue varchar(255) default NULL,
270 PRIMARY KEY (subfieldid),
273 KEY subfieldcode (subfieldcode),
274 KEY subfieldvalue (subfieldvalue)
277 authid bigint(20) NOT NULL default '0',
278 tagsubfield char(4) NOT NULL default '',
279 tagorder tinyint(4) NOT NULL default '1',
280 subfieldorder tinyint(4) NOT NULL default '1',
281 word varchar(255) NOT NULL default '',
282 sndx_word varchar(255) NOT NULL default '',
284 KEY marc_search (tagsubfield,word),
286 KEY sndx_word (sndx_word)
289 suggestionid int(8) NOT NULL auto_increment,
290 suggestedby int(11) NOT NULL default '0',
291 managedby int(11) default NULL ,
292 STATUS varchar(10) NOT NULL default '',
294 author varchar(80) default NULL ,
295 title varchar(80) default NULL ,
296 copyrightdate smallint(6) default NULL ,
297 publishercode varchar(255) default NULL ,
298 date timestamp(8) NOT NULL ,
299 volumedesc varchar(255) default NULL ,
300 publicationyear smallint(6) default '0',
301 place varchar(255) default NULL ,
302 isbn varchar(10) default NULL ,
303 mailoverseeing smallint(1) default '0',
304 biblionumber int(11) default NULL ,
305 PRIMARY KEY (suggestionid) ,
306 KEY suggestedby(suggestedby) ,
307 KEY managedby(managedby)
309 aqbasket => "(basketno int(11) NOT NULL auto_increment,
312 booksellerid varchar(10),
313 authorisedby varchar(10),
314 booksellerinvoicenumber text,
315 PRIMARY KEY (basketno)
317 serial => "(serialid int(11) NOT NULL auto_increment,
318 biblionumber varchar(100) NOT NULL default '',
319 subscriptionid varchar(100) NOT NULL default '',
320 serialseq varchar(100) NOT NULL default '',
321 status tinyint(4) NOT NULL default '0',
322 planneddate date NOT NULL default '0000-00-00',
323 PRIMARY KEY (serialid)
325 subscription => "(biblionumber int(11) NOT NULL default '0',
326 subscriptionid int(11) NOT NULL auto_increment,
327 librarian varchar(100) default '',
328 startdate date default '0000-00-00',
329 aqbooksellerid int(11) default '0',
330 cost int(11) default '0',
331 aqbudgetid int(11) default '0',
332 weeklength tinyint(4) default '0',
333 monthlength tinyint(4) default '0',
334 numberlength tinyint(4) default '0',
335 periodicity tinyint(4) default '0',
336 dow varchar(100) default '',
337 numberingmethod varchar(100) default '',
339 status varchar(100) NOT NULL default '',
340 add1 int(11) default 0,
341 every1 int(11) default 0,
342 whenmorethan1 int(11) default 0,
345 add2 int(11) default 0,
346 every2 int(11) default 0,
347 whenmorethan2 int(11) default 0,
350 add3 int(11) default 0,
351 every3 int(11) default 0,
352 innerloop1 int(11) default 0,
353 innerloop2 int(11) default 0,
354 innerloop3 int(11) default 0,
355 whenmorethan3 int(11) default 0,
358 PRIMARY KEY (subscriptionid)
360 subscriptionhistory => "(biblionumber int(11) NOT NULL default '0',
361 subscriptionid int(11) NOT NULL default '0',
362 histstartdate date NOT NULL default '0000-00-00',
363 enddate date default '0000-00-00',
364 missinglist longtext NOT NULL,
365 recievedlist longtext NOT NULL,
366 opacnote varchar(150) NOT NULL default '',
367 librariannote varchar(150) NOT NULL default '',
368 PRIMARY KEY (subscriptionid),
369 KEY biblionumber (biblionumber)
373 my %requirefields = (
374 biblio => { 'abstract' => 'text' },
375 deletedbiblio => { 'abstract' => 'text', 'marc' => 'blob' },
376 deleteditems => { 'marc' => 'blob', 'paidfor' => 'text' },
378 'lccn' => 'char(25)',
379 'url' => 'varchar(255)',
382 deletedbiblioitems => {
383 'lccn' => 'char(25)',
384 'url' => 'varchar(255)',
387 branchtransfers => { 'datearrived' => 'datetime' },
388 statistics => { 'borrowernumber' => 'int(11)' },
390 'invoicedisc' => 'float(6,4)',
391 'nocalc' => 'int(11)'
394 'userid' => 'char(30)',
395 'password' => 'char(30)',
396 'flags' => 'int(11)',
397 'textmessaging' => 'varchar(30)',
398 'zipcode' => 'varchar(25)',
399 'homezipcode' => 'varchar(25)',
400 'sort1' => 'char(80)',
401 'sort2' => 'char(80)',
403 aqorders => { 'budgetdate' => 'date',
404 'sort1' => 'char(80)',
405 'sort2' => 'char(80)', },
406 aqbudget => {'aqbudgetid' => 'tinyint(4) auto_increment primary key'},
407 items => {'paidfor' => 'text', 'location' => 'char(80)'},
409 #added so that reference items are not available for reserves...
410 itemtypes => { 'notforloan' => 'smallint(6)' },
411 systempreferences => { 'explanation' => 'char(80)',
412 'type' => 'char(20)',
413 'options' => 'text' },
414 z3950servers => { 'syntax' => 'char(80)' },
415 marc_tag_structure =>{
416 'frameworkcode' => 'char(4) not NULL default \'\''},
417 marc_subfield_structure =>{'seealso' => 'char(255)',
418 'frameworkcode' => 'char(4) not NULL default \'\'',
419 'hidden' => 'tinyint(1)',
420 'isurl' => 'tinyint(1)',
421 'link' => 'char(80)',
423 bookshelf => {'owner' => 'char(80)',
424 'category' => 'char(1)',
426 marc_biblio => { 'frameworkcode' => 'char(4) not NULL default \'\'' },
429 my %dropable_table = (
430 classification => 'classification',
431 multipart => 'multipart',
432 multivolume => 'multivolume',
433 newitems => 'newitems',
434 procedures => 'procedures',
435 publisher => 'publisher',
436 searchstats => 'searchstats',
437 serialissues => 'serialissues',
440 my %uselessfields = (
441 aqorders => "requisitionedby,authorisedby,booksellerid,
442 deliverydays,followupdays,
443 numberfollowupsallowed,numberfollowupssent,
444 dateprinted,sourced,quantityreceiveddamaged,
445 subscriptionfrom,subscriptionto
448 # the other hash contains other actions that can't be done elsewhere. they are done
449 # either BEFORE of AFTER everything else, depending on "when" entry (default => AFTER)
451 # The tabledata hash contains data that should be in the tables.
452 # The uniquefieldrequired hash entry is used to determine which (if any) fields
453 # must not exist in the table for this row to be inserted. If the
454 # uniquefieldrequired entry is already in the table, the existing data is not
455 # modified, unless the forceupdate hash entry is also set. Fields in the
456 # anonymous "forceupdate" hash will be forced to be updated to the default
457 # values given in the %tabledata hash.
462 uniquefieldrequired => 'bit',
464 flag => 'superlibrarian',
465 flagdesc => 'Access to all librarian functions',
469 uniquefieldrequired => 'bit',
472 flagdesc => 'Circulate books',
476 uniquefieldrequired => 'bit',
479 flagdesc => 'View Catalogue (Librarian Interface)',
483 uniquefieldrequired => 'bit',
485 flag => 'parameters',
486 flagdesc => 'Set Koha system paramters',
490 uniquefieldrequired => 'bit',
493 flagdesc => 'Add or modify borrowers',
497 uniquefieldrequired => 'bit',
499 flag => 'permissions',
500 flagdesc => 'Set user permissions',
504 uniquefieldrequired => 'bit',
506 flag => 'reserveforothers',
507 flagdesc => 'Reserve books for patrons',
511 uniquefieldrequired => 'bit',
514 flagdesc => 'Borrow books',
518 uniquefieldrequired => 'bit',
520 flag => 'reserveforself',
521 flagdesc => 'Reserve books for self',
525 uniquefieldrequired => 'bit',
527 flag => 'editcatalogue',
528 flagdesc => 'Edit Catalogue (Modify bibliographic/holdings data)',
532 uniquefieldrequired => 'bit',
534 flag => 'updatecharges',
535 flagdesc => 'Update borrower charges',
539 systempreferences => [
541 uniquefieldrequired => 'variable',
542 forceupdate => { 'explanation' => 1,
544 variable => 'LibraryName',
545 value => '<i><b>Koha<br/>Free Software ILS<br/><br/></b>Koha : a gift, a contribution<br/> in Maori</i>',
546 explanation => 'Library name as shown on main opac page',
551 uniquefieldrequired => 'variable',
552 forceupdate => { 'explanation' => 1,
554 variable => 'autoMemberNum',
556 explanation => 'Member number is auto-calculated',
561 uniquefieldrequired => 'variable',
562 forceupdate => { 'explanation' => 1,
565 variable => 'acquisitions',
568 'Normal, budget-based acquisitions, or Simple bibliographic-data acquisitions',
570 options => 'simple|normal'
573 uniquefieldrequired => 'variable',
574 forceupdate => { 'explanation' => 1,
577 variable => 'dateformat',
580 'date format (us mm/dd/yyyy, metric dd/mm/yyy, ISO yyyy/mm/dd)',
582 options => 'metric|us|iso'
585 uniquefieldrequired => 'variable',
586 variable => 'template',
587 forceupdate => { 'explanation' => 1,
590 explanation => 'Preference order for intranet interface templates',
594 uniquefieldrequired => 'variable',
595 variable => 'autoBarcode',
596 forceupdate => { 'explanation' => 1,
599 explanation => 'Barcode is auto-calculated',
603 uniquefieldrequired => 'variable',
604 variable => 'insecure',
605 forceupdate => { 'explanation' => 1,
609 'If YES, no auth at all is needed. Be careful if you set this to yes!',
613 uniquefieldrequired => 'variable',
614 variable => 'authoritysep',
615 forceupdate => { 'explanation' => 1,
620 'the separator used in authority/thesaurus. Usually --',
625 uniquefieldrequired => 'variable',
626 variable => 'opaclanguages',
627 forceupdate => { 'explanation' => 1,
630 explanation => 'Set the preferred order for translations. The top language will be tried first.',
634 uniquefieldrequired => 'variable',
635 variable => 'opacthemes',
636 forceupdate => { 'explanation' => 1,
639 explanation => 'Set the preferred order for themes. The top theme will be tried first.',
643 uniquefieldrequired => 'variable',
644 variable => 'timeout',
645 forceupdate => { 'explanation' => 1,
648 explanation => 'Inactivity timeout for cookies authentication (in seconds)',
652 uniquefieldrequired => 'variable',
654 forceupdate => { 'explanation' => 1,
657 explanation => 'Turn on MARC support',
661 uniquefieldrequired => 'variable',
662 variable => 'marcflavour',
663 forceupdate => { 'explanation' => 1,
668 'your MARC flavor (MARC21 or UNIMARC) used for character encoding',
670 options => 'MARC21|UNIMARC'
673 uniquefieldrequired => 'variable',
674 variable => 'checkdigit',
676 forceupdate => { 'explanation' => 1,
679 explanation => 'Validity checks on membership number: none or "Katipo" style checks',
681 options => 'none|katipo'
684 uniquefieldrequired => 'variable',
685 variable => 'maxoutstanding',
686 forceupdate => { 'explanation' => 1,
690 'maximum amount withstanding to be able make reserves ',
694 uniquefieldrequired => 'variable',
695 variable => 'maxreserves',
696 forceupdate => { 'explanation' => 1,
700 'maximum number of reserves a member can make',
705 uniquefieldrequired => 'variable',
706 variable => 'noissuescharge',
707 forceupdate => { 'explanation' => 1,
711 'maximum amount withstanding to be able to check out an item',
716 uniquefieldrequired => 'variable',
717 variable => 'KohaAdminEmailAddress',
718 forceupdate => { 'explanation' => 1,
720 value => 'your.mail@here',
721 explanation => 'the email address where borrowers modifs are sent',
725 uniquefieldrequired => 'variable',
727 forceupdate => { 'explanation' => 1,
730 explanation => 'the gist rate. NOT in %, but in numeric form (0.12 for 12%)',
734 uniquefieldrequired => 'variable',
735 variable => 'ldapserver',
736 forceupdate => { 'explanation' => 1,
739 explanation => 'your ldap server',
743 uniquefieldrequired => 'variable',
744 variable => 'ldapinfos',
745 forceupdate => { 'explanation' => 1,
748 explanation => 'ldap info. The ldap will be used in dn : uid=xxx, <ldapinfos>',
752 uniquefieldrequired => 'variable',
753 variable => 'printcirculationslips',
754 forceupdate => { 'explanation' => 1,
757 explanation => 'if set to 1, print circulation slips. If set to 0, don\'t',
761 uniquefieldrequired => 'variable',
762 variable => 'suggestion',
763 forceupdate => { 'explanation' => 1,
766 explanation => 'if set to 1, suggestions are activated in OPAC',
770 uniquefieldrequired => 'variable',
772 forceupdate => { 'explanation' => 1,
774 value => 'Fill with appropriate value...',
775 explanation => 'ISBD',
779 uniquefieldrequired => 'variable',
780 variable => 'virtualshelves',
781 forceupdate => { 'explanation' => 1,
784 explanation => 'Set virtual shelves management ON or OFF',
791 my %fielddefinitions = (
794 field => 'printername',
803 field => 'bookfundid',
812 field => 'aqbudgetid',
813 type => 'tinyint(4)',
817 extra => 'auto_increment'
827 extra => 'auto_increment'
832 field => 'z3950random',
833 type => 'varchar(40)',
841 type => 'varchar(40)',
855 # Get version of MySQL database engine.
856 my $mysqlversion = `mysqld --version`;
857 $mysqlversion =~ /Ver (\S*) /;
859 if ( $mysqlversion ge '3.23' ) {
860 print "Could convert to MyISAM database tables...\n" unless $silent;
863 #---------------------------------
866 # Collect all tables into a list
867 $sth = $dbh->prepare("show tables");
869 while ( my ($table) = $sth->fetchrow ) {
870 $existingtables{$table} = 1;
874 # Now add any missing tables
875 foreach $table ( keys %requiretables ) {
876 unless ( $existingtables{$table} ) {
877 print "Adding $table table...\n" unless $silent;
878 my $sth = $dbh->prepare("create table $table $requiretables{$table}");
881 print "Error : $sth->errstr \n";
887 # now drop useless tables
888 foreach $table ( keys %dropable_table ) {
889 if ( $existingtables{$table} ) {
890 print "Dropping unused table $table\n" if $debug and not $silent;
891 $dbh->do("drop table $table");
893 print "Error : $dbh->errstr \n";
897 unless ( $existingtables{'z3950servers'} ) {
898 #MJR: added syntax entries to close bug 624
899 print "Adding z3950servers table...\n" unless $silent;
900 my $sti = $dbh->prepare( "create table z3950servers (
913 $sti = $dbh->prepare( "insert into z3950servers
914 values ('z3950.loc.gov',
918 'Library of Congress',
923 unless ( $existingtables{'issuingrules'} ) {
924 $dbh->do("alter table categoryitem rename issuingrules");
925 $dbh->do("ALTER TABLE issuingrules ADD maxissueqty int(4) default NULL");
926 $dbh->do("ALTER TABLE issuingrules ADD issuelength int(4) default NULL");
927 $dbh->do("ALTER TABLE issuingrules ADD branchcode varchar(4) NOT NULL default ''");
928 print "renaming categoryitem\n" unless $silent;
932 #---------------------------------
935 foreach $table ( keys %requirefields ) {
936 print "Check table $table\n" if $debug and not $silent;
937 $sth = $dbh->prepare("show columns from $table");
940 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
942 $types{$column} = $type;
944 foreach $column ( keys %{ $requirefields{$table} } ) {
945 print " Check column $column [$types{$column}]\n" if $debug and not $silent;
946 if ( !$types{$column} ) {
948 # column doesn't exist
949 print "Adding $column field to $table table...\n" unless $silent;
950 $query = "alter table $table
951 add column $column " . $requirefields{$table}->{$column};
952 print "Execute: $query\n" if $debug;
953 my $sti = $dbh->prepare($query);
956 print "**Error : $sti->errstr \n";
963 foreach $table ( keys %fielddefinitions ) {
964 print "Check table $table\n" if $debug;
965 $sth = $dbh->prepare("show columns from $table");
968 while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
970 $definitions->{$column}->{type} = $type;
971 $definitions->{$column}->{null} = $null;
972 $definitions->{$column}->{key} = $key;
973 $definitions->{$column}->{default} = $default;
974 $definitions->{$column}->{extra} = $extra;
976 my $fieldrow = $fielddefinitions{$table};
977 foreach my $row (@$fieldrow) {
978 my $field = $row->{field};
979 my $type = $row->{type};
980 my $null = $row->{null};
981 my $key = $row->{key};
982 my $default = $row->{default};
983 $default="''" unless $default;
984 my $extra = $row->{extra};
985 my $def = $definitions->{$field};
986 unless ( $type eq $def->{type}
987 && $null eq $def->{null}
988 && $key eq $def->{key}
989 && $default eq $def->{default}
990 && $extra eq $def->{extra} )
996 if ( $key eq 'PRI' ) {
997 $key = 'PRIMARY KEY';
999 unless ( $extra eq 'auto_increment' ) {
1002 # if it's a new column use "add", if it's an old one, use "change".
1004 if ($definitions->{$field}->{type}) {
1005 $action="change $field"
1009 # if it's a primary key, drop the previous pk, before altering the table
1011 if ($key ne 'PRIMARY KEY') {
1012 $sth =$dbh->prepare("alter table $table $action $field $type $null $key $extra default ?");
1014 $sth =$dbh->prepare("alter table $table drop primary key, $action $field $type $null $key $extra default ?");
1016 $sth->execute($default);
1017 print " Alter $field in $table\n" unless $silent;
1022 # Get list of columns from borrowers table
1025 $sth = $dbh->prepare("show columns from borrowers");
1027 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1029 $itemtypes{$column} = $type;
1030 $nullenabled{$column} = $null;
1033 unless ( $itemtypes{'cardnumber'} eq 'varchar(20)' ) {
1034 $itemtypes{'cardnumber'} =~ /varchar\((\d+)\)/;
1036 if ( $oldlength < 16 ) {
1037 print "Setting maximum cardnumber length to 16 (was $oldlength) and marking unique.\n" unless $silent;
1040 "alter table borrowers change cardnumber cardnumber varchar(16)");
1045 "alter table borrowers drop index cardnumber");
1050 "alter table borrowers add unique(cardnumber)");
1056 # Get list of columns from items table
1057 $sth = $dbh->prepare("show columns from items");
1059 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1061 $itemtypes{$column} = $type;
1062 $nullenabled{$column} = $null;
1065 unless ( $itemtypes{'barcode'} eq 'varchar(20)' ) {
1066 $itemtypes{'barcode'} =~ /varchar\((\d+)\)/;
1068 if ( $oldlength < 20 ) {
1069 print "Setting maximum barcode length to 20 (was $oldlength).\n" unless $silent;
1072 "alter table items change barcode barcode varchar(20)");
1077 # dropping unique barcode index & setting barcode to null allowed.
1079 $sth = $dbh->prepare("show index from items");
1081 while ( my ( $table, $non_unique, $key_name, $Seq_in_index, $Column_name, $Collation, $cardinality, $sub_part, $Packed, $comment ) = $sth->fetchrow )
1083 if ($key_name eq 'barcode' && $non_unique eq 0) {
1084 print "dropping BARCODE index to enable empty barcodes\n" unless $silent;
1085 $dbh->do("ALTER TABLE `items` DROP INDEX `barcode`");
1088 $dbh->do("ALTER TABLE `items` CHANGE `barcode` `barcode` VARCHAR( 20 )") unless ($nullenabled{barcode} eq 'YES');
1091 # creating fulltext index in bibliothesaurus if needed
1093 $sth = $dbh->prepare("show index from bibliothesaurus");
1096 while ( my ( $table, $non_unique, $key_name, $Seq_in_index, $Column_name, $Collation, $cardinality, $sub_part, $Packed, $comment ) = $sth->fetchrow )
1098 if ($key_name eq 'category_2') {
1102 print "Creating fulltext index on bibliothesaurus\n" unless $exists or $silent;
1103 $dbh->do('create fulltext index category_2 on bibliothesaurus (category,freelib)') unless $exists;
1106 # creating index in z3950results if needed
1108 $sth = $dbh->prepare("show index from z3950results");
1111 while ( my ( $table, $non_unique, $key_name, $Seq_in_index, $Column_name, $Collation, $cardinality, $sub_part, $Packed, $comment ) = $sth->fetchrow )
1113 if ($key_name eq 'query_server') {
1117 print "Creating index on z3950results\n" unless $exists or $silent;
1118 $dbh->do('create unique index query_server on z3950results (queryid,server)') unless $exists;
1120 # changing z3950daemon field to NULL in marc_breeding
1121 $dbh->do("ALTER TABLE `marc_breeding` CHANGE `z3950random` `z3950random` VARCHAR( 40 )");
1123 # making borrowernumber an auto_increment field
1124 $dbh->do("ALTER TABLE `borrowers` CHANGE `borrowernumber` `borrowernumber` INTEGER auto_increment");
1126 # changing indexes in marc_*_structure to use frameworkcode
1127 $dbh->do('alter table marc_subfield_structure drop index tab');
1128 $dbh->do('create index tab on marc_subfield_structure (frameworkcode,tab)');
1129 $dbh->do('alter table marc_subfield_structure drop index kohafield');
1130 $dbh->do('create index kohafield on marc_subfield_structure (frameworkcode,kohafield)');
1133 # extending the timestamp in branchtransfers...
1134 my %branchtransfers;
1136 $sth = $dbh->prepare("show columns from branchtransfers");
1138 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1140 $branchtransfers{$column} = $type;
1143 unless ( $branchtransfers{'datesent'} eq 'datetime' ) {
1144 print "Setting type of datesent in branchtransfers to datetime.\n" unless $silent;
1147 "alter table branchtransfers change datesent datesent datetime");
1151 unless ( $branchtransfers{'datearrived'} eq 'datetime' ) {
1152 print "Setting type of datearrived in branchtransfers to datetime.\n" unless $silent;
1155 "alter table branchtransfers change datearrived datearrived datetime");
1159 # changing the branchcategories table around...
1160 my %branchcategories;
1162 $sth = $dbh->prepare("show columns from branchcategories");
1164 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1166 $branchcategories{$column} = $type;
1169 unless ( $branchcategories{'categorycode'} eq 'varchar(4)' ) {
1171 "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n" unless $silent;
1174 "alter table branchcategories change categorycode categorycode varchar(4) not null"
1179 "alter table branchcategories add primary key (categorycode)");
1183 unless ( $branchcategories{'categoryname'} eq 'text' ) {
1184 print "Changing branchcode in branchcategories to categoryname text.\n" unless $silent;
1187 "alter table branchcategories change branchcode categoryname text");
1191 unless ( $branchcategories{'codedescription'} eq 'text' ) {
1193 "Replacing branchholding in branchcategories with codedescription text.\n" unless $silent;
1196 "alter table branchcategories change branchholding codedescription text"
1201 # changing the items table around...
1204 $sth = $dbh->prepare("show columns from items");
1206 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1208 $items{$column} = $type;
1211 if ($items{'bulk'} eq "varchar(30)") {
1212 print " Setting callnumber in items table\n" unless $silent;
1214 $dbh->prepare("ALTER TABLE `items` CHANGE `bulk` `itemcallnumber` VARCHAR( 30 ) DEFAULT NULL");
1216 $sti = $dbh->prepare("update marc_subfield_structure set kohafield=\"items.itemcallnumber\" where kohafield=\"items.bulk\"");
1220 # changing the marc_subfield_structure table around...
1221 my %marc_subfield_structure;
1223 $sth = $dbh->prepare("show columns from marc_subfield_structure");
1225 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1227 $marc_subfield_structure{$column} = $type;
1230 if ($marc_subfield_structure{thesaurus_category}) {
1231 print " changing thesaurus_category in marc_subfield_structure table\n" unless $silent;
1233 $dbh->prepare("ALTER TABLE marc_subfield_structure CHANGE `thesaurus_category` `authtypecode` VARCHAR(10 ) DEFAULT NULL");
1238 # creating index in issuingrules if needed
1240 $sth = $dbh->prepare("show index from issuingrules");
1243 while ( my ( $table, $non_unique, $key_name, $Seq_in_index, $Column_name, $Collation, $cardinality, $sub_part, $Packed, $comment ) = $sth->fetchrow )
1245 if ($key_name eq 'PRIMARY') {
1249 print "Creating index on issuing rules\n" unless $exists or $silent;
1250 $dbh->do('ALTER TABLE issuingrules ADD PRIMARY KEY ( branchcode, categorycode, itemtype )') unless $exists;
1252 $dbh->do('ALTER TABLE marc_tag_structure drop primary key');
1253 $dbh->do('ALTER TABLE marc_tag_structure ADD PRIMARY KEY ( frameworkcode, tagfield )');
1255 $dbh->do('ALTER TABLE marc_subfield_structure drop primary key');
1256 $dbh->do('ALTER TABLE marc_subfield_structure ADD PRIMARY KEY ( frameworkcode, tagfield, tagsubfield )');
1258 # Get list of columns from marc_word table
1261 $sth = $dbh->prepare("show columns from marc_word");
1263 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1265 $marc_word{$column} = $type;
1266 $nullenabled{$column} = $null;
1268 if ($marc_word{subfieldid}) {
1269 #create field tagsubfield, copy tag+subfieldid, then drop tag and subfieldid
1270 print "Modifying marc_word (concat on tag and subfield for better perfs)\n" unless $silent;
1271 $dbh->do("ALTER TABLE `marc_word` ADD `tagsubfield` CHAR( 4 ) NOT NULL AFTER `bibid`");
1272 $dbh->do("update marc_word set tagsubfield=concat(tag,subfieldid)");
1273 $dbh->do("alter table marc_word drop tag");
1274 $dbh->do("alter table marc_word drop subfieldid");
1275 $dbh->do("create index Search_Marc on marc_word (tagsubfield,word)");
1277 # Populate tables with required data
1279 # fill aqbasket if it's empty and aqorder is not
1280 # => it means it has just been created & must be filled
1281 $sth = $dbh->prepare("select count(*) from aqbasket");
1283 if ($sth->fetchrow == 0) {
1284 $sth = $dbh->prepare("select count(*) from aqorders");
1286 if ($sth->fetchrow >0) {
1287 print "Populating new table aqbasket\n";
1288 print "IMPORTANT NOTE: error message \"Duplicate entry 'X' for key 1\" may appear. it should not be a real trouble\n";
1289 $sth=$dbh->prepare("select distinct basketno,booksellerid,authorisedby,entrydate,booksellerinvoicenumber from aqorders");
1291 my ($basketno,$booksellerid,$authorisedby,$entrydate,$booksellerinvoicenumber);
1292 my $sth2 = $dbh->prepare("insert into aqbasket (basketno,creationdate,booksellerid,authorisedby,booksellerinvoicenumber) values (?,?,?,?,?)");
1293 while (($basketno,$booksellerid,$authorisedby,$entrydate,$booksellerinvoicenumber) = $sth->fetchrow) {
1294 print "$basketno,$entrydate,$booksellerid,$authorisedby,$booksellerinvoicenumber\n";
1295 $sth2->execute($basketno,$entrydate,$booksellerid,$authorisedby,$booksellerinvoicenumber);
1299 foreach my $table ( keys %tabledata ) {
1300 print "Checking for data required in table $table...\n" unless $silent;
1301 my $tablerows = $tabledata{$table};
1302 foreach my $row (@$tablerows) {
1303 my $uniquefieldrequired = $row->{uniquefieldrequired};
1304 my $uniquevalue = $row->{$uniquefieldrequired};
1305 my $forceupdate = $row->{forceupdate};
1308 "select $uniquefieldrequired from $table where $uniquefieldrequired=?"
1310 $sth->execute($uniquevalue);
1312 foreach my $field (keys %$forceupdate) {
1313 if ($forceupdate->{$field}) {
1314 my $sth=$dbh->prepare("update systempreferences set $field=? where $uniquefieldrequired=?");
1315 $sth->execute($row->{$field}, $uniquevalue);
1319 print "Adding row to $table: " unless $silent;
1323 foreach my $field ( keys %$row ) {
1324 next if $field eq 'uniquefieldrequired';
1325 next if $field eq 'forceupdate';
1326 my $value = $row->{$field};
1327 push @values, $value;
1328 print " $field => $value" unless $silent;
1329 $fieldlist .= "$field,";
1330 $placeholders .= "?,";
1332 print "\n" unless $silent;
1333 $fieldlist =~ s/,$//;
1334 $placeholders =~ s/,$//;
1337 "insert into $table ($fieldlist) values ($placeholders)");
1338 $sth->execute(@values);
1343 # at last, remove useless fields
1344 foreach $table ( keys %uselessfields ) {
1345 my @fields = split /,/,$uselessfields{$table};
1348 foreach my $fieldtodrop (@fields) {
1349 $fieldtodrop =~ s/\t//g;
1350 $fieldtodrop =~ s/\n//g;
1352 $sth = $dbh->prepare("show columns from $table");
1354 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
1356 $exists =1 if ($column eq $fieldtodrop);
1359 print "deleting $fieldtodrop field in $table...\n" unless $silent;
1360 my $sth = $dbh->prepare("alter table $table drop $fieldtodrop");
1372 # Revision 1.94 2004/09/06 10:00:29 tipaul
1373 # adding a "location" field to the library.
1374 # This field is useful when the callnumber contains no information on the room where the item is stored.
1375 # With this field, we now have 3 levels of informations to find a book :
1380 # This should be versatile enough to solve any storing method.
1381 # This hack is quite simple, due to the nice Biblio.pm API. The MARC => koha db link is automatically managed. Just add the link in the parameters section.
1383 # Revision 1.93 2004/08/12 14:50:50 tipaul
1386 # Revision 1.92 2004/08/06 16:38:42 tipaul
1387 # changing DB structure to calculate next issue number.
1388 # Seems to work fine.
1390 # Still misses the date calculation & the test of end of subscription (maybe for monday ?)
1392 # Revision 1.91 2004/07/15 09:52:28 tipaul
1393 # Acquisition & Suggestion :
1394 # * acquisition rewritte : create a aqbasket table to deal with "bookseller order header".
1395 # * add "close basket" feature : a closed basket can't be modified
1396 # * suggestion feature : manage suggestions in acquisition (after suggestion filled in OPAC)
1398 # Revision 1.90 2004/07/06 08:24:18 tipaul
1399 # adding 2 free fields that can be used for sorting purposes
1401 # Revision 1.89 2004/07/02 15:55:08 tipaul
1402 # Adding 2 new fields, called "sort1" and "sort2"
1403 # They can be used for sorting & statistics reasons by the library.
1405 # Revision 1.88 2004/06/26 23:34:26 rangi
1408 # Revision 1.87 2004/06/23 13:03:09 tipaul
1409 # fixes in DB structure
1411 # Revision 1.86 2004/06/22 11:30:57 tipaul
1412 # adding -s (silent) flag, to have a silent install.
1413 # only updater will be verbose
1415 # Revision 1.85 2004/06/17 15:19:44 tipaul
1416 # missing Marc_Search index on marc_word
1418 # Revision 1.84 2004/06/17 08:25:21 tipaul
1419 # DB modifs : merging tag & subfield in marc_word table
1421 # Revision 1.83 2004/06/10 08:32:02 tipaul
1422 # MARC authority management (continued)
1424 # Revision 1.82 2004/06/03 12:46:58 tipaul
1425 # * frameworks and itemtypes are independant
1427 # WARNING : will work only if applied to a 2.0 base. some modifs have been done since last commit that will NOT be applied if you run updatedatabase again.
1429 # Revision 1.81 2004/05/28 09:56:21 tipaul
1432 # Revision 1.80 2004/05/28 08:32:00 tipaul
1434 # * MARC authority file
1435 # * seealso & hidden in MARC biblio structure.
1437 # Revision 1.79 2004/05/18 09:50:07 tipaul
1438 # *** empty log message ***
1440 # Revision 1.78 2004/05/10 09:29:33 tipaul
1441 # css is now the default theme for OPAC.
1442 # It will be the theme used for improvements and new things in OPAC.
1444 # Revision 1.77 2004/05/06 14:56:51 tipaul
1445 # adding table issuingrules (previously called categoryitem
1447 # Revision 1.76 2004/05/03 09:32:25 tipaul
1448 # adding printcirculationsplit parameter (already existed, but was not in systempref by defaul)
1450 # Revision 1.75 2004/04/14 19:49:00 tipaul
1451 # seealso field set to 255 chars
1453 # Revision 1.74 2004/03/11 16:10:16 tipaul
1454 # *** empty log message ***
1456 # Revision 1.73 2004/03/06 20:26:13 tipaul
1457 # adding seealso feature in MARC searches