bun icon indicating copy to clipboard operation
bun copied to clipboard

Drizzle migrations not working

Open mcornaglia opened this issue 1 year ago • 27 comments
trafficstars

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?

  1. bun add drizzle-orm postgres
  2. bun add -D drizzle-kit
  3. Add a sample schema file under ./src/db as schema.ts
  4. Add a script in the package.json "migration:generate": "drizzle-kit generate:pg --schema=./src/db/schema.ts"
  5. 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?

image

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":[]}

mcornaglia avatar Nov 27 '23 21:11 mcornaglia

Use drizzle-kit generate:pg instead

gnllucena avatar Nov 30 '23 04:11 gnllucena

Use drizzle-kit generate:pg instead

You mean without defining the schema path ?

mcornaglia avatar Nov 30 '23 18:11 mcornaglia

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..

mcornaglia avatar Nov 30 '23 21:11 mcornaglia

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

gnllucena avatar Nov 30 '23 21:11 gnllucena

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'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.

mcornaglia avatar Nov 30 '23 21:11 mcornaglia

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

mcornaglia avatar Dec 01 '23 18:12 mcornaglia

can confirm this is happening to me too on bun 1.0.15

hazelnutcloud avatar Dec 09 '23 06:12 hazelnutcloud

Just install node js to your system and try run bun run migration:generate again it will solved your issue

mbabalawlar avatar Dec 12 '23 10:12 mbabalawlar

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

xslim avatar Jan 05 '24 22:01 xslim

Downgrading to drizzle-kit version 0.20.6 worked for me

ViniciusCestarii avatar Jan 18 '24 22:01 ViniciusCestarii

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 😴

loudsven avatar Jan 23 '24 09:01 loudsven

Same issue on "drizzle-kit": "^0.20.13", and bun 1.0.25.

oasaleh avatar Jan 29 '24 03:01 oasaleh

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"

ehogiovanni avatar Feb 02 '24 22:02 ehogiovanni

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.

peXed avatar Feb 10 '24 23:02 peXed

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.

ScripTech avatar Feb 21 '24 19:02 ScripTech

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" ?

xslim avatar Feb 22 '24 03:02 xslim

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 avatar Feb 26 '24 06:02 aivangogh

@aivangogh, which version of Bun do you have?

brokolosov avatar Feb 28 '24 17:02 brokolosov

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

image

image

ScripTech avatar Feb 28 '24 18:02 ScripTech

@aivangogh, which version of Bun do you have?

current the latest one, v1.0.29

aivangogh avatar Feb 28 '24 19:02 aivangogh

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.

jondcallahan avatar Feb 28 '24 19:02 jondcallahan

@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 avatar Feb 28 '24 19:02 JoeyRichter

@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

brokolosov avatar Feb 29 '24 08:02 brokolosov

@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

I have the issue. Could you provide your Dockerfile?

supfiger avatar Mar 16 '24 06:03 supfiger

@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

brokolosov avatar Mar 25 '24 07:03 brokolosov

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

Blankeos avatar May 01 '24 16:05 Blankeos

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];
});

phenax avatar Jun 30 '24 15:06 phenax

any updates on this? any idea how to fix it?

artemszelenov avatar Jul 28 '24 08:07 artemszelenov

I also ran into this issue right now

coffeeispower avatar Jul 28 '24 20:07 coffeeispower

Getting same issue with Bun 1.1.18 and drizzle 0.32.1

LeulAria avatar Aug 02 '24 20:08 LeulAria