4 # -------------------------------------------------------------------
5 # $Id: sqlt-diff,v 1.1 2003-10-17 16:42:14 kycl4rk Exp $
6 # -------------------------------------------------------------------
7 # Copyright (C) 2002 The SQLFairy Authors
9 # This program is free software; you can redistribute it and/or
10 # modify it under the terms of the GNU General Public License as
11 # published by the Free Software Foundation; version 2.
13 # This program is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 # General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program; if not, write to the Free Software
20 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 # -------------------------------------------------------------------
26 sqlt-diff - find the differences b/w two schemas
34 For a list of all valid parsers:
40 sqlt-diff [options] file1=parser file2=parser
44 -d|--debug Show debugging info
48 This script is part of the SQL Fairy project. It will find the
49 differences between two schemas.
53 # -------------------------------------------------------------------
60 use vars qw( $VERSION );
61 $VERSION = sprintf "%d.%02d", q$Revision: 1.1 $ =~ /(\d+)\.(\d+)/;
63 my ( @input, $list, $help, $debug );
64 for my $arg ( @ARGV ) {
65 if ( $arg =~ m/^-?-l(ist)?$/ ) {
68 elsif ( $arg =~ m/^-?-h(elp)?$/ ) {
71 elsif ( $arg =~ m/^-?-d(ebug)?$/ ) {
74 elsif ( $arg =~ m/^([^=]+)=(.+)$/ ) {
75 push @input, { file => $1, parser => $2 };
78 pod2usage( msg => "Unknown argument '$arg'" );
82 pod2usage(1) if $help;
84 my $tr = SQL::Translator->new;
85 my @parsers = $tr->list_parsers;
86 my %valid_parsers = map { $_, 1 } @parsers;
89 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
94 pod2usage( msg => 'Too many file args' ) if @input > 2;
96 my ( $schema1, $schema2 );
98 for my $in ( @input ) {
99 my $file = $in->{'file'};
100 my $parser = $in->{'parser'};
102 die "Unable to read file '$file'\n" unless -r $file;
103 die "'$parser' is an invalid parser\n" unless $valid_parsers{ $parser };
105 my $t = SQL::Translator->new;
107 $t->parser( $parser ) or die $tr->error;
108 $t->producer( 'YAML' ) or die $tr->error;
109 my $out = $t->translate( $file ) or die $tr->error;
110 my $schema = $t->schema;
111 unless ( $schema->name ) {
112 $schema->name( $file );
124 #print "Schemas =\n", Dumper( \@schemas ), "\n" if $debug;
126 my @matrix = ( [ $schema1, $schema2 ], [ $schema2, $schema1 ] );
129 for my $rec ( @matrix ) {
132 my $s1_name = $s1->name;
133 my $s2_name = $s2->name;
134 print "Schema1 = '$s1_name', schema2 = '$s2_name'\n" if $debug;
135 for my $t1 ( $s1->get_tables ) {
136 my $t1_name = $t1->name;
137 my $t2 = $s2->get_table( $t1_name );
139 print "Checking '$s1_name' table '$t1_name'\n" if $debug;
141 push @diffs, "Schema '$s2_name' is missing table '$t1_name'";
145 my $t2_name = $t2->name;
146 for my $t1_field ( $t1->get_fields ) {
147 my $fname = $t1_field->name;
148 my $t2_field = $t2->get_field( $fname );
149 my $f1_full_name = "$s1_name.$t1_name.$fname";
150 print "Checking '$f1_full_name'\n" if $debug;
152 unless ( $t2_field ) {
154 "Table '$s2_name.$t2_name' is missing field '$fname'";
158 my $f2_full_name = "$s2_name.$t2_name.$fname";
159 my $t1_type = $t1_field->data_type;
160 my $t1_size = $t1_field->size;
161 my $t2_type = $t2_field->data_type;
162 my $t2_size = $t2_field->size;
164 if ( $t1_type ne $t2_type ) {
165 push @diffs, "'$f1_full_name' type = '$t1_type' and ".
166 "'$f2_full_name' type = '$t2_type'";
169 if ( defined $t1_size && ( $t1_size ne $t2_size ) ) {
170 push @diffs, "'$f1_full_name' size = '$t1_size' and ".
171 "'$f2_full_name' size = '$t2_size'";
178 print "Diffs\n-----\n";
179 print join( "\n", @diffs, '' );
182 print "There were no differences.\n";
185 # -------------------------------------------------------------------
186 # Bring out number weight & measure in a year of dearth.
188 # -------------------------------------------------------------------
194 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
198 SQL::Translator, L<http://sqlfairy.sourceforge.net>.