From 633536672ee5936cd72e76a02f9fc20fa5854787 Mon Sep 17 00:00:00 2001 From: Vladislav Sokov Date: Wed, 3 Jul 2024 12:06:12 +0300 Subject: [PATCH] fixup! fixup! fixup! Refactoring --- .../actual_db_schema/migrations_controller.rb | 66 +++++++++++++++++-- .../actual_db_schema/migrations_helper.rb | 54 --------------- .../actual_db_schema/rollback_service.rb | 18 ----- test/dummy_app/db/secondary_schema.rb | 7 +- 4 files changed, 65 insertions(+), 80 deletions(-) delete mode 100644 app/helpers/actual_db_schema/migrations_helper.rb delete mode 100644 app/services/actual_db_schema/rollback_service.rb diff --git a/app/controllers/actual_db_schema/migrations_controller.rb b/app/controllers/actual_db_schema/migrations_controller.rb index c5d3ae6..ac89721 100644 --- a/app/controllers/actual_db_schema/migrations_controller.rb +++ b/app/controllers/actual_db_schema/migrations_controller.rb @@ -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 diff --git a/app/helpers/actual_db_schema/migrations_helper.rb b/app/helpers/actual_db_schema/migrations_helper.rb deleted file mode 100644 index 9f782f4..0000000 --- a/app/helpers/actual_db_schema/migrations_helper.rb +++ /dev/null @@ -1,54 +0,0 @@ -# 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 diff --git a/app/services/actual_db_schema/rollback_service.rb b/app/services/actual_db_schema/rollback_service.rb deleted file mode 100644 index 9d3e697..0000000 --- a/app/services/actual_db_schema/rollback_service.rb +++ /dev/null @@ -1,18 +0,0 @@ -# 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 diff --git a/test/dummy_app/db/secondary_schema.rb b/test/dummy_app/db/secondary_schema.rb index be9176e..98fcf7b 100644 --- a/test/dummy_app/db/secondary_schema.rb +++ b/test/dummy_app/db/secondary_schema.rb @@ -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