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 |
|
|
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 ) || 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_%' |
|
|
"; |
|
|
foreach my $table (@tables) { |
|
|
$sql .= " and c.relname like '$table'"; |
|
|
} |
|
|
my $sth = $mdbh->prepare($sql); |
|
|
$sth->execute() || die; |
|
|
while(my $row = $sth->fetchrow_hashref()) { |
|
|
push @tables,$row->{table}; |
|
|
} |
|
|
} |
|
115 |
|
|
116 |
debug "Comparing tables: ".join(", ",@tables)."\n"; |
debug "Comparing tables: ".join(", ",@tables)."\n"; |
117 |
|
|
120 |
|
|
121 |
foreach my $table (@tables) { |
foreach my $table (@tables) { |
122 |
|
|
123 |
my ($sth); |
my $sth; |
|
|
|
|
# find table oid |
|
|
$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; |
|
|
} |
|
|
|
|
|
my $oid = $row->{oid}; |
|
124 |
|
|
125 |
# diff schema |
# diff schema |
126 |
|
|
130 |
my @cols_test; # all colums to test (without skipped) |
my @cols_test; # all colums to test (without skipped) |
131 |
my @cols; # all colums (for insert) |
my @cols; # all colums (for insert) |
132 |
|
|
|
my $sql=" |
|
|
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 |
|
|
"; |
|
133 |
|
|
134 |
$sth = $mdbh->prepare($sql); |
foreach my $row ($mscheme->explain_table($table)) { |
|
$sth->execute() || die; |
|
|
while(my $row = $sth->fetchrow_hashref()) { |
|
135 |
# attname | format_type | attnotnull | atthasdef | attnum |
# attname | format_type | attnotnull | atthasdef | attnum |
136 |
|
|
137 |
push @cols,$row->{attname}; |
push @cols,$row->{attname}; |
151 |
push @cols_null,$row->{attname}; |
push @cols_null,$row->{attname}; |
152 |
} |
} |
153 |
} |
} |
|
$sth->finish(); |
|
154 |
|
|
155 |
if ($debug) { |
if ($debug) { |
156 |
print STDERR "DEBUG: table $table [$oid] not null: (",join(", ",@cols_notnull),")"; |
print STDERR "DEBUG: table $table not null cols: (",join(", ",@cols_notnull),")"; |
157 |
print STDERR " - null: (",join(", ",@cols_null),")" if (@cols_null); |
print STDERR " - null cols: (",join(", ",@cols_null),")" if (@cols_null); |
158 |
print STDERR " - skip: (",join(", ",@cols_skip),")" if (@cols_skip); |
print STDERR " - skip cols: (",join(", ",@cols_skip),")" if (@cols_skip); |
159 |
print STDERR "\n"; |
print STDERR "\n"; |
160 |
} |
} |
161 |
|
|
185 |
debug_sql($sql); |
debug_sql($sql); |
186 |
$sth = $mdbh->prepare($sql); |
$sth = $mdbh->prepare($sql); |
187 |
$sth->execute() || die; |
$sth->execute() || die; |
188 |
$row = $sth->fetchrow_hashref(); |
my $row = $sth->fetchrow_hashref(); |
189 |
if ($row) { |
if ($row) { |
190 |
$sql=" |
$sql=" |
191 |
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"; |
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"; |
399 |
$diff_total += $diff_row; |
$diff_total += $diff_row; |
400 |
} |
} |
401 |
|
|
402 |
print STDERR "$diff_total differences in all tables\n" if ($verbose && $diff_total > 0); |
if ($verbose) { |
403 |
|
if ($diff_total == 0) { |
404 |
|
print STDERR "databases are same"; |
405 |
|
} elsif ($diff_total > 0) { |
406 |
|
print STDERR "$diff_total differences in all tables\n"; |
407 |
|
} else { |
408 |
|
die "this shouldn't happend. please report a bug!"; |
409 |
|
} |
410 |
|
} |
411 |
|
|
412 |
$mdbh->disconnect(); |
$mdbh->disconnect(); |
413 |
$sdbh->disconnect(); |
$sdbh->disconnect(); |