Skip to content

Instantly share code, notes, and snippets.

@bakineggs
Created October 3, 2012 07:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakineggs/3825498 to your computer and use it in GitHub Desktop.
Save bakineggs/3825498 to your computer and use it in GitHub Desktop.
Test to ensure people update db/schema.rb when they add a migration
# spec/lint/database_schema_spec.rb
describe 'db/schema.rb' do
it 'includes the changes from all migrations' do
if checksum_line = File.read('db/schema.rb').split("\n").detect {|line| line.match /^# Checksum: \w+$/}
checksum = checksum_line.match(/^# Checksum: (\w+)$/)[1]
checksum.should == Digest::MD5.hexdigest(Dir.glob('db/migrate/*').sort.map {|path| File.read path}.join "\n")
else
fail 'db/schema.rb does not contain a migration directory checksum! Please run rake db:migrate'
end
end
end
# lib/tasks/migrate.rake
namespace :db do
task :migrate do
checksum = Digest::MD5.hexdigest Dir.glob('db/migrate/*').sort.map {|path| File.read path}.join "\n"
`echo '# Checksum: #{checksum}' >> db/schema.rb`
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment