Skip to content

Instantly share code, notes, and snippets.

@AD7six
Created August 11, 2017 09:36
Show Gist options
  • Save AD7six/577db7b1fbb892a9d53d01a5e651f766 to your computer and use it in GitHub Desktop.
Save AD7six/577db7b1fbb892a9d53d01a5e651f766 to your computer and use it in GitHub Desktop.
#!/bin/bash
# This file is managed by vagrant, do not modify manually
# To override, on the host copy default/files/usr/local/bin/migrateDb
# to local/files/usr/local/bin/migrateDb
# and reprovision
DATABASE_URL=${DATABASE_URL:-mysql://user:pass@host/db}
if [[ "$DATABASE_URL" =~ ^([^:]+)://([^:]+):([^@]*)@([^/]+)/(.*)$ ]]; then
DATABASE_SCHEME=${BASH_REMATCH[1]}
DATABASE_USER=${BASH_REMATCH[2]}
DATABASE_PASS=${BASH_REMATCH[3]}
DATABASE_HOST=${BASH_REMATCH[4]}
DATABASE_DB=${BASH_REMATCH[5]}
fi
## Errors that don't matter when re running a migration
ACCEPTABLE_MIGRATION_RESULT_RE="^$|already exists|Duplicate column|Duplicate key|Can't DROP"
echo "Ensuring migrations are current"
echo ""
if [[ ! -d /tmp/migrations ]]; then
git clone <migrations repo source> /tmp/migrations
else
(cd /tmp/migrations; git pull > /dev/null)
fi
echo "Running migrations"
echo ""
migrations=`find /tmp/migrations -type f | sort`
for migration in $migrations; do
echo " Running ${migration##*/}"
output=`mysql --user=$DATABASE_USER --password=$DATABASE_PASS --host=$DATABASE_HOST $DATABASE_DB < $migration 2>&1`
if ! [[ "$output" =~ $ACCEPTABLE_MIGRATION_RESULT_RE ]]; then
echo " $output"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment