Permalink
Browse files

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>
  • Loading branch information...
1 parent c284412 commit 7f84f14efabf3e342a231b8aa9650cb484c802d6 @cristibalan cristibalan committed with lifo Aug 8, 2009
View
10 activerecord/lib/active_record/migration.rb
@@ -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
View
11 activerecord/test/cases/migration_test.rb
@@ -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"
View
7 railties/lib/tasks/databases.rake
@@ -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' ]

0 comments on commit 7f84f14

Please sign in to comment.