This page is for applications that manage their own database schema: Prisma, Drizzle, TypeORM, Knex, Sequelize, Alembic, Django, Laravel, Rails, Flyway. A misplaced migration is the first cause of a blocked update.
Why it is tricky on Webgune
Three platform behaviors combine with migrations:
| Behavior | Consequence for the schema |
|---|---|
| Zero-downtime update of a site or API: the old and the new version run side by side for a few seconds (Deployments and rollback) | the old version reads a database already migrated by the new one |
| Automatic return to the previous version when the new one is not ready after 15 minutes, or crashes in a loop | the old version restarts on a migrated database |
| Several instances in "Ultra high availability" mode | each instance runs the migration at startup |
On top of that, the build has no access to any database: a migration placed in the build script fails with a network error (How builds work).
The scenario to avoid
- The new version starts and migrates the database, from schema v10 to schema v11.
- The update then fails for another reason: missing variable, memory exceeded, wrong port, 15-minute timeout exceeded.
- Webgune puts the previous version back into service.
- The old version finds a v11 schema it does not know and refuses to start.
- 01New versionmigrates the schema from v10 to v11
- 02Failurevariable, memory, port or timeout
- 03Rollbackthe old version comes back
- Unknown v11 schema : the old version refuses to start
The same sequence can happen after a successful release: if the new version then crashes in a loop, automatic repair ends up returning to the last successful version, on the already migrated database.
Four practices
| Practice | Why |
|---|---|
| Migrations compatible with the previous version | the old version keeps running during the switchover and after a rollback |
| Migration launched separately, before deployment | application startup no longer touches the schema |
| Backup before a major migration | you can go back if something goes wrong |
| A single process migrates, with a lock | two instances migrating at the same time get in each other's way |
Writing a compatible migration
The principle: add first, remove in a later version. Each deployed version must work with the schema of the previous version and with that of the next.
| Change wanted | Avoid | Do |
|---|---|---|
| Rename a column | RENAME COLUMN in a single version | v1: add the new column and write to both. v2: read the new one. v3: drop the old one |
| Drop a column | dropping it in the version that stops using it | v1: stop using it. v2: drop it |
| Add a required column | NOT NULL with no default value | add the column with a default value, or nullable, then the constraint in a later version |
| Change a type | change the type in place | new column, copy the data, switch over, drop |
| Drop a table | DROP TABLE in the version that stops using it | v1: stop using it. v2: drop it |
| Add an index on a large table | creation that blocks during the switchover | PostgreSQL: CREATE INDEX CONCURRENTLY, outside a transaction |
With Prisma, prisma migrate dev generates migrations that rename by dropping then recreating the column: review the generated SQL before committing it.
Running the migration separately
Take the migration out of the start command, and run it before deploying the version that needs the new schema. Two possible places:
- the service's web terminal, which runs in a live instance, with its variables and access to the project's database;
- a step in your pipeline, with the database's public address (Databases).
npx prisma migrate deploy # Prisma
npx drizzle-kit migrate # Drizzle
alembic upgrade head # Alembic
python manage.py migrate # Django
php artisan migrate --force # Laravel
bin/rails db:migrate # Rails
The terminal opens on the version currently online. To run a migration that only the new version contains, keep the migration at startup (see below), or run it from your pipeline.
If you keep the migration at startup
This is the simplest solution, acceptable if your migrations are compatible with the previous version. In the start command or the start script:
sh -c "npx prisma migrate deploy && node server.js"
Then check two points:
- The lock. Prisma, Alembic on PostgreSQL, Django, Laravel, Rails and Flyway take one, or apply each migration in a transaction. A homemade SQL script run at launch does not: two instances can apply it at the same time.
- A clean failure. If the migration fails, the process must stop with an error code, before opening its port. The old version then stays in service (Health check).
A long migration delays the opening of the port. Beyond 15 minutes, the update is rejected.
If the old version refuses the database
Typical message in the Logs tab:
Database schema (v11) is newer than this version
The fix is to go forward: redeploy the version that ran the migration. It is compatible with the database as it is now. Restoring the old schema would lose the data written since.
- Open the failed deployment in the Deployments tab and read the "Post-deployment check" note at the end of the log.
- Fix that cause: missing variable, memory, port, start command.
- Click Redeploy.
Do not use Rollback to a version older than the migration: it puts the image back online without touching the database, and the old version will refuse the schema in the same way.
Before a major migration
- Open the database in the project, Backups tab, and click Dump: a logical export of the database, restorable elsewhere (Backups and restore).
- Test the migration on a Staging service connected to a copy of the database.
- Deploy to production at a quiet time.
- Keep the dump until the next version is online.
Several services on the same database
When an API and a worker share a database, only one of the two should carry the migrations. Deploy it first, then the other. Both must stay compatible with the schema in the interval.
Not to be confused with
Migrating a project database to a database managed by Webgune (full copy of the data, the old database remaining intact) is a different operation, described on Databases.
Updated on