Skip to content

chore: update migrations - #1170

Merged
steebchen merged 1 commit into
mainfrom
chore/migrations
Nov 18, 2025
Merged

steebchen merged 1 commit into
mainfrom
chore/migrations

Conversation

@steebchen

@steebchen steebchen commented Nov 18, 2025 •

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Chores
    • Updated database constraint naming conventions for consistency across multiple tables
    • Upgraded database migration metadata schema from version 7 to 8 with modernized structure
    • Refactored migration snapshot format to use normalized entity-based representation
    • Updated database dialect naming convention

@coderabbitai

coderabbitai Bot commented Nov 18, 2025 •

Copy link
Copy Markdown
Contributor

Walkthrough

Database schema migration introducing constraint naming standardization and snapshot format upgrade from version 7 to version 8. Constraint names across five tables converted to snake_case convention. Migration snapshots restructured from nested per-table definitions to a flattened DDL-based array model with normalized entity types.

Changes

Cohort / File(s) Change Summary
Constraint Renaming
packages/db/migrations/1763457421_smiling_elektra.sql
Renames six database constraints across model_history, model_provider_mapping, model_provider_mapping_history, provider_key, and organization tables from camelCase to snake_case convention (e.g., model_history_modelId_minuteTimestamp_unique → model_history_model_id_minute_timestamp_unique)
Migration Snapshots Format Update
packages/db/migrations/meta/1747760252_snapshot.json, 1747915707_snapshot.json, 1747927048_snapshot.json, 1747930278_snapshot.json, 1748033482_snapshot.json
Updates snapshot schema from version 7 to version 8; restructures legacy nested per-table definitions into normalized DDL array containing discrete entityType entries (tables, pks, columns, fks, uniques); changes dialect from "postgresql" to "postgres"; removes embedded column maps and per-table metadata in favor of flattened entity records
Migrations Journal
packages/db/migrations/meta/_journal.json
Adds new journal entry for migration idx 64 with tag "1763457421_smiling_elektra", version "8", and breakpoints enabled

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • Constraint rename migration: Straightforward SQL constraint renames; verify naming consistency and that all affected tables are covered
  • Snapshot format migration: Large-scale structural changes but homogeneous pattern (same v7→v8 transformation applied to five snapshot files); focus review on ensuring correctness of the normalized DDL structure and that no data is lost in the restructuring
  • Areas requiring attention:
    • Verify all constraint names follow snake_case convention consistently across all tables
    • Confirm migration snapshots maintain referential integrity and complete schema representation in the new DDL format
    • Ensure snapshot version and dialect changes (v7→v8, postgresql→postgres) are applied uniformly across all files

Possibly related PRs

Suggested reviewers

  • smakosh

Pre-merge checks and finishing touches

❌ Failed checks (1 inconclusive)
Check name Status Explanation Resolution
Title check ❓ Inconclusive The title 'chore: update migrations' is vague and generic, using the non-descriptive term 'update migrations' without conveying what specifically changed or why. Consider a more specific title that describes the primary change, such as 'chore: normalize database constraint naming and migrate snapshot format' to clarify the scope of migration updates.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch chore/migrations

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.

@steebchen
steebchen enabled auto-merge November 18, 2025 09:20
@steebchen
steebchen added this pull request to the merge queue Nov 18, 2025

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
packages/db/migrations/meta/1747760252_snapshot.json (1)

167-181: Same note: prefer timestamptz/jsonb in new changes.

Keep as-is for historical snapshots; adopt timestamptz/jsonb in new migrations to reduce future drift.

Also applies to: 359-370, 599-610

packages/db/migrations/meta/1747927048_snapshot.json (1)

599-610: Same non-blocker: consider jsonb/timestamptz in future migrations.

Applies repo-wide; no change requested here.

Also applies to: 1503-1517

packages/db/migrations/meta/1748033482_snapshot.json (1)

115-126: Prefer jsonb and timestamptz in future migrations; existing snapshots are auto-generated.

Verification confirms json and timestamp types exist throughout migration snapshots (lines 115–126, 183–197, 612–623 all verified in 1748033482_snapshot.json). For future work: PostgreSQL does favor "jsonb" over "json" for better indexing and query performance, and "timestamptz" over "timestamp" for timezone correctness. Since these snapshots are auto-generated by your schema migration tool, standardize the preference at the ORM configuration level rather than manually refactoring existing records.

packages/db/migrations/meta/_journal.json (1)

2-3: Update journal header to align with snapshot versions (v8/postgres).

All 65 snapshots in the migration metadata are version 8 with PostgreSQL dialect, but the journal header remains version 7 with legacy "postgresql" dialect. This inconsistency should be corrected to prevent tool confusion.

-  "version": "7",
-  "dialect": "postgresql",
+  "version": "8",
+  "dialect": "postgres",
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between de2552e and 418e836.

