Ignore all TT test while TT is broken
[dbsrgits/SQL-Translator.git] / bin / sqlt-diff
CommitLineData
e12ca55a 1#!/usr/bin/perl -w
2# vim: set ft=perl:
3
4# -------------------------------------------------------------------
d1a895ce 5# $Id: sqlt-diff,v 1.18 2007-03-21 15:20:50 duality72 Exp $
e12ca55a 6# -------------------------------------------------------------------
daf4f623 7# Copyright (C) 2002-4 The SQLFairy Authors
e12ca55a 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
fb5ab406 40 sqlt-diff [options] file_name1=parser1 file_name2=parser2
e12ca55a 41
42Options:
43
44 -d|--debug Show debugging info
51ffe5ee 45 -t|--trace Turn on tracing for Parse::RecDescent
0b1b149e 46 -c|--case-insensitive Compare tables/columns case-insensitively
d990d84b 47 --ignore-index-names Ignore index name differences
48 --ignore-constraint-names Ignore constraint name differences
0b1b149e 49 --mysql_parser_version=<#####> Specify a target MySQL parser version
50 for dealing with /*! comments
51 --output-db=<Producer> This Producer will be used instead of one
52 corresponding to parser1 to format output
53 for new tables
d1a895ce 54 --ignore-view-sql Ignore view SQL differences
55 --ignore-proc-sql Ignore procedure SQL differences
e12ca55a 56
57=head1 DESCRIPTION
58
942485ea 59sqlt-diff is a utility for creating a file of SQL commands necessary to
60transform the first schema provided to the second. While not yet
61exhaustive in its ability to mutate the entire schema, it will report the
62following
63
64=over
65
66=item * New tables
67
68Using the Producer class of the target (second) schema, any tables missing
69in the first schema will be generated in their entirety (fields, constraints,
70indices).
71
72=item * Missing/altered fields
73
74Any fields missing or altered between the two schemas will be reported
75as:
76
77 ALTER TABLE <table_name>
78 [DROP <field_name>]
79 [CHANGE <field_name> <datatype> (<size>)] ;
80
81=item * Missing/altered indices
82
83Any indices missing or of a different type or on different fields will be
84indicated. Indices that should be dropped will be reported as such:
85
86 DROP INDEX <index_name> ON <table_name> ;
87
88An index of a different type or on different fields will be reported as a
89new index as such:
90
91 CREATE [<index_type>] INDEX [<index_name>] ON <table_name>
92 ( <field_name>[,<field_name>] ) ;
93
94=back
95
96"ALTER/DROP TABLE" and "CREATE INDEX" statements B<are not> generated by
97the Producer, unfortunately, and may require massaging before being passed to
98your target database.
e12ca55a 99
100=cut
101
102# -------------------------------------------------------------------
103
104use strict;
105use Pod::Usage;
106use Data::Dumper;
107use SQL::Translator;
51ffe5ee 108use SQL::Translator::Diff;
942485ea 109use SQL::Translator::Schema::Constants;
e12ca55a 110
111use vars qw( $VERSION );
d1a895ce 112$VERSION = sprintf "%d.%02d", q$Revision: 1.18 $ =~ /(\d+)\.(\d+)/;
e12ca55a 113
d1a895ce 114my ( @input, $list, $help, $debug, $trace, $caseopt, $ignore_index_names,
115 $ignore_constraint_names, $output_db, $mysql_parser_version,
116 $ignore_view_sql, $ignore_proc_sql );
e12ca55a 117for my $arg ( @ARGV ) {
118 if ( $arg =~ m/^-?-l(ist)?$/ ) {
119 $list = 1;
120 }
121 elsif ( $arg =~ m/^-?-h(elp)?$/ ) {
122 $help = 1;
123 }
124 elsif ( $arg =~ m/^-?-d(ebug)?$/ ) {
125 $debug = 1;
126 }
51ffe5ee 127 elsif ( $arg =~ m/^-?-t(race)?$/ ) {
128 $trace = 1;
129 }
d990d84b 130 elsif ( $arg =~ m/^-?-c(ase-insensitive)?$/ ) {
51ffe5ee 131 $caseopt = 1;
132 }
d990d84b 133 elsif ( $arg =~ m/^--ignore-index-names$/ ) {
134 $ignore_index_names = 1;
135 }
fb5ab406 136 elsif ( $arg =~ m/^--ignore-constraint-names$/ ) {
d990d84b 137 $ignore_constraint_names = 1;
138 }
0b1b149e 139 elsif ( $arg =~ m/^--mysql-parser-version=(.+)$/ ) {
140 $mysql_parser_version = $1;
141 }
fb5ab406 142 elsif ( $arg =~ m/^--output-db=(.+)$/ ) {
143 $output_db = $1;
144 }
d1a895ce 145 elsif ( $arg =~ m/^--ignore-view-sql$/ ) {
146 $ignore_view_sql = 1;
147 }
148 elsif ( $arg =~ m/^--ignore-proc-sql$/ ) {
149 $ignore_proc_sql = 1;
150 }
e12ca55a 151 elsif ( $arg =~ m/^([^=]+)=(.+)$/ ) {
152 push @input, { file => $1, parser => $2 };
153 }
154 else {
155 pod2usage( msg => "Unknown argument '$arg'" );
156 }
157}
158
51ffe5ee 159pod2usage(1) if $help || !@ARGV;
942485ea 160pod2usage('Please specify only two schemas to diff') if scalar @input > 2;
e12ca55a 161
162my $tr = SQL::Translator->new;
163my @parsers = $tr->list_parsers;
164my %valid_parsers = map { $_, 1 } @parsers;
165
166if ( $list ) {
167 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
168 print "\n";
169 exit(0);
170}
171
172pod2usage( msg => 'Too many file args' ) if @input > 2;
173
51ffe5ee 174my ( $source_schema, $source_db, $target_schema, $target_db ) = map {
175 my $file = $_->{'file'};
176 my $parser = $_->{'parser'};
e12ca55a 177
178 die "Unable to read file '$file'\n" unless -r $file;
179 die "'$parser' is an invalid parser\n" unless $valid_parsers{ $parser };
180
d1a895ce 181 my $t = SQL::Translator->new(parser_args => {mysql_parser_version => $mysql_parser_version});
e12ca55a 182 $t->debug( $debug );
51ffe5ee 183 $t->trace( $trace );
e12ca55a 184 $t->parser( $parser ) or die $tr->error;
e12ca55a 185 my $out = $t->translate( $file ) or die $tr->error;
186 my $schema = $t->schema;
187 unless ( $schema->name ) {
188 $schema->name( $file );
189 }
190
51ffe5ee 191 ($schema, $parser);
192} @input;
193
194my $result = SQL::Translator::Diff::schema_diff($source_schema, $source_db,
195 $target_schema, $target_db,
d990d84b 196 { caseopt => $caseopt,
197 ignore_index_names => $ignore_index_names,
198 ignore_constraint_names => $ignore_constraint_names,
d1a895ce 199 ignore_view_sql => $ignore_view_sql,
200 ignore_proc_sql => $ignore_proc_sql,
2d4796c7 201 output_db => $output_db,
d990d84b 202 debug => $debug,
203 trace => $trace });
51ffe5ee 204if($result)
205{
206 print $result;
e12ca55a 207}
51ffe5ee 208else
209{
210 print "No differences found.";
e12ca55a 211}
212
213# -------------------------------------------------------------------
214# Bring out number weight & measure in a year of dearth.
215# William Blake
216# -------------------------------------------------------------------
217
218=pod
219
220=head1 AUTHOR
221
222Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
223
224=head1 SEE ALSO
225
226SQL::Translator, L<http://sqlfairy.sourceforge.net>.
227
228=cut