Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 652f565

Browse files
add PostgreSQLUpsert support + test
1 parent 3972182 commit 652f565

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

‎Sources/FluentPostgreSQL/FluentPostgreSQLQuery.swift‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public struct FluentPostgreSQLQuery: FluentSQLQuery {
2424
public typealias SelectExpression = PostgreSQLSelectExpression
2525
public typealias Join = PostgreSQLJoin
2626
public typealias OrderBy = PostgreSQLOrderBy
27+
public typealias Upsert = PostgreSQLUpsert
2728

2829
public var statement: Statement
2930
public var table: TableIdentifier
@@ -34,6 +35,7 @@ public struct FluentPostgreSQLQuery: FluentSQLQuery {
3435
public var orderBy: [OrderBy]
3536
public var limit: Int?
3637
public var offset: Int?
38+
public var upsert: PostgreSQLUpsert?
3739
public var defaultBinaryOperator: GenericSQLBinaryOperator
3840

3941
public static func query(_ statement: Statement, _ table: TableIdentifier) -> FluentPostgreSQLQuery {
@@ -47,6 +49,7 @@ public struct FluentPostgreSQLQuery: FluentSQLQuery {
4749
orderBy: [],
4850
limit: nil,
4951
offset: nil,
52+
upsert: nil,
5053
defaultBinaryOperator: .and
5154
)
5255
}

‎Sources/FluentPostgreSQL/PostgreSQLDatabase+QuerySupporting.swift‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ extension PostgreSQLDatabase: QuerySupporting {
6565
values.append(row.value)
6666
}
6767
insert.values.append(values)
68+
insert.upsert = fluent.upsert
6869
insert.returning.append(.all)
6970
query = .insert(insert)
7071
case ._select:

‎Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,23 @@ class FluentPostgreSQLTests: XCTestCase {
360360
// let earth = try Planet.query(on: conn).filter(\.name, .ilike, "earth").first().wait()
361361
// XCTAssertEqual(earth?.name, "Earth")
362362
}
363+
364+
func testCreateOrUpdate() throws {
365+
let conn = try benchmarker.pool.requestConnection().wait()
366+
defer { benchmarker.pool.releaseConnection(conn) }
367+
defer { try? Planet.revert(on: conn).wait() }
368+
try Planet.prepare(on: conn).wait()
363369

370+
let a = Planet(id: 1, name: "Mars")
371+
let b = Planet(id: 1, name: "Earth")
372+
373+
_ = try a.create(orUpdate: true, on: conn).wait()
374+
_ = try b.create(orUpdate: true, on: conn).wait()
375+
376+
let c = try Planet.find(1, on: conn).wait()
377+
XCTAssertEqual(c?.name, "Earth")
378+
}
379+
364380
static let allTests = [
365381
("testMinimumViableModelDeclaration", testMinimumViableModelDeclaration),
366382
("testGH24", testGH24),
@@ -374,6 +390,7 @@ class FluentPostgreSQLTests: XCTestCase {
374390
("testEmptySubset", testEmptySubset),
375391
("testSort", testSort),
376392
("testCustomFilter", testCustomFilter),
393+
("testCreateOrUpdate", testCreateOrUpdate),
377394
]
378395
}
379396

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /