Skip to content

Commit

Permalink
Delete rolled back phantom migration (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-darbinyan authored Dec 17, 2024
1 parent ff0040d commit 353075a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/actual_db_schema/patches/migration_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ def migrate(migration)
migrator = down_migrator_for(migration)
migrator.extend(ActualDbSchema::Patches::Migrator)
migrator.migrate
File.delete(migration.filename)
end

def branch_for(version)
Expand Down
5 changes: 5 additions & 0 deletions test/rake_task_secondary_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
assert_empty TestingState.down
utils.run_migrations
assert_equal %i[second first], TestingState.down
assert_empty utils.migrated_files
end

describe "with irreversible migration" do
Expand All @@ -61,6 +62,7 @@ def down
assert_empty ActualDbSchema.failed
utils.run_migrations
assert_equal(%w[20130906111513_irreversible.rb], ActualDbSchema.failed.map { |m| File.basename(m.filename) })
assert_equal %w[20130906111513_irreversible.rb], utils.migrated_files
end
end
end
Expand All @@ -76,6 +78,7 @@ def down
Rake::Task["db:rollback_branches:manual"].reenable
end
assert_equal %i[second first], TestingState.down
assert_empty utils.migrated_files
end

it "skips migrations if the input is 'n'" do
Expand All @@ -90,6 +93,7 @@ def down
end
assert_empty TestingState.down
assert_equal %i[first second], TestingState.up
assert_equal %w[20130906111511_first.rb 20130906111512_second.rb], utils.migrated_files
end

describe "with irreversible migration" do
Expand Down Expand Up @@ -117,6 +121,7 @@ def down
end
assert_equal %i[second first], TestingState.down
assert_equal(%w[20130906111513_irreversible.rb], ActualDbSchema.failed.map { |m| File.basename(m.filename) })
assert_equal %w[20130906111513_irreversible.rb], utils.migrated_files
end
end
end
Expand Down
6 changes: 6 additions & 0 deletions test/rake_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
utils.run_migrations
assert_equal %i[second first], TestingState.down
assert_match(/\[ActualDbSchema\] Rolling back phantom migration/, TestingState.output)
assert_empty utils.migrated_files
end

describe "with irreversible migration" do
Expand All @@ -62,6 +63,7 @@ def down
assert_equal(%w[20130906111513_irreversible.rb], ActualDbSchema.failed.map { |m| File.basename(m.filename) })
assert_match(/Error encountered during rollback:/, TestingState.output)
assert_match(/ActiveRecord::IrreversibleMigration/, TestingState.output)
assert_equal %w[20130906111513_irreversible.rb], utils.migrated_files
end
end

Expand Down Expand Up @@ -90,6 +92,7 @@ def down
assert_match(/Try these steps to fix and move forward:/, TestingState.output)
assert_match(/Below are the details of the problematic migrations:/, TestingState.output)
assert_match(%r{File: tmp/migrated/20130906111510_irreversible.rb}, TestingState.output)
assert_equal %w[20130906111510_irreversible.rb], utils.migrated_files
end
end
end
Expand All @@ -105,6 +108,7 @@ def down
Rake::Task["db:rollback_branches:manual"].reenable
end
assert_equal %i[second first], TestingState.down
assert_empty utils.migrated_files
end

it "skips migrations if the input is 'n'" do
Expand All @@ -119,6 +123,7 @@ def down
end
assert_empty TestingState.down
assert_equal %i[first second], TestingState.up
assert_equal %w[20130906111511_first.rb 20130906111512_second.rb], utils.migrated_files
end

describe "with irreversible migration" do
Expand Down Expand Up @@ -146,6 +151,7 @@ def down
end
assert_equal %i[second first], TestingState.down
assert_equal(%w[20130906111513_irreversible.rb], ActualDbSchema.failed.map { |m| File.basename(m.filename) })
assert_equal %w[20130906111513_irreversible.rb], utils.migrated_files
end
end
end
Expand Down
18 changes: 18 additions & 0 deletions test/rake_tasks_all_databases_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
assert_empty TestingState.down
utils.run_migrations
assert_equal %i[second_primary first_primary second_secondary first_secondary], TestingState.down
assert_empty utils.migrated_files(TestingState.db_config)
end

describe "with irreversible migration" do
Expand Down Expand Up @@ -82,6 +83,10 @@ def down
utils.run_migrations
failed = ActualDbSchema.failed.map { |m| File.basename(m.filename) }
assert_equal(%w[20130906111513_irreversible_primary.rb 20130906111513_irreversible_secondary.rb], failed)
assert_equal(
%w[20130906111513_irreversible_primary.rb 20130906111513_irreversible_secondary.rb],
utils.migrated_files(TestingState.db_config)
)
end
end
end
Expand All @@ -99,6 +104,15 @@ def down
end
assert_empty TestingState.down
assert_equal %i[first_primary second_primary first_secondary second_secondary], TestingState.up
assert_equal(
%w[
20130906111511_first_primary.rb
20130906111512_second_primary.rb
20130906111514_first_secondary.rb
20130906111515_second_secondary.rb
],
utils.migrated_files(TestingState.db_config)
)
end

describe "with irreversible migration" do
Expand Down Expand Up @@ -131,6 +145,10 @@ def down
end
failed = ActualDbSchema.failed.map { |m| File.basename(m.filename) }
assert_equal(%w[20130906111513_irreversible_primary.rb 20130906111513_irreversible_secondary.rb], failed)
assert_equal(
%w[20130906111513_irreversible_primary.rb 20130906111513_irreversible_secondary.rb],
utils.migrated_files(TestingState.db_config)
)
end
end
end
Expand Down

0 comments on commit 353075a

Please sign in to comment.