Skip to content

[Mysql] Fix binary/varbinary column types as Buffer#5800

Open
ThaiTrevor wants to merge 2 commits into
drizzle-team:mainfrom
ThaiTrevor:fix/issue-1188-drizzle-orm-bug-mysql2-binary-varbinary
Open

[Mysql] Fix binary/varbinary column types as Buffer#5800
ThaiTrevor wants to merge 2 commits into
drizzle-team:mainfrom
ThaiTrevor:fix/issue-1188-drizzle-orm-bug-mysql2-binary-varbinary

Conversation

@ThaiTrevor
Copy link
Copy Markdown

🤖 AI-assisted contribution — This PR was drafted with AI assistance and reviewed by a native Vietnamese speaker before submission. Placeholders, terminology, and file format were validated automatically. I will respond to review feedback. Happy to revise or close if not a fit.


Summary

Closes #1188.

mysql2 returns BINARY / VARBINARY columns as a Node Buffer, but drizzle-orm typed the column data as string and lossily called Buffer.prototype.toString() on the driver value in mapFromDriverValue. That silently corrupted any non-UTF-8 binary content (e.g. a UUID stored as binary(16), or a hash stored as varbinary(32)).

This aligns the column data type with what the driver actually returns:

  • dataType is now 'buffer' (same convention as sqlite-core blob in buffer mode).
  • The inferred data type is Buffer; driverParam accepts Buffer | string since mysql2 also accepts hex strings as input.
  • mapFromDriverValue returns the Buffer as-is (or wraps a Uint8Array / string via Buffer.from) instead of stringifying it.

Files changed

  • drizzle-orm/src/mysql-core/columns/binary.ts
  • drizzle-orm/src/mysql-core/columns/varbinary.ts
  • drizzle-orm/type-tests/mysql/tables.ts — update .default('') to .default(Buffer.from('')) so the existing type test continues to compile against the new type.
  • integration-tests/tests/mysql/mysql-common.ts — update the mysql "all types" suite to insert/expect Buffer for the binary and varbin columns.

Note for maintainers

This is a typed-API change for users who relied on the previous string inference, but the previous runtime behaviour was already broken for true binary payloads (silent UTF-8 conversion), so this aligns the types with the driver's real output and lets users store/retrieve binary data safely.

The parallel files in singlestore-core (columns/binary.ts, columns/varbinary.ts) share the same broken pattern; they are intentionally left untouched here to keep this PR scoped to the issue, but happy to apply the same fix in a follow-up if you'd like.

Test plan

  • pnpm --filter drizzle-orm test:types — type-tests in drizzle-orm/type-tests/mysql/tables.ts now assert Buffer instead of string for binary / varbinary.
  • cd integration-tests && pnpm test — mysql "all types" test inserts a Buffer and asserts it is returned as Buffer.

Closes drizzle-team#1188

mysql2 returns BINARY/VARBINARY columns as Node Buffer, but drizzle-orm
inferred them as `string` and lossily called `.toString()` on the Buffer
in `mapFromDriverValue`, corrupting arbitrary binary content (e.g. a
UUID stored as `binary(16)`).

Align the column data type with the driver:
- `dataType` is now `'buffer'` (same convention as `sqlite-core` blob in
  buffer mode).
- The inferred `data` type is `Buffer`; `driverParam` accepts
  `Buffer | string` since mysql2 also accepts hex strings.
- `mapFromDriverValue` returns the `Buffer` as-is (or wraps a
  `Uint8Array`/`string` via `Buffer.from`).

Updates the type-test default values and the mysql "all types"
integration test expectations to use `Buffer` accordingly.
@ThaiTrevor ThaiTrevor marked this pull request as ready for review May 25, 2026 17:02
@ThaiTrevor
Copy link
Copy Markdown
Author

Quick check-in — anything I can adjust to make this easier to review? Happy to break it down, add tests, or revise the approach. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: MySQL2 binary/varbinary types are incorrectly typed as strings instead of buffers

1 participant