From 76e16d578c8705a314d6c65de1e73894a85de743 Mon Sep 17 00:00:00 2001 From: Angelelz Date: Thu, 24 Aug 2023 20:55:04 -0400 Subject: [PATCH] [Mysql]: Added defaultCurrentTimestamp and onUpdateCurrentTimestamp to datetime - The method defaultNow() has been deprecated in favor of defaultCurrentTimestamp - The method onUpdateNow() has been deprecated in favor of onUpdateCurrentTimestamp --- .../src/mysql-core/columns/date.common.ts | 26 +++++++++++++++++++ .../src/mysql-core/columns/datetime.ts | 10 +++---- 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/drizzle-orm/src/mysql-core/columns/date.common.ts b/drizzle-orm/src/mysql-core/columns/date.common.ts index 9800a3d90..adefb787d 100644 --- a/drizzle-orm/src/mysql-core/columns/date.common.ts +++ b/drizzle-orm/src/mysql-core/columns/date.common.ts @@ -11,6 +11,7 @@ import { MySqlColumn, MySqlColumnBuilder } from './common.ts'; export interface MySqlDateColumnBaseConfig { hasOnUpdateNow: boolean; + fsp: number | undefined; } export abstract class MySqlDateColumnBaseBuilder< @@ -20,16 +21,41 @@ export abstract class MySqlDateColumnBaseBuilder< > extends MySqlColumnBuilder { static readonly [entityKind]: string = 'MySqlDateColumnBuilder'; + /** + * @deprecated Use `defaultCurrentTimestamp()` instead. + * + * Adds `DEFAULT (now())` to the column + */ defaultNow() { return this.default(sql`(now())`); } + /** + * Adds `DEFAULT CURRENT_TIMESTAMP` to the column + * */ + defaultCurrentTimestamp() { + const fsp = this.config.fsp ? `(${this.config.fsp})` : ''; + return this.default(sql.raw(`CURRENT_TIMESTAMP${fsp}`)); + } + // "on update now" also adds an implicit default value to the column - https://dev.mysql.com/doc/refman/8.0/en/timestamp-initialization.html + /** + * @deprecated Use `defaultCurrentTimestamp()` instead. + */ onUpdateNow(): HasDefault { this.config.hasOnUpdateNow = true; this.config.hasDefault = true; return this as HasDefault; } + + /** + * Adds `ON UPDATE CURRENT_TIMESTAMP` to the column + * */ + onUpdateCurrentTimestamp(): HasDefault { + this.config.hasOnUpdateNow = true; + this.config.hasDefault = true; + return this as HasDefault; + } } export abstract class MySqlDateBaseColumn< diff --git a/drizzle-orm/src/mysql-core/columns/datetime.ts b/drizzle-orm/src/mysql-core/columns/datetime.ts index 801801969..181f07217 100644 --- a/drizzle-orm/src/mysql-core/columns/datetime.ts +++ b/drizzle-orm/src/mysql-core/columns/datetime.ts @@ -3,7 +3,7 @@ import type { ColumnBaseConfig } from '~/column.ts'; import { entityKind } from '~/entity.ts'; import type { AnyMySqlTable } from '~/mysql-core/table.ts'; import { type Equal } from '~/utils.ts'; -import { MySqlColumn, MySqlColumnBuilder } from './common.ts'; +import { MySqlDateBaseColumn, MySqlDateColumnBaseBuilder } from './date.common.ts'; export type MySqlDateTimeBuilderInitial = MySqlDateTimeBuilder<{ name: TName; @@ -15,7 +15,7 @@ export type MySqlDateTimeBuilderInitial = MySqlDateTimeBui }>; export class MySqlDateTimeBuilder> - extends MySqlColumnBuilder + extends MySqlDateColumnBaseBuilder { static readonly [entityKind]: string = 'MySqlDateTimeBuilder'; @@ -35,7 +35,7 @@ export class MySqlDateTimeBuilder> extends MySqlColumn { +export class MySqlDateTime> extends MySqlDateBaseColumn { static readonly [entityKind]: string = 'MySqlDateTime'; readonly fsp: number | undefined; @@ -69,7 +69,7 @@ export type MySqlDateTimeStringBuilderInitial = MySqlDateT }>; export class MySqlDateTimeStringBuilder> - extends MySqlColumnBuilder + extends MySqlDateColumnBaseBuilder { static readonly [entityKind]: string = 'MySqlDateTimeStringBuilder'; @@ -89,7 +89,7 @@ export class MySqlDateTimeStringBuilder> extends MySqlColumn { +export class MySqlDateTimeString> extends MySqlDateBaseColumn { static readonly [entityKind]: string = 'MySqlDateTimeString'; readonly fsp: number | undefined;