Advertisement
Guest User

Untitled

a guest
Oct 24th, 2016
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.30 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. RED='\033[0;31m'
  4. GREEN='\033[0;32m'
  5. YELLOW='\033[0;33m'
  6. NO_COLOR='\033[0m'
  7.  
  8. CHECKING_OUT_BRANCH=$3
  9. OLD_BRANCH=$1
  10. NEW_BRANCH=$2
  11.  
  12. function ask_yes_or_no(){
  13. exec < /dev/tty
  14. while true; do
  15. read
  16. if [ "$REPLY" = "" ]; then
  17. REPLY='y'
  18. fi
  19. case $(echo $REPLY | tr '[A-Z]' '[a-z]') in
  20. y|yes) echo "yes"; break;;
  21. *) echo "no" ; break;;
  22. esac
  23. done
  24. }
  25.  
  26. if [ $CHECKING_OUT_BRANCH -eq 1 ]
  27. then
  28. FILES_CHANGED=`git diff $OLD_BRANCH $NEW_BRANCH --name-status`
  29.  
  30. BUNDLE_CHANGED=`echo "$FILES_CHANGED" | egrep 'M\tGemfile.lock'`
  31. if [ ! -z "$BUNDLE_CHANGED" ]; then
  32. echo "\n ===> ⚠️ ${YELLOW}Your ${RED}Gemfile.lock ${YELLOW}has changed. Run 'bundle install'? [Y/n]${NO_COLOR}"
  33. if [[ 'yes' == $(ask_yes_or_no) ]]; then
  34. bundle
  35. else
  36. echo " ===> ⚠️ ${YELLOW} 'bundle install' skipped!${NO_COLOR}\n"
  37. fi
  38. fi
  39.  
  40. MIGRATIONS_REMOVED=`echo "$FILES_CHANGED" | egrep 'D\tdb/migrate/([0-9]+)' | sort -r`
  41. MIGRATIONS_ADDED=`echo "$FILES_CHANGED" | egrep 'A\tdb/migrate/([0-9]+)'`
  42.  
  43. if [ ! -z "$MIGRATIONS_REMOVED" ]; then
  44. echo "\n ===> 🌀 ${YELLOW}Some migrations need to be rolled back. Run migrations? [Y/n]${NO_COLOR}"
  45.  
  46. if [[ 'yes' == $(ask_yes_or_no) ]]; then
  47. echo " ===> 🌀 ${GREEN}Running migrations...${NO_COLOR}\n"
  48. for migration in $MIGRATIONS_REMOVED
  49. do
  50. if [ $migration == "D" ]; then
  51. continue
  52. fi
  53. git checkout "$OLD_BRANCH" -- "$migration"
  54. VERSION=`echo "$migration" | cut -d'_' -f1 | cut -d'/' -f3`
  55. bundle exec rake db:migrate:down VERSION="$VERSION"
  56. git reset
  57. rm "$migration"
  58. done
  59. bundle exec rake db:test:prepare
  60. git checkout db/schema.rb
  61. else
  62. echo " ===> ⚠️ ${YELLOW} Migrations skipped!${NO_COLOR}"
  63. fi
  64. fi
  65.  
  66. if [ ! -z "$MIGRATIONS_ADDED" ]; then
  67. echo "\n ===> 🌀 ${YELLOW}New migrations have been added. Run migrations? [Y/n]${NO_COLOR}"
  68. if [[ 'yes' == $(ask_yes_or_no) ]]; then
  69. echo " ===> 🌀 ${GREEN}Running migrations...${NO_COLOR}\n"
  70. bundle exec rake db:migrate db:test:prepare
  71. else
  72. echo " ===> ⚠️ ${YELLOW} Migrations skipped!${NO_COLOR}"
  73. fi
  74. fi
  75.  
  76. echo "\n ===> ✅ ${GREEN} You're good to go!!!${NO_COLOR} 🍺\n"
  77. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement