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=24dbf35c05b9fd3017007168bcdf1a998ce68adc;hb=bb95133fcfbd4698daa59debdbaa73a4d1e6252a;hpb=bf276b2bde33aabd3534d49bb4ea8accfb8dd4bb diff --git a/yarrg/db-idempotent-populate b/yarrg/db-idempotent-populate index 24dbf35..aa80262 100755 --- a/yarrg/db-idempotent-populate +++ b/yarrg/db-idempotent-populate @@ -45,6 +45,7 @@ if (@ARGV and $ARGV[0] eq '-D') { @ARGV==1 or die; my ($oceanname) = @ARGV; +$|=1; #---------- setup ---------- @@ -140,8 +141,7 @@ END return unless @need_recreate; # yes: - print " Recreating $table:\n"; - print " $_\n" foreach @need_recreate; + print " Recreating $table: ", join('; ',@need_recreate); my $have_fields= join ',', @have_fields; my $have_field_specs= join ",\n", @have_field_specs; @@ -163,12 +163,12 @@ $want_field_specs END #----- Do we need to compact ids ? ----- - return unless + (print("\n"), return) unless defined $cpact_idfield and grep { m/^remove autoinc/ } @need_recreate; # yes: - print " will compact\n"; + print "; will compact.\n"; unshift @$cpact_needupdates, [ $table ], [ $cpact_idfield ]; push @need_compact, { @@ -273,39 +273,34 @@ sub commodsortkey ($) { } { - my $insert= $dbh->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) @@ -331,15 +326,60 @@ 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}); } + + 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(); } @@ -361,10 +401,69 @@ END } +#---------- 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; - print " Vacuuming.\n"; $dbh->do('VACUUM'); }