4 # -------------------------------------------------------------------
5 # $Id: sqlt-diff 1440 2009-01-17 16:31:57Z jawnsy $
6 # -------------------------------------------------------------------
7 # Copyright (C) 2002-2009 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] file_name1=parser1 file_name2=parser2
44 -d|--debug Show debugging info
45 -t|--trace Turn on tracing for Parse::RecDescent
46 -c|--case-insensitive Compare tables/columns case-insensitively
47 --ignore-index-names Ignore index name differences
48 --ignore-constraint-names Ignore constraint name differences
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
54 --ignore-view-sql Ignore view SQL differences
55 --ignore-proc-sql Ignore procedure SQL differences
56 --no-batch-alters Do not clump multile alters to the same table into a
57 single ALTER TABLE statement where possible.
61 sqlt-diff is a utility for creating a file of SQL commands necessary to
62 transform the first schema provided to the second. While not yet
63 exhaustive in its ability to mutate the entire schema, it will report the
70 Using the Producer class of the target (second) schema, any tables missing
71 in the first schema will be generated in their entirety (fields, constraints,
74 =item * Missing/altered fields
76 Any fields missing or altered between the two schemas will be reported
79 ALTER TABLE <table_name>
81 [CHANGE <field_name> <datatype> (<size>)] ;
83 =item * Missing/altered indices
85 Any indices missing or of a different type or on different fields will be
86 indicated. Indices that should be dropped will be reported as such:
88 DROP INDEX <index_name> ON <table_name> ;
90 An index of a different type or on different fields will be reported as a
93 CREATE [<index_type>] INDEX [<index_name>] ON <table_name>
94 ( <field_name>[,<field_name>] ) ;
98 ALTER, CREATE, DROP statements are created by
99 SQL::Translator::Producer::*, see there for support/problems.
101 Currently (v0.0900), only MySQL is supported by this code.
105 # -------------------------------------------------------------------
111 use SQL::Translator::Diff;
112 use SQL::Translator::Schema::Constants;
114 my ( @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, $no_batch_alters );
117 for my $arg ( @ARGV ) {
118 if ( $arg =~ m/^-?-l(ist)?$/ ) {
121 elsif ( $arg =~ m/^-?-h(elp)?$/ ) {
124 elsif ( $arg =~ m/^-?-d(ebug)?$/ ) {
127 elsif ( $arg =~ m/^-?-t(race)?$/ ) {
130 elsif ( $arg =~ m/^-?-c(ase-insensitive)?$/ ) {
133 elsif ( $arg =~ m/^--ignore-index-names$/ ) {
134 $ignore_index_names = 1;
136 elsif ( $arg =~ m/^--ignore-constraint-names$/ ) {
137 $ignore_constraint_names = 1;
139 elsif ( $arg =~ m/^--mysql-parser-version=(.+)$/ ) {
140 $mysql_parser_version = $1;
142 elsif ( $arg =~ m/^--output-db=(.+)$/ ) {
145 elsif ( $arg =~ m/^--ignore-view-sql$/ ) {
146 $ignore_view_sql = 1;
148 elsif ( $arg =~ m/^--ignore-proc-sql$/ ) {
149 $ignore_proc_sql = 1;
151 elsif ( $arg =~ m/^([^=]+)=(.+)$/ ) {
152 push @input, { file => $1, parser => $2 };
154 elsif ( $arg =~ m/^--no-batch-alters$/ ) {
155 $no_batch_alters = 1;
158 pod2usage( msg => "Unknown argument '$arg'" );
162 print STDERR <<'EOM';
163 This code is experimental, currently the new code only supports MySQL or
164 SQLite diffing. To add support for other databases, please patch the relevant
165 SQL::Translator::Producer:: module. If you need compatibility with the old
166 sqlt-diff, please use sqlt-diff-old, and look into helping us make this one
170 pod2usage(1) if $help || !@ARGV;
171 pod2usage('Please specify only two schemas to diff') if scalar @input > 2;
173 my $tr = SQL::Translator->new;
174 my @parsers = $tr->list_parsers;
175 my %valid_parsers = map { $_, 1 } @parsers;
178 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
183 pod2usage( msg => 'Too many file args' ) if @input > 2;
185 my ( $source_schema, $source_db, $target_schema, $target_db ) = map {
186 my $file = $_->{'file'};
187 my $parser = $_->{'parser'};
189 die "Unable to read file '$file'\n" unless -r $file;
190 die "'$parser' is an invalid parser\n" unless $valid_parsers{ $parser };
192 my $t = SQL::Translator->new(parser_args => {mysql_parser_version => $mysql_parser_version});
195 $t->parser( $parser ) or die $tr->error;
196 my $out = $t->translate( $file ) or die $tr->error;
197 my $schema = $t->schema;
198 unless ( $schema->name ) {
199 $schema->name( $file );
205 my $result = SQL::Translator::Diff::schema_diff($source_schema, $source_db,
206 $target_schema, $target_db,
207 { caseopt => $caseopt,
208 ignore_index_names => $ignore_index_names,
209 ignore_constraint_names => $ignore_constraint_names,
210 ignore_view_sql => $ignore_view_sql,
211 ignore_proc_sql => $ignore_proc_sql,
212 output_db => $output_db,
213 no_batch_alters => $no_batch_alters,
222 print "No differences found.";
225 # -------------------------------------------------------------------
226 # Bring out number weight & measure in a year of dearth.
228 # -------------------------------------------------------------------
234 Ken Y. Clark E<lt>kclark@cpan.orgE<gt>.
238 SQL::Translator, L<http://sqlfairy.sourceforge.net>.