Koha/updater/updatedatabase
Paul POULAIN 6344ec8140 splitting updatedatabase in 2 parts
- update 22to30, that does the update from 22 to 3.0 using old mechanism.
- updatedatabase, that just does new updating stuff.

Note that there are fixes on some column size (next commit will contain fixed kohastructure.sql)

Signed-off-by: Chris Cormack <crc@liblime.com>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
2007-10-08 15:54:05 -05:00

205 lines
7.2 KiB
Perl
Executable file

#!/usr/bin/perl
# $Id$
# Database Updater
# This script checks for required updates to the database.
# Part of the Koha Library Software www.koha.org
# Licensed under the GPL.
# Bugs/ToDo:
# - Would also be a good idea to offer to do a backup at this time...
# NOTE: If you do something more than once in here, make it table driven.
use strict;
# CPAN modules
use DBI;
use Getopt::Long;
# Koha modules
use C4::Context;
use MARC::Record;
use MARC::File::XML ( BinaryEncoding => 'utf8' );
# FIXME - The user might be installing a new database, so can't rely
# on /etc/koha.conf anyway.
my $debug = 0;
my (
$sth, $sti,
$query,
%existingtables, # tables already in database
%types,
$table,
$column,
$type, $null, $key, $default, $extra,
$prefitem, # preference item in systempreferences table
);
my $silent;
GetOptions(
's' =>\$silent
);
my $dbh = C4::Context->dbh;
$|=1; # flushes output
=item
Deal with virtualshelves
=cut
my $DBversion = "3.00.00.001";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
# update virtualshelves table to
#
$dbh->do("ALTER TABLE `bookshelf` RENAME `virtualshelves`");
$dbh->do("ALTER TABLE `shelfcontents` RENAME `virtualshelfcontents`");
$dbh->do("ALTER TABLE `virtualshelfcontents` ADD `biblionumber` INT( 11 ) NOT NULL");
$dbh->do("UPDATE `virtualshelfcontents` SET biblionumber=(SELECT biblionumber FROM items WHERE items.itemnumber=virtualshelfcontents.itemnumber)");
# drop all foreign keys : otherwise, we can't drop itemnumber field.
DropAllForeignKeys('virtualshelfcontents');
# create the new foreign keys (on biblionumber)
$dbh->do("ALTER TABLE `virtualshelfcontents` ADD FOREIGN KEY biblionumber_fk (biblionumber) REFERENCES biblio (biblionumber) ON UPDATE CASCADE ON DELETE CASCADE");
# re-create the foreign key on virtualshelf
$dbh->do("ALTER TABLE `virtualshelfcontents` ADD FOREIGN KEY shelfnumber_fk (shelfnumber) REFERENCES virtualshelves (shelfnumber) ON UPDATE CASCADE ON DELETE CASCADE");
# now we can drop the itemnumber column
$dbh->do("ALTER TABLE `virtualshelfcontents` DROP `itemnumber`");
print "Upgrade to $DBversion done (virtualshelves)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.00.002";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("DROP TABLE sessions");
$dbh->do("CREATE TABLE `sessions` (
`id` char(32) NOT NULL,
`a_session` text NOT NULL,
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
print "Upgrade to $DBversion done (sessions uses CGI::session, new table structure for sessions)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.00.003";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
if (C4::Context->preference("opaclanguage") eq "fr") {
$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')");
} else {
$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')");
}
print "Upgrade to $DBversion done (adding ReservesNeedReturns systempref, in circulation)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.00.004";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$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')");
print "Upgrade to $DBversion done (adding DebugLevel systempref, in 'Admin' tab)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.00.005";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("CREATE TABLE `tags` (
`entry` varchar(255) NOT NULL default '',
`weight` bigint(20) NOT NULL default 0,
PRIMARY KEY (`entry`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
$dbh->do("CREATE TABLE `nozebra` (
`server` varchar(20) NOT NULL,
`indexname` varchar(40) NOT NULL,
`value` varchar(250) NOT NULL,
`biblionumbers` longtext NOT NULL,
KEY `indexname` (`server`,`indexname`),
KEY `value` (`server`,`value`))
ENGINE=InnoDB DEFAULT CHARSET=utf8;
");
print "Upgrade to $DBversion done (adding tags and nozebra tables )\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.00.006";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$dbh->do("UPDATE issues SET issuedate=timestamp WHERE issuedate='0000-00-00'");
print "Upgrade to $DBversion done (filled issues.issuedate with timestamp)\n";
SetVersion ($DBversion);
}
$DBversion = "3.00.00.007";
if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
$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')");
print "Upgrade to $DBversion done (set SessionStorage variable)\n";
SetVersion ($DBversion);
}
=item DropAllForeignKeys($table)
Drop all foreign keys of the table $table
=cut
sub DropAllForeignKeys {
my ($table) = @_;
# get the table description
my $sth = $dbh->prepare("SHOW CREATE TABLE $table");
$sth->execute;
my $vsc_structure = $sth->fetchrow;
# split on CONSTRAINT keyword
my @fks = split /CONSTRAINT /,$vsc_structure;
# parse each entry
foreach (@fks) {
# isolate what is before FOREIGN KEY, if there is something, it's a foreign key to drop
$_ = /(.*) FOREIGN KEY.*/;
my $id = $1;
if ($id) {
# we have found 1 foreign, drop it
$dbh->do("ALTER TABLE $table DROP FOREIGN KEY $id");
$id="";
}
}
}
=item TransformToNum
Transform the Koha version from a 4 parts string
to a number, with just 1 .
=cut
sub TransformToNum {
my $version = shift;
# remove the 3 last . to have a Perl number
$version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
return $version;
}
=item SetVersion
set the DBversion in the systempreferences
=cut
sub SetVersion {
my $kohaversion = TransformToNum(shift);
if (C4::Context->preference('Version')) {
my $finish=$dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'");
$finish->execute($kohaversion);
} else {
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')");
$finish->execute($kohaversion);
}
}
exit;
# $Log$
# Revision 1.172 2007/07/19 10:21:22 hdl