Skip to content

Instantly share code, notes, and snippets.

@mothra
Forked from bestie/pre-commit
Last active December 15, 2015 05:19
Show Gist options
  • Save mothra/5208175 to your computer and use it in GitHub Desktop.
Save mothra/5208175 to your computer and use it in GitHub Desktop.
#!/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.
# put this in .git/hooks/pre-commit
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. You can override this warning by committing with the --no-verify flag."
exit 1
end
exit 0
@mdcclv
Copy link

mdcclv commented Mar 20, 2013

Are these the right conditions for this to be checking? I feel like our frequent problem would be caught better by
if migrations_staged? && schema_not_modified?

@mothra
Copy link
Author

mothra commented Apr 9, 2013

The most frequent problems that I've seen with schema.rb would be covered by these conditions. We have a separate rspec test that checks for a migration without a schema.rb change.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment