Skip to content

Commit

Permalink
feat: default to current timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
Austin authored and igalklebanov committed Dec 17, 2024
1 parent 6be09d8 commit d2f0a27
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/schema/column-definition-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { GeneratedNode } from '../operation-node/generated-node.js'
import { DefaultValueNode } from '../operation-node/default-value-node.js'
import { parseOnModifyForeignAction } from '../parser/on-modify-action-parser.js'
import { Expression } from '../expression/expression.js'
import { RawNode } from '../operation-node/raw-node.js'

export class ColumnDefinitionBuilder implements OperationNodeSource {
readonly #node: ColumnDefinitionNode
Expand Down Expand Up @@ -371,6 +372,29 @@ export class ColumnDefinitionBuilder implements OperationNodeSource {
)
}

/**
* Adds `DEFAULT CURRENT_TIMESTAMP` for the column.
*
* ### Examples
*
* ```ts
* db.schema.createTable('test')
* .addColumn('created_at', 'datetime', (col) =>
* col.defaultToCurrentTimestamp(),
* )
* .execute()
* ```
*/
defaultToCurrentTimestamp(): ColumnDefinitionBuilder {
return new ColumnDefinitionBuilder(
ColumnDefinitionNode.cloneWith(this.#node, {
defaultTo: DefaultValueNode.create(
RawNode.createWithSql('current_timestamp'),
),
}),
)
}

/**
* Adds a check constraint for the column.
*
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 @@ -484,6 +484,53 @@ for (const dialect of DIALECTS) {
throw new Error(`Unknown dialect: ${dialect}`)
}

it('should create table with default timestamp column', async () => {
if (dialect !== 'postgres') {
const builder = ctx.db.schema
.createTable('test')
.addColumn('created_at', 'datetime', (col) =>
col.defaultToCurrentTimestamp(),
)

testSql(builder, dialect, {
postgres: NOT_SUPPORTED,
mysql: {
sql: 'create table `test` (`created_at` datetime default current_timestamp)',
parameters: [],
},
mssql: {
sql: 'create table "test" ("created_at" datetime default current_timestamp)',
parameters: [],
},
sqlite: {
sql: 'create table "test" ("created_at" datetime default current_timestamp)',
parameters: [],
},
})

await builder.execute()
} else {
const builder = ctx.db.schema
.createTable('test')
.addColumn('created_at', 'timestamptz', (col) =>
col.defaultToCurrentTimestamp(),
)
.addColumn('data', 'varchar')

testSql(builder, dialect, {
postgres: {
sql: 'create table "test" ("created_at" timestamptz default current_timestamp, "data" varchar)',
parameters: [],
},
mysql: NOT_SUPPORTED,
mssql: NOT_SUPPORTED,
sqlite: NOT_SUPPORTED,
})

await builder.execute()
}
})

it('should create a table with a unique constraints', async () => {
const builder = ctx.db.schema
.createTable('test')
Expand Down

0 comments on commit d2f0a27

Please sign in to comment.