adding zipcode and homezipcode into borrowers table (bug #246
[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("/etc/koha.conf.tmp");
23
24 # FIXME - /etc/koha.conf might not exist, so shouldn't use
25 # C4::Context.
26
27 # FIXME - The user might be installing a new database, so can't rely
28 # on /etc/koha.conf anyway.
29
30 my $debug = 0;
31
32 my (
33     $sth, $sti,
34     $query,
35     %existingtables,    # tables already in database
36     %types,
37     $table,
38     $column,
39     $type, $null, $key, $default, $extra,
40     $prefitem,          # preference item in systempreferences table
41 );
42
43 my $dbh = C4::Context->dbh;
44
45 #-------------------
46 # Defines
47
48 # Tables to add if they don't exist
49 my %requiretables = (
50     shelfcontents => "( shelfnumber int not null,
51                                                         itemnumber int not null,
52                                                         flags int)",
53     bookshelf => "( shelfnumber int auto_increment primary key,
54                                                 shelfname char(255))",
55     z3950queue => "( id int auto_increment primary key,
56                                                 term text,
57                                                 type char(10),
58                                                 startdate int,
59                                                 enddate int,
60                                                 done smallint,
61                                                 results longblob,
62                                                 numrecords int,
63                                                 servers text,
64                                                 identifier char(30))",
65     z3950results => "( id int auto_increment primary key,
66                                                 queryid int,
67                                                 server char(255),
68                                                 startdate int,
69                                                 enddate int,
70                                                 results longblob,
71                                                 numrecords int,
72                                                 numdownloaded int,
73                                                 highestseen int,
74                                                 active smallint)",
75     branchrelations => "( branchcode varchar(4),
76                                                         categorycode varchar(4))",
77     websites => "( websitenumber int(11) NOT NULL auto_increment,
78                                                 biblionumber int(11) NOT NULL default '0',
79                                                 title text,
80                                                 description text,
81                                                 url varchar(255),
82                                                 PRIMARY KEY (websitenumber) )",
83     marcrecorddone => "( isbn char(40),
84                                                                 issn char(40),
85                                                                 lccn char(40),
86                                                                 controlnumber char(40))",
87     uploadedmarc => "( id int(11) NOT NULL auto_increment PRIMARY KEY,
88                                                         marc longblob,
89                                                         hidden smallint(6) default NULL,
90                                                         name varchar(255) default NULL)",
91     ethnicity => "( code varchar(10) NOT NULL default '',
92                                         name varchar(255) default NULL,
93                                         PRIMARY KEY  (code)   )",
94     sessions => "( sessionID varchar(255) NOT NULL default '',
95                                                 userid varchar(255) default NULL,
96                                                 ip varchar(16) default NULL,
97                                                 lasttime int,
98                                                 PRIMARY KEY (sessionID)   )",
99     sessionqueries => "( sessionID varchar(255) NOT NULL default '',
100                                                                 userid char(100) NOT NULL default '',
101                                                                 ip char(18) NOT NULL default '',
102                                                                 url text NOT NULL default ''  )",
103     bibliothesaurus => "( id bigint(20) NOT NULL auto_increment,
104                                                         freelib char(255) NOT NULL default '',
105                                                         stdlib char(255) NOT NULL default '',
106                                                         category char(10) NOT NULL default '',
107                                                         level tinyint(4) NOT NULL default '1',
108                                                         hierarchy char(80) NOT NULL default '',
109                                                         father bigint(20) NOT NULL default '',
110                                                         PRIMARY KEY  (id),
111                                                         KEY freelib (freelib),
112                                                         KEY stdlib (stdlib),
113                                                         KEY category (category),
114                                                         KEY hierarchy (hierarchy)
115                                                         )",
116     marc_biblio => "(
117                                                 bibid bigint(20) unsigned NOT NULL auto_increment,
118                                                 biblionumber int(11) NOT NULL default '0',
119                                                 datecreated date NOT NULL default '0000-00-00',
120                                                 datemodified date default NULL,
121                                                 origincode char(20) default NULL,
122                                                 PRIMARY KEY  (bibid),
123                                                 KEY origincode (origincode),
124                                                 KEY biblionumber (biblionumber)
125                                                 ) ",
126     marc_blob_subfield => "(
127                                         blobidlink bigint(20) NOT NULL auto_increment,
128                                         subfieldvalue longtext NOT NULL,
129                                         PRIMARY KEY  (blobidlink)
130                                         ) ",
131     marc_subfield_structure => "(
132                                                 tagfield char(3) NOT NULL default '',
133                                                 tagsubfield char(1) NOT NULL default '',
134                                                 liblibrarian char(255) NOT NULL default '',
135                                                 libopac char(255) NOT NULL default '',
136                                                 repeatable tinyint(4) NOT NULL default '0',
137                                                 mandatory tinyint(4) NOT NULL default '0',
138                                                 kohafield char(40)  default NULL,
139                                                 tab tinyint(1) default NULL,
140                                                 authorised_value char(10) default NULL,
141                                                 thesaurus_category char(10) default NULL,
142                                                 value_builder char(80) default NULL,
143                                                 PRIMARY KEY  (tagfield,tagsubfield),
144                                                 KEY kohafield (kohafield),
145                                                 KEY tab (tab)
146                                                 )",
147     marc_subfield_table => "(
148                                                 subfieldid bigint(20) unsigned NOT NULL auto_increment,
149                                                 bibid bigint(20) unsigned NOT NULL default '0',
150                                                 tag char(3) NOT NULL default '',
151                                                 tagorder tinyint(4) NOT NULL default '1',
152                                                 tag_indicator char(2) NOT NULL default '',
153                                                 subfieldcode char(1) NOT NULL default '',
154                                                 subfieldorder tinyint(4) NOT NULL default '1',
155                                                 subfieldvalue varchar(255) default NULL,
156                                                 valuebloblink bigint(20) default NULL,
157                                                 PRIMARY KEY  (subfieldid),
158                                                 KEY bibid (bibid),
159                                                 KEY tag (tag),
160                                                 KEY tag_indicator (tag_indicator),
161                                                 KEY subfieldorder (subfieldorder),
162                                                 KEY subfieldcode (subfieldcode),
163                                                 KEY subfieldvalue (subfieldvalue),
164                                                 KEY tagorder (tagorder)
165                                         )",
166     marc_tag_structure => "(
167                                         tagfield char(3) NOT NULL default '',
168                                         liblibrarian char(255) NOT NULL default '',
169                                         libopac char(255) NOT NULL default '',
170                                         repeatable tinyint(4) NOT NULL default '0',
171                                         mandatory tinyint(4) NOT NULL default '0',
172                                         authorised_value char(10) default NULL,
173                                         PRIMARY KEY  (tagfield)
174                                         )",
175     marc_word => "(
176                                 bibid bigint(20) NOT NULL default '0',
177                                 tag char(3) NOT NULL default '',
178                                 tagorder tinyint(4) NOT NULL default '1',
179                                 subfieldid char(1) NOT NULL default '',
180                                 subfieldorder tinyint(4) NOT NULL default '1',
181                                 word varchar(255) NOT NULL default '',
182                                 sndx_word varchar(255) NOT NULL default '',
183                                 KEY bibid (bibid),
184                                 KEY tag (tag),
185                                 KEY tagorder (tagorder),
186                                 KEY subfieldid (subfieldid),
187                                 KEY subfieldorder (subfieldorder),
188                                 KEY word (word),
189                                 KEY sndx_word (sndx_word)
190                         )",
191     marc_breeding => "(  id bigint(20) NOT NULL auto_increment,
192                                 file varchar(80) NOT NULL default '',
193                                 isbn varchar(10) NOT NULL default '',
194                                 title varchar(128) default NULL,
195                                 author varchar(80) default NULL,
196                                 marc text NOT NULL,
197                                 encoding varchar(40) default NULL,
198                                 PRIMARY KEY  (id),
199                                 KEY title (title),
200                                 KEY isbn (isbn)
201                         )",
202     authorised_values => "(id int(11) NOT NULL auto_increment,
203                                 category char(10) NOT NULL default '',
204                                 authorised_value char(80) NOT NULL default '',
205                                 lib char(80) NULL,
206                                 PRIMARY KEY  (id),
207                                 KEY name (category)
208                         )",
209     userflags => "( bit int(11) NOT NULL default '0',
210                                 flag char(30), flagdesc char(255),
211                                 defaulton int(11)
212                         )",
213 );
214
215 my %requirefields = (
216     biblio        => { 'abstract' => 'text' },
217     deletedbiblio => { 'abstract' => 'text' },
218     biblioitems   => {
219         'lccn' => 'char(25)',
220         'url'  => 'varchar(255)',
221         'marc' => 'text'
222     },
223     deletedbiblioitems => {
224         'lccn' => 'char(25)',
225         'url'  => 'varchar(255)',
226         'marc' => 'text'
227     },
228     branchtransfers => { 'datearrived'    => 'datetime' },
229     statistics      => { 'borrowernumber' => 'int(11)' },
230     aqbooksellers   => {
231         'invoicedisc' => 'float(6,4)',
232         'nocalc'      => 'int(11)'
233     },
234     borrowers => {
235         'userid'        => 'char(30)',
236         'password'      => 'char(30)',
237         'flags'         => 'int(11)',
238         'textmessaging' => 'varchar(30)'
239     },
240     aqorders => { 'budgetdate' => 'date' },
241
242     #added so that reference items are not available for reserves...
243     itemtypes         => { 'notforloan'  => 'smallint(6)' },
244     systempreferences => { 'explanation' => 'char(80)' },
245     z3950servers      => { 'syntax'      => 'char(80)' },
246     borrowers => {'zipcode' => 'varchar(25)',
247                                                 'homezipcode' => 'varchar(25)'}
248 );
249
250 my %dropable_table = (
251     classification => 'classification',
252     multipart      => 'multipart',
253     multivolume    => 'multivolume',
254     newitems       => 'newitems',
255     procedures     => 'procedures',
256     publisher      => 'publisher',
257     searchstats    => 'searchstats',
258     serialissues   => 'serialissues',
259 );
260
261 # The tabledata hash contains data that should be in the tables.
262 # The uniquefieldrequired hash entry is used to determine which (if any) fields
263 # must not exist in the table for this row to be inserted.  If the
264 # uniquefieldrequired entry is already in the table, the existing data is not
265 # modified.
266
267 my %tabledata = (
268     userflags => [
269         {
270             uniquefieldrequired => 'bit',
271             bit                 => 0,
272             flag                => 'superlibrarian',
273             flagdesc            => 'Access to all librarian functions',
274             defaulton           => 0
275         },
276         {
277             uniquefieldrequired => 'bit',
278             bit                 => 1,
279             flag                => 'circulate',
280             flagdesc            => 'Circulate books',
281             defaulton           => 0
282         },
283         {
284             uniquefieldrequired => 'bit',
285             bit                 => 2,
286             flag                => 'catalogue',
287             flagdesc            => 'View Catalogue (Librarian Interface)',
288             defaulton           => 0
289         },
290         {
291             uniquefieldrequired => 'bit',
292             bit                 => 3,
293             flag                => 'parameters',
294             flagdesc            => 'Set Koha system paramters',
295             defaulton           => 0
296         },
297         {
298             uniquefieldrequired => 'bit',
299             bit                 => 4,
300             flag                => 'borrowers',
301             flagdesc            => 'Add or modify borrowers',
302             defaulton           => 0
303         },
304         {
305             uniquefieldrequired => 'bit',
306             bit                 => 5,
307             flag                => 'permissions',
308             flagdesc            => 'Set user permissions',
309             defaulton           => 0
310         },
311         {
312             uniquefieldrequired => 'bit',
313             bit                 => 6,
314             flag                => 'reserveforothers',
315             flagdesc            => 'Reserve books for patrons',
316             defaulton           => 0
317         },
318         {
319             uniquefieldrequired => 'bit',
320             bit                 => 7,
321             flag                => 'borrow',
322             flagdesc            => 'Borrow books',
323             defaulton           => 1
324         },
325         {
326             uniquefieldrequired => 'bit',
327             bit                 => 8,
328             flag                => 'reserveforself',
329             flagdesc            => 'Reserve books for self',
330             defaulton           => 0
331         },
332         {
333             uniquefieldrequired => 'bit',
334             bit                 => 9,
335             flag                => 'editcatalogue',
336             flagdesc  => 'Edit Catalogue (Modify bibliographic/holdings data)',
337             defaulton => 0
338         },
339         {
340             uniquefieldrequired => 'bit',
341             bit                 => 10,
342             flag                => 'updatecharges',
343             flagdesc            => 'Update borrower charges',
344             defaulton           => 0
345         },
346     ],
347     systempreferences => [
348         {
349             uniquefieldrequired => 'variable',
350             variable            => 'autoMemberNum',
351             value               => '1',
352             explanation         => '1 or 0. If 1, Member number is auto-calculated'
353         },
354         {
355             uniquefieldrequired => 'variable',
356             variable            => 'acquisitions',
357             value               => 'simple',
358             explanation         =>
359 'normal or simple : whether to use "acqui" or "acqui.simple" acquisition system'
360         },
361         {
362             uniquefieldrequired => 'variable',
363             variable            => 'dateformat',
364             value               => 'metric',
365             explanation         => 'metric, us, or iso'
366         },
367         {
368             uniquefieldrequired => 'variable',
369             variable            => 'template',
370             value               => 'default',
371             explanation         => 'template default name'
372         },
373         {
374             uniquefieldrequired => 'variable',
375             variable            => 'autoBarcode',
376             value               => '1',
377             explanation         => '1 or 0. If 1, Barcode is auto-calculated'
378         },
379         {
380             uniquefieldrequired => 'variable',
381             variable            => 'insecure',
382             value               => 'NO',
383             explanation         =>
384 'if YES, no auth at all is needed. Be careful if you set this to yes !'
385         },
386         {
387             uniquefieldrequired => 'variable',
388             variable            => 'authoritysep',
389             value               => '--',
390             explanation         =>
391             'the separator used in authority/thesaurus. Usually --'
392         },
393         {
394             uniquefieldrequired => 'variable',
395             variable            => 'opaclanguages',
396             value               => 'en',
397             explanation         => 'languages'
398         },
399         {
400             uniquefieldrequired => 'variable',
401             variable            => 'opacthemes',
402             value               => 'default',
403             explanation         => 'theme'
404         },
405         {
406             uniquefieldrequired => 'variable',
407             variable            => 'timeout',
408             value               => '12000000',
409             explanation         => 'login timeout'
410         },
411         {
412             uniquefieldrequired => 'variable',
413             variable            => 'marc',
414             value               => 'ON',
415             explanation         => 'MARC support (ON or OFF)'
416         },
417         {
418             uniquefieldrequired => 'variable',
419             variable            => 'marcflavour',
420             value               => 'MARC21',
421             explanation         =>
422             'your MARC flavor (MARC21 or UNIMARC) used for character encoding'
423         },
424         {
425             uniquefieldrequired => 'variable',
426             variable            => 'checkdigit',
427             value               => 'katipo',
428             explanation         =>
429             'none= no check on member cardnumber. katipo= katipo check'
430         },
431         {
432             uniquefieldrequired => 'variable',
433             variable            => 'dateformat',
434             value               => 'ISO',
435             explanation         =>
436             'date format (US mm/dd/yyyy, metric dd/mm/yyy, ISO yyyy/mm/dd) '
437         },
438         {
439             uniquefieldrequired => 'variable',
440             variable            => 'maxoutstanding',
441             value               => '5',
442             explanation         =>
443             'maximum amount withstanding to be able make reserves '
444         },
445         {
446             uniquefieldrequired => 'variable',
447             variable            => 'maxreserves',
448             value               => '5',
449             explanation         =>
450             'maximum number of reserves a member can make '
451         },
452         {
453             uniquefieldrequired => 'variable',
454             variable            => 'noissuescharge',
455             value               => '5',
456             explanation         =>
457             'maximum amount withstanding to be able to check out an item '
458         },
459         {
460             uniquefieldrequired => 'variable',
461             variable            => 'KohaAdminEmailAddress',
462             value               => 'your.mail@here',
463             explanation => 'the email adress where borrowers modifs are sent'
464         },
465     ],
466
467 );
468
469 my %fielddefinitions = (
470     printers => [
471         {
472             field   => 'printername',
473             type    => 'char(40)',
474             null    => '',
475             key     => 'PRI',
476             default => ''
477         },
478     ],
479     aqbookfund => [
480         {
481             field   => 'bookfundid',
482             type    => 'char(5)',
483             null    => '',
484             key     => 'PRI',
485             default => ''
486         },
487     ],
488     z3950servers => [
489         {
490             field   => 'id',
491             type    => 'int',
492             null    => '',
493             key     => 'PRI',
494             default => '',
495             extra   => 'auto_increment'
496         },
497     ],
498 );
499
500 #-------------------
501 # Initialize
502
503 # Start checking
504
505 # Get version of MySQL database engine.
506 my $mysqlversion = `mysqld --version`;
507 $mysqlversion =~ /Ver (\S*) /;
508 $mysqlversion = $1;
509 if ( $mysqlversion ge '3.23' ) {
510     print "Could convert to MyISAM database tables...\n";
511 }
512
513 #---------------------------------
514 # Tables
515
516 # Collect all tables into a list
517 $sth = $dbh->prepare("show tables");
518 $sth->execute;
519 while ( my ($table) = $sth->fetchrow ) {
520     $existingtables{$table} = 1;
521 }
522
523 # Now add any missing tables
524 foreach $table ( keys %requiretables ) {
525     print "Checking $table table...\n" if $debug;
526     unless ( $existingtables{$table} ) {
527         print "Adding $table table...\n";
528         my $sth = $dbh->prepare("create table $table $requiretables{$table}");
529         $sth->execute;
530         if ( $sth->err ) {
531             print "Error : $sth->errstr \n";
532             $sth->finish;
533         }    # if error
534     }    # unless exists
535 }    # foreach
536
537 # now drop useless tables
538 foreach $table ( keys %dropable_table ) {
539     print "Dropping unused tables...\n" if $debug;
540     if ( $existingtables{$table} ) {
541         $dbh->do("drop table $table");
542         if ( $dbh->err ) {
543             print "Error : $dbh->errstr \n";
544         }
545     }
546 }
547 unless ( $existingtables{'z3950servers'} ) {
548     print "Adding z3950servers table...\n";
549     my $sti = $dbh->prepare( "create table z3950servers (
550                                                                                 host char(255),
551                                                                                 port int,
552                                                                                 db char(255),
553                                                                                 userid char(255),
554                                                                                 password char(255),
555                                                                                 name text,
556                                                                                 id int,
557                                                                                 checked smallint,
558                                                                                 rank int)"
559     );
560     $sti->execute;
561     $sti = $dbh->prepare( "insert into z3950servers
562                                                                 values ('z3950.loc.gov',
563                                                                 7090,
564                                                                 'voyager',
565                                                                 '', '',
566                                                                 'Library of Congress',
567                                                                 1, 1, 1)"
568     );
569     $sti->execute;
570 }
571
572 #---------------------------------
573 # Columns
574
575 foreach $table ( keys %requirefields ) {
576     print "Check table $table\n" if $debug;
577     $sth = $dbh->prepare("show columns from $table");
578     $sth->execute();
579     undef %types;
580     while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
581     {
582         $types{$column} = $type;
583     }    # while
584     foreach $column ( keys %{ $requirefields{$table} } ) {
585         print "  Check column $column\n" if $debug;
586         if ( !$types{$column} ) {
587
588             # column doesn't exist
589             print "Adding $column field to $table table...\n";
590             $query = "alter table $table
591                         add column $column " . $requirefields{$table}->{$column};
592             print "Execute: $query\n" if $debug;
593             my $sti = $dbh->prepare($query);
594             $sti->execute;
595             if ( $sti->err ) {
596                 print "**Error : $sti->errstr \n";
597                 $sti->finish;
598             }    # if error
599         }    # if column
600     }    # foreach column
601 }    # foreach table
602
603 foreach $table ( keys %fielddefinitions ) {
604     print "Check table $table\n" if $debug;
605     $sth = $dbh->prepare("show columns from $table");
606     $sth->execute();
607     my $definitions;
608     while ( ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
609     {
610         $definitions->{$column}->{type}    = $type;
611         $definitions->{$column}->{null}    = $null;
612         $definitions->{$column}->{key}     = $key;
613         $definitions->{$column}->{default} = $default;
614         $definitions->{$column}->{extra}   = $extra;
615     }    # while
616     my $fieldrow = $fielddefinitions{$table};
617     foreach my $row (@$fieldrow) {
618         my $field   = $row->{field};
619         my $type    = $row->{type};
620         my $null    = $row->{null};
621         my $key     = $row->{key};
622         my $default = $row->{default};
623         my $extra   = $row->{extra};
624         my $def     = $definitions->{$field};
625         unless ( $type eq $def->{type}
626             && $null eq $def->{null}
627             && $key eq $def->{key}
628             && $default eq $def->{default}
629             && $extra eq $def->{extra} )
630         {
631
632             if ( $null eq '' ) {
633                 $null = 'NOT NULL';
634             }
635             if ( $key eq 'PRI' ) {
636                 $key = 'PRIMARY KEY';
637             }
638             unless ( $extra eq 'auto_increment' ) {
639                 $extra = '';
640             }
641             my $sth =
642               $dbh->prepare(
643 "alter table $table change $field $field $type $null $key $extra default ?"
644             );
645             $sth->execute($default);
646             print "  Alter $field in $table\n";
647         }
648     }
649 }
650
651 # Get list of columns from items table
652 my %itemtypes;
653
654 $sth = $dbh->prepare("show columns from items");
655 $sth->execute;
656 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
657 {
658     $itemtypes{$column} = $type;
659 }
660
661 unless ( $itemtypes{'barcode'} eq 'varchar(20)' ) {
662     $itemtypes{'barcode'} =~ /varchar\((\d+)\)/;
663     my $oldlength = $1;
664     if ( $oldlength < 20 ) {
665         print "Setting maximum barcode length to 20 (was $oldlength).\n";
666         my $sti =
667           $dbh->prepare(
668             "alter table items change barcode barcode varchar(20) not null");
669         $sti->execute;
670     }
671 }
672
673 # extending the timestamp in branchtransfers...
674 my %branchtransfers;
675
676 $sth = $dbh->prepare("show columns from branchtransfers");
677 $sth->execute;
678 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
679 {
680     $branchtransfers{$column} = $type;
681 }
682
683 unless ( $branchtransfers{'datesent'} eq 'datetime' ) {
684     print "Setting type of datesent in branchtransfers to datetime.\n";
685     my $sti =
686       $dbh->prepare(
687         "alter table branchtransfers change datesent datesent datetime");
688     $sti->execute;
689 }
690
691 unless ( $branchtransfers{'datearrived'} eq 'datetime' ) {
692     print "Setting type of datearrived in branchtransfers to datetime.\n";
693     my $sti =
694       $dbh->prepare(
695         "alter table branchtransfers change datearrived datearrived datetime");
696     $sti->execute;
697 }
698
699 # changing the branchcategories table around...
700 my %branchcategories;
701
702 $sth = $dbh->prepare("show columns from branchcategories");
703 $sth->execute;
704 while ( my ( $column, $type, $null, $key, $default, $extra ) = $sth->fetchrow )
705 {
706     $branchcategories{$column} = $type;
707 }
708
709 unless ( $branchcategories{'categorycode'} eq 'varchar(4)' ) {
710     print
711 "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
712     my $sti =
713       $dbh->prepare(
714 "alter table branchcategories change categorycode categorycode varchar(4) not null"
715     );
716     $sti->execute;
717     $sti =
718       $dbh->prepare(
719         "alter table branchcategories add primary key (categorycode)");
720     $sti->execute;
721 }
722
723 unless ( $branchcategories{'categoryname'} eq 'text' ) {
724     print "Changing branchcode in branchcategories to categoryname text.\n";
725     my $sth =
726       $dbh->prepare(
727         "alter table branchcategories change branchcode categoryname text");
728     $sth->execute;
729 }
730
731 unless ( $branchcategories{'codedescription'} eq 'text' ) {
732     print
733 "Replacing branchholding in branchcategories with codedescription text.\n";
734     my $sth =
735       $dbh->prepare(
736         "alter table branchcategories change branchholding codedescription text"
737     );
738     $sth->execute;
739 }
740
741 # Populate tables with required data
742
743 foreach my $table ( keys %tabledata ) {
744     print "Checking for data required in table $table...\n";
745     my $tablerows = $tabledata{$table};
746     foreach my $row (@$tablerows) {
747         my $uniquefieldrequired = $row->{uniquefieldrequired};
748         my $uniquevalue         = $row->{$uniquefieldrequired};
749         my $sth                 =
750           $dbh->prepare(
751 "select $uniquefieldrequired from $table where $uniquefieldrequired=?"
752         );
753         $sth->execute($uniquevalue);
754         unless ( $sth->rows ) {
755             print "Adding row to $table: ";
756             my @values;
757             my $fieldlist;
758             my $placeholders;
759             foreach my $field ( keys %$row ) {
760                 (next) if ( $field eq 'uniquefieldrequired' );
761                 my $value = $row->{$field};
762                 push @values, $value;
763                 print "  $field => $value";
764                 $fieldlist .= "$field,";
765                 $placeholders .= "?,";
766             }
767             print "\n";
768             $fieldlist    =~ s/,$//;
769             $placeholders =~ s/,$//;
770             my $sth =
771               $dbh->prepare(
772                 "insert into $table ($fieldlist) values ($placeholders)");
773             $sth->execute(@values);
774         }
775     }
776 }
777
778 $sth->finish;
779
780 exit;
781
782 # $Log$
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 #