Skip to content

Commit

Permalink
feat: add 'add column if not exists' for postgres (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkusWendorf authored Mar 10, 2024
1 parent 6f91355 commit 4b14014
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/operation-node/column-definition-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export interface ColumnDefinitionNode extends OperationNode {
readonly endModifiers?: ReadonlyArray<OperationNode>
readonly nullsNotDistinct?: boolean
readonly identity?: boolean
readonly ifNotExists?: boolean
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/operation-node/operation-node-transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ export class OperationNodeTransformer {
endModifiers: this.transformNodeList(node.endModifiers),
nullsNotDistinct: node.nullsNotDistinct,
identity: node.identity,
ifNotExists: node.ifNotExists,
})
}

Expand Down
4 changes: 4 additions & 0 deletions src/query-compiler/default-query-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,10 @@ export class DefaultQueryCompiler
}

protected override visitColumnDefinition(node: ColumnDefinitionNode): void {
if (node.ifNotExists) {
this.append('if not exists ')
}

this.visitNode(node.column)

this.append(' ')
Expand Down
10 changes: 10 additions & 0 deletions src/schema/column-definition-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,16 @@ export class ColumnDefinitionBuilder implements OperationNodeSource {
)
}

/**
* Adds `if not exists` specifier.
* This only works for PostgreSQL.
*/
ifNotExists(): ColumnDefinitionBuilder {
return new ColumnDefinitionBuilder(
ColumnDefinitionNode.cloneWith(this.#node, { ifNotExists: true }),
)
}

/**
* This can be used to add any additional SQL to the end of the column definition.
*
Expand Down
20 changes: 19 additions & 1 deletion test/node/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ for (const dialect of DIALECTS) {
const builder = ctx.db.schema
.createTable('test')
.addColumn('a', 'integer', (col) =>
col.identity().notNull().primaryKey()
col.identity().notNull().primaryKey(),
)
.addColumn('b', 'integer', (col) =>
col
Expand Down Expand Up @@ -2329,6 +2329,24 @@ for (const dialect of DIALECTS) {

await builder.execute()
})

it('should add a column with "if not exists" modifier', async () => {
const builder = ctx.db.schema
.alterTable('test')
.addColumn('desc', 'varchar(20)', (cb) => cb.ifNotExists())

testSql(builder, dialect, {
postgres: {
sql: 'alter table "test" add column if not exists "desc" varchar(20)',
parameters: [],
},
mysql: NOT_SUPPORTED,
mssql: NOT_SUPPORTED,
sqlite: NOT_SUPPORTED,
})

await builder.execute()
})
}

if (dialect === 'postgres' || dialect === 'mysql') {
Expand Down

0 comments on commit 4b14014

Please sign in to comment.