📒 Files selected for processing (7)
  • packages/db/migrations/1763457421_smiling_elektra.sql (1 hunks)
  • packages/db/migrations/meta/1747760252_snapshot.json (1 hunks)
  • packages/db/migrations/meta/1747915707_snapshot.json (1 hunks)
  • packages/db/migrations/meta/1747927048_snapshot.json (1 hunks)
  • packages/db/migrations/meta/1747930278_snapshot.json (1 hunks)
  • packages/db/migrations/meta/1748033482_snapshot.json (1 hunks)
  • packages/db/migrations/meta/_journal.json (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (5)
  • GitHub Check: generate / run
  • GitHub Check: test / run
  • GitHub Check: build / run
  • GitHub Check: lint / run
  • GitHub Check: autofix
🔇 Additional comments (5)
packages/db/migrations/1763457421_smiling_elektra.sql (2)

1-4: Constraint naming standardization looks good for most tables.

The migration standardizes constraint names to snake_case across model_history, model_provider_mapping, model_provider_mapping_history, and provider_key tables. This is consistent with the PR objective of naming standardization.


5-6: Clarify the inconsistent constraint type suffix on organization table.

Lines 5–6 rename organization's unique constraints but change the suffix from _unique to _key, deviating from the pattern in lines 1–4 where the _unique suffix is preserved. While _key is a valid PostgreSQL convention for unique constraints, this inconsistency within the same migration is worth clarifying.

  • Is the use of _key suffix for organization constraints intentional (e.g., following a specific naming convention for this table), or should it follow the same _unique pattern as the other tables?
  • Verify that organization_stripeCustomerId_unique and organization_stripeSubscriptionId_unique exist in the current schema.
  • Verify that the new names (organization_stripe_customer_id_key and organization_stripe_subscription_id_key) do not conflict with any existing constraints.
packages/db/migrations/meta/_journal.json (1)

454-458: New journal entry looks good.

Idx 64, tag 1763457421_smiling_elektra, v8 with breakpoints=true is consistent with the snapshot upgrades.

packages/db/migrations/meta/1747915707_snapshot.json (1)

332-341: Uniques present and well-named.

api_key_token_unique, session_token_unique, user_email_unique look consistent with snake_case standardization.

Also applies to: 1571-1580, 1715-1724

packages/db/migrations/meta/1747927048_snapshot.json (1)

1296-1311: Project toggles look consistent.

caching_enabled and cache_duration_seconds defaults align across snapshots.

Also applies to: 1329-1343

Comment on lines +912 to +926
"type": "numeric",
"typeSchema": null,
"notNull": true,
"dimensions": 0,
"default": {
"type": "unknown",
"value": "'0'"
},
"generated": null,
"identity": null,
"name": "credits",
"schema": "public",
"table": "organization",
"entityType": "columns"
},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟡 Minor

🧩 Analysis chain

Verify default for numeric(credits) renders as numeric 0.

Default is represented as "'0'". Ensure generated SQL is DEFAULT 0 (numeric), not a text literal.


🏁 Script executed:

#!/usr/bin/env bash
# Inspect how defaults are emitted in generated SQL (if available in repo artifacts)
fd -a -g 'packages/db/migrations/*' | rg -nP 'CREATE TABLE.*organization|credits|DEFAULT'

Length of output: 0



🏁 Script executed:

#!/bin/bash
# Explore migration directory structure and identify migration system
echo "=== Migration directory structure ==="
find packages/db/migrations -type f -name "*.json" -o -name "*.ts" -o -name "*.js" | head -20

echo -e "\n=== Check package.json for migration tool ==="
cat packages/db/package.json | grep -A5 -B5 "drizzle\|knex\|migrate\|sql"

echo -e "\n=== Look for migration generation code ==="
fd -g "*.ts" -g "*.js" packages/db | xargs rg -l "DEFAULT|default.*numeric\|0.*numeric" -C2 | head -10

Length of output: 2049


🏁 Script executed:

#!/bin/bash
# Check the actual snapshot file for context on how defaults are structured
echo "=== Check nearby entries in snapshot.json for comparison ==="
jq '.schemas[0].tables[0].columns | .[0:5]' packages/db/migrations/meta/1747930278_snapshot.json 2>/dev/null | head -50

Length of output: 65


🏁 Script executed:

#!/bin/bash
# Search for migration generation or SQL building logic
echo "=== Search for how 'unknown' type defaults are handled ==="
rg -n "type.*unknown\|DEFAULT.*0|'0'" packages/db --type ts --type js -C3 | head -40

Length of output: 58


🏁 Script executed:

#!/bin/bash
# Look for the actual generated SQL if available
echo "=== Search for generated migration SQL files ==="
find packages/db -name "*.up.sql" -o -name "*.down.sql" -o -name "*migration*.sql" 2>/dev/null | xargs cat 2>/dev/null | grep -A3 -B3 "organization\|credits" | head -30

