@nodii/dsar 0.20.0 → 0.23.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (61) hide show
  1. package/dist/bind-pattern-a.d.ts +127 -13
  2. package/dist/bind-pattern-a.d.ts.map +1 -1
  3. package/dist/bind-pattern-a.js +553 -32
  4. package/dist/bind-pattern-a.js.map +1 -1
  5. package/dist/bind-pattern-b.d.ts.map +1 -1
  6. package/dist/bind-pattern-b.js +58 -3
  7. package/dist/bind-pattern-b.js.map +1 -1
  8. package/dist/grpc/dsar-service.d.ts +29 -0
  9. package/dist/grpc/dsar-service.d.ts.map +1 -1
  10. package/dist/grpc/dsar-service.js +34 -0
  11. package/dist/grpc/dsar-service.js.map +1 -1
  12. package/dist/grpc/proto-path.d.ts +33 -0
  13. package/dist/grpc/proto-path.d.ts.map +1 -0
  14. package/dist/grpc/proto-path.js +87 -0
  15. package/dist/grpc/proto-path.js.map +1 -0
  16. package/dist/hold-failure-mode.d.ts +24 -0
  17. package/dist/hold-failure-mode.d.ts.map +1 -1
  18. package/dist/hold-failure-mode.js +24 -0
  19. package/dist/hold-failure-mode.js.map +1 -1
  20. package/dist/hold-gate-probe.d.ts +61 -0
  21. package/dist/hold-gate-probe.d.ts.map +1 -0
  22. package/dist/hold-gate-probe.js +146 -0
  23. package/dist/hold-gate-probe.js.map +1 -0
  24. package/dist/holds.d.ts +60 -1
  25. package/dist/holds.d.ts.map +1 -1
  26. package/dist/holds.js +60 -3
  27. package/dist/holds.js.map +1 -1
  28. package/dist/index.d.ts +16 -6
  29. package/dist/index.d.ts.map +1 -1
  30. package/dist/index.js +28 -5
  31. package/dist/index.js.map +1 -1
  32. package/dist/migrations/index.d.ts.map +1 -1
  33. package/dist/migrations/index.js +10 -0
  34. package/dist/migrations/index.js.map +1 -1
  35. package/dist/orchestrator.d.ts +48 -0
  36. package/dist/orchestrator.d.ts.map +1 -1
  37. package/dist/orchestrator.js +297 -20
  38. package/dist/orchestrator.js.map +1 -1
  39. package/dist/register.d.ts +35 -1
  40. package/dist/register.d.ts.map +1 -1
  41. package/dist/register.js +26 -0
  42. package/dist/register.js.map +1 -1
  43. package/dist/retention-sweeper.d.ts +252 -0
  44. package/dist/retention-sweeper.d.ts.map +1 -0
  45. package/dist/retention-sweeper.js +356 -0
  46. package/dist/retention-sweeper.js.map +1 -0
  47. package/dist/retention.d.ts +109 -1
  48. package/dist/retention.d.ts.map +1 -1
  49. package/dist/retention.js +216 -3
  50. package/dist/retention.js.map +1 -1
  51. package/dist/subject-kinds.d.ts +74 -0
  52. package/dist/subject-kinds.d.ts.map +1 -0
  53. package/dist/subject-kinds.js +141 -0
  54. package/dist/subject-kinds.js.map +1 -0
  55. package/dist/types.d.ts +423 -7
  56. package/dist/types.d.ts.map +1 -1
  57. package/dist/types.js +164 -0
  58. package/dist/types.js.map +1 -1
  59. package/package.json +7 -4
  60. package/proto/nodii/dsar/v1/dsar.proto +202 -0
  61. package/src/migrations/001-lifecycle-requests.sql +422 -0
@@ -0,0 +1,422 @@
1
+ -- @nodii/dsar v0.1.x — lifecycle_requests schema per spec § 5.3 / § 5.7.
2
+ --
3
+ -- Per comm doctrine § 7.5: UNIQUE (request_id, rpc_method) + 7-day retention.
4
+ -- Mirrors saga's migration shape; one row per (request_id, rpc_method) tuple.
5
+ --
6
+ -- Services consume this via `applyDsarMigrations(sql)` at boot or via their
7
+ -- standard migration runner (Flyway / golang-migrate / alembic) by copying
8
+ -- this file verbatim.
9
+
10
+ CREATE TABLE IF NOT EXISTS lifecycle_requests (
11
+ id UUID PRIMARY KEY,
12
+ request_id TEXT NOT NULL,
13
+ rpc_method TEXT NOT NULL,
14
+ tenant_id UUID NOT NULL,
15
+ subject_kind TEXT NOT NULL,
16
+ subject_id TEXT NOT NULL,
17
+ status TEXT NOT NULL,
18
+ request_payload JSONB NOT NULL,
19
+ response_cached JSONB,
20
+ error_detail TEXT,
21
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
22
+ completed_at TIMESTAMPTZ,
23
+ cleanup_after TIMESTAMPTZ NOT NULL DEFAULT (now() + interval '7 days'),
24
+ CONSTRAINT lifecycle_requests_request_id_rpc_uniq UNIQUE (request_id, rpc_method)
25
+ );
26
+
27
+ -- Idempotent upgrade-safety for SIBLING tables created by an older (0.3.x)
28
+ -- migration: CREATE TABLE IF NOT EXISTS is a no-op on a pre-existing table,
29
+ -- so every non-PK column of the current canonical schema is re-asserted via
30
+ -- ADD COLUMN IF NOT EXISTS (no-op on fresh/current tables; backfills an old
31
+ -- one). NOT NULL columns carry a DEFAULT so the add succeeds on populated
32
+ -- rows. (id is the PK column — never ADD COLUMN it.)
33
+ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS request_id TEXT NOT NULL DEFAULT '';
34
+ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS rpc_method TEXT NOT NULL DEFAULT '';
35
+ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS tenant_id UUID;
36
+ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS subject_kind TEXT NOT NULL DEFAULT '';
37
+ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS subject_id TEXT NOT NULL DEFAULT '';
38
+ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT '';
39
+ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS request_payload JSONB NOT NULL DEFAULT '{}'::jsonb;
40
+ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS response_cached JSONB;
41
+ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS error_detail TEXT;
42
+ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS created_at TIMESTAMPTZ NOT NULL DEFAULT now();
43
+ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS completed_at TIMESTAMPTZ;
44
+ ALTER TABLE lifecycle_requests ADD COLUMN IF NOT EXISTS cleanup_after TIMESTAMPTZ NOT NULL DEFAULT (now() + interval '7 days');
45
+
46
+ -- D412 (08-rls v7 A1): tenant_id TEXT -> UUID, idempotent + guarded. A table
47
+ -- created by a pre-D412 (<=0.8.x) migration has tenant_id TEXT; convert it
48
+ -- in-place (USING tenant_id::uuid succeeds on valid-UUID data, fails loudly
49
+ -- otherwise). The data_type guard makes a fresh/already-UUID table a no-op.
50
+ DO $rls_uuid$
51
+ BEGIN
52
+ IF EXISTS (
53
+ SELECT 1 FROM information_schema.columns
54
+ WHERE table_schema = current_schema()
55
+ AND table_name = 'lifecycle_requests'
56
+ AND column_name = 'tenant_id'
57
+ AND data_type <> 'uuid'
58
+ ) THEN
59
+ EXECUTE 'ALTER TABLE lifecycle_requests
60
+ ALTER COLUMN tenant_id TYPE uuid USING tenant_id::uuid';
61
+ END IF;
62
+ END
63
+ $rls_uuid$;
64
+
65
+ CREATE INDEX IF NOT EXISTS idx_lifecycle_requests_tenant_at
66
+ ON lifecycle_requests (tenant_id, created_at);
67
+ CREATE INDEX IF NOT EXISTS idx_lifecycle_requests_cleanup
68
+ ON lifecycle_requests (cleanup_after);
69
+
70
+ -- Orchestrator-side aggregator per LOCKED 11-dsar § 6.2 (2026-06-03).
71
+ --
72
+ -- One aggregator row per request_id (the sole PK — request_id is a UUIDv7
73
+ -- per § 2 / § 3). This is intentional asymmetry vs the § 6.1 SIBLING table
74
+ -- (lifecycle_requests above), which keeps composite (request_id, rpc_method);
75
+ -- rpc_method here is a plain column, NOT part of the PK.
76
+ --
77
+ -- per_sibling_state is an OBJECT map keyed by lowercase sibling_id →
78
+ -- { status, reason, completed_at }. status / sibling status values are
79
+ -- lowercase (in_flight | completed | partial | failed | held).
80
+ CREATE TABLE IF NOT EXISTS platform_lifecycle_requests (
81
+ request_id UUID PRIMARY KEY,
82
+ rpc_method TEXT NOT NULL,
83
+ tenant_id UUID NOT NULL,
84
+ subject_kind TEXT NOT NULL,
85
+ subject_id UUID NOT NULL,
86
+ requested_by TEXT NOT NULL,
87
+ reason TEXT,
88
+ status TEXT NOT NULL, -- 'in_flight' | 'completed' | 'partial' | 'failed' | 'held'
89
+ per_sibling_state JSONB NOT NULL, -- { '<sibling_id>': { status, reason, completed_at } } (LOWERCASE keys)
90
+ started_at TIMESTAMPTZ NOT NULL DEFAULT now(),
91
+ completed_at TIMESTAMPTZ
92
+ );
93
+
94
+ -- Idempotent upgrade-safety for the orchestrator-aggregator table created by
95
+ -- an older (0.3.x) migration. SIBLINGS create-but-never-WRITE this table, so
96
+ -- the additive ADD COLUMN IF NOT EXISTS below fully unblocks them: the
97
+ -- subsequent CREATE INDEX on started_at succeeds even on an old table that
98
+ -- predates the § 6.2 conformance change (b1461a50) which added started_at /
99
+ -- per_sibling_state / reason. (request_id is the PK column — never ADD COLUMN
100
+ -- it.) SCOPE: this is ADDITIVE ONLY — it does NOT attempt the destructive
101
+ -- §6.2 rewrite (TEXT->UUID type changes, id-PK -> request_id-PK conversion);
102
+ -- that re-alignment is the ORCHESTRATOR's (tenant-service) job on its own
103
+ -- populated data per b1461a50.
104
+ ALTER TABLE platform_lifecycle_requests ADD COLUMN IF NOT EXISTS rpc_method TEXT NOT NULL DEFAULT '';
105
+ -- tenant_id/subject_id: nullable UUID (no safe non-null default for a UUID; both
106
+ -- always pre-exist on every real 0.3.x+ table so this is a guaranteed no-op — it
107
+ -- only adds them on a malformed table lacking them, before the (tenant_id,...) index).
108
+ ALTER TABLE platform_lifecycle_requests ADD COLUMN IF NOT EXISTS tenant_id UUID;
109
+ ALTER TABLE platform_lifecycle_requests ADD COLUMN IF NOT EXISTS subject_kind TEXT NOT NULL DEFAULT '';
110
+ ALTER TABLE platform_lifecycle_requests ADD COLUMN IF NOT EXISTS subject_id UUID;
111
+ ALTER TABLE platform_lifecycle_requests ADD COLUMN IF NOT EXISTS requested_by TEXT NOT NULL DEFAULT '';
112
+ ALTER TABLE platform_lifecycle_requests ADD COLUMN IF NOT EXISTS reason TEXT;
113
+ ALTER TABLE platform_lifecycle_requests ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT '';
114
+ ALTER TABLE platform_lifecycle_requests ADD COLUMN IF NOT EXISTS per_sibling_state JSONB NOT NULL DEFAULT '{}'::jsonb;
115
+ ALTER TABLE platform_lifecycle_requests ADD COLUMN IF NOT EXISTS started_at TIMESTAMPTZ NOT NULL DEFAULT now();
116
+ ALTER TABLE platform_lifecycle_requests ADD COLUMN IF NOT EXISTS completed_at TIMESTAMPTZ;
117
+ -- Best-effort, column-existence-guarded backfill of the § 6.2 renames (never
118
+ -- errors if the legacy column is absent — fresh/current tables skip these):
119
+ -- legacy sibling_results -> per_sibling_state (only where still default)
120
+ -- legacy created_at -> started_at
121
+ -- The existence check is resolved against the table on the connection's
122
+ -- search_path via to_regclass() + pg_attribute (NOT an unscoped
123
+ -- information_schema.columns lookup, which would false-positive when a DIFFERENT
124
+ -- schema on the same DB happens to carry the legacy column).
125
+ DO $do$
126
+ DECLARE
127
+ rel oid := to_regclass('platform_lifecycle_requests');
128
+ BEGIN
129
+ IF rel IS NOT NULL AND EXISTS (
130
+ SELECT 1 FROM pg_attribute
131
+ WHERE attrelid = rel AND attname = 'sibling_results' AND NOT attisdropped
132
+ ) THEN
133
+ EXECUTE 'UPDATE platform_lifecycle_requests
134
+ SET per_sibling_state = sibling_results
135
+ WHERE per_sibling_state = ''{}''::jsonb
136
+ AND sibling_results IS NOT NULL';
137
+ END IF;
138
+ IF rel IS NOT NULL AND EXISTS (
139
+ SELECT 1 FROM pg_attribute
140
+ WHERE attrelid = rel AND attname = 'created_at' AND NOT attisdropped
141
+ ) THEN
142
+ EXECUTE 'UPDATE platform_lifecycle_requests
143
+ SET started_at = created_at
144
+ WHERE created_at IS NOT NULL';
145
+ END IF;
146
+ END
147
+ $do$;
148
+
149
+ -- Additive (non-§6.2 but non-breaking): tenant query-perf index. § 6.2 shows
150
+ -- no index; this is an OK additive keep for tenant-scoped status listings.
151
+ CREATE INDEX IF NOT EXISTS idx_platform_lifecycle_requests_tenant_at
152
+ ON platform_lifecycle_requests (tenant_id, started_at);
153
+
154
+ -- retention_policies registry per spec § 5.11.5.
155
+ CREATE TABLE IF NOT EXISTS retention_policies (
156
+ service_id TEXT NOT NULL,
157
+ data_class TEXT NOT NULL,
158
+ subject_kind TEXT NOT NULL,
159
+ rpc_addr TEXT NOT NULL,
160
+ registered_at TIMESTAMPTZ NOT NULL DEFAULT now(),
161
+ soft_deleted_at TIMESTAMPTZ,
162
+ PRIMARY KEY (service_id, data_class, subject_kind)
163
+ );
164
+
165
+ CREATE INDEX IF NOT EXISTS idx_retention_policies_active
166
+ ON retention_policies (data_class, subject_kind)
167
+ WHERE soft_deleted_at IS NULL;
168
+
169
+ -- D720 — retention_schedules: the platform-global, effective-dated statutory
170
+ -- window registry the purpose-spent sweeper consumes. Platform-global (no
171
+ -- <svc>_ prefix, beside retention_policies + data_residency_holds) because a
172
+ -- jurisdiction's statutory period is a property of the LAW, not of whichever
173
+ -- service holds the record. The ROWS are operator/counsel-owned data — this
174
+ -- migration ships the shape and seeds nothing.
175
+ -- Emitted from retentionSchedulesDdl() so the runner and the standalone
176
+ -- createRetentionSchedulesTable() artifact can never drift.
177
+ CREATE TABLE IF NOT EXISTS retention_schedules (
178
+ id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
179
+ jurisdiction TEXT NOT NULL,
180
+ document_class TEXT NOT NULL,
181
+ entity_kind TEXT NOT NULL,
182
+ retention_period TEXT NOT NULL,
183
+ anchor TEXT NOT NULL,
184
+ extended_by_proceeding BOOLEAN NOT NULL DEFAULT false,
185
+ statute_citation TEXT,
186
+ effective_from TIMESTAMPTZ NOT NULL DEFAULT now(),
187
+ effective_to TIMESTAMPTZ,
188
+ created_at TIMESTAMPTZ NOT NULL DEFAULT now()
189
+ );
190
+ ALTER TABLE retention_schedules ADD COLUMN IF NOT EXISTS statute_citation TEXT;
191
+ ALTER TABLE retention_schedules ADD COLUMN IF NOT EXISTS effective_to TIMESTAMPTZ;
192
+ DO $retention_schedules_anchor$
193
+ BEGIN
194
+ IF NOT EXISTS (
195
+ SELECT 1 FROM pg_constraint WHERE conname = 'retention_schedules_anchor_valid'
196
+ ) THEN
197
+ ALTER TABLE retention_schedules ADD CONSTRAINT retention_schedules_anchor_valid
198
+ CHECK (anchor IN ('creation', 'last_activity', 'relationship_end', 'filing'));
199
+ END IF;
200
+ END
201
+ $retention_schedules_anchor$;
202
+ DO $retention_schedules_window$
203
+ BEGIN
204
+ IF NOT EXISTS (
205
+ SELECT 1 FROM pg_constraint WHERE conname = 'retention_schedules_window_valid'
206
+ ) THEN
207
+ ALTER TABLE retention_schedules ADD CONSTRAINT retention_schedules_window_valid
208
+ CHECK (effective_to IS NULL OR effective_to > effective_from);
209
+ END IF;
210
+ END
211
+ $retention_schedules_window$;
212
+ CREATE UNIQUE INDEX IF NOT EXISTS uq_retention_schedules_effective
213
+ ON retention_schedules (jurisdiction, document_class, entity_kind, effective_from);
214
+ CREATE INDEX IF NOT EXISTS idx_retention_schedules_lookup
215
+ ON retention_schedules (jurisdiction, document_class, entity_kind)
216
+ WHERE effective_to IS NULL;
217
+
218
+ -- data_residency_holds — orchestrator-side hold table per spec § 5.8 +
219
+ -- locked billing spec 505b8c66 (data_class-granular + statutory hold kinds).
220
+ CREATE TABLE IF NOT EXISTS data_residency_holds (
221
+ id UUID PRIMARY KEY,
222
+ tenant_id UUID NOT NULL,
223
+ subject_id TEXT, -- NULL = tenant-level hold
224
+ hold_kind TEXT NOT NULL, -- legacy: "erase"|"restrict"; statutory: "legal"|"compliance"|"business"|"aml_regulatory"
225
+ data_class TEXT, -- v0.4.0: NULL = all classes; e.g. 'billing_invoice_pii'
226
+ hold_request_id TEXT, -- v0.4.0: dedupe key (invoice_id|attempt_id|orchestrator request_id)
227
+ statute_citation TEXT NOT NULL,
228
+ expires_at TIMESTAMPTZ,
229
+ placed_by TEXT NOT NULL,
230
+ placed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
231
+ lifted_at TIMESTAMPTZ,
232
+ lifted_by TEXT
233
+ );
234
+
235
+ -- Idempotent upgrade for tables created by a pre-v0.4.0 migration.
236
+ ALTER TABLE data_residency_holds ADD COLUMN IF NOT EXISTS data_class TEXT;
237
+ ALTER TABLE data_residency_holds ADD COLUMN IF NOT EXISTS hold_request_id TEXT;
238
+
239
+ -- D412 (08-rls v7 A1): tenant_id TEXT -> UUID, idempotent + guarded (mirrors
240
+ -- lifecycle_requests). Must run BEFORE the (tenant_id, ...) indexes below.
241
+ DO $rls_uuid$
242
+ BEGIN
243
+ IF EXISTS (
244
+ SELECT 1 FROM information_schema.columns
245
+ WHERE table_schema = current_schema()
246
+ AND table_name = 'data_residency_holds'
247
+ AND column_name = 'tenant_id'
248
+ AND data_type <> 'uuid'
249
+ ) THEN
250
+ EXECUTE 'ALTER TABLE data_residency_holds
251
+ ALTER COLUMN tenant_id TYPE uuid USING tenant_id::uuid';
252
+ END IF;
253
+ END
254
+ $rls_uuid$;
255
+
256
+ CREATE INDEX IF NOT EXISTS idx_data_residency_holds_lookup
257
+ ON data_residency_holds (tenant_id, subject_id, hold_kind)
258
+ WHERE lifted_at IS NULL;
259
+
260
+ CREATE INDEX IF NOT EXISTS idx_data_residency_holds_class
261
+ ON data_residency_holds (tenant_id, data_class)
262
+ WHERE lifted_at IS NULL;
263
+
264
+ -- Dedupe key for idempotent auto-AML holds (ON CONFLICT (hold_request_id)).
265
+ CREATE UNIQUE INDEX IF NOT EXISTS uq_data_residency_holds_request
266
+ ON data_residency_holds (hold_request_id)
267
+ WHERE hold_request_id IS NOT NULL;
268
+
269
+ -- hold_kind allow-list + statutory-citation CHECK per billing spec 505b8c66.
270
+ DO $do$
271
+ BEGIN
272
+ IF NOT EXISTS (
273
+ SELECT 1 FROM pg_constraint
274
+ WHERE conname = 'data_residency_holds_hold_kind_valid'
275
+ AND conrelid = 'data_residency_holds'::regclass
276
+ ) THEN
277
+ ALTER TABLE data_residency_holds
278
+ ADD CONSTRAINT data_residency_holds_hold_kind_valid
279
+ CHECK (hold_kind IN ('erase','restrict','legal','compliance','business','aml_regulatory'));
280
+ END IF;
281
+ END
282
+ $do$;
283
+
284
+ DO $do$
285
+ BEGIN
286
+ IF NOT EXISTS (
287
+ SELECT 1 FROM pg_constraint
288
+ WHERE conname = 'data_residency_holds_citation_required_for_statutory'
289
+ AND conrelid = 'data_residency_holds'::regclass
290
+ ) THEN
291
+ ALTER TABLE data_residency_holds
292
+ ADD CONSTRAINT data_residency_holds_citation_required_for_statutory
293
+ CHECK (hold_kind NOT IN ('legal','aml_regulatory') OR statute_citation IS NOT NULL);
294
+ END IF;
295
+ END
296
+ $do$;
297
+
298
+ -- ============================================================================
299
+ -- D412 / 08-rls-doctrine v7 § 1.1 (A1) — canonical 5-policy RLS for every
300
+ -- tenant-scoped table. Generated from a1RlsMigrationBlock() in migrations/
301
+ -- index.ts (a drift-guard test asserts this stays identical). ENABLE +
302
+ -- non-owner GRANTs + the nodii_owner bypass policy are UNCONDITIONAL so a
303
+ -- still-TEXT tenant_id is fail-CLOSED (RLS on, non-owner deny-all); the
304
+ -- per-verb authenticated/services policies are guarded on tenant_id being
305
+ -- uuid. Byte-equal to @nodii/db-rls withRls + generateTableGrants.
306
+ -- D412 / 08-rls v7 A1 — lifecycle_requests: ENABLE + GRANTs + owner-bypass are
307
+ -- UNCONDITIONAL (a still-TEXT tenant_id is fail-CLOSED — RLS on, non-owner
308
+ -- deny-all until the per-verb policies land). The per-verb authenticated +
309
+ -- nodii_services policies compare tenant_id to a uuid, so they are guarded on
310
+ -- the column actually being uuid (deferred while a sibling table is mid
311
+ -- TEXT->UUID rewrite; they land on the next run once it is uuid).
312
+ ALTER TABLE lifecycle_requests ENABLE ROW LEVEL SECURITY;
313
+ GRANT SELECT, INSERT, UPDATE, DELETE ON lifecycle_requests TO nodii_services;
314
+ GRANT SELECT, INSERT, UPDATE, DELETE ON lifecycle_requests TO authenticated;
315
+ DROP POLICY IF EXISTS lifecycle_requests_owner_policy ON lifecycle_requests;
316
+ CREATE POLICY lifecycle_requests_owner_policy ON lifecycle_requests FOR ALL TO nodii_owner USING (true) WITH CHECK (true);
317
+ DO $rls_lifecycle_requests$
318
+ BEGIN
319
+ IF EXISTS (
320
+ SELECT 1 FROM information_schema.columns
321
+ WHERE table_schema = current_schema()
322
+ AND table_name = 'lifecycle_requests'
323
+ AND column_name = 'tenant_id'
324
+ AND data_type = 'uuid'
325
+ ) THEN
326
+ EXECUTE 'DROP POLICY IF EXISTS lifecycle_requests_auth_select_policy ON lifecycle_requests';
327
+ EXECUTE 'CREATE POLICY lifecycle_requests_auth_select_policy ON lifecycle_requests FOR SELECT TO authenticated USING (tenant_id IN (SELECT auth.session_tenant_ids()))';
328
+ EXECUTE 'DROP POLICY IF EXISTS lifecycle_requests_auth_insert_policy ON lifecycle_requests';
329
+ EXECUTE 'CREATE POLICY lifecycle_requests_auth_insert_policy ON lifecycle_requests FOR INSERT TO authenticated WITH CHECK (tenant_id IN (SELECT auth.session_tenant_ids()))';
330
+ EXECUTE 'DROP POLICY IF EXISTS lifecycle_requests_auth_update_policy ON lifecycle_requests';
331
+ EXECUTE 'CREATE POLICY lifecycle_requests_auth_update_policy ON lifecycle_requests FOR UPDATE TO authenticated USING (tenant_id IN (SELECT auth.session_tenant_ids())) WITH CHECK (tenant_id IN (SELECT auth.session_tenant_ids()))';
332
+ EXECUTE 'DROP POLICY IF EXISTS lifecycle_requests_auth_delete_policy ON lifecycle_requests';
333
+ EXECUTE 'CREATE POLICY lifecycle_requests_auth_delete_policy ON lifecycle_requests FOR DELETE TO authenticated USING (tenant_id IN (SELECT auth.session_tenant_ids()))';
334
+ EXECUTE 'DROP POLICY IF EXISTS lifecycle_requests_svc_select_policy ON lifecycle_requests';
335
+ EXECUTE 'CREATE POLICY lifecycle_requests_svc_select_policy ON lifecycle_requests FOR SELECT TO nodii_services USING (tenant_id = auth.get_tenant_id())';
336
+ EXECUTE 'DROP POLICY IF EXISTS lifecycle_requests_svc_insert_policy ON lifecycle_requests';
337
+ EXECUTE 'CREATE POLICY lifecycle_requests_svc_insert_policy ON lifecycle_requests FOR INSERT TO nodii_services WITH CHECK (tenant_id = auth.get_tenant_id())';
338
+ EXECUTE 'DROP POLICY IF EXISTS lifecycle_requests_svc_update_policy ON lifecycle_requests';
339
+ EXECUTE 'CREATE POLICY lifecycle_requests_svc_update_policy ON lifecycle_requests FOR UPDATE TO nodii_services USING (tenant_id = auth.get_tenant_id()) WITH CHECK (tenant_id = auth.get_tenant_id())';
340
+ EXECUTE 'DROP POLICY IF EXISTS lifecycle_requests_svc_delete_policy ON lifecycle_requests';
341
+ EXECUTE 'CREATE POLICY lifecycle_requests_svc_delete_policy ON lifecycle_requests FOR DELETE TO nodii_services USING (tenant_id = auth.get_tenant_id())';
342
+ END IF;
343
+ END
344
+ $rls_lifecycle_requests$;
345
+ -- D412 / 08-rls v7 A1 — platform_lifecycle_requests: ENABLE + GRANTs + owner-bypass are
346
+ -- UNCONDITIONAL (a still-TEXT tenant_id is fail-CLOSED — RLS on, non-owner
347
+ -- deny-all until the per-verb policies land). The per-verb authenticated +
348
+ -- nodii_services policies compare tenant_id to a uuid, so they are guarded on
349
+ -- the column actually being uuid (deferred while a sibling table is mid
350
+ -- TEXT->UUID rewrite; they land on the next run once it is uuid).
351
+ ALTER TABLE platform_lifecycle_requests ENABLE ROW LEVEL SECURITY;
352
+ GRANT SELECT, INSERT, UPDATE, DELETE ON platform_lifecycle_requests TO nodii_services;
353
+ GRANT SELECT, INSERT, UPDATE, DELETE ON platform_lifecycle_requests TO authenticated;
354
+ DROP POLICY IF EXISTS platform_lifecycle_requests_owner_policy ON platform_lifecycle_requests;
355
+ CREATE POLICY platform_lifecycle_requests_owner_policy ON platform_lifecycle_requests FOR ALL TO nodii_owner USING (true) WITH CHECK (true);
356
+ DO $rls_platform_lifecycle_requests$
357
+ BEGIN
358
+ IF EXISTS (
359
+ SELECT 1 FROM information_schema.columns
360
+ WHERE table_schema = current_schema()
361
+ AND table_name = 'platform_lifecycle_requests'
362
+ AND column_name = 'tenant_id'
363
+ AND data_type = 'uuid'
364
+ ) THEN
365
+ EXECUTE 'DROP POLICY IF EXISTS platform_lifecycle_requests_auth_select_policy ON platform_lifecycle_requests';
366
+ EXECUTE 'CREATE POLICY platform_lifecycle_requests_auth_select_policy ON platform_lifecycle_requests FOR SELECT TO authenticated USING (tenant_id IN (SELECT auth.session_tenant_ids()))';
367
+ EXECUTE 'DROP POLICY IF EXISTS platform_lifecycle_requests_auth_insert_policy ON platform_lifecycle_requests';
368
+ EXECUTE 'CREATE POLICY platform_lifecycle_requests_auth_insert_policy ON platform_lifecycle_requests FOR INSERT TO authenticated WITH CHECK (tenant_id IN (SELECT auth.session_tenant_ids()))';
369
+ EXECUTE 'DROP POLICY IF EXISTS platform_lifecycle_requests_auth_update_policy ON platform_lifecycle_requests';
370
+ EXECUTE 'CREATE POLICY platform_lifecycle_requests_auth_update_policy ON platform_lifecycle_requests FOR UPDATE TO authenticated USING (tenant_id IN (SELECT auth.session_tenant_ids())) WITH CHECK (tenant_id IN (SELECT auth.session_tenant_ids()))';
371
+ EXECUTE 'DROP POLICY IF EXISTS platform_lifecycle_requests_auth_delete_policy ON platform_lifecycle_requests';
372
+ EXECUTE 'CREATE POLICY platform_lifecycle_requests_auth_delete_policy ON platform_lifecycle_requests FOR DELETE TO authenticated USING (tenant_id IN (SELECT auth.session_tenant_ids()))';
373
+ EXECUTE 'DROP POLICY IF EXISTS platform_lifecycle_requests_svc_select_policy ON platform_lifecycle_requests';
374
+ EXECUTE 'CREATE POLICY platform_lifecycle_requests_svc_select_policy ON platform_lifecycle_requests FOR SELECT TO nodii_services USING (tenant_id = auth.get_tenant_id())';
375
+ EXECUTE 'DROP POLICY IF EXISTS platform_lifecycle_requests_svc_insert_policy ON platform_lifecycle_requests';
376
+ EXECUTE 'CREATE POLICY platform_lifecycle_requests_svc_insert_policy ON platform_lifecycle_requests FOR INSERT TO nodii_services WITH CHECK (tenant_id = auth.get_tenant_id())';
377
+ EXECUTE 'DROP POLICY IF EXISTS platform_lifecycle_requests_svc_update_policy ON platform_lifecycle_requests';
378
+ EXECUTE 'CREATE POLICY platform_lifecycle_requests_svc_update_policy ON platform_lifecycle_requests FOR UPDATE TO nodii_services USING (tenant_id = auth.get_tenant_id()) WITH CHECK (tenant_id = auth.get_tenant_id())';
379
+ EXECUTE 'DROP POLICY IF EXISTS platform_lifecycle_requests_svc_delete_policy ON platform_lifecycle_requests';
380
+ EXECUTE 'CREATE POLICY platform_lifecycle_requests_svc_delete_policy ON platform_lifecycle_requests FOR DELETE TO nodii_services USING (tenant_id = auth.get_tenant_id())';
381
+ END IF;
382
+ END
383
+ $rls_platform_lifecycle_requests$;
384
+ -- D412 / 08-rls v7 A1 — data_residency_holds: ENABLE + GRANTs + owner-bypass are
385
+ -- UNCONDITIONAL (a still-TEXT tenant_id is fail-CLOSED — RLS on, non-owner
386
+ -- deny-all until the per-verb policies land). The per-verb authenticated +
387
+ -- nodii_services policies compare tenant_id to a uuid, so they are guarded on
388
+ -- the column actually being uuid (deferred while a sibling table is mid
389
+ -- TEXT->UUID rewrite; they land on the next run once it is uuid).
390
+ ALTER TABLE data_residency_holds ENABLE ROW LEVEL SECURITY;
391
+ GRANT SELECT, INSERT, UPDATE, DELETE ON data_residency_holds TO nodii_services;
392
+ GRANT SELECT, INSERT, UPDATE, DELETE ON data_residency_holds TO authenticated;
393
+ DROP POLICY IF EXISTS data_residency_holds_owner_policy ON data_residency_holds;
394
+ CREATE POLICY data_residency_holds_owner_policy ON data_residency_holds FOR ALL TO nodii_owner USING (true) WITH CHECK (true);
395
+ DO $rls_data_residency_holds$
396
+ BEGIN
397
+ IF EXISTS (
398
+ SELECT 1 FROM information_schema.columns
399
+ WHERE table_schema = current_schema()
400
+ AND table_name = 'data_residency_holds'
401
+ AND column_name = 'tenant_id'
402
+ AND data_type = 'uuid'
403
+ ) THEN
404
+ EXECUTE 'DROP POLICY IF EXISTS data_residency_holds_auth_select_policy ON data_residency_holds';
405
+ EXECUTE 'CREATE POLICY data_residency_holds_auth_select_policy ON data_residency_holds FOR SELECT TO authenticated USING (tenant_id IN (SELECT auth.session_tenant_ids()))';
406
+ EXECUTE 'DROP POLICY IF EXISTS data_residency_holds_auth_insert_policy ON data_residency_holds';
407
+ EXECUTE 'CREATE POLICY data_residency_holds_auth_insert_policy ON data_residency_holds FOR INSERT TO authenticated WITH CHECK (tenant_id IN (SELECT auth.session_tenant_ids()))';
408
+ EXECUTE 'DROP POLICY IF EXISTS data_residency_holds_auth_update_policy ON data_residency_holds';
409
+ EXECUTE 'CREATE POLICY data_residency_holds_auth_update_policy ON data_residency_holds FOR UPDATE TO authenticated USING (tenant_id IN (SELECT auth.session_tenant_ids())) WITH CHECK (tenant_id IN (SELECT auth.session_tenant_ids()))';
410
+ EXECUTE 'DROP POLICY IF EXISTS data_residency_holds_auth_delete_policy ON data_residency_holds';
411
+ EXECUTE 'CREATE POLICY data_residency_holds_auth_delete_policy ON data_residency_holds FOR DELETE TO authenticated USING (tenant_id IN (SELECT auth.session_tenant_ids()))';
412
+ EXECUTE 'DROP POLICY IF EXISTS data_residency_holds_svc_select_policy ON data_residency_holds';
413
+ EXECUTE 'CREATE POLICY data_residency_holds_svc_select_policy ON data_residency_holds FOR SELECT TO nodii_services USING (tenant_id = auth.get_tenant_id())';
414
+ EXECUTE 'DROP POLICY IF EXISTS data_residency_holds_svc_insert_policy ON data_residency_holds';
415
+ EXECUTE 'CREATE POLICY data_residency_holds_svc_insert_policy ON data_residency_holds FOR INSERT TO nodii_services WITH CHECK (tenant_id = auth.get_tenant_id())';
416
+ EXECUTE 'DROP POLICY IF EXISTS data_residency_holds_svc_update_policy ON data_residency_holds';
417
+ EXECUTE 'CREATE POLICY data_residency_holds_svc_update_policy ON data_residency_holds FOR UPDATE TO nodii_services USING (tenant_id = auth.get_tenant_id()) WITH CHECK (tenant_id = auth.get_tenant_id())';
418
+ EXECUTE 'DROP POLICY IF EXISTS data_residency_holds_svc_delete_policy ON data_residency_holds';
419
+ EXECUTE 'CREATE POLICY data_residency_holds_svc_delete_policy ON data_residency_holds FOR DELETE TO nodii_services USING (tenant_id = auth.get_tenant_id())';
420
+ END IF;
421
+ END
422
+ $rls_data_residency_holds$;