Repository navigation
Postgres enum types and unique constraints - #27
Conversation
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
PG.enumType declares a Postgres enum that is both a column type (rows decode to its labels, inserts and comparisons take only them) and a schema object: generate creates it before the tables that use it, adds labels in place with ADD VALUE ... BEFORE, renames one behind a confirm_rename hint, and asks before dropping it. Removing or reordering labels, or using a label in the migration that adds it, is reported as unsupported. PG.unique and the column-level `unique` option declare UNIQUE constraints, diffed and verified as constraints rather than indexes, so a database drizzle-kit pushed verifies clean and a changed constraint is dropped with DROP CONSTRAINT. verify reads enums from pg_enum and unique constraints from pg_constraint, and searches the current schema after its scratch schema, so types of the current schema deparse alike on both sides. The drizzle-kit importer converts enums, enum columns and unique constraints. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Runnable and CHWrite took CHInsert<any, ...>. TypeScript 5 compares a concrete insert with it method by method, and the generic onConflictDoUpdate fails that comparison: ConflictTarget<any> is any string, and `any` is not assignable to a `never` Computed. TypeScript 7 accepts it, so the repo never saw it. Both now take AnyInsert, an insert by its tag, state and phantom, which is all compiling and running read. check-package typechecks a dedicated consumer file with TypeScript 5.9 as well, in its own program, since TypeScript 5's variance measurement depends on which comparison runs first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… checks Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to No actionable issue has been established that would prevent merging after normal checks. Pre-merge checks ·
|
Adds first-class Postgres enums and
UNIQUEconstraints across the query side, DDL, snapshots, the diff,generate, migrations,verifyand the drizzle-kit importer. Hazel hit both gaps moving off drizzle (MapleTechLabs/hazel#330). Also fixesRunnableunder TypeScript 5.API
PG.enumTypeis both a column type and a schema object (S.isSchemaObject). It reads, writes and compares likePG.brand(PG.text, Schema.Literals(...)): a value that is not a label is a type error. Exporting it is optional, because tables bring the enums they use. Drivers send enum arrays as text ({a,b}), soarray(enum)decodes that form and writes array text.PG.unique/ columnuniquedeclareADD CONSTRAINT ... UNIQUE, not unique indexes.uniquedoes not make a column optional on insert.onConflict*takes the constraint's columns or{ constraint }as before.Generate / migrate
CREATE TYPEruns first andDROP TYPElast. Dropping a type needs aconfirm_data_losshint (kind: "enum").CREATE TYPEis guarded byduplicate_object, so moving a hand-madePG.custom("public.x")enum toenumTypeapplies cleanly.ADD VALUE ... BEFORE <next>, so the order is kept.RENAME VALUE, behind a newconfirm_renamehint.unsafe use of new value; a live test confirms it. The docs cover this and the PG12 requirement forADD VALUEin a transaction.uniqueIndex→uniquewith the same name drops the index first.Verify
pg_enumand unique constraints frompg_constraint(contype = 'u',NULLS NOT DISTINCTread from the definition, so it works before PG15). A constraint's backing index is never treated as an index.publictypes resolve, and both sides deparse them unqualified. This removes the falsepublic.xvsxdrift in types, defaults and predicates.PG.customenums still verify clean.Drizzle importer
Converts
enums(public→ unqualified), enum columns (typeSchema), anduniques.Tests
src/pg/enum.test-d.ts): row, insert and comparison types.--hints, removal refused.src/migrate/pg-enums-uniques.test.ts): migrate/verify round trips through label add, rename, index→constraint, type drop; drift detection; an enum in another schema; thePG.customworkaround and its move toenumType; and the Hazel case, a drizzle-kit-pushed database with enums and unique constraints adopted via baseline that verifies clean and generates nothing.Other commits
Runnable/CHWritetookCHInsert<any, …>. TS 5 compares that method by method, and the genericonConflictDoUpdatefails the check, sorun(insert)andcompile(insert)were type errors for consumers on TS 5. They now takeAnyInsert(tag, state, phantom). There is a test-d for it.check-packagealso typechecks a dedicated consumer file with TS 5.9, in its own program because TS 5 variance measurement is order-dependent. That check fails without the fix.union:crossTenantcoverage gap onmainmadebun run testfail; it is now covered.bun run build,bun run typecheck,bun run testandbun run test:packagepass. There are two changesets: minor (features) and patch (Runnable).🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need.Summary by CodeRabbit