diff --git a/GRDB/Core/TransactionObserver.swift b/GRDB/Core/TransactionObserver.swift index da033b840..3bd438075 100644 --- a/GRDB/Core/TransactionObserver.swift +++ b/GRDB/Core/TransactionObserver.swift @@ -774,13 +774,14 @@ public protocol TransactionObserver: AnyObject { /// from being applied on the observed tables. func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool - /// Returns whether statements made by the statement should be notified to the observer. + /// Returns whether this observer should unconditionally be notified for all database changes, regardless of + /// ``observes(eventsOfKind:)``. /// /// This method can be used to observe indirect writes from statements (e.g. a statement invoking /// a custom SQL function which internally runs its own statements). ``observes(eventsOfKind:)`` /// would not be called for such statements because potential writes can't be inferred from the syntax of /// these statements. - func observes(statement: Statement) -> Bool + var observesAllDatabaseChanges: Bool { get } /// Called when the database was modified in some unspecified way. /// @@ -860,8 +861,8 @@ public protocol TransactionObserver: AnyObject { extension TransactionObserver { /// The default implementation does not observe statements. - public func observes(statement: Statement) -> Bool { - return false + public var observesAllDatabaseChanges: Bool { + false } /// The default implementation does nothing. @@ -958,7 +959,11 @@ final class TransactionObservation { } func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool { - observer?.observes(eventsOfKind: eventKind) ?? false + guard let observer else { + return false + } + return observer.observesAllDatabaseChanges || observer + .observes(eventsOfKind: eventKind) } func observations(for statement: Statement) -> StatementObservation? { @@ -966,7 +971,7 @@ final class TransactionObservation { return nil } - if observer.observes(statement: statement) { + if observer.observesAllDatabaseChanges { return StatementObservation( transactionObservation: self, trackingEvents: .all) diff --git a/GRDB/Documentation.docc/Extension/DatabaseRegionObservation.md b/GRDB/Documentation.docc/Extension/DatabaseRegionObservation.md index 172cc169b..947018909 100644 --- a/GRDB/Documentation.docc/Extension/DatabaseRegionObservation.md +++ b/GRDB/Documentation.docc/Extension/DatabaseRegionObservation.md @@ -79,7 +79,7 @@ let observation = DatabaseRegionObservation( `DatabaseRegionObservation` will not notify impactful transactions whenever the database is modified in an undetectable way: - Changes performed by external database connections. -- Changes performed by SQLite statements that are not compiled and executed by GRDB. +- Changes performed by SQLite statements that are not compiled and executed by GRDB, including statements ran from a custom SQL function that is itself invoked with GRDB APIs. - Changes to the database schema, changes to internal system tables such as `sqlite_master`. - Changes to [`WITHOUT ROWID`](https://www.sqlite.org/withoutrowid.html) tables. diff --git a/GRDB/Documentation.docc/Extension/TransactionObserver.md b/GRDB/Documentation.docc/Extension/TransactionObserver.md index 72acd5e51..02128fa74 100644 --- a/GRDB/Documentation.docc/Extension/TransactionObserver.md +++ b/GRDB/Documentation.docc/Extension/TransactionObserver.md @@ -125,6 +125,9 @@ class PlayerObserver: TransactionObserver { When the `observes(eventsOfKind:)` method returns false for all event kinds, the observer is still notified of transactions. +In addition to ``observes(eventsOfKind:)``, transaction observers can implement the `observesAllDatabaseChanges` getter. When set to `true`, the observer will +observe all changes to the database. + ## Observation Extent **You can specify how long an observer is notified of database changes and transactions.** @@ -232,7 +235,7 @@ The changes and transactions that are not automatically notified to transaction - Read-only transactions. - Changes and transactions performed by external database connections. -- Changes performed by SQLite statements that are not both compiled and executed through GRDB APIs. +- Changes performed by SQLite statements that are not both compiled and executed through GRDB APIs, including statements ran from a custom SQL function that is itself invoked with GRDB APIs. - Changes to the database schema, changes to internal system tables such as `sqlite_master`. - Changes to [`WITHOUT ROWID`](https://www.sqlite.org/withoutrowid.html) tables. - The deletion of duplicate rows triggered by [`ON CONFLICT REPLACE`](https://www.sqlite.org/lang_conflict.html) clauses (this last exception might change in a future release of SQLite). diff --git a/GRDB/Documentation.docc/Extension/ValueObservation.md b/GRDB/Documentation.docc/Extension/ValueObservation.md index a1d192e23..9d4fa6a38 100644 --- a/GRDB/Documentation.docc/Extension/ValueObservation.md +++ b/GRDB/Documentation.docc/Extension/ValueObservation.md @@ -228,7 +228,7 @@ This ``tracking(region:_:fetch:)`` method lets you entirely separate the **obser `ValueObservation` will not fetch and notify a fresh value whenever the database is modified in an undetectable way: - Changes performed by external database connections. -- Changes performed by SQLite statements that are not compiled and executed by GRDB. +- Changes performed by SQLite statements that are not compiled and executed by GRDB, including statements ran from a custom SQL function that is itself invoked with GRDB APIs. - Changes to the database schema, changes to internal system tables such as `sqlite_master`. - Changes to [`WITHOUT ROWID`](https://www.sqlite.org/withoutrowid.html) tables. diff --git a/Tests/GRDBTests/TransactionObserverObserveStatementTests.swift b/Tests/GRDBTests/TransactionObserverObserveEverythingTest.swift similarity index 67% rename from Tests/GRDBTests/TransactionObserverObserveStatementTests.swift rename to Tests/GRDBTests/TransactionObserverObserveEverythingTest.swift index b764ce82a..f5b3f7932 100644 --- a/Tests/GRDBTests/TransactionObserverObserveStatementTests.swift +++ b/Tests/GRDBTests/TransactionObserverObserveEverythingTest.swift @@ -9,12 +9,12 @@ private class EverythingObserver: TransactionObserver { changedTables.insert(event.tableName) } - func observes(eventsOfKind eventKind: GRDB.DatabaseEventKind) -> Bool { - return true + var observesAllDatabaseChanges: Bool { + true } - func observes(statement: Statement) -> Bool { - return true + func observes(eventsOfKind eventKind: DatabaseEventKind) -> Bool { + fatalError("Should not be called since observesAllDatbaaseChanges is true") } func databaseDidCommit(_ db: GRDB.Database) {} @@ -26,7 +26,7 @@ private struct SendablePtr: @unchecked Sendable { let ptr: OpaquePointer } -class TransactionObserverObserveStatementTests: GRDBTestCase { +class TransactionObserverObserveEverythingTests: GRDBTestCase { func testIndirectWrite() throws { var config = Configuration() config.prepareDatabase{ database in @@ -57,4 +57,20 @@ class TransactionObserverObserveStatementTests: GRDBTestCase { XCTAssert(observer.changedTables == ["foo"]) } + + func testDisablesTruncateOptimization() throws { + let db = try makeDatabaseQueue() + try db.writeWithoutTransaction { db in + try db.execute(sql: "CREATE TABLE foo (bar TEXT)") + try db.execute(sql: "INSERT INTO foo DEFAULT VALUES") + } + + let observer = EverythingObserver() + db.add(transactionObserver: observer) + try db.writeWithoutTransaction { db in + try db.execute(sql: "DELETE FROM foo") + } + + XCTAssert(observer.changedTables == ["foo"]) + } }