11 |
use Getopt::Long; |
use Getopt::Long; |
12 |
use DBI; |
use DBI; |
13 |
use Data::Dumper; |
use Data::Dumper; |
14 |
|
use Pg::Scheme; |
15 |
|
|
16 |
$| = 1; |
$| = 1; |
17 |
|
|
75 |
my $sql = shift; |
my $sql = shift; |
76 |
$sql =~ s/[\n\r]/ /gs; |
$sql =~ s/[\n\r]/ /gs; |
77 |
$sql =~ s/\s\s+/ /g; |
$sql =~ s/\s\s+/ /g; |
78 |
print STDERR "SQL: $sql\n"; |
print STDERR "DEBUG: SQL: $sql\n"; |
79 |
} |
} |
80 |
|
|
81 |
sub debug_row { |
sub debug_row { |
83 |
my $row = shift; |
my $row = shift; |
84 |
my @cols = @_; |
my @cols = @_; |
85 |
if (! $row) { |
if (! $row) { |
86 |
print STDERR "ROW data is undef!\n"; |
print STDERR "DEBUG: ROW data is undef!\n"; |
87 |
return; |
return; |
88 |
} |
} |
89 |
print STDERR "ROW: [",$#cols+1,"] "; |
print STDERR "DEBUG: ROW: [",$#cols+1,"] "; |
90 |
foreach my $col (@cols) { |
foreach my $col (@cols) { |
91 |
print STDERR "$col:"; |
print STDERR "$col:"; |
92 |
if ($row->{$col}) { |
if ($row->{$col}) { |
99 |
print STDERR "\n"; |
print STDERR "\n"; |
100 |
} |
} |
101 |
|
|
102 |
|
sub debug { |
103 |
|
return if (!$debug); |
104 |
|
print STDERR "DEBUG: ",@_; |
105 |
|
} |
106 |
|
|
107 |
$verbose = 1 if ($debug); |
$verbose = 1 if ($debug); |
108 |
|
|
109 |
|
# init object for scheme in master database |
110 |
|
my $mscheme = new Pg::Scheme( 'dbh' => $mdbh, 'DEBUG' => 0 ) || die "can't query schema"; |
111 |
|
|
112 |
# which tables to compare? |
# which tables to compare? |
113 |
|
|
114 |
my @tables; |
my @tables = $mscheme->list_tables($tables); |
|
if ($tables) { |
|
|
@tables = split(/,/,$tables); |
|
|
} else { |
|
|
# take all tables |
|
|
#$sql="select tablename from pg_tables where tablename not like 'pg_%' and tablename not like '_rserv_%'"; |
|
|
# show tables (based on psql \dt) |
|
|
$sql = " |
|
|
SELECT c.relname as table |
|
|
FROM pg_catalog.pg_class c |
|
|
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
|
|
WHERE c.relkind = 'r' |
|
|
AND n.nspname NOT IN ('pg_catalog', 'pg_toast') |
|
|
AND pg_catalog.pg_table_is_visible(c.oid) |
|
|
and c.relname not like '_rserv_%' |
|
|
"; |
|
|
my $sth = $mdbh->prepare($sql); |
|
|
$sth->execute() || die; |
|
|
while(my $row = $sth->fetchrow_hashref()) { |
|
|
push @tables,$row->{table}; |
|
|
} |
|
|
} |
|
115 |
|
|
116 |
print "Comparing tables: ",join(", ",@tables),"\n" if ($debug); |
debug "Comparing tables: ".join(", ",@tables)."\n"; |
117 |
|
|
118 |
my $cols; |
my $cols; |
119 |
my $diff_total = 0; |
my $diff_total = 0; |
120 |
|
|
121 |
foreach my $table (@tables) { |
foreach my $table (@tables) { |
122 |
|
|
123 |
my ($sth); |
my $sth; |
124 |
|
|
125 |
# find table oid |
# diff schema |
|
$sql = " |
|
|
SELECT c.oid, n.nspname, c.relname |
|
|
FROM pg_catalog.pg_class c |
|
|
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace |
|
|
WHERE pg_catalog.pg_table_is_visible(c.oid) |
|
|
AND c.relname = '$table' |
|
|
ORDER BY 2, 3 |
|
|
"; |
|
|
|
|
|
$sth = $mdbh->prepare($sql); |
|
|
$sth->execute() || die; |
|
|
my $row = $sth->fetchrow_hashref(); |
|
|
if (! $row) { |
|
|
print STDERR "Can't find OID of table '$table'\n"; |
|
|
exit 1; |
|
|
} |
|
126 |
|
|
127 |
my $oid = $row->{oid}; |
# all colums (for insert) |
128 |
|
my @cols = @{$mscheme->cols($table)}; |
129 |
|
|
130 |
# diff schema |
# colums compared by a=b |
131 |
|
my @cols_notnull = @{$mscheme->cols_notnull($table)}; |
132 |
|
|
133 |
|
# colums compared by a=b or a is null and b is null |
134 |
|
my @cols_null = @{$mscheme->cols_null($table)}; |
135 |
|
|
136 |
|
# primary key columns |
137 |
|
my @cols_pk = @{$mscheme->cols_pk($table)}; |
138 |
|
|
139 |
|
# columns to compare (not in primary key) |
140 |
|
my @cols_cmp = @{$mscheme->cols_notpk($table)}; |
141 |
|
|
|
my @cols_notnull;# colums compared by a=b |
|
|
my @cols_null; # colums compared by a=b or a is null and b is null |
|
142 |
my @cols_skip; # skipped columns |
my @cols_skip; # skipped columns |
143 |
my @cols_test; # all colums to test (without skipped) |
my @cols_test; # all colums to test (without skipped) |
|
my @cols; # all colums (for insert) |
|
144 |
|
|
145 |
my $sql=" |
foreach my $row (@{$mscheme->pg_attribute($table)}) { |
|
SELECT a.attname, |
|
|
pg_catalog.format_type(a.atttypid, a.atttypmod), |
|
|
a.attnotnull, a.atthasdef, a.attnum |
|
|
FROM pg_catalog.pg_attribute a |
|
|
WHERE a.attrelid = $oid AND a.attnum > 0 AND NOT a.attisdropped |
|
|
ORDER BY a.attnum |
|
|
"; |
|
|
|
|
|
$sth = $mdbh->prepare($sql); |
|
|
$sth->execute() || die; |
|
|
while(my $row = $sth->fetchrow_hashref()) { |
|
146 |
# attname | format_type | attnotnull | atthasdef | attnum |
# attname | format_type | attnotnull | atthasdef | attnum |
147 |
|
|
|
push @cols,$row->{attname}; |
|
|
|
|
148 |
# FIXME: do something with attributes which shouldn't be compared |
# FIXME: do something with attributes which shouldn't be compared |
149 |
# (date, time, datetime, timestamp) |
# (date, time, datetime, timestamp) |
150 |
if ($row->{format_type} =~ /(date)|(time)/i) { |
if ($row->{format_type} =~ /(date)|(time)/i) { |
151 |
push @cols_skip,$row->{attname}; |
push @cols_skip,$row->{attname}; |
|
next; |
|
|
} |
|
|
|
|
|
push @cols_test,$row->{attname}; |
|
|
|
|
|
if ($row->{attnotnull}) { |
|
|
push @cols_notnull,$row->{attname}; |
|
152 |
} else { |
} else { |
153 |
push @cols_null,$row->{attname}; |
push @cols_test,$row->{attname}; |
154 |
} |
} |
155 |
|
|
156 |
} |
} |
|
$sth->finish(); |
|
157 |
|
|
158 |
if ($debug) { |
if ($debug) { |
159 |
print STDERR "table $table [$oid] not null: (",join(", ",@cols_notnull),")"; |
print STDERR "DEBUG: table $table not null cols: (",join(", ",@cols_notnull),")"; |
160 |
print STDERR " - null: (",join(", ",@cols_null),")" if (@cols_null); |
print STDERR " - null cols: (",join(", ",@cols_null),")" if (@cols_null); |
161 |
print STDERR " - skip: (",join(", ",@cols_skip),")" if (@cols_skip); |
print STDERR " - skip cols: (",join(", ",@cols_skip),")" if (@cols_skip); |
162 |
print STDERR "\n"; |
print STDERR "\n"; |
163 |
} |
} |
164 |
|
|
165 |
# diff data |
# diff data |
166 |
|
|
|
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; |
|
|
$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}}++; |
|
|
} |
|
|
|
|
|
} |
|
167 |
if (! @cols_pk) { |
if (! @cols_pk) { |
168 |
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"; |
169 |
@cols_pk = @cols; |
@cols_pk = @cols; |
170 |
} |
} |
171 |
|
|
|
my @cols_cmp; # columns to compare |
|
|
|
|
|
foreach my $col (@cols_test) { |
|
|
push @cols_cmp,$col if (! $in_pk{$col}); |
|
|
} |
|
172 |
|
|
173 |
if ($verbose) { |
if ($verbose) { |
174 |
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"; |
203 |
|
|
204 |
my $order = sql_order(@cols_pk); |
my $order = sql_order(@cols_pk); |
205 |
$msql .= $order; |
$msql .= $order; |
206 |
$ssql .= sql_where(@cols_pk) . $order; |
$ssql .= $order; |
207 |
|
|
208 |
debug_sql($msql); |
debug_sql($msql); |
|
debug_sql($ssql); |
|
209 |
|
|
210 |
my $msth = $mdbh->prepare($msql) || die; |
my $msth = $mdbh->prepare($msql) || die; |
211 |
$msth->execute() || die; |
$msth->execute() || die; |
212 |
|
|
213 |
my $ssth = $sdbh->prepare($ssql) || die; |
my $ssth = $sdbh->prepare($ssql) || die; |
214 |
|
$ssth->execute() || die; |
215 |
|
|
216 |
my $diff_row = 0; |
my $diff_row = 0; |
217 |
|
|
218 |
my ($mrow,$srow); |
my ($mrow,$srow); |
219 |
my ($have_mrow,$have_srow) = (0,0); |
# have_* |
220 |
my @pk_val; |
use constant NO_ROW => 0; |
221 |
|
use constant FETCH_ROW => 1; |
222 |
|
use constant HAVE_ROW => 2; |
223 |
|
my ($have_mrow,$have_srow) = (FETCH_ROW,FETCH_ROW); |
224 |
|
|
225 |
|
while ($have_mrow != NO_ROW || $have_srow != NO_ROW) { |
226 |
|
|
227 |
|
debug "have mrow: $have_mrow srow: $have_srow\n"; |
228 |
|
|
229 |
|
sub pk_val { |
230 |
|
my $row = shift || die "need row"; |
231 |
|
my @cols = shift || die "need cols"; |
232 |
|
my @val; |
233 |
|
foreach my $col (@cols) { |
234 |
|
push @val,$row->{$col}; |
235 |
|
} |
236 |
|
return @val; |
237 |
|
} |
238 |
|
|
|
my $more_rows = 1; |
|
|
while (!$have_mrow || !$have_srow) { |
|
239 |
# fetch row from master |
# fetch row from master |
240 |
if (!$have_mrow) { |
if ($have_mrow == FETCH_ROW) { |
241 |
print "fetch row from master [$more_rows]: $msql\n" if ($debug); |
debug "fetch row from master: $msql\n"; |
242 |
$mrow = $msth->fetchrow_hashref(); |
$mrow = $msth->fetchrow_hashref(); |
243 |
debug_row($mrow,@cols); |
debug_row($mrow,@cols); |
244 |
|
|
245 |
if ($mrow) { |
if ($mrow) { |
246 |
# fill-in primary key values |
# fill-in primary key values |
247 |
@pk_val = (); |
$have_mrow = HAVE_ROW; |
|
foreach my $col (@cols_pk) { |
|
|
push @pk_val,$mrow->{$col}; |
|
|
} |
|
|
$have_mrow = 1; |
|
248 |
} else { |
} else { |
249 |
$have_mrow = 0; |
$have_mrow = NO_ROW; |
250 |
} |
} |
251 |
} |
} |
252 |
|
|
253 |
# fetch row from slave |
# fetch row from slave |
254 |
if (!$have_srow) { |
if ($have_srow == FETCH_ROW) { |
255 |
print "fetch row from slave [$more_rows]: $ssql\n" if ($debug); |
debug "fetch row from slave: $ssql\n"; |
|
$ssth->execute(@pk_val) || die; |
|
256 |
$srow = $ssth->fetchrow_hashref(); |
$srow = $ssth->fetchrow_hashref(); |
257 |
debug_row($srow,@cols); |
debug_row($srow,@cols); |
258 |
if ($srow) { |
if ($srow) { |
259 |
$have_srow = 1; |
$have_srow = HAVE_ROW; |
260 |
} else { |
} else { |
261 |
$have_srow = 0; |
$have_srow = NO_ROW; |
262 |
} |
} |
263 |
} |
} |
264 |
|
|
265 |
# end of this table? |
debug "have mrow: $have_mrow srow: $have_srow\n"; |
|
if (!$have_mrow && !$have_srow) { |
|
|
last; |
|
|
} |
|
266 |
|
|
267 |
# insert into slave database |
# insert into slave database |
268 |
sub sql_insert { |
sub sql_insert { |
299 |
return $sql; |
return $sql; |
300 |
} |
} |
301 |
|
|
302 |
|
# update row in slave database |
303 |
|
sub sql_update { |
304 |
|
my $table = shift @_ || die "need table as argument"; |
305 |
|
my $col = shift @_ || die "need col to update"; |
306 |
|
my $val = shift @_ || die "need new val"; |
307 |
|
my @cols_pk = @_ || die "need pk idenitifier"; |
308 |
|
|
309 |
|
my $sql = "udate $table set $col=".$mdbh->quote($val); |
310 |
|
debug_sql($sql); |
311 |
|
return $sql; |
312 |
|
} |
313 |
# master slave |
# master slave |
314 |
# 1 = 1 test |
# 1 = 1 test |
315 |
# 1 < 2 insert mrow |
# 1 < 2 insert mrow |
321 |
|
|
322 |
# check key cols for row |
# check key cols for row |
323 |
foreach my $col (@cols_pk) { |
foreach my $col (@cols_pk) { |
324 |
if ( (!$mrow && $srow) || ($mrow && $srow && ($mrow->{$col} gt $srow->{$col})) ) { |
if ( ($have_mrow == NO_ROW && $have_srow == HAVE_ROW) || |
325 |
|
($have_mrow == HAVE_ROW && $have_srow == HAVE_ROW && $mrow->{$col} gt $srow->{$col}) ) { |
326 |
$diff_row++; |
$diff_row++; |
327 |
$pk_same = 0; |
$pk_same = 0; |
328 |
print sql_delete($table,$srow,@cols_pk),"\n"; |
print STDERR "EXTRA row in table '$table' pk: [".join(",",@cols_pk)."] value (".join(",",pk_val($srow,@cols_pk)).")\n" if ($verbose); |
329 |
$have_srow = 0; # fetch new slave row |
print sql_delete($table,$srow,@cols_pk),";\n"; |
330 |
|
$have_srow = FETCH_ROW; |
331 |
last; |
last; |
332 |
} elsif ( ($mrow && !$srow) || ($mrow && $srow && ($mrow->{$col} lt $srow->{$col})) ) { |
} elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) || |
333 |
|
($have_mrow == HAVE_ROW && $have_srow == HAVE_ROW && $mrow->{$col} lt $srow->{$col}) ) { |
334 |
$diff_row++; |
$diff_row++; |
335 |
$pk_same = 0; |
$pk_same = 0; |
336 |
print sql_insert($table,$mrow,@cols),"\n"; |
print STDERR "MISSING row in table '$table' pk: [".join(",",@cols_pk)."] value (".join(",",pk_val($mrow,@cols_pk)).")\n" if ($verbose); |
337 |
$have_mrow = 0; |
print sql_insert($table,$mrow,@cols),";\n"; |
338 |
|
$have_mrow = FETCH_ROW; |
339 |
last; |
last; |
340 |
} |
} |
341 |
} |
} |
342 |
|
|
343 |
if ($pk_same) { |
if ($pk_same && $have_mrow == HAVE_ROW && $have_srow == HAVE_ROW) { |
344 |
# check non-key cols for row |
# check non-key cols for row |
345 |
foreach my $col (@cols_cmp) { |
foreach my $col (@cols_cmp) { |
346 |
if ($mrow->{$col} ne $srow->{$col}) { |
if ($mrow->{$col} ne $srow->{$col}) { |
347 |
$diff_row++; |
$diff_row++; |
348 |
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); |
349 |
print sql_delete($table,$srow,@cols_pk),"\n"; |
print sql_update($table,$col,$mrow->{$col},@cols_pk),";\n"; |
|
print sql_insert($table,$mrow,@cols),"\n"; |
|
350 |
} |
} |
351 |
} |
} |
352 |
|
$have_mrow = FETCH_ROW; |
353 |
|
$have_srow = FETCH_ROW; |
354 |
} |
} |
355 |
} |
} |
356 |
|
|
358 |
$diff_total += $diff_row; |
$diff_total += $diff_row; |
359 |
} |
} |
360 |
|
|
361 |
print STDERR "$diff_total differences in all tables\n" if ($verbose && $diff_total > 0); |
if ($verbose) { |
362 |
|
if ($diff_total == 0) { |
363 |
|
print STDERR "databases are same"; |
364 |
|
} elsif ($diff_total > 0) { |
365 |
|
print STDERR "$diff_total differences in all tables\n"; |
366 |
|
} else { |
367 |
|
die "this shouldn't happend. please report a bug!"; |
368 |
|
} |
369 |
|
} |
370 |
|
|
371 |
$mdbh->disconnect(); |
$mdbh->disconnect(); |
372 |
$sdbh->disconnect(); |
$sdbh->disconnect(); |