bun
bun copied to clipboard
Drizzle migrations not working
What version of Bun is running?
1.0.14+d8be3e51b
What platform is your computer?
Microsoft Windows NT 10.0.22631.0 x64
What steps can reproduce the bug?
- bun add drizzle-orm postgres
- bun add -D drizzle-kit
- Add a sample schema file under ./src/db as schema.ts
- Add a script in the package.json "migration:generate": "drizzle-kit generate:pg --schema=./src/db/schema.ts"
- Run
bun run migration:generate
What is the expected behavior?
The migration to get created and generate the sql file
What do you see instead?
Additional information
That's my first issue so I hope that my report is a legit one and it's well explained. I've struggled with this for a hour before simply deciding to try out on node for comparison to understand whether I was doing something wrong or not. I cannot seem to find a way to overcome this issue with bun, with or without the drizzle.config.ts file. I've ran a simple migration but everything I get is the journal.json file containing: {"version":"5","dialect":"pg","entries":[]}
Use drizzle-kit generate:pg instead
Use
drizzle-kit generate:pginstead
You mean without defining the schema path ?
I've find out that this isn't only a matter of generating migrations but I also cannot connect to postgres with introspect;pg. I suppose there's something going on with pg and bun..
I didn't define the path on the cli call, because I use the drizzle.config.ts configuration:
import { serverEnv } from "@/envServer.mjs"
import type { Config } from "drizzle-kit"
export default {
schema: "./src/data/schema/*",
out: "./src/data/migrations",
driver: "pg",
dbCredentials: {
connectionString: serverEnv.CONNECTION_STRING,
},
} satisfies Config
edit:
I've followed this doc: https://orm.drizzle.team/docs/migrations
I didn't define the path on the cli call, because I use the drizzle.config.ts configuration:
import { serverEnv } from "@/envServer.mjs" import type { Config } from "drizzle-kit" export default { schema: "./src/data/schema/*", out: "./src/data/migrations", driver: "pg", dbCredentials: { connectionString: serverEnv.CONNECTION_STRING, }, } satisfies Configedit:
I've followed this doc: https://orm.drizzle.team/docs/migrations
I've followed that as well, the main point is the fact that for some reason, even by pointing within the CLI to the specific schema, does not generate any migration. Although the equivalent, with npm, works accordingly. So I'm guessing there's either a problem on drizzle pg-core package, or maybe I've got some dirty bun package dep. I've also tried removing the bun.lockb and reinstalling the modules but nothing changed.
I've tried also following the tutorial here on a brand new project, following it step by step, still didn't work out: https://bun.sh/guides/ecosystem/drizzle
can confirm this is happening to me too on bun 1.0.15
Just install node js to your system and try run bun run migration:generate again it will solved your issue
I have same issue with latest Bun and Sqlite locally. running under bun:
bunx drizzle-kit generate:sqlite
drizzle-kit: v0.20.10
drizzle-orm: v0.29.3
No config path provided, using default 'drizzle.config.ts'
Reading config file '/Users/taras/src/volt/voltpay/drizzle.config.ts'
0 tables
works under npm (Docker node:alpine):
/usr/src/app # npx drizzle-kit generate:sqlite
drizzle-kit: v0.20.10
drizzle-orm: v0.29.3
No config path provided, using default 'drizzle.config.ts'
Reading config file '/usr/src/app/drizzle.config.ts'
3 tables
merchants 3 columns 0 indexes 0 fks
transactions 9 columns 0 indexes 2 fks
users 3 columns 0 indexes 0 fks
Downgrading to drizzle-kit version 0.20.6 worked for me
Downgrading to drizzle-kit version 0.20.6 worked for me
It didn't work for me: drizzle-kit: v0.20.6 drizzle-orm: v0.29.3 0 tables No schema changes, nothing to migrate 😴
Same issue on "drizzle-kit": "^0.20.13", and bun 1.0.25.
Maybe is because the connection of the database still open.
Do you set in the migration file?
In this file I put: connection.end()
import postgres from 'postgres'
import chalk from 'chalk'
import { drizzle } from 'drizzle-orm/postgres-js'
import { migrate } from 'drizzle-orm/postgres-js/migrator'
import { env } from '@/infra/env'
const connection = postgres(env.DB_URL, { max: 1 })
const db = drizzle(connection)
await migrate(db, { migrationsFolder: 'drizzle' })
console.log(chalk.greenBright('Migrations applied successfully!'))
await connection.end()
process.exit()
and works fine.
Need to drop the drizzle migrations of the database before run again
"drizzle-orm": "^0.29.3" "drizzle-kit": "^0.20.14"
Same issue on a fresh install. Bun v 1.0.26, drizzle-lit 0.20.14
Running npx drizzle-kit, works perfect. bunx drizzle-kit and it does not find ny tables.
I had the same issue, if you are using it on Win WSL, you have to install nvm from the nvm install node and for schema generation use npm and bun to migrate.
Solution:
- Install node on wsl
- use npm to run
drizzle-kit generate:pg - use bun migrate to migrate.
I had the same issue, if you are using it on Win WSL, you have to install nvm from the nvm install node and for schema generation use npm and bun to migrate.
Solution:
- Install node on wsl
- use npm to run
drizzle-kit generate:pg- use bun migrate to migrate.
What is "bun migrate" ?
It works on me, I'm using WSL with bun. I follow this doc: https://orm.drizzle.team/docs/migrations
Here is my config:
drizzle.config.ts
import type { Config } from "drizzle-kit";
export default {
schema: "./src/schema.ts",
out: "./src/db/migrations",
driver: "pg",
dbCredentials: {
host: process.env.DB_HOST!,
user: process.env.DB_USER!,
password: process.env.DB_PASSWORD!,
database: process.env.DB_NAME!,
},
} satisfies Config;
package.json on scripts
"db:generate": "bunx drizzle-kit generate:pg --schema ./src/db/schema.ts --out=./src/db/migrations"
this is console output:
$ bun db:generate
$ bunx drizzle-kit generate:pg --schema ./src/db/schema.ts --out=./src/db/migrations
drizzle-kit: v0.20.14
drizzle-orm: v0.29.4
2 tables
sessions 3 columns 0 indexes 1 fks
users 2 columns 0 indexes 0 fks
[✓] Your SQL migration file ➜ src/db/migrations/0000_cheerful_loa.sql 🚀
@aivangogh, which version of Bun do you have?
I had the same issue, if you are using it on Win WSL, you have to install nvm from the nvm install node and for schema generation use npm and bun to migrate. Solution:
- Install node on wsl
- use npm to run
drizzle-kit generate:pg- use bun migrate to migrate.
What is "bun migrate" ?
It's my NPM Scripts command defined on packages.json
@aivangogh, which version of Bun do you have?
current the latest one, v1.0.29
I seem to only see this issue when using bun + drizzle + neon as the pg provider. When I run migrations against a railway hosted pg database it works as expected. The fix for me is to run the migration script with node, but i'd like to be able to use bun for this.
@aivangogh Do you have Node installed? If so, try running the script with the Bun runtime. You can use the --bun flag to force the script to use bun. For example, bunx --bun drizzle-kit generate:pg --schema ./src/db/schema.ts --out=./src/db/migrations.
In my experience, it works when I have Node installed, but it does not work if Node is not installed or if I force the migration to use the Bun runtime.
@JoeyRichter confirmed. I use Docker in the project, and the backend is based on the official Bun Docker image. Nothing worked until I installed Node. After that, it worked for the first time only using bunx drizzle-kit generate:pg. Surprisingly, later, bunx is no longer needed, and it works with just drizzle-kit generate:pg
@JoeyRichter confirmed. I use Docker in the project, and the backend is based on the official Bun Docker image. Nothing worked until I installed Node. After that, it worked for the first time only using
bunx drizzle-kit generate:pg. Surprisingly, later,bunxis no longer needed, and it works with justdrizzle-kit generate:pg
I have the issue. Could you provide your Dockerfile?
@supfiger here you are
ARG VARIANT=latest
FROM oven/bun:${VARIANT}
RUN apt-get update \
&& apt-get -y install --no-install-recommends \
git \
nano \
curl \
vim-tiny \
ca-certificates \
&& apt-get auto-remove -y \
&& apt-get clean -y \
&& chsh -s $(which bash) bun \
&& echo 'export PS1="\e[01;32m\u\e[m:\e[01;34m\w\e[m\$ "' >> /home/bun/.bashrc
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
RUN . ~/.bashrc && nvm install v20.11.1
# USER bun
Also noticed when I have environment variables that need to be accessed via Bun.env.*, it doesn't work.
You have to either use dotenv, because the script has to run on node?
I tried running it with bunx --bun drizzle-kit generate:sqlite but no luck because it doesn't seem to find the schema folder I have set for it. It is able to read the config.ts
I'm currently using a schema-compat.cjs to get around the module issue.
const schema = require('./schema');
Object.keys(schema.__proto__).forEach(key => {
exports[key] = schema[key];
});
any updates on this? any idea how to fix it?
I also ran into this issue right now
Getting same issue with Bun 1.1.18 and drizzle 0.32.1