Sha256: e737ca854ea4f84500525feb43cd6b9352d4ef423ba576878909264a11774a9d

Contents?: true

Size: 1.04 KB

Versions: 11

Compression:

Stored size: 1.04 KB

Contents

class CastLookupKeyValues < ActiveRecord::Migration[5.1]
  def up
    # Different LookupKey types handle casting a bit differently
    PuppetclassLookupKey.unscoped.preload(:lookup_values).where(override: true).where.not(key_type: 'string').find_each do |key|
      cast_key_and_values(key)
    end
  end

  private

  def cast_key_and_values(key)
    fix_value(key, :default_value)
    key.lookup_values.each do |lv|
      fix_value(lv, :value)
    end
  end

  def safemode_box
    @safemode_box ||= Safemode::Box.new
  end

  def fix_value(obj, attribute)
    return if obj.omit && !obj.try(:merge_default)
    value = obj.send(attribute)
    return unless value.is_a? String
    return if value.contains_erb?
    fixed = safemode_box.eval(value)
    obj.update_column(attribute, fixed)
  rescue StandardError => e
    say "Failed to cast #{attribute} for #{obj.inspect}:"
    say "Value: #{value}", subitem: true
    say "Error: #{e.message}", subitem: true
    say 'Perhaps it is invalid? Casting skipped, manual action may be needed.', subitem: true
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
foreman_puppet-1.0.5 db/migrate_foreman/20180816134832_cast_lookup_key_values.rb
foreman_puppet-1.0.4 db/migrate_foreman/20180816134832_cast_lookup_key_values.rb
foreman_puppet-1.0.3 db/migrate_foreman/20180816134832_cast_lookup_key_values.rb
foreman_puppet-1.0.2 db/migrate_foreman/20180816134832_cast_lookup_key_values.rb
foreman_puppet-1.0.1 db/migrate_foreman/20180816134832_cast_lookup_key_values.rb
foreman_puppet-1.0.0 db/migrate_foreman/20180816134832_cast_lookup_key_values.rb
foreman_puppet-1.0.0.rc.2 db/migrate_foreman/20180816134832_cast_lookup_key_values.rb
foreman_puppet-1.0.0.rc.1 db/migrate_foreman/20180816134832_cast_lookup_key_values.rb
foreman_puppet-0.1.0 db/migrate_foreman/20180816134832_cast_lookup_key_values.rb
foreman_puppet-0.0.2 db/migrate_foreman/20180816134832_cast_lookup_key_values.rb
foreman_puppet-0.0.1 db/migrate_foreman/20180816134832_cast_lookup_key_values.rb