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 |
|
|
119 |
$verbose = 1 if ($debug); |
$verbose = 1 if ($debug); |
120 |
|
|
121 |
# init object for scheme in master database |
# init object for scheme in master database |
122 |
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 master schema"; |
123 |
|
my $sscheme = new Pg::Scheme( 'dbh' => $sdbh, 'DEBUG' => 0 ) || die "can't query slave schema"; |
124 |
|
|
125 |
# which tables to compare? |
# which tables to compare? |
126 |
|
|
128 |
|
|
129 |
debug "Comparing tables: ".join(", ",@tables)."\n"; |
debug "Comparing tables: ".join(", ",@tables)."\n"; |
130 |
|
|
131 |
|
# start transaction |
132 |
|
print "begin work;\n"; |
133 |
|
|
134 |
|
# disable active triggers on slave database |
135 |
|
my @triggers = $sscheme->get_triggers(); |
136 |
|
|
137 |
|
foreach my $tr (@triggers) { |
138 |
|
print "update pg_trigger set tgenabled = false where tgname='$tr';\n"; |
139 |
|
} |
140 |
|
|
141 |
my $cols; |
my $cols; |
142 |
my $diff_total = 0; |
my $diff_total = 0; |
143 |
|
|
147 |
|
|
148 |
# diff schema |
# diff schema |
149 |
|
|
150 |
my @cols_notnull;# colums compared by a=b |
# all colums (for insert) |
151 |
my @cols_null; # colums compared by a=b or a is null and b is null |
my @cols = @{$mscheme->cols($table)}; |
152 |
|
|
153 |
|
# colums compared by a=b |
154 |
|
my @cols_notnull = @{$mscheme->cols_notnull($table)}; |
155 |
|
|
156 |
|
# colums compared by a=b or a is null and b is null |
157 |
|
my @cols_null = @{$mscheme->cols_null($table)}; |
158 |
|
|
159 |
|
# primary key columns |
160 |
|
my @cols_pk = @{$mscheme->cols_pk($table)}; |
161 |
|
|
162 |
|
# columns to compare (not in primary key) |
163 |
|
my @cols_cmp = @{$mscheme->cols_notpk($table)}; |
164 |
|
|
165 |
my @cols_skip; # skipped columns |
my @cols_skip; # skipped columns |
166 |
my @cols_test; # all colums to test (without skipped) |
my @cols_test; # all colums to test (without skipped) |
|
my @cols; # all colums (for insert) |
|
|
|
|
167 |
|
|
168 |
foreach my $row ($mscheme->explain_table($table)) { |
foreach my $row (@{$mscheme->pg_attribute($table)}) { |
169 |
# attname | format_type | attnotnull | atthasdef | attnum |
# attname | format_type | attnotnull | atthasdef | attnum |
170 |
|
|
|
push @cols,$row->{attname}; |
|
|
|
|
171 |
# FIXME: do something with attributes which shouldn't be compared |
# FIXME: do something with attributes which shouldn't be compared |
172 |
# (date, time, datetime, timestamp) |
# (date, time, datetime, timestamp) |
173 |
if ($row->{format_type} =~ /(date)|(time)/i) { |
if ($row->{format_type} =~ /(date)|(time)/i) { |
174 |
push @cols_skip,$row->{attname}; |
push @cols_skip,$row->{attname}; |
|
next; |
|
|
} |
|
|
|
|
|
push @cols_test,$row->{attname}; |
|
|
|
|
|
if ($row->{attnotnull}) { |
|
|
push @cols_notnull,$row->{attname}; |
|
175 |
} else { |
} else { |
176 |
push @cols_null,$row->{attname}; |
push @cols_test,$row->{attname}; |
177 |
} |
} |
178 |
|
|
179 |
} |
} |
180 |
|
|
181 |
if ($debug) { |
if ($debug) { |
187 |
|
|
188 |
# diff data |
# diff data |
189 |
|
|
|
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}}++; |
|
|
} |
|
|
|
|
|
} |
|
190 |
if (! @cols_pk) { |
if (! @cols_pk) { |
191 |
print STDERR "can't find PK rows for table '$table' using all\n"; |
print STDERR "can't find PK rows for table '$table' using all\n"; |
192 |
@cols_pk = @cols; |
@cols_pk = @cols; |
193 |
} |
} |
194 |
|
|
|
my @cols_cmp; # columns to compare |
|
|
|
|
|
foreach my $col (@cols_test) { |
|
|
push @cols_cmp,$col if (! $in_pk{$col}); |
|
|
} |
|
195 |
|
|
196 |
if ($verbose) { |
if ($verbose) { |
197 |
print "table '$table' using for key: (",join(", ",@cols_pk),") to compare cols: (",join(", ",@cols_cmp),")\n"; |
print "table '$table' using for key: (",join(", ",@cols_pk),") to compare cols: (",join(", ",@cols_cmp),")\n"; |
289 |
|
|
290 |
# insert into slave database |
# insert into slave database |
291 |
sub sql_insert { |
sub sql_insert { |
292 |
|
my $dbh = shift @_ || die "need dbh"; |
293 |
my $table = shift @_ || die "need table as argument"; |
my $table = shift @_ || die "need table as argument"; |
294 |
my $row = shift @_ || die "need row data"; |
my $row = shift @_ || die "need row data"; |
295 |
my @cols = @_; |
my @cols = @_; |
297 |
my $sql = "insert into $table (".join(",",@cols).") values ("; |
my $sql = "insert into $table (".join(",",@cols).") values ("; |
298 |
my $comma = ""; |
my $comma = ""; |
299 |
foreach my $col (@cols) { |
foreach my $col (@cols) { |
300 |
$sql .= $comma.$mdbh->quote($row->{$col}); |
$sql .= $comma.$dbh->quote($row->{$col}); |
301 |
$comma = ","; |
$comma = ","; |
302 |
} |
} |
303 |
$sql.=")"; |
$sql.=")"; |
307 |
|
|
308 |
# delete from slave database |
# delete from slave database |
309 |
sub sql_delete { |
sub sql_delete { |
310 |
|
my $dbh = shift @_ || die "need dbh"; |
311 |
my $table = shift @_ || die "need table as argument"; |
my $table = shift @_ || die "need table as argument"; |
312 |
my $row = shift @_ || die "need row as argument"; |
my $row = shift @_ || die "need row as argument"; |
313 |
my @cols_pk = @_; |
my @cols_pk = @_; |
314 |
|
|
315 |
my $where = sql_where(@cols_pk); |
my $where = sql_where(@cols_pk); |
316 |
|
|
317 |
my $sql = "delete from $table "; |
my $sql = "delete from $table"; |
318 |
foreach my $col (@cols_pk) { |
foreach my $col (@cols_pk) { |
319 |
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"; |
320 |
$where =~ s/\?/$val/; |
$where =~ s/\?/$val/; |
321 |
} |
} |
322 |
$sql .= $where; |
$sql .= $where; |
326 |
|
|
327 |
# update row in slave database |
# update row in slave database |
328 |
sub sql_update { |
sub sql_update { |
329 |
|
my $dbh = shift @_ || die "need dbh"; |
330 |
my $table = shift @_ || die "need table as argument"; |
my $table = shift @_ || die "need table as argument"; |
331 |
my $col = shift @_ || die "need col to update"; |
my $col = shift @_ || die "need col to update"; |
332 |
my $val = shift @_ || die "need new val"; |
my $row = shift @_ || die "need row"; |
333 |
my @cols_pk = @_ || die "need pk idenitifier"; |
my @cols_pk = @_; |
334 |
|
|
335 |
my $sql = "udate $table set $col=".$mdbh->quote($val); |
my $sql = "update $table set $col=".$dbh->quote($row->{$col}); |
336 |
|
my $where = sql_where(@cols_pk); |
337 |
|
foreach my $col (@cols_pk) { |
338 |
|
my $val = $dbh->quote($row->{$col}) || die "can't find value in row for col $col"; |
339 |
|
$where =~ s/\?/$val/; |
340 |
|
} |
341 |
|
$sql .= $where; |
342 |
debug_sql($sql); |
debug_sql($sql); |
343 |
return $sql; |
return $sql; |
344 |
} |
} |
358 |
$diff_row++; |
$diff_row++; |
359 |
$pk_same = 0; |
$pk_same = 0; |
360 |
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); |
361 |
print sql_delete($table,$srow,@cols_pk),";\n"; |
print sql_delete($sdbh,$table,$srow,@cols_pk),";\n"; |
362 |
$have_srow = FETCH_ROW; |
$have_srow = FETCH_ROW; |
363 |
last; |
last; |
364 |
} elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) || |
} elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) || |
366 |
$diff_row++; |
$diff_row++; |
367 |
$pk_same = 0; |
$pk_same = 0; |
368 |
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); |
369 |
print sql_insert($table,$mrow,@cols),";\n"; |
print sql_insert($mdbh,$table,$mrow,@cols),";\n"; |
370 |
$have_mrow = FETCH_ROW; |
$have_mrow = FETCH_ROW; |
371 |
last; |
last; |
372 |
} |
} |
378 |
if ($mrow->{$col} ne $srow->{$col}) { |
if ($mrow->{$col} ne $srow->{$col}) { |
379 |
$diff_row++; |
$diff_row++; |
380 |
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); |
381 |
print sql_update($table,$col,$mrow->{$col},@cols_pk),";\n"; |
print sql_update($mdbh,$table,$col,$mrow,@cols_pk),";\n"; |
382 |
} |
} |
383 |
} |
} |
384 |
$have_mrow = FETCH_ROW; |
$have_mrow = FETCH_ROW; |
400 |
} |
} |
401 |
} |
} |
402 |
|
|
403 |
|
# enable triggers again on slave |
404 |
|
foreach my $tr (@triggers) { |
405 |
|
print "update pg_trigger set tgenabled = true where tgname='$tr';\n"; |
406 |
|
} |
407 |
|
# end transaction |
408 |
|
print "commit;\n"; |
409 |
|
|
410 |
$mdbh->disconnect(); |
$mdbh->disconnect(); |
411 |
$sdbh->disconnect(); |
$sdbh->disconnect(); |