oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
71.82k stars 2.55k forks source link

Drizzle-kit support with --bun #8545

Open itsyoboieltr opened 5 months ago

itsyoboieltr commented 5 months ago

What version of Bun is running?

1.0.25+a8ff7be64

What platform is your computer?

Darwin 23.2.0 arm64 arm

What steps can reproduce the bug?

Run any command with drizzle-kit using --bun, for example: drizzle-kit generate:pg or drizzle-kit push:pg. The command should execute without any errors, but it cannot see any db schema files, so the effect of the command will be invalid, most of the time this results in bun dropping your whole db. The reason for this is detailed in this issue on the drizzle-kit-mirror repository: #199

I found another way to fix it. It's kind of working, but we can use export in schema.ts First, go to node_modules/drizzle-kit/bin.js and find src/serializer/Imports.ts(pg for pgImports.ts and mysql for mysqlImports.ts). And, change forEach statement in prepareFromExports or prepareFromMySqlImports(function that contains forEach) to for ... in statement. The problem is that Bun's Object.values or Object.keys for the Module object (a return value of require) give us nothing unlike Nodejs. const schema = require('./db/schema.ts') const exports = Object.values(schema) // [] and somehow for ... in can iterate the object even in Bun.

For reproduction, feel free to use this repo: dbest-stack

  1. Set the variable DATABASE_URL inside .env to the url of your postgres database
  2. Run bun push
  3. The default command used by the repo is drizzle-kit push:pg (because bun --bun drizzle-kit push:pg is buggy), so it should push your schema successfully.
  4. Change the push script inside the package.json from drizzle-kit push:pg to bun --bun drizzle-kit push:pg
  5. Run bun push
  6. Watch your database disappear (lol)

What is the expected behavior?

The db schema is pushed, migrations are generated.

What do you see instead?

The db is dropped (on push), the migrations are not generated.

Additional information

Seems to be something with Object.values or require?

atadeuccislideworks commented 3 months ago

On windows I'm having a similar issue but it just crashes instead


PS D:\company\project-api> bunx --bun drizzle-kit studio
drizzle-kit: v0.20.14
drizzle-orm: v0.30.7

No config path provided, using default path
Reading config file 'D:\company\project-api\drizzle.config.ts'

[Warning] Drizzle Studio is currently in Beta. If you find anything that is not working as expected or should be improved, feel free to create an issue on GitHub: https://github.com/drizzle-team/drizzle-kit-mirror/issues/new or write to us on Discord: https://discord.gg/WcRKz2FFxN
46193 |         $2.log({
46194 |           kind: "cmd",
46195 |           cmd: this._command,
46196 |           verbose: $2.verbose && !this._quiet
46197 |         });
46198 |         this.child = $2.spawn($2.prefix + this._command, {
                             ^
TypeError: Executable not found in $PATH: "C:\Program Files\Git\bin\bash.EXE"
 code: "ERR_INVALID_ARG_TYPE"

      at spawn (node:child_process:650:15)
      at node:child_process:2:41
      at run (D:\company\project-api\node_modules\drizzle-kit\bin.cjs:46198:22)
      at D:\company\project-api\node_modules\drizzle-kit\bin.cjs:46150:59
46193 |         $2.log({
46194 |           kind: "cmd",
46195 |           cmd: this._command,
46196 |           verbose: $2.verbose && !this._quiet
46197 |         });
46198 |         this.child = $2.spawn($2.prefix + this._command, {
                             ^
TypeError: Executable not found in $PATH: "C:\Program Files\Git\bin\bash.EXE"
 code: "ERR_INVALID_ARG_TYPE"

      at spawn (node:child_process:650:15)
      at node:child_process:2:41
      at run (D:\company\project-api\node_modules\drizzle-kit\bin.cjs:46198:22)
      at D:\company\project-api\node_modules\drizzle-kit\bin.cjs:46150:59```
0xVinCal commented 2 months ago

Related issue: https://github.com/drizzle-team/drizzle-kit-mirror/issues/199