diff --git a/packages/better-auth/src/db/get-migration.ts b/packages/better-auth/src/db/get-migration.ts index 5d1b0e4662..61d93f62bb 100644 --- a/packages/better-auth/src/db/get-migration.ts +++ b/packages/better-auth/src/db/get-migration.ts @@ -3,6 +3,7 @@ import type { CreateTableBuilder, } from "kysely"; import type { FieldAttribute, FieldType } from "."; +import { sql } from "kysely"; import { createLogger } from "../utils/logger"; import type { BetterAuthOptions } from "../types"; import { createKyselyAdapter } from "../adapters/kysely-adapter/dialect"; @@ -239,13 +240,24 @@ export async function getMigrations(config: BetterAuthOptions) { .addColumn(fieldName, type, (col) => { col = field.required !== false ? col.notNull() : col; if (field.references) { - col = col.references( - `${field.references.model}.${field.references.field}`, - ); + col = col + .references( + `${field.references.model}.${field.references.field}`, + ) + .onDelete(field.references.onDelete || "cascade"); } if (field.unique) { col = col.unique(); } + if ( + field.type === "date" && + typeof field.defaultValue === "function" && + (dbType === "postgres" || + dbType === "mysql" || + dbType === "mssql") + ) { + col = col.defaultTo(sql`CURRENT_TIMESTAMP`); + } return col; }); migrations.push(exec); @@ -276,18 +288,26 @@ export async function getMigrations(config: BetterAuthOptions) { }, ); + const indices: Array<{ table: string; field: string }> = []; for (const [fieldName, field] of Object.entries(table.fields)) { const type = getType(field, fieldName); dbT = dbT.addColumn(fieldName, type, (col) => { col = field.required !== false ? col.notNull() : col; if (field.references) { - col = col.references( - `${field.references.model}.${field.references.field}`, - ); + col = col + .references(`${field.references.model}.${field.references.field}`) + .onDelete(field.references.onDelete || "cascade"); } if (field.unique) { col = col.unique(); } + if ( + field.type === "date" && + typeof field.defaultValue === "function" && + (dbType === "postgres" || dbType === "mysql" || dbType === "mssql") + ) { + col = col.defaultTo(sql`CURRENT_TIMESTAMP`); + } return col; }); } diff --git a/packages/cli/test/__snapshots__/migrations.sql b/packages/cli/test/__snapshots__/migrations.sql index 13b82ff1d2..3d0d5b2dbb 100644 --- a/packages/cli/test/__snapshots__/migrations.sql +++ b/packages/cli/test/__snapshots__/migrations.sql @@ -1,7 +1,7 @@ create table "user" ("id" text not null primary key, "name" text not null, "email" text not null unique, "emailVerified" integer not null, "image" text, "createdAt" date not null, "updatedAt" date not null); -create table "session" ("id" text not null primary key, "expiresAt" date not null, "token" text not null unique, "createdAt" date not null, "updatedAt" date not null, "ipAddress" text, "userAgent" text, "userId" text not null references "user" ("id")); +create table "session" ("id" text not null primary key, "expiresAt" date not null, "token" text not null unique, "createdAt" date not null, "updatedAt" date not null, "ipAddress" text, "userAgent" text, "userId" text not null references "user" ("id") on delete cascade); -create table "account" ("id" text not null primary key, "accountId" text not null, "providerId" text not null, "userId" text not null references "user" ("id"), "accessToken" text, "refreshToken" text, "idToken" text, "accessTokenExpiresAt" date, "refreshTokenExpiresAt" date, "scope" text, "password" text, "createdAt" date not null, "updatedAt" date not null); +create table "account" ("id" text not null primary key, "accountId" text not null, "providerId" text not null, "userId" text not null references "user" ("id") on delete cascade, "accessToken" text, "refreshToken" text, "idToken" text, "accessTokenExpiresAt" date, "refreshTokenExpiresAt" date, "scope" text, "password" text, "createdAt" date not null, "updatedAt" date not null); create table "verification" ("id" text not null primary key, "identifier" text not null, "value" text not null, "expiresAt" date not null, "createdAt" date, "updatedAt" date); \ No newline at end of file