Skip to content

Instantly share code, notes, and snippets.

  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save defenestrator/62f2767e51a2411403728c74a8d1bf21 to your computer and use it in GitHub Desktop.
Laravel Envoyer hook to run migrations in a sane manner
# Application base directory
site= "example.com"
# Navigate to the release being deployed.
cd {{ release }}
# Number of migrations in the new release
releaseMigrations= ls database/migrations | wc -l
# Number of migrations in the current production app
currentMigrations= ls ${site}/current/database/migrations | wc -l
# If there are more migrations in the new release than on production
if [ ${releaseMigrations} -gt ${currentMigrations} ]; then
# Exit on error
set -e
# Function to be run in trap before exit
# So that production comes back up on migration errors
function beforeExit {
(cd ${site}/current && php artisan up)
}
# Lock down the production app instance before making DB changes
(cd ${site}/current && php artisan down)
# Run the migrations
php artisan migrate --force
# Bring production back online
(cd ${site}/current && php artisan up)
# Keep track of the last command executed
trap 'lastCommand=$thisCommand; thisCommand=$BASH_COMMAND' DEBUG
# If any command exits with a non-zero status
if [ $? -ne 0 ]; then
# echo error then run beforeExit to bring production back up before exit
trap 'echo "\"${lastCommand}\" command failed with exit code $?." && beforeExit' EXIT
else
echo 'New migrations completed successfully'
fi
else
echo "No migrations detected"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment