-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Phantom migrations status rake task (#35)
* phantom migrations rake task * refactor * refactor * fix loading commands * fix tests * add tests
- Loading branch information
Showing
8 changed files
with
161 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActualDbSchema | ||
module Commands | ||
# Base class for all commands | ||
class Base | ||
def call | ||
unless ActualDbSchema.config.fetch(:enabled, true) | ||
raise "ActualDbSchema is disabled. Set ActualDbSchema.config[:enabled] = true to enable it." | ||
end | ||
|
||
if ActiveRecord::Migration.current_version >= 6 | ||
ActiveRecord::Tasks::DatabaseTasks.raise_for_multi_db(command: "db:rollback_branches") | ||
end | ||
|
||
call_impl | ||
end | ||
|
||
private | ||
|
||
def call_impl | ||
raise NotImplementedError | ||
end | ||
|
||
def context | ||
@context ||= | ||
ActiveRecord::Base.connection.migration_context.tap do |c| | ||
c.extend(ActualDbSchema::Patches::MigrationContext) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActualDbSchema | ||
module Commands | ||
# Shows the list of phantom migrations | ||
class List < Base | ||
private | ||
|
||
def call_impl | ||
preambule | ||
table | ||
end | ||
|
||
def indexed_phantom_migrations | ||
@indexed_phantom_migrations ||= context.migrations.index_by { |m| m.version.to_s } | ||
end | ||
|
||
def preambule | ||
puts "\nPhantom migrations\n\n" | ||
puts "Below is a list of irrelevant migrations executed in unmerged branches." | ||
puts "To bring your database schema up to date, the migrations marked as \"up\" should be rolled back." | ||
puts "\ndatabase: #{ActiveRecord::Base.connection_db_config.database}\n\n" | ||
puts %(#{"Status".center(8)} #{"Migration ID".ljust(14)} Migration File) | ||
puts "-" * 50 | ||
end | ||
|
||
def table | ||
context.migrations_status.each do |status, version| | ||
migration = indexed_phantom_migrations[version] | ||
next unless migration | ||
|
||
puts %(#{status.center(8)} #{version.to_s.ljust(14)} #{migration.filename.gsub("#{Rails.root}/", "")}) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActualDbSchema | ||
module Commands | ||
# Rolls back all phantom migrations | ||
class Rollback < Base | ||
private | ||
|
||
def call_impl | ||
context.rollback_branches | ||
|
||
return if ActualDbSchema.failed.empty? | ||
|
||
puts "" | ||
puts "[ActualDbSchema] Irreversible migrations were found from other branches. Roll them back or fix manually:" | ||
puts "" | ||
puts ActualDbSchema.failed.map { |migration| "- #{migration.filename}" }.join("\n") | ||
puts "" | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters