Deprecate table constraints built from SQLExpressible - prefer SQLSpecificExpressible instead

Fixes #1371
This commit is contained in:
Gwendal Roué committed 2023-05-15 12:45:33 +02:00
1 parent 0d49d7612f
commit 4ff057ac3b
2 files changed
+68 -1

No files matched your search

@@ -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: <https://www.sqlite.org/lang_createtable.html#ckconst>
///
/// - 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: <https://www.sqlite.org/lang_createtable.html#ckconst>
///
/// - parameter condition: The checked condition.
public func check(_ condition: some SQLSpecificExpressible) {
checkConstraints.append(condition.sqlExpression)
}
/// Adds a check constraint.
///
/// For example:
@@ -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