From: Ian Jackson Date: Mon, 2 Nov 2009 15:41:52 +0000 (+0000) Subject: Do some referential integrity checking X-Git-Tag: 6.0~21 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~yarrgweb/git?p=ypp-sc-tools.web-live.git;a=commitdiff_plain;h=735a20880b0e6c0b5ad2d62f77f2c3afd0d77ad4 Do some referential integrity checking --- diff --git a/yarrg/db-idempotent-populate b/yarrg/db-idempotent-populate index 6ca8382..2cce5c7 100755 --- a/yarrg/db-idempotent-populate +++ b/yarrg/db-idempotent-populate @@ -55,6 +55,113 @@ db_connect(); $dbh->trace(1) if $trace; +#---------- referential integrity constraints ---------- + +# SQLite doesn't support foreign key constraints so we do it by steam: + +sub nooutput ($) { + my ($stmts) = @_; + my $ekindcount= 0; + my $letxt= ''; + foreach my $stmt (split /\;/, $stmts) { + next unless $stmt =~ /\S/; + + my $etxt= ''; + $stmt =~ s/^([ \t]*\#.*)$/ $etxt .= $1."\n"; ''; /mge; + $etxt= $letxt unless length $etxt; + $letxt= $etxt; + + $stmt =~ s/^\s+//; $stmt =~ s/\s+$//; + my $sth= $dbh->prepare($stmt); + $sth->execute(); + my $row; + my $ecount= 0; + my @cols= @{ $sth->{NAME_lc} }; + my $w= 11; + while ($row= $sth->fetchrow_hashref) { + if (!$ecount++) { + print STDERR "REFERENTIAL INTEGRITY ERROR\n"; + print STDERR "\n$etxt\n $stmt\n\n"; + printf STDERR "|%-${w}s", $_ foreach @cols; print STDERR "|\n"; + print STDERR "+",('-'x$w) foreach @cols; print STDERR "+\n"; + } + if ($ecount>5) { print STDERR "...\n"; last; } + printf STDERR "|%-$w.${w}s", + (defined $row->{$_} ? $row->{$_} : 'NULL') + foreach @cols; + print STDERR "\n"; + } + next unless $ecount; + + $ekindcount++; + print STDERR "\n\n"; + } + die "REFERENTIAL INTEGRITY ERRORS $ekindcount\n" + if $ekindcount; +} + +sub check_referential_integrity () { + foreach my $bs (qw(buy sell)) { + nooutput(< 1; + +END +} + +sub chkcommit () { + check_referential_integrity(); + $dbh->commit(); +} + #---------- schema ---------- foreach my $bs (qw(buy sell)) { @@ -117,7 +224,6 @@ $fields ); DROP TABLE aside_$table; END - $dbh->commit; } table('commods', <commit; +chkcommit(); #---------- commodity list ---------- @@ -253,7 +359,7 @@ END foreach my $cl (sort keys %incl) { $addclass->execute($cl, $incl{$cl}); } - $dbh->commit; + chkcommit(); } #---------- vessel types ---------- @@ -271,5 +377,5 @@ END my @qa= ($name, $shotdamage, map { $v->{$_} } qw(Mass Volume)); $idempotent->execute(@qa); } - $dbh->commit; + chkcommit(); }