6.2 KiB
Migrating From GRDB 5 to GRDB 6
This guide aims at helping you upgrading your applications from GRDB 5 to GRDB 6.
- Preparing the Migration to GRDB 6
- New requirements
- Primary Associated Types
- Record Changes
- Other Changes
Preparing the Migration to GRDB 6
If you haven't made it yet, upgrade to the latest GRDB 5 release first, and fix any deprecation warning prior to the GRDB 6 upgrade.
You can then upgrade to GRDB 6. Due to the breaking changes, it is possible that your application code no longer compiles. Follow the fix-its that suggest simple syntactic changes. Other modifications that you need to apply are described below.
New requirements
GRDB requirements have been bumped:
- Swift 5.7+ (was Swift 5.3+)
- Xcode 14.0+ (was Xcode 12.0+)
- iOS 11.0+ (unchanged)
- macOS 10.13+ (was macOS 10.10+)
- tvOS 11.0+ (was tvOS 11.0+)
- watchOS 4.0+ (was watchOS 2.0+)
- SQLite 3.19.3+ (was SQLite 3.8.5+)
Primary Associated Types
Request protocols now come with a primary associated type, enabled by SE-0346. This is a great opportunity to simplify your extensions:
-extension DerivableRequest where RowDecoder == Book {
+extension DerivableRequest<Book> {
/// Order books by title, in a localized case-insensitive fashion
func orderByTitle() -> Self {
order(Column("title").collating(.localizedCaseInsensitiveCompare))
}
}
Your extensions to QueryInterfaceRequest can be streamlined as well, thanks to SE-0361:
-extension QueryInterfaceRequest where RowDecoder == Player {
+extension QueryInterfaceRequest<Player> {
func selectID() -> QueryInterfaceRequest<Int64> {
selectPrimaryKey()
}
}
The Cursor protocol has also gained a primary associated type (the type of its elements).
Record Changes
The record protocols have been refactored. We tried to keep the amount of modifications to your existing code as small as possible, but some changes could not be avoided.
-
The
FetchableRecord.init(row:)initializer can now throw errors.-let player = Player(row: row) +let player = try Player(row: row)Decodable records that derive their
FetchableRecordimplementation from the standardDecodableprotocol now throw errors when they find unexpected database values (they used to crash in GRDB 5).If you subclass the
Recordtype, you have to update your override ofinit(row:):class Player: Record { - required init(row: Row) { + required init(row: Row) throws { self.id = row["id"] self.name = row["name"] - super.init(row: row) + try super.init(row: row) } }In record types that do not derive their
FetchableRecord.init(row:)implementation from the standardDecodableprotocol, you are responsible for throwing decoding errors, as in the sample code below:Handling untrusted input
For example:
struct LogEntry: FetchableRecord { var date: Date init(row: Row) throws { let dbValue: DatabaseValue = row["date"] if dbValue.isNull { // Handle NULL throw ... } else if let date = Date.fromDatabaseValue(dbValue) { self.date = date } else { // Handle invalid date throw ... } } }
Other Changes
-
The initializer of in-memory databases can now throw errors:
-let dbQueue = DatabaseQueue() +let dbQueue = try DatabaseQueue() -
The
selectID()method is removed. You can provide your own implementation, based on the newselectPrimaryKey(as:)method:extension QueryInterfaceRequest<Player> { func selectID() -> QueryInterfaceRequest<Int64> { selectPrimaryKey() } } -
Cursor.isEmptyis now a throwing property, instead of a method:-if try cursor.isEmpty() { ... } +if try cursor.isEmpty { ... } -
The
Record.copy()method was removed, without replacement. -
The
DerivableRequest.limit(_:offset_:)method was removed, without replacement.You can still limit
QueryInterfaceRequest, but associations can no longer be limited:// Still OK: a limited request of authors let request = Author.limit(10) // Still OK: a limited request of books let request = author.request(for: Author.books).limit(10) // No longer possible: including a limited association let request = Author.including(all: Author.books.limit(10)) -
DatabaseRegionObservation.start(in:onError:onChange:)now returns a cancellable.let observation = DatabaseRegionObservation.tracking(Player.all()) // GRDB 5 do { let observer = try observation.start(in: dbQueue) { db in print("Players were modified") } } catch { // handle error } // GRDB 6 let cancellable = observation.start( in: dbQueue, onError: { error in /* handle error */ }, onChange: { db in print("Players were modified") })The
DatabaseRegionObservation.extentproperty was removed. You now control the duration of the observation with the cancellable returned from thestartmethod. -
Database cursors no longer have a
statementproperty. When you want information about the database statement used by a cursor, use dedicated cursor properties. For example:-let sql = cursor.statement.sql -let columns = cursor.statement.columnNames +let sql = cursor.sql +let columns = cursor.columnNames