From 466a1ad4d5b71dab642e40c1b9c5c8aa8da6d455 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 17 Nov 2018 18:56:29 +0100 Subject: [PATCH 1/4] Database.reindex(collation:) --- GRDB/QueryInterface/TableDefinition.swift | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/GRDB/QueryInterface/TableDefinition.swift b/GRDB/QueryInterface/TableDefinition.swift index ca8b231df6..41fefb83ce 100644 --- a/GRDB/QueryInterface/TableDefinition.swift +++ b/GRDB/QueryInterface/TableDefinition.swift @@ -216,6 +216,30 @@ extension Database { public func drop(index name: String) throws { try execute("DROP INDEX \(name.quotedDatabaseIdentifier)") } + + /// Delete and recreate from scratch all indices that use this collation. + /// + /// This method is useful when the definition of a collation sequence + /// has changed. + /// + /// See https://www.sqlite.org/lang_reindex.html + /// + /// - throws: A DatabaseError whenever an SQLite error occurs. + public func reindex(collation: Database.CollationName) throws { + try execute("REINDEX \(collation.rawValue)") + } + + /// Delete and recreate from scratch all indices that use this collation. + /// + /// This method is useful when the definition of a collation sequence + /// has changed. + /// + /// See https://www.sqlite.org/lang_reindex.html + /// + /// - throws: A DatabaseError whenever an SQLite error occurs. + public func reindex(collation: DatabaseCollation) throws { + try reindex(collation: Database.CollationName(collation.name)) + } } /// The TableDefinition class lets you define table columns and constraints. From 53adad16e50b48575e8292008b7386c582a20aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 17 Nov 2018 18:56:38 +0100 Subject: [PATCH 2/4] Tests for Database.reindex(collation:) --- Tests/GRDBTests/TableDefinitionTests.swift | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/Tests/GRDBTests/TableDefinitionTests.swift b/Tests/GRDBTests/TableDefinitionTests.swift index b25ec0aa21..b85dda3f1a 100644 --- a/Tests/GRDBTests/TableDefinitionTests.swift +++ b/Tests/GRDBTests/TableDefinitionTests.swift @@ -573,4 +573,15 @@ class TableDefinitionTests: GRDBTestCase { XCTAssertTrue(try db.indexes(on: "test").isEmpty) } } + + func testReindex() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.reindex(collation: .binary) + assertEqualSQL(lastSQLQuery, "REINDEX BINARY") + + try db.reindex(collation: .localizedCompare) + assertEqualSQL(lastSQLQuery, "REINDEX swiftLocalizedCompare") + } + } } From 064f1005e5372ea7989fae70fbdb82ad66cdfdff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 17 Nov 2018 19:12:20 +0100 Subject: [PATCH 3/4] CHANGELOG --- CHANGELOG.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index f57e685e3a..135c6e435d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,14 @@ Release Notes ### New +- [#442](https://github.com/groue/GRDB.swift/pull/442): Reindex + + ```swift + // Deletes and recreates from scratch all indices that use this + // locale-dependent collation. + try db.reindex(collation: .localizedCompare) + ``` + - ValueObservation has three new factory methods that accept an array of database regions, and complete the existing variadic methods (addresses [#441](https://github.com/groue/GRDB.swift/issues/441)): ```swift @@ -16,6 +24,15 @@ Release Notes - ValueReducer, the protocol that fuels ValueObservation, is flagged [**:fire: EXPERIMENTAL**](README.md#what-are-experimental-features). It will remain so until more experience has been acquired. +### API diff + +```diff + extension Database { ++ func reindex(collation: Database.CollationName) throws ++ func reindex(collation: DatabaseCollation) throws + } +``` + ## 3.5.0 From 55c8d350b83de0bd164f6e35550fbc0792a67b71 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 17 Nov 2018 19:17:25 +0100 Subject: [PATCH 4/4] CHANGELOG --- CHANGELOG.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 135c6e435d..b3168af774 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -31,6 +31,25 @@ Release Notes + func reindex(collation: Database.CollationName) throws + func reindex(collation: DatabaseCollation) throws } + + struct ValueObservation { ++ static func tracking( ++ _ regions: [DatabaseRegionConvertible], ++ reducer: Reducer) ++ -> ValueObservation + } + + extension ValueObservation where Reducer == Void { ++ static func tracking( ++ _ regions: [DatabaseRegionConvertible], ++ fetch: @escaping (Database) throws -> Value) ++ -> ValueObservation> ++ static func tracking( ++ _ regions: [DatabaseRegionConvertible], ++ fetchDistinct fetch: @escaping (Database) throws -> Value) ++ -> ValueObservation> ++ where Value: Equatable + } ```