Length of output: 48


🏁 Script executed:

#!/bin/bash
# Read the snapshot file directly to inspect structure
echo "=== First 100 lines of snapshot ==="
head -100 packages/db/migrations/meta/1747930278_snapshot.json

Length of output: 2235


🏁 Script executed:

#!/bin/bash
# Search for organization table schema in codebase
echo "=== Search for 'organization' table schema definition ==="
rg -n "organization\|credits" packages/db/src --type ts -C5 | head -60

Length of output: 57


🏁 Script executed:

#!/bin/bash
# Look for Drizzle schema files
echo "=== Find schema definition files ==="
fd -g "*.ts" -g "*.js" packages/db/src | xargs rg -l "table\|schema" | head -20

Length of output: 87


🏁 Script executed:

#!/bin/bash
# Check how defaults are defined in schema
echo "=== Search for numeric default patterns in schema ==="
rg -n "\.default\(|numeric\|num\(" packages/db/src --type ts -B2 -A2 | head -50

Length of output: 2668



🏁 Script executed:

#!/bin/bash
# Find the organization table definition in schema and surrounding context
echo "=== Find organization table full definition ==="
rg -n "organization\|createTable" packages/db/src/schema.ts -A30 | head -80

Length of output: 47


🏁 Script executed:

#!/bin/bash
# Look for any generated SQL or migration output files
echo "=== Search for any SQL output or migration logs ==="
find packages/db -type f \( -name "*.sql" -o -name "*.log" -o -name "*.out" \) 2>/dev/null | xargs cat 2>/dev/null | head -50

Length of output: 1517


🏁 Script executed:

#!/bin/bash
# Check how Drizzle decimal defaults are defined in the codebase
echo "=== Review all decimal field defaults ==="
rg -n "decimal()" packages/db/src/schema.ts -B1 -A3

Length of output: 1509


🏁 Script executed:

#!/bin/bash
# Inspect the snapshot for the organization table specifically
echo "=== Extract organization table from snapshot ==="
jq '.ddl[] | select(.table == "organization")' packages/db/migrations/meta/1747930278_snapshot.json | head -100

Length of output: 1792



🏁 Script executed:

#!/bin/bash
# Search the SQL file for the organization table creation
echo "=== Find organization CREATE TABLE in generated SQL ==="
rg -n "CREATE TABLE.*organization\|\"organization\"" packages/db -A50 | head -100

Length of output: 55


🏁 Script executed:

#!/bin/bash
# Check if there's a specific migration file for organization.credits
echo "=== Search for migration files mentioning credits ==="
find packages/db/migrations -type f -name "*.sql" -exec grep -l "credits" {} \;

Length of output: 166


🏁 Script executed:

#!/bin/bash
# Verify Drizzle documentation pattern for decimal defaults
echo "=== Check other decimal default patterns for comparison ==="
jq '.ddl[] | select(.type == "numeric" and .default != null)' packages/db/migrations/meta/1747930278_snapshot.json

Length of output: 343



🏁 Script executed:

#!/bin/bash
# Read the migration files containing credits
echo "=== Migration 1747927048 ==="
cat packages/db/migrations/1747927048_flimsy_black_tom.sql

Length of output: 204


🏁 Script executed:

#!/bin/bash
# Read the second migration
echo "=== Migration 1749245675 ==="
cat packages/db/migrations/1749245675_cuddly_krista_starr.sql

Length of output: 652


Change default("0") to default(0) for numeric type correctness.

The generated SQL renders DEFAULT '0' (text literal with quotes) instead of the correct DEFAULT 0 (numeric literal). While PostgreSQL's implicit cast handles this, it violates type safety.

Schema location: packages/db/src/schema.ts:123
Current: credits: decimal().notNull().default("0")
Should be: credits: decimal().notNull().default(0)

The issue is confirmed in the migration: ALTER TABLE "organization" ADD COLUMN "credits" numeric DEFAULT '0' NOT NULL;

🤖 Prompt for AI Agents
In packages/db/migrations/meta/1747930278_snapshot.json around lines 912-926 and
in packages/db/src/schema.ts at line 123, the numeric column default is stored
as a string ('0') causing the migration to emit DEFAULT '0' instead of DEFAULT
0; change the schema to use a numeric default and update the migration snapshot:
in packages/db/src/schema.ts replace decimal().notNull().default("0") with
decimal().notNull().default(0), then regenerate or edit the migration snapshot
so the column's default is numeric (type numeric/unknown value 0 or equivalent
without quotes) and re-run migrations/tests to confirm the SQL now emits DEFAULT
0.

Merged via the queue into main with commit f9bd92a Nov 18, 2025
8 checks passed
@steebchen
steebchen deleted the chore/migrations branch November 18, 2025 09:27
@coderabbitai coderabbitai Bot mentioned this pull request Jan 16, 2026
6 tasks
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.

1 participant