Operations

Upgrades and Schema Changes

What a deploy changes in the database, and how to change fields without losing content.

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:

  1. The migrations of Tenon, then those of better-auth and its plugins.
  2. A check of the localized fields.
  3. seed and onInit. 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:sqlite and 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:

ChangeExisting documents
Add a fieldHave no value. default applies only to new documents.
Add a required fieldHave no value. The next publish asks for it.
Remove a fieldKeep the value in the database, but reads leave it out. The next save drops it.
Rename a fieldThe same as removing the old field and adding a new one. The values do not move.
Change the type of a fieldKeep the old value. Reads return it as stored, and the next save fails until an editor fixes it.
Add a rule like unique or maxLengthKeep their values. The rule applies on the next save.
Change localizedThe values move at start. See Localized fields.
Change media.imagesKeep 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:

  1. Add the new field and keep the old one. Deploy.
  2. 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 });
      }
    }
    
  3. Remove the old field. Deploy.
  • Each cms.update() publishes, and runs the hooks and the validation like a save in the admin.
  • The imported cms reads published content. To copy drafts too, read with cms.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 localized on again brings them back.
  • A new defaultLocale moves nothing. Tenon logs a warning with the number of documents that have no row in it.
  • A locale you remove from locales keeps 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:

  1. Read the release notes of every version in between.
  2. Back up with npx tenon backup.
  3. Run npx tenon upgrade.
  4. Start the project locally. The new migrations run on the first request.
  5. 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:

  1. Install the old version of tenoncms again.
  2. Point it to a new, empty database.
  3. 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.