amnesia icon indicating copy to clipboard operation
amnesia copied to clipboard

Message does not get deleted

Open a9a4k opened this issue 9 years ago • 1 comments

For some reason delete does not actually delete a message. Here is a relevant test. You can skip first three lines related to phoenix, it is related to the code writing to amnesia.

test "server receives message, stores it and replies with `received` status", %{socket: socket, user: user, chat: chat} do
    {:ok, _, socket} = subscribe_and_join(socket, "chats:#{chat.id}", %{})
    ref = push socket, "message", %{body: "the body", client_id: 1, created_at: :os.system_time(:seconds)}
    assert_reply ref, :ok, %{client_id: 1, status: "received"}

    [message] = Amnesia.transaction do
      selection = Message.where chat_id == chat.id, select: [id, body, client_id, chat_id, created_at, status, user_id]
      selection
      |> Amnesia.Selection.values
    end
    chat_id = chat.id
    user_id = user.id
    assert [id, "the body", 1, ^chat_id, _, "received", ^user_id] = message

    # Trying to delete here
    status = Amnesia.transaction do
      IO.inspect Message.last
      Message.last |> Message.delete
    end

    IO.inspect status

    # receiving ** (MatchError) no match of right hand side value: [[1, "the body", 1, 293, 1465198234, "received", 1514]]
    [] = Amnesia.transaction do
      selection = Message.where chat_id == chat.id, select: [id, body, client_id, chat_id, created_at, status, user_id]
      selection
      |> Amnesia.Selection.values
    end
  end

a9a4k avatar Jun 06 '16 07:06 a9a4k

Is the Message of the type bag?

I believe if we need to delete items from a bag we need to use mnesia:delete_object

Try doing the following:

status = Amnesia.transaction do
    IO.inspect Message.last
    Message.last |> :mnesia.delete_object
end

jmerriweather avatar Aug 03 '16 06:08 jmerriweather