Running Migrations Once During a Multi-Host Kamal Deploy | t27duck

12-Jun-2026 5
Using a pre-deploy hook can prevent ActiveRecord::ConcurrentMigrationError when multiple web containers boot at the same time. At my day job, we deploy our Rails app in production to three separate web hosts using Kamal. Each container runs bin/docker-entrypoint on startup, which runs bin/rails db:prepare. That worked fine when there was only one host (like staging), but ran into issues with many hosts. All containers start at roughly the same moment, all three try to apply pending migrations, and all three race for the same migration advisory lock. One of them wins. The other two see the lock is held and bail out with ActiveRecord::ConcurrentMigrationError. If migrations finish in milliseconds, you might get lucky. Ours sometimes take a few seconds, which is plenty of time for the "losers" to give up and crash the boot. The fix is small and lives in two places: a Kamal pre-deploy hook that runs db:prepare exactly once, and a slimmed-down bin/docker-entrypoint that no longer tries to migrate at all. .
Use coupon code:

RUBYONRAILS

to get 30% discount on our bundle!
Prepare for your next tech interview with our comprehensive collection of programming interview guides. Covering JavaScript, Ruby on Rails, React, and Python, these highly-rated books offer thousands of essential questions and answers to boost your interview success. Buy our 'Ultimate Job Interview Preparation eBook Bundle' featuring 2200+ questions across multiple languages. Ultimate Job Interview Preparation eBook Bundle