From 1b753802dd010f4fc62e8eaa832ee3df48e2b8ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 25 Sep 2019 20:03:00 +0200 Subject: [PATCH] Address #620, but a failing test remains --- GRDB/Core/DatabaseRegion.swift | 24 ++-- Tests/GRDBTests/FTS4TableBuilderTests.swift | 110 ------------------ .../GRDBTests/ValueObservationRowTests.swift | 110 ++++++++++++++++++ 3 files changed, 127 insertions(+), 117 deletions(-) diff --git a/GRDB/Core/DatabaseRegion.swift b/GRDB/Core/DatabaseRegion.swift index 793c82f4b..5f5aa5cda 100644 --- a/GRDB/Core/DatabaseRegion.swift +++ b/GRDB/Core/DatabaseRegion.swift @@ -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) } diff --git a/Tests/GRDBTests/FTS4TableBuilderTests.swift b/Tests/GRDBTests/FTS4TableBuilderTests.swift index d5b962052..4069ca0a1 100644 --- a/Tests/GRDBTests/FTS4TableBuilderTests.swift +++ b/Tests/GRDBTests/FTS4TableBuilderTests.swift @@ -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(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(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(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"]]]) - } - } } diff --git a/Tests/GRDBTests/ValueObservationRowTests.swift b/Tests/GRDBTests/ValueObservationRowTests.swift index be03c5d80..4e3c8dc58 100644 --- a/Tests/GRDBTests/ValueObservationRowTests.swift +++ b/Tests/GRDBTests/ValueObservationRowTests.swift @@ -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(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(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(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]]]) + } + } }