-
-
Save ghiculescu/990a79f00f6979dc0a4d to your computer and use it in GitHub Desktop.
Rails Git pre-commit hook for ensuring schema.rb and migration changes commit atomically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# based on https://gist.github.com/bestie/1223064 | |
# Ensures that if a new migration is added, rake db:migrate is also run (and thus the schema.rb file is updated) | |
def schema_modified? | |
%x[ git status |grep schema.rb ] != '' | |
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. | |
def new_migrations_staged? | |
%x[ git ls-files -o --exclude-standard |grep 'db\/migrate\/20[0-9]*' ] != '' | |
end | |
if new_migrations_staged? && !schema_modified? | |
puts | |
puts "WARNING: Commit blocked" | |
puts | |
puts "Schema.rb not modified. Cannot submit new migration without also running it." | |
puts | |
puts "Run `rake db:migrate` to run your migration, then stage your schema.rb file, then try committing." | |
puts | |
exit 1 | |
end | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment