slotlock 0.3.0 → 0.5.1

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.
package/ddl.js CHANGED
@@ -235,7 +235,11 @@ export const SLOTLOCK_SCHEMA_FUNCTIONS = Object.freeze([
235
235
  'slotlock.installed_schema_version()',
236
236
  ]);
237
237
  /** The table of suspended tenants: they may read, and write nothing (store.ts withTenant). */
238
- export const SLOTLOCK_TENANT_ADMIN_TABLES = Object.freeze(['tenant_suspensions']);
238
+ export const SLOTLOCK_TENANT_ADMIN_TABLES = Object.freeze([
239
+ 'tenant_suspensions',
240
+ 'webhook_tenants',
241
+ 'rate_buckets',
242
+ ]);
239
243
  /**
240
244
  * The SECURITY DEFINER functions through which a server checks a tenant's suspension and erases a
241
245
  * tenant, by signature.
@@ -243,6 +247,10 @@ export const SLOTLOCK_TENANT_ADMIN_TABLES = Object.freeze(['tenant_suspensions']
243
247
  export const SLOTLOCK_TENANT_ADMIN_FUNCTIONS = Object.freeze([
244
248
  'slotlock.tenant_suspended(text)',
245
249
  'slotlock.erase_tenant(text)',
250
+ 'slotlock.register_webhook_tenant(text)',
251
+ 'slotlock.webhook_tenants(text)',
252
+ 'slotlock.unregister_webhook_tenant(text)',
253
+ 'slotlock.take_rate_token(text, double precision, double precision)',
246
254
  ]);
247
255
  /**
248
256
  * Functions only the deployment role runs: suspending and lifting a suspension. No other role is
@@ -560,12 +568,30 @@ CREATE TABLE IF NOT EXISTS slotlock.dashboard_tokens (
560
568
  owner_hash bytea,
561
569
  expires_at timestamptz NOT NULL,
562
570
  CONSTRAINT slotlock_dashboard_tokens_pkey PRIMARY KEY (kind, token_hash),
563
- CONSTRAINT slotlock_dashboard_tokens_kind_valid CHECK (kind IN ('sign_in', 'form', 'ended_session')),
571
+ CONSTRAINT slotlock_dashboard_tokens_kind_valid
572
+ CHECK (kind IN ('sign_in', 'form', 'ended_session', 'confirmation')),
564
573
  CONSTRAINT slotlock_dashboard_tokens_hash_valid CHECK (octet_length(token_hash) = 32),
565
574
  CONSTRAINT slotlock_dashboard_tokens_owner_valid
566
575
  CHECK (owner_hash IS NULL OR octet_length(owner_hash) = 32),
567
576
  CONSTRAINT slotlock_dashboard_tokens_form_owner CHECK (kind <> 'form' OR owner_hash IS NOT NULL)
568
577
  );
578
+ -- 0.3.1 records accepted write confirmations here too (kind 'confirmation'). A schema from an
579
+ -- earlier release has the narrower check; replace it only then, so a migration with nothing to
580
+ -- change takes no lock on the table.
581
+ DO $slotlock_dashboard_token_kinds$
582
+ BEGIN
583
+ IF NOT EXISTS (
584
+ SELECT 1 FROM pg_catalog.pg_constraint
585
+ WHERE conrelid = 'slotlock.dashboard_tokens'::regclass
586
+ AND conname = 'slotlock_dashboard_tokens_kind_valid'
587
+ AND pg_catalog.pg_get_constraintdef(oid) LIKE '%confirmation%'
588
+ ) THEN
589
+ ALTER TABLE slotlock.dashboard_tokens DROP CONSTRAINT IF EXISTS slotlock_dashboard_tokens_kind_valid;
590
+ ALTER TABLE slotlock.dashboard_tokens ADD CONSTRAINT slotlock_dashboard_tokens_kind_valid
591
+ CHECK (kind IN ('sign_in', 'form', 'ended_session', 'confirmation'));
592
+ END IF;
593
+ END
594
+ $slotlock_dashboard_token_kinds$;
569
595
  CREATE INDEX IF NOT EXISTS slotlock_dashboard_tokens_expiry_idx
570
596
  ON slotlock.dashboard_tokens (expires_at);
571
597
  CREATE INDEX IF NOT EXISTS slotlock_dashboard_tokens_owner_idx
@@ -787,6 +813,9 @@ BEGIN
787
813
  previous := previous || jsonb_build_object(setting, current_setting(setting, true));
788
814
  PERFORM set_config(setting, requested_tenant_ref, true);
789
815
  END LOOP;
816
+ -- What erasure deletes is not a change anyone is told of: the feed goes with the tenant.
817
+ previous := previous || jsonb_build_object('slotlock.erasing', current_setting('slotlock.erasing', true));
818
+ PERFORM set_config('slotlock.erasing', requested_tenant_ref, true);
790
819
 
791
820
  erased_table := 'calendar_event_attendees';
792
821
  DELETE FROM slotlock.calendar_event_attendees WHERE tenant_ref = requested_tenant_ref;
@@ -839,10 +868,31 @@ BEGIN
839
868
  DELETE FROM slotlock.hold_commands WHERE tenant_ref = requested_tenant_ref;
840
869
  GET DIAGNOSTICS erased_rows = ROW_COUNT;
841
870
  RETURN NEXT;
871
+ erased_table := 'waitlist_entries';
872
+ DELETE FROM slotlock.waitlist_entries WHERE tenant_ref = requested_tenant_ref;
873
+ GET DIAGNOSTICS erased_rows = ROW_COUNT;
874
+ RETURN NEXT;
842
875
  erased_table := 'resources';
843
876
  DELETE FROM slotlock.resources WHERE tenant_ref = requested_tenant_ref;
844
877
  GET DIAGNOSTICS erased_rows = ROW_COUNT;
845
878
  RETURN NEXT;
879
+ -- After every booking table: what their deletion just appended goes with the rest.
880
+ erased_table := 'outbox';
881
+ DELETE FROM slotlock.outbox WHERE tenant_ref = requested_tenant_ref;
882
+ GET DIAGNOSTICS erased_rows = ROW_COUNT;
883
+ RETURN NEXT;
884
+ erased_table := 'outbox_pruned';
885
+ DELETE FROM slotlock.outbox_pruned WHERE tenant_ref = requested_tenant_ref;
886
+ GET DIAGNOSTICS erased_rows = ROW_COUNT;
887
+ RETURN NEXT;
888
+ erased_table := 'webhook_endpoints';
889
+ DELETE FROM slotlock.webhook_endpoints WHERE tenant_ref = requested_tenant_ref;
890
+ GET DIAGNOSTICS erased_rows = ROW_COUNT;
891
+ RETURN NEXT;
892
+ erased_table := 'webhook_tenants';
893
+ DELETE FROM slotlock.webhook_tenants WHERE tenant_ref = requested_tenant_ref;
894
+ GET DIAGNOSTICS erased_rows = ROW_COUNT;
895
+ RETURN NEXT;
846
896
  erased_table := 'api_keys';
847
897
  WITH erased AS (
848
898
  DELETE FROM slotlock.api_keys WHERE tenant_ref = requested_tenant_ref RETURNING secret_hash
@@ -870,6 +920,101 @@ BEGIN
870
920
  END
871
921
  $slotlock_erase_tenant$;
872
922
  REVOKE ALL ON FUNCTION ${SLOTLOCK_TENANT_ADMIN_FUNCTIONS[1]} FROM PUBLIC;
923
+
924
+ -- The tenants with a webhook endpoint, which a server delivers for. Row-level security hides every
925
+ -- other tenant's endpoints from the serving role, so this list (names only) is how a server that
926
+ -- restarted finds the tenants whose events still wait. No role but its owner reads it.
927
+ CREATE TABLE IF NOT EXISTS slotlock.webhook_tenants (
928
+ tenant_ref text PRIMARY KEY,
929
+ registered_at timestamptz NOT NULL DEFAULT now(),
930
+ CONSTRAINT slotlock_webhook_tenants_tenant_ref_valid
931
+ CHECK (octet_length(tenant_ref) BETWEEN 1 AND 500)
932
+ );
933
+ ALTER TABLE slotlock.webhook_tenants ENABLE ROW LEVEL SECURITY;
934
+ REVOKE ALL ON slotlock.webhook_tenants FROM PUBLIC;
935
+
936
+ CREATE OR REPLACE FUNCTION slotlock.register_webhook_tenant(requested_tenant_ref text)
937
+ RETURNS void
938
+ LANGUAGE sql
939
+ VOLATILE
940
+ SECURITY DEFINER
941
+ SET search_path = pg_catalog, slotlock, pg_temp
942
+ AS $slotlock_register_webhook_tenant$
943
+ INSERT INTO slotlock.webhook_tenants (tenant_ref) VALUES (requested_tenant_ref)
944
+ ON CONFLICT (tenant_ref) DO NOTHING;
945
+ $slotlock_register_webhook_tenant$;
946
+ REVOKE ALL ON FUNCTION ${SLOTLOCK_TENANT_ADMIN_FUNCTIONS[2]} FROM PUBLIC;
947
+
948
+ -- A page of 1,000 tenant names after \`after\` (NULL: from the start); a caller pages to the end.
949
+ DROP FUNCTION IF EXISTS slotlock.webhook_tenants();
950
+ CREATE OR REPLACE FUNCTION slotlock.webhook_tenants(after text)
951
+ RETURNS SETOF text
952
+ LANGUAGE sql
953
+ STABLE
954
+ SECURITY DEFINER
955
+ SET search_path = pg_catalog, slotlock, pg_temp
956
+ AS $slotlock_webhook_tenants$
957
+ SELECT tenant_ref FROM slotlock.webhook_tenants
958
+ WHERE after IS NULL OR tenant_ref > after
959
+ ORDER BY tenant_ref LIMIT 1000;
960
+ $slotlock_webhook_tenants$;
961
+ REVOKE ALL ON FUNCTION ${SLOTLOCK_TENANT_ADMIN_FUNCTIONS[3]} FROM PUBLIC;
962
+
963
+ -- Called when a tenant's last endpoint is deleted, so the list holds only tenants with one.
964
+ CREATE OR REPLACE FUNCTION slotlock.unregister_webhook_tenant(requested_tenant_ref text)
965
+ RETURNS void
966
+ LANGUAGE sql
967
+ VOLATILE
968
+ SECURITY DEFINER
969
+ SET search_path = pg_catalog, slotlock, pg_temp
970
+ AS $slotlock_unregister_webhook_tenant$
971
+ DELETE FROM slotlock.webhook_tenants WHERE tenant_ref = requested_tenant_ref;
972
+ $slotlock_unregister_webhook_tenant$;
973
+ REVOKE ALL ON FUNCTION ${SLOTLOCK_TENANT_ADMIN_FUNCTIONS[4]} FROM PUBLIC;
974
+
975
+ -- Request budgets shared by every server of a deployment (SLOTLOCK_SHARED_LIMITS): one token bucket
976
+ -- per limit and tenant, keyed by a digest, refilled and taken in one statement so racing servers
977
+ -- never both take the last token. No role but its owner reads it.
978
+ CREATE TABLE IF NOT EXISTS slotlock.rate_buckets (
979
+ bucket text PRIMARY KEY,
980
+ tokens double precision NOT NULL,
981
+ at timestamptz NOT NULL,
982
+ CONSTRAINT slotlock_rate_buckets_bucket_valid CHECK (bucket ~ '^[0-9a-f]{64}$')
983
+ );
984
+ ALTER TABLE slotlock.rate_buckets ENABLE ROW LEVEL SECURITY;
985
+ REVOKE ALL ON slotlock.rate_buckets FROM PUBLIC;
986
+
987
+ -- True when a token was taken: the bucket refills at per_second up to burst, and starts full.
988
+ CREATE OR REPLACE FUNCTION slotlock.take_rate_token(
989
+ requested_bucket text, per_second double precision, burst double precision
990
+ )
991
+ RETURNS boolean
992
+ LANGUAGE plpgsql
993
+ VOLATILE
994
+ SECURITY DEFINER
995
+ SET search_path = pg_catalog, slotlock, pg_temp
996
+ AS $slotlock_take_rate_token$
997
+ DECLARE
998
+ taken boolean;
999
+ BEGIN
1000
+ IF requested_bucket !~ '^[0-9a-f]{64}$' OR NOT (per_second > 0) OR NOT (burst >= 1) THEN
1001
+ RAISE EXCEPTION 'Slotlock rate bucket arguments are invalid' USING ERRCODE = 'invalid_parameter_value';
1002
+ END IF;
1003
+ INSERT INTO slotlock.rate_buckets AS kept (bucket, tokens, at)
1004
+ VALUES (requested_bucket, burst - 1, clock_timestamp())
1005
+ ON CONFLICT (bucket) DO UPDATE
1006
+ SET tokens = LEAST(burst, kept.tokens + EXTRACT(EPOCH FROM clock_timestamp() - kept.at) * per_second) - 1,
1007
+ at = clock_timestamp()
1008
+ WHERE LEAST(burst, kept.tokens + EXTRACT(EPOCH FROM clock_timestamp() - kept.at) * per_second) >= 1
1009
+ RETURNING true INTO taken;
1010
+ -- Now and then, buckets idle for an hour (full again long since) are dropped.
1011
+ IF random() < 0.001 THEN
1012
+ DELETE FROM slotlock.rate_buckets WHERE at < clock_timestamp() - interval '1 hour';
1013
+ END IF;
1014
+ RETURN COALESCE(taken, false);
1015
+ END
1016
+ $slotlock_take_rate_token$;
1017
+ REVOKE ALL ON FUNCTION ${SLOTLOCK_TENANT_ADMIN_FUNCTIONS[5]} FROM PUBLIC;
873
1018
  `;
874
1019
  // A constraint an upgrade added NOT VALID (no table scan under its ACCESS EXCLUSIVE lock) is checked
875
1020
  // here. applySchema runs this in a transaction of its own once the DDL has committed: inside the
@@ -913,6 +1058,337 @@ ON CONFLICT (singleton) DO UPDATE
913
1058
  SET version = EXCLUDED.version, ddl_sha256 = NULL, catalog_sha256 = NULL,
914
1059
  applied_at = EXCLUDED.applied_at;
915
1060
  `;
1061
+ // The event feed (RFC 0002 P1-5). Every committed change to a tenant's holds, bookings and calendar
1062
+ // events appends a row here in the same transaction, by trigger, so no write path can skip it and
1063
+ // nothing is recorded that did not commit. Rows carry ids, times and states, never titles,
1064
+ // descriptions or attendees. A reader orders rows by (txid, id) and reads only transactions below
1065
+ // its snapshot's xmin: every one of those has ended, so a transaction that commits late can never
1066
+ // add a row behind a position a reader has passed. Maintenance deletes rows past the retention.
1067
+ const SLOTLOCK_OUTBOX_DDL = `
1068
+ CREATE TABLE IF NOT EXISTS slotlock.outbox (
1069
+ id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
1070
+ txid xid8 NOT NULL DEFAULT pg_current_xact_id(),
1071
+ tenant_ref text NOT NULL,
1072
+ resource_id uuid,
1073
+ type text NOT NULL,
1074
+ subject text NOT NULL,
1075
+ data jsonb NOT NULL,
1076
+ occurred_at timestamptz NOT NULL DEFAULT statement_timestamp(),
1077
+ CONSTRAINT slotlock_outbox_tenant_ref_valid CHECK (octet_length(tenant_ref) BETWEEN 1 AND 500),
1078
+ CONSTRAINT slotlock_outbox_type_valid CHECK (type ~ '^[a-z]+\\.[a-z_]+$')
1079
+ );
1080
+ -- Who made the change (withTenant's actor: an API key, an agent), for the history.
1081
+ ALTER TABLE slotlock.outbox ADD COLUMN IF NOT EXISTS actor text;
1082
+ CREATE INDEX IF NOT EXISTS slotlock_outbox_tenant_resource_idx
1083
+ ON slotlock.outbox (tenant_ref, resource_id, id) WHERE resource_id IS NOT NULL;
1084
+ CREATE INDEX IF NOT EXISTS slotlock_outbox_tenant_subject_idx
1085
+ ON slotlock.outbox (tenant_ref, subject, id);
1086
+ CREATE INDEX IF NOT EXISTS slotlock_outbox_tenant_position_idx
1087
+ ON slotlock.outbox (tenant_ref, txid, id);
1088
+ CREATE INDEX IF NOT EXISTS slotlock_outbox_tenant_time_idx
1089
+ ON slotlock.outbox (tenant_ref, occurred_at);
1090
+ -- How far a tenant's feed has been pruned: no change after (txid, id) was ever deleted, so a cursor
1091
+ -- at or past it has missed none, and one before it may have, and is refused. Created once, it starts
1092
+ -- every tenant already known at its oldest kept change (or now): what an earlier version pruned is
1093
+ -- before it.
1094
+ DO $slotlock_outbox_pruned$
1095
+ BEGIN
1096
+ IF pg_catalog.to_regclass('slotlock.outbox_pruned') IS NULL THEN
1097
+ -- No change is deleted between reading what is kept and the mark it starts at.
1098
+ LOCK TABLE slotlock.outbox IN SHARE ROW EXCLUSIVE MODE;
1099
+ CREATE TABLE IF NOT EXISTS slotlock.outbox_pruned (
1100
+ tenant_ref text PRIMARY KEY,
1101
+ txid xid8 NOT NULL,
1102
+ id bigint NOT NULL,
1103
+ CONSTRAINT slotlock_outbox_pruned_tenant_ref_valid CHECK (octet_length(tenant_ref) BETWEEN 1 AND 500)
1104
+ );
1105
+ INSERT INTO slotlock.outbox_pruned (tenant_ref, txid, id)
1106
+ SELECT known.tenant_ref, min(known.txid), 0
1107
+ FROM (
1108
+ SELECT tenant_ref, txid FROM slotlock.outbox
1109
+ UNION ALL
1110
+ SELECT tenant_ref, pg_catalog.pg_snapshot_xmin(pg_catalog.pg_current_snapshot())
1111
+ FROM slotlock.resources WHERE tenant_ref IS NOT NULL
1112
+ ) known
1113
+ GROUP BY known.tenant_ref;
1114
+ END IF;
1115
+ END
1116
+ $slotlock_outbox_pruned$;
1117
+ -- Whatever deletes changes (maintenance, an older server's, an operator's), the mark follows it.
1118
+ CREATE OR REPLACE FUNCTION slotlock.outbox_mark_pruned()
1119
+ RETURNS trigger
1120
+ LANGUAGE plpgsql
1121
+ SET search_path = pg_catalog, slotlock, pg_temp
1122
+ AS $slotlock_outbox_mark_pruned$
1123
+ BEGIN
1124
+ INSERT INTO slotlock.outbox_pruned AS mark (tenant_ref, txid, id)
1125
+ SELECT DISTINCT ON (pruned.tenant_ref) pruned.tenant_ref, pruned.txid, pruned.id
1126
+ FROM pruned
1127
+ ORDER BY pruned.tenant_ref, pruned.txid DESC, pruned.id DESC
1128
+ ON CONFLICT (tenant_ref) DO UPDATE SET txid = excluded.txid, id = excluded.id
1129
+ WHERE (excluded.txid, excluded.id) > (mark.txid, mark.id);
1130
+ RETURN NULL;
1131
+ END
1132
+ $slotlock_outbox_mark_pruned$;
1133
+ CREATE OR REPLACE TRIGGER slotlock_outbox_mark_pruned
1134
+ AFTER DELETE ON slotlock.outbox REFERENCING OLD TABLE AS pruned
1135
+ FOR EACH STATEMENT EXECUTE FUNCTION slotlock.outbox_mark_pruned();
1136
+
1137
+ CREATE TABLE IF NOT EXISTS slotlock.webhook_endpoints (
1138
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
1139
+ tenant_ref text NOT NULL,
1140
+ url text NOT NULL,
1141
+ secret text NOT NULL,
1142
+ event_types text[],
1143
+ cursor_txid xid8 NOT NULL,
1144
+ cursor_id bigint NOT NULL DEFAULT 0,
1145
+ -- Set when maintenance pruned a change this (disabled) endpoint had not received: enabling it
1146
+ -- again is refused rather than skipping that change unseen.
1147
+ changes_pruned boolean NOT NULL DEFAULT false,
1148
+ attempts integer NOT NULL DEFAULT 0,
1149
+ next_attempt_at timestamptz,
1150
+ failing_since timestamptz,
1151
+ last_status integer,
1152
+ last_error text,
1153
+ disabled_at timestamptz,
1154
+ disabled_reason text,
1155
+ lease_until timestamptz,
1156
+ lease_token uuid,
1157
+ created_at timestamptz NOT NULL DEFAULT now(),
1158
+ CONSTRAINT slotlock_webhook_endpoints_identity_valid CHECK (
1159
+ octet_length(tenant_ref) BETWEEN 1 AND 500
1160
+ AND octet_length(url) BETWEEN 1 AND 2048
1161
+ AND secret ~ '^whsec_[A-Za-z0-9+/]{43}=$'
1162
+ ),
1163
+ CONSTRAINT slotlock_webhook_endpoints_types_valid CHECK (
1164
+ event_types IS NULL OR cardinality(event_types) BETWEEN 1 AND 20
1165
+ ),
1166
+ CONSTRAINT slotlock_webhook_endpoints_error_valid CHECK (
1167
+ last_error IS NULL OR char_length(last_error) <= 500
1168
+ )
1169
+ );
1170
+ ALTER TABLE slotlock.webhook_endpoints ADD COLUMN IF NOT EXISTS changes_pruned boolean NOT NULL DEFAULT false;
1171
+ CREATE INDEX IF NOT EXISTS slotlock_webhook_endpoints_tenant_idx
1172
+ ON slotlock.webhook_endpoints (tenant_ref, created_at);
1173
+
1174
+ -- Who waits for time on a resource or in a pool, offered in order (offerWaitlist): the first whose
1175
+ -- window comes free gets a hold of it, for offer_ttl_ms, and the feed says so (waitlist.offered).
1176
+ CREATE TABLE IF NOT EXISTS slotlock.waitlist_entries (
1177
+ id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
1178
+ tenant_ref text NOT NULL,
1179
+ resource_id uuid,
1180
+ pool_ref text,
1181
+ starts_at timestamptz NOT NULL,
1182
+ ends_at timestamptz NOT NULL,
1183
+ holder_ref text NOT NULL,
1184
+ offer_ttl_ms bigint NOT NULL,
1185
+ status text NOT NULL DEFAULT 'waiting',
1186
+ hold_id uuid,
1187
+ offered_resource_id uuid,
1188
+ offered_at timestamptz,
1189
+ created_at timestamptz NOT NULL DEFAULT statement_timestamp(),
1190
+ -- The resource is the entry's own tenant's, as the database sees it, whatever wrote the row.
1191
+ CONSTRAINT slotlock_waitlist_entries_resource_fk FOREIGN KEY (resource_id, tenant_ref)
1192
+ REFERENCES slotlock.resources (id, tenant_ref) ON DELETE CASCADE,
1193
+ CONSTRAINT slotlock_waitlist_entries_target_valid CHECK ((resource_id IS NULL) <> (pool_ref IS NULL)),
1194
+ CONSTRAINT slotlock_waitlist_entries_window_valid CHECK (ends_at > starts_at),
1195
+ CONSTRAINT slotlock_waitlist_entries_status_valid CHECK (status IN ('waiting', 'offered', 'expired')),
1196
+ CONSTRAINT slotlock_waitlist_entries_identity_valid CHECK (
1197
+ octet_length(tenant_ref) BETWEEN 1 AND 500 AND octet_length(holder_ref) BETWEEN 1 AND 500
1198
+ ),
1199
+ CONSTRAINT slotlock_waitlist_entries_ttl_valid CHECK (offer_ttl_ms BETWEEN 60000 AND 86400000),
1200
+ CONSTRAINT slotlock_waitlist_entries_offer_valid CHECK (
1201
+ (status = 'offered') = (hold_id IS NOT NULL AND offered_at IS NOT NULL AND offered_resource_id IS NOT NULL)
1202
+ )
1203
+ );
1204
+ CREATE INDEX IF NOT EXISTS slotlock_waitlist_entries_queue_idx
1205
+ ON slotlock.waitlist_entries (tenant_ref, created_at, id) WHERE status = 'waiting';
1206
+ CREATE INDEX IF NOT EXISTS slotlock_waitlist_entries_tenant_idx
1207
+ ON slotlock.waitlist_entries (tenant_ref, starts_at);
1208
+
1209
+ -- The tenant a reservation row belongs to: its own, or (a hold) its resource's.
1210
+ CREATE OR REPLACE FUNCTION slotlock.outbox_reservation_change()
1211
+ RETURNS trigger
1212
+ LANGUAGE plpgsql
1213
+ SET search_path = pg_catalog, slotlock, pg_temp
1214
+ AS $slotlock_outbox_reservation$
1215
+ DECLARE
1216
+ changed slotlock.reservations%ROWTYPE;
1217
+ owner text;
1218
+ kind text;
1219
+ BEGIN
1220
+ changed := CASE WHEN TG_OP = 'DELETE' THEN OLD ELSE NEW END;
1221
+ -- A calendar event's occupation is reported as the event's change.
1222
+ IF changed.calendar_event_id IS NOT NULL THEN RETURN NULL; END IF;
1223
+ owner := changed.tenant_ref;
1224
+ IF owner IS NULL THEN
1225
+ SELECT resource.tenant_ref INTO owner FROM slotlock.resources resource
1226
+ WHERE resource.id = changed.resource_id;
1227
+ END IF;
1228
+ IF owner IS NULL THEN RETURN NULL; END IF;
1229
+ -- Erasure (erase_tenant, running as the schema's owner) is not reported. The marker is an ordinary
1230
+ -- setting, so it counts only when the owner set it: the serving role cannot hide its own writes.
1231
+ IF owner = current_setting('slotlock.erasing', true)
1232
+ AND current_user = (SELECT pg_catalog.pg_get_userbyid(nspowner) FROM pg_catalog.pg_namespace
1233
+ WHERE nspname = 'slotlock') THEN
1234
+ RETURN NULL;
1235
+ END IF;
1236
+ IF TG_OP = 'INSERT' THEN
1237
+ kind := CASE WHEN NEW.status = 'held' THEN 'hold.created' ELSE 'reservation.created' END;
1238
+ ELSIF TG_OP = 'UPDATE' THEN
1239
+ IF OLD.status = 'held' AND NEW.status <> 'held' THEN
1240
+ kind := 'hold.confirmed';
1241
+ ELSIF (OLD.starts_at, OLD.ends_at, OLD.resource_id, OLD.revision, OLD.buffer_after_ms)
1242
+ IS NOT DISTINCT FROM
1243
+ (NEW.starts_at, NEW.ends_at, NEW.resource_id, NEW.revision, NEW.buffer_after_ms) THEN
1244
+ RETURN NULL;
1245
+ ELSE
1246
+ kind := CASE WHEN NEW.status = 'held' THEN 'hold.updated' ELSE 'reservation.updated' END;
1247
+ END IF;
1248
+ ELSE
1249
+ kind := CASE
1250
+ WHEN OLD.status <> 'held' THEN 'reservation.cancelled'
1251
+ WHEN OLD.expires_at <= statement_timestamp() THEN 'hold.expired'
1252
+ ELSE 'hold.released'
1253
+ END;
1254
+ END IF;
1255
+ INSERT INTO slotlock.outbox (tenant_ref, resource_id, type, subject, data, actor)
1256
+ VALUES (
1257
+ owner, changed.resource_id, kind,
1258
+ COALESCE(changed.external_ref, changed.id::text),
1259
+ jsonb_strip_nulls(jsonb_build_object(
1260
+ 'id', changed.id,
1261
+ 'reference', changed.external_ref,
1262
+ 'resource_id', changed.resource_id,
1263
+ 'start', changed.starts_at,
1264
+ 'end', changed.ends_at,
1265
+ 'buffer_after_ms', changed.buffer_after_ms,
1266
+ 'revision', CASE WHEN changed.status = 'held' THEN NULL ELSE changed.revision END,
1267
+ 'expires_at', CASE WHEN changed.status = 'held' THEN changed.expires_at END,
1268
+ -- A move says where it came from.
1269
+ 'before', CASE WHEN TG_OP = 'UPDATE' THEN jsonb_build_object(
1270
+ 'resource_id', OLD.resource_id, 'start', OLD.starts_at, 'end', OLD.ends_at,
1271
+ 'revision', OLD.revision) END
1272
+ )),
1273
+ NULLIF(current_setting('slotlock.actor', true), '')
1274
+ );
1275
+ RETURN NULL;
1276
+ END
1277
+ $slotlock_outbox_reservation$;
1278
+
1279
+ CREATE OR REPLACE FUNCTION slotlock.outbox_calendar_event_change()
1280
+ RETURNS trigger
1281
+ LANGUAGE plpgsql
1282
+ SET search_path = pg_catalog, slotlock, pg_temp
1283
+ AS $slotlock_outbox_calendar_event$
1284
+ DECLARE
1285
+ changed slotlock.calendar_events%ROWTYPE;
1286
+ kind text;
1287
+ BEGIN
1288
+ changed := CASE WHEN TG_OP = 'DELETE' THEN OLD ELSE NEW END;
1289
+ -- Erasure (erase_tenant, running as the schema's owner) is not reported. The marker is an ordinary
1290
+ -- setting, so it counts only when the owner set it: the serving role cannot hide its own writes.
1291
+ IF changed.tenant_ref = current_setting('slotlock.erasing', true)
1292
+ AND current_user = (SELECT pg_catalog.pg_get_userbyid(nspowner) FROM pg_catalog.pg_namespace
1293
+ WHERE nspname = 'slotlock') THEN
1294
+ RETURN NULL;
1295
+ END IF;
1296
+ IF TG_OP = 'INSERT' THEN
1297
+ kind := 'event.created';
1298
+ ELSIF TG_OP = 'UPDATE' THEN
1299
+ -- Extending a series to the rolling horizon changes nothing anyone wrote.
1300
+ IF OLD.revision = NEW.revision AND OLD.resource_id = NEW.resource_id THEN RETURN NULL; END IF;
1301
+ kind := 'event.updated';
1302
+ ELSE
1303
+ kind := 'event.cancelled';
1304
+ END IF;
1305
+ INSERT INTO slotlock.outbox (tenant_ref, resource_id, type, subject, data, actor)
1306
+ VALUES (
1307
+ changed.tenant_ref, changed.resource_id, kind, changed.id::text,
1308
+ jsonb_strip_nulls(jsonb_build_object(
1309
+ 'id', changed.id,
1310
+ 'resource_id', changed.resource_id,
1311
+ 'start', changed.starts_at,
1312
+ 'end', changed.ends_at,
1313
+ 'timezone', changed.timezone,
1314
+ 'status', changed.status,
1315
+ 'transparency', changed.transparency,
1316
+ 'recurring', changed.recurrence_rule IS NOT NULL,
1317
+ 'revision', changed.revision,
1318
+ 'before', CASE WHEN TG_OP = 'UPDATE' THEN jsonb_build_object(
1319
+ 'resource_id', OLD.resource_id, 'start', OLD.starts_at, 'end', OLD.ends_at,
1320
+ 'status', OLD.status, 'transparency', OLD.transparency, 'revision', OLD.revision) END
1321
+ )),
1322
+ NULLIF(current_setting('slotlock.actor', true), '')
1323
+ );
1324
+ RETURN NULL;
1325
+ END
1326
+ $slotlock_outbox_calendar_event$;
1327
+
1328
+ -- Time zone rules change (tzdata): an event is kept as the wall-clock time it was booked for in its
1329
+ -- zone as well as the instant, so maintenance can find a future one-off event whose wall time now
1330
+ -- falls at another instant. PostgreSQL's own tzdata resolves both. A zone it does not know gives
1331
+ -- NULL rather than an error, and such an event is left alone.
1332
+ CREATE OR REPLACE FUNCTION slotlock.zoned_wall(instant timestamptz, zone text)
1333
+ RETURNS timestamp LANGUAGE plpgsql STABLE SET search_path = pg_catalog, slotlock, pg_temp
1334
+ AS $slotlock_zoned_wall$
1335
+ BEGIN
1336
+ RETURN instant AT TIME ZONE zone;
1337
+ EXCEPTION WHEN OTHERS THEN
1338
+ RETURN NULL;
1339
+ END
1340
+ $slotlock_zoned_wall$;
1341
+ CREATE OR REPLACE FUNCTION slotlock.zoned_instant(wall timestamp, zone text)
1342
+ RETURNS timestamptz LANGUAGE plpgsql STABLE SET search_path = pg_catalog, slotlock, pg_temp
1343
+ AS $slotlock_zoned_instant$
1344
+ BEGIN
1345
+ RETURN wall AT TIME ZONE zone;
1346
+ EXCEPTION WHEN OTHERS THEN
1347
+ RETURN NULL;
1348
+ END
1349
+ $slotlock_zoned_instant$;
1350
+ ALTER TABLE slotlock.calendar_events ADD COLUMN IF NOT EXISTS wall_starts_at timestamp;
1351
+ ALTER TABLE slotlock.calendar_events ADD COLUMN IF NOT EXISTS wall_ends_at timestamp;
1352
+ -- Set when a shift was reported, so it is reported once; any change of the event's time clears it.
1353
+ ALTER TABLE slotlock.calendar_events ADD COLUMN IF NOT EXISTS zone_shift_reported_at timestamptz;
1354
+ CREATE OR REPLACE FUNCTION slotlock.calendar_event_wall_time()
1355
+ RETURNS trigger LANGUAGE plpgsql SET search_path = pg_catalog, slotlock, pg_temp
1356
+ AS $slotlock_calendar_event_wall_time$
1357
+ BEGIN
1358
+ IF TG_OP = 'INSERT'
1359
+ OR NEW.starts_at IS DISTINCT FROM OLD.starts_at OR NEW.ends_at IS DISTINCT FROM OLD.ends_at
1360
+ OR NEW.timezone IS DISTINCT FROM OLD.timezone THEN
1361
+ NEW.wall_starts_at := slotlock.zoned_wall(NEW.starts_at, NEW.timezone);
1362
+ NEW.wall_ends_at := slotlock.zoned_wall(NEW.ends_at, NEW.timezone);
1363
+ NEW.zone_shift_reported_at := NULL;
1364
+ END IF;
1365
+ RETURN NEW;
1366
+ END
1367
+ $slotlock_calendar_event_wall_time$;
1368
+ DROP TRIGGER IF EXISTS slotlock_calendar_event_wall_time ON slotlock.calendar_events;
1369
+ CREATE TRIGGER slotlock_calendar_event_wall_time
1370
+ BEFORE INSERT OR UPDATE ON slotlock.calendar_events
1371
+ FOR EACH ROW EXECUTE FUNCTION slotlock.calendar_event_wall_time();
1372
+ -- Events written before this release: their wall time as the rules read now.
1373
+ UPDATE slotlock.calendar_events
1374
+ SET wall_starts_at = slotlock.zoned_wall(starts_at, timezone),
1375
+ wall_ends_at = slotlock.zoned_wall(ends_at, timezone)
1376
+ WHERE wall_starts_at IS NULL AND ends_at > now();
1377
+
1378
+ -- Feed health: a source's last failure, and when its staleness was last reported on the feed.
1379
+ ALTER TABLE slotlock.calendar_coverage ADD COLUMN IF NOT EXISTS last_error text;
1380
+ ALTER TABLE slotlock.calendar_coverage ADD COLUMN IF NOT EXISTS last_error_at timestamptz;
1381
+ ALTER TABLE slotlock.calendar_coverage ADD COLUMN IF NOT EXISTS stale_reported_at timestamptz;
1382
+
1383
+ DROP TRIGGER IF EXISTS slotlock_outbox_reservation_change ON slotlock.reservations;
1384
+ CREATE TRIGGER slotlock_outbox_reservation_change
1385
+ AFTER INSERT OR UPDATE OR DELETE ON slotlock.reservations
1386
+ FOR EACH ROW EXECUTE FUNCTION slotlock.outbox_reservation_change();
1387
+ DROP TRIGGER IF EXISTS slotlock_outbox_calendar_event_change ON slotlock.calendar_events;
1388
+ CREATE TRIGGER slotlock_outbox_calendar_event_change
1389
+ AFTER INSERT OR UPDATE OR DELETE ON slotlock.calendar_events
1390
+ FOR EACH ROW EXECUTE FUNCTION slotlock.outbox_calendar_event_change();
1391
+ `;
916
1392
  // btree_gist is created in slotlock when it is missing; one installed elsewhere stays where it is.
917
1393
  const CORE_DDL_BODY = `
918
1394
  ${DEPLOYMENT_SEARCH_PATH_PIN}CREATE SCHEMA IF NOT EXISTS slotlock;
@@ -986,6 +1462,16 @@ ALTER TABLE slotlock.resources ADD COLUMN IF NOT EXISTS tenant_ref text;
986
1462
  -- Its CHECK holds SLOTLOCK_MAX_AVAILABILITY_RULES as it was when first added: changing the cap
987
1463
  -- needs a constraint under a new name.
988
1464
  ALTER TABLE slotlock.resources ADD COLUMN IF NOT EXISTS availability_rules jsonb;
1465
+ ALTER TABLE slotlock.resources ADD COLUMN IF NOT EXISTS booking_rules jsonb;
1466
+ -- Since when the booking rules have asked for check-in: only bookings made after it are released
1467
+ -- unclaimed (releaseNoShows), so turning the rule on releases none of the bookings already made.
1468
+ ALTER TABLE slotlock.resources ADD COLUMN IF NOT EXISTS check_in_since timestamptz;
1469
+ -- When the booking was claimed (checkInReservation); null while it has not been.
1470
+ ALTER TABLE slotlock.reservations ADD COLUMN IF NOT EXISTS checked_in_at timestamptz;
1471
+ -- The pool (a vehicle class, a room type) whose interchangeable members this resource is one of.
1472
+ ALTER TABLE slotlock.resources ADD COLUMN IF NOT EXISTS pool_ref text;
1473
+ CREATE INDEX IF NOT EXISTS slotlock_resources_pool_idx
1474
+ ON slotlock.resources (tenant_ref, pool_ref, id) WHERE pool_ref IS NOT NULL;
989
1475
  -- Who sees a resource: 'shared' ones are listed to agents and booked through them, 'private' ones
990
1476
  -- (a tax-deadline calendar, say) only by the tenant's own code. Fixed when the resource is created.
991
1477
  ALTER TABLE slotlock.resources ADD COLUMN IF NOT EXISTS visibility text NOT NULL DEFAULT 'shared';
@@ -995,6 +1481,14 @@ ALTER TABLE slotlock.reservations ADD COLUMN IF NOT EXISTS tenant_ref text;
995
1481
  ALTER TABLE slotlock.reservations ADD COLUMN IF NOT EXISTS external_ref text;
996
1482
  ALTER TABLE slotlock.reservations ADD COLUMN IF NOT EXISTS buffer_after_ms bigint NOT NULL DEFAULT 0;
997
1483
  ALTER TABLE slotlock.reservations ADD COLUMN IF NOT EXISTS occupied_ends_at timestamptz;
1484
+ -- A keyed hold's holder (its principal) and the group of alternatives it was taken in: an agent
1485
+ -- releases or books only its own holds, and booking one releases the rest of its group.
1486
+ ALTER TABLE slotlock.reservations ADD COLUMN IF NOT EXISTS holder_ref text;
1487
+ ALTER TABLE slotlock.reservations ADD COLUMN IF NOT EXISTS hold_group_id uuid;
1488
+ CREATE INDEX IF NOT EXISTS slotlock_reservations_hold_group_idx
1489
+ ON slotlock.reservations (hold_group_id) WHERE hold_group_id IS NOT NULL;
1490
+ CREATE INDEX IF NOT EXISTS slotlock_reservations_holder_idx
1491
+ ON slotlock.reservations (holder_ref, expires_at) WHERE status = 'held' AND holder_ref IS NOT NULL;
998
1492
  ALTER TABLE slotlock.reservations ADD COLUMN IF NOT EXISTS revision bigint NOT NULL DEFAULT 1;
999
1493
 
1000
1494
  -- timestamptz + interval is STABLE (session timezone aware), so PostgreSQL correctly refuses it
@@ -1212,6 +1706,25 @@ BEGIN
1212
1706
  ELSE false END
1213
1707
  );
1214
1708
  END IF;
1709
+ IF NOT EXISTS (
1710
+ SELECT 1 FROM pg_constraint
1711
+ WHERE conname = 'slotlock_resources_pool_ref_valid'
1712
+ ) THEN
1713
+ ALTER TABLE slotlock.resources
1714
+ ADD CONSTRAINT slotlock_resources_pool_ref_valid CHECK (
1715
+ pool_ref IS NULL OR (char_length(pool_ref) BETWEEN 1 AND 100 AND pool_ref ~ '^[a-z0-9][a-z0-9._-]*$')
1716
+ ) NOT VALID;
1717
+ END IF;
1718
+ IF NOT EXISTS (
1719
+ SELECT 1 FROM pg_constraint
1720
+ WHERE conname = 'slotlock_resources_booking_rules_valid'
1721
+ AND conrelid = 'slotlock.resources'::regclass
1722
+ ) THEN
1723
+ ALTER TABLE slotlock.resources
1724
+ ADD CONSTRAINT slotlock_resources_booking_rules_valid CHECK (
1725
+ booking_rules IS NULL OR jsonb_typeof(booking_rules) = 'object'
1726
+ );
1727
+ END IF;
1215
1728
  IF NOT EXISTS (
1216
1729
  SELECT 1 FROM pg_constraint
1217
1730
  WHERE conname = 'slotlock_resources_visibility_valid'
@@ -1550,6 +2063,11 @@ ALTER TABLE slotlock.calendar_events ADD COLUMN IF NOT EXISTS roll_retry_at time
1550
2063
  -- when its rule has no end, or none Slotlock could follow it to. NULL: not worked out yet (a series
1551
2064
  -- from before 0.3, until its next roll), and so treated as going on. Unused for a one-off event.
1552
2065
  ALTER TABLE slotlock.calendar_events ADD COLUMN IF NOT EXISTS recurrence_ends_by timestamptz;
2066
+ -- The turnaround a booked event keeps after each occurrence (its resource's, when it was booked), so
2067
+ -- the occurrences a later roll adds keep it too. At most a day.
2068
+ ALTER TABLE slotlock.calendar_events
2069
+ ADD COLUMN IF NOT EXISTS buffer_after_ms integer NOT NULL DEFAULT 0
2070
+ CONSTRAINT slotlock_calendar_events_buffer_valid CHECK (buffer_after_ms BETWEEN 0 AND 86400000);
1553
2071
  ALTER TABLE slotlock.calendar_events
1554
2072
  DROP CONSTRAINT IF EXISTS slotlock_calendar_events_recurrence_cursor_bounded;
1555
2073
  DO $slotlock_calendar_event_recurrence_cursor$
@@ -2083,7 +2601,7 @@ CREATE INDEX IF NOT EXISTS slotlock_calendar_event_occurrences_resource_time_idx
2083
2601
  ON slotlock.calendar_event_occurrences (tenant_ref, starts_at, ends_at);
2084
2602
  CREATE INDEX IF NOT EXISTS slotlock_reservations_calendar_event_idx
2085
2603
  ON slotlock.reservations (calendar_event_id) WHERE calendar_event_id IS NOT NULL;
2086
- ${SLOTLOCK_API_KEYS_DDL}${SLOTLOCK_DASHBOARD_DDL}${SLOTLOCK_TENANT_ADMIN_DDL}${DEPLOYMENT_RECORD_VERSION}`;
2604
+ ${SLOTLOCK_OUTBOX_DDL}${SLOTLOCK_API_KEYS_DDL}${SLOTLOCK_DASHBOARD_DDL}${SLOTLOCK_TENANT_ADMIN_DDL}${DEPLOYMENT_RECORD_VERSION}`;
2087
2605
  /** The whole deployment DDL, run in one transaction: the schema, then its constraints validated. */
