Sha256: 5fc749aa4554b2eed024f1a65221d3f451a99e951023a983a6e382bc3dea629b

Contents?: true

Size: 563 Bytes

Versions: 18

Compression:

Stored size: 563 Bytes

Contents

class MakeJobTemplateNameUnique < ActiveRecord::Migration
  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

18 entries across 18 versions & 1 rubygems

Version Path
foreman_remote_execution-1.3.7 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.3.6 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.4.1 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.3.5 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.3.4 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.3.3 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.3.2 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.3.1 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.3.0 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.2.2 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.2.1 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.2.0 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.1.1 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.1.0 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-1.0.0 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-0.3.2 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-0.3.1 db/migrate/20160125155108_make_job_template_name_unique.rb
foreman_remote_execution-0.3.0 db/migrate/20160125155108_make_job_template_name_unique.rb