Skip to content

Commit 616cf86

Browse files
committed
fix: allow array column type
Previously supplying e.g. `int4[]` as column type would error out because the brackets are treated as part of the type name (as opposed to denoting that it's an array of `int4`s). Remove the quoting around `data_type`s so we can supply `foo[]` instead of `_foo`.
1 parent f045594 commit 616cf86

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/api/columns.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ const addColumnSqlize = ({
146146

147147
return format(
148148
`
149-
ALTER TABLE %I.%I ADD COLUMN %I %I
149+
ALTER TABLE %I.%I ADD COLUMN %I ${type}
150150
${defaultValueSql}
151151
${isIdentitySql}
152152
${isNullableSql}
@@ -155,8 +155,7 @@ ALTER TABLE %I.%I ADD COLUMN %I %I
155155
${commentSql}`,
156156
schema,
157157
table,
158-
name,
159-
type
158+
name
160159
)
161160
}
162161
const getColumnSqlize = (tableId: number, name: string) => {
@@ -198,13 +197,11 @@ const alterColumnSqlize = (
198197
type === undefined
199198
? ''
200199
: format(
201-
'ALTER TABLE %I.%I ALTER COLUMN %I SET DATA TYPE %I USING %I::%I;',
200+
`ALTER TABLE %I.%I ALTER COLUMN %I SET DATA TYPE ${type} USING %I::${type};`,
202201
old.schema,
203202
old.table,
204203
old.name,
205-
type,
206-
old.name,
207-
type
204+
old.name
208205
)
209206
let defaultValueSql: string
210207
if (drop_default) {

test/integration/index.spec.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,24 @@ describe('/tables', async () => {
323323
await axios.delete(`${URL}/columns/${newTable.id}.1`)
324324
await axios.delete(`${URL}/tables/${newTable.id}`)
325325
})
326+
it('POST /columns array type', async () => {
327+
const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'a' })
328+
await axios.post(`${URL}/columns`, {
329+
table_id: newTable.id,
330+
name: 'b',
331+
type: 'int2[]',
332+
})
333+
334+
const { data: columns } = await axios.get(`${URL}/columns`)
335+
const newColumn = columns.find(
336+
(column) =>
337+
column.id === `${newTable.id}.1` && column.name === 'b' && column.format === '_int2'
338+
)
339+
assert.equal(newColumn.name, 'b')
340+
341+
await axios.delete(`${URL}/columns/${newTable.id}.1`)
342+
await axios.delete(`${URL}/tables/${newTable.id}`)
343+
})
326344
it('/columns default_value with expressions', async () => {
327345
const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'a' })
328346
const { data: newColumn } = await axios.post(`${URL}/columns`, {

0 commit comments

Comments
 (0)