Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ajuste clés étrangères pour resource_related #3713

Merged
merged 2 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Comment on lines +6 to +7
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

L'intention était là mais c'était placé au mauvais endroit 😬 C'est attendu dans references et pas dans add/3

add(:reason, :string, null: false)
end

Expand Down
Original file line number Diff line number Diff line change
@@ -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
6 changes: 5 additions & 1 deletion apps/transport/test/db/resource_related_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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