Skip to content

Fixed issues when removing FK and other fixes #54

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 63 additions & 20 deletions percona-toolkit/plugins/ptosc_plugin_rename_fk.pl
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package pt_online_schema_change_plugin;

use Data::Dumper;
use strict;

my $table_name;
my $table_fks = {};
my $executed = 0;
my $OLD_table_fks = {};


sub get_fks {
my ( $self, $ddl, $opts ) = @_;
Expand Down Expand Up @@ -59,40 +62,80 @@ sub before_create_new_table {
my $fks = get_fks($self, $row->[1]);

while (my ($key, $val) = each (%$fks)) {
$table_fks->{"$key"} = {ddl => $val->{'ddl'}};
# print Dumper(\$val);
$OLD_table_fks->{"$key"} = {
name => $val->{'name'},
colnames => $val->{'colnames'}
};
}
}

sub after_drop_old_table {
# This is used for DEBUG purposes, can be removed
print ">>>>>> PLUGIN to rename the FK's to the original name executed after drop the old table\n";

my ($self, %args) = @_;
my $dbh = $self->{cxn}->dbh;
my $row = $dbh->selectrow_arrayref("SHOW CREATE TABLE $table_name");
my $fks = get_fks($self, $row->[1]);

# Here we start creating the SQL to rename the FK's
my $sql_fk = "ALTER TABLE $table_name \n";
# print Dumper(\$fks);
# Check if any FK in the new table
if ((keys %$fks) > 0) {
# This is used for DEBUG purposes, can be removed
print ">>>>>> PLUGIN to rename the FK's to the original name executed after drop the old table [after_drop_old_table]\n";
$executed = 1;
change_fk($dbh, $fks);
}
}

# Variable to control if we need add an extra ","
my $count = 0;
sub before_drop_triggers {
if ($executed == 0) {
my ($self, %args) = @_;
my $dbh = $self->{cxn}->dbh;
my $row = $dbh->selectrow_arrayref("SHOW CREATE TABLE $table_name");
my $fks = get_fks($self, $row->[1]);

# Check if any FK in the new table
if ((keys %$fks) > 0) {
# This is used for DEBUG purposes, can be removed
print ">>>>>> PLUGIN to rename the FK's to the original name executed after drop the old table [before_drop_triggers]\n";
$executed = 1;
change_fk($dbh, $fks);
}
}
}

# Loop the FK's to create the SQL
while (my ($key_name, $val) = each (%$fks)) {
if ($count++ > 0) {
$sql_fk .= ", \n";
sub change_fk {
my ($dbh, $fks) = @_;
# Here we start creating the SQL to rename the FK's
my $sql_fk = "ALTER TABLE $table_name \n";

# Variable to control if we need add an extra ","
my $count = 0;

# Loop the FK's to create the SQL
while (my ($key_name, $val) = each (%$fks)) {
if (exists $OLD_table_fks->{substr($key_name, 1)}) {
if ($count++ > 0) {
$sql_fk .= ", \n";
}

$sql_fk = $sql_fk . " DROP FOREIGN KEY `$key_name`, \n";
$sql_fk = $sql_fk . " ADD CONSTRAINT " . substr($key_name, 1) .
" FOREIGN KEY ( " . $val->{colnames} . ") " .
" REFERENCES " . $val->{parent_tblname} . " (" . $val->{parent_colnames} . ") ";
# print Dumper(\$OLD_table_fks)
}
}

$sql_fk = $sql_fk . " DROP FOREIGN KEY `$key_name`, \n";
$sql_fk = $sql_fk . " ADD " . $table_fks->{substr($key_name, 1)}{ddl};
}
# This will print the SQL to the console used for DEBUG purposes
print ">>>>>> PLUGIN SQL: \n$sql_fk\n";

# This will print the SQL to the console used for DEBUG purposes
print ">>>>>> PLUGIN SQL: \n$sql_fk\n";
# This line will effectively execute the SQL to rename (DROP and ADD) the FK's
$dbh->do($sql_fk);

}

# This line will effectively execute the SQL to rename (DROP and ADD) the FK's
$dbh->do($sql_fk);
sub test {

}

1;
Loading