Run Database Migrations
Run a migration before each deployment of an application. Add a BEFORE job to the application and describe it in a JobConfig. The platform creates a Cloud Run Job. The main service deploys only after the job succeeds.
The platform no longer supports the old runMigrationJob flag. Use jobs and JobConfig.
Prerequisites
- An
Applicationmanifest and aDatabasemanifest. - A container image that can run your migrations, for example with Flyway, Liquibase or a script.
Steps
-
Add the job and the database access to the
Application.spec:
buildDefinition: payment-api
target: CLOUD_RUN
project: payment-gateway
jobs:
- name: migrate
type: BEFORE # also: AFTER, SCHEDULED, ON_DEMAND
accessControl:
database:
name: payments-db
schema: payments # a logical database from the Database spec.databases
privileges: [USAGE, CREATE]
secretSource:
envVar: DATABASE_URL -
Create a
JobConfigfor each environment. Its name is<application-name>-<job-name>. It uses the image of the application.apiVersion: lowops.manifests.v1
kind: JobConfig
metadata:
name: payment-api-migrate
project: payment-gateway
environment: production
organizational-unit: retail-banking
organization: fincorp
spec:
version: "1.4.0" # the image tag to run
container:
command: ["/migrate.sh"] # the entrypoint that applies your migrations
env:
- name: MIGRATION_MODE
value: "up" -
Declare each logical database in
spec.databasesof theDatabasemanifest. Add the PostgreSQL extensions there. TheaccessControlblock grants privileges only. It does not create tables. -
Commit the manifests in one pull request and merge it.
Verify
Each deployment runs the BEFORE job first. The main Cloud Run service waits for the job to succeed.