Advertisement
Guest User

Untitled

a guest
Jan 21st, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.27 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Update these to reflect your local database
  4. MYSQL_USER=root
  5. MYSQL_PASSWORD=password
  6. MYSQL_DB=migration_demo
  7.  
  8. set -e
  9.  
  10. function apply_migration () {
  11. MIGRATIONDIR=$1
  12. MIGRATION=$2
  13.  
  14. MIGRATIONFILE=${MIGRATIONDIR}/${MIGRATION}
  15.  
  16. mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DB} < ${MIGRATIONFILE}
  17.  
  18. # For this to work, migrations *must* return a non-zero exit code on failure
  19. if [[ $? -ne 0 ]]; then
  20. echo "ERROR applying '${MIGRATIONFILE}'!!"
  21. bash -c 'exit 1'
  22. else
  23. mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DB} \
  24. -e "INSERT INTO applied_migrations(name) VALUES ('${MIGRATION}')"
  25. fi
  26. }
  27.  
  28. function migrate () {
  29. MIGRATIONDIR=$1
  30. APPLIEDFILE=.applied_migrations
  31. ALLFILE=.all_migrations
  32.  
  33. # Retrieve list of applied migrations from the DB
  34. mysql -u${MYSQL_USER} -p${MYSQL_PASSWORD} ${MYSQL_DB} \
  35. -e 'SELECT name FROM applied_migrations ORDER BY name ASC' \
  36. | tail -n +2 > ${APPLIEDFILE}
  37.  
  38. # Retrieve list of *all* migrations from the current version's migrations dir
  39. find ${MIGRATIONDIR}/* -print0 | xargs -0 basename -a > ${ALLFILE}
  40.  
  41. # Compute pending migrations from the difference
  42. PENDING=$(comm -1 -3 ${APPLIEDFILE} ${ALLFILE})
  43.  
  44. # Apply each migration
  45. for m in ${PENDING}; do
  46. apply_migration ${MIGRATIONDIR} ${m}
  47. done
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement