splitting updatedatabase in 2 parts
[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 use strict;
16
17 # CPAN modules
18 use DBI;
19 use Getopt::Long;
20 # Koha modules
21 use C4::Context;
22
23 use MARC::Record;
24 use MARC::File::XML ( BinaryEncoding => 'utf8' );
25  
26 # FIXME - The user might be installing a new database, so can't rely
27 # on /etc/koha.conf anyway.
28
29 my $debug = 0;
30
31 my (
32     $sth, $sti,
33     $query,
34     %existingtables,    # tables already in database
35     %types,
36     $table,
37     $column,
38     $type, $null, $key, $default, $extra,
39     $prefitem,          # preference item in systempreferences table
40 );
41
42 my $silent;
43 GetOptions(
44     's' =>\$silent
45     );
46 my $dbh = C4::Context->dbh;
47 $|=1; # flushes output
48
49 =item
50     Deal with virtualshelves
51 =cut
52
53 my $DBversion = "3.00.00.001";
54 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
55     # update virtualshelves table to
56     # 
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);
71 }
72
73
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);
84 }
85
86
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')");
91     } else {
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')");
93     }
94     print "Upgrade to $DBversion done (adding ReservesNeedReturns systempref, in circulation)\n";
95     SetVersion ($DBversion);
96 }
97
98
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);
104 }
105
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;
113                 ");
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;
122                 ");
123     print "Upgrade to $DBversion done (adding tags and nozebra tables )\n";
124     SetVersion ($DBversion);
125 }
126
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);
132 }
133
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);
139 }
140
141 =item DropAllForeignKeys($table)
142
143   Drop all foreign keys of the table $table
144   
145 =cut
146
147 sub DropAllForeignKeys {
148     my ($table) = @_;
149     # get the table description
150     my $sth = $dbh->prepare("SHOW CREATE TABLE $table");
151     $sth->execute;
152     my $vsc_structure = $sth->fetchrow;
153     # split on CONSTRAINT keyword
154     my @fks = split /CONSTRAINT /,$vsc_structure;
155     # parse each entry
156     foreach (@fks) {
157         # isolate what is before FOREIGN KEY, if there is something, it's a foreign key to drop
158         $_ = /(.*) FOREIGN KEY.*/;
159         my $id = $1;
160         if ($id) {
161             # we have found 1 foreign, drop it
162             $dbh->do("ALTER TABLE $table DROP FOREIGN KEY $id");
163             $id="";
164         }
165     }
166 }
167
168
169
170
171
172
173
174 =item TransformToNum
175
176   Transform the Koha version from a 4 parts string
177   to a number, with just 1 .
178   
179 =cut
180
181 sub TransformToNum {
182     my $version = shift;
183     # remove the 3 last . to have a Perl number
184     $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
185     return $version;
186 }
187
188 =item SetVersion
189     set the DBversion in the systempreferences
190 =cut
191
192 sub SetVersion {
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);
197     } else {
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);
200     }
201 }
202 exit;
203
204 # $Log$
205 # Revision 1.172  2007/07/19 10:21:22  hdl