Skip to content

Add migration for llm_trajectories table#445

Merged
lalalune merged 1 commit intodevfrom
odi-db
Apr 12, 2026
Merged

Add migration for llm_trajectories table#445
lalalune merged 1 commit intodevfrom
odi-db

Conversation

@odilitime
Copy link
Copy Markdown
Contributor

@odilitime odilitime commented Apr 11, 2026

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


Note

Medium Risk
Introduces a new database table with indexes and foreign keys; main risk is migration/runtime impact on large databases and potential downstream reliance on the new schema.

Overview
Adds SQL migration 0062_add_llm_trajectories_table.sql to create a new llm_trajectories table for storing LLM request/response text, token/cost accounting, latency, success/error state, and JSON metadata.

The migration also adds several query indexes and foreign key constraints to organizations (cascade delete), users, and api_keys (set null), and updates Drizzle’s migrations journal (meta/_journal.json) to register migration 0062_add_llm_trajectories_table.

Reviewed by Cursor Bugbot for commit 0f203f7. Bugbot is set up for automated code reviews on this repo. Configure here.

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
Copilot AI review requested due to automatic review settings April 11, 2026 00:23
@vercel
Copy link
Copy Markdown

vercel bot commented Apr 11, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
eliza-cloud-v2 Ready Ready Preview, Comment Apr 11, 2026 0:27am

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Apr 11, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 9341e464-23b2-4a7f-be62-4e377e57c0ed

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch odi-db

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Postgres migration so environments that apply raw SQL migrations (and rely on Drizzle’s migration journal) create the llm_trajectories table used for storing LLM call prompt/response trajectories and related metrics.

Changes:

  • Add 0062_add_llm_trajectories_table.sql to create llm_trajectories, its indexes, and foreign keys.
  • Update packages/db/migrations/meta/_journal.json to register migration 0062_add_llm_trajectories_table for Drizzle tracking.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/db/migrations/meta/_journal.json Registers the new 0062 migration in Drizzle’s journal so it is tracked/applied in order.
packages/db/migrations/0062_add_llm_trajectories_table.sql Creates the llm_trajectories table with indexes and FK constraints to organizations, users, and api_keys.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@claude
Copy link
Copy Markdown

claude bot commented Apr 11, 2026

PR Review: Add migration for llm_trajectories table

The migration is well-structured overall — uses IF NOT EXISTS guards, DO blocks for idempotent FK constraints, and the SQL matches the Drizzle schema file. A few issues worth addressing:


Bugs / Data Integrity

1. _journal.json loses its trailing newline

The PR removes the final newline from _journal.json. This can cause noisy diffs and may break POSIX-compliant tooling. The trailing newline should be preserved.

2. Derived columns without CHECK constraints

total_tokens and total_cost are computed values (input + output) but have no constraints enforcing this invariant. Callers can silently write inconsistent totals. Suggested additions to the migration:

ALTER TABLE "llm_trajectories"
  ADD CONSTRAINT "chk_total_tokens" CHECK ("total_tokens" = "input_tokens" + "output_tokens");
ALTER TABLE "llm_trajectories"
  ADD CONSTRAINT "chk_total_cost" CHECK ("total_cost" = "input_cost" + "output_cost");

If totals are always computed server-side and never caller-supplied, consider dropping the derived columns in favour of generated columns or a view instead.

3. request_id has no index and no uniqueness guarantee

If request_id is a correlation/trace ID for lookups or deduplication, it needs a unique constraint or at minimum an index. A lookup by request_id is currently a full table scan:

CREATE UNIQUE INDEX IF NOT EXISTS "llm_trajectories_request_id_idx"
  ON "llm_trajectories" ("request_id")
  WHERE "request_id" IS NOT NULL;

Performance

4. No index on user_id

Indexes cover org+created, org+model, purpose, and org+purpose+created — but not user_id. Querying all LLM calls for a specific user (usage dashboards, billing) will require a full sequential scan:

CREATE INDEX IF NOT EXISTS "llm_trajectories_user_id_idx" ON "llm_trajectories" ("user_id");

5. Unbounded TEXT columns for prompt content

system_prompt, user_prompt, and response_text are unconstrained text. With modern large context windows, individual rows can be 500KB-2MB. Under production load the table will grow very fast. Consider partitioning by created_at (monthly) before the table gets large, and whether full prompt text is required or if a truncated version suffices for fine-tuning.


Security

6. Sensitive data stored in plaintext

This table will hold raw system prompts and user conversations for every LLM call — potentially including confidential business logic (system prompts) and user PII. Recommend: row-level security scoped to organization_id, a data retention/TTL policy to prevent unbounded growth, and evaluating whether system_prompt should be encrypted at rest.


Process

7. Schema file pre-dates the migration

Per CLAUDE.md, schema + migration should be committed together. packages/db/schemas/llm-trajectories.ts was already on main before this PR, creating a window where the schema definition existed but the migration had not been applied. Worth noting in the PR description why the migration was split, to avoid setting a precedent.


Summary

Severity Issue
Medium Derived columns (total_tokens, total_cost) lack CHECK constraints
Medium request_id is unindexed — full scan for lookups
Medium No index on user_id
Low _journal.json loses trailing newline
Low Unbounded TEXT prompt columns — partition/retention plan needed
Low Raw prompt data in plaintext — access controls recommended
Nit Schema-before-migration pattern deviates from CLAUDE.md workflow

The migration will apply cleanly and the SQL matches the Drizzle schema, so nothing will break. The derived-column consistency and missing indexes are the most important fixes before this table sees production traffic.

@lalalune lalalune merged commit db8afb9 into dev Apr 12, 2026
17 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants