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=> "( code BIGINT not null AUTO_INCREMENT,
103 freelib CHAR (255) not null ,
104 stdlib CHAR (255) not null ,
105 type CHAR (80) not null ,
107 INDEX (freelib),index(stdlib),index(type) )",
109 bibid bigint(20) unsigned NOT NULL auto_increment,
110 biblionumber int(11) NOT NULL default '0',
111 datecreated date NOT NULL default '0000-00-00',
112 datemodified date default NULL,
113 origincode char(20) default NULL,
115 KEY origincode (origincode),
116 KEY biblionumber (biblionumber)
118 marc_blob_subfield => "(
119 blobidlink bigint(20) NOT NULL auto_increment,
120 subfieldvalue longtext NOT NULL,
121 PRIMARY KEY (blobidlink)
123 marc_subfield_structure => "(
124 tagfield char(3) NOT NULL default '',
125 tagsubfield char(1) NOT NULL default '',
126 liblibrarian char(255) NOT NULL default '',
127 libopac char(255) NOT NULL default '',
128 repeatable tinyint(4) NOT NULL default '0',
129 mandatory tinyint(4) NOT NULL default '0',
130 kohafield char(40) NOT NULL default '',
132 authorised_value char(10) default NULL,
133 PRIMARY KEY (tagfield,tagsubfield),
134 KEY kohafield (kohafield),
137 marc_subfield_table => "(
138 subfieldid bigint(20) unsigned NOT NULL auto_increment,
139 bibid bigint(20) unsigned NOT NULL default '0',
140 tag char(3) NOT NULL default '',
141 tagorder tinyint(4) NOT NULL default '1',
142 tag_indicator char(2) NOT NULL default '',
143 subfieldcode char(1) NOT NULL default '',
144 subfieldorder tinyint(4) NOT NULL default '1',
145 subfieldvalue varchar(255) default NULL,
146 valuebloblink bigint(20) default NULL,
147 PRIMARY KEY (subfieldid),
150 KEY tag_indicator (tag_indicator),
151 KEY subfieldorder (subfieldorder),
152 KEY subfieldcode (subfieldcode),
153 KEY subfieldvalue (subfieldvalue),
154 KEY tagorder (tagorder)
156 marc_tag_structure => "(
157 tagfield char(3) NOT NULL default '',
158 liblibrarian char(255) NOT NULL default '',
159 libopac char(255) NOT NULL default '',
160 repeatable tinyint(4) NOT NULL default '0',
161 mandatory tinyint(4) NOT NULL default '0',
162 PRIMARY KEY (tagfield)
165 bibid bigint(20) NOT NULL default '0',
166 tag char(3) NOT NULL default '',
167 tagorder tinyint(4) NOT NULL default '1',
168 subfieldid char(1) NOT NULL default '',
169 subfieldorder tinyint(4) NOT NULL default '1',
170 word varchar(255) NOT NULL default '',
171 sndx_word varchar(255) NOT NULL default '',
174 KEY tagorder (tagorder),
175 KEY subfieldid (subfieldid),
176 KEY subfieldorder (subfieldorder),
178 KEY sndx_word (sndx_word)
180 marc_breeding =>"( file varchar(80) NOT NULL default '',
181 isbn varchar(10) NOT NULL default '',
185 authorised_values => "(id int(11) NOT NULL auto_increment,
186 category char(10) NOT NULL default '',
187 authorised_value char(80) NOT NULL default '',
195 biblio=>{ 'abstract' => 'text' },
196 deletedbiblio=>{ 'abstract' => 'text' },
197 biblioitems=>{ 'lccn' => 'char(25)',
198 'url' => 'varchar(255)',
200 deletedbiblioitems=>{ 'lccn' => 'char(25)',
201 'url' => 'varchar(255)',
203 branchtransfers=>{ 'datearrived' => 'datetime' },
204 statistics=>{'borrowernumber' =>'int(11)'},
205 aqbooksellers=>{'invoicedisc' =>'float(6,4)',
206 'nocalc' => 'int(11)'},
207 borrowers=>{'userid' => 'char(30)',
208 'password' => 'char(30)',},
209 aqorders=>{'budgetdate' => 'date'},
210 #added so that reference items are not available for reserves...
211 itemtypes=>{'notforloan' => 'smallint(6)'},
212 systempreferences =>{'explanation' => 'char(80)'},
216 classification =>'classification',
217 multipart =>'multipart',
218 multivolume =>'multivolume',
219 newitems =>'newitems',
220 procedures =>'procedures',
221 publisher =>'publisher',
222 searchstats =>'searchstats',
223 serialissues =>'serialissues',
226 # Default system preferences
228 'autoMemberNum'=> ['1','1 or else. If 1, Barcode is auto-calculated'],
229 'acquisitions'=> ['simple','normal or simple : will use acquisition system found in directory acqui.simple or acquisition'],
230 'template' => ['default','template default name'],
231 'autoBarcode' => ['0','1 or else. If 1, Barcode is auto-calculated'],
232 'insecure' => ['no','if YES, no auth at all is needed. Be careful if you set this to yes !']
238 # Get version of MySQL database engine.
239 my $mysqlversion=`mysqld --version`;
240 $mysqlversion=~/Ver (\S*) /;
242 if ($mysqlversion ge '3.23') {
243 print "Could convert to MyISAM database tables...\n";
246 #---------------------------------
249 # Collect all tables into a list
250 $sth=$dbh->prepare("show tables");
252 while (my ($table) = $sth->fetchrow) {
253 $existingtables{$table}=1;
256 # Now add any missing tables
257 foreach $table ( keys %requiretables ) {
258 print "Checking $table table...\n" if $debug;
259 unless ($existingtables{$table} ) {
260 print "Adding $table table...\n";
261 my $sth=$dbh->prepare(
262 "create table $table $requiretables{$table}" );
265 print "Error : $sth->errstr \n";
271 # now drop useless tables
272 foreach $table ( keys %dropable_table) {
273 print "Dropping unused tables...\n" if $debug;
274 if ($existingtables{$table} ) {
275 $dbh->do("drop table $table");
277 print "Error : $dbh->errstr \n";
281 unless ($existingtables{'z3950servers'}) {
282 print "Adding z3950servers table...\n";
283 my $sti=$dbh->prepare("create table z3950servers (
294 $sti=$dbh->prepare("insert into z3950servers
295 values ('z3950.loc.gov',
299 'Library of Congress',
304 #---------------------------------
307 foreach $table ( keys %requirefields ) {
308 print "Check table $table\n" if $debug;
309 $sth=$dbh->prepare("show columns from $table");
312 while ( ($column, $type, $null, $key, $default, $extra)
314 $types{$column}=$type;
316 foreach $column ( keys %{ $requirefields{$table} } ) {
317 print " Check column $column\n" if $debug;
318 if ( ! $types{$column} ) {
319 # column doesn't exist
320 print "Adding $column field to $table table...\n";
321 $query="alter table $table
322 add column $column " . $requirefields{$table}->{$column} ;
323 print "Execute: $query\n" if $debug;
324 my $sti=$dbh->prepare($query);
327 print "**Error : $sti->errstr \n";
334 # Get list of columns from items table
337 $sth=$dbh->prepare("show columns from items");
339 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
340 $itemtypes{$column}=$type;
343 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
344 $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
347 print "Setting maximum barcode length to 20 (was $oldlength).\n";
348 my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
353 # extending the timestamp in branchtransfers...
356 $sth=$dbh->prepare("show columns from branchtransfers");
358 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
359 $branchtransfers{$column}=$type;
362 unless ($branchtransfers{'datesent'} eq 'datetime') {
363 print "Setting type of datesent in branchtransfers to datetime.\n";
364 my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
368 unless ($branchtransfers{'datearrived'} eq 'datetime') {
369 print "Setting type of datearrived in branchtransfers to datetime.\n";
370 my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
374 # changing the branchcategories table around...
375 my %branchcategories;
377 $sth=$dbh->prepare("show columns from branchcategories");
379 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
380 $branchcategories{$column}=$type;
383 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
384 print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
385 my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
387 $sti=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
391 unless ($branchcategories{'categoryname'} eq 'text') {
392 print "Changing branchcode in branchcategories to categoryname text.\n";
393 my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
397 unless ($branchcategories{'codedescription'} eq 'text') {
398 print "Replacing branchholding in branchcategories with codedescription text.\n";
399 my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
404 # Populate systempreferences if it is empty
406 foreach $prefitem ( keys %defaultprefs ) {
407 $sth=$dbh->prepare("select value
408 from systempreferences
410 $sth->execute($prefitem);
411 unless ($sth->rows) {
412 print "Adding system preference item $prefitem with value " .$defaultprefs{$prefitem}[0] ."\n";
413 $sti=$dbh->prepare("insert into systempreferences (variable, value,explanation) values (?,?,?)");
414 $sti->execute($prefitem,$defaultprefs{$prefitem}[0],$defaultprefs{$prefitem}[1]);
424 # Revision 1.23 2002/10/25 10:55:46 tipaul
426 # * bugfixes and improvements
427 # * manage mandatory MARC subfields
428 # * 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.
429 # this submit contains everything needed :
432 # * "links" management
433 # * creation of a html-list if a subfield is mapped to an authorised value.
435 # Note this is different from authorities support, which will come soon.
436 # 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...
438 # Revision 1.22 2002/10/15 10:08:19 tipaul
439 # fixme corrected, re-indent and adding the marc_breeding table (see commit of marcimport.pl for more explanations about breeding)
441 # Revision 1.21 2002/10/14 11:48:59 tipaul
444 # Revision 1.20 2002/10/10 04:49:41 arensb
445 # Added some FIXME comments.
447 # Revision 1.19 2002/10/05 10:17:17 arensb
448 # Merged with arensb-context branch: use C4::Context->dbh instead of
449 # &C4Connect, and generally prefer C4::Context over C4::Database.
451 # Revision 1.18.2.2 2002/10/05 06:18:43 arensb
452 # Added a whole mess of FIXME comments.
454 # Revision 1.18.2.1 2002/10/04 02:46:00 arensb
455 # Use C4::Connect instead of C4::Database, C4::Connect->dbh instead
458 # Revision 1.18 2002/09/24 13:50:55 tipaul
459 # long WAS the road to 1.3.0...
460 # coming VERY SOON NOW...
461 # modifying installer and buildrelease to update the DB
463 # Revision 1.17 2002/09/24 12:57:35 tipaul
464 # long WAS the road to 1.3.0...
465 # coming VERY SOON NOW...
466 # modifying installer and buildrelease to update the DB
468 # Revision 1.16 2002/07/31 02:34:27 finlayt
470 # added "notforloan" field to the itemtypes table.
472 # Revision 1.15 2002/07/20 22:30:06 rangi
473 # Making sure fix makes it into the main branch as well
476 # Revision 1.14 2002/07/08 16:20:26 tonnesen
477 # Added sessionqueries table and password/userid fields to borrowers table
479 # Revision 1.13 2002/07/04 18:05:36 tonnesen
482 # Revision 1.12 2002/07/04 16:41:06 tonnesen
483 # Merged changes from rel-1-2. Abstracted table structure changes by alan.