Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more detailed information to the rollback output #103

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 43 additions & 11 deletions lib/actual_db_schema/commands/rollback.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,48 @@ def initialize(context, manual_mode: false)
private

def call_impl
context.rollback_branches(manual_mode: @manual_mode)

return if ActualDbSchema.failed.empty?
phantom_migrations = context.rollback_branches(manual_mode: @manual_mode)
successfully_rolled_back = filter_successful_migrations(phantom_migrations)
return if phantom_migrations.empty?

puts_preamble
puts_into
puts_intro_info
puts_successful_rollback_info(successfully_rolled_back) unless successfully_rolled_back.empty?
puts_unsuccessful_rollback_info unless ActualDbSchema.failed.empty?
puts_preamble
end

def filter_successful_migrations(phantom_migrations)
phantom_migrations.reject { |migration| ActualDbSchema.failed.map(&:migration).include?(migration) }
end

def puts_successful_rollback_info(list)
msg = "#{list.count} phantom #{"migration".pluralize(list.count)} were successfully rolled back."

puts colorize(msg, :green)
puts ""
puts successful_rollback_list(list)
puts ""
end

def successful_rollback_list(list)
list.map.with_index(1) do |migration, index|
filename = migration.filename.sub(File.join(Rails.root, "/"), "")
<<~MSG
\t#{colorize("[Migration##{index}]", :green)}
\t- #{filename}
MSG
end
end

def puts_unsuccessful_rollback_info
failed_rollback_count = ActualDbSchema.failed.count
msg = "#{failed_rollback_count} phantom #{"migration".pluralize(failed_rollback_count)} could not " \
"be rolled back automatically. Roll them back or fix manually:"
puts colorize(msg, :red)
puts ""
puts failed_migrations_list
puts_preamble
puts ""
end

def failed_migrations_list
Expand All @@ -42,18 +75,17 @@ def failed_migrations_list
end
end

def puts_intro_info
msg = "Phantom migrations were detected and actual_db_schema attempted to automatically roll them back.\n"
puts colorize(msg, :yellow)
end

def puts_preamble
puts ""
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
Expand Down
2 changes: 1 addition & 1 deletion test/rake_task_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def down
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)
assert_match(/1 phantom migration could not be rolled back automatically/, TestingState.output)
end
end
end
Expand Down