Change to the way the duplicate warning works and displays. Streamlines not-a-duplica...
[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 $DBversion = "3.00.00.008";
142 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
143         $dbh->do("ALTER TABLE `biblio` ADD `datecreated` DATE NOT NULL AFTER `timestamp` ;");
144         $dbh->do("UPDATE biblio SET datecreated=timestamp");
145     print "Upgrade to $DBversion done (biblio creation date)\n";
146     SetVersion ($DBversion);
147 }
148
149 =item DropAllForeignKeys($table)
150
151   Drop all foreign keys of the table $table
152   
153 =cut
154
155 sub DropAllForeignKeys {
156     my ($table) = @_;
157     # get the table description
158     my $sth = $dbh->prepare("SHOW CREATE TABLE $table");
159     $sth->execute;
160     my $vsc_structure = $sth->fetchrow;
161     # split on CONSTRAINT keyword
162     my @fks = split /CONSTRAINT /,$vsc_structure;
163     # parse each entry
164     foreach (@fks) {
165         # isolate what is before FOREIGN KEY, if there is something, it's a foreign key to drop
166         $_ = /(.*) FOREIGN KEY.*/;
167         my $id = $1;
168         if ($id) {
169             # we have found 1 foreign, drop it
170             $dbh->do("ALTER TABLE $table DROP FOREIGN KEY $id");
171             $id="";
172         }
173     }
174 }
175
176
177
178
179
180
181
182 =item TransformToNum
183
184   Transform the Koha version from a 4 parts string
185   to a number, with just 1 .
186   
187 =cut
188
189 sub TransformToNum {
190     my $version = shift;
191     # remove the 3 last . to have a Perl number
192     $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
193     return $version;
194 }
195
196 =item SetVersion
197     set the DBversion in the systempreferences
198 =cut
199
200 sub SetVersion {
201     my $kohaversion = TransformToNum(shift);
202     if (C4::Context->preference('Version')) {
203       my $finish=$dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'");
204       $finish->execute($kohaversion);
205     } else {
206       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')");
207       $finish->execute($kohaversion);
208     }
209 }
210 exit;
211
212 # $Log$
213 # Revision 1.172  2007/07/19 10:21:22  hdl