drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.18k stars 617 forks source link

[BUG]: I get `cannot insert multiple commands into a prepared statement` when migrations more than one #2794

Open kravetsone opened 2 months ago

kravetsone commented 2 months ago

What version of drizzle-orm are you using?

0.33.0

What version of drizzle-kit are you using?

0.24.0

Describe the Bug

I'm writing tests and mocking postgres driver to pglite (yes, it's very cool), but I can't apply migrations when they're in meta/_journal.json more than 1

import { mock } from "bun:test";
import { PGlite } from "@electric-sql/pglite";
import { drizzle } from "drizzle-orm/pglite";
import { migrate } from "drizzle-orm/pglite/migrator";

const pglite = new PGlite();
const db = drizzle(pglite);

await migrate(db, {
    migrationsFolder: "../../packages/db/drizzle",
});

mock.module("postgres", () => ({ default: () => pglite }));
mock.module("drizzle-orm/postgres-js", () => ({ drizzle }));

Based on the error, I found workaround - you need to create a new folder for migration and delete it every time (so that there are no more than one migrations)

My example

bunx drizzle-kit generate --config .\mock.config.ts
// @filename: mock.config.ts
import type { Config } from "drizzle-kit";

export default {
    schema: "./src/schema.ts",
    out: "./drizzle-mocks", // i change out to another than "drizzle" folder
    dialect: "postgresql",
    dbCredentials: {
        url: process.env.DATABASE_URL as string,
    },
} satisfies Config;

Drizzle is great!

Expected behavior

No response

Environment & setup

No response

EthanNC commented 2 months ago

I set this up with vitest and managed to get it working with more than one migration in my folder. I got tripped up on having the right path for migrationFolder. This could be your issue depending on where you calling your test script from.

const client = new PGlite();
export const db = drizzle(client);

vi.mock("postgres", () => ({ default: () => client }));
vi.mock("drizzle-orm/postgres-js", () => ({ drizzle }));
// Apply migrations before each test
beforeEach(async () => {
  await migrate(db, {
    migrationsFolder: "./packages/core/migrations",
  });
});

// Clean up the database after each test
afterEach(async () => {
  await db.execute(sql`drop schema if exists public cascade`);
  await db.execute(sql`create schema public`);
  await db.execute(sql`drop schema if exists drizzle cascade`);
});

// Free up resources after all tests are done
afterAll(async () => {
  client.close();
});

Here is the code: link

JacobReynolds commented 2 months ago

I was able to pin this down to missing --> statement-breakpoint statements within migrations that I had manually changed. Adding those in between commands fixed the issue.