diff --git a/CHANGELOG.md b/CHANGELOG.md index a9b62adb7..7cf081224 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -92,6 +92,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: - **New**: [#993](https://github.com/groue/GRDB.swift/pull/993) by [@groue](https://github.com/groue): QueryInterfaceRequest builder with dynamic table/view name - **New**: Types that adopt both `DatabaseValueConvertible` and `Codable` now profit from automatic JSON encoding and decoding. +- **New**: [#1010](https://github.com/groue/GRDB.swift/pull/1010) by [@tternes](https://github.com/tternes): Add VACUUM INTO Support - **Documentation Update**: The [Requests](README.md#requests) chapter was updated for the new `Table` type that can build requests without any record type. - **Documentation Update**: The [Custom Value Types](README.md#custom-value-types) chapter was extended about the new support for codable value types encoded as JSON arrays or objects. diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index caf66bbf3..51b965804 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -320,6 +320,45 @@ extension DatabaseWriter { try writeWithoutTransaction { try $0.execute(sql: "VACUUM") } } + // VACUUM INTO was introduced in SQLite 3.27.0: + // https://www.sqlite.org/releaselog/3_27_0.html + // + // Old versions of SQLCipher won't have it, but I don't know how to perform + // availability checks that depend on the version of the SQLCipher CocoaPod + // chosen by the application. So let's just have the method fail at runtime. + // + // This method is declared on DatabaseWriter instead of DatabaseReader, + // so that it is not available on DatabaseSnaphot. VACUUM INTO is not + // available inside the transaction that is kept open by DatabaseSnaphot. + #if GRDBCUSTOMSQLITE || GRDBCIPHER + /// Creates a new database file at the specified path with a minimum + /// amount of disk space. + /// + /// Databases encrypted with SQLCipher are copied with the same password + /// and configuration as the original database. + /// + /// See https://www.sqlite.org/lang_vacuum.html#vacuuminto for more information. + /// + /// - Parameter filePath: file path for new database + public func vacuum(into filePath: String) throws { + try writeWithoutTransaction { + try $0.execute(sql: "VACUUM INTO ?", arguments: [filePath]) + } + } + #else + /// Creates a new database file at the specified path with a minimum + /// amount of disk space. + /// See https://www.sqlite.org/lang_vacuum.html#vacuuminto for more information. + /// + /// - Parameter filePath: file path for new database + @available(OSX 10.16, iOS 14, tvOS 14, watchOS 7, *) + public func vacuum(into filePath: String) throws { + try writeWithoutTransaction { + try $0.execute(sql: "VACUUM INTO ?", arguments: [filePath]) + } + } + #endif + // MARK: - Database Observation /// A write-only observation only uses the serialized writer diff --git a/README.md b/README.md index ac1045007..90b94f765 100644 --- a/README.md +++ b/README.md @@ -8334,7 +8334,7 @@ Sample Code **Thanks** - [Pierlis](http://pierlis.com), where we write great software. -- [@alextrob](https://github.com/alextrob), [@alexwlchan](https://github.com/alexwlchan), [@bellebethcooper](https://github.com/bellebethcooper), [@bfad](https://github.com/bfad), [@cfilipov](https://github.com/cfilipov), [@charlesmchen-signal](https://github.com/charlesmchen-signal), [@Chiliec](https://github.com/Chiliec), [@chrisballinger](https://github.com/chrisballinger), [@darrenclark](https://github.com/darrenclark), [@davidkraus](https://github.com/davidkraus), [@eburns-vmware](https://github.com/eburns-vmware), [@felixscheinost](https://github.com/felixscheinost), [@fpillet](http://github.com/fpillet), [@gcox](https://github.com/gcox), [@GetToSet](https://github.com/GetToSet), [@gjeck](https://github.com/gjeck), [@gusrota](https://github.com/gusrota), [@haikusw](https://github.com/haikusw), [@hartbit](https://github.com/hartbit), [@holsety](https://github.com/holsety), [@jroselightricks](https://github.com/jroselightricks), [@kdubb](https://github.com/kdubb), [@kluufger](https://github.com/kluufger), [@KyleLeneau](https://github.com/KyleLeneau), [@mallman](https://github.com/mallman), [@MartinP7r](https://github.com/MartinP7r), [@Marus](https://github.com/Marus), [@MaxDesiatov](https://github.com/MaxDesiatov), [@michaelkirk-signal](https://github.com/michaelkirk-signal), [@mtancock](https://github.com/mtancock), [@pakko972](https://github.com/pakko972), [@peter-ss](https://github.com/peter-ss), [@pierlo](https://github.com/pierlo), [@pocketpixels](https://github.com/pocketpixels), [@pp5x](https://github.com/pp5x), [@professordeng](https://github.com/professordeng), [@robcas3](https://github.com/robcas3), [@runhum](https://github.com/runhum), [@schveiguy](https://github.com/schveiguy), [@SD10](https://github.com/SD10), [@sobri909](https://github.com/sobri909), [@sroddy](https://github.com/sroddy), [@steipete](https://github.com/steipete), [@swiftlyfalling](https://github.com/swiftlyfalling), [@Timac](https://github.com/Timac), [@valexa](https://github.com/valexa), [@wuyuehyang](https://github.com/wuyuehyang), and [@zmeyc](https://github.com/zmeyc) for their contributions, help, and feedback on GRDB. +- [@alextrob](https://github.com/alextrob), [@alexwlchan](https://github.com/alexwlchan), [@bellebethcooper](https://github.com/bellebethcooper), [@bfad](https://github.com/bfad), [@cfilipov](https://github.com/cfilipov), [@charlesmchen-signal](https://github.com/charlesmchen-signal), [@Chiliec](https://github.com/Chiliec), [@chrisballinger](https://github.com/chrisballinger), [@darrenclark](https://github.com/darrenclark), [@davidkraus](https://github.com/davidkraus), [@eburns-vmware](https://github.com/eburns-vmware), [@felixscheinost](https://github.com/felixscheinost), [@fpillet](http://github.com/fpillet), [@gcox](https://github.com/gcox), [@GetToSet](https://github.com/GetToSet), [@gjeck](https://github.com/gjeck), [@gusrota](https://github.com/gusrota), [@haikusw](https://github.com/haikusw), [@hartbit](https://github.com/hartbit), [@holsety](https://github.com/holsety), [@jroselightricks](https://github.com/jroselightricks), [@kdubb](https://github.com/kdubb), [@kluufger](https://github.com/kluufger), [@KyleLeneau](https://github.com/KyleLeneau), [@mallman](https://github.com/mallman), [@MartinP7r](https://github.com/MartinP7r), [@Marus](https://github.com/Marus), [@MaxDesiatov](https://github.com/MaxDesiatov), [@michaelkirk-signal](https://github.com/michaelkirk-signal), [@mtancock](https://github.com/mtancock), [@pakko972](https://github.com/pakko972), [@peter-ss](https://github.com/peter-ss), [@pierlo](https://github.com/pierlo), [@pocketpixels](https://github.com/pocketpixels), [@pp5x](https://github.com/pp5x), [@professordeng](https://github.com/professordeng), [@robcas3](https://github.com/robcas3), [@runhum](https://github.com/runhum), [@schveiguy](https://github.com/schveiguy), [@SD10](https://github.com/SD10), [@sobri909](https://github.com/sobri909), [@sroddy](https://github.com/sroddy), [@steipete](https://github.com/steipete), [@swiftlyfalling](https://github.com/swiftlyfalling), [@Timac](https://github.com/Timac), [@tternes](https://github.com/tternes), [@valexa](https://github.com/valexa), [@wuyuehyang](https://github.com/wuyuehyang), and [@zmeyc](https://github.com/zmeyc) for their contributions, help, and feedback on GRDB. - [@aymerick](https://github.com/aymerick) and [@kali](https://github.com/kali) because SQL. - [ccgus/fmdb](https://github.com/ccgus/fmdb) for its excellency. diff --git a/Tests/GRDBTests/DatabaseWriterTests.swift b/Tests/GRDBTests/DatabaseWriterTests.swift index 117b965a6..4bfdd8c95 100644 --- a/Tests/GRDBTests/DatabaseWriterTests.swift +++ b/Tests/GRDBTests/DatabaseWriterTests.swift @@ -193,6 +193,48 @@ class DatabaseWriterTests : GRDBTestCase { try XCTAssertNil(Row.fetchOne(db, sql: "SELECT * FROM sqlite_master")) } } + + func testVacuumInto() throws { + guard #available(OSX 10.16, iOS 14, tvOS 14, watchOS 7, *) else { + throw XCTSkip("VACUUM INTO is not available") + } + // Prevent SQLCipher failures + guard sqlite3_libversion_number() >= 3027000 else { + throw XCTSkip("VACUUM INTO is not available") + } + + func testVacuumInto(writer: DatabaseWriter) throws { + var migrator = DatabaseMigrator() + migrator.registerMigration("init") { db in + try db.execute(sql: """ + CREATE TABLE t1 (id INTEGER PRIMARY KEY AUTOINCREMENT, b, c); + INSERT INTO t1 (b, c) VALUES (1, 1); + INSERT INTO t1 (b, c) VALUES (2, 2); + """) + } + + try migrator.migrate(writer) + + try writer.read { db in + try XCTAssertTrue(db.tableExists("t1")) + XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT count(*) from t1"), 2) + } + + let intoPath = NSTemporaryDirectory().appending(ProcessInfo.processInfo.globallyUniqueString).appending("-vacuum-into-db.sqlite") + try writer.vacuum(into: intoPath) + + // open newly created file and ensure table was copied, and + // encrypted like the original. + let newWriter = try DatabaseQueue(path: intoPath, configuration: writer.configuration) + try newWriter.read { db in + try XCTAssertTrue(db.tableExists("t1")) + XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT count(*) from t1"), 2) + } + } + + try testVacuumInto(writer: makeDatabaseQueue()) + try testVacuumInto(writer: makeDatabasePool()) + } // See https://github.com/groue/GRDB.swift/issues/424 func testIssue424() throws {