4 # -------------------------------------------------------------------
5 # Copyright (C) 2002-2009 The SQLFairy Authors
7 # This program is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU General Public License as
9 # published by the Free Software Foundation; version 2.
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 # -------------------------------------------------------------------
24 sqlt-diff - find the differences b/w two schemas
32 For a list of all valid parsers:
38 sqlt-diff [options] file_name1=parser1 file_name2=parser2
42 -d|--debug Show debugging info
43 -t|--trace Turn on tracing for Parse::RecDescent
44 -c|--case-insensitive Compare tables/columns case-insensitively
45 --ignore-index-names Ignore index name differences
46 --ignore-constraint-names Ignore constraint name differences
47 --mysql_parser_version=<#####> Specify a target MySQL parser version
48 for dealing with /*! comments
49 --output-db=<Producer> This Producer will be used instead of one
50 corresponding to parser1 to format output
52 --ignore-view-sql Ignore view SQL differences
53 --ignore-proc-sql Ignore procedure SQL differences
54 --no-batch-alters Do not clump multile alters to the same table into a
55 single ALTER TABLE statement where possible.
59 sqlt-diff is a utility for creating a file of SQL commands necessary to
60 transform the first schema provided to the second. While not yet
61 exhaustive in its ability to mutate the entire schema, it will report the
68 Using the Producer class of the target (second) schema, any tables missing
69 in the first schema will be generated in their entirety (fields, constraints,
72 =item * Missing/altered fields
74 Any fields missing or altered between the two schemas will be reported
77 ALTER TABLE <table_name>
79 [CHANGE <field_name> <datatype> (<size>)] ;
81 =item * Missing/altered indices
83 Any indices missing or of a different type or on different fields will be
84 indicated. Indices that should be dropped will be reported as such:
86 DROP INDEX <index_name> ON <table_name> ;
88 An index of a different type or on different fields will be reported as a
91 CREATE [<index_type>] INDEX [<index_name>] ON <table_name>
92 ( <field_name>[,<field_name>] ) ;
96 ALTER, CREATE, DROP statements are created by
97 SQL::Translator::Producer::*, see there for support/problems.
99 Currently (v0.0900), only MySQL is supported by this code.
103 # -------------------------------------------------------------------
110 use SQL::Translator::Diff;
111 use SQL::Translator::Schema::Constants;
113 use vars qw( $VERSION );
116 my ( @input, $list, $help, $debug, $trace, $caseopt, $ignore_index_names,
117 $ignore_constraint_names, $output_db, $mysql_parser_version,
118 $ignore_view_sql, $ignore_proc_sql, $no_batch_alters );
119 for my $arg ( @ARGV ) {
120 if ( $arg =~ m/^-?-l(ist)?$/ ) {
123 elsif ( $arg =~ m/^-?-h(elp)?$/ ) {
126 elsif ( $arg =~ m/^-?-d(ebug)?$/ ) {
129 elsif ( $arg =~ m/^-?-t(race)?$/ ) {
132 elsif ( $arg =~ m/^-?-c(ase-insensitive)?$/ ) {
135 elsif ( $arg =~ m/^--ignore-index-names$/ ) {
136 $ignore_index_names = 1;
138 elsif ( $arg =~ m/^--ignore-constraint-names$/ ) {
139 $ignore_constraint_names = 1;
141 elsif ( $arg =~ m/^--mysql-parser-version=(.+)$/ ) {
142 $mysql_parser_version = $1;
144 elsif ( $arg =~ m/^--output-db=(.+)$/ ) {
147 elsif ( $arg =~ m/^--ignore-view-sql$/ ) {
148 $ignore_view_sql = 1;
150 elsif ( $arg =~ m/^--ignore-proc-sql$/ ) {
151 $ignore_proc_sql = 1;
153 elsif ( $arg =~ m/^([^=]+)=(.+)$/ ) {
154 push @input, { file => $1, parser => $2 };
156 elsif ( $arg =~ m/^--no-batch-alters$/ ) {
157 $no_batch_alters = 1;
160 pod2usage( msg => "Unknown argument '$arg'" );
164 print STDERR <<'EOM';
165 This code is experimental, currently the new code only supports MySQL or
166 SQLite diffing. To add support for other databases, please patch the relevant
167 SQL::Translator::Producer:: module. If you need compatibility with the old
168 sqlt-diff, please use sqlt-diff-old, and look into helping us make this one
172 pod2usage(1) if $help || !@ARGV;
173 pod2usage('Please specify only two schemas to diff') if scalar @input > 2;
175 my $tr = SQL::Translator->new;
176 my @parsers = $tr->list_parsers;
177 my %valid_parsers = map { $_, 1 } @parsers;
180 print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
185 pod2usage( msg => 'Too many file args' ) if @input > 2;
187 my ( $source_schema, $source_db, $target_schema, $target_db ) = map {
188 my $file = $_->{'file'};
189 my $parser = $_->{'parser'};
191 die "Unable to read file '$file'\n" unless -r $file;
192 die "'$parser' is an invalid parser\n" unless $valid_parsers{ $parser };
194 my $t = SQL::Translator->new(parser_args => {mysql_parser_version => $mysql_parser_version});
197 $t->parser( $parser ) or die $tr->error;
198 my $out = $t->translate( $file ) or die $tr->error;
199 my $schema = $t->schema;
200 unless ( $schema->name ) {
201 $schema->name( $file );
207 my $result = SQL::Translator::Diff::schema_diff($source_schema, $source_db,
208 $target_schema, $target_db,
209 { caseopt => $caseopt,
210 ignore_index_names => $ignore_index_names,
211 ignore_constraint_names => $ignore_constraint_names,
212 ignore_view_sql => $ignore_view_sql,
213 ignore_proc_sql => $ignore_proc_sql,
214 output_db => $output_db,
215 no_batch_alters => $no_batch_alters,
224 print "No differences found.";
227 # -------------------------------------------------------------------
228 # Bring out number weight & measure in a year of dearth.
230 # -------------------------------------------------------------------
236 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
240 SQL::Translator, L<http://sqlfairy.sourceforge.net>.