[Pg-kit] collect enums from table columns when not directly exported#5791
Open
lukpod1 wants to merge 1 commit into
Open
[Pg-kit] collect enums from table columns when not directly exported#5791lukpod1 wants to merge 1 commit into
lukpod1 wants to merge 1 commit into
Conversation
0b9ec88 to
9311755
Compare
Enums defined in a separate file (e.g. enums.ts) and imported into schema.ts for use in table columns — but not re-exported — were invisible to prepareFromExports. generatePgSnapshot received enums=[], so pushing to a clean database omitted all CREATE TYPE statements and produced "type does not exist" errors. After collecting tables, walk each table's columns via getTableColumns and add any PgEnum found in column.enum that hasn't already been collected.
9311755 to
9ef14fa
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When enums are defined in a separate file (e.g.
enums.ts) and imported intoschema.tsonly for use in table columns — without being re-exported —prepareFromExportsnever sees them.generatePgSnapshotthen receivesenums = [], so pushing to a clean database emits theCREATE TABLEstatements that reference the types but skips theCREATE TYPEstatements entirely, producing:This is a common pattern: splitting schema definitions across multiple files is encouraged in the drizzle docs, and there is no obvious reason a user would know they must re-export every enum that a table column references.
Root cause
prepareFromExportsonly scansObject.values(exports). Enums that are imported transitively (used in columns but not re-exported) are never present in that object.Fix
After collecting tables, walk each table's columns via
getTableColumns(already imported). If a column carries a.enumproperty that satisfiesisPgEnum(already imported) and the enum hasn't been collected yet, add it to the list.No new imports or dependencies required.
Tests
Added
drizzle-kit/tests/pg-imports.test.tswith two cases: