/[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.1 by dpavlin, Tue Aug 12 10:38:23 2003 UTC revision 1.3 by dpavlin, Tue Aug 12 12:00:07 2003 UTC
# Line 74  sub debug_sql { Line 74  sub debug_sql {
74          my $sql = shift;          my $sql = shift;
75          $sql =~ s/[\n\r]/ /gs;          $sql =~ s/[\n\r]/ /gs;
76          $sql =~ s/\s\s+/ /g;          $sql =~ s/\s\s+/ /g;
77          print STDERR "SQL: $sql\n";          print STDERR "DEBUG: SQL: $sql\n";
78  }  }
79    
80  sub debug_row {  sub debug_row {
# Line 82  sub debug_row { Line 82  sub debug_row {
82          my $row = shift;          my $row = shift;
83          my @cols = @_;          my @cols = @_;
84          if (! $row) {          if (! $row) {
85                  print STDERR "ROW data is undef!\n";                  print STDERR "DEBUG: ROW data is undef!\n";
86                  return;                  return;
87          }          }
88          print STDERR "ROW: [",$#cols+1,"] ";          print STDERR "DEBUG: ROW: [",$#cols+1,"] ";
89          foreach my $col (@cols) {          foreach my $col (@cols) {
90                  print STDERR "$col:";                  print STDERR "$col:";
91                  if ($row->{$col}) {                  if ($row->{$col}) {
# Line 98  sub debug_row { Line 98  sub debug_row {
98          print STDERR "\n";          print STDERR "\n";
99  }  }
100    
101    sub debug {
102            return if (!$debug);
103            print STDERR "DEBUG: ",@_;
104    }
105    
106  $verbose = 1 if ($debug);  $verbose = 1 if ($debug);
107    
108  # which tables to compare?  # which tables to compare?
# Line 118  if ($tables) { Line 123  if ($tables) {
123                  AND pg_catalog.pg_table_is_visible(c.oid)                  AND pg_catalog.pg_table_is_visible(c.oid)
124                  and c.relname not like '_rserv_%'                  and c.relname not like '_rserv_%'
125          ";          ";
126            foreach my $table (@tables) {
127                    $sql .= " and c.relname like '$table'";
128            }
129          my $sth = $mdbh->prepare($sql);          my $sth = $mdbh->prepare($sql);
130          $sth->execute() || die;          $sth->execute() || die;
131          while(my $row = $sth->fetchrow_hashref()) {          while(my $row = $sth->fetchrow_hashref()) {
# Line 125  if ($tables) { Line 133  if ($tables) {
133          }          }
134  }  }
135    
136  print "Comparing tables: ",join(", ",@tables),"\n" if ($debug);  debug "Comparing tables: ".join(", ",@tables)."\n";
137    
138  my $cols;  my $cols;
139  my $diff_total = 0;  my $diff_total = 0;
# Line 196  ORDER BY a.attnum Line 204  ORDER BY a.attnum
204          $sth->finish();          $sth->finish();
205    
206          if ($debug) {          if ($debug) {
207                  print STDERR "table $table [$oid] not null: (",join(", ",@cols_notnull),")";                  print STDERR "DEBUG: table $table [$oid] not null: (",join(", ",@cols_notnull),")";
208                  print STDERR " - null: (",join(", ",@cols_null),")" if (@cols_null);                  print STDERR " - null: (",join(", ",@cols_null),")" if (@cols_null);
209                  print STDERR " - skip: (",join(", ",@cols_skip),")" if (@cols_skip);                  print STDERR " - skip: (",join(", ",@cols_skip),")" if (@cols_skip);
210                  print STDERR "\n";                  print STDERR "\n";
# Line 287  ORDER BY Line 295  ORDER BY
295    
296          my $order = sql_order(@cols_pk);          my $order = sql_order(@cols_pk);
297          $msql .= $order;          $msql .= $order;
298          $ssql .= sql_where(@cols_pk) . $order;          $ssql .= $order;
299    
300          debug_sql($msql);          debug_sql($msql);
         debug_sql($ssql);  
301    
302          my $msth = $mdbh->prepare($msql) || die;          my $msth = $mdbh->prepare($msql) || die;
303          $msth->execute() || die;          $msth->execute() || die;
304    
305          my $ssth = $sdbh->prepare($ssql) || die;          my $ssth = $sdbh->prepare($ssql) || die;
306            $ssth->execute() || die;
307    
308          my $diff_row = 0;          my $diff_row = 0;
309    
310          my ($mrow,$srow);          my ($mrow,$srow);
311          my ($have_mrow,$have_srow) = (0,0);          # have_*
312          my @pk_val;          use constant NO_ROW => 0;
313            use constant FETCH_ROW => 1;
314            use constant HAVE_ROW => 2;
315            my ($have_mrow,$have_srow) = (FETCH_ROW,FETCH_ROW);
316    
317            while ($have_mrow != NO_ROW || $have_srow != NO_ROW) {
318    
319                    debug "have mrow: $have_mrow srow: $have_srow\n";
320    
321                    sub pk_val {
322                            my $row = shift || die "need row";
323                            my @cols = shift || die "need cols";
324                            my @val;
325                            foreach my $col (@cols) {
326                                    push @val,$row->{$col};
327                            }
328                            return @val;
329                    }
330    
         my $more_rows = 1;  
         while (!$have_mrow || !$have_srow) {  
331                  # fetch row from master                  # fetch row from master
332                  if (!$have_mrow) {                  if ($have_mrow == FETCH_ROW) {
333                          print "fetch row from master [$more_rows]: $msql\n" if ($debug);                          debug "fetch row from master: $msql\n";
334                          $mrow = $msth->fetchrow_hashref();                          $mrow = $msth->fetchrow_hashref();
335                          debug_row($mrow,@cols);                          debug_row($mrow,@cols);
336    
337                          if ($mrow) {                          if ($mrow) {
338                                  # fill-in primary key values                                  # fill-in primary key values
339                                  @pk_val = ();                                  $have_mrow = HAVE_ROW;
                                 foreach my $col (@cols_pk) {  
                                         push @pk_val,$mrow->{$col};  
                                 }  
                                 $have_mrow = 1;  
340                          } else {                          } else {
341                                  $have_mrow = 0;                                  $have_mrow = NO_ROW;
342                          }                          }
343                  }                  }
344    
345                  # fetch row from slave                  # fetch row from slave
346                  if (!$have_srow) {                  if ($have_srow == FETCH_ROW) {
347                          print "fetch row from slave [$more_rows]: $ssql\n" if ($debug);                          debug "fetch row from slave: $ssql\n";
                         $ssth->execute(@pk_val) || die;  
348                          $srow = $ssth->fetchrow_hashref();                          $srow = $ssth->fetchrow_hashref();
349                          debug_row($srow,@cols);                          debug_row($srow,@cols);
350                          if ($srow) {                          if ($srow) {
351                                  $have_srow = 1;                                  $have_srow = HAVE_ROW;
352                          } else {                          } else {
353                                  $have_srow = 0;                                  $have_srow = NO_ROW;
354                          }                          }
355                  }                  }
356    
357                  # end of this table?                  debug "have mrow: $have_mrow srow: $have_srow\n";
                 if (!$have_mrow && !$have_srow) {  
                         last;  
                 }  
358    
359                  # insert into slave database                  # insert into slave database
360                  sub sql_insert {                  sub sql_insert {
# Line 376  ORDER BY Line 391  ORDER BY
391                          return $sql;                          return $sql;
392                  }                  }
393    
394                    # update row in slave database
395                    sub sql_update {
396                            my $table = shift @_ || die "need table as argument";
397                            my $col = shift @_ || die "need col to update";
398                            my $val = shift @_ || die "need new val";
399                            my @cols_pk = @_ || die "need pk idenitifier";
400    
401                            my $sql = "udate $table set $col=".$mdbh->quote($val);
402                            debug_sql($sql);
403                            return $sql;
404                    }
405                  # master        slave                  # master        slave
406                  # 1     =       1       test                  # 1     =       1       test
407                  # 1     <       2       insert mrow                  # 1     <       2       insert mrow
# Line 387  ORDER BY Line 413  ORDER BY
413    
414                  # check key cols for row                  # check key cols for row
415                  foreach my $col (@cols_pk) {                  foreach my $col (@cols_pk) {
416                          if ( (!$mrow && $srow) || ($mrow && $srow && ($mrow->{$col} gt $srow->{$col})) ) {                          if ( ($have_mrow == NO_ROW && $have_srow == HAVE_ROW) ||
417                                    ($have_mrow == HAVE_ROW && $have_srow == HAVE_ROW && $mrow->{$col} gt $srow->{$col}) ) {
418                                  $diff_row++;                                  $diff_row++;
419                                  $pk_same = 0;                                  $pk_same = 0;
420                                  print sql_delete($table,$srow,@cols_pk),"\n";                                  print STDERR "EXTRA row in table '$table' pk: [".join(",",@cols_pk)."] value (".join(",",pk_val($srow,@cols_pk)).")\n" if ($verbose);
421                                  $have_srow = 0; # fetch new slave row                                  print sql_delete($table,$srow,@cols_pk),";\n";
422                                    $have_srow = FETCH_ROW;
423                                  last;                                  last;
424                          } elsif ( ($mrow && !$srow) || ($mrow && $srow && ($mrow->{$col} lt $srow->{$col})) ) {                          } elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) ||
425                                    ($have_mrow == HAVE_ROW && $have_srow == HAVE_ROW && $mrow->{$col} lt $srow->{$col}) ) {
426                                  $diff_row++;                                  $diff_row++;
427                                  $pk_same = 0;                                  $pk_same = 0;
428                                  print sql_insert($table,$mrow,@cols),"\n";                                  print STDERR "MISSING row in table '$table' pk: [".join(",",@cols_pk)."] value (".join(",",pk_val($mrow,@cols_pk)).")\n" if ($verbose);
429                                  $have_mrow = 0;                                  print sql_insert($table,$mrow,@cols),";\n";
430                                    $have_mrow = FETCH_ROW;
431                                  last;                                  last;
432                          }                          }
433                  }                  }
434    
435                  if ($pk_same) {                  if ($pk_same && $have_mrow == HAVE_ROW && $have_srow == HAVE_ROW) {
436                          # check non-key cols for row                          # check non-key cols for row
437                          foreach my $col (@cols_cmp) {                          foreach my $col (@cols_cmp) {
438                                  if ($mrow->{$col} ne $srow->{$col}) {                                  if ($mrow->{$col} ne $srow->{$col}) {
439                                          $diff_row++;                                          $diff_row++;
440                                          print STDERR "DIFF in table '$table' row ($col): [".join(",",@pk_val)."] '$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);
441                                          print sql_delete($table,$srow,@cols_pk),"\n";                                          print sql_update($table,$col,$mrow->{$col},@cols_pk),";\n";
                                         print sql_insert($table,$mrow,@cols),"\n";  
442                                  }                                  }
443                          }                          }
444                            $have_mrow = FETCH_ROW;
445                            $have_srow = FETCH_ROW;
446                  }                  }
447          }          }
448    

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.3

  ViewVC Help
Powered by ViewVC 1.1.26