Skip to content

Commit 25c8d4a

Browse files
elianddbclaude
andcommitted
Eliminate duplicate logic in foreignKeyComparableTypes
Remove redundant switch case for string types since the mixed string type logic handles both same-type and mixed-type scenarios with identical charset validation logic. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 2b7ab56 commit 25c8d4a

File tree

1 file changed

+4
-10
lines changed

1 file changed

+4
-10
lines changed

sql/plan/alter_foreign_key.go

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -651,15 +651,9 @@ func foreignKeyComparableTypes(ctx *sql.Context, type1 sql.Type, type2 sql.Type)
651651
t1 := type1.Type()
652652
t2 := type2.Type()
653653

654-
// Same type with different parameters
654+
// Handle same-type cases for special types
655655
if t1 == t2 {
656656
switch t1 {
657-
case sqltypes.Char, sqltypes.VarChar, sqltypes.Binary, sqltypes.VarBinary:
658-
// There seems to be a special case where CHAR/VARCHAR/BINARY/VARBINARY can have unequal lengths.
659-
// Have not tested every type nor combination, but this seems specific to those 4 types.
660-
type1String := type1.(sql.StringType)
661-
type2String := type2.(sql.StringType)
662-
return type1String.Collation().CharacterSet() == type2String.Collation().CharacterSet()
663657
case sqltypes.Enum:
664658
// Enum types can reference each other in foreign keys regardless of their string values.
665659
// MySQL allows enum foreign keys to match based on underlying numeric values.
@@ -671,14 +665,14 @@ func foreignKeyComparableTypes(ctx *sql.Context, type1 sql.Type, type2 sql.Type)
671665
case sqltypes.Set:
672666
// MySQL allows set foreign keys to match based on underlying numeric values.
673667
return true
674-
default:
675-
return false
676668
}
677669
}
678670

679-
// Mixed string types: CHAR/VARCHAR or BINARY/VARBINARY
671+
// Handle string types (both same-type with different lengths and mixed types)
680672
if ((t1 == sqltypes.Char || t1 == sqltypes.VarChar) && (t2 == sqltypes.Char || t2 == sqltypes.VarChar)) ||
681673
((t1 == sqltypes.Binary || t1 == sqltypes.VarBinary) && (t2 == sqltypes.Binary || t2 == sqltypes.VarBinary)) {
674+
// There seems to be a special case where CHAR/VARCHAR/BINARY/VARBINARY can have unequal lengths.
675+
// Have not tested every type nor combination, but this seems specific to those 4 types.
682676
type1String := type1.(sql.StringType)
683677
type2String := type2.(sql.StringType)
684678
return type1String.Collation().CharacterSet() == type2String.Collation().CharacterSet()

0 commit comments

Comments
 (0)