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.
23 # FIXME - /etc/koha.conf might not exist, so shouldn't use
26 # FIXME - The user might be installing a new database, so can't rely
27 # on /etc/koha.conf anyway.
34 %existingtables, # tables already in database
38 $type, $null, $key, $default, $extra,
39 $prefitem, # preference item in systempreferences table
42 my $dbh = C4::Context->dbh;
47 # Tables to add if they don't exist
49 shelfcontents=>"( shelfnumber int not null,
50 itemnumber int not null,
52 bookshelf=>"( shelfnumber int auto_increment primary key,
53 shelfname char(255))",
54 z3950queue=>"( id int auto_increment primary key,
63 identifier char(30))",
64 z3950results=>"( id int auto_increment primary key,
74 branchrelations=>"( branchcode varchar(4),
75 categorycode varchar(4))",
76 websites=>"( websitenumber int(11) NOT NULL auto_increment,
77 biblionumber int(11) NOT NULL default '0',
81 PRIMARY KEY (websitenumber) )",
82 marcrecorddone=>"( isbn char(40),
85 controlnumber char(40))",
86 uploadedmarc=>"( id int(11) NOT NULL auto_increment PRIMARY KEY,
88 hidden smallint(6) default NULL,
89 name varchar(255) default NULL)",
90 ethnicity=>"( code varchar(10) NOT NULL default '',
91 name varchar(255) default NULL,
92 PRIMARY KEY (code) )",
93 sessions=>"( sessionID varchar(255) NOT NULL default '',
94 userid varchar(255) default NULL,
95 ip varchar(16) default NULL,
97 PRIMARY KEY (sessionID) )",
98 sessionqueries=>"( sessionID varchar(255) NOT NULL default '',
99 userid char(100) NOT NULL default '',
100 ip char(18) NOT NULL default '',
101 url text NOT NULL default '' )",
102 bibliothesaurus=> "( CREATE TABLE bibliothesaurus (
103 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 '',
110 KEY freelib (freelib),
112 KEY category (category),
113 KEY hierarchy (hierarchy)
116 bibid bigint(20) unsigned NOT NULL auto_increment,
117 biblionumber int(11) NOT NULL default '0',
118 datecreated date NOT NULL default '0000-00-00',
119 datemodified date default NULL,
120 origincode char(20) default NULL,
122 KEY origincode (origincode),
123 KEY biblionumber (biblionumber)
125 marc_blob_subfield => "(
126 blobidlink bigint(20) NOT NULL auto_increment,
127 subfieldvalue longtext NOT NULL,
128 PRIMARY KEY (blobidlink)
130 marc_subfield_structure => "(
131 tagfield char(3) NOT NULL default '',
132 tagsubfield char(1) NOT NULL default '',
133 liblibrarian char(255) NOT NULL default '',
134 libopac char(255) NOT NULL default '',
135 repeatable tinyint(4) NOT NULL default '0',
136 mandatory tinyint(4) NOT NULL default '0',
137 kohafield char(40) NOT NULL default '',
139 authorised_value char(10) default NULL,
140 thesaurus_category char(10) default NULL,
141 value_builder char(80) NOT NULL default '',
142 PRIMARY KEY (tagfield,tagsubfield),
143 KEY kohafield (kohafield),
146 marc_subfield_table => "(
147 subfieldid bigint(20) unsigned NOT NULL auto_increment,
148 bibid bigint(20) unsigned NOT NULL default '0',
149 tag char(3) NOT NULL default '',
150 tagorder tinyint(4) NOT NULL default '1',
151 tag_indicator char(2) NOT NULL default '',
152 subfieldcode char(1) NOT NULL default '',
153 subfieldorder tinyint(4) NOT NULL default '1',
154 subfieldvalue varchar(255) default NULL,
155 valuebloblink bigint(20) default NULL,
156 PRIMARY KEY (subfieldid),
159 KEY tag_indicator (tag_indicator),
160 KEY subfieldorder (subfieldorder),
161 KEY subfieldcode (subfieldcode),
162 KEY subfieldvalue (subfieldvalue),
163 KEY tagorder (tagorder)
165 marc_tag_structure => "(
166 tagfield char(3) NOT NULL default '',
167 liblibrarian char(255) NOT NULL default '',
168 libopac char(255) NOT NULL default '',
169 repeatable tinyint(4) NOT NULL default '0',
170 mandatory tinyint(4) NOT NULL default '0',
171 PRIMARY KEY (tagfield)
174 bibid bigint(20) NOT NULL default '0',
175 tag char(3) NOT NULL default '',
176 tagorder tinyint(4) NOT NULL default '1',
177 subfieldid char(1) NOT NULL default '',
178 subfieldorder tinyint(4) NOT NULL default '1',
179 word varchar(255) NOT NULL default '',
180 sndx_word varchar(255) NOT NULL default '',
183 KEY tagorder (tagorder),
184 KEY subfieldid (subfieldid),
185 KEY subfieldorder (subfieldorder),
187 KEY sndx_word (sndx_word)
189 marc_breeding =>"( file varchar(80) NOT NULL default '',
190 isbn varchar(10) NOT NULL default '',
194 authorised_values => "(id int(11) NOT NULL auto_increment,
195 category char(10) NOT NULL default '',
196 authorised_value char(80) NOT NULL default '',
204 biblio=>{ 'abstract' => 'text' },
205 deletedbiblio=>{ 'abstract' => 'text' },
206 biblioitems=>{ 'lccn' => 'char(25)',
207 'url' => 'varchar(255)',
209 deletedbiblioitems=>{ 'lccn' => 'char(25)',
210 'url' => 'varchar(255)',
212 branchtransfers=>{ 'datearrived' => 'datetime' },
213 statistics=>{'borrowernumber' =>'int(11)'},
214 aqbooksellers=>{'invoicedisc' =>'float(6,4)',
215 'nocalc' => 'int(11)'},
216 borrowers=>{'userid' => 'char(30)',
217 'password' => 'char(30)',},
218 aqorders=>{'budgetdate' => 'date'},
219 #added so that reference items are not available for reserves...
220 itemtypes=>{'notforloan' => 'smallint(6)'},
221 systempreferences =>{'explanation' => 'char(80)'},
225 classification =>'classification',
226 multipart =>'multipart',
227 multivolume =>'multivolume',
228 newitems =>'newitems',
229 procedures =>'procedures',
230 publisher =>'publisher',
231 searchstats =>'searchstats',
232 serialissues =>'serialissues',
235 # Default system preferences
237 'autoMemberNum'=> ['1','1 or else. If 1, Barcode is auto-calculated'],
238 'acquisitions'=> ['simple','normal or simple : will use acquisition system found in directory acqui.simple or acquisition'],
239 'template' => ['default','template default name'],
240 'autoBarcode' => ['0','1 or else. If 1, Barcode is auto-calculated'],
241 'insecure' => ['no','if YES, no auth at all is needed. Be careful if you set this to yes !'],
242 'authoritysep' => ['--','the separator used in authority/thesaurus. Usually --']
248 # Get version of MySQL database engine.
249 my $mysqlversion=`mysqld --version`;
250 $mysqlversion=~/Ver (\S*) /;
252 if ($mysqlversion ge '3.23') {
253 print "Could convert to MyISAM database tables...\n";
256 #---------------------------------
259 # Collect all tables into a list
260 $sth=$dbh->prepare("show tables");
262 while (my ($table) = $sth->fetchrow) {
263 $existingtables{$table}=1;
266 # Now add any missing tables
267 foreach $table ( keys %requiretables ) {
268 print "Checking $table table...\n" if $debug;
269 unless ($existingtables{$table} ) {
270 print "Adding $table table...\n";
271 my $sth=$dbh->prepare(
272 "create table $table $requiretables{$table}" );
275 print "Error : $sth->errstr \n";
281 # now drop useless tables
282 foreach $table ( keys %dropable_table) {
283 print "Dropping unused tables...\n" if $debug;
284 if ($existingtables{$table} ) {
285 $dbh->do("drop table $table");
287 print "Error : $dbh->errstr \n";
291 unless ($existingtables{'z3950servers'}) {
292 print "Adding z3950servers table...\n";
293 my $sti=$dbh->prepare("create table z3950servers (
304 $sti=$dbh->prepare("insert into z3950servers
305 values ('z3950.loc.gov',
309 'Library of Congress',
314 #---------------------------------
317 foreach $table ( keys %requirefields ) {
318 print "Check table $table\n" if $debug;
319 $sth=$dbh->prepare("show columns from $table");
322 while ( ($column, $type, $null, $key, $default, $extra)
324 $types{$column}=$type;
326 foreach $column ( keys %{ $requirefields{$table} } ) {
327 print " Check column $column\n" if $debug;
328 if ( ! $types{$column} ) {
329 # column doesn't exist
330 print "Adding $column field to $table table...\n";
331 $query="alter table $table
332 add column $column " . $requirefields{$table}->{$column} ;
333 print "Execute: $query\n" if $debug;
334 my $sti=$dbh->prepare($query);
337 print "**Error : $sti->errstr \n";
344 # Get list of columns from items table
347 $sth=$dbh->prepare("show columns from items");
349 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
350 $itemtypes{$column}=$type;
353 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
354 $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
357 print "Setting maximum barcode length to 20 (was $oldlength).\n";
358 my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
363 # extending the timestamp in branchtransfers...
366 $sth=$dbh->prepare("show columns from branchtransfers");
368 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
369 $branchtransfers{$column}=$type;
372 unless ($branchtransfers{'datesent'} eq 'datetime') {
373 print "Setting type of datesent in branchtransfers to datetime.\n";
374 my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
378 unless ($branchtransfers{'datearrived'} eq 'datetime') {
379 print "Setting type of datearrived in branchtransfers to datetime.\n";
380 my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
384 # changing the branchcategories table around...
385 my %branchcategories;
387 $sth=$dbh->prepare("show columns from branchcategories");
389 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
390 $branchcategories{$column}=$type;
393 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
394 print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
395 my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
397 $sti=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
401 unless ($branchcategories{'categoryname'} eq 'text') {
402 print "Changing branchcode in branchcategories to categoryname text.\n";
403 my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
407 unless ($branchcategories{'codedescription'} eq 'text') {
408 print "Replacing branchholding in branchcategories with codedescription text.\n";
409 my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
414 # Populate systempreferences if it is empty
416 foreach $prefitem ( keys %defaultprefs ) {
417 $sth=$dbh->prepare("select value
418 from systempreferences
420 $sth->execute($prefitem);
421 unless ($sth->rows) {
422 print "Adding system preference item $prefitem with value " .$defaultprefs{$prefitem}[0] ."\n";
423 $sti=$dbh->prepare("insert into systempreferences (variable, value,explanation) values (?,?,?)");
424 $sti->execute($prefitem,$defaultprefs{$prefitem}[0],$defaultprefs{$prefitem}[1]);
434 # Revision 1.25 2002/11/12 16:44:38 tipaul
437 # * 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)
439 # Revision 1.24 2002/10/30 14:00:23 arensb
440 # (bug fix): Fixed typo.
442 # Revision 1.23 2002/10/25 10:55:46 tipaul
444 # * bugfixes and improvements
445 # * manage mandatory MARC subfields
446 # * 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.
447 # this submit contains everything needed :
450 # * "links" management
451 # * creation of a html-list if a subfield is mapped to an authorised value.
453 # Note this is different from authorities support, which will come soon.
454 # 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...
456 # Revision 1.22 2002/10/15 10:08:19 tipaul
457 # fixme corrected, re-indent and adding the marc_breeding table (see commit of marcimport.pl for more explanations about breeding)
459 # Revision 1.21 2002/10/14 11:48:59 tipaul
462 # Revision 1.20 2002/10/10 04:49:41 arensb
463 # Added some FIXME comments.
465 # Revision 1.19 2002/10/05 10:17:17 arensb
466 # Merged with arensb-context branch: use C4::Context->dbh instead of
467 # &C4Connect, and generally prefer C4::Context over C4::Database.
469 # Revision 1.18.2.2 2002/10/05 06:18:43 arensb
470 # Added a whole mess of FIXME comments.
472 # Revision 1.18.2.1 2002/10/04 02:46:00 arensb
473 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
476 # Revision 1.18 2002/09/24 13:50:55 tipaul
477 # long WAS the road to 1.3.0...
478 # coming VERY SOON NOW...
479 # modifying installer and buildrelease to update the DB
481 # Revision 1.17 2002/09/24 12:57:35 tipaul
482 # long WAS the road to 1.3.0...
483 # coming VERY SOON NOW...
484 # modifying installer and buildrelease to update the DB
486 # Revision 1.16 2002/07/31 02:34:27 finlayt
488 # added "notforloan" field to the itemtypes table.
490 # Revision 1.15 2002/07/20 22:30:06 rangi
491 # Making sure fix makes it into the main branch as well
494 # Revision 1.14 2002/07/08 16:20:26 tonnesen
495 # Added sessionqueries table and password/userid fields to borrowers table
497 # Revision 1.13 2002/07/04 18:05:36 tonnesen
500 # Revision 1.12 2002/07/04 16:41:06 tonnesen
501 # Merged changes from rel-1-2. Abstracted table structure changes by alan.