[PR #3906] [MERGED] fix(db): add FK onDelete cascade and CURRENT_TIMESTAMP defaults on generation #5075

Closed
opened 2026-03-13 12:09:56 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/better-auth/better-auth/pull/3906
Author: @Kinfe123
Created: 8/10/2025
Status: Merged
Merged: 8/14/2025
Merged by: @Bekacru

Base: mainHead: fix/generation-raw-file


📝 Commits (5)

  • 889100c fix(db): add FK onDelete (cascade) and CURRENT_TIMESTAMP defaults for date columns
  • 26a3fef default generate
  • 5ebb8fb console
  • 13204ae update
  • 3e2a4ef Merge branch 'main' into fix/generation-raw-file

📊 Changes

2 files changed (+28 additions, -8 deletions)

View changed files

📝 packages/better-auth/src/db/get-migration.ts (+26 -6)
📝 packages/cli/test/__snapshots__/migrations.sql (+2 -2)

📄 Description

before

create table "user" ("id" text not null primary key, "name" text not null, "email" text not null unique, "email_verified" boolean not null, "image" text, "created_at" text, "updated_at" text, "phone_number" text unique, "phone_number_verified" boolean, "first_name" text, "last_name" text, "bio" text, "company" text, "location" text, "github_username" text, "public_repos" integer, "followers" integer, "following" integer, "two_factor_enabled" boolean, "profile_url" text, "avatar_url" text, "username" text, "description" text, "is_verified" boolean, "twitter_handle" text);

create table "session" ("id" text not null primary key, "expires_at" timestamp not null, "token" text not null unique, "created_at" timestamp not null, "updated_at" timestamp not null, "ip_address" text, "user_agent" text, "user_id" text not null references "user" ("id"), "active_organization_id" text);

create table "account" ("id" text not null primary key, "account_id" text not null, "provider_id" text not null, "user_id" text not null references "user" ("id"), "access_token" text, "refresh_token" text, "id_token" text, "access_token_expires_at" timestamp, "refresh_token_expires_at" timestamp, "scope" text, "password" text, "created_at" timestamp not null, "updated_at" timestamp not null);

create table "verification" ("id" text not null primary key, "identifier" text not null, "value" text not null, "expires_at" timestamp not null, "created_at" timestamp, "updated_at" timestamp);

create table "jwks" ("id" text not null primary key, "public_key" text not null, "private_key" text not null, "created_at" timestamp not null);

create table "organization" ("id" text not null primary key, "name" text not null, "slug" text not null unique, "logo" text, "created_at" timestamp not null, "metadata" text);

create table "member" ("id" text not null primary key, "organization_id" text not null references "organization" ("id"), "user_id" text not null references "user" ("id"), "role" text not null, "created_at" timestamp not null);

create table "invitation" ("id" text not null primary key, "organization_id" text not null references "organization" ("id"), "email" text not null, "role" text, "status" text not null, "expires_at" timestamp not null, "inviter_id" text not null references "user" ("id")); 

after patch

alter table "session" add column "expires_at" timestamp not null;

alter table "session" add column "created_at" timestamp not null;

alter table "session" add column "updated_at" timestamp not null;

alter table "session" add column "ip_address" text;

alter table "session" add column "user_agent" text;

alter table "session" add column "user_id" text not null references "user" ("id") on delete cascade;

alter table "session" add column "active_organization_id" text;

alter table "jwks" add column "public_key" text not null;

alter table "jwks" add column "private_key" text not null;

alter table "jwks" add column "created_at" timestamp not null;

alter table "organization" add column "created_at" timestamp not null;

alter table "member" add column "organization_id" text not null references "organization" ("id") on delete cascade;

alter table "member" add column "user_id" text not null references "user" ("id") on delete cascade;

alter table "member" add column "created_at" timestamp not null;

alter table "invitation" add column "organization_id" text not null references "organization" ("id") on delete cascade;

alter table "invitation" add column "expires_at" timestamp not null;

alter table "invitation" add column "inviter_id" text not null references "user" ("id") on delete cascade;

create table "user" ("id" text not null primary key, "name" text not null, "email" text not null unique, "email_verified" boolean not null, "image" text, "created_at" timestamp default CURRENT_TIMESTAMP not null, "updated_at" timestamp default CURRENT_TIMESTAMP not null, "phone_number" text unique, "phone_number_verified" boolean, "first_name" text, "last_name" text, "bio" text, "company" text, "location" text, "github_username" text, "public_repos" integer, "followers" integer, "following" integer, "two_factor_enabled" boolean, "profile_url" text, "avatar_url" text, "username" text, "description" text, "is_verified" boolean, "twitter_handle" text);

create table "account" ("id" text not null primary key, "account_id" text not null, "provider_id" text not null, "user_id" text not null references "user" ("id") on delete cascade, "access_token" text, "refresh_token" text, "id_token" text, "access_token_expires_at" timestamp, "refresh_token_expires_at" timestamp, "scope" text, "password" text, "created_at" timestamp not null, "updated_at" timestamp not null);

create table "verification" ("id" text not null primary key, "identifier" text not null, "value" text not null, "expires_at" timestamp not null, "created_at" timestamp default CURRENT_TIMESTAMP, "updated_at" timestamp default CURRENT_TIMESTAMP);

Summary by cubic

Added onDelete: cascade to foreign key constraints and set CURRENT_TIMESTAMP as the default for date columns in generated database tables and migrations.

  • Migration
    • Foreign keys now delete related rows automatically.
    • Date columns default to the current timestamp if not provided.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/better-auth/better-auth/pull/3906 **Author:** [@Kinfe123](https://github.com/Kinfe123) **Created:** 8/10/2025 **Status:** ✅ Merged **Merged:** 8/14/2025 **Merged by:** [@Bekacru](https://github.com/Bekacru) **Base:** `main` ← **Head:** `fix/generation-raw-file` --- ### 📝 Commits (5) - [`889100c`](https://github.com/better-auth/better-auth/commit/889100ca4208b660d718e4423d789f6f312d7718) fix(db): add FK onDelete (cascade) and CURRENT_TIMESTAMP defaults for date columns - [`26a3fef`](https://github.com/better-auth/better-auth/commit/26a3fefc047b07186e4de825930c9aaa427a07f0) default generate - [`5ebb8fb`](https://github.com/better-auth/better-auth/commit/5ebb8fb2212512a2891a78e912fb9820d8a2c9dc) console - [`13204ae`](https://github.com/better-auth/better-auth/commit/13204ae35decc84a24c041ad203d6b79266262c6) update - [`3e2a4ef`](https://github.com/better-auth/better-auth/commit/3e2a4ef1e75dbd05ea6387319c5a9d52f4dc8d18) Merge branch 'main' into fix/generation-raw-file ### 📊 Changes **2 files changed** (+28 additions, -8 deletions) <details> <summary>View changed files</summary> 📝 `packages/better-auth/src/db/get-migration.ts` (+26 -6) 📝 `packages/cli/test/__snapshots__/migrations.sql` (+2 -2) </details> ### 📄 Description before ```sql create table "user" ("id" text not null primary key, "name" text not null, "email" text not null unique, "email_verified" boolean not null, "image" text, "created_at" text, "updated_at" text, "phone_number" text unique, "phone_number_verified" boolean, "first_name" text, "last_name" text, "bio" text, "company" text, "location" text, "github_username" text, "public_repos" integer, "followers" integer, "following" integer, "two_factor_enabled" boolean, "profile_url" text, "avatar_url" text, "username" text, "description" text, "is_verified" boolean, "twitter_handle" text); create table "session" ("id" text not null primary key, "expires_at" timestamp not null, "token" text not null unique, "created_at" timestamp not null, "updated_at" timestamp not null, "ip_address" text, "user_agent" text, "user_id" text not null references "user" ("id"), "active_organization_id" text); create table "account" ("id" text not null primary key, "account_id" text not null, "provider_id" text not null, "user_id" text not null references "user" ("id"), "access_token" text, "refresh_token" text, "id_token" text, "access_token_expires_at" timestamp, "refresh_token_expires_at" timestamp, "scope" text, "password" text, "created_at" timestamp not null, "updated_at" timestamp not null); create table "verification" ("id" text not null primary key, "identifier" text not null, "value" text not null, "expires_at" timestamp not null, "created_at" timestamp, "updated_at" timestamp); create table "jwks" ("id" text not null primary key, "public_key" text not null, "private_key" text not null, "created_at" timestamp not null); create table "organization" ("id" text not null primary key, "name" text not null, "slug" text not null unique, "logo" text, "created_at" timestamp not null, "metadata" text); create table "member" ("id" text not null primary key, "organization_id" text not null references "organization" ("id"), "user_id" text not null references "user" ("id"), "role" text not null, "created_at" timestamp not null); create table "invitation" ("id" text not null primary key, "organization_id" text not null references "organization" ("id"), "email" text not null, "role" text, "status" text not null, "expires_at" timestamp not null, "inviter_id" text not null references "user" ("id")); ``` after patch ```sql alter table "session" add column "expires_at" timestamp not null; alter table "session" add column "created_at" timestamp not null; alter table "session" add column "updated_at" timestamp not null; alter table "session" add column "ip_address" text; alter table "session" add column "user_agent" text; alter table "session" add column "user_id" text not null references "user" ("id") on delete cascade; alter table "session" add column "active_organization_id" text; alter table "jwks" add column "public_key" text not null; alter table "jwks" add column "private_key" text not null; alter table "jwks" add column "created_at" timestamp not null; alter table "organization" add column "created_at" timestamp not null; alter table "member" add column "organization_id" text not null references "organization" ("id") on delete cascade; alter table "member" add column "user_id" text not null references "user" ("id") on delete cascade; alter table "member" add column "created_at" timestamp not null; alter table "invitation" add column "organization_id" text not null references "organization" ("id") on delete cascade; alter table "invitation" add column "expires_at" timestamp not null; alter table "invitation" add column "inviter_id" text not null references "user" ("id") on delete cascade; create table "user" ("id" text not null primary key, "name" text not null, "email" text not null unique, "email_verified" boolean not null, "image" text, "created_at" timestamp default CURRENT_TIMESTAMP not null, "updated_at" timestamp default CURRENT_TIMESTAMP not null, "phone_number" text unique, "phone_number_verified" boolean, "first_name" text, "last_name" text, "bio" text, "company" text, "location" text, "github_username" text, "public_repos" integer, "followers" integer, "following" integer, "two_factor_enabled" boolean, "profile_url" text, "avatar_url" text, "username" text, "description" text, "is_verified" boolean, "twitter_handle" text); create table "account" ("id" text not null primary key, "account_id" text not null, "provider_id" text not null, "user_id" text not null references "user" ("id") on delete cascade, "access_token" text, "refresh_token" text, "id_token" text, "access_token_expires_at" timestamp, "refresh_token_expires_at" timestamp, "scope" text, "password" text, "created_at" timestamp not null, "updated_at" timestamp not null); create table "verification" ("id" text not null primary key, "identifier" text not null, "value" text not null, "expires_at" timestamp not null, "created_at" timestamp default CURRENT_TIMESTAMP, "updated_at" timestamp default CURRENT_TIMESTAMP); ``` <!-- This is an auto-generated description by cubic. --> --- ## Summary by cubic Added onDelete: cascade to foreign key constraints and set CURRENT_TIMESTAMP as the default for date columns in generated database tables and migrations. - **Migration** - Foreign keys now delete related rows automatically. - Date columns default to the current timestamp if not provided. <!-- End of auto-generated description by cubic. --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-03-13 12:09:56 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/better-auth#5075