Skip to content

Commit

Permalink
Enable using the attached model during pivot editing (#565)
Browse files Browse the repository at this point in the history
* Make sure the value of the right-hand side of a new pivot is set to the model that particular pivot refers to so the edit closure can pull details from it without having to match up IDs by hand. Add a test for it. While we're at it, add a sample of an extra pivot property to PlanetTag and clean up its migration.
  • Loading branch information
gwynne authored Jun 3, 2023
1 parent 0b3e3a4 commit 04691f8
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
API breakage: constructor PlanetTag.init(id:planetID:tagID:) has been removed
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ on:
push: { branches: [ main ] }

env:
LOG_LEVEL: debug
LOG_LEVEL: info
SWIFT_DETERMINISTIC_HASHING: 1
POSTGRES_HOSTNAME: 'psql-a'
POSTGRES_HOSTNAME_A: 'psql-a'
Expand Down
19 changes: 13 additions & 6 deletions Sources/FluentBenchmark/SolarSystem/PlanetTag.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,36 @@ public final class PlanetTag: Model {

@Parent(key: "tag_id")
public var tag: Tag

@OptionalField(key: "comments")
public var comments: String?

public init() { }

public init(id: IDValue? = nil, planetID: Planet.IDValue, tagID: Tag.IDValue) {
public init(id: IDValue? = nil, planetID: Planet.IDValue, tagID: Tag.IDValue, comments: String? = nil) {
self.id = id
self.$planet.id = planetID
self.$tag.id = tagID
self.comments = comments
}
}

public struct PlanetTagMigration: Migration {
public init() { }

public func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("planet+tag")
.field("id", .uuid, .identifier(auto: false))
.field("planet_id", .uuid, .required, .references("planets", "id"))
.field("tag_id", .uuid, .required, .references("tags", "id"))
database.schema(PlanetTag.schema)
.id()
.field("planet_id", .uuid, .required)
.field("tag_id", .uuid, .required)
.field("comments", .string)
.foreignKey("planet_id", references: Planet.schema, .id)
.foreignKey("tag_id", references: Tag.schema, .id)
.create()
}

public func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema("planet+tag").delete()
database.schema(PlanetTag.schema).delete()
}
}

Expand Down
17 changes: 17 additions & 0 deletions Sources/FluentBenchmark/Tests/SiblingsTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,23 @@ extension FluentBenchmarker {
XCTAssertEqual(tags.count, 2)
XCTAssertEqual(tags.map(\.name).sorted(), ["Inhabited", "Small Rocky"])
}

try earth.$tags.detachAll(on: self.database).wait()

// check pivot provided to the edit closure has the "to" model when attaching
try earth.$tags.attach([inhabited, smallRocky], on: self.database) { pivot in
guard pivot.$tag.value != nil else {
return XCTFail("planet tag pivot should have tag available during editing")
}
pivot.comments = "Tagged with name \(pivot.tag.name)"
}.wait()

do {
let pivots = try earth.$tags.$pivots.get(reload: true, on: self.database).wait()

XCTAssertEqual(pivots.count, 2)
XCTAssertEqual(pivots.compactMap(\.comments).sorted(), ["Tagged with name Inhabited", "Tagged with name Small Rocky"])
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions Sources/FluentKit/Properties/Siblings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ public final class SiblingsProperty<From, To, Through>
let pivot = Through()
pivot[keyPath: self.from].id = fromID
pivot[keyPath: self.to].id = toID
pivot[keyPath: self.to].value = to
edit(pivot)
return pivot
}.create(on: database)
Expand Down Expand Up @@ -180,6 +181,7 @@ public final class SiblingsProperty<From, To, Through>
let pivot = Through()
pivot[keyPath: self.from].id = fromID
pivot[keyPath: self.to].id = toID
pivot[keyPath: self.to].value = to
edit(pivot)
return pivot.save(on: database)
}
Expand Down

0 comments on commit 04691f8

Please sign in to comment.