--- pgdiff 2003/08/12 12:00:07 1.3 +++ pgdiff 2003/10/28 18:56:54 1.11 @@ -11,6 +11,7 @@ use Getopt::Long; use DBI; use Data::Dumper; +use Pg::Scheme; $| = 1; @@ -42,7 +43,7 @@ --slavehost=hostname --slaveport=port --slaveuser=username --slavepassword=string --slavefile=filename - --tables[s]=table[,table...] + --table[s]=table[,table...] "; # exit ((scalar(@ARGV) < 2)? 1:0); exit; @@ -62,8 +63,20 @@ print "Master connection is $minfo\n" if ($debug); print "Slave connection is $sinfo\n" if ($debug); -my $mdbh = DBI->connect("DBI:Pg:$minfo", $masteruser, $masterpassword, { PrintError => 1 }); -my $sdbh = DBI->connect("DBI:Pg:$sinfo", $slaveuser, $slavepassword, { PrintError => 1 }); +my $mdbh = DBI->connect("DBI:Pg:$minfo", $masteruser, $masterpassword, { PrintError => 0 } ); +if (! $mdbh) { + print "Can't connect to master database $master"; + print "on $masterhost" if ($masterhost); + print "\n"; + exit 1; +} +my $sdbh = DBI->connect("DBI:Pg:$sinfo", $slaveuser, $slavepassword, { PrintError => 0 }); +if (! $sdbh) { + print "Can't connect to slave database $slave"; + print "on $slavehost" if ($slavehost); + print "\n"; + exit 1; +} my ($diff_shema,$diff_data) = (0,0); @@ -105,162 +118,95 @@ $verbose = 1 if ($debug); +# init object for scheme in master database +my $mscheme = new Pg::Scheme( 'dbh' => $mdbh, 'DEBUG' => 0 ) || die "can't query master schema"; +my $sscheme = new Pg::Scheme( 'dbh' => $sdbh, 'DEBUG' => 0 ) || die "can't query slave schema"; + # which tables to compare? -my @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}; - } -} +my @tables = $mscheme->list_tables($tables); debug "Comparing tables: ".join(", ",@tables)."\n"; +# start transaction +print "begin work;\n"; + +# disable active triggers on slave database +my @triggers = $sscheme->get_activetriggers(); + +foreach my $tr (@triggers) { + print "update pg_trigger set tgenabled = false where tgname='$tr';\n"; +} + my $cols; my $diff_total = 0; foreach my $table (@tables) { - 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; +print "-- schema...\n"; + # diff schema + foreach my $row (@{$mscheme->pg_attribute($table)}) { +# print Dumper($row); } - my $oid = $row->{oid}; +print "-- constraints...\n"; + # diff constraints + foreach my $tr (@{$mscheme->triggers($table)}) { +# print Dumper($tr); + } +print "-- triggers...\n"; + # diff triggers + foreach my $tr (@{$mscheme->triggers($table)}) { +# print Dumper($tr); + } - # diff schema + # all colums (for insert) + my @cols = @{$mscheme->cols($table)}; - my @cols_notnull;# colums compared by a=b - my @cols_null; # colums compared by a=b or a is null and b is null - my @cols_skip; # skipped columns - my @cols_test; # all colums to test (without skipped) - my @cols; # all colums (for insert) + # colums compared by a=b + my @cols_notnull = @{$mscheme->cols_notnull($table)}; - 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 -"; + # colums compared by a=b or a is null and b is null + my @cols_null = @{$mscheme->cols_null($table)}; + + # primary key columns + my @cols_pk = @{$mscheme->cols_pk($table)}; + + # columns to compare (not in primary key) + my @cols_cmp = @{$mscheme->cols_notpk($table)}; - $sth = $mdbh->prepare($sql); - $sth->execute() || die; - while(my $row = $sth->fetchrow_hashref()) { - # attname | format_type | attnotnull | atthasdef | attnum + my @cols_skip; # skipped columns + my @cols_test; # all colums to test (without skipped) - push @cols,$row->{attname}; + foreach my $row (@{$mscheme->pg_attribute($table)}) { + # attname format_type attnotnull atthasdef attnum default references # FIXME: do something with attributes which shouldn't be compared # (date, time, datetime, timestamp) if ($row->{format_type} =~ /(date)|(time)/i) { push @cols_skip,$row->{attname}; - next; - } - - push @cols_test,$row->{attname}; - - if ($row->{attnotnull}) { - push @cols_notnull,$row->{attname}; } else { - push @cols_null,$row->{attname}; + push @cols_test,$row->{attname}; } + } - $sth->finish(); if ($debug) { - print STDERR "DEBUG: table $table [$oid] not null: (",join(", ",@cols_notnull),")"; - print STDERR " - null: (",join(", ",@cols_null),")" if (@cols_null); - print STDERR " - skip: (",join(", ",@cols_skip),")" if (@cols_skip); + print STDERR "DEBUG: table $table not null cols: (",join(", ",@cols_notnull),")"; + print STDERR " - null cols: (",join(", ",@cols_null),")" if (@cols_null); + print STDERR " - skip cols: (",join(", ",@cols_skip),")" if (@cols_skip); print STDERR "\n"; } # diff data - 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}}++; - } - - } if (! @cols_pk) { print STDERR "can't find PK rows for table '$table' using all\n"; @cols_pk = @cols; } - my @cols_cmp; # columns to compare - - foreach my $col (@cols_test) { - push @cols_cmp,$col if (! $in_pk{$col}); - } if ($verbose) { print "table '$table' using for key: (",join(", ",@cols_pk),") to compare cols: (",join(", ",@cols_cmp),")\n"; @@ -299,11 +245,11 @@ debug_sql($msql); - my $msth = $mdbh->prepare($msql) || die; - $msth->execute() || die; + my $msth = $mdbh->prepare($msql) || die $mdbh->errstr; + $msth->execute() || die $msth->errstr; - my $ssth = $sdbh->prepare($ssql) || die; - $ssth->execute() || die; + my $ssth = $sdbh->prepare($ssql) || die $sdbh->errstr; + $ssth->execute() || die $ssth->errstr; my $diff_row = 0; @@ -358,6 +304,7 @@ # insert into slave database sub sql_insert { + my $dbh = shift @_ || die "need dbh"; my $table = shift @_ || die "need table as argument"; my $row = shift @_ || die "need row data"; my @cols = @_; @@ -365,7 +312,7 @@ my $sql = "insert into $table (".join(",",@cols).") values ("; my $comma = ""; foreach my $col (@cols) { - $sql .= $comma.$mdbh->quote($row->{$col}); + $sql .= $comma.$dbh->quote($row->{$col}); $comma = ","; } $sql.=")"; @@ -375,15 +322,16 @@ # delete from slave database sub sql_delete { + my $dbh = shift @_ || die "need dbh"; my $table = shift @_ || die "need table as argument"; my $row = shift @_ || die "need row as argument"; my @cols_pk = @_; my $where = sql_where(@cols_pk); - my $sql = "delete from $table "; + my $sql = "delete from $table"; foreach my $col (@cols_pk) { - 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"; $where =~ s/\?/$val/; } $sql .= $where; @@ -393,12 +341,19 @@ # update row in slave database sub sql_update { + my $dbh = shift @_ || die "need dbh"; my $table = shift @_ || die "need table as argument"; my $col = shift @_ || die "need col to update"; - my $val = shift @_ || die "need new val"; - my @cols_pk = @_ || die "need pk idenitifier"; + my $row = shift @_ || die "need row"; + my @cols_pk = @_; - my $sql = "udate $table set $col=".$mdbh->quote($val); + my $sql = "update $table set $col=".$dbh->quote($row->{$col}); + my $where = sql_where(@cols_pk); + foreach my $col (@cols_pk) { + my $val = $dbh->quote($row->{$col}) || die "can't find value in row for col $col"; + $where =~ s/\?/$val/; + } + $sql .= $where; debug_sql($sql); return $sql; } @@ -418,7 +373,7 @@ $diff_row++; $pk_same = 0; print STDERR "EXTRA row in table '$table' pk: [".join(",",@cols_pk)."] value (".join(",",pk_val($srow,@cols_pk)).")\n" if ($verbose); - print sql_delete($table,$srow,@cols_pk),";\n"; + print sql_delete($sdbh,$table,$srow,@cols_pk),";\n"; $have_srow = FETCH_ROW; last; } elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) || @@ -426,7 +381,7 @@ $diff_row++; $pk_same = 0; print STDERR "MISSING row in table '$table' pk: [".join(",",@cols_pk)."] value (".join(",",pk_val($mrow,@cols_pk)).")\n" if ($verbose); - print sql_insert($table,$mrow,@cols),";\n"; + print sql_insert($mdbh,$table,$mrow,@cols),";\n"; $have_mrow = FETCH_ROW; last; } @@ -438,7 +393,7 @@ if ($mrow->{$col} ne $srow->{$col}) { $diff_row++; print STDERR "DIFF in table '$table' row ($col): [".join(",",@cols_pk)."] '$mrow->{$col}' != '$srow->{$col}'\n" if ($verbose); - print sql_update($table,$col,$mrow->{$col},@cols_pk),";\n"; + print sql_update($mdbh,$table,$col,$mrow,@cols_pk),";\n"; } } $have_mrow = FETCH_ROW; @@ -450,7 +405,22 @@ $diff_total += $diff_row; } -print STDERR "$diff_total differences in all tables\n" if ($verbose && $diff_total > 0); +if ($verbose) { + if ($diff_total == 0) { + print STDERR "databases are same"; + } elsif ($diff_total > 0) { + print STDERR "$diff_total differences in all tables\n"; + } else { + die "this shouldn't happend. please report a bug!"; + } +} + +# enable triggers again on slave +foreach my $tr (@triggers) { + print "update pg_trigger set tgenabled = true where tgname='$tr';\n"; +} +# end transaction +print "commit;\n"; $mdbh->disconnect(); $sdbh->disconnect();