Added manual.
[dbsrgits/SQL-Translator.git] / bin / sqlt-diff
CommitLineData
e12ca55a 1#!/usr/bin/perl -w
2# vim: set ft=perl:
3
4# -------------------------------------------------------------------
21f5bf40 5# $Id: sqlt-diff,v 1.2 2004-01-25 18:16:57 kycl4rk Exp $
e12ca55a 6# -------------------------------------------------------------------
7# Copyright (C) 2002 The SQLFairy Authors
8#
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.
12#
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.
17#
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
21# 02111-1307 USA
22# -------------------------------------------------------------------
23
24=head1 NAME
25
26sqlt-diff - find the differences b/w two schemas
27
28=head1 SYNOPSIS
29
30For help:
31
32 sqlt-diff -h|--help
33
34For a list of all valid parsers:
35
36 sqlt -l|--list
37
38To diff two schemas:
39
21f5bf40 40 sqlt-diff [options] file_name1=parser file_name2=parser
e12ca55a 41
42Options:
43
44 -d|--debug Show debugging info
45
46=head1 DESCRIPTION
47
48This script is part of the SQL Fairy project. It will find the
49differences between two schemas.
50
51=cut
52
53# -------------------------------------------------------------------
54
55use strict;
56use Pod::Usage;
57use Data::Dumper;
58use SQL::Translator;
59
60use vars qw( $VERSION );
21f5bf40 61$VERSION = sprintf "%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/;
e12ca55a 62
63my ( @input, $list, $help, $debug );
64for my $arg ( @ARGV ) {
65 if ( $arg =~ m/^-?-l(ist)?$/ ) {
66 $list = 1;
67 }
68 elsif ( $arg =~ m/^-?-h(elp)?$/ ) {
69 $help = 1;
70 }
71 elsif ( $arg =~ m/^-?-d(ebug)?$/ ) {
72 $debug = 1;
73 }
74 elsif ( $arg =~ m/^([^=]+)=(.+)$/ ) {
75 push @input, { file => $1, parser => $2 };
76 }
77 else {
78 pod2usage( msg => "Unknown argument '$arg'" );
79 }
80}
81
82pod2usage(1) if $help;
83
84my $tr = SQL::Translator->new;
85my @parsers = $tr->list_parsers;
86my %valid_parsers = map { $_, 1 } @parsers;
87
88if ( $list ) {
89 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
90 print "\n";
91 exit(0);
92}
93
94pod2usage( msg => 'Too many file args' ) if @input > 2;
95
96my ( $schema1, $schema2 );
97my $i = 1;
98for my $in ( @input ) {
99 my $file = $in->{'file'};
100 my $parser = $in->{'parser'};
101
102 die "Unable to read file '$file'\n" unless -r $file;
103 die "'$parser' is an invalid parser\n" unless $valid_parsers{ $parser };
104
105 my $t = SQL::Translator->new;
106 $t->debug( $debug );
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 );
113 }
114
115 if ( $i == 1 ) {
116 $schema1 = $schema;
117 }
118 else {
119 $schema2 = $schema;
120 }
121 $i++;
122}
123
124#print "Schemas =\n", Dumper( \@schemas ), "\n" if $debug;
125
126my @matrix = ( [ $schema1, $schema2 ], [ $schema2, $schema1 ] );
127
128my @diffs;
129for my $rec ( @matrix ) {
130 my $s1 = $rec->[0];
131 my $s2 = $rec->[1];
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 );
138
139 print "Checking '$s1_name' table '$t1_name'\n" if $debug;
140 unless ( $t2 ) {
141 push @diffs, "Schema '$s2_name' is missing table '$t1_name'";
142 next;
143 }
144
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;
151
152 unless ( $t2_field ) {
153 push @diffs,
154 "Table '$s2_name.$t2_name' is missing field '$fname'";
155 next;
156 }
157
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;
163
164 if ( $t1_type ne $t2_type ) {
165 push @diffs, "'$f1_full_name' type = '$t1_type' and ".
166 "'$f2_full_name' type = '$t2_type'";
167 }
168
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'";
172 }
173 }
174 }
175}
176
177if ( @diffs ) {
178 print "Diffs\n-----\n";
179 print join( "\n", @diffs, '' );
180}
181else {
182 print "There were no differences.\n";
183}
184
185# -------------------------------------------------------------------
186# Bring out number weight & measure in a year of dearth.
187# William Blake
188# -------------------------------------------------------------------
189
190=pod
191
192=head1 AUTHOR
193
194Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
195
196=head1 SEE ALSO
197
198SQL::Translator, L<http://sqlfairy.sourceforge.net>.
199
200=cut