Remove duplicates on migration

pull/11383/head
Oliver Günther 2 years ago
parent 1f821cb8f6
commit 0ff1aecec0
No known key found for this signature in database
GPG Key ID: 88872239EB414F99
  1. 12
      db/migrate/20220929114423_add_unique_index_to_ldap_groups_membership.rb

@ -1,5 +1,17 @@
class AddUniqueIndexToLdapGroupsMembership < ActiveRecord::Migration[7.0]
def change
reversible do |dir|
dir.up { remove_duplicate_memberships! }
end
add_index :ldap_groups_memberships, %i[user_id group_id], unique: true
end
def remove_duplicate_memberships!
ActiveRecord::Base.connection.execute <<~SQL.squish
DELETE FROM ldap_groups_memberships m1
USING ldap_groups_memberships m2
WHERE m1.id < m2.id AND m1.user_id = m2.user_id AND m1.group_id = m2.group_id;
SQL
end
end

Loading…
Cancel
Save