fix(postgres): preserve timezone when editing timestamp precision
Closes #10518
This commit is contained in:
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", () => {
|
it("keeps quoted mixed-case identifiers distinct when detecting copied-column duplicates", () => {
|
||||||
const postgresNames = new Set([tableStructureIdentifierComparisonKey("Foo", "postgres")]);
|
const postgresNames = new Set([tableStructureIdentifierComparisonKey("Foo", "postgres")]);
|
||||||
expect(postgresNames.has(tableStructureIdentifierComparisonKey("foo", "postgres"))).toBe(false);
|
expect(postgresNames.has(tableStructureIdentifierComparisonKey("foo", "postgres"))).toBe(false);
|
||||||
|
|||||||
@@ -1297,11 +1297,32 @@ export function splitDataType(raw: string): { baseType: string; params: string }
|
|||||||
return { baseType, params };
|
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 } {
|
function splitDataTypeForDatabase(dbType: DatabaseType | undefined, raw: string): { baseType: string; params: string } {
|
||||||
if (dbType === "duckdb") {
|
if (dbType === "duckdb") {
|
||||||
const parsed = splitDuckdbScalarDataType(raw);
|
const parsed = splitDuckdbScalarDataType(raw);
|
||||||
if (parsed) return parsed;
|
if (parsed) return parsed;
|
||||||
}
|
}
|
||||||
|
if (dbType === "postgres") {
|
||||||
|
const parsed = splitPostgresTemporalDataType(raw);
|
||||||
|
if (parsed) return parsed;
|
||||||
|
}
|
||||||
if (dbType === "xugu") {
|
if (dbType === "xugu") {
|
||||||
const match = raw.trim().match(/^(TIME|TIMESTAMP)\s*\(([^()]*)\)\s+WITH\s+TIME\s+ZONE$/i);
|
const match = raw.trim().match(/^(TIME|TIMESTAMP)\s*\(([^()]*)\)\s+WITH\s+TIME\s+ZONE$/i);
|
||||||
if (match) {
|
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);
|
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;
|
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;
|
if (dbType !== "xugu") return null;
|
||||||
const match = baseType.trim().match(/^(TIME|TIMESTAMP)\s+WITH\s+TIME\s+ZONE$/i);
|
const match = baseType.trim().match(/^(TIME|TIMESTAMP)\s+WITH\s+TIME\s+ZONE$/i);
|
||||||
if (!match) return null;
|
if (!match) return null;
|
||||||
|
|||||||
@@ -784,6 +784,53 @@ fn builds_postgres_type_change_that_drops_default() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn builds_postgres_timestamp_precision_changes_without_losing_timezone_semantics() {
|
||||||
|
let mut with_timezone = column("with_timezone");
|
||||||
|
with_timezone.data_type = "timestamp(3) with time zone".to_string();
|
||||||
|
with_timezone.original = Some(ColumnInfo {
|
||||||
|
name: "with_timezone".to_string(),
|
||||||
|
data_type: "timestamp(6) with time zone".to_string(),
|
||||||
|
is_nullable: true,
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
let with_timezone_result = build_single_column_alter_sql(SingleColumnAlterSqlOptions {
|
||||||
|
database_type: Some(DatabaseType::Postgres),
|
||||||
|
driver_profile: None,
|
||||||
|
schema: Some("public".to_string()),
|
||||||
|
table_name: "events".to_string(),
|
||||||
|
column: with_timezone,
|
||||||
|
});
|
||||||
|
assert_eq!(
|
||||||
|
with_timezone_result.statements,
|
||||||
|
vec![
|
||||||
|
r#"ALTER TABLE "public"."events" ALTER COLUMN "with_timezone" TYPE timestamp(3) with time zone USING "with_timezone"::timestamp(3) with time zone;"#
|
||||||
|
]
|
||||||
|
);
|
||||||
|
|
||||||
|
let mut without_timezone = column("without_timezone");
|
||||||
|
without_timezone.data_type = "timestamp(3) without time zone".to_string();
|
||||||
|
without_timezone.original = Some(ColumnInfo {
|
||||||
|
name: "without_timezone".to_string(),
|
||||||
|
data_type: "timestamp(6) without time zone".to_string(),
|
||||||
|
is_nullable: true,
|
||||||
|
..Default::default()
|
||||||
|
});
|
||||||
|
let without_timezone_result = build_single_column_alter_sql(SingleColumnAlterSqlOptions {
|
||||||
|
database_type: Some(DatabaseType::Postgres),
|
||||||
|
driver_profile: None,
|
||||||
|
schema: Some("public".to_string()),
|
||||||
|
table_name: "events".to_string(),
|
||||||
|
column: without_timezone,
|
||||||
|
});
|
||||||
|
assert_eq!(
|
||||||
|
without_timezone_result.statements,
|
||||||
|
vec![
|
||||||
|
r#"ALTER TABLE "public"."events" ALTER COLUMN "without_timezone" TYPE timestamp(3) without time zone USING "without_timezone"::timestamp(3) without time zone;"#
|
||||||
|
]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn builds_xugu_timezone_temporal_precision_in_final_ddl() {
|
fn builds_xugu_timezone_temporal_precision_in_final_ddl() {
|
||||||
let mut local_time = column("local_time");
|
let mut local_time = column("local_time");
|
||||||
|
|||||||
Reference in new issue
Block a user