fix(cli): add FK onDelete cascade and CURRENT_TIMESTAMP defaults on generation (#3906)

* fix(db): add FK onDelete (cascade) and CURRENT_TIMESTAMP defaults for date columns

* default generate

* console

* update
This commit is contained in:
KinfeMichael Tariku
2025-08-13 18:14:01 -07:00
committed by Bereket Engida
parent 86e3b0f7f9
commit c5c9bf0744
2 changed files with 28 additions and 8 deletions
+26 -6
View File
@@ -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;
});
}
@@ -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);