Skip to content

Enum Migration: Adding multiple new cases to an already existing enum does only add the last case #189

@DavidWalter

Description

@DavidWalter

Thank you for providing this excellent framework! 🚀

Describe the bug

Adding multiple new cases to an already existing enum does only add the last case before the update() call.

To Reproduce

  1. Add an enum
    struct Migration1: AsyncMigration {
        let name = "CreateTaskType"

        func prepare(on database: Database) async throws {
            let taskType = try await database.enum(TaskType.schema)
                .case(TaskType.simple.rawValue)
                .create()
        }

        func revert(on database: Database) async throws {
            try await database.enum(TaskType.schema)
                .delete()
        }
    }
  1. Extend the enum in a second migration
    struct Migration2: AsyncMigration {
        let name = "AddMoreTaskTypes"

        func prepare(on database: Database) async throws {
            let taskTypeExtended = try await database.enum(TaskType.schema)
                            .case(TaskType.singleChoice.rawValue) // this does not get added.
                            .case(TaskType.multipleChoice.rawValue)
                            .update()

        }

        func revert(on database: Database) async throws {
            // none 
        }
    }
  1. The migration goes through fine. But when I try to add a model with the .singleChoice type in it. This returns

failed: caught error: "server: invalid input value for enum task_type: "singleChoice" (enum_in)"

When then checking in the PostgreSQL schema, only the last case before the update has been added.

Expected behavior

I expected to be able to add multiple enum cases to an existing enum with a migration like database.enum().case().case().case().update().

Environment

  • Vapor Framework version: 4.55.0
  • Vapor Toolbox version: 18.3.3
  • OS version: macOS 12.2.1 (21D62)

Additional context

A workaround is to do the migration like this:

    struct Migration2: AsyncMigration {
        let name = "AddMoreTaskTypes"

        func prepare(on database: Database) async throws {
            _ = try await database.enum(TaskType.schema)
                            .case(TaskType.singleChoice.rawValue)
                            .update()
            _ = try await database.enum(TaskType.schema)
                            .case(TaskType.multipleChoice.rawValue)
                            .update()
        }

        func revert(on database: Database) async throws {
            // none 
        }
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions