/[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

Diff of /pgdiff

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.4 by dpavlin, Tue Aug 12 18:45:04 2003 UTC revision 1.7 by dpavlin, Fri Aug 15 22:06:19 2003 UTC
# Line 63  $sinfo = "$sinfo port=$slaveport" if (de Line 63  $sinfo = "$sinfo port=$slaveport" if (de
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    
# Line 107  sub debug { Line 119  sub debug {
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 schema";
123    
124  # which tables to compare?  # which tables to compare?
125    
# Line 124  foreach my $table (@tables) { Line 136  foreach my $table (@tables) {
136    
137          # diff schema          # diff schema
138    
139          my @cols_notnull;# colums compared by a=b          # all colums (for insert)
140          my @cols_null;  # colums compared by a=b or a is null and b is null          my @cols = @{$mscheme->cols($table)};
141    
142            # colums compared by a=b
143            my @cols_notnull = @{$mscheme->cols_notnull($table)};
144    
145            # colums compared by a=b or a is null and b is null
146            my @cols_null = @{$mscheme->cols_null($table)};
147    
148            # primary key columns
149            my @cols_pk = @{$mscheme->cols_pk($table)};
150    
151            # columns to compare (not in primary key)
152            my @cols_cmp = @{$mscheme->cols_notpk($table)};
153    
154          my @cols_skip;  # skipped columns          my @cols_skip;  # skipped columns
155          my @cols_test;  # all colums to test (without skipped)          my @cols_test;  # all colums to test (without skipped)
         my @cols;       # all colums (for insert)  
   
156    
157          foreach my $row ($mscheme->explain_table($table)) {          foreach my $row (@{$mscheme->pg_attribute($table)}) {
158                  # attname | format_type | attnotnull | atthasdef | attnum                  # attname | format_type | attnotnull | atthasdef | attnum
159    
                 push @cols,$row->{attname};  
   
160                  # FIXME: do something with attributes which shouldn't be compared                  # FIXME: do something with attributes which shouldn't be compared
161                  # (date, time, datetime, timestamp)                  # (date, time, datetime, timestamp)
162                  if ($row->{format_type} =~ /(date)|(time)/i) {                  if ($row->{format_type} =~ /(date)|(time)/i) {
163                          push @cols_skip,$row->{attname};                          push @cols_skip,$row->{attname};
                         next;  
                 }  
   
                 push @cols_test,$row->{attname};  
   
                 if ($row->{attnotnull}) {  
                         push @cols_notnull,$row->{attname};  
164                  } else {                  } else {
165                          push @cols_null,$row->{attname};                          push @cols_test,$row->{attname};
166                  }                  }
167    
168          }          }
169    
170          if ($debug) {          if ($debug) {
# Line 161  foreach my $table (@tables) { Line 176  foreach my $table (@tables) {
176    
177          # diff data          # diff data
178    
         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}}++;  
                 }  
                   
         }  
179          if (! @cols_pk) {          if (! @cols_pk) {
180                  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";
181                  @cols_pk = @cols;                  @cols_pk = @cols;
182          }          }
183    
         my @cols_cmp;   # columns to compare  
   
         foreach my $col (@cols_test) {  
                 push @cols_cmp,$col if (! $in_pk{$col});  
         }  
