Added some more tables to the database updater. Modified barcode to be
[koha.git] / updater / updatedatabase
1 #!/usr/bin/perl
2
3 # This script will check for required updates to the database.  Would also be a
4 # good idea to offer to do a backup at this time...
5
6
7 use C4::Database;
8 use C4::Catalogue;
9 use DBI;
10 use C4::Acquisitions;
11 use C4::Output;
12 my $dbh=C4Connect;
13
14 my %tables;
15 my %types;
16
17 my $sth=$dbh->prepare("show tables");
18 $sth->execute;
19 while (my ($table) = $sth->fetchrow) {
20     $tables{$table}=1;
21 }
22
23
24 # Add tables for virtual bookshelf management
25
26 unless ($tables{'shelfcontents'}) {
27     print "Adding shelfcontents table...\n";
28     my $sti=$dbh->prepare("create table shelfcontents (shelfnumber int not null, itemnumber int not null, flags int)");
29     $sti->execute;
30 }
31 unless ($tables{'bookshelf'}) {
32     print "Adding bookshelf table...\n";
33     my $sti=$dbh->prepare("create table bookshelf (shelfnumber int auto_increment primary key, shelfname char(255))");
34     $sti->execute;
35 }
36
37 # Add tables required by Z-3950 scripts
38
39 unless ($tables{'z3950queue'}) {
40     print "Adding z3950queue table...\n";
41     my $sti=$dbh->prepare("create table z3950queue (id int auto_increment primary key, term text, type char(10), startdate int, enddate int, done smallint, results longblob, numercords int, servers text, identifier char(30))");
42     $sti->execute;
43 }
44
45 unless ($tables{'z3950results'}) {
46     print "Adding z3950results table...\n";
47     my $sti=$dbh->prepare("create table z3950results (id int auto_increment primary key, queryid int, server char(255), startdate int, enddate int, results longblob, numrecords int, numdownloaded int, highestseen int, active smallint)");
48     $sti->execute;
49 }
50 unless ($tables{'z3950servers'}) {
51     print "Adding z3950servers table...\n";
52     my $sti=$dbh->prepare("create table z3950servers (host char(255), port int, db char(255), userid char(255), password char(255), name text, id int, checked smallint, rank int)");
53     $sti->execute;
54     $sti=$dbh->prepare("insert into z3950servers values ('z3950.loc.gov', 7090, 'voyager', '', '', 'Library of Congress', 1, 1, 1)");
55     $sti->execute;
56 }
57
58
59
60 # Get list of columns from biblioitems table
61
62 my $sth=$dbh->prepare("show columns from biblioitems");
63 $sth->execute;
64 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
65     $types{$column}=$type;
66 }
67 unless ($types{'lccn'}) {
68     # Add LCCN field to biblioitems db
69     print "Adding lccn field to biblioitems table...\n";
70     my $sti=$dbh->prepare("alter table biblioitems add column lccn char(25)");
71     $sti->execute;
72 }
73 unless ($types{'marc'}) {
74     # Add MARC field to biblioitems db (not used anymore)
75     print "Adding marc field to biblioitems table...\n";
76     my $sti=$dbh->prepare("alter table biblioitems add column marc text");
77     $sti->execute;
78 }
79
80 # Get list of columns from biblioitems table
81 my %itemtypes;
82
83 my $sth=$dbh->prepare("show columns from items");
84 $sth->execute;
85 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
86     $itemtypes{$column}=$type;
87 }
88
89 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
90     $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
91     my $oldlength=$1;
92     if ($oldlength<20) {
93         print "Setting maximum barcode length to 20 (was $oldlength).\n";
94         my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
95         $sti->execute;
96     }
97 }
98
99
100 $sth->finish;
101 $dbh->disconnect;