Sha256: ef80c197c9076840b2658c2b4b7876998721b8c6e0f4651cdb967c59580a0a2e

Contents?: true

Size: 568 Bytes

Versions: 140

Compression:

Stored size: 568 Bytes

Contents

class MakeJobTemplateNameUnique < ActiveRecord::Migration[4.2]
  def up
    duplicates = JobTemplate.unscoped.group(:name).count.delete_if { |_name, value| value == 1 }
    duplicates.each_key do |duplicated_name|
      templates = JobTemplate.where(:name => duplicated_name)
      templates.each_with_index do |template, index|
        new_name = JobTemplate.exists?(:name => "#{template.name}-#{index}") ? "#{template.name}-#{index}-#{SecureRandom.hex(2)}" : "#{template.name}-#{index}"
        template.update_attribute(:name, new_name)
      end
    end
  end
end

Version data entries

140 entries across 140 versions & 1 rubygems

Version Path
foreman_remote_execution-4.2.3 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-4.2.2 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-4.2.1 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-4.2.0 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-4.1.0 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-3.3.7 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-4.0.0 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-3.3.6 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-3.3.5 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-3.3.4 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-3.3.3 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-3.2.2 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-3.3.2 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-3.3.1 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-3.3.0 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-3.2.1 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-3.2.0 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-3.1.0 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-2.0.10 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-2.0.9 db/migrate/20160125155108_make_job_template_name_unique.rb