# Evolve a deployed site's schema

> Change the schema and content model of a live EmDash site, and keep your seed file in sync.

Source: https://emdashcmseverything.com/official-docs/deployment/schema-evolution/

EmDash stores collections, fields, and taxonomies in the database beside the content. Use this guide to change that live content model without confusing it with a code deploy, a first-time seed, or an EmDash core migration. The examples use Cloudflare D1; the same separation applies to every [database adapter](https://emdashcmseverything.com/official-docs/deployment/database/).

## What changes what

A site goes through four distinct workflows. Each one touches a different layer:

| Workflow                       | What changes                       | How                                                                    |
| ------------------------------ | ---------------------------------- | ---------------------------------------------------------------------- |
| Content editing                | Entries, media, settings           | Admin panel or [content API](https://emdashcmseverything.com/official-docs/reference/api/)                          |
| Code deploy                    | Templates, config, EmDash version  | `wrangler deploy` — may migrate EmDash-managed database tables         |
| First-time bootstrap           | Everything, from empty             | Migrations + seed file + setup wizard, automatic on first boot         |
| Schema evolution               | Collections, fields, taxonomies    | Admin panel or `emdash schema` against the live site (this page)       |

The seed file only participates in the third row. Its schema and structure are applied once, on the first request before the setup wizard has been completed. Deploying a changed seed file against an existing database does nothing — evolving a live site's schema always happens through the admin panel or the API.

> **Note**
>
> Core migrations update EmDash's internal tables and the physical layout of collection tables. Runtime
> migration remains automatic by default, or a deployment can apply the exact build manifest before
> traffic. Core migrations do not add or remove your collections, fields, or taxonomies. PostgreSQL
> deployments must retain the [required schema ownership and
> permissions](https://emdashcmseverything.com/official-docs/deployment/database/#database-role-requirements). See [Manage Core Database
> Migrations](https://emdashcmseverything.com/official-docs/deployment/core-migrations/) for the deployment workflow.

## Change the schema in the admin panel

The admin panel is the primary way to evolve a deployed site. Open **Content Types** in the admin and add, edit, or remove collections and fields. Changes take effect immediately — the content API, the loader, and the editing UI all read the schema from the database at runtime.

See [Collections & Fields](https://emdashcmseverything.com/official-docs/concepts/collections/) for the available field types, validation rules, and widget options.

After changing the schema, regenerate the TypeScript types your templates use. The `emdash types` command reads the schema from a running instance, so it can point at the deployed site directly:

```bash
npx emdash types --url https://example.com
```

## Change the schema from the CLI

The `emdash schema` commands talk to a running instance over its REST API, so they work against a deployed site the same way they work against local dev. Authenticate once with the device flow:

```bash
npx emdash login --url https://example.com
```

Alternatively, create an API token in the admin under **Settings → API Tokens** and pass it with `--token` or the `EMDASH_TOKEN` environment variable — useful for CI.

Then evolve the schema with the same commands you would use locally:

```bash
npx emdash schema add-field posts subtitle --type string --label "Subtitle" --url https://example.com
npx emdash schema remove-field posts legacy_field --url https://example.com
npx emdash schema create projects --label Projects --url https://example.com
```

These commands can be checked into a script so each environment receives the same ordered change. The commands are not automatically idempotent: rerunning `create` or `add-field` against an object that already exists can fail. Inspect the target with `emdash schema list` or `get`, record which environment completed each step, and stop on the first error.

See the [CLI reference](https://emdashcmseverything.com/official-docs/reference/cli/#emdash-schema) for the full command list.

> **Caution**
>
> Removing a field deletes its column and values. A JSON export cannot restore them. Take a raw
> database backup, then test the exact command against a disposable copy before production. See
> [Rehearse changes on a preview environment](#rehearse-changes-on-a-preview-environment).

## Keep the seed file in sync

The seed file embedded in your build determines what a **fresh** database initializes to: a new preview environment, a disaster-recovery rebuild, or a second deployment of the same site. If the seed still describes the starter blog while production has evolved into something else, every fresh environment bootstraps with the wrong model.

The build embeds the first seed file found at `.emdash/seed.json`, the path in `package.json#emdash.seed`, or `seed/seed.json`. If none is present, a built-in default seed (the starter blog model) is embedded, and `astro dev` logs a warning.

After evolving a deployed site's schema, export the live model back into your repository. `emdash export-seed` reads a local SQLite file. Create the SQL files as described in [Create an offsite D1 dump](https://emdashcmseverything.com/official-docs/guides/backups/#create-an-offsite-d1-dump), load the tables and rows into a local database, and export the seed:

```bash
sqlite3 prod.db < backup-schema.sql
sqlite3 prod.db < backup-folders.sql
sqlite3 prod.db < backup-data.sql
npx emdash export-seed --database prod.db > .emdash/seed.json
```

The exported seed contains the settings, collections, taxonomies, menus, redirects, widget areas, and sections of the live site. Add `--with-content` to include entries. Commit the updated `.emdash/seed.json` together with the code that depends on the new schema, so a fresh environment always bootstraps to a model the code understands.

## Rehearse changes on a preview environment

A destructive schema change (removing a field, restructuring a collection) is safest rehearsed against a disposable copy of production.

1. Create a separate preview D1 database and let Wrangler add it to the `preview` environment:

   ```bash
   npx wrangler d1 create emdash-db-preview \
     --binding DB --env preview --update-config
   ```

   Confirm that `env.preview.d1_databases` contains the new database name and UUID. Bindings are not inherited from the top-level Wrangler configuration.

2. Create the SQL files from production as described in [Create an offsite D1 dump](https://emdashcmseverything.com/official-docs/guides/backups/#create-an-offsite-d1-dump), then import them in order through the preview environment's `DB` binding:

   ```bash
   npx wrangler d1 execute DB --env preview --remote --file=./backup-schema.sql
   npx wrangler d1 execute DB --env preview --remote --file=./backup-folders.sql
   npx wrangler d1 execute DB --env preview --remote --file=./backup-data.sql
   npx wrangler d1 execute DB --env preview --remote --file=./backup-indexes.sql
   ```

   The preview site rebuilds its search index on the first call to the search API, as described in that section.

3. Build the project, deploy it to the preview environment, then run the schema change against the preview URL:

   ```bash
   npm run build
   npx wrangler deploy --env preview
   npx emdash schema remove-field posts legacy_field --url https://preview.example.com
   ```

4. Verify the public pages, admin forms, generated types, and any template that reads the changed fields. Take a fresh production database backup, then run the same commands once against production.

## Recover from a wrong turn

- **A field was removed by mistake.** The column and its data are gone from the live database. Restore from a D1 [Time Travel](https://developers.cloudflare.com/d1/reference/time-travel/) point-in-time backup, or re-add the field and restore its values from an earlier [offsite D1 dump](https://emdashcmseverything.com/official-docs/guides/backups/#create-an-offsite-d1-dump).
- **A fresh environment bootstrapped with the wrong model.** The embedded seed was stale or missing. Update `.emdash/seed.json` (see [Keep the seed file in sync](#keep-the-seed-file-in-sync)), rebuild, and point the deploy at an empty database to bootstrap again.
- **The schema and the templates disagree.** Deploys and schema changes are independent, so order them deliberately: additive schema changes (new collection, new optional field) go first, then the code that uses them. For removals, deploy the code that stops using the field first, then remove the field.
