Fixed a few warnings.
[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 use C4::Database;
17 use C4::Catalogue;
18 use DBI;
19 use C4::Acquisitions;
20 use C4::Output;
21
22 sub droptable {
23         my ($dbh,$tablename)=@_;
24         if ($tables{$tablename}) {
25                 print "     - $tablename...\n";
26                 my $sti=$dbh->prepare("DROP TABLE $tablename");
27                 $sti->execute;
28                 return 1;
29         }
30         return 0;
31 }
32 sub dosql {
33         my ($dbh,$sql_cmd)=@_;
34         my $sti=$dbh->prepare($sql_cmd);
35         $sti->execute;
36         if ($sti->err) {
37                 print "error : ".$sti->errstr." \n tried to execute : $sql_cmd\n";
38                 $sti->finish;
39         }
40 }
41
42
43 my $dbh=C4Connect;
44
45 my $sth=$dbh->prepare("show tables");
46 $sth->execute;
47 while (my ($table) = $sth->fetchrow) {
48     $tables{$table}=1;
49     print "table $table\n";
50 }
51
52 #print "creating thesaurus...\n";
53 #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))");
54 #       my $sti=$dbh->prepare("select subject from bibliosubject");
55 #       $sti->execute;
56 #       $i=0;
57 #       while ($line =$sti->fetchrow_hashref) {
58 #               $i++;
59 #               print "$i $line->{'subject'}\n";
60 #               $sti2=$dbh->prepare("select count(*) as t from bibliothesaurus where freelib=".$dbh->quote($line->{'subject'}));
61 #               $sti2->execute;
62 #               if ($sti2->err) {
63 #                       print "error : ".$sti2->errstr." \n tried to execute : $sql_cmd\n";
64 #                       die;
65 #               }
66 #               $line2=$sti2->fetchrow_hashref;
67 #               if ($line2->{'t'} ==0) {
68 #                       dosql($dbh,"insert into bibliothesaurus (freelib,stdlib) values (".$dbh->quote($line->{'subject'}).",".$dbh->quote($line->{'subject'}).")");
69 #               } else {
70 #                       print "pas ecriture pour : $line->{'subject'}\n";
71 #               }
72 #
73 #       }
74
75 #aqbookfund : the sample db is full of trash data. Delete and recreate
76 # <FORPRODDEL>
77         print "aqbookfund...";
78         dosql($dbh,"delete from aqbookfund");
79         dosql($dbh,"INSERT INTO aqbookfund VALUES( '5', 'Young Adult Fiction', '3')");
80         dosql($dbh,"INSERT INTO aqbookfund VALUES( '4', 'Fiction', '2')");
81         dosql($dbh,"INSERT INTO aqbookfund VALUES( '2', 'Talking books', '4')");
82         dosql($dbh,"INSERT INTO aqbookfund VALUES( '13', 'Newspapers & journals', '4')");
83         dosql($dbh,"INSERT INTO aqbookfund VALUES( '10', 'Te Ao Maori', '1')");
84         dosql($dbh,"INSERT INTO aqbookfund VALUES( '15', 'CDs, CD Roms, Maps, etc', '4')");
85         dosql($dbh,"INSERT INTO aqbookfund VALUES( '9', 'Junior Fiction', '3')");
86         dosql($dbh,"INSERT INTO aqbookfund VALUES( '7', 'Junior Non-Fiction', '3')");
87         dosql($dbh,"INSERT INTO aqbookfund VALUES( '72', 'Creative NZ', '4')");
88         dosql($dbh,"INSERT INTO aqbookfund VALUES( '11', 'Reference', '1')");
89         dosql($dbh,"INSERT INTO aqbookfund VALUES( '12', 'Videos', '4')");
90         dosql($dbh,"INSERT INTO aqbookfund VALUES( '8', 'Junior Paperback', '3')");
91         dosql($dbh,"INSERT INTO aqbookfund VALUES( '73', 'Large Print Link-up', '2')");
92         dosql($dbh,"INSERT INTO aqbookfund VALUES( '3', 'Large Print', '2')");
93         dosql($dbh,"INSERT INTO aqbookfund VALUES( '71', 'Creative NZ NonFiction', '1')");
94         dosql($dbh,"INSERT INTO aqbookfund VALUES( '6', 'Picture Books', '3')");
95         dosql($dbh,"INSERT INTO aqbookfund VALUES( '14', 'Nga pukapuka Maori', '3')");
96         dosql($dbh,"INSERT INTO aqbookfund VALUES( '80', 'Donations, junior books', '5')");
97         dosql($dbh,"INSERT INTO aqbookfund VALUES( '81', 'Donations, adult books', '5')");
98         dosql($dbh,"INSERT INTO aqbookfund VALUES( '82', 'Donations, magazines', '5')");
99         dosql($dbh,"INSERT INTO aqbookfund VALUES( '83', 'Donations, non books', '5')");
100         dosql($dbh,"INSERT INTO aqbookfund VALUES( '91', 'Vertical File', '5')");
101         dosql($dbh,"INSERT INTO aqbookfund VALUES( '100', 'Loans', '6')");
102         dosql($dbh,"INSERT INTO aqbookfund VALUES( '1', 'Test', '1')");
103 #</FORPRODDEL>
104         dosql($dbh,"ALTER TABLE aqbookfund DROP PRIMARY KEY, ADD PRIMARY KEY(bookfundid);");
105         print "done\n accountlines...";
106         dosql($dbh,"ALTER TABLE accountlines CHANGE itemnumber itemnumber INT (11) not null;");
107         dosql($dbh,"ALTER TABLE accountlines DROP PRIMARY KEY, ADD PRIMARY KEY(borrowernumber,accountno,itemnumber);");
108 # accountoffset not done (not possible ?)
109 #additionalauthor : not possible (no field useable, 1 index OK)
110         print "done\n aqbooksellers...";
111         dosql($dbh,"ALTER TABLE aqbooksellers CHANGE id id INT (11) not null;");
112         dosql($dbh,"ALTER TABLE aqbooksellers DROP PRIMARY KEY, ADD PRIMARY KEY(id);");
113         print "done\n aqbudget...";
114         dosql($dbh,"ALTER TABLE aqbudget DROP PRIMARY KEY, ADD PRIMARY KEY(bookfundid, bookfundid);");
115         print "done\n aqorderbreakdown...";
116         dosql($dbh,"ALTER TABLE aqorderbreakdown CHANGE ordernumber ordernumber INT (11) not null;");
117         dosql($dbh,"ALTER TABLE aqorderbreakdown CHANGE linenumber linenumber INT (11) not null;");
118         dosql($dbh,"ALTER TABLE aqorderbreakdown CHANGE branchcode branchcode CHAR (4) not null;");
119 #<FORPRODADD>
120 #       ALTER TABLE aqorderbreakdown DROP PRIMARY KEY, ADD PRIMARY KEY(ordernumber,linenumber,branchcode);");
121 #</FORPRODADD>
122
123         print "done\n biblio/borrowers...";
124         dosql($dbh,"ALTER TABLE aqorderdelivery DROP PRIMARY KEY, ADD PRIMARY KEY(ordernumber);");
125         dosql($dbh,"ALTER TABLE aqorders DROP PRIMARY KEY, ADD PRIMARY KEY(ordernumber);");
126         dosql($dbh,"ALTER TABLE biblio DROP PRIMARY KEY, ADD PRIMARY KEY(biblionumber);");
127         dosql($dbh,"ALTER TABLE biblioitems DROP PRIMARY KEY, ADD PRIMARY KEY(biblionumber, biblioitemnumber)");
128 #       dosql($dbh,"CREATE INDEX SUBTITLE ON bibliosubtitle (subtitle(80))");
129         dosql($dbh,"ALTER TABLE borexp CHANGE borrowernumber borrowernumber INT (11) not null");
130         dosql($dbh,"ALTER TABLE borexp CHANGE newexp newexp DATE not null");
131         dosql($dbh,"ALTER TABLE branches DROP PRIMARY KEY, ADD PRIMARY KEY(branchcode)");
132         dosql($dbh,"ALTER TABLE deletedbiblio DROP PRIMARY KEY, ADD PRIMARY KEY(biblionumber, biblionumber)");
133         dosql($dbh,"ALTER TABLE deletedbiblioitems DROP PRIMARY KEY, ADD PRIMARY KEY(biblioitemnumber)");
134         dosql($dbh,"ALTER TABLE deletedborrowers DROP PRIMARY KEY, ADD PRIMARY KEY(borrowernumber)");
135         dosql($dbh,"ALTER TABLE deleteditems DROP PRIMARY KEY, ADD PRIMARY KEY(itemnumber)");
136         dosql($dbh,"ALTER TABLE issues CHANGE date_due date_due DATE not null");
137 #<FORPRODADD>
138 # ALTER TABLE issues DROP PRIMARY KEY, ADD PRIMARY KEY(borrowernumber,itemnumber,date_due)");
139 #</FORPRODADD>
140         print "done\n items...";
141         dosql($dbh,"ALTER TABLE items DROP PRIMARY KEY, ADD PRIMARY KEY(itemnumber)");
142         dosql($dbh,"ALTER TABLE itemsprices CHANGE itemnumber itemnumber INT (11) not null");
143         dosql($dbh,"ALTER TABLE itemsprices DROP PRIMARY KEY, ADD PRIMARY KEY(itemnumber)");
144         dosql($dbh,"ALTER TABLE itemtypes DROP PRIMARY KEY, ADD PRIMARY KEY(itemtype)");
145         print "done\n various...";
146         dosql($dbh,"ALTER TABLE categories DROP PRIMARY KEY, ADD PRIMARY KEY(categorycode)");
147         dosql($dbh,"ALTER TABLE categoryitem DROP PRIMARY KEY, ADD PRIMARY KEY(categorycode,itemtype)");
148         dosql($dbh,"ALTER TABLE currency CHANGE currency currency VARCHAR (10) not null");
149         dosql($dbh,"ALTER TABLE currency CHANGE rate rate FLOAT (7,5) not null");
150         dosql($dbh,"ALTER TABLE currency DROP PRIMARY KEY, ADD PRIMARY KEY(currency)");
151         dosql($dbh,"ALTER TABLE printers CHANGE printername printername CHAR (40) not null");
152         dosql($dbh,"ALTER TABLE printers DROP PRIMARY KEY, ADD PRIMARY KEY(printername)");
153 #<FORPRODADD>
154 # ALTER TABLE reserves DROP PRIMARY KEY, ADD PRIMARY KEY(borrowernumber,biblionumber,reservedate)
155 #</FORPRODADD>
156         dosql($dbh,"ALTER TABLE stopwords CHANGE word word VARCHAR (255) not null");
157         dosql($dbh,"ALTER TABLE stopwords DROP PRIMARY KEY, ADD PRIMARY KEY(word)");
158         dosql($dbh,"ALTER TABLE systempreferences DROP PRIMARY KEY, ADD PRIMARY KEY(variable)");
159         dosql($dbh,"ALTER TABLE users CHANGE usercode usercode VARCHAR (10) not null");
160         dosql($dbh,"ALTER TABLE users DROP PRIMARY KEY, ADD PRIMARY KEY(usercode)");
161
162 print "dropping tables...\n";
163 my $total=0;
164 $total += droptable($dbh,'branchcategories');
165 $total += droptable($dbh,'classification');
166 $total += droptable($dbh,'multipart');
167 $total += droptable($dbh,'multivolume');
168 $total += droptable($dbh,'newitems');
169 $total += droptable($dbh,'procedures');
170 $total += droptable($dbh,'publisher');
171 $total += droptable($dbh,'searchstats');
172 $total += droptable($dbh,'serialissues');
173 print "         $total tables dropped\n";