Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mysql]: Added defaultCurrentTimestamp and onUpdateCurrentTimestamp to datetime #1114

Open
wants to merge 8 commits into
base: beta
Choose a base branch
from
26 changes: 26 additions & 0 deletions drizzle-orm/src/mysql-core/columns/date.common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { MySqlColumn, MySqlColumnBuilder } from './common.ts';

export interface MySqlDateColumnBaseConfig {
hasOnUpdateNow: boolean;
fsp: number | undefined;
}

export abstract class MySqlDateColumnBaseBuilder<
Expand All @@ -20,16 +21,41 @@ export abstract class MySqlDateColumnBaseBuilder<
> extends MySqlColumnBuilder<T, TRuntimeConfig & MySqlDateColumnBaseConfig, TExtraConfig> {
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> {
this.config.hasOnUpdateNow = true;
this.config.hasDefault = true;
return this as HasDefault<this>;
}

/**
* Adds `ON UPDATE CURRENT_TIMESTAMP` to the column
* */
onUpdateCurrentTimestamp(): HasDefault<this> {
this.config.hasOnUpdateNow = true;
this.config.hasDefault = true;
return this as HasDefault<this>;
}
}

export abstract class MySqlDateBaseColumn<
Expand Down
10 changes: 5 additions & 5 deletions drizzle-orm/src/mysql-core/columns/datetime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<TName extends string> = MySqlDateTimeBuilder<{
name: TName;
Expand All @@ -15,7 +15,7 @@ export type MySqlDateTimeBuilderInitial<TName extends string> = MySqlDateTimeBui
}>;

export class MySqlDateTimeBuilder<T extends ColumnBuilderBaseConfig<'date', 'MySqlDateTime'>>
extends MySqlColumnBuilder<T, MySqlDatetimeConfig>
extends MySqlDateColumnBaseBuilder<T, MySqlDatetimeConfig>
{
static readonly [entityKind]: string = 'MySqlDateTimeBuilder';

Expand All @@ -35,7 +35,7 @@ export class MySqlDateTimeBuilder<T extends ColumnBuilderBaseConfig<'date', 'MyS
}
}

export class MySqlDateTime<T extends ColumnBaseConfig<'date', 'MySqlDateTime'>> extends MySqlColumn<T> {
export class MySqlDateTime<T extends ColumnBaseConfig<'date', 'MySqlDateTime'>> extends MySqlDateBaseColumn<T> {
static readonly [entityKind]: string = 'MySqlDateTime';

readonly fsp: number | undefined;
Expand Down Expand Up @@ -69,7 +69,7 @@ export type MySqlDateTimeStringBuilderInitial<TName extends string> = MySqlDateT
}>;

export class MySqlDateTimeStringBuilder<T extends ColumnBuilderBaseConfig<'string', 'MySqlDateTimeString'>>
extends MySqlColumnBuilder<T, MySqlDatetimeConfig>
extends MySqlDateColumnBaseBuilder<T, MySqlDatetimeConfig>
{
static readonly [entityKind]: string = 'MySqlDateTimeStringBuilder';

Expand All @@ -89,7 +89,7 @@ export class MySqlDateTimeStringBuilder<T extends ColumnBuilderBaseConfig<'strin
}
}

export class MySqlDateTimeString<T extends ColumnBaseConfig<'string', 'MySqlDateTimeString'>> extends MySqlColumn<T> {
export class MySqlDateTimeString<T extends ColumnBaseConfig<'string', 'MySqlDateTimeString'>> extends MySqlDateBaseColumn<T> {
static readonly [entityKind]: string = 'MySqlDateTimeString';

readonly fsp: number | undefined;
Expand Down