vapor / sql-kit

*️⃣ Build SQL queries in Swift. Extensible, protocol-based design that supports DQL, DML, and DDL.
MIT License
248 stars 58 forks source link

Adding multiple enum cases discards items except one. #138

Closed omochi closed 2 years ago

omochi commented 3 years ago

Describe the bug

When I try to add multiple enum cases, fluent add only one case and silently discards other cases.

For example, with following migration

struct AddCases: Migration {
        func prepare(on database: Database) -> EventLoopFuture<Void> {
            return database.enum("myEnum")
                .case("foo")
                .case("bar")
                .case("baz")
                .update()
                .map { (_) in () }
        }
}

After run it, only baz added to database and foo and bar are skipped.

Expected behavior

All cases added.

Environment

vapor 4.49.0 fluent-postgres-driver 2.2.0 sql-kit 3.10.0

Additional context

I found wrong code. SQLAlterEnumBuilder only hold last one case.

    @discardableResult
    public func add(value: SQLExpression) -> Self {
        self.alterEnum.value = value
        return self
    }

https://github.com/vapor/sql-kit/blob/main/Sources/SQLKit/Builders/SQLAlterEnumBuilder.swift#L28-L32

gwynne commented 2 years ago

This is unfortunately deliberate, as the query syntax supported by PostgreSQL only allows adding a single enum case at a time.