X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.db-test.git;a=blobdiff_plain;f=yarrg%2Fdb-idempotent-populate;h=aa8026272f3727f77ccd7c740eb7da43040fe110;hp=5e0a8d5059d3f4ccc2d719257c8e76d7113430d0;hb=ee1e57fa0fab5d840206d73a3968fd59d7fa7127;hpb=e353043ae5bf5a7df54f650c51f00d6c3b862ba9 diff --git a/yarrg/db-idempotent-populate b/yarrg/db-idempotent-populate index 5e0a8d5..aa80262 100755 --- a/yarrg/db-idempotent-populate +++ b/yarrg/db-idempotent-populate @@ -45,6 +45,8 @@ if (@ARGV and $ARGV[0] eq '-D') { @ARGV==1 or die; my ($oceanname) = @ARGV; +$|=1; + #---------- setup ---------- parse_info_serverside(); @@ -55,27 +57,67 @@ db_connect(); $dbh->trace(1) if $trace; -#---------- schema ---------- -foreach my $bs (qw(buy sell)) { - db_doall(<prepare(<execute($table,$table); + my ($sql)= $autoinc->fetchrow_array(); + die unless defined $sql; + push @need_recreate, 'remove autoinc' + if $sql =~ m/\bautoinc/i; + } + + #----- check whether we need to add fields ----- my $check= $dbh->prepare("SELECT * FROM $table LIMIT 1"); $check->execute(); @@ -83,69 +125,114 @@ sub table ($$) { $have_fields{$_}=1 foreach @{ $check->{NAME_lc} }; $check->finish(); - my (@have_fields, @missing_fields); - my $have_field_specs=''; + my @have_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); + foreach my $f (@want_fields) { 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"; + push @have_field_specs, $want_field_specs{$f}; } else { - push @missing_fields, $f; + push @need_recreate, "field $f"; } } - return unless @missing_fields; - print " Adding missing fields to $table: @missing_fields ...\n"; + #----- Do we need to recreate ? ----- + return unless @need_recreate; + # yes: + + print " Recreating $table: ", join('; ',@need_recreate); my $have_fields= join ',', @have_fields; + my $have_field_specs= join ",\n", @have_field_specs; db_doall(< $table, + Id => $cpact_idfield, + Updates => $cpact_needupdates, + Fields => [ @want_fields ], + FieldSpecs => $want_field_specs + }; } -table('commods', <prepare(<<'END') + my $insert= $dbh->prepare(<<'END'); INSERT OR IGNORE INTO commods (unitmass, unitvolume, commodname) VALUES (?,?,?); END - ; - my $setsizes= $dbh->prepare(<<'END') + my $setsizes= $dbh->prepare(<<'END'); UPDATE commods SET unitmass = ?, unitvolume = ? WHERE commodname = ? END - ; - my $setordval= $dbh->prepare(<<'END') + my $setordval= $dbh->prepare(<<'END'); UPDATE commods SET ordval = ? WHERE commodname = ? END - ; - my $setclass= $dbh->prepare(<<'END') + my $setclass= $dbh->prepare(<<'END'); UPDATE commods SET commodclass = ? WHERE commodname = ? END - ; - my $setinclass= $dbh->prepare(<<'END') + my $setinclass= $dbh->prepare(<<'END'); UPDATE commods SET inclass = ? WHERE commodname = ? END - ; my %incl; foreach my $commod (sort { commodsortkey($a) cmp commodsortkey($b) @@ -245,18 +326,63 @@ END db_doall(<prepare(<<'END') + my $addclass= $dbh->prepare(<<'END'); INSERT INTO commodclasses (commodclass, size) VALUES (?,?) END - ; foreach my $cl (sort keys %incl) { $addclass->execute($cl, $incl{$cl}); } - db_chkcommit(); + + my $search= $dbh->prepare(<<'END'); + SELECT commodname,commodid FROM commods; +END + my %check; + foreach my $bs (qw(buy sell)) { + $check{$bs}= $dbh->prepare(<prepare(<<'END'); + DELETE FROM commods WHERE commodid = ? +END + $search->execute(); + my $any=0; + while (my $row= $search->fetchrow_hashref()) { + next if defined $commods{$row->{'commodname'}}; + print $any++ ? '; ' : " Dropping old commodities: ", + $row->{'commodname'}; + foreach my $bs (qw(buy sell)) { + $check{$bs}->execute($row->{'commodid'}); + my $problem= $check{$bs}->fetchrow_hashref(); + if ($problem) { + print "\n"; + die <{'commodid'} + $row->{'commodname'} + but + $bs + $problem->{'islandname'} + $problem->{'stallname'} + $problem->{'qty'} at $problem->{'price'} +END + } + } + $delete->execute($row->{'commodid'}); + } + print ".\n" if $any; + db_check_referential_integrity(); } + #---------- vessel types ---------- { my $idempotent= $dbh->prepare(<<'END') @@ -272,5 +398,72 @@ END my @qa= ($name, $shotdamage, map { $v->{$_} } qw(Mass Volume)); $idempotent->execute(@qa); } - db_chkcommit(); +} + + +#---------- compact IDs ---------- + +sub getminmax ($$$) { + my ($tab,$minmax,$f) = @_; + my $sth= $dbh->prepare("SELECT $minmax($f) FROM $tab"); + $sth->execute(); + my ($val)= $sth->fetchrow_array(); + return defined($val) ? $val : '?'; +} + +foreach my $cp (@need_compact) { + print " Compacting $cp->{Table}"; + my $tab= $cp->{Table}; + my $id= $cp->{Id}; + my $tmp_field_specs= $cp->{FieldSpecs}; + my $fields= join ',', @{$cp->{Fields}}; + $tmp_field_specs =~ s/\bprimary key\b/UNIQUE/i or + die "$tab $tmp_field_specs ?"; + db_doall(<1..%d:", + $cp->{Id}, + getminmax($tab,'min',$id), + $oldmax, + getminmax("aside_$tab",'max',"new_$id")); + my @updates= @{ $cp->{Updates} }; + while (@updates) { + my $utabs= shift @updates; + my $ufields= shift @updates; + foreach my $utab (@$utabs) { + printf(" %s",$utab); + my $fh= '.'; + foreach my $ufield (@$ufields) { + printf("%s%s",$fh,$ufield); $fh=','; + db_doall(<{AutoCommit} = 1; + $dbh->do('VACUUM'); }