Bug fix. Changed acquisitions link to /acquisitions
[koha.git] / 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 use strict;
15
16 # CPAN modules
17 use DBI;
18
19 # Koha modules
20 use C4::Database;
21
22 my $debug=0;
23
24 my (
25         $sth,
26         $query,
27         %existingtables,        # tables already in database
28         %types,
29         $table,
30         $column,
31         $type, $null, $key, $default, $extra,
32 );
33
34 #-------------------
35 # Defines
36
37 # Tables to add if they don't exist
38 my %requiretables=(
39     shelfcontents=>"( shelfnumber int not null, 
40         itemnumber int not null, 
41         flags int)",
42     bookshelf=>"( shelfnumber int auto_increment primary key, 
43         shelfname char(255))",
44     z3950queue=>"( id int auto_increment primary key, 
45         term text, 
46         type char(10), 
47         startdate int, 
48         enddate int, 
49         done smallint, 
50         servers text, 
51         identifier char(30))",
52     z3950results=>"( id int auto_increment primary key, 
53         queryid int, 
54         server char(255), 
55         startdate int, 
56         enddate int, 
57         results longblob, 
58         numrecords int, 
59         numdownloaded int, 
60         highestseen int, 
61         active smallint)",
62     branchrelations=>"( branchcode varchar(4), 
63         categorycode varchar(4))",
64     marcrecorddone=>"( isbn char(40),
65                       issn char(40),
66                       lccn char(40),
67                       controlnumber char(40))",
68     uploadedmarc=>"( id int(11) NOT NULL auto_increment PRIMARY KEY,
69                     marc longblob,
70                     hidden smallint(6) default NULL,
71                     name varchar(255) default NULL)",
72     ethnicity=>"( code varchar(10) NOT NULL default '',
73                 name varchar(255) default NULL,
74                 PRIMARY KEY  (code)   )",
75 );
76
77 my %requirefields=(
78     biblio=>{ 'abstract' => 'text' },
79     deletedbiblio=>{ 'abstract' => 'text' },
80     biblioitems=>{ 'lccn' => 'char(25)',
81                 'url' => 'varchar(255)',
82                 'marc' => 'text' },
83     deletedbiblioitems=>{ 'lccn' => 'char(25)',
84                 'url' => 'varchar(255)',
85                 'marc' => 'text' },
86     branchtransfers=>{ 'datearrived' => 'datetime' },
87 );
88
89 #-------------------
90 # Initialize
91 my $dbh=C4Connect;
92
93 # Start checking
94
95 # Get version of MySQL database engine.
96 my $mysqlversion=`mysqld --version`;
97 $mysqlversion=~/Ver (\S*) /;
98 $mysqlversion=$1;
99 if ($mysqlversion ge '3.23') {
100     print "Could convert to MyISAM database tables...\n";
101 }
102
103 #---------------------------------
104 # Tables
105
106 # Collect all tables into a list
107 $sth=$dbh->prepare("show tables");
108 $sth->execute;
109 while (my ($table) = $sth->fetchrow) {
110     $existingtables{$table}=1;
111 }
112
113 # Now add any missing tables
114 foreach $table ( keys %requiretables ) {
115     print "Checking $table table...\n" if $debug;;
116     unless ($existingtables{$table} ) {
117         print "Adding $table table...\n";
118         my $sth=$dbh->prepare(
119                 "create table $table $requiretables{$table}" );
120         $sth->execute;
121         if ($sth->err) {
122                 print "Error : $sth->errstr \n";
123                 $sth->finish;
124         } # if error
125     } # unless exists
126 } # foreach
127
128 unless ($existingtables{'z3950servers'}) {
129     print "Adding z3950servers table...\n";
130     my $sti=$dbh->prepare("create table z3950servers (
131         host char(255), 
132         port int, 
133         db char(255), 
134         userid char(255), 
135         password char(255), 
136         name text, 
137         id int, 
138         checked smallint, 
139         rank int)");
140     $sti->execute;
141     $sti=$dbh->prepare("insert into z3950servers 
142         values ('z3950.loc.gov', 
143         7090, 
144         'voyager', 
145         '', '', 
146         'Library of Congress', 
147         1, 1, 1)");
148     $sti->execute;
149 }
150
151 #---------------------------------
152 # Columns
153
154 foreach $table ( keys %requirefields ) {
155     print "Check table $table\n";
156     $sth=$dbh->prepare("show columns from $table");
157     $sth->execute();
158     undef %types;
159     while ( ($column, $type, $null, $key, $default, $extra) 
160                 = $sth->fetchrow) {
161         $types{$column}=$type;
162     } # while 
163     foreach $column ( keys %{ $requirefields{$table} } )  {
164         print "  Check column $column\n";
165         if ( ! $types{$column} ) {
166             # column doesn't exist
167             print "Adding $column field to $table table...\n";
168             $query="alter table $table
169                 add column $column " . $requirefields{$table}->{$column} ;
170             print "Execute: $query\n" if $debug;
171             my $sti=$dbh->prepare($query);
172             $sti->execute;
173             if ($sti->err) {
174                     print "**Error : $sti->errstr \n";
175                     $sti->finish;
176             } # if error
177         } # if column
178     } # foreach column
179 } # foreach table
180
181 # Get list of columns from items table
182 my %itemtypes;
183
184 my $sth=$dbh->prepare("show columns from items");
185 $sth->execute;
186 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
187     $itemtypes{$column}=$type;
188 }
189
190 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
191     $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
192     my $oldlength=$1;
193     if ($oldlength<20) {
194         print "Setting maximum barcode length to 20 (was $oldlength).\n";
195         my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
196         $sti->execute;
197     }
198 }
199
200 # extending the timestamp in branchtransfers...
201 my %branchtransfers;
202
203 my $sth=$dbh->prepare("show columns from branchtransfers");
204 $sth->execute;
205 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
206     $branchtransfers{$column}=$type;
207 }
208
209 unless ($branchtransfers{'datesent'} eq 'datetime') {
210     print "Setting type of datesent in branchtransfers to datetime.\n";
211     my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
212     $sti->execute;
213 }
214
215 unless ($branchtransfers{'datearrived'} eq 'datetime') {
216     print "Setting type of datearrived in branchtransfers to datetime.\n";
217     my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
218     $sti->execute;
219 }
220
221 # changing the branchcategories table around...
222 my %branchcategories;
223
224 my $sth=$dbh->prepare("show columns from branchcategories");
225 $sth->execute;
226 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
227     $branchcategories{$column}=$type;
228 }
229
230 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
231     print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
232     my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
233     $sth->execute;
234     $sth=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
235     $sth->execute;
236 }
237
238 unless ($branchcategories{'branchcode'} eq 'varchar(4)') {
239     print "Changing branchcode in branchcategories to categoryname text.\n";
240     my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
241     $sth->execute;
242 }
243
244 unless ($branchcategories{'codedescription'} eq 'text') {
245     print "Replacing branchholding in branchcategories with codedescription text.\n";
246     my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
247     $sth->execute;
248 }
249
250
251 $sth->finish;
252 $dbh->disconnect;
253
254 exit;
255
256 # $Log$
257 # Revision 1.4.2.6  2002/06/20 15:07:08  amillar
258 # Add table ethnicity
259 #