]> git.imager.perl.org - bse.git/blobdiff - schema/mysql_build.pl
allow setting default for option on creation
[bse.git] / schema / mysql_build.pl
index 183a8021490749dbe90d4894921d9f7711a6d42a..65482554c69cab8d2809abeb5d833441bb092efb 100644 (file)
@@ -5,10 +5,11 @@ use strict;
 my $db = 'bsebuilder';
 my $un = 'bsebuilder';
 my $pw = 'bsebuilder';
-my $dist = "/home/tony/dev/bse/base/bse/schema/bse.sql";
+my $dist = shift || "schema/bse.sql";
 
 my $dbh = DBI->connect("dbi:mysql:$db", $un, $pw)
   or die "Cannot connect to db: ",DBI->errstr;
+$dbh->{PrintError} = 0; # we report our own errors
 
 my $tl = $dbh->prepare("show tables")
   or die "prepare show tables ",$dbh->errstr;
@@ -20,21 +21,41 @@ while (my $row = $tl->fetchrow_arrayref) {
   push(@drop_tables, $row->[0]);
 }
 undef $tl;
-for my $drop (@drop_tables) {
-  $dbh->do("drop table $drop")
-    or die "Could not drop old table: ", $dbh->errstr;
+my %tables = map { $_ => 1 } @drop_tables;
+my $error;
+my $dropped = 1;
+# need this loop to handle references between tables restricting us
+# from dropping them
+while ($dropped && keys %tables) {
+  my $dropped = 0;
+  my @tables = keys %tables;
+  for my $drop (@tables) { # not keys %tables, since we modify it
+    if ($dbh->do("drop table $drop")) {
+      ++$dropped;
+      delete $tables{$drop};
+    }
+    else {
+      $error = "Could not drop old table: ". $dbh->errstr;
+    }
+  }
+}
+if (keys %tables) {
+  print "Could not drop bsebuilder tables:\n   ", join("\n  ", sort keys %tables), "\n";
+  die $error;
 }
 
 system "mysql -u$un -p$pw $db <$dist"
   and die "Error loading database";
 
-$tl = $dbh->prepare("show tables")
-  or die "prepare show tables ",$dbh->errstr;
+$tl = $dbh->prepare("show table status")
+  or die "prepare show table status ",$dbh->errstr;
 $tl->execute
-  or die "execute show tables ",$tl->errstr;
+  or die "execute show table status ",$tl->errstr;
 my @tables;
+my %engines;
 while (my $row = $tl->fetchrow_arrayref) {
   push(@tables, $row->[0]);
+  $engines{$row->[0]} = $row->[1];
 }
 undef $tl;
 
@@ -42,6 +63,7 @@ my @expected = qw(field type null key default extra);
 my @want =     qw(field type null default extra);
 for my $table (@tables) {
   print "Table $table\n";
+  print "Engine $engines{$table}\n";
   my $ti = $dbh->prepare("describe $table")
     or die "prepare describe $table: ",$dbh->errstr;
   $ti->execute()