This are my notes in the fields of computer science and technology. Everything is written with ABSOLUTE NO WARRANTY of fitness for any purpose. Of course, feel free to comment anything.

Thursday, August 14, 2008

Fake migrations for rails < 2.0.2

The following rake task can be used to update the migration pointer without actually migrate in rails databases for rails under 2.0.2. This is sometimes useful if you applied something manually or want to skip some migration anyway.

Usage examples:

rake db:pretend:migrate
rake db:pretend:migrate VERSION=20080730181045
rake db:pretend:rollback
namespace(:db) do
namespace(:pretend) do
desc "Pretend the database migrated 1 step or to VERSION=<nn>"
task :migrate => :environment do
c = ActiveRecord::Base.connection
if ENV['VERSION']
version = ENV['VERSION'].to_i
else
version = c.select_one("select version from schema_info")['version'].to_i + 1
end
c.execute("update schema_info set version = #{version}")
puts "Current version: #{version}"
end
desc "Pretend that the last migration did not happen"
task :rollback => :environment do
c = ActiveRecord::Base.connection
version = c.select_one("select version from schema_info")['version'].to_i - 1
c.execute("update schema_info set version = #{version}")
puts "Current version: #{version}"
end
end
end

No comments:

About Me

My photo
Hamburg, Hamburg, Germany
Former molecular biologist and web developer (Rails) and currently research scientist in bioinformatics.