Skip to content

Commit 4bca6aa

Browse files
committed
Improve constraint handling and table modification logic
- Enhance constraint and column handling in DDL operations. - Improve test coverage for dropping constraints and columns with complex relationships. - Refactor code for clarity, including updates to function signatures and processing logic. Signed-off-by: katsumata <12413150+winor30@users.noreply.github.com>
1 parent 68edea3 commit 4bca6aa

File tree

2 files changed

+89
-10
lines changed

2 files changed

+89
-10
lines changed

internal/hammer/diff.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ func (g *Generator) generateDDLForDropConstraintIndexAndTable(table *Table) DDL
515515
g.dropedChangeStream = append(g.dropedChangeStream, identsToComparable(cs.Name))
516516
}
517517
}
518-
ddl.AppendDDL(g.generateDDLForDropNamedConstraintsMatchingPredicate(func(constraint *ast.TableConstraint) bool {
518+
ddl.AppendDDL(g.generateDDLForDropNamedConstraintsMatchingPredicate(func(_ *Table, constraint *ast.TableConstraint) bool {
519519
fk, ok := constraint.Constraint.(*ast.ForeignKey)
520520
if !ok {
521521
return false
@@ -677,20 +677,25 @@ func (g *Generator) generateDDLForColumns(from, to *Table) DDL {
677677
func (g *Generator) generateDDLForDropColumn(table *ast.Path, column *ast.Ident) DDL {
678678
ddl := DDL{}
679679

680-
ddl.AppendDDL(g.generateDDLForDropNamedConstraintsMatchingPredicate(func(constraint *ast.TableConstraint) bool {
680+
ddl.AppendDDL(g.generateDDLForDropNamedConstraintsMatchingPredicate(func(t *Table, constraint *ast.TableConstraint) bool {
681681
fk, ok := constraint.Constraint.(*ast.ForeignKey)
682682
if !ok {
683683
return false
684684
}
685-
for _, c := range fk.Columns {
686-
if identsToComparable(column) == identsToComparable(c) {
687-
return true
685+
686+
if identsToComparable(t.Name.Idents...) == identsToComparable(table.Idents...) {
687+
for _, c := range fk.Columns {
688+
if identsToComparable(column) == identsToComparable(c) {
689+
return true
690+
}
688691
}
689692
}
690693

691-
for _, refColumn := range fk.ReferenceColumns {
692-
if identsToComparable(column) == identsToComparable(refColumn) {
693-
return true
694+
if identsToComparable(fk.ReferenceTable.Idents...) == identsToComparable(table.Idents...) {
695+
for _, refColumn := range fk.ReferenceColumns {
696+
if identsToComparable(column) == identsToComparable(refColumn) {
697+
return true
698+
}
694699
}
695700
}
696701

@@ -1239,12 +1244,12 @@ func (g *Generator) findSearchIndexByColumn(indexes []*ast.CreateSearchIndex, co
12391244
return result
12401245
}
12411246

1242-
func (g *Generator) generateDDLForDropNamedConstraintsMatchingPredicate(predicate func(constraint *ast.TableConstraint) bool) DDL {
1247+
func (g *Generator) generateDDLForDropNamedConstraintsMatchingPredicate(predicate func(table *Table, constraint *ast.TableConstraint) bool) DDL {
12431248
ddl := DDL{}
12441249

12451250
for _, table := range g.from.tables {
12461251
for _, constraint := range table.TableConstraints {
1247-
if predicate(constraint) {
1252+
if predicate(table, constraint) {
12481253
ddl.AppendDDL(g.generateDDLForDropNamedConstraint(table.Name, constraint))
12491254
}
12501255
}

internal/hammer/diff_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,80 @@ CREATE TABLE t2 (
10401040
`ALTER TABLE t2 DROP COLUMN t2_2`,
10411041
},
10421042
},
1043+
{
1044+
name: "Do not drop unrelated constraint (same column name)",
1045+
from: `
1046+
CREATE TABLE t1 (
1047+
t1_1 INT64 NOT NULL,
1048+
t1_2 INT64,
1049+
) PRIMARY KEY(t1_1);
1050+
1051+
CREATE TABLE t2 (
1052+
t2_1 INT64 NOT NULL,
1053+
) PRIMARY KEY(t2_1);
1054+
1055+
CREATE TABLE t3 (
1056+
t3_1 INT64 NOT NULL,
1057+
t1_2 INT64,
1058+
CONSTRAINT FK_t3 FOREIGN KEY (t1_2) REFERENCES t2 (t2_1),
1059+
) PRIMARY KEY(t3_1);
1060+
`,
1061+
to: `
1062+
CREATE TABLE t1 (
1063+
t1_1 INT64 NOT NULL,
1064+
) PRIMARY KEY(t1_1);
1065+
1066+
CREATE TABLE t2 (
1067+
t2_1 INT64 NOT NULL,
1068+
) PRIMARY KEY(t2_1);
1069+
1070+
CREATE TABLE t3 (
1071+
t3_1 INT64 NOT NULL,
1072+
t1_2 INT64,
1073+
CONSTRAINT FK_t3 FOREIGN KEY (t1_2) REFERENCES t2 (t2_1),
1074+
) PRIMARY KEY(t3_1);
1075+
`,
1076+
expected: []string{
1077+
`ALTER TABLE t1 DROP COLUMN t1_2`,
1078+
},
1079+
},
1080+
{
1081+
name: "Do not drop unrelated constraint (same referenced column name)",
1082+
from: `
1083+
CREATE TABLE t1 (
1084+
t1_1 INT64 NOT NULL,
1085+
t1_2 INT64,
1086+
) PRIMARY KEY(t1_1);
1087+
1088+
CREATE TABLE t2 (
1089+
t1_2 INT64 NOT NULL,
1090+
) PRIMARY KEY(t1_2);
1091+
1092+
CREATE TABLE t3 (
1093+
t3_1 INT64 NOT NULL,
1094+
t1_2 INT64,
1095+
CONSTRAINT FK_t3 FOREIGN KEY (t1_2) REFERENCES t2 (t1_2),
1096+
) PRIMARY KEY(t3_1);
1097+
`,
1098+
to: `
1099+
CREATE TABLE t1 (
1100+
t1_1 INT64 NOT NULL,
1101+
) PRIMARY KEY(t1_1);
1102+
1103+
CREATE TABLE t2 (
1104+
t1_2 INT64 NOT NULL,
1105+
) PRIMARY KEY(t1_2);
1106+
1107+
CREATE TABLE t3 (
1108+
t3_1 INT64 NOT NULL,
1109+
t1_2 INT64,
1110+
CONSTRAINT FK_t3 FOREIGN KEY (t1_2) REFERENCES t2 (t1_2),
1111+
) PRIMARY KEY(t3_1);
1112+
`,
1113+
expected: []string{
1114+
`ALTER TABLE t1 DROP COLUMN t1_2`,
1115+
},
1116+
},
10431117
{
10441118
name: "Drop constraint referencing dropped table.",
10451119
from: `

0 commit comments

Comments
 (0)