usr bin env perl use strict use warnings ARGV or die Usage mysqldiff d

  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
#!/usr/bin/env perl
use strict;
use warnings;
@ARGV == 2 or die 'Usage: mysqldiff dbname1 dbname2';
my @tables = map {
my $scheme;
if (-e $_) {
# file
my $create_table = slurp($_);
my $tmp_dbname = join '_', 'tmp', time();
system(qw/mysqladmin -uroot create/, $tmp_dbname);
system('mysql', '-uroot', $tmp_dbname, '-e', $create_table);
my @cmd = (qw/mysqldump -uroot --no-data=true/, $tmp_dbname);
$scheme = `@cmd`;
system(qw/mysql -uroot/, $tmp_dbname, '-e', "drop database $tmp_dbname");
} elsif (/ /) {
# remote db
my @cmd = (qw/mysqldump --no-data=true/, split /\s+/, $_);
$scheme = `@cmd`;
my $tmp_dbname = join '_', 'tmp', time();
system(qw/mysqladmin -uroot create/, $tmp_dbname);
system('mysql', '-uroot', $tmp_dbname, '-e', $scheme);
@cmd = (qw/mysqldump -uroot --no-data=true/, $tmp_dbname);
$scheme = `@cmd`;
system(qw/mysql -uroot/, $tmp_dbname, '-e', "drop database $tmp_dbname");
} else {
# local db
my $dbname = $_;
my @cmd = (qw/mysqldump -uroot --no-data=true/, $dbname);
$scheme = `@cmd`;
}
parse($scheme);
} @ARGV;
print diff(@tables);
sub slurp {
my $filename = shift;
open my $fh, '<', $filename or die;
my $res = do { local $/; <$fh> };
close $fh;
$res;
}
sub parse {
my $tables = [];
my $scheme = shift or return $tables;
for ($scheme =~ /(CREATE TABLE .*? ENGINE[^;]*);/smg) {
my $content = $_;
$content =~ /`(.*?)`/ or next;
my $table_name = $1;
my ($columns, $unique_keys, $keys, $primary_keys);
for my $line (split /\n/, $content) {
$line =~ /^CREATE/ and next;
$line =~ /^\)/ and next;
if ($line =~ /^\s*PRIMARY KEY\s+\((.*)\)/) {
push @$primary_keys, $1;
} elsif ($line =~ /^\s*UNIQUE KEY\s+`(.*)`\s+\((.*)\)/) {
push @$unique_keys, {
name => $1,
column => $2,
};
} elsif ($line =~ /^\s*KEY\s+`(.*)`\s+\((.*)\)/) {
push @$keys, {
name => $1,
column => $2,
};
} elsif ($line =~ /^\s*`(.*?)`\s+(.+?)[\n,]?$/) {
push @$columns, {
column => $1,
definition => $2,
}
}
}
push @$tables, {
table_name => $table_name,
primary_keys => $primary_keys || [],
unique_keys => $unique_keys || [],
keys => $keys || [],
columns => $columns || [],
content => $content,
};
}
return $tables;
}
sub diff {
my ($old, $new) = @_;
my $diff = '';
my @old_table_names = sort map { $_->{table_name} } @$old;
my @new_table_names = sort map { $_->{table_name} } @$new;
my (%old_hash, %new_hash);
for (@$old) {
$old_hash{$_->{table_name}} = $_;
}
for (@$new) {
$new_hash{$_->{table_name}} = $_;
}
for my $name (@new_table_names) {
if ($old_hash{$name}) {
my $old_hash = $old_hash{$name};
my $new_hash = $new_hash{$name};
$diff .= table_diff($name, $old_hash, $new_hash);
} else {
$diff .= $new_hash{$name}->{content} . ";\n\n";
}
}
return $diff;
}
sub table_diff {
my ($name, $old, $new) = @_;
my @change = grep {$_} (
column_diff($old, $new),
key_diff($old, $new),
);
@change or return '';
return sprintf(
"ALTER TABLE `%s` %s;\n\n",
$name,
join(', ', @change),
);
}
sub column_diff {
my ($old, $new) = @_;
my $old_columns = $old->{columns};
my $new_columns = $new->{columns};
my (@change, %old_hash, %new_hash, %all_hash);
for (@$old_columns) {
$old_hash{$_->{column}} = $_;
$all_hash{$_->{column}} = $_;
}
for (@$new_columns) {
$new_hash{$_->{column}} = $_;
$all_hash{$_->{column}} = $_;
}
for my $column (sort keys %all_hash) {
my $old_definition = $old_hash{$column}->{definition};
my $new_definition = $new_hash{$column}->{definition};
if (!$old_definition) {
push @change, "ADD `$column` $new_definition";
} elsif (!$new_definition) {
push @change, "DROP `$column`";
} elsif ($old_definition ne $new_definition) {
push @change, "MODIFY `$column` $new_definition";
}
}
return @change;
}
sub key_diff {
my ($old, $new) = @_;
my @change;
{# key
my $old_keys = $old->{keys};
my $new_keys = $new->{keys};
my (%old_hash, %new_hash);
for (@$old_keys) {
$old_hash{$_->{column}} = 1;
}
for (@$new_keys) {
$new_hash{$_->{column}} = 1;
}
# add key
for my $key (@$new_keys) {
$old_hash{$key->{column}} and next;
my $name = join '_', map { s{[`()]}{}g; $_ } split /,/, $key->{column};
push @change, "ADD INDEX `$name` ($key->{column})";
}
# drop key
for my $key (@$old_keys) {
$new_hash{$key->{column}} and next;
push @change, "DROP INDEX `$key->{name}`";
}
};
{# unique_key
my $old_keys = $old->{unique_keys};
my $new_keys = $new->{unique_keys};
my (%old_hash, %new_hash);
for (@$old_keys) {
$old_hash{$_->{column}} = 1;
}
for (@$new_keys) {
$new_hash{$_->{column}} = 1;
}
# add unique_key
for my $key (@$new_keys) {
$old_hash{$key->{column}} and next;
my $name = join '_', map { s{[`()]}{}g; $_ } split /,/, $key->{column};
push @change, "ADD UNIQUE INDEX `$name` ($key->{column})";
}
# drop unique_key
for my $key (@$old_keys) {
$new_hash{$key->{column}} and next;
push @change, "DROP INDEX `$key->{name}`";
}
};
return @change;
}
__END__
=head1 NAME
mysqldiff - diff for mysql
=head1 SYNOPSIS
mysqldiff dbname1 dbname2
mysqldiff '-uroot -hlocalhost dbname1' '-uroot -hlocalhost dbname2'
mysqldiff createtable1.sql createtable2.sql
=cut