small script that show in HTML a MARC biblio stored in koha. Very useful for test...
[koha_fer] / 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
16 use strict;
17
18 # CPAN modules
19 use DBI;
20
21 # Koha modules
22 use C4::Database;
23
24 my $debug=0;
25
26 my (
27         $sth, $sti,
28         $query,
29         %existingtables,        # tables already in database
30         %types,
31         $table,
32         $column,
33         $type, $null, $key, $default, $extra,
34         $prefitem,              # preference item in systempreferences table
35 );
36
37 #-------------------
38 # Defines
39
40 # Tables to add if they don't exist
41 my %requiretables=(
42     shelfcontents=>"( shelfnumber int not null, 
43         itemnumber int not null, 
44         flags int)",
45     bookshelf=>"( shelfnumber int auto_increment primary key, 
46         shelfname char(255))",
47     z3950queue=>"( id int auto_increment primary key, 
48         term text, 
49         type char(10), 
50         startdate int, 
51         enddate int, 
52         done smallint, 
53         results longblob, 
54         numrecords int, 
55         servers text, 
56         identifier char(30))",
57     z3950results=>"( id int auto_increment primary key, 
58         queryid int, 
59         server char(255), 
60         startdate int, 
61         enddate int, 
62         results longblob, 
63         numrecords int, 
64         numdownloaded int, 
65         highestseen int, 
66         active smallint)",
67     branchrelations=>"( branchcode varchar(4), 
68         categorycode varchar(4))",
69     websites=>"( websitenumber int(11) NOT NULL auto_increment,
70         biblionumber int(11) NOT NULL default '0',
71         title text,
72         description text,
73         url varchar(255),
74         PRIMARY KEY (websitenumber) )",
75     marcrecorddone=>"( isbn char(40),
76                       issn char(40),
77                       lccn char(40),
78                       controlnumber char(40))",
79     uploadedmarc=>"( id int(11) NOT NULL auto_increment PRIMARY KEY,
80                     marc longblob,
81                     hidden smallint(6) default NULL,
82                     name varchar(255) default NULL)",
83     ethnicity=>"( code varchar(10) NOT NULL default '',
84                 name varchar(255) default NULL,
85                 PRIMARY KEY  (code)   )",
86     sessions=>"( sessionID varchar(255) NOT NULL default '',
87                 userid varchar(255) default NULL,
88                 ip varchar(16) default NULL,
89                 lasttime int,
90                 PRIMARY KEY (sessionID)   )",
91     sessionqueries=>"( sessionID varchar(255) NOT NULL default '',
92                        userid char(100) NOT NULL default '',
93                        ip char(18) NOT NULL default '',
94                        url text NOT NULL default ''  )",
95 );
96
97
98 my %requirefields=(
99     biblio=>{ 'abstract' => 'text' },
100     deletedbiblio=>{ 'abstract' => 'text' },
101     biblioitems=>{ 'lccn' => 'char(25)',
102                 'url' => 'varchar(255)',
103                 'marc' => 'text' },
104     deletedbiblioitems=>{ 'lccn' => 'char(25)',
105                 'url' => 'varchar(255)',
106                 'marc' => 'text' },
107     branchtransfers=>{ 'datearrived' => 'datetime' },
108     statistics=>{'borrowernumber' =>'int(11)'},
109     aqbooksellers=>{'invoicedisc' =>'float(6,4)',
110                      'nocalc' => 'int(11)'},
111     borrowers=>{'userid' => 'char(30)',
112                 'password' => 'char(30)',},
113     aqorders=>{'budgetdate' => 'date'},            
114 );
115
116 # Default system preferences
117 my %defaultprefs=(
118     'autoMemberNum'=> '1',
119     'acquisitions'=> 'simple',
120 );
121
122 #-------------------
123 # Initialize
124 my $dbh=C4Connect;
125
126 # Start checking
127
128 # Get version of MySQL database engine.
129 my $mysqlversion=`mysqld --version`;
130 $mysqlversion=~/Ver (\S*) /;
131 $mysqlversion=$1;
132 if ($mysqlversion ge '3.23') {
133     print "Could convert to MyISAM database tables...\n";
134 }
135
136 #---------------------------------
137 # Tables
138
139 # Collect all tables into a list
140 $sth=$dbh->prepare("show tables");
141 $sth->execute;
142 while (my ($table) = $sth->fetchrow) {
143     $existingtables{$table}=1;
144 }
145
146 # Now add any missing tables
147 foreach $table ( keys %requiretables ) {
148     print "Checking $table table...\n" if $debug;
149     unless ($existingtables{$table} ) {
150         print "Adding $table table...\n";
151         my $sth=$dbh->prepare(
152                 "create table $table $requiretables{$table}" );
153         $sth->execute;
154         if ($sth->err) {
155                 print "Error : $sth->errstr \n";
156                 $sth->finish;
157         } # if error
158     } # unless exists
159 } # foreach
160
161 unless ($existingtables{'z3950servers'}) {
162     print "Adding z3950servers table...\n";
163     my $sti=$dbh->prepare("create table z3950servers (
164         host char(255), 
165         port int, 
166         db char(255), 
167         userid char(255), 
168         password char(255), 
169         name text, 
170         id int, 
171         checked smallint, 
172         rank int)");
173     $sti->execute;
174     $sti=$dbh->prepare("insert into z3950servers 
175         values ('z3950.loc.gov', 
176         7090, 
177         'voyager', 
178         '', '', 
179         'Library of Congress', 
180         1, 1, 1)");
181     $sti->execute;
182 }
183
184 #---------------------------------
185 # Columns
186
187 foreach $table ( keys %requirefields ) {
188     print "Check table $table\n" if $debug;
189     $sth=$dbh->prepare("show columns from $table");
190     $sth->execute();
191     undef %types;
192     while ( ($column, $type, $null, $key, $default, $extra) 
193                 = $sth->fetchrow) {
194         $types{$column}=$type;
195     } # while 
196     foreach $column ( keys %{ $requirefields{$table} } )  {
197         print "  Check column $column\n" if $debug;
198         if ( ! $types{$column} ) {
199             # column doesn't exist
200             print "Adding $column field to $table table...\n";
201             $query="alter table $table
202                 add column $column " . $requirefields{$table}->{$column} ;
203             print "Execute: $query\n" if $debug;
204             my $sti=$dbh->prepare($query);
205             $sti->execute;
206             if ($sti->err) {
207                     print "**Error : $sti->errstr \n";
208                     $sti->finish;
209             } # if error
210         } # if column
211     } # foreach column
212 } # foreach table
213
214 # Get list of columns from items table
215 my %itemtypes;
216
217 my $sth=$dbh->prepare("show columns from items");
218 $sth->execute;
219 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
220     $itemtypes{$column}=$type;
221 }
222
223 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
224     $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
225     my $oldlength=$1;
226     if ($oldlength<20) {
227         print "Setting maximum barcode length to 20 (was $oldlength).\n";
228         my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
229         $sti->execute;
230     }
231 }
232
233 # extending the timestamp in branchtransfers...
234 my %branchtransfers;
235
236 my $sth=$dbh->prepare("show columns from branchtransfers");
237 $sth->execute;
238 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
239     $branchtransfers{$column}=$type;
240 }
241
242 unless ($branchtransfers{'datesent'} eq 'datetime') {
243     print "Setting type of datesent in branchtransfers to datetime.\n";
244     my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
245     $sti->execute;
246 }
247
248 unless ($branchtransfers{'datearrived'} eq 'datetime') {
249     print "Setting type of datearrived in branchtransfers to datetime.\n";
250     my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
251     $sti->execute;
252 }
253
254 # changing the branchcategories table around...
255 my %branchcategories;
256
257 my $sth=$dbh->prepare("show columns from branchcategories");
258 $sth->execute;
259 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
260     $branchcategories{$column}=$type;
261 }
262
263 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
264     print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
265     my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
266     $sti->execute;
267     $sti=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
268     $sti->execute;
269 }
270
271 unless ($branchcategories{'categoryname'} eq 'text') {
272     print "Changing branchcode in branchcategories to categoryname text.\n";
273     my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
274     $sth->execute;
275 }
276
277 unless ($branchcategories{'codedescription'} eq 'text') {
278     print "Replacing branchholding in branchcategories with codedescription text.\n";
279     my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
280     $sth->execute;
281 }
282
283
284 # Populate systempreferences if it is empty
285
286 foreach $prefitem ( keys %defaultprefs ) {
287     $sth=$dbh->prepare("select value 
288         from systempreferences 
289         where variable=?");
290     $sth->execute($prefitem);
291     unless ($sth->rows) {
292         print "Adding system preference item $prefitem with value " .
293                 $defaultprefs{$prefitem} ."\n";
294         $sti=$dbh->prepare("
295           insert into systempreferences (variable, value) 
296           values (?,?)");
297         $sti->execute($prefitem,$defaultprefs{$prefitem});
298     } # unless
299 } # foreach
300
301
302 $sth->finish;
303 $dbh->disconnect;
304
305 exit;
306
307 # $Log$
308 # Revision 1.15  2002/07/20 22:30:06  rangi
309 # Making sure fix makes it into the main branch as well
310 # Fix for bug 69
311 #
312 # Revision 1.14  2002/07/08 16:20:26  tonnesen
313 # Added sessionqueries table and password/userid fields to borrowers table
314 #
315 # Revision 1.13  2002/07/04 18:05:36  tonnesen
316 # bug fix
317 #
318 # Revision 1.12  2002/07/04 16:41:06  tonnesen
319 # Merged changes from rel-1-2.  Abstracted table structure changes by alan.
320 #