|
| 1 | +#!/bin/bash |
| 2 | +old_ref=$1 |
| 3 | +new_ref=$2 |
| 4 | + |
| 5 | +if [ -z "$old_ref" ]; then |
| 6 | + old_ref=`git rev-parse ORIG_HEAD` |
| 7 | +fi |
| 8 | + |
| 9 | +if [ -z "$new_ref" ]; then |
| 10 | + new_ref=`git rev-parse HEAD` |
| 11 | +fi |
| 12 | + |
| 13 | +files_changed=`git diff $old_ref $new_ref --name-status` |
| 14 | +migrations_removed=`echo "$files_changed" | egrep 'D\tdb/migrate/([0-9]+)' | sort -r` |
| 15 | +migrations_added=`echo "$files_changed" | egrep 'A\tdb/migrate/([0-9]+)'` |
| 16 | + |
| 17 | +# CHECK IF WE NEED TO DO A BUNDLE |
| 18 | +bundle_changed=`echo "$files_changed" | egrep 'M\tGemfile.lock'` |
| 19 | +if [ ! -z "$bundle_changed" ]; then |
| 20 | + echo "Your Gemfile.lock has changed, running bundle" |
| 21 | + bundle |
| 22 | +fi |
| 23 | + |
| 24 | +if [ ! -z "$migrations_removed" ]; then |
| 25 | + echo "Rolling back missing migrations" |
| 26 | + for migration in $migrations_removed |
| 27 | + do |
| 28 | + if [ $migration == "D" ]; then |
| 29 | + continue |
| 30 | + fi |
| 31 | + git checkout "$old_ref" -- "$migration" |
| 32 | + version=`echo "$migration" | cut -d'_' -f1 | cut -d'/' -f3` |
| 33 | + bundle exec rake db:migrate:down VERSION="$version" |
| 34 | + git reset |
| 35 | + rm "$migration" |
| 36 | + done |
| 37 | + bundle exec rake db:test:prepare |
| 38 | + git checkout $new_ref -- db/schema.rb |
| 39 | +fi |
| 40 | + |
| 41 | +if [ ! -z "$old_ref" ]; then |
| 42 | + echo "New migrations have been added running migrations" |
| 43 | + bundle exec rake db:migrate db:test:prepare |
| 44 | + git checkout $new_ref -- db/schema.rb |
| 45 | +fi |
| 46 | + |
| 47 | +# CHECK IF ENVIRONMENT HAS CHANGED |
| 48 | +env_erb_changed=`echo "$files_changed" | egrep 'M\t(.env.erb|bin/setup)'` |
| 49 | +if [ ! -z "$env_erb_changed" ]; then |
| 50 | + echo 'Environment stuff has changed, you should run ./bin/setup!!' |
| 51 | +fi |
0 commit comments