Repository navigation
fix(target-postgres): bytea and vector literal defaults verify after they are applied (TML-3283) - #30603
Conversation
…(TML-3283) pg/bytea gains a canonical form: the base64 its codec writes, read from base64 or from the \x hex text PostgreSQL prints for a bytea default. The schema check applies it to both sides, so a bytea default verifies after it is applied, and contract infer prints it as a base64 literal. Like the date and time types, the same function is the cast from pg/text, so a PSL Bytes default may be written in PostgreSQL hex. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
…rints (TML-3283) pgvector/vector gains a canonical form: its array of numbers, kept as it is or read from the [1,2,3] text PostgreSQL prints. The schema check applies it to each element of a list default, so a vector(3)[] default verifies after the runner applies it. It adds no cast from text. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
…output = hex (TML-3283) Postgres prints a bytea default in the session's bytea_output format, so a session set to escape reported 'hello'::bytea where pg/bytea reads hex. Introspection now pins bytea_output = hex beside TimeZone, DateStyle and IntervalStyle, and restores the caller's value afterwards. The codec literal defaults test now applies its migration through the runner, since the schema check after it reads bytea defaults back. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
… CLI (TML-3283) A journey writes Bytes and Bytes[] defaults in base64 and in PostgreSQL hex, emits base64 for all of them, and runs db init, and separately migration plan and db migrate onto existing columns; strict db verify is clean and an inserted row holds the bytes of hello. contract infer prints a bytea default as a base64 literal, and the inferred schema emits and verifies clean. ADR 254 says which types read the text PostgreSQL prints. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
… it reads (TML-3283) The canonical-form inventory needs a table of values for every type that declares a canonical form, and pg/bytea now declares one. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
The app fragment covers the contract infer output change and the storage hash it brings. Extensions need no change. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
…es (TML-3283) Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
…ytea input (TML-3283) The text inside a SQL string literal for a bytea column is PostgreSQL bytea input: \x hex, or the escape format, where \\ is a backslash, a backslash and three octal digits is that byte, and any other character is its UTF-8 bytes. The default parser now turns that text into the base64 of its bytes, element by element for a list, and keeps text PostgreSQL refuses as the raw expression. Before, a raw SQL default 'aGVsbG8='::bytea was read as base64 and reported equal to a database default holding hello, and 'hello'::bytea failed the schema check after db init. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
…it (TML-3283) A PSL string reads \x68 as the character h, so Bytes @default("\x68656c6c6f") reached the cast as h656c6c6f, and the refusal suggested the same spelling the user wrote. The refusals now quote the value as a PSL string and give hex as "\\x68656c6c6f" next to the base64 example. ADR 254 does the same, and says a raw SQL bytea default is read as bytea input. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
…-3283) JSON cannot hold -0 and pgvector treats -0 and 0 as equal, but the comparison wrote them differently, so a database default [-0,1,2] did not match a contract [0,1,2]. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
…tea pin (TML-3283) The Prisma 7 binding comment now gives the reason the reader keeps raw SQL for Bytes and DateTime defaults: storing values changes contract hashes (TML-3455). The README says the bytea_output pin also rewrites a bytea constant in check, index and policy text on an escape server. typeWrittenAsText becomes typeCanonicalFromText, and the comment that repeated the introspection setting values is gone. Signed-off-by: willbot <[email protected]> Signed-off-by: Will Madden <[email protected]> Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Navigate logical layers of code changes, visualize relationships, and explore their blast radius. 🧰 Additional context used📚 Code guidelines (2)No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configuration
📒 Files selected for processing (22)
Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 8 remain after this review. 📝 WalkthroughWalkthroughThis change adds canonicalization for PostgreSQL bytea and pgvector values, normalizes bytea defaults from PostgreSQL output, and pins bytea introspection output to hex. It adds migration, schema verification, inference, and CLI coverage, plus upgrade instructions for affected bytea defaults. ChangesPostgreSQL Default Canonicalization
Priority: ➖ Normal Estimated code review effort: 3 (Moderate) | ~25 minutes Change: Bug fix Suggested reviewers: Merge Risk: ⚪ Minimal · up to No confirmed issue blocks merging. Follow the documented signing or migration steps when deploying re-emitted bytea contracts. Security Architecture ReviewSecurity architecture risk: 🔵 Low · up to The change uses existing validation and comparison mechanisms without establishing a new privilege or authentication boundary. Risk is bounded mainly to shared-connection output settings and coordinating re-generated contracts with database markers before deployment. Retained concerns
Security review detailsSecurity Blast Radius
Security Findings and Attack Paths
Trust Boundaries and Controls
Resilience and Maintainability Implications
Hardening Proposals
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 24.14% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 29 functions across 16 files. (6 skipped: 6 unsupported.)
✨ Finishing Touches 💡 1📝 Generate docstrings 💡
🧪 Generate unit tests (beta)
Comment |
@prisma/orm-extension-arktype-json
@prisma/orm-extension-middleware-cache
@prisma/orm-extension-paradedb
@prisma/orm-extension-pgvector
@prisma/orm-extension-postgis
@prisma/orm-extension-supabase
@prisma/orm-family-mongo
@prisma/orm-family-sql
@prisma/orm-framework
@prisma/orm-mongo
@prisma/orm-postgres
@prisma/orm-sqlite
@prisma/orm-target-mongo
@prisma/orm-target-postgres
@prisma/orm-target-sqlite
@prisma/orm-toolchain
commit: |
size-limit report 📦
|
At a glance
On
main,prisma db initwrites this table correctly and then rolls it back withMIGRATION.SCHEMA_VERIFY_FAILED. Both defaults are reported as mismatches: the contract expects"aGVsbG8="and the database has"\x68656c6c6f".aGVsbG8=is the base64 form of the bytes ofhello, which the contract stores.\x68656c6c6fis the same bytes as Postgres prints them. The schema check that runs after applying compared the two spellings as they stood, so everyBytesliteral default faileddb init,db updateanddb migrate. A pgvectorvector(3)[]list default failed the same way.With this PR all three commands apply these defaults, and
db verify --strictis clean.Refs: TML-3283
The decision
Each Postgres data type that Postgres prints differently from the contract already says how to read that text.
interval,timetz, the date types,int8andnumericeach declare atoCanonicalForm, and the default comparison applies it to both sides before comparing (#30532). This PR adds the same forbyteaand for pgvector'svector. It does not add a new mechanism.How it fits together
pg/byteareads base64 or Postgres's\xhex and returns the base64 the contract stores. As for the date types, the same function is the type's cast from text. So a PSLBytesdefault may be written either way, and is stored as base64:@default("\\x68656c6c6f")storesaGVsbG8=. The backslash is doubled because\x68inside a PSL string is the letterh. The refusal message for a malformed value shows that spelling.vectorreads Postgres's[1,2,3]text into the number array the contract stores, and reads-0as0.bytea_output = 'hex', besideTimeZone,DateStyleandIntervalStyle, and restores the caller's value afterwards. A server set toescapeprints abyteadefault as'hello'::bytea, which the canonical form could not read as hex.byteadefault is read as Postgres bytea input. The text inside a SQL string literal for abyteacolumn is hex or escape format, never base64. So the Postgres default parser turns it into the base64 of its bytes. Without this,sql`'aGVsbG8='::bytea, which holds the 8 ASCII bytes of that text, would have compared equal tohello. As a result,sql`'hello'::byteanow applies and verifies; onmainit failed too.parsePostgresDefaultgains only the bytea input reading. The shared SQL family code and the codecs are unchanged.What users see change
contract infernow prints abyteadefault as a literal,@default("aGVsbG8="), where it printed@default(sql`'\x68656c6c6f'::bytea`). Thesqldefaults it printed before still verify. Re-inferring gives a contract with a new storage hash. The upgrade note inupgrade-instructions/pending/bytea-defaults-verify/says how to sign or migrate to it.Escape-format text in a raw SQL default is read as UTF-8, which matches Postgres on a UTF-8 database.
Testing performed
Manual, through the CLI in a project made with
prisma orm init, against a PostgreSQL 15 server logging every statement:contract emitstoresaGVsbG8=for"aGVsbG8=",["aGVsbG8="]and"\\x68656c6c6f".db initsucceeds,db verify --strictis clean, and an inserted row holds the bytes ofhello.migration plananddb migratesucceeds and verifies clean.bytea_output = 'escape'set on the database and on the role, verify is still clean. The server log shows the CLI switching tohexand restoringescape.contract inferon a hand-made table prints@default("aGVsbG8="). The inferred schema emits and verifies clean.contract emitwithPSL_INVALID_LITERAL, naming the field and giving a correct example.vector(3)[]default through TypeScript authoring applies and verifies clean.Automated:
db init,migration planthendb migrate, strict verify, the stored bytes, and thecontract inferround trip.byteadefaults written as raw SQL in escape and hex form, including octal escapes and lists, apply and verify.hello.vector(3)[]default applies and verifies, both when the table is created and throughsetDefault.byteadefault applies and verifies in a session set to escape output.mainbefore its fix.pnpm build,pnpm typecheck, lint and coverage for every touched package,pnpm fixtures:check,pnpm lint:deps,pnpm check:upgrade-coverage, and the bytea, data-type-default and Prisma 7 integration files.Not fixed here
inetwritten as/32or in upper case, andtsquerywritten asfat & rat, still fail verify, because the contract keeps the written spelling. So does a vector value thatrealrounds.standard_conforming_strings = off,byteadefaults are written as the wrong bytes. This is already the case onmain.BytesandDateTimedefaults as raw SQL. Switching them to values changes contract hashes.Alternatives considered
codec.decodeandencodeJson. This was the earlier plan for TML-3283, and it does not fit:Codec.decodeis async, while the default comparison is synchronous.IntervalStyle = 'postgres'. So the interval defaults that verify today would fail.contract inferhas no contract, so it would have to choose a codec from a native type. That cannot build a parameterized codec such asvector(3).byteadefault would stop verifying.bytearule inparsePostgresDefaultalone. That would copy the codec's hex reading a third time, could not cover pgvector, and would leave PSL unable to accept hex.Agent: atreides-42
🤖 Generated with Claude Code
Summary by CodeRabbit
byteadefaults in hex and base64 forms, including defaults within arrays.byteaoutput and restores the original session settings afterward.byteadefaults may change the contract hash without changing the database. Re-emit the contract, then verify and sign the database or apply an empty migration before deploying.