Last active
April 21, 2024 20:08
-
-
Save defenestrator/62f2767e51a2411403728c74a8d1bf21 to your computer and use it in GitHub Desktop.
Laravel Envoyer hook to run migrations in a sane manner
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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
You may use the existing
project
hook variable to replacesite
.Documentation on Hook Variables