Merge pull request #6135 from opf/fix/remove_duplicate_direct_relations

remove potential duplicates before unique index
pull/6138/head
Markus Kahl 7 years ago committed by GitHub
commit 4e903bf5c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      db/migrate/20180105130053_rebuild_dag.rb

@ -28,15 +28,16 @@
class RebuildDag < ActiveRecord::Migration[5.0]
def up
add_column :relations, :count, :integer, default: 0, null: false
set_count_to_1
truncate_closure_entries
remove_duplicate_relations
if index_exists?(:relations, relation_types)
remove_index :relations, relation_types
end
truncate_closure_entries
add_column :relations, :count, :integer, default: 0, null: false
set_count_to_1
add_index :relations,
%i(from_id to_id hierarchy relates duplicates blocks follows includes requires),
@ -139,6 +140,24 @@ class RebuildDag < ActiveRecord::Migration[5.0]
SQL
end
def remove_duplicate_relations
equal_conditions = relation_types.map do |type|
"r1.#{type} = r2.#{type}"
end.join(' AND ')
ActiveRecord::Base.connection.execute <<-SQL
DELETE
FROM relations
WHERE id IN (SELECT id FROM (SELECT r1.id
FROM relations r1
JOIN relations r2
ON r1.id > r2.id
AND r1.from_id = r2.from_id
AND r1.to_id = r2.to_id
AND #{equal_conditions}) delete_ids)
SQL
end
def relation_types
%i(hierarchy relates duplicates blocks follows includes requires)
end

Loading…
Cancel
Save