From d2f0a272300cf68bdfd3fe862fb1c651757f063e Mon Sep 17 00:00:00 2001 From: Austin Date: Thu, 10 Oct 2024 23:40:05 +0800 Subject: [PATCH] feat: default to current timestamp --- src/schema/column-definition-builder.ts | 24 +++++++++++++ test/node/src/schema.test.ts | 47 +++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/schema/column-definition-builder.ts b/src/schema/column-definition-builder.ts index 4328d99fd..72b1fe2e3 100644 --- a/src/schema/column-definition-builder.ts +++ b/src/schema/column-definition-builder.ts @@ -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 @@ -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. * diff --git a/test/node/src/schema.test.ts b/test/node/src/schema.test.ts index 850f199de..1882b3c6d 100644 --- a/test/node/src/schema.test.ts +++ b/test/node/src/schema.test.ts @@ -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')