74 |
my $sql = shift; |
my $sql = shift; |
75 |
$sql =~ s/[\n\r]/ /gs; |
$sql =~ s/[\n\r]/ /gs; |
76 |
$sql =~ s/\s\s+/ /g; |
$sql =~ s/\s\s+/ /g; |
77 |
print STDERR "SQL: $sql\n"; |
print STDERR "DEBUG: SQL: $sql\n"; |
78 |
} |
} |
79 |
|
|
80 |
sub debug_row { |
sub debug_row { |
82 |
my $row = shift; |
my $row = shift; |
83 |
my @cols = @_; |
my @cols = @_; |
84 |
if (! $row) { |
if (! $row) { |
85 |
print STDERR "ROW data is undef!\n"; |
print STDERR "DEBUG: ROW data is undef!\n"; |
86 |
return; |
return; |
87 |
} |
} |
88 |
print STDERR "ROW: [",$#cols+1,"] "; |
print STDERR "DEBUG: ROW: [",$#cols+1,"] "; |
89 |
foreach my $col (@cols) { |
foreach my $col (@cols) { |
90 |
print STDERR "$col:"; |
print STDERR "$col:"; |
91 |
if ($row->{$col}) { |
if ($row->{$col}) { |
98 |
print STDERR "\n"; |
print STDERR "\n"; |
99 |
} |
} |
100 |
|
|
101 |
|
sub debug { |
102 |
|
return if (!$debug); |
103 |
|
print STDERR "DEBUG: ",@_; |
104 |
|
} |
105 |
|
|
106 |
$verbose = 1 if ($debug); |
$verbose = 1 if ($debug); |
107 |
|
|
108 |
# which tables to compare? |
# which tables to compare? |
123 |
AND pg_catalog.pg_table_is_visible(c.oid) |
AND pg_catalog.pg_table_is_visible(c.oid) |
124 |
and c.relname not like '_rserv_%' |
and c.relname not like '_rserv_%' |
125 |
"; |
"; |
126 |
|
foreach my $table (@tables) { |
127 |
|
$sql .= " and c.relname like '$table'"; |
128 |
|
} |
129 |
my $sth = $mdbh->prepare($sql); |
my $sth = $mdbh->prepare($sql); |
130 |
$sth->execute() || die; |
$sth->execute() || die; |
131 |
while(my $row = $sth->fetchrow_hashref()) { |
while(my $row = $sth->fetchrow_hashref()) { |
133 |
} |
} |
134 |
} |
} |
135 |
|
|
136 |
print "Comparing tables: ",join(", ",@tables),"\n" if ($debug); |
debug "Comparing tables: ".join(", ",@tables)."\n"; |
137 |
|
|
138 |
my $cols; |
my $cols; |
139 |
my $diff_total = 0; |
my $diff_total = 0; |
204 |
$sth->finish(); |
$sth->finish(); |
205 |
|
|
206 |
if ($debug) { |
if ($debug) { |
207 |
print STDERR "table $table [$oid] not null: (",join(", ",@cols_notnull),")"; |
print STDERR "DEBUG: table $table [$oid] not null: (",join(", ",@cols_notnull),")"; |
208 |
print STDERR " - null: (",join(", ",@cols_null),")" if (@cols_null); |
print STDERR " - null: (",join(", ",@cols_null),")" if (@cols_null); |
209 |
print STDERR " - skip: (",join(", ",@cols_skip),")" if (@cols_skip); |
print STDERR " - skip: (",join(", ",@cols_skip),")" if (@cols_skip); |
210 |
print STDERR "\n"; |
print STDERR "\n"; |
295 |
|
|
296 |
my $order = sql_order(@cols_pk); |
my $order = sql_order(@cols_pk); |
297 |
$msql .= $order; |
$msql .= $order; |
298 |
$ssql .= sql_where(@cols_pk) . $order; |
$ssql .= $order; |
299 |
|
|
300 |
debug_sql($msql); |
debug_sql($msql); |
301 |
debug_sql($ssql); |
debug_sql($ssql); |
304 |
$msth->execute() || die; |
$msth->execute() || die; |
305 |
|
|
306 |
my $ssth = $sdbh->prepare($ssql) || die; |
my $ssth = $sdbh->prepare($ssql) || die; |
307 |
|
$ssth->execute() || die; |
308 |
|
|
309 |
my $diff_row = 0; |
my $diff_row = 0; |
310 |
|
|
311 |
my ($mrow,$srow); |
my ($mrow,$srow); |
312 |
my ($have_mrow,$have_srow) = (0,0); |
# have_* |
313 |
my @pk_val; |
use constant NO_ROW => 0; |
314 |
|
use constant FETCH_ROW => 1; |
315 |
|
use constant HAVE_ROW => 2; |
316 |
|
my ($have_mrow,$have_srow) = (FETCH_ROW,FETCH_ROW); |
317 |
|
|
318 |
|
while ($have_mrow != NO_ROW || $have_srow != NO_ROW) { |
319 |
|
|
320 |
|
debug "have mrow: $have_mrow srow: $have_srow\n"; |
321 |
|
|
322 |
|
sub pk_val { |
323 |
|
my $row = shift || die "need row"; |
324 |
|
my @cols = shift || die "need cols"; |
325 |
|
my @val; |
326 |
|
foreach my $col (@cols) { |
327 |
|
push @val,$row->{$col}; |
328 |
|
} |
329 |
|
return @val; |
330 |
|
} |
331 |
|
|
|
my $more_rows = 1; |
|
|
while (!$have_mrow || !$have_srow) { |
|
332 |
# fetch row from master |
# fetch row from master |
333 |
if (!$have_mrow) { |
if ($have_mrow == FETCH_ROW) { |
334 |
print "fetch row from master [$more_rows]: $msql\n" if ($debug); |
debug "fetch row from master: $msql\n"; |
335 |
$mrow = $msth->fetchrow_hashref(); |
$mrow = $msth->fetchrow_hashref(); |
336 |
debug_row($mrow,@cols); |
debug_row($mrow,@cols); |
337 |
|
|
338 |
if ($mrow) { |
if ($mrow) { |
339 |
# fill-in primary key values |
# fill-in primary key values |
340 |
@pk_val = (); |
$have_mrow = HAVE_ROW; |
|
foreach my $col (@cols_pk) { |
|
|
push @pk_val,$mrow->{$col}; |
|
|
} |
|
|
$have_mrow = 1; |
|
341 |
} else { |
} else { |
342 |
$have_mrow = 0; |
$have_mrow = NO_ROW; |
343 |
} |
} |
344 |
} |
} |
345 |
|
|
346 |
# fetch row from slave |
# fetch row from slave |
347 |
if (!$have_srow) { |
if ($have_srow == FETCH_ROW) { |
348 |
print "fetch row from slave [$more_rows]: $ssql\n" if ($debug); |
debug "fetch row from slave: $ssql\n"; |
|
$ssth->execute(@pk_val) || die; |
|
349 |
$srow = $ssth->fetchrow_hashref(); |
$srow = $ssth->fetchrow_hashref(); |
350 |
debug_row($srow,@cols); |
debug_row($srow,@cols); |
351 |
if ($srow) { |
if ($srow) { |
352 |
$have_srow = 1; |
$have_srow = HAVE_ROW; |
353 |
} else { |
} else { |
354 |
$have_srow = 0; |
$have_srow = NO_ROW; |
355 |
} |
} |
356 |
} |
} |
357 |
|
|
358 |
# end of this table? |
debug "have mrow: $have_mrow srow: $have_srow\n"; |
|
if (!$have_mrow && !$have_srow) { |
|
|
last; |
|
|
} |
|
359 |
|
|
360 |
# insert into slave database |
# insert into slave database |
361 |
sub sql_insert { |
sub sql_insert { |
403 |
|
|
404 |
# check key cols for row |
# check key cols for row |
405 |
foreach my $col (@cols_pk) { |
foreach my $col (@cols_pk) { |
406 |
if ( (!$mrow && $srow) || ($mrow && $srow && ($mrow->{$col} gt $srow->{$col})) ) { |
if ( ($have_mrow == NO_ROW && $have_srow == HAVE_ROW) || |
407 |
|
($have_mrow == HAVE_ROW && $have_srow == HAVE_ROW && $mrow->{$col} gt $srow->{$col}) ) { |
408 |
$diff_row++; |
$diff_row++; |
409 |
$pk_same = 0; |
$pk_same = 0; |
410 |
print sql_delete($table,$srow,@cols_pk),"\n"; |
print sql_delete($table,$srow,@cols_pk),"\n"; |
411 |
$have_srow = 0; # fetch new slave row |
$have_srow = FETCH_ROW; |
412 |
last; |
last; |
413 |
} elsif ( ($mrow && !$srow) || ($mrow && $srow && ($mrow->{$col} lt $srow->{$col})) ) { |
} elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) || |
414 |
|
($have_mrow == HAVE_ROW && $have_srow == HAVE_ROW && $mrow->{$col} lt $srow->{$col}) ) { |
415 |
$diff_row++; |
$diff_row++; |
416 |
$pk_same = 0; |
$pk_same = 0; |
417 |
print sql_insert($table,$mrow,@cols),"\n"; |
print sql_insert($table,$mrow,@cols),"\n"; |
418 |
$have_mrow = 0; |
$have_mrow = FETCH_ROW; |
419 |
last; |
last; |
420 |
} |
} |
421 |
} |
} |
422 |
|
|
423 |
if ($pk_same) { |
if ($pk_same && $have_mrow == HAVE_ROW && $have_srow == HAVE_ROW) { |
424 |
# check non-key cols for row |
# check non-key cols for row |
425 |
foreach my $col (@cols_cmp) { |
foreach my $col (@cols_cmp) { |
426 |
if ($mrow->{$col} ne $srow->{$col}) { |
if ($mrow->{$col} ne $srow->{$col}) { |
427 |
$diff_row++; |
$diff_row++; |
428 |
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); |
429 |
print sql_delete($table,$srow,@cols_pk),"\n"; |
print sql_delete($table,$srow,@cols_pk),"\n"; |
430 |
print sql_insert($table,$mrow,@cols),"\n"; |
print sql_insert($table,$mrow,@cols),"\n"; |
431 |
|
$have_mrow = FETCH_ROW; |
432 |
|
$have_srow = FETCH_ROW; |
433 |
} |
} |
434 |
} |
} |
435 |
} |
} |