Skip to content

Commit

Permalink
don't run eagerloads on empty result set, fixes #117 (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
tanner0101 authored Dec 26, 2019
1 parent c232981 commit 27d8b35
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
18 changes: 18 additions & 0 deletions Sources/FluentBenchmark/FluentBenchmarker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public final class FluentBenchmarker {
try self.testPerformance()
try self.testSoftDeleteWithQuery()
try self.testDuplicatedUniquePropertyName()
try self.testEmptyEagerLoadChildren()
}

public func testCreate() throws {
Expand Down Expand Up @@ -1757,6 +1758,23 @@ public final class FluentBenchmarker {
//
}
}

// https://github.com/vapor/fluent-kit/issues/117
public func testEmptyEagerLoadChildren() throws {
try runTest(#function, [
GalaxyMigration(),
PlanetMigration(),
GalaxySeed(),
PlanetSeed()
]) {
let galaxies = try Galaxy.query(on: self.database)
.filter(\.$name == "foo")
.with(\.$planets)
.all().wait()

XCTAssertEqual(galaxies.count, 0)
}
}

// MARK: Utilities

Expand Down
15 changes: 10 additions & 5 deletions Sources/FluentKit/Query/QueryBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -641,12 +641,17 @@ public final class QueryBuilder<Model>
// if eager loads exist, run them, and update models
if !self.eagerLoads.requests.isEmpty {
return done.flatMap {
return .andAllSucceed(self.eagerLoads.requests.values.map { eagerLoad in
// don't run eager loads if result set was empty
guard !all.isEmpty else {
return self.database.eventLoop.makeSucceededFuture(())
}
// run eager loads
return EventLoopFuture<Void>.andAllSucceed(self.eagerLoads.requests.values.map { eagerLoad in
return eagerLoad.run(models: all, on: self.database)
}, on: self.database.eventLoop)
}.flatMapThrowing {
try all.forEach { model in
try model.eagerLoad(from: self.eagerLoads)
}, on: self.database.eventLoop).flatMapThrowing {
try all.forEach { model in
try model.eagerLoad(from: self.eagerLoads)
}
}
}
} else {
Expand Down

0 comments on commit 27d8b35

Please sign in to comment.