mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-28 04:24:20 +00:00
Expose Database.erase()
This commit is contained in:
1 parent
11f8807545
commit
ce0df653f4
4 files changed
+68
-58
No files matched your search
@@ -1080,16 +1080,53 @@ public final class Database {
|
||||
publicStatementCache.clear()
|
||||
}
|
||||
|
||||
// MARK: - Erasing
|
||||
|
||||
func erase() throws {
|
||||
#if SQLITE_HAS_CODEC
|
||||
// SQLCipher does not support the backup API:
|
||||
// https://discuss.zetetic.net/t/using-the-sqlite-online-backup-api/2631
|
||||
// So we'll drop all database objects one after the other.
|
||||
|
||||
// Prevent foreign keys from messing with drop table statements
|
||||
let foreignKeysEnabled = try Bool.fetchOne(self, sql: "PRAGMA foreign_keys")!
|
||||
if foreignKeysEnabled {
|
||||
try execute(sql: "PRAGMA foreign_keys = OFF")
|
||||
}
|
||||
|
||||
try throwingFirstError(
|
||||
execute: {
|
||||
// Remove all database objects, one after the other
|
||||
try inTransaction {
|
||||
let sql = "SELECT type, name FROM sqlite_master WHERE name NOT LIKE 'sqlite_%'"
|
||||
while let row = try Row.fetchOne(self, sql: sql) {
|
||||
let type: String = row["type"]
|
||||
let name: String = row["name"]
|
||||
try execute(sql: "DROP \(type) \(name.quotedDatabaseIdentifier)")
|
||||
}
|
||||
return .commit
|
||||
}
|
||||
},
|
||||
finally: {
|
||||
// Restore foreign keys if needed
|
||||
if foreignKeysEnabled {
|
||||
try execute(sql: "PRAGMA foreign_keys = ON")
|
||||
}
|
||||
})
|
||||
#else
|
||||
try DatabaseQueue().backup(to: self)
|
||||
#endif
|
||||
}
|
||||
|
||||
// MARK: - Backup
|
||||
|
||||
static func backup(
|
||||
from dbFrom: Database,
|
||||
func backup(
|
||||
to dbDest: Database,
|
||||
afterBackupInit: (() -> Void)? = nil,
|
||||
afterBackupStep: (() -> Void)? = nil)
|
||||
throws
|
||||
{
|
||||
guard let backup = sqlite3_backup_init(dbDest.sqliteConnection, "main", dbFrom.sqliteConnection, "main") else {
|
||||
guard let backup = sqlite3_backup_init(dbDest.sqliteConnection, "main", sqliteConnection, "main") else {
|
||||
throw DatabaseError(resultCode: dbDest.lastErrorCode, message: dbDest.lastErrorMessage)
|
||||
}
|
||||
guard Int(bitPattern: backup) != Int(SQLITE_ERROR) else {
|
||||
|
||||
@@ -278,18 +278,22 @@ extension DatabaseReader {
|
||||
/// the backup. Those writes may, or may not, be reflected in the backup,
|
||||
/// but they won't trigger any error.
|
||||
public func backup(to writer: DatabaseWriter) throws {
|
||||
try backup(to: writer, afterBackupInit: nil, afterBackupStep: nil)
|
||||
try writer.writeWithoutTransaction { dbDest in
|
||||
try backup(to: dbDest)
|
||||
}
|
||||
}
|
||||
|
||||
func backup(to writer: DatabaseWriter, afterBackupInit: (() -> Void)?, afterBackupStep: (() -> Void)?) throws {
|
||||
func backup(
|
||||
to dbDest: Database,
|
||||
afterBackupInit: (() -> Void)? = nil,
|
||||
afterBackupStep: (() -> Void)? = nil)
|
||||
throws
|
||||
{
|
||||
try read { dbFrom in
|
||||
try writer.writeWithoutTransaction { dbDest in
|
||||
try Database.backup(
|
||||
from: dbFrom,
|
||||
to: dbDest,
|
||||
afterBackupInit: afterBackupInit,
|
||||
afterBackupStep: afterBackupStep)
|
||||
}
|
||||
try dbFrom.backup(
|
||||
to: dbDest,
|
||||
afterBackupInit: afterBackupInit,
|
||||
afterBackupStep: afterBackupStep)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -291,40 +291,7 @@ extension DatabaseWriter {
|
||||
/// - precondition: database is not accessed concurrently during the
|
||||
/// execution of this method.
|
||||
public func erase() throws {
|
||||
#if SQLITE_HAS_CODEC
|
||||
// SQLCipher does not support the backup API:
|
||||
// https://discuss.zetetic.net/t/using-the-sqlite-online-backup-api/2631
|
||||
// So we'll drop all database objects one after the other.
|
||||
try writeWithoutTransaction { db in
|
||||
// Prevent foreign keys from messing with drop table statements
|
||||
let foreignKeysEnabled = try Bool.fetchOne(db, sql: "PRAGMA foreign_keys")!
|
||||
if foreignKeysEnabled {
|
||||
try db.execute(sql: "PRAGMA foreign_keys = OFF")
|
||||
}
|
||||
|
||||
try throwingFirstError(
|
||||
execute: {
|
||||
// Remove all database objects, one after the other
|
||||
try db.inTransaction {
|
||||
let sql = "SELECT type, name FROM sqlite_master WHERE name NOT LIKE 'sqlite_%'"
|
||||
while let row = try Row.fetchOne(db, sql: sql) {
|
||||
let type: String = row["type"]
|
||||
let name: String = row["name"]
|
||||
try db.execute(sql: "DROP \(type) \(name.quotedDatabaseIdentifier)")
|
||||
}
|
||||
return .commit
|
||||
}
|
||||
},
|
||||
finally: {
|
||||
// Restore foreign keys if needed
|
||||
if foreignKeysEnabled {
|
||||
try db.execute(sql: "PRAGMA foreign_keys = ON")
|
||||
}
|
||||
})
|
||||
}
|
||||
#else
|
||||
try DatabaseQueue().backup(to: self)
|
||||
#endif
|
||||
try writeWithoutTransaction { try $0.erase() }
|
||||
}
|
||||
|
||||
// MARK: - Claiming Disk Space
|
||||
|
||||
@@ -35,7 +35,7 @@ class DatabasePoolBackupTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: this test is fragile: understand if somethig is wrong, or not:
|
||||
// TODO: fix flaky test
|
||||
// @available(OSX 10.10, *)
|
||||
// func testConcurrentWriteDuringBackup() throws {
|
||||
// let source = try makeDatabasePool(filename: "source.sqlite")
|
||||
@@ -58,17 +58,19 @@ class DatabasePoolBackupTests: GRDBTestCase {
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// try source.backup(
|
||||
// to: destination,
|
||||
// afterBackupInit: {
|
||||
// s1.signal()
|
||||
// _ = s2.wait(timeout: .distantFuture)
|
||||
// },
|
||||
// afterBackupStep: {
|
||||
// try! source.write { db in
|
||||
// try db.execute(sql: "INSERT INTO items (id) VALUES (NULL)")
|
||||
// }
|
||||
// })
|
||||
// try destination.writeWithoutTransaction { dbDestination in
|
||||
// try source.backup(
|
||||
// to: dbDestination,
|
||||
// afterBackupInit: {
|
||||
// s1.signal()
|
||||
// _ = s2.wait(timeout: .distantFuture)
|
||||
// },
|
||||
// afterBackupStep: {
|
||||
// try! source.write { db in
|
||||
// try db.execute(sql: "INSERT INTO items (id) VALUES (NULL)")
|
||||
// }
|
||||
// })
|
||||
// }
|
||||
//
|
||||
// try source.read { db in
|
||||
// XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT COUNT(*) FROM items")!, 3)
|
||||
|
||||
Reference in new issue
Block a user