fix(postgres): preserve timezone when editing timestamp precision

Closes #10518
This commit is contained in:
t8y2 authored and t8y2 committed 2026-09-29 00:47:45 +08:00
1 parent 4d85afd173
commit 914a97326c
3 files changed
+115

No files matched your search

@@ -202,6 +202,49 @@ describe("tableStructureEditorState", () => {
});
});
describe("PostgreSQL temporal precision", () => {
it("keeps the time-zone qualifier returned by PostgreSQL metadata", () => {
const drafts = createColumnDrafts(
[
{ name: "with_tz", data_type: "timestamp(6) with time zone", is_nullable: true, column_default: null, is_primary_key: false },
{ name: "without_tz", data_type: "timestamp(6) without time zone", is_nullable: true, column_default: null, is_primary_key: false },
],
"postgres",
);
expect(drafts.map((column) => column.dataType)).toEqual(["timestamp(6) with time zone", "timestamp(6) without time zone"]);
expect(drafts.map((column) => column.original?.data_type)).toEqual(drafts.map((column) => column.dataType));
expect(hasExistingColumnTypeChange(drafts)).toBe(false);
expect(dataTypeBaseInputValue("postgres", drafts[0]!.dataType)).toBe("timestamp with time zone");
expect(dataTypeBaseInputValue("postgres", drafts[1]!.dataType)).toBe("timestamp without time zone");
expect(dataTypeLengthInputValue("postgres", drafts[0]!.dataType)).toBe("6");
expect(dataTypeLengthInputValue("postgres", drafts[1]!.dataType)).toBe("6");
expect(combineDataTypeForDatabase("postgres", dataTypeBaseInputValue("postgres", drafts[0]!.dataType), "3")).toBe("timestamp(3) with time zone");
expect(combineDataTypeForDatabase("postgres", dataTypeBaseInputValue("postgres", drafts[1]!.dataType), "3")).toBe("timestamp(3) without time zone");
});
it("rebuilds qualified timestamp precision without changing time-zone semantics", () => {
expect(combineDataTypeForDatabase("postgres", "timestamp with time zone", "3")).toBe("timestamp(3) with time zone");
expect(combineDataTypeForDatabase("postgres", "timestamp without time zone", "3")).toBe("timestamp(3) without time zone");
expect(combineDataTypeForDatabase("postgres", "timestamptz", "3")).toBe("timestamptz(3)");
expect(combineDataTypeForDatabase("postgres", "timestamp", "3")).toBe("timestamp(3)");
});
it("does not reinterpret temporal arrays, domains, or user-defined types as scalar timestamps", () => {
expect(dataTypeBaseInputValue("postgres", "timestamp(6) with time zone[]")).toBe("timestamp with time zone[]");
expect(dataTypeLengthInputValue("postgres", "timestamp(6) with time zone[]")).toBe("");
expect(dataTypeBaseInputValue("postgres", "timestamptz(6)[][]")).toBe("timestamptz[][]");
expect(dataTypeLengthInputValue("postgres", "timestamptz(6)[][]")).toBe("");
const metadataTypes = ["audit.timestamp_domain", 'audit."timestamp"', "custom_timestamp(6)"];
const drafts = createColumnDrafts(
metadataTypes.map((dataType, index) => ({ name: `custom_${index}`, data_type: dataType, is_nullable: true, column_default: null, is_primary_key: false })),
"postgres",
);
expect(drafts.map((column) => column.dataType)).toEqual(metadataTypes);
});
});
it("keeps quoted mixed-case identifiers distinct when detecting copied-column duplicates", () => {
const postgresNames = new Set([tableStructureIdentifierComparisonKey("Foo", "postgres")]);
expect(postgresNames.has(tableStructureIdentifierComparisonKey("foo", "postgres"))).toBe(false);
@@ -1297,11 +1297,32 @@ export function splitDataType(raw: string): { baseType: string; params: string }
return { baseType, params };
}
function splitPostgresTemporalDataType(raw: string): { baseType: string; params: string } | null {
// format_type() places the precision before the time-zone qualifier. Keep
// that qualifier in the base selector so editing only the precision cannot
// silently turn timestamptz into timestamp. The anchored built-in names
// deliberately exclude domains and schema-qualified custom types.
const match = raw.trim().match(/^(TIME|TIMESTAMP|TIMETZ|TIMESTAMPTZ)\s*\(([^()]*)\)(?:\s+((?:WITH|WITHOUT)\s+TIME\s+ZONE))?((?:\s*\[\])*)$/i);
if (!match) return null;
const typeName = match[1]!;
const qualifier = match[3]?.replace(/\s+/g, " ");
if (qualifier && !/^(?:TIME|TIMESTAMP)$/i.test(typeName)) return null;
const arraySuffix = (match[4] ?? "").replace(/\s+/g, "");
return {
baseType: `${typeName}${qualifier ? ` ${qualifier}` : ""}${arraySuffix}`,
params: match[2]!.trim(),
};
}
function splitDataTypeForDatabase(dbType: DatabaseType | undefined, raw: string): { baseType: string; params: string } {
if (dbType === "duckdb") {
const parsed = splitDuckdbScalarDataType(raw);
if (parsed) return parsed;
}
if (dbType === "postgres") {
const parsed = splitPostgresTemporalDataType(raw);
if (parsed) return parsed;
}
if (dbType === "xugu") {
const match = raw.trim().match(/^(TIME|TIMESTAMP)\s*\(([^()]*)\)\s+WITH\s+TIME\s+ZONE$/i);
if (match) {
@@ -1466,6 +1487,10 @@ function combineQualifiedTemporalType(baseType: string, params: string, dbType:
const match = baseType.trim().match(/^(TIMESTAMP)\s+WITHOUT\s+TIME\s+ZONE$/i);
return match ? (params ? `${match[1]}(${params}) WITHOUT TIME ZONE` : baseType.trim()) : null;
}
if (dbType === "postgres") {
const match = baseType.trim().match(/^(TIME|TIMESTAMP)\s+((?:WITH|WITHOUT)\s+TIME\s+ZONE)$/i);
return match ? (params ? `${match[1]}(${params}) ${match[2]!.replace(/\s+/g, " ")}` : baseType.trim()) : null;
}
if (dbType !== "xugu") return null;
const match = baseType.trim().match(/^(TIME|TIMESTAMP)\s+WITH\s+TIME\s+ZONE$/i);
if (!match) return null;