From b28f75c5a2b00f13cf1398fb12d783e76f1d0a94 Mon Sep 17 00:00:00 2001 From: Thaddeus Ternes Date: Mon, 12 Jul 2021 01:19:45 -0500 Subject: [PATCH 1/9] Add VACUUM INTO support to DatabaseWriter --- GRDB/Core/DatabaseWriter.swift | 6 +++++ Tests/GRDBTests/DatabaseWriterTests.swift | 33 +++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index caf66bbf3..1f17f4e22 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -320,6 +320,12 @@ extension DatabaseWriter { try writeWithoutTransaction { try $0.execute(sql: "VACUUM") } } + public func vacuum(into: String) throws { + try writeWithoutTransaction { + try $0.execute(sql: "VACUUM INTO ?", arguments: [into]) + } + } + // MARK: - Database Observation /// A write-only observation only uses the serialized writer diff --git a/Tests/GRDBTests/DatabaseWriterTests.swift b/Tests/GRDBTests/DatabaseWriterTests.swift index 117b965a6..81c98d571 100644 --- a/Tests/GRDBTests/DatabaseWriterTests.swift +++ b/Tests/GRDBTests/DatabaseWriterTests.swift @@ -158,6 +158,11 @@ class DatabaseWriterTests : GRDBTestCase { try testEraseAndVacuum(writer: makeDatabaseQueue()) try testEraseAndVacuum(writer: makeDatabasePool()) } + + func testVacuumInto() throws { + try testVacuumInto(writer: makeDatabaseQueue()) + try testVacuumInto(writer: makeDatabasePool()) + } private func testEraseAndVacuum(writer: DatabaseWriter) throws { var migrator = DatabaseMigrator() @@ -194,6 +199,34 @@ class DatabaseWriterTests : GRDBTestCase { } } + private 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 (Row.fetchOne(db, sql: "SELECT count(*) from t1")![0] as! Int64), 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 + let newWriter = try DatabaseQueue(path: intoPath) + try newWriter.read { db in + try XCTAssertTrue(db.tableExists("t1")) + XCTAssertEqual(try (Row.fetchOne(db, sql: "SELECT count(*) from t1")![0] as! Int64), 2) + } + } + // See https://github.com/groue/GRDB.swift/issues/424 func testIssue424() throws { let dbQueue = try makeDatabaseQueue() From ed9fb39982172c1325605db89a612b1b1d68b7f0 Mon Sep 17 00:00:00 2001 From: Thaddeus Ternes Date: Mon, 12 Jul 2021 01:26:34 -0500 Subject: [PATCH 2/9] Add inline documentation --- GRDB/Core/DatabaseWriter.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index 1f17f4e22..fdc8393fe 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -320,6 +320,11 @@ extension DatabaseWriter { try writeWithoutTransaction { try $0.execute(sql: "VACUUM") } } + /// Creates a new database file at the specified file with a minimum + /// amount of disk space. + /// See https://www.sqlite.org/lang_vacuum.html for more information. + /// + /// - Parameter into: <#into description#> public func vacuum(into: String) throws { try writeWithoutTransaction { try $0.execute(sql: "VACUUM INTO ?", arguments: [into]) From 7d0c8c66577845212b90632b58815e7f7251b6af Mon Sep 17 00:00:00 2001 From: Thaddeus Ternes Date: Mon, 12 Jul 2021 02:28:49 -0500 Subject: [PATCH 3/9] Refactor tests, fix inline documentation parameter value --- GRDB/Core/DatabaseWriter.swift | 2 +- Tests/GRDBTests/DatabaseWriterTests.swift | 60 +++++++++++------------ 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index fdc8393fe..bcc2ce1be 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -324,7 +324,7 @@ extension DatabaseWriter { /// amount of disk space. /// See https://www.sqlite.org/lang_vacuum.html for more information. /// - /// - Parameter into: <#into description#> + /// - Parameter into: filename for new database public func vacuum(into: String) throws { try writeWithoutTransaction { try $0.execute(sql: "VACUUM INTO ?", arguments: [into]) diff --git a/Tests/GRDBTests/DatabaseWriterTests.swift b/Tests/GRDBTests/DatabaseWriterTests.swift index 81c98d571..0816c67b9 100644 --- a/Tests/GRDBTests/DatabaseWriterTests.swift +++ b/Tests/GRDBTests/DatabaseWriterTests.swift @@ -158,11 +158,6 @@ class DatabaseWriterTests : GRDBTestCase { try testEraseAndVacuum(writer: makeDatabaseQueue()) try testEraseAndVacuum(writer: makeDatabasePool()) } - - func testVacuumInto() throws { - try testVacuumInto(writer: makeDatabaseQueue()) - try testVacuumInto(writer: makeDatabasePool()) - } private func testEraseAndVacuum(writer: DatabaseWriter) throws { var migrator = DatabaseMigrator() @@ -198,33 +193,38 @@ class DatabaseWriterTests : GRDBTestCase { try XCTAssertNil(Row.fetchOne(db, sql: "SELECT * FROM sqlite_master")) } } - - private 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); - """) + + func testVacuumInto() throws { + 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 + let newWriter = try DatabaseQueue(path: intoPath) + try newWriter.read { db in + try XCTAssertTrue(db.tableExists("t1")) + XCTAssertEqual(try Int.fetchOne(db, sql: "SELECT count(*) from t1"), 2) + } } - try migrator.migrate(writer) - - try writer.read { db in - try XCTAssertTrue(db.tableExists("t1")) - XCTAssertEqual(try (Row.fetchOne(db, sql: "SELECT count(*) from t1")![0] as! Int64), 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 - let newWriter = try DatabaseQueue(path: intoPath) - try newWriter.read { db in - try XCTAssertTrue(db.tableExists("t1")) - XCTAssertEqual(try (Row.fetchOne(db, sql: "SELECT count(*) from t1")![0] as! Int64), 2) - } + try testVacuumInto(writer: makeDatabaseQueue()) + try testVacuumInto(writer: makeDatabasePool()) } // See https://github.com/groue/GRDB.swift/issues/424 From dc40da1ef29c045cb8ca2c4a7f05525836ffa75c Mon Sep 17 00:00:00 2001 From: Thaddeus Ternes Date: Mon, 19 Jul 2021 21:59:43 -0500 Subject: [PATCH 4/9] Improvements based on PR feedback --- GRDB/Core/DatabaseWriter.swift | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index bcc2ce1be..de0859e74 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -320,16 +320,32 @@ extension DatabaseWriter { try writeWithoutTransaction { try $0.execute(sql: "VACUUM") } } - /// Creates a new database file at the specified file with a minimum + // VACUUM INTO was introduced in SQLite 3.27.0: + // https://www.sqlite.org/releaselog/3_27_0.html + #if GRDBCUSTOMSQLITE + /// Creates a new database file at the specified path with a minimum /// amount of disk space. - /// See https://www.sqlite.org/lang_vacuum.html for more information. + /// See https://www.sqlite.org/lang_vacuum.html#vacuuminto for more information. /// - /// - Parameter into: filename for new database - public func vacuum(into: String) throws { + /// - Parameter filePath: file path for new database + public func vacuum(into filePath: String) throws { try writeWithoutTransaction { - try $0.execute(sql: "VACUUM INTO ?", arguments: [into]) + try $0.execute(sql: "VACUUM INTO ?", arguments: [filePath]) } } + #elseif !GRDBCIPHER + /// 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 From c115b6a92d9a2562a09c00703587b5ba50aeccd1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 20 Jul 2021 19:31:22 +0200 Subject: [PATCH 5/9] Explain why vacuum(into:) is not available on DatabaseReader --- GRDB/Core/DatabaseWriter.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index de0859e74..237c7b727 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -322,6 +322,10 @@ extension DatabaseWriter { // VACUUM INTO was introduced in SQLite 3.27.0: // https://www.sqlite.org/releaselog/3_27_0.html + // + // 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 /// Creates a new database file at the specified path with a minimum /// amount of disk space. From 6db245efe5c03a3c6b671bb395beffbe4eb57ae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 20 Jul 2021 19:35:59 +0200 Subject: [PATCH 6/9] CHANGELOG --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) 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. From 8d4416d02e5b6eaf67265b86f81d4b09280c65f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 20 Jul 2021 19:36:49 +0200 Subject: [PATCH 7/9] Update thank you notice --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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. From 368594b051b42bf862d97cb3b2b415dc2e1d02d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 20 Jul 2021 19:42:40 +0200 Subject: [PATCH 8/9] Disable VACUUM INTO tests when not available --- Tests/GRDBTests/DatabaseWriterTests.swift | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Tests/GRDBTests/DatabaseWriterTests.swift b/Tests/GRDBTests/DatabaseWriterTests.swift index 0816c67b9..6fdfce217 100644 --- a/Tests/GRDBTests/DatabaseWriterTests.swift +++ b/Tests/GRDBTests/DatabaseWriterTests.swift @@ -195,6 +195,10 @@ class DatabaseWriterTests : GRDBTestCase { } func testVacuumInto() throws { + guard #available(OSX 10.16, iOS 14, tvOS 14, watchOS 7, *) else { + throw XCTSkip("VACUUM INTO is not available") + } + func testVacuumInto(writer: DatabaseWriter) throws { var migrator = DatabaseMigrator() migrator.registerMigration("init") { db in From 6cc45202a02f860625b0a7d289e43aadf28ae43f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Tue, 20 Jul 2021 20:02:18 +0200 Subject: [PATCH 9/9] Enable vacuum(into:) with SQLCipher --- GRDB/Core/DatabaseWriter.swift | 12 ++++++++++-- Tests/GRDBTests/DatabaseWriterTests.swift | 9 +++++++-- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/GRDB/Core/DatabaseWriter.swift b/GRDB/Core/DatabaseWriter.swift index 237c7b727..51b965804 100644 --- a/GRDB/Core/DatabaseWriter.swift +++ b/GRDB/Core/DatabaseWriter.swift @@ -323,12 +323,20 @@ extension DatabaseWriter { // 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 + #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 @@ -337,7 +345,7 @@ extension DatabaseWriter { try $0.execute(sql: "VACUUM INTO ?", arguments: [filePath]) } } - #elseif !GRDBCIPHER + #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. diff --git a/Tests/GRDBTests/DatabaseWriterTests.swift b/Tests/GRDBTests/DatabaseWriterTests.swift index 6fdfce217..4bfdd8c95 100644 --- a/Tests/GRDBTests/DatabaseWriterTests.swift +++ b/Tests/GRDBTests/DatabaseWriterTests.swift @@ -198,6 +198,10 @@ class DatabaseWriterTests : GRDBTestCase { 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() @@ -219,8 +223,9 @@ class DatabaseWriterTests : GRDBTestCase { 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 - let newWriter = try DatabaseQueue(path: 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)