@composable-os/cli 0.0.0-stage → 0.1.0-beta.11
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/LICENSE +109 -0
- package/README.md +24 -2
- package/dist/accounts-UHYLCLEY.js +45 -0
- package/dist/blocks-5IFU25J6.js +233 -0
- package/dist/chunk-2ABSVMGB.js +13 -0
- package/dist/chunk-2IVEVPMY.js +669 -0
- package/dist/chunk-2TWV3SVB.js +349 -0
- package/dist/chunk-2XC5JLBO.js +1931 -0
- package/dist/chunk-324WJXNP.js +320 -0
- package/dist/chunk-5KSXSK7Y.js +47 -0
- package/dist/chunk-6ELZRNUU.js +116 -0
- package/dist/chunk-6UWRT4VM.js +220 -0
- package/dist/chunk-6WGFA2IV.js +643 -0
- package/dist/chunk-6XTU2MTP.js +11278 -0
- package/dist/chunk-7CQGMVDO.js +63 -0
- package/dist/chunk-7D4GKZOX.js +615 -0
- package/dist/chunk-7OGCMQXG.js +284 -0
- package/dist/chunk-DHS2CV2M.js +139 -0
- package/dist/chunk-DU6OIFOU.js +2358 -0
- package/dist/chunk-E4WCYTCT.js +347 -0
- package/dist/chunk-E64SG24J.js +88 -0
- package/dist/chunk-FJIZGS5Q.js +698 -0
- package/dist/chunk-GYTEW66E.js +50 -0
- package/dist/chunk-HDIOVFAZ.js +544 -0
- package/dist/chunk-K34UKWEI.js +298 -0
- package/dist/chunk-KGHA67HG.js +765 -0
- package/dist/chunk-LAAL4YGR.js +365 -0
- package/dist/chunk-LO6WGU5Y.js +26189 -0
- package/dist/chunk-MBIKVRAC.js +1425 -0
- package/dist/chunk-P6UWZ6QQ.js +426 -0
- package/dist/chunk-PDD2ETVF.js +1302 -0
- package/dist/chunk-UB4I2QKR.js +31575 -0
- package/dist/chunk-WHKJGF36.js +2418 -0
- package/dist/chunk-XIDLW3EP.js +15471 -0
- package/dist/chunk-YFTLIEWU.js +504 -0
- package/dist/chunk-YNUZRTPH.js +182 -0
- package/dist/chunk-ZAWES2UD.js +9364 -0
- package/dist/chunk-ZZFWMVJO.js +704 -0
- package/dist/cli-5UNY5G5L.js +136 -0
- package/dist/cli-UYZMVQMR.js +24534 -0
- package/dist/cli.js +25 -0
- package/dist/client-repo-SE4EKG6W.js +55 -0
- package/dist/create.js +25 -0
- package/dist/fields-FTWWMD7N.js +50 -0
- package/dist/grants-LOW6TSWD.js +32 -0
- package/dist/invoke-6KVT6WJI.js +41 -0
- package/dist/moves-E2HKYCTK.js +41 -0
- package/dist/order-U4KZLLSJ.js +19 -0
- package/dist/person-UHLVBVLR.js +67 -0
- package/dist/plan-on-sale-TJPFCNL7.js +26 -0
- package/dist/platform/migrations/0001_init.sql +59 -0
- package/dist/platform/migrations/0002_users_settings.sql +34 -0
- package/dist/platform/migrations/0003_people_organisations.sql +225 -0
- package/dist/platform/migrations/0004_activities_tasks.sql +88 -0
- package/dist/platform/migrations/0005_views_audit.sql +79 -0
- package/dist/platform/migrations/0006_search.sql +223 -0
- package/dist/platform/migrations/0007_crm_views.sql +141 -0
- package/dist/platform/migrations/0008_audit_redaction.sql +116 -0
- package/dist/platform/migrations/0010_approval_queue.sql +90 -0
- package/dist/platform/migrations/0011_connected_apps.sql +36 -0
- package/dist/platform/migrations/0020_extensions.sql +373 -0
- package/dist/platform/migrations/0030_extension_proposals.sql +29 -0
- package/dist/platform/migrations/0050_live_row_indexes.sql +32 -0
- package/dist/platform/migrations/0060_audit_read_counts.sql +31 -0
- package/dist/platform/migrations/0061_audit_retention.sql +95 -0
- package/dist/platform/migrations/0062_run_history_retention.sql +6 -0
- package/dist/platform/migrations/0063_pending_action_compaction.sql +18 -0
- package/dist/platform/migrations/0064_drop_rate_limit.sql +6 -0
- package/dist/platform/migrations/0065_storage_stats.sql +13 -0
- package/dist/platform/migrations/0070_drop_proposals_ext_idx.sql +9 -0
- package/dist/platform/migrations/0080_views_dashboards.sql +46 -0
- package/dist/platform/migrations/0100_stage_history.sql +54 -0
- package/dist/platform/migrations/0110_integrations.sql +103 -0
- package/dist/platform/migrations/0111_email_calendar_sync.sql +137 -0
- package/dist/platform/migrations/0112_sync_item_occurred_at.sql +8 -0
- package/dist/platform/migrations/0130_automations.sql +113 -0
- package/dist/platform/migrations/0131_notifications.sql +29 -0
- package/dist/platform/migrations/0132_automation_runtime.sql +14 -0
- package/dist/platform/migrations/0150_ai_summaries_embeddings.sql +48 -0
- package/dist/platform/migrations/0160_releases.sql +41 -0
- package/dist/platform/migrations/0161_setup_wizard.sql +30 -0
- package/dist/platform/migrations/0170_automation_connections.sql +32 -0
- package/dist/platform/migrations/0171_automation_outbox.sql +23 -0
- package/dist/platform/migrations/0172_notification_delivery.sql +46 -0
- package/dist/platform/migrations/0200_assistant_conversations.sql +25 -0
- package/dist/platform/migrations/0205_policy_adjudicator.sql +61 -0
- package/dist/platform/migrations/0210_daily_digest.sql +33 -0
- package/dist/platform/migrations/0220_entitlements.sql +13 -0
- package/dist/platform/migrations/0221_support_grants.sql +33 -0
- package/dist/platform/migrations/0250_judge.sql +11 -0
- package/dist/platform/migrations/0251_adjudication_judgement.sql +8 -0
- package/dist/platform/migrations/0252_judge_usage.sql +14 -0
- package/dist/platform/migrations/0255_summary_checks.sql +9 -0
- package/dist/platform/migrations/0256_outbound_check.sql +9 -0
- package/dist/platform/migrations/0260_ai_usage.sql +20 -0
- package/dist/platform/migrations/0270_assistant_conversation_refs.sql +29 -0
- package/dist/platform/migrations/0310_assignment_rules.sql +54 -0
- package/dist/platform/migrations/0320_duplicates.sql +41 -0
- package/dist/platform/migrations/0330_erase_completeness.sql +54 -0
- package/dist/platform/migrations/0350_assistant_thread.sql +38 -0
- package/dist/platform/migrations/0351_assistant_model_roles.sql +26 -0
- package/dist/platform/migrations/0352_assistant_memory.sql +74 -0
- package/dist/platform/migrations/0358_ai_cache_observation.sql +24 -0
- package/dist/platform/migrations/0359_ai_compaction_decision.sql +25 -0
- package/dist/platform/migrations/0360_ai_cache_estimate.sql +35 -0
- package/dist/platform/migrations/0361_voice_preferences.sql +27 -0
- package/dist/platform/migrations/0362_voice_usage.sql +17 -0
- package/dist/platform/migrations/0365_routines.sql +38 -0
- package/dist/platform/migrations/0370_approval_execution.sql +20 -0
- package/dist/platform/migrations/0371_agent_jobs.sql +65 -0
- package/dist/platform/migrations/0380_rec_views.sql +94 -0
- package/dist/platform/migrations/0400_customer_audit_actor.sql +142 -0
- package/dist/platform/migrations/0401_customer_accounts.sql +61 -0
- package/dist/platform/migrations/0402_erase_requests.sql +34 -0
- package/dist/platform/migrations/0410_idempotency_keys.sql +45 -0
- package/dist/platform/migrations/0411_device_sessions.sql +33 -0
- package/dist/platform/migrations/0420_transition_history.sql +100 -0
- package/dist/platform/migrations/0421_orders.sql +134 -0
- package/dist/platform/migrations/0422_order_payments.sql +109 -0
- package/dist/platform/migrations/0440_capacity.sql +100 -0
- package/dist/platform/migrations/0441_capacity_views.sql +40 -0
- package/dist/platform/migrations/0450_order_grants.sql +21 -0
- package/dist/platform/migrations/0451_pack_roles.sql +18 -0
- package/dist/platform/migrations/0460_content.sql +59 -0
- package/dist/platform/migrations/0461_media.sql +52 -0
- package/dist/platform/migrations/0470_publish_versions.sql +36 -0
- package/dist/platform/migrations/0471_permission_grants.sql +18 -0
- package/dist/platform/migrations/0490_staff_invites.sql +40 -0
- package/dist/platform/migrations/0491_sign_in_mail_kind.sql +10 -0
- package/dist/platform/migrations/0492_email_confirmation_and_sign_in_confirm.sql +30 -0
- package/dist/platform/migrations/0500_published_media_field.sql +25 -0
- package/dist/platform/migrations/0501_import_keys.sql +23 -0
- package/dist/platform/migrations/0510_video_and_file_media.sql +109 -0
- package/dist/platform/migrations/0516_order_tax.sql +63 -0
- package/dist/platform/migrations/0520_publish_requests.sql +20 -0
- package/dist/platform/migrations/0530_subscriptions.sql +173 -0
- package/dist/platform/migrations/0531_subscription_trials.sql +56 -0
- package/dist/platform/migrations/0532_subscription_offers.sql +80 -0
- package/dist/platform/migrations/0533_allowances.sql +262 -0
- package/dist/platform/migrations/0560_recurring.sql +75 -0
- package/dist/platform/migrations/0580_subscription_grants.sql +21 -0
- package/dist/platform/migrations/0581_once_per_person.sql +34 -0
- package/dist/platform/migrations/0600_tool_actions_ran_at_queue.sql +22 -0
- package/dist/platform/migrations/0610_waitlists.sql +54 -0
- package/dist/platform/migrations/0630_subscription_changes.sql +122 -0
- package/dist/platform/migrations/0650_person_links.sql +90 -0
- package/dist/platform/migrations/0670_unmoved_guard.sql +52 -0
- package/dist/platform/migrations/0690_sellable_refund_due.sql +98 -0
- package/dist/platform/sdk/allowance.ts +522 -0
- package/dist/platform/sdk/automation-check.ts +534 -0
- package/dist/platform/sdk/automation.ts +743 -0
- package/dist/platform/sdk/behaviour.ts +215 -0
- package/dist/platform/sdk/capacity.ts +303 -0
- package/dist/platform/sdk/contract.ts +909 -0
- package/dist/platform/sdk/document.ts +935 -0
- package/dist/platform/sdk/field-types.ts +111 -0
- package/dist/platform/sdk/formula/ast.ts +72 -0
- package/dist/platform/sdk/formula/compile.ts +454 -0
- package/dist/platform/sdk/formula/index.ts +4 -0
- package/dist/platform/sdk/formula/parser.ts +417 -0
- package/dist/platform/sdk/index.ts +36 -0
- package/dist/platform/sdk/manifest.ts +1785 -0
- package/dist/platform/sdk/media.ts +108 -0
- package/dist/platform/sdk/permissions.ts +60 -0
- package/dist/platform/sdk/recurring.ts +321 -0
- package/dist/platform/sdk/renamed.ts +63 -0
- package/dist/platform/sdk/rich-text-fixture.ts +68 -0
- package/dist/platform/sdk/rich-text.ts +221 -0
- package/dist/platform/sdk/runtime.ts +410 -0
- package/dist/platform/sdk/sellable.ts +1138 -0
- package/dist/platform/sdk/slug.ts +25 -0
- package/dist/platform/sdk/states.ts +598 -0
- package/dist/platform/sdk/testing.ts +847 -0
- package/dist/platform/sdk/unique.ts +81 -0
- package/dist/products-2H5DR232.js +11 -0
- package/dist/run-7P4RQALH.js +648 -0
- package/dist/run-PO3W5BFA.js +508 -0
- package/dist/run-ZCGFMEZF.js +309 -0
- package/dist/src-CRO35DYY.js +46 -0
- package/dist/src-I3IJ4NKC.js +354 -0
- package/dist/token-MOJUA2U6.js +70 -0
- package/dist/token-util-TENRN4V7.js +6 -0
- package/dist/visibility-TVXZ67FD.js +41 -0
- package/package.json +33 -4
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
-- Migration 0020: extension runtime state, the `extension` actor, and base views for the schema
|
|
2
|
+
-- reconciler (stage 3, ADR 0023, ADR 0024, ADR 0025, ADR 0040).
|
|
3
|
+
--
|
|
4
|
+
-- This migration touches core tables only. The x_* tables and the crm_* views that join them are
|
|
5
|
+
-- owned by the reconciler (packages/core/src/extensions/reconciler.ts), never by migrations.
|
|
6
|
+
|
|
7
|
+
-- 1. The audit log accepts actor `extension` (calls made by extension code through
|
|
8
|
+
-- codemode:crm). SQLite cannot alter a CHECK constraint, so core_audit is rebuilt with the
|
|
9
|
+
-- same rows. Its children (core_audit_changes, core_audit_redactions) reference it by name;
|
|
10
|
+
-- foreign keys are deferred so the swap is checked once, at commit. The redaction trigger
|
|
11
|
+
-- that writes core_audit is dropped first and recreated after, so the rename sees a
|
|
12
|
+
-- consistent schema.
|
|
13
|
+
PRAGMA defer_foreign_keys = ON;
|
|
14
|
+
|
|
15
|
+
DROP TRIGGER core_audit_redactions_apply_audit;
|
|
16
|
+
|
|
17
|
+
CREATE TABLE core_audit_next (
|
|
18
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
19
|
+
at TEXT NOT NULL,
|
|
20
|
+
operation TEXT NOT NULL,
|
|
21
|
+
risk TEXT NOT NULL
|
|
22
|
+
CHECK (risk IN ('read', 'write', 'bulk-write', 'destructive', 'schema', 'extension', 'outbound')),
|
|
23
|
+
actor TEXT NOT NULL CHECK (actor IN ('human', 'assistant', 'mcp', 'automation', 'extension')),
|
|
24
|
+
user_id TEXT,
|
|
25
|
+
origin TEXT NOT NULL CHECK (json_valid(origin)),
|
|
26
|
+
input TEXT NOT NULL CHECK (json_valid(input)),
|
|
27
|
+
result TEXT CHECK (result IS NULL OR json_valid(result)),
|
|
28
|
+
gate_decision TEXT NOT NULL CHECK (gate_decision IN ('auto', 'review', 'deny')),
|
|
29
|
+
outcome TEXT NOT NULL CHECK (outcome IN ('ok', 'forbidden', 'denied', 'review', 'error')),
|
|
30
|
+
error TEXT CHECK (error IS NULL OR json_valid(error)),
|
|
31
|
+
-- Migration 0010: the pending action an audit row queued or applied.
|
|
32
|
+
pending_action_id TEXT
|
|
33
|
+
);
|
|
34
|
+
|
|
35
|
+
INSERT INTO core_audit_next
|
|
36
|
+
(id, at, operation, risk, actor, user_id, origin, input, result, gate_decision, outcome, error,
|
|
37
|
+
pending_action_id)
|
|
38
|
+
SELECT id, at, operation, risk, actor, user_id, origin, input, result, gate_decision, outcome, error,
|
|
39
|
+
pending_action_id
|
|
40
|
+
FROM core_audit;
|
|
41
|
+
|
|
42
|
+
DROP TABLE core_audit;
|
|
43
|
+
ALTER TABLE core_audit_next RENAME TO core_audit;
|
|
44
|
+
|
|
45
|
+
CREATE INDEX core_audit_at_idx ON core_audit (at, id);
|
|
46
|
+
CREATE INDEX core_audit_user_idx ON core_audit (user_id, at);
|
|
47
|
+
CREATE INDEX core_audit_operation_idx ON core_audit (operation, at);
|
|
48
|
+
CREATE INDEX core_audit_actor_idx ON core_audit (actor, at);
|
|
49
|
+
CREATE INDEX core_audit_pending_action_idx ON core_audit (pending_action_id)
|
|
50
|
+
WHERE pending_action_id IS NOT NULL;
|
|
51
|
+
|
|
52
|
+
-- The approval queue (migration 0010) accepts actor `extension` too: a gate `review` on an
|
|
53
|
+
-- extension's call queues a pending action like any other. Same rebuild, same deferred keys.
|
|
54
|
+
CREATE TABLE core_change_set_next (
|
|
55
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
56
|
+
title TEXT,
|
|
57
|
+
session_id TEXT,
|
|
58
|
+
client_id TEXT,
|
|
59
|
+
client_name TEXT,
|
|
60
|
+
user_id TEXT NOT NULL,
|
|
61
|
+
actor TEXT NOT NULL CHECK (actor IN ('human', 'assistant', 'mcp', 'automation', 'extension')),
|
|
62
|
+
origin TEXT NOT NULL CHECK (json_valid(origin)),
|
|
63
|
+
explicit INTEGER NOT NULL DEFAULT 0 CHECK (explicit IN (0, 1)),
|
|
64
|
+
state TEXT NOT NULL DEFAULT 'open' CHECK (state IN ('open', 'committed', 'closed')),
|
|
65
|
+
created_at TEXT NOT NULL,
|
|
66
|
+
updated_at TEXT NOT NULL,
|
|
67
|
+
committed_at TEXT,
|
|
68
|
+
closed_at TEXT
|
|
69
|
+
);
|
|
70
|
+
INSERT INTO core_change_set_next SELECT * FROM core_change_set;
|
|
71
|
+
DROP TABLE core_change_set;
|
|
72
|
+
ALTER TABLE core_change_set_next RENAME TO core_change_set;
|
|
73
|
+
CREATE INDEX core_change_set_session_idx ON core_change_set (session_id, state, created_at);
|
|
74
|
+
CREATE INDEX core_change_set_state_idx ON core_change_set (state, created_at);
|
|
75
|
+
CREATE INDEX core_change_set_user_idx ON core_change_set (user_id, created_at);
|
|
76
|
+
|
|
77
|
+
CREATE TABLE core_pending_action_next (
|
|
78
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
79
|
+
change_set_id TEXT NOT NULL REFERENCES core_change_set (id),
|
|
80
|
+
operation TEXT NOT NULL,
|
|
81
|
+
risk TEXT NOT NULL
|
|
82
|
+
CHECK (risk IN ('read', 'write', 'bulk-write', 'destructive', 'schema', 'extension', 'outbound')),
|
|
83
|
+
input TEXT NOT NULL CHECK (json_valid(input)),
|
|
84
|
+
reason TEXT NOT NULL,
|
|
85
|
+
actor TEXT NOT NULL CHECK (actor IN ('human', 'assistant', 'mcp', 'automation', 'extension')),
|
|
86
|
+
user_id TEXT NOT NULL,
|
|
87
|
+
origin TEXT NOT NULL CHECK (json_valid(origin)),
|
|
88
|
+
granted_permissions TEXT CHECK (granted_permissions IS NULL OR json_valid(granted_permissions)),
|
|
89
|
+
bulk_size INTEGER NOT NULL DEFAULT 1,
|
|
90
|
+
preview TEXT NOT NULL CHECK (json_valid(preview)),
|
|
91
|
+
simulated TEXT NOT NULL CHECK (json_valid(simulated)),
|
|
92
|
+
provisional TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(provisional)),
|
|
93
|
+
depends_on TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(depends_on)),
|
|
94
|
+
status TEXT NOT NULL DEFAULT 'pending'
|
|
95
|
+
CHECK (status IN ('pending', 'applied', 'failed', 'skipped', 'rejected', 'expired')),
|
|
96
|
+
status_reason TEXT,
|
|
97
|
+
error TEXT CHECK (error IS NULL OR json_valid(error)),
|
|
98
|
+
bindings TEXT CHECK (bindings IS NULL OR json_valid(bindings)),
|
|
99
|
+
result TEXT CHECK (result IS NULL OR json_valid(result)),
|
|
100
|
+
request_audit_id TEXT,
|
|
101
|
+
applied_audit_id TEXT,
|
|
102
|
+
decided_by TEXT,
|
|
103
|
+
decided_at TEXT,
|
|
104
|
+
created_at TEXT NOT NULL,
|
|
105
|
+
expires_at TEXT NOT NULL
|
|
106
|
+
);
|
|
107
|
+
INSERT INTO core_pending_action_next SELECT * FROM core_pending_action;
|
|
108
|
+
DROP TABLE core_pending_action;
|
|
109
|
+
ALTER TABLE core_pending_action_next RENAME TO core_pending_action;
|
|
110
|
+
CREATE INDEX core_pending_action_set_idx ON core_pending_action (change_set_id, id);
|
|
111
|
+
CREATE INDEX core_pending_action_status_idx ON core_pending_action (status, expires_at);
|
|
112
|
+
|
|
113
|
+
CREATE TRIGGER core_audit_no_update BEFORE UPDATE ON core_audit
|
|
114
|
+
WHEN NOT (
|
|
115
|
+
EXISTS (SELECT 1 FROM core_audit_redactions r WHERE r.audit_id = OLD.id)
|
|
116
|
+
AND NEW.id IS OLD.id
|
|
117
|
+
AND NEW.at IS OLD.at
|
|
118
|
+
AND NEW.operation IS OLD.operation
|
|
119
|
+
AND NEW.risk IS OLD.risk
|
|
120
|
+
AND NEW.actor IS OLD.actor
|
|
121
|
+
AND NEW.user_id IS OLD.user_id
|
|
122
|
+
AND NEW.origin IS OLD.origin
|
|
123
|
+
AND NEW.gate_decision IS OLD.gate_decision
|
|
124
|
+
AND NEW.outcome IS OLD.outcome
|
|
125
|
+
AND NEW.input IS '"[erased]"'
|
|
126
|
+
AND NEW.result IS (CASE WHEN OLD.result IS NULL THEN NULL ELSE '"[erased]"' END)
|
|
127
|
+
AND NEW.error IS (CASE WHEN OLD.error IS NULL THEN NULL
|
|
128
|
+
ELSE json_object('code', json_extract(OLD.error, '$.code'), 'message', '[erased]') END)
|
|
129
|
+
)
|
|
130
|
+
BEGIN
|
|
131
|
+
SELECT RAISE(ABORT, 'core_audit is append-only');
|
|
132
|
+
END;
|
|
133
|
+
|
|
134
|
+
CREATE TRIGGER core_audit_no_delete BEFORE DELETE ON core_audit
|
|
135
|
+
BEGIN
|
|
136
|
+
SELECT RAISE(ABORT, 'core_audit is append-only');
|
|
137
|
+
END;
|
|
138
|
+
|
|
139
|
+
CREATE TRIGGER core_audit_redactions_apply_audit AFTER INSERT ON core_audit_redactions
|
|
140
|
+
WHEN NEW.audit_id IS NOT NULL
|
|
141
|
+
BEGIN
|
|
142
|
+
UPDATE core_audit SET
|
|
143
|
+
input = '"[erased]"',
|
|
144
|
+
result = CASE WHEN result IS NULL THEN NULL ELSE '"[erased]"' END,
|
|
145
|
+
error = CASE WHEN error IS NULL THEN NULL
|
|
146
|
+
ELSE json_object('code', json_extract(error, '$.code'), 'message', '[erased]') END
|
|
147
|
+
WHERE id = NEW.audit_id;
|
|
148
|
+
END;
|
|
149
|
+
|
|
150
|
+
-- 2. Extension principals. Each installed extension acts as a core_user row of kind
|
|
151
|
+
-- `extension` (id `extension:<id>`), so created_by/owner columns and the audit log can name
|
|
152
|
+
-- it. They cannot sign in (no account), are never the first admin, and are not listed.
|
|
153
|
+
ALTER TABLE core_user ADD COLUMN kind TEXT NOT NULL DEFAULT 'human'
|
|
154
|
+
CHECK (kind IN ('human', 'extension'));
|
|
155
|
+
|
|
156
|
+
DROP TRIGGER core_user_first_admin;
|
|
157
|
+
CREATE TRIGGER core_user_first_admin
|
|
158
|
+
AFTER INSERT ON core_user
|
|
159
|
+
WHEN NEW.kind = 'human' AND (SELECT COUNT(*) FROM core_user WHERE kind = 'human') = 1
|
|
160
|
+
BEGIN
|
|
161
|
+
UPDATE core_user SET role = 'admin' WHERE id = NEW.id;
|
|
162
|
+
END;
|
|
163
|
+
|
|
164
|
+
DROP VIEW crm_users;
|
|
165
|
+
CREATE VIEW crm_users AS
|
|
166
|
+
SELECT u.id, u.name, u.email, u.role, u.image, u.created_at
|
|
167
|
+
FROM core_user u
|
|
168
|
+
WHERE u.kind = 'human';
|
|
169
|
+
|
|
170
|
+
-- 3. Base read views for core objects. crm_people, crm_organisations, crm_activities and
|
|
171
|
+
-- crm_tasks become `SELECT b.* FROM core_base_<x> b` plus LEFT JOINs to extension side
|
|
172
|
+
-- tables, regenerated by the reconciler. Core migrations change core_base_* (never the crm_*
|
|
173
|
+
-- views of these four objects); SQLite expands `b.*` when the view is used, so new core
|
|
174
|
+
-- columns appear in crm_* without a reconcile. query.sql cannot name core_base_* (core_
|
|
175
|
+
-- prefix), only the crm_* views over them.
|
|
176
|
+
-- The bodies are the 0007 definitions.
|
|
177
|
+
CREATE VIEW core_base_people AS
|
|
178
|
+
SELECT
|
|
179
|
+
person.id,
|
|
180
|
+
person.display_name,
|
|
181
|
+
person.given_name,
|
|
182
|
+
person.family_name,
|
|
183
|
+
(SELECT e.email FROM core_person_emails e
|
|
184
|
+
WHERE e.person_id = person.id AND e.is_primary = 1) AS primary_email,
|
|
185
|
+
(SELECT json_group_array(e.email) FROM
|
|
186
|
+
(SELECT email FROM core_person_emails WHERE person_id = person.id ORDER BY position) e) AS emails,
|
|
187
|
+
(SELECT ph.number FROM core_person_phones ph
|
|
188
|
+
WHERE ph.person_id = person.id AND ph.is_primary = 1) AS primary_phone,
|
|
189
|
+
aff.id AS primary_affiliation_id,
|
|
190
|
+
aff.title AS job_title,
|
|
191
|
+
aff.organisation_id AS organisation_id,
|
|
192
|
+
org.name AS organisation_name,
|
|
193
|
+
person.location,
|
|
194
|
+
person.timezone,
|
|
195
|
+
person.owner_id,
|
|
196
|
+
person.avatar_key,
|
|
197
|
+
(SELECT json_group_array(t.name) FROM
|
|
198
|
+
(SELECT tg.name FROM core_record_tags rt JOIN core_tags tg ON tg.id = rt.tag_id
|
|
199
|
+
WHERE rt.object_type = 'person' AND rt.record_id = person.id ORDER BY tg.name) t) AS tags,
|
|
200
|
+
person.created_at,
|
|
201
|
+
person.updated_at
|
|
202
|
+
FROM core_person person
|
|
203
|
+
LEFT JOIN core_affiliation aff
|
|
204
|
+
ON aff.person_id = person.id AND aff.is_primary = 1 AND aff.deleted_at IS NULL
|
|
205
|
+
LEFT JOIN core_organisation org
|
|
206
|
+
ON org.id = aff.organisation_id AND org.deleted_at IS NULL
|
|
207
|
+
WHERE person.deleted_at IS NULL;
|
|
208
|
+
|
|
209
|
+
CREATE VIEW core_base_organisations AS
|
|
210
|
+
SELECT
|
|
211
|
+
organisation.id,
|
|
212
|
+
organisation.name,
|
|
213
|
+
(SELECT d.domain FROM core_organisation_domains d
|
|
214
|
+
WHERE d.organisation_id = organisation.id AND d.is_primary = 1) AS primary_domain,
|
|
215
|
+
(SELECT json_group_array(d.domain) FROM
|
|
216
|
+
(SELECT domain FROM core_organisation_domains WHERE organisation_id = organisation.id
|
|
217
|
+
ORDER BY position) d) AS domains,
|
|
218
|
+
organisation.website,
|
|
219
|
+
organisation.industry,
|
|
220
|
+
organisation.parent_id,
|
|
221
|
+
parent.name AS parent_name,
|
|
222
|
+
organisation.owner_id,
|
|
223
|
+
organisation.avatar_key,
|
|
224
|
+
(SELECT COUNT(*) FROM core_affiliation a JOIN core_person p ON p.id = a.person_id
|
|
225
|
+
WHERE a.organisation_id = organisation.id AND a.is_current = 1
|
|
226
|
+
AND a.deleted_at IS NULL AND p.deleted_at IS NULL) AS current_people_count,
|
|
227
|
+
(SELECT json_group_array(t.name) FROM
|
|
228
|
+
(SELECT tg.name FROM core_record_tags rt JOIN core_tags tg ON tg.id = rt.tag_id
|
|
229
|
+
WHERE rt.object_type = 'organisation' AND rt.record_id = organisation.id
|
|
230
|
+
ORDER BY tg.name) t) AS tags,
|
|
231
|
+
organisation.created_at,
|
|
232
|
+
organisation.updated_at
|
|
233
|
+
FROM core_organisation organisation
|
|
234
|
+
LEFT JOIN core_organisation parent
|
|
235
|
+
ON parent.id = organisation.parent_id AND parent.deleted_at IS NULL
|
|
236
|
+
WHERE organisation.deleted_at IS NULL;
|
|
237
|
+
|
|
238
|
+
CREATE VIEW core_base_activities AS
|
|
239
|
+
SELECT
|
|
240
|
+
activity.id,
|
|
241
|
+
activity.type,
|
|
242
|
+
activity.subject,
|
|
243
|
+
activity.body,
|
|
244
|
+
activity.payload,
|
|
245
|
+
activity.occurred_at,
|
|
246
|
+
activity.author_user_id,
|
|
247
|
+
activity.author_actor,
|
|
248
|
+
activity.created_at,
|
|
249
|
+
activity.updated_at
|
|
250
|
+
FROM core_activity activity
|
|
251
|
+
WHERE activity.deleted_at IS NULL;
|
|
252
|
+
|
|
253
|
+
CREATE VIEW core_base_tasks AS
|
|
254
|
+
SELECT
|
|
255
|
+
task.id,
|
|
256
|
+
task.title,
|
|
257
|
+
task.notes,
|
|
258
|
+
task.due_at,
|
|
259
|
+
task.status,
|
|
260
|
+
task.completed_at,
|
|
261
|
+
task.assignee_id,
|
|
262
|
+
task.created_by,
|
|
263
|
+
task.created_at,
|
|
264
|
+
task.updated_at
|
|
265
|
+
FROM core_task task
|
|
266
|
+
WHERE task.deleted_at IS NULL;
|
|
267
|
+
|
|
268
|
+
DROP VIEW crm_people;
|
|
269
|
+
CREATE VIEW crm_people AS SELECT b.* FROM core_base_people b;
|
|
270
|
+
DROP VIEW crm_organisations;
|
|
271
|
+
CREATE VIEW crm_organisations AS SELECT b.* FROM core_base_organisations b;
|
|
272
|
+
DROP VIEW crm_activities;
|
|
273
|
+
CREATE VIEW crm_activities AS SELECT b.* FROM core_base_activities b;
|
|
274
|
+
DROP VIEW crm_tasks;
|
|
275
|
+
CREATE VIEW crm_tasks AS SELECT b.* FROM core_base_tasks b;
|
|
276
|
+
|
|
277
|
+
-- 4. Extension state (ADR 0025). One row per installed extension; its manifest is the JSON the
|
|
278
|
+
-- sandbox produced from its source at `commit_oid` in the extension workspace.
|
|
279
|
+
CREATE TABLE core_extensions (
|
|
280
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
281
|
+
source TEXT NOT NULL CHECK (source IN ('shipped', 'runtime')),
|
|
282
|
+
status TEXT NOT NULL CHECK (status IN ('active', 'disabled', 'quarantined')),
|
|
283
|
+
status_reason TEXT,
|
|
284
|
+
version TEXT NOT NULL,
|
|
285
|
+
commit_oid TEXT NOT NULL,
|
|
286
|
+
-- The last version that ran without failing, offered for rollback on quarantine.
|
|
287
|
+
last_good_commit TEXT,
|
|
288
|
+
manifest TEXT NOT NULL CHECK (json_valid(manifest)),
|
|
289
|
+
-- Consecutive failed runs; 3 quarantines the extension.
|
|
290
|
+
failure_count INTEGER NOT NULL DEFAULT 0,
|
|
291
|
+
-- The extension principal (core_user kind 'extension').
|
|
292
|
+
user_id TEXT NOT NULL REFERENCES core_user (id),
|
|
293
|
+
installed_at TEXT NOT NULL,
|
|
294
|
+
installed_by TEXT REFERENCES core_user (id) ON DELETE SET NULL,
|
|
295
|
+
updated_at TEXT NOT NULL
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
-- Every version ever installed (a commit in the workspace), for history and rollback.
|
|
299
|
+
CREATE TABLE core_extension_versions (
|
|
300
|
+
extension_id TEXT NOT NULL REFERENCES core_extensions (id) ON DELETE CASCADE,
|
|
301
|
+
commit_oid TEXT NOT NULL,
|
|
302
|
+
version TEXT NOT NULL,
|
|
303
|
+
manifest TEXT NOT NULL CHECK (json_valid(manifest)),
|
|
304
|
+
installed_at TEXT NOT NULL,
|
|
305
|
+
installed_by TEXT REFERENCES core_user (id) ON DELETE SET NULL,
|
|
306
|
+
PRIMARY KEY (extension_id, commit_oid)
|
|
307
|
+
);
|
|
308
|
+
|
|
309
|
+
-- Run history: one row per execution of extension code (tool calls, manifest evaluation).
|
|
310
|
+
CREATE TABLE core_extension_runs (
|
|
311
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
312
|
+
extension_id TEXT NOT NULL,
|
|
313
|
+
commit_oid TEXT,
|
|
314
|
+
kind TEXT NOT NULL CHECK (kind IN ('tool', 'manifest', 'automation')),
|
|
315
|
+
name TEXT,
|
|
316
|
+
actor TEXT,
|
|
317
|
+
user_id TEXT,
|
|
318
|
+
started_at TEXT NOT NULL,
|
|
319
|
+
duration_ms INTEGER NOT NULL,
|
|
320
|
+
outcome TEXT NOT NULL CHECK (outcome IN ('ok', 'error', 'timeout')),
|
|
321
|
+
error TEXT CHECK (error IS NULL OR json_valid(error)),
|
|
322
|
+
logs TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(logs)),
|
|
323
|
+
operations TEXT NOT NULL DEFAULT '[]' CHECK (json_valid(operations))
|
|
324
|
+
);
|
|
325
|
+
CREATE INDEX core_extension_runs_ext_idx ON core_extension_runs (extension_id, started_at);
|
|
326
|
+
|
|
327
|
+
-- codemode:storage: per-extension key-value storage.
|
|
328
|
+
CREATE TABLE core_extension_kv (
|
|
329
|
+
extension_id TEXT NOT NULL,
|
|
330
|
+
key TEXT NOT NULL CHECK (length(key) BETWEEN 1 AND 512),
|
|
331
|
+
value TEXT NOT NULL CHECK (json_valid(value)),
|
|
332
|
+
updated_at TEXT NOT NULL,
|
|
333
|
+
PRIMARY KEY (extension_id, key)
|
|
334
|
+
);
|
|
335
|
+
|
|
336
|
+
-- Reconciler state (ADR 0024): the physical x_* tables and columns behind declared objects and
|
|
337
|
+
-- fields. Removed objects and fields are `hidden` (data kept) until dropped explicitly.
|
|
338
|
+
CREATE TABLE core_extension_objects (
|
|
339
|
+
name TEXT PRIMARY KEY NOT NULL,
|
|
340
|
+
extension_id TEXT NOT NULL,
|
|
341
|
+
local_name TEXT NOT NULL,
|
|
342
|
+
table_name TEXT NOT NULL UNIQUE,
|
|
343
|
+
status TEXT NOT NULL CHECK (status IN ('active', 'hidden')),
|
|
344
|
+
hidden_at TEXT,
|
|
345
|
+
definition TEXT NOT NULL CHECK (json_valid(definition)),
|
|
346
|
+
created_at TEXT NOT NULL,
|
|
347
|
+
updated_at TEXT NOT NULL
|
|
348
|
+
);
|
|
349
|
+
|
|
350
|
+
CREATE TABLE core_extension_fields (
|
|
351
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
352
|
+
extension_id TEXT NOT NULL,
|
|
353
|
+
-- Registry name of the object the field belongs to: `organisation`, `example.project`.
|
|
354
|
+
object TEXT NOT NULL,
|
|
355
|
+
-- Name as declared in the extension.
|
|
356
|
+
local_name TEXT NOT NULL,
|
|
357
|
+
-- Registry field name: the local name on the extension's own objects, `<ext>.<name>` elsewhere.
|
|
358
|
+
name TEXT NOT NULL,
|
|
359
|
+
table_name TEXT NOT NULL,
|
|
360
|
+
-- NULL for formula fields (computed in the crm_* view).
|
|
361
|
+
column_name TEXT,
|
|
362
|
+
type TEXT NOT NULL,
|
|
363
|
+
definition TEXT NOT NULL CHECK (json_valid(definition)),
|
|
364
|
+
status TEXT NOT NULL CHECK (status IN ('active', 'hidden')),
|
|
365
|
+
hidden_at TEXT,
|
|
366
|
+
created_at TEXT NOT NULL,
|
|
367
|
+
updated_at TEXT NOT NULL
|
|
368
|
+
);
|
|
369
|
+
CREATE UNIQUE INDEX core_extension_fields_name_uq ON core_extension_fields (object, name);
|
|
370
|
+
CREATE UNIQUE INDEX core_extension_fields_column_uq ON core_extension_fields (table_name, column_name)
|
|
371
|
+
WHERE column_name IS NOT NULL;
|
|
372
|
+
|
|
373
|
+
UPDATE core_meta SET value = '20' WHERE key = 'schema_version';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
-- Migration 0030: extension proposals (stage 3b, ADR 0041).
|
|
2
|
+
--
|
|
3
|
+
-- `extension.propose` commits an extension's working copy and queues one pending action
|
|
4
|
+
-- (operation `extension.apply`, risk `extension`) in the caller's change set. What a reviewer
|
|
5
|
+
-- needs to judge it lives here, keyed by that pending action: the per-file git diff from the
|
|
6
|
+
-- installed version, the host-computed impact (schema changes, permissions requested or
|
|
7
|
+
-- widened, host modules, triggers), the check result and the AI-written summary (model role
|
|
8
|
+
-- `build`). Core tables only; written by trusted host code.
|
|
9
|
+
CREATE TABLE core_extension_proposals (
|
|
10
|
+
action_id TEXT PRIMARY KEY NOT NULL REFERENCES core_pending_action (id),
|
|
11
|
+
extension_id TEXT NOT NULL,
|
|
12
|
+
commit_oid TEXT NOT NULL,
|
|
13
|
+
-- The installed commit the proposal was diffed against (null: not installed yet).
|
|
14
|
+
base_commit TEXT,
|
|
15
|
+
source TEXT NOT NULL CHECK (source IN ('shipped', 'runtime')),
|
|
16
|
+
version TEXT NOT NULL,
|
|
17
|
+
message TEXT NOT NULL,
|
|
18
|
+
diff TEXT NOT NULL CHECK (json_valid(diff)),
|
|
19
|
+
impact TEXT NOT NULL CHECK (json_valid(impact)),
|
|
20
|
+
check_result TEXT NOT NULL CHECK (json_valid(check_result)),
|
|
21
|
+
-- The `build` model role's plain-English summary, or null when no AI is configured.
|
|
22
|
+
ai_summary TEXT,
|
|
23
|
+
ai_model TEXT,
|
|
24
|
+
created_by TEXT NOT NULL,
|
|
25
|
+
created_at TEXT NOT NULL
|
|
26
|
+
);
|
|
27
|
+
CREATE INDEX core_extension_proposals_ext_idx ON core_extension_proposals (extension_id, created_at);
|
|
28
|
+
|
|
29
|
+
UPDATE core_meta SET value = '30' WHERE key = 'schema_version';
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
-- Migration 0050: partial Trash indexes, so list pages stop scanning every live row
|
|
2
|
+
-- (docs/research/cloudflare-cost-audit.md).
|
|
3
|
+
--
|
|
4
|
+
-- The 0003/0004 indexes on deleted_at covered every row. D1 databases carry no ANALYZE statistics,
|
|
5
|
+
-- so the planner took `deleted_at IS NULL` (true for nearly every row) as selective. It walked
|
|
6
|
+
-- the whole table through that index, computed the crm_* views' correlated subqueries for every
|
|
7
|
+
-- row, then sorted. The display-name, name and occurred-at indexes went unused, and so did the
|
|
8
|
+
-- primary key for `id IN (...)` lookups.
|
|
9
|
+
--
|
|
10
|
+
-- A default 50-row person.list on 20k people read about 82k rows. search.global's hydration
|
|
11
|
+
-- read about 20k rows per query.
|
|
12
|
+
--
|
|
13
|
+
-- Trash queries always say `deleted_at IS NOT NULL` or `deleted_at < ?`, so partial indexes still
|
|
14
|
+
-- serve them, and the planner can no longer pick them for live-row reads.
|
|
15
|
+
|
|
16
|
+
DROP INDEX core_person_deleted_idx;
|
|
17
|
+
CREATE INDEX core_person_deleted_idx ON core_person (deleted_at) WHERE deleted_at IS NOT NULL;
|
|
18
|
+
|
|
19
|
+
DROP INDEX core_organisation_deleted_idx;
|
|
20
|
+
CREATE INDEX core_organisation_deleted_idx ON core_organisation (deleted_at)
|
|
21
|
+
WHERE deleted_at IS NOT NULL;
|
|
22
|
+
|
|
23
|
+
DROP INDEX core_activity_deleted_idx;
|
|
24
|
+
CREATE INDEX core_activity_deleted_idx ON core_activity (deleted_at) WHERE deleted_at IS NOT NULL;
|
|
25
|
+
|
|
26
|
+
DROP INDEX core_task_deleted_idx;
|
|
27
|
+
CREATE INDEX core_task_deleted_idx ON core_task (deleted_at) WHERE deleted_at IS NOT NULL;
|
|
28
|
+
|
|
29
|
+
-- task.list's default order (due date, then id) had no index, so every page sorted all tasks.
|
|
30
|
+
CREATE INDEX core_task_due_at_idx ON core_task (due_at, id);
|
|
31
|
+
|
|
32
|
+
UPDATE core_meta SET value = '50' WHERE key = 'schema_version';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
-- Migration 0060: hourly read counts instead of one audit row per non-human read (ADR 0090,
|
|
2
|
+
-- docs/research/cloudflare-cost-audit.md finding F6, decision D3).
|
|
3
|
+
--
|
|
4
|
+
-- Before this, every successful read by an MCP client, extension, automation or the assistant
|
|
5
|
+
-- wrote a core_audit row: about 6 rows written (5 indexes) and ~1 KB stored per read. Successful
|
|
6
|
+
-- non-human reads are now counted here instead, one row per hour, actor, user, caller and
|
|
7
|
+
-- operation, upserted. Writes, refusals and failed reads still write full core_audit rows.
|
|
8
|
+
-- Human reads are still not recorded at all.
|
|
9
|
+
--
|
|
10
|
+
-- Rows hold ids and counts only (no inputs), so an erase (ADR 0019) has nothing to redact here.
|
|
11
|
+
-- They are not append-only: an upsert has to update the count. The audit retention purge
|
|
12
|
+
-- (migration 0061) deletes hours older than the retention window.
|
|
13
|
+
CREATE TABLE core_audit_read_counts (
|
|
14
|
+
-- The UTC hour, 'YYYY-MM-DDTHH:00:00.000Z'.
|
|
15
|
+
hour TEXT NOT NULL,
|
|
16
|
+
actor TEXT NOT NULL CHECK (actor IN ('human', 'assistant', 'mcp', 'automation', 'extension')),
|
|
17
|
+
user_id TEXT NOT NULL,
|
|
18
|
+
-- Who exactly, within the actor: the connected app (cap_…) for MCP, the extension id, the
|
|
19
|
+
-- automation id, else the MCP client id, else ''.
|
|
20
|
+
caller TEXT NOT NULL DEFAULT '',
|
|
21
|
+
operation TEXT NOT NULL,
|
|
22
|
+
count INTEGER NOT NULL DEFAULT 0,
|
|
23
|
+
-- How many of them saw the session's simulated pending changes (ADR 0032).
|
|
24
|
+
simulated INTEGER NOT NULL DEFAULT 0,
|
|
25
|
+
first_at TEXT NOT NULL,
|
|
26
|
+
last_at TEXT NOT NULL,
|
|
27
|
+
PRIMARY KEY (hour, actor, user_id, caller, operation)
|
|
28
|
+
) WITHOUT ROWID;
|
|
29
|
+
CREATE INDEX core_audit_read_counts_user_idx ON core_audit_read_counts (user_id, hour);
|
|
30
|
+
|
|
31
|
+
UPDATE core_meta SET value = '60' WHERE key = 'schema_version';
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
-- Migration 0061: audit retention, a sanctioned purge path through the append-only triggers
|
|
2
|
+
-- (ADR 0090; amends ADR 0017, in the spirit of ADR 0019's redaction markers).
|
|
3
|
+
--
|
|
4
|
+
-- Retention is an org setting, `audit_retention` = {"years": null | 1 | 2 | 7}. null (the
|
|
5
|
+
-- default, and the meaning of a missing row) keeps the audit log forever. Only a human admin can
|
|
6
|
+
-- change it (`audit.setRetention`, risk destructive).
|
|
7
|
+
--
|
|
8
|
+
-- DELETE on core_audit, core_audit_changes and core_audit_redactions stays refused unless
|
|
9
|
+
-- retention is not "forever" AND the row is older than the cutoff of a purge marker in
|
|
10
|
+
-- core_audit_purges. A marker can only be inserted with the current setting and a cutoff at least
|
|
11
|
+
-- that many years before the database clock, and markers are append-only. So:
|
|
12
|
+
-- - an ordinary DELETE of audit rows inside the retention window still fails "append-only";
|
|
13
|
+
-- - nothing can purge what the admin's setting keeps, whatever code inserts the marker;
|
|
14
|
+
-- - every purge leaves a permanent marker, and its own audit row (`audit.purge`).
|
|
15
|
+
CREATE TABLE core_audit_purges (
|
|
16
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
17
|
+
-- Audit rows with `at` before this may be deleted, with their change rows and markers.
|
|
18
|
+
cutoff TEXT NOT NULL,
|
|
19
|
+
-- The retention setting in force when the marker was written.
|
|
20
|
+
retention_years INTEGER NOT NULL CHECK (retention_years IN (1, 2, 7)),
|
|
21
|
+
-- The audit.purge invocation's own audit row, written in the same batch.
|
|
22
|
+
audit_id TEXT,
|
|
23
|
+
user_id TEXT,
|
|
24
|
+
at TEXT NOT NULL
|
|
25
|
+
);
|
|
26
|
+
CREATE INDEX core_audit_purges_cutoff_idx ON core_audit_purges (cutoff);
|
|
27
|
+
|
|
28
|
+
CREATE TRIGGER core_audit_purges_no_update BEFORE UPDATE ON core_audit_purges
|
|
29
|
+
BEGIN
|
|
30
|
+
SELECT RAISE(ABORT, 'core_audit_purges is append-only');
|
|
31
|
+
END;
|
|
32
|
+
|
|
33
|
+
CREATE TRIGGER core_audit_purges_no_delete BEFORE DELETE ON core_audit_purges
|
|
34
|
+
BEGIN
|
|
35
|
+
SELECT RAISE(ABORT, 'core_audit_purges is append-only');
|
|
36
|
+
END;
|
|
37
|
+
|
|
38
|
+
-- A marker must match the current setting and lie at least that many years in the past.
|
|
39
|
+
CREATE TRIGGER core_audit_purges_check BEFORE INSERT ON core_audit_purges
|
|
40
|
+
WHEN NOT (
|
|
41
|
+
NEW.retention_years IS (
|
|
42
|
+
SELECT json_extract(value, '$.years') FROM core_settings WHERE key = 'audit_retention'
|
|
43
|
+
)
|
|
44
|
+
AND NEW.cutoff <= strftime('%Y-%m-%dT%H:%M:%fZ', 'now', '-' || NEW.retention_years || ' years')
|
|
45
|
+
)
|
|
46
|
+
BEGIN
|
|
47
|
+
SELECT RAISE(ABORT, 'audit purge cutoff is inside the retention window');
|
|
48
|
+
END;
|
|
49
|
+
|
|
50
|
+
-- Guards: replace the unconditional DELETE refusals (migrations 0005, 0008, 0020).
|
|
51
|
+
|
|
52
|
+
DROP TRIGGER core_audit_no_delete;
|
|
53
|
+
CREATE TRIGGER core_audit_no_delete BEFORE DELETE ON core_audit
|
|
54
|
+
WHEN NOT (
|
|
55
|
+
(SELECT json_extract(value, '$.years') FROM core_settings WHERE key = 'audit_retention')
|
|
56
|
+
IS NOT NULL
|
|
57
|
+
AND OLD.at < coalesce((SELECT max(cutoff) FROM core_audit_purges), '')
|
|
58
|
+
)
|
|
59
|
+
BEGIN
|
|
60
|
+
SELECT RAISE(ABORT, 'core_audit is append-only');
|
|
61
|
+
END;
|
|
62
|
+
|
|
63
|
+
DROP TRIGGER core_audit_changes_no_delete;
|
|
64
|
+
CREATE TRIGGER core_audit_changes_no_delete BEFORE DELETE ON core_audit_changes
|
|
65
|
+
WHEN NOT (
|
|
66
|
+
(SELECT json_extract(value, '$.years') FROM core_settings WHERE key = 'audit_retention')
|
|
67
|
+
IS NOT NULL
|
|
68
|
+
AND OLD.at < coalesce((SELECT max(cutoff) FROM core_audit_purges), '')
|
|
69
|
+
)
|
|
70
|
+
BEGIN
|
|
71
|
+
SELECT RAISE(ABORT, 'core_audit_changes is append-only');
|
|
72
|
+
END;
|
|
73
|
+
|
|
74
|
+
-- A redaction marker goes with the audit or change row it redacted (its foreign key would
|
|
75
|
+
-- otherwise keep that row forever). The erase's own audit row is newer and stays.
|
|
76
|
+
DROP TRIGGER core_audit_redactions_no_delete;
|
|
77
|
+
CREATE TRIGGER core_audit_redactions_no_delete BEFORE DELETE ON core_audit_redactions
|
|
78
|
+
WHEN NOT (
|
|
79
|
+
(SELECT json_extract(value, '$.years') FROM core_settings WHERE key = 'audit_retention')
|
|
80
|
+
IS NOT NULL
|
|
81
|
+
AND coalesce(
|
|
82
|
+
(SELECT a.at FROM core_audit a WHERE a.id = OLD.audit_id),
|
|
83
|
+
(SELECT c.at FROM core_audit_changes c WHERE c.id = OLD.change_id)
|
|
84
|
+
) < coalesce((SELECT max(cutoff) FROM core_audit_purges), '')
|
|
85
|
+
)
|
|
86
|
+
BEGIN
|
|
87
|
+
SELECT RAISE(ABORT, 'core_audit_redactions is append-only');
|
|
88
|
+
END;
|
|
89
|
+
|
|
90
|
+
-- The default: keep forever.
|
|
91
|
+
INSERT INTO core_settings (key, value, updated_at)
|
|
92
|
+
VALUES ('audit_retention', '{"years":null}', strftime('%Y-%m-%dT%H:%M:%fZ', 'now'))
|
|
93
|
+
ON CONFLICT (key) DO NOTHING;
|
|
94
|
+
|
|
95
|
+
UPDATE core_meta SET value = '61' WHERE key = 'schema_version';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
-- Migration 0062: run history retention (ADR 0090, cost audit F16). The daily cron deletes
|
|
2
|
+
-- successful extension and automation runs after 30 days and failed ones after 90. This index
|
|
3
|
+
-- lets each chunk find the oldest runs of an outcome without scanning the table.
|
|
4
|
+
CREATE INDEX core_extension_runs_outcome_idx ON core_extension_runs (outcome, started_at);
|
|
5
|
+
|
|
6
|
+
UPDATE core_meta SET value = '62' WHERE key = 'schema_version';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
-- Migration 0063: compacting decided pending actions (ADR 0090, cost audit F17).
|
|
2
|
+
--
|
|
3
|
+
-- 30 days after a pending action was decided (applied, failed, skipped, rejected) or expired,
|
|
4
|
+
-- the daily cron replaces its input, preview and simulated records with empty values and keeps
|
|
5
|
+
-- a summary, `summary` = {"records": n, "actions": {"update": n, ...}, "inputBytes": n, ...},
|
|
6
|
+
-- next to the columns that already say what, who and when (operation, risk, actor, user_id,
|
|
7
|
+
-- status, decided_by, decided_at, result, error). `compacted_at` says when that happened.
|
|
8
|
+
ALTER TABLE core_pending_action ADD COLUMN compacted_at TEXT;
|
|
9
|
+
ALTER TABLE core_pending_action ADD COLUMN summary TEXT
|
|
10
|
+
CHECK (summary IS NULL OR json_valid(summary));
|
|
11
|
+
|
|
12
|
+
-- Compaction candidates, oldest first: decided or expired, not yet compacted. Expired actions
|
|
13
|
+
-- have no decided_at, so the key is the decision time, else the expiry.
|
|
14
|
+
CREATE INDEX core_pending_action_compact_idx
|
|
15
|
+
ON core_pending_action (coalesce(decided_at, expires_at))
|
|
16
|
+
WHERE compacted_at IS NULL AND status <> 'pending';
|
|
17
|
+
|
|
18
|
+
UPDATE core_meta SET value = '63' WHERE key = 'schema_version';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
-- Migration 0064: MCP rate limits moved to the Workers Rate Limiting binding (ADR 0091, cost
|
|
2
|
+
-- audit F7, decision D4). Nothing writes the per-request D1 counter any more, so it is dropped.
|
|
3
|
+
DROP INDEX core_rate_limit_window_idx;
|
|
4
|
+
DROP TABLE core_rate_limit;
|
|
5
|
+
|
|
6
|
+
UPDATE core_meta SET value = '64' WHERE key = 'schema_version';
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
-- Migration 0065: the storage gauge's cache (ADR 0090). The daily cron, or an admin's refresh,
|
|
2
|
+
-- records the database size and the row counts of the big tables here, so Settings → Storage
|
|
3
|
+
-- reads one row instead of counting millions on every page view.
|
|
4
|
+
CREATE TABLE core_storage_stats (
|
|
5
|
+
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
6
|
+
-- D1's meta.size_after in bytes, or null where the runtime does not report it.
|
|
7
|
+
size_bytes INTEGER,
|
|
8
|
+
-- JSON {"core_audit": n, ...}.
|
|
9
|
+
tables TEXT NOT NULL CHECK (json_valid(tables)),
|
|
10
|
+
measured_at TEXT NOT NULL
|
|
11
|
+
);
|
|
12
|
+
|
|
13
|
+
UPDATE core_meta SET value = '65' WHERE key = 'schema_version';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
-- Migration 0070: drop an unused index (docs/research/cloudflare-cost-audit.md, ADR 0043).
|
|
2
|
+
--
|
|
3
|
+
-- core_extension_proposals is read by action_id (its primary key) for the approval screen. Nothing
|
|
4
|
+
-- queries it by (extension_id, created_at), so the index only cost an extra row written per
|
|
5
|
+
-- proposal and its storage.
|
|
6
|
+
|
|
7
|
+
DROP INDEX IF EXISTS core_extension_proposals_ext_idx;
|
|
8
|
+
|
|
9
|
+
UPDATE core_meta SET value = '70' WHERE key = 'schema_version';
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
-- Migration 0080: view types and dashboards (stage 4a, ADR 0027, ADR 0056, ADR 0057).
|
|
2
|
+
--
|
|
3
|
+
-- 1. Saved views can be shared with the org. A view spec is now a discriminated union
|
|
4
|
+
-- (`table | board | calendar | gallery`); stored stage-1 table specs are already valid
|
|
5
|
+
-- (`{ "type": "table", ... }`), so no data changes.
|
|
6
|
+
ALTER TABLE core_views ADD COLUMN shared INTEGER NOT NULL DEFAULT 0 CHECK (shared IN (0, 1));
|
|
7
|
+
CREATE INDEX core_views_shared_object_idx ON core_views (object, position) WHERE shared = 1;
|
|
8
|
+
|
|
9
|
+
-- 2. Dashboards: a title, filters, a layout grid and widgets (`spec`, JSON), owned by a user and
|
|
10
|
+
-- private to them unless shared. The shipped Overview dashboard lives in code, not here.
|
|
11
|
+
CREATE TABLE core_dashboards (
|
|
12
|
+
id TEXT PRIMARY KEY NOT NULL,
|
|
13
|
+
owner_id TEXT NOT NULL REFERENCES core_user (id) ON DELETE CASCADE,
|
|
14
|
+
title TEXT NOT NULL CHECK (title <> ''),
|
|
15
|
+
description TEXT,
|
|
16
|
+
shared INTEGER NOT NULL DEFAULT 0 CHECK (shared IN (0, 1)),
|
|
17
|
+
spec TEXT NOT NULL CHECK (json_valid(spec)),
|
|
18
|
+
created_at TEXT NOT NULL,
|
|
19
|
+
updated_at TEXT NOT NULL
|
|
20
|
+
);
|
|
21
|
+
CREATE INDEX core_dashboards_owner_idx ON core_dashboards (owner_id, title);
|
|
22
|
+
CREATE INDEX core_dashboards_shared_idx ON core_dashboards (title) WHERE shared = 1;
|
|
23
|
+
|
|
24
|
+
-- 3. Each user's Home: the dashboard rendered at `/`. No row means the shipped Overview.
|
|
25
|
+
CREATE TABLE core_user_home (
|
|
26
|
+
user_id TEXT PRIMARY KEY NOT NULL REFERENCES core_user (id) ON DELETE CASCADE,
|
|
27
|
+
dashboard_id TEXT NOT NULL,
|
|
28
|
+
updated_at TEXT NOT NULL
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
-- 4. Typed extension settings (ADR 0058): one JSON object per extension, validated against the
|
|
32
|
+
-- `settings` its manifest declares.
|
|
33
|
+
CREATE TABLE core_extension_settings (
|
|
34
|
+
extension_id TEXT PRIMARY KEY NOT NULL,
|
|
35
|
+
value TEXT NOT NULL CHECK (json_valid(value)),
|
|
36
|
+
updated_at TEXT NOT NULL,
|
|
37
|
+
updated_by TEXT REFERENCES core_user (id) ON DELETE SET NULL
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
-- 5. Indexes the Overview dashboard's widgets need (ADR 0057: every widget query walks an
|
|
41
|
+
-- index). Partial on live rows, so Trash reads never pick them (see migration 0050).
|
|
42
|
+
CREATE INDEX core_person_created_idx ON core_person (created_at) WHERE deleted_at IS NULL;
|
|
43
|
+
CREATE INDEX core_organisation_created_idx ON core_organisation (created_at)
|
|
44
|
+
WHERE deleted_at IS NULL;
|
|
45
|
+
|
|
46
|
+
UPDATE core_meta SET value = '80' WHERE key = 'schema_version';
|