Question

Laravel Database Migration and Rollback with App Platform

Laravel, as many other PHP frameworks, has command line tools to migrate database schema up and down.

It is not clear how to use those schema migration command line tools with the DigitalOcean App Platform. For instance:

  • If horizontal scaling is used, it is not clear which container should run the migration command, and how to make it so that the migration is executed once only

  • If the service is rolled back, how to make it also automatically roll back the database schema? Or at least to restore the database to the previous state at the point in time before the update happened?

Any additional comments / examples on database schema migration and rollback with the App Platform?

Thanks!


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

We recently introduced the jobs component type that can be used for this, where you can specify the type such that the job will run pre-deploy or post-deploy.

This feature is currently only available via API/CLI (doctl), but not yet the UI.

We have an example of this here: https://github.com/digitalocean/sample-golang-notes/blob/migrate-job/.do/app.yaml

name: sample-golang-notes
services:
- name: web
  github:
    repo: digitalocean/sample-golang-notes
    branch: migrate-job
  envs:
  - key: DATABASE_URL
    value: ${db.DATABASE_URL}

jobs:
- name: migrate
  kind: PRE_DEPLOY
  github:
    repo: digitalocean/sample-golang-notes
    branch: migrate-job
  build_command: mkdir -p bin && cd cmd/migrate && go build -o ../../bin/migrate
  run_command: bin/migrate
  envs:
  - key: DATABASE_URL
    value: ${db.DATABASE_URL}

databases:
- name: db
  engine: PG
  version: "12"

You can submit this using doctl (make sure you’re using the latest version):

doctl apps create --spec .do/app.yaml

We have another job type that will be landing in production soon as well — executed on failed deploys — so stay tuned for that.

@ReWild 👋

This is a great question. We’re working to make operations like database migrations much easier to do on the App Platform, and plan to rollout support for pre-deploy and post-deploy jobs very soon for exactly this use-case.

In the meantime, you have a couple options:

  • Include the migration command in your custom run command. This can be unsafe in the case where you’re scaled up to multiple instances, and if your migration process isn’t locking on the DB to ensure only 1 is running.
  • Manually execute the migration using Console access in the UI. This has the downside of not running automatically with every deploy.

We’ll follow-up on this question when we ship pre/post deploy jobs, very soon, so stay tuned.