Skip to content

Commit

Permalink
Formatted output & don't fail fast (#100)
Browse files Browse the repository at this point in the history
* formatted output & don't fail fast

* fix cop

* remove temp file

* put file back

* revert changes
  • Loading branch information
ka8725 committed Sep 6, 2024
1 parent 4b01ec0 commit dc1f54b
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 9 deletions.
1 change: 1 addition & 0 deletions lib/actual_db_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
require_relative "actual_db_schema/store"
require_relative "actual_db_schema/version"
require_relative "actual_db_schema/migration"
require_relative "actual_db_schema/failed_migration"
require_relative "actual_db_schema/migration_context"
require_relative "actual_db_schema/patches/migration_proxy"
require_relative "actual_db_schema/patches/migrator"
Expand Down
40 changes: 38 additions & 2 deletions lib/actual_db_schema/commands/rollback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ module ActualDbSchema
module Commands
# Rolls back all phantom migrations
class Rollback < Base
UNICODE_COLORS = {
red: 31,
green: 32,
yellow: 33
}.freeze

def initialize(context, manual_mode: false)
@manual_mode = manual_mode || manual_mode_default?
super(context)
Expand All @@ -16,16 +22,46 @@ def call_impl

return if ActualDbSchema.failed.empty?

puts_preamble
puts_into
puts ""
puts "[ActualDbSchema] Irreversible migrations were found from other branches. Roll them back or fix manually:"
puts failed_migrations_list
puts_preamble
end

def failed_migrations_list
ActualDbSchema.failed.map.with_index(1) do |failed, index|
filename = failed.short_filename
exception = failed.exception
<<~MSG
\t#{colorize("[Migration##{index}]", :yellow)}
\t- #{filename}
\t\t#{exception.inspect.gsub("\n", "\n\t ")}
MSG
end
end

def puts_preamble
puts ""
puts ActualDbSchema.failed.map { |migration| "- #{migration.filename}" }.join("\n")
puts %(\u2757\u2757\u2757 #{colorize("[ActualDbSchema]", :red)})
puts ""
end

def puts_into
msg = "#{ActualDbSchema.failed.count} phantom migration(s) could not be rolled back automatically."
msg += " Roll them back or fix manually:"
puts colorize(msg, :red)
end

def manual_mode_default?
ActualDbSchema.config[:auto_rollback_disabled]
end

def colorize(text, color)
code = UNICODE_COLORS.fetch(color, 37)
"\e[#{code}m#{text}\e[0m"
end
end
end
end
13 changes: 13 additions & 0 deletions lib/actual_db_schema/failed_migration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# frozen_string_literal: true

module ActualDbSchema
FailedMigration = Struct.new(:migration, :exception, keyword_init: true) do
def filename
migration.filename
end

def short_filename
migration.filename.sub(File.join(Rails.root, "/"), "")
end
end
end
8 changes: 1 addition & 7 deletions lib/actual_db_schema/patches/migration_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def rollback_branches(manual_mode: false)
show_info_for(migration) if manual_mode
migrate(migration) if !manual_mode || user_wants_rollback?
rescue StandardError => e
handle_migration_error(e, migration)
ActualDbSchema.failed << FailedMigration.new(migration: migration, exception: e)
end
end

Expand Down Expand Up @@ -71,12 +71,6 @@ def show_info_for(migration)
puts File.read(migration.filename)
end

def handle_migration_error(error, migration)
raise unless error.message.include?("ActiveRecord::IrreversibleMigration")

ActualDbSchema.failed << migration
end

def migrate(migration)
migrator = down_migrator_for(migration)
migrator.extend(ActualDbSchema::Patches::Migrator)
Expand Down
25 changes: 25 additions & 0 deletions test/rake_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,31 @@ def down
assert_equal(%w[20130906111513_irreversible.rb], ActualDbSchema.failed.map { |m| File.basename(m.filename) })
end
end

describe "with irreversible migration is the first" do
before do
utils.define_migration_file("20130906111510_irreversible.rb", <<~RUBY)
class Irreversible < ActiveRecord::Migration[6.0]
def up
TestingState.up << :irreversible
end
def down
raise ActiveRecord::IrreversibleMigration
end
end
RUBY
end

it "doesn't fail fast and has formatted output" do
utils.prepare_phantom_migrations
assert_equal %i[irreversible first second], TestingState.up
assert_empty ActualDbSchema.failed
utils.run_migrations
assert_equal(%w[20130906111510_irreversible.rb], ActualDbSchema.failed.map { |m| File.basename(m.filename) })
assert_match(/1 phantom migration\(s\) could not be rolled back automatically/, TestingState.output)
end
end
end

describe "db:rollback_branches:manual" do
Expand Down

0 comments on commit dc1f54b

Please sign in to comment.