Our team is developing a Rails app on MySQL and using config.active_record.schema_format = :sql
per The Rails Guides.
Naturally, our AUTO_INCREMENT
values in development_structure.sql
get out-of-sync as we develop in parallel. We know that having different values in our databases for AUTO_INCREMENT
is not a technical problem. However, it creates a lot of diff
noise when we diff
before checking-in. On more than one occasion we have broken our build because one of us missed an important change in development_structure.sql
that was disguised by all the noise.
Any suggestions on how to eliminate this diff
noise so our eyes can focus on important changes?
Thanks.
A variant of the accepted answer is to include the following in a .rake file in lib/tasks:
Rake::Task["db:structure:dump"].enhance do
path = Rails.root.join('db', 'structure.sql')
File.write path, File.read(path).gsub(/ AUTO_INCREMENT=\d*/, '')
end
This has the advantage of making the change in behavior appear more intentional (as suggested by: http://edgar.tumblr.com/post/52300664342/how-to-extend-an-existing-rake-task), esp. if put into a descriptively named file (e.g. 'skip_auto_increment.rake').
@Ian, thank you very much for the hint. But on Rails 3.2 now it is much simpler. There is no need to invoke tasks since it leads to stack level too deep error and there is no need to override the task description. So my code is like that:
namespace :db do
namespace :structure do
task :dump do
path = Rails.root.join('db', 'structure.sql')
File.write path, File.read(path).gsub(/ AUTO_INCREMENT=\d*/, '')
end
end
end
Works both for db:migrate
and db:structure:dump
.
At Razoo, we ended up overriding db:migrate
task :migrate do
Rake::Task['db:migrate'].invoke
Rake::Task['db:structure:dump'].invoke
end
and then db:structure:dump
namespace :structure do
desc "Dump the database structure to a SQL file"
task :dump do
Rake::Task['db:structure:dump'].invoke
# open up the development_structure.sql file and gsub AUTO_INCREMENT=\d* with AUTO_INCREMENT
end
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With