Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
laziness, impatience, and hubris
 
PerlMonks  

Comparing databases - what is the best way?

by Win (Novice)
on Apr 13, 2006 at 04:01 UTC ( [id://543021]=perlquestion: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.

Win has asked for the wisdom of the Perl Monks concerning the following question:

Is there a Perl module that will let me compare the tables of two databases and provide a summary of the similarities and differences between the two databases?
  • Comment on Comparing databases - what is the best way?

Replies are listed 'Best First'.
Re: Comparing databases - what is the best way?
by jesuashok (Curate) on Apr 13, 2006 at 04:36 UTC

      jesuashok, your URL isn't a clickable link, and even if it was, it wouldn't work.

      Win, searching for compare on CPAN gives me some promising modules, namely Data-Compare and List-Compare with which you can roll your own solution.

Re: Comparing databases - what is the best way?
by DrHyde (Prior) on Apr 13, 2006 at 05:25 UTC
    Provided that the tables aren't too big, I'd think about using $dbh->selectall_arrayref("...") to slurp each pair of tables into memory, use Data::Dumper to turn them into strings, and something diff-ish to find differences.

    If the tables are too big for that, then you'll need to do a bit more work. For each pair of tables, get a list of the values of the primary key(s), and find any differences - that'll catch added and deleted records. Then you'll also need to compare the individual records for each value of the primary key that is common to both tables

Re: Comparing databases - what is the best way?
by salva (Canon) on Apr 13, 2006 at 07:05 UTC
    If you run a select on every table ordered by the primary key you can compare the rows without loading the full tables in memory.

    For instance, supposing the primary key my_key is of some string type:

    my $sql = <<EOSQL; select my_key, foo1, foo2, ... from bar order by my_key EOSQL my $sth1 = $dbh1->prepare($sql); my $sth2 = $dbh2->prepare($sql); unless ($sth1->execute and $sth2->execute) { die ...; } my $row1 = $sth1->fetchrow_arrayref; my $row2 = $sth1->fetchrow_arrayref; while ($row1 and $row2) { if ($row1->[0] lt $row2->[0]) { print "$row1->[0] only in db 1\n" $row1 = $sth1->fetchrow_arrayref; } elsif ($row1->[0] gt $row2->[0]) { print "$row2->[0] only in db 2\n" $row2 = $sth2->fetchrow_arrayref; } else { # a better equality check for rows could be needed # if the db software is different or if the db # schemas are not identical: if ("@$row1" eq "@$row2") { print "row $row1->[0] differ\n" } $row1 = $sth1->fetchrow_arrayref; $row2 = $sth2->fetchrow_arrayref; } } while ($row1) { print "$row1->[0] only in db 1\n" $row1 = $sth1->fetchrow_arrayref; } while ($row2) { print "$row2->[0] only in db 2\n" $row2 = $sth2->fetchrow_arrayref; }
Re: Comparing databases - what is the best way?
by roboticus (Chancellor) on Apr 13, 2006 at 08:38 UTC
    Win:

    Your question isn't very clear. Are you trying to find out the difference between the structure or content of the two databases?

    It might be helpful to review How (Not) To Ask A Question

    --roboticus

Re: Comparing databases - what is the best way?
by jplindstrom (Monsignor) on Apr 13, 2006 at 09:34 UTC
    If what you want to do is to compare the table structure, SQL::Translator may work for you. Pretty impressive project. It has a sqlt-diff tool which can be used to create alter scripts between schemas.

    I researched this the other day, looking for a tool to help upgrading schemas, and my impression is that the capabilities seems a bit spotty, and the database support is somewhat uneven. If you use MySQL it may work just fine, if you use Oracle (like I do in the current project) or some other database, it may depend on what features you use in the schema. For example, the Oracle diff seemed to lack support for sequences and partitions, just to name a few things.

    (One other tool I looked at briefly is Embarcadero Change Manager, which looked very competent and feature-complete. It operates on db connections though, not SQL DDL statements which I would prefer.)

    /J

      It looks great. Unfortunately the support for SQL is in its early stages of development at present.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://543021]
Approved by holli
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.