Add rake db:forward - opposite of db:rollback [#768 state:resolved]

Example:
  rake db:forward # performs the next migration
  rake db:forward STEP=4 # performs the next 4 migrations

Signed-off-by: Pratik Naik <pratiknaik@gmail.com>
This commit is contained in:
Cristi Balan 2009-08-08 17:39:31 +01:00 committed by Pratik Naik
parent c284412b14
commit 7f84f14efa
3 changed files with 28 additions and 0 deletions

View File

@ -397,6 +397,16 @@ module ActiveRecord
down(migrations_path, finish ? finish.version : 0)
end
def forward(migrations_path, steps=1)
migrator = self.new(:up, migrations_path)
start_index = migrator.migrations.index(migrator.current_migration)
return unless start_index
finish = migrator.migrations[start_index + steps]
up(migrations_path, finish ? finish.version : 0)
end
def up(migrations_path, target_version = nil)
self.new(:up, migrations_path, target_version).migrate
end

View File

@ -1136,6 +1136,17 @@ if ActiveRecord::Base.connection.supports_migrations?
assert_equal(0, ActiveRecord::Migrator.current_version)
end
def test_migrator_forward
ActiveRecord::Migrator.migrate(MIGRATIONS_ROOT + "/valid", 1)
assert_equal(1, ActiveRecord::Migrator.current_version)
ActiveRecord::Migrator.forward(MIGRATIONS_ROOT + "/valid", 2)
assert_equal(3, ActiveRecord::Migrator.current_version)
ActiveRecord::Migrator.forward(MIGRATIONS_ROOT + "/valid")
assert_equal(3, ActiveRecord::Migrator.current_version)
end
def test_schema_migrations_table_name
ActiveRecord::Base.table_name_prefix = "prefix_"
ActiveRecord::Base.table_name_suffix = "_suffix"

View File

@ -172,6 +172,13 @@ namespace :db do
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
desc 'Pushes the schema to the next version. Specify the number of steps with STEP=n'
task :forward => :environment do
step = ENV['STEP'] ? ENV['STEP'].to_i : 1
ActiveRecord::Migrator.forward('db/migrate/', step)
Rake::Task["db:schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end
desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
task :reset => [ 'db:drop', 'db:setup' ]