Repository navigation
feat(longitudinal): persist immutable observation evidence #248
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 19 commits
e36af18
223c402
96d9fd5
219ad3f
2bbe2ab
8b2b04a
4e83c0d
263494e
3891688
cf66f3d
561f999
c0ad5d8
bf8351d
5fb0985
b1ab98b
3b24bce
456cf12
bc1ad6c
65bca82
7d69fc5
27ee781
bf4b3ce
a514b66
052cd95
b4b6505
78892b2
46816e4
73dadc6
a436b5a
dad81fc
1f86a81
46bf001
6483b72
8b0fbe1
fb5e161
9a080b7
b3cb2ea
441cea8
b472cea
377777e
8b34b24
c6b0583
dfb18f4
b9e7b8d
2a2b85d
10394d6
b5b1a36
75e4d0a
ccb0b4b
49a4b23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| -- Durable, immutable normalized longitudinal observation evidence. | ||
|
|
||
| CREATE TABLE IF NOT EXISTS longitudinal_observation ( | ||
| observation_record_ref text PRIMARY KEY, | ||
| tenant_ref text NOT NULL, | ||
| enrollment_ref text NOT NULL, | ||
| source_system_ref text NOT NULL, | ||
| source_observation_ref text NOT NULL, | ||
| construct_ref text NOT NULL, | ||
| measure_ref text NOT NULL, | ||
| validity_start_at_unix_ms bigint NOT NULL, | ||
| validity_end_at_unix_ms bigint NOT NULL, | ||
| recorded_at_unix_ms bigint NOT NULL, | ||
| received_at_unix_ms bigint NOT NULL, | ||
| ingested_at_unix_ms bigint NOT NULL, | ||
| timezone_name text NOT NULL, | ||
| utc_offset_minutes smallint NOT NULL, | ||
| clock_anomaly_code text, | ||
| CONSTRAINT longitudinal_observation_source_identity_unique | ||
| UNIQUE (tenant_ref, enrollment_ref, source_system_ref, source_observation_ref), | ||
| CONSTRAINT longitudinal_observation_reference_check CHECK ( | ||
| observation_record_ref = btrim(observation_record_ref) AND observation_record_ref <> '' AND observation_record_ref !~ '^[0-9]+$' | ||
| AND tenant_ref = btrim(tenant_ref) AND tenant_ref <> '' AND tenant_ref !~ '^[0-9]+$' | ||
| AND enrollment_ref = btrim(enrollment_ref) AND enrollment_ref <> '' AND enrollment_ref !~ '^[0-9]+$' | ||
| AND source_system_ref = btrim(source_system_ref) AND source_system_ref <> '' AND source_system_ref !~ '^[0-9]+$' | ||
| AND source_observation_ref = btrim(source_observation_ref) AND source_observation_ref <> '' AND source_observation_ref !~ '^[0-9]+$' | ||
| AND construct_ref = btrim(construct_ref) AND construct_ref <> '' AND construct_ref !~ '^[0-9]+$' | ||
| AND measure_ref = btrim(measure_ref) AND measure_ref <> '' AND measure_ref !~ '^[0-9]+$' | ||
| ), | ||
| CONSTRAINT longitudinal_observation_time_check CHECK ( | ||
| validity_start_at_unix_ms > 0 AND validity_end_at_unix_ms >= validity_start_at_unix_ms | ||
| AND recorded_at_unix_ms > 0 AND received_at_unix_ms > 0 AND ingested_at_unix_ms >= received_at_unix_ms | ||
| ), | ||
| CONSTRAINT longitudinal_observation_timezone_check CHECK ( | ||
| timezone_name = btrim(timezone_name) AND timezone_name <> '' AND timezone_name !~ '^[0-9]+$' | ||
| AND utc_offset_minutes BETWEEN -720 AND 840 | ||
| ), | ||
| CONSTRAINT longitudinal_observation_anomaly_check CHECK ( | ||
| clock_anomaly_code IS NULL OR clock_anomaly_code = 'recorded_after_received' | ||
| ) | ||
|
Comment on lines
+94
to
+100
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📝 Info: Anomaly-code mapping stays consistent with domain flagging and CHECK constraint The domain sets Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| ); | ||
|
|
||
| CREATE TABLE IF NOT EXISTS longitudinal_membership_share ( | ||
| observation_record_ref text NOT NULL | ||
| REFERENCES longitudinal_observation(observation_record_ref) ON DELETE RESTRICT, | ||
| membership_sequence bigint NOT NULL, | ||
| membership_context_ref text NOT NULL, | ||
| weight_parts_per_10_000 integer NOT NULL, | ||
| PRIMARY KEY (observation_record_ref, membership_sequence), | ||
| CONSTRAINT longitudinal_membership_context_unique | ||
| UNIQUE (observation_record_ref, membership_context_ref), | ||
| CONSTRAINT longitudinal_membership_sequence_check CHECK (membership_sequence > 0), | ||
| CONSTRAINT longitudinal_membership_reference_check CHECK ( | ||
| membership_context_ref = btrim(membership_context_ref) | ||
| AND membership_context_ref <> '' | ||
| AND membership_context_ref !~ '^[0-9]+$' | ||
| ), | ||
| CONSTRAINT longitudinal_membership_weight_check CHECK ( | ||
| weight_parts_per_10_000 BETWEEN 1 AND 10000 | ||
| ) | ||
| ); | ||
|
|
||
| CREATE OR REPLACE FUNCTION reject_longitudinal_mutation() | ||
| RETURNS trigger | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| BEGIN | ||
| RAISE EXCEPTION 'longitudinal observation evidence is immutable' | ||
| USING ERRCODE = '55000'; | ||
| END; | ||
| $$; | ||
|
|
||
| DROP TRIGGER IF EXISTS longitudinal_observation_immutable_update ON longitudinal_observation; | ||
| CREATE TRIGGER longitudinal_observation_immutable_update | ||
| BEFORE UPDATE OR DELETE ON longitudinal_observation | ||
| FOR EACH ROW EXECUTE FUNCTION reject_longitudinal_mutation(); | ||
|
|
||
| DROP TRIGGER IF EXISTS longitudinal_observation_immutable_truncate ON longitudinal_observation; | ||
| CREATE TRIGGER longitudinal_observation_immutable_truncate | ||
| BEFORE TRUNCATE ON longitudinal_observation | ||
| FOR EACH STATEMENT EXECUTE FUNCTION reject_longitudinal_mutation(); | ||
|
|
||
| DROP TRIGGER IF EXISTS longitudinal_membership_immutable_update ON longitudinal_membership_share; | ||
| CREATE TRIGGER longitudinal_membership_immutable_update | ||
| BEFORE UPDATE OR DELETE ON longitudinal_membership_share | ||
| FOR EACH ROW EXECUTE FUNCTION reject_longitudinal_mutation(); | ||
|
|
||
| DROP TRIGGER IF EXISTS longitudinal_membership_immutable_truncate ON longitudinal_membership_share; | ||
| CREATE TRIGGER longitudinal_membership_immutable_truncate | ||
| BEFORE TRUNCATE ON longitudinal_membership_share | ||
| FOR EACH STATEMENT EXECUTE FUNCTION reject_longitudinal_mutation(); | ||
|
|
||
| CREATE OR REPLACE FUNCTION enforce_longitudinal_membership_total() | ||
| RETURNS trigger | ||
| LANGUAGE plpgsql | ||
| AS $$ | ||
| DECLARE | ||
| total_weight bigint; | ||
| BEGIN | ||
| SELECT COALESCE(SUM(weight_parts_per_10_000), 0) | ||
| INTO total_weight | ||
| FROM longitudinal_membership_share | ||
| WHERE observation_record_ref = NEW.observation_record_ref; | ||
| IF total_weight <> 10000 THEN | ||
| RAISE EXCEPTION 'longitudinal membership shares must sum to 10000' | ||
| USING ERRCODE = '23514'; | ||
| END IF; | ||
| RETURN NULL; | ||
| END; | ||
| $$; | ||
|
|
||
| -- A membership-only trigger cannot observe an observation whose membership vector is empty, | ||
| -- because no child INSERT exists to fire it. Defer the same invariant from the parent INSERT so | ||
| -- the complete vector can be inserted in the transaction while a header-only commit still fails. | ||
| DROP TRIGGER IF EXISTS longitudinal_observation_membership_total_check ON longitudinal_observation; | ||
| CREATE CONSTRAINT TRIGGER longitudinal_observation_membership_total_check | ||
| AFTER INSERT ON longitudinal_observation | ||
| DEFERRABLE INITIALLY DEFERRED | ||
| FOR EACH ROW EXECUTE FUNCTION enforce_longitudinal_membership_total(); | ||
|
|
||
| DROP TRIGGER IF EXISTS longitudinal_membership_total_check ON longitudinal_membership_share; | ||
| CREATE CONSTRAINT TRIGGER longitudinal_membership_total_check | ||
| AFTER INSERT ON longitudinal_membership_share | ||
| DEFERRABLE INITIALLY DEFERRED | ||
| FOR EACH ROW EXECUTE FUNCTION enforce_longitudinal_membership_total(); | ||
Uh oh!
There was an error while loading. Please reload this page.