--- pgdiff 2003/08/12 18:45:04 1.4 +++ pgdiff 2003/08/13 13:27:32 1.6 @@ -107,7 +107,7 @@ $verbose = 1 if ($debug); # init object for scheme in master database -my $mscheme = new Pg::Scheme( 'dbh' => $mdbh ) || die "can't query schema"; +my $mscheme = new Pg::Scheme( 'dbh' => $mdbh, 'DEBUG' => 0 ) || die "can't query schema"; # which tables to compare? @@ -124,32 +124,35 @@ # diff schema - my @cols_notnull;# colums compared by a=b - my @cols_null; # colums compared by a=b or a is null and b is null + # all colums (for insert) + my @cols = @{$mscheme->cols($table)}; + + # colums compared by a=b + my @cols_notnull = @{$mscheme->cols_notnull($table)}; + + # colums compared by a=b or a is null and b is null + my @cols_null = @{$mscheme->cols_null($table)}; + + # primary key columns + my @cols_pk = @{$mscheme->cols_pk($table)}; + + # columns to compare (not in primary key) + my @cols_cmp = @{$mscheme->cols_notpk($table)}; + my @cols_skip; # skipped columns my @cols_test; # all colums to test (without skipped) - my @cols; # all colums (for insert) - - foreach my $row ($mscheme->explain_table($table)) { + foreach my $row (@{$mscheme->pg_attribute($table)}) { # attname | format_type | attnotnull | atthasdef | attnum - push @cols,$row->{attname}; - # FIXME: do something with attributes which shouldn't be compared # (date, time, datetime, timestamp) if ($row->{format_type} =~ /(date)|(time)/i) { push @cols_skip,$row->{attname}; - next; - } - - push @cols_test,$row->{attname}; - - if ($row->{attnotnull}) { - push @cols_notnull,$row->{attname}; } else { - push @cols_null,$row->{attname}; + push @cols_test,$row->{attname}; } + } if ($debug) { @@ -161,55 +164,11 @@ # diff data - my @cols_pk; # columns which are primary key - my %in_pk; - - $sql=" -SELECT - i.indexrelid as indexrelid, i.indrelid as indrelid, - count(a.attname) as cols_in_pk -FROM - pg_catalog.pg_class c, - pg_catalog.pg_index i, - pg_catalog.pg_attribute a -WHERE - c.oid = i.indrelid - and i.indisunique - and c.relname = '$table' - and a.attrelid = i.indexrelid -GROUP BY - i.indexrelid, i.indrelid, c.relname, i.indisprimary, i.indisunique -ORDER BY - cols_in_pk ASC, i.indisprimary DESC, i.indisunique DESC, c.relname DESC -"; - debug_sql($sql); - $sth = $mdbh->prepare($sql); - $sth->execute() || die; - my $row = $sth->fetchrow_hashref(); - if ($row) { - $sql=" - select a1.attname as attname from pg_attribute a1, pg_attribute a2 where a1.attrelid = ".$row->{indexrelid}." and a2.attrelid=".$row->{indrelid}." and a1.attname = a2.attname and a2.attnotnull"; - - debug_sql($sql); - my $sth2 = $mdbh->prepare($sql); - $sth2->execute() || die; - @cols_pk = (); - while (my $row2 = $sth2->fetchrow_hashref()) { - push @cols_pk,$row2->{attname}; - $in_pk{$row2->{attname}}++; - } - - } if (! @cols_pk) { print STDERR "can't find PK rows for table '$table' using all\n"; @cols_pk = @cols; } - my @cols_cmp; # columns to compare - - foreach my $col (@cols_test) { - push @cols_cmp,$col if (! $in_pk{$col}); - } if ($verbose) { print "table '$table' using for key: (",join(", ",@cols_pk),") to compare cols: (",join(", ",@cols_cmp),")\n"; @@ -307,6 +266,7 @@ # insert into slave database sub sql_insert { + my $dbh = shift @_ || die "need dbh"; my $table = shift @_ || die "need table as argument"; my $row = shift @_ || die "need row data"; my @cols = @_; @@ -314,7 +274,7 @@ my $sql = "insert into $table (".join(",",@cols).") values ("; my $comma = ""; foreach my $col (@cols) { - $sql .= $comma.$mdbh->quote($row->{$col}); + $sql .= $comma.$dbh->quote($row->{$col}); $comma = ","; } $sql.=")"; @@ -324,15 +284,16 @@ # delete from slave database sub sql_delete { + my $dbh = shift @_ || die "need dbh"; my $table = shift @_ || die "need table as argument"; my $row = shift @_ || die "need row as argument"; my @cols_pk = @_; my $where = sql_where(@cols_pk); - my $sql = "delete from $table "; + my $sql = "delete from $table"; foreach my $col (@cols_pk) { - my $val = $sdbh->quote($row->{$col}) || die "can't find value in row for col $col"; + my $val = $dbh->quote($row->{$col}) || die "can't find value in row for col $col"; $where =~ s/\?/$val/; } $sql .= $where; @@ -342,12 +303,19 @@ # update row in slave database sub sql_update { + my $dbh = shift @_ || die "need dbh"; my $table = shift @_ || die "need table as argument"; my $col = shift @_ || die "need col to update"; - my $val = shift @_ || die "need new val"; - my @cols_pk = @_ || die "need pk idenitifier"; + my $row = shift @_ || die "need row"; + my @cols_pk = @_; - my $sql = "udate $table set $col=".$mdbh->quote($val); + my $sql = "update $table set $col=".$dbh->quote($row->{$col}); + my $where = sql_where(@cols_pk); + foreach my $col (@cols_pk) { + my $val = $dbh->quote($row->{$col}) || die "can't find value in row for col $col"; + $where =~ s/\?/$val/; + } + $sql .= $where; debug_sql($sql); return $sql; } @@ -367,7 +335,7 @@ $diff_row++; $pk_same = 0; print STDERR "EXTRA row in table '$table' pk: [".join(",",@cols_pk)."] value (".join(",",pk_val($srow,@cols_pk)).")\n" if ($verbose); - print sql_delete($table,$srow,@cols_pk),";\n"; + print sql_delete($sdbh,$table,$srow,@cols_pk),";\n"; $have_srow = FETCH_ROW; last; } elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) || @@ -375,7 +343,7 @@ $diff_row++; $pk_same = 0; print STDERR "MISSING row in table '$table' pk: [".join(",",@cols_pk)."] value (".join(",",pk_val($mrow,@cols_pk)).")\n" if ($verbose); - print sql_insert($table,$mrow,@cols),";\n"; + print sql_insert($mdbh,$table,$mrow,@cols),";\n"; $have_mrow = FETCH_ROW; last; } @@ -387,7 +355,7 @@ if ($mrow->{$col} ne $srow->{$col}) { $diff_row++; print STDERR "DIFF in table '$table' row ($col): [".join(",",@cols_pk)."] '$mrow->{$col}' != '$srow->{$col}'\n" if ($verbose); - print sql_update($table,$col,$mrow->{$col},@cols_pk),";\n"; + print sql_update($mdbh,$table,$col,$mrow,@cols_pk),";\n"; } } $have_mrow = FETCH_ROW;