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

Pubsub notifications not being sent on a cascade_destroy #1186

Closed
sevenseacat opened this issue May 21, 2024 · 0 comments
Closed

Pubsub notifications not being sent on a cascade_destroy #1186

sevenseacat opened this issue May 21, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@sevenseacat
Copy link
Contributor

I've tried to replicate this in a failing test in Ash and I can't, which is frustrating. I have it as a failing test in my app though, so I'll try to break that down here.

Describe the bug

When adding cascade_destroy to a resource, and the cascaded records have pubsub notifications configured on destroy, the pubsub notifications are not being generated/sent.

This failure only started occurring after this commit - 23d7479

To Reproduce

Short version - see failing test in Tunez mix test test/tunez/music/album_test.exs:70

Long version:

  • An Album resource:
defmodule Tunez.Music.Album do
  use Ash.Resource,
    domain: Tunez.Music,
    data_layer: AshPostgres.DataLayer

  actions do 
    destroy :destroy do
      change cascade_destroy(:notifications, return_notifications?: true)
    end
  end

  relationships do 
    has_many :notifications, Tunez.Accounts.Notification
  end
end
  • A Notification resource
defmodule Tunez.Accounts.Notification do
  use Ash.Resource,
    domain: Tunez.Accounts,
    data_layer: AshPostgres.DataLayer,
    notifiers: [Ash.Notifier.PubSub]

  postgres do
    references do
      reference :album, deferrable: :initially
    end
  end

  pub_sub do
    prefix "notifications"
    module TunezWeb.Endpoint
    publish :destroy, [:user_id, "destroy"]
  end

  relationships do
    belongs_to :user, Tunez.Accounts.User do
      allow_nil? false
    end

    belongs_to :album, Tunez.Music.Album do
      domain Tunez.Music
    end
  end

  actions do
    defaults [:read, :destroy]
  end
end
  • The test:
    test "sends pubsub notifications about the notification deletion" do
      follower = insert(:user)
      album = insert(:album)
      %{id: notification_id} = insert(:notification, %{album: album, user: follower})
      TunezWeb.Endpoint.subscribe("notifications:#{follower.id}:destroy")

      Music.destroy_album!(album, authorize?: false)

      assert_received %Phoenix.Socket.Broadcast{
        payload: %{data: %{id: ^notification_id}, action: %{name: :destroy}}
      }
    end
  end
  1) test destroy sends pubsub notifications about the notification deletion (TunezWeb.Music.AlbumTest)
     test/tunez/music/album_test.exs:62
     Assertion failed, no matching message after 0ms
     The following variables were pinned:
       notification_id = "9085a13b-3042-4445-a513-4a8164dcfa65"
     The process mailbox is empty.
     code: assert_received %Phoenix.Socket.Broadcast{payload: %{data: %{id: ^notification_id}, action: %{name: :destroy}}}
     stacktrace:
       test/tunez/music/album_test.exs:70: (test)

On an interesting note, if I change the test to use Music.destroy_album!(album, authorize?: false, return_notifications?: true) I do get the notification returned, so its being generated, just not sent? This isn't a bulk action - it's only a single record. I'm not sure what's going on and can't really replicate it anywhere else.

Expected behavior

The test should pass, as it is subscribed to a topic that a pubsub broadcast should be being sent on, when the cascaded record is destroyed.

Runtime

  • Elixir version 1.16.2-otp-26
  • Erlang version 26.2.3
  • OS macOS something
  • Ash version main branch, sha 9fc5ddf
  • any related extension versions AshPostgres 2.0.1 (not sure if related?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Development

No branches or pull requests

2 participants