drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

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

Open tslater opened this issue 2 years ago • 10 comments

What version of drizzle-orm are you using?

0.28.5

What version of drizzle-kit are you using?

0.19.13

Describe the Bug

The mysql2 library parses binary & varbinary columns as Buffer.

They also automatically convert Buffers to hex strings, so passing those in to a query is escaped correctly.

Expected behavior

The type should be Buffer

Environment & setup

Generally

tslater avatar Sep 07 '23 17:09 tslater

There was a pull request for this, but it has been closed due to complications between the planetscale and mysql2 adapters differing in terms of behavior: https://github.com/drizzle-team/drizzle-orm/pull/425

tslater avatar Sep 07 '23 17:09 tslater

💎 $200 bounty created by @tslater 👉 To claim this bounty, submit your pull request on Algora 📝 Before proceeding, please make sure you can receive payouts in your country 💵 Payment arrives in your account 2-5 days after the bounty is rewarded 💯 You keep 100% of the bounty award 🙏 Thank you for contributing to drizzle-team/drizzle-orm!

algora-pbc avatar Sep 07 '23 17:09 algora-pbc

There's anyone already working on it?

I would like to help with it 😄

wesleymatosdev avatar Sep 15 '23 13:09 wesleymatosdev

@ologbonowiwi Do you think you can pound through it quickly? I thought there was someone on here that said they wanted to, but I only see it in my email and not here. I wonder if it was deleted? I say go for it since it's been a week and no sign of them.

tslater avatar Sep 15 '23 22:09 tslater

I'm not sure @tslater.

I want to help but I don't know the project internals, so it'll be quite a challenge.

/attempt #1188

wesleymatosdev avatar Sep 16 '23 09:09 wesleymatosdev

@ologbonowiwi Hey! Thanks for sending me a DM on linkedin, I have my github completely muted and missed this.

I had tried getting it working but as in the PR, the mysql2 package (which is API compliant with the mysql package) parses binary and varbinary columns as Buffer while the @planetscale/database parses them as string | null.

I tried playing around to get it to work but ran into the issue that they both rely on the drizzle-orm/mysql-core sub-package (specifically the MySqlVarBinaryBuilderInitial). So changing the column definitions in one changes them in the other.

Life got busy and I converted to neon / postgress in my projects so I abandoned it.

Hope I could help, but this is pretty complicated and I'm not sure how to fix it. Let me know if you need anything else!

ryanagillie avatar Sep 19 '23 00:09 ryanagillie

Thanks for the clarification, @ryanagillie; only this comment helped a lot!

wesleymatosdev avatar Sep 19 '23 02:09 wesleymatosdev

The work on #1253 from my side is mostly done, this was the last update from @AndriiSherman on the discord thread.

image

Posting it here so we have the issue updated.

wesleymatosdev avatar Jan 30 '24 15:01 wesleymatosdev

@AndriiSherman any updates?

tslater avatar Jul 16 '24 21:07 tslater

a workaround for this ( tested with mysql2 driver ):

https://orm.drizzle.team/docs/custom-types#examples

const mediumblob = (name: string) => customType<{ data: Buffer; driverData: string }>({
  dataType() {
    return 'mediumblob';
  },
  fromDriver(value) {
    return Buffer.from(value);
  },

  toDriver(value: Buffer): string {
    // @ts-ignore
    return value;
  },
})(name);

trim21 avatar Sep 30 '24 10:09 trim21

FYI since it's been over a year, I'm removing the bounty. Can't be on the hook forever...

💎 $200 bounty created by @tslater 👉 To claim this bounty, submit your pull request on Algora 📝 Before proceeding, please make sure you can receive payouts in your country 💵 Payment arrives in your account 2-5 days after the bounty is rewarded 💯 You keep 100% of the bounty award 🙏 Thank you for contributing to drizzle-team/drizzle-orm!

tslater avatar Apr 16 '25 20:04 tslater

The work was done on my side, but unfortunately Drizzle team doesn't review a lot of PRs. I asked about it dozens of times, but no response from them...

A tip would be appreciate tho, since the work was done on my side...

wesleymatosdev avatar Apr 16 '25 21:04 wesleymatosdev

I also encountered the same issue and was struggling with it, but I found a type-safe solution as shown below. I hope it will be helpful to someone.

  • https://github.com/drizzle-team/drizzle-orm/issues/4751#issuecomment-3167225777

I thank @trim21 for his suggestions about customType function!

hiro1989 avatar Aug 08 '25 12:08 hiro1989

Hey everyone!

I've created this message to send in a batch to all opened issues we have, just because there are a lot of them and I want to update all of you with our current work, why issues are not responded to, and the amount of work that has been done by our team over ~8 months.

I saw a lot of issues with suggestions on how to fix something while we were not responding – so thanks everyone. Also, thanks to everyone patiently waiting for a response from us and continuing to use Drizzle!

We currently have 4 major branches with a lot of work done. Each branch was handled by different devs and teams to make sure we could make all the changes in parallel.


First branch is drizzle-kit rewrite

All of the work can be found on the alternation-engine branch. Here is a PR with the work done: https://github.com/drizzle-team/drizzle-orm/pull/4439

As you can see, it has 167k added lines of code and 67k removed, which means we've completely rewritten the drizzle-kit alternation engine, the way we handle diffs for each dialect, together with expanding our test suite from 600 tests to ~9k test units for all different types of actions you can do with kit. More importantly, we changed the migration folder structure and made commutative migrations, so you won't face complex conflicts on migrations when working in a team.

What's left here:

  • We are finishing handling defaults for Postgres, the last being geometry (yes, we fixed the srid issue here as well).
  • We are finishing commutative migrations for all dialects.
  • We are finishing up the command, so the migration flow will be as simple as drizzle-kit up for you.

Where it brings us:

  • We are getting drizzle-kit into a new good shape where we can call it [email protected]!

Timeline:

  • We need ~2 weeks to finish all of the above and send this branch to beta for testing.

Second big branch is a complex one with several HUGE updates

  • Bringing Relational Queries v2 finally live. We've done a lot of work here to actually make it faster than RQBv1 and much better from a DX point of view. But in implementing it, we had to make another big rewrite, so we completely rewrote the drizzle-orm type system, which made it much simpler and improved type performance by ~21.4x:
(types instantiations for 3300 lines production drizzle schema + 990 lines relations)

TS v5.8.3: 728.8k -> 34.1k
TS v5.9.2: 553.7k -> 25.4k

You can read more about it here.

What's left here:

Where it brings us:

  • We are getting drizzle-orm into a new good shape where we can call it [email protected]!

Breaking changes:

  • We will have them, but we will have open channels for everyone building on top of drizzle types, so we can guide you through all the changes.

Third branch is adding support for CockroachDB and MSSQL dialects

Support for them is already in the alternation-engine branch and will be available together with the drizzle-kit rewrite.

Summary

All of the work we are doing is crucial and should be done sooner rather than later. We've received a lot of feedback and worked really hard to find the best strategies and decisions for API, DX, architecture, etc., so we can confidently mark it as v1 and be sure we can improve it and remain flexible for all the features you are asking for, while becoming even better for everyone building on top of the drizzle API as well.

We didn't want to stay with some legacy decisions and solutions we had, and instead wanted to shape Drizzle in a way that will be best looking ahead to 2025–2026 trends (v1 will get proper effect support, etc.).

We believe that all of the effort we've put in will boost Drizzle and benefit everyone using it.

Thanks everyone, as we said, we are here to stay for a long time to build a great tool together!

Timelines

We are hoping to get v1 for drizzle in beta this fall and same timeline for latest. Right after that we can go through all of the issues and PRs and resond everyone. v1 for drizzle should close ~70% of all the bug tickets we have, so on beta release we will start marking them as closed!

AndriiSherman avatar Aug 30 '25 18:08 AndriiSherman

I closed the PR https://github.com/drizzle-team/drizzle-orm/pull/1253. will be easier to rewrite from the new implementation than to fix it after the breaking changes.

Resources in case anyone wants to tackle this:

  • https://github.com/drizzle-team/drizzle-orm/pull/425 from @ryanagillie
  • my PR: https://github.com/drizzle-team/drizzle-orm/pull/1253
  • workaround by @hiro1989

wesleymatosdev avatar Oct 08 '25 01:10 wesleymatosdev