Skip to content

bug: pnpm db:pull rewrites schema.ts and relations.ts, dropping typed json + shared imports #628

Description

@mcmxcdev

Describe the bug

Running pnpm db:pull (which runs drizzle-kit pull) in packages/db completely rewrites packages/db/drizzle/schema.ts and packages/db/drizzle/relations.ts, discarding hand-maintained customizations that the introspector cannot reproduce. On the current main (ad24abc0), a single db:pull produces a ~1,570-line diff across the two files (+752 / -820 in one run).

The regeneration is lossy in ways that break type safety and our lint/format conventions, so the output can't just be committed as-is — it has to be manually reconciled every time, which defeats the purpose of the pull.

What gets lost / changed

  1. Typed json columns lose their $type<>() annotations. Hand-written columns like:

    settings: json().$type<SlackSpacesMeta>(),
    meta: json().$type<UserMeta>(),
    eventMeta: json("event_meta").$type<EventMeta>(),

    are regenerated as untyped json(). This drops compile-time type safety on every meta column (SlackSpacesMeta, SlackUserMeta, UserMeta, EventMeta, OrgMeta, LocationMeta, AttendanceMeta, UpdateRequestMeta).

  2. Shared enum/type imports are dropped and enums are re-inlined. The maintained file imports enums and meta types from @acme/shared/app/enums and @acme/shared/app/types:

    import { AchievementCadence, DayOfWeek, /* … */ } from "@acme/shared/app/enums";
    import type { AttendanceMeta, EventMeta, /* … */ } from "@acme/shared/app/types";

    db:pull deletes these imports and re-inlines every enum as a literal pgEnum(...) definition, so the schema no longer shares a single source of truth with the app.

  3. Import ordering is scrambled in both files (imports are emitted in introspection order, not our prettier/eslint sorted order), producing large no-op churn on top of the real changes.

To reproduce

  1. cd packages/db
  2. pnpm db:pull
  3. git diff drizzle/schema.ts drizzle/relations.ts

Observe a full rewrite of both files, with the losses listed above.

Expected

pnpm db:pull should be usable to sync schema changes from the database without destroying the typed-json annotations, shared @acme/shared imports, and import ordering — or the workflow should be documented so a pull can be reconciled against the maintained file instead of overwriting it.

Additional information

  • drizzle.config.ts points schema at ./drizzle/schema.ts and out at ../db/drizzle, so the pull writes back over the hand-maintained files.
  • Affected files: packages/db/drizzle/schema.ts, packages/db/drizzle/relations.ts.
  • Observed on main @ ad24abc0.

Possible directions

  • Introspect into a separate throwaway path (the config already has a commented out: "../db/src/schema" line) and diff/merge manually, keeping drizzle/schema.ts as the authored source.
  • Or add a post-pull codemod/script that re-applies the $type<>() annotations, shared imports, and formatting.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    Fields

    Priority

    Medium

    Effort

    Medium

    App

    Database

    Projects

    Status
    In progress

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions