Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Out-of-sync AUTO_INCREMENT values in development_structure.sql from Rails/MySQL creates diff noise

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.

like image 880
Ian Lotinsky Avatar asked Feb 05 '10 21:02

Ian Lotinsky


3 Answers

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').

like image 33
Jan Hettich Avatar answered Oct 02 '22 14:10

Jan Hettich


@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.

like image 120
sekrett Avatar answered Oct 02 '22 14:10

sekrett


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
like image 28
Ian Lotinsky Avatar answered Oct 02 '22 13:10

Ian Lotinsky