How do you run database migrations in a rolling deploy?
AdvancedProductionRolling Updates, Delegation and Safe Deployments
Answer
Run the migration once, before the app hosts update, and make it backward compatible so old and new app versions can run side by side during the rollout.
- hosts: app[0]
tasks:
- ansible.builtin.command: /opt/app/bin/migrate --to {{ app_version }}
run_once: true
register: mig
changed_when: "'Applied' in mig.stdout"
- hosts: app
serial: "25%"
roles: [app]
Take a backup first, use a migration tool's own locking (Flyway, Alembic) so concurrent runs cannot corrupt state, and split risky changes into expand, deploy, contract steps across releases. Never rely on run_once alone to prevent two parallel pipeline runs; use pipeline concurrency controls too.
More on Rolling Updates, Delegation and Safe Deployments
How do you do a rolling update with Ansible?BeginnerWhat forms can `serial` take?IntermediateShow the standard pattern for a zero-downtime rolling deploy behind a load balancer.IntermediateWhy do you add a health check inside the deployment play?BeginnerHow do `any_errors_fatal` and `max_fail_percentage` help a rollout?BeginnerHow do you do a canary deployment?Intermediate