Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added a VERSION parameter to the migrate task that allows you to do "…

…rake migrate VERSION=34" to migrate to the 34th version traveling up or down depending on the current version. Added ActiveRecord::Migrator.migrate that can figure out whether to go up or down based on the target version and the current

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1780 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 47a3bafe26fb5f773ac082162c3e595ce80d9ad7 1 parent 8266867
@dhh dhh authored
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Added ActiveRecord::Migrator.migrate that can figure out whether to go up or down based on the target version and the current
+
* Added better error message for "packets out of order" #1630 [courtenay]
* Fixed first run of "rake migrate" on PostgreSQL by not expecting a return value on the id #1640
View
23 activerecord/lib/active_record/migration.rb
@@ -108,6 +108,18 @@ class IrreversibleMigration < ActiveRecordError#:nodoc:
# add_column :items, :completed_items_count
# end
# end
+ #
+ # And some times you need to do something in SQL not abstracted directly by migrations:
+ #
+ # class MakeJoinUnique < ActiveRecord::Migration
+ # def self.up
+ # execute "ALTER TABLE `pages_linked_pages` ADD UNIQUE `page_id_linked_page_id` (`page_id`,`linked_page_id`)"
+ # end
+ #
+ # def self.down
+ # execute "ALTER TABLE `pages_linked_pages` DROP INDEX `page_id_linked_page_id`"
+ # end
+ # end
class Migration
class << self
def up() end
@@ -122,6 +134,17 @@ def method_missing(method, *arguments, &block)
class Migrator#:nodoc:
class << self
+ def migrate(migrations_path, target_version = nil)
+ case
+ when target_version.nil?, current_version < target_version
+ up(migrations_path, target_version)
+ when current_version > target_version
+ down(migrations_path, target_version)
+ when current_version == target_version
+ return # You're on the right version
+ end
+ end
+
def up(migrations_path, target_version = nil)
self.new(:up, migrations_path, target_version).migrate
end
View
15 activerecord/test/migration_test.rb
@@ -182,5 +182,20 @@ def test_migrator_one_up_one_down
assert !Person.column_methods_hash.include?(:last_name)
assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }
end
+
+ def test_migrator_going_down_due_to_version_target
+ ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/fixtures/migrations/', 1)
+ ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations/', 0)
+
+ assert !Person.column_methods_hash.include?(:last_name)
+ assert_raises(ActiveRecord::StatementInvalid) { Reminder.column_methods_hash }
+
+ ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/fixtures/migrations/')
+
+ Person.reset_column_information
+ assert Person.column_methods_hash.include?(:last_name)
+ assert Reminder.create("content" => "hello world", "remind_at" => Time.now)
+ assert_equal "hello world", Reminder.find(:first).content
+ end
end
end
View
7 railties/CHANGELOG
@@ -1,8 +1,11 @@
*SVN*
-* Extend Ruby version check to include RUBY_RELEASE_DATE >= '2005-12-25', the final Ruby 1.8.2 release. #1674 [court3nay@gmail.com]
+* Added a VERSION parameter to the migrate task that allows you to do "rake migrate VERSION=34" to migrate to the 34th version traveling up or down depending on the current version
+
+* Extend Ruby version check to include RUBY_RELEASE_DATE >= '2005-12-25', the final Ruby 1.8.2 release #1674 [court3nay@gmail.com]
+
+* Improved documentation for environment config files #1625 [court3nay@gmail.com]
-* Improved documentation for environment config files. #1625 [court3nay@gmail.com]
*0.13.0*
View
4 railties/fresh_rakefile
@@ -187,7 +187,7 @@ task :clear_logs => :environment do
end
end
-desc "Migrate the database according to the migrate scripts in db/migrate"
+desc "Migrate the database according to the migrate scripts in db/migrate (only supported on PG/MySQL). A specific version can be targetted with VERSION=x"
task :migrate => :environment do
- ActiveRecord::Migrator.up(File.dirname(__FILE__) + '/db/migrate/')
+ ActiveRecord::Migrator.migrate(File.dirname(__FILE__) + '/db/migrate/', ENV["VERSION"])
end
Please sign in to comment.
Something went wrong with that request. Please try again.