Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add options to run `only` or `skip` some steps in converter and importer (#35127)

Gerhard Schlager58558849acf0
diff --git a/migrations/bin/cli b/migrations/bin/cli
index b610b3940b4..b8722740949 100755
--- a/migrations/bin/cli
+++ b/migrations/bin/cli
@@ -15,14 +15,32 @@ module Migrations
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"
+ option :only,
+ type: :string,
+ desc: "Run only specified steps (comma-separated)",
+ banner: "step1,step2"
+ option :skip,
+ type: :string,
+ desc: "Skip specified steps (comma-separated)",
+ banner: "step1,step2"
def convert(converter_type)
- ::Migrations::CLI::ConvertCommand.new(converter_type, options).execute
+ modified_options = modify_step_options
+ ::Migrations::CLI::ConvertCommand.new(converter_type, modified_options).execute
end
desc "import", "Import a file"
option :reset, type: :boolean, desc: "Reset MappingsDB before importing data"
+ option :only,
+ type: :string,
+ desc: "Run only specified steps (comma-separated)",
+ banner: "step1,step2"
+ option :skip,
+ type: :string,
+ desc: "Skip specified steps (comma-separated)",
+ banner: "step1,step2"
def import
- ::Migrations::CLI::ImportCommand.new(options).execute
+ modified_options = modify_step_options
+ ::Migrations::CLI::ImportCommand.new(modified_options).execute
end
desc "upload", "Upload media uploads"
@@ -44,6 +62,19 @@ module Migrations
def self.exit_on_failure?
true
end
+
+ private
+
+ def modify_step_options
+ modified_options = options.dup
+ modified_options[:only] = split_step_class_names(options[:only])
+ modified_options[:skip] = split_step_class_names(options[:skip])
+ modified_options
+ end
+
+ def split_step_class_names(class_names)
+ class_names.presence&.split(",")&.map { |name| name.strip.demodulize.underscore } || []
+ end
end
end
end
@@ -55,5 +86,7 @@ else
end
# rubocop:disable Discourse/NoChdir
-Dir.chdir(File.expand_path("../..", __dir__)) { ::Migrations::CLI::Application.start }
+Dir.chdir(File.expand_path("../..", __dir__)) do
+ ::Migrations::CLI::ExceptionHandler.handle_and_exit { ::Migrations::CLI::Application.start }
+end
# rubocop:enable Discourse/NoChdir
diff --git a/migrations/lib/cli/convert_command.rb b/migrations/lib/cli/convert_command.rb
index ab11e7caf42..941f357687e 100644
--- a/migrations/lib/cli/convert_command.rb
+++ b/migrations/lib/cli/convert_command.rb
@@ -14,7 +14,7 @@ module Migrations::CLI
::Migrations::Database.reset!(settings[:intermediate_db][:path]) if @options[:reset]
converter = "migrations/converters/#{@converter_type}/converter".camelize.constantize
- converter.new(settings).run
+ converter.new(settings).run(only_steps: @options[:only], skip_steps: @options[:skip])
end
private
diff --git a/migrations/lib/cli/exception_handler.rb b/migrations/lib/cli/exception_handler.rb
new file mode 100644
index 00000000000..40efe8746b2
--- /dev/null
+++ b/migrations/lib/cli/exception_handler.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+module Migrations
+ module CLI
+ class ExceptionHandler
+ def self.handle_and_exit(&block)
+ block.call
+ rescue ClassFilter::UnknownClassNamesError => e
+ handle_unknown_class_names_error(e)
+ exit(1)
+ rescue => e
+ puts "An error occurred: #{e.message}".red
+ puts e.backtrace.join("\n")
+ exit(1)
+ end
+
+ private
+
+ def self.handle_unknown_class_names_error(error)
+ all_suggestions_found = true
+
+ error.missing_names.each do |missing_name|
+ suggestions =
+ DidYouMean::SpellChecker.new(dictionary: error.available_names).correct(missing_name)
+ puts "Unknown step '#{missing_name}'".red
+ if suggestions.any?
+ puts "Did you mean: #{suggestions.join(", ")}".yellow
+ else
+ all_suggestions_found = false
+ end
+ end
+
+ if !all_suggestions_found
+ puts "Available steps are: ".yellow + error.available_names.join(", ")
+ end
+ end
+ end
+ end
+end
diff --git a/migrations/lib/common/class_filter.rb b/migrations/lib/common/class_filter.rb
new file mode 100644
index 00000000000..10ddcfbb41e
--- /dev/null
+++ b/migrations/lib/common/class_filter.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+module Migrations
+ class ClassFilter
+ class UnknownClassNamesError < StandardError
+ attr_reader :missing_names, :available_names
+
+ def initialize(missing_names, available_names)
+ @missing_names = missing_names.sort
+ @available_names = available_names.sort
+ super("Unknown class names: #{missing_names.join(", ")}")
+ end
+ end
+
+ def self.filter(classes, skip: [], only: [])
+ new(classes, skip:, only:).filter
+ end
+
+ def initialize(classes, skip: [], only: [])
+ @classes = classes
+ @skip = skip
+ @only = only
+ @normalized_class_names = build_normalized_names
+ end
+
+ def filter
+ validate_class_names!
+
+ classes_to_include = @classes.dup
+ classes_to_include.select! { |klass| class_included?(@only, klass) } if @only.any?
+ classes_to_include.reject! { |klass| class_included?(@skip, klass) } if @skip.any?
+
+ classes_with_dependencies = Set.new(classes_to_include)
+ classes_to_include.each { |klass| add_dependencies(klass, classes_with_dependencies) }
+
+ classes_with_dependencies.to_a
+ end
+
+ private
+
+ def build_normalized_names
+ Hash[@classes.map { |klass| [klass, klass.name.demodulize.underscore] }]
+ end
+
+ def validate_class_names!
+ available_names = @normalized_class_names.values
+ all_specified_names = (@skip + @only).uniq
+ missing_names = all_specified_names - available_names
+
+ raise UnknownClassNamesError.new(missing_names, available_names) if missing_names.any?
+ end
+
+ def add_dependencies(klass, included_set)
+ return unless klass.respond_to?(:dependencies) && klass.dependencies.present?
+
+ klass.dependencies.each do |dependency|
+ next if class_included?(@skip, dependency)
+ next if included_set.include?(dependency)
+
+ included_set << dependency
+ add_dependencies(dependency, included_set)
+ end
+ end
+
+ def class_included?(class_names, klass)
+ class_names.include?(@normalized_class_names[klass])
+ end
+ end
+end
diff --git a/migrations/lib/importer/topological_sorter.rb b/migrations/lib/common/topological_sorter.rb
similarity index 73%
rename from migrations/lib/importer/topological_sorter.rb
rename to migrations/lib/common/topological_sorter.rb
index 7d1bab0d79a..62e0565b27a 100644
--- a/migrations/lib/importer/topological_sorter.rb
+++ b/migrations/lib/common/topological_sorter.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-module Migrations::Importer
+module Migrations
class TopologicalSorter
def self.sort(classes)
new(classes).sort
@@ -15,7 +15,7 @@ module Migrations::Importer
in_degree = Hash.new(0)
@dependency_graph.each_value { |edges| edges.each { |edge| in_degree[edge] += 1 } }
- queue = @classes.reject { |cls| in_degree[cls] > 0 }
+ queue = @classes.reject { |klass| in_degree[klass] > 0 }
result = []
while queue.any?
@@ -40,8 +40,11 @@ module Migrations::Importer
@classes
.sort_by(&:to_s)
.each do |klass|
- dependencies = klass.dependencies || []
- dependencies.each { |dependency| graph[dependency] << klass }
+ if klass.respond_to?(:dependencies) && (dependencies = klass.dependencies).present?
+ dependencies
+ .select { |dep| @classes.include?(dep) }
+ .each { |dependency| graph[dependency] << klass }
+ end
graph[klass] ||= []
end
graph
diff --git a/migrations/lib/converters/base/converter.rb b/migrations/lib/converters/base/converter.rb
index 211d4c86204..2a151995e49 100644
--- a/migrations/lib/converters/base/converter.rb
+++ b/migrations/lib/converters/base/converter.rb
@@ -8,7 +8,7 @@ module Migrations::Converters::Base
@settings = settings
end
- def run
+ def run(only_steps: [], skip_steps: [])
if respond_to?(:setup)
puts "Initializing..."
setup
@@ -16,7 +16,7 @@ module Migrations::Converters::Base
create_database
- steps.each do |step_class|
+ filter_steps(steps, only_steps, skip_steps).each do |step_class|
step = create_step(step_class)
before_step_execution(step)
execute_step(step)
@@ -82,5 +82,9 @@ module Migrations::Converters::Base
args = default_args.merge(step_args(step_class))
step_class.new(StepTracker.new, args)
end
+
+ def filter_steps(step_classes, only_steps, skip_steps)
+ ::Migrations::ClassFilter.filter(step_classes, only: only_steps, skip: skip_steps)
+ end
end
end
diff --git a/migrations/lib/importer/executor.rb b/migrations/lib/importer/executor.rb
index 0b134f4cdc8..dfb1d757114 100644
--- a/migrations/lib/importer/executor.rb
+++ b/migrations/lib/importer/executor.rb
@@ -6,6 +6,7 @@ module Migrations::Importer
@intermediate_db = ::Migrations::Database.connect(config[:intermediate_db])
@discourse_db = DiscourseDB.new
@shared_data = SharedData.new(@discourse_db)
+ @options = options
attach_mappings_db(config[:mappings_db], options[:reset])
attach_uploads_db(config[:uploads_db])
@@ -50,7 +51,10 @@ module Migrations::Importer
.constants
.map { |c| steps_module.const_get(c) }
.select { |klass| klass.is_a?(Class) && klass < ::Migrations::Importer::Step }
- TopologicalSorter.sort(classes)
+
+ filtered_classes =
+ ::Migrations::ClassFilter.filter(classes, only: @options[:only], skip: @options[:skip])
+ ::Migrations::TopologicalSorter.sort(filtered_classes)
end
def execute_steps