2088
2606
  export const SLOTLOCK_CORE_DDL = `${CORE_DDL_BODY}${DEPLOYMENT_VALIDATE_CONSTRAINTS}${DEPLOYMENT_RESTORE_FORCED_ROW_SECURITY}${DEPLOYMENT_SEARCH_PATH_RESTORE}`;
2089
2607
  /**
@@ -2111,6 +2629,10 @@ export const SLOTLOCK_TENANT_TABLES = Object.freeze([
2111
2629
  'calendar_event_commands',
2112
2630
  'calendar_coverage',
2113
2631
  'hold_commands',
2632
+ 'outbox',
2633
+ 'outbox_pruned',
2634
+ 'webhook_endpoints',
2635
+ 'waitlist_entries',
2114
2636
  ]);
2115
2637
  const POSTGRES_SETTING_NAME = /^[a-z][a-z0-9_]*(?:\.[a-z][a-z0-9_]*)+$/;
2116
2638
  const POSTGRES_ROLE_NAME = /^[a-z_][a-z0-9_]{0,62}$/;
@@ -2172,6 +2694,14 @@ ALTER TABLE slotlock.calendar_coverage ENABLE ROW LEVEL SECURITY;
2172
2694
  ALTER TABLE slotlock.calendar_coverage FORCE ROW LEVEL SECURITY;
2173
2695
  ALTER TABLE slotlock.hold_commands ENABLE ROW LEVEL SECURITY;
2174
2696
  ALTER TABLE slotlock.hold_commands FORCE ROW LEVEL SECURITY;
2697
+ ALTER TABLE slotlock.outbox ENABLE ROW LEVEL SECURITY;
2698
+ ALTER TABLE slotlock.outbox FORCE ROW LEVEL SECURITY;
2699
+ ALTER TABLE slotlock.outbox_pruned ENABLE ROW LEVEL SECURITY;
2700
+ ALTER TABLE slotlock.outbox_pruned FORCE ROW LEVEL SECURITY;
2701
+ ALTER TABLE slotlock.webhook_endpoints ENABLE ROW LEVEL SECURITY;
2702
+ ALTER TABLE slotlock.webhook_endpoints FORCE ROW LEVEL SECURITY;
2703
+ ALTER TABLE slotlock.waitlist_entries ENABLE ROW LEVEL SECURITY;
2704
+ ALTER TABLE slotlock.waitlist_entries FORCE ROW LEVEL SECURITY;
2175
2705
 
2176
2706
  -- One database must have one tenant-context contract. Re-applying with an adapter-specific
2177
2707
  -- setting deliberately rebinds the stable policies instead of silently keeping a prior setting.
@@ -2196,7 +2726,11 @@ BEGIN
2196
2726
  'calendar_event_tombstones',
2197
2727
  'calendar_event_commands',
2198
2728
  'calendar_coverage',
2199
- 'hold_commands'
2729
+ 'hold_commands',
2730
+ 'outbox',
2731
+ 'outbox_pruned',
2732
+ 'webhook_endpoints',
2733
+ 'waitlist_entries'
2200
2734
  )
2201
2735
  LOOP
2202
2736
  EXECUTE format(
@@ -2367,6 +2901,46 @@ BEGIN
2367
2901
  USING (tenant_ref = NULLIF(current_setting('${tenantContextSetting}', true), ''))
2368
2902
  WITH CHECK (tenant_ref = NULLIF(current_setting('${tenantContextSetting}', true), ''));
2369
2903
  END IF;
2904
+
2905
+ IF NOT EXISTS (
2906
+ SELECT 1 FROM pg_policies
2907
+ WHERE schemaname = 'slotlock' AND tablename = 'outbox'
2908
+ AND policyname = 'tenant_isolation_v1'
2909
+ ) THEN
2910
+ CREATE POLICY tenant_isolation_v1 ON slotlock.outbox
2911
+ USING (tenant_ref = NULLIF(current_setting('${tenantContextSetting}', true), ''))
2912
+ WITH CHECK (tenant_ref = NULLIF(current_setting('${tenantContextSetting}', true), ''));
2913
+ END IF;
2914
+
2915
+ IF NOT EXISTS (
2916
+ SELECT 1 FROM pg_policies
2917
+ WHERE schemaname = 'slotlock' AND tablename = 'outbox_pruned'
2918
+ AND policyname = 'tenant_isolation_v1'
2919
+ ) THEN
2920
+ CREATE POLICY tenant_isolation_v1 ON slotlock.outbox_pruned
2921
+ USING (tenant_ref = NULLIF(current_setting('${tenantContextSetting}', true), ''))
2922
+ WITH CHECK (tenant_ref = NULLIF(current_setting('${tenantContextSetting}', true), ''));
2923
+ END IF;
2924
+
2925
+ IF NOT EXISTS (
2926
+ SELECT 1 FROM pg_policies
2927
+ WHERE schemaname = 'slotlock' AND tablename = 'webhook_endpoints'
2928
+ AND policyname = 'tenant_isolation_v1'
2929
+ ) THEN
2930
+ CREATE POLICY tenant_isolation_v1 ON slotlock.webhook_endpoints
2931
+ USING (tenant_ref = NULLIF(current_setting('${tenantContextSetting}', true), ''))
2932
+ WITH CHECK (tenant_ref = NULLIF(current_setting('${tenantContextSetting}', true), ''));
2933
+ END IF;
2934
+
2935
+ IF NOT EXISTS (
2936
+ SELECT 1 FROM pg_policies
2937
+ WHERE schemaname = 'slotlock' AND tablename = 'waitlist_entries'
2938
+ AND policyname = 'tenant_isolation_v1'
2939
+ ) THEN
2940
+ CREATE POLICY tenant_isolation_v1 ON slotlock.waitlist_entries
2941
+ USING (tenant_ref = NULLIF(current_setting('${tenantContextSetting}', true), ''))
2942
+ WITH CHECK (tenant_ref = NULLIF(current_setting('${tenantContextSetting}', true), ''));
2943
+ END IF;
2370
2944
  END
2371
2945
  $slotlock_rls$;
2372
2946
 
@@ -2396,6 +2970,14 @@ COMMENT ON POLICY tenant_isolation_v1 ON slotlock.calendar_coverage
2396
2970
  IS 'slotlock:tenant-context:${tenantContextSetting}';
2397
2971
  COMMENT ON POLICY tenant_isolation_v1 ON slotlock.hold_commands
2398
2972
  IS 'slotlock:tenant-context:${tenantContextSetting}';
2973
+ COMMENT ON POLICY tenant_isolation_v1 ON slotlock.outbox
2974
+ IS 'slotlock:tenant-context:${tenantContextSetting}';
2975
+ COMMENT ON POLICY tenant_isolation_v1 ON slotlock.outbox_pruned
2976
+ IS 'slotlock:tenant-context:${tenantContextSetting}';
2977
+ COMMENT ON POLICY tenant_isolation_v1 ON slotlock.webhook_endpoints
2978
+ IS 'slotlock:tenant-context:${tenantContextSetting}';
2979
+ COMMENT ON POLICY tenant_isolation_v1 ON slotlock.waitlist_entries
2980
+ IS 'slotlock:tenant-context:${tenantContextSetting}';
2399
2981
 
2400
2982
  INSERT INTO slotlock.rls_policy_contracts (
2401
2983
  table_name, tenant_context_setting, policy_fingerprint, installed_at
@@ -2431,7 +3013,11 @@ WHERE schemaname = 'slotlock'
2431
3013
  'calendar_event_tombstones',
2432
3014
  'calendar_event_commands',
2433
3015
  'calendar_coverage',
2434
- 'hold_commands'
3016
+ 'hold_commands',
3017
+ 'outbox',
3018
+ 'outbox_pruned',
3019
+ 'webhook_endpoints',
3020
+ 'waitlist_entries'
2435
3021
  )
2436
3022
  AND policyname = 'tenant_isolation_v1'
2437
3023
  ON CONFLICT (table_name) DO UPDATE