Skip to content

Commit

Permalink
Add success message and update error summary
Browse files Browse the repository at this point in the history
  • Loading branch information
m-darbinyan committed Dec 16, 2024
1 parent 5c3b59e commit 18081be
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 26 deletions.
67 changes: 43 additions & 24 deletions lib/actual_db_schema/commands/rollback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Commands
# Rolls back all phantom migrations
class Rollback < Base
include ActualDbSchema::OutputFormatter
include ActionView::Helpers::TextHelper

def initialize(context, manual_mode: false)
@manual_mode = manual_mode || manual_mode_default?
Expand All @@ -14,40 +15,58 @@ def initialize(context, manual_mode: false)
private

def call_impl
context.rollback_branches(manual_mode: @manual_mode)
rolled_back = context.rollback_branches(manual_mode: @manual_mode)

return if ActualDbSchema.failed.empty?
return unless rolled_back

puts_preamble
puts_into
puts ""
puts failed_migrations_list
puts_preamble
ActualDbSchema.failed.empty? ? print_success : print_error
end

def print_success
puts colorize("[ActualDbSchema] All phantom migrations rolled back successfully! 🎉", :green)
end

def print_error
header_message = <<~HEADER
#{ActualDbSchema.failed.count} phantom migration(s) could not be rolled back automatically.
Try these steps to fix and move forward:
1. Ensure the migrations are reversible (define #up and #down methods or use #reversible).
2. If the migration references code or tables from another branch, restore or remove them.
3. Once fixed, run `rails db:migrate` again.
Below are the details of the problematic migrations:
HEADER

print_error_summary("#{header_message}\n#{failed_migrations_list}")
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
<<~MIGRATION
#{colorize("Migration ##{index}:", :yellow)}
File: #{failed.short_filename}
Branch: #{failed.branch}
MIGRATION
end.join("\n")
end

def puts_preamble
puts ""
puts %(\u2757\u2757\u2757 #{colorize("[ActualDbSchema]", :red)})
puts ""
def print_error_summary(content)
width = 100
indent = 4
gem_name = "ActualDbSchema"

puts colorize("╔═ [#{gem_name}] #{"═" * (width - gem_name.length - 5)}╗", :red)
print_wrapped_content(content, width, indent)
puts colorize("╚#{"═" * width}╝", :red)
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)
def print_wrapped_content(content, width, indent)
usable_width = width - indent - 4
wrapped_content = word_wrap(content, line_width: usable_width)
wrapped_content.each_line do |line|
puts "#{" " * indent}#{line.chomp}"
end
end

def manual_mode_default?
Expand Down
2 changes: 1 addition & 1 deletion lib/actual_db_schema/failed_migration.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module ActualDbSchema
FailedMigration = Struct.new(:migration, :exception, keyword_init: true) do
FailedMigration = Struct.new(:migration, :exception, :branch, keyword_init: true) do
def filename
migration.filename
end
Expand Down
11 changes: 10 additions & 1 deletion lib/actual_db_schema/patches/migration_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,19 @@ module MigrationContext
include ActualDbSchema::OutputFormatter

def rollback_branches(manual_mode: false)
rolled_back = false

phantom_migrations.reverse_each do |migration|
next unless status_up?(migration)

rolled_back = true
show_info_for(migration) if manual_mode
migrate(migration) if !manual_mode || user_wants_rollback?
rescue StandardError => e
handle_rollback_error(migration, e)
end

rolled_back
end

def phantom_migrations
Expand Down Expand Up @@ -99,7 +104,11 @@ def handle_rollback_error(migration, exception)
ERROR

puts colorize(error_message, :red)
ActualDbSchema.failed << FailedMigration.new(migration: migration, exception: exception)
ActualDbSchema.failed << FailedMigration.new(
migration: migration,
exception: exception,
branch: branch_for(migration.version.to_s)
)
end
end
end
Expand Down
3 changes: 3 additions & 0 deletions test/rake_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ def down
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)
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)
end
end
end
Expand Down

0 comments on commit 18081be

Please sign in to comment.