Skip to content

Commit ca27f75

Browse files
committed
Test the stable order of views with a primary key
1 parent 99ecf76 commit ca27f75

File tree

2 files changed

+69
-1
lines changed

2 files changed

+69
-1
lines changed

GRDB/QueryInterface/SQL/SQLRelation.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,6 @@ extension SQLRelation: Refinable {
286286
with { relation in
287287
relation.ordering = relation.ordering.appending(Ordering(orderings: { [relation] db in
288288
do {
289-
#warning("TODO: test with views")
290289
// Order by primary key. Don't order by rowid because those are
291290
// not stable: rowids can change after a vacuum.
292291
return try db.primaryKey(source.tableName).columns.map { SQLExpression.column($0).sqlOrdering }

Tests/GRDBTests/DatabaseDumpTests.swift

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ private struct Player: Codable, MutablePersistableRecord {
2222
mutating func didInsert(_ inserted: InsertionSuccess) {
2323
id = inserted.rowID
2424
}
25+
26+
enum Columns {
27+
static let name = Column(CodingKeys.name)
28+
static let teamId = Column(CodingKeys.teamId)
29+
}
2530
}
2631

2732
private struct Team: Codable, PersistableRecord {
@@ -1141,6 +1146,70 @@ final class DatabaseDumpTests: GRDBTestCase {
11411146
}
11421147
}
11431148

1149+
func test_dumpTables_single_view_with_schema_source() throws {
1150+
struct SchemaSource: DatabaseSchemaSource {
1151+
func columnsForPrimaryKey(_ db: Database, inView view: DatabaseObjectID) throws -> [String]? {
1152+
["teamId", "name"]
1153+
}
1154+
}
1155+
dbConfiguration.schemaSource = SchemaSource()
1156+
1157+
try makeRugbyDatabase().write { db in
1158+
try db.create(view: "playerName", as: Player
1159+
.orderByPrimaryKey()
1160+
.filter { $0.teamId != nil }
1161+
.select { [$0.teamId, $0.name] })
1162+
1163+
do {
1164+
// Default order: use the view ordering
1165+
do {
1166+
// Default format
1167+
let stream = TestStream()
1168+
try db.dumpTables(["playerName"], to: stream)
1169+
XCTAssertEqual(stream.output, """
1170+
FRA|Antoine Dupond
1171+
ENG|Owen Farrell
1172+
1173+
""")
1174+
}
1175+
do {
1176+
// Custom format
1177+
let stream = TestStream()
1178+
try db.dumpTables(["playerName"], format: .json(), to: stream)
1179+
XCTAssertEqual(stream.output, """
1180+
[{"teamId":"FRA","name":"Antoine Dupond"},
1181+
{"teamId":"ENG","name":"Owen Farrell"}]
1182+
1183+
""")
1184+
}
1185+
}
1186+
1187+
do {
1188+
// Stable order (primary key)
1189+
do {
1190+
// Default format
1191+
let stream = TestStream()
1192+
try db.dumpTables(["playerName"], stableOrder: true, to: stream)
1193+
XCTAssertEqual(stream.output, """
1194+
ENG|Owen Farrell
1195+
FRA|Antoine Dupond
1196+
1197+
""")
1198+
}
1199+
do {
1200+
// Custom format
1201+
let stream = TestStream()
1202+
try db.dumpTables(["playerName"], format: .json(), stableOrder: true, to: stream)
1203+
XCTAssertEqual(stream.output, """
1204+
[{"teamId":"ENG","name":"Owen Farrell"},
1205+
{"teamId":"FRA","name":"Antoine Dupond"}]
1206+
1207+
""")
1208+
}
1209+
}
1210+
}
1211+
}
1212+
11441213
func test_dumpTables_multiple() throws {
11451214
try makeRugbyDatabase().read { db in
11461215
do {

0 commit comments

Comments
 (0)