Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- # Update these to reflect your local database
- MYSQL_USER=root
- MYSQL_PASSWORD=password
- MYSQL_DB=migration_demo
- set -e
- function apply_migration () {
- MIGRATIONDIR=$1
- MIGRATION=$2
- MIGRATIONFILE=${MIGRATIONDIR}/${MIGRATION}
- mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DB} < ${MIGRATIONFILE}
- # For this to work, migrations *must* return a non-zero exit code on failure
- if [[ $? -ne 0 ]]; then
- echo "ERROR applying '${MIGRATIONFILE}'!!"
- bash -c 'exit 1'
- else
- mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DB} \
- -e "INSERT INTO applied_migrations(name) VALUES ('${MIGRATION}')"
- fi
- }
- function migrate () {
- MIGRATIONDIR=$1
- APPLIEDFILE=.applied_migrations
- ALLFILE=.all_migrations
- # Retrieve list of applied migrations from the DB
- mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DB} \
- -e 'SELECT name FROM applied_migrations ORDER BY name ASC' \
- | tail -n +2 > ${APPLIEDFILE}
- # Retrieve list of *all* migrations from the current version's migrations dir
- find ${MIGRATIONDIR}/* -print0 | xargs -0 basename -a > ${ALLFILE}
- # Compute pending migrations from the difference
- PENDING=$(comm -1 -3 ${APPLIEDFILE} ${ALLFILE})
- # Apply each migration
- for m in ${PENDING}; do
- apply_migration ${MIGRATIONDIR} ${m}
- done
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement