Test that the truncate optimization does not prevent transaction observers from observing individual deletions

This commit is contained in:
Gwendal Roué committed 2016-09-22 07:45:32 +02:00
1 parent dc58ff19d4
commit e7378cbba0
1 file changed
+42
@@ -315,6 +315,48 @@ class TransactionObserverTests: GRDBTestCase {
}
}
func testTruncateOptimization() {
// https://www.sqlite.org/c3ref/update_hook.html
//
// > In the current implementation, the update hook is not invoked [...]
// > when rows are deleted using the truncate optimization.
//
// https://www.sqlite.org/lang_delete.html#truncateopt
//
// > When the WHERE is omitted from a DELETE statement and the table
// > being deleted has no triggers, SQLite uses an optimization to erase
// > the entire table content without having to visit each row of the
// > table individually.
//
// Here we test that the truncate optimization does not prevent
// transaction observers from observing individual deletions.
assertNoError {
let dbQueue = try makeDatabaseQueue()
let observer = Observer()
dbQueue.add(transactionObserver: observer)
try dbQueue.inDatabase { db in
let artist1 = Artist(name: "Gerhard Richter")
let artist2 = Artist(name: "Vincent Fournier")
try artist1.insert(db)
try artist2.insert(db)
try db.execute("DELETE FROM artists")
XCTAssertEqual(observer.lastCommittedEvents.count, 2)
let artist1DeleteEvent = observer.lastCommittedEvents.filter {
self.match(event: $0, kind: .delete, tableName: "artists", rowId: artist1.id!)
}.first
XCTAssertTrue(artist1DeleteEvent != nil)
let artist2DeleteEvent = observer.lastCommittedEvents.filter {
self.match(event: $0, kind: .delete, tableName: "artists", rowId: artist1.id!)
}.first
XCTAssertTrue(artist2DeleteEvent != nil)
}
}
}
func testCascadingDeleteEvents() {
assertNoError {
let dbQueue = try makeDatabaseQueue()