forked from dbsrgits/sql-translator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlt-diff
executable file
·234 lines (181 loc) · 6.89 KB
/
sqlt-diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#!/usr/bin/env perl
# vim: set ft=perl:
# -------------------------------------------------------------------
# Copyright (C) 2002-2009 The SQLFairy Authors
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307 USA
# -------------------------------------------------------------------
=head1 NAME
sqlt-diff - find the differences b/w two schemas
=head1 SYNOPSIS
For help:
sqlt-diff -h|--help
For a list of all valid parsers:
sqlt -l|--list
To diff two schemas:
sqlt-diff [options] file_name1=parser1 file_name2=parser2
Options:
-d|--debug Show debugging info
-t|--trace Turn on tracing for Parse::RecDescent
-c|--case-insensitive Compare tables/columns case-insensitively
--ignore-index-names Ignore index name differences
--ignore-constraint-names Ignore constraint name differences
--mysql_parser_version=<#####> Specify a target MySQL parser version
for dealing with /*! comments
--output-db=<Producer> This Producer will be used instead of one
corresponding to parser1 to format output
for new tables
--ignore-view-sql Ignore view SQL differences
--ignore-proc-sql Ignore procedure SQL differences
--no-batch-alters Do not clump multile alters to the same table into a
single ALTER TABLE statement where possible.
--quote=<character> Use <character> to quote all table and field
names in statements
=head1 DESCRIPTION
sqlt-diff is a utility for creating a file of SQL commands necessary to
transform the first schema provided to the second. While not yet
exhaustive in its ability to mutate the entire schema, it will report the
following
=over
=item * New tables
Using the Producer class of the target (second) schema, any tables missing
in the first schema will be generated in their entirety (fields, constraints,
indices).
=item * Missing/altered fields
Any fields missing or altered between the two schemas will be reported
as:
ALTER TABLE <table_name>
[DROP <field_name>]
[CHANGE <field_name> <datatype> (<size>)] ;
=item * Missing/altered indices
Any indices missing or of a different type or on different fields will be
indicated. Indices that should be dropped will be reported as such:
DROP INDEX <index_name> ON <table_name> ;
An index of a different type or on different fields will be reported as a
new index as such:
CREATE [<index_type>] INDEX [<index_name>] ON <table_name>
( <field_name>[,<field_name>] ) ;
=back
ALTER, CREATE, DROP statements are created by
SQL::Translator::Producer::*, see there for support/problems.
Currently (v0.0900), only MySQL is supported by this code.
=cut
# -------------------------------------------------------------------
use strict;
use warnings;
use Pod::Usage;
use Data::Dumper;
use Getopt::Long;
use SQL::Translator;
use SQL::Translator::Diff;
use SQL::Translator::Schema::Constants;
use vars qw( $VERSION );
$VERSION = '1.59';
my ( @input, $list, $help, $debug, $trace, $caseopt, $ignore_index_names,
$ignore_constraint_names, $output_db, $mysql_parser_version,
$ignore_view_sql, $ignore_proc_sql, $no_batch_alters, $quote
);
GetOptions(
'l|list' => \$list,
'h|help' => \$help,
'd|debug' => \$debug,
't|trace' => \$trace,
'c|case-insensitive' => \$caseopt,
'ignore-index-names' => \$ignore_index_names,
'ignore-constraint-names' => \$ignore_constraint_names,
'mysql_parser_version:s' => \$mysql_parser_version,
'output-db:s' => \$output_db,
'ignore-view-sql' => \$ignore_view_sql,
'ignore-proc-sql' => \$ignore_proc_sql,
'quote:s' => \$quote,
'no-batch-alters' => \$no_batch_alters,
) or pod2usage(2);
for my $arg ( @ARGV ) {
if ( $arg =~ m/^([^=]+)=(.+)$/ ) {
push @input, { file => $1, parser => $2 };
}
}
print STDERR <<'EOM' unless $ENV{SQLT_NEWDIFF_NOWARN};
This code is experimental, currently the new code only supports MySQL or
SQLite diffing. To add support for other databases, please patch the relevant
SQL::Translator::Producer:: module. If you need compatibility with the old
sqlt-diff, please use sqlt-diff-old, and look into helping us make this one
work for you
EOM
my $tr = SQL::Translator->new;
my @parsers = $tr->list_parsers;
my %valid_parsers = map { $_, 1 } @parsers;
if ( $list ) {
print "\nParsers:\n", map { "\t$_\n" } sort @parsers;
print "\n";
exit(0);
}
pod2usage(1) if $help || !@input;
pod2usage(msg => 'Please specify two schemas to diff') if scalar @input != 2;
my ( $source_schema, $source_db, $target_schema, $target_db ) = map {
my $file = $_->{'file'};
my $parser = $_->{'parser'};
die "Unable to read file '$file'\n" unless -r $file;
die "'$parser' is an invalid parser\n" unless $valid_parsers{ $parser };
my $t = SQL::Translator->new(parser_args => {
mysql_parser_version => $mysql_parser_version
});
$t->debug( $debug );
$t->trace( $trace );
$t->parser( $parser ) or die $tr->error;
my $out = $t->translate( $file ) or die $tr->error;
my $schema = $t->schema;
unless ( $schema->name ) {
$schema->name( $file );
}
($schema, $parser);
} @input;
my $result = SQL::Translator::Diff::schema_diff(
$source_schema, $source_db,
$target_schema, $target_db,
{
caseopt => $caseopt || 0,
ignore_index_names => $ignore_index_names || 0,
ignore_constraint_names => $ignore_constraint_names || 0,
ignore_view_sql => $ignore_view_sql || 0,
ignore_proc_sql => $ignore_proc_sql || 0,
output_db => $output_db,
no_batch_alters => $no_batch_alters || 0,
debug => $debug || 0,
trace => $trace || 0,
producer_args => {
quote_table_names => $quote || '',
quote_field_names => $quote || '',
},
}
);
if($result)
{
print $result;
}
else
{
print "No differences found.";
}
# -------------------------------------------------------------------
# Bring out number weight & measure in a year of dearth.
# William Blake
# -------------------------------------------------------------------
=pod
=head1 AUTHOR
Ken Youens-Clark E<lt>[email protected]<gt>.
=head1 SEE ALSO
SQL::Translator, L<http://sqlfairy.sourceforge.net>.
=cut