Add comments, make branchrelations consistent with other table additions
[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 #use C4::Catalogue;
20 use C4::Acquisitions;
21 use C4::Output;
22
23 my %tables;
24 my %types;
25
26 #-------------------
27 # Initialize
28 my $dbh=C4Connect;
29
30 # Start checking
31
32 # Get version of MySQL database engine.
33 my $mysqlversion=`mysqld --version`;
34 $mysqlversion=~/Ver (\S*) /;
35 $mysqlversion=$1;
36 if ($mysqlversion ge '3.23') {
37     print "Could convert to MyISAM database tables...\n";
38 }
39
40 #---------------------------------
41 # Tables
42
43 # Collect all tables into a list
44 my $sth=$dbh->prepare("show tables");
45 $sth->execute;
46 while (my ($table) = $sth->fetchrow) {
47     $tables{$table}=1;
48 }
49
50 # Now add any missing tables
51
52 # Add tables for virtual bookshelf management
53 unless ($tables{'shelfcontents'}) {
54     print "Adding shelfcontents table...\n";
55     my $sti=$dbh->prepare("create table shelfcontents (
56         shelfnumber int not null, 
57         itemnumber int not null, 
58         flags int)");
59     $sti->execute;
60 }
61 unless ($tables{'bookshelf'}) {
62     print "Adding bookshelf table...\n";
63     my $sti=$dbh->prepare("create table bookshelf (
64         shelfnumber int auto_increment primary key, 
65         shelfname char(255))");
66     $sti->execute;
67 }
68
69 # Add tables required by Z-3950 scripts
70
71 unless ($tables{'z3950queue'}) {
72     print "Adding z3950queue table...\n";
73     my $sti=$dbh->prepare("create table z3950queue (
74         id int auto_increment primary key, 
75         term text, 
76         type char(10), 
77         startdate int, 
78         enddate int, 
79         done smallint, 
80         results longblob, 
81         numrecords int, 
82         servers text, 
83         identifier char(30))");
84     $sti->execute;
85 }
86
87 unless ($tables{'z3950results'}) {
88     print "Adding z3950results table...\n";
89     my $sti=$dbh->prepare("create table z3950results (
90         id int auto_increment primary key, 
91         queryid int, 
92         server char(255), 
93         startdate int, 
94         enddate int, 
95         results longblob, 
96         numrecords int, 
97         numdownloaded int, 
98         highestseen int, 
99         active smallint)");
100     $sti->execute;
101 }
102 unless ($tables{'z3950servers'}) {
103     print "Adding z3950servers table...\n";
104     my $sti=$dbh->prepare("create table z3950servers (
105         host char(255), 
106         port int, 
107         db char(255), 
108         userid char(255), 
109         password char(255), 
110         name text, 
111         id int, 
112         checked smallint, 
113         rank int)");
114     $sti->execute;
115     $sti=$dbh->prepare("insert into z3950servers 
116         values ('z3950.loc.gov', 
117         7090, 
118         'voyager', 
119         '', '', 
120         'Library of Congress', 
121         1, 1, 1)");
122     $sti->execute;
123 }
124
125 # Create new branchrelations table if it doesnt already exist....
126 unless ($tables{'branchrelations'} ) {
127     print "creating branchrelations table\n";
128     my $sth=$dbh->prepare("create table branchrelations (
129         branchcode varchar(4), 
130         categorycode varchar(4))");
131     $sth->execute;
132 }
133
134 #---------------------------------
135 # Columns
136
137
138 # Get list of columns from biblioitems table
139
140 my $sth=$dbh->prepare("show columns from biblioitems");
141 $sth->execute;
142 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
143     $types{$column}=$type;
144 }
145 unless ($types{'lccn'}) {
146     # Add LCCN field to biblioitems db
147     print "Adding lccn field to biblioitems table...\n";
148     my $sti=$dbh->prepare("alter table biblioitems 
149         add column lccn char(25)");
150     $sti->execute;
151 }
152 unless ($types{'marc'}) {
153     # Add MARC field to biblioitems db (not used anymore)
154     print "Adding marc field to biblioitems table...\n";
155     my $sti=$dbh->prepare("alter table biblioitems 
156         add column marc text");
157     $sti->execute;
158 }
159
160 # Get list of columns from biblioitems table
161 my %itemtypes;
162
163 my $sth=$dbh->prepare("show columns from items");
164 $sth->execute;
165 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
166     $itemtypes{$column}=$type;
167 }
168
169 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
170     $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
171     my $oldlength=$1;
172     if ($oldlength<20) {
173         print "Setting maximum barcode length to 20 (was $oldlength).\n";
174         my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
175         $sti->execute;
176     }
177 }
178
179 # extending the timestamp in branchtransfers...
180 my %branchtransfers;
181
182 my $sth=$dbh->prepare("show columns from branchtransfers");
183 $sth->execute;
184 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
185     $branchtransfers{$column}=$type;
186 }
187
188 unless ($branchtransfers{'datesent'} eq 'datetime') {
189     print "Setting type of datesent in branchtransfers to datetime.\n";
190     my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
191     $sti->execute;
192 }
193
194 unless ($branchtransfers{'datearrived'} eq 'datetime') {
195     print "Setting type of datearrived in branchtransfers to datetime.\n";
196     my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
197     $sti->execute;
198 }
199
200 # changing the branchcategories table around...
201 my %branchcategories;
202
203 my $sth=$dbh->prepare("show columns from branchcategories");
204 $sth->execute;
205 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
206     $branchcategories{$column}=$type;
207 }
208
209 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
210     print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
211     my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
212     $sth->execute;
213     $sth=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
214     $sth->execute;
215 }
216
217 unless ($branchcategories{'branchcode'} eq 'varchar(4)') {
218     print "Changing branchcode in branchcategories to categoryname text.\n";
219     my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
220     $sth->execute;
221 }
222
223 unless ($branchcategories{'codedescription'} eq 'text') {
224     print "Replacing branchholding in branchcategories with codedescription text.\n";
225     my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
226     $sth->execute;
227 }
228
229
230 $sth->finish;
231 $dbh->disconnect;