Upgrades and schema versions
For: an operator who moves an API to a newer version of the authtempo packages.
What it does: this guide applies the migrations before the new code runs, so no request meets an old database.
Before you start
Section titled “Before you start”- A backup of the database. Backups tells how.
- The schema version that the new core needs:
SCHEMA_VERSION, listed on Schema and migrations.
- Read the changelog of the new version. Changelog lists each release.
- Update the
@authtempopackages of the app to the same version. Keep one version of core in the app. - On D1, apply the migrations of the adapter to the real database.
The @authtempo packages are on npm. Add the CLI to your app with pnpm add -D @authtempo/cli, and run each command as pnpm exec authtempo ….
authtempo migrate --target d1 --database my-app --remote- On Postgres, apply them once per deploy, before the first request.
The @authtempo packages are on npm. Add the CLI to your app with pnpm add -D @authtempo/cli, and run each command as pnpm exec authtempo ….
authtempo migrate --target postgres --url-env DATABASE_URL- With the compatibility package, add
--tables compatto the command of step 3 or step 4. - On D1, that command copies each new compat file under the next number of the app.
- Deploy the new code.
- Run
authtempo checkwith the base URL of the API. - Sign in once by each method that the API serves.
Upgrade with the CLI
Section titled “Upgrade with the CLI”authtempo upgrade does step 2 for you. It reads the package.json of the app and moves each @authtempo package to one version. It runs no install.
- When a crossed minor has a “Breaking:” line, the command prints the line and exits 1. It still writes the versions. Read each line and change your code before the install.
- When the schema version of the target core differs, the next steps name
authtempo migrate. Run it as in step 3 or step 4, before the deploy. authtempo upgrade --dry-runprints the change and writes nothing.
authtempo upgrade lists each flag and each exit code. Deprecations lists each item that a minor removes.
How it behaves
Section titled “How it behaves”- Each adapter reads
ta_meta.schema_versiononce. An older or missing version makes every operation throwStorageErrorwithschema_mismatch. - The table prefix
ta_keeps the old product name tempo-auth for compatibility. Schema and migrations states the rule. - A migration is append-only. A new version adds a migration and never changes an old one.
- A migration of a newer version runs safely under the older code. So apply it first, then deploy.
applyPostgresMigrationsis safe on every deploy, also from two replicas at once. Each migration takes an advisory lock and reads the applied names again.wrangler d1 migrations applyrolls back a file that fails, so a refused file changes nothing.- Version 16 fills
ta_session.provedin one statement. On D1 an app with more than 100,000 live sessions of verified users applies that fill by hand, in pages, before step 3. Schema and migrations gives the steps, and the Better Auth guide gives them for the compat tables.
If it fails
Section titled “If it fails”- Every route answers 500, and the log shows
schema_mismatch: a migration did not run. Apply it. - On D1 the file of version 16 fails at the execution limits of D1: its fill met too many sessions. The version stays 15. Apply the fill by hand, as the schema page tells.
no such tableon D1 at a compat file: its number is lower than the Better Auth table migration. Give it the next number.- Two copies of
@authtempo/corein the app: two derive gates, and twice the legacy derives at once. Keep one version.