Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lleger/1610699 to your computer and use it in GitHub Desktop.
Save lleger/1610699 to your computer and use it in GitHub Desktop.
Rails Migrations Post Merge Git Commit Hook
Get notifications when there are migrations that need to be run in a Rails project:
https://img.skitch.com/20110418-c9y3ttap3tai7frc43f38qtb8e.jpg
In your Rails project edit the .git/config file to add the following:
[rails]
automigrate = false
automigrateforegroundcolor = yellow
automigratebackgroundcolor = black
automigrateblink = false
Supported colors are black, red, green, yellow, blue, magenta, cyan, white
In your Rails project, create a file in the .git/hooks directory named post-merge
touch ~/path_to_your_project/.git/post-merge
Edit that file and paste the following:
#!/usr/bin/env ruby
def set_color(color, default = 0)
case color.downcase.gsub("\n", '')
when /\d/ : color
when 'black' then '0'
when 'red' then '1'
when 'green' then '2'
when 'yellow' then '3'
when 'blue' then '4'
when 'magenta' then '5'
when 'cyan' then '6'
when 'white' then '7'
else
default
end
end
automigrate = `git config rails.automigrate`.index('true')
migratable = `git diff HEAD^`.index("db/migrate")
fg_color = set_color(`git config rails.automigrateforegroundcolor`, 7)
bg_color = set_color(`git config rails.automigratebackgroundcolor`, 0)
blink = (`git config rails.automigrateblink`.index('true')) ? 5 : 0
if migratable
if automigrate
puts "\e[#{blink};1;3#{fg_color};4#{bg_color}mMigrating Database... Please wait.\e[0m"
`rake db:migrate && rake db:test:prepare`
puts "\e[#{blink};1;3#{fg_color};4#{bg_color}mDone migrating database.\e[0m"
else
puts "\e[#{blink};1;3#{fg_color};4#{bg_color}mThere are pending migrations.\e[0m"
end
end
Save the file and make it executable:
chmod 755 ~/path_to_your_rails_project/.git/post-merge
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment