class YleTf::Config::Migration

Constants

BACKEND_MIGRATIONS

Attributes

config[R]
config_source[R]

Public Class Methods

migrate_old_config(config, **opts) click to toggle source
# File lib/yle_tf/config/migration.rb, line 29
def self.migrate_old_config(config, **opts)
  new(config, **opts).migrated_config
end
new(config, **opts) click to toggle source
# File lib/yle_tf/config/migration.rb, line 35
def initialize(config, **opts)
  @config = config
  @config_source = opts.fetch(:source)
end

Public Instance Methods

copy_with_defaults(config, defaults) click to toggle source
# File lib/yle_tf/config/migration.rb, line 104
def copy_with_defaults(config, defaults)
  deep_merge(deep_copy(config), defaults)
end
deprecation_warning() click to toggle source

Returns a `Proc` to print deprecation warnings unless denied by an env var

# File lib/yle_tf/config/migration.rb, line 49
def deprecation_warning
  return nil if ENV['TF_OLD_CONFIG_WARNINGS'] == 'false'

  lambda do |new_config|
    Logger.warn("Old configuration found in #{config_source}")
    Logger.warn("Please migrate to relevant parts of:\n" \
                "#{sanitize_config(new_config)}")
    Logger.warn(
      'See https://github.com/Yleisradio/yle_tf/wiki/Migrating-Configuration for more details'
    )
  end
end
find_old_backend_config_keys(keys) click to toggle source
# File lib/yle_tf/config/migration.rb, line 93
def find_old_backend_config_keys(keys)
  return {} if !old_backend_config.is_a?(Hash)

  keys.select do |old_key, _new_key|
    old_backend_config.key?(old_key) &&
      # Special case for 'file' as it is now used for option Hash for the
      # 'file' backend
      !(old_key == 'file' && old_backend_config['file'].is_a?(Hash))
  end
end
migrate_old_backend_config() { |new_config| ... } click to toggle source

TODO: Remove support in v2.0

# File lib/yle_tf/config/migration.rb, line 63
def migrate_old_backend_config
  changed = false

  new_config = BACKEND_MIGRATIONS.inject(config) do |prev_config, (type, keys)|
    migrate_old_backend_config_keys(prev_config, type, keys) { changed = true }
  end

  yield(new_config) if changed && block_given?

  new_config
end
migrate_old_backend_config_keys(config, type, keys) { |new_config| ... } click to toggle source
# File lib/yle_tf/config/migration.rb, line 75
def migrate_old_backend_config_keys(config, type, keys)
  migrated_keys = find_old_backend_config_keys(keys)
  return config if migrated_keys.empty?

  defaults = {
    'backend' => {
      type => {}
    }
  }
  copy_with_defaults(config, defaults).tap do |new_config|
    migrated_keys.each do |old_key, new_key|
      new_config['backend'][type][new_key] = old_backend_config[old_key]
    end

    yield new_config
  end
end
migrated_config() click to toggle source
# File lib/yle_tf/config/migration.rb, line 44
def migrated_config
  migrate_old_backend_config(&deprecation_warning)
end
old_backend_config() click to toggle source
# File lib/yle_tf/config/migration.rb, line 40
def old_backend_config
  config['backend']
end
sanitize_config(config) click to toggle source
# File lib/yle_tf/config/migration.rb, line 108
def sanitize_config(config)
  backend_config = config['backend'].select do |key, value|
    key == 'type' || value.is_a?(Hash)
  end

  YAML.dump('backend' => backend_config)
end