compare two MySQL database structures

S

Sara

Hi All,

Just want to know if there is any straightforward solution in Perl to
compare two MySQL database structures.

I was able to get all the table names and the column names in the
database but want to know if this is the only way of doing it. I hope
there is a simpler way to directly convert the database structure
(without the data in it) to nested data structure, without looping
through each table.

I actually wrote a small program to change the mysqldump output to a
HoH so that it can be compared with another HoH using is_deeply method
in Test::More module.

# code segment
# subroutine for creating datastructure out of mysqldump output
sub get_struc($) {
my $file = shift; # the mysqldump file

my $string;
open my $fh, $file or die "Can't open file '$file': $!\n";
{ local $/; $string = <$fh>; close $fh }

my $re; # magic regex from perlre for matching braces
$re = qr{ \( (?:(?> [^\(\)]+ ) | (??{ $re }))* \) }x;

my @matches = $string =~ /(CREATE TABLE `[^`]+` $re)/g;

my %tables;
foreach (@matches) {
my ($tbl,$cols);
$tbl = $1, $cols = $2 if /CREATE TABLE `([^`]+)`
\(\n(.+)\n\)/s;
if ($cols =~ s/\s*PRIMARY KEY\s*\((.*?)\)\s*//) {
$tables{$tbl}{'primary_keys'} = $1;
}
foreach my $c (split /\n/, $cols) {
my ($col,$type);
$c =~ s/^\s+|\,\s*$//g;
$col = $1, $type = $2 if $c =~ /`([^`]+)` (.+)/;
$tables{$tbl}{$col} = $type;
}
}
return \%tables;
}

If there is any way to get the output of mysqldump from within Perl
even that would be fine.

I searched through documentation of DBI, DBD::mysql and DBI::FAQ but
wasn't able to find anything specific to this problem.

Any pointers would be of great use.

Thanks,
Sara
 
R

robic0

Hi All,

Just want to know if there is any straightforward solution in Perl to
compare two MySQL database structures.
I don't want to read your code unless you can properly explain the above
statement. What does the adjective 'straightfoward' mean in this question?

Fields are fields, columnar data is columnar data. Getting field names and
data are pretty much sql 101. If you want to extract names and data programatically,
thats also basic. You can extract all field names and compare that for a start,
if thats ok then move on to columnar data extraction.

Whats the big deal? Don't know how? If you don't know how, don't preface your
question insinuating you do...
 
R

robic0

I don't want to read your code unless you can properly explain the above
statement. What does the adjective 'straightfoward' mean in this question?

Fields are fields, columnar data is columnar data. Getting field names and
data are pretty much sql 101. If you want to extract names and data programatically,
thats also basic. You can extract all field names and compare that for a start,
if thats ok then move on to columnar data extraction.

Whats the big deal? Don't know how? If you don't know how, don't preface your
question insinuating you do...
I'm going to qualify the 'field names' statement. Whatever DBI you are using
should have that method. ADO should also.
 
S

sualeh.fatehi

Sara,

Does this project need to be done in Perl? The open-source
SchemaCrawler tool will do what you need. SchemaCrawler outputs details
of your schema (tables, views, procedures, and more) in a diff-able
plain-text format (text, CSV, or XHTML). SchemaCrawler can also output
data (including CLOBs and BLOBs) in the same plain-text formats. You
can use a standard diff program to diff the current output with a
reference version of the output. SchemaCrawler can be run either from
the command line, or as an ant task. A lot of examples are available
with the download to help you get started.

SchemaCrawler is free, open-source, cross-platform (operating system
and database) tool, written in Java, that is available at SourceForge:
http://schemacrawler.sourceforge.net/
You will need to provide a JDBC driver for your database. No other
third-party jars are required.

Sualeh Fatehi.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top