Merge pull request #264 from github/discard-foreign-keys
Discard foreign keys
This commit is contained in:
commit
4d903d0119
2
build.sh
2
build.sh
@ -2,7 +2,7 @@
|
||||
#
|
||||
#
|
||||
|
||||
RELEASE_VERSION="1.0.20"
|
||||
RELEASE_VERSION="1.0.21"
|
||||
|
||||
function build {
|
||||
osname=$1
|
||||
|
@ -57,6 +57,12 @@ This is somewhat similar to a Nagios `n`-times test, where `n` in our case is al
|
||||
|
||||
Optional. Default is `safe`. See more discussion in [cut-over](cut-over.md)
|
||||
|
||||
### discard-foreign-keys
|
||||
|
||||
**Danger**: this flag will _silently_ discard any foreign keys existing on your table.
|
||||
|
||||
At this time (10-2016) `gh-ost` does not support foreign keys on migrated tables (it bails out when it notices a FK on the migrated table). However, it is able to support _dropping_ of foreign keys via this flag. If you're trying to get rid of foreign keys in your environment, this is a useful flag.
|
||||
|
||||
### exact-rowcount
|
||||
|
||||
A `gh-ost` execution need to copy whatever rows you have in your existing table onto the ghost table. This can, and often be, a large number. Exactly what that number is?
|
||||
|
@ -70,6 +70,7 @@ type MigrationContext struct {
|
||||
ApproveRenamedColumns bool
|
||||
SkipRenamedColumns bool
|
||||
IsTungsten bool
|
||||
DiscardForeignKeys bool
|
||||
|
||||
config ContextConfig
|
||||
configMutex *sync.Mutex
|
||||
|
@ -61,6 +61,7 @@ func main() {
|
||||
flag.BoolVar(&migrationContext.ApproveRenamedColumns, "approve-renamed-columns", false, "in case your `ALTER` statement renames columns, gh-ost will note that and offer its interpretation of the rename. By default gh-ost does not proceed to execute. This flag approves that gh-ost's interpretation si correct")
|
||||
flag.BoolVar(&migrationContext.SkipRenamedColumns, "skip-renamed-columns", false, "in case your `ALTER` statement renames columns, gh-ost will note that and offer its interpretation of the rename. By default gh-ost does not proceed to execute. This flag tells gh-ost to skip the renamed columns, i.e. to treat what gh-ost thinks are renamed columns as unrelated columns. NOTE: you may lose column data")
|
||||
flag.BoolVar(&migrationContext.IsTungsten, "tungsten", false, "explicitly let gh-ost know that you are running on a tungsten-replication based topology (you are likely to also provide --assume-master-host)")
|
||||
flag.BoolVar(&migrationContext.DiscardForeignKeys, "discard-foreign-keys", false, "DANGER! This flag will migrate a table that has foreign keys and will NOT create foreign keys on the ghost table, thus your altered table will have NO foreign keys. This is useful for intentional dropping of foreign keys")
|
||||
|
||||
executeFlag := flag.Bool("execute", false, "actually execute the alter & migrate the table. Default is noop: do some tests and exit")
|
||||
flag.BoolVar(&migrationContext.TestOnReplica, "test-on-replica", false, "Have the migration run on a replica, not on the master. At the end of migration replication is stopped, and tables are swapped and immediately swap-revert. Replication remains stopped and you can compare the two tables for building trust")
|
||||
|
@ -63,7 +63,7 @@ func (this *Inspector) ValidateOriginalTable() (err error) {
|
||||
if err := this.validateTable(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := this.validateTableForeignKeys(); err != nil {
|
||||
if err := this.validateTableForeignKeys(this.migrationContext.DiscardForeignKeys); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := this.validateTableTriggers(); err != nil {
|
||||
@ -349,9 +349,11 @@ func (this *Inspector) validateTable() error {
|
||||
}
|
||||
|
||||
// validateTableForeignKeys makes sure no foreign keys exist on the migrated table
|
||||
func (this *Inspector) validateTableForeignKeys() error {
|
||||
func (this *Inspector) validateTableForeignKeys(allowChildForeignKeys bool) error {
|
||||
query := `
|
||||
SELECT TABLE_SCHEMA, TABLE_NAME
|
||||
SELECT
|
||||
SUM(REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_SCHEMA=? AND TABLE_NAME=?) as num_child_side_fk,
|
||||
SUM(REFERENCED_TABLE_NAME IS NOT NULL AND REFERENCED_TABLE_SCHEMA=? AND REFERENCED_TABLE_NAME=?) as num_parent_side_fk
|
||||
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
|
||||
WHERE
|
||||
REFERENCED_TABLE_NAME IS NOT NULL
|
||||
@ -359,24 +361,34 @@ func (this *Inspector) validateTableForeignKeys() error {
|
||||
OR (REFERENCED_TABLE_SCHEMA=? AND REFERENCED_TABLE_NAME=?)
|
||||
)
|
||||
`
|
||||
numForeignKeys := 0
|
||||
err := sqlutils.QueryRowsMap(this.db, query, func(rowMap sqlutils.RowMap) error {
|
||||
fkSchema := rowMap.GetString("TABLE_SCHEMA")
|
||||
fkTable := rowMap.GetString("TABLE_NAME")
|
||||
log.Infof("Found foreign key on %s.%s related to %s.%s", sql.EscapeName(fkSchema), sql.EscapeName(fkTable), sql.EscapeName(this.migrationContext.DatabaseName), sql.EscapeName(this.migrationContext.OriginalTableName))
|
||||
numForeignKeys++
|
||||
numParentForeignKeys := 0
|
||||
numChildForeignKeys := 0
|
||||
err := sqlutils.QueryRowsMap(this.db, query, func(m sqlutils.RowMap) error {
|
||||
numChildForeignKeys = m.GetInt("num_child_side_fk")
|
||||
numParentForeignKeys = m.GetInt("num_parent_side_fk")
|
||||
return nil
|
||||
},
|
||||
this.migrationContext.DatabaseName,
|
||||
this.migrationContext.OriginalTableName,
|
||||
this.migrationContext.DatabaseName,
|
||||
this.migrationContext.OriginalTableName,
|
||||
this.migrationContext.DatabaseName,
|
||||
this.migrationContext.OriginalTableName,
|
||||
this.migrationContext.DatabaseName,
|
||||
this.migrationContext.OriginalTableName,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if numForeignKeys > 0 {
|
||||
return log.Errorf("Found %d foreign keys related to %s.%s. Foreign keys are not supported. Bailing out", numForeignKeys, sql.EscapeName(this.migrationContext.DatabaseName), sql.EscapeName(this.migrationContext.OriginalTableName))
|
||||
if numParentForeignKeys > 0 {
|
||||
return log.Errorf("Found %d parent-side foreign keys on %s.%s. Parent-side foreign keys are not supported. Bailing out", numParentForeignKeys, sql.EscapeName(this.migrationContext.DatabaseName), sql.EscapeName(this.migrationContext.OriginalTableName))
|
||||
}
|
||||
if numChildForeignKeys > 0 {
|
||||
if allowChildForeignKeys {
|
||||
log.Debugf("Foreign keys found and will be dropped, as per given --discard-foreign-keys flag")
|
||||
return nil
|
||||
}
|
||||
return log.Errorf("Found %d child-side foreign keys on %s.%s. Child-side foreign keys are not supported. Bailing out", numChildForeignKeys, sql.EscapeName(this.migrationContext.DatabaseName), sql.EscapeName(this.migrationContext.OriginalTableName))
|
||||
}
|
||||
log.Debugf("Validated no foreign keys exist on table")
|
||||
return nil
|
||||
|
32
localtests/discard-fk/create.sql
Normal file
32
localtests/discard-fk/create.sql
Normal file
@ -0,0 +1,32 @@
|
||||
drop table if exists gh_ost_test_child;
|
||||
drop table if exists gh_ost_test;
|
||||
drop table if exists gh_ost_test_fk_parent;
|
||||
create table gh_ost_test_fk_parent (
|
||||
id int auto_increment,
|
||||
ts timestamp,
|
||||
primary key(id)
|
||||
);
|
||||
create table gh_ost_test (
|
||||
id int auto_increment,
|
||||
i int not null,
|
||||
parent_id int not null,
|
||||
primary key(id),
|
||||
constraint test_fk foreign key (parent_id) references gh_ost_test_fk_parent (id) on delete no action
|
||||
) auto_increment=1;
|
||||
|
||||
insert into gh_ost_test_fk_parent (id) values (1),(2),(3);
|
||||
|
||||
drop event if exists gh_ost_test;
|
||||
delimiter ;;
|
||||
create event gh_ost_test
|
||||
on schedule every 1 second
|
||||
starts current_timestamp
|
||||
ends current_timestamp + interval 60 second
|
||||
on completion not preserve
|
||||
enable
|
||||
do
|
||||
begin
|
||||
insert into gh_ost_test values (null, 11, 1);
|
||||
insert into gh_ost_test values (null, 13, 2);
|
||||
insert into gh_ost_test values (null, 17, 3);
|
||||
end ;;
|
1
localtests/discard-fk/extra_args
Normal file
1
localtests/discard-fk/extra_args
Normal file
@ -0,0 +1 @@
|
||||
--discard-foreign-keys
|
41
localtests/fail-fk-parent/create.sql
Normal file
41
localtests/fail-fk-parent/create.sql
Normal file
@ -0,0 +1,41 @@
|
||||
drop table if exists gh_ost_test_child;
|
||||
drop table if exists gh_ost_test;
|
||||
create table gh_ost_test (
|
||||
id int auto_increment,
|
||||
primary key(id)
|
||||
) engine=innodb auto_increment=1;
|
||||
|
||||
create table gh_ost_test_child (
|
||||
id int auto_increment,
|
||||
i int not null,
|
||||
parent_id int not null,
|
||||
constraint test_fk foreign key (parent_id) references gh_ost_test (id) on delete no action,
|
||||
primary key(id)
|
||||
) engine=innodb;
|
||||
insert into gh_ost_test (id) values (1),(2),(3);
|
||||
|
||||
drop event if exists gh_ost_test;
|
||||
drop event if exists gh_ost_test_cleanup;
|
||||
|
||||
delimiter ;;
|
||||
create event gh_ost_test
|
||||
on schedule every 1 second
|
||||
starts current_timestamp
|
||||
ends current_timestamp + interval 60 second
|
||||
on completion not preserve
|
||||
enable
|
||||
do
|
||||
begin
|
||||
insert into gh_ost_test_child values (null, 11, 1);
|
||||
insert into gh_ost_test_child values (null, 13, 2);
|
||||
insert into gh_ost_test_child values (null, 17, 3);
|
||||
end ;;
|
||||
|
||||
create event gh_ost_test_cleanup
|
||||
on schedule at current_timestamp + interval 60 second
|
||||
on completion not preserve
|
||||
enable
|
||||
do
|
||||
begin
|
||||
drop table if exists gh_ost_test_child;
|
||||
end ;;
|
0
localtests/fail-fk-parent/expect_failure
Normal file
0
localtests/fail-fk-parent/expect_failure
Normal file
1
localtests/fail-fk-parent/extra_args
Normal file
1
localtests/fail-fk-parent/extra_args
Normal file
@ -0,0 +1 @@
|
||||
--discard-foreign-keys
|
32
localtests/fail-fk/create.sql
Normal file
32
localtests/fail-fk/create.sql
Normal file
@ -0,0 +1,32 @@
|
||||
drop table if exists gh_ost_test_child;
|
||||
drop table if exists gh_ost_test;
|
||||
drop table if exists gh_ost_test_fk_parent;
|
||||
create table gh_ost_test_fk_parent (
|
||||
id int auto_increment,
|
||||
ts timestamp,
|
||||
primary key(id)
|
||||
);
|
||||
create table gh_ost_test (
|
||||
id int auto_increment,
|
||||
i int not null,
|
||||
parent_id int not null,
|
||||
primary key(id),
|
||||
constraint test_fk foreign key (parent_id) references gh_ost_test_fk_parent (id) on delete no action
|
||||
) auto_increment=1;
|
||||
|
||||
insert into gh_ost_test_fk_parent (id) values (1),(2),(3);
|
||||
|
||||
drop event if exists gh_ost_test;
|
||||
delimiter ;;
|
||||
create event gh_ost_test
|
||||
on schedule every 1 second
|
||||
starts current_timestamp
|
||||
ends current_timestamp + interval 60 second
|
||||
on completion not preserve
|
||||
enable
|
||||
do
|
||||
begin
|
||||
insert into gh_ost_test values (null, 11, 1);
|
||||
insert into gh_ost_test values (null, 13, 2);
|
||||
insert into gh_ost_test values (null, 17, 3);
|
||||
end ;;
|
0
localtests/fail-fk/expect_failure
Normal file
0
localtests/fail-fk/expect_failure
Normal file
@ -95,7 +95,18 @@ test_single() {
|
||||
echo_dot
|
||||
bash $exec_command_file 1> $test_logfile 2>&1
|
||||
|
||||
if [ $? -ne 0 ] ; then
|
||||
execution_result=$?
|
||||
|
||||
if [ -f $tests_path/$test_name/expect_failure ] ; then
|
||||
if [ $execution_result -eq 0 ] ; then
|
||||
echo
|
||||
echo "ERROR $test_name execution was expected to exit on error but did not. cat $test_logfile"
|
||||
return 1
|
||||
fi
|
||||
return 0
|
||||
fi
|
||||
|
||||
if [ $execution_result -ne 0 ] ; then
|
||||
echo
|
||||
echo "ERROR $test_name execution failure. cat $test_logfile"
|
||||
return 1
|
||||
|
Loading…
Reference in New Issue
Block a user