diaspora/db/migrate/20150828132451_remove_duplicate_and_empty_pods.rb
Florian Staudacher 738413c65f fix some issues with pod-checking
add tooltips in the frontend
fix a JS problem with empty hostname
use `find_in_batches` correctly
add a migration to clean up the pods table + unique index on hostname
2015-08-30 18:50:34 +02:00

24 lines
568 B
Ruby

class RemoveDuplicateAndEmptyPods < ActiveRecord::Migration
def up
remove_dupes
remove_empty_or_nil
add_index :pods, :host, unique: true, length: 190 # =190*4 for utf8mb4
end
def down
remove_index :pods, :host
end
private
def remove_dupes
duplicates = Pod.group(:host).count.select {|_, v| v > 1 }.keys
ids = duplicates.flat_map {|pod| Pod.where(host: pod).order(created_at: :asc).pluck(:id).tap(&:shift) }
Pod.where(id: ids).destroy_all
end
def remove_empty_or_nil
Pod.where(host: [nil, ""]).destroy_all
end
end