Skip to content

Commit e020435

Browse files
authored
fix: prioritize type overrides over column overrides correctly (#423)
* fix: prioritize type overrides over column overrides correctly Fix the logic for resolving custom type overrides to ensure that type overrides are applied correctly even when column overrides exist for unrelated columns. Previously, the code returned early when a column override was found, ignoring type overrides that should apply to the column's type. This caused incorrect early exit and improper type resolution. The fix changes the order of checks to first look for a column override for the specific column, and if none is found, then apply the type override for the column's type. Added a test to verify that type overrides are respected even when unrelated column overrides exist. * format * fix
1 parent 6d1a7dc commit e020435

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

.changeset/proud-ads-poke.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@ts-safeql/generate": patch
3+
---
4+
5+
fixed an issue where custom type was inferred incorrectly in conjunction with custom columns

packages/generate/src/generate-insert.test.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,25 @@ describe("INSERT validation", () => {
472472
`,
473473
});
474474
});
475+
476+
test("INSERT with type override in conjunction with unrelated column override should use custom type", async () => {
477+
await testQuery({
478+
options: {
479+
overrides: {
480+
types: { timestamptz: "Instant" },
481+
columns: { "tbl2.col": "CustomType" },
482+
},
483+
},
484+
schema: `
485+
CREATE TABLE IF NOT EXISTS tbl (
486+
col TIMESTAMPTZ NOT NULL
487+
);
488+
`,
489+
query: `
490+
INSERT INTO tbl (col) VALUES (NOW()) RETURNING *
491+
`,
492+
expected: [["col", { kind: "type", value: "Instant", type: "timestamptz" }]],
493+
unknownColumns: ["col"],
494+
});
495+
});
475496
});

packages/generate/src/generate.ts

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -479,36 +479,25 @@ function getResolvedTargetEntry(params: {
479479

480480
if (!pgType) return undefined;
481481

482-
if (params.context.overrides?.columns && params.context.overrides.columns.size > 0) {
483-
const override = params.context.overrides.columns
484-
.get(params.col.introspected?.tableName ?? "")
485-
?.get(params.col.described.name);
486-
487-
return fmap(
488-
override,
489-
(value): ResolvedTarget => ({ kind: "type", value, type: pgType.name }),
490-
);
491-
}
482+
const columnOverride = params?.context?.overrides?.columns
483+
.get(params.col.introspected?.tableName ?? "")
484+
?.get(params.col.described.name);
492485

493-
if (params.context.overrides?.types) {
494-
const override = params.context.overrides.types.get(pgType.name);
495-
496-
return fmap(
497-
override,
498-
({ value }): ResolvedTarget => ({ kind: "type", value, type: pgType.name }),
499-
);
486+
if (columnOverride !== undefined) {
487+
return { kind: "type", value: columnOverride, type: pgType.name } satisfies ResolvedTarget;
500488
}
501489

502-
if (params.context.overrides?.types === undefined || pgType === undefined) {
503-
return undefined;
504-
}
490+
const typeOverride = params?.context?.overrides?.types.get(pgType.name);
505491

506-
const override = params.context.overrides.types.get(pgType.name);
492+
if (typeOverride !== undefined) {
493+
return {
494+
kind: "type",
495+
value: typeOverride.value,
496+
type: pgType.name,
497+
} satisfies ResolvedTarget;
498+
}
507499

508-
return fmap(
509-
override,
510-
({ value }): ResolvedTarget => ({ kind: "type", value, type: pgType.name }),
511-
);
500+
return undefined;
512501
})();
513502

514503
const value = valueAsOverride ?? valueAsEnum ?? valueAsType;

0 commit comments

Comments
 (0)