Last Updated: February 25, 2016
·
1.892K
· bashir

Script to ensure added migration files are not missed from commit

pre-commit hook to prevent you from forgetting to check in migration files.

https://gist.github.com/1223064

the code (put it in .git/hooks/pre-commit):

#!/usr/bin/env ruby
# vim: set syntax=ruby

# Ensures that changes to the Rails schema.rb file may only be committed if a
# migration file is also committed at the same time.

def schema_modified?
  %x[ git diff --cached |grep schema.rb ] != ''
end

def no_migrations_staged?
  %x[ git diff --cached |grep 'db\/migrate\/20[0-9]*' ] == ''
end

# regex for typical migration
# db/migrate/20110901091759_remove_invalid_venue_phone_numbers.rb
# db\/migrate\/20[0-9]{12}_.*\.rb$
# or
# db\/migrate\/20[0-9]*
# Warning may only work for like 100 years or something.

if schema_modified? && no_migrations_staged?
  puts "Cannot commit modified schema.rb without any migrations."
  exit 1
end

exit 0