-
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.
- Loading branch information
1 parent
bc614fa
commit 994aea7
Showing
13 changed files
with
209 additions
and
152 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
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 |
---|---|---|
@@ -1,97 +1,19 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative "../../helpers/actual_db_schema/migrations_helper" | ||
require_relative "../../services/actual_db_schema/rollback_service" | ||
module ActualDbSchema | ||
# Controller to display the list of phantom migrations for each database connection. | ||
class MigrationsController < ActionController::Base | ||
before_action :load_migrations, only: %i[index] | ||
include MigrationsHelper | ||
|
||
def index; end | ||
|
||
def show | ||
@migration = load_migration(params[:id], params[:database]) | ||
end | ||
def show; end | ||
|
||
def rollback | ||
version = params[:id] | ||
database = params[:database] | ||
|
||
rollback_migration(version, database) | ||
|
||
redirect_to migrations_path, notice: "Migration #{version} has been rolled back." | ||
end | ||
|
||
private | ||
|
||
def load_migrations | ||
@migrations = [] | ||
|
||
ActualDbSchema.for_each_db_connection do | ||
context = prepare_context | ||
indexed_phantom_migrations = context.migrations.index_by { |m| m.version.to_s } | ||
|
||
context.migrations_status.each do |status, version| | ||
migration = indexed_phantom_migrations[version] | ||
next unless migration | ||
|
||
@migrations << build_migration_struct(status, migration) | ||
end | ||
end | ||
end | ||
|
||
def load_migration(version, database) | ||
ActualDbSchema.for_each_db_connection do | ||
next unless ActualDbSchema.db_config[:database] == database | ||
|
||
context = prepare_context | ||
migration = find_migration_in_context(context, version) | ||
return migration if migration | ||
end | ||
nil | ||
end | ||
|
||
def rollback_migration(version, database) | ||
ActualDbSchema.for_each_db_connection do | ||
next unless ActualDbSchema.db_config[:database] == database | ||
|
||
context = prepare_context | ||
if context.migrations.detect { |m| m.version.to_s == version } | ||
context.run(:down, version.to_i) | ||
break | ||
end | ||
end | ||
end | ||
|
||
def find_migration_in_context(context, version) | ||
migration = context.migrations.detect { |m| m.version.to_s == version } | ||
return unless migration | ||
|
||
status = context.migrations_status.detect { |_s, v| v.to_s == version }&.first || "unknown" | ||
build_migration_struct(status, migration) | ||
end | ||
|
||
def prepare_context | ||
context = ActualDbSchema.fetch_migration_context | ||
context.extend(ActualDbSchema::Patches::MigrationContext) | ||
context | ||
end | ||
|
||
def build_migration_struct(status, migration) | ||
MigrationStruct.new( | ||
status: status, | ||
version: migration.version.to_s, | ||
name: migration.name, | ||
branch: branch_for(migration.version), | ||
database: ActualDbSchema.db_config[:database], | ||
filename: migration.filename | ||
) | ||
end | ||
|
||
def branch_for(version) | ||
metadata.fetch(version.to_s, {})[:branch] || "unknown" | ||
end | ||
|
||
def metadata | ||
ActualDbSchema::Store.instance.read | ||
RollbackService.perform(params[:id], params[:database]) | ||
redirect_to migrations_path | ||
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,54 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActualDbSchema | ||
# Helper methods for loading and displaying migrations. | ||
module MigrationsHelper | ||
def load_migrations | ||
migrations = [] | ||
|
||
ActualDbSchema.for_each_db_connection do | ||
context = ActualDbSchema.prepare_context | ||
indexed_migrations = context.migrations.index_by { |m| m.version.to_s } | ||
|
||
context.migrations_status.each do |status, version| | ||
migration = indexed_migrations[version] | ||
migrations << build_migration_struct(status, migration) if migration | ||
end | ||
end | ||
|
||
migrations | ||
end | ||
|
||
def load_migration(version, database) | ||
ActualDbSchema.for_each_db_connection do | ||
next unless ActualDbSchema.db_config[:database] == database | ||
|
||
context = ActualDbSchema.prepare_context | ||
migration = find_migration_in_context(context, version) | ||
return migration if migration | ||
end | ||
nil | ||
end | ||
|
||
private | ||
|
||
def build_migration_struct(status, migration) | ||
MigrationStruct.new( | ||
status: status, | ||
version: migration.version.to_s, | ||
name: migration.name, | ||
branch: ActualDbSchema.branch_for(ActualDbSchema.metadata, migration.version), | ||
database: ActualDbSchema.db_config[:database], | ||
filename: migration.filename | ||
) | ||
end | ||
|
||
def find_migration_in_context(context, version) | ||
migration = context.migrations.detect { |m| m.version.to_s == version } | ||
return unless migration | ||
|
||
status = context.migrations_status.detect { |_s, v| v.to_s == version }&.first || "unknown" | ||
build_migration_struct(status, migration) | ||
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
module ActualDbSchema | ||
# Service class to handle the rollback of database migrations. | ||
class RollbackService | ||
def self.perform(version, database) | ||
ActualDbSchema.for_each_db_connection do | ||
next unless ActualDbSchema.db_config[:database] == database | ||
|
||
context = ActualDbSchema.prepare_context | ||
if context.migrations.detect { |m| m.version.to_s == version } | ||
context.run(:down, version.to_i) | ||
break | ||
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
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
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
Oops, something went wrong.