discourse/migrations/lib/settings_parser.rb
Selase Krakani 6c91148db8
DEV: Refactor uploads_importer script (#29292)
* DEV: Implement uploads command entrypoint

- Setup Thor UploadsCommand for CLI
- First pass at modularizing various parts of the exising `uploads_import` script

* DEV: First attempt at modularizing missing uploads fixer task

Move missing upload fix to a dedicated uploads task implementation unit

* DEV: First attempt at modularizing missing uploads uploader task

Move uploader to a dedicated uploads task implementation unit

* DEV: First attempt at modularizing missing uploads optimizer task

Move optimizer to a dedicated uploads task implementation unit

* DEV: Various follow up fixes to get optimization working

- Start threads early
- Improve "log" message formatting
- Add missing `copy_to_tempfile` method on "uploader" task

* DEV: Refactor a bit more

Deduplicate and move most of threading premitives to base task as-is

* DEV: Remove redundant condition in uploads db migration

* DEV: More deduplication

Move task retry logic to base class and tidy up other implementation
details carried over from the existing script
2024-10-31 13:31:12 +00:00

82 lines
1.8 KiB
Ruby

# frozen_string_literal: true
module Migrations
class SettingsParser
class InvalidYaml < StandardError
end
class ValidationError < StandardError
end
REQUIRED_KEYS = %i[source_db_path output_db_path root_paths]
def initialize(options)
@options = options
validate!
end
def [](key)
@options[key]
end
def []=(key, value)
@options[key] = value
end
def fetch(key, default)
@options.fetch(key, default)
end
def self.parse!(path)
new(YAML.load_file(path, symbolize_names: true))
rescue Psych::SyntaxError => e
raise InvalidYaml.new(e.message)
end
private
def validate!
validate_required_keys
validate_paths
validate_options
end
def validate_required_keys
missing = REQUIRED_KEYS - @options.keys
raise ValidationError, "Missing required keys: #{missing.join(", ")}" if missing.any?
end
def validate_paths
%i[source_db_path output_db_path].each do |key|
path = @options[key]
next unless path
dir = File.dirname(path)
raise ValidationError, "Directory not writable: #{dir}" unless File.writable?(dir)
end
if !@options[:root_paths].is_a?(Array)
raise ValidationError, "Root paths must be an array of paths"
end
@options[:root_paths].each do |path|
raise ValidationError, "Directory not readable: #{path}" unless File.readable?(path)
end
end
def validate_options
validate_thread_count_factor if @options[:thread_count_factor]
end
def validate_thread_count_factor
count = @options[:thread_count_factor]
unless count.is_a?(Numeric) && count.positive?
raise ValidationError, "Thread count factor must be numeric and positive"
end
end
end
end