Skip to content

Commit 5e57fd7

Browse files
committed
fix: allow implicit conversion of column types
Done using the USING clause of ALTER COLUMN, like so: ALTER TABLE schema_name.table_name ALTER column_name SET DATA TYPE text USING column_name::text; It's possible to allow users to specify their own conversion expression, but decided to omit it for now. We'll revisit that when the need arises.
1 parent 29a48c4 commit 5e57fd7

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/api/columns.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,10 +171,11 @@ const alterColumnSqlize = ({
171171
typeof name === 'undefined' || name === oldName
172172
? ''
173173
: `ALTER TABLE "${schema}"."${table}" RENAME COLUMN "${oldName}" TO "${name}";`
174+
// We use USING to allow implicit conversion of incompatible types (e.g. int4 -> text).
174175
const typeSql =
175176
type === undefined
176177
? ''
177-
: `ALTER TABLE "${schema}"."${table}" ALTER COLUMN "${oldName}" SET DATA TYPE "${type}";`
178+
: `ALTER TABLE "${schema}"."${table}" ALTER COLUMN "${oldName}" SET DATA TYPE "${type}" USING "${oldName}"::"${type}";`
178179
let defaultValueSql = ''
179180
if (drop_default) {
180181
defaultValueSql = `ALTER TABLE "${schema}"."${table}" ALTER COLUMN "${oldName}" DROP DEFAULT;`

test/integration/index.spec.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,23 @@ describe('/tables', async () => {
355355
await axios.delete(`${URL}/columns/${newTable.id}.1`)
356356
await axios.delete(`${URL}/tables/${newTable.id}`)
357357
})
358+
it('PATCH /columns "incompatible" types', async () => {
359+
const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'foo' })
360+
await axios.post(`${URL}/columns`, {
361+
table_id: newTable.id,
362+
name: 'bar',
363+
type: 'text',
364+
})
365+
366+
const { data: updatedColumn } = await axios.patch(`${URL}/columns/${newTable.id}.1`, {
367+
type: 'int4',
368+
})
369+
370+
assert.strictEqual(updatedColumn.format, 'int4')
371+
372+
await axios.delete(`${URL}/columns/${newTable.id}.1`)
373+
await axios.delete(`${URL}/tables/${newTable.id}`)
374+
})
358375
it('DELETE /columns', async () => {
359376
const { data: newTable } = await axios.post(`${URL}/tables`, { name: 'foo bar' })
360377
await axios.post(`${URL}/columns`, { table_id: newTable.id, name: 'foo bar', type: 'int2' })

0 commit comments

Comments
 (0)