Skip to content

Commit 06215ac

Browse files
authored
schemadiff: additional FK column type matching rules (#14751)
Signed-off-by: Shlomi Noach <2607934+shlomi-noach@users.noreply.github.com>
1 parent 18181f6 commit 06215ac

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

go/vt/schemadiff/schema.go

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -350,12 +350,26 @@ func (s *Schema) normalize() error {
350350
return errors.Join(errs, err)
351351
}
352352
}
353-
colTypeEqualForForeignKey := func(a, b *sqlparser.ColumnType) bool {
354-
return a.Type == b.Type &&
355-
a.Unsigned == b.Unsigned &&
356-
a.Zerofill == b.Zerofill &&
357-
sqlparser.Equals.ColumnCharset(a.Charset, b.Charset) &&
358-
sqlparser.Equals.SliceOfString(a.EnumValues, b.EnumValues)
353+
colTypeCompatibleForForeignKey := func(child, parent *sqlparser.ColumnType) bool {
354+
if child.Type == parent.Type {
355+
return true
356+
}
357+
if child.Type == "char" && parent.Type == "varchar" {
358+
return true
359+
}
360+
return false
361+
}
362+
colTypeEqualForForeignKey := func(child, parent *sqlparser.ColumnType) bool {
363+
if colTypeCompatibleForForeignKey(child, parent) &&
364+
child.Unsigned == parent.Unsigned &&
365+
child.Zerofill == parent.Zerofill &&
366+
sqlparser.Equals.ColumnCharset(child.Charset, parent.Charset) &&
367+
child.Options.Collate == parent.Options.Collate &&
368+
sqlparser.Equals.SliceOfString(child.EnumValues, parent.EnumValues) {
369+
// Complete identify (other than precision which is ignored)
370+
return true
371+
}
372+
return false
359373
}
360374

361375
// Now validate foreign key columns:

go/vt/schemadiff/schema_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,31 @@ func TestInvalidSchema(t *testing.T) {
378378
// InnoDB allows different string length
379379
schema: "create table t10(id varchar(50) primary key); create table t11 (id int primary key, i varchar(100), key ix(i), constraint f10 foreign key (i) references t10(id) on delete restrict)",
380380
},
381+
{
382+
// explicit charset/collation
383+
schema: "create table t10(id varchar(50) charset utf8mb4 collate utf8mb4_0900_ai_ci primary key); create table t11 (id int primary key, i varchar(100) charset utf8mb4 collate utf8mb4_0900_ai_ci, key ix(i), constraint f10 foreign key (i) references t10(id) on delete restrict)",
384+
},
385+
{
386+
// weirdly allowed: varchar->char
387+
schema: "create table t10(id varchar(50) charset utf8mb4 collate utf8mb4_0900_ai_ci primary key); create table t11 (id int primary key, i char(100) charset utf8mb4 collate utf8mb4_0900_ai_ci, key ix(i), constraint f10 foreign key (i) references t10(id) on delete restrict)",
388+
},
389+
{
390+
// but weirdly not allowed: char->varchar
391+
schema: "create table t10(id char(50) charset utf8mb4 collate utf8mb4_0900_ai_ci primary key); create table t11 (id int primary key, i varchar(50) charset utf8mb4 collate utf8mb4_0900_ai_ci, key ix(i), constraint f10 foreign key (i) references t10(id) on delete restrict)",
392+
expectErr: &ForeignKeyColumnTypeMismatchError{Table: "t11", Constraint: "f10", Column: "i", ReferencedTable: "t10", ReferencedColumn: "id"},
393+
},
381394
{
382395
schema: "create table t10(id varchar(50) charset utf8mb3 primary key); create table t11 (id int primary key, i varchar(100) charset utf8mb4, key ix(i), constraint f10 foreign key (i) references t10(id) on delete restrict)",
383396
expectErr: &ForeignKeyColumnTypeMismatchError{Table: "t11", Constraint: "f10", Column: "i", ReferencedTable: "t10", ReferencedColumn: "id"},
384397
},
398+
{
399+
schema: "create table t10(id varchar(50) charset utf8mb4 collate utf8mb4_0900_ai_ci primary key); create table t11 (id int primary key, i varchar(100) charset utf8mb4 collate utf8mb4_general_ci, key ix(i), constraint f10 foreign key (i) references t10(id) on delete restrict)",
400+
expectErr: &ForeignKeyColumnTypeMismatchError{Table: "t11", Constraint: "f10", Column: "i", ReferencedTable: "t10", ReferencedColumn: "id"},
401+
},
402+
{
403+
schema: "create table t10(id VARCHAR(50) charset utf8mb4 collate utf8mb4_0900_ai_ci primary key); create table t11 (id int primary key, i VARCHAR(100) charset utf8mb4 collate utf8mb4_general_ci, key ix(i), constraint f10 foreign key (i) references t10(id) on delete restrict)",
404+
expectErr: &ForeignKeyColumnTypeMismatchError{Table: "t11", Constraint: "f10", Column: "i", ReferencedTable: "t10", ReferencedColumn: "id"},
405+
},
385406
}
386407
for _, ts := range tt {
387408
t.Run(ts.schema, func(t *testing.T) {

0 commit comments

Comments
 (0)