Skip to content

Commit 4bf0727

Browse files
authored
Merge pull request #27 from vapor/skip-preparations
skip preparations
2 parents 149a8d5 + acb0c63 commit 4bf0727

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

Sources/FluentProvider/Provider.swift

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ public final class Provider: Vapor.Provider {
3939
/// ex: user_pet vs. user+pet vs. user^pet
4040
/// default is _
4141
public let pivotNameConnector: String?
42+
43+
/// If true, preparations will not be run.
44+
public let skipPreparations: Bool?
4245

4346
/// If true, foreign keys will automatically
4447
/// be added by Fluent
@@ -56,6 +59,7 @@ public final class Provider: Vapor.Provider {
5659
migrationEntityName: String? = nil,
5760
pivotNameConnector: String? = nil,
5861
autoForeignKeys: Bool? = nil,
62+
skipPreparations: Bool? = nil,
5963
log: Bool? = nil
6064
) {
6165
self.idKey = idKey
@@ -66,6 +70,7 @@ public final class Provider: Vapor.Provider {
6670
self.migrationEntityName = migrationEntityName
6771
self.pivotNameConnector = pivotNameConnector
6872
self.autoForeignKeys = autoForeignKeys
73+
self.skipPreparations = skipPreparations
6974
self.log = log
7075
}
7176

@@ -117,7 +122,18 @@ public final class Provider: Vapor.Provider {
117122

118123
self.defaultPageKey = fluent["defaultPageKey"]?.string
119124
self.defaultPageSize = fluent["defaultPageSize"]?.int
120-
self.migrationEntityName = fluent["migrationEntityName"]?.string
125+
if let name = fluent["migrationEntityName"] {
126+
if name.isNull {
127+
self.migrationEntityName = nil
128+
self.skipPreparations = true
129+
} else {
130+
self.migrationEntityName = name.string
131+
self.skipPreparations = false
132+
}
133+
} else {
134+
self.migrationEntityName = nil
135+
self.skipPreparations = false
136+
}
121137
self.pivotNameConnector = fluent["pivotNameConnector"]?.string
122138
self.autoForeignKeys = fluent["autoForeignKeys"]?.bool
123139
self.log = fluent["log"]?.bool
@@ -191,8 +207,10 @@ public final class Provider: Vapor.Provider {
191207
if let keyNamingConvention = self.keyNamingConvention {
192208
database.keyNamingConvention = keyNamingConvention
193209
}
194-
195-
try drop.prepare()
210+
211+
if skipPreparations != true {
212+
try drop.prepare()
213+
}
196214
}
197215

198216
public func beforeRun(_ drop: Droplet) throws { }

Tests/FluentProviderTests/CacheTests.swift

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import XCTest
22
@testable import FluentProvider
3+
import SQLite
34

45
class CacheTests: XCTestCase {
5-
override func setUp() {
6-
Node.fuzzy = [Node.self]
7-
}
8-
96
func testHappyPath() throws {
107
// config specifying memory database
118
var config = try Config(arguments: ["vapor", "serve", "--port=8832", "--env=debug"])
@@ -56,7 +53,42 @@ class CacheTests: XCTestCase {
5653
}
5754

5855

56+
func testSkipPreparations() throws {
57+
// config specifying memory database
58+
var config = try Config(arguments: ["vapor", "serve", "--port=8833", "--env=debug"])
59+
try config.set("fluent.driver", "memory")
60+
try config.set("droplet.cache", "fluent")
61+
try config.set("fluent.migrationEntityName", Node.null)
62+
try config.addProvider(FluentProvider.Provider.self)
63+
64+
// add the entity for storing fluent caches
65+
config.preparations.append(FluentCache.CacheEntity.self)
66+
67+
// create droplet with Fluent provider
68+
let drop = try Droplet(config)
69+
70+
// run the droplet
71+
background {
72+
try! drop.run()
73+
}
74+
drop.console.wait(seconds: 1)
75+
76+
// test cache
77+
XCTAssert(drop.cache is FluentCache)
78+
79+
do {
80+
try drop.cache.set("foo", "bar")
81+
XCTFail("Should not have set properly")
82+
} catch SQLite.SQLiteError.prepare {
83+
// pass
84+
// a sqlite prepare error should be thrown
85+
// since preparations were skipped
86+
}
87+
}
88+
89+
5990
static var allTests = [
6091
("testHappyPath", testHappyPath),
92+
("testSkipPreparations", testSkipPreparations),
6193
]
6294
}

0 commit comments

Comments
 (0)