fix the previous patch to add a syspref that permit to choose the renewal period...
[koha.git] / installer / data / mysql / updatedatabase30.pl
1 #!/usr/bin/perl
2
3
4 # Database Updater
5 # This script checks for required updates to the database.
6
7 # Part of the Koha Library Software www.koha.org
8 # Licensed under the GPL.
9
10 # Bugs/ToDo:
11 # - Would also be a good idea to offer to do a backup at this time...
12
13 # NOTE:  If you do something more than once in here, make it table driven.
14
15 # NOTE: Please keep the version in kohaversion.pl up-to-date!
16
17 use strict;
18 # use warnings;
19
20 # CPAN modules
21 use DBI;
22 use Getopt::Long;
23 # Koha modules
24 use C4::Context;
25 use C4::Installer;
26
27 use MARC::Record;
28 use MARC::File::XML ( BinaryEncoding => 'utf8' );
29  
30 # FIXME - The user might be installing a new database, so can't rely
31 # on /etc/koha.conf anyway.
32
33 my $debug = 0;
34
35 my (
36     $sth, $sti,
37     $query,
38     %existingtables,    # tables already in database
39     %types,
40     $table,
41     $column,
42     $type, $null, $key, $default, $extra,
43     $prefitem,          # preference item in systempreferences table
44 );
45
46 my $silent;
47 GetOptions(
48     's' =>\$silent
49     );
50 my $dbh = C4::Context->dbh;
51 $|=1; # flushes output
52
53 =item
54
55     Deal with virtualshelves
56
57 =cut
58
59 my $DBversion = '3.00.01.000';
60 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
61     print "Upgrade to $DBversion done (start of 3.0.1)\n";
62     SetVersion ($DBversion);
63 }
64
65 $DBversion = '3.00.01.001';
66 if ( C4::Context->preference('Version') < TransformToNum($DBversion) ) {
67     # use statistics where available
68     $dbh->do("
69         ALTER TABLE statistics ADD KEY  tmp_stats (type, itemnumber, borrowernumber)
70     ");
71     $dbh->do("
72         UPDATE issues iss
73         SET issuedate = (
74             SELECT max(datetime)
75             FROM statistics 
76             WHERE type = 'issue'
77             AND itemnumber = iss.itemnumber
78             AND borrowernumber = iss.borrowernumber
79         )
80         WHERE issuedate IS NULL;
81     ");  
82     $dbh->do("ALTER TABLE statistics DROP KEY tmp_stats");
83
84     # default to last renewal date
85     $dbh->do("
86         UPDATE issues
87         SET issuedate = lastreneweddate
88         WHERE issuedate IS NULL
89         and lastreneweddate IS NOT NULL
90     ");
91
92     my $num_bad_issuedates = $dbh->selectrow_array("SELECT COUNT(*) FROM issues WHERE issuedate IS NULL");
93     if ($num_bad_issuedates > 0) {
94         print STDERR "After the upgrade to $DBversion, there are still $num_bad_issuedates loan(s) with a NULL (blank) loan date. ",
95                      "Please check the issues table in your database.";
96     }
97     print "Upgrade to $DBversion done (bug 2582: set null issues.issuedate to lastreneweddate)";
98     SetVersion($DBversion);
99 }
100
101 $DBversion = "3.00.01.002";
102 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
103     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('AllowRenewalLimitOverride', '0', 'if ON, allows renewal limits to be overridden on the circulation screen',NULL,'YesNo')");
104     print "Upgrade to $DBversion done (add new syspref)\n";
105     SetVersion ($DBversion);
106 }
107
108 $DBversion = "3.00.01.003";
109 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
110     $dbh->do("INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('MergeAuthoritiesOnUpdate', '1', 'if ON, Updateing authorities will automatically updates biblios',NULL,'YesNo')");
111     print "Upgrade to $DBversion done (add new syspref)\n";
112     SetVersion ($DBversion);
113 }
114
115 $DBversion = "3.00.01.004";
116 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
117   if (lc(C4::Context->preference('marcflavour')) eq "unimarc"){
118     $dbh->do("INSERT IGNORE INTO `marc_tag_structure` (`tagfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `authorised_value`, `frameworkcode`) VALUES ('099', 'Informations locales', '', 0, 0, '', '');");
119     $dbh->do("INSERT IGNORE INTO `marc_tag_structure` (`frameworkcode`,`tagfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `authorised_value`) SELECT DISTINCT(frameworkcode),'099', 'Informations locales', '', 0, 0, '' from biblio_framework");
120     $dbh->do(<<ENDOFSQL);
121 INSERT IGNORE INTO marc_subfield_structure (`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value`, `authtypecode`, `value_builder`, `isurl`, `hidden`, `seealso`, `link`, `defaultvalue`,frameworkcode )
122 VALUES ('099', 'c', 'date creation notice (koha)', '', 0, 0, 'biblio.datecreated', -1, '', '', '', NULL, 0, '', '', NULL, ''),
123 ('099', 'd', 'date modification notice (koha)', '', 0, 0, 'biblio.timestamp', -1, '', '', '', NULL, 0, '', '', NULL, ''),
124 ('995', '2', 'Perdu', '', 0, 0, 'items.itemlost', 10, '', '', '', NULL, 1, '', NULL, NULL, '');
125 ENDOFSQL
126     $dbh->do(<<ENDOFSQL1);
127 INSERT IGNORE INTO marc_subfield_structure (`frameworkcode`,`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value`, `authtypecode`, `value_builder`, `isurl`, `hidden`, seealso, link, defaultvalue )
128 SELECT DISTINCT(frameworkcode), '099', 'c', 'date creation notice (koha)', '', 0, 0, 'biblio.datecreated', -1, '', '', '', NULL, 0, '', '', NULL from biblio_framework;
129 ENDOFSQL1
130     $dbh->do(<<ENDOFSQL2);
131 INSERT IGNORE INTO marc_subfield_structure (`frameworkcode`,`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value`, `authtypecode`, `value_builder`, `isurl`, `hidden`, seealso, link, defaultvalue )
132 SELECT DISTINCT(frameworkcode), '099', 'd', 'date modification notice (koha)', '', 0, 0, 'biblio.timestamp', -1, '', '', '', NULL, 0, '', '', NULL from biblio_framework;
133 ENDOFSQL2
134     $dbh->do(<<ENDOFSQL3);
135 INSERT IGNORE INTO marc_subfield_structure (`frameworkcode`,`tagfield`, `tagsubfield`, `liblibrarian`, `libopac`, `repeatable`, `mandatory`, `kohafield`, `tab`, `authorised_value`, `authtypecode`, `value_builder`, `isurl`, `hidden`, seealso, link, defaultvalue )
136 SELECT DISTINCT(frameworkcode), '995', '2', 'Perdu', '', 0, 0, 'items.itemlost', 10, '', '', '', NULL, 1, '', NULL, NULL from biblio_framework;
137 ENDOFSQL3
138       print "Upgrade to $DBversion done (updates MARC framework structure)\n";
139     }
140     SetVersion ($DBversion);
141 }
142
143 $DBversion = "3.00.01.005";
144 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
145    $dbh->do(<<ENDOFNOTFORLOANOVERRIDE);
146 INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('AllowNotForLoanOverride', '0', 'if ON, enables the librarian to choose when they want to check out a notForLoan regular item',NULL,'YesNo')
147 ENDOFNOTFORLOANOVERRIDE
148       print "Upgrade to $DBversion done (Adding AllowNotForLoanOverride System preference)\n";
149 }
150
151 $DBversion = "3.00.01.005";
152 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
153     $dbh->do("ALTER TABLE issuingrules ADD COLUMN `finedays` int(11) default NULL AFTER `fine`");
154     print "Upgrade to $DBversion done (Adding a field in issuingrules table)\n";
155     SetVersion ($DBversion);
156 }
157
158 $DBversion = "3.00.01.006";
159 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
160     $dbh->do("ALTER TABLE `aqbudget` CHANGE `aqbudgetid` `aqbudgetid` INT( 11 ) NOT NULL AUTO_INCREMENT");
161     print "Upgrade to $DBversion done (Change the field)\n";
162     SetVersion ($DBversion);
163 }
164
165 $DBversion = "3.00.01.007";
166 if (C4::Context->preference("Version") < TransformToNum($DBversion)) {
167     $dbh->do(<<ENDOFRENEWAL);
168 INSERT INTO systempreferences (variable,value,explanation,options,type) VALUES('RenewalPeriodBase', 'date_due', 'Set whether the renewal date should be counted from the date_due or from the moment the Patron asks for renewal ','date_due|now','Choice');
169 ENDOFRENEWAL
170     print "Upgrade to $DBversion done (Change the field)\n";
171     SetVersion ($DBversion);
172 }
173
174 =item DropAllForeignKeys($table)
175
176   Drop all foreign keys of the table $table
177
178 =cut
179
180 sub DropAllForeignKeys {
181     my ($table) = @_;
182     # get the table description
183     my $sth = $dbh->prepare("SHOW CREATE TABLE $table");
184     $sth->execute;
185     my $vsc_structure = $sth->fetchrow;
186     # split on CONSTRAINT keyword
187     my @fks = split /CONSTRAINT /,$vsc_structure;
188     # parse each entry
189     foreach (@fks) {
190         # isolate what is before FOREIGN KEY, if there is something, it's a foreign key to drop
191         $_ = /(.*) FOREIGN KEY.*/;
192         my $id = $1;
193         if ($id) {
194             # we have found 1 foreign, drop it
195             $dbh->do("ALTER TABLE $table DROP FOREIGN KEY $id");
196             $id="";
197         }
198     }
199 }
200
201
202 =item TransformToNum
203
204   Transform the Koha version from a 4 parts string
205   to a number, with just 1 .
206
207 =cut
208
209 sub TransformToNum {
210     my $version = shift;
211     # remove the 3 last . to have a Perl number
212     $version =~ s/(.*\..*)\.(.*)\.(.*)/$1$2$3/;
213     return $version;
214 }
215
216 =item SetVersion
217
218     set the DBversion in the systempreferences
219
220 =cut
221
222 sub SetVersion {
223     my $kohaversion = TransformToNum(shift);
224     if (C4::Context->preference('Version')) {
225       my $finish=$dbh->prepare("UPDATE systempreferences SET value=? WHERE variable='Version'");
226       $finish->execute($kohaversion);
227     } else {
228       my $finish=$dbh->prepare("INSERT into systempreferences (variable,value,explanation) values ('Version',?,'The Koha database version. WARNING: Do not change this value manually, it is maintained by the webinstaller')");
229       $finish->execute($kohaversion);
230     }
231 }
232 exit;
233