diff --git a/elixir/apps/domain/lib/domain/resources.ex b/elixir/apps/domain/lib/domain/resources.ex index ac6ad3974..6ece59839 100644 --- a/elixir/apps/domain/lib/domain/resources.ex +++ b/elixir/apps/domain/lib/domain/resources.ex @@ -64,7 +64,7 @@ defmodule Domain.Resources do {preload, _opts} = Keyword.pop(opts, :preload, []) {:ok, resources} = - Resource.Query.all() + Resource.Query.not_deleted() |> Resource.Query.by_account_id(subject.account.id) |> Resource.Query.by_authorized_actor_id(subject.actor.id) |> Resource.Query.with_at_least_one_gateway_group() diff --git a/elixir/apps/domain/priv/repo/migrations/20240205162620_delete_connections_for_deleted_resources.exs b/elixir/apps/domain/priv/repo/migrations/20240205162620_delete_connections_for_deleted_resources.exs new file mode 100644 index 000000000..a88139c52 --- /dev/null +++ b/elixir/apps/domain/priv/repo/migrations/20240205162620_delete_connections_for_deleted_resources.exs @@ -0,0 +1,15 @@ +defmodule Domain.Repo.Migrations.DeleteConnectionsForDeletedResources do + use Ecto.Migration + + def change do + execute(""" + DELETE FROM resource_connections + WHERE resource_id IN (SELECT id FROM resources WHERE deleted_at IS NOT NULL) + """) + + execute(""" + DELETE FROM resource_connections + WHERE gateway_group_id IN (SELECT id FROM gateway_groups WHERE deleted_at IS NOT NULL) + """) + end +end diff --git a/elixir/apps/domain/test/domain/resources_test.exs b/elixir/apps/domain/test/domain/resources_test.exs index b8ce9abb1..3d1fff70a 100644 --- a/elixir/apps/domain/test/domain/resources_test.exs +++ b/elixir/apps/domain/test/domain/resources_test.exs @@ -336,7 +336,14 @@ defmodule Domain.ResourcesTest do actor: actor, subject: subject } do - resource = Fixtures.Resources.create_resource(account: account) + gateway_group = Fixtures.Gateways.create_group(account: account) + + resource = + Fixtures.Resources.create_resource( + account: account, + connections: [%{gateway_group_id: gateway_group.id}] + ) + actor_group = Fixtures.Actors.create_group(account: account) Fixtures.Actors.create_membership(account: account, actor: actor, group: actor_group) @@ -346,7 +353,7 @@ defmodule Domain.ResourcesTest do resource: resource ) - {:ok, _resource} = delete_resource(resource, subject) + resource |> Ecto.Changeset.change(deleted_at: DateTime.utc_now()) |> Repo.update!() assert list_authorized_resources(subject) == {:ok, []} end