Sha256: 1527f6059ae8e87f379c647f10eb6b207f01fc680bd4dc0576879f615061aef5

Contents?: true

Size: 1.5 KB

Versions: 163

Compression:

Stored size: 1.5 KB

Contents

class AddContentConstraints < ActiveRecord::Migration[5.2]
  def up
    Katello::ProductContent.where(:content_id => nil).delete_all
    Katello::ProductContent.where(:product_id => nil).delete_all

    change_column :katello_product_contents, :content_id, :integer, :null => false
    change_column :katello_product_contents, :product_id, :integer, :null => false

    Katello::ProductContent.reset_column_information

    duplicates = Katello::Content.having('count(*) > 1').group(:cp_content_id, :organization_id).select(:cp_content_id, :organization_id)

    duplicates.each do |dup|
      contents = Katello::Content.where(organization_id: dup.organization_id, cp_content_id: dup.cp_content_id).to_a
      first = contents.pop
      content_to_delete = []
      contents.each do |content|
        content.product_contents.each do |pc|
          if !first.products.include?(pc.product)
            pc.content = first
            pc.save!
          else
            pc.delete
          end
        end
        content_to_delete << content.id
      end
      ::Katello::Content.where(id: content_to_delete).delete_all
    end

    add_index :katello_contents, [:cp_content_id, :organization_id], :unique => true, :name => :katello_contents_cpcid_orgid_uniq
  end

  def down
    change_column :katello_product_contents, :content_id, :integer, :null => true
    change_column :katello_product_contents, :product_id, :integer, :null => true
    remove_index :katello_contents, :name => :katello_contents_cpcid_orgid_uniq
  end
end

Version data entries

163 entries across 163 versions & 1 rubygems

Version Path
katello-4.14.2 db/migrate/20190205142619_add_content_constraints.rb
katello-4.15.0 db/migrate/20190205142619_add_content_constraints.rb
katello-4.15.0.rc2 db/migrate/20190205142619_add_content_constraints.rb
katello-4.15.0.rc1 db/migrate/20190205142619_add_content_constraints.rb
katello-4.14.1 db/migrate/20190205142619_add_content_constraints.rb
katello-4.14.0 db/migrate/20190205142619_add_content_constraints.rb
katello-4.14.0.rc3 db/migrate/20190205142619_add_content_constraints.rb
katello-4.14.0.rc2 db/migrate/20190205142619_add_content_constraints.rb
katello-4.14.0.rc1.1 db/migrate/20190205142619_add_content_constraints.rb
katello-4.14.0.rc1 db/migrate/20190205142619_add_content_constraints.rb
katello-4.13.1 db/migrate/20190205142619_add_content_constraints.rb
katello-4.13.0 db/migrate/20190205142619_add_content_constraints.rb
katello-4.12.1 db/migrate/20190205142619_add_content_constraints.rb
katello-4.13.0.rc1 db/migrate/20190205142619_add_content_constraints.rb
katello-4.12.0 db/migrate/20190205142619_add_content_constraints.rb
katello-4.12.0.rc3 db/migrate/20190205142619_add_content_constraints.rb
katello-4.12.0.rc2 db/migrate/20190205142619_add_content_constraints.rb
katello-4.12.0.rc1 db/migrate/20190205142619_add_content_constraints.rb
katello-4.11.1 db/migrate/20190205142619_add_content_constraints.rb
katello-4.11.0 db/migrate/20190205142619_add_content_constraints.rb