Skip to content

Commit 6a1b5dc

Browse files
committed
fix(lib): minor bugs, pass integration tests
1 parent 75cbb41 commit 6a1b5dc

File tree

3 files changed

+33
-23
lines changed

3 files changed

+33
-23
lines changed

src/lib/PostgresMetaColumns.ts

+30-20
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { PostgresMetaResult, PostgresColumn } from './types'
66

77
export default class PostgresMetaColumns {
88
query: (sql: string) => Promise<PostgresMetaResult<any>>
9-
metaTable: PostgresMetaTables
9+
metaTables: PostgresMetaTables
1010

1111
constructor(query: (sql: string) => Promise<PostgresMetaResult<any>>) {
1212
this.query = query
13-
this.metaTable = new PostgresMetaTables(query)
13+
this.metaTables = new PostgresMetaTables(query)
1414
}
1515

1616
async list({ includeSystemSchemas = false } = {}): Promise<PostgresMetaResult<PostgresColumn[]>> {
@@ -102,7 +102,7 @@ export default class PostgresMetaColumns {
102102
is_unique?: boolean
103103
comment?: string
104104
}): Promise<PostgresMetaResult<PostgresColumn>> {
105-
const { data, error } = await this.metaTable.retrieve({ id: table_id })
105+
const { data, error } = await this.metaTables.retrieve({ id: table_id })
106106
if (error) {
107107
return { data: null, error }
108108
}
@@ -176,28 +176,30 @@ COMMIT;`
176176
const nameSql =
177177
name === undefined || name === old!.name
178178
? ''
179-
: `ALTER TABLE ${old!.schema}.${old!.table} RENAME COLUMN ${old!.name} TO ${name};`
179+
: `ALTER TABLE ${ident(old!.schema)}.${ident(old!.table)} RENAME COLUMN ${ident(
180+
old!.name
181+
)} TO ${ident(name)};`
180182
// We use USING to allow implicit conversion of incompatible types (e.g. int4 -> text).
181183
const typeSql =
182184
type === undefined
183185
? ''
184-
: `ALTER TABLE ${old!.schema}.${old!.table} ALTER COLUMN ${
186+
: `ALTER TABLE ${ident(old!.schema)}.${ident(old!.table)} ALTER COLUMN ${ident(
185187
old!.name
186-
} SET DATA TYPE ${type} USING ${old!.name}::${type};`
188+
)} SET DATA TYPE ${type} USING ${ident(old!.name)}::${type};`
187189

188190
let defaultValueSql: string
189191
if (drop_default) {
190-
defaultValueSql = `ALTER TABLE ${old!.schema}.${old!.table} ALTER COLUMN ${
191-
old!.name
192-
} DROP DEFAULT;`
192+
defaultValueSql = `ALTER TABLE ${ident(old!.schema)}.${ident(
193+
old!.table
194+
)} ALTER COLUMN ${ident(old!.name)} DROP DEFAULT;`
193195
} else if (default_value === undefined) {
194196
defaultValueSql = ''
195197
} else {
196198
const defaultValue =
197199
default_value_format === 'expression' ? default_value : literal(default_value)
198-
defaultValueSql = `ALTER TABLE ${old!.schema}.${old!.table} ALTER COLUMN ${
199-
old!.name
200-
} SET DEFAULT ${defaultValue};`
200+
defaultValueSql = `ALTER TABLE ${ident(old!.schema)}.${ident(
201+
old!.table
202+
)} ALTER COLUMN ${ident(old!.name)} SET DEFAULT ${defaultValue};`
201203
}
202204
// What identitySql does vary depending on the old and new values of
203205
// is_identity and identity_generation.
@@ -209,32 +211,38 @@ COMMIT;`
209211
// | false | - | add identity | drop if exists |
210212
let identitySql = `ALTER TABLE ${ident(old!.schema)}.${ident(old!.table)} ALTER COLUMN ${ident(
211213
old!.name
212-
)};`
214+
)}`
213215
if (is_identity === false) {
214-
identitySql += 'DROP IDENTITY IF EXISTS;'
216+
identitySql += ' DROP IDENTITY IF EXISTS;'
215217
} else if (old!.is_identity === true) {
216218
if (identity_generation === undefined) {
217219
identitySql = ''
218220
} else {
219-
identitySql += `SET GENERATED ${identity_generation};`
221+
identitySql += ` SET GENERATED ${identity_generation};`
220222
}
221223
} else if (is_identity === undefined) {
222224
identitySql = ''
223225
} else {
224-
identitySql += `ADD GENERATED ${identity_generation} AS IDENTITY;`
226+
identitySql += ` ADD GENERATED ${identity_generation} AS IDENTITY;`
225227
}
226228
let isNullableSql: string
227229
if (is_nullable === undefined) {
228230
isNullableSql = ''
229231
} else {
230232
isNullableSql = is_nullable
231-
? `ALTER TABLE ${old!.schema}.${old!.table} ALTER COLUMN ${old!.name} DROP NOT NULL;`
232-
: `ALTER TABLE ${old!.schema}.${old!.table} ALTER COLUMN ${old!.name} SET NOT NULL;`
233+
? `ALTER TABLE ${ident(old!.schema)}.${ident(old!.table)} ALTER COLUMN ${ident(
234+
old!.name
235+
)} DROP NOT NULL;`
236+
: `ALTER TABLE ${ident(old!.schema)}.${ident(old!.table)} ALTER COLUMN ${ident(
237+
old!.name
238+
)} SET NOT NULL;`
233239
}
234240
const commentSql =
235241
comment === undefined
236242
? ''
237-
: `COMMENT ON COLUMN ${old!.schema}.${old!.table}.${old!.name} IS ${comment};`
243+
: `COMMENT ON COLUMN ${ident(old!.schema)}.${ident(old!.table)}.${ident(
244+
old!.name
245+
)} IS ${literal(comment)};`
238246

239247
// nameSql must be last.
240248
// defaultValueSql must be after typeSql.
@@ -263,7 +271,9 @@ COMMIT;`
263271
if (error) {
264272
return { data: null, error }
265273
}
266-
const sql = `ALTER TABLE ${column!.schema}.${column!.table} DROP COLUMN ${column!.name};`
274+
const sql = `ALTER TABLE ${ident(column!.schema)}.${ident(column!.table)} DROP COLUMN ${ident(
275+
column!.name
276+
)};`
267277
{
268278
const { error } = await this.query(sql)
269279
if (error) {

src/lib/PostgresMetaExtensions.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export default class PostgresMetaExtensions {
1414
}
1515

1616
async retrieve({ name }: { name: string }): Promise<PostgresMetaResult<PostgresExtension>> {
17-
const sql = `${extensionsSql} WHERE name = ${name};`
17+
const sql = `${extensionsSql} WHERE name = ${literal(name)};`
1818
const { data, error } = await this.query(sql)
1919
if (error) {
2020
return { data, error }

src/lib/PostgresMetaTables.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ export default class PostgresMetaTables {
124124
const alter = `ALTER TABLE ${ident(old!.schema)}.${ident(old!.name)}`
125125
const schemaSql = schema === undefined ? '' : `${alter} SET SCHEMA ${ident(schema)};`
126126
let nameSql = ''
127-
if (name !== undefined) {
127+
if (name !== undefined && name !== old!.name) {
128128
const currentSchema = schema === undefined ? old!.schema : schema
129-
nameSql = `ALTER TABLE ${ident(currentSchema)}.${ident(old!.name)} RENAME TO ${ident(name)}`
129+
nameSql = `ALTER TABLE ${ident(currentSchema)}.${ident(old!.name)} RENAME TO ${ident(name)};`
130130
}
131131
let enableRls = ''
132132
if (rls_enabled !== undefined) {

0 commit comments

Comments
 (0)