-
Notifications
You must be signed in to change notification settings - Fork 10
/
sqlite-event-store.ddl
89 lines (79 loc) · 2.44 KB
/
sqlite-event-store.ddl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
-- SQLITE event store
PRAGMA foreign_keys = ON;
DROP TABLE IF EXISTS events;
DROP TABLE IF EXISTS entity_events;
CREATE TABLE entity_events
(
entity TEXT NOT NULL,
event TEXT NOT NULL,
PRIMARY KEY (entity, event) ON CONFLICT IGNORE
);
CREATE TABLE events
(
entity TEXT NOT NULL,
entityKey TEXT NOT NULL,
event TEXT NOT NULL,
data TEXT NOT NULL,
eventId TEXT NOT NULL UNIQUE CHECK (eventId LIKE '________-____-____-____-____________'),
commandId TEXT NOT NULL UNIQUE CHECK (commandId LIKE '________-____-____-____-____________'),
-- previous event uuid; null for first event; null does not trigger UNIQUE constraint
previousId TEXT UNIQUE,
timestamp TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
-- ordering sequence
sequence INTEGER PRIMARY KEY, -- sequence for all events in all entities
FOREIGN KEY (entity, event) REFERENCES entity_events (entity, event)
);
CREATE INDEX entity_index ON events (entityKey, entity);
-- immutable entity_events
CREATE TRIGGER no_delete_entity_events
BEFORE DELETE
ON entity_events
BEGIN
SELECT RAISE(FAIL, 'Cannot delete entity_events');
END;
CREATE TRIGGER no_update_entity_events
BEFORE UPDATE
ON entity_events
BEGIN
SELECT RAISE(FAIL, 'Cannot update entity_events');
END;
-- immutable events
CREATE TRIGGER no_delete_events
BEFORE DELETE
ON events
BEGIN
SELECT RAISE(FAIL, 'Cannot delete events');
END;
CREATE TRIGGER no_update_events
BEFORE UPDATE
ON events
BEGIN
SELECT RAISE(FAIL, 'Cannot update events');
END;
-- Can only use null previousId for first event in an entity
CREATE TRIGGER first_event_for_entity
BEFORE INSERT
ON events
FOR EACH ROW
WHEN NEW.previousId IS NULL
AND EXISTS (SELECT 1
FROM events
WHERE NEW.entityKey = entityKey
AND NEW.entity = entity)
BEGIN
SELECT RAISE(FAIL, 'previousId can only be null for first entity event');
END;
-- previousId must be in the same entity as the event
CREATE TRIGGER previousId_in_same_entity
BEFORE INSERT
ON events
FOR EACH ROW
WHEN NEW.previousId IS NOT NULL
AND NOT EXISTS (SELECT 1
FROM events
WHERE NEW.previousId = eventId
AND NEW.entityKey = entityKey
AND NEW.entity = entity)
BEGIN
SELECT RAISE(FAIL, 'previousId must be in same entity');
END;