Skip to content

Commit

Permalink
Merge pull request #27 from vapor/skip-preparations
Browse files Browse the repository at this point in the history
skip preparations
  • Loading branch information
tanner0101 authored Jun 6, 2017
2 parents 149a8d5 + acb0c63 commit 4bf0727
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
24 changes: 21 additions & 3 deletions Sources/FluentProvider/Provider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public final class Provider: Vapor.Provider {
/// ex: user_pet vs. user+pet vs. user^pet
/// default is _
public let pivotNameConnector: String?

/// If true, preparations will not be run.
public let skipPreparations: Bool?

/// If true, foreign keys will automatically
/// be added by Fluent
Expand All @@ -56,6 +59,7 @@ public final class Provider: Vapor.Provider {
migrationEntityName: String? = nil,
pivotNameConnector: String? = nil,
autoForeignKeys: Bool? = nil,
skipPreparations: Bool? = nil,
log: Bool? = nil
) {
self.idKey = idKey
Expand All @@ -66,6 +70,7 @@ public final class Provider: Vapor.Provider {
self.migrationEntityName = migrationEntityName
self.pivotNameConnector = pivotNameConnector
self.autoForeignKeys = autoForeignKeys
self.skipPreparations = skipPreparations
self.log = log
}

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

self.defaultPageKey = fluent["defaultPageKey"]?.string
self.defaultPageSize = fluent["defaultPageSize"]?.int
self.migrationEntityName = fluent["migrationEntityName"]?.string
if let name = fluent["migrationEntityName"] {
if name.isNull {
self.migrationEntityName = nil
self.skipPreparations = true
} else {
self.migrationEntityName = name.string
self.skipPreparations = false
}
} else {
self.migrationEntityName = nil
self.skipPreparations = false
}
self.pivotNameConnector = fluent["pivotNameConnector"]?.string
self.autoForeignKeys = fluent["autoForeignKeys"]?.bool
self.log = fluent["log"]?.bool
Expand Down Expand Up @@ -191,8 +207,10 @@ public final class Provider: Vapor.Provider {
if let keyNamingConvention = self.keyNamingConvention {
database.keyNamingConvention = keyNamingConvention
}

try drop.prepare()

if skipPreparations != true {
try drop.prepare()
}
}

public func beforeRun(_ drop: Droplet) throws { }
Expand Down
40 changes: 36 additions & 4 deletions Tests/FluentProviderTests/CacheTests.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import XCTest
@testable import FluentProvider
import SQLite

class CacheTests: XCTestCase {
override func setUp() {
Node.fuzzy = [Node.self]
}

func testHappyPath() throws {
// config specifying memory database
var config = try Config(arguments: ["vapor", "serve", "--port=8832", "--env=debug"])
Expand Down Expand Up @@ -56,7 +53,42 @@ class CacheTests: XCTestCase {
}


func testSkipPreparations() throws {
// config specifying memory database
var config = try Config(arguments: ["vapor", "serve", "--port=8833", "--env=debug"])
try config.set("fluent.driver", "memory")
try config.set("droplet.cache", "fluent")
try config.set("fluent.migrationEntityName", Node.null)
try config.addProvider(FluentProvider.Provider.self)

// add the entity for storing fluent caches
config.preparations.append(FluentCache.CacheEntity.self)

// create droplet with Fluent provider
let drop = try Droplet(config)

// run the droplet
background {
try! drop.run()
}
drop.console.wait(seconds: 1)

// test cache
XCTAssert(drop.cache is FluentCache)

do {
try drop.cache.set("foo", "bar")
XCTFail("Should not have set properly")
} catch SQLite.SQLiteError.prepare {
// pass
// a sqlite prepare error should be thrown
// since preparations were skipped
}
}


static var allTests = [
("testHappyPath", testHappyPath),
("testSkipPreparations", testSkipPreparations),
]
}

0 comments on commit 4bf0727

Please sign in to comment.