require "cases/helper" module ActiveRecord class InvertibleMigrationTest < ActiveRecord::TestCase class SilentMigration < ActiveRecord::Migration def write(text = '') # sssshhhhh!! end end class InvertibleMigration < SilentMigration def change create_table("horses") do |t| t.column :content, :text t.column :remind_at, :datetime end end end class InvertibleRevertMigration < SilentMigration def change revert do create_table("horses") do |t| t.column :content, :text t.column :remind_at, :datetime end end end end class NonInvertibleMigration < SilentMigration def change create_table("horses") do |t| t.column :content, :text t.column :remind_at, :datetime end remove_column "horses", :content end end class LegacyMigration < ActiveRecord::Migration def self.up create_table("horses") do |t| t.column :content, :text t.column :remind_at, :datetime end end def self.down drop_table("horses") end end class RevertWholeMigration < SilentMigration def initialize(name = self.class.name, version = nil, migration) @migration = migration super(name, version) end def change revert @migration end end class NestedRevertWholeMigration < RevertWholeMigration def change revert { super } end end def teardown if ActiveRecord::Base.connection.table_exists?("horses") ActiveRecord::Base.connection.drop_table("horses") end end def test_no_reverse migration = NonInvertibleMigration.new migration.migrate(:up) assert_raises(IrreversibleMigration) do migration.migrate(:down) end end def test_migrate_up migration = InvertibleMigration.new migration.migrate(:up) assert migration.connection.table_exists?("horses"), "horses should exist" end def test_migrate_down migration = InvertibleMigration.new migration.migrate :up migration.migrate :down assert !migration.connection.table_exists?("horses") end def test_migrate_revert migration = InvertibleMigration.new revert = InvertibleRevertMigration.new migration.migrate :up revert.migrate :up assert !migration.connection.table_exists?("horses") revert.migrate :down assert migration.connection.table_exists?("horses") migration.migrate :down assert !migration.connection.table_exists?("horses") end def test_migrate_revert_whole_migration migration = InvertibleMigration.new [LegacyMigration, InvertibleMigration].each do |klass| revert = RevertWholeMigration.new(klass) migration.migrate :up revert.migrate :up assert !migration.connection.table_exists?("horses") revert.migrate :down assert migration.connection.table_exists?("horses") migration.migrate :down assert !migration.connection.table_exists?("horses") end end def test_migrate_nested_revert_whole_migration revert = NestedRevertWholeMigration.new(InvertibleRevertMigration) revert.migrate :down assert revert.connection.table_exists?("horses") revert.migrate :up assert !revert.connection.table_exists?("horses") end def test_revert_order block = Proc.new{|t| t.string :name } recorder = ActiveRecord::Migration::CommandRecorder.new(ActiveRecord::Base.connection) recorder.instance_eval do create_table("apples", &block) revert do create_table("bananas", &block) revert do create_table("clementines") create_table("dates") end create_table("elderberries") end revert do create_table("figs") create_table("grapes") end end assert_equal [[:create_table, ["apples"], block], [:drop_table, ["elderberries"]], [:create_table, ["clementines"], nil], [:create_table, ["dates"], nil], [:drop_table, ["bananas"]], [:drop_table, ["grapes"]], [:drop_table, ["figs"]]], recorder.commands end def test_legacy_up LegacyMigration.migrate :up assert ActiveRecord::Base.connection.table_exists?("horses"), "horses should exist" end def test_legacy_down LegacyMigration.migrate :up LegacyMigration.migrate :down assert !ActiveRecord::Base.connection.table_exists?("horses"), "horses should not exist" end def test_up LegacyMigration.up assert ActiveRecord::Base.connection.table_exists?("horses"), "horses should exist" end def test_down LegacyMigration.up LegacyMigration.down assert !ActiveRecord::Base.connection.table_exists?("horses"), "horses should not exist" end def test_migrate_down_with_table_name_prefix ActiveRecord::Base.table_name_prefix = 'p_' ActiveRecord::Base.table_name_suffix = '_s' migration = InvertibleMigration.new migration.migrate(:up) assert_nothing_raised { migration.migrate(:down) } assert !ActiveRecord::Base.connection.table_exists?("p_horses_s"), "p_horses_s should not exist" ensure ActiveRecord::Base.table_name_prefix = ActiveRecord::Base.table_name_suffix = '' end end end