63 |
print "Master connection is $minfo\n" if ($debug); |
print "Master connection is $minfo\n" if ($debug); |
64 |
print "Slave connection is $sinfo\n" if ($debug); |
print "Slave connection is $sinfo\n" if ($debug); |
65 |
|
|
66 |
my $mdbh = DBI->connect("DBI:Pg:$minfo", $masteruser, $masterpassword, { PrintError => 1 }); |
my $mdbh = DBI->connect("DBI:Pg:$minfo", $masteruser, $masterpassword, { PrintError => 0 } ); |
67 |
my $sdbh = DBI->connect("DBI:Pg:$sinfo", $slaveuser, $slavepassword, { PrintError => 1 }); |
if (! $mdbh) { |
68 |
|
print "Can't connect to master database $master"; |
69 |
|
print "on $masterhost" if ($masterhost); |
70 |
|
print "\n"; |
71 |
|
exit 1; |
72 |
|
} |
73 |
|
my $sdbh = DBI->connect("DBI:Pg:$sinfo", $slaveuser, $slavepassword, { PrintError => 0 }); |
74 |
|
if (! $sdbh) { |
75 |
|
print "Can't connect to slave database $slave"; |
76 |
|
print "on $slavehost" if ($slavehost); |
77 |
|
print "\n"; |
78 |
|
exit 1; |
79 |
|
} |
80 |
|
|
81 |
my ($diff_shema,$diff_data) = (0,0); |
my ($diff_shema,$diff_data) = (0,0); |
82 |
|
|
278 |
|
|
279 |
# insert into slave database |
# insert into slave database |
280 |
sub sql_insert { |
sub sql_insert { |
281 |
|
my $dbh = shift @_ || die "need dbh"; |
282 |
my $table = shift @_ || die "need table as argument"; |
my $table = shift @_ || die "need table as argument"; |
283 |
my $row = shift @_ || die "need row data"; |
my $row = shift @_ || die "need row data"; |
284 |
my @cols = @_; |
my @cols = @_; |
286 |
my $sql = "insert into $table (".join(",",@cols).") values ("; |
my $sql = "insert into $table (".join(",",@cols).") values ("; |
287 |
my $comma = ""; |
my $comma = ""; |
288 |
foreach my $col (@cols) { |
foreach my $col (@cols) { |
289 |
$sql .= $comma.$mdbh->quote($row->{$col}); |
$sql .= $comma.$dbh->quote($row->{$col}); |
290 |
$comma = ","; |
$comma = ","; |
291 |
} |
} |
292 |
$sql.=")"; |
$sql.=")"; |
296 |
|
|
297 |
# delete from slave database |
# delete from slave database |
298 |
sub sql_delete { |
sub sql_delete { |
299 |
|
my $dbh = shift @_ || die "need dbh"; |
300 |
my $table = shift @_ || die "need table as argument"; |
my $table = shift @_ || die "need table as argument"; |
301 |
my $row = shift @_ || die "need row as argument"; |
my $row = shift @_ || die "need row as argument"; |
302 |
my @cols_pk = @_; |
my @cols_pk = @_; |
303 |
|
|
304 |
my $where = sql_where(@cols_pk); |
my $where = sql_where(@cols_pk); |
305 |
|
|
306 |
my $sql = "delete from $table "; |
my $sql = "delete from $table"; |
307 |
foreach my $col (@cols_pk) { |
foreach my $col (@cols_pk) { |
308 |
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"; |
309 |
$where =~ s/\?/$val/; |
$where =~ s/\?/$val/; |
310 |
} |
} |
311 |
$sql .= $where; |
$sql .= $where; |
315 |
|
|
316 |
# update row in slave database |
# update row in slave database |
317 |
sub sql_update { |
sub sql_update { |
318 |
|
my $dbh = shift @_ || die "need dbh"; |
319 |
my $table = shift @_ || die "need table as argument"; |
my $table = shift @_ || die "need table as argument"; |
320 |
my $col = shift @_ || die "need col to update"; |
my $col = shift @_ || die "need col to update"; |
321 |
my $val = shift @_ || die "need new val"; |
my $row = shift @_ || die "need row"; |
322 |
my @cols_pk = @_ || die "need pk idenitifier"; |
my @cols_pk = @_; |
323 |
|
|
324 |
my $sql = "udate $table set $col=".$mdbh->quote($val); |
my $sql = "update $table set $col=".$dbh->quote($row->{$col}); |
325 |
|
my $where = sql_where(@cols_pk); |
326 |
|
foreach my $col (@cols_pk) { |
327 |
|
my $val = $dbh->quote($row->{$col}) || die "can't find value in row for col $col"; |
328 |
|
$where =~ s/\?/$val/; |
329 |
|
} |
330 |
|
$sql .= $where; |
331 |
debug_sql($sql); |
debug_sql($sql); |
332 |
return $sql; |
return $sql; |
333 |
} |
} |
347 |
$diff_row++; |
$diff_row++; |
348 |
$pk_same = 0; |
$pk_same = 0; |
349 |
print STDERR "EXTRA row in table '$table' pk: [".join(",",@cols_pk)."] value (".join(",",pk_val($srow,@cols_pk)).")\n" if ($verbose); |
print STDERR "EXTRA row in table '$table' pk: [".join(",",@cols_pk)."] value (".join(",",pk_val($srow,@cols_pk)).")\n" if ($verbose); |
350 |
print sql_delete($table,$srow,@cols_pk),";\n"; |
print sql_delete($sdbh,$table,$srow,@cols_pk),";\n"; |
351 |
$have_srow = FETCH_ROW; |
$have_srow = FETCH_ROW; |
352 |
last; |
last; |
353 |
} elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) || |
} elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) || |
355 |
$diff_row++; |
$diff_row++; |
356 |
$pk_same = 0; |
$pk_same = 0; |
357 |
print STDERR "MISSING row in table '$table' pk: [".join(",",@cols_pk)."] value (".join(",",pk_val($mrow,@cols_pk)).")\n" if ($verbose); |
print STDERR "MISSING row in table '$table' pk: [".join(",",@cols_pk)."] value (".join(",",pk_val($mrow,@cols_pk)).")\n" if ($verbose); |
358 |
print sql_insert($table,$mrow,@cols),";\n"; |
print sql_insert($mdbh,$table,$mrow,@cols),";\n"; |
359 |
$have_mrow = FETCH_ROW; |
$have_mrow = FETCH_ROW; |
360 |
last; |
last; |
361 |
} |
} |
367 |
if ($mrow->{$col} ne $srow->{$col}) { |
if ($mrow->{$col} ne $srow->{$col}) { |
368 |
$diff_row++; |
$diff_row++; |
369 |
print STDERR "DIFF in table '$table' row ($col): [".join(",",@cols_pk)."] '$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); |
370 |
print sql_update($table,$col,$mrow->{$col},@cols_pk),";\n"; |
print sql_update($mdbh,$table,$col,$mrow,@cols_pk),";\n"; |
371 |
} |
} |
372 |
} |
} |
373 |
$have_mrow = FETCH_ROW; |
$have_mrow = FETCH_ROW; |