Skip to content

Commit

Permalink
Allow to use duplicated entity when creating notice
Browse files Browse the repository at this point in the history
We have noticed that there are some duplicate entities resulting from
importing data from various sources. While we do plan to merge them
eventually, we still need to allow their use in creating new notices for
the time being. While this situation is less than ideal, we can put the
constraint back once we no longer have duplicate entities.
Alternatively, we could consolidate all existing entities into the
notices table and store them as jsonb.
  • Loading branch information
peter-hank committed Mar 10, 2023
1 parent 605ed9d commit 8c2e518
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 5 deletions.
6 changes: 4 additions & 2 deletions app/models/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ class Entity < ApplicationRecord
# == Validations ==========================================================
validates :address_line_1, length: { maximum: 255 }
validates_inclusion_of :kind, in: KINDS
validates_uniqueness_of :name,
scope: ADDITIONAL_DEDUPLICATION_FIELDS
validates :name,
uniqueness: { scope: ADDITIONAL_DEDUPLICATION_FIELDS },
on: %i[create update],
if: -> { changed? || !persisted? }

# == Callbacks ============================================================
# Force search reindex on related notices
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveUniqueNameConstraintFromEntities < ActiveRecord::Migration[6.1]
def change
remove_index :entities, ['name', 'address_line_1', 'city', 'state', 'zip', 'country_code', 'phone', 'email']
end
end
3 changes: 1 addition & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_01_02_190032) do
ActiveRecord::Schema.define(version: 2023_03_10_092957) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -267,7 +267,6 @@
t.index ["city"], name: "index_entities_on_city"
t.index ["country_code"], name: "index_entities_on_country_code"
t.index ["email"], name: "index_entities_on_email"
t.index ["name", "address_line_1", "city", "state", "zip", "country_code", "phone", "email"], name: "unique_entity_attribute_index", unique: true
t.index ["name"], name: "index_entities_on_name"
t.index ["phone"], name: "index_entities_on_phone"
t.index ["state"], name: "index_entities_on_state"
Expand Down
2 changes: 1 addition & 1 deletion spec/models/dmca_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,6 @@ def mock_assessment(notice, high_risk)

expect(notice).not_to be_on_behalf_of_principal
end
end
end
end
end
48 changes: 48 additions & 0 deletions spec/models/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,47 @@
end
end

context 'validating scoped name attribute' do
params = {
title: 'A title',
type: 'DMCA',
date_sent: '2013-05-22',
date_received: '2013-05-23',
works_attributes: [
{
description: 'The Lion King on YouTube',
infringing_urls_attributes: [
{
url: 'https://example.com'
}
]
}
],
entity_notice_roles_attributes: [
{
name: 'recipient',
entity_attributes: {
name: 'Google'
}
},
{
name: 'sender',
entity_attributes: {
name: 'The Sender'
}
}
]
}

it 'doesn\'t validate a name of an existing entity when a duplicated entity is used but not updated' do
create_duplicated_entity

n = Notice.new(params)

expect(n.valid?).to eq true
end
end

it_behaves_like 'an object with hierarchical relationships'

private
Expand All @@ -126,4 +167,11 @@ def test_redaction(content)
expect(entity.name).not_to include content
expect(entity.name_original).to include content
end

def create_duplicated_entity
2.times do
e = Entity.new(name: 'The Sender')
e.save(validate: false)
end
end
end

0 comments on commit 8c2e518

Please sign in to comment.