Welcome to mirror list, hosted at ThFree Co, Russian Federation.

20170813160104_cleanup_aspects_and_add_unique_index.rb « migrate « db - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 7741d5b0840a3604996810b3c831a32aa35130ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# frozen_string_literal: true

class CleanupAspectsAndAddUniqueIndex < ActiveRecord::Migration[5.1]
  class Aspect < ApplicationRecord
  end

  def up
    cleanup_aspects
    add_index :aspects, %i[user_id name], name: :index_aspects_on_user_id_and_name, length: {name: 190}, unique: true
  end

  def down
    remove_index :aspects, name: :index_aspects_on_user_id_and_name
  end

  def cleanup_aspects
    Aspect.where(user_id: 0).delete_all
    Aspect.joins("INNER JOIN aspects as a2 ON aspects.user_id = a2.user_id AND aspects.name = a2.name")
          .where("aspects.id > a2.id").each do |aspect|
      aspect.update(name: "#{aspect.name}_#{UUID.generate(:compact)}")
    end
  end
end