MCPcopy
hub / github.com/github/gh-ost / validateTableForeignKeys

Method validateTableForeignKeys

go/logic/inspect.go:500–549  ·  view source on GitHub ↗

validateTableForeignKeys makes sure no foreign keys exist on the migrated table

(allowChildForeignKeys bool)

Source from the content-addressed store, hash-verified

498
499// validateTableForeignKeys makes sure no foreign keys exist on the migrated table
500func (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
552func (isp *Inspector) validateTableTriggers() error {

Callers 1

ValidateOriginalTableMethod · 0.95

Calls 4

EscapeNameFunction · 0.92
WarningMethod · 0.65
ErrorfMethod · 0.65
DebugfMethod · 0.65

Tested by

no test coverage detected