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

Contents of /pgdiff

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.6 - (show annotations)
Wed Aug 13 13:27:32 2003 UTC (19 years, 5 months ago) by dpavlin
Branch: MAIN
Changes since 1.5: +18 -9 lines
fixed insert

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 use Pg::Scheme;
15
16 $| = 1;
17
18 my ($debug,$verbose) = (0,0);
19 my ($help,$masterhost,$masterport,$masteruser,$masterpassword,
20 $slavehost,$slaveport,$slaveuser,$slavepassword,
21 $masterfile,
22 $slavefile,
23 );
24 my $tables;
25
26 my $result = GetOptions(
27 "debug!" => \$debug, "verbose!" => \$verbose, "help" => \$help,
28 "masterhost=s" => \$masterhost, "masterport=i" => \$masterport,
29 "masteruser=s" => \$masteruser, "masterpassword=s" => \$masterpassword,
30 "masterfile=s" => \$masterfile,
31 "slavehost=s" => \$slavehost, "slaveport=i" => \$slaveport,
32 "slaveuser=s" => \$slaveuser, "slavepassword=s" => \$slavepassword,
33 "slavefile=s" => \$slavefile,
34 "tables=s" => \$tables,
35 );
36
37 if (defined($help) || (scalar(@ARGV) < 2)) {
38 print "Usage: $0 [options] masterdb slavedb
39 Options:
40 --masterhost=hostname --masterport=port
41 --masteruser=username --masterpassword=string
42 --masterfile=filename
43 --slavehost=hostname --slaveport=port
44 --slaveuser=username --slavepassword=string
45 --slavefile=filename
46 --tables[s]=table[,table...]
47 ";
48 # exit ((scalar(@ARGV) < 2)? 1:0);
49 exit;
50 }
51
52 my $master = $ARGV[0] || "master";
53 my $slave = $ARGV[1] || "slave";
54
55 my $minfo = "dbname=$master";
56 $minfo = "$minfo host=$masterhost" if (defined($masterhost));
57 $minfo = "$minfo port=$masterport" if (defined($masterport));
58
59 my $sinfo = "dbname=$slave";
60 $sinfo = "$sinfo host=$slavehost" if (defined($slavehost));
61 $sinfo = "$sinfo port=$slaveport" if (defined($slaveport));
62
63 print "Master connection is $minfo\n" if ($debug);
64 print "Slave connection is $sinfo\n" if ($debug);
65
66 my $mdbh = DBI->connect("DBI:Pg:$minfo", $masteruser, $masterpassword, { PrintError => 1 });
67 my $sdbh = DBI->connect("DBI:Pg:$sinfo", $slaveuser, $slavepassword, { PrintError => 1 });
68
69 my ($diff_shema,$diff_data) = (0,0);
70
71 my $sql;
72
73 sub debug_sql {
74 return if (! $debug);
75 my $sql = shift;
76 $sql =~ s/[\n\r]/ /gs;
77 $sql =~ s/\s\s+/ /g;
78 print STDERR "DEBUG: SQL: $sql\n";
79 }
80
81 sub debug_row {
82 return if (! $debug);
83 my $row = shift;
84 my @cols = @_;
85 if (! $row) {
86 print STDERR "DEBUG: ROW data is undef!\n";
87 return;
88 }
89 print STDERR "DEBUG: ROW: [",$#cols+1,"] ";
90 foreach my $col (@cols) {
91 print STDERR "$col:";
92 if ($row->{$col}) {
93 print $row->{$col};
94 } else {
95 print "null";
96 }
97 print " ";
98 }
99 print STDERR "\n";
100 }
101
102 sub debug {
103 return if (!$debug);
104 print STDERR "DEBUG: ",@_;
105 }
106
107 $verbose = 1 if ($debug);
108
109 # init object for scheme in master database
110 my $mscheme = new Pg::Scheme( 'dbh' => $mdbh, 'DEBUG' => 0 ) || die "can't query schema";
111
112 # which tables to compare?
113
114 my @tables = $mscheme->list_tables($tables);
115
116 debug "Comparing tables: ".join(", ",@tables)."\n";
117
118 my $cols;
119 my $diff_total = 0;
120
121 foreach my $table (@tables) {
122
123 my $sth;
124
125 # diff schema
126
127 # all colums (for insert)
128 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
143 my @cols_test; # all colums to test (without skipped)
144
145 foreach my $row (@{$mscheme->pg_attribute($table)}) {
146 # attname | format_type | attnotnull | atthasdef | attnum
147
148 # FIXME: do something with attributes which shouldn't be compared
149 # (date, time, datetime, timestamp)
150 if ($row->{format_type} =~ /(date)|(time)/i) {
151 push @cols_skip,$row->{attname};
152 } else {
153 push @cols_test,$row->{attname};
154 }
155
156 }
157
158 if ($debug) {
159 print STDERR "DEBUG: table $table not null cols: (",join(", ",@cols_notnull),")";
160 print STDERR " - null cols: (",join(", ",@cols_null),")" if (@cols_null);
161 print STDERR " - skip cols: (",join(", ",@cols_skip),")" if (@cols_skip);
162 print STDERR "\n";
163 }
164
165 # diff data
166
167 if (! @cols_pk) {
168 print STDERR "can't find PK rows for table '$table' using all\n";
169 @cols_pk = @cols;
170 }
171
172
173 if ($verbose) {
174 print "table '$table' using for key: (",join(", ",@cols_pk),") to compare cols: (",join(", ",@cols_cmp),")\n";
175 }
176
177 # diff data
178
179 my $msql = "select ".join(",",@cols)." from $table";
180 my $ssql = $msql;
181
182 sub sql_where {
183 my @cols = @_;
184 my $and = "";
185 my $where = " where ";
186 foreach my $col (@cols) {
187 $where .= "$and$col=?";
188 $and = " and ";
189 }
190 return $where;
191 }
192
193 sub sql_order {
194 my @cols = @_;
195 my $order = " order by ";
196 my $comma = "";
197 foreach my $col (@cols) {
198 $order .= "$comma$col asc";
199 $comma = ", ";
200 }
201 return $order;
202 }
203
204 my $order = sql_order(@cols_pk);
205 $msql .= $order;
206 $ssql .= $order;
207
208 debug_sql($msql);
209
210 my $msth = $mdbh->prepare($msql) || die;
211 $msth->execute() || die;
212
213 my $ssth = $sdbh->prepare($ssql) || die;
214 $ssth->execute() || die;
215
216 my $diff_row = 0;
217
218 my ($mrow,$srow);
219 # have_*
220 use constant NO_ROW => 0;
221 use constant FETCH_ROW => 1;
222 use constant HAVE_ROW => 2;
223 my ($have_mrow,$have_srow) = (FETCH_ROW,FETCH_ROW);
224
225 while ($have_mrow != NO_ROW || $have_srow != NO_ROW) {
226
227 debug "have mrow: $have_mrow srow: $have_srow\n";
228
229 sub pk_val {
230 my $row = shift || die "need row";
231 my @cols = shift || die "need cols";
232 my @val;
233 foreach my $col (@cols) {
234 push @val,$row->{$col};
235 }
236 return @val;
237 }
238
239 # fetch row from master
240 if ($have_mrow == FETCH_ROW) {
241 debug "fetch row from master: $msql\n";
242 $mrow = $msth->fetchrow_hashref();
243 debug_row($mrow,@cols);
244
245 if ($mrow) {
246 # fill-in primary key values
247 $have_mrow = HAVE_ROW;
248 } else {
249 $have_mrow = NO_ROW;
250 }
251 }
252
253 # fetch row from slave
254 if ($have_srow == FETCH_ROW) {
255 debug "fetch row from slave: $ssql\n";
256 $srow = $ssth->fetchrow_hashref();
257 debug_row($srow,@cols);
258 if ($srow) {
259 $have_srow = HAVE_ROW;
260 } else {
261 $have_srow = NO_ROW;
262 }
263 }
264
265 debug "have mrow: $have_mrow srow: $have_srow\n";
266
267 # insert into slave database
268 sub sql_insert {
269 my $dbh = shift @_ || die "need dbh";
270 my $table = shift @_ || die "need table as argument";
271 my $row = shift @_ || die "need row data";
272 my @cols = @_;
273
274 my $sql = "insert into $table (".join(",",@cols).") values (";
275 my $comma = "";
276 foreach my $col (@cols) {
277 $sql .= $comma.$dbh->quote($row->{$col});
278 $comma = ",";
279 }
280 $sql.=")";
281 debug_sql($sql);
282 return $sql;
283 }
284
285 # delete from slave database
286 sub sql_delete {
287 my $dbh = shift @_ || die "need dbh";
288 my $table = shift @_ || die "need table as argument";
289 my $row = shift @_ || die "need row as argument";
290 my @cols_pk = @_;
291
292 my $where = sql_where(@cols_pk);
293
294 my $sql = "delete from $table";
295 foreach my $col (@cols_pk) {
296 my $val = $dbh->quote($row->{$col}) || die "can't find value in row for col $col";
297 $where =~ s/\?/$val/;
298 }
299 $sql .= $where;
300 debug_sql($sql);
301 return $sql;
302 }
303
304 # update row in slave database
305 sub sql_update {
306 my $dbh = shift @_ || die "need dbh";
307 my $table = shift @_ || die "need table as argument";
308 my $col = shift @_ || die "need col to update";
309 my $row = shift @_ || die "need row";
310 my @cols_pk = @_;
311
312 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);
320 return $sql;
321 }
322 # master slave
323 # 1 = 1 test
324 # 1 < 2 insert mrow
325 # 2 > 1 delete srow
326 # 1 = undef insert mrow
327 # undef = 1 delete srow
328
329 my $pk_same = 1;
330
331 # check key cols for row
332 foreach my $col (@cols_pk) {
333 if ( ($have_mrow == NO_ROW && $have_srow == HAVE_ROW) ||
334 ($have_mrow == HAVE_ROW && $have_srow == HAVE_ROW && $mrow->{$col} gt $srow->{$col}) ) {
335 $diff_row++;
336 $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);
338 print sql_delete($sdbh,$table,$srow,@cols_pk),";\n";
339 $have_srow = FETCH_ROW;
340 last;
341 } elsif ( ($have_mrow == HAVE_ROW && $have_srow == NO_ROW) ||
342 ($have_mrow == HAVE_ROW && $have_srow == HAVE_ROW && $mrow->{$col} lt $srow->{$col}) ) {
343 $diff_row++;
344 $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);
346 print sql_insert($mdbh,$table,$mrow,@cols),";\n";
347 $have_mrow = FETCH_ROW;
348 last;
349 }
350 }
351
352 if ($pk_same && $have_mrow == HAVE_ROW && $have_srow == HAVE_ROW) {
353 # check non-key cols for row
354 foreach my $col (@cols_cmp) {
355 if ($mrow->{$col} ne $srow->{$col}) {
356 $diff_row++;
357 print STDERR "DIFF in table '$table' row ($col): [".join(",",@cols_pk)."] '$mrow->{$col}' != '$srow->{$col}'\n" if ($verbose);
358 print sql_update($mdbh,$table,$col,$mrow,@cols_pk),";\n";
359 }
360 }
361 $have_mrow = FETCH_ROW;
362 $have_srow = FETCH_ROW;
363 }
364 }
365
366 print STDERR "$diff_row differences in table $table\n" if ($verbose && $diff_row > 0);
367 $diff_total += $diff_row;
368 }
369
370 if ($verbose) {
371 if ($diff_total == 0) {
372 print STDERR "databases are same";
373 } elsif ($diff_total > 0) {
374 print STDERR "$diff_total differences in all tables\n";
375 } else {
376 die "this shouldn't happend. please report a bug!";
377 }
378 }
379
380 $mdbh->disconnect();
381 $sdbh->disconnect();

  ViewVC Help
Powered by ViewVC 1.1.26