Skip to content

Commit

Permalink
Miscellaneous bug fixes (#610)
Browse files Browse the repository at this point in the history
* Remove numerous Sendable warning workarounds that are no longer needed

* Make `.with(\.$foo, withDeleted: true)` work correctly when used with @CompositeChildren

* Support the full set of SQL utilities when a custom aggregate query is specified

* Fix DocC warnings

* In the FluentBenchmarks, convert some migrations to async and change "Sun" to "Sol" because I'm pointlessly pedantic that way.

* Make various FluentBenchmarks tests not crash when simple things go wrong

* Make testParent_serialization() actually test something related to a parent property.

* Use structured logging when we log things

* Fix Sendable warnings, and in the process also the behavior of ISO8601TimestampFormat when both fractional and non-fractional formats are used in the same project.

* Another Sendable warning

* Further flesh out the structured logging with better recursion and not omitting some of the query's information.
  • Loading branch information
gwynne authored May 30, 2024
1 parent cb91bf9 commit d69efce
Show file tree
Hide file tree
Showing 54 changed files with 360 additions and 318 deletions.
6 changes: 3 additions & 3 deletions Sources/FluentBenchmark/FluentBenchmarker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public final class FluentBenchmarker {

// MARK: Utilities

internal func runTest(
func runTest(
_ name: String,
_ migrations: [any Migration],
_ test: () throws -> ()
) throws {
try self.runTest(name, migrations, { _ in try test() })
}

internal func runTest(
func runTest(
_ name: String,
_ migrations: [any Migration],
_ test: (any Database) throws -> ()
Expand All @@ -74,7 +74,7 @@ public final class FluentBenchmarker {
try self.runTest(name, migrations, on: self.database, test)
}

internal func runTest(
func runTest(
_ name: String,
_ migrations: [any Migration],
on database: any Database,
Expand Down
35 changes: 17 additions & 18 deletions Sources/FluentBenchmark/SolarSystem/Galaxy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import XCTest
public final class Galaxy: Model, @unchecked Sendable {
public static let schema = "galaxies"

@ID(key: .id)
@ID
public var id: UUID?

@Field(key: "name")
Expand All @@ -18,45 +18,44 @@ public final class Galaxy: Model, @unchecked Sendable {
@Siblings(through: GalacticJurisdiction.self, from: \.$id.$galaxy, to: \.$id.$jurisdiction)
public var jurisdictions: [Jurisdiction]

public init() { }
public init() {}

public init(id: UUID? = nil, name: String) {
self.id = id
self.name = name
}
}

public struct GalaxyMigration: Migration {
public struct GalaxyMigration: AsyncMigration {
public init() {}

public func prepare(on database: any Database) -> EventLoopFuture<Void> {
database.schema("galaxies")
.field("id", .uuid, .identifier(auto: false))
public func prepare(on database: any Database) async throws {
try await database.schema("galaxies")
.id()
.field("name", .string, .required)
.create()
}

public func revert(on database: any Database) -> EventLoopFuture<Void> {
database.schema("galaxies").delete()
public func revert(on database: any Database) async throws {
try await database.schema("galaxies").delete()
}
}

public struct GalaxySeed: Migration {
public init() { }
public struct GalaxySeed: AsyncMigration {
public init() {}

public func prepare(on database: any Database) -> EventLoopFuture<Void> {
.andAllSucceed([
public func prepare(on database: any Database) async throws {
try await [
"Andromeda",
"Milky Way",
"Pinwheel Galaxy",
"Messier 82"
].map {
Galaxy(name: $0)
.create(on: database)
}, on: database.eventLoop)
]
.map { Galaxy(name: $0) }
.create(on: database)
}

public func revert(on database: any Database) -> EventLoopFuture<Void> {
Galaxy.query(on: database).delete()
public func revert(on database: any Database) async throws {
try await Galaxy.query(on: database).delete()
}
}
2 changes: 1 addition & 1 deletion Sources/FluentBenchmark/SolarSystem/Planet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public struct PlanetSeed: Migration {
.andAllSucceed(stars.map { star in
let planets: [Planet]
switch star.name {
case "Sun":
case "Sol":
planets = [
.init(name: "Mercury"),
.init(name: "Venus"),
Expand Down
57 changes: 31 additions & 26 deletions Sources/FluentBenchmark/SolarSystem/Star.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import XCTest
public final class Star: Model, @unchecked Sendable {
public static let schema = "stars"

@ID(key: .id)
@ID
public var id: UUID?

@Field(key: "name")
Expand All @@ -23,48 +23,53 @@ public final class Star: Model, @unchecked Sendable {

public init() { }

public init(id: IDValue? = nil, name: String) {
public init(id: IDValue? = nil, name: String, galaxyId: Galaxy.IDValue? = nil) {
self.id = id
self.name = name
if let galaxyId {
self.$galaxy.id = galaxyId
}
}
}

public struct StarMigration: Migration {
public func prepare(on database: any Database) -> EventLoopFuture<Void> {
database.schema("stars")
.field("id", .uuid, .identifier(auto: false))
public struct StarMigration: AsyncMigration {
public func prepare(on database: any Database) async throws {
try await database.schema("stars")
.id()
.field("name", .string, .required)
.field("galaxy_id", .uuid, .required, .references("galaxies", "id"))
.field("deleted_at", .datetime)
.create()
}

public func revert(on database: any Database) -> EventLoopFuture<Void> {
database.schema("stars").delete()
public func revert(on database: any Database) async throws {
try await database.schema("stars").delete()
}
}

public final class StarSeed: Migration {
public init() { }
public final class StarSeed: AsyncMigration {
public init() {}

public func prepare(on database: any Database) -> EventLoopFuture<Void> {
Galaxy.query(on: database).all().flatMap { galaxies in
.andAllSucceed(galaxies.map { galaxy in
let stars: [Star]
switch galaxy.name {
case "Milky Way":
stars = [.init(name: "Sun"), .init(name: "Alpha Centauri")]
case "Andromeda":
stars = [.init(name: "Alpheratz")]
default:
stars = []
}
return galaxy.$stars.create(stars, on: database)
}, on: database.eventLoop)
public func prepare(on database: any Database) async throws {
var stars: [Star] = []

for galaxy in try await Galaxy.query(on: database).all() {
switch galaxy.name {
case "Milky Way":
stars.append(contentsOf: [
.init(name: "Sol", galaxyId: galaxy.id!),
.init(name: "Alpha Centauri", galaxyId: galaxy.id!)
])
case "Andromeda":
stars.append(.init(name: "Alpheratz", galaxyId: galaxy.id!))
default:
break
}
}
try await stars.create(on: database)
}

public func revert(on database: any Database) -> EventLoopFuture<Void> {
Star.query(on: database).delete(force: true)
public func revert(on database: any Database) async throws {
try await Star.query(on: database).delete(force: true)
}
}
16 changes: 8 additions & 8 deletions Sources/FluentBenchmark/Tests/EagerLoadTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension FluentBenchmarker {
switch galaxy.name {
case "Milky Way":
XCTAssertEqual(
galaxy.stars.contains { $0.name == "Sun" },
galaxy.stars.contains { $0.name == "Sol" },
true
)
XCTAssertEqual(
Expand All @@ -68,15 +68,15 @@ extension FluentBenchmarker {
try Planet.query(on: self.database).filter(\.$name == "Jupiter").delete().wait()

let sun1 = try XCTUnwrap(Star.query(on: self.database)
.filter(\.$name == "Sun")
.filter(\.$name == "Sol")
.with(\.$planets, withDeleted: true)
.first().wait()
)
XCTAssertTrue(sun1.planets.contains { $0.name == "Earth" })
XCTAssertTrue(sun1.planets.contains { $0.name == "Jupiter" })

let sun2 = try XCTUnwrap(Star.query(on: self.database)
.filter(\.$name == "Sun")
.filter(\.$name == "Sol")
.with(\.$planets)
.first().wait()
)
Expand All @@ -96,7 +96,7 @@ extension FluentBenchmarker {
for planet in planets {
switch planet.name {
case "Earth":
XCTAssertEqual(planet.star.name, "Sun")
XCTAssertEqual(planet.star.name, "Sol")
case "Proxima Centauri b":
XCTAssertEqual(planet.star.name, "Alpha Centauri")
default: break
Expand All @@ -109,14 +109,14 @@ extension FluentBenchmarker {
try self.runTest(#function, [
SolarSystem()
]) {
try Star.query(on: self.database).filter(\.$name == "Sun").delete().wait()
try Star.query(on: self.database).filter(\.$name == "Sol").delete().wait()

let planet = try XCTUnwrap(Planet.query(on: self.database)
.filter(\.$name == "Earth")
.with(\.$star, withDeleted: true)
.first().wait()
)
XCTAssertEqual(planet.star.name, "Sun")
XCTAssertEqual(planet.star.name, "Sol")

XCTAssertThrowsError(
try Planet.query(on: self.database)
Expand Down Expand Up @@ -145,13 +145,13 @@ extension FluentBenchmarker {
for planet in planets {
switch planet.name {
case "Earth":
XCTAssertEqual(planet.star.name, "Sun")
XCTAssertEqual(planet.star.name, "Sol")
XCTAssertEqual(planet.tags.map { $0.name }.sorted(), ["Inhabited", "Small Rocky"])
case "Proxima Centauri b":
XCTAssertEqual(planet.star.name, "Alpha Centauri")
XCTAssertEqual(planet.tags.map { $0.name }, ["Small Rocky"])
case "Jupiter":
XCTAssertEqual(planet.star.name, "Sun")
XCTAssertEqual(planet.star.name, "Sol")
XCTAssertEqual(planet.tags.map { $0.name }, ["Gas Giant"])
default: break
}
Expand Down
16 changes: 8 additions & 8 deletions Sources/FluentBenchmark/Tests/GroupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ extension FluentBenchmarker {
XCTAssertEqual(moon.name, "Moon")
XCTAssertEqual(moon.planet.name, "Earth")
XCTAssertEqual(moon.planet.type, .smallRocky)
XCTAssertEqual(moon.planet.star.name, "Sun")
XCTAssertEqual(moon.planet.star.name, "Sol")
XCTAssertEqual(moon.planet.star.galaxy.name, "Milky Way")

// Test JSON
Expand All @@ -36,7 +36,7 @@ extension FluentBenchmarker {
XCTAssertEqual(decoded.name, "Moon")
XCTAssertEqual(decoded.planet.name, "Earth")
XCTAssertEqual(decoded.planet.type, .smallRocky)
XCTAssertEqual(decoded.planet.star.name, "Sun")
XCTAssertEqual(decoded.planet.star.name, "Sol")
XCTAssertEqual(decoded.planet.star.galaxy.name, "Milky Way")

// Test deeper filter
Expand Down Expand Up @@ -66,7 +66,7 @@ extension FluentBenchmarker {
// XCTAssertEqual(moon.name, "Moon")
// XCTAssertEqual(moon.planet.name, "Earth")
// XCTAssertEqual(moon.planet.type, .smallRocky)
// XCTAssertEqual(moon.planet.star.name, "Sun")
// XCTAssertEqual(moon.planet.star.name, "Sol")
// XCTAssertEqual(moon.planet.star.galaxy.name, "Milky Way")
//
// // Test JSON
Expand All @@ -75,7 +75,7 @@ extension FluentBenchmarker {
// XCTAssertEqual(decoded.name, "Moon")
// XCTAssertEqual(decoded.planet.name, "Earth")
// XCTAssertEqual(decoded.planet.type, .smallRocky)
// XCTAssertEqual(decoded.planet.star.name, "Sun")
// XCTAssertEqual(decoded.planet.star.name, "Sol")
// XCTAssertEqual(decoded.planet.star.galaxy.name, "Milky Way")
//
// // Test deeper filter
Expand Down Expand Up @@ -189,7 +189,7 @@ private struct FlatMoonSeed: Migration {
name: "Earth",
type: .smallRocky,
star: .init(
name: "Sun",
name: "Sol",
galaxy: .init(name: "Milky Way")
)
)
Expand All @@ -200,7 +200,7 @@ private struct FlatMoonSeed: Migration {
name: "Jupiter",
type: .gasGiant,
star: .init(
name: "Sun",
name: "Sol",
galaxy: .init(name: "Milky Way")
)
)
Expand Down Expand Up @@ -313,7 +313,7 @@ private struct FlatMoonSeed: Migration {
// name: "Earth",
// type: .smallRocky,
// star: .init(
// name: "Sun",
// name: "Sol",
// galaxy: .init(name: "Milky Way")
// )
// )
Expand All @@ -324,7 +324,7 @@ private struct FlatMoonSeed: Migration {
// name: "Jupiter",
// type: .gasGiant,
// star: .init(
// name: "Sun",
// name: "Sol",
// galaxy: .init(name: "Milky Way")
// )
// )
Expand Down
10 changes: 5 additions & 5 deletions Sources/FluentBenchmark/Tests/JoinTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extension FluentBenchmarker {
let star = try planet.joined(Star.self)
switch planet.name {
case "Earth":
XCTAssertEqual(star.name, "Sun")
XCTAssertEqual(star.name, "Sol")
case "Proxima Centauri b":
XCTAssertEqual(star.name, "Alpha Centauri")
default: break
Expand All @@ -42,7 +42,7 @@ extension FluentBenchmarker {
for galaxy in galaxies {
let star = try galaxy.joined(Star.self)
switch star.name {
case "Sun", "Alpha Centauri":
case "Sol", "Alpha Centauri":
XCTAssertEqual(galaxy.name, "Milky Way")
case "Alpheratz":
XCTAssertEqual(galaxy.name, "Andromeda")
Expand Down Expand Up @@ -193,7 +193,7 @@ extension FluentBenchmarker {
let planets = try Planet.query(on: self.database)
.field(\.$name)
.join(Star.self, on: \Planet.$star.$id == \Star.$id)
.filter(Star.self, \.$name ~~ ["Sun", "Alpha Centauri"])
.filter(Star.self, \.$name ~~ ["Sol", "Alpha Centauri"])
.field(Star.self, \.$name)
.all().wait()

Expand All @@ -203,7 +203,7 @@ extension FluentBenchmarker {
XCTAssertNil(star.$id.value)
switch planet.name {
case "Earth":
XCTAssertEqual(star.name, "Sun")
XCTAssertEqual(star.name, "Sol")
case "Proxima Centauri b":
XCTAssertEqual(star.name, "Alpha Centauri")
default: break
Expand All @@ -226,7 +226,7 @@ extension FluentBenchmarker {
XCTAssertFalse(planets.isEmpty)

let morePlanets = try Planet.query(on: self.database)
.join(Star.self, on: \Planet.$star.$id == \Star.$id && \Star.$name != "Sun")
.join(Star.self, on: \Planet.$star.$id == \Star.$id && \Star.$name != "Sol")
.all().wait()

XCTAssertEqual(morePlanets.count, 1)
Expand Down
Loading

0 comments on commit d69efce

Please sign in to comment.