Skip to content

Commit

Permalink
Merge pull request #442 from groue/feature/reindex
Browse files Browse the repository at this point in the history
Reindex
  • Loading branch information
groue authored Nov 17, 2018
2 parents f80f59c + 55c8d35 commit 8d18b06
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
36 changes: 36 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -16,6 +24,34 @@ 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
}

struct ValueObservation<Reducer> {
+ static func tracking(
+ _ regions: [DatabaseRegionConvertible],
+ reducer: Reducer)
+ -> ValueObservation
}

extension ValueObservation where Reducer == Void {
+ static func tracking<Value>(
+ _ regions: [DatabaseRegionConvertible],
+ fetch: @escaping (Database) throws -> Value)
+ -> ValueObservation<ValueReducers.Raw<Value>>
+ static func tracking<Value>(
+ _ regions: [DatabaseRegionConvertible],
+ fetchDistinct fetch: @escaping (Database) throws -> Value)
+ -> ValueObservation<ValueReducers.Distinct<Value>>
+ where Value: Equatable
}
```


## 3.5.0

Expand Down
24 changes: 24 additions & 0 deletions GRDB/QueryInterface/TableDefinition.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions Tests/GRDBTests/TableDefinitionTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
}

0 comments on commit 8d18b06

Please sign in to comment.