Skip to content

Commit

Permalink
Add a callback argument for alterTable.addColumn
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas committed May 11, 2022
1 parent 1c78641 commit a13e678
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 16 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kysely",
"version": "0.19.0",
"version": "0.19.1",
"description": "Type safe SQL query builder",
"repository": {
"type": "git",
Expand Down
33 changes: 18 additions & 15 deletions src/schema/alter-table-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { OnModifyForeignAction } from '../operation-node/references-node.js'
import { RenameColumnNode } from '../operation-node/rename-column-node.js'
import { CompiledQuery } from '../query-compiler/compiled-query.js'
import { Compilable } from '../util/compilable.js'
import { freeze } from '../util/object-utils.js'
import { freeze, noop } from '../util/object-utils.js'
import { preventAwait } from '../util/prevent-await.js'
import {
ColumnDefinitionBuilder,
Expand Down Expand Up @@ -97,24 +97,23 @@ export class AlterTableBuilder {

/**
* See {@link CreateTableBuilder.addColumn}
*
* Unlike {@link CreateTableBuilder.addColumn} this method returns the column builder
* and doesn't take a callback as the last argument. This is because you can only
* add one column per `ALTER TABLE` query.
*/
addColumn(
columnName: string,
dataType: DataTypeExpression
dataType: DataTypeExpression,
build: AlterTableAddColumnBuilderCallback = noop
): AlterTableAddColumnBuilder {
return new AlterTableAddColumnBuilder({
...this.#props,
columnBuilder: new ColumnDefinitionBuilder(
ColumnDefinitionNode.create(
columnName,
parseDataTypeExpression(dataType)
)
),
})
return build(
new AlterTableAddColumnBuilder({
...this.#props,
columnBuilder: new ColumnDefinitionBuilder(
ColumnDefinitionNode.create(
columnName,
parseDataTypeExpression(dataType)
)
),
})
)
}

/**
Expand Down Expand Up @@ -437,6 +436,10 @@ export interface AlterTableAddColumnBuilderProps
readonly columnBuilder: ColumnDefinitionBuilder
}

export type AlterTableAddColumnBuilderCallback = (
builder: AlterTableAddColumnBuilder
) => AlterTableAddColumnBuilder

export class AlterTableModifyColumnBuilder
implements ColumnDefinitionBuilderInterface, OperationNodeSource, Compilable
{
Expand Down
34 changes: 34 additions & 0 deletions test/node/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,40 @@ for (const dialect of BUILT_IN_DIALECTS) {
})
})

it('should add a column using a callback', async () => {
const builder = ctx.db.schema
.alterTable('test')
.addColumn('bool_col', 'boolean', (col) => col.notNull())

testSql(builder, dialect, {
postgres: {
sql: 'alter table "test" add column "bool_col" boolean not null',
parameters: [],
},
mysql: {
sql: 'alter table `test` add column `bool_col` boolean not null',
parameters: [],
},
sqlite: {
sql: 'alter table "test" add column "bool_col" boolean not null',
parameters: [],
},
})

await builder.execute()

expect(await getColumnMeta('test.bool_col')).to.eql({
name: 'bool_col',
isNullable: false,
dataType:
dialect === 'postgres'
? 'bool'
: dialect === 'sqlite'
? 'boolean'
: 'tinyint',
})
})

if (dialect !== 'sqlite') {
it('should add a unique column', async () => {
const builder = ctx.db.schema
Expand Down

0 comments on commit a13e678

Please sign in to comment.