Skip to content

Commit

Permalink
Make MigrateCommand async
Browse files Browse the repository at this point in the history
  • Loading branch information
gwynne committed Jan 10, 2024
1 parent 53aa3e8 commit 03ad414
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Sources/Fluent/FluentProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ extension Application {
migrationLogLevel: .info
)
self.application.lifecycle.use(Lifecycle())
self.application.commands.use(MigrateCommand(), as: "migrate")
self.application.asyncCommands.use(MigrateCommand(), as: "migrate")
}

public var migrationLogLevel: Logger.Level {
Expand Down
25 changes: 12 additions & 13 deletions Sources/Fluent/MigrateCommand.swift
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

import ConsoleKit
import FluentKit
import Vapor

public final class MigrateCommand: Command {
public final class MigrateCommand: AsyncCommand {
public struct Signature: CommandSignature {
@Flag(name: "revert")
var revert: Bool
Expand All @@ -14,23 +13,23 @@ public final class MigrateCommand: Command {
public let signature = Signature()

public var help: String {
return "Prepare or revert your database migrations"
"Prepare or revert your database migrations"
}

init() { }

public func run(using context: CommandContext, signature: Signature) throws {
public func run(using context: CommandContext, signature: Signature) async throws {
context.console.info("Migrate Command: \(signature.revert ? "Revert" : "Prepare")")
try context.application.migrator.setupIfNeeded().wait()
try await context.application.migrator.setupIfNeeded().get()
if signature.revert {
try self.revert(using: context)
try await self.revert(using: context)
} else {
try self.prepare(using: context)
try await self.prepare(using: context)
}
}

private func revert(using context: CommandContext) throws {
let migrations = try context.application.migrator.previewRevertLastBatch().wait()
private func revert(using context: CommandContext) async throws {
let migrations = try await context.application.migrator.previewRevertLastBatch().get()
guard migrations.count > 0 else {
context.console.print("No migrations to revert.")
return
Expand All @@ -43,15 +42,15 @@ public final class MigrateCommand: Command {
context.console.print(dbid?.string ?? "default")
}
if context.console.confirm("Would you like to continue?".consoleText(.warning)) {
try context.application.migrator.revertLastBatch().wait()
try await context.application.migrator.revertLastBatch().get()
context.console.print("Migration successful")
} else {
context.console.warning("Migration cancelled")
}
}

private func prepare(using context: CommandContext) throws {
let migrations = try context.application.migrator.previewPrepareBatch().wait()
private func prepare(using context: CommandContext) async throws {
let migrations = try await context.application.migrator.previewPrepareBatch().get()
guard migrations.count > 0 else {
context.console.print("No new migrations.")
return
Expand All @@ -64,7 +63,7 @@ public final class MigrateCommand: Command {
context.console.print(dbid?.string ?? "default")
}
if context.console.confirm("Would you like to continue?".consoleText(.warning)) {
try context.application.migrator.prepareBatch().wait()
try await context.application.migrator.prepareBatch().get()
context.console.print("Migration successful")
} else {
context.console.warning("Migration cancelled")
Expand Down

0 comments on commit 03ad414

Please sign in to comment.