8000 fix: handling of type identifier in column create() and update() · ITninja04/postgres-meta@12eed99 · GitHub
[go: up one dir, main page]

Skip to content

Commit 12eed99

Browse files
DukeManhsoedirgo
authored andcommitted
fix: handling of type identifier in column create() and update()
1 parent 41091e5 commit 12eed99

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/lib/PostgresMetaColumns.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import { DEFAULT_SYSTEM_SCHEMAS } from './constants'
44
import { columnsSql } from './sql'
55
import { PostgresMetaResult, PostgresColumn } from './types'
66

7-
// TODO: Fix handling of `type` in `create()` and `update()`.
8-
// `type` on its own is not enough, e.g. `1::my type` should be `1::"my type"`.
9-
// `ident(type)` is not enough, e.g. `"int2[]"` should be `"int2"[]`.
107
export default class PostgresMetaColumns {
118
query: (sql: string) => Promise<PostgresMetaResult<any>>
129
metaTables: PostgresMetaTables
@@ -163,7 +160,7 @@ export default class PostgresMetaColumns {
163160

164161
const sql = `
165162
BEGIN;
166-
ALTER TABLE ${ident(schema)}.${ident(table)} ADD COLUMN ${ident(name)} ${type}
163+
ALTER TABLE ${ident(schema)}.${ident(table)} ADD COLUMN ${ident(name)} ${typeIdent(type)}
167164
${defaultValueClause}
168165
${isNullableClause}
169166
${isPrimaryKeyClause}
@@ -223,7 +220,7 @@ COMMIT;`
223220
? ''
224221
: `ALTER TABLE ${ident(old!.schema)}.${ident(old!.table)} ALTER COLUMN ${ident(
225222
old!.name
226-
)} SET DATA TYPE ${ident(type)} USING ${ident(old!.name)}::${ident(type)};`
223+
)} SET DATA TYPE ${typeIdent(type)} USING ${ident(old!.name)}::${typeIdent(type)};`
227224

228225
let defaultValueSql: string
229226
if (drop_default) {
@@ -349,3 +346,7 @@ COMMIT;`
349346
return { data: column!, error: null }
350347
}
351348
}
349+
350+
const typeIdent = (type: string) => {
351+
return type.endsWith('[]') ? `${ident(type.slice(0, -2))}[]` : ident(type)
352+
}

test/lib/columns.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,53 @@ test('update with name unchanged', async () => {
444444
await pgMeta.tables.remove(testTable!.id)
445445
})
446446

447+
test('update with array types', async () => {
448+
const { data: testTable } = await pgMeta.tables.create({ name: 't' })
449+
450+
let res = await pgMeta.columns.create({
451+
table_id: testTable!.id,
452+
name: 'c',
453+
type: 'text',
454+
})
455+
res = await pgMeta.columns.update(res.data!.id, {
456+
type: 'text[]',
457+
})
458+
expect(res).toMatchInlineSnapshot(
459+
{
460+
data: {
461+
id: expect.stringMatching(/^\d+\.1$/),
462+
table_id: expect.any(Number),
463+
},
464+
},
465+
`
466+
Object {
467+
"data": Object {
468+
"comment": null,
469+
"data_type": "ARRAY",
470+
"default_value": null,
471+
"enums": Array [],
472+
"format": "_text",
473+
"id": StringMatching /\\^\\\\d\\+\\\\\\.1\\$/,
474+
"identity_generation": null,
475+
"is_generated": false,
476+
"is_identity": false,
477+
"is_nullable": true,
478+
"is_unique": false,
479+
"is_updatable": true,
480+
"name": "c",
481+
"ordinal_position": 1,
482+
"schema": "public",
483+
"table": "t",
484+
"table_id": Any<Number>,
485+
},
486+
"error": null,
487+
}
488+
`
489+
)
490+
491+
await pgMeta.tables.remove(testTable!.id)
492+
})
493+
447494
test('update with incompatible types', async () => {
448495
const { data: testTable } = await pgMeta.tables.create({ name: 't' })
449496

0 commit comments

Comments
 (0)
0