/[pgdiff]/pgdiff
This is repository of my old source code which isn't updated any more. Go to git.rot13.org for current projects!
ViewVC logotype

Annotation of /pgdiff

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (hide annotations)
Tue Aug 12 11:43:36 2003 UTC (19 years, 5 months ago) by dpavlin
Branch: MAIN
Changes since 1.1: +53 -33 lines
lot of fixes on the way to working version

1 dpavlin 1.1 #!/usr/bin/perl -w
2    
3     # Compare data in two different databases
4     #
5     # 2003-08-07 Dobrica Pavlinusic
6     #
7     # somewhat based on ides from
8     # DataDiff 0.1 by Jon D. Frisby, http://www.mrjoy.com
9    
10     use strict;
11     use Getopt::Long;
12     use DBI;
13     use Data::Dumper;
14    
15     $| = 1;
16    
17     my ($debug,$verbose) = (0,0);
18     my ($help,$masterhost,$masterport,$masteruser,$masterpassword,
19     $slavehost,$slaveport,$slaveuser,$slavepassword,
20     $masterfile,
21     $slavefile,
22     );
23     my $tables;
24    
25     my $result = GetOptions(
26     "debug!" => \$debug, "verbose!" => \$verbose, "help" => \$help,
27     "masterhost=s" => \$masterhost, "masterport=i" => \$masterport,
28     "masteruser=s" => \$masteruser, "masterpassword=s" => \$masterpassword,
29     "masterfile=s" => \$masterfile,
30     "slavehost=s" => \$slavehost, "slaveport=i" => \$slaveport,
31     "slaveuser=s" => \$slaveuser, "slavepassword=s" => \$slavepassword,
32     "slavefile=s" => \$slavefile,
33     "tables=s" => \$tables,
34     );
35    
36     if (defined($help) || (scalar(@ARGV) < 2)) {
37     print "Usage: $0 [options] masterdb slavedb
38     Options:
39     --masterhost=hostname --masterport=port
40     --masteruser=username --masterpassword=string
41     --masterfile=filename
42     --slavehost=hostname --slaveport=port
43     --slaveuser=username --slavepassword=string
44     --slavefile=filename
45     --tables[s]=table[,table...]
46     ";
47     # exit ((scalar(@ARGV) < 2)? 1:0);
48     exit;
49     }
50    
51     my $master = $ARGV[0] || "master";
52     my $slave = $ARGV[1] || "slave";
53    
54     my $minfo = "dbname=$master";
55     $minfo = "$minfo host=$masterhost" if (defined($masterhost));
56     $minfo = "$minfo port=$masterport" if (defined($masterport));
57    
58     my $sinfo = "dbname=$slave";
59     $sinfo = "$sinfo host=$slavehost" if (defined($slavehost));
60     $sinfo = "$sinfo port=$slaveport" if (defined($slaveport));
61    
62     print "Master connection is $minfo\n" if ($debug);
63     print "Slave connection is $sinfo\n" if ($debug);
64    
65     my $mdbh = DBI->connect("DBI:Pg:$minfo", $masteruser, $masterpassword, { PrintError => 1 });
66     my $sdbh = DBI->connect("DBI:Pg:$sinfo", $slaveuser, $slavepassword, { PrintError => 1 });
67    
68     my ($diff_shema,$diff_data) = (0,0);
69    
70     my $sql;
71    
72     sub debug_sql {
73     return if (! $debug);
74     my $sql = shift;
75     $sql =~ s/[\n\r]/ /gs;
76     $sql =~ s/\s\s+/ /g;
77 dpavlin 1.2 print STDERR "DEBUG: SQL: $sql\n";
78 dpavlin 1.1 }
79    
80     sub debug_row {
81     return if (! $debug);
82     my $row = shift;
83     my @cols = @_;
84     if (! $row) {
85 dpavlin 1.2 print STDERR "DEBUG: ROW data is undef!\n";
86 dpavlin 1.1 return;
87     }
88 dpavlin 1.2 print STDERR "DEBUG: ROW: [",$#cols+1,"] ";
89 dpavlin 1.1 foreach my $col (@cols) {
90     print STDERR "$col:";
91     if ($row->{$col}) {
92     print $row->{$col};
93     } else {
94     print "null";
95     }
96     print " ";
97     }
98     print STDERR "\n";
99     }
100    
101 dpavlin 1.2 sub debug {
102     return if (!$debug);
103     print STDERR "DEBUG: ",@_;
104     }
105    
106 dpavlin 1.1 $verbose = 1 if ($debug);
107    
108     # which tables to compare?
109    
110     my @tables;
111     if ($tables) {
112     @tables = split(/,/,$tables);
113     } else {
114     # take all tables
115     #$sql="select tablename from pg_tables where tablename not like 'pg_%' and tablename not like '_rserv_%'";
116     # show tables (based on psql \dt)
117     $sql = "
118     SELECT c.relname as table
119     FROM pg_catalog.pg_class c
120     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
121     WHERE c.relkind = 'r'
122     AND n.nspname NOT IN ('pg_catalog', 'pg_toast')
123     AND pg_catalog.pg_table_is_visible(c.oid)
124     and c.relname not like '_rserv_%'
125     ";
126 dpavlin 1.2 foreach my $table (@tables) {
127     $sql .= " and c.relname like '$table'";
128     }
129 dpavlin 1.1 my $sth = $mdbh->prepare($sql);
130     $sth->execute() || die;
131     while(my $row = $sth->fetchrow_hashref()) {
132     push @tables,$row->{table};
133     }
134     }
135    
136 dpavlin 1.2 debug "Comparing tables: ".join(", ",@tables)."\n";
137 dpavlin 1.1
138     my $cols;
139     my $diff_total = 0;
140    
141     foreach my $table (@tables) {
142    
143     my ($sth);
144    
145     # find table oid
146     $sql = "
147     SELECT c.oid, n.nspname, c.relname
148     FROM pg_catalog.pg_class c
149     LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
150     WHERE pg_catalog.pg_table_is_visible(c.oid)
151     AND c.relname = '$table'
152     ORDER BY 2, 3
153     ";
154    
155     $sth = $mdbh->prepare($sql);
156     $sth->execute() || die;
157     my $row = $sth->fetchrow_hashref();
158     if (! $row) {
159     print STDERR "Can't find OID of table '$table'\n";
160     exit 1;
161     }
162    
163     my $oid = $row->{oid};
164    
165     # diff schema
166    
167     my @cols_notnull;# colums compared by a=b
168     my @cols_null; # colums compared by a=b or a is null and b is null
169     my @cols_skip; # skipped columns
170     my @cols_test; # all colums to test (without skipped)
171     my @cols; # all colums (for insert)
172    
173     my $sql="
174     SELECT a.attname,
175     pg_catalog.format_type(a.atttypid, a.atttypmod),
176     a.attnotnull, a.atthasdef, a.attnum
177     FROM pg_catalog.pg_attribute a
178     WHERE a.attrelid = $oid AND a.attnum > 0 AND NOT a.attisdropped
179     ORDER BY a.attnum
180     ";
181    
182     $sth = $mdbh->prepare($sql);
183     $sth->execute() || die;
184     while(my $row = $sth->fetchrow_hashref()) {
185     # attname | format_type | attnotnull | atthasdef | attnum
186    
187     push @cols,$row->{attname};
188    
189     # FIXME: do something with attributes which shouldn't be compared
190     # (date, time, datetime, timestamp)
191     if ($row->{format_type} =~ /(date)|(time)/i) {
192     push @cols_skip,$row->{attname};
193     next;
194     }
195    
196     push @cols_test,$row->{attname};
197    
198     if ($row->{attnotnull}) {
199     push @cols_notnull,$row->{attname};
200     } else {
201     push @cols_null,$row->{attname};
202     }
203     }
204     $sth->finish();
205    
206     if ($debug) {
207 dpavlin 1.2 print STDERR "DEBUG: table $table [$oid] not null: (",join(", ",@cols_notnull),")";
208 dpavlin 1.1 print STDERR " - null: (",join(", ",@cols_null),")" if (@cols_null);
209     print STDERR " - skip: (",join(", ",@cols_skip),")" if (@cols_skip);
210     print STDERR "\n";
211     }
212    
213     # diff data
214    
215     my @cols_pk; # columns which are primary key
216     my %in_pk;
217    
218     $sql="
219     SELECT
220     i.indexrelid as indexrelid, i.indrelid as indrelid,
221     count(a.attname) as cols_in_pk
222     FROM
223     pg_catalog.pg_class c,
224     pg_catalog.pg_index i,
225     pg_catalog.pg_attribute a
226     WHERE
227     c.oid = i.indrelid
228     and i.indisunique
229     and c.relname = '$table'
230     and a.attrelid = i.indexrelid
231     GROUP BY
232     i.indexrelid, i.indrelid, c.relname, i.indisprimary, i.indisunique
233     ORDER BY
234     cols_in_pk ASC, i.indisprimary DESC, i.indisunique DESC, c.relname DESC
235     ";
236     debug_sql($sql);
237     $sth = $mdbh->prepare($sql);
238     $sth->execute() || die;
239     $row = $sth->fetchrow_hashref();
240     if ($row) {
241     $sql="
242     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";
243    
244     debug_sql($sql);
245     my $sth2 = $mdbh->prepare($sql);
246     $sth2->execute() || die;
247     @cols_pk = ();
248     while (my $row2 = $sth2->fetchrow_hashref()) {
249     push @cols_pk,$row2->{attname};
250     $in_pk{$row2->{attname}}++;
251     }
252    
253     }
254     if (! @cols_pk) {
255     print STDERR "can't find PK rows for table '$table' using all\n";
256     @cols_pk = @cols;
257     }
258    
259     my @cols_cmp; # columns to compare
260    
261     foreach my $col (@cols_test) {
262     push @cols_cmp,$col if (! $in_pk{$col});
263     }
264    
265     if ($verbose) {
266     print "table '$table' using for key: (",join(", ",@cols_pk),") to compare cols: (",join(", ",@cols_cmp),")\n";
267     }
268    
269     # diff data
270    
271     my $msql = "select ".join(",",@cols)." from $table";
272     my $ssql = $msql;
273    
274     sub sql_where {
275     my @cols = @_;
276     my $and = "";
277     my $where = " where ";
278     foreach my $col (@cols) {
279     $where .= "$and$col=?";
280     $and = " and ";
281     }
282     return $where;
283     }
284    
285     sub sql_order {
286     my @cols = @_;
287     my $order = " order by ";
288     my $comma = "";
289     foreach my $col (@cols) {
290     $order .= "$comma$col asc";
291     $comma = ", ";
292     }
293     return $order;
294     }
295    
296     my $order = sql_order(@cols_pk);
297     $msql .= $order;
298 dpavlin 1.2 $ssql .= $order;
299 dpavlin 1.1
300     debug_sql($msql);
301     debug_sql($ssql);
302    
303     my $msth = $mdbh->prepare($msql) || die;
304     $msth->execute() || die;
305    
306     my $ssth = $sdbh->prepare($ssql) || die;
307 dpavlin 1.2 $ssth->execute() || die;
308 dpavlin 1.1
309     my $diff_row = 0;
310    
311     my ($mrow,$srow);
312 dpavlin 1.2 # have_*
313     use constant NO_ROW => 0;
314     use constant FETCH_ROW => 1;
315     use constant HAVE_ROW => 2;
316     my ($have_mrow,$have_srow) = (FETCH_ROW,FETCH_ROW);
317    
318     while ($have_mrow != NO_ROW || $have_srow != NO_ROW) {
319    
320     debug "have mrow: $have_mrow srow: $have_srow\n";
321    
322     sub pk_val {
323     my $row = shift || die "need row";
324     my @cols = shift || die "need cols";
325     my @val;
326     foreach my $col (@cols) {
327     push @val,$row->{$col};
328     }
329     return @val;
330     }
331 dpavlin 1.1
332     # fetch row from master
333 dpavlin 1.2 if ($have_mrow == FETCH_ROW) {
334     debug "fetch row from master: $msql\n";
335 dpavlin 1.1 $mrow = $msth->fetchrow_hashref();
336     debug_row($mrow,@cols);
337    
338     if ($mrow) {
339     # fill-in primary key values
340 dpavlin 1.2 $have_mrow = HAVE_ROW;
341 dpavlin 1.1 } else {
342 dpavlin 1.2 $have_mrow = NO_ROW;
343 dpavlin 1.1 }
344     }
345    
346     # fetch row from slave
347 dpavlin 1.2 if ($have_srow == FETCH_ROW) {
348     debug "fetch row from slave: $ssql\n";
349 dpavlin 1.1 $srow = $ssth->fetchrow_hashref();
350     debug_row($srow,@cols);
351     if ($srow) {
352 dpavlin 1.2 $have_srow = HAVE_ROW;
353 dpavlin 1.1 } else {
354 dpavlin 1.2 $have_srow = NO_ROW;
355 dpavlin 1.1 }
356     }
357    
358 dpavlin 1.2 debug "have mrow: $have_mrow srow: $have_srow\n";
359 dpavlin 1.1
360     # insert into slave database
361     sub sql_insert {
362     my $table = shift @_ || die "need table as argument";
363     my $row = shift @_ || die "need row data";
364     my @cols = @_;
365    
366     my $sql = "insert into $table (".join(",",@cols).") values (";
367     my $comma = "";
368     foreach my $col (@cols) {
369     $sql .= $comma.$mdbh->quote($row->{$col});
370     $comma = ",";
371     }
372     $sql.=")";
373     debug_sql($sql);
374     return $sql;
375     }
376    
377     # delete from slave database
378     sub sql_delete {
379     my $table = shift @_ || die "need table as argument";
380     my $row = shift @_ || die "need row as argument";
381     my @cols_pk = @_;
382    
383     my $where = sql_where(@cols_pk);
384    
385     my $sql = "delete from $table ";
386     foreach my $col (@cols_pk) {
387     my $val = $sdbh->quote($row->{$col}) || die "can't find value in row for col $col";
388     $where =~ s/\?/$val/;
389     }
390     $sql .= $where;
391     debug_sql($sql);
392     return $sql;
393     }
394    
395     # master slave
396     # 1 = 1 test
397     # 1 < 2 insert mrow
398     # 2 > 1 delete srow
399     # 1 = undef insert mrow
400     # undef = 1 delete srow
401    
402     my $pk_same = 1;
403    
404     # check key cols for row
405     foreach my $col (@cols_pk) {
406 dpavlin 1.2 if ( ($have_mrow == NO_ROW && $have_srow == HAVE_ROW) ||
407     ($have_mrow == HAVE_ROW && $have_srow == HAVE_ROW && $mrow->{$col} gt $srow->{$col}) ) {
408 dpavlin 1.1 $diff_row++;
409     $pk_same = 0;
410     print sql_delete($table,$srow,@cols_pk),"\n";
411 dpavlin 1.2 $have_srow = FETCH_ROW;
412 dpavlin 1.1 last;
413 dpavlin 1.2 } elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) ||
414     ($have_mrow == HAVE_ROW && $have_srow == HAVE_ROW && $mrow->{$col} lt $srow->{$col}) ) {
415 dpavlin 1.1 $diff_row++;
416     $pk_same = 0;
417     print sql_insert($table,$mrow,@cols),"\n";
418 dpavlin 1.2 $have_mrow = FETCH_ROW;
419 dpavlin 1.1 last;
420     }
421     }
422    
423 dpavlin 1.2 if ($pk_same && $have_mrow == HAVE_ROW && $have_srow == HAVE_ROW) {
424 dpavlin 1.1 # check non-key cols for row
425     foreach my $col (@cols_cmp) {
426     if ($mrow->{$col} ne $srow->{$col}) {
427     $diff_row++;
428 dpavlin 1.2 print STDERR "DIFF in table '$table' row ($col): [".join(",",@cols_pk)."] '$mrow->{$col}' != '$srow->{$col}'\n" if ($verbose);
429 dpavlin 1.1 print sql_delete($table,$srow,@cols_pk),"\n";
430     print sql_insert($table,$mrow,@cols),"\n";
431 dpavlin 1.2 $have_mrow = FETCH_ROW;
432     $have_srow = FETCH_ROW;
433 dpavlin 1.1 }
434     }
435     }
436     }
437    
438     print STDERR "$diff_row differences in table $table\n" if ($verbose && $diff_row > 0);
439     $diff_total += $diff_row;
440     }
441    
442     print STDERR "$diff_total differences in all tables\n" if ($verbose && $diff_total > 0);
443    
444     $mdbh->disconnect();
445     $sdbh->disconnect();

  ViewVC Help
Powered by ViewVC 1.1.26