From c471d43bb8d7b888d03c03968544f72984b6c0b2 Mon Sep 17 00:00:00 2001 From: Antoine Augusti Date: Wed, 10 Jan 2024 15:22:18 +0100 Subject: [PATCH] =?UTF-8?q?Ajuste=20cl=C3=A9s=20=C3=A9trang=C3=A8res=20pou?= =?UTF-8?q?r=20resource=5Frelated=20(#3713)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20230404074406_add_resource_related.exs | 4 ++-- ...0240110085755_resource_related_on_delete.exs | 17 +++++++++++++++++ .../transport/test/db/resource_related_test.exs | 6 +++++- 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 apps/transport/priv/repo/migrations/20240110085755_resource_related_on_delete.exs diff --git a/apps/transport/priv/repo/migrations/20230404074406_add_resource_related.exs b/apps/transport/priv/repo/migrations/20230404074406_add_resource_related.exs index 0367a16f12..957977e21e 100644 --- a/apps/transport/priv/repo/migrations/20230404074406_add_resource_related.exs +++ b/apps/transport/priv/repo/migrations/20230404074406_add_resource_related.exs @@ -3,8 +3,8 @@ defmodule DB.Repo.Migrations.AddResourceRelated do def change do create table(:resource_related, primary_key: false) do - add(:resource_src_id, references(:resource), on_delete: :delete_all, null: false) - add(:resource_dst_id, references(:resource), on_delete: :delete_all, null: false) + add(:resource_src_id, references(:resource, on_delete: :nothing), null: false) + add(:resource_dst_id, references(:resource, on_delete: :nothing), null: false) add(:reason, :string, null: false) end diff --git a/apps/transport/priv/repo/migrations/20240110085755_resource_related_on_delete.exs b/apps/transport/priv/repo/migrations/20240110085755_resource_related_on_delete.exs new file mode 100644 index 0000000000..4894933822 --- /dev/null +++ b/apps/transport/priv/repo/migrations/20240110085755_resource_related_on_delete.exs @@ -0,0 +1,17 @@ +defmodule DB.Repo.Migrations.ResourceRelatedOnDelete do + use Ecto.Migration + + def change do + # Review foreign key settings previously set in + # 20230404074406_add_resource_related.exs + alter table(:resource_related) do + modify(:resource_src_id, references(:resource, on_delete: :delete_all), + from: references(:resource, on_delete: :nothing) + ) + + modify(:resource_dst_id, references(:resource, on_delete: :delete_all), + from: references(:resource, on_delete: :nothing) + ) + end + end +end diff --git a/apps/transport/test/db/resource_related_test.exs b/apps/transport/test/db/resource_related_test.exs index de0ac5da14..ae32e0625e 100644 --- a/apps/transport/test/db/resource_related_test.exs +++ b/apps/transport/test/db/resource_related_test.exs @@ -8,7 +8,7 @@ defmodule DB.ResourceRelatedTest do Ecto.Adapters.SQL.Sandbox.checkout(DB.Repo) end - test "can create and load resources_related" do + test "can create, load and delete resources_related" do %DB.Resource{id: r1_id} = r1 = insert(:resource) %DB.Resource{id: r2_id} = insert(:resource) insert(:resource_related, resource_src_id: r1_id, resource_dst_id: r2_id, reason: :gtfs_rt_gtfs) @@ -21,5 +21,9 @@ defmodule DB.ResourceRelatedTest do resource_dst: %DB.Resource{id: ^r2_id} } ] = DB.Repo.preload(r1, resources_related: [:resource_dst]).resources_related + + # When deleting a resource, the `resource_related` row linking to it is deleted as well + DB.Repo.delete!(r1) + assert [] == DB.Repo.all(DB.ResourceRelated) end end