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-1.0.10 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-2.0.1 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-1.0.9 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-2.0.0 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-1.0.8 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-1.0.7 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-1.0.6 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-1.0.5 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-1.0.4 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-1.0.3 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-1.0.2 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-1.0.1 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-1.0.0 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-0.12.3 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-0.11.5 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-0.12.2 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-0.12.1 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-0.12.0 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-0.11.4 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb
foreman_openscap-0.11.3 db/migrate/20171016125613_add_content_title_unique_constraint.foreman_openscap.rb