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.57k stars 490 forks source link

[BUG]: IF NOT EXISTS in Migrations leads to inconsistencies #2356

Open mothershipper opened 1 month ago

mothershipper commented 1 month ago

What version of drizzle-orm are you using?

0.30.10

What version of drizzle-kit are you using?

0.21.2

Describe the Bug

When generating postgres migrations, drizzle is adding IF NOT EXISTS to the CREATE statement:

Ex:

CREATE TABLE IF NOT EXISTS "rates" (
    "id" text PRIMARY KEY NOT NULL,
    "created_at" timestamp DEFAULT now() NOT NULL,
    "updated_at" timestamp DEFAULT now() NOT NULL,
    "deleted_at" timestamp,
);

As a result, name collisions on tables do not error, and the migration applies "successfully" despite no changes. This punts detection of the issue further down the line and not during development/deployment.

Expected behavior

I'd expect an error to be thrown and the migration aborted if I have table naming collisions. It's much easier to catch these mistakes early than waiting for errors like "unknown column YYYY in table XXXX" to be thrown from the app at runtime.

Environment & setup

postgres, create two tables with the same name