From 1e80aecfbdad9612669997304bc547a83f5896f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 10 Nov 2019 12:34:33 +0100 Subject: [PATCH 1/6] Failing test for batch update policy --- .../MutablePersistableRecordUpdateTests.swift | 31 +++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift b/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift index 650a76563..ec055182b 100644 --- a/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift +++ b/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift @@ -35,8 +35,7 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { } func testRequestUpdateAll() throws { - let dbQueue = try makeDatabaseQueue() - try dbQueue.write { db in + try makeDatabaseQueue().write { db in let assignment = Columns.score <- 0 try Player.updateAll(db, assignment) @@ -282,4 +281,32 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { try XCTAssertEqual(Player.fetchOne(db, key: 1)!.score, 2) } } + + func testPersistenceConflictPolicyAbort() throws { + struct AbortPlayer: PersistableRecord { + static let databaseTableName = "player" + static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .abort, update: .abort) + func encode(to container: inout PersistenceContainer) { } + } + try makeDatabaseQueue().write { db in + try AbortPlayer.updateAll(db, Column("score") <- 0) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE "player" SET "score" = 0 + """) + } + } + + func testPersistenceConflictPolicyIgnore() throws { + struct IgnorePlayer: PersistableRecord { + static let databaseTableName = "player" + static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .abort, update: .ignore) + func encode(to container: inout PersistenceContainer) { } + } + try makeDatabaseQueue().write { db in + try IgnorePlayer.updateAll(db, Column("score") <- 0) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE OR IGNORE "player" SET "score" = 0 + """) + } + } } From 58263e9b60e8bce7a2b8a5e2fe23ec6f7bc50d36 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 10 Nov 2019 12:42:01 +0100 Subject: [PATCH 2/6] Fix failing test for batch update policy --- .../Request/QueryInterfaceRequest.swift | 7 ++++++- .../SQLGeneration/SQLQueryGenerator.swift | 15 +++++++++++++-- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index 1db6cd367..bb828db19 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -507,7 +507,12 @@ extension QueryInterfaceRequest where T: MutablePersistableRecord { /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. @discardableResult public func updateAll(_ db: Database, _ assignments: [ColumnAssignment]) throws -> Int { - guard let updateStatement = try SQLQueryGenerator(query).makeUpdateStatement(db, assignments) else { + let conflictResolution = RowDecoder.persistenceConflictPolicy.conflictResolutionForUpdate + guard let updateStatement = try SQLQueryGenerator(query).makeUpdateStatement( + db, + conflictResolution: conflictResolution, + assignments: assignments) else + { // database not hit return 0 } diff --git a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift index dcf3f9957..789f0ab4b 100644 --- a/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift +++ b/GRDB/QueryInterface/SQLGeneration/SQLQueryGenerator.swift @@ -170,7 +170,12 @@ struct SQLQueryGenerator { } /// Returns nil if assignments is empty - func makeUpdateStatement(_ db: Database, _ assignments: [ColumnAssignment]) throws -> UpdateStatement? { + func makeUpdateStatement( + _ db: Database, + conflictResolution: Database.ConflictResolution, + assignments: [ColumnAssignment]) + throws -> UpdateStatement? + { if let groupExpressions = try groupPromise?.resolve(db), !groupExpressions.isEmpty { // Programmer error fatalError("Can't update query with GROUP BY clause") @@ -197,7 +202,13 @@ struct SQLQueryGenerator { var context = SQLGenerationContext.queryGenerationContext(aliases: relation.allAliases) - var sql = try "UPDATE " + relation.source.sql(db, &context) + var sql = "UPDATE " + + if conflictResolution != .abort { + sql += "OR \(conflictResolution.rawValue) " + } + + sql += try relation.source.sql(db, &context) let assignmentsSQL = assignments .map({ assignment in From e0ea30041f35d6be52792854b319655c3ff34e30 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 10 Nov 2019 13:00:19 +0100 Subject: [PATCH 3/6] Batch update: customization of conflict resolution --- .../Request/QueryInterfaceRequest.swift | 22 +++++-- GRDB/Record/PersistableRecord.swift | 23 +++++-- .../MutablePersistableRecordUpdateTests.swift | 63 ++++++++++++++++++- 3 files changed, 96 insertions(+), 12 deletions(-) diff --git a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift index bb828db19..433276135 100644 --- a/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift +++ b/GRDB/QueryInterface/Request/QueryInterfaceRequest.swift @@ -503,11 +503,18 @@ extension QueryInterfaceRequest where T: MutablePersistableRecord { /// } /// /// - parameter db: A database connection. - /// - returns: The number of updated rows + /// - parameter conflictResolution: A policy for conflict resolution, + /// defaulting to the record's persistenceConflictPolicy. + /// - parameter assignments: An array of column assignments. + /// - returns: The number of updated rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. @discardableResult - public func updateAll(_ db: Database, _ assignments: [ColumnAssignment]) throws -> Int { - let conflictResolution = RowDecoder.persistenceConflictPolicy.conflictResolutionForUpdate + public func updateAll( + _ db: Database, + onConflict conflictResolution: Database.ConflictResolution? = nil, + _ assignments: [ColumnAssignment]) throws -> Int + { + let conflictResolution = conflictResolution ?? RowDecoder.persistenceConflictPolicy.conflictResolutionForUpdate guard let updateStatement = try SQLQueryGenerator(query).makeUpdateStatement( db, conflictResolution: conflictResolution, @@ -530,16 +537,21 @@ extension QueryInterfaceRequest where T: MutablePersistableRecord { /// } /// /// - parameter db: A database connection. - /// - returns: The number of updated rows + /// - parameter conflictResolution: A policy for conflict resolution, + /// defaulting to the record's persistenceConflictPolicy. + /// - parameter assignment: A column assignment. + /// - parameter otherAssignments: Eventual other column assignments. + /// - returns: The number of updated rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. @discardableResult public func updateAll( _ db: Database, + onConflict conflictResolution: Database.ConflictResolution? = nil, _ assignment: ColumnAssignment, _ otherAssignments: ColumnAssignment...) throws -> Int { - return try updateAll(db, [assignment] + otherAssignments) + return try updateAll(db, onConflict: conflictResolution, [assignment] + otherAssignments) } } diff --git a/GRDB/Record/PersistableRecord.swift b/GRDB/Record/PersistableRecord.swift index 87b07372a..56795f70d 100644 --- a/GRDB/Record/PersistableRecord.swift +++ b/GRDB/Record/PersistableRecord.swift @@ -530,11 +530,19 @@ extension MutablePersistableRecord { /// } /// /// - parameter db: A database connection. - /// - returns: The number of updated rows + /// - parameter conflictResolution: A policy for conflict resolution, + /// defaulting to the record's persistenceConflictPolicy. + /// - parameter assignments: An array of column assignments. + /// - returns: The number of updated rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. @discardableResult - public static func updateAll(_ db: Database, _ assignments: [ColumnAssignment]) throws -> Int { - return try all().updateAll(db, assignments) + public static func updateAll( + _ db: Database, + onConflict conflictResolution: Database.ConflictResolution? = nil, + _ assignments: [ColumnAssignment]) + throws -> Int + { + return try all().updateAll(db, onConflict: conflictResolution, assignments) } /// Updates all records; returns the number of updated records. @@ -547,16 +555,21 @@ extension MutablePersistableRecord { /// } /// /// - parameter db: A database connection. - /// - returns: The number of updated rows + /// - parameter conflictResolution: A policy for conflict resolution, + /// defaulting to the record's persistenceConflictPolicy. + /// - parameter assignment: A column assignment. + /// - parameter otherAssignments: Eventual other column assignments. + /// - returns: The number of updated rows. /// - throws: A DatabaseError is thrown whenever an SQLite error occurs. @discardableResult public static func updateAll( _ db: Database, + onConflict conflictResolution: Database.ConflictResolution? = nil, _ assignment: ColumnAssignment, _ otherAssignments: ColumnAssignment...) throws -> Int { - return try updateAll(db, [assignment] + otherAssignments) + return try updateAll(db, onConflict: conflictResolution, [assignment] + otherAssignments) } } diff --git a/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift b/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift index ec055182b..6488a6be8 100644 --- a/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift +++ b/Tests/GRDBTests/MutablePersistableRecordUpdateTests.swift @@ -282,7 +282,7 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { } } - func testPersistenceConflictPolicyAbort() throws { + func testConflictPolicyAbort() throws { struct AbortPlayer: PersistableRecord { static let databaseTableName = "player" static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .abort, update: .abort) @@ -293,10 +293,25 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { XCTAssertEqual(self.lastSQLQuery, """ UPDATE "player" SET "score" = 0 """) + + try AbortPlayer.updateAll(db, [Column("score") <- 0]) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE "player" SET "score" = 0 + """) + + try AbortPlayer.all().updateAll(db, Column("score") <- 0) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE "player" SET "score" = 0 + """) + + try AbortPlayer.all().updateAll(db, [Column("score") <- 0]) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE "player" SET "score" = 0 + """) } } - func testPersistenceConflictPolicyIgnore() throws { + func testConflictPolicyIgnore() throws { struct IgnorePlayer: PersistableRecord { static let databaseTableName = "player" static let persistenceConflictPolicy = PersistenceConflictPolicy(insert: .abort, update: .ignore) @@ -307,6 +322,50 @@ class MutablePersistableRecordUpdateTests: GRDBTestCase { XCTAssertEqual(self.lastSQLQuery, """ UPDATE OR IGNORE "player" SET "score" = 0 """) + + try IgnorePlayer.updateAll(db, [Column("score") <- 0]) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE OR IGNORE "player" SET "score" = 0 + """) + + try IgnorePlayer.all().updateAll(db, Column("score") <- 0) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE OR IGNORE "player" SET "score" = 0 + """) + + try IgnorePlayer.all().updateAll(db, [Column("score") <- 0]) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE OR IGNORE "player" SET "score" = 0 + """) + } + } + + func testConflictPolicyCustom() throws { + try makeDatabaseQueue().write { db in + try Player.updateAll(db, Column("score") <- 0) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE "player" SET "score" = 0 + """) + + try Player.updateAll(db, onConflict: .ignore, Column("score") <- 0) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE OR IGNORE "player" SET "score" = 0 + """) + + try Player.updateAll(db, onConflict: .ignore, [Column("score") <- 0]) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE OR IGNORE "player" SET "score" = 0 + """) + + try Player.all().updateAll(db, onConflict: .ignore, Column("score") <- 0) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE OR IGNORE "player" SET "score" = 0 + """) + + try Player.all().updateAll(db, onConflict: .ignore, [Column("score") <- 0]) + XCTAssertEqual(self.lastSQLQuery, """ + UPDATE OR IGNORE "player" SET "score" = 0 + """) } } } From 630a2c358a298898e80ecad7318bead3b566937c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 10 Nov 2019 13:05:33 +0100 Subject: [PATCH 4/6] Document conflict resolution for batch updates --- README.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/README.md b/README.md index ff2b6a916..21c7df79a 100644 --- a/README.md +++ b/README.md @@ -4659,6 +4659,13 @@ As a convenience, you can also use the `+=`, `-=`, `*=`, or `/=` operators: try Player.updateAll(db, scoreColumn += bonusColumn * 2) ``` +Default [Conflict Resolution] rules apply, and you may also provide a specific one: + +```swift +// UPDATE OR IGNORE player SET ... +try Player.updateAll(db, onConflict: .ignore, /* assignments... */) +``` + > :point_up: **Note** The `updateAll` method is only available for records that adopts the [PersistableRecord] protocol. From e1ff862ffc72509a054713e19b977431a23b2b71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 10 Nov 2019 13:12:42 +0100 Subject: [PATCH 5/6] CHANGELOG --- CHANGELOG.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e0389ba8a..57980cc0c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,9 +6,7 @@ All notable changes to this project will be documented in this file. GRDB adheres to [Semantic Versioning](https://semver.org/), with one expection: APIs flagged [**:fire: EXPERIMENTAL**](README.md#what-are-experimental-features). Those are unstable, and may break between any two minor releases of the library. - #### 4.x Releases @@ -59,9 +57,11 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one expection: - [0.110.0](#01100), ... - + +**Fixed** + +- [#647](https://github.com/groue/GRDB.swift/pull/647): Honor conflict resolution for batch updates ## 4.6.0 From 5b24f598a5ac1b539e263d14a58c41ba6a3dd359 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 10 Nov 2019 17:07:12 +0100 Subject: [PATCH 6/6] v4.6.1 --- CHANGELOG.md | 11 ++++++++++- GRDB.swift.podspec | 2 +- Makefile | 4 ++-- README.md | 6 +++--- Support/Info.plist | 2 +- 5 files changed, 17 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57980cc0c..b26043feb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,12 +6,14 @@ All notable changes to this project will be documented in this file. GRDB adheres to [Semantic Versioning](https://semver.org/), with one expection: APIs flagged [**:fire: EXPERIMENTAL**](README.md#what-are-experimental-features). Those are unstable, and may break between any two minor releases of the library. + #### 4.x Releases -- `4.6.x` Releases - [4.6.0](#460) +- `4.6.x` Releases - [4.6.0](#460) | [4.6.1](#461) - `4.5.x` Releases - [4.5.0](#450) - `4.4.x` Releases - [4.4.0](#440) - `4.3.x` Releases - [4.3.0](#430) @@ -57,7 +59,14 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one expection: - [0.110.0](#01100), ... + + + +## 4.6.1 + +Released November 10, 2019 • [diff](https://github.com/groue/GRDB.swift/compare/v4.6.0...v4.6.1) **Fixed** diff --git a/GRDB.swift.podspec b/GRDB.swift.podspec index e4131f4d7..8172ca7be 100644 --- a/GRDB.swift.podspec +++ b/GRDB.swift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'GRDB.swift' - s.version = '4.6.0' + s.version = '4.6.1' s.license = { :type => 'MIT', :file => 'LICENSE' } s.summary = 'A toolkit for SQLite databases, with a focus on application development.' diff --git a/Makefile b/Makefile index 1ed94b091..910985736 100644 --- a/Makefile +++ b/Makefile @@ -452,8 +452,8 @@ ifdef JAZZY --author 'Gwendal Roué' \ --author_url https://github.com/groue \ --github_url https://github.com/groue/GRDB.swift \ - --github-file-prefix https://github.com/groue/GRDB.swift/tree/v4.6.0 \ - --module-version 4.6.0 \ + --github-file-prefix https://github.com/groue/GRDB.swift/tree/v4.6.1 \ + --module-version 4.6.1 \ --module GRDB \ --root-url http://groue.github.io/GRDB.swift/docs/4.6/ \ --output Documentation/Reference \ diff --git a/README.md b/README.md index 21c7df79a..7ca44155d 100644 --- a/README.md +++ b/README.md @@ -5,14 +5,14 @@ GRDB 4 [![Swift 4.2](https://img.shields.io/badge/swift-4.2-orange.svg?style=fla --- -**Latest release**: November 9, 2019 • version 4.6.0 • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 3 to GRDB 4](Documentation/GRDB3MigrationGuide.md) +**Latest release**: November 10, 2019 • version 4.6.1 • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 3 to GRDB 4](Documentation/GRDB3MigrationGuide.md) **Requirements**: iOS 9.0+ / macOS 10.9+ / tvOS 9.0+ / watchOS 2.0+ • Swift 4.2+ / Xcode 10.0+ | Swift version | GRDB version | | ------------- | ----------------------------------------------------------- | -| **Swift 5** | **v4.6.0** | -| **Swift 4.2** | **v4.6.0** | +| **Swift 5** | **v4.6.1** | +| **Swift 4.2** | **v4.6.1** | | Swift 4.1 | [v3.7.0](https://github.com/groue/GRDB.swift/tree/v3.7.0) | | Swift 4 | [v2.10.0](https://github.com/groue/GRDB.swift/tree/v2.10.0) | | Swift 3.2 | [v1.3.0](https://github.com/groue/GRDB.swift/tree/v1.3.0) | diff --git a/Support/Info.plist b/Support/Info.plist index fa76e3e5b..ee4f7fbd8 100644 --- a/Support/Info.plist +++ b/Support/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 4.6.0 + 4.6.1 CFBundleSignature ???? CFBundleVersion