From 8a5a9dcbf64843f064b6e8a0b9c6eea8f0b8536e Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Wed, 9 Apr 2008 16:20:15 +0000 Subject: Add support for interleaving migrations by storing which migrations have run in the new schema_migrations table. Closes #11493 [jordi] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@9244 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/test/cases/ar_schema_test.rb | 4 +- activerecord/test/cases/migration_test.rb | 52 ++++++++++++++++++---- activerecord/test/cases/schema_dumper_test.rb | 6 +-- .../interleaved/pass_1/3_innocent_jointable.rb | 12 +++++ .../interleaved/pass_2/1_people_have_last_names.rb | 9 ++++ .../interleaved/pass_2/3_innocent_jointable.rb | 12 +++++ .../interleaved/pass_3/1_people_have_last_names.rb | 9 ++++ .../interleaved/pass_3/2_i_raise_on_down.rb | 8 ++++ .../interleaved/pass_3/3_innocent_jointable.rb | 12 +++++ activerecord/test/schema/sybase.drop.sql | 2 +- 10 files changed, 111 insertions(+), 15 deletions(-) create mode 100644 activerecord/test/migrations/interleaved/pass_1/3_innocent_jointable.rb create mode 100644 activerecord/test/migrations/interleaved/pass_2/1_people_have_last_names.rb create mode 100644 activerecord/test/migrations/interleaved/pass_2/3_innocent_jointable.rb create mode 100644 activerecord/test/migrations/interleaved/pass_3/1_people_have_last_names.rb create mode 100644 activerecord/test/migrations/interleaved/pass_3/2_i_raise_on_down.rb create mode 100644 activerecord/test/migrations/interleaved/pass_3/3_innocent_jointable.rb (limited to 'activerecord/test') diff --git a/activerecord/test/cases/ar_schema_test.rb b/activerecord/test/cases/ar_schema_test.rb index 9fdd8398b7..431dc7a141 100644 --- a/activerecord/test/cases/ar_schema_test.rb +++ b/activerecord/test/cases/ar_schema_test.rb @@ -25,8 +25,8 @@ if ActiveRecord::Base.connection.supports_migrations? end assert_nothing_raised { @connection.select_all "SELECT * FROM fruits" } - assert_nothing_raised { @connection.select_all "SELECT * FROM schema_info" } - assert_equal 7, @connection.select_one("SELECT version FROM schema_info")['version'].to_i + assert_nothing_raised { @connection.select_all "SELECT * FROM schema_migrations" } + assert_equal 7, ActiveRecord::Migrator::current_version end end diff --git a/activerecord/test/cases/migration_test.rb b/activerecord/test/cases/migration_test.rb index eb6723bbf3..885d89b6da 100644 --- a/activerecord/test/cases/migration_test.rb +++ b/activerecord/test/cases/migration_test.rb @@ -7,6 +7,7 @@ require 'models/topic' require MIGRATIONS_ROOT + "/valid/1_people_have_last_names" require MIGRATIONS_ROOT + "/valid/2_we_need_reminders" require MIGRATIONS_ROOT + "/decimal/1_give_me_big_numbers" +require MIGRATIONS_ROOT + "/interleaved/pass_3/2_i_raise_on_down" if ActiveRecord::Base.connection.supports_migrations? class BigNumber < ActiveRecord::Base; end @@ -34,8 +35,8 @@ if ActiveRecord::Base.connection.supports_migrations? end def teardown - ActiveRecord::Base.connection.initialize_schema_information - ActiveRecord::Base.connection.update "UPDATE #{ActiveRecord::Migrator.schema_info_table_name} SET version = 0" + ActiveRecord::Base.connection.initialize_schema_migrations_table + ActiveRecord::Base.connection.execute "DELETE FROM #{ActiveRecord::Migrator.schema_migrations_table_name}" %w(reminders people_reminders prefix_reminders_suffix).each do |table| Reminder.connection.drop_table(table) rescue nil @@ -779,6 +780,39 @@ if ActiveRecord::Base.connection.supports_migrations? assert !Reminder.table_exists? end + def test_finds_migrations + migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/valid").migrations + [['1', 'people_have_last_names'], + ['2', 'we_need_reminders'], + ['3', 'innocent_jointable']].each_with_index do |pair, i| + migrations[i].version == pair.first + migrations[1].name == pair.last + end + end + + def test_finds_pending_migrations + ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_2", 1) + migrations = ActiveRecord::Migrator.new(:up, MIGRATIONS_ROOT + "/interleaved/pass_2").pending_migrations + assert_equal 1, migrations.size + migrations[0].version == '3' + migrations[0].name == 'innocent_jointable' + end + + def test_migrator_interleaved_migrations + ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_1") + + assert_nothing_raised do + ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/interleaved/pass_2") + end + + Person.reset_column_information + assert Person.column_methods_hash.include?(:last_name) + + assert_nothing_raised do + ActiveRecord::Migrator.down(MIGRATIONS_ROOT + "/interleaved/pass_3") + end + end + def test_migrator_verbosity ActiveRecord::Migrator.up(MIGRATIONS_ROOT + "/valid", 1) assert PeopleHaveLastNames.message_count > 0 @@ -817,16 +851,16 @@ if ActiveRecord::Base.connection.supports_migrations? assert_equal(3, ActiveRecord::Migrator.current_version) ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid") - assert_equal(2, ActiveRecord::Migrator.current_version) + assert_equal(2, ActiveRecord::Migrator.current_version) ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid") - assert_equal(1, ActiveRecord::Migrator.current_version) + assert_equal(1, ActiveRecord::Migrator.current_version) ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid") - assert_equal(0, ActiveRecord::Migrator.current_version) + assert_equal(0, ActiveRecord::Migrator.current_version) ActiveRecord::Migrator.rollback(MIGRATIONS_ROOT + "/valid") - assert_equal(0, ActiveRecord::Migrator.current_version) + assert_equal(0, ActiveRecord::Migrator.current_version) end def test_migrator_run @@ -839,15 +873,15 @@ if ActiveRecord::Base.connection.supports_migrations? assert_equal(0, ActiveRecord::Migrator.current_version) end - def test_schema_info_table_name + def test_schema_migrations_table_name ActiveRecord::Base.table_name_prefix = "prefix_" ActiveRecord::Base.table_name_suffix = "_suffix" Reminder.reset_table_name - assert_equal "prefix_schema_info_suffix", ActiveRecord::Migrator.schema_info_table_name + assert_equal "prefix_schema_migrations_suffix", ActiveRecord::Migrator.schema_migrations_table_name ActiveRecord::Base.table_name_prefix = "" ActiveRecord::Base.table_name_suffix = "" Reminder.reset_table_name - assert_equal "schema_info", ActiveRecord::Migrator.schema_info_table_name + assert_equal "schema_migrations", ActiveRecord::Migrator.schema_migrations_table_name ensure ActiveRecord::Base.table_name_prefix = "" ActiveRecord::Base.table_name_suffix = "" diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index d96bd62e56..ba8bff3b44 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -16,7 +16,7 @@ if ActiveRecord::Base.connection.respond_to?(:tables) output = standard_dump assert_match %r{create_table "accounts"}, output assert_match %r{create_table "authors"}, output - assert_no_match %r{create_table "schema_info"}, output + assert_no_match %r{create_table "schema_migrations"}, output end def test_schema_dump_excludes_sqlite_sequence @@ -81,7 +81,7 @@ if ActiveRecord::Base.connection.respond_to?(:tables) output = stream.string assert_no_match %r{create_table "accounts"}, output assert_match %r{create_table "authors"}, output - assert_no_match %r{create_table "schema_info"}, output + assert_no_match %r{create_table "schema_migrations"}, output end def test_schema_dump_with_regexp_ignored_table @@ -92,7 +92,7 @@ if ActiveRecord::Base.connection.respond_to?(:tables) output = stream.string assert_no_match %r{create_table "accounts"}, output assert_match %r{create_table "authors"}, output - assert_no_match %r{create_table "schema_info"}, output + assert_no_match %r{create_table "schema_migrations"}, output end def test_schema_dump_illegal_ignored_table_value diff --git a/activerecord/test/migrations/interleaved/pass_1/3_innocent_jointable.rb b/activerecord/test/migrations/interleaved/pass_1/3_innocent_jointable.rb new file mode 100644 index 0000000000..21c9ca5328 --- /dev/null +++ b/activerecord/test/migrations/interleaved/pass_1/3_innocent_jointable.rb @@ -0,0 +1,12 @@ +class InnocentJointable < ActiveRecord::Migration + def self.up + create_table("people_reminders", :id => false) do |t| + t.column :reminder_id, :integer + t.column :person_id, :integer + end + end + + def self.down + drop_table "people_reminders" + end +end \ No newline at end of file diff --git a/activerecord/test/migrations/interleaved/pass_2/1_people_have_last_names.rb b/activerecord/test/migrations/interleaved/pass_2/1_people_have_last_names.rb new file mode 100644 index 0000000000..81af5fef5e --- /dev/null +++ b/activerecord/test/migrations/interleaved/pass_2/1_people_have_last_names.rb @@ -0,0 +1,9 @@ +class PeopleHaveLastNames < ActiveRecord::Migration + def self.up + add_column "people", "last_name", :string + end + + def self.down + remove_column "people", "last_name" + end +end \ No newline at end of file diff --git a/activerecord/test/migrations/interleaved/pass_2/3_innocent_jointable.rb b/activerecord/test/migrations/interleaved/pass_2/3_innocent_jointable.rb new file mode 100644 index 0000000000..21c9ca5328 --- /dev/null +++ b/activerecord/test/migrations/interleaved/pass_2/3_innocent_jointable.rb @@ -0,0 +1,12 @@ +class InnocentJointable < ActiveRecord::Migration + def self.up + create_table("people_reminders", :id => false) do |t| + t.column :reminder_id, :integer + t.column :person_id, :integer + end + end + + def self.down + drop_table "people_reminders" + end +end \ No newline at end of file diff --git a/activerecord/test/migrations/interleaved/pass_3/1_people_have_last_names.rb b/activerecord/test/migrations/interleaved/pass_3/1_people_have_last_names.rb new file mode 100644 index 0000000000..81af5fef5e --- /dev/null +++ b/activerecord/test/migrations/interleaved/pass_3/1_people_have_last_names.rb @@ -0,0 +1,9 @@ +class PeopleHaveLastNames < ActiveRecord::Migration + def self.up + add_column "people", "last_name", :string + end + + def self.down + remove_column "people", "last_name" + end +end \ No newline at end of file diff --git a/activerecord/test/migrations/interleaved/pass_3/2_i_raise_on_down.rb b/activerecord/test/migrations/interleaved/pass_3/2_i_raise_on_down.rb new file mode 100644 index 0000000000..9b1ce9f017 --- /dev/null +++ b/activerecord/test/migrations/interleaved/pass_3/2_i_raise_on_down.rb @@ -0,0 +1,8 @@ +class IRaiseOnDown < ActiveRecord::Migration + def self.up + end + + def self.down + raise + end +end \ No newline at end of file diff --git a/activerecord/test/migrations/interleaved/pass_3/3_innocent_jointable.rb b/activerecord/test/migrations/interleaved/pass_3/3_innocent_jointable.rb new file mode 100644 index 0000000000..21c9ca5328 --- /dev/null +++ b/activerecord/test/migrations/interleaved/pass_3/3_innocent_jointable.rb @@ -0,0 +1,12 @@ +class InnocentJointable < ActiveRecord::Migration + def self.up + create_table("people_reminders", :id => false) do |t| + t.column :reminder_id, :integer + t.column :person_id, :integer + end + end + + def self.down + drop_table "people_reminders" + end +end \ No newline at end of file diff --git a/activerecord/test/schema/sybase.drop.sql b/activerecord/test/schema/sybase.drop.sql index ebb91931fb..1a2cc9ff56 100644 --- a/activerecord/test/schema/sybase.drop.sql +++ b/activerecord/test/schema/sybase.drop.sql @@ -31,5 +31,5 @@ DROP TABLE legacy_things DROP TABLE numeric_data DROP TABLE mixed_case_monkeys DROP TABLE minimalistics -DROP TABLE schema_info +DROP TABLE schema_migrations go -- cgit v1.2.3