Skip to content

Instantly share code, notes, and snippets.

@leandro
Created December 21, 2020 22:50
Show Gist options
  • Save leandro/327e9a899c25a47ab023c22c695c4ec8 to your computer and use it in GitHub Desktop.
Save leandro/327e9a899c25a47ab023c22c695c4ec8 to your computer and use it in GitHub Desktop.
Docker entrypoint para migrations no rails (e mais)
#!/bin/sh
set -e
[ -z "$(env | grep RUN_RAILS_CMDS)" ] && RUN_RAILS_CMDS=1
rails_cmd () {
if [ -z "$RUN_RAILS_CMDS" ]; then
echo "Rails commands are deactivated because RUN_RAILS_CMDS is empty." >&2
return 0
else
eval "$@"
CMD_CODE=$?
[ "$CMD_CODE" == "0" ] && echo "$CMD_OUTPUT"
return $CMD_CODE
fi
}
HASH_FILE="tmp/assets-digest.txt"
LATEST_HASH=""
cd $APP_DIR
echo '-- Checking if there is any migration pending...'
PENDING_MIGRATIONS_OUTPUT=$(rails_cmd "bundle exec rake db:migrate:status | grep -E '\\d{14}' | awk '{print \$1}' | grep down | wc -l")
if [ "$PENDING_MIGRATIONS_OUTPUT" == "0" ]; then
echo -e '- Great! There is no pending migration to be executed.\n'
else
echo '- Yup! There is. Running pending migrations...'
echo '---------------------------------------------------'
rails_cmd rails db:migrate
echo '---------------------------------------------------'
echo -e '- Migrations finished.\n'
fi
echo '-- Checking if assets have changed since last compilation...'
CURRENT_HASH=$(
find app/webpack -type f -exec sha256sum {} \; \
| awk '{print $1}' \
| sha256sum \
| awk '{print $1}'
)
if [ -f "$HASH_FILE" ]; then
LATEST_HASH=$(cat $HASH_FILE)
if [ "$CURRENT_HASH" != "$LATEST_HASH" ]; then
echo '- Ooops! They have. Recompiling...'
echo '---------------------------------------------------'
rails_cmd rails webpacker:compile
echo "$CURRENT_HASH" > $HASH_FILE
echo '---------------------------------------------------'
echo -e '- Compiled! Assets digest registry file updated.\n'
else
echo '- Cool! They have not. Skipping compilation.'
fi
else
echo "- The assets weren't compiled ever before. Compiling..."
echo '---------------------------------------------------'
rails_cmd rails webpacker:compile
echo "$CURRENT_HASH" > $HASH_FILE
echo '---------------------------------------------------'
echo -e 'Compiled! Assets digest registry file created.\n'
fi
exec "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment