From 4ff057ac3bfbe59edaf2744db561c95b0b13e0e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Rou=C3=A9?= Date: Mon, 15 May 2023 12:45:33 +0200 Subject: [PATCH] Deprecate table constraints built from SQLExpressible - prefer SQLSpecificExpressible instead Fixes #1371 --- .../Schema/TableDefinition.swift | 42 ++++++++++++++++++- Tests/GRDBTests/TableDefinitionTests.swift | 27 ++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) diff --git a/GRDB/QueryInterface/Schema/TableDefinition.swift b/GRDB/QueryInterface/Schema/TableDefinition.swift index fc21127ec..ef3782d6d 100644 --- a/GRDB/QueryInterface/Schema/TableDefinition.swift +++ b/GRDB/QueryInterface/Schema/TableDefinition.swift @@ -510,7 +510,8 @@ public struct TableOptions: OptionSet { /// /// ### Define Others Constraints /// -/// - ``check(_:)`` +/// - ``check(_:)-6u1za`` +/// - ``check(_:)-jpcg`` /// - ``check(sql:)`` /// - ``constraint(literal:)`` /// - ``constraint(sql:)`` @@ -908,10 +909,49 @@ public final class TableDefinition { /// Related SQLite documentation: /// /// - parameter condition: The checked condition. + @available(*, deprecated) public func check(_ condition: some SQLExpressible) { checkConstraints.append(condition.sqlExpression) } + /// Adds a check constraint. + /// + /// For example: + /// + /// ```swift + /// // CREATE TABLE player ( + /// // personalPhone TEXT, + /// // workPhone TEXT, + /// // CHECK personalPhone IS NOT NULL OR workPhone IS NOT NULL + /// // ) + /// try db.create(table: "player") { t in + /// t.column("personalPhone", .text) + /// t.column("workPhone", .text) + /// let personalPhone = Column("personalPhone") + /// let workPhone = Column("workPhone") + /// t.check(personalPhone != nil || workPhone != nil) + /// } + /// ``` + /// + /// When defining a check constraint on a single column, you can use the + /// ``ColumnDefinition/check(_:)`` shortcut: + /// + /// ```swift + /// // CREATE TABLE player( + /// // name TEXT CHECK (LENGTH(name) > 0) + /// // ) + /// try db.create(table: "player") { t in + /// t.column("name", .text).check { length($0) > 0 } + /// } + /// ``` + /// + /// Related SQLite documentation: + /// + /// - parameter condition: The checked condition. + public func check(_ condition: some SQLSpecificExpressible) { + checkConstraints.append(condition.sqlExpression) + } + /// Adds a check constraint. /// /// For example: diff --git a/Tests/GRDBTests/TableDefinitionTests.swift b/Tests/GRDBTests/TableDefinitionTests.swift index 1f1a97f8d..7c081b938 100644 --- a/Tests/GRDBTests/TableDefinitionTests.swift +++ b/Tests/GRDBTests/TableDefinitionTests.swift @@ -545,6 +545,33 @@ class TableDefinitionTests: GRDBTestCase { } } + @available(*, deprecated) + func testTableCheck_deprecated() throws { + let dbQueue = try makeDatabaseQueue() + try dbQueue.inDatabase { db in + try db.create(table: "test") { t in + // Deprecated because this does not what the user means! + t.check("a < b") + t.column("a", .integer) + t.column("b", .integer) + } + assertEqualSQL(lastSQLQuery!, """ + CREATE TABLE "test" (\ + "a" INTEGER, \ + "b" INTEGER, \ + CHECK ('a < b')\ + ) + """) + + // Sanity check: insert should fail because the 'a < b' string is false for SQLite + do { + try db.execute(sql: "INSERT INTO test (a, b) VALUES (0, 1)") + XCTFail() + } catch { + } + } + } + func testConstraintLiteral() throws { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in