Skip to content

Commit fa4382f

Browse files
authored
Merge pull request #652 from vapor/tn-auto-migrate
Add autoMigrate and autoRevert methods
2 parents 7793f5c + 32c6861 commit fa4382f

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

Sources/Fluent/FluentProvider.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ extension Application {
3838
)
3939
}
4040

41+
/// Automatically runs forward migrations without confirmation.
42+
/// This can be triggered by passing `--auto-migrate` flag.
43+
public func autoMigrate() -> EventLoopFuture<Void> {
44+
self.migrator.setupIfNeeded().flatMap {
45+
self.migrator.prepareBatch()
46+
}
47+
}
48+
49+
/// Automatically runs reverse migrations without confirmation.
50+
/// This can be triggered by passing `--auto-revert` during boot.
51+
public func autoRevert() -> EventLoopFuture<Void> {
52+
self.migrator.setupIfNeeded().flatMap {
53+
self.migrator.revertAllBatches()
54+
}
55+
}
56+
4157
public struct Fluent {
4258
final class Storage {
4359
let databases: Databases
@@ -70,12 +86,10 @@ extension Application {
7086

7187
let signature = try Signature(from: &application.environment.commandInput)
7288
if signature.autoRevert {
73-
try application.migrator.setupIfNeeded().wait()
74-
try application.migrator.revertAllBatches().wait()
89+
try application.autoRevert().wait()
7590
}
7691
if signature.autoMigrate {
77-
try application.migrator.setupIfNeeded().wait()
78-
try application.migrator.prepareBatch().wait()
92+
try application.autoMigrate().wait()
7993
}
8094
}
8195

0 commit comments

Comments
 (0)