Skip to content

Commit

Permalink
add IDENTITY column support. (kysely-org#823)
Browse files Browse the repository at this point in the history
  • Loading branch information
igalklebanov authored and thecodrr committed Sep 3, 2024
1 parent d11c8ae commit 9bf7c27
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/operation-node/column-definition-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { ColumnNode } from './column-node.js'
import { DefaultValueNode } from './default-value-node.js'
import { GeneratedNode } from './generated-node.js'
import { OperationNode } from './operation-node.js'
import { RawNode } from './raw-node.js'
import { ReferencesNode } from './references-node.js'

export type ColumnDefinitionNodeProps = Omit<
Expand All @@ -28,6 +27,7 @@ export interface ColumnDefinitionNode extends OperationNode {
readonly frontModifiers?: ReadonlyArray<OperationNode>
readonly endModifiers?: ReadonlyArray<OperationNode>
readonly nullsNotDistinct?: boolean
readonly identity?: 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 @@ -485,6 +485,7 @@ export class OperationNodeTransformer {
frontModifiers: this.transformNodeList(node.frontModifiers),
endModifiers: this.transformNodeList(node.endModifiers),
nullsNotDistinct: node.nullsNotDistinct,
identity: node.identity,
})
}

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 @@ -684,6 +684,10 @@ export class DefaultQueryCompiler
this.visitNode(node.generated)
}

if (node.identity) {
this.append(' identity')
}

if (node.defaultTo) {
this.append(' ')
this.visitNode(node.defaultTo)
Expand Down
19 changes: 18 additions & 1 deletion src/schema/column-definition-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ export class ColumnDefinitionBuilder implements OperationNodeSource {
)
}

/**
* Makes the column an identity column.
*
* This only works on some dialects like MS SQL Server (MSSQL).
*
* For PostgreSQL's `generated always as identity` use {@link generatedAlwaysAsIdentity}.
*/
identity(): ColumnDefinitionBuilder {
return new ColumnDefinitionBuilder(
ColumnDefinitionNode.cloneWith(this.#node, { identity: true })
)
}

/**
* Makes the column the primary key.
*
Expand Down Expand Up @@ -248,7 +261,11 @@ export class ColumnDefinitionBuilder implements OperationNodeSource {
}

/**
* Adds the `generated always as identity` specifier on supported dialects.
* Adds the `generated always as identity` specifier.
*
* This only works on some dialects like PostgreSQL.
*
* For MS SQL Server (MSSQL)'s identity column use {@link identity}.
*/
generatedAlwaysAsIdentity(): ColumnDefinitionBuilder {
return new ColumnDefinitionBuilder(
Expand Down
5 changes: 1 addition & 4 deletions test/node/src/introspect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,10 +831,7 @@ for (const dialect of DIALECTS) {
await ctx.db.schema
.createTable('some_schema.pet')
.addColumn('some_column', 'integer', (col) =>
col
.notNull()
.modifyFront(sql`identity(1,1)`)
.primaryKey(),
col.identity().notNull().primaryKey()
)
.addColumn('some_column_plus_1', sql``, (col) =>
col.modifyEnd(sql`as (some_column + 1)`),
Expand Down
7 changes: 2 additions & 5 deletions test/node/src/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,10 +299,7 @@ for (const dialect of DIALECTS) {
const builder = ctx.db.schema
.createTable('test')
.addColumn('a', 'integer', (col) =>
col
.notNull()
.modifyFront(sql`identity(1,1)`)
.primaryKey(),
col.identity().notNull().primaryKey()
)
.addColumn('b', 'integer', (col) =>
col
Expand Down Expand Up @@ -343,7 +340,7 @@ for (const dialect of DIALECTS) {
mssql: {
sql: [
'create table "test"',
'("a" integer identity(1,1) not null primary key,',
'("a" integer identity not null primary key,',
'"b" integer references "test" ("a") on delete no action on update no action check (b < 10),',
'"c" varchar,',
'"d" varchar(10),',
Expand Down
6 changes: 1 addition & 5 deletions test/node/src/test-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,11 +373,7 @@ export function createTableWithId(

if (dialect === 'mssql') {
return builder.addColumn('id', 'integer', (col) =>
col
.notNull()
// TODO: change to method when its implemented
.modifyFront(sql`identity(1,1)`)
.primaryKey(),
col.identity().notNull().primaryKey()
)
}

Expand Down

0 comments on commit 9bf7c27

Please sign in to comment.