Sha256: 54b461037e6e5a8d11b28f0063bfcca443cffd68022c62df6e07bf9697ee8fa3

Contents?: true

Size: 955 Bytes

Versions: 65

Compression:

Stored size: 955 Bytes

Contents

class AddContentTitleUniqueConstraint < ActiveRecord::Migration[4.2]
  def change
    titles = ForemanOpenscap::ScapContent.unscoped.group(:title).count.select { |key, value| value > 1 }.keys
    titles.each do |title|
      duplicates = ForemanOpenscap::ScapContent.unscoped.where :title => title
      say "#{duplicates.count} Scap Contents with duplicate title detected: #{title}"
      duplicates.each.with_index do |item, index|
        next if index == 0
        new_title = item.title + " #{index + 1}"
        say "Renaming Scap Content #{item.title} with id #{item.id} to #{new_title}"
        item.update_attribute(:title, new_title)
      end
    end

    remove_index :foreman_openscap_scap_contents, :name => 'index_scaptimony_scap_contents_on_title' if index_exists?(:foreman_openscap_scap_contents, :title, :name => 'index_scaptimony_scap_contents_on_title')
    add_index :foreman_openscap_scap_contents, :title, :unique => true
  end
end

Version data entries

65 entries across 65 versions & 1 rubygems

Version Path
foreman_openscap-10.0.3 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-10.0.2 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-10.0.1 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-10.0.0 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-9.0.4 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-9.0.3 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-9.0.2 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-9.0.1 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-9.0.0 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-8.0.0 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-7.1.1 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-7.1.0 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-7.0.0 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-6.0.0 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-5.2.3 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-5.2.2 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-5.1.1 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-5.2.1 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-5.2.0 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-5.1.0 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb