From bf276b2bde33aabd3534d49bb4ea8accfb8dd4bb Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Mon, 2 Nov 2009 16:50:42 +0000 Subject: [PATCH 1/1] Ready to write id compacter --- yarrg/db-idempotent-populate | 200 +++++++++++++++++++++++++---------- 1 file changed, 147 insertions(+), 53 deletions(-) diff --git a/yarrg/db-idempotent-populate b/yarrg/db-idempotent-populate index 5e0a8d5..24dbf35 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; + #---------- setup ---------- parse_info_serverside(); @@ -55,27 +56,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 +124,115 @@ 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:\n"; + print " $_\n" foreach @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', <execute($cl, $incl{$cl}); } - db_chkcommit(); } + #---------- vessel types ---------- { my $idempotent= $dbh->prepare(<<'END') @@ -272,5 +358,13 @@ END my @qa= ($name, $shotdamage, map { $v->{$_} } qw(Mass Volume)); $idempotent->execute(@qa); } - db_chkcommit(); +} + + +#---------- put it all into effect ---------- +db_chkcommit(); +{ + local $dbh->{AutoCommit} = 1; + print " Vacuuming.\n"; + $dbh->do('VACUUM'); } -- 2.30.2