Skip to content
This repository was archived by the owner on Nov 25, 2026. It is now read-only.

TML-1662: Store column native type in contract - #30

Merged
wmadden merged 39 commits into
mainfrom
column-native-type-in-contract
Dec 5, 2025
Merged

wmadden merged 39 commits into
mainfrom
column-native-type-in-contract

Conversation

@wmadden

@wmadden wmadden commented Nov 29, 2025 •

Copy link
Copy Markdown
Contributor

Currently we store the column type as the codec identifier. We need to be able to verify
the database schema using only the contract data stored in the database, so without knowing
the column-codec mapping we can't do that.

This makes it much simpler: the contract columns will store nativeType and codecId.
Codec information will be ignored by the migration system and the database when comparing
contract to schema. Application query DSLs can ignore the native type and use the codec ID
exclusively.

Summary of changes to implement the “native + codec types” brief

  • SQL contract column shape and validation

    • Updated StorageColumn in @prisma-next/sql-contract to require nativeType: string + codecId: string + nullable: boolean for all SQL columns.
    • Adjusted Arktype validators in both sql-contract and sql-contract-ts so contracts and storage structures are only valid when both nativeType and codecId are present.
    • Updated the SQL contract README and factories (col, table, storage, contract) and tests to use the new { nativeType, codecId, nullable } shape everywhere.
  • Contract authoring and example contracts

    • Refactored the SQL TS contract builder (@prisma-next/sql-contract-ts/contract-builder) so .build():
      • Reads codecId from the column builder state.
      • Requires a nativeType on each column (set when the column is defined), and throws if it is missing.
      • Emits storage.tables.*.columns.* as { nativeType, codecId, nullable }.
    • Updated the demo app contract (examples/prisma-next-demo/prisma/contract.ts) to use column-type descriptors like int4Column, textColumn, timestamptzColumn, and vectorColumn, which carry both nativeType and codecId into the builder.
  • Schema IR and verification

    • Clarified SqlColumnIR in @prisma-next/sql-schema-ir as:
      • typeId = codec ID, nativeType? = DB-specific type.
    • Ensured the Postgres control adapter sets SqlColumnIR.nativeType for introspected columns based on information_schema (handling varchar, numeric, etc.).
    • Updated SQL family schemaVerify to:
      • Compare contractColumn.nativeType to schemaColumn.nativeType ?? schemaColumn.typeId for type compatibility.
      • Keep nullability checks separate.
      • Use the SQL type metadata registry to warn when codecId’s registry nativeType disagrees with the contract’s nativeType, without blocking structural validation.
  • Migration planner (planMigration)

    • Updated the SQL migration planner in @prisma-next/sql-migrations so columnTypeCompatible:
      • Compares contract.nativeType to schema.nativeType (no longer mixing codec IDs with native types).
      • Applies the same nullability rules as db init requires (no narrowing; widening allowed).
    • Left the rest of the planner logic intact: it still plans additive operations (create table, add column/PK/UK/FK/index, extension ops), tolerates schema supersets, fills in subsets, and fails on real conflicts.
  • Lanes, runtime, and emitter

    • Updated SQL lane, ORM lane, and relational-core logic to treat codecId as the only type ID for codecs and type inference (replacing usages of StorageColumn.type).
    • Adjusted SQL runtime codec validation to collect and check codecIds from columns.
    • Tightened the SQL contract emitter so it:
      • Validates that every column has nativeType: string and codecId: string.
      • Emits contract.d.ts with the enriched column type { nativeType, codecId, nullable }, making the generated contract type reflect both DB structure and codec configuration.

Summary by CodeRabbit

  • New Features

    • Public column-type descriptors added (including vector) for reuse.
  • Improvements

    • Contracts now represent columns with nativeType + codecId; validation, emit, tooling and runtime flows updated.
    • Query/plan metadata and parameter descriptors expose codecId and nativeType instead of the prior type field.
    • Better separation of schema vs logical validation; reduced redundant re-validation.
  • Tests

    • Tests updated to new column shape; added per-test temp-dir helpers and disk-based contract emit/load utilities.
  • Chores

    • Added package exports and build entries for column-type and test descriptor modules.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Nov 29, 2025 •

Copy link