From 07b944039ce5b3a07a5cb3534289010c0937e7d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 15 Mar 2018 19:52:44 +0100 Subject: [PATCH 1/6] Database.isSQLiteInternalTable(_:) --- GRDB/Core/Database+Schema.swift | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/GRDB/Core/Database+Schema.swift b/GRDB/Core/Database+Schema.swift index e0da07cc1..748bbb4fc 100644 --- a/GRDB/Core/Database+Schema.swift +++ b/GRDB/Core/Database+Schema.swift @@ -30,6 +30,19 @@ extension Database { return try Row.fetchOne(self, "SELECT 1 FROM (SELECT sql, type, name FROM sqlite_master UNION SELECT sql, type, name FROM sqlite_temp_master) WHERE type = 'table' AND LOWER(name) = ?", arguments: [tableName.lowercased()]) != nil } + /// Returns whether a table is an internal SQLite table. + /// + /// For more information, see https://www.sqlite.org/fileformat2.html + public func isSQLiteInternalTable(_ tableName: String) -> Bool { + // https://www.sqlite.org/fileformat2.html#internal_schema_objects + // > The names of internal schema objects always begin with "sqlite_" + // > and any table, index, view, or trigger whose name begins with + // > "sqlite_" is an internal schema object. SQLite prohibits + // > applications from creating objects whose names begin with + // > "sqlite_". + return tableName.starts(with: "sqlite_") + } + /// Returns whether a view exists. public func viewExists(_ viewName: String) throws -> Bool { // SQlite identifiers are case-insensitive, case-preserving (http://www.alberton.info/dbms_identifiers_and_case_sensitivity.html) From d61551cedb0936e26791b5c044e7d11841f15adb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 15 Mar 2018 19:57:03 +0100 Subject: [PATCH 2/6] Database.isSQLiteInternalTable(_:) doc --- CHANGELOG.md | 4 ++++ README.md | 1 + 2 files changed, 5 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5e6989370..0e28e766e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,10 @@ Release Notes ## Next Version +### New + +- `Database.isSQLiteInternalTable(_:)` returns whether a table name is an internal SQLite table. + ### Breaking Changes - The Record protocols have been renamed: `RowConvertible` to `FetchableRecord`, `Persistable` to `PersistableRecord`, and `TableMapping` to `TableRecord`. diff --git a/README.md b/README.md index b8d821d63..368828eea 100644 --- a/README.md +++ b/README.md @@ -1647,6 +1647,7 @@ try db.indexes(on: "players") // [IndexInfo], the indexes defined on the tab try db.foreignKeys(on: "players") // [ForeignKeyInfo], the foreign keys defined on the table try db.primaryKey("players") // PrimaryKeyInfo try db.table("players", hasUniqueKey: ["email"]) // Bool, true if column(s) is a unique key +Database.isSQLiteInternalTable("players") // Bool, true if argument is the name of an internal to SQLite table ``` From 0d7f392bcde975262bdcb6a18cb25ddcdd0f5261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 15 Mar 2018 20:04:50 +0100 Subject: [PATCH 3/6] Database.isGRDBInternalTable(_:) --- CHANGELOG.md | 1 + GRDB/Core/Database+Schema.swift | 11 ++++++++++- README.md | 3 ++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e28e766e..4ccbb6642 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Release Notes ### New - `Database.isSQLiteInternalTable(_:)` returns whether a table name is an internal SQLite table. +- `Database.isGRDBInternalTable(_:)` returns whether a table name is an internal GRDB table. ### Breaking Changes diff --git a/GRDB/Core/Database+Schema.swift b/GRDB/Core/Database+Schema.swift index 748bbb4fc..4c053acb5 100644 --- a/GRDB/Core/Database+Schema.swift +++ b/GRDB/Core/Database+Schema.swift @@ -32,6 +32,8 @@ extension Database { /// Returns whether a table is an internal SQLite table. /// + /// Those are tables whose name begins with "sqlite_". + /// /// For more information, see https://www.sqlite.org/fileformat2.html public func isSQLiteInternalTable(_ tableName: String) -> Bool { // https://www.sqlite.org/fileformat2.html#internal_schema_objects @@ -42,7 +44,14 @@ extension Database { // > "sqlite_". return tableName.starts(with: "sqlite_") } - + + /// Returns whether a table is an internal GRDB table. + /// + /// Those are tables whose name begins with "grdb_". + public func isGRDBInternalTable(_ tableName: String) -> Bool { + return tableName.starts(with: "grdb_") + } + /// Returns whether a view exists. public func viewExists(_ viewName: String) throws -> Bool { // SQlite identifiers are case-insensitive, case-preserving (http://www.alberton.info/dbms_identifiers_and_case_sensitivity.html) diff --git a/README.md b/README.md index 368828eea..cae0b6f3a 100644 --- a/README.md +++ b/README.md @@ -1647,7 +1647,8 @@ try db.indexes(on: "players") // [IndexInfo], the indexes defined on the tab try db.foreignKeys(on: "players") // [ForeignKeyInfo], the foreign keys defined on the table try db.primaryKey("players") // PrimaryKeyInfo try db.table("players", hasUniqueKey: ["email"]) // Bool, true if column(s) is a unique key -Database.isSQLiteInternalTable("players") // Bool, true if argument is the name of an internal to SQLite table +Database.isSQLiteInternalTable(...) // Bool, true if argument is the name of an internal SQLite table +Database.isGRDBInternalTable(...) // Bool, true if argument is the name of an internal GRDB table ``` From fa8ed89eae9230e545a05ec7035e68e883960739 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 15 Mar 2018 20:10:59 +0100 Subject: [PATCH 4/6] DatabaseMigrator.appliedMigrations(_:) --- GRDB/Migration/DatabaseMigrator.swift | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/GRDB/Migration/DatabaseMigrator.swift b/GRDB/Migration/DatabaseMigrator.swift index b6de0e654..d378b900a 100644 --- a/GRDB/Migration/DatabaseMigrator.swift +++ b/GRDB/Migration/DatabaseMigrator.swift @@ -146,6 +146,11 @@ public struct DatabaseMigrator { } } + /// Returns the set of applied migration identifiers. + public func appliedMigrations(_ reader: DatabaseReader) throws -> Set { + return try reader.read { try appliedMigrations($0) } + } + // MARK: - Non public @@ -160,13 +165,13 @@ public struct DatabaseMigrator { try db.execute("CREATE TABLE IF NOT EXISTS grdb_migrations (identifier TEXT NOT NULL PRIMARY KEY)") } - private func appliedIdentifiers(_ db: Database) throws -> Set { + private func appliedMigrations(_ db: Database) throws -> Set { return try Set(String.fetchAll(db, "SELECT identifier FROM grdb_migrations")) } private func runMigrations(_ db: Database) throws { - let appliedIdentifiers = try self.appliedIdentifiers(db) - for migration in migrations where !appliedIdentifiers.contains(migration.identifier) { + let appliedMigrations = try self.appliedMigrations(db) + for migration in migrations where !appliedMigrations.contains(migration.identifier) { try migration.run(db) } } @@ -184,13 +189,13 @@ public struct DatabaseMigrator { GRDBPrecondition(prefixMigrations.last?.identifier == targetIdentifier, "undefined migration: \(String(reflecting: targetIdentifier))") // Subsequent migration must not be applied - let appliedIdentifiers = try self.appliedIdentifiers(db) + let appliedMigrations = try self.appliedMigrations(db) if prefixMigrations.count < migrations.count { let nextIdentifier = migrations[prefixMigrations.count].identifier - GRDBPrecondition(!appliedIdentifiers.contains(nextIdentifier), "database is already migrated beyond migration \(String(reflecting: targetIdentifier))") + GRDBPrecondition(!appliedMigrations.contains(nextIdentifier), "database is already migrated beyond migration \(String(reflecting: targetIdentifier))") } - for migration in prefixMigrations where !appliedIdentifiers.contains(migration.identifier) { + for migration in prefixMigrations where !appliedMigrations.contains(migration.identifier) { try migration.run(db) } } From c7ad3ef4a93b194b1e14ed60e575b5669d051b90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 15 Mar 2018 20:15:23 +0100 Subject: [PATCH 5/6] DatabaseMigrator.appliedMigrations(in:) doc & changelog --- CHANGELOG.md | 1 + GRDB/Migration/DatabaseMigrator.swift | 2 +- README.md | 9 +++++++++ 3 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ccbb6642..03c8fbd21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ Release Notes - `Database.isSQLiteInternalTable(_:)` returns whether a table name is an internal SQLite table. - `Database.isGRDBInternalTable(_:)` returns whether a table name is an internal GRDB table. +- `DatabaseMigrator.appliedMigrations(in:)` returns the set of applied migrations identifiers. ### Breaking Changes diff --git a/GRDB/Migration/DatabaseMigrator.swift b/GRDB/Migration/DatabaseMigrator.swift index d378b900a..d9390a6b5 100644 --- a/GRDB/Migration/DatabaseMigrator.swift +++ b/GRDB/Migration/DatabaseMigrator.swift @@ -147,7 +147,7 @@ public struct DatabaseMigrator { } /// Returns the set of applied migration identifiers. - public func appliedMigrations(_ reader: DatabaseReader) throws -> Set { + public func appliedMigrations(in reader: DatabaseReader) throws -> Set { return try reader.read { try appliedMigrations($0) } } diff --git a/README.md b/README.md index cae0b6f3a..50f8a8a04 100644 --- a/README.md +++ b/README.md @@ -3854,6 +3854,15 @@ try migrator.migrate(dbQueue, upTo: "v1") // fatal error: database is already migrated beyond migration "v1" ``` +Check if a migration has been applied: + +```swift +let appliedMigrations = try migrator.appliedMigration(in: dbQueue) +if appliedMigrations.contains("v2") { + // "v2" has been applied +} +``` + ### Advanced Database Schema Changes From ebe1d93c3afa93b611a3153ed64cba461ceb292c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Thu, 15 Mar 2018 20:22:39 +0100 Subject: [PATCH 6/6] Polish --- CHANGELOG.md | 2 +- README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 03c8fbd21..33c757cf0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ Release Notes - `Database.isSQLiteInternalTable(_:)` returns whether a table name is an internal SQLite table. - `Database.isGRDBInternalTable(_:)` returns whether a table name is an internal GRDB table. -- `DatabaseMigrator.appliedMigrations(in:)` returns the set of applied migrations identifiers. +- `DatabaseMigrator.appliedMigrations(in:)` returns the set of applied migrations identifiers in a database. ### Breaking Changes diff --git a/README.md b/README.md index 50f8a8a04..448db7e93 100644 --- a/README.md +++ b/README.md @@ -3859,7 +3859,7 @@ Check if a migration has been applied: ```swift let appliedMigrations = try migrator.appliedMigration(in: dbQueue) if appliedMigrations.contains("v2") { - // "v2" has been applied + // "v2" migration has been applied } ```