@capybara-hq/kernel 0.1.0-rc.129 → 0.1.0-rc.130
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/dist/admin/usage.service.d.ts +20 -37
- package/dist/admin/usage.service.js +99 -122
- package/dist/automations/unattended.js +2 -0
- package/dist/background/wake-rules.js +1 -0
- package/dist/channels/outbound.js +2 -0
- package/dist/config/schema.d.ts +2 -0
- package/dist/config/schema.js +17 -1
- package/dist/conversations/conversations.service.js +5 -0
- package/dist/conversations/event-log.d.ts +6 -2
- package/dist/conversations/event-log.js +23 -10
- package/dist/conversations/event-org-backfill.d.ts +111 -0
- package/dist/conversations/event-org-backfill.js +264 -0
- package/dist/conversations/event-org-backfill.module.d.ts +3 -0
- package/dist/conversations/event-org-backfill.module.js +18 -0
- package/dist/conversations/events.d.ts +14 -0
- package/dist/conversations/events.js +2 -0
- package/dist/conversations/files.js +1 -0
- package/dist/conversations/leak-check.js +1 -0
- package/dist/conversations/prompt-suggestion-store.js +1 -0
- package/dist/conversations/prompt-suggestions.js +1 -0
- package/dist/conversations/queue.js +1 -0
- package/dist/conversations/selection.js +1 -0
- package/dist/conversations/titles.js +1 -0
- package/dist/db/kernel-schema.d.ts +8 -15
- package/dist/db/migrator.d.ts +6 -0
- package/dist/db/migrator.js +58 -6
- package/dist/evals/run-replay.js +4 -2
- package/dist/evals/sets.service.d.ts +1 -7
- package/dist/evals/sets.service.js +6 -15
- package/dist/evals/snapshot.js +2 -0
- package/dist/evals/trajectory.d.ts +7 -10
- package/dist/evals/trajectory.js +31 -23
- package/dist/evals/trajectory.service.d.ts +1 -7
- package/dist/evals/trajectory.service.js +10 -19
- package/dist/evals/worker.d.ts +2 -0
- package/dist/evals/worker.js +15 -2
- package/dist/identity/settings.d.ts +1 -0
- package/dist/identity/settings.js +3 -3
- package/dist/index.d.ts +1 -1
- package/dist/index.js +1 -1
- package/dist/infra/outbox/registry.d.ts +14 -0
- package/dist/infra/outbox/registry.js +15 -0
- package/dist/kernel.d.ts +6 -0
- package/dist/kernel.js +31 -2
- package/dist/lifecycle/kernel-lifecycle.js +2 -3
- package/dist/models/embeddings.d.ts +1 -0
- package/dist/models/embeddings.js +8 -0
- package/dist/models/images.d.ts +1 -0
- package/dist/models/images.js +7 -0
- package/dist/models/price-snapshot.d.ts +43 -0
- package/dist/models/price-snapshot.js +145 -0
- package/dist/platform/ops/admin.ops.js +1 -1
- package/dist/push/recipients.js +9 -8
- package/dist/roles/worker.module.js +6 -0
- package/dist/runtime/idle-arm.js +3 -0
- package/dist/runtime/idle-compaction.d.ts +1 -1
- package/dist/runtime/idle-compaction.js +6 -2
- package/dist/runtime/idle-return.js +3 -0
- package/dist/runtime/maintenance.d.ts +3 -1
- package/dist/runtime/maintenance.js +18 -5
- package/dist/runtime/permission-context.service.js +1 -0
- package/dist/runtime/plugin-context.service.js +1 -0
- package/dist/runtime/project/knowledge.service.js +2 -0
- package/dist/runtime/run-context.service.js +4 -0
- package/dist/runtime/run-deletion.d.ts +1 -0
- package/dist/runtime/run-deletion.js +7 -3
- package/dist/runtime/run-queue.js +3 -0
- package/dist/runtime/run-worker.js +1 -0
- package/dist/runtime/stream.js +4 -3
- package/dist/runtime/subagents/deliveries.js +1 -0
- package/dist/runtime/subagents/dispatch.js +1 -0
- package/dist/runtime/subagents/progress.js +1 -0
- package/dist/runtime/subagents/skill-fork.js +2 -0
- package/dist/runtime/usage-report.js +1 -0
- package/dist/runtime/waits.d.ts +1 -1
- package/dist/runtime/waits.js +3 -1
- package/dist/sandbox/managed-device-context.js +1 -0
- package/dist/sandbox/operation-authority.js +2 -0
- package/dist/sandbox/operation-intents.js +1 -0
- package/dist/sandbox/permission-call-grants.js +2 -0
- package/dist/sandbox/permission-facts.js +2 -0
- package/dist/sandbox/review/classifier.js +1 -0
- package/dist/sandbox/review/review.service.js +1 -0
- package/dist/skills/catalog.js +1 -0
- package/dist/skills/invoke.js +2 -0
- package/dist/tools/admin-connectors.js +2 -2
- package/dist/tools/builtins/builtin-tools.js +1 -0
- package/dist/tools/builtins/skill-load.js +2 -0
- package/dist/tools/connector-account-policies.js +3 -3
- package/dist/tools/connectors.service.js +13 -25
- package/dist/tools/drift.js +8 -14
- package/dist/tools/policy.js +2 -2
- package/dist/tools/registry.js +1 -2
- package/dist/tools/tool-watch.processor.d.ts +1 -1
- package/dist/tools/tool-watch.processor.js +1 -1
- package/dist/tools/tool-watch.service.js +22 -7
- package/dist/ui/views.js +1 -0
- package/package.json +1 -1
- package/src/migrations/0213_contract_org_security_policy.sql +43 -0
- package/src/migrations/0214_model_usage_price_snapshot.sql +2 -0
- package/src/migrations/0215_conversation_events_org_id.sql +48 -0
|
@@ -673,12 +673,11 @@ let ConnectorsService = class ConnectorsService {
|
|
|
673
673
|
.where("id", "=", connectorId)
|
|
674
674
|
.where("org_id", "=", orgId)
|
|
675
675
|
.execute();
|
|
676
|
-
//
|
|
677
|
-
//
|
|
678
|
-
// unnamed derived ones and the org's own (`admin`) stay
|
|
676
|
+
// A named tool takes its template policy. A tool removed from the template returns to its
|
|
677
|
+
// derived policy. Tools already using an unnamed derived policy stay as they are.
|
|
679
678
|
const atDefault = await t
|
|
680
679
|
.selectFrom("connector_tools")
|
|
681
|
-
.select(["name", "spec", "default_policy", "
|
|
680
|
+
.select(["name", "spec", "default_policy", "policy_source"])
|
|
682
681
|
.where("connector_id", "=", connectorId)
|
|
683
682
|
.forUpdate()
|
|
684
683
|
.execute();
|
|
@@ -692,13 +691,11 @@ let ConnectorsService = class ConnectorsService {
|
|
|
692
691
|
: policyFromAnnotations(tool.spec.annotations);
|
|
693
692
|
await t
|
|
694
693
|
.updateTable("connector_tools")
|
|
695
|
-
.set(
|
|
696
|
-
? { declaration_policy: next }
|
|
697
|
-
: { default_policy: next, policy_source: named ? "template" : "derived" })
|
|
694
|
+
.set({ default_policy: next, policy_source: named ? "template" : "derived" })
|
|
698
695
|
.where("connector_id", "=", connectorId)
|
|
699
696
|
.where("name", "=", tool.name)
|
|
700
697
|
.execute();
|
|
701
|
-
const previous = tool.
|
|
698
|
+
const previous = tool.default_policy;
|
|
702
699
|
if (next !== previous) {
|
|
703
700
|
policyChanges[tool.name] = { from: previous, to: next };
|
|
704
701
|
}
|
|
@@ -956,7 +953,6 @@ let ConnectorsService = class ConnectorsService {
|
|
|
956
953
|
"spec_hash",
|
|
957
954
|
"status",
|
|
958
955
|
"default_policy",
|
|
959
|
-
"declaration_policy",
|
|
960
956
|
"source",
|
|
961
957
|
"policy_source",
|
|
962
958
|
"missing_since",
|
|
@@ -985,15 +981,11 @@ let ConnectorsService = class ConnectorsService {
|
|
|
985
981
|
annotations: spec.annotations ?? null,
|
|
986
982
|
connectorData: spec.connectorData ?? null,
|
|
987
983
|
status: t.status,
|
|
988
|
-
defaultPolicy: t.
|
|
984
|
+
defaultPolicy: t.default_policy,
|
|
989
985
|
specHash: t.spec_hash,
|
|
990
986
|
issue: toolIssue(r.slug, next ?? spec),
|
|
991
987
|
source: t.source,
|
|
992
|
-
policySource: t.policy_source
|
|
993
|
-
? Object.hasOwn(r.template_tool_policies ?? {}, t.name)
|
|
994
|
-
? "template"
|
|
995
|
-
: "derived"
|
|
996
|
-
: t.policy_source,
|
|
988
|
+
policySource: t.policy_source,
|
|
997
989
|
missing: t.missing_since !== null,
|
|
998
990
|
reviewedAt: t.reviewed_at,
|
|
999
991
|
pending: next && t.pending_spec_hash !== null
|
|
@@ -1712,16 +1704,12 @@ let ConnectorsService = class ConnectorsService {
|
|
|
1712
1704
|
spec_hash: tool.pending_spec_hash,
|
|
1713
1705
|
source: nextSource,
|
|
1714
1706
|
}),
|
|
1715
|
-
...(
|
|
1716
|
-
|
|
1717
|
-
:
|
|
1718
|
-
?
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
? "derived"
|
|
1722
|
-
: "template",
|
|
1723
|
-
}
|
|
1724
|
-
: {}),
|
|
1707
|
+
...(rederive && {
|
|
1708
|
+
default_policy: declaration,
|
|
1709
|
+
policy_source: app.template_tool_policies?.[name] === undefined
|
|
1710
|
+
? "derived"
|
|
1711
|
+
: "template",
|
|
1712
|
+
}),
|
|
1725
1713
|
}
|
|
1726
1714
|
: { status: "disabled" }),
|
|
1727
1715
|
pending_spec: null,
|
package/dist/tools/drift.js
CHANGED
|
@@ -319,8 +319,8 @@ let ToolDrift = class ToolDrift {
|
|
|
319
319
|
if (!c)
|
|
320
320
|
continue;
|
|
321
321
|
const sameAsReviewed = r.specHash === c.spec_hash && r.source === c.source;
|
|
322
|
-
//
|
|
323
|
-
//
|
|
322
|
+
// An unreviewed tool has no approved definition to compare. Replace the current spec
|
|
323
|
+
// directly, keeping its pending-review or disabled state.
|
|
324
324
|
const replace = {
|
|
325
325
|
spec: JSON.stringify(r.spec),
|
|
326
326
|
spec_hash: r.specHash,
|
|
@@ -328,18 +328,12 @@ let ToolDrift = class ToolDrift {
|
|
|
328
328
|
pending_spec: null,
|
|
329
329
|
pending_spec_hash: null,
|
|
330
330
|
pending_source: null,
|
|
331
|
-
...(c.policy_source === "
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
default_policy: app.template_tool_policies?.[r.name] ?? policyFromAnnotations(r.spec.annotations),
|
|
338
|
-
policy_source: app.template_tool_policies?.[r.name] === undefined
|
|
339
|
-
? "derived"
|
|
340
|
-
: "template",
|
|
341
|
-
}
|
|
342
|
-
: {}),
|
|
331
|
+
...((c.policy_source === "derived" || c.source !== r.source) && {
|
|
332
|
+
default_policy: app.template_tool_policies?.[r.name] ?? policyFromAnnotations(r.spec.annotations),
|
|
333
|
+
policy_source: app.template_tool_policies?.[r.name] === undefined
|
|
334
|
+
? "derived"
|
|
335
|
+
: "template",
|
|
336
|
+
}),
|
|
343
337
|
};
|
|
344
338
|
// 复核过的工具:新定义(含不合规的)只进 pending,`spec` 始终是上次复核通过的定义(复核对比的基准);
|
|
345
339
|
// 不合规的新定义只能停用(approve 会因不合规被拒,reject 清掉)
|
package/dist/tools/policy.js
CHANGED
|
@@ -137,7 +137,7 @@ export function createDecidePolicy(db) {
|
|
|
137
137
|
return { unavailable: `App ${app.slug} is disabled` };
|
|
138
138
|
const row = await db
|
|
139
139
|
.selectFrom("connector_tools")
|
|
140
|
-
.select(["default_policy", "
|
|
140
|
+
.select(["default_policy", "spec_hash", "status", "missing_since"])
|
|
141
141
|
.where("connector_id", "=", tool.source.connectorId)
|
|
142
142
|
.where("name", "=", tool.source.name)
|
|
143
143
|
.executeTakeFirst();
|
|
@@ -148,7 +148,7 @@ export function createDecidePolicy(db) {
|
|
|
148
148
|
return { unavailable: "Tool is disabled" };
|
|
149
149
|
if (row.status !== "enabled")
|
|
150
150
|
return { unavailable: "Tool is pending review" };
|
|
151
|
-
connectorDefault = row.
|
|
151
|
+
connectorDefault = row.default_policy;
|
|
152
152
|
hashCurrent = row.spec_hash === tool.specHash;
|
|
153
153
|
// The call is made with the acting account's own connection (the credential the pipeline
|
|
154
154
|
// injects for `ctx.accountId`): that account's settings apply, never another's
|
package/dist/tools/registry.js
CHANGED
|
@@ -636,7 +636,6 @@ let ToolRegistry = class ToolRegistry {
|
|
|
636
636
|
"spec_hash",
|
|
637
637
|
"status",
|
|
638
638
|
"default_policy",
|
|
639
|
-
"declaration_policy",
|
|
640
639
|
"source",
|
|
641
640
|
"missing_since",
|
|
642
641
|
])
|
|
@@ -656,7 +655,7 @@ let ToolRegistry = class ToolRegistry {
|
|
|
656
655
|
spec: t.spec,
|
|
657
656
|
specHash: t.spec_hash,
|
|
658
657
|
status: t.status,
|
|
659
|
-
defaultPolicy: t.
|
|
658
|
+
defaultPolicy: t.default_policy,
|
|
660
659
|
source: t.source,
|
|
661
660
|
gone: t.missing_since !== null,
|
|
662
661
|
})),
|
|
@@ -8,7 +8,7 @@ import { type ToolWatchJobData } from "./tool-watch-queue.js";
|
|
|
8
8
|
/**
|
|
9
9
|
* The consumer of {@link TOOL_WATCH_QUEUE} (`cap-kernel-tool-watch`, its own worker so a slow
|
|
10
10
|
* device never delays the maintenance queue): each `tool-watch` job polls one watched question's
|
|
11
|
-
* watcher through {@link ToolWatchService},
|
|
11
|
+
* watcher through {@link ToolWatchService}, scoped to the stored question's org.
|
|
12
12
|
* Concurrency `tools.watchConcurrency` (polls are I/O-bound). Worker role only
|
|
13
13
|
* (`ToolWatchWorkersModule`); built with `autorun: false` and started by the worker role after the
|
|
14
14
|
* extensions loaded, so the watchers are registered.
|
|
@@ -22,7 +22,7 @@ import { ToolsModule } from "./tools.module.js";
|
|
|
22
22
|
/**
|
|
23
23
|
* The consumer of {@link TOOL_WATCH_QUEUE} (`cap-kernel-tool-watch`, its own worker so a slow
|
|
24
24
|
* device never delays the maintenance queue): each `tool-watch` job polls one watched question's
|
|
25
|
-
* watcher through {@link ToolWatchService},
|
|
25
|
+
* watcher through {@link ToolWatchService}, scoped to the stored question's org.
|
|
26
26
|
* Concurrency `tools.watchConcurrency` (polls are I/O-bound). Worker role only
|
|
27
27
|
* (`ToolWatchWorkersModule`); built with `autorun: false` and started by the worker role after the
|
|
28
28
|
* extensions loaded, so the watchers are registered.
|
|
@@ -14,6 +14,7 @@ import { sql } from "kysely";
|
|
|
14
14
|
import { EffectiveConfigs } from "../extensions/effective-config.js";
|
|
15
15
|
import { isExtensionEnabled } from "../extensions/enabled.js";
|
|
16
16
|
import { KernelDb } from "../infra/database/kernel-db.js";
|
|
17
|
+
import { asPlatform, withOrgScope } from "../infra/database/org-scope.js";
|
|
17
18
|
import { KERNEL_LOGGER } from "../infra/tokens.js";
|
|
18
19
|
import { attachCallScope } from "./call-scope.js";
|
|
19
20
|
import { OrgToolSwitches } from "./org-tool-switches.js";
|
|
@@ -66,14 +67,28 @@ let ToolWatchService = class ToolWatchService {
|
|
|
66
67
|
async process(job, token) {
|
|
67
68
|
let failures = job.data.failures;
|
|
68
69
|
try {
|
|
69
|
-
|
|
70
|
-
|
|
70
|
+
// org-filter: deployment-wide lookup of a watched question's org
|
|
71
|
+
const owner = await asPlatform("resolve the org of a watched question", () => this.db
|
|
72
|
+
.selectFrom("questions")
|
|
73
|
+
.select("org_id")
|
|
74
|
+
.where("id", "=", job.data.questionId)
|
|
75
|
+
.executeTakeFirst());
|
|
76
|
+
if (!owner)
|
|
71
77
|
return;
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
78
|
+
const next = await withOrgScope(owner.org_id, async () => {
|
|
79
|
+
const decision = await this.decide({ ...job.data, orgId: owner.org_id });
|
|
80
|
+
if (decision === "done")
|
|
81
|
+
return;
|
|
82
|
+
const count = decision === "failure" ? failures + 1 : 0;
|
|
83
|
+
if (decision === "wake" || count >= TOOL_WATCH_MAX_FAILURES) {
|
|
84
|
+
await this.questions.wake(job.data.questionId);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
return count;
|
|
88
|
+
});
|
|
89
|
+
if (next === undefined)
|
|
75
90
|
return;
|
|
76
|
-
|
|
91
|
+
failures = next;
|
|
77
92
|
}
|
|
78
93
|
catch (err) {
|
|
79
94
|
this.log.warn("a tool watch poll failed in the kernel; polling again", {
|
|
@@ -89,7 +104,7 @@ let ToolWatchService = class ToolWatchService {
|
|
|
89
104
|
}
|
|
90
105
|
/** What one poll of the question's watcher says (no writes). */
|
|
91
106
|
async decide(data) {
|
|
92
|
-
//
|
|
107
|
+
// The poll is scoped to the persisted question's org.
|
|
93
108
|
const row = await this.db
|
|
94
109
|
.selectFrom("questions as q")
|
|
95
110
|
.innerJoin("runs as r", "r.id", "q.run_id")
|
package/dist/ui/views.js
CHANGED
|
@@ -36,6 +36,7 @@ const whereOf = (t) => t.scope === "platform" ? { scope: "platform" } : { orgId:
|
|
|
36
36
|
* The card a `tool_result` of the conversation recorded for `callId`, or null (the inline-card
|
|
37
37
|
* attestation of {@link UiViewsDeps.recordedCard}). Scans one conversation's tool results.
|
|
38
38
|
*/
|
|
39
|
+
// org-filter: callers verified conversationId belongs to the caller's org before loading the card
|
|
39
40
|
export async function recordedCard(db, conversationId, callId) {
|
|
40
41
|
const row = await db
|
|
41
42
|
.selectFrom("conversation_events")
|
package/package.json
CHANGED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
DROP TRIGGER IF EXISTS org_security_legacy_settings_fence ON orgs;
|
|
2
|
+
DROP TRIGGER IF EXISTS org_security_legacy_connector_policy_fence ON connector_tools;
|
|
3
|
+
|
|
4
|
+
-- capybara:allow-destructive 0212 copied the rules and all replicas using the old switches have drained.
|
|
5
|
+
DROP TABLE IF EXISTS org_tool_switches;
|
|
6
|
+
|
|
7
|
+
DROP FUNCTION IF EXISTS fence_legacy_org_security_settings();
|
|
8
|
+
DROP FUNCTION IF EXISTS fence_legacy_org_tool_switches();
|
|
9
|
+
DROP FUNCTION IF EXISTS fence_legacy_connector_tool_policy();
|
|
10
|
+
|
|
11
|
+
UPDATE orgs
|
|
12
|
+
SET settings = CASE
|
|
13
|
+
WHEN jsonb_typeof(settings -> 'automations') = 'object'
|
|
14
|
+
THEN (settings - 'sandboxToolPolicy') #- '{automations,forbiddenTools}'
|
|
15
|
+
ELSE settings - 'sandboxToolPolicy'
|
|
16
|
+
END
|
|
17
|
+
WHERE jsonb_typeof(settings) = 'object' AND (
|
|
18
|
+
settings ? 'sandboxToolPolicy'
|
|
19
|
+
OR (jsonb_typeof(settings -> 'automations') = 'object' AND settings -> 'automations' ? 'forbiddenTools')
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
-- Keep the effective baseline that the expansion reader used. The outgoing release still reads
|
|
23
|
+
-- the nullable column alongside default_policy during the roll, so clear it after moving its value.
|
|
24
|
+
UPDATE connector_tools t
|
|
25
|
+
SET default_policy = COALESCE(t.declaration_policy, CASE
|
|
26
|
+
WHEN t.policy_source <> 'admin' THEN t.default_policy
|
|
27
|
+
WHEN c.template_tool_policies ->> t.name IN ('allow', 'ask', 'deny')
|
|
28
|
+
THEN c.template_tool_policies ->> t.name
|
|
29
|
+
WHEN t.spec #> '{annotations,readOnlyHint}' = 'true'::jsonb THEN 'allow'
|
|
30
|
+
ELSE 'ask'
|
|
31
|
+
END),
|
|
32
|
+
policy_source = CASE
|
|
33
|
+
WHEN t.policy_source <> 'admin' THEN t.policy_source
|
|
34
|
+
WHEN c.template_tool_policies ->> t.name IN ('allow', 'ask', 'deny') THEN 'template'
|
|
35
|
+
ELSE 'derived'
|
|
36
|
+
END,
|
|
37
|
+
declaration_policy = NULL
|
|
38
|
+
FROM connectors c
|
|
39
|
+
WHERE c.id = t.connector_id AND (t.policy_source = 'admin' OR t.declaration_policy IS NOT NULL);
|
|
40
|
+
|
|
41
|
+
ALTER TABLE connector_tools DROP CONSTRAINT IF EXISTS connector_tools_policy_source_check;
|
|
42
|
+
ALTER TABLE connector_tools ADD CONSTRAINT connector_tools_policy_source_check
|
|
43
|
+
CHECK (policy_source IN ('derived', 'template'));
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
-- Expand conversation_events with nullable org_id, composite conversation ownership FK (NOT VALID),
|
|
2
|
+
-- and dark org_scope RLS policy (issue #1175;
|
|
3
|
+
-- docs/superpowers/specs/2026-12-01-db-roles-tenancy-design.md, "Conversation events").
|
|
4
|
+
-- Short locks only: no backfill and no VALIDATE CONSTRAINT run inside this migration transaction.
|
|
5
|
+
-- Existing rows are backfilled in committed 5,000-row batches by the worker maintenance job
|
|
6
|
+
-- (EventOrgBackfillJob). No trigger is installed: old replicas may still insert without org_id
|
|
7
|
+
-- during the rolling window; VALIDATE CONSTRAINT and SET NOT NULL belong to the next release
|
|
8
|
+
-- after old writers drain.
|
|
9
|
+
|
|
10
|
+
ALTER TABLE conversation_events ADD COLUMN IF NOT EXISTS org_id text;
|
|
11
|
+
|
|
12
|
+
-- capybara:allow-destructive add conversations_id_org_id_uq and NOT VALID composite FK idempotently when absent; no rows or columns are removed
|
|
13
|
+
DO $$
|
|
14
|
+
BEGIN
|
|
15
|
+
IF NOT EXISTS (
|
|
16
|
+
SELECT 1
|
|
17
|
+
FROM pg_constraint c
|
|
18
|
+
JOIN pg_class r ON r.oid = c.conrelid
|
|
19
|
+
JOIN pg_namespace n ON n.oid = r.relnamespace
|
|
20
|
+
WHERE n.nspname = current_schema()
|
|
21
|
+
AND r.relname = 'conversations'
|
|
22
|
+
AND c.conname = 'conversations_id_org_id_uq'
|
|
23
|
+
) THEN
|
|
24
|
+
ALTER TABLE conversations
|
|
25
|
+
ADD CONSTRAINT conversations_id_org_id_uq UNIQUE (id, org_id);
|
|
26
|
+
END IF;
|
|
27
|
+
|
|
28
|
+
IF NOT EXISTS (
|
|
29
|
+
SELECT 1
|
|
30
|
+
FROM pg_constraint c
|
|
31
|
+
JOIN pg_class r ON r.oid = c.conrelid
|
|
32
|
+
JOIN pg_namespace n ON n.oid = r.relnamespace
|
|
33
|
+
WHERE n.nspname = current_schema()
|
|
34
|
+
AND r.relname = 'conversation_events'
|
|
35
|
+
AND c.conname = 'conversation_events_conversation_org_fkey'
|
|
36
|
+
) THEN
|
|
37
|
+
ALTER TABLE conversation_events
|
|
38
|
+
ADD CONSTRAINT conversation_events_conversation_org_fkey
|
|
39
|
+
FOREIGN KEY (conversation_id, org_id)
|
|
40
|
+
REFERENCES conversations (id, org_id)
|
|
41
|
+
ON DELETE CASCADE
|
|
42
|
+
NOT VALID;
|
|
43
|
+
END IF;
|
|
44
|
+
END
|
|
45
|
+
$$;
|
|
46
|
+
|
|
47
|
+
DROP POLICY IF EXISTS org_scope ON conversation_events;
|
|
48
|
+
CREATE POLICY org_scope ON conversation_events USING (rls_admits(org_id)) WITH CHECK (rls_admits(org_id));
|