FIX: migrations-tooling CLI didn't work anymore (#29777)

The previous approach of splitting Thor commands into multiple files caused problems when the same method name was used in multiple commands.

This also loads the Rails environment only for commands that need it. That makes the CLI boot faster for most commands or when the help should be shown. That's also why we can't use `Rails.root` in the CLI.
This commit is contained in:
Gerhard Schlager
2024-11-19 23:51:53 +01:00
committed by GitHub
parent a8ca82b11f
commit 75f4a14568
5 changed files with 114 additions and 96 deletions

View File

@ -1,52 +1,48 @@
# frozen_string_literal: true
module Migrations::CLI::ConvertCommand
def self.included(thor)
thor.class_eval do
desc "convert [FROM]", "Convert a file"
option :settings, type: :string, desc: "Path of settings file", banner: "path"
option :reset, type: :boolean, desc: "Reset database before converting data"
def convert(converter_type)
converter_type = converter_type.downcase
validate_converter_type!(converter_type)
module Migrations::CLI
class ConvertCommand
def initialize(converter_type, options)
@converter_type = converter_type.downcase
@options = options
end
settings = load_settings(converter_type)
def execute
validate_converter_type!
settings = load_settings
::Migrations::Database.reset!(settings[:intermediate_db][:path]) if options[:reset]
::Migrations::Database.reset!(settings[:intermediate_db][:path]) if @options[:reset]
converter = "migrations/converters/#{converter_type}/converter".camelize.constantize
converter.new(settings).run
end
converter = "migrations/converters/#{@converter_type}/converter".camelize.constantize
converter.new(settings).run
end
private
private
def validate_converter_type!(type)
converter_names = ::Migrations::Converters.names
def validate_converter_type!
converter_names = ::Migrations::Converters.names
raise Thor::Error, <<~MSG if !converter_names.include?(type)
Unknown converter name: #{type}
Valid names are: #{converter_names.join(", ")}
MSG
end
raise Thor::Error, <<~MSG if !converter_names.include?(@converter_type)
Unknown converter name: #{@converter_type}
Valid names are: #{converter_names.join(", ")}
MSG
end
def validate_settings_path!(settings_path)
if !File.exist?(settings_path)
raise Thor::Error, "Settings file not found: #{settings_path}"
end
end
def validate_settings_path!(settings_path)
raise Thor::Error, "Settings file not found: #{settings_path}" if !File.exist?(settings_path)
end
def load_settings(converter_type)
settings_path = calculate_settings_path(converter_type)
validate_settings_path!(settings_path)
def load_settings
settings_path = calculate_settings_path
validate_settings_path!(settings_path)
YAML.safe_load(File.read(settings_path), symbolize_names: true)
end
YAML.safe_load(File.read(settings_path), symbolize_names: true)
end
def calculate_settings_path(converter_type)
settings_path =
options[:settings] || ::Migrations::Converters.default_settings_path(converter_type)
File.expand_path(settings_path, Dir.pwd)
end
def calculate_settings_path
settings_path =
@options[:settings] || ::Migrations::Converters.default_settings_path(@converter_type)
File.expand_path(settings_path, Dir.pwd)
end
end
end