Skip to content

Commit b36bd0b

Browse files
committed
order by updates
1 parent 7413187 commit b36bd0b

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

Sources/FluentPostgreSQL/PostgreSQLDatabase+Fluent.swift

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ extension PostgreSQLQuery {
8282
case .insert:
8383
query = .insert(.init(
8484
table: fluent.table,
85-
values: fluent.values,
85+
columns: .init(fluent.values.keys),
86+
values: [.init(fluent.values.values)],
8687
returning: fluent.returning
8788
))
8889
case .select:
@@ -165,10 +166,6 @@ extension PostgreSQLDatabase: QuerySupporting & JoinSupporting & MigrationSuppor
165166
query.joins.append(join)
166167
}
167168

168-
public static func prepareMigrationMetadata(on conn: PostgreSQLConnection) -> EventLoopFuture<Void> {
169-
fatalError()
170-
}
171-
172169
public static func query(_ entity: String) -> PostgreSQLQuery.FluentQuery {
173170
return .init(table: .init(name: entity))
174171
}
@@ -363,7 +360,7 @@ extension PostgreSQLDatabase: QuerySupporting & JoinSupporting & MigrationSuppor
363360
}
364361

365362
public static func querySort(_ column: PostgreSQLQuery.Column, _ direction: PostgreSQLQuery.OrderBy.Direction) -> PostgreSQLQuery.OrderBy {
366-
return .init(columns: [column], direction: direction)
363+
return .init(.column(column), direction: direction)
367364
}
368365

369366
public static var querySortDirectionAscending: PostgreSQLQuery.OrderBy.Direction {

Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,8 @@ class FluentPostgreSQLTests: XCTestCase {
226226
try DefaultTest.prepare(on: conn).wait()
227227
_ = try DefaultTest().save(on: conn).wait()
228228
let fetched = try DefaultTest.query(on: conn).first().wait()!
229-
// within 10 seconds
230-
XCTAssertEqual(Date().timeIntervalSinceReferenceDate, fetched.date!.timeIntervalSinceReferenceDate, accuracy: 15)
229+
// within 1 minute
230+
XCTAssertEqual(Date().timeIntervalSinceReferenceDate, fetched.date!.timeIntervalSinceReferenceDate, accuracy: 60)
231231
}
232232

233233
func testGH30() throws {
@@ -411,6 +411,29 @@ class FluentPostgreSQLTests: XCTestCase {
411411
_ = try User.query(on: conn).filter(\.id ~~ [1, 2]).all().wait()
412412
_ = try User.query(on: conn).filter(\.id ~~ [1, 2, 3]).all().wait()
413413
}
414+
415+
func testSort() throws {
416+
struct Planet: PostgreSQLModel, PostgreSQLMigration, Equatable {
417+
var id: Int?
418+
var name: String
419+
init(id: Int? = nil, name: String) {
420+
self.id = id
421+
self.name = name
422+
}
423+
}
424+
let conn = try benchmarker.pool.requestConnection().wait()
425+
defer { benchmarker.pool.releaseConnection(conn) }
426+
try Planet.prepare(on: conn).wait()
427+
defer { _ = try? Planet.revert(on: conn).wait() }
428+
429+
_ = try Planet(name: "Jupiter").save(on: conn).wait()
430+
_ = try Planet(name: "Earth").save(on: conn).wait()
431+
_ = try Planet(name: "Mars").save(on: conn).wait()
432+
433+
let unordered = try Planet.query(on: conn).all().wait()
434+
let ordered = try Planet.query(on: conn).sort(\.name).all().wait()
435+
XCTAssertNotEqual(unordered, ordered)
436+
}
414437

415438
static let allTests = [
416439
("testSchema", testSchema),
@@ -437,6 +460,7 @@ class FluentPostgreSQLTests: XCTestCase {
437460
("testDocs_type", testDocs_type),
438461
("testContains", testContains),
439462
("testEmptySubset", testEmptySubset),
463+
("testSort", testSort),
440464
]
441465
}
442466

0 commit comments

Comments
 (0)