/[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.6 by dpavlin, Wed Aug 13 13:27:32 2003 UTC
# Line 107  sub debug { Line 107  sub debug {
107  $verbose = 1 if ($debug);  $verbose = 1 if ($debug);
108    
109  # init object for scheme in master database  # init object for scheme in master database
110  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";
111    
112  # which tables to compare?  # which tables to compare?
113    
# Line 124  foreach my $table (@tables) { Line 124  foreach my $table (@tables) {
124    
125          # diff schema          # diff schema
126    
127          my @cols_notnull;# colums compared by a=b          # all colums (for insert)
128          my @cols_null;  # colums compared by a=b or a is null and b is null          my @cols = @{$mscheme->cols($table)};
129    
130            # 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    
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          foreach my $row ($mscheme->explain_table($table)) {          foreach my $row (@{$mscheme->pg_attribute($table)}) {
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          }          }
157    
158          if ($debug) {          if ($debug) {
# Line 161  foreach my $table (@tables) { Line 164  foreach my $table (@tables) {
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;  
         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}}++;  
                 }  
                   
         }  
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";
# Line 307  ORDER BY Line 266  ORDER BY
266    
267                  # insert into slave database                  # insert into slave database
268                  sub sql_insert {                  sub sql_insert {
269                            my $dbh = shift @_ || die "need dbh";
270                          my $table = shift @_ || die "need table as argument";                          my $table = shift @_ || die "need table as argument";
271                          my $row = shift @_ || die "need row data";                          my $row = shift @_ || die "need row data";
272                          my @cols = @_;                          my @cols = @_;
# Line 314  ORDER BY Line 274  ORDER BY
274                          my $sql = "insert into $table (".join(",",@cols).") values (";                          my $sql = "insert into $table (".join(",",@cols).") values (";
275                          my $comma = "";                          my $comma = "";
276                          foreach my $col (@cols) {                          foreach my $col (@cols) {
277                                  $sql .= $comma.$mdbh->quote($row->{$col});                                  $sql .= $comma.$dbh->quote($row->{$col});
278                                  $comma = ",";                                  $comma = ",";
279                          }                          }
280                          $sql.=")";                          $sql.=")";
# Line 324  ORDER BY Line 284  ORDER BY
284    
285                  # delete from slave database                  # delete from slave database
286                  sub sql_delete {                  sub sql_delete {
287                            my $dbh = shift @_ || die "need dbh";
288                          my $table = shift @_ || die "need table as argument";                          my $table = shift @_ || die "need table as argument";
289                          my $row = shift @_ || die "need row as argument";                          my $row = shift @_ || die "need row as argument";
290                          my @cols_pk = @_;                          my @cols_pk = @_;
291    
292                          my $where = sql_where(@cols_pk);                          my $where = sql_where(@cols_pk);
293    
294                          my $sql = "delete from $table ";                          my $sql = "delete from $table";
295                          foreach my $col (@cols_pk) {                          foreach my $col (@cols_pk) {
296                                  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";
297                                  $where =~ s/\?/$val/;                                  $where =~ s/\?/$val/;
298                          }                          }
299                          $sql .= $where;                          $sql .= $where;
# Line 342  ORDER BY Line 303  ORDER BY
303    
304                  # update row in slave database                  # update row in slave database
305                  sub sql_update {                  sub sql_update {
306                            my $dbh = shift @_ || die "need dbh";
307                          my $table = shift @_ || die "need table as argument";                          my $table = shift @_ || die "need table as argument";
308                          my $col = shift @_ || die "need col to update";                          my $col = shift @_ || die "need col to update";
309                          my $val = shift @_ || die "need new val";                          my $row = shift @_ || die "need row";
310                          my @cols_pk = @_ || die "need pk idenitifier";                          my @cols_pk = @_;
311    
312                          my $sql = "udate $table set $col=".$mdbh->quote($val);                          my $sql = "update $table set $col=".$dbh->quote($row->{$col});
313                            my $where = sql_where(@cols_pk);
314                            foreach my $col (@cols_pk) {
315                                    my $val = $dbh->quote($row->{$col}) || die "can't find value in row for col $col";
316                                    $where =~ s/\?/$val/;
317                            }
318                            $sql .= $where;
319                          debug_sql($sql);                          debug_sql($sql);
320                          return $sql;                          return $sql;
321                  }                  }
# Line 367  ORDER BY Line 335  ORDER BY
335                                  $diff_row++;                                  $diff_row++;
336                                  $pk_same = 0;                                  $pk_same = 0;
337                                  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);
338                                  print sql_delete($table,$srow,@cols_pk),";\n";                                  print sql_delete($sdbh,$table,$srow,@cols_pk),";\n";
339                                  $have_srow = FETCH_ROW;                                  $have_srow = FETCH_ROW;
340                                  last;                                  last;
341                          } elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) ||                          } elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) ||
# Line 375  ORDER BY Line 343  ORDER BY
343                                  $diff_row++;                                  $diff_row++;
344                                  $pk_same = 0;                                  $pk_same = 0;
345                                  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);
346                                  print sql_insert($table,$mrow,@cols),";\n";                                  print sql_insert($mdbh,$table,$mrow,@cols),";\n";
347                                  $have_mrow = FETCH_ROW;                                  $have_mrow = FETCH_ROW;
348                                  last;                                  last;
349                          }                          }
# Line 387  ORDER BY Line 355  ORDER BY
355                                  if ($mrow->{$col} ne $srow->{$col}) {                                  if ($mrow->{$col} ne $srow->{$col}) {
356                                          $diff_row++;                                          $diff_row++;
357                                          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);
358                                          print sql_update($table,$col,$mrow->{$col},@cols_pk),";\n";                                          print sql_update($mdbh,$table,$col,$mrow,@cols_pk),";\n";
359                                  }                                  }
360                          }                          }
361                          $have_mrow = FETCH_ROW;                          $have_mrow = FETCH_ROW;

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

  ViewVC Help
Powered by ViewVC 1.1.26