prisma / migrate

Issues for Prisma Migrate are now tracked at prisma/prisma. This repo was used to track issues for Prisma Migrate Experimental and is now deprecated.
https://www.prisma.io/docs/concepts/components/prisma-migrate
Apache License 2.0
767 stars 22 forks source link

Migration fails when changing an enum value in Postgres #614

Closed kraglalbert closed 3 years ago

kraglalbert commented 3 years ago

Bug description

Today I updated prisma-cli from version 2.4.x to the latest version and a migration that previously worked now fails. The migration itself is pretty simple since it's just changing an enum value. Below are the generated files for the migration:

steps.json:

{
  "version": "0.3.14-fixed",
  "steps": [
    {
      "tag": "UpdateEnum",
      "enum": "ConsultationStatus",
      "createdValues": [
        "CONFIRMED"
      ],
      "deletedValues": [
        "ACCEPTED"
      ]
    }
  ]
}

README.md:

# Migration `20201001212409-update-consultation-status-enum`

This migration has been generated by kraglalbert at 10/1/2020, 2:24:09 PM.
You can check out the [state of the schema](./schema.prisma) after the migration.

## Database Steps

CREATE TYPE "ConsultationStatus_new" AS ENUM ('PENDING', 'REJECTED', 'CONFIRMED', 'COMPLETED');
ALTER TABLE "public"."Consultation" ALTER COLUMN "status" DROP DEFAULT,
                        ALTER COLUMN "status" TYPE "ConsultationStatus_new" USING ("status"::text::"ConsultationStatus_new"),
                        ALTER COLUMN "status" SET DEFAULT 'PENDING';
ALTER TYPE "ConsultationStatus" RENAME TO "ConsultationStatus_old";
ALTER TYPE "ConsultationStatus_new" RENAME TO "ConsultationStatus";
DROP TYPE "ConsultationStatus_old"

## Changes

diff --git schema.prisma schema.prisma
migration 20200922171456-add-start-and-end-time-to-consultation..20201001212409-update-consultation-status-enum
--- datamodel.dml
+++ datamodel.dml
@@ -1,7 +1,7 @@
 datasource postgres {
   provider = "postgresql"
-  url = "***"
+  url = "***"
 }
 generator client {
   provider = "prisma-client-js"
@@ -115,7 +115,7 @@
 enum ConsultationStatus {
   PENDING
   REJECTED
-  ACCEPTED
+  CONFIRMED
   COMPLETED
 }

I'm working with Postgres which allows enum types to be defined. The migrations before this one are applied successfully but this one fails which prevents the migrations after it from being applied as well. Is this a bug or is there something I'm doing wrong on my end?

How to reproduce

  1. Update a value in an existing enum in schema.prisma
  2. Save the migration with prisma migrate save --experimental
  3. Apply the migration with prisma migrate up --experimental
  4. Get the following output:
    
    Database Changes:

Migration Database actions Status

20201001212409-update-consultation-status-enum statements.

You can get the detailed db changes with prisma migrate up --experimental --verbose Or read about them here: ./migrations/20201001212409-update-consultation-status-enum/README.md

Error: Connector error: Error querying the database: Error querying the database: db error: ERROR: current transaction is aborted, commands ignored until end of transaction block 0: migration_core::api::ApplyMigration with migration_id="20201001212409-update-consultation-status-enum" at migration-engine/core/src/api.rs:93


## Expected behavior

<!-- A clear and concise description of what you expected to happen. -->
The migration is applied successfully.

## Environment & setup

- OS: Mac OS
- Database: PostgreSQL
- Node.js version: v12.14.1
- Prisma version:
<!--[Run `prisma -v` to see your Prisma version and paste it between the ´´´]-->

@prisma/cli : 2.9.0 @prisma/client : 2.4.1 Current platform : darwin Query Engine : query-engine 369b3694b7edb869fad14827a33ad3f3f49bbc20 (at node_modules/@prisma/cli/query-engine-darwin) Migration Engine : migration-engine-cli 369b3694b7edb869fad14827a33ad3f3f49bbc20 (at node_modules/@prisma/cli/migration-engine-darwin) Introspection Engine : introspection-core 369b3694b7edb869fad14827a33ad3f3f49bbc20 (at node_modules/@prisma/cli/introspection-engine-darwin) Format Binary : prisma-fmt 369b3694b7edb869fad14827a33ad3f3f49bbc20 (at node_modules/@prisma/cli/prisma-fmt-darwin) Studio : 0.296.0



Thank you!
AviBueno commented 3 years ago

@kraglalbert just dropped by to say that the one helpful thing I got from the abstract "Error querying the database" message is that it pointed me to this issue, which made me understand our problem and how to fix it. Thanks!

tomhoule commented 3 years ago

This should be fixed in recent prisma migrate versions. Please open a new issue if it happens with the preview version or later (2.13.1). Thanks for reporting!