Skip to content

Commit

Permalink
DROP KEY(UNIQUE KEY含む)に対応
Browse files Browse the repository at this point in the history
  • Loading branch information
hisaichi5518 committed May 28, 2012
1 parent a5354d7 commit dc45770
Showing 1 changed file with 32 additions and 17 deletions.
49 changes: 32 additions & 17 deletions mysqldiff
Expand Up @@ -2,9 +2,6 @@
use strict;
use warnings;

# TODO
# - drop key

@ARGV == 2 or die 'Usage: mysqldiff dbname1 dbname2';

my @tables = map {
Expand Down Expand Up @@ -60,10 +57,16 @@ sub parse {
$line =~ /^\)/ and next;
if ($line =~ /^\s*PRIMARY KEY\s+\((.*)\)/) {
push @$primary_keys, $1;
} elsif ($line =~ /^\s*UNIQUE KEY.*?\((.*)\)/) {
push @$unique_keys, $1;
} elsif ($line =~ /^\s*KEY.*?\((.*)\)/) {
push @$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,
Expand Down Expand Up @@ -159,15 +162,21 @@ sub key_diff {
my $new_keys = $new->{keys};
my (%old_hash, %new_hash);
for (@$old_keys) {
$old_hash{$_} = 1;
$old_hash{$_->{column}} = 1;
}
for (@$new_keys) {
$new_hash{$_} = 1;
$new_hash{$_->{column}} = 1;
}
# add key
for my $key (@$new_keys) {
$old_hash{$key} and next;
my $name = join '_', map { s{[`()]}{}g; $_ } split /,/, $key;
push @change, "ADD INDEX `$name` ($key)";
$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}`";
}
};

Expand All @@ -176,15 +185,21 @@ sub key_diff {
my $new_keys = $new->{unique_keys};
my (%old_hash, %new_hash);
for (@$old_keys) {
$old_hash{$_} = 1;
$old_hash{$_->{column}} = 1;
}
for (@$new_keys) {
$new_hash{$_} = 1;
$new_hash{$_->{column}} = 1;
}
# add unique_key
for my $key (@$new_keys) {
$old_hash{$key} and next;
my $name = join '_', map { s{[`()]}{}g; $_ } split /,/, $key;
push @change, "ADD UNIQUE INDEX `$name` ($key)";
$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}`";
}
};

Expand Down

0 comments on commit dc45770

Please sign in to comment.