--- pgdiff 2003/08/12 10:38:23 1.1 +++ pgdiff 2003/08/12 11:43:36 1.2 @@ -74,7 +74,7 @@ my $sql = shift; $sql =~ s/[\n\r]/ /gs; $sql =~ s/\s\s+/ /g; - print STDERR "SQL: $sql\n"; + print STDERR "DEBUG: SQL: $sql\n"; } sub debug_row { @@ -82,10 +82,10 @@ my $row = shift; my @cols = @_; if (! $row) { - print STDERR "ROW data is undef!\n"; + print STDERR "DEBUG: ROW data is undef!\n"; return; } - print STDERR "ROW: [",$#cols+1,"] "; + print STDERR "DEBUG: ROW: [",$#cols+1,"] "; foreach my $col (@cols) { print STDERR "$col:"; if ($row->{$col}) { @@ -98,6 +98,11 @@ print STDERR "\n"; } +sub debug { + return if (!$debug); + print STDERR "DEBUG: ",@_; +} + $verbose = 1 if ($debug); # which tables to compare? @@ -118,6 +123,9 @@ AND pg_catalog.pg_table_is_visible(c.oid) and c.relname not like '_rserv_%' "; + foreach my $table (@tables) { + $sql .= " and c.relname like '$table'"; + } my $sth = $mdbh->prepare($sql); $sth->execute() || die; while(my $row = $sth->fetchrow_hashref()) { @@ -125,7 +133,7 @@ } } -print "Comparing tables: ",join(", ",@tables),"\n" if ($debug); +debug "Comparing tables: ".join(", ",@tables)."\n"; my $cols; my $diff_total = 0; @@ -196,7 +204,7 @@ $sth->finish(); if ($debug) { - print STDERR "table $table [$oid] not null: (",join(", ",@cols_notnull),")"; + print STDERR "DEBUG: table $table [$oid] not null: (",join(", ",@cols_notnull),")"; print STDERR " - null: (",join(", ",@cols_null),")" if (@cols_null); print STDERR " - skip: (",join(", ",@cols_skip),")" if (@cols_skip); print STDERR "\n"; @@ -287,7 +295,7 @@ my $order = sql_order(@cols_pk); $msql .= $order; - $ssql .= sql_where(@cols_pk) . $order; + $ssql .= $order; debug_sql($msql); debug_sql($ssql); @@ -296,50 +304,58 @@ $msth->execute() || die; my $ssth = $sdbh->prepare($ssql) || die; + $ssth->execute() || die; my $diff_row = 0; my ($mrow,$srow); - my ($have_mrow,$have_srow) = (0,0); - my @pk_val; + # have_* + use constant NO_ROW => 0; + use constant FETCH_ROW => 1; + use constant HAVE_ROW => 2; + my ($have_mrow,$have_srow) = (FETCH_ROW,FETCH_ROW); + + while ($have_mrow != NO_ROW || $have_srow != NO_ROW) { + + debug "have mrow: $have_mrow srow: $have_srow\n"; + + sub pk_val { + my $row = shift || die "need row"; + my @cols = shift || die "need cols"; + my @val; + foreach my $col (@cols) { + push @val,$row->{$col}; + } + return @val; + } - my $more_rows = 1; - while (!$have_mrow || !$have_srow) { # fetch row from master - if (!$have_mrow) { - print "fetch row from master [$more_rows]: $msql\n" if ($debug); + if ($have_mrow == FETCH_ROW) { + debug "fetch row from master: $msql\n"; $mrow = $msth->fetchrow_hashref(); debug_row($mrow,@cols); if ($mrow) { # fill-in primary key values - @pk_val = (); - foreach my $col (@cols_pk) { - push @pk_val,$mrow->{$col}; - } - $have_mrow = 1; + $have_mrow = HAVE_ROW; } else { - $have_mrow = 0; + $have_mrow = NO_ROW; } } # fetch row from slave - if (!$have_srow) { - print "fetch row from slave [$more_rows]: $ssql\n" if ($debug); - $ssth->execute(@pk_val) || die; + if ($have_srow == FETCH_ROW) { + debug "fetch row from slave: $ssql\n"; $srow = $ssth->fetchrow_hashref(); debug_row($srow,@cols); if ($srow) { - $have_srow = 1; + $have_srow = HAVE_ROW; } else { - $have_srow = 0; + $have_srow = NO_ROW; } } - # end of this table? - if (!$have_mrow && !$have_srow) { - last; - } + debug "have mrow: $have_mrow srow: $have_srow\n"; # insert into slave database sub sql_insert { @@ -387,29 +403,33 @@ # check key cols for row foreach my $col (@cols_pk) { - if ( (!$mrow && $srow) || ($mrow && $srow && ($mrow->{$col} gt $srow->{$col})) ) { + if ( ($have_mrow == NO_ROW && $have_srow == HAVE_ROW) || + ($have_mrow == HAVE_ROW && $have_srow == HAVE_ROW && $mrow->{$col} gt $srow->{$col}) ) { $diff_row++; $pk_same = 0; print sql_delete($table,$srow,@cols_pk),"\n"; - $have_srow = 0; # fetch new slave row + $have_srow = FETCH_ROW; last; - } elsif ( ($mrow && !$srow) || ($mrow && $srow && ($mrow->{$col} lt $srow->{$col})) ) { + } elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) || + ($have_mrow == HAVE_ROW && $have_srow == HAVE_ROW && $mrow->{$col} lt $srow->{$col}) ) { $diff_row++; $pk_same = 0; print sql_insert($table,$mrow,@cols),"\n"; - $have_mrow = 0; + $have_mrow = FETCH_ROW; last; } } - if ($pk_same) { + if ($pk_same && $have_mrow == HAVE_ROW && $have_srow == HAVE_ROW) { # check non-key cols for row foreach my $col (@cols_cmp) { if ($mrow->{$col} ne $srow->{$col}) { $diff_row++; - print STDERR "DIFF in table '$table' row ($col): [".join(",",@pk_val)."] '$mrow->{$col}' != '$srow->{$col}'\n" if ($verbose); + print STDERR "DIFF in table '$table' row ($col): [".join(",",@cols_pk)."] '$mrow->{$col}' != '$srow->{$col}'\n" if ($verbose); print sql_delete($table,$srow,@cols_pk),"\n"; print sql_insert($table,$mrow,@cols),"\n"; + $have_mrow = FETCH_ROW; + $have_srow = FETCH_ROW; } } }