Upgrades and Schema Changes
A deploy can change two things: your schema, like a new or renamed field, and the version of Tenon. Neither needs a command. Tenon updates its own tables when it starts, and stored content stays as it is until an editor saves it. This page explains what happens to existing content after each kind of change, and how to upgrade safely.
How Tenon updates the database
Tenon keeps its tables up to date with migrations. They are code inside tenoncms, and they run on the first database query of a process:
- The migrations of Tenon, then those of better-auth and its plugins.
- A check of the localized fields.
seedandonInit. See Lifecycle hooks.
- On Postgres, PGlite and libSQL, all migrations run in one transaction. If one fails, nothing changes, and the next query tries again.
- Serverless hosts start many instances at once. On Postgres and libSQL, they wait for each other, so only one migrates.
node:sqliteand Durable Objects have no lock between processes. Run one process per database.
Your own fields need no migration. All documents live in one table as JSON, so a new field is just a new key.
Change your schema
Tenon does not rewrite stored documents when you change a field. Each document keeps what it has until its next save, and the save applies the new schema:
| Change | Existing documents |
|---|---|
| Add a field | Have no value. default applies only to new documents. |
Add a required field | Have no value. The next publish asks for it. |
| Remove a field | Keep the value in the database, but reads leave it out. The next save drops it. |
| Rename a field | The same as removing the old field and adding a new one. The values do not move. |
| Change the type of a field | Keep the old value. Reads return it as stored, and the next save fails until an editor fixes it. |
Add a rule like unique or maxLength | Keep their values. The rule applies on the next save. |
Change localized | The values move at start. See Localized fields. |
Change media.images | Keep their old variants. Run tenon media regenerate. |
Rename a field
A rename loses the values of the old field. To keep them, rename in two deploys:
- Add the new field and keep the old one. Deploy.
- Copy the values with a script:scripts/rename-summary.ts
import cms from "../tenon.config"; const posts = await cms.find("posts"); for (const post of posts) { if (post.summary && !post.excerpt) { await cms.update("posts", post.id, { excerpt: post.summary }); } } - Remove the old field. Deploy.
- Each
cms.update()publishes, and runs the hooks and the validation like a save in the admin. - The imported
cmsreads published content. To copy drafts too, read withcms.withDrafts()and save with{ draft: true }. - Test the script on a copy first: restore a backup into your local project.
Remove a collection or a global
Its documents stay in the database. Reads cannot reach them any more, but a backup still holds them. A restore of that backup into your config then stops, because the archive names a collection your config does not have.
To remove a collection cleanly, delete its documents first, in the admin or with cms.delete(). Then remove it from the config.
Localized fields
When you turn localized on or off for a field, Tenon moves the stored values on the next start: from the shared data to the row of the default locale, or back. It logs each move, for example moved posts.title to locale rows (12 documents).
- The other locales keep their rows. Reads ignore them while the field is shared, so turning
localizedon again brings them back. - A new
defaultLocalemoves nothing. Tenon logs a warning with the number of documents that have no row in it. - A locale you remove from
localeskeeps its rows. Reads ignore them.
See Localization.
Upgrade Tenon
tenon upgrade installs the latest version. Below 1.0, a new minor version MAY break your project:
- Read the release notes of every version in between.
- Back up with
npx tenon backup. - Run
npx tenon upgrade. - Start the project locally. The new migrations run on the first request.
- Deploy. Production runs the same migrations on its first request.
See tenon upgrade.
Roll back
Migrations only go forward. An older version of tenoncms refuses to start on a database that a newer version migrated, with an error like corrupted migrations: previously executed migration … is missing.
To go back:
- Install the old version of
tenoncmsagain. - Point it to a new, empty database.
- Restore the backup you made before the upgrade with
tenon restore.
A full backup restores only into the same version that wrote it, so it fits the old version. Content written since the backup is lost, so roll back soon, or test the upgrade locally first.
Related resources
- tenon upgrade: the command, step by step.
- Backup and restore: back up before a change.
- Fields:
default,requiredand the other field options. - Localization: locales and localized fields.