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.83k stars 659 forks source link

[BUG]: No migration is generated for `pgEnum` deletion #3485

Open MatanYadaev opened 1 year ago

MatanYadaev commented 1 year ago

What version of drizzle-orm are you using?

0.28.5

What version of drizzle-kit are you using?

0.19.13

Describe the Bug

When adding a new pgEnum to the schema, and running drizzle-kit generate:pg, it generates a new SQL migration, but when removing this pgEnum and running the same command, it doesn't do anything.

Reproduction steps:

Add the pgEnum to the schema:

// schema.ts
export const statusEnum = pgEnum('status', [
  'draft',
  'in-progress',
  'completed',
]);

Running drizzle-kit generate:pg:

# drizzle/0001_dusty_jack_power.sql
DO $$ BEGIN
 CREATE TYPE "status" AS ENUM('draft', 'in-progress', 'completed');
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;

Removing the pgEnum:

// schema.ts
// Empty file

Running drizzle-kit generate:pg:

No schema changes, nothing to migrate 😴

Expected behavior

The last command execution should create a new SQL migration:

DROP TYPE "status";

Environment & setup

Mac OS 13.5 Node 20 Postgres 15.3

WinterOdin commented 1 year ago

Did you figured it out?

jeanhdev commented 1 year ago

Any update on this ?

MutabaziAlleluia commented 10 months ago

This is still an issue. Removed a value from my enum, No schema changes, nothing to migrate 😴

valcker commented 5 months ago

Still an issue

craxrev commented 4 months ago

I think the idea to generate a bunch of custom queries that handle all (or some) cases for when we change a type in drizzle, it should be roughly something like this: 1) rename the changed type to something else 2) create new type with the new changes 3) assign that type to every column where it's used, it could be in multiple tables 4) drop the renamed type in 1.

Issues could arise where a certain value from the old type is being used in db that it can't be casted to the newer type values or if we have DEFAULT value constraint being set on certain columns which needs to be dropped first before changing the column type.

We could automate generating such queries or just write them manually:

-- 01_migration.sql
CREATE TYPE "your_type" AS ENUM('FIRST', 'SECOND', 'THIRD', 'FRTH');
ALTER TABLE "your_table" ADD COLUMN "your_column" "your_type";

-- 02_migration.sql
ALTER TYPE "your_type" RENAME TO "your_type_old";
CREATE TYPE "your_type" AS ENUM('FIRST', 'SECOND', 'THIRD');
ALTER TABLE "your_table" ADD COLUMN "your_column" "your_type";
DROP TYPE "your_type_old";
pranshuchittora commented 4 months ago

As of "drizzle-orm": "^0.32.0". This issue still exists. No drop enums in migrations.

patrickdevivo commented 4 months ago

Encountered this as well, but then discovered this warning from the Supabase docs:

image

https://supabase.com/docs/guides/database/postgres/enums#removing-enum-values

So I wonder if this is somewhat intended behavior - or at least a recognition that removing enum values could create problems?

pranshuchittora commented 4 months ago

Interesting point @patrickdevivo , but IMO the ORM should have a consistent schema overtime. If an enum is dropped from your schema, but not as part of your db migration (which is the current behavior). Then overtime there the schema on db will deviate to an extend. If you want to do some schema debugging or recreate then the pg_dump will differ which could raise false alarms.

maxpaj commented 1 month ago

Still an issue with PG enums...