Skip to main content

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 Application manifest and a Database manifest.
  • A container image that can run your migrations, for example with Flyway, Liquibase or a script.

Steps​

  1. 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
  2. Create a JobConfig for 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"
  3. Declare each logical database in spec.databases of the Database manifest. Add the PostgreSQL extensions there. The accessControl block grants privileges only. It does not create tables.

  4. 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.

Next​