Skip to content

Latest commit

 

History

History
49 lines (33 loc) · 849 Bytes

public-events.markdown

File metadata and controls

49 lines (33 loc) · 849 Bytes

Polymorphism

The payload type is a little different.

SELECT * FROM key_count('events', 'payload');

Varies with type

SELECT type,
       jsonb_object_keys(event),
       COUNT(*)
FROM events
GROUP BY type, jsonb_object_keys(event)

Let's do PublicEvent

CREATE TABLE public_events (
  public_event_id SERIAL PRIMARY KEY,
  event_id INTEGER NOT NULL
    REFERENCES events(event_id),
  payload JSONB NOT NULL);

INSERT INTO public_events (event_id, payload)
SELECT event_id, payload FROM events WHERE type = 'PublicEvent';

Note that the foreign key points the other way!

Kind of a trivial example

SELECT * FROM key_count('public_events', 'payload');

Clean up

ALTER TABLE public_events DROP COLUMN payload;
UPDATE events SET payload = NULL WHERE type = 'PublicEvent';