From ca27f75760f3ac66e99d9081df2b0b93e06cb3bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 6 Sep 2025 11:31:43 +0200 Subject: [PATCH] Test the stable order of views with a primary key --- GRDB/QueryInterface/SQL/SQLRelation.swift | 1 - Tests/GRDBTests/DatabaseDumpTests.swift | 69 +++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/GRDB/QueryInterface/SQL/SQLRelation.swift b/GRDB/QueryInterface/SQL/SQLRelation.swift index 764d236a0..6fdf125db 100644 --- a/GRDB/QueryInterface/SQL/SQLRelation.swift +++ b/GRDB/QueryInterface/SQL/SQLRelation.swift @@ -286,7 +286,6 @@ extension SQLRelation: Refinable { with { relation in relation.ordering = relation.ordering.appending(Ordering(orderings: { [relation] db in do { - #warning("TODO: test with views") // Order by primary key. Don't order by rowid because those are // not stable: rowids can change after a vacuum. return try db.primaryKey(source.tableName).columns.map { SQLExpression.column($0).sqlOrdering } diff --git a/Tests/GRDBTests/DatabaseDumpTests.swift b/Tests/GRDBTests/DatabaseDumpTests.swift index 1013d713c..8a654f85a 100644 --- a/Tests/GRDBTests/DatabaseDumpTests.swift +++ b/Tests/GRDBTests/DatabaseDumpTests.swift @@ -22,6 +22,11 @@ private struct Player: Codable, MutablePersistableRecord { mutating func didInsert(_ inserted: InsertionSuccess) { id = inserted.rowID } + + enum Columns { + static let name = Column(CodingKeys.name) + static let teamId = Column(CodingKeys.teamId) + } } private struct Team: Codable, PersistableRecord { @@ -1141,6 +1146,70 @@ final class DatabaseDumpTests: GRDBTestCase { } } + func test_dumpTables_single_view_with_schema_source() throws { + struct SchemaSource: DatabaseSchemaSource { + func columnsForPrimaryKey(_ db: Database, inView view: DatabaseObjectID) throws -> [String]? { + ["teamId", "name"] + } + } + dbConfiguration.schemaSource = SchemaSource() + + try makeRugbyDatabase().write { db in + try db.create(view: "playerName", as: Player + .orderByPrimaryKey() + .filter { $0.teamId != nil } + .select { [$0.teamId, $0.name] }) + + do { + // Default order: use the view ordering + do { + // Default format + let stream = TestStream() + try db.dumpTables(["playerName"], to: stream) + XCTAssertEqual(stream.output, """ + FRA|Antoine Dupond + ENG|Owen Farrell + + """) + } + do { + // Custom format + let stream = TestStream() + try db.dumpTables(["playerName"], format: .json(), to: stream) + XCTAssertEqual(stream.output, """ + [{"teamId":"FRA","name":"Antoine Dupond"}, + {"teamId":"ENG","name":"Owen Farrell"}] + + """) + } + } + + do { + // Stable order (primary key) + do { + // Default format + let stream = TestStream() + try db.dumpTables(["playerName"], stableOrder: true, to: stream) + XCTAssertEqual(stream.output, """ + ENG|Owen Farrell + FRA|Antoine Dupond + + """) + } + do { + // Custom format + let stream = TestStream() + try db.dumpTables(["playerName"], format: .json(), stableOrder: true, to: stream) + XCTAssertEqual(stream.output, """ + [{"teamId":"ENG","name":"Owen Farrell"}, + {"teamId":"FRA","name":"Antoine Dupond"}] + + """) + } + } + } + } + func test_dumpTables_multiple() throws { try makeRugbyDatabase().read { db in do {