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