v1

Release /

Handle runtime migrations

When you make changes to your database schema you may need to update your application and change your production database simultaneously.

You can handle database schema migrations on Northflank in various ways:

  • Using a job triggered by CI
  • Restarting your deployment with command overrides
  • Executing commands in a container's shell
  • By configuring a release flow (coming soon), where you can run a migration during a deployment and automatically promote the new build when the migration is complete

Migrate using a job triggered by CI

You can run a migration automatically by configuring a job to run whenever a new build is completed.

You can:

  • build directly from the same repository and branch as the deployment that requires migration, with CI and CD enabled on the job
  • deploy an image from a build service that also builds the image for your deployment service, with CD enabled

Create a new manual job with the source for your migration code, either from a repository or a build service. Enable run on image change on your job, select CD & pipeline promotion or always, depending on your desired workflow.

Enter any necessary configuration details to run your migration, for example a command override if your migration code needs to be called with a command other than the default. For example Python Django might require a command like python manage.py migrate your_app, or for Rails bin/rails db:migrate VERSION=20221115174300.

Your job will now execute when your latest commit to the repository is built, so your migration should occur at the same time as your new deployment. You can check the logs for the job run to ensure it has executed successfully.

Migrate using command override

You can run a migration using command override to execute the migration code when deploying your latest commit. Your deployment will begin redeploying as soon as you save the custom command.

Enter command manually

## Run migration, then start the server
bin/sh -c "node migrate/migration-code.js; node built/server.js"

## Run migration, then start the server if it is successful (migration process returns exit code 0)
bin/sh -c "node migrate/migration-code.js && node built/server.js"

Use a script

If you have added a script containing your migration command, for example in package.json for node-based applications or Pipfile for Python applications using Pipenv, you can include this in the command override.

yarn migrate-and-run

Migrate by executing a shell command

You can manually execute commands in a running workload by accessing the shell from the Northflank application, or by using the Northflank CLI or JS client.

Navigate to your deployment and ensure the commit with your migration is deployed. Open a shell for a running container and enter the command to run your migration.

© 2022 Northflank Ltd. All rights reserved.