-
Notifications
You must be signed in to change notification settings - Fork 1
/
git_rails
87 lines (71 loc) · 3.1 KB
/
git_rails
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#!/bin/bash
old_ref=$1
new_ref=$2
colors[0]='\033[1;32m'
colors[1]='\033[0;35m'
colors[2]='\033[0;33m'
colors[3]='\033[1;33m'
colors[4]='\033[0;34m'
no_color='\033[0m'
rand_color=$[ $RANDOM % 5 ]
if [ -z "$old_ref" ]; then
old_ref=`git rev-parse ORIG_HEAD`
fi
if [ -z "$new_ref" ]; then
new_ref=`git rev-parse HEAD`
fi
files_changed=`git diff $old_ref $new_ref --name-status`
# CHECK IF WE NEED TO DO A BUNDLE
bundle_changed=`echo "$files_changed" | grep $'M\tGemfile.lock'`
if [ ! -z "$bundle_changed" ]; then
arr[0]="There are changes to Gemfile.lock, but don't worry - I've got you covered. Bundling now ..."
arr[1]="'Bundling' isn't just an old-timey tradition, it's also what I'm doing right now!"
arr[2]="Gemfile.lock has changed. COMMENCE RUNNING BUNDLE in 5 ... 4 ... 3 ... 2 ... 1!"
rand=$[ $RANDOM % 3 ]
echo "-----------------------------------------"
echo -e ${colors[$rand_color]}${arr[$rand]}${no_color}
echo "-----------------------------------------"
bundle
fi
migrations=`git diff --name-status $old_ref $new_ref -- db/migrate | grep '^[AD]'`
if [ ! -z "$migrations" ]; then
arr[0]="New database structure like what! Time to run migrations ..."
arr[1]="Call me a crane for the way I'm handling these migrations!"
arr[2]="Making sure the ol' db is up-to-date: it's migration time"
rand=$[ $RANDOM % 3 ]
echo "-----------------------------------------"
echo -e ${colors[$rand_color]}${arr[$rand]}${no_color}
echo "-----------------------------------------"
for migration in $migrations
do
# CHECK THE MIGRATION TYPE AND CONTINUE TO THE FILENAME
if [ $migration == "D" ]; then
migration_type="down"
continue
elif [ $migration == "A" ]; then
migration_type="up"
continue
fi
# BUILD THE MIGRATION COMMAND FROM THE VERSION AND TYPE
version=`echo "$migration" | cut -d'_' -f1 | cut -d'/' -f3`
migrate_command="ActiveRecord::Migration.new.migration_context.migrations.detect {|d| d.version == $version}.migrate(:$migration_type) rescue ActiveRecord::Migrator.migrations('db/migrate').detect {|d| d.version == $version}.migrate(:$migration_type) rescue nil"
# APPEND OR PREPREND TO THE COMMAND LIST DEPENDING ON MIGRATION TYPE
if [[ $migration_type == "down" ]]; then
# CHECKOUT DOWN MIGRATION AND SAVE PATH FOR CLEANUP
git checkout "$old_ref" -- "$migration"
migration_cleanup="$migration_cleanup $migration"
migrate_command="$migrate_command;ActiveRecord::Base.connection.exec_query(\"DELETE FROM schema_migrations WHERE version = '$version'\") rescue nil"
migrate_commands="$migrate_command;$migrate_commands"
else
migrate_command="$migrate_command;ActiveRecord::Base.connection.exec_query(\"INSERT INTO schema_migrations (version) VALUES ('$version') ON CONFLICT (version) DO NOTHING\") rescue nil"
migrate_commands="$migrate_commands;$migrate_command"
fi
done
# RUN THE MIGRATIONS (AND TEST PREPARE)
bundle exec rails r "$migrate_commands"
# CLEAN UP DOWN MIGRATIONS
if [ ! -z "$migration_cleanup" ]; then
git reset $new_ref -- $migration_cleanup
rm $migration_cleanup
fi
fi