-
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
994aea7
commit 6335366
Showing
4 changed files
with
65 additions
and
80 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,75 @@ | ||
# 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 | ||
include MigrationsHelper | ||
|
||
def index; end | ||
|
||
def show; end | ||
|
||
def rollback | ||
RollbackService.perform(params[:id], params[:database]) | ||
rollback_migration(params[:id], params[:database]) | ||
redirect_to migrations_path | ||
end | ||
|
||
private | ||
|
||
helper_method 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 | ||
|
||
helper_method 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 | ||
|
||
def rollback_migration(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 | ||
|
||
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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