diff --git a/GRDB/Documentation.docc/RecordTimestamps.md b/GRDB/Documentation.docc/RecordTimestamps.md index 6de026d6a..e5b714b69 100644 --- a/GRDB/Documentation.docc/RecordTimestamps.md +++ b/GRDB/Documentation.docc/RecordTimestamps.md @@ -137,25 +137,27 @@ This section provides a sample protocol for records that track their creation an You can copy it in your application. Make sure it fits the needs of your application! Not all apps have the same needs regarding timestamps. Perform adaptations when needed. ```swift -/// A type that tracks its creation and modification dates, as described in +/// A type that tracks its creation and modification dates. See /// protocol TimestampedRecord { var creationDate: Date? { get set } var modificationDate: Date? { get set } } -extension TimestampedRecord { - /// Sets `modificationDate` to the transaction date, and `creationDate` if - /// not set yet. - mutating func touch(_ db: Database) throws { - if creationDate == nil { - creationDate = try db.transactionDate - } - modificationDate = try db.transactionDate +extension TimestampedRecord where Self: MutablePersistableRecord { + /// By default, `TimestampedRecord` types set `creationDate` and + /// `modificationDate` to the transaction date, if they are nil, + /// before insertion. + /// + /// `TimestampedRecord` types that customize the `willInsert` + /// persistence callback should call `initializeTimestamps` from + /// their implementation. + mutating func willInsert(_ db: Database) throws { + try initializeTimestamps(db) } - /// Sets both `creationDate` and `modificationDate` to the transaction date, - /// if they are not set yet. + /// Sets `creationDate` and `modificationDate` to the transaction date, + /// if they are nil. mutating func initializeTimestamps(_ db: Database) throws { if creationDate == nil { creationDate = try db.transactionDate @@ -164,23 +166,27 @@ extension TimestampedRecord { modificationDate = try db.transactionDate } } -} - -extension TimestampedRecord where Self: MutablePersistableRecord { - /// By default, TimestampedRecord types initialize their timestamps - /// before insertion. + + /// Sets `modificationDate`, and executes an `UPDATE` statement + /// on all columns. /// - /// Records that customize `willInsert` should call - /// `initializeTimestamps` from their implementation. - mutating func willInsert(_ db: Database) throws { - try initializeTimestamps(db) + /// - parameter date: The modification date. If nil, the + /// transaction date is used. + mutating func updateWithTimestamp(_ db: Database, modificationDate: Date? = nil) throws { + self.modificationDate = try modificationDate ?? db.transactionDate + try update(db) } - /// Sets `modificationDate` to the transaction date, and executes an - /// `UPDATE` statement on all columns. - mutating func updateWithTimestamp(_ db: Database) throws { - try touch(db) - try update(db) + /// Sets `modificationDate`, and executes an `UPDATE` statement that + /// updates the `modificationDate` column, if and only if the record + /// was modified. + /// + /// - parameter date: The modification date. If nil, the + /// transaction date is used. + mutating func touch(_ db: Database, modificationDate: Date? = nil) throws { + try updateChanges(db) { + $0.modificationDate = try modificationDate ?? db.transactionDate + } } } ``` @@ -196,16 +202,20 @@ extension Player: Codable, MutablePersistableRecord, FetchableRecord, Timestampe } try dbQueue.write { db in - // Insertion sets the creation and modification dates. + // An inserted record has both a creation and a modification date. var player = Player(name: "Arthur", score: 1000) try player.insert(db) assert(player.creationDate != nil) assert(player.modificationDate != nil) - // Call updateWithTimestamp() instead of update() in order - // to bump the modification date. + // updateWithTimestamp() bumps the modification date and updates + // the record in the database. player.score += 1 try player.updateWithTimestamp(db) + + // touch() updates the modification date only in the database. + try player.touch(db) + try player.touch(db, date: Date()) } ``` diff --git a/Tests/GRDBTests/TransactionDateTests.swift b/Tests/GRDBTests/TransactionDateTests.swift index a82f0cfab..3f1ed19b1 100644 --- a/Tests/GRDBTests/TransactionDateTests.swift +++ b/Tests/GRDBTests/TransactionDateTests.swift @@ -143,10 +143,54 @@ class TransactionDateTests: GRDBTestCase { currentDate = newTransactionDate try dbQueue.write { db in var player = try Player.fetchOne(db, key: 1)! + player.name = "Barbara" try player.updateWithTimestamp(db) XCTAssertEqual(player.creationDate, .distantPast) XCTAssertEqual(player.modificationDate, newTransactionDate) + + try player.updateWithTimestamp(db, modificationDate: .distantFuture) + XCTAssertEqual(player.creationDate, .distantPast) + XCTAssertEqual(player.modificationDate, .distantFuture) + } + } + + func test_TimestampedRecord_touch() throws { + struct Player: Codable, MutablePersistableRecord, FetchableRecord, TimestampedRecord { + var id: Int64? + var creationDate: Date? + var modificationDate: Date? + var name: String + + mutating 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() + } + + var player = Player(name: "Arthur") + try player.insert(db) + } + + let newTransactionDate = Date() + currentDate = newTransactionDate + try dbQueue.write { db in + var player = try Player.fetchOne(db, key: 1)! + try player.touch(db) + XCTAssertEqual(player.modificationDate, newTransactionDate) + + try player.touch(db, modificationDate: .distantFuture) + XCTAssertEqual(player.modificationDate, .distantFuture) } } @@ -195,40 +239,32 @@ class TransactionDateTests: GRDBTestCase { XCTAssertEqual(player.creationDate, .distantPast) XCTAssertEqual(player.modificationDate, .distantPast) } - - let newTransactionDate = Date() - currentDate = newTransactionDate - try dbQueue.write { db in - var player = try Player.fetchOne(db, key: 1)! - player.name = "Barbara" - try player.updateWithTimestamp(db) - XCTAssertEqual(player.creationDate, .distantPast) - XCTAssertEqual(player.modificationDate, newTransactionDate) - } } } // The protocol in RecordTimestamps.md -/// A type that tracks its creation and modification dates, as described in +/// A type that tracks its creation and modification dates. See /// protocol TimestampedRecord { var creationDate: Date? { get set } var modificationDate: Date? { get set } } -extension TimestampedRecord { - /// Sets `modificationDate` to the transaction date, and `creationDate` if - /// not set yet. - mutating func touch(_ db: Database) throws { - if creationDate == nil { - creationDate = try db.transactionDate - } - modificationDate = try db.transactionDate +extension TimestampedRecord where Self: MutablePersistableRecord { + /// By default, `TimestampedRecord` types set `creationDate` and + /// `modificationDate` to the transaction date, if they are nil, + /// before insertion. + /// + /// `TimestampedRecord` types that customize the `willInsert` + /// persistence callback should call `initializeTimestamps` from + /// their implementation. + mutating func willInsert(_ db: Database) throws { + try initializeTimestamps(db) } - /// Sets both `creationDate` and `modificationDate` to the transaction date, - /// if they are not set yet. + /// Sets `creationDate` and `modificationDate` to the transaction date, + /// if they are nil. mutating func initializeTimestamps(_ db: Database) throws { if creationDate == nil { creationDate = try db.transactionDate @@ -237,22 +273,26 @@ extension TimestampedRecord { modificationDate = try db.transactionDate } } -} - -extension TimestampedRecord where Self: MutablePersistableRecord { - /// By default, TimestampedRecord types initialize their timestamps - /// before insertion. - /// - /// Records that customize `willInsert` should call - /// `initializeTimestamps` from their implementation. - mutating func willInsert(_ db: Database) throws { - try initializeTimestamps(db) - } - /// Sets `modificationDate` to the transaction date, and executes an - /// `UPDATE` statement on all columns. - mutating func updateWithTimestamp(_ db: Database) throws { - try touch(db) + /// Sets `modificationDate`, and executes an `UPDATE` statement + /// on all columns. + /// + /// - parameter date: The modification date. If nil, the + /// transaction date is used. + mutating func updateWithTimestamp(_ db: Database, modificationDate: Date? = nil) throws { + self.modificationDate = try modificationDate ?? db.transactionDate try update(db) } + + /// Sets `modificationDate`, and executes an `UPDATE` statement that + /// updates the `modificationDate` column, if and only if the record + /// was modified. + /// + /// - parameter date: The modification date. If nil, the + /// transaction date is used. + mutating func touch(_ db: Database, modificationDate: Date? = nil) throws { + try updateChanges(db) { + $0.modificationDate = try modificationDate ?? db.transactionDate + } + } }