184    
185          if ($verbose) {          if ($verbose) {
186                  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";
# Line 307  ORDER BY Line 278  ORDER BY
278    
279                  # insert into slave database                  # insert into slave database
280                  sub sql_insert {                  sub sql_insert {
281                            my $dbh = shift @_ || die "need dbh";
282                          my $table = shift @_ || die "need table as argument";                          my $table = shift @_ || die "need table as argument";
283                          my $row = shift @_ || die "need row data";                          my $row = shift @_ || die "need row data";
284                          my @cols = @_;                          my @cols = @_;
# Line 314  ORDER BY Line 286  ORDER BY
286                          my $sql = "insert into $table (".join(",",@cols).") values (";                          my $sql = "insert into $table (".join(",",@cols).") values (";
287                          my $comma = "";                          my $comma = "";
288                          foreach my $col (@cols) {                          foreach my $col (@cols) {
289                                  $sql .= $comma.$mdbh->quote($row->{$col});                                  $sql .= $comma.$dbh->quote($row->{$col});
290                                  $comma = ",";                                  $comma = ",";
291                          }                          }
292                          $sql.=")";                          $sql.=")";
# Line 324  ORDER BY Line 296  ORDER BY
296    
297                  # delete from slave database                  # delete from slave database
298                  sub sql_delete {                  sub sql_delete {
299                            my $dbh = shift @_ || die "need dbh";
300                          my $table = shift @_ || die "need table as argument";                          my $table = shift @_ || die "need table as argument";
301                          my $row = shift @_ || die "need row as argument";                          my $row = shift @_ || die "need row as argument";
302                          my @cols_pk = @_;                          my @cols_pk = @_;
303    
304                          my $where = sql_where(@cols_pk);                          my $where = sql_where(@cols_pk);
305    
306                          my $sql = "delete from $table ";                          my $sql = "delete from $table";
307                          foreach my $col (@cols_pk) {                          foreach my $col (@cols_pk) {
308                                  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";
309                                  $where =~ s/\?/$val/;                                  $where =~ s/\?/$val/;
310                          }                          }
311                          $sql .= $where;                          $sql .= $where;
# Line 342  ORDER BY Line 315  ORDER BY
315    
316                  # update row in slave database                  # update row in slave database
317                  sub sql_update {                  sub sql_update {
318                            my $dbh = shift @_ || die "need dbh";
319                          my $table = shift @_ || die "need table as argument";                          my $table = shift @_ || die "need table as argument";
320                          my $col = shift @_ || die "need col to update";                          my $col = shift @_ || die "need col to update";
321                          my $val = shift @_ || die "need new val";                          my $row = shift @_ || die "need row";
322                          my @cols_pk = @_ || die "need pk idenitifier";                          my @cols_pk = @_;
323    
324                          my $sql = "udate $table set $col=".$mdbh->quote($val);                          my $sql = "update $table set $col=".$dbh->quote($row->{$col});
325                            my $where = sql_where(@cols_pk);
326                            foreach my $col (@cols_pk) {
327                                    my $val = $dbh->quote($row->{$col}) || die "can't find value in row for col $col";
328                                    $where =~ s/\?/$val/;
329                            }
330                            $sql .= $where;
331                          debug_sql($sql);                          debug_sql($sql);
332                          return $sql;                          return $sql;
333                  }                  }
# Line 367  ORDER BY Line 347  ORDER BY
347                                  $diff_row++;                                  $diff_row++;
348                                  $pk_same = 0;                                  $pk_same = 0;
349                                  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);
350                                  print sql_delete($table,$srow,@cols_pk),";\n";                                  print sql_delete($sdbh,$table,$srow,@cols_pk),";\n";
351                                  $have_srow = FETCH_ROW;                                  $have_srow = FETCH_ROW;
352                                  last;                                  last;
353                          } elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) ||                          } elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) ||
# Line 375  ORDER BY Line 355  ORDER BY
355                                  $diff_row++;                                  $diff_row++;
356                                  $pk_same = 0;                                  $pk_same = 0;
357                                  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);
358                                  print sql_insert($table,$mrow,@cols),";\n";                                  print sql_insert($mdbh,$table,$mrow,@cols),";\n";
359                                  $have_mrow = FETCH_ROW;                                  $have_mrow = FETCH_ROW;
360                                  last;                                  last;
361                          }                          }
# Line 387  ORDER BY Line 367  ORDER BY
367                                  if ($mrow->{$col} ne $srow->{$col}) {                                  if ($mrow->{$col} ne $srow->{$col}) {
368                                          $diff_row++;                                          $diff_row++;
369                                          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);
370                                          print sql_update($table,$col,$mrow->{$col},@cols_pk),";\n";                                          print sql_update($mdbh,$table,$col,$mrow,@cols_pk),";\n";
371                                  }                                  }
372                          }                          }
373                          $have_mrow = FETCH_ROW;                          $have_mrow = FETCH_ROW;

Legend:
Removed from v.1.4  
changed lines
  Added in v.1.7

  ViewVC Help
Powered by ViewVC 1.1.26