Skip to content

Commit 1f65473

Browse files
author
Shlomi Noach
committed
support for --discard-foreign-keys
This flag makes migration silently and happily discard any existing foreign keys on migrated table. This is useful for intentional dropping of foreign keys, as gh-ost does not otherwise have support for foreign key migration. At some time in the future gh-ost may support foreign key migration, at which time this flag will be removed
1 parent c749edc commit 1f65473

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

go/base/context.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ type MigrationContext struct {
7070
ApproveRenamedColumns bool
7171
SkipRenamedColumns bool
7272
IsTungsten bool
73+
DiscardForeignKeys bool
7374

7475
config ContextConfig
7576
configMutex *sync.Mutex

go/cmd/gh-ost/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ func main() {
6161
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")
6262
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")
6363
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)")
64+
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")
6465

6566
executeFlag := flag.Bool("execute", false, "actually execute the alter & migrate the table. Default is noop: do some tests and exit")
6667
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")

go/logic/inspect.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,11 +375,15 @@ func (this *Inspector) validateTableForeignKeys() error {
375375
if err != nil {
376376
return err
377377
}
378-
if numForeignKeys > 0 {
379-
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))
378+
if numForeignKeys == 0 {
379+
log.Debugf("Validated no foreign keys exist on table")
380+
return nil
380381
}
381-
log.Debugf("Validated no foreign keys exist on table")
382-
return nil
382+
if this.migrationContext.DiscardForeignKeys {
383+
log.Debugf("Foreign keys found and will be dropped, as per given --discard-foreign-keys flag")
384+
return nil
385+
}
386+
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))
383387
}
384388

385389
// validateTableTriggers makes sure no triggers exist on the migrated table

0 commit comments

Comments
 (0)