Skip to content

Commit 12b93d5

Browse files
committed
Getting type transforms plugged into more places
1 parent 9c85746 commit 12b93d5

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

sql/analyzer/apply_foreign_keys.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@ func getForeignKeyReferences(ctx *sql.Context, a *Analyzer, tbl sql.ForeignKeyTa
273273
if err != nil {
274274
return nil, err
275275
}
276+
277+
typeTransforms, err := plan.GetChildParentTypeTransforms(tblSch, parentTbl.Schema(), fk)
278+
if err != nil {
279+
return nil, err
280+
}
281+
276282
var selfCols map[string]int
277283
if fk.IsSelfReferential() {
278284
selfCols = make(map[string]int)
@@ -284,11 +290,12 @@ func getForeignKeyReferences(ctx *sql.Context, a *Analyzer, tbl sql.ForeignKeyTa
284290
ForeignKey: fk,
285291
SelfCols: selfCols,
286292
RowMapper: plan.ForeignKeyRowMapper{
287-
Index: parentIndex,
288-
Updater: parentUpdater,
289-
SourceSch: tblSch,
290-
IndexPositions: indexPositions,
291-
AppendTypes: appendTypes,
293+
Index: parentIndex,
294+
Updater: parentUpdater,
295+
SourceSch: tblSch,
296+
TargetTypeTransforms: typeTransforms,
297+
IndexPositions: indexPositions,
298+
AppendTypes: appendTypes,
292299
},
293300
}
294301
}

sql/plan/alter_foreign_key.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,15 +199,22 @@ func ResolveForeignKey(ctx *sql.Context, tbl sql.ForeignKeyTable, refTbl sql.For
199199
selfCols[strings.ToLower(col.Name)] = i
200200
}
201201
}
202+
203+
typeTransforms, err := GetChildParentTypeTransforms(tbl.Schema(), refTbl.Schema(), fkDef)
204+
if err != nil {
205+
return err
206+
}
207+
202208
reference := &ForeignKeyReferenceHandler{
203209
ForeignKey: fkDef,
204210
SelfCols: selfCols,
205211
RowMapper: ForeignKeyRowMapper{
206-
Index: refTblIndex,
207-
Updater: refTbl.GetForeignKeyEditor(ctx),
208-
SourceSch: tbl.Schema(),
209-
IndexPositions: indexPositions,
210-
AppendTypes: appendTypes,
212+
Index: refTblIndex,
213+
Updater: refTbl.GetForeignKeyEditor(ctx),
214+
SourceSch: tbl.Schema(),
215+
TargetTypeTransforms: typeTransforms,
216+
IndexPositions: indexPositions,
217+
AppendTypes: appendTypes,
211218
},
212219
}
213220

@@ -531,7 +538,7 @@ func FindForeignKeyColMapping(
531538
localRowPos, ok := localSchPositionMap[colName]
532539
if !ok {
533540
// Will happen if a column is renamed that is referenced by a foreign key
534-
//TODO: enforce that renaming a column referenced by a foreign key updates that foreign key
541+
// TODO: enforce that renaming a column referenced by a foreign key updates that foreign key
535542
return nil, nil, fmt.Errorf("column `%s` in foreign key `%s` cannot be found",
536543
colName, fkName)
537544
}

sql/plan/foreign_key_editor.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,6 @@ func GetChildParentMapping(parentSch sql.Schema, childSch sql.Schema, fkDef sql.
566566
// GetChildParentTypeTransforms returs a set of functions to transform the value in the child table to the
567567
// corresponding type in the parent table, if necessary
568568
func GetChildParentTypeTransforms(parentSch sql.Schema, childSch sql.Schema, fkDef sql.ForeignKeyConstraint) ([]func(ctx *sql.Context, val any) (any, error), error) {
569-
570569
parentMap := make(map[string]int)
571570
for i, col := range parentSch {
572571
parentMap[strings.ToLower(col.Name)] = i

0 commit comments

Comments
 (0)