Skip to content

Commit 8163ab9

Browse files
committed
Use custom down migration method for up migrations also
1 parent 3201473 commit 8163ab9

File tree

1 file changed

+31
-17
lines changed

1 file changed

+31
-17
lines changed

bin/git_rails

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ if [ -z "$new_ref" ]; then
1111
fi
1212

1313
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]+)'`
1614

1715
# CHECK IF WE NEED TO DO A BUNDLE
1816
bundle_changed=`echo "$files_changed" | egrep 'M\tGemfile.lock'`
@@ -21,26 +19,42 @@ if [ ! -z "$bundle_changed" ]; then
2119
bundle
2220
fi
2321

24-
if [ ! -z "$migrations_removed" ]; then
25-
echo "Rolling back missing migrations"
26-
for migration in $migrations_removed
22+
migrations=`git diff --name-status $old_ref $new_ref -- db/migrate`
23+
if [ ! -z "$migrations" ]; then
24+
echo "Running migrations!"
25+
for migration in $migrations
2726
do
27+
# CHECK THE MIGRATION TYPE AND CONTINUE TO THE FILENAME
2828
if [ $migration == "D" ]; then
29+
migration_type="down"
30+
continue
31+
elif [ $migration == "A" ]; then
32+
migration_type="up"
2933
continue
3034
fi
31-
git checkout "$old_ref" -- "$migration"
35+
36+
# BUILD THE MIGRATION COMMAND FROM THE VERSION AND TYPE
3237
version=`echo "$migration" | cut -d'_' -f1 | cut -d'/' -f3`
33-
migrate_commands="$migrate_commandsActiveRecord::Migrator.run(:down, 'db/migrate', $version) rescue nil"
38+
migrate_command="ActiveRecord::Migrator.run(:$migration_type, 'db/migrate', $version) rescue nil"
39+
40+
# APPEND OR PREPREND TO THE COMMAND LIST DEPENDING ON MIGRATION TYPE
41+
if [[ $migration_type == "down" ]]; then
42+
# CHECKOUT DOWN MIGRATION AND SAVE PATH FOR CLEANUP
43+
git checkout "$old_ref" -- "$migration"
44+
migration_cleanup="$migration_cleanup $migration"
45+
migrate_commands="$migrate_command$migrate_commands"
46+
else
47+
migrate_commands="$migrate_commands$migrate_command"
48+
fi
3449
done
35-
echo "$migrate_commands" | bundle exec rails c
36-
bundle exec rake db:test:prepare
37-
git checkout $new_ref -- db/schema.rb
38-
git reset HEAD -- db/migrate
39-
git clean -df db/migrate
40-
fi
4150

42-
if [ ! -z "$migrations_added" ]; then
43-
echo "New migrations have been added running migrations"
44-
bundle exec rake db:migrate db:test:prepare
45-
git checkout $new_ref -- db/schema.rb
51+
# RUN THE MIGRATIONS (AND TEST PREPARE)
52+
echo "$migrate_commands" | bundle exec rails c > /dev/null
53+
bundle exec rake db:test:prepare > /dev/null
54+
55+
# CLEAN UP DOWN MIGRATIONS
56+
if [ ! -z "$migration_cleanup" ]; then
57+
git reset $new_ref -- $migration_cleanup
58+
rm $migration_cleanup
59+
fi
4660
fi

0 commit comments

Comments
 (0)