Modularize table creation, driven by hash instead of redundant code.
authoramillar <amillar>
Tue, 21 May 2002 05:47:24 +0000 (05:47 +0000)
committeramillar <amillar>
Tue, 21 May 2002 05:47:24 +0000 (05:47 +0000)
updater/updatedatabase

index d98d07b..76895e2 100755 (executable)
@@ -16,12 +16,44 @@ use DBI;
 
 # Koha modules
 use C4::Database;
-#use C4::Catalogue;
-use C4::Acquisitions;
-use C4::Output;
 
-my %tables;
+my %existingtables;    # tables already in database
 my %types;
+my $table;
+
+#-------------------
+# Defines
+
+# Tables to add if they don't exist
+my %requiretables=(
+    shelfcontents=>"( shelfnumber int not null, 
+       itemnumber int not null, 
+       flags int)",
+    bookshelf=>"( shelfnumber int auto_increment primary key, 
+       shelfname char(255))",
+    z3950queue=>"( id int auto_increment primary key, 
+       term text, 
+       type char(10), 
+       startdate int, 
+       enddate int, 
+       done smallint, 
+       results longblob, 
+       numrecords int, 
+       servers text, 
+       identifier char(30))",
+    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)",
+    branchrelations=>"( branchcode varchar(4), 
+       categorycode varchar(4))",
+);
 
 #-------------------
 # Initialize
@@ -44,62 +76,25 @@ if ($mysqlversion ge '3.23') {
 my $sth=$dbh->prepare("show tables");
 $sth->execute;
 while (my ($table) = $sth->fetchrow) {
-    $tables{$table}=1;
+    $existingtables{$table}=1;
 }
 
 # Now add any missing tables
-
-# Add tables for virtual bookshelf management
-unless ($tables{'shelfcontents'}) {
-    print "Adding shelfcontents table...\n";
-    my $sti=$dbh->prepare("create table shelfcontents (
-       shelfnumber int not null, 
-       itemnumber int not null, 
-       flags int)");
-    $sti->execute;
-}
-unless ($tables{'bookshelf'}) {
-    print "Adding bookshelf table...\n";
-    my $sti=$dbh->prepare("create table bookshelf (
-       shelfnumber int auto_increment primary key, 
-       shelfname char(255))");
-    $sti->execute;
-}
-
-# Add tables required by Z-3950 scripts
-
-unless ($tables{'z3950queue'}) {
-    print "Adding z3950queue table...\n";
-    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, 
-       numrecords int, 
-       servers text, 
-       identifier char(30))");
-    $sti->execute;
-}
-
-unless ($tables{'z3950results'}) {
-    print "Adding z3950results table...\n";
-    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)");
-    $sti->execute;
-}
-unless ($tables{'z3950servers'}) {
+foreach $table ( keys %requiretables ) {
+    unless ($existingtables{$table} ) {
+       print "Adding $table table...\n";
+       my $sth=$dbh->prepare(
+               "create table $table $requiretables{$table}" );
+       $sth->execute;
+        if ($sth->err) {
+                print "Error : $sth->errstr \n";
+                $sth->finish;
+        } # if error
+    } # unless exists
+} # foreach
+exit;
+
+unless ($existingtables{'z3950servers'}) {
     print "Adding z3950servers table...\n";
     my $sti=$dbh->prepare("create table z3950servers (
        host char(255), 
@@ -122,15 +117,6 @@ unless ($tables{'z3950servers'}) {
     $sti->execute;
 }
 
-# Create new branchrelations table if it doesnt already exist....
-unless ($tables{'branchrelations'} ) {
-    print "creating branchrelations table\n";
-    my $sth=$dbh->prepare("create table branchrelations (
-       branchcode varchar(4), 
-       categorycode varchar(4))");
-    $sth->execute;
-}
-
 #---------------------------------
 # Columns