diff options
author | Piotr Sarnacki <drogus@gmail.com> | 2010-07-26 14:06:14 +0200 |
---|---|---|
committer | Piotr Sarnacki <drogus@gmail.com> | 2010-09-03 22:59:09 +0200 |
commit | 75f8ac6ea71e4b2337f870b91ac05df98f33a8d2 (patch) | |
tree | b3fe95ec53958ce38a2b5fe23e7878dafd675b92 /activerecord/test/cases | |
parent | 2068b8cb6a8508fae9cd1a7f57e68d938c6403e6 (diff) | |
download | rails-75f8ac6ea71e4b2337f870b91ac05df98f33a8d2.tar.gz rails-75f8ac6ea71e4b2337f870b91ac05df98f33a8d2.tar.bz2 rails-75f8ac6ea71e4b2337f870b91ac05df98f33a8d2.zip |
Implemented ActiveRecord::Migrations#copy based on James Adam's idea
ActiveRecord::Migration#copy allows to copy migrations from one place
to another, changing migrations versions and adding scope to filename.
For example:
ActiveRecord::Migration.copy("db/migrate",
:blog_engine => "vendor/gems/blog/db/migrate")
will copy all migrations from vendor/gems/blog/db/migrate to db/migrate
with such format:
Versions of copied migrations will be reversioned to be appended after
migrations that already exists in db/migrate
Diffstat (limited to 'activerecord/test/cases')
-rw-r--r-- | activerecord/test/cases/helper.rb | 18 | ||||
-rw-r--r-- | activerecord/test/cases/migration_test.rb | 111 |
2 files changed, 129 insertions, 0 deletions
diff --git a/activerecord/test/cases/helper.rb b/activerecord/test/cases/helper.rb index 1fb59d3589..4bf3c25d28 100644 --- a/activerecord/test/cases/helper.rb +++ b/activerecord/test/cases/helper.rb @@ -83,3 +83,21 @@ begin ensure $stdout = original_stdout end + +class << Time + unless method_defined? :now_before_time_travel + alias_method :now_before_time_travel, :now + end + + def now + (@now ||= nil) || now_before_time_travel + end + + def travel_to(time, &block) + @now = time + block.call + ensure + @now = nil + end +end + diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index 96b97fdd8a..85e1898b15 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -1875,5 +1875,116 @@ if ActiveRecord::Base.connection.supports_migrations? end end end + + class CopyMigrationsTest < ActiveRecord::TestCase + def setup + end + + def clear + ActiveRecord::Base.timestamped_migrations = true + to_delete = Dir[@migrations_path + "/*.rb"] - @existing_migrations + File.delete(*to_delete) + end + + def test_copying_migrations_without_timestamps + ActiveRecord::Base.timestamped_migrations = false + @migrations_path = MIGRATIONS_ROOT + "/valid" + @existing_migrations = Dir[@migrations_path + "/*.rb"] + + copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy"}) + assert File.exists?(@migrations_path + "/4_people_have_hobbies.bukkits.rb") + assert File.exists?(@migrations_path + "/5_people_have_descriptions.bukkits.rb") + assert_equal [@migrations_path + "/4_people_have_hobbies.bukkits.rb", @migrations_path + "/5_people_have_descriptions.bukkits.rb"], copied + + files_count = Dir[@migrations_path + "/*.rb"].length + copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy"}) + assert_equal files_count, Dir[@migrations_path + "/*.rb"].length + assert copied.empty? + ensure + clear + end + + def test_copying_migrations_without_timestamps_from_2_sources + ActiveRecord::Base.timestamped_migrations = false + @migrations_path = MIGRATIONS_ROOT + "/valid" + @existing_migrations = Dir[@migrations_path + "/*.rb"] + + sources = ActiveSupport::OrderedHash.new + sources[:bukkits] = sources[:omg] = MIGRATIONS_ROOT + "/to_copy" + ActiveRecord::Migration.copy(@migrations_path, sources) + assert File.exists?(@migrations_path + "/4_people_have_hobbies.omg.rb") + assert File.exists?(@migrations_path + "/5_people_have_descriptions.omg.rb") + assert File.exists?(@migrations_path + "/6_people_have_hobbies.bukkits.rb") + assert File.exists?(@migrations_path + "/7_people_have_descriptions.bukkits.rb") + + files_count = Dir[@migrations_path + "/*.rb"].length + ActiveRecord::Migration.copy(@migrations_path, sources) + assert_equal files_count, Dir[@migrations_path + "/*.rb"].length + ensure + clear + end + + def test_copying_migrations_with_timestamps + @migrations_path = MIGRATIONS_ROOT + "/valid_with_timestamps" + @existing_migrations = Dir[@migrations_path + "/*.rb"] + + Time.travel_to(created_at = Time.utc(2010, 7, 26, 10, 10, 10)) do + copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy_with_timestamps"}) + assert File.exists?(@migrations_path + "/20100726101010_people_have_hobbies.bukkits.rb") + assert File.exists?(@migrations_path + "/20100726101011_people_have_descriptions.bukkits.rb") + expected = [@migrations_path + "/20100726101010_people_have_hobbies.bukkits.rb", + @migrations_path + "/20100726101011_people_have_descriptions.bukkits.rb"] + assert_equal expected, copied + + files_count = Dir[@migrations_path + "/*.rb"].length + copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy_with_timestamps"}) + assert_equal files_count, Dir[@migrations_path + "/*.rb"].length + assert copied.empty? + end + ensure + clear + end + + def test_copying_migrations_with_timestamps_from_2_sources + @migrations_path = MIGRATIONS_ROOT + "/valid_with_timestamps" + @existing_migrations = Dir[@migrations_path + "/*.rb"] + + sources = ActiveSupport::OrderedHash.new + sources[:bukkits] = sources[:omg] = MIGRATIONS_ROOT + "/to_copy_with_timestamps" + + Time.travel_to(created_at = Time.utc(2010, 7, 26, 10, 10, 10)) do + copied = ActiveRecord::Migration.copy(@migrations_path, sources) + assert File.exists?(@migrations_path + "/20100726101010_people_have_hobbies.omg.rb") + assert File.exists?(@migrations_path + "/20100726101011_people_have_descriptions.omg.rb") + assert File.exists?(@migrations_path + "/20100726101012_people_have_hobbies.bukkits.rb") + assert File.exists?(@migrations_path + "/20100726101013_people_have_descriptions.bukkits.rb") + assert_equal 4, copied.length + + files_count = Dir[@migrations_path + "/*.rb"].length + ActiveRecord::Migration.copy(@migrations_path, sources) + assert_equal files_count, Dir[@migrations_path + "/*.rb"].length + end + ensure + clear + end + + def test_copying_migrations_with_timestamps_to_destination_with_timestamps_in_future + @migrations_path = MIGRATIONS_ROOT + "/valid_with_timestamps" + @existing_migrations = Dir[@migrations_path + "/*.rb"] + + Time.travel_to(created_at = Time.utc(2010, 2, 20, 10, 10, 10)) do + ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy_with_timestamps"}) + assert File.exists?(@migrations_path + "/20100301010102_people_have_hobbies.bukkits.rb") + assert File.exists?(@migrations_path + "/20100301010103_people_have_descriptions.bukkits.rb") + + files_count = Dir[@migrations_path + "/*.rb"].length + copied = ActiveRecord::Migration.copy(@migrations_path, {:bukkits => MIGRATIONS_ROOT + "/to_copy_with_timestamps"}) + assert_equal files_count, Dir[@migrations_path + "/*.rb"].length + assert copied.empty? + end + ensure + clear + end + end end |