validateTableForeignKeys makes sure no foreign keys exist on the migrated table
(allowChildForeignKeys bool)
| 498 | |
| 499 | // validateTableForeignKeys makes sure no foreign keys exist on the migrated table |
| 500 | func (isp *Inspector) validateTableForeignKeys(allowChildForeignKeys bool) error { |
| 501 | if isp.migrationContext.SkipForeignKeyChecks { |
| 502 | isp.migrationContext.Log.Warning("--skip-foreign-key-checks provided: will not check for foreign keys") |
| 503 | return nil |
| 504 | } |
| 505 | query := ` |
| 506 | SELECT /* gh-ost */ |
| 507 | SUM(REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_SCHEMA=? AND TABLE_NAME=?) as num_child_side_fk, |
| 508 | SUM(REFERENCED_TABLE_NAME IS NOT NULL AND REFERENCED_TABLE_SCHEMA=? AND REFERENCED_TABLE_NAME=?) as num_parent_side_fk |
| 509 | FROM |
| 510 | INFORMATION_SCHEMA.KEY_COLUMN_USAGE |
| 511 | WHERE |
| 512 | REFERENCED_TABLE_NAME IS NOT NULL |
| 513 | AND ( |
| 514 | (TABLE_SCHEMA=? AND TABLE_NAME=?) |
| 515 | OR |
| 516 | (REFERENCED_TABLE_SCHEMA=? AND REFERENCED_TABLE_NAME=?) |
| 517 | )` |
| 518 | numParentForeignKeys := 0 |
| 519 | numChildForeignKeys := 0 |
| 520 | err := sqlutils.QueryRowsMap(isp.db, query, func(m sqlutils.RowMap) error { |
| 521 | numChildForeignKeys = m.GetInt("num_child_side_fk") |
| 522 | numParentForeignKeys = m.GetInt("num_parent_side_fk") |
| 523 | return nil |
| 524 | }, |
| 525 | isp.migrationContext.DatabaseName, |
| 526 | isp.migrationContext.OriginalTableName, |
| 527 | isp.migrationContext.DatabaseName, |
| 528 | isp.migrationContext.OriginalTableName, |
| 529 | isp.migrationContext.DatabaseName, |
| 530 | isp.migrationContext.OriginalTableName, |
| 531 | isp.migrationContext.DatabaseName, |
| 532 | isp.migrationContext.OriginalTableName, |
| 533 | ) |
| 534 | if err != nil { |
| 535 | return err |
| 536 | } |
| 537 | if numParentForeignKeys > 0 { |
| 538 | return isp.migrationContext.Log.Errorf("found %d parent-side foreign keys on %s.%s. Parent-side foreign keys are not supported. Bailing out", numParentForeignKeys, sql.EscapeName(isp.migrationContext.DatabaseName), sql.EscapeName(isp.migrationContext.OriginalTableName)) |
| 539 | } |
| 540 | if numChildForeignKeys > 0 { |
| 541 | if allowChildForeignKeys { |
| 542 | isp.migrationContext.Log.Debugf("Foreign keys found and will be dropped, as per given --discard-foreign-keys flag") |
| 543 | return nil |
| 544 | } |
| 545 | return isp.migrationContext.Log.Errorf("found %d child-side foreign keys on %s.%s. Child-side foreign keys are not supported. Bailing out", numChildForeignKeys, sql.EscapeName(isp.migrationContext.DatabaseName), sql.EscapeName(isp.migrationContext.OriginalTableName)) |
| 546 | } |
| 547 | isp.migrationContext.Log.Debugf("Validated no foreign keys exist on table") |
| 548 | return nil |
| 549 | } |
| 550 | |
| 551 | // validateTableTriggers makes sure no triggers exist on the migrated table. if --include_triggers is used then it fetches the triggers |
| 552 | func (isp *Inspector) validateTableTriggers() error { |
no test coverage detected