Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/lib/PostgresMetaColumns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ export default class PostgresMetaColumns {
is_unique = false,
comment,
check,
noTransaction = false,
}: {
table_id: number
name: string
Expand All @@ -124,6 +125,7 @@ export default class PostgresMetaColumns {
is_unique?: boolean
comment?: string
check?: string
noTransaction?: boolean
}): Promise<PostgresMetaResult<PostgresColumn>> {
const { data, error } = await this.metaTables.retrieve({ id: table_id })
if (error) {
Expand Down Expand Up @@ -163,7 +165,16 @@ export default class PostgresMetaColumns {
? ''
: `COMMENT ON COLUMN ${ident(schema)}.${ident(table)}.${ident(name)} IS ${literal(comment)}`

const sql = `
const sql = noTransaction
? `
ALTER TABLE ${ident(schema)}.${ident(table)} ADD COLUMN ${ident(name)} ${typeIdent(type)}
${defaultValueClause}
${isNullableClause}
${isPrimaryKeyClause}
${isUniqueClause}
${checkSql};
${commentSql};`
: `
BEGIN;
ALTER TABLE ${ident(schema)}.${ident(table)} ADD COLUMN ${ident(name)} ${typeIdent(type)}
${defaultValueClause}
Expand Down
5 changes: 4 additions & 1 deletion src/lib/PostgresMetaTables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,16 @@ export default class PostgresMetaTables {
name,
schema = 'public',
comment,
noTransaction = false,
}: PostgresTableCreate): Promise<PostgresMetaResult<PostgresTable>> {
const tableSql = `CREATE TABLE ${ident(schema)}.${ident(name)} ();`
const commentSql =
comment === undefined
? ''
: `COMMENT ON TABLE ${ident(schema)}.${ident(name)} IS ${literal(comment)};`
const sql = `BEGIN; ${tableSql} ${commentSql} COMMIT;`
const sql = noTransaction
? `${tableSql} ${commentSql}`
: `BEGIN; ${tableSql} ${commentSql} COMMIT;`
const { error } = await this.query(sql)
if (error) {
return { data: null, error }
Expand Down
2 changes: 2 additions & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export const postgresColumnCreateSchema = Type.Object({
is_unique: Type.Optional(Type.Boolean()),
comment: Type.Optional(Type.String()),
check: Type.Optional(Type.String()),
noTransaction: Type.Optional(Type.Boolean()),
})
export type PostgresColumnCreate = Static<typeof postgresColumnCreateSchema>

Expand Down Expand Up @@ -387,6 +388,7 @@ export const postgresTableCreateSchema = Type.Object({
name: Type.String(),
schema: Type.Optional(Type.String()),
comment: Type.Optional(Type.String()),
noTransaction: Type.Optional(Type.Boolean()),
})
export type PostgresTableCreate = Static<typeof postgresTableCreateSchema>

Expand Down