Skip to content

Commit

Permalink
Add rake db:forward - opposite of db:rollback [#768 state:resolved]
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
cristibalan authored and lifo committed Aug 8, 2009
1 parent c284412 commit 7f84f14
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
10 changes: 10 additions & 0 deletions activerecord/lib/active_record/migration.rb
Expand Up @@ -397,6 +397,16 @@ def rollback(migrations_path, steps=1)
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
Expand Down
11 changes: 11 additions & 0 deletions activerecord/test/cases/migration_test.rb
Expand Up @@ -1136,6 +1136,17 @@ def test_migrator_rollback
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"
Expand Down
7 changes: 7 additions & 0 deletions railties/lib/tasks/databases.rake
Expand Up @@ -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' ]

Expand Down

0 comments on commit 7f84f14

Please sign in to comment.