From 86bde428bfc5cc4d3003ade2781aa72329e9b73e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Wed, 22 Feb 2023 06:13:32 +0100 Subject: [PATCH] Make TimestampedRecord a subprotocol of MutablePersistableRecord --- GRDB/Documentation.docc/RecordTimestamps.md | 16 +++--- Tests/GRDBTests/TransactionDateTests.swift | 60 +++++++++++++++++++-- 2 files changed, 63 insertions(+), 13 deletions(-) diff --git a/GRDB/Documentation.docc/RecordTimestamps.md b/GRDB/Documentation.docc/RecordTimestamps.md index 08089fa7e..2afc02564 100644 --- a/GRDB/Documentation.docc/RecordTimestamps.md +++ b/GRDB/Documentation.docc/RecordTimestamps.md @@ -141,17 +141,17 @@ It provides the following methods: - `updateWithTimestamp()` behaves like ``MutablePersistableRecord/update(_:onConflict:)``, but it also bumps the modification date. - `updateChangesWithTimestamp()` behaves like ``MutablePersistableRecord/updateChanges(_:onConflict:modify:)``, but it also bumps the modification date if the record is modified. - `touch()` only updates the modification date, just like the `touch` unix command. -- `initializeTimestamps()` is available for `TimestampedRecord` types that customize their `willInsert` callback. +- `TimestampedRecord` types that customize their `willInsert` callback should call `initializeTimestamps()` in their implementation. ```swift -/// A type that tracks its creation and modification dates. See +/// A record type that tracks its creation and modification dates. See /// -protocol TimestampedRecord { +protocol TimestampedRecord: MutablePersistableRecord { var creationDate: Date? { get set } var modificationDate: Date? { get set } } -extension TimestampedRecord where Self: MutablePersistableRecord { +extension TimestampedRecord { /// By default, `TimestampedRecord` types set `creationDate` and /// `modificationDate` to the transaction date, if they are nil, /// before insertion. @@ -247,7 +247,7 @@ extension TimestampedRecord where Self: MutablePersistableRecord { Usage: ```swift -extension Player: Codable, MutablePersistableRecord, FetchableRecord, TimestampedRecord { +extension Player: Codable, TimestampedRecord, FetchableRecord { /// Update auto-incremented id upon successful insertion mutating func didInsert(_ inserted: InsertionSuccess) { id = inserted.rowID @@ -272,11 +272,11 @@ try dbQueue.write { db in $0.score = 1000 } - // updateChanges() is able to always bump the modification date, while - // only updating the changed columns. + // updateChanges() is able to always bump the modification date, even if + // other columns are not changed. try player.updateChanges(db) { $0.score = 1000 - $0.modificationDate = db.transactionDate + $0.modificationDate = try db.transactionDate } // touch() only updates the modification date in the database. diff --git a/Tests/GRDBTests/TransactionDateTests.swift b/Tests/GRDBTests/TransactionDateTests.swift index 8618fb5b2..3d6754173 100644 --- a/Tests/GRDBTests/TransactionDateTests.swift +++ b/Tests/GRDBTests/TransactionDateTests.swift @@ -278,8 +278,8 @@ class TransactionDateTests: GRDBTestCase { } } - func test_TimestampedRecord_customized_willInsert() throws { - struct Player: Codable, MutablePersistableRecord, FetchableRecord, TimestampedRecord { + func test_TimestampedRecord_struct_with_customized_willInsert() throws { + struct Player: Codable, TimestampedRecord, FetchableRecord { var id: Int64? var creationDate: Date? var modificationDate: Date? @@ -324,18 +324,68 @@ class TransactionDateTests: GRDBTestCase { XCTAssertEqual(player.modificationDate, .distantPast) } } + + func test_TimestampedRecord_class_with_non_mutating_willInsert() throws { + class Player: Codable, TimestampedRecord, PersistableRecord, FetchableRecord { + var id: Int64? + var creationDate: Date? + var modificationDate: Date? + var name: String + + init(id: Int64? = nil, creationDate: Date? = nil, modificationDate: Date? = nil, name: String) { + self.id = id + self.creationDate = creationDate + self.modificationDate = modificationDate + self.name = name + } + + func willInsert(_ db: Database) throws { + // Can't call initializeTimestamps because it is mutating + if creationDate == nil { + creationDate = try db.transactionDate + } + if modificationDate == nil { + modificationDate = try db.transactionDate + } + } + + func didInsert(_ inserted: InsertionSuccess) { + id = inserted.rowID + } + } + + var currentDate = Date.distantPast + dbConfiguration.transactionClock = .custom { _ in currentDate } + let dbQueue = try makeDatabaseQueue() + try dbQueue.write { db in + try db.create(table: "player") { t in + t.autoIncrementedPrimaryKey("id") + t.column("creationDate", .datetime).notNull() + t.column("modificationDate", .datetime).notNull() + t.column("name", .text).notNull() + } + } + + currentDate = Date.distantPast + try dbQueue.write { db in + let player = Player(name: "Arthur") + try player.insert(db) + XCTAssertEqual(player.creationDate, .distantPast) + XCTAssertEqual(player.modificationDate, .distantPast) + } + } } // The protocol in RecordTimestamps.md -/// A type that tracks its creation and modification dates. See +/// A record type that tracks its creation and modification dates. See /// -protocol TimestampedRecord { +protocol TimestampedRecord: MutablePersistableRecord { var creationDate: Date? { get set } var modificationDate: Date? { get set } } -extension TimestampedRecord where Self: MutablePersistableRecord { +extension TimestampedRecord { /// By default, `TimestampedRecord` types set `creationDate` and /// `modificationDate` to the transaction date, if they are nil, /// before insertion.