Sha256: 5b9c835ca300cede8ff53779de751b55e4240914769cf03a5f21cfcc30fcc876

Contents?: true

Size: 1.97 KB

Versions: 6

Compression:

Stored size: 1.97 KB

Contents

class MoveKatelloFieldsFromHostgroups < ActiveRecord::Migration[6.0]
  def up
    if User.unscoped.where(login: User::ANONYMOUS_ADMIN).exists?
      User.as_anonymous_admin do
        copy_data_from_hostgroup
      end
    end

    change_table :hostgroups do |table|
      table.remove(
        :content_source_id,
        :kickstart_repository_id,
        :content_view_id,
        :lifecycle_environment_id
      )
    end
  end

  def down
    change_table :hostgroups do |t|
      t.column :kickstart_repository_id, :integer, :null => true
      t.column :content_source_id, :integer, :null => true
      t.column :content_view_id, :integer, :null => true
      t.column :lifecycle_environment_id, :integer, :null => true
    end
  end

  def copy_data_from_hostgroup
    hg_table = ::Hostgroup.arel_table
    hostgroups = ::Hostgroup.unscoped.where(
           hg_table[:content_source_id].eq(nil)
      .and(hg_table[:kickstart_repository_id].eq(nil))
      .and(hg_table[:content_view_id].eq(nil))
      .and(hg_table[:lifecycle_environment_id].eq(nil))
      .not)
    hostgroups.in_batches do |batch|
      batch.pluck(
        :id,
        :content_source_id,
        :kickstart_repository_id,
        :content_view_id,
        :lifecycle_environment_id
      ).each do |hostgroup_id, content_source_id, kickstart_repository_id, content_view_id, lifecycle_environment_id|
        content_facet = ::Katello::Hostgroup::ContentFacet.find_or_create_by(hostgroup_id: hostgroup_id)
        content_facet.content_source_id = content_source_id
        content_facet.kickstart_repository_id = kickstart_repository_id
        content_facet.content_view_id = content_view_id
        content_facet.lifecycle_environment_id = lifecycle_environment_id
        unless content_facet.save(validate: false)
          Rails.logger.warn("Unable to save content facet hostgroup for #{content_facet.inspect} ")
          Rails.logger.warn(content_facet.errors.full_messages.join("\n"))
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
katello-4.0.3 db/migrate/20200514092553_move_katello_fields_from_hostgroups.katello.rb
katello-3.18.5 db/migrate/20200514092553_move_katello_fields_from_hostgroups.katello.rb
katello-4.0.2.1 db/migrate/20200514092553_move_katello_fields_from_hostgroups.katello.rb
katello-4.0.2 db/migrate/20200514092553_move_katello_fields_from_hostgroups.katello.rb
katello-3.18.4 db/migrate/20200514092553_move_katello_fields_from_hostgroups.katello.rb
katello-3.18.3.1 db/migrate/20200514092553_move_katello_fields_from_hostgroups.katello.rb