Skip to content

Instantly share code, notes, and snippets.

@hossainemruz
Created November 9, 2018 09:27
Show Gist options
  • Save hossainemruz/522fbcc69bee4f73fe63a3bcbff336c7 to your computer and use it in GitHub Desktop.
Save hossainemruz/522fbcc69bee4f73fe63a3bcbff336c7 to your computer and use it in GitHub Desktop.
#!/bin/bash
DB_NAME="database_name"
DB_USER="username"
DB_PASS="password"
DB_HOST="localhost"
DB_PORT="3306"
while ! nc -q 1 $DB_HOST $DB_PORT </dev/null; do
echo "Waiting for database to be ready....."
sleep 5
done
#get current version
GET_DATABASE_VERSION="SELECT name FROM version TOP 1"
#
echo "Getting data version"
version=$(mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME -e "SELECT name FROM version")
#trim string to get current version name
version=${version:5}
x="./config/scripts/migration/$version"
echo $version
MIGRATION_PATH="./config/scripts/migration/*"
#get migration file for newer version
for filename in $MIGRATION_PATH -maxdepth 2
do
if [[ -f $filename ]]; then
if [[ "$filename" > "$x" ]] || [ "$filename" == "$x" ]; then
echo "Running migration file: $filename"
mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME < $filename
fi
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment