Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing element from enum not working #8137

Closed
nimish-gupta opened this issue Jul 7, 2021 · 3 comments · Fixed by prisma/prisma-engines#2121
Closed

Removing element from enum not working #8137

nimish-gupta opened this issue Jul 7, 2021 · 3 comments · Fixed by prisma/prisma-engines#2121
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: enum "type"/block `enum` topic: migrate
Milestone

Comments

@nimish-gupta
Copy link

Bug description

So in a schema if you have any field with an array of enum as type and if you remove the value from that enum, then it should only create one migration file with all changes.
But after the generation of the migration file, if you re-run the migrate command, it will ask for a new migration name and when you entered some name for migration it errors by this. And also it will create a new migration file, like this

/*
  Warnings:
  - Changed the column `positions` on the `test` table from a scalar field to a list field. If there are non-null values in that column, this step will fail.
*/
-- AlterTable
ALTER TABLE "test" ALTER COLUMN "positions" SET DATA TYPE "Position"[];

How to reproduce

  1. Remove any value (like Last) from enum Position.
  2. Run this command
    prisma generate --schema=./schema.prisma && prisma migrate dev --schema=./schema.prisma
    This will create a new migration file like this
/*
  Warnings:
  - The values [Last] on the enum `Position` will be removed. If these variants are still used in the database, this will fail.
*/
-- AlterEnum
BEGIN;
CREATE TYPE "Position_new" AS ENUM ('First', 'Second');
ALTER TABLE "test" ALTER COLUMN "positions" TYPE "Position_new" USING ("positions"::text::"Position_new");
ALTER TYPE "Position" RENAME TO "Position_old";
ALTER TYPE "Position_new" RENAME TO "Position";
DROP TYPE "Position_old";
COMMIT;
  1. Now again run this command
    prisma migrate dev --schema=./schema.prisma

Expected behavior

After running the third step it should prompt for anything because the schema isn't changed. And appropriate migration should be done in DB.

Prisma information

enum Position {
  First
  Second
  Last
}
model Test {
  id        String     @id @default(uuid())
  name      String     @unique
  positions Position[]
  // common fields
  createdAt DateTime @default(now()) @map("created_at")
  updatedAt DateTime @default(now()) @updatedAt @map("updated_at")
  @@map("test")
}

Environment & setup

  • OS: Macos
  • Database: Postgres
  • Node.js version: 14.17.0

Prisma Version

prisma               : 2.26.0
@prisma/client       : 2.26.0
Current platform     : darwin
Query Engine         : query-engine 9b816b3aa13cc270074f172f30d6eda8a8ce867d (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine     : migration-engine-cli 9b816b3aa13cc270074f172f30d6eda8a8ce867d (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine : introspection-core 9b816b3aa13cc270074f172f30d6eda8a8ce867d (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary        : prisma-fmt 9b816b3aa13cc270074f172f30d6eda8a8ce867d (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash : 9b816b3aa13cc270074f172f30d6eda8a8ce867d
Studio               : 0.408.0
@nimish-gupta nimish-gupta added the kind/bug A reported bug. label Jul 7, 2021
@janpio janpio added team/schema Issue for team Schema. topic: migrate bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Jul 7, 2021
@nimish-gupta
Copy link
Author

Got this working by manually updating the migration.sql file as

/*
  Warnings:
  - The values [Last] on the enum `Position` will be removed. If these variants are still used in the database, this will fail.
*/
-- AlterEnum
BEGIN;
CREATE TYPE "Position_new" AS ENUM ('First', 'Second');
ALTER TABLE "test" ALTER COLUMN "positions" TYPE "Position_new"[] USING ("positions"::"Position"[]::text[]::"Position_new"[]);
ALTER TYPE "Position" RENAME TO "Position_old";
ALTER TYPE "Position_new" RENAME TO "Position";
DROP TYPE "Position_old";
COMMIT;

@tomhoule
Copy link
Contributor

tomhoule commented Jul 7, 2021

Yes I think we're not properly dealing with enum arrays for this kind of migrations, thanks for reporting the problem, it should be pretty easy to fix.

@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels Jul 11, 2021
@thebiglabasky thebiglabasky added this to the 2.29.0 milestone Jul 28, 2021
@thebiglabasky
Copy link

Might be related to #7712

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. team/schema Issue for team Schema. topic: enum "type"/block `enum` topic: migrate
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants