diff --git a/CHANGELOG.md b/CHANGELOG.md index f6ae5ac96..cf4270541 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: #### 6.x Releases -- `6.24.x` Releases - [6.24.0](#6240) +- `6.24.x` Releases - [6.24.0](#6240) - [6.24.1](#6241) - `6.23.x` Releases - [6.23.0](#6230) - `6.22.x` Releases - [6.22.0](#6220) - `6.21.x` Releases - [6.21.0](#6210) @@ -122,6 +122,12 @@ GRDB adheres to [Semantic Versioning](https://semver.org/), with one exception: --- +## 6.24.1 + +Released January 6, 2024 + +- **New**: [#1477](https://github.com/groue/GRDB.swift/pull/1477): Remove shadow tables from database dump + ## 6.24.0 Released January 6, 2024 diff --git a/GRDB.swift.podspec b/GRDB.swift.podspec index afb5040f5..466b8cf16 100644 --- a/GRDB.swift.podspec +++ b/GRDB.swift.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'GRDB.swift' - s.version = '6.24.0' + s.version = '6.24.1' s.license = { :type => 'MIT', :file => 'LICENSE' } s.summary = 'A toolkit for SQLite databases, with a focus on application development.' diff --git a/GRDB/Dump/Database+Dump.swift b/GRDB/Dump/Database+Dump.swift index 146efed16..e98716edc 100644 --- a/GRDB/Dump/Database+Dump.swift +++ b/GRDB/Dump/Database+Dump.swift @@ -129,6 +129,9 @@ extension Database { /// /// > Note: Internal SQLite and GRDB schema objects are not recorded /// > (those with a name that starts with "sqlite_" or "grdb_"). + /// > + /// > [Shadow tables](https://www.sqlite.org/vtab.html#xshadowname) are + /// > not recorded, starting SQLite 3.37+. /// /// - Parameters: /// - format: The output format. @@ -252,7 +255,7 @@ extension Database { """) for row in sqlRows { let name: String = row[1] - if Database.isSQLiteInternalTable(name) || Database.isGRDBInternalTable(name) { + if try ignoresObject(named: name) { continue } stream.writeln(row[0]) @@ -266,12 +269,42 @@ extension Database { ORDER BY name COLLATE NOCASE """) .filter { - !(Database.isSQLiteInternalTable($0) || Database.isGRDBInternalTable($0)) + try !ignoresObject(named: $0) } if tables.isEmpty { return } stream.write("\n") try _dumpTables(tables, format: format, tableHeader: .always, stableOrder: true, to: &stream) } + + private func ignoresObject(named name: String) throws -> Bool { + if Database.isSQLiteInternalTable(name) { return true } + if Database.isGRDBInternalTable(name) { return true } + if try isShadowTable(name) { return true } + return false + } + + private func isShadowTable(_ tableName: String) throws -> Bool { +#if GRDBCUSTOMSQLITE || GRDBCIPHER + // Maybe SQLCipher is too old: check actual version + if sqlite3_libversion_number() >= 3037000 { + guard let table = try table(tableName) else { + // Not a table + return false + } + return table.kind == .shadow + } +#else + if #available(iOS 15.4, macOS 12.4, tvOS 15.4, watchOS 8.5, *) { // SQLite 3.37+ + guard let table = try table(tableName) else { + // Not a table + return false + } + return table.kind == .shadow + } +#endif + // Don't know + return false + } } /// Options for printing table names. diff --git a/GRDB/Dump/DatabaseReader+dump.swift b/GRDB/Dump/DatabaseReader+dump.swift index e18807d05..cd3649cae 100644 --- a/GRDB/Dump/DatabaseReader+dump.swift +++ b/GRDB/Dump/DatabaseReader+dump.swift @@ -120,6 +120,9 @@ extension DatabaseReader { /// /// > Note: Internal SQLite and GRDB schema objects are not recorded /// > (those with a name that starts with "sqlite_" or "grdb_"). + /// > + /// > [Shadow tables](https://www.sqlite.org/vtab.html#xshadowname) are + /// > not recorded, starting SQLite 3.37+. /// /// - Parameters: /// - format: The output format. diff --git a/README.md b/README.md index a908cf6ab..e7c052d6e 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ CI Status

-**Latest release**: January 6, 2024 • [version 6.24.0](https://github.com/groue/GRDB.swift/tree/v6.24.0) • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 5 to GRDB 6](Documentation/GRDB6MigrationGuide.md) +**Latest release**: January 6, 2024 • [version 6.24.1](https://github.com/groue/GRDB.swift/tree/v6.24.1) • [CHANGELOG](CHANGELOG.md) • [Migrating From GRDB 5 to GRDB 6](Documentation/GRDB6MigrationGuide.md) **Requirements**: iOS 11.0+ / macOS 10.13+ / tvOS 11.0+ / watchOS 4.0+ • SQLite 3.19.3+ • Swift 5.7+ / Xcode 14+ diff --git a/Support/Info.plist b/Support/Info.plist index 9364f2c4c..b009897a4 100644 --- a/Support/Info.plist +++ b/Support/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 6.24.0 + 6.24.1 CFBundleSignature ???? CFBundleVersion diff --git a/Tests/GRDBTests/DatabaseDumpTests.swift b/Tests/GRDBTests/DatabaseDumpTests.swift index 0665e44ee..3dc49a9fc 100644 --- a/Tests/GRDBTests/DatabaseDumpTests.swift +++ b/Tests/GRDBTests/DatabaseDumpTests.swift @@ -1336,6 +1336,78 @@ final class DatabaseDumpTests: GRDBTestCase { } } + func test_dumpContent_ignores_shadow_tables() throws { + guard sqlite3_libversion_number() >= 3037000 else { + throw XCTSkip("Can't detect shadow tables") + } + + try makeDatabaseQueue().write { db in + try db.create(table: "document") { t in + t.autoIncrementedPrimaryKey("id") + t.column("body") + } + + try db.execute(sql: "INSERT INTO document VALUES (1, 'Hello world!')") + + try db.create(virtualTable: "document_ft", using: FTS4()) { t in + t.synchronize(withTable: "document") + t.column("body") + } + + let stream = TestStream() + try db.dumpContent(to: stream) + print(stream.output) + XCTAssertEqual(stream.output, """ + sqlite_master + CREATE TABLE "document" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "body"); + CREATE TRIGGER "__document_ft_ai" AFTER INSERT ON "document" BEGIN + INSERT INTO "document_ft"("docid", "body") VALUES(new."id", new."body"); + END; + CREATE TRIGGER "__document_ft_au" AFTER UPDATE ON "document" BEGIN + INSERT INTO "document_ft"("docid", "body") VALUES(new."id", new."body"); + END; + CREATE TRIGGER "__document_ft_bd" BEFORE DELETE ON "document" BEGIN + DELETE FROM "document_ft" WHERE docid=old."id"; + END; + CREATE TRIGGER "__document_ft_bu" BEFORE UPDATE ON "document" BEGIN + DELETE FROM "document_ft" WHERE docid=old."id"; + END; + CREATE VIRTUAL TABLE "document_ft" USING fts4(body, content="document"); + + document + 1|Hello world! + + document_ft + Hello world! + + """) + } + } + + func test_dumpContent_ignores_GRDB_internal_tables() throws { + let dbQueue = try makeDatabaseQueue() + var migrator = DatabaseMigrator() + migrator.registerMigration("v1") { db in + try db.create(table: "player") { t in + t.autoIncrementedPrimaryKey("id") + } + } + try migrator.migrate(dbQueue) + + try dbQueue.read { db in + let stream = TestStream() + try db.dumpContent(to: stream) + print(stream.output) + XCTAssertEqual(stream.output, """ + sqlite_master + CREATE TABLE "player" ("id" INTEGER PRIMARY KEY AUTOINCREMENT); + + player + + """) + } + } + // MARK: - Support Databases private func makeValuesDatabase() throws -> DatabaseQueue {