Sha256: 39bc06fc19a3454e730a2b09ea2ea1650abfbc19dfea4cb02016190c29656990

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

class MigrateHostTypeInHostConfigGroups < ActiveRecord::Migration[6.0]
  def up
    host_config_group_ids = HostConfigGroup.where(host_type: %w[Host::Base Host::Managed]).pluck(:host_id).uniq
    host_config_group_ids.each do |host_id|
      host_facet_id = ForemanPuppet::HostPuppetFacet.where(host_id: host_id).pick(:id)
      HostConfigGroup.where(host_type: %w[Host::Base Host::Managed], host_id: host_id).update_all(host_type: 'ForemanPuppet::HostPuppetFacet', host_id: host_facet_id)
    end
    hostgroup_config_group_ids = HostConfigGroup.where(host_type: 'Hostgroup').pluck(:host_id).uniq
    hostgroup_config_group_ids.each do |hostgroup_id|
      hostgroup_facet_id = ForemanPuppet::HostgroupPuppetFacet.where(hostgroup_id: hostgroup_id).pick(:id)
      HostConfigGroup.where(host_type: 'Hostgroup', host_id: hostgroup_id).update_all(host_type: 'ForemanPuppet::HostgroupPuppetFacet', host_id: hostgroup_facet_id)
    end
  end

  def down
    host_config_group_ids = HostConfigGroup.where(host_type: 'ForemanPuppet::HostPuppetFacet').pluck(:host_id).uniq
    host_facet_ids = Hash[ForemanPuppet::HostPuppetFacet.where(id: host_config_group_ids).pluck(:id, :host_id)]
    host_config_group_ids.each do |host_puppet_facet_id|
      HostConfigGroup.where(host_type: 'ForemanPuppet::HostPuppetFacet', host_id: host_puppet_facet_id)
                     .update_all(host_type: 'Host::Managed', host_id: host_facet_ids[host_puppet_facet_id])
    end
    hostgroup_config_group_ids = HostConfigGroup.where(host_type: 'ForemanPuppet::HostgroupPuppetFacet').pluck(:host_id).uniq
    hostgroup_facet_ids = Hash[ForemanPuppet::HostgroupPuppetFacet.where(id: hostgroup_config_group_ids).pluck(:id, :hostgroup_id)]
    hostgroup_config_group_ids.each do |hostgroup_facet_id|
      HostConfigGroup.where(host_type: 'ForemanPuppet::HostgroupPuppetFacet', host_id: hostgroup_facet_id)
                     .update_all(host_type: 'Hostgroup', host_id: hostgroup_facet_ids[hostgroup_facet_id])
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
foreman_puppet-0.0.2 db/migrate/20200803113903_migrate_host_type_in_host_config_groups.foreman_puppet.rb
foreman_puppet-0.0.1 db/migrate/20200803113903_migrate_host_type_in_host_config_groups.foreman_puppet.rb