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
Binary file added .DS_Store
Binary file not shown.
2 changes: 2 additions & 0 deletions src/operation-node/alter-table-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import type { RenameConstraintNode } from './rename-constraint-node.js'

export type AlterTableNodeTableProps = Pick<
AlterTableNode,
| 'ifExists'
| 'renameTo'
| 'setSchema'
| 'addConstraint'
Expand All @@ -34,6 +35,7 @@ export type AlterTableColumnAlterationNode =
export interface AlterTableNode extends OperationNode {
readonly kind: 'AlterTableNode'
readonly table: TableNode
readonly ifExists?: boolean
readonly renameTo?: TableNode
readonly setSchema?: IdentifierNode
readonly columnAlterations?: ReadonlyArray<AlterTableColumnAlterationNode>
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 @@ -836,6 +836,7 @@ export class OperationNodeTransformer {
return requireAllProps<AlterTableNode>({
kind: 'AlterTableNode',
table: this.transformNode(node.table, queryId),
ifExists: node.ifExists,
renameTo: this.transformNode(node.renameTo, queryId),
setSchema: this.transformNode(node.setSchema, queryId),
columnAlterations: this.transformNodeList(
Expand Down
5 changes: 5 additions & 0 deletions src/query-compiler/default-query-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1165,6 +1165,11 @@ export class DefaultQueryCompiler

protected override visitAlterTable(node: AlterTableNode): void {
this.append('alter table ')

if (node.ifExists) {
this.append('if exists ')
}

this.visitNode(node.table)
this.append(' ')

Expand Down
14 changes: 14 additions & 0 deletions src/schema/alter-table-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ export class AlterTableBuilder implements ColumnAlteringInterface {
this.#props = freeze(props)
}

/**
* Adds the "if exists" modifier.
*
* If the table doesn't exist, no error is thrown if this method has been called.
*/
ifExists(): AlterTableBuilder {
return new AlterTableBuilder({
...this.#props,
node: AlterTableNode.cloneWithTableProps(this.#props.node, {
ifExists: true,
}),
})
}

renameTo(newTableName: string): AlterTableExecutor {
return new AlterTableExecutor({
...this.#props,
Expand Down
27 changes: 27 additions & 0 deletions test/node/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1869,6 +1869,8 @@ for (const dialect of DIALECTS) {
.createTable('test')
.addColumn('id', 'bigint', (col) => col.primaryKey())
.addColumn('first_name', 'varchar(255)')
.addColumn('last_name', 'varchar(255)')
.addColumn('age', 'integer')
.execute()

await ctx.db.schema
Expand Down Expand Up @@ -2544,6 +2546,31 @@ for (const dialect of DIALECTS) {
.execute()
})

if (dialect === 'postgres' || dialect === 'mysql' || dialect === 'sqlite') {
it('should alter table with if exists', async () => {
const builder = ctx.db.schema
.alterTable('test')
.ifExists()
.addColumn('date_col', 'date')

testSql(builder, dialect, {
postgres: {
sql: 'alter table if exists "test" add column "date_col" date',
parameters: [],
},
mysql: {
sql: 'alter table if exists `test` add column `date_col` date',
parameters: [],
},
mssql: NOT_SUPPORTED,
sqlite: {
sql: 'alter table if exists "test" add column "date_col" date',
parameters: [],
},
})
})
}

describe('add column', () => {
it('should add a column', async () => {
const builder = ctx.db.schema
Expand Down
Loading