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

Add addPrimaryKeyConstraint for AlterTableBuilder #639

Merged
merged 1 commit into from
Aug 9, 2023
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
18 changes: 18 additions & 0 deletions src/schema/alter-table-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
import { AlterTableExecutor } from './alter-table-executor.js'
import { AlterTableAddForeignKeyConstraintBuilder } from './alter-table-add-foreign-key-constraint-builder.js'
import { AlterTableDropConstraintBuilder } from './alter-table-drop-constraint-builder.js'
import { PrimaryConstraintNode } from '../operation-node/primary-constraint-node.js'

/**
* This builder can be used to create a `alter table` query.
Expand Down Expand Up @@ -212,6 +213,23 @@ export class AlterTableBuilder implements ColumnAlteringInterface {
})
}

/**
* See {@link CreateTableBuilder.addPrimaryKeyConstraint}
*/
addPrimaryKeyConstraint(
constraintName: string,
columns: string[]
): AlterTableExecutor {
return new AlterTableExecutor({
...this.#props,
node: AlterTableNode.cloneWithTableProps(this.#props.node, {
addConstraint: AddConstraintNode.create(
PrimaryConstraintNode.create(columns, constraintName)
),
}),
})
}

dropConstraint(constraintName: string): AlterTableDropConstraintBuilder {
return new AlterTableDropConstraintBuilder({
...this.#props,
Expand Down
47 changes: 47 additions & 0 deletions test/node/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2468,6 +2468,53 @@ for (const dialect of DIALECTS) {
})
}

if (dialect !== 'sqlite') {
describe('add primary key constraint', async () => {
it('should add a primary key constraint', async () => {
const builder = ctx.db.schema
.alterTable('test')
.addPrimaryKeyConstraint('test_pkey', ['integer_col'])

testSql(builder, dialect, {
postgres: {
sql: 'alter table "test" add constraint "test_pkey" primary key ("integer_col")',
parameters: [],
},
mysql: {
sql: 'alter table `test` add constraint `test_pkey` primary key (`integer_col`)',
parameters: [],
},
sqlite: NOT_SUPPORTED,
})

await builder.execute()
})

it('should add a primary key constraint for multiple columns', async () => {
const builder = ctx.db.schema
.alterTable('test')
.addPrimaryKeyConstraint('test_pkey', [
'integer_col',
'varchar_col',
])

testSql(builder, dialect, {
postgres: {
sql: 'alter table "test" add constraint "test_pkey" primary key ("integer_col", "varchar_col")',
parameters: [],
},
mysql: {
sql: 'alter table `test` add constraint `test_pkey` primary key (`integer_col`, `varchar_col`)',
parameters: [],
},
sqlite: NOT_SUPPORTED,
})

await builder.execute()
})
})
}

if (dialect !== 'sqlite') {
describe('parse schema name', () => {
beforeEach(cleanup)
Expand Down
Loading