'Run Laravel migrations on just one server (like ->onOneServer() for scheduled cronjobs)

I'm running a Laravel app on multiple servers at the same time. It's running in a Docker image. Each time I deploy some code changes, Docker image is restarted and then it runs after_deployment.sh script which contains php artisan migrate --force. This script is executed by ALL of my servers, not just one.

All servers use the same central DB. So if two servers try to run the php artisan migrate at roughly the same time, only the first server succeeds, the other fails somewhere in the middle and logs an error. This is not great.

There was a similar problem with running cronjobs (Laravel Scheduler) on just one server, but that is now solved as Laravel provides the ->onOneServer(); method for Scheduled jobs.

Is there something similar for running migrations on just one server? Something like php artisan migrate --force --on-one-server?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source