merging rel-1-2 and main
[koha.git] / updater / updatedatabase
1 #!/usr/bin/perl
2
3 # Database Updater
4 # This script checks for required updates to the database.  
5
6 # Part of the Koha Library Software www.koha.org
7 # Licensed under the GPL.
8
9 # Bugs/ToDo: 
10 # - Would also be a good idea to offer to do a backup at this time...
11
12 use strict;
13
14 # CPAN modules
15 use DBI;
16
17 # Koha modules
18 use C4::Database;
19
20 my %existingtables;     # tables already in database
21 my %types;
22 my $table;
23
24 #-------------------
25 # Defines
26
27 # Tables to add if they don't exist
28 my %requiretables=(
29     shelfcontents=>"( shelfnumber int not null, 
30         itemnumber int not null, 
31         flags int)",
32     bookshelf=>"( shelfnumber int auto_increment primary key, 
33         shelfname char(255))",
34     z3950queue=>"( id int auto_increment primary key, 
35         term text, 
36         type char(10), 
37         startdate int, 
38         enddate int, 
39         done smallint, 
40         results longblob, 
41         numrecords int, 
42         servers text, 
43         identifier char(30))",
44     z3950results=>"( id int auto_increment primary key, 
45         queryid int, 
46         server char(255), 
47         startdate int, 
48         enddate int, 
49         results longblob, 
50         numrecords int, 
51         numdownloaded int, 
52         highestseen int, 
53         active smallint)",
54     branchrelations=>"( branchcode varchar(4), 
55         categorycode varchar(4))",
56     websites=>"( websitenumber int(11) NOT NULL auto_increment,
57         biblionumber int(11) NOT NULL default '0',
58         title text,
59         description text,
60         url varchar(255),
61         PRIMARY KEY (websitenumber) )",
62 );
63
64 #-------------------
65 # Initialize
66 my $dbh=C4Connect;
67
68 # Start checking
69
70 # Get version of MySQL database engine.
71 my $mysqlversion=`mysqld --version`;
72 $mysqlversion=~/Ver (\S*) /;
73 $mysqlversion=$1;
74 if ($mysqlversion ge '3.23') {
75     print "Could convert to MyISAM database tables...\n";
76 }
77
78 #---------------------------------
79 # Tables
80
81 # Collect all tables into a list
82 my $sth=$dbh->prepare("show tables");
83 $sth->execute;
84 while (my ($table) = $sth->fetchrow) {
85     $existingtables{$table}=1;
86 }
87
88 # Now add any missing tables
89 foreach $table ( keys %requiretables ) {
90     unless ($existingtables{$table} ) {
91         print "Adding $table table...\n";
92         my $sth=$dbh->prepare(
93                 "create table $table $requiretables{$table}" );
94         $sth->execute;
95         if ($sth->err) {
96                 print "Error : $sth->errstr \n";
97                 $sth->finish;
98         } # if error
99     } # unless exists
100 } # foreach
101 exit;
102
103 unless ($existingtables{'z3950servers'}) {
104     print "Adding z3950servers table...\n";
105     my $sti=$dbh->prepare("create table z3950servers (
106         host char(255), 
107         port int, 
108         db char(255), 
109         userid char(255), 
110         password char(255), 
111         name text, 
112         id int, 
113         checked smallint, 
114         rank int)");
115     $sti->execute;
116     $sti=$dbh->prepare("insert into z3950servers 
117         values ('z3950.loc.gov', 
118         7090, 
119         'voyager', 
120         '', '', 
121         'Library of Congress', 
122         1, 1, 1)");
123     $sti->execute;
124 }
125
126 #---------------------------------
127 # Columns
128
129
130 # Get list of columns from biblioitems table
131
132 my $sth=$dbh->prepare("show columns from biblioitems");
133 $sth->execute;
134 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
135     $types{$column}=$type;
136 }
137 unless ($types{'lccn'}) {
138     # Add LCCN field to biblioitems db
139     print "Adding lccn field to biblioitems table...\n";
140     my $sti=$dbh->prepare("alter table biblioitems 
141         add column lccn char(25)");
142     $sti->execute;
143 }
144 unless ($types{'marc'}) {
145     # Add MARC field to biblioitems db (not used anymore)
146     print "Adding marc field to biblioitems table...\n";
147     my $sti=$dbh->prepare("alter table biblioitems 
148         add column marc text");
149     $sti->execute;
150 }
151
152 # Get list of columns from biblioitems table
153 my %itemtypes;
154
155 my $sth=$dbh->prepare("show columns from items");
156 $sth->execute;
157 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
158     $itemtypes{$column}=$type;
159 }
160
161 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
162     $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
163     my $oldlength=$1;
164     if ($oldlength<20) {
165         print "Setting maximum barcode length to 20 (was $oldlength).\n";
166         my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
167         $sti->execute;
168     }
169 }
170
171 # extending the timestamp in branchtransfers...
172 my %branchtransfers;
173
174 my $sth=$dbh->prepare("show columns from branchtransfers");
175 $sth->execute;
176 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
177     $branchtransfers{$column}=$type;
178 }
179
180 unless ($branchtransfers{'datesent'} eq 'datetime') {
181     print "Setting type of datesent in branchtransfers to datetime.\n";
182     my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
183     $sti->execute;
184 }
185
186 unless ($branchtransfers{'datearrived'} eq 'datetime') {
187     print "Setting type of datearrived in branchtransfers to datetime.\n";
188     my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
189     $sti->execute;
190 }
191
192 # changing the branchcategories table around...
193 my %branchcategories;
194
195 my $sth=$dbh->prepare("show columns from branchcategories");
196 $sth->execute;
197 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
198     $branchcategories{$column}=$type;
199 }
200
201 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
202     print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
203     my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
204     $sth->execute;
205     $sth=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
206     $sth->execute;
207 }
208
209 unless ($branchcategories{'branchcode'} eq 'varchar(4)') {
210     print "Changing branchcode in branchcategories to categoryname text.\n";
211     my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
212     $sth->execute;
213 }
214
215 unless ($branchcategories{'codedescription'} eq 'text') {
216     print "Replacing branchholding in branchcategories with codedescription text.\n";
217     my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
218     $sth->execute;
219 }
220
221
222 $sth->finish;
223 $dbh->disconnect;