Merged with arensb-context branch: use C4::Context->dbh instead of
[koha.git] / updater / updatedatabase2.pl
1 #!/usr/bin/perl
2
3 # WARNING : this script is intended to be run as is only on a sample DB.
4 # in the sample DB downloadable from sourceforge, the aqbookfund table is full of trash data.
5 # this script drops this table and refill it with correct data.
6 # some tables have strange data too, so primary key cannot be created.
7
8 # if you want to apply this patch on a running DB :
9 # - do a backup !
10 # - remove the lines between <FORPRODDEL> and </FORPRODDEL>
11 # - remove the # before the lines between <FORPRODADD> and </FORPRODADD>
12 #    this will cause a primary key to be created if possible on your DB
13 # YOU'VE BEEN WARNING !!!!!
14
15
16
17 # Copyright 2000-2002 Katipo Communications
18 #
19 # This file is part of Koha.
20 #
21 # Koha is free software; you can redistribute it and/or modify it under the
22 # terms of the GNU General Public License as published by the Free Software
23 # Foundation; either version 2 of the License, or (at your option) any later
24 # version.
25 #
26 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
27 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
28 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
29 #
30 # You should have received a copy of the GNU General Public License along with
31 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
32 # Suite 330, Boston, MA  02111-1307 USA
33
34 use C4::Context;
35 use C4::Catalogue;
36 use DBI;
37 use C4::Acquisitions;
38 use C4::Output;
39
40 sub droptable {
41         my ($dbh,$tablename)=@_;
42         if ($tables{$tablename}) {      # FIXME - $tables undefined
43                 print "     - $tablename...\n";
44                 my $sti=$dbh->prepare("DROP TABLE $tablename");
45                 $sti->execute;
46                 return 1;
47         }
48         return 0;
49 }
50 sub dosql {
51         my ($dbh,$sql_cmd)=@_;
52         my $sti=$dbh->prepare($sql_cmd);
53         $sti->execute;
54         if ($sti->err) {
55                 print "error : ".$sti->errstr." \n tried to execute : $sql_cmd\n";
56                 $sti->finish;
57         }
58 }
59
60
61 my $dbh = C4::Context->dbh;
62
63 my %tables;
64 my $sth=$dbh->prepare("show tables");
65 $sth->execute;
66 while (my ($table) = $sth->fetchrow) {
67     $tables{$table}=1;
68 }
69
70 print "creating thesaurus...\n";
71 dosql($dbh,"CREATE TABLE bibliothesaurus (code BIGINT not null AUTO_INCREMENT, freelib CHAR (255) not null , stdlib CHAR (255) not null , type CHAR (80) not null , PRIMARY KEY (code), INDEX (freelib),index(stdlib),index(type))");
72         my $sti=$dbh->prepare("select subject from bibliosubject");
73         $sti->execute;
74         $i=0;
75         while ($line =$sti->fetchrow_hashref) {
76                 $i++;
77                 print "$i $line->{'subject'}\n";
78                 $sti2=$dbh->prepare("select count(*) as t from bibliothesaurus where freelib=".$dbh->quote($line->{'subject'}));
79                 $sti2->execute;
80                 if ($sti2->err) {
81                         print "error : ".$sti2->errstr." \n tried to execute : $sql_cmd\n";
82                         # FIXME - $sql_cmd undefined
83                         die;
84                 }
85                 $line2=$sti2->fetchrow_hashref;
86                 if ($line2->{'t'} ==0) {
87                         dosql($dbh,"insert into bibliothesaurus (freelib,stdlib) values (".$dbh->quote($line->{'subject'}).",".$dbh->quote($line->{'subject'}).")");
88                 } else {
89                         print "pas ecriture pour : $line->{'subject'}\n";
90                 }
91
92         }
93
94 #aqbookfund : the sample db is full of trash data. Delete and recreate
95 # <FORPRODDEL>
96         print "aqbookfund...";
97         dosql($dbh,"delete from aqbookfund");
98         dosql($dbh,"INSERT INTO aqbookfund VALUES( '5', 'Young Adult Fiction', '3')");
99         dosql($dbh,"INSERT INTO aqbookfund VALUES( '4', 'Fiction', '2')");
100         dosql($dbh,"INSERT INTO aqbookfund VALUES( '2', 'Talking books', '4')");
101         dosql($dbh,"INSERT INTO aqbookfund VALUES( '13', 'Newspapers & journals', '4')");
102         dosql($dbh,"INSERT INTO aqbookfund VALUES( '10', 'Te Ao Maori', '1')");
103         dosql($dbh,"INSERT INTO aqbookfund VALUES( '15', 'CDs, CD Roms, Maps, etc', '4')");
104         dosql($dbh,"INSERT INTO aqbookfund VALUES( '9', 'Junior Fiction', '3')");
105         dosql($dbh,"INSERT INTO aqbookfund VALUES( '7', 'Junior Non-Fiction', '3')");
106         dosql($dbh,"INSERT INTO aqbookfund VALUES( '72', 'Creative NZ', '4')");
107         dosql($dbh,"INSERT INTO aqbookfund VALUES( '11', 'Reference', '1')");
108         dosql($dbh,"INSERT INTO aqbookfund VALUES( '12', 'Videos', '4')");
109         dosql($dbh,"INSERT INTO aqbookfund VALUES( '8', 'Junior Paperback', '3')");
110         dosql($dbh,"INSERT INTO aqbookfund VALUES( '73', 'Large Print Link-up', '2')");
111         dosql($dbh,"INSERT INTO aqbookfund VALUES( '3', 'Large Print', '2')");
112         dosql($dbh,"INSERT INTO aqbookfund VALUES( '71', 'Creative NZ NonFiction', '1')");
113         dosql($dbh,"INSERT INTO aqbookfund VALUES( '6', 'Picture Books', '3')");
114         dosql($dbh,"INSERT INTO aqbookfund VALUES( '14', 'Nga pukapuka Maori', '3')");
115         dosql($dbh,"INSERT INTO aqbookfund VALUES( '80', 'Donations, junior books', '5')");
116         dosql($dbh,"INSERT INTO aqbookfund VALUES( '81', 'Donations, adult books', '5')");
117         dosql($dbh,"INSERT INTO aqbookfund VALUES( '82', 'Donations, magazines', '5')");
118         dosql($dbh,"INSERT INTO aqbookfund VALUES( '83', 'Donations, non books', '5')");
119         dosql($dbh,"INSERT INTO aqbookfund VALUES( '91', 'Vertical File', '5')");
120         dosql($dbh,"INSERT INTO aqbookfund VALUES( '100', 'Loans', '6')");
121         dosql($dbh,"INSERT INTO aqbookfund VALUES( '1', 'Test', '1')");
122 #</FORPRODDEL>
123         dosql($dbh,"ALTER TABLE aqbookfund DROP PRIMARY KEY, ADD PRIMARY KEY(bookfundid);");
124         print "done\n accountlines...";
125         dosql($dbh,"ALTER TABLE accountlines CHANGE itemnumber itemnumber INT (11) not null;");
126         dosql($dbh,"ALTER TABLE accountlines DROP PRIMARY KEY, ADD PRIMARY KEY(borrowernumber,accountno,itemnumber);");
127 # accountoffset not done (not possible ?)
128 #additionalauthor : not possible (no field useable, 1 index OK)
129         print "done\n aqbooksellers...";
130         dosql($dbh,"ALTER TABLE aqbooksellers CHANGE id id INT (11) not null;");
131         dosql($dbh,"ALTER TABLE aqbooksellers DROP PRIMARY KEY, ADD PRIMARY KEY(id);");
132         print "done\n aqbudget...";
133         dosql($dbh,"ALTER TABLE aqbudget DROP PRIMARY KEY, ADD PRIMARY KEY(bookfundid, bookfundid);");
134         print "done\n aqorderbreakdown...";
135         dosql($dbh,"ALTER TABLE aqorderbreakdown CHANGE ordernumber ordernumber INT (11) not null;");
136         dosql($dbh,"ALTER TABLE aqorderbreakdown CHANGE linenumber linenumber INT (11) not null;");
137         dosql($dbh,"ALTER TABLE aqorderbreakdown CHANGE branchcode branchcode CHAR (4) not null;");
138 #<FORPRODADD>
139 #       ALTER TABLE aqorderbreakdown DROP PRIMARY KEY, ADD PRIMARY KEY(ordernumber,linenumber,branchcode);");
140 #</FORPRODADD>
141
142         print "done\n biblio/borrowers...";
143         dosql($dbh,"ALTER TABLE aqorderdelivery DROP PRIMARY KEY, ADD PRIMARY KEY(ordernumber);");
144         dosql($dbh,"ALTER TABLE aqorders DROP PRIMARY KEY, ADD PRIMARY KEY(ordernumber);");
145         dosql($dbh,"ALTER TABLE biblio DROP PRIMARY KEY, ADD PRIMARY KEY(biblionumber);");
146         dosql($dbh,"ALTER TABLE biblioitems DROP PRIMARY KEY, ADD PRIMARY KEY(biblionumber, biblioitemnumber)");
147         dosql($dbh,"CREATE INDEX SUBTITLE ON bibliosubtitle (subtitle(80))");
148         dosql($dbh,"ALTER TABLE borexp CHANGE borrowernumber borrowernumber INT (11) not null");
149         dosql($dbh,"ALTER TABLE borexp CHANGE newexp newexp DATE not null");
150         dosql($dbh,"ALTER TABLE branches DROP PRIMARY KEY, ADD PRIMARY KEY(branchcode)");
151         dosql($dbh,"ALTER TABLE deletedbiblio DROP PRIMARY KEY, ADD PRIMARY KEY(biblionumber, biblionumber)");
152         dosql($dbh,"ALTER TABLE deletedbiblioitems DROP PRIMARY KEY, ADD PRIMARY KEY(biblioitemnumber)");
153         dosql($dbh,"ALTER TABLE deletedborrowers DROP PRIMARY KEY, ADD PRIMARY KEY(borrowernumber)");
154         dosql($dbh,"ALTER TABLE deleteditems DROP PRIMARY KEY, ADD PRIMARY KEY(itemnumber)");
155         dosql($dbh,"ALTER TABLE issues CHANGE date_due date_due DATE not null");
156 #<FORPRODADD>
157 # ALTER TABLE issues DROP PRIMARY KEY, ADD PRIMARY KEY(borrowernumber,itemnumber,date_due)");
158 #</FORPRODADD>
159         print "done\n items...";
160         dosql($dbh,"ALTER TABLE items DROP PRIMARY KEY, ADD PRIMARY KEY(itemnumber)");
161         dosql($dbh,"ALTER TABLE itemsprices CHANGE itemnumber itemnumber INT (11) not null");
162         dosql($dbh,"ALTER TABLE itemsprices DROP PRIMARY KEY, ADD PRIMARY KEY(itemnumber)");
163         dosql($dbh,"ALTER TABLE itemtypes DROP PRIMARY KEY, ADD PRIMARY KEY(itemtype)");
164         print "done\n various...";
165         dosql($dbh,"ALTER TABLE categories DROP PRIMARY KEY, ADD PRIMARY KEY(categorycode)");
166         dosql($dbh,"ALTER TABLE categoryitem DROP PRIMARY KEY, ADD PRIMARY KEY(categorycode,itemtype)");
167         dosql($dbh,"ALTER TABLE currency CHANGE currency currency VARCHAR (10) not null");
168         dosql($dbh,"ALTER TABLE currency CHANGE rate rate FLOAT (7,5) not null");
169         dosql($dbh,"ALTER TABLE currency DROP PRIMARY KEY, ADD PRIMARY KEY(currency)");
170         dosql($dbh,"ALTER TABLE printers CHANGE printername printername CHAR (40) not null");
171         dosql($dbh,"ALTER TABLE printers DROP PRIMARY KEY, ADD PRIMARY KEY(printername)");
172 #<FORPRODADD>
173 # ALTER TABLE reserves DROP PRIMARY KEY, ADD PRIMARY KEY(borrowernumber,biblionumber,reservedate)
174 #</FORPRODADD>
175         dosql($dbh,"ALTER TABLE stopwords CHANGE word word VARCHAR (255) not null");
176         dosql($dbh,"ALTER TABLE stopwords DROP PRIMARY KEY, ADD PRIMARY KEY(word)");
177         dosql($dbh,"ALTER TABLE systempreferences DROP PRIMARY KEY, ADD PRIMARY KEY(variable)");
178         dosql($dbh,"ALTER TABLE users CHANGE usercode usercode VARCHAR (10) not null");
179         dosql($dbh,"ALTER TABLE users DROP PRIMARY KEY, ADD PRIMARY KEY(usercode)");
180
181 print "dropping tables...\n";
182 my $total=0;
183 $total += droptable($dbh,'branchcategories');
184 $total += droptable($dbh,'classification');
185 $total += droptable($dbh,'multipart');
186 $total += droptable($dbh,'multivolume');
187 $total += droptable($dbh,'newitems');
188 $total += droptable($dbh,'procedures');
189 $total += droptable($dbh,'publisher');
190 $total += droptable($dbh,'searchstats');
191 $total += droptable($dbh,'serialissues');
192 print "         $total tables dropped\n";