payloadcms / payload

Payload is the open-source, fullstack Next.js framework, giving you instant backend superpowers. Get a full TypeScript backend and admin panel instantly. Use Payload as a headless CMS or for building powerful applications.
https://payloadcms.com
MIT License
23.58k stars 1.5k forks source link

Changing enum names isn't handled properly by generated migration files #4677

Open stephaniesyed opened 9 months ago

stephaniesyed commented 9 months ago

Link to reproduction

No response

Describe the Bug

When an enum get renamed, the generated migration file tries to change the data type for the column that uses it, but it fails.

The file has

DO $$ BEGIN
 CREATE TYPE "enum_employees_roles" AS ENUM('super-admin', 'admin');
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
...
ALTER TABLE "employees_roles" ALTER COLUMN "value" SET DATA TYPE enum_employees_roles;

Which results in the following error when trying to migrate:

stephanie.syed@LXYWG0WJ6C partner-manager % make migrate
Running in docker: yarn run payload migrate
yarn run v1.22.19
$ cross-env PAYLOAD_CONFIG_PATH=src/payload.config.ts payload migrate
[20:14:38] INFO (payload): Starting Payload...
[20:14:38] INFO (payload): Reading migration files from /home/node/app/src/migrations
[20:14:38] INFO (payload): Migrating: 20240103_193408_rename_shipperLocation_and_user
[20:14:38] ERROR (payload): Error running migration 20240103_193408_rename_shipperLocation_and_user column "value" cannot be cast automatically to type enum_employees_roles.
    err: {
      "type": "DatabaseError",
      "message": "column \"value\" cannot be cast automatically to type enum_employees_roles",
      "stack":
          error: column "value" cannot be cast automatically to type enum_employees_roles
              at /home/node/app/node_modules/pg-pool/index.js:45:11
              at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      "length": 206,
      "name": "error",
      "severity": "ERROR",
      "code": "42804",
      "hint": "You might need to specify \"USING value::enum_employees_roles\".",
      "file": "tablecmds.c",
      "line": "9657",
      "routine": "ATPrepAlterColumnType"
    }
[20:14:38] INFO (payload): Done.
Done in 1.66s.

Changing the second line to


ALTER TABLE "employees_roles" ALTER COLUMN "value" SET DATA TYPE enum_employees_roles using employee_roles::text::enum_employees_roles;
``` fixes it and lets the migration run successfully

### To Reproduce

1. Create a model with an enum field
2. Create and run migrations for the model
3. Change the name of the enum field
4. Generate the second migration
5. Attempt to upgrade

### Payload Version

2.4.0

### Adapters and Plugins

none
DavidOliver commented 5 months ago

I've run into this without having changed names myself updating from 2.11.2 to 2.13.0.

✔ Warnings detected during schema push:

· You're about to change versionstatus column type from enuminterests_v_version_status to _enuminterests_v_version_status_v with 2 items · You're about to change version_country column type from enumorganisations_v_version_country to _enumorganisations_v_version_country_v with 1 items · You're about to change versionstatus column type from enumnews_posts_v_version_status to _enumnews_posts_v_version_status_v with 17 items · You're about to change version_authorisation column type from enummembers_v_version_authorisation to _enummembers_v_version_authorisation_v with 100 items · You're about to change version_personTitle column type from enummembers_v_person_title to _enummembers_v_person_title_v with 100 items

DATA LOSS WARNING: Possible data loss detected if schema is pushed.

Accept warnings and push schema to database? … yes error: column "version__status" cannot be cast automatically to type _enum__interests_v_version_status_v at /data/projects/ac/payload/node_modules/pg-pool/index.js:45:11 at processTicksAndRejections (node:internal/process/task_queues:95:5) at DrizzleORMPgClient.query (/data/projects/ac/payload/node_modules/drizzle-kit/payload.js:34498:21) at apply (/data/projects/ac/payload/node_modules/drizzle-kit/payload.js:36648:9) at Object.connect (/data/projects/ac/payload/node_modules/@payloadcms/db-postgres/src/connect.ts:124:3) at BasePayload.init (/data/projects/ac/payload/node_modules/payload/src/payload.ts:359:7) at getPayload (/data/projects/ac/payload/node_modules/payload/src/payload.ts:430:22) at initHTTP (/data/projects/ac/payload/node_modules/payload/src/initHTTP.ts:33:19) at Payload.init (/data/projects/ac/payload/node_modules/payload/src/index.ts:17:21)

Example before and after column type names:

enum__interests_v_version_status
_enum__interests_v_version_status_v

I'm guessing the new types/type names are caused by https://github.com/payloadcms/payload/pull/5045 ?

Edit: I've now nuked the database in dev allowing Payload to recreate the tables upon running, and column type name is now:

_enum__interests_v_version_status_v
lynndylanhurley commented 5 months ago

this was helpful, thank you @stephaniesyed