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

20221030193943_cleanup_duplicate_pods.rb « migrate « db - github.com/diaspora/diaspora.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 18fd5740dfc3e4f1cbb9b7c66cd388feee4c2c13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

class CleanupDuplicatePods < ActiveRecord::Migration[6.1]
  class Pod < ApplicationRecord
  end

  def change
    reversible do |change|
      change.up do
        remove_duplicates
        cleanup_mixed_case_pods

        Pod.where(port: nil).update_all(port: -1) # rubocop:disable Rails/SkipsModelValidations
      end

      change.down do
        Pod.where(port: -1).update_all(port: nil) # rubocop:disable Rails/SkipsModelValidations
      end
    end

    change_column_null :pods, :port, false
  end

  private

  def remove_duplicates
    Pod.where(port: nil).group(:host).having("count(*) > 1").pluck(:host).each do |host|
      cleanup_duplicates(Pod.where(host: host).order(:id).ids)
    end
  end

  def cleanup_mixed_case_pods
    Pod.where("lower(host) != host").pluck(:host, :port).each do |host, port|
      pod_ids = Pod.where("lower(host) = ?", host.downcase).where(port: port).order(:id).ids
      cleanup_duplicates(pod_ids.dup) if pod_ids.size > 1
      Pod.find(pod_ids.first).update(host: host.downcase)
    end
  end

  def cleanup_duplicates(duplicate_ids)
    target_pod_id = duplicate_ids.shift

    duplicate_ids.each do |pod_id|
      Person.where(pod_id: pod_id).update_all(pod_id: target_pod_id) # rubocop:disable Rails/SkipsModelValidations
      Pod.delete(pod_id)
    end
  end
end