From 38f5226e95aebd1667cb625ff380fb5a4ab714d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 31 Aug 2025 11:30:53 +0200 Subject: [PATCH] Database.table(_:hasUniqueKey:) now accept views --- GRDB/Core/Database+Schema.swift | 36 ++++++++++++++++------------ Tests/GRDBTests/IndexInfoTests.swift | 28 ++++++++++++++++++++++ 2 files changed, 49 insertions(+), 15 deletions(-) diff --git a/GRDB/Core/Database+Schema.swift b/GRDB/Core/Database+Schema.swift index dfe5a9b19..8febc3767 100644 --- a/GRDB/Core/Database+Schema.swift +++ b/GRDB/Core/Database+Schema.swift @@ -1067,6 +1067,9 @@ extension Database { /// returns the columns of the unique key, ordered as the matching index (or /// primary key). The case of returned columns is not guaranteed to match /// the case of input columns. + /// + /// This method accepts both tables and views. For views, the primary + /// key returned by the schemaSource is considered as a unique key. func columnsForUniqueKey( _ columns: some Collection, in tableName: String @@ -1077,23 +1080,26 @@ extension Database { return nil } - // Check rowid - let primaryKey = try self.primaryKey(tableName) - if primaryKey.tableHasRowID && lowercasedColumns == ["rowid"] { - return ["rowid"] + // Check primaryKey (ignoring views if the schemaSource does not customize them). + if let primaryKey = try? self.primaryKey(tableName) { + if primaryKey.tableHasRowID && lowercasedColumns == ["rowid"] { + return ["rowid"] + } + + // Check primaryKey + if Set(primaryKey.columns.map { $0.lowercased() }).isSubset(of: lowercasedColumns) { + return primaryKey.columns + } } - // Check primaryKey - if Set(primaryKey.columns.map { $0.lowercased() }).isSubset(of: lowercasedColumns) { - return primaryKey.columns - } - - // Check unique indexes - let matchingIndex = try indexes(on: tableName).first { index in - index.isUnique && Set(index.columns.map { $0.lowercased() }).isSubset(of: lowercasedColumns) - } - if let matchingIndex { - return matchingIndex.columns + // Check unique indexes (ignoring views) + if try tableExists(tableName) { + let matchingIndex = try indexes(on: tableName).first { index in + index.isUnique && Set(index.columns.map { $0.lowercased() }).isSubset(of: lowercasedColumns) + } + if let matchingIndex { + return matchingIndex.columns + } } // No matching unique key found diff --git a/Tests/GRDBTests/IndexInfoTests.swift b/Tests/GRDBTests/IndexInfoTests.swift index 9ce46a36f..9ebec7dd8 100644 --- a/Tests/GRDBTests/IndexInfoTests.swift +++ b/Tests/GRDBTests/IndexInfoTests.swift @@ -71,9 +71,37 @@ class IndexInfoTests: GRDBTestCase { try XCTAssertFalse(db.table("citizenships", hasUniqueKey: ["year"])) try XCTAssertFalse(db.table("citizenships", hasUniqueKey: ["personId"])) try XCTAssertFalse(db.table("citizenships", hasUniqueKey: ["countryIsoCode"])) + + try db.execute(sql: "CREATE VIEW personView AS SELECT * FROM persons") + try XCTAssertFalse(db.table("personView", hasUniqueKey: ["rowid"])) + try XCTAssertFalse(db.table("personView", hasUniqueKey: ["id"])) + try XCTAssertFalse(db.table("personView", hasUniqueKey: ["email"])) + try XCTAssertFalse(db.table("personView", hasUniqueKey: [])) + try XCTAssertFalse(db.table("personView", hasUniqueKey: ["name"])) + try XCTAssertFalse(db.table("personView", hasUniqueKey: ["id", "email"])) } } + func testColumnsThatUniquelyIdentityRows_with_view_and_schema_source() throws { + struct MySchemaSource: DatabaseSchemaSource { + func columnsForPrimaryKey(_ db: Database, inView view: DatabaseObjectID) throws -> [String]? { + ["id"] + } + } + + dbConfiguration.schemaSource = MySchemaSource() + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.execute(sql: "CREATE TABLE persons (id INTEGER PRIMARY KEY, name TEXT, email TEXT UNIQUE)") + try db.execute(sql: "CREATE VIEW personView AS SELECT * FROM persons") + + try XCTAssertTrue(db.table("personView", hasUniqueKey: ["id"])) + try XCTAssertFalse(db.table("personView", hasUniqueKey: [])) + try XCTAssertFalse(db.table("personView", hasUniqueKey: ["name"])) + try XCTAssertTrue(db.table("personView", hasUniqueKey: ["id", "email"])) + } + } + // Regression test for https://github.com/groue/GRDB.swift/issues/840 func testIndexOnExpressionIsExcluded() throws { let dbQueue = try makeDatabaseQueue()