-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinit.sql
39 lines (35 loc) · 1.7 KB
/
init.sql
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
-- a single table is used for all events in the cqrs system
CREATE TABLE events
(
aggregate_type text NOT NULL,
aggregate_id text NOT NULL,
sequence bigint CHECK (sequence >= 0) NOT NULL,
event_type text NOT NULL,
event_version text NOT NULL,
payload json NOT NULL,
metadata json NOT NULL,
PRIMARY KEY (aggregate_type, aggregate_id, sequence)
);
-- this table is only needed if snapshotting is employed
CREATE TABLE snapshots
(
aggregate_type text NOT NULL,
aggregate_id text NOT NULL,
last_sequence bigint CHECK (last_sequence >= 0) NOT NULL,
current_snapshot bigint CHECK (current_snapshot >= 0) NOT NULL,
payload json NOT NULL,
PRIMARY KEY (aggregate_type, aggregate_id, last_sequence)
);
-- one view table should be created for every `PostgresViewRepository` used
-- replace name with the value used in `PostgresViewRepository::new(view_name: String)`
CREATE TABLE test_view
(
view_id text NOT NULL,
version bigint CHECK (version >= 0) NOT NULL,
payload json NOT NULL,
PRIMARY KEY (view_id)
);
INSERT INTO public.events (aggregate_type, aggregate_id, sequence, event_type, event_version, payload, metadata)
VALUES ('Customer', 'previous_event_in_need_of_upcast', 1, 'NameAdded', '1.0', '{"NameAdded": {}}', '{}');
CREATE USER test_user WITH ENCRYPTED PASSWORD 'test_pass';
GRANT ALL PRIVILEGES ON DATABASE postgres TO test_user;