Record.update(_) always execute an UPDATE statement

Even if there is no column to update, for tables made of a single primary key column. The goal is to make sure that TransitionObserverType can observe all updates, event updates from minimal tables.
This commit is contained in:
Gwendal Roué committed 2015-09-29 18:36:14 +02:00
1 parent fff3f8203e
commit d0e5c8b115
4 files changed
+40 -21

No files matched your search

@@ -581,4 +581,31 @@ class TransactionObserverTests: GRDBTestCase {
}
}
func testMinimalRowIDUpdateObservation() {
// Here we test that updating a Record made of a single primary key
// column performs an actual UPDATE statement, even though it is
// totally useless (UPDATE id = 1 FROM records WHERE id = 1).
//
// It is important to update something, so that TransactionObserverType
// can observe a change.
//
// The goal is to be able to write tests with minimal tables,
// including tables made of a single primary key column. The less we
// have exceptions, the better it is.
assertNoError {
try dbQueue.inDatabase { db in
try MinimalRowID.setupInDatabase(db)
let record = MinimalRowID()
try record.save(db)
self.observer.resetCounts()
try record.update(db)
XCTAssertEqual(self.observer.didChangeCount, 1)
XCTAssertEqual(self.observer.willCommitCount, 1)
XCTAssertEqual(self.observer.didCommitCount, 1)
XCTAssertEqual(self.observer.didRollbackCount, 0)
}
}
}
}
+11 -4
View File
@@ -123,8 +123,7 @@ final class DataMapper {
return insertStatement
}
/// Returns nil if there is no column to update
func updateStatement() -> UpdateStatement? {
func updateStatement() -> UpdateStatement {
// Fail early if primary key does not resolve to a database row.
guard let primaryKeyDictionary = resolvingPrimaryKeyDictionary else {
fatalError("Invalid primary key in \(storable)")
@@ -137,8 +136,16 @@ final class DataMapper {
}
// We need something to update.
guard updatedDictionary.count > 0 else {
return nil
if updatedDictionary.count == 0 {
// IMPLEMENTATION NOTE
//
// It is important to update something, so that
// TransactionObserverType can observe a change even though this
// change is useless.
//
// The goal is to be able to write tests with minimal tables,
// including tables made of a single primary key column.
updatedDictionary = storedDatabaseDictionary
}
// Update
+2 -16
View File
@@ -329,22 +329,8 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabaseStorable {
updated.
*/
public func update(db: Database) throws {
// We'll throw RecordError.RecordNotFound if record does not exist.
let exists: Bool
if let statement = DataMapper(db, self).updateStatement() {
let changes = try statement.execute()
exists = changes.changedRowCount > 0
} else {
// No statement means that there is no column to update.
//
// I remember opening rdar://10236982 because CoreData was crashing
// with entities without any attribute. So let's accept Record
// that don't have any column to update.
exists = self.exists(db)
}
if !exists {
let changes = try DataMapper(db, self).updateStatement().execute()
if changes.changedRowCount == 0 {
throw RecordError.RecordNotFound(self)
}
-1
View File
@@ -6,7 +6,6 @@
- [X] databaseShouldCommit() should be renamed databaseWillCommit(), and throws. The error should obviously pop up.
- [X] test callbacks when a regular error is thrown (such as a constraint error, for example)
- [ ] See if TransactionObserver makes it possible to provide external data storage.
- [ ] Have Record.update(_) execute a database statement even if there is no column to update. The fact that it does not today makes TransitionObserver unable to handle this kind of records like others. Drop this exception, and make sure we have a test for it.
Not sure: