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
21.44k stars 484 forks source link

[BUG]: Drizzle migration does not rollback if it fails #2510

Open taylorwoodmorpheus opened 2 weeks ago

taylorwoodmorpheus commented 2 weeks ago

What version of drizzle-orm are you using?

^0.30.2

What version of drizzle-kit are you using?

0.21.4

Describe the Bug

  1. Generate a migration file with two or more statements.

    ALTER TABLE `user` ADD `name` varchar(255) NOT NULL;--> This runs successfully
    ALTER TABLE `user` ADD CONSTRAINT `name_unique_idx` UNIQUE(`name`);--> This fails due to conflicting names (all names are '') at this point in time
  2. Then execute this migration against a planetscale DB like so

import { drizzle } from "drizzle-orm/planetscale-serverless";

import { Client } from "@planetscale/database";
import { migrate } from "drizzle-orm/planetscale-serverless/migrator";
import { env } from "~/env.mjs";

console.log("Starting migration");
console.log("Creating DB connection");
const client = new Client({
  url: env.DEV_DATABASE_URL,
});
const db = drizzle(client);

console.log("Running migrations");
const result = await migrate(db, { migrationsFolder: "migrations" })
  .then(() => {
    console.log("Migrations finished");
  })
  .catch((err) => {
    console.error("Migrating produced an error: ", err);
  });
console.log(result);
console.log("Finished migration");
  1. The migration will fail due to the second statement
  2. The effect of the first statement will still exist in the database. It isn't rolled back even though the migration failed. It appears that the migration didn't run within a single transaction.

Expected behavior

At step (4.) I expect the entire migration to be rolled back since it failed.

Environment & setup

A node.js program running against a Planetscale DB.