Skip to content

Commit

Permalink
Fix phantom migration rollback failures caused by cross-branch acronyms
Browse files Browse the repository at this point in the history
  • Loading branch information
m-darbinyan committed Dec 18, 2024
1 parent 353075a commit 2171b6d
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/actual_db_schema/patches/migration_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def show_info_for(migration)
end

def migrate(migration)
migration.name = extract_class_name(migration.filename)

message = "[ActualDbSchema] Rolling back phantom migration #{migration.version} #{migration.name} " \
"(from branch: #{branch_for(migration.version.to_s)})"
puts colorize(message, :gray)
Expand All @@ -89,6 +91,11 @@ def migrate(migration)
File.delete(migration.filename)
end

def extract_class_name(filename)
content = File.read(filename)
content.match(/^class\s+([A-Za-z0-9_]+)\s+</)[1]
end

def branch_for(version)
metadata.fetch(version, {})[:branch] || "unknown"
end
Expand Down
27 changes: 27 additions & 0 deletions test/rake_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,33 @@ def down
assert_equal %w[20130906111510_irreversible.rb], utils.migrated_files
end
end

describe "with acronyms defined" do
before do
utils.define_migration_file("20241218064344_ts360.rb", <<~RUBY)
class Ts360 < ActiveRecord::Migration[6.0]
def up
TestingState.up << :ts360
end
def down
TestingState.down << :ts360
end
end
RUBY
end

it "rolls back the phantom migrations without failing" do
utils.prepare_phantom_migrations
assert_equal %i[first second ts360], TestingState.up
assert_empty ActualDbSchema.failed
utils.define_acronym("TS360")
utils.run_migrations
assert_equal %i[ts360 second first], TestingState.down
assert_empty ActualDbSchema.failed
assert_empty utils.migrated_files
end
end
end

describe "db:rollback_branches:manual" do
Expand Down
6 changes: 6 additions & 0 deletions test/support/test_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ def branch_for(version)
metadata.fetch(version.to_s, {})[:branch]
end

def define_acronym(acronym)
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym acronym
end
end

private

def cleanup_call(prefix_name = nil)
Expand Down

0 comments on commit 2171b6d

Please sign in to comment.