From b2e849cf9facb0da13dc1a3d086c155a48f2b703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tao=20Bror=20Bojl=C3=A9n?= Date: Fri, 9 Aug 2019 20:30:22 +0300 Subject: [PATCH] add ON DELETE to relations --- .../20190809165902_add_on_deletes.exs | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 backend/priv/repo/migrations/20190809165902_add_on_deletes.exs diff --git a/backend/priv/repo/migrations/20190809165902_add_on_deletes.exs b/backend/priv/repo/migrations/20190809165902_add_on_deletes.exs new file mode 100644 index 0000000..861c317 --- /dev/null +++ b/backend/priv/repo/migrations/20190809165902_add_on_deletes.exs @@ -0,0 +1,40 @@ +defmodule Backend.Repo.Migrations.AddOnDeletes do + use Ecto.Migration + + def change do + # Add ON DELETE CASCADE to foreign key relations. + + # crawls -> instances + execute("ALTER TABLE crawls DROP CONSTRAINT crawls_instance_domain_fkey") + + execute( + "ALTER TABLE crawls ADD CONSTRAINT crawls_instance_domain_fkey FOREIGN KEY (instance_domain) REFERENCES instances(domain) ON DELETE CASCADE" + ) + + # instance_peers -> instances + execute("ALTER TABLE instance_peers DROP CONSTRAINT instance_peers_source_domain_fkey") + + execute( + "ALTER TABLE instance_peers ADD CONSTRAINT instance_peers_source_domain_fkey FOREIGN KEY (source_domain) REFERENCES instances(domain) ON DELETE CASCADE" + ) + + execute("ALTER TABLE instance_peers DROP CONSTRAINT instance_peers_target_domain_fkey") + + execute( + "ALTER TABLE instance_peers ADD CONSTRAINT instance_peers_target_domain_fkey FOREIGN KEY (target_domain) REFERENCES instances(domain) ON DELETE CASCADE" + ) + + # edges -> instances + execute("ALTER TABLE edges DROP CONSTRAINT edges_source_domain_fkey") + + execute( + "ALTER TABLE edges ADD CONSTRAINT edges_source_domain_fkey FOREIGN KEY (source_domain) REFERENCES instances(domain) ON DELETE CASCADE" + ) + + execute("ALTER TABLE edges DROP CONSTRAINT edges_target_domain_fkey") + + execute( + "ALTER TABLE edges ADD CONSTRAINT edges_target_domain_fkey FOREIGN KEY (target_domain) REFERENCES instances(domain) ON DELETE CASCADE" + ) + end +end