+sub table ($$) {
+ my ($table,$fields) = @_;
+ db_doall(" CREATE TABLE IF NOT EXISTS $table (\n$fields );");
+
+ my $check= $dbh->prepare("SELECT * FROM $table LIMIT 1");
+ $check->execute();
+ my %have_fields;
+ $have_fields{$_}=1 foreach @{ $check->{NAME_lc} };
+ $check->finish();
+
+ my (@have_fields, @missing_fields);
+ my $have_field_specs='';
+
+ foreach my $fspec (split /,/, $fields) {
+ next unless $fspec =~ m/\S/;
+ $fspec =~ m/^\s*(\w+)\s+(\w.*\S)\s*$/ or die "$table $fspec ?";
+ my ($f,$spec) = ($1,$2);
+ if ($have_fields{$f}) {
+ push @have_fields, $f;
+ $have_field_specs .= ",\n" if length $have_field_specs;
+ $have_field_specs .= "\t$f\t\t$spec\n";
+ } else {
+ push @missing_fields, $f;
+ }
+ }
+
+ return unless @missing_fields;
+ print " Adding missing fields to $table: @missing_fields ...\n";
+
+ my $have_fields= join ',', @have_fields;
+
+ db_doall(<<END);
+ CREATE TEMPORARY TABLE aside_$table (
+$have_field_specs );
+ INSERT INTO aside_$table SELECT $have_fields FROM $table;
+
+ DROP TABLE $table;
+ CREATE TABLE $table (
+$fields );
+
+ INSERT INTO $table ($have_fields) SELECT $have_fields FROM aside_$table;
+
+ DROP TABLE aside_$table;
+END
+ $dbh->commit;
+}
+
+table('commods', <<END);
+ commodid INTEGER PRIMARY KEY NOT NULL,