Docs / PostgreSQL
Run PostgreSQL migrations as a release command
A migration changes the database your running app depends on, so it deserves its own step with its own logs. This page shows how to run migrations as a release command, with NestJS scripts you can copy, and how to order the work so a failure is cheap to undo.
Run migrations as a release command
Run schema changes as their own step, not inside the app’s start command. A release command runs one bounded command with the exact image and bound environment of the current deployment, so the migration sees the same code and the same DATABASE_URL as the app. It has a timeout and its own logs, and you see its exit code.
Resilis generates the job for you. Raw manifests, image overrides and environment overrides are not accepted, which keeps the command within the app you deployed.
NestJS and TypeORM scripts
In NestJS, keep TypeORM’s synchronize off and point a script at the built data source. The release command then calls the script:
package.json scripts
{
"scripts": {
"build": "nest build",
"start:prod": "node dist/main",
"migration:run": "typeorm migration:run -d dist/data-source.js",
"migration:revert": "typeorm migration:revert -d dist/data-source.js"
}
} Example prompt to your agent
Run "npm run migration:run" as a release command on the current
development deployment. Show me the exit code and the last 50 log lines. Order the work so a failure is cheap
Try the migration in development, then staging, before production. Each environment has its own database, so a mistake in development touches nothing a customer uses.
Write each migration so the previous release can still run against it. Add a column before the code reads it, and remove a column only after no running release uses it. Then a failed deploy can roll back the app without a schema fight.
Turn on continuous backups before you migrate production, so you have a recovery point from just before the change. Rolling back the app does not undo a migration or restore data; the two are separate.
Check the result
Read the release command’s exit code and the end of its logs. Then open the database in the built-in browser, or query it from the app, and confirm the new table or column is there before you ship code that depends on it.
Never print DATABASE_URL while you debug a failed migration. Read the error text instead, which does not contain the connection value.
How do I run migrations safely?
Run schema changes as a release command, not inside the app’s start command. A release command uses the exact image and bound environment of the current deployment, with a timeout and its own logs. Try the migration in development or staging first, and write it so the previous release can still run against it, such as adding a column before you start reading it.
Turn on continuous backups before you migrate production. If a migration goes wrong, roll back the app, then restore the database to the minute before it ran, in place or into a new database to check first.
Managed PostgreSQL on Resilis
Create a private PostgreSQL database in your project, bind it to your app and turn on continuous backups. The database draws from your runtime credit.