6 # This script checks for required updates to the database.
8 # Part of the Koha Library Software www.koha.org
9 # Licensed under the GPL.
12 # - Would also be a good idea to offer to do a backup at this time...
14 # NOTE: If you do something more than once in here, make it table driven.
24 use MARC::File::XML ( BinaryEncoding => 'utf8' );
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
46 my $dbh = C4::Context->dbh;
47 $|=1; # flushes output
50 Deal with virtualshelves
53 my $DBversion = "3.00.00.001";
54 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
55 # update virtualshelves table to
57 $dbh->do("ALTER TABLE `bookshelf` RENAME `virtualshelves`");
58 $dbh->do("ALTER TABLE `shelfcontents` RENAME `virtualshelfcontents`");
59 $dbh->do("ALTER TABLE `virtualshelfcontents` ADD `biblionumber` INT( 11 ) NOT NULL");
60 $dbh->do("UPDATE `virtualshelfcontents` SET biblionumber=(SELECT biblionumber FROM items WHERE items.itemnumber=virtualshelfcontents.itemnumber)");
61 # drop all foreign keys : otherwise, we can't drop itemnumber field.
62 DropAllForeignKeys('virtualshelfcontents');
63 # create the new foreign keys (on biblionumber)
64 $dbh->do("ALTER TABLE `virtualshelfcontents` ADD FOREIGN KEY biblionumber_fk (biblionumber) REFERENCES biblio (biblionumber) ON UPDATE CASCADE ON DELETE CASCADE");
65 # re-create the foreign key on virtualshelf
66 $dbh->do("ALTER TABLE `virtualshelfcontents` ADD FOREIGN KEY shelfnumber_fk (shelfnumber) REFERENCES virtualshelves (shelfnumber) ON UPDATE CASCADE ON DELETE CASCADE");
67 # now we can drop the itemnumber column
68 $dbh->do("ALTER TABLE `virtualshelfcontents` DROP `itemnumber`");
69 print "Upgrade to $DBversion done (virtualshelves)\n";
70 SetVersion ($DBversion);
74 $DBversion = "3.00.00.002";
75 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
76 $dbh->do("DROP TABLE sessions");
77 $dbh->do("CREATE TABLE `sessions` (
78 `id` char(32) NOT NULL,
79 `a_session` text NOT NULL,
80 UNIQUE KEY `id` (`id`)
81 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
82 print "Upgrade to $DBversion done (sessions uses CGI::session, new table structure for sessions)\n";
83 SetVersion ($DBversion);
87 $DBversion = "3.00.00.003";
88 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
89 if (C4::Context->preference("opaclanguage") eq "fr") {
90 $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('ReservesNeedReturns','0','Si ce paramètre est mis à 1, une réservation posée sur un exemplaire présent sur le site devra être passée en retour pour être disponible. Sinon, elle sera automatiquement disponible, Koha considère que le bibliothécaire place la réservation en ayant le document en mains','','YesNo')");
92 $dbh->do("INSERT INTO `systempreferences` (variable,value,explanation,options,type) VALUES('ReservesNeedReturns','0','If set, a reserve done on an item available in this branch need a check-in, otherwise, a reserve on a specific item, that is on the branch & available is considered as available','','YesNo')");
94 print "Upgrade to $DBversion done (adding ReservesNeedReturns systempref, in circulation)\n";
95 SetVersion ($DBversion);
99 $DBversion = "3.00.00.004";
100 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
101 $dbh->do("INSERT INTO `systempreferences` VALUES ('DebugLevel','2','set the level of error info sent to the browser. 0=none, 1=some, 2=most','0|1|2','Choice')");
102 print "Upgrade to $DBversion done (adding DebugLevel systempref, in 'Admin' tab)\n";
103 SetVersion ($DBversion);
106 $DBversion = "3.00.00.005";
107 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
108 $dbh->do("CREATE TABLE `tags` (
109 `entry` varchar(255) NOT NULL default '',
110 `weight` bigint(20) NOT NULL default 0,
111 PRIMARY KEY (`entry`)
112 ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
114 $dbh->do("CREATE TABLE `nozebra` (
115 `server` varchar(20) NOT NULL,
116 `indexname` varchar(40) NOT NULL,
117 `value` varchar(250) NOT NULL,
118 `biblionumbers` longtext NOT NULL,
119 KEY `indexname` (`server`,`indexname`),
120 KEY `value` (`server`,`value`))
121 ENGINE=InnoDB DEFAULT CHARSET=utf8;
123 print "Upgrade to $DBversion done (adding tags and nozebra tables )\n";
124 SetVersion ($DBversion);
127 $DBversion = "3.00.00.006";
128 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
129 $dbh->do("UPDATE issues SET issuedate=timestamp WHERE issuedate='0000-00-00'");
130 print "Upgrade to $DBversion done (filled issues.issuedate with timestamp)\n";
131 SetVersion ($DBversion);
134 $DBversion = "3.00.00.007";
135 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
136 $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES ('SessionStorage','mysql','Use mysql or a temporary file for storing session data','mysql|tmp','Choice')");
137 print "Upgrade to $DBversion done (set SessionStorage variable)\n";
138 SetVersion ($DBversion);
141 =item DropAllForeignKeys($table)
143 Drop all foreign keys of the table $table
147 sub DropAllForeignKeys {
149 # get the table description
150 my $sth = $dbh->prepare("SHOW CREATE TABLE $table");
152 my $vsc_structure = $sth->fetchrow;
153 # split on CONSTRAINT keyword
154 my @fks = split /CONSTRAINT /,$vsc_structure;
157 # isolate what is before FOREIGN KEY, if there is something, it's a foreign key to drop
158 $_ = /(.*) FOREIGN KEY.*/;
161 # we have found 1 foreign, drop it
162 $dbh->do("ALTER TABLE $table DROP FOREIGN KEY $id");
176 Transform the Koha version from a 4 parts string
177 to a number, with just 1 .
183 # remove the 3 last . to have a Perl number
184 $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
189 set the DBversion in the systempreferences
193 my $kohaversion = TransformToNum(shift);
194 if (C4::Context->preference('Version')) {
195 my $finish=$dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'");
196 $finish->execute($kohaversion);
198 my $finish=$dbh->prepare("INSERT into systempreferences (variable,value,explanation) values ('Version',?,'The Koha database version. Don t change this value manually, it s holded by the webinstaller')");
199 $finish->execute($kohaversion);
205 # Revision 1.172 2007/07/19 10:21:22 hdl