moredetail.pl presents circulation information taken from the
[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
18 my $mysqlversion=`mysqld --version`;
19 $mysqlversion=~/Ver (\S*) /;
20 $mysqlversion=$1;
21 if ($mysqlversion ge '3.23') {
22     print "Could convert to MyISAM database tables...\n";
23 }
24
25 my $sth=$dbh->prepare("show tables");
26 $sth->execute;
27 while (my ($table) = $sth->fetchrow) {
28     $tables{$table}=1;
29 }
30
31
32 # Add tables for virtual bookshelf management
33
34 unless ($tables{'shelfcontents'}) {
35     print "Adding shelfcontents table...\n";
36     my $sti=$dbh->prepare("create table shelfcontents (shelfnumber int not null, itemnumber int not null, flags int)");
37     $sti->execute;
38 }
39 unless ($tables{'bookshelf'}) {
40     print "Adding bookshelf table...\n";
41     my $sti=$dbh->prepare("create table bookshelf (shelfnumber int auto_increment primary key, shelfname char(255))");
42     $sti->execute;
43 }
44
45 # Add tables required by Z-3950 scripts
46
47 unless ($tables{'z3950queue'}) {
48     print "Adding z3950queue table...\n";
49     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))");
50     $sti->execute;
51 }
52
53 unless ($tables{'z3950results'}) {
54     print "Adding z3950results table...\n";
55     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)");
56     $sti->execute;
57 }
58 unless ($tables{'z3950servers'}) {
59     print "Adding z3950servers table...\n";
60     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)");
61     $sti->execute;
62     $sti=$dbh->prepare("insert into z3950servers values ('z3950.loc.gov', 7090, 'voyager', '', '', 'Library of Congress', 1, 1, 1)");
63     $sti->execute;
64 }
65
66
67
68 # Get list of columns from biblioitems table
69
70 my $sth=$dbh->prepare("show columns from biblioitems");
71 $sth->execute;
72 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
73     $types{$column}=$type;
74 }
75 unless ($types{'lccn'}) {
76     # Add LCCN field to biblioitems db
77     print "Adding lccn field to biblioitems table...\n";
78     my $sti=$dbh->prepare("alter table biblioitems add column lccn char(25)");
79     $sti->execute;
80 }
81 unless ($types{'marc'}) {
82     # Add MARC field to biblioitems db (not used anymore)
83     print "Adding marc field to biblioitems table...\n";
84     my $sti=$dbh->prepare("alter table biblioitems add column marc text");
85     $sti->execute;
86 }
87
88 # Get list of columns from biblioitems table
89 my %itemtypes;
90
91 my $sth=$dbh->prepare("show columns from items");
92 $sth->execute;
93 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
94     $itemtypes{$column}=$type;
95 }
96
97 unless ($itemtypes{'barcode'} eq 'varchar(20)') {
98     $itemtypes{'barcode'}=~/varchar\((\d+)\)/;
99     my $oldlength=$1;
100     if ($oldlength<20) {
101         print "Setting maximum barcode length to 20 (was $oldlength).\n";
102         my $sti=$dbh->prepare("alter table items change barcode barcode varchar(20) not null");
103         $sti->execute;
104     }
105 }
106
107 # extending the timestamp in branchtransfers...
108 my %branchtransfers;
109
110 my $sth=$dbh->prepare("show columns from branchtransfers");
111 $sth->execute;
112 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
113     $branchtransfers{$column}=$type;
114 }
115
116 unless ($branchtransfers{'datesent'} eq 'datetime') {
117     print "Setting type of datesent in branchtransfers to datetime.\n";
118     my $sti=$dbh->prepare("alter table branchtransfers change datesent datesent datetime");
119     $sti->execute;
120 }
121
122 unless ($branchtransfers{'datearrived'} eq 'datetime') {
123     print "Setting type of datearrived in branchtransfers to datetime.\n";
124     my $sti=$dbh->prepare("alter table branchtransfers change datearrived datearrived datetime");
125     $sti->execute;
126 }
127
128 # changing the branchcategories table around...
129 my %branchcategories;
130
131 my $sth=$dbh->prepare("show columns from branchcategories");
132 $sth->execute;
133 while (my ($column, $type, $null, $key, $default, $extra) = $sth->fetchrow) {
134     $branchcategories{$column}=$type;
135 }
136
137 unless ($branchcategories{'categorycode'} eq 'varchar(4)') {
138     print "Setting type of categorycode in branchcategories to varchar(4),\n and making the primary key.\n";
139     my $sti=$dbh->prepare("alter table branchcategories change categorycode categorycode varchar(4) not null");
140     $sth->execute;
141     $sth=$dbh->prepare("alter table branchcategories add primary key (categorycode)");
142     $sth->execute;
143 }
144
145 unless ($branchcategories{'branchcode'} eq 'varchar(4)') {
146     print "Changing branchcode in branchcategories to categoryname text.\n";
147     my $sth=$dbh->prepare("alter table branchcategories change branchcode categoryname text");
148     $sth->execute;
149 }
150
151 unless ($branchcategories{'codedescription'} eq 'text') {
152     print "Replacing branchholding in branchcategories with codedescription text.\n";
153     my $sth=$dbh->prepare("alter table branchcategories change branchholding codedescription text");
154     $sth->execute;
155 }
156
157 # Create new branchrelations table if it doesnt already exist....
158 my $branchrelationsexists;
159
160 my $sth=$dbh->prepare("show tables");
161 $sth->execute;
162 while (my ($tablename) = $sth->fetchrow) {
163     if ($tablename == "branchrelations") {
164         $branchrelationsexists = 1;
165     }
166 }
167
168 unless ($branchrelationsexists) {
169     print "creating branchrelations table";
170     my $sth->prepare("create table branchrelations (branchcode varchar(4), categorycode varchar(4))");
171     $sth->execute;
172 }
173
174 $sth->finish;
175 $dbh->disconnect;