Home > Net >  Swift Vapor: remove database field via migration and from struct at the same time
Swift Vapor: remove database field via migration and from struct at the same time

Time:09-30

I have added a database migration to remove a field:

static func prepare(on conn: PostgreSQLConnection) -> EventLoopFuture<Void> {
    return Database.update(User.self, on: conn) { builder in
        builder.deleteField(for: \.name)
    }
}

Now this works fine except that when I remove the field name from my User struct, this doesn't compile anymore, because obviously, the keypath for name can't be found.
So this requires me to leave the field in the User struct until after the migration has run and then I can delete the field but then I also need to delete the migration.

That doesn't feel like a good practice to me and I'm probably doing something wrong here.
What can I do instead?

CodePudding user response:

What you can do is get the field by name and have it remove that:

builder.deleteField(User.Database.QueryField.column("users", "name"))

where

  • users is the table name
  • name is the column/field name

CodePudding user response:

This is a complete migration showing a field added and then the revert to remove it:

    struct UpdateUserTable1: Migration {
        func prepare(on database: Database) -> EventLoopFuture<Void> {
            database.schema("User")
                .field("nextAttemptAt", .datetime)
                .update()
        }

        func revert(on database: Database) -> EventLoopFuture<Void> {
            database.schema("User")
                .deleteField("nextAttemptAt")
                .update()
        }
    }

As you can see, the migration doesn't require the field to be defined in the Model or not at any stage. This is the correct way to do it using Fluent.

  • Related