Skip to content

Commit a4d566e

Browse files
author
Shlomi Noach
committed
counting child vs parent sie foreign keys on table,
parent-side foreign keys cannot be discarded
1 parent 56e0833 commit a4d566e

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

go/logic/inspect.go

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ func (this *Inspector) ValidateOriginalTable() (err error) {
6363
if err := this.validateTable(); err != nil {
6464
return err
6565
}
66-
if err := this.validateTableForeignKeys(); err != nil {
66+
if err := this.validateTableForeignKeys(this.migrationContext.DiscardForeignKeys); err != nil {
6767
return err
6868
}
6969
if err := this.validateTableTriggers(); err != nil {
@@ -349,41 +349,49 @@ func (this *Inspector) validateTable() error {
349349
}
350350

351351
// validateTableForeignKeys makes sure no foreign keys exist on the migrated table
352-
func (this *Inspector) validateTableForeignKeys() error {
352+
func (this *Inspector) validateTableForeignKeys(allowChildForeignKeys bool) error {
353353
query := `
354-
SELECT TABLE_SCHEMA, TABLE_NAME
354+
SELECT
355+
SUM(REFERENCED_TABLE_NAME IS NOT NULL AND TABLE_SCHEMA=? AND TABLE_NAME=?) as num_child_side_fk,
356+
SUM(REFERENCED_TABLE_NAME IS NOT NULL AND REFERENCED_TABLE_SCHEMA=? AND REFERENCED_TABLE_NAME=?) as num_parent_side_fk
355357
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
356358
WHERE
357359
REFERENCED_TABLE_NAME IS NOT NULL
358360
AND ((TABLE_SCHEMA=? AND TABLE_NAME=?)
359361
OR (REFERENCED_TABLE_SCHEMA=? AND REFERENCED_TABLE_NAME=?)
360362
)
361363
`
362-
numForeignKeys := 0
363-
err := sqlutils.QueryRowsMap(this.db, query, func(rowMap sqlutils.RowMap) error {
364-
fkSchema := rowMap.GetString("TABLE_SCHEMA")
365-
fkTable := rowMap.GetString("TABLE_NAME")
366-
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))
367-
numForeignKeys++
364+
numParentForeignKeys := 0
365+
numChildForeignKeys := 0
366+
err := sqlutils.QueryRowsMap(this.db, query, func(m sqlutils.RowMap) error {
367+
numChildForeignKeys = m.GetInt("num_child_side_fk")
368+
numParentForeignKeys = m.GetInt("num_parent_side_fk")
368369
return nil
369370
},
370371
this.migrationContext.DatabaseName,
371372
this.migrationContext.OriginalTableName,
372373
this.migrationContext.DatabaseName,
373374
this.migrationContext.OriginalTableName,
375+
this.migrationContext.DatabaseName,
376+
this.migrationContext.OriginalTableName,
377+
this.migrationContext.DatabaseName,
378+
this.migrationContext.OriginalTableName,
374379
)
375380
if err != nil {
376381
return err
377382
}
378-
if numForeignKeys == 0 {
379-
log.Debugf("Validated no foreign keys exist on table")
380-
return nil
383+
if numParentForeignKeys > 0 {
384+
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))
381385
}
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
386+
if numChildForeignKeys > 0 {
387+
if allowChildForeignKeys {
388+
log.Debugf("Foreign keys found and will be dropped, as per given --discard-foreign-keys flag")
389+
return nil
390+
}
391+
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))
385392
}
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))
393+
log.Debugf("Validated no foreign keys exist on table")
394+
return nil
387395
}
388396

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

0 commit comments

Comments
 (0)