- Introduced new database migration for enhanced user and organization management. - Updated package dependencies to include new Better Auth modules for API keys, Expo, and i18n. - Implemented SMS functionality for phone verification and password resets. - Enhanced authentication plugins with username and phone number support. - Added performance configuration options for session cookie caching and API key updates. - Updated email templates to include organization invitation messages. - Improved testing coverage for new features and configurations.
2.4 KiB
Auth Migrations
This project keeps two migration artifacts separate:
- Better Auth target schema: the full schema generated from
src/auth.migration.ts. - D1 migrations: incremental SQL files applied by Wrangler to the current database.
The target schema is a reference snapshot. Do not apply it directly to a production database that already has tables.
Generate
Run this after changing Better Auth options or plugins that affect persistence:
pnpm db:generate
Review the generated SQL in:
docs/schema/better-auth-target.sql
This file is generated from an in-memory SQLite database because Better Auth CLI needs a database adapter to discover the target schema. That is acceptable for schema generation, but it only represents the desired final shape.
Check
CI and local readiness use:
pnpm db:check
This command generates SQL into a temporary file with Better Auth CLI and compares it with the committed migration file.
The check compares against:
docs/schema/better-auth-target.sql
Apply Locally
pnpm db:apply:local
Do not use auth migrate for D1. D1 migrations are applied by Wrangler so local and remote environments use the same migration history.
Production Migration Rule
Production D1 databases may already contain an older Better Auth schema. In that case:
-
Export a backup and current schema before changing anything:
wrangler d1 export cfw-auth --remote --output=backup.sql wrangler d1 export cfw-auth --remote --output=prod-schema.sql --no-data wrangler d1 migrations list cfw-auth --remote -
Treat
docs/schema/better-auth-target.sqlas the target. -
Write incremental files under
migrations/that move the existing production schema to the target. -
Apply with Wrangler:
wrangler d1 migrations apply cfw-auth --remote
Current Migration Layout
migrations/0001_baseline_existing_auth.sqlrecords a baseline for databases that already have the older auth tables.migrations/0002_add_better_auth_account_center.sqladds the current account-center tables, fields, indexes, API key table, and JWTjwkstable.
If a brand-new empty D1 database is needed, do not use the baseline path as-is. Either apply a full initial schema generated from docs/schema/better-auth-target.sql, or create a separate fresh-database migration sequence.