fix(postgres): support array types when adding columns

This commit is contained in:
zipg
2026-08-08 11:13:42 +08:00
committed by GitHub
parent 36e5fa4f4c
commit 50c42d8935
2 changed files with 25 additions and 3 deletions
@@ -5,6 +5,12 @@ export function hasExistingColumnTypeChange(columns: readonly EditableStructureC
return columns.some((column) => !!column.original && !column.markedForDrop && column.dataType !== column.original.data_type);
}
const POSTGRES_SERIAL_PSEUDO_TYPES = new Set(["smallserial", "serial", "bigserial"]);
function withPostgresArrayTypes(types: readonly string[]): string[] {
return [...types, ...types.filter((type) => !POSTGRES_SERIAL_PSEUDO_TYPES.has(type)).map((type) => `${type}[]`)];
}
export const DATA_TYPE_OPTIONS: Record<string, string[]> = {
mysql: [
"tinyint",
@@ -58,7 +64,7 @@ export const DATA_TYPE_OPTIONS: Record<string, string[]> = {
"multipolygon",
"geometrycollection",
],
postgres: [
postgres: withPostgresArrayTypes([
"smallint",
"int2",
"integer",
@@ -122,7 +128,7 @@ export const DATA_TYPE_OPTIONS: Record<string, string[]> = {
"tstzrange",
"daterange",
"oid",
],
]),
sqlite: ["integer", "real", "text", "blob", "numeric"],
rqlite: ["integer", "real", "text", "blob", "numeric"],
turso: ["integer", "real", "text", "blob", "numeric"],
@@ -1161,7 +1167,7 @@ export function isDataTypeLengthDisabled(_dbType: DatabaseType | undefined, base
} else if (_dbType === "manticoresearch") {
return key !== "bit" && key !== "float_vector";
} else if (_dbType === "postgres" || _dbType === "gaussdb" || _dbType === "kwdb" || _dbType === "opengauss" || _dbType === "highgo" || _dbType === "uxdb" || _dbType === "vastbase" || _dbType === "kingbase") {
return POSTGRES_TYPE_LENGTH_DISABLES.includes(key);
return key.endsWith("[]") || POSTGRES_TYPE_LENGTH_DISABLES.includes(key);
} else if (isOracleLikeStructureType(_dbType)) {
// Dameng/Oracle integer aliases have fixed precision; MySQL-style display widths generate invalid DDL.
return ORACLE_LIKE_TYPE_LENGTH_DISABLES.includes(key);
+16
View File
@@ -14,6 +14,7 @@ import {
generateUniqueIndexName,
getColumnEditorControls,
getDataTypeOptions,
isDataTypeLengthDisabled,
isProtectedManticoreIdColumn,
isDamengIdentityCompatibleDataType,
isMysqlEnumDataType,
@@ -485,6 +486,21 @@ test("returns data type options for compatible table structure editors", () => {
assert.equal(getDataTypeOptions("sqlserver").includes("nvarchar"), true);
});
test("returns PostgreSQL array type options without serial pseudo-types", () => {
const options = getDataTypeOptions("postgres");
assert.equal(options.includes("integer[]"), true);
assert.equal(options.includes("character varying[]"), true);
assert.equal(options.includes("jsonb[]"), true);
assert.equal(options.includes("serial[]"), false);
assert.equal(options.includes("smallserial[]"), false);
assert.equal(options.includes("bigserial[]"), false);
});
test("does not append length parameters after PostgreSQL array brackets", () => {
assert.equal(isDataTypeLengthDisabled("postgres", "varchar[]"), true);
assert.equal(combineDataTypeForDatabase("postgres", "varchar[]", "20"), "varchar[]");
});
test("returns Xugu data type options", () => {
const options = getDataTypeOptions("xugu");
assert.equal(options.includes("INTEGER"), true);