From 0f203f709b8130b1e6bfa255ce0d7bf49608a6e9 Mon Sep 17 00:00:00 2001 From: Odilitime Date: Fri, 10 Apr 2026 17:22:21 -0700 Subject: [PATCH] Add migration for llm_trajectories table Introduces 0062_add_llm_trajectories_table.sql so the table exists in databases that only apply SQL migrations; journal entry aligns with Drizzle tracking. Made-with: Cursor --- .../0062_add_llm_trajectories_table.sql | 47 +++++++++++++++++++ packages/db/migrations/meta/_journal.json | 9 +++- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 packages/db/migrations/0062_add_llm_trajectories_table.sql diff --git a/packages/db/migrations/0062_add_llm_trajectories_table.sql b/packages/db/migrations/0062_add_llm_trajectories_table.sql new file mode 100644 index 000000000..5d4098bdb --- /dev/null +++ b/packages/db/migrations/0062_add_llm_trajectories_table.sql @@ -0,0 +1,47 @@ +CREATE TABLE IF NOT EXISTS "llm_trajectories" ( + "id" uuid PRIMARY KEY DEFAULT gen_random_uuid() NOT NULL, + "organization_id" uuid NOT NULL, + "user_id" uuid, + "api_key_id" uuid, + "model" text NOT NULL, + "provider" text NOT NULL, + "purpose" text, + "request_id" text, + "system_prompt" text, + "user_prompt" text, + "response_text" text, + "input_tokens" integer DEFAULT 0 NOT NULL, + "output_tokens" integer DEFAULT 0 NOT NULL, + "total_tokens" integer DEFAULT 0 NOT NULL, + "input_cost" numeric(12, 6) DEFAULT '0.000000', + "output_cost" numeric(12, 6) DEFAULT '0.000000', + "total_cost" numeric(12, 6) DEFAULT '0.000000', + "latency_ms" integer, + "is_successful" boolean DEFAULT true NOT NULL, + "error_message" text, + "metadata" jsonb DEFAULT '{}'::jsonb NOT NULL, + "created_at" timestamp DEFAULT now() NOT NULL +); + +CREATE INDEX IF NOT EXISTS "llm_trajectories_org_created_idx" ON "llm_trajectories" ("organization_id", "created_at"); +CREATE INDEX IF NOT EXISTS "llm_trajectories_org_model_idx" ON "llm_trajectories" ("organization_id", "model"); +CREATE INDEX IF NOT EXISTS "llm_trajectories_purpose_idx" ON "llm_trajectories" ("purpose"); +CREATE INDEX IF NOT EXISTS "llm_trajectories_org_purpose_created_idx" ON "llm_trajectories" ("organization_id", "purpose", "created_at"); + +DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'llm_trajectories_organization_id_organizations_id_fk') THEN + ALTER TABLE "llm_trajectories" ADD CONSTRAINT "llm_trajectories_organization_id_organizations_id_fk" FOREIGN KEY ("organization_id") REFERENCES "public"."organizations"("id") ON DELETE cascade ON UPDATE no action; + END IF; +END $$; + +DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'llm_trajectories_user_id_users_id_fk') THEN + ALTER TABLE "llm_trajectories" ADD CONSTRAINT "llm_trajectories_user_id_users_id_fk" FOREIGN KEY ("user_id") REFERENCES "public"."users"("id") ON DELETE set null ON UPDATE no action; + END IF; +END $$; + +DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'llm_trajectories_api_key_id_api_keys_id_fk') THEN + ALTER TABLE "llm_trajectories" ADD CONSTRAINT "llm_trajectories_api_key_id_api_keys_id_fk" FOREIGN KEY ("api_key_id") REFERENCES "public"."api_keys"("id") ON DELETE set null ON UPDATE no action; + END IF; +END $$; diff --git a/packages/db/migrations/meta/_journal.json b/packages/db/migrations/meta/_journal.json index 9f28b1c02..99576de40 100644 --- a/packages/db/migrations/meta/_journal.json +++ b/packages/db/migrations/meta/_journal.json @@ -414,6 +414,13 @@ "when": 1775788338243, "tag": "0061_add_steward_user_identity_columns", "breakpoints": true + }, + { + "idx": 59, + "version": "7", + "when": 1775866779597, + "tag": "0062_add_llm_trajectories_table", + "breakpoints": true } ] -} +} \ No newline at end of file