Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add 'add column if not exists' for postgres #900

Merged
merged 1 commit into from
Mar 10, 2024
Merged
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
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
Loading