Source: guides/schema_evolution.md #
On this page
Generated automatically from the published contract sources.
Source path: guides/schema_evolution.md.
Contract text #
Schema Evolution — the served guide #
This guide explains how you change a database schema. Its instructions derive from the requirements it cites; it declares no requirements of its own (CTX-06; CHI-L0-13).
A server application can invoke the database package's migration function before it starts serving. Merely including migration files does not run them on a server. For a serverless application, the platform applies the version's migration files before serving it at deploy, promote or rollback (DBS-L0-06).
The development operation names below describe work to implement with your project's tooling. They are not installed Turn Zero Cloud CLI commands. The coordinated migration and snapshot actions described under Production are planned, not available in the current build.
The discipline #
Schema changes are migration files. Keep each database schema change in an ordered, versioned migration file in the application's repository. Apply the files in order. Once production has started a version carrying a migration, never edit it; write a new migration for the next change, and edit an earlier one only as the next paragraph allows. The planned platform migration action associates the applied migration set with an environment's backend version (PLD-L0-55).
When a file may still be edited. A file is applied in a database once that database's schema_migrations table records its name, beside a hash of the file's content (DB-L0-03). A migration walk that finds a recorded file edited fails before any file runs, naming the file, so an edit is never skipped in silence. Edit a file freely until a database records it. A file only the development database has applied may still be edited until production starts a version carrying it, at the promote with two environments or at the deploy with one. Edit it, then clear the development database with clear_development_database, so the next migration walk applies the edited file (DBS-L0-10). Once production has started a version carrying the file, a failed deploy included, never edit or rename it; write a new migration instead.
Identify destructive changes. Identify migrations that can remove or rewrite data, such as dropping a table or column, narrowing a type, or rewriting rows. Explain the expected data loss before running one. Marking a file destructive does not make the current migration walk request browser approval or take a snapshot. Verify the target database and arrange the required backup before applying it.
The development commands #
Six development operations follow (CHI-L0-13). Provide them through the application's chosen database tooling, and document the actual commands in that application's development instructions:
- migrate — apply pending migrations to the development database.
- reset — rebuild the development database by replaying all migrations. This deletes its existing data; verify the database connection before running it.
- seed — load a known development data set, usually after a reset.
- status — report which migrations are applied and which remain pending.
- diff — compare the database schema with the intended schema and draft a migration for the differences. Review the generated SQL before applying it.
- snapshot — create a restorable copy before a risky change, using the backup tools available for that database.
A local run reads the development environment file, which the turnzero command's provision line writes for the development environment, so it reaches the development database and never the production database (PLD-L0-40). A reset or a seed run locally therefore changes development data alone. Point these commands at the development connection string alone. The platform never returns the production connection credential to you (SEC-L0-07).
Production #
Current behavior. A server application applies migrations before serving, through the database package's migration function or its own code (DBS-L0-06; DB-L0-03). Its own code keeps the same convention: ordered files, a ledger table, and a transaction advisory lock inside each transaction it guards. If its walk fails, the process must refuse to serve.
For a serverless application, put the SQL files directly under migrations/ at the artifact's root. The platform runs the package's walk before the version serves at deploy, promote or rollback. It keeps these files beside the version's function tree and uses that version's kept files for promote and rollback. Files in nested folders and files without the lowercase .sql ending do not run. A function never calls the migration walk. A restart or platform redeploy runs no serverless migration.
Each walk targets the environment's own database. A development deploy or local server run uses development. A promote, rollback or one-environment production deploy uses production (PLD-L0-96; PLD-L0-43). Pending files apply in order, and each file commits separately. If a later file fails, earlier successful files remain applied. A failed serverless walk fails the act and keeps the earlier version serving. Where none was serving, nothing serves afterwards. The platform opens its own direct connection as the application's database role, with verified TLS (DBS-L0-06).
Neither walk increments a backend version or takes a pre-migration snapshot. Reverting the artifact does not undo database changes. Review each migration and its recovery procedure before deployment.
Planned migration action. The planned apply_migration action will pause traffic in a declared maintenance state, take a pre-migration snapshot, apply pending migrations, increment the backend version, and resume with the matching client promoted atomically. It will also recover on failure (PLD-L0-56). The action is not registered in the current build: do not call it, tell anyone to call it, or rely on these safeguards today.
Client handshake. The client handshake is served for the native clients a realm declares (SVC-L0-11; ACS-L0-15). The client sends x-turnzero-cloud-client: <client_id>/<version> on each request, and the router refuses a version below the client's minimum_version with 426 client_upgrade_required, naming the minimum and the update_url. Every application response carries x-turnzero-cloud-version, the serving deploy version. Before a schema change an installed client cannot survive, read each client's versions_seen through read_app_sign_in, then raise its minimum_version with configure_app_sign_in, whose warnings name the versions in use that the raise refuses. A request without the header is never refused, so keep a web client and any undeclared client compatible through the application's tests. Neither current migration walk performs the planned backend version increment (PLD-L0-55).
Rollback and restore. The planned rollback within one backend version does not change the database version (PLD-L0-57). The current roll_back action promotes an earlier deployed version and takes no snapshot (PLD-L0-43; PLD-L0-63). For a serverless version, it runs that version's kept migration files before serving it. This can apply pending files and never undoes previously committed changes (DBS-L0-06).
Across a data-model change, ask the account's owner to choose a recovery route. Use the application's own database tooling to take a pre-migration copy if you plan to restore it. Restoring that copy discards later writes. You can also apply a separately authored down-migration. The platform's restore_snapshot action and promote-time snapshot are not registered in the current build. Explain the data effects and use a verified recovery procedure appropriate to the implemented database tooling.
For a dedicated production database, you can roll back to a version without the migration and then restore the database (API-L0-33). Verify that the earlier version is serving before you request restore_database. Choose a time before the migration's applied timestamp in schema_migrations. Explain the loss of writes made after that time and ask the owner to approve the restore in the browser.
Source Markdown #
# Schema Evolution — the served guide
This guide explains how you change a database schema. Its instructions derive from the requirements it cites; it declares no requirements of its own (CTX-06; CHI-L0-13).
A server application can invoke the database package's migration function before it starts serving. Merely including migration files does not run them on a server. For a serverless application, the platform applies the version's migration files before serving it at deploy, promote or rollback (DBS-L0-06).
The development operation names below describe work to implement with your project's tooling. They are not installed Turn Zero Cloud CLI commands. The coordinated migration and snapshot actions described under Production are planned, not available in the current build.
## The discipline
**Schema changes are migration files.** Keep each database schema change in an ordered, versioned migration file in the application's repository. Apply the files in order. Once production has started a version carrying a migration, never edit it; write a new migration for the next change, and edit an earlier one only as the next paragraph allows. The planned platform migration action associates the applied migration set with an environment's backend version (PLD-L0-55).
**When a file may still be edited.** A file is applied in a database once that database's `schema_migrations` table records its name, beside a hash of the file's content (DB-L0-03). A migration walk that finds a recorded file edited fails before any file runs, naming the file, so an edit is never skipped in silence. Edit a file freely until a database records it. A file only the development database has applied may still be edited until production starts a version carrying it, at the promote with two environments or at the deploy with one. Edit it, then clear the development database with `clear_development_database`, so the next migration walk applies the edited file (DBS-L0-10). Once production has started a version carrying the file, a failed deploy included, never edit or rename it; write a new migration instead.
**Identify destructive changes.** Identify migrations that can remove or rewrite data, such as dropping a table or column, narrowing a type, or rewriting rows. Explain the expected data loss before running one. Marking a file destructive does not make the current migration walk request browser approval or take a snapshot. Verify the target database and arrange the required backup before applying it.
## The development commands
Six development operations follow (CHI-L0-13). Provide them through the application's chosen database tooling, and document the actual commands in that application's development instructions:
- **migrate** — apply pending migrations to the development database.
- **reset** — rebuild the development database by replaying all migrations. This deletes its existing data; verify the database connection before running it.
- **seed** — load a known development data set, usually after a reset.
- **status** — report which migrations are applied and which remain pending.
- **diff** — compare the database schema with the intended schema and draft a migration for the differences. Review the generated SQL before applying it.
- **snapshot** — create a restorable copy before a risky change, using the backup tools available for that database.
A local run reads the development environment file, which the turnzero command's `provision` line writes for the development environment, so it reaches the development database and never the production database (PLD-L0-40). A reset or a seed run locally therefore changes development data alone. Point these commands at the development connection string alone. The platform never returns the production connection credential to you (SEC-L0-07).
## Production
**Current behavior.** A server application applies migrations before serving, through the database package's migration function or its own code (DBS-L0-06; DB-L0-03). Its own code keeps the same convention: ordered files, a ledger table, and a transaction advisory lock inside each transaction it guards. If its walk fails, the process must refuse to serve.
For a serverless application, put the SQL files directly under `migrations/` at the artifact's root. The platform runs the package's walk before the version serves at deploy, promote or rollback. It keeps these files beside the version's function tree and uses that version's kept files for promote and rollback. Files in nested folders and files without the lowercase `.sql` ending do not run. A function never calls the migration walk. A restart or platform redeploy runs no serverless migration.
Each walk targets the environment's own database. A development deploy or local server run uses development. A promote, rollback or one-environment production deploy uses production (PLD-L0-96; PLD-L0-43). Pending files apply in order, and each file commits separately. If a later file fails, earlier successful files remain applied. A failed serverless walk fails the act and keeps the earlier version serving. Where none was serving, nothing serves afterwards. The platform opens its own direct connection as the application's database role, with verified TLS (DBS-L0-06).
Neither walk increments a backend version or takes a pre-migration snapshot. Reverting the artifact does not undo database changes. Review each migration and its recovery procedure before deployment.
**Planned migration action.** The planned `apply_migration` action will pause traffic in a declared maintenance state, take a pre-migration snapshot, apply pending migrations, increment the backend version, and resume with the matching client promoted atomically. It will also recover on failure (PLD-L0-56). The action is not registered in the current build: do not call it, tell anyone to call it, or rely on these safeguards today.
**Client handshake.** The client handshake is served for the native clients a realm declares (SVC-L0-11; ACS-L0-15). The client sends `x-turnzero-cloud-client: <client_id>/<version>` on each request, and the router refuses a version below the client's `minimum_version` with 426 `client_upgrade_required`, naming the minimum and the `update_url`. Every application response carries `x-turnzero-cloud-version`, the serving deploy version. Before a schema change an installed client cannot survive, read each client's `versions_seen` through `read_app_sign_in`, then raise its `minimum_version` with `configure_app_sign_in`, whose `warnings` name the versions in use that the raise refuses. A request without the header is never refused, so keep a web client and any undeclared client compatible through the application's tests. Neither current migration walk performs the planned backend version increment (PLD-L0-55).
**Rollback and restore.** The planned rollback within one backend version does not change the database version (PLD-L0-57). The current `roll_back` action promotes an earlier deployed version and takes no snapshot (PLD-L0-43; PLD-L0-63). For a serverless version, it runs that version's kept migration files before serving it. This can apply pending files and never undoes previously committed changes (DBS-L0-06).
Across a data-model change, ask the account's owner to choose a recovery route. Use the application's own database tooling to take a pre-migration copy if you plan to restore it. Restoring that copy discards later writes. You can also apply a separately authored down-migration. The platform's `restore_snapshot` action and promote-time snapshot are not registered in the current build. Explain the data effects and use a verified recovery procedure appropriate to the implemented database tooling.
For a dedicated production database, you can roll back to a version without the migration and then restore the database (API-L0-33). Verify that the earlier version is serving before you request `restore_database`. Choose a time before the migration's `applied` timestamp in `schema_migrations`. Explain the loss of writes made after that time and ask the owner to approve the restore in the browser.