-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathBrowserDB.swift
493 lines (409 loc) · 21 KB
/
BrowserDB.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import XCGLogger
import Deferred
import Shared
public let NotificationDatabaseWasRecreated = Notification.Name("NotificationDatabaseWasRecreated")
private let log = Logger.syncLogger
public typealias Args = [Any?]
protocol Changeable {
func run(_ sql: String, withArgs args: Args?) -> Success
func run(_ commands: [String]) -> Success
func run(_ commands: [(sql: String, args: Args?)]) -> Success
}
protocol Queryable {
func runQuery<T>(_ sql: String, args: Args?, factory: @escaping (SDRow) -> T) -> Deferred<Maybe<Cursor<T>>>
}
public enum DatabaseOpResult {
case success
case failure
case closed
}
open class BrowserDB {
fileprivate let db: SwiftData
fileprivate let files: FileAccessor
fileprivate let filename: String
fileprivate let secretKey: String?
// SQLITE_MAX_VARIABLE_NUMBER = 999 by default. This controls how many ?s can
// appear in a query string.
open static let MaxVariableNumber = 999
// SQLite standard error codes when the DB file is locked, busy or the disk is
// full. These error codes indicate that any issues with writing to the database
// are temporary and we should not wipe out and re-create the database file when
// we encounter them.
enum SQLiteRecoverableError: Int {
case Busy = 5
case Locked = 6
case ReadOnly = 8
case IOErr = 10
case Full = 13
}
public init(filename: String, secretKey: String? = nil, files: FileAccessor) {
log.debug("Initializing BrowserDB: \(filename).")
self.files = files
self.filename = filename
self.secretKey = secretKey
let file = URL(fileURLWithPath: (try! files.getAndEnsureDirectory())).appendingPathComponent(filename).path
self.db = SwiftData(filename: file, key: secretKey, prevKey: nil)
if AppConstants.BuildChannel == .developer && secretKey != nil {
log.debug("Will attempt to use encrypted DB: \(file) with secret = \(secretKey ?? "nil")")
}
}
// Creates the specified database schema in a new database.
fileprivate func createSchema(_ conn: SQLiteDBConnection, schema: Schema) -> Bool {
log.debug("Try create \(schema.name) version \(schema.version)")
if !schema.create(conn) {
// If schema couldn't be created, we'll bail without setting the `PRAGMA user_version`.
log.debug("Creation failed.")
return false
}
if let error = conn.setVersion(schema.version) {
log.error("Unable to update the schema version; \(error.localizedDescription)")
}
return true
}
// Updates the specified database schema in an existing database.
fileprivate func updateSchema(_ conn: SQLiteDBConnection, schema: Schema) -> Bool {
log.debug("Trying to update table '\(schema.name)' from version \(conn.version) to \(schema.version)")
if !schema.update(conn, from: conn.version) {
// If schema couldn't be updated, we'll bail without setting the `PRAGMA user_version`.
log.debug("Updating failed.")
return false
}
if let error = conn.setVersion(schema.version) {
log.error("Unable to update the schema version; \(error.localizedDescription)")
}
return true
}
// Checks if the database schema needs created or updated and acts accordingly.
// Calls to this function will be serialized to prevent race conditions when
// creating or updating the schema.
func prepareSchema(_ schema: Schema) -> DatabaseOpResult {
SentryIntegration.shared.addAttributes(["dbSchema.\(schema.name).version": schema.version])
// Ensure the database has not already been closed before attempting to
// create or update the schema.
guard !db.closed else {
log.info("Database is closed; Skipping schema create or update.")
return .closed
}
// Get the current schema version for the database.
var currentVersion = 0
_ = self.db.withConnection(.readOnly, cb: { connection -> NSError? in
currentVersion = connection.version
return nil
})
// If the current schema version for the database matches the specified
// `Schema` version, no further action is necessary and we can bail out.
// NOTE: This assumes that we always use *ONE* `Schema` per database file
// since SQLite can only track a single value in `PRAGMA user_version`.
if currentVersion == schema.version {
log.debug("Schema \(schema.name) already exists at version \(schema.version). Skipping additional schema preparation.")
return .success
}
// Set an attribute for Sentry to include with any future error/crash
// logs to indicate what schema version we're coming from and going to.
SentryIntegration.shared.addAttributes(["dbUpgrade.\(schema.name).from": currentVersion, "dbUpgrade.\(schema.name).to": schema.version])
// This should not ever happen since the schema version should always be
// increasing whenever a structural change is made in an app update.
guard currentVersion <= schema.version else {
log.error("Schema \(schema.name) cannot be downgraded from version \(currentVersion) to \(schema.version).")
SentryIntegration.shared.sendWithStacktrace(message: "Schema \(schema.name) cannot be downgraded from version \(currentVersion) to \(schema.version).", tag: "BrowserDB", severity: .error)
return .failure
}
log.debug("Schema \(schema.name) needs created or updated from version \(currentVersion) to \(schema.version).")
var success = true
if let error = self.db.transaction({ connection -> Bool in
log.debug("Create or update \(schema.name) version \(schema.version) on \(Thread.current.description).")
// In the event that `prepareSchema()` is called a second time before the schema
// update is complete, we can check if we're *now* up-to-date and bail out early.
if connection.version == schema.version {
success = true
return success
}
// If `PRAGMA user_version` is zero, check if we can safely create the
// database schema from scratch.
if connection.version == 0 {
// Query for the existence of the `tableList` table to determine if we are
// migrating from an older DB version.
let sqliteMasterCursor = connection.executeQueryUnsafe("SELECT COUNT(*) AS number FROM sqlite_master WHERE type = 'table' AND name = 'tableList'", factory: IntFactory, withArgs: [] as Args)
let tableListTableExists = sqliteMasterCursor[0] == 1
sqliteMasterCursor.close()
// If the `tableList` table doesn't exist, we can simply invoke
// `createSchema()` to create a brand new DB from scratch.
if !tableListTableExists {
log.debug("Schema \(schema.name) doesn't exist. Creating.")
success = self.createSchema(connection, schema: schema)
return success
}
}
log.info("Attempting to update schema from version \(currentVersion) to \(schema.version).")
SentryIntegration.shared.send(message: "Attempting to update schema from version \(currentVersion) to \(schema.version).", tag: "BrowserDB", severity: .info)
// If we can't create a brand new schema from scratch, we must
// call `updateSchema()` to go through the update process.
if self.updateSchema(connection, schema: schema) {
log.debug("Updated schema \(schema.name).")
success = true
return success
}
// If we failed to update the schema, we'll drop everything from the DB
// and create everything again from scratch. Assuming our schema upgrade
// code is correct, this *shouldn't* happen. If it does, log it to Sentry.
log.error("Update failed for schema \(schema.name) from version \(currentVersion) to \(schema.version). Dropping and re-creating.")
SentryIntegration.shared.sendWithStacktrace(message: "Update failed for schema \(schema.name) from version \(currentVersion) to \(schema.version). Dropping and re-creating.", tag: "BrowserDB", severity: .error)
let _ = schema.drop(connection)
success = self.createSchema(connection, schema: schema)
return success
}) {
guard !db.closed else {
log.info("Database is closed; Skipping schema create or update.")
return .closed
}
// If we got an error, move the file and try again. This will probably break things that are
// already attached and expecting a working DB, but at least we should be able to restart.
log.error("Unable to get a transaction: \(error.localizedDescription)")
SentryIntegration.shared.sendWithStacktrace(message: "Unable to get a transaction: \(error.localizedDescription)", tag: "BrowserDB", severity: .error)
// Check if the error we got is recoverable (e.g. SQLITE_BUSY, SQLITE_LOCK, SQLITE_FULL).
// If so, we *shouldn't* move the database file to a backup location and re-create it.
// Instead, just crash so that we don't lose any data.
if let _ = SQLiteRecoverableError.init(rawValue: error.code) {
fatalError(error.localizedDescription)
}
success = false
}
guard success else {
return moveDatabaseToBackupLocation(schema)
}
return .success
}
func moveDatabaseToBackupLocation(_ schema: Schema) -> DatabaseOpResult {
// Make sure that we don't still have open the files that we want to move!
// Note that we use sqlite3_close_v2, which might actually _not_ close the
// database file yet. For this reason we move the -shm and -wal files, too.
db.forceClose()
// Attempt to make a backup as long as the DB file still exists
if self.files.exists(self.filename) {
log.warning("Couldn't create or update schema \(schema.name). Attempting to move \(self.filename) to another location.")
SentryIntegration.shared.sendWithStacktrace(message: "Couldn't create or update schema \(schema.name). Attempting to move \(self.filename) to another location.", tag: "BrowserDB", severity: .warning)
// Note that a backup file might already exist! We append a counter to avoid this.
var bakCounter = 0
var bak: String
repeat {
bakCounter += 1
bak = "\(self.filename).bak.\(bakCounter)"
} while self.files.exists(bak)
do {
try self.files.move(self.filename, toRelativePath: bak)
let shm = self.filename + "-shm"
let wal = self.filename + "-wal"
log.debug("Moving \(shm) and \(wal)…")
if self.files.exists(shm) {
log.debug("\(shm) exists.")
try self.files.move(shm, toRelativePath: bak + "-shm")
}
if self.files.exists(wal) {
log.debug("\(wal) exists.")
try self.files.move(wal, toRelativePath: bak + "-wal")
}
log.debug("Finished moving \(self.filename) successfully.")
} catch let error as NSError {
log.error("Unable to move \(self.filename) to another location. \(error)")
SentryIntegration.shared.sendWithStacktrace(message: "Unable to move \(self.filename) to another location. \(error)", tag: "BrowserDB", severity: .error)
}
} else {
// No backup was attempted since the DB file did not exist
log.error("The DB \(self.filename) has been deleted while previously in use.")
SentryIntegration.shared.sendWithStacktrace(message: "The DB \(self.filename) has been deleted while previously in use.", tag: "BrowserDB", severity: .info)
}
// Re-open the connection to the new database file.
self.reopenIfClosed()
// Notify the world that we moved the database after the schema has been
// created. This allows us to reset Sync and start over in the case of
// corruption.
defer {
NotificationCenter.default.post(name: NotificationDatabaseWasRecreated, object: self.filename)
}
var success = true
// Attempt to re-create the schema in the new database file.
if let error = self.db.transaction({ connection -> Bool in
success = self.createSchema(connection, schema: schema)
return success
}) {
log.error("Unable to get a transaction while re-creating the database: \(error.localizedDescription)")
SentryIntegration.shared.sendWithStacktrace(message: "Unable to get a transaction while re-creating the database: \(error.localizedDescription)", tag: "BrowserDB", severity: .error)
return .failure
}
return success ? .success : .failure
}
func withConnection<T>(flags: SwiftData.Flags, err: inout NSError?, callback: @escaping (_ connection: SQLiteDBConnection, _ err: inout NSError?) -> T) -> T {
var res: T!
err = db.withConnection(flags) { connection in
// An error may occur if the internet connection is dropped.
var err: NSError? = nil
res = callback(connection, &err)
return err
}
return res
}
func withConnection<T>(_ err: inout NSError?, callback: @escaping (_ connection: SQLiteDBConnection, _ err: inout NSError?) -> T) -> T {
/*
* Opening a WAL-using database with a hot journal cannot complete in read-only mode.
* The supported mechanism for a read-only query against a WAL-using SQLite database is to use PRAGMA query_only,
* but this isn't all that useful for us, because we have a mixed read/write workload.
*/
return withConnection(flags: SwiftData.Flags.readWriteCreate, err: &err, callback: callback)
}
func transaction(_ err: inout NSError?, callback: @escaping (_ connection: SQLiteDBConnection, _ err: inout NSError?) -> Bool) -> NSError? {
return self.transaction(synchronous: true, err: &err, callback: callback)
}
func transaction(synchronous: Bool=true, err: inout NSError?, callback: @escaping (_ connection: SQLiteDBConnection, _ err: inout NSError?) -> Bool) -> NSError? {
return db.transaction(synchronous: synchronous) { connection in
var err: NSError? = nil
return callback(connection, &err)
}
}
}
extension BrowserDB {
func vacuum() {
log.debug("Vacuuming a BrowserDB.")
_ = db.withConnection(SwiftData.Flags.readWriteCreate, synchronous: true) { connection in
return connection.vacuum()
}
}
func checkpoint() {
log.debug("Checkpointing a BrowserDB.")
_ = db.transaction(synchronous: true) { connection in
connection.checkpoint()
return true
}
}
}
extension BrowserDB {
public class func varlist(_ count: Int) -> String {
return "(" + Array(repeating: "?", count: count).joined(separator: ", ") + ")"
}
enum InsertOperation: String {
case Insert = "INSERT"
case Replace = "REPLACE"
case InsertOrIgnore = "INSERT OR IGNORE"
case InsertOrReplace = "INSERT OR REPLACE"
case InsertOrRollback = "INSERT OR ROLLBACK"
case InsertOrAbort = "INSERT OR ABORT"
case InsertOrFail = "INSERT OR FAIL"
}
/**
* Insert multiple sets of values into the given table.
*
* Assumptions:
* 1. The table exists and contains the provided columns.
* 2. Every item in `values` is the same length.
* 3. That length is the same as the length of `columns`.
* 4. Every value in each element of `values` is non-nil.
*
* If there are too many items to insert, multiple individual queries will run
* in sequence.
*
* A failure anywhere in the sequence will cause immediate return of failure, but
* will not roll back — use a transaction if you need one.
*/
func bulkInsert(_ table: String, op: InsertOperation, columns: [String], values: [Args]) -> Success {
// Note that there's a limit to how many ?s can be in a single query!
// So here we execute 999 / (columns * rows) insertions per query.
// Note that we can't use variables for the column names, so those don't affect the count.
if values.isEmpty {
log.debug("No values to insert.")
return succeed()
}
let variablesPerRow = columns.count
// Sanity check.
assert(values[0].count == variablesPerRow)
let cols = columns.joined(separator: ", ")
let queryStart = "\(op.rawValue) INTO \(table) (\(cols)) VALUES "
let varString = BrowserDB.varlist(variablesPerRow)
let insertChunk: ([Args]) -> Success = { vals -> Success in
let valuesString = Array(repeating: varString, count: vals.count).joined(separator: ", ")
let args: Args = vals.flatMap { $0 }
return self.run(queryStart + valuesString, withArgs: args)
}
let rowCount = values.count
if (variablesPerRow * rowCount) < BrowserDB.MaxVariableNumber {
return insertChunk(values)
}
log.debug("Splitting bulk insert across multiple runs. I hope you started a transaction!")
let rowsPerInsert = (999 / variablesPerRow)
let chunks = chunk(values, by: rowsPerInsert)
log.debug("Inserting in \(chunks.count) chunks.")
// There's no real reason why we can't pass the ArraySlice here, except that I don't
// want to keep fighting Swift.
return walk(chunks, f: { insertChunk(Array($0)) })
}
func runWithConnection<T>(_ block: @escaping (_ connection: SQLiteDBConnection, _ err: inout NSError?) -> T) -> Deferred<Maybe<T>> {
return DeferredDBOperation(db: self.db, block: block).start()
}
func write(_ sql: String, withArgs args: Args? = nil) -> Deferred<Maybe<Int>> {
return self.runWithConnection() { (connection, err) -> Int in
err = connection.executeChange(sql, withArgs: args)
if err == nil {
let modified = connection.numberOfRowsModified
log.debug("Modified rows: \(modified).")
return modified
}
return 0
}
}
public func forceClose() {
db.forceClose()
}
public func reopenIfClosed() {
db.reopenIfClosed()
}
}
extension BrowserDB: Changeable {
func run(_ sql: String, withArgs args: Args? = nil) -> Success {
return run([(sql, args)])
}
func run(_ commands: [String]) -> Success {
return self.run(commands.map { (sql: $0, args: nil) })
}
/**
* Runs an array of SQL commands. Note: These will all run in order in a transaction and will block
* the caller's thread until they've finished. If any of them fail the operation will abort (no more
* commands will be run) and the transaction will roll back, returning a DatabaseError.
*/
func run(_ commands: [(sql: String, args: Args?)]) -> Success {
if commands.isEmpty {
return succeed()
}
var err: NSError? = nil
let errorResult = self.transaction(&err) { (conn, err) -> Bool in
for (sql, args) in commands {
err = conn.executeChange(sql, withArgs: args)
if let err = err {
log.warning("SQL operation failed: \(err.localizedDescription)")
return false
}
}
return true
}
if let err = err ?? errorResult {
return deferMaybe(DatabaseError(err: err))
}
return succeed()
}
}
extension BrowserDB: Queryable {
func runQuery<T>(_ sql: String, args: Args?, factory: @escaping (SDRow) -> T) -> Deferred<Maybe<Cursor<T>>> {
return runWithConnection { (connection, _) -> Cursor<T> in
return connection.executeQuery(sql, factory: factory, withArgs: args)
}
}
func queryReturnsResults(_ sql: String, args: Args? = nil) -> Deferred<Maybe<Bool>> {
return self.runQuery(sql, args: args, factory: { _ in true })
>>== { deferMaybe($0[0] ?? false) }
}
func queryReturnsNoResults(_ sql: String, args: Args? = nil) -> Deferred<Maybe<Bool>> {
return self.runQuery(sql, args: nil, factory: { _ in false })
>>== { deferMaybe($0[0] ?? true) }
}
}