Initial fix to bug 456: hardwired paths
[koha.git] / updater / updatedatabase
1 #!/usr/bin/perl
2
3 # $Id$
4
5 # Database Updater
6 # This script checks for required updates to the database.
7
8 # Part of the Koha Library Software www.koha.org
9 # Licensed under the GPL.
10
11 # Bugs/ToDo:
12 # - Would also be a good idea to offer to do a backup at this time...
13
14 # NOTE:  If you do something more than once in here, make it table driven.
15
16 use strict;
17
18 # CPAN modules
19 use DBI;
20
21 # Koha modules
22 use C4::Context;
23
24 # FIXME - The user might be installing a new database, so can't rely
25 # on /etc/koha.conf anyway.
26
27 my $debug = 0;
28
29 my (
30     $sth, $sti,
31     $query,
32     %existingtables,    # tables already in database
33     %types,
34     $table,
35     $column,
36     $type, $null, $key, $default, $extra,
37     $prefitem,          # preference item in systempreferences table
38 );
39
40 my $dbh = C4::Context->dbh;
41
42 #-------------------
43 # Defines
44
45 # Tables to add if they don't exist
46 my %requiretables = (
47     shelfcontents => "( shelfnumber int not null,
48                                                         itemnumber int not null,
49                                                         flags int)",
50     bookshelf => "( shelfnumber int auto_increment primary key,
51                                                 shelfname char(255))",
52     z3950queue => "( id int auto_increment primary key,
53                                                 term text,
54                                                 type char(10),
55                                                 startdate int,
56                                                 enddate int,
57                                                 done smallint,
58                                                 results longblob,
59                                                 numrecords int,
60                                                 servers text,
61                                                 identifier char(30))",
62     z3950results => "( id int auto_increment primary key,
63                                                 queryid int,
64                                                 server char(255),
65                                                 startdate int,
66                                                 enddate int,
67                                                 results longblob,
68                                                 numrecords int,
69                                                 numdownloaded int,
70                                                 highestseen int,
71                                                 active smallint)",
72     branchrelations => "( branchcode varchar(4),
73                                                         categorycode varchar(4))",
74     websites => "( websitenumber int(11) NOT NULL auto_increment,
75                                                 biblionumber int(11) NOT NULL default '0',
76                                                 title text,
77                                                 description text,
78                                                 url varchar(255),
79                                                 PRIMARY KEY (websitenumber) )",
80     marcrecorddone => "( isbn char(40),
81                                                                 issn char(40),
82                                                                 lccn char(40),
83                                                                 controlnumber char(40))",
84     uploadedmarc => "( id int(11) NOT NULL auto_increment PRIMARY KEY,
85                                                         marc longblob,
86                                                         hidden smallint(6) default NULL,
87                                                         name varchar(255) default NULL)",
88     ethnicity => "( code varchar(10) NOT NULL default '',
89                                         name varchar(255) default NULL,
90                                         PRIMARY KEY  (code)   )",
91     sessions => "( sessionID varchar(255) NOT NULL default '',
92                                                 userid varchar(255) default NULL,
93                                                 ip varchar(16) default NULL,
94                                                 lasttime int,
95                                                 PRIMARY KEY (sessionID)   )",
96     sessionqueries => "( sessionID varchar(255) NOT NULL default '',
97                                                                 userid char(100) NOT NULL default '',
98                                                                 ip char(18) NOT NULL default '',
99                                                                 url text NOT NULL default ''  )",
100     bibliothesaurus => "( id bigint(20) NOT NULL auto_increment,
101                                                         freelib char(255) NOT NULL default '',
102                                                         stdlib char(255) NOT NULL default '',
103                                                         category char(10) NOT NULL default '',
104                                                         level tinyint(4) NOT NULL default '1',
105                                                         hierarchy char(80) NOT NULL default '',
106                                                         father bigint(20) NOT NULL default '',
107                                                         PRIMARY KEY  (id),
108                                                         KEY freelib (freelib),
109                                                         KEY stdlib (stdlib),
110                                                         KEY category (category),
111                                                         KEY hierarchy (hierarchy)
112                                                         )",
113     marc_biblio => "(
114                                                 bibid bigint(20) unsigned NOT NULL auto_increment,
115                                                 biblionumber int(11) NOT NULL default '0',
116                                                 datecreated date NOT NULL default '0000-00-00',
117                                                 datemodified date default NULL,
118                                                 origincode char(20) default NULL,
119                                                 PRIMARY KEY  (bibid),
120                                                 KEY origincode (origincode),
121                                                 KEY biblionumber (biblionumber)
122                                                 ) ",
123     marc_blob_subfield => "(
124                                         blobidlink bigint(20) NOT NULL auto_increment,
125                                         subfieldvalue longtext NOT NULL,
126                                         PRIMARY KEY  (blobidlink)
127                                         ) ",
128     marc_subfield_structure => "(
129                                                 tagfield char(3) NOT NULL default '',
130                                                 tagsubfield char(1) NOT NULL default '',
131                                                 liblibrarian char(255) NOT NULL default '',
132                                                 libopac char(255) NOT NULL default '',
133                                                 repeatable tinyint(4) NOT NULL default '0',
134                                                 mandatory tinyint(4) NOT NULL default '0',
135                                                 kohafield char(40)  default NULL,
136                                                 tab tinyint(1) default NULL,
137                                                 authorised_value char(10) default NULL,
138                                                 thesaurus_category char(10) default NULL,
139                                                 value_builder char(80) default NULL,
140                                                 PRIMARY KEY  (tagfield,tagsubfield),
141                                                 KEY kohafield (kohafield),
142                                                 KEY tab (tab)
143                                                 )",
144     marc_subfield_table => "(
145                                                 subfieldid bigint(20) unsigned NOT NULL auto_increment,
146                                                 bibid bigint(20) unsigned NOT NULL default '0',
147                                                 tag char(3) NOT NULL default '',
148                                                 tagorder tinyint(4) NOT NULL default '1',
149                                                 tag_indicator char(2) NOT NULL default '',
150                                                 subfieldcode char(1) NOT NULL default '',
151                                                 subfieldorder tinyint(4) NOT NULL default '1',
152                                                 subfieldvalue varchar(255) default NULL,
153                                                 valuebloblink bigint(20) default NULL,
154                                                 PRIMARY KEY  (subfieldid),
155                                                 KEY bibid (bibid),
156                                                 KEY tag (tag),
157                                                 KEY tag_indicator (tag_indicator),
158                                                 KEY subfieldorder (subfieldorder),
159                                                 KEY subfieldcode (subfieldcode),
160                                                 KEY subfieldvalue (subfieldvalue),
161                                                 KEY tagorder (tagorder)
162                                         )",
163     marc_tag_structure => "(
164                                         tagfield char(3) NOT NULL default '',
165                                         liblibrarian char(255) NOT NULL default '',
166                                         libopac char(255) NOT NULL default '',
167                                         repeatable tinyint(4) NOT NULL default '0',
168                                         mandatory tinyint(4) NOT NULL default '0',
169                                         authorised_value char(10) default NULL,
170                                         PRIMARY KEY  (tagfield)
171                                         )",
172     marc_word => "(
173                                 bibid bigint(20) NOT NULL default '0',
174                                 tag char(3) NOT NULL default '',
175                                 tagorder tinyint(4) NOT NULL default '1',
176                                 subfieldid char(1) NOT NULL default '',
177                                 subfieldorder tinyint(4) NOT NULL default '1',
178                                 word varchar(255) NOT NULL default '',
179                                 sndx_word varchar(255) NOT NULL default '',
180                                 KEY bibid (bibid),
181                                 KEY tag (tag),
182                                 KEY tagorder (tagorder),
183                                 KEY subfieldid (subfieldid),
184                                 KEY subfieldorder (subfieldorder),
185                                 KEY word (word),
186                                 KEY sndx_word (sndx_word)
187                         )",
188     marc_breeding => "(  id bigint(20) NOT NULL auto_increment,
189                                 file varchar(80) NOT NULL default '',
190                                 isbn varchar(10) NOT NULL default '',
191                                 title varchar(128) default NULL,
192                                 author varchar(80) default NULL,
193                                 marc text NOT NULL,
194                                 encoding varchar(40) default NULL,
195                                 PRIMARY KEY  (id),
196                                 KEY title (title),
197                                 KEY isbn (isbn)
198                         )",
199     authorised_values => "(id int(11) NOT NULL auto_increment,
200                                 category char(10) NOT NULL default '',
201                                 authorised_value char(80) NOT NULL default '',
202                                 lib char(80) NULL,
203                                 PRIMARY KEY  (id),
204                                 KEY name (category)
205                         )",
206     userflags => "( bit int(11) NOT NULL default '0',
207                                 flag char(30), flagdesc char(255),
208                                 defaulton int(11)
209                         )",
210 );
211
212 my %requirefields = (
213     biblio        => { 'abstract' => 'text' },
214     deletedbiblio => { 'abstract' => 'text' },
215     biblioitems   => {
216         'lccn' => 'char(25)',
217         'url'  => 'varchar(255)',
218         'marc' => 'text'
219     },
220     deletedbiblioitems => {
221         'lccn' => 'char(25)',
222         'url'  => 'varchar(255)',
223         'marc' => 'text'
224     },
225     branchtransfers => { 'datearrived'    => 'datetime' },
226     statistics      => { 'borrowernumber' => 'int(11)' },
227     aqbooksellers   => {
228         'invoicedisc' => 'float(6,4)',
229         'nocalc'      => 'int(11)'
230     },
231     borrowers => {
232         'userid'        => 'char(30)',
233         'password'      => 'char(30)',
234         'flags'         => 'int(11)',
235         'textmessaging' => 'varchar(30)'
236     },
237     aqorders => { 'budgetdate' => 'date' },
238
239     #added so that reference items are not available for reserves...
240     itemtypes         => { 'notforloan'  => 'smallint(6)' },
241     systempreferences => { 'explanation' => 'char(80)' },
242     z3950servers      => { 'syntax'      => 'char(80)' },
243     borrowers => {'zipcode' => 'varchar(25)',
244                                                 'homezipcode' => 'varchar(25)'}
245 );
246
247 my %dropable_table = (
248     classification => 'classification',
249     multipart      => 'multipart',
250     multivolume    => 'multivolume',
251     newitems       => 'newitems',
252     procedures     => 'procedures',
253     publisher      => 'publisher',
254     searchstats    => 'searchstats',
255     serialissues   => 'serialissues',
256 );
257
258 # The tabledata hash contains data that should be in the tables.
259 # The uniquefieldrequired hash entry is used to determine which (if any) fields
260 # must not exist in the table for this row to be inserted.  If the
261 # uniquefieldrequired entry is already in the table, the existing data is not
262 # modified.
263
264 my %tabledata = (
265     userflags => [
266         {
267             uniquefieldrequired => 'bit',
268             bit                 => 0,
269             flag                => 'superlibrarian',
270             flagdesc            => 'Access to all librarian functions',
271             defaulton           => 0
272         },
273         {
274             uniquefieldrequired => 'bit',
275             bit                 => 1,
276             flag                => 'circulate',
277             flagdesc            => 'Circulate books',
278             defaulton           => 0
279         },
280         {
281             uniquefieldrequired => 'bit',
282             bit                 => 2,
283             flag                => 'catalogue',
284             flagdesc            => 'View Catalogue (Librarian Interface)',
285             defaulton           => 0
286         },
287         {
288             uniquefieldrequired => 'bit',
289             bit                 => 3,
290             flag                => 'parameters',
291             flagdesc            => 'Set Koha system paramters',
292             defaulton           => 0
293         },
294         {
295             uniquefieldrequired => 'bit',
296             bit                 => 4,
297             flag                => 'borrowers',
298             flagdesc            => 'Add or modify borrowers',
299             defaulton           => 0
300         },
301         {
302             uniquefieldrequired => 'bit',
303             bit                 => 5,
304             flag                => 'permissions',
305             flagdesc            => 'Set user permissions',
306             defaulton           => 0
307         },
308         {
309             uniquefieldrequired => 'bit',
310             bit                 => 6,
311             flag                => 'reserveforothers',
312             flagdesc            => 'Reserve books for patrons',
313             defaulton           => 0
314         },
315         {
316             uniquefieldrequired => 'bit',
317             bit                 => 7,
318             flag                => 'borrow',
319             flagdesc            => 'Borrow books',
320             defaulton           => 1
321         },
322         {
323             uniquefieldrequired => 'bit',
324             bit                 => 8,
325             flag                => 'reserveforself',
326             flagdesc            => 'Reserve books for self',
327             defaulton           => 0
328         },
329         {
330             uniquefieldrequired => 'bit',
331             bit                 => 9,
332             flag                => 'editcatalogue',
333             flagdesc  => 'Edit Catalogue (Modify bibliographic/holdings data)',
334             defaulton => 0
335         },
336         {
337             uniquefieldrequired => 'bit',
338             bit                 => 10,
339             flag                => 'updatecharges',
340             flagdesc            => 'Update borrower charges',
341             defaulton           => 0
342         },
343     ],
344     systempreferences => [
345         {
346             uniquefieldrequired => 'variable',
347             variable            => 'autoMemberNum',
348             value               => '1',
349             explanation         => '1 or 0. If 1, Member number is auto-calculated'
350         },
351         {
352             uniquefieldrequired => 'variable',
353             variable            => 'acquisitions',
354             value               => 'simple',
355             explanation         =>
356 'normal or simple : whether to use "acqui" or "acqui.simple" acquisition system'
357         },
358         {
359             uniquefieldrequired => 'variable',
360             variable            => 'dateformat',
361             value               => 'metric',
362             explanation         => 'metric, us, or iso'
363         },
364         {
365             uniquefieldrequired => 'variable',
366             variable            => 'template',
367             value               => 'default',
368             explanation         => 'template default name'
369         },
370         {
371             uniquefieldrequired => 'variable',
372             variable            => 'autoBarcode',
373             value               => '1',
374             explanation         => '1 or 0. If 1, Barcode is auto-calculated'
375         },
376         {
377             uniquefieldrequired => 'variable',
378             variable            => 'insecure',
379             value               => 'NO',
380             explanation         =>
381 'if YES, no auth at all is needed. Be careful if you set this to yes !'
382         },
383         {
384             uniquefieldrequired => 'variable',
385             variable            => 'authoritysep',
386             value               => '--',
387             explanation         =>
388             'the separator used in authority/thesaurus. Usually --'
389         },
390         {
391             uniquefieldrequired => 'variable',
392             variable            => 'opaclanguages',
393             value               => 'en',
394             explanation         => 'languages'
395         },
396         {
397             uniquefieldrequired => 'variable',
398             variable            => 'opacthemes',
399             value               => 'default',
400             explanation         => 'theme'
401         },
402         {
403             uniquefieldrequired => 'variable',
404             variable            => 'timeout',
405             value               => '12000000',
406             explanation         => 'login timeout'
407         },
408         {
409             uniquefieldrequired => 'variable',
410             variable            => 'marc',
411             value               => 'ON',
412             explanation         => 'MARC support (ON or OFF)'
413         },
414         {
415             uniquefieldrequired => 'variable',
416             variable            => 'marcflavour',
417             value               => 'MARC21',
418             explanation         =>
419             'your MARC flavor (MARC21 or UNIMARC) used for character encoding'
420         },
421         {
422             uniquefieldrequired => 'variable',
423             variable            => 'checkdigit',
424             value               => 'katipo',
425             explanation         =>
426             'none= no check on member cardnumber. katipo= katipo check'
427         },
428         {
429             uniquefieldrequired => 'variable',
430             variable            => 'dateformat',
431             value               => 'ISO',
432             explanation         =>
433             'date format (US mm/dd/yyyy, metric dd/mm/yyy, ISO yyyy/mm/dd) '
434         },
435         {
436             uniquefieldrequired => 'variable',
437             variable            => 'maxoutstanding',
438             value               => '5',
439             explanation         =>
440             'maximum amount withstanding to be able make reserves '
441         },
442         {
443             uniquefieldrequired => 'variable',
444             variable            => 'maxreserves',
445             value               => '5',
446             explanation         =>
447             'maximum number of reserves a member can make '
448         },
449         {
450             uniquefieldrequired => 'variable',
451             variable            => 'noissuescharge',
452             value               => '5',
453             explanation         =>
454             'maximum amount withstanding to be able to check out an item '
455         },
456         {
457             uniquefieldrequired => 'variable',
458             variable            => 'KohaAdminEmailAddress',
459             value               => 'your.mail@here',
460             explanation => 'the email adress where borrowers modifs are sent'
461         },
462     ],
463
464 );
465
466 my %fielddefinitions = (
467     printers => [
468         {
469             field   => 'printername',
470             type    => 'char(40)',
471             null    => '',
472             key     => 'PRI',
473             default => ''
474         },
475     ],
476     aqbookfund => [
477         {
478             field   => 'bookfundid',
479             type    => 'char(5)',
480             null    => '',
481             key     => 'PRI',
482             default => ''
483         },
484     ],
485     z3950servers => [
486         {
487             field   => 'id',
488             type    => 'int',
489             null    => '',
490             key     => 'PRI',
491             default => '',
492             extra   => 'auto_increment'
493         },
494     ],
495 );
496
497 #-------------------
498 # Initialize
499
500 # Start checking
501
502 # Get version of MySQL database engine.
503 my $mysqlversion = `mysqld --version`;
504 $mysqlversion =~ /Ver (\S*) /;
505 $mysqlversion = $1;
506 if ( $mysqlversion ge '3.23' ) {
507     print "Could convert to MyISAM database tables...\n";
508 }
509
510 #---------------------------------
511 # Tables
512
513 # Collect all tables into a list
514 $sth = $dbh->prepare("show tables");
515 $sth->execute;
516 while ( my ($table) = $sth->fetchrow ) {
517     $existingtables{$table} = 1;
518 }
519
520 # Now add any missing tables
521 foreach $table ( keys %requiretables ) {
522     print "Checking $table table...\n" if $debug;
523     unless ( $existingtables{$table} ) {
524         print "Adding $table table...\n";
525         my $sth = $dbh->prepare("create table $table $requiretables{$table}");
526         $sth->execute;
527         if ( $sth->err ) {
528             print "Error : $sth->errstr \n";
529             $sth->finish;
530         }    # if error
531     }    # unless exists
532 }    # foreach
533
534 # now drop useless tables
535 foreach $table ( keys %dropable_table ) {
536     print "Dropping unused tables...\n" if $debug;
537     if ( $existingtables{$table} ) {
538         $dbh->do("drop table $table");
539         if ( $dbh->err ) {
540             print "Error : $dbh->errstr \n";
541         }
542     }
543 }
544 unless ( $existingtables{'z3950servers'} ) {
545     print "Adding z3950servers table...\n";
546     my $sti = $dbh->prepare( "create table z3950servers (
547                                                                                 host char(255),
548                                                                                 port int,
549                                                                                 db char(255),
550                                                                                 userid char(255),
551                                                                                 password char(255),
552                                                                                 name text,
553                                                                                 id int,
554                                                                                 checked smallint,
555                                                                                 rank int)"
556     );
557     $sti->execute;
558     $sti = $dbh->prepare( "insert into z3950servers
559                                                                 values ('z3950.loc.gov',
560                                                                 7090,
561                                                                 'voyager',
562                                                                 '', '',
563                                                                 'Library of Congress',
564                                                                 1, 1, 1)"
565     );
566     $sti->execute;
567 }
568
569 #---------------------------------
570 # Columns
571
572 foreach $table ( keys %requirefields ) {
573     print "Check table $table\n" if $debug;
574     $sth = $dbh->prepare("show columns from $table");
575     $sth->execute();
576     undef %types;
577     while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
578     {
579         $types{$column} = $type;
580     }    # while
581     foreach $column ( keys %{ $requirefields{$table} } ) {
582         print "  Check column $column\n" if $debug;
583         if ( !$types{$column} ) {
584
585             # column doesn't exist
586             print "Adding $column field to $table table...\n";
587             $query = "alter table $table
588                         add column $column " . $requirefields{$table}->{$column};
589             print "Execute: $query\n" if $debug;
590             my $sti = $dbh->prepare($query);
591             $sti->execute;
592             if ( $sti->err ) {
593                 print "**Error : $sti->errstr \n";
594                 $sti->finish;
595             }    # if error
596         }    # if column
597     }    # foreach column
598 }    # foreach table
599
600 foreach $table ( keys %fielddefinitions ) {
601     print "Check table $table\n" if $debug;
602     $sth = $dbh->prepare("show columns from $table");
603     $sth->execute();
604     my $definitions;
605     while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
606     {
607         $definitions->{$column}->{type}    = $type;
608         $definitions->{$column}->{null}    = $null;
609         $definitions->{$column}->{key}     = $key;
610         $definitions->{$column}->{default} = $default;
611         $definitions->{$column}->{extra}   = $extra;
612     }    # while
613     my $fieldrow = $fielddefinitions{$table};
614     foreach my $row (@$fieldrow) {
615         my $field   = $row->{field};
616         my $type    = $row->{type};
617         my $null    = $row->{null};
618         my $key     = $row->{key};
619         my $default = $row->{default};
620         my $extra   = $row->{extra};
621         my $def     = $definitions->{$field};
622         unless ( $type eq $def->{type}
623             && $null eq $def->{null}
624             && $key eq $def->{key}
625             && $default eq $def->{default}
626             && $extra eq $def->{extra} )
627         {
628
629             if ( $null eq '' ) {
630                 $null = 'NOT NULL';
631             }
632             if ( $key eq 'PRI' ) {
633                 $key = 'PRIMARY KEY';
634             }
635             unless ( $extra eq 'auto_increment' ) {
636                 $extra = '';
637             }
638             my $sth =
639               $dbh->prepare(
640 "alter table $table change $field $field $type $null $key $extra default ?"
641             );
642             $sth->execute($default);
643             print "  Alter $field in $table\n";
644         }
645     }
646 }
647
648 # Get list of columns from items table
649 my %itemtypes;
650
651 $sth = $dbh->prepare("show columns from items");
652 $sth->execute;
653 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
654 {
655     $itemtypes{$column} = $type;
656 }
657
658 unless ( $itemtypes{'barcode'} eq 'varchar(20)' ) {
659     $itemtypes{'barcode'} =~ /varchar\((\d+)\)/;
660     my $oldlength = $1;
661     if ( $oldlength < 20 ) {
662         print "Setting maximum barcode length to 20 (was $oldlength).\n";
663         my $sti =
664           $dbh->prepare(
665             "alter table items change barcode barcode varchar(20) not null");
666         $sti->execute;
667     }
668 }
669
670 # extending the timestamp in branchtransfers...
671 my %branchtransfers;
672
673 $sth = $dbh->prepare("show columns from branchtransfers");
674 $sth->execute;
675 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
676 {
677     $branchtransfers{$column} = $type;
678 }
679
680 unless ( $branchtransfers{'datesent'} eq 'datetime' ) {
681     print "Setting type of datesent in branchtransfers to datetime.\n";
682     my $sti =
683       $dbh->prepare(
684         "alter table branchtransfers change datesent datesent datetime");
685     $sti->execute;
686 }
687
688 unless ( $branchtransfers{'datearrived'} eq 'datetime' ) {
689     print "Setting type of datearrived in branchtransfers to datetime.\n";
690     my $sti =
691       $dbh->prepare(
692         "alter table branchtransfers change datearrived datearrived datetime");
693     $sti->execute;
694 }
695
696 # changing the branchcategories table around...
697 my %branchcategories;
698
699 $sth = $dbh->prepare("show columns from branchcategories");
700 $sth->execute;
701 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
702 {
703     $branchcategories{$column} = $type;
704 }
705
706 unless ( $branchcategories{'categorycode'} eq 'varchar(4)' ) {
707     print
708 "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
709     my $sti =
710       $dbh->prepare(
711 "alter table branchcategories change categorycode categorycode varchar(4) not null"
712     );
713     $sti->execute;
714     $sti =
715       $dbh->prepare(
716         "alter table branchcategories add primary key (categorycode)");
717     $sti->execute;
718 }
719
720 unless ( $branchcategories{'categoryname'} eq 'text' ) {
721     print "Changing branchcode in branchcategories to categoryname text.\n";
722     my $sth =
723       $dbh->prepare(
724         "alter table branchcategories change branchcode categoryname text");
725     $sth->execute;
726 }
727
728 unless ( $branchcategories{'codedescription'} eq 'text' ) {
729     print
730 "Replacing branchholding in branchcategories with codedescription text.\n";
731     my $sth =
732       $dbh->prepare(
733         "alter table branchcategories change branchholding codedescription text"
734     );
735     $sth->execute;
736 }
737
738 # Populate tables with required data
739
740 foreach my $table ( keys %tabledata ) {
741     print "Checking for data required in table $table...\n";
742     my $tablerows = $tabledata{$table};
743     foreach my $row (@$tablerows) {
744         my $uniquefieldrequired = $row->{uniquefieldrequired};
745         my $uniquevalue         = $row->{$uniquefieldrequired};
746         my $sth                 =
747           $dbh->prepare(
748 "select $uniquefieldrequired from $table where $uniquefieldrequired=?"
749         );
750         $sth->execute($uniquevalue);
751         unless ( $sth->rows ) {
752             print "Adding row to $table: ";
753             my @values;
754             my $fieldlist;
755             my $placeholders;
756             foreach my $field ( keys %$row ) {
757                 (next) if ( $field eq 'uniquefieldrequired' );
758                 my $value = $row->{$field};
759                 push @values, $value;
760                 print "  $field => $value";
761                 $fieldlist .= "$field,";
762                 $placeholders .= "?,";
763             }
764             print "\n";
765             $fieldlist    =~ s/,$//;
766             $placeholders =~ s/,$//;
767             my $sth =
768               $dbh->prepare(
769                 "insert into $table ($fieldlist) values ($placeholders)");
770             $sth->execute(@values);
771         }
772     }
773 }
774
775 $sth->finish;
776
777 exit;
778
779 # $Log$
780 # Revision 1.48  2003/05/20 19:50:45  slef
781 # Initial fix to bug 456: hardwired paths
782 #
783 # Revision 1.47  2003/05/15 12:23:33  tipaul
784 # adding zipcode and homezipcode into borrowers table (bug #246
785 #
786 # Revision 1.46  2003/05/08 12:48:24  wolfpac444
787 # Added "noissuescharge" parameter
788 #
789 # Revision 1.45  2003/05/08 12:26:16  wolfpac444
790 # Bug fixes
791 #
792 # Revision 1.44  2003/05/03 05:39:57  rangi
793 # Fixing bug 429
794 # (Wording changes in the explanation fields in system preferences)
795 #
796 # Revision 1.43  2003/05/02 23:01:09  rangi
797 # Adding the textmessaging column to the borrowers table.
798 # insertdata.pl is expecting this to exist, and hence modifying/adding
799 # borrowers was broken.
800 #
801 # Also ran they script thru perltidy
802 #
803 # Revision 1.42  2003/04/29 16:53:25  tipaul
804 # really proud of this commit :-)
805 # z3950 search and import seems to works fine.
806 # Let me explain how :
807 # * a "search z3950" button is added in the addbiblio template.
808 # * when clicked, a popup appears and z3950/search.pl is called
809 # * z3950/search.pl calls addz3950search in the DB
810 # * the z3950 daemon retrieve the records and stores them in z3950results AND in marc_breeding table.
811 # * as long as there as searches pending, the popup auto refresh every 2 seconds, and says how many searches are pending.
812 # * when the user clicks on a z3950 result => the parent popup is called with the requested biblio, and auto-filled
813 #
814 # Note :
815 # * character encoding support : (It's a nightmare...) In the z3950servers table, a "encoding" column has been added. You can put "UNIMARC" or "USMARC" in this column. Depending on this, the char_decode in C4::Biblio.pm replaces marc-char-encode by an iso 8859-1 encoding. Note that in the breeding import this value has been added too, for a better support.
816 # * the marc_breeding and z3950* tables have been modified : they have an encoding column and the random z3950 number is stored too for convenience => it's the key I use to list only requested biblios in the popup.
817 #
818 # Revision 1.41  2003/04/29 08:09:44  tipaul
819 # z3950 support is coming...
820 # * adding a syntax column in z3950 table = this column will say wether the z3950 must be called with PerferedRecordsyntax => USMARC or PerferedRecordsyntax => UNIMARC. I tried some french UNIMARC z3950 servers, and some only send USMARC, some only UNIMARC, some can answer with both.
821 # Note this is a 1st draft. More to follow (today ? I hope).
822 #
823 # Revision 1.40  2003/04/22 10:48:27  wolfpac444
824 # Added "father" column to bibliothesaurus table
825 #
826 # Revision 1.39  2003/04/04 08:45:00  tipaul
827 # last commits before 1.9.1
828 #
829 # Revision 1.38  2003/03/18 10:58:19  tipaul
830 # adding checkdigit parameter that choose how to check the members cardnumber.
831 # At the moment :
832 # * none = no checking
833 # * katipo = checked as before
834 #
835 # Revision 1.37  2003/01/30 01:47:48  acli
836 # Corrected syntax error reported by Benedict
837 #
838 # Made the indentation somewhat easier to read; the messiness probably caused
839 # the original syntax error.
840 #
841 # Revision 1.36  2003/01/28 15:13:30  tipaul
842 # userflag table now created in upgrade script (bugfix #171)
843 #
844 # Revision 1.35  2003/01/27 03:12:49  acli
845 # Reworded the description for "acquisitions" to make it fit on the screen
846 #
847 # Added "iso" to dateformat, since dateformat is not yet being used anyway
848 #
849 # Revision 1.34  2003/01/23 12:30:02  tipaul
850 # introducint marcflavour in systempref file : used for character decoding
851 #
852 # Revision 1.33  2003/01/21 09:03:27  tipaul
853 # bugfix (NOTE : this bugs makes installation of the 1.3.3 a little fuzzy. Please fix your DB if you installed 1.3.3)
854 #
855 # Revision 1.32  2003/01/16 10:29:45  tipaul
856 # adding a MARC parameter in systempref ( which is ON or OFF)
857 # the search will be a marc search if MARC=ON
858 # and a standard (v1.2) search if MARC=OFF
859 #
860 # Revision 1.31  2003/01/06 13:32:43  tipaul
861 # *** empty log message ***
862 #
863 # Revision 1.29  2003/01/06 11:14:11  tipaul
864 # last bugfixes before 1.3.3 : systempref table correctly filled
865 #
866 # Revision 1.28  2002/12/10 13:27:47  tipaul
867 # bugfixes (davide mails in koha-dev)
868 #
869 # Revision 1.27  2002/11/26 15:04:54  tipaul
870 # road to 1.3.2. Updating db structure during installation
871 #
872 # Revision 1.26  2002/11/12 17:42:40  tonnesen
873 # Merged some features over from rel-1-2, including primary key checking.
874 #
875 # Revision 1.25  2002/11/12 16:44:38  tipaul
876 # road to 1.3.2 :
877 # * many bugfixes
878 # * adding value_builder : you can map a subfield in the marc_subfield_structure to a sub stored in "value_builder" directory. In this directory you can create screen used to build values with any method. In this commit is a 1st draft of the builder for 100$a unimarc french subfield, which is composed of 35 digits, with 12 differents values (only the 4th first are provided for instance)
879 #
880 # Revision 1.24  2002/10/30 14:00:23  arensb
881 # (bug fix): Fixed typo.
882 #
883 # Revision 1.23  2002/10/25 10:55:46  tipaul
884 # Road to 1.3.2
885 # * bugfixes and improvements
886 # * manage mandatory MARC subfields
887 # * new table : authorised_values. this table contains categories and authorised values for the category. On MARC management, you can map a subfield to a authorised_values category. If you do this, the subfield can only be filled with a authorised_value of the selected category.
888 # this submit contains everything needed :
889 # * updatedatabase
890 # * admin screens
891 # * "links" management
892 # * creation of a html-list if a subfield is mapped to an authorised value.
893 #
894 # Note this is different from authorities support, which will come soon.
895 # The authorised_values is supposed to contains a "small" number of authorised values for a category (less than 50-100). If you enter more authorised values than this, it should be hard to find what you want in a BIG list...
896 #
897 # Revision 1.22  2002/10/15 10:08:19  tipaul
898 # fixme corrected, re-indent and adding the marc_breeding table (see commit of marcimport.pl for more explanations about breeding)
899 #
900 # Revision 1.21  2002/10/14 11:48:59  tipaul
901 # bugfix
902 #
903 # Revision 1.20  2002/10/10 04:49:41  arensb
904 # Added some FIXME comments.
905 #
906 # Revision 1.19  2002/10/05 10:17:17  arensb
907 # Merged with arensb-context branch: use C4::Context->dbh instead of
908 # &C4Connect, and generally prefer C4::Context over C4::Database.
909 #
910 # Revision 1.18.2.2  2002/10/05 06:18:43  arensb
911 # Added a whole mess of FIXME comments.
912 #
913 # Revision 1.18.2.1  2002/10/04 02:46:00  arensb
914 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
915 # C4Connect.
916 #
917 # Revision 1.18  2002/09/24 13:50:55  tipaul
918 # long WAS the road to 1.3.0...
919 # coming VERY SOON NOW...
920 # modifying installer and buildrelease to update the DB
921 #
922 # Revision 1.17  2002/09/24 12:57:35  tipaul
923 # long WAS the road to 1.3.0...
924 # coming VERY SOON NOW...
925 # modifying installer and buildrelease to update the DB
926 #
927 # Revision 1.16  2002/07/31 02:34:27  finlayt
928 #
929 # added "notforloan" field to the itemtypes table.
930 #
931 # Revision 1.15  2002/07/20 22:30:06  rangi
932 # Making sure fix makes it into the main branch as well
933 # Fix for bug 69
934 #
935 # Revision 1.14  2002/07/08 16:20:26  tonnesen
936 # Added sessionqueries table and password/userid fields to borrowers table
937 #
938 # Revision 1.13  2002/07/04 18:05:36  tonnesen
939 # bug fix
940 #
941 # Revision 1.12  2002/07/04 16:41:06  tonnesen
942 # Merged changes from rel-1-2.  Abstracted table structure changes by alan.
943 #