mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-29 04:24:21 +00:00
Address #620, but a failing test remains
This commit is contained in:
1 parent
3f3bdde10c
commit
1b753802dd
3 files changed
+127
-117
No files matched your search
@@ -191,17 +191,27 @@ extension DatabaseRegion {
|
||||
// performed in observes(eventsOfKind:), the event argument is
|
||||
// guaranteed to be about the fetched table. We thus only have to
|
||||
// check for rowIds.
|
||||
assert(event.tableName == tableRegions[tableRegions.startIndex].key) // sanity check in debug mode
|
||||
let tableRegion = tableRegions[tableRegions.startIndex].value
|
||||
return tableRegion.contains(rowID: event.rowID)
|
||||
|
||||
let (tableName, tableRegion) = tableRegions[tableRegions.startIndex]
|
||||
if event.tableName == tableName {
|
||||
return tableRegion.contains(rowID: event.rowID)
|
||||
} else {
|
||||
// Shouldn't happen if the precondition is met.
|
||||
// But FTS4 (and maybe other virtual tables) perform
|
||||
// unadvertised changes. So let's assume the region is modified,
|
||||
// just is case.
|
||||
// See https://github.com/groue/GRDB.swift/issues/620
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
// Slow path when several tables are observed.
|
||||
guard let tableRegion = tableRegions[event.tableName] else {
|
||||
// Shouldn't happen if the precondition is met.
|
||||
fatalError("""
|
||||
precondition failure: event was not filtered out in observes(eventsOfKind:) \
|
||||
by region.isModified(byEventsOfKind:)
|
||||
""")
|
||||
// But FTS4 (and maybe other virtual tables) perform
|
||||
// unadvertised changes. So let's assume the region is modified,
|
||||
// just is case.
|
||||
// See https://github.com/groue/GRDB.swift/issues/620
|
||||
return true
|
||||
}
|
||||
return tableRegion.contains(rowID: event.rowID)
|
||||
}
|
||||
|
||||
@@ -330,114 +330,4 @@ class FTS4TableBuilderTests: GRDBTestCase {
|
||||
XCTAssertTrue(uncompressCalled)
|
||||
}
|
||||
}
|
||||
|
||||
// Issue 620
|
||||
func testObservation() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(virtualTable: "ft_documents", using: FTS4())
|
||||
}
|
||||
|
||||
var rows: [[Row]] = []
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
let request = SQLRequest<Row>(sql: "SELECT * FROM ft_documents")
|
||||
let observation = ValueObservation.tracking(value: request.fetchAll)
|
||||
let observer = observation.start(
|
||||
in: dbQueue,
|
||||
onError: { error in
|
||||
XCTFail("unexpected error: \(error)")
|
||||
},
|
||||
onChange: {
|
||||
rows.append($0)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
try withExtendedLifetime(observer) {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "INSERT INTO ft_documents VALUES (?)", arguments: ["foo"])
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(rows, [[], [["content":"foo"]]])
|
||||
}
|
||||
}
|
||||
|
||||
// Issue 620
|
||||
func testObservationSynchronized1() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "documents") { t in
|
||||
t.column("id", .integer).primaryKey()
|
||||
t.column("content", .text)
|
||||
}
|
||||
try db.create(virtualTable: "ft_documents", using: FTS4()) { t in
|
||||
t.synchronize(withTable: "documents")
|
||||
t.column("content")
|
||||
}
|
||||
}
|
||||
|
||||
var rows: [[Row]] = []
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
let request = SQLRequest<Row>(sql: "SELECT * FROM documents")
|
||||
let observation = ValueObservation.tracking(value: request.fetchAll)
|
||||
let observer = observation.start(
|
||||
in: dbQueue,
|
||||
onError: { error in
|
||||
XCTFail("unexpected error: \(error)")
|
||||
},
|
||||
onChange: {
|
||||
rows.append($0)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
try withExtendedLifetime(observer) {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "INSERT INTO documents (content) VALUES (?)", arguments: ["foo"])
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(rows, [[], [["id":1, "content":"foo"]]])
|
||||
}
|
||||
}
|
||||
|
||||
// Issue 620
|
||||
func testObservationSynchronized2() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "documents") { t in
|
||||
t.column("id", .integer).primaryKey()
|
||||
t.column("content", .text)
|
||||
}
|
||||
try db.create(virtualTable: "ft_documents", using: FTS4()) { t in
|
||||
t.synchronize(withTable: "documents")
|
||||
t.column("content")
|
||||
}
|
||||
}
|
||||
|
||||
var rows: [[Row]] = []
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
let request = SQLRequest<Row>(sql: "SELECT * FROM ft_documents")
|
||||
let observation = ValueObservation.tracking(value: request.fetchAll)
|
||||
let observer = observation.start(
|
||||
in: dbQueue,
|
||||
onError: { error in
|
||||
XCTFail("unexpected error: \(error)")
|
||||
},
|
||||
onChange: {
|
||||
rows.append($0)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
try withExtendedLifetime(observer) {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute(sql: "INSERT INTO documents (content) VALUES (?)", arguments: ["foo"])
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(rows, [[], [["content":"foo"]]])
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -154,4 +154,114 @@ class ValueObservationRowTests: GRDBTestCase {
|
||||
["id":2, "name":"bar"],
|
||||
nil])
|
||||
}
|
||||
|
||||
func testFTS4Observation() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(virtualTable: "ft_documents", using: FTS4())
|
||||
}
|
||||
|
||||
var rows: [[Row]] = []
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
let request = SQLRequest<Row>(sql: "SELECT * FROM ft_documents")
|
||||
let observation = ValueObservation.tracking(value: request.fetchAll)
|
||||
let observer = observation.start(
|
||||
in: dbQueue,
|
||||
onError: { error in
|
||||
XCTFail("unexpected error: \(error)")
|
||||
},
|
||||
onChange: {
|
||||
rows.append($0)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
try withExtendedLifetime(observer) {
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "INSERT INTO ft_documents VALUES (?)", arguments: ["foo"])
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(rows, [[], [["content":"foo"]]])
|
||||
}
|
||||
}
|
||||
|
||||
func testSynchronizedFTS4Observation() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "documents") { t in
|
||||
t.column("id", .integer).primaryKey()
|
||||
t.column("content", .text)
|
||||
}
|
||||
try db.create(virtualTable: "ft_documents", using: FTS4()) { t in
|
||||
t.synchronize(withTable: "documents")
|
||||
t.column("content")
|
||||
}
|
||||
}
|
||||
|
||||
var rows: [[Row]] = []
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
let request = SQLRequest<Row>(sql: "SELECT * FROM ft_documents")
|
||||
let observation = ValueObservation.tracking(value: request.fetchAll)
|
||||
let observer = observation.start(
|
||||
in: dbQueue,
|
||||
onError: { error in
|
||||
XCTFail("unexpected error: \(error)")
|
||||
},
|
||||
onChange: {
|
||||
rows.append($0)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
try withExtendedLifetime(observer) {
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "INSERT INTO documents (content) VALUES (?)", arguments: ["foo"])
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(rows, [[], [["content":"foo"]]])
|
||||
}
|
||||
}
|
||||
|
||||
func testJoinedFTS4Observation() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "document") { t in
|
||||
t.autoIncrementedPrimaryKey("id")
|
||||
}
|
||||
try db.create(virtualTable: "ft_document", using: FTS4()) { t in
|
||||
t.column("content")
|
||||
}
|
||||
}
|
||||
|
||||
var rows: [[Row]] = []
|
||||
let notificationExpectation = expectation(description: "notification")
|
||||
notificationExpectation.assertForOverFulfill = true
|
||||
notificationExpectation.expectedFulfillmentCount = 2
|
||||
|
||||
let request = SQLRequest<Row>(sql: """
|
||||
SELECT document.* FROM document
|
||||
JOIN ft_document ON ft_document.rowid = document.id
|
||||
WHERE ft_document MATCH 'foo'
|
||||
""")
|
||||
let observation = ValueObservation.tracking(value: request.fetchAll)
|
||||
let observer = observation.start(
|
||||
in: dbQueue,
|
||||
onError: { error in
|
||||
XCTFail("unexpected error: \(error)")
|
||||
},
|
||||
onChange: {
|
||||
rows.append($0)
|
||||
notificationExpectation.fulfill()
|
||||
})
|
||||
try withExtendedLifetime(observer) {
|
||||
try dbQueue.write { db in
|
||||
try db.execute(sql: "INSERT INTO document (id) VALUES (?)", arguments: [1])
|
||||
try db.execute(sql: "INSERT INTO ft_document (rowid, content) VALUES (?, ?)", arguments: [1, "foo"])
|
||||
}
|
||||
waitForExpectations(timeout: 1, handler: nil)
|
||||
XCTAssertEqual(rows, [[], [["id":1]]])
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user