diff options
author | Pratik Naik <pratiknaik@gmail.com> | 2009-08-08 20:47:14 +0100 |
---|---|---|
committer | Pratik Naik <pratiknaik@gmail.com> | 2009-08-08 20:47:14 +0100 |
commit | 6464d76feb94c7547fc6c046c010ea5be9bb6fe6 (patch) | |
tree | cc008cf182e8cbb0f06bd6b6b7e8abb4282bd405 /activerecord | |
parent | c6fe49b00921cda55af2dc311dd432795c4313f5 (diff) | |
download | rails-6464d76feb94c7547fc6c046c010ea5be9bb6fe6.tar.gz rails-6464d76feb94c7547fc6c046c010ea5be9bb6fe6.tar.bz2 rails-6464d76feb94c7547fc6c046c010ea5be9bb6fe6.zip |
DRY migration's rollback/forward methods
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/migration.rb | 29 |
1 files changed, 15 insertions, 14 deletions
diff --git a/activerecord/lib/active_record/migration.rb b/activerecord/lib/active_record/migration.rb index d205e57db3..adb3a3f75e 100644 --- a/activerecord/lib/active_record/migration.rb +++ b/activerecord/lib/active_record/migration.rb @@ -388,23 +388,11 @@ module ActiveRecord end def rollback(migrations_path, steps=1) - migrator = self.new(:down, migrations_path) - start_index = migrator.migrations.index(migrator.current_migration) - - return unless start_index - - finish = migrator.migrations[start_index + steps] - down(migrations_path, finish ? finish.version : 0) + move(:down, migrations_path, steps) 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) + move(:up, migrations_path, steps) end def up(migrations_path, target_version = nil) @@ -440,6 +428,19 @@ module ActiveRecord # Use the Active Record objects own table_name, or pre/suffix from ActiveRecord::Base if name is a symbol/string name.table_name rescue "#{ActiveRecord::Base.table_name_prefix}#{name}#{ActiveRecord::Base.table_name_suffix}" end + + private + + def move(direction, migrations_path, steps) + migrator = self.new(direction, migrations_path) + start_index = migrator.migrations.index(migrator.current_migration) + + if start_index + finish = migrator.migrations[start_index + steps] + version = finish ? finish.version : 0 + send(direction, migrations_path, version) + end + end end def initialize(direction, migrations_path, target_version = nil) |