@tickernelz/paperclip-pro-db 2026.1011.2 → 2026.1012.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/backup-lib.d.ts +2 -0
- package/dist/backup-lib.d.ts.map +1 -1
- package/dist/backup-lib.js +52 -15
- package/dist/backup-lib.js.map +1 -1
- package/dist/backup-lib.test.js +64 -2
- package/dist/backup-lib.test.js.map +1 -1
- package/dist/client.d.ts.map +1 -1
- package/dist/client.js +81 -42
- package/dist/client.js.map +1 -1
- package/dist/client.test.js +33 -0
- package/dist/client.test.js.map +1 -1
- package/dist/connections-v3-schema-core-migration.test.js +3 -0
- package/dist/connections-v3-schema-core-migration.test.js.map +1 -1
- package/dist/heartbeat-run-issue-privacy-migration.test.d.ts +2 -0
- package/dist/heartbeat-run-issue-privacy-migration.test.d.ts.map +1 -0
- package/dist/heartbeat-run-issue-privacy-migration.test.js +265 -0
- package/dist/heartbeat-run-issue-privacy-migration.test.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/migrations/0315_chunky_chamber.sql +3 -0
- package/dist/migrations/0316_lumpy_maria_hill.sql +157 -0
- package/dist/migrations/0317_colossal_otto_octavius.sql +24 -0
- package/dist/migrations/0318_supreme_garia.sql +1 -0
- package/dist/migrations/0319_curvy_shadow_king.sql +27 -0
- package/dist/migrations/0320_chubby_vin_gonzales.sql +7 -0
- package/dist/migrations/0321_familiar_titania.sql +4 -0
- package/dist/migrations/0322_cool_naoko.sql +3 -0
- package/dist/migrations/0323_whole_steel_serpent.sql +20 -0
- package/dist/migrations/0324_loving_the_hood.sql +77 -0
- package/dist/migrations/0325_agent_commentary.sql +33 -0
- package/dist/migrations/0326_mcp_file_transfers.sql +48 -0
- package/dist/migrations/0327_easy_eternity.sql +232 -0
- package/dist/migrations/0328_private_task_access.sql +278 -0
- package/dist/migrations/0329_private_task_draft_assets.sql +28 -0
- package/dist/migrations/0330_rapid_emma_frost.sql +44 -0
- package/dist/migrations/0331_premium_slayback.sql +81 -0
- package/dist/migrations/0332_messy_famine.sql +189 -0
- package/dist/migrations/0333_strong_blacklash.sql +23 -0
- package/dist/migrations/0334_heavy_captain_midlands.sql +15 -0
- package/dist/migrations/0335_daily_onslaught.sql +16 -0
- package/dist/migrations/0336_acoustic_human_fly.sql +1 -0
- package/dist/migrations/0337_reflective_kree.sql +98 -0
- package/dist/migrations/0338_petite_shockwave.sql +4 -0
- package/dist/migrations/0339_long_franklin_storm.sql +153 -0
- package/dist/migrations/0340_aberrant_machine_man.sql +6 -0
- package/dist/migrations/0341_blushing_screwball.sql +17 -0
- package/dist/migrations/0342_sad_proteus.sql +23 -0
- package/dist/migrations/0343_chunky_mentallo.sql +52 -0
- package/dist/migrations/0344_free_wolf_cub.sql +18 -0
- package/dist/migrations/0345_spooky_white_tiger.sql +7 -0
- package/dist/migrations/0346_panoramic_spyke.sql +74 -0
- package/dist/migrations/0347_little_night_nurse.sql +1 -0
- package/dist/migrations/meta/_journal.json +232 -1
- package/dist/private-task-draft-assets-migration.test.d.ts +2 -0
- package/dist/private-task-draft-assets-migration.test.d.ts.map +1 -0
- package/dist/private-task-draft-assets-migration.test.js +35 -0
- package/dist/private-task-draft-assets-migration.test.js.map +1 -0
- package/dist/question-response-history-migration.test.d.ts +2 -0
- package/dist/question-response-history-migration.test.d.ts.map +1 -0
- package/dist/question-response-history-migration.test.js +53 -0
- package/dist/question-response-history-migration.test.js.map +1 -0
- package/dist/schema/accounting.d.ts +1282 -0
- package/dist/schema/accounting.d.ts.map +1 -0
- package/dist/schema/accounting.js +120 -0
- package/dist/schema/accounting.js.map +1 -0
- package/dist/schema/adapter_auth_sessions.d.ts +2 -2
- package/dist/schema/agent_commentary.d.ts +181 -0
- package/dist/schema/agent_commentary.d.ts.map +1 -0
- package/dist/schema/agent_commentary.js +24 -0
- package/dist/schema/agent_commentary.js.map +1 -0
- package/dist/schema/agent_identity_keys.d.ts +131 -0
- package/dist/schema/agent_identity_keys.d.ts.map +1 -0
- package/dist/schema/agent_identity_keys.js +20 -0
- package/dist/schema/agent_identity_keys.js.map +1 -0
- package/dist/schema/agent_runtime_state.d.ts +2 -2
- package/dist/schema/agent_runtime_state.js +2 -2
- package/dist/schema/agent_runtime_state.js.map +1 -1
- package/dist/schema/agent_wakeup_requests.d.ts.map +1 -1
- package/dist/schema/agent_wakeup_requests.js +6 -0
- package/dist/schema/agent_wakeup_requests.js.map +1 -1
- package/dist/schema/agents.d.ts +132 -2
- package/dist/schema/agents.d.ts.map +1 -1
- package/dist/schema/agents.js +10 -2
- package/dist/schema/agents.js.map +1 -1
- package/dist/schema/ai_connection_defaults.d.ts +2 -2
- package/dist/schema/ai_connection_defaults.js +1 -1
- package/dist/schema/ai_connection_defaults.js.map +1 -1
- package/dist/schema/ai_connection_routing.d.ts +8 -8
- package/dist/schema/ai_provider_defaults.d.ts +2 -2
- package/dist/schema/ai_provider_defaults.js +1 -1
- package/dist/schema/ai_provider_defaults.js.map +1 -1
- package/dist/schema/ai_subscriptions.d.ts +619 -0
- package/dist/schema/ai_subscriptions.d.ts.map +1 -0
- package/dist/schema/ai_subscriptions.js +61 -0
- package/dist/schema/ai_subscriptions.js.map +1 -0
- package/dist/schema/budget_incidents.d.ts +2 -2
- package/dist/schema/budget_incidents.js +3 -3
- package/dist/schema/budget_incidents.js.map +1 -1
- package/dist/schema/budget_policies.d.ts +68 -0
- package/dist/schema/budget_policies.d.ts.map +1 -1
- package/dist/schema/budget_policies.js +7 -1
- package/dist/schema/budget_policies.js.map +1 -1
- package/dist/schema/chat_channels.d.ts +21 -4
- package/dist/schema/chat_channels.d.ts.map +1 -1
- package/dist/schema/chat_channels.js +3 -2
- package/dist/schema/chat_channels.js.map +1 -1
- package/dist/schema/chat_github.d.ts +108 -0
- package/dist/schema/chat_github.d.ts.map +1 -1
- package/dist/schema/chat_github.js +7 -0
- package/dist/schema/chat_github.js.map +1 -1
- package/dist/schema/chat_slack_registrations.d.ts +287 -0
- package/dist/schema/chat_slack_registrations.d.ts.map +1 -0
- package/dist/schema/chat_slack_registrations.js +26 -0
- package/dist/schema/chat_slack_registrations.js.map +1 -0
- package/dist/schema/chat_voice_callbacks.d.ts +128 -0
- package/dist/schema/chat_voice_callbacks.d.ts.map +1 -0
- package/dist/schema/chat_voice_callbacks.js +18 -0
- package/dist/schema/chat_voice_callbacks.js.map +1 -0
- package/dist/schema/chat_voice_inbound.d.ts +461 -0
- package/dist/schema/chat_voice_inbound.d.ts.map +1 -0
- package/dist/schema/chat_voice_inbound.js +45 -0
- package/dist/schema/chat_voice_inbound.js.map +1 -0
- package/dist/schema/chat_voice_reports.d.ts +184 -0
- package/dist/schema/chat_voice_reports.d.ts.map +1 -0
- package/dist/schema/chat_voice_reports.js +24 -0
- package/dist/schema/chat_voice_reports.js.map +1 -0
- package/dist/schema/chat_voice_sessions.d.ts +730 -0
- package/dist/schema/chat_voice_sessions.d.ts.map +1 -0
- package/dist/schema/chat_voice_sessions.js +85 -0
- package/dist/schema/chat_voice_sessions.js.map +1 -0
- package/dist/schema/companies.d.ts +19 -2
- package/dist/schema/companies.d.ts.map +1 -1
- package/dist/schema/companies.js +3 -2
- package/dist/schema/companies.js.map +1 -1
- package/dist/schema/cost_events.d.ts +141 -3
- package/dist/schema/cost_events.d.ts.map +1 -1
- package/dist/schema/cost_events.js +20 -3
- package/dist/schema/cost_events.js.map +1 -1
- package/dist/schema/decision_models.d.ts +579 -0
- package/dist/schema/decision_models.d.ts.map +1 -0
- package/dist/schema/decision_models.js +50 -0
- package/dist/schema/decision_models.js.map +1 -0
- package/dist/schema/dot_runner.d.ts +980 -0
- package/dist/schema/dot_runner.d.ts.map +1 -0
- package/dist/schema/dot_runner.js +82 -0
- package/dist/schema/dot_runner.js.map +1 -0
- package/dist/schema/fast_responses.d.ts +662 -0
- package/dist/schema/fast_responses.d.ts.map +1 -0
- package/dist/schema/fast_responses.js +65 -0
- package/dist/schema/fast_responses.js.map +1 -0
- package/dist/schema/finance_events.d.ts +36 -2
- package/dist/schema/finance_events.d.ts.map +1 -1
- package/dist/schema/finance_events.js +5 -2
- package/dist/schema/finance_events.js.map +1 -1
- package/dist/schema/heartbeat_runs.d.ts +138 -0
- package/dist/schema/heartbeat_runs.d.ts.map +1 -1
- package/dist/schema/heartbeat_runs.js +17 -1
- package/dist/schema/heartbeat_runs.js.map +1 -1
- package/dist/schema/index.d.ts +17 -0
- package/dist/schema/index.d.ts.map +1 -1
- package/dist/schema/index.js +17 -0
- package/dist/schema/index.js.map +1 -1
- package/dist/schema/issue_access_grants.d.ts +161 -0
- package/dist/schema/issue_access_grants.d.ts.map +1 -0
- package/dist/schema/issue_access_grants.js +21 -0
- package/dist/schema/issue_access_grants.js.map +1 -0
- package/dist/schema/issue_comments.d.ts +36 -0
- package/dist/schema/issue_comments.d.ts.map +1 -1
- package/dist/schema/issue_comments.js +2 -0
- package/dist/schema/issue_comments.js.map +1 -1
- package/dist/schema/issues.d.ts +51 -0
- package/dist/schema/issues.d.ts.map +1 -1
- package/dist/schema/issues.js +6 -0
- package/dist/schema/issues.js.map +1 -1
- package/dist/schema/plugin_lifecycle_acknowledgments.d.ts +59 -0
- package/dist/schema/plugin_lifecycle_acknowledgments.d.ts.map +1 -0
- package/dist/schema/plugin_lifecycle_acknowledgments.js +12 -0
- package/dist/schema/plugin_lifecycle_acknowledgments.js.map +1 -0
- package/dist/schema/project_access_members.d.ts +111 -0
- package/dist/schema/project_access_members.d.ts.map +1 -0
- package/dist/schema/project_access_members.js +18 -0
- package/dist/schema/project_access_members.js.map +1 -0
- package/dist/schema/projects.d.ts +51 -0
- package/dist/schema/projects.d.ts.map +1 -1
- package/dist/schema/projects.js +9 -1
- package/dist/schema/projects.js.map +1 -1
- package/dist/schema/public_mcp.d.ts +2100 -0
- package/dist/schema/public_mcp.d.ts.map +1 -0
- package/dist/schema/public_mcp.js +191 -0
- package/dist/schema/public_mcp.js.map +1 -0
- package/dist/schema/resource_lifecycle_events.d.ts +2 -2
- package/dist/schema/resource_lifecycle_events.d.ts.map +1 -1
- package/dist/schema/resource_lifecycle_events.js +2 -1
- package/dist/schema/resource_lifecycle_events.js.map +1 -1
- package/dist/schema/status_cards.d.ts +2 -2
- package/dist/schema/status_cards.js +2 -2
- package/dist/schema/status_cards.js.map +1 -1
- package/dist/schema/tool_access.d.ts +18 -0
- package/dist/schema/tool_access.d.ts.map +1 -1
- package/dist/schema/tool_access.js +3 -3
- package/dist/schema/tool_access.js.map +1 -1
- package/dist/schema/user_company_preferences.d.ts +127 -0
- package/dist/schema/user_company_preferences.d.ts.map +1 -0
- package/dist/schema/user_company_preferences.js +16 -0
- package/dist/schema/user_company_preferences.js.map +1 -0
- package/dist/speko-voice-migration.test.d.ts +2 -0
- package/dist/speko-voice-migration.test.d.ts.map +1 -0
- package/dist/speko-voice-migration.test.js +88 -0
- package/dist/speko-voice-migration.test.js.map +1 -0
- package/dist/work-signals.d.ts +20 -0
- package/dist/work-signals.d.ts.map +1 -0
- package/dist/work-signals.js +123 -0
- package/dist/work-signals.js.map +1 -0
- package/dist/work-signals.test.d.ts +2 -0
- package/dist/work-signals.test.d.ts.map +1 -0
- package/dist/work-signals.test.js +177 -0
- package/dist/work-signals.test.js.map +1 -0
- package/package.json +4 -4
- package/dist/seed.d.ts +0 -2
- package/dist/seed.d.ts.map +0 -1
- package/dist/seed.js +0 -91
- package/dist/seed.js.map +0 -1
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
-- Lock resource writers before the journal foreign key and index take locks.
|
|
2
|
+
LOCK TABLE "agents", "projects" IN SHARE ROW EXCLUSIVE MODE;--> statement-breakpoint
|
|
3
|
+
CREATE TABLE IF NOT EXISTS "plugin_lifecycle_acknowledgments" (
|
|
4
|
+
"plugin_id" uuid NOT NULL,
|
|
5
|
+
"event_id" bigint NOT NULL,
|
|
6
|
+
"acknowledged_at" timestamp with time zone DEFAULT now() NOT NULL,
|
|
7
|
+
CONSTRAINT "plugin_lifecycle_acknowledgments_plugin_id_event_id_pk" PRIMARY KEY("plugin_id","event_id")
|
|
8
|
+
);
|
|
9
|
+
--> statement-breakpoint
|
|
10
|
+
DO $$ BEGIN
|
|
11
|
+
ALTER TABLE "plugin_lifecycle_acknowledgments" ADD CONSTRAINT "plugin_lifecycle_acknowledgments_plugin_id_plugins_id_fk" FOREIGN KEY ("plugin_id") REFERENCES "public"."plugins"("id") ON DELETE cascade ON UPDATE no action;
|
|
12
|
+
EXCEPTION WHEN duplicate_object THEN NULL; END $$;--> statement-breakpoint
|
|
13
|
+
DO $$ BEGIN
|
|
14
|
+
ALTER TABLE "plugin_lifecycle_acknowledgments" ADD CONSTRAINT "plugin_lifecycle_acknowledgments_event_id_resource_lifecycle_events_id_fk" FOREIGN KEY ("event_id") REFERENCES "public"."resource_lifecycle_events"("id") ON DELETE cascade ON UPDATE no action;
|
|
15
|
+
EXCEPTION WHEN duplicate_object THEN NULL; END $$;--> statement-breakpoint
|
|
16
|
+
CREATE INDEX IF NOT EXISTS "plugin_lifecycle_acknowledgments_event_idx" ON "plugin_lifecycle_acknowledgments" USING btree ("event_id");--> statement-breakpoint
|
|
17
|
+
CREATE INDEX IF NOT EXISTS "resource_lifecycle_events_resource_order_idx" ON "resource_lifecycle_events" USING btree ("company_id","resource_type","resource_id","id");--> statement-breakpoint
|
|
18
|
+
-- Older JavaScript backups lost identity metadata. Repair before seeding records.
|
|
19
|
+
DO $$
|
|
20
|
+
DECLARE next_id bigint;
|
|
21
|
+
BEGIN
|
|
22
|
+
IF EXISTS (
|
|
23
|
+
SELECT 1 FROM information_schema.columns
|
|
24
|
+
WHERE table_schema = 'public' AND table_name = 'resource_lifecycle_events'
|
|
25
|
+
AND column_name = 'id' AND is_identity = 'NO' AND column_default IS NULL
|
|
26
|
+
) THEN
|
|
27
|
+
SELECT COALESCE(MAX("id"), 0) + 1 INTO next_id FROM "resource_lifecycle_events";
|
|
28
|
+
EXECUTE format('ALTER TABLE "resource_lifecycle_events" ALTER COLUMN "id" ADD GENERATED ALWAYS AS IDENTITY (START WITH %s)', next_id);
|
|
29
|
+
END IF;
|
|
30
|
+
END $$;--> statement-breakpoint
|
|
31
|
+
-- Seed a one-time current-state baseline before plugins can consume the journal.
|
|
32
|
+
INSERT INTO "resource_lifecycle_events" ("company_id", "resource_type", "resource_id", "action")
|
|
33
|
+
SELECT "company_id", 'agent', "id", 'create'
|
|
34
|
+
FROM "agents"
|
|
35
|
+
WHERE "status" NOT IN ('pending_approval', 'terminated')
|
|
36
|
+
UNION ALL
|
|
37
|
+
SELECT "company_id", 'project', "id", 'create'
|
|
38
|
+
FROM "projects"
|
|
39
|
+
ON CONFLICT ("company_id", "resource_type", "resource_id") WHERE "action" = 'create' DO NOTHING;--> statement-breakpoint
|
|
40
|
+
-- Restore current stop/delete intent without repeating an existing final transition.
|
|
41
|
+
INSERT INTO "resource_lifecycle_events" ("company_id", "resource_type", "resource_id", "action")
|
|
42
|
+
SELECT a."company_id", 'agent', a."id", CASE a."status" WHEN 'paused' THEN 'pause' ELSE 'terminate' END
|
|
43
|
+
FROM "agents" a
|
|
44
|
+
WHERE a."status" IN ('paused', 'terminated')
|
|
45
|
+
AND (SELECT e."action" FROM "resource_lifecycle_events" e
|
|
46
|
+
WHERE e."company_id" = a."company_id" AND e."resource_type" = 'agent'
|
|
47
|
+
AND e."resource_id" = a."id" AND e."action" <> 'create'
|
|
48
|
+
ORDER BY e."id" DESC LIMIT 1)
|
|
49
|
+
IS DISTINCT FROM CASE a."status" WHEN 'paused' THEN 'pause' ELSE 'terminate' END;--> statement-breakpoint
|
|
50
|
+
-- A partial journal may end at pause even though the current agent is running.
|
|
51
|
+
INSERT INTO "resource_lifecycle_events" ("company_id", "resource_type", "resource_id", "action")
|
|
52
|
+
SELECT a."company_id", 'agent', a."id", 'resume'
|
|
53
|
+
FROM "agents" a
|
|
54
|
+
WHERE a."status" NOT IN ('pending_approval', 'paused', 'terminated')
|
|
55
|
+
AND (SELECT e."action" FROM "resource_lifecycle_events" e
|
|
56
|
+
WHERE e."company_id" = a."company_id" AND e."resource_type" = 'agent'
|
|
57
|
+
AND e."resource_id" = a."id" AND e."action" <> 'create'
|
|
58
|
+
ORDER BY e."id" DESC LIMIT 1) = 'pause';
|
|
59
|
+
--> statement-breakpoint
|
|
60
|
+
-- Preserve project archive state, including resources created before capture.
|
|
61
|
+
INSERT INTO "resource_lifecycle_events" ("company_id", "resource_type", "resource_id", "action")
|
|
62
|
+
SELECT p."company_id", 'project', p."id", 'archive'
|
|
63
|
+
FROM "projects" p
|
|
64
|
+
WHERE p."archived_at" IS NOT NULL
|
|
65
|
+
AND (SELECT e."action" FROM "resource_lifecycle_events" e
|
|
66
|
+
WHERE e."company_id" = p."company_id" AND e."resource_type" = 'project'
|
|
67
|
+
AND e."resource_id" = p."id" AND e."action" <> 'create'
|
|
68
|
+
ORDER BY e."id" DESC LIMIT 1) IS DISTINCT FROM 'archive';--> statement-breakpoint
|
|
69
|
+
-- A partial journal may end at archive even though the project was restored.
|
|
70
|
+
INSERT INTO "resource_lifecycle_events" ("company_id", "resource_type", "resource_id", "action")
|
|
71
|
+
SELECT p."company_id", 'project', p."id", 'update'
|
|
72
|
+
FROM "projects" p
|
|
73
|
+
WHERE p."archived_at" IS NULL
|
|
74
|
+
AND (SELECT e."action" FROM "resource_lifecycle_events" e
|
|
75
|
+
WHERE e."company_id" = p."company_id" AND e."resource_type" = 'project'
|
|
76
|
+
AND e."resource_id" = p."id" AND e."action" <> 'create'
|
|
77
|
+
ORDER BY e."id" DESC LIMIT 1) = 'archive';
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
-- Internal feedback only; no backfill or external delivery.
|
|
2
|
+
CREATE TABLE IF NOT EXISTS "agent_commentary" (
|
|
3
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
4
|
+
"company_id" uuid NOT NULL,
|
|
5
|
+
"agent_id" uuid NOT NULL,
|
|
6
|
+
"run_id" uuid NOT NULL,
|
|
7
|
+
"issue_id" uuid,
|
|
8
|
+
"kind" text NOT NULL,
|
|
9
|
+
"body" text NOT NULL,
|
|
10
|
+
"idempotency_key" text NOT NULL,
|
|
11
|
+
"payload_hash" text NOT NULL,
|
|
12
|
+
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
|
13
|
+
CONSTRAINT "agent_commentary_kind_check" CHECK ("agent_commentary"."kind" in ('complaint', 'suggestion'))
|
|
14
|
+
);
|
|
15
|
+
--> statement-breakpoint
|
|
16
|
+
DO $$ BEGIN
|
|
17
|
+
ALTER TABLE "agent_commentary" ADD CONSTRAINT "agent_commentary_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;
|
|
18
|
+
EXCEPTION WHEN duplicate_object THEN NULL; END $$;
|
|
19
|
+
--> statement-breakpoint
|
|
20
|
+
DO $$ BEGIN
|
|
21
|
+
ALTER TABLE "agent_commentary" ADD CONSTRAINT "agent_commentary_agent_id_agents_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."agents"("id") ON DELETE cascade ON UPDATE no action;
|
|
22
|
+
EXCEPTION WHEN duplicate_object THEN NULL; END $$;
|
|
23
|
+
--> statement-breakpoint
|
|
24
|
+
DO $$ BEGIN
|
|
25
|
+
ALTER TABLE "agent_commentary" ADD CONSTRAINT "agent_commentary_run_id_heartbeat_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."heartbeat_runs"("id") ON DELETE cascade ON UPDATE no action;
|
|
26
|
+
EXCEPTION WHEN duplicate_object THEN NULL; END $$;
|
|
27
|
+
--> statement-breakpoint
|
|
28
|
+
DO $$ BEGIN
|
|
29
|
+
ALTER TABLE "agent_commentary" ADD CONSTRAINT "agent_commentary_issue_id_issues_id_fk" FOREIGN KEY ("issue_id") REFERENCES "public"."issues"("id") ON DELETE set null ON UPDATE no action;
|
|
30
|
+
EXCEPTION WHEN duplicate_object THEN NULL; END $$;
|
|
31
|
+
--> statement-breakpoint
|
|
32
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "agent_commentary_run_key_uq" ON "agent_commentary" USING btree ("company_id","run_id","idempotency_key");--> statement-breakpoint
|
|
33
|
+
CREATE INDEX IF NOT EXISTS "agent_commentary_company_kind_created_idx" ON "agent_commentary" USING btree ("company_id","kind","created_at");
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
CREATE TABLE IF NOT EXISTS "mcp_attachment_uploads" (
|
|
2
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
3
|
+
"company_id" uuid NOT NULL,
|
|
4
|
+
"user_id" text NOT NULL,
|
|
5
|
+
"request_id" uuid NOT NULL,
|
|
6
|
+
"task_id" uuid NOT NULL,
|
|
7
|
+
"arguments_hash" text NOT NULL,
|
|
8
|
+
"original_filename" text NOT NULL,
|
|
9
|
+
"content_type" text NOT NULL,
|
|
10
|
+
"byte_size" integer NOT NULL,
|
|
11
|
+
"sha256" text NOT NULL,
|
|
12
|
+
"attachment_id" uuid,
|
|
13
|
+
"object_key" text NOT NULL,
|
|
14
|
+
"storage_provider" text NOT NULL,
|
|
15
|
+
"expires_at" timestamp with time zone NOT NULL,
|
|
16
|
+
"cleaned_at" timestamp with time zone,
|
|
17
|
+
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
|
18
|
+
);
|
|
19
|
+
--> statement-breakpoint
|
|
20
|
+
CREATE TABLE IF NOT EXISTS "mcp_file_tickets" (
|
|
21
|
+
"token_hash" text PRIMARY KEY NOT NULL,
|
|
22
|
+
"company_id" uuid NOT NULL,
|
|
23
|
+
"grant_id" uuid NOT NULL,
|
|
24
|
+
"upload_id" uuid,
|
|
25
|
+
"attachment_id" uuid,
|
|
26
|
+
"expires_at" timestamp with time zone NOT NULL,
|
|
27
|
+
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
|
28
|
+
);
|
|
29
|
+
--> statement-breakpoint
|
|
30
|
+
DO $$ BEGIN
|
|
31
|
+
ALTER TABLE "mcp_attachment_uploads" ADD CONSTRAINT "mcp_attachment_uploads_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;
|
|
32
|
+
EXCEPTION WHEN duplicate_object THEN NULL; END $$;--> statement-breakpoint
|
|
33
|
+
DO $$ BEGIN
|
|
34
|
+
ALTER TABLE "mcp_attachment_uploads" ADD CONSTRAINT "mcp_attachment_uploads_user_id_user_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."user"("id") ON DELETE cascade ON UPDATE no action;
|
|
35
|
+
EXCEPTION WHEN duplicate_object THEN NULL; END $$;--> statement-breakpoint
|
|
36
|
+
DO $$ BEGIN
|
|
37
|
+
ALTER TABLE "mcp_file_tickets" ADD CONSTRAINT "mcp_file_tickets_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;
|
|
38
|
+
EXCEPTION WHEN duplicate_object THEN NULL; END $$;--> statement-breakpoint
|
|
39
|
+
DO $$ BEGIN
|
|
40
|
+
ALTER TABLE "mcp_file_tickets" ADD CONSTRAINT "mcp_file_tickets_grant_id_mcp_oauth_grants_id_fk" FOREIGN KEY ("grant_id") REFERENCES "public"."mcp_oauth_grants"("id") ON DELETE cascade ON UPDATE no action;
|
|
41
|
+
EXCEPTION WHEN duplicate_object THEN NULL; END $$;--> statement-breakpoint
|
|
42
|
+
DO $$ BEGIN
|
|
43
|
+
ALTER TABLE "mcp_file_tickets" ADD CONSTRAINT "mcp_file_tickets_upload_id_mcp_attachment_uploads_id_fk" FOREIGN KEY ("upload_id") REFERENCES "public"."mcp_attachment_uploads"("id") ON DELETE cascade ON UPDATE no action;
|
|
44
|
+
EXCEPTION WHEN duplicate_object THEN NULL; END $$;--> statement-breakpoint
|
|
45
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "mcp_attachment_upload_request_uq" ON "mcp_attachment_uploads" USING btree ("company_id","user_id","request_id");--> statement-breakpoint
|
|
46
|
+
CREATE INDEX IF NOT EXISTS "mcp_attachment_upload_expiry_idx" ON "mcp_attachment_uploads" USING btree ("expires_at");--> statement-breakpoint
|
|
47
|
+
CREATE INDEX IF NOT EXISTS "mcp_file_ticket_expiry_idx" ON "mcp_file_tickets" USING btree ("expires_at");--> statement-breakpoint
|
|
48
|
+
CREATE INDEX IF NOT EXISTS "mcp_file_ticket_grant_idx" ON "mcp_file_tickets" USING btree ("grant_id");
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
-- Preserve values while upgrading exact accounting. Additions are replay-safe.
|
|
2
|
+
CREATE TABLE IF NOT EXISTS "accounting_runtime_baselines" (
|
|
3
|
+
"agent_id" uuid PRIMARY KEY NOT NULL,
|
|
4
|
+
"company_id" uuid NOT NULL,
|
|
5
|
+
"cost_cents" numeric(24, 7) NOT NULL,
|
|
6
|
+
"input_tokens" numeric(30, 0) NOT NULL,
|
|
7
|
+
"cached_input_tokens" numeric(30, 0) NOT NULL,
|
|
8
|
+
"output_tokens" numeric(30, 0) NOT NULL,
|
|
9
|
+
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
|
10
|
+
);
|
|
11
|
+
--> statement-breakpoint
|
|
12
|
+
CREATE TABLE IF NOT EXISTS "billing_invoice_lines" (
|
|
13
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
14
|
+
"company_id" uuid NOT NULL,
|
|
15
|
+
"invoice_id" uuid NOT NULL,
|
|
16
|
+
"external_id" text NOT NULL,
|
|
17
|
+
"kind" text NOT NULL,
|
|
18
|
+
"amount_cents" numeric(24, 7) NOT NULL,
|
|
19
|
+
"occurred_at" timestamp with time zone NOT NULL,
|
|
20
|
+
"cost_event_id" uuid,
|
|
21
|
+
"run_id" uuid,
|
|
22
|
+
"provider_request_id" text,
|
|
23
|
+
"model" text,
|
|
24
|
+
"pricing" jsonb DEFAULT '{}'::jsonb NOT NULL,
|
|
25
|
+
CONSTRAINT "billing_invoice_lines_amount_check" CHECK ("billing_invoice_lines"."amount_cents" >= 0),
|
|
26
|
+
CONSTRAINT "billing_invoice_lines_kind_check" CHECK ("billing_invoice_lines"."kind" in ('inference', 'fee', 'credit'))
|
|
27
|
+
);
|
|
28
|
+
--> statement-breakpoint
|
|
29
|
+
CREATE TABLE IF NOT EXISTS "billing_invoices" (
|
|
30
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
31
|
+
"company_id" uuid NOT NULL,
|
|
32
|
+
"biller" text NOT NULL,
|
|
33
|
+
"external_id" text NOT NULL,
|
|
34
|
+
"currency" text NOT NULL,
|
|
35
|
+
"receipt_hash" text NOT NULL,
|
|
36
|
+
"imported_by" text NOT NULL,
|
|
37
|
+
"created_at" timestamp with time zone DEFAULT now() NOT NULL
|
|
38
|
+
);
|
|
39
|
+
--> statement-breakpoint
|
|
40
|
+
CREATE TABLE IF NOT EXISTS "budget_reservations" (
|
|
41
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
42
|
+
"company_id" uuid NOT NULL,
|
|
43
|
+
"run_id" uuid NOT NULL,
|
|
44
|
+
"agent_id" uuid NOT NULL,
|
|
45
|
+
"project_id" uuid,
|
|
46
|
+
"amount_cents" numeric(24, 7) NOT NULL,
|
|
47
|
+
"state" text DEFAULT 'held' NOT NULL,
|
|
48
|
+
"provider_started_at" timestamp with time zone,
|
|
49
|
+
"settled_at" timestamp with time zone,
|
|
50
|
+
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
|
51
|
+
CONSTRAINT "budget_reservations_state_check" CHECK ("budget_reservations"."state" in ('held', 'settled', 'released')),
|
|
52
|
+
CONSTRAINT "budget_reservations_amount_check" CHECK ("budget_reservations"."amount_cents" >= 0)
|
|
53
|
+
);
|
|
54
|
+
--> statement-breakpoint
|
|
55
|
+
CREATE TABLE IF NOT EXISTS "cost_adjustments" (
|
|
56
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
57
|
+
"company_id" uuid NOT NULL,
|
|
58
|
+
"cost_event_id" uuid NOT NULL,
|
|
59
|
+
"invoice_line_id" uuid,
|
|
60
|
+
"idempotency_key" text NOT NULL,
|
|
61
|
+
"receipt_hash" text NOT NULL,
|
|
62
|
+
"previous_cents" numeric(24, 7) NOT NULL,
|
|
63
|
+
"corrected_cents" numeric(24, 7) NOT NULL,
|
|
64
|
+
"previous_status" text NOT NULL,
|
|
65
|
+
"previous_pricing" jsonb DEFAULT '{}'::jsonb NOT NULL,
|
|
66
|
+
"reason" text NOT NULL,
|
|
67
|
+
"actor_id" text NOT NULL,
|
|
68
|
+
"pricing" jsonb DEFAULT '{}'::jsonb NOT NULL,
|
|
69
|
+
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
|
70
|
+
CONSTRAINT "cost_adjustments_amount_check" CHECK ("cost_adjustments"."previous_cents" >= 0 and "cost_adjustments"."corrected_cents" >= 0)
|
|
71
|
+
);
|
|
72
|
+
--> statement-breakpoint
|
|
73
|
+
CREATE TABLE IF NOT EXISTS "provider_billing_snapshots" (
|
|
74
|
+
"id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL,
|
|
75
|
+
"company_id" uuid NOT NULL,
|
|
76
|
+
"provider" text NOT NULL,
|
|
77
|
+
"account_id" text NOT NULL,
|
|
78
|
+
"scope_id" text NOT NULL,
|
|
79
|
+
"day" text NOT NULL,
|
|
80
|
+
"amount_cents" numeric(24, 7) NOT NULL,
|
|
81
|
+
"revision" integer DEFAULT 1 NOT NULL,
|
|
82
|
+
"updated_at" timestamp with time zone DEFAULT now() NOT NULL,
|
|
83
|
+
CONSTRAINT "provider_billing_snapshots_amount_check" CHECK ("provider_billing_snapshots"."amount_cents" >= 0)
|
|
84
|
+
);
|
|
85
|
+
--> statement-breakpoint
|
|
86
|
+
CREATE TABLE IF NOT EXISTS "run_usage_receipts" (
|
|
87
|
+
"id" uuid PRIMARY KEY NOT NULL,
|
|
88
|
+
"company_id" uuid NOT NULL,
|
|
89
|
+
"run_id" uuid NOT NULL,
|
|
90
|
+
"source_id" uuid NOT NULL,
|
|
91
|
+
"sequence" integer NOT NULL,
|
|
92
|
+
"receipt_hash" text NOT NULL,
|
|
93
|
+
"receipt_json" jsonb NOT NULL,
|
|
94
|
+
"complete" boolean NOT NULL,
|
|
95
|
+
"received_at" timestamp with time zone NOT NULL,
|
|
96
|
+
"created_at" timestamp with time zone DEFAULT now() NOT NULL,
|
|
97
|
+
CONSTRAINT "run_usage_receipts_positive_sequence" CHECK ("run_usage_receipts"."sequence" > 0)
|
|
98
|
+
);
|
|
99
|
+
--> statement-breakpoint
|
|
100
|
+
DROP INDEX IF EXISTS "budget_incidents_policy_window_threshold_idx";--> statement-breakpoint
|
|
101
|
+
ALTER TABLE "agent_runtime_state" ALTER COLUMN "total_cost_cents" SET DATA TYPE numeric(24, 7);--> statement-breakpoint
|
|
102
|
+
ALTER TABLE "agents" ALTER COLUMN "spent_monthly_cents" SET DATA TYPE numeric(24, 7);--> statement-breakpoint
|
|
103
|
+
ALTER TABLE "budget_incidents" ALTER COLUMN "amount_observed" SET DATA TYPE numeric(24, 7);--> statement-breakpoint
|
|
104
|
+
ALTER TABLE "companies" ALTER COLUMN "spent_monthly_cents" SET DATA TYPE numeric(24, 7);--> statement-breakpoint
|
|
105
|
+
ALTER TABLE "cost_events" ALTER COLUMN "cost_cents" SET DATA TYPE numeric(24, 7);--> statement-breakpoint
|
|
106
|
+
ALTER TABLE "finance_events" ALTER COLUMN "amount_cents" SET DATA TYPE numeric(24, 7);--> statement-breakpoint
|
|
107
|
+
ALTER TABLE "agents" ADD COLUMN IF NOT EXISTS "spend_month_utc" text;--> statement-breakpoint
|
|
108
|
+
ALTER TABLE "budget_policies" ADD COLUMN IF NOT EXISTS "reservation_cents" numeric(24, 7) DEFAULT '0' NOT NULL;--> statement-breakpoint
|
|
109
|
+
ALTER TABLE "budget_policies" ADD COLUMN IF NOT EXISTS "unpriced_usage_policy" text DEFAULT 'block' NOT NULL;--> statement-breakpoint
|
|
110
|
+
ALTER TABLE "budget_policies" ADD COLUMN IF NOT EXISTS "enforcement_version" integer DEFAULT 0 NOT NULL;--> statement-breakpoint
|
|
111
|
+
ALTER TABLE "budget_policies" ADD COLUMN IF NOT EXISTS "enforcement_delivered_version" integer DEFAULT 0 NOT NULL;--> statement-breakpoint
|
|
112
|
+
ALTER TABLE "companies" ADD COLUMN IF NOT EXISTS "spend_month_utc" text;--> statement-breakpoint
|
|
113
|
+
ALTER TABLE "cost_events" ADD COLUMN IF NOT EXISTS "idempotency_key" text;--> statement-breakpoint
|
|
114
|
+
ALTER TABLE "cost_events" ADD COLUMN IF NOT EXISTS "receipt_hash" text;--> statement-breakpoint
|
|
115
|
+
ALTER TABLE "cost_events" ADD COLUMN IF NOT EXISTS "provider_request_id" text;--> statement-breakpoint
|
|
116
|
+
ALTER TABLE "cost_events" ADD COLUMN IF NOT EXISTS "reported_cost_cents" numeric(24, 7);--> statement-breakpoint
|
|
117
|
+
ALTER TABLE "cost_events" ADD COLUMN IF NOT EXISTS "pricing_provenance" jsonb;--> statement-breakpoint
|
|
118
|
+
ALTER TABLE "finance_events" ADD COLUMN IF NOT EXISTS "idempotency_key" text;--> statement-breakpoint
|
|
119
|
+
ALTER TABLE "finance_events" ADD COLUMN IF NOT EXISTS "receipt_hash" text;--> statement-breakpoint
|
|
120
|
+
ALTER TABLE "heartbeat_runs" ADD COLUMN IF NOT EXISTS "cost_accounting_pending" boolean DEFAULT false NOT NULL;--> statement-breakpoint
|
|
121
|
+
ALTER TABLE "heartbeat_runs" ADD COLUMN IF NOT EXISTS "cost_accounted_at" timestamp with time zone;--> statement-breakpoint
|
|
122
|
+
ALTER TABLE "heartbeat_runs" ADD COLUMN IF NOT EXISTS "accounting_projection_version" text;--> statement-breakpoint
|
|
123
|
+
ALTER TABLE "heartbeat_runs" ADD COLUMN IF NOT EXISTS "accounting_last_attempt_at" timestamp with time zone;--> statement-breakpoint
|
|
124
|
+
ALTER TABLE "heartbeat_runs" ADD COLUMN IF NOT EXISTS "accounting_last_error" text;--> statement-breakpoint
|
|
125
|
+
ALTER TABLE "heartbeat_runs" ADD COLUMN IF NOT EXISTS "accounting_attempt_count" integer DEFAULT 0 NOT NULL;--> statement-breakpoint
|
|
126
|
+
DO $$ BEGIN
|
|
127
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'accounting_runtime_baselines_agent_id_agents_id_fk' AND conrelid = 'public.accounting_runtime_baselines'::regclass) THEN
|
|
128
|
+
ALTER TABLE "accounting_runtime_baselines" ADD CONSTRAINT "accounting_runtime_baselines_agent_id_agents_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."agents"("id") ON DELETE cascade ON UPDATE no action;
|
|
129
|
+
END IF;
|
|
130
|
+
END $$;--> statement-breakpoint
|
|
131
|
+
DO $$ BEGIN
|
|
132
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'accounting_runtime_baselines_company_id_companies_id_fk' AND conrelid = 'public.accounting_runtime_baselines'::regclass) THEN
|
|
133
|
+
ALTER TABLE "accounting_runtime_baselines" ADD CONSTRAINT "accounting_runtime_baselines_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;
|
|
134
|
+
END IF;
|
|
135
|
+
END $$;--> statement-breakpoint
|
|
136
|
+
DO $$ BEGIN
|
|
137
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'billing_invoice_lines_company_id_companies_id_fk' AND conrelid = 'public.billing_invoice_lines'::regclass) THEN
|
|
138
|
+
ALTER TABLE "billing_invoice_lines" ADD CONSTRAINT "billing_invoice_lines_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;
|
|
139
|
+
END IF;
|
|
140
|
+
END $$;--> statement-breakpoint
|
|
141
|
+
DO $$ BEGIN
|
|
142
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'billing_invoice_lines_invoice_id_billing_invoices_id_fk' AND conrelid = 'public.billing_invoice_lines'::regclass) THEN
|
|
143
|
+
ALTER TABLE "billing_invoice_lines" ADD CONSTRAINT "billing_invoice_lines_invoice_id_billing_invoices_id_fk" FOREIGN KEY ("invoice_id") REFERENCES "public"."billing_invoices"("id") ON DELETE cascade ON UPDATE no action;
|
|
144
|
+
END IF;
|
|
145
|
+
END $$;--> statement-breakpoint
|
|
146
|
+
DO $$ BEGIN
|
|
147
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'billing_invoice_lines_cost_event_id_cost_events_id_fk' AND conrelid = 'public.billing_invoice_lines'::regclass) THEN
|
|
148
|
+
ALTER TABLE "billing_invoice_lines" ADD CONSTRAINT "billing_invoice_lines_cost_event_id_cost_events_id_fk" FOREIGN KEY ("cost_event_id") REFERENCES "public"."cost_events"("id") ON DELETE cascade ON UPDATE no action;
|
|
149
|
+
END IF;
|
|
150
|
+
END $$;--> statement-breakpoint
|
|
151
|
+
DO $$ BEGIN
|
|
152
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'billing_invoices_company_id_companies_id_fk' AND conrelid = 'public.billing_invoices'::regclass) THEN
|
|
153
|
+
ALTER TABLE "billing_invoices" ADD CONSTRAINT "billing_invoices_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;
|
|
154
|
+
END IF;
|
|
155
|
+
END $$;--> statement-breakpoint
|
|
156
|
+
DO $$ BEGIN
|
|
157
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'budget_reservations_company_id_companies_id_fk' AND conrelid = 'public.budget_reservations'::regclass) THEN
|
|
158
|
+
ALTER TABLE "budget_reservations" ADD CONSTRAINT "budget_reservations_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;
|
|
159
|
+
END IF;
|
|
160
|
+
END $$;--> statement-breakpoint
|
|
161
|
+
DO $$ BEGIN
|
|
162
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'budget_reservations_run_id_heartbeat_runs_id_fk' AND conrelid = 'public.budget_reservations'::regclass) THEN
|
|
163
|
+
ALTER TABLE "budget_reservations" ADD CONSTRAINT "budget_reservations_run_id_heartbeat_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."heartbeat_runs"("id") ON DELETE cascade ON UPDATE no action;
|
|
164
|
+
END IF;
|
|
165
|
+
END $$;--> statement-breakpoint
|
|
166
|
+
DO $$ BEGIN
|
|
167
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'budget_reservations_agent_id_agents_id_fk' AND conrelid = 'public.budget_reservations'::regclass) THEN
|
|
168
|
+
ALTER TABLE "budget_reservations" ADD CONSTRAINT "budget_reservations_agent_id_agents_id_fk" FOREIGN KEY ("agent_id") REFERENCES "public"."agents"("id") ON DELETE cascade ON UPDATE no action;
|
|
169
|
+
END IF;
|
|
170
|
+
END $$;--> statement-breakpoint
|
|
171
|
+
DO $$ BEGIN
|
|
172
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'cost_adjustments_company_id_companies_id_fk' AND conrelid = 'public.cost_adjustments'::regclass) THEN
|
|
173
|
+
ALTER TABLE "cost_adjustments" ADD CONSTRAINT "cost_adjustments_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;
|
|
174
|
+
END IF;
|
|
175
|
+
END $$;--> statement-breakpoint
|
|
176
|
+
DO $$ BEGIN
|
|
177
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'cost_adjustments_cost_event_id_cost_events_id_fk' AND conrelid = 'public.cost_adjustments'::regclass) THEN
|
|
178
|
+
ALTER TABLE "cost_adjustments" ADD CONSTRAINT "cost_adjustments_cost_event_id_cost_events_id_fk" FOREIGN KEY ("cost_event_id") REFERENCES "public"."cost_events"("id") ON DELETE cascade ON UPDATE no action;
|
|
179
|
+
END IF;
|
|
180
|
+
END $$;--> statement-breakpoint
|
|
181
|
+
DO $$ BEGIN
|
|
182
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'cost_adjustments_invoice_line_id_billing_invoice_lines_id_fk' AND conrelid = 'public.cost_adjustments'::regclass) THEN
|
|
183
|
+
ALTER TABLE "cost_adjustments" ADD CONSTRAINT "cost_adjustments_invoice_line_id_billing_invoice_lines_id_fk" FOREIGN KEY ("invoice_line_id") REFERENCES "public"."billing_invoice_lines"("id") ON DELETE cascade ON UPDATE no action;
|
|
184
|
+
END IF;
|
|
185
|
+
END $$;--> statement-breakpoint
|
|
186
|
+
DO $$ BEGIN
|
|
187
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'provider_billing_snapshots_company_id_companies_id_fk' AND conrelid = 'public.provider_billing_snapshots'::regclass) THEN
|
|
188
|
+
ALTER TABLE "provider_billing_snapshots" ADD CONSTRAINT "provider_billing_snapshots_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;
|
|
189
|
+
END IF;
|
|
190
|
+
END $$;--> statement-breakpoint
|
|
191
|
+
DO $$ BEGIN
|
|
192
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'run_usage_receipts_company_id_companies_id_fk' AND conrelid = 'public.run_usage_receipts'::regclass) THEN
|
|
193
|
+
ALTER TABLE "run_usage_receipts" ADD CONSTRAINT "run_usage_receipts_company_id_companies_id_fk" FOREIGN KEY ("company_id") REFERENCES "public"."companies"("id") ON DELETE cascade ON UPDATE no action;
|
|
194
|
+
END IF;
|
|
195
|
+
END $$;--> statement-breakpoint
|
|
196
|
+
DO $$ BEGIN
|
|
197
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'run_usage_receipts_run_id_heartbeat_runs_id_fk' AND conrelid = 'public.run_usage_receipts'::regclass) THEN
|
|
198
|
+
ALTER TABLE "run_usage_receipts" ADD CONSTRAINT "run_usage_receipts_run_id_heartbeat_runs_id_fk" FOREIGN KEY ("run_id") REFERENCES "public"."heartbeat_runs"("id") ON DELETE cascade ON UPDATE no action;
|
|
199
|
+
END IF;
|
|
200
|
+
END $$;--> statement-breakpoint
|
|
201
|
+
CREATE INDEX IF NOT EXISTS "accounting_runtime_baselines_company_idx" ON "accounting_runtime_baselines" USING btree ("company_id");--> statement-breakpoint
|
|
202
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "billing_invoice_lines_identity_idx" ON "billing_invoice_lines" USING btree ("invoice_id","external_id");--> statement-breakpoint
|
|
203
|
+
CREATE INDEX IF NOT EXISTS "billing_invoice_lines_company_idx" ON "billing_invoice_lines" USING btree ("company_id","invoice_id");--> statement-breakpoint
|
|
204
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "billing_invoices_identity_idx" ON "billing_invoices" USING btree ("company_id","biller","external_id");--> statement-breakpoint
|
|
205
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "budget_reservations_run_idx" ON "budget_reservations" USING btree ("company_id","run_id");--> statement-breakpoint
|
|
206
|
+
CREATE INDEX IF NOT EXISTS "budget_reservations_active_idx" ON "budget_reservations" USING btree ("company_id","agent_id","project_id") WHERE "budget_reservations"."state" = 'held';--> statement-breakpoint
|
|
207
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "cost_adjustments_receipt_idx" ON "cost_adjustments" USING btree ("company_id","idempotency_key");--> statement-breakpoint
|
|
208
|
+
CREATE INDEX IF NOT EXISTS "cost_adjustments_event_idx" ON "cost_adjustments" USING btree ("company_id","cost_event_id","created_at");--> statement-breakpoint
|
|
209
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "cost_adjustments_invoice_line_idx" ON "cost_adjustments" USING btree ("company_id","invoice_line_id");--> statement-breakpoint
|
|
210
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "provider_billing_snapshots_identity_idx" ON "provider_billing_snapshots" USING btree ("company_id","provider","account_id","scope_id","day");--> statement-breakpoint
|
|
211
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "run_usage_receipts_source_sequence_idx" ON "run_usage_receipts" USING btree ("company_id","run_id","source_id","sequence");--> statement-breakpoint
|
|
212
|
+
CREATE INDEX IF NOT EXISTS "run_usage_receipts_run_latest_idx" ON "run_usage_receipts" USING btree ("company_id","run_id","received_at");--> statement-breakpoint
|
|
213
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "cost_events_company_receipt_idx" ON "cost_events" USING btree ("company_id","idempotency_key");--> statement-breakpoint
|
|
214
|
+
CREATE INDEX IF NOT EXISTS "cost_events_provider_request_idx" ON "cost_events" USING btree ("company_id","biller","provider_request_id");--> statement-breakpoint
|
|
215
|
+
CREATE INDEX IF NOT EXISTS "cost_events_company_project_occurred_idx" ON "cost_events" USING btree ("company_id","project_id","occurred_at");--> statement-breakpoint
|
|
216
|
+
CREATE INDEX IF NOT EXISTS "cost_events_unpriced_idx" ON "cost_events" USING btree ("company_id","occurred_at","id") WHERE "cost_events"."cost_status" = 'unpriced' and "cost_events"."billing_type" <> 'subscription_included';--> statement-breakpoint
|
|
217
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "finance_events_company_receipt_idx" ON "finance_events" USING btree ("company_id","idempotency_key");--> statement-breakpoint
|
|
218
|
+
CREATE INDEX IF NOT EXISTS "heartbeat_runs_cost_accounting_pending_idx" ON "heartbeat_runs" USING btree ("updated_at","id") WHERE "heartbeat_runs"."cost_accounting_pending" = true;--> statement-breakpoint
|
|
219
|
+
CREATE UNIQUE INDEX IF NOT EXISTS "budget_incidents_policy_window_threshold_idx" ON "budget_incidents" USING btree ("policy_id","window_start","threshold_type") WHERE "budget_incidents"."status" = 'open';--> statement-breakpoint
|
|
220
|
+
DO $$ BEGIN
|
|
221
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'budget_policies_reservation_nonnegative' AND conrelid = 'public.budget_policies'::regclass) THEN
|
|
222
|
+
ALTER TABLE "budget_policies" ADD CONSTRAINT "budget_policies_reservation_nonnegative" CHECK ("budget_policies"."reservation_cents" >= 0);
|
|
223
|
+
END IF;
|
|
224
|
+
END $$;--> statement-breakpoint
|
|
225
|
+
DO $$ BEGIN
|
|
226
|
+
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'cost_events_nonnegative_amounts' AND conrelid = 'public.cost_events'::regclass) THEN
|
|
227
|
+
ALTER TABLE "cost_events" ADD CONSTRAINT "cost_events_nonnegative_amounts" CHECK ("cost_events"."cost_cents" >= 0 and "cost_events"."input_tokens" >= 0 and "cost_events"."cached_input_tokens" >= 0 and "cost_events"."output_tokens" >= 0);
|
|
228
|
+
END IF;
|
|
229
|
+
END $$;
|
|
230
|
+
--> statement-breakpoint
|
|
231
|
+
-- Preserve the ledger precision in status-card update snapshots. Reapplying this type widening is safe.
|
|
232
|
+
ALTER TABLE "status_card_updates" ALTER COLUMN "cost_cents" SET DATA TYPE numeric(24, 7);
|