From 68833e11aba0a7dda1063021fa73bfcb736b8b35 Mon Sep 17 00:00:00 2001 From: Alex Sherman Date: Tue, 14 Jul 2020 17:11:42 +0500 Subject: [PATCH] Add uniquie constraints to domain_contacts & nameservers --- ...dd_unique_constraints_to_domain_objects.rb | 36 +++++++++++++++++++ db/structure.sql | 27 +++++++++++--- test/models/contact_test.rb | 2 +- test/models/domain_test.rb | 12 +++---- 4 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 db/migrate/20200714115338_add_unique_constraints_to_domain_objects.rb diff --git a/db/migrate/20200714115338_add_unique_constraints_to_domain_objects.rb b/db/migrate/20200714115338_add_unique_constraints_to_domain_objects.rb new file mode 100644 index 000000000..8c1b25c73 --- /dev/null +++ b/db/migrate/20200714115338_add_unique_constraints_to_domain_objects.rb @@ -0,0 +1,36 @@ +class AddUniqueConstraintsToDomainObjects < ActiveRecord::Migration[6.0] + def up + + execute <<-SQL + alter table domain_contacts + drop constraint if exists uniq_contact_of_type_per_domain; + SQL + + execute <<-SQL + alter table nameservers + drop constraint if exists uniq_hostname_per_domain; + SQL + + execute <<-SQL + alter table domain_contacts + add constraint uniq_contact_of_type_per_domain unique (domain_id, type, contact_id); + SQL + + execute <<-SQL + alter table nameservers + add constraint uniq_hostname_per_domain unique (domain_id, hostname); + SQL + end + + def down + execute <<-SQL + alter table domain_contacts + drop constraint if exists uniq_contact_of_type_per_domain; + SQL + + execute <<-SQL + alter table nameservers + drop constraint if exists uniq_hostname_per_domain; + SQL + end +end diff --git a/db/structure.sql b/db/structure.sql index 960481c44..2455bec5a 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -3195,7 +3195,15 @@ ALTER TABLE ONLY public.blocked_domains -- --- Name: uniq_contact_uuid; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- Name: domain_contacts uniq_contact_of_type_per_domain; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.domain_contacts + ADD CONSTRAINT uniq_contact_of_type_per_domain UNIQUE (domain_id, type, contact_id); + + +-- +-- Name: contacts uniq_contact_uuid; Type: CONSTRAINT; Schema: public; Owner: - -- ALTER TABLE ONLY public.contacts @@ -3203,7 +3211,7 @@ ALTER TABLE ONLY public.contacts -- --- Name: uniq_domain_uuid; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- Name: domains uniq_domain_uuid; Type: CONSTRAINT; Schema: public; Owner: - -- ALTER TABLE ONLY public.domains @@ -3211,7 +3219,15 @@ ALTER TABLE ONLY public.domains -- --- Name: uniq_reserved_domains_name; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- Name: nameservers uniq_hostname_per_domain; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.nameservers + ADD CONSTRAINT uniq_hostname_per_domain UNIQUE (domain_id, hostname); + + +-- +-- Name: reserved_domains uniq_reserved_domains_name; Type: CONSTRAINT; Schema: public; Owner: - -- ALTER TABLE ONLY public.reserved_domains @@ -3219,7 +3235,7 @@ ALTER TABLE ONLY public.reserved_domains -- --- Name: uniq_uuid; Type: CONSTRAINT; Schema: public; Owner: -; Tablespace: +-- Name: auctions uniq_uuid; Type: CONSTRAINT; Schema: public; Owner: - -- ALTER TABLE ONLY public.auctions @@ -4535,6 +4551,7 @@ INSERT INTO "schema_migrations" (version) VALUES ('20200505150413'), ('20200518104105'), ('20200529115011'), -('20200630081231'); +('20200630081231'), +('20200714115338'); diff --git a/test/models/contact_test.rb b/test/models/contact_test.rb index 4e500e98d..8847c8c49 100644 --- a/test/models/contact_test.rb +++ b/test/models/contact_test.rb @@ -177,7 +177,7 @@ class ContactTest < ActiveSupport::TestCase def test_linked_when_in_use_as_domain_contact Domain.update_all(registrant_id: contacts(:william).id) - DomainContact.update_all(contact_id: @contact.id) + DomainContact.first.update(contact_id: @contact.id) assert @contact.linked? end diff --git a/test/models/domain_test.rb b/test/models/domain_test.rb index 83e12118d..726def1b1 100644 --- a/test/models/domain_test.rb +++ b/test/models/domain_test.rb @@ -134,9 +134,9 @@ class DomainTest < ActiveSupport::TestCase contact = contacts(:john) domain.admin_contacts << contact - domain.admin_contacts << contact - - assert domain.invalid? + assert_raise ActiveRecord::RecordNotUnique do + domain.admin_contacts << contact + end end def test_invalid_when_the_same_tech_contact_is_linked_twice @@ -144,9 +144,9 @@ class DomainTest < ActiveSupport::TestCase contact = contacts(:john) domain.tech_contacts << contact - domain.tech_contacts << contact - - assert domain.invalid? + assert_raise ActiveRecord::RecordNotUnique do + domain.tech_contacts << contact + end end def test_validates_name_server_count_when_name_servers_are_required