Skip to content

Commit

Permalink
fixup! fixup! fixup! Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
VladislavSokov committed Jul 3, 2024
1 parent 994aea7 commit 6335366
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 80 deletions.
66 changes: 61 additions & 5 deletions app/controllers/actual_db_schema/migrations_controller.rb
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
54 changes: 0 additions & 54 deletions app/helpers/actual_db_schema/migrations_helper.rb

This file was deleted.

18 changes: 0 additions & 18 deletions app/services/actual_db_schema/rollback_service.rb

This file was deleted.

7 changes: 4 additions & 3 deletions test/dummy_app/db/secondary_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# This file is the source Rails uses to define your schema when running `rails
# db:schema:load`. When creating a new database, `rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2013_09_06_111515) do
ActiveRecord::Schema.define(version: 2013_09_06_111513) do

end

0 comments on commit 6335366

Please sign in to comment.