commanded / eventstore

Event store using PostgreSQL for persistence
MIT License
1.06k stars 146 forks source link

[question] Deleting event streams #157

Closed mwillema closed 5 years ago

mwillema commented 5 years ago

From what I've seen there's no way to delete event streams, is that right?

I guess I can always do it manually be removing the proper records from the underlying database.

I know that conceptually events should never be removed. However I'm migrating a legacy system from which I import events. So as the development is making progress, I find myself re-importing these events from the legacy system over and over.

slashdotdash commented 5 years ago

No, it's not supported yet. I can add it as a feature request. For example, Greg Young's Event Store supports soft and hard deletion of streams.

I guess I can always do it manually be removing the proper records from the underlying database.

There are Postgres rules on the events tables to prevent updates and deletes. You would need to drop those rules before attempting to delete any events.

DROP RULE no_update_stream_events ON stream_events;
DROP RULE no_delete_stream_events ON stream_events;
DROP RULE no_update_events ON events;
DROP RULE no_delete_events ON events;

Be warned that deleting events will break the event store.

My recommendation would be to replace the events with empty data and set the event type as a deleted event struct (e.g. Elixir.MyApp.DeletedEvent).

UPDATE events 
SET 
  event_type = 'Elixir.MyApp.DeletedEvent', 
  data = convert_to('{}', current_setting('server_encoding')), 
  metadata = convert_to('{}', current_setting('server_encoding'))
WHERE event_id = '<event_id>';

You would need to define a deleted event module:


defmodule MyApp.DeletedEvent do
  defstruct [:data]
end
LostKobrakai commented 5 years ago

I'd be interesting in that as well, as deleting streams can be a good approach for handling data, which are required to be deletable by law.

slashdotdash commented 5 years ago

Closing this issue. I've added a new "Event stream deletion" feature proposal in #170.