From cd16e921fe743bf5ccbf8b4145e5ad7cf8a27aec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 27 Nov 2022 11:47:56 +0100 Subject: [PATCH] Tests for primary key definition with NOT NULL constraint by default --- .../Schema/TableDefinition.swift | 10 +- Tests/GRDBTests/TableDefinitionTests.swift | 113 ++++++++++++++---- 2 files changed, 99 insertions(+), 24 deletions(-) diff --git a/GRDB/QueryInterface/Schema/TableDefinition.swift b/GRDB/QueryInterface/Schema/TableDefinition.swift index ca2c8d67d..279150231 100644 --- a/GRDB/QueryInterface/Schema/TableDefinition.swift +++ b/GRDB/QueryInterface/Schema/TableDefinition.swift @@ -609,9 +609,13 @@ public final class TableDefinition { onConflict conflictResolution: Database.ConflictResolution? = nil) -> ColumnDefinition { - column(name, type) - .notNull() - .primaryKey(onConflict: conflictResolution) + let pk = column(name, type).primaryKey(onConflict: conflictResolution) + if type == .integer { + // INTEGER PRIMARY KEY is always NOT NULL + return pk + } else { + return pk.notNull() + } } /// Defines the primary key on multiple columns. diff --git a/Tests/GRDBTests/TableDefinitionTests.swift b/Tests/GRDBTests/TableDefinitionTests.swift index e63c134d6..5c32c5204 100644 --- a/Tests/GRDBTests/TableDefinitionTests.swift +++ b/Tests/GRDBTests/TableDefinitionTests.swift @@ -8,7 +8,7 @@ class TableDefinitionTests: GRDBTestCase { try dbQueue.inDatabase { db in // Simple table creation try db.create(table: "test") { t in - t.column("id", .integer).primaryKey() + t.primaryKey("id", .integer) t.column("name", .text) } @@ -25,7 +25,7 @@ class TableDefinitionTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(table: "test", temporary: true, ifNotExists: true, withoutRowID: true) { t in - t.column("id", .integer).primaryKey() + t.primaryKey("id", .integer) } assertEqualSQL(lastSQLQuery!, """ CREATE TEMPORARY TABLE IF NOT EXISTS "test" (\ @@ -36,7 +36,7 @@ class TableDefinitionTests: GRDBTestCase { try dbQueue.inDatabase { db in try db.create(table: "test2", options: [.temporary, .ifNotExists, .withoutRowID]) { t in - t.column("id", .integer).primaryKey() + t.primaryKey("id", .integer) } assertEqualSQL(lastSQLQuery!, """ CREATE TEMPORARY TABLE IF NOT EXISTS "test2" (\ @@ -59,7 +59,7 @@ class TableDefinitionTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(table: "test3", options: [.strict]) { t in - t.column("id", .integer).primaryKey() + t.primaryKey("id", .integer) t.column("a", .integer) t.column("b", .real) t.column("c", .text) @@ -145,7 +145,7 @@ class TableDefinitionTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inTransaction { db in try db.create(table: "test") { t in - t.column("id", .integer).primaryKey(onConflict: .fail) + t.primaryKey("id", .integer, onConflict: .fail) } assertEqualSQL(lastSQLQuery!, """ CREATE TABLE "test" (\ @@ -156,6 +156,7 @@ class TableDefinitionTests: GRDBTestCase { } try dbQueue.inTransaction { db in try db.create(table: "test") { t in + // legacy api t.column("id", .integer).primaryKey(autoincrement: true) } assertEqualSQL(lastSQLQuery!, """ @@ -332,7 +333,7 @@ class TableDefinitionTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(table: "parent") { t in - t.column("name", .text).primaryKey() + t.primaryKey("name", .text) t.column("email", .text).unique() } try db.create(table: "pkless") { t in @@ -393,6 +394,7 @@ class TableDefinitionTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inTransaction { db in try db.create(table: "test") { t in + // Legacy api t.primaryKey(["a", "b"]) t.column("a", .text) t.column("b", .text) @@ -408,6 +410,23 @@ class TableDefinitionTests: GRDBTestCase { } try dbQueue.inTransaction { db in try db.create(table: "test") { t in + t.primaryKey { pk in + pk.column("a", .text) + pk.column("b", .text) + } + } + assertEqualSQL(lastSQLQuery!, """ + CREATE TABLE "test" (\ + "a" TEXT NOT NULL, \ + "b" TEXT NOT NULL, \ + PRIMARY KEY ("a", "b")\ + ) + """) + return .rollback + } + try dbQueue.inTransaction { db in + try db.create(table: "test") { t in + // Legacy api t.primaryKey(["a", "b"], onConflict: .fail) t.column("a", .text) t.column("b", .text) @@ -421,6 +440,22 @@ class TableDefinitionTests: GRDBTestCase { """) return .rollback } + try dbQueue.inTransaction { db in + try db.create(table: "test") { t in + t.primaryKey(onConflict: .fail) { pk in + pk.column("a", .text) + pk.column("b", .text) + } + } + assertEqualSQL(lastSQLQuery!, """ + CREATE TABLE "test" (\ + "a" TEXT NOT NULL, \ + "b" TEXT NOT NULL, \ + PRIMARY KEY ("a", "b") ON CONFLICT FAIL\ + ) + """) + return .rollback + } } func testTableUniqueKey() throws { @@ -449,9 +484,10 @@ class TableDefinitionTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(table: "parent") { t in - t.primaryKey(["a", "b"]) - t.column("a", .text) - t.column("b", .text) + t.primaryKey { pk in + pk.column("a", .text) + pk.column("b", .text) + } } try db.create(table: "child") { t in t.foreignKey(["c", "d"], references: "parent", onDelete: .cascade, onUpdate: .cascade) @@ -526,7 +562,7 @@ class TableDefinitionTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(table: "test1") { t in - t.column("id", .integer).primaryKey() + t.primaryKey("id", .integer) t.column("id2", .integer).references("test1") } assertEqualSQL(lastSQLQuery!, """ @@ -536,34 +572,69 @@ class TableDefinitionTests: GRDBTestCase { ) """) - try db.create(table: "test2") { t in + try db.create(table: "test2_legacy") { t in + // Legacy api t.column("id", .integer) - t.column("id2", .integer).references("test2") + t.column("id2", .integer).references("test2_legacy") t.primaryKey(["id"]) } assertEqualSQL(lastSQLQuery!, """ - CREATE TABLE "test2" (\ + CREATE TABLE "test2_legacy" (\ "id" INTEGER, \ - "id2" INTEGER REFERENCES "test2"("id"), \ + "id2" INTEGER REFERENCES "test2_legacy"("id"), \ PRIMARY KEY ("id")\ ) """) - try db.create(table: "test3") { t in + try db.create(table: "test2") { t in + t.column("id2", .integer).references("test2") + t.primaryKey { pk in + pk.column("id", .integer) + } + } + assertEqualSQL(lastSQLQuery!, """ + CREATE TABLE "test2" (\ + "id2" INTEGER REFERENCES "test2"("id"), \ + "id" INTEGER NOT NULL, \ + PRIMARY KEY ("id")\ + ) + """) + + try db.create(table: "test3Legacy") { t in t.column("a", .integer) t.column("b", .integer) t.column("c", .integer) t.column("d", .integer) - t.foreignKey(["c", "d"], references: "test3") + t.foreignKey(["c", "d"], references: "test3Legacy") t.primaryKey(["a", "b"]) } assertEqualSQL(lastSQLQuery!, """ - CREATE TABLE "test3" (\ + CREATE TABLE "test3Legacy" (\ "a" INTEGER, \ "b" INTEGER, \ "c" INTEGER, \ "d" INTEGER, \ PRIMARY KEY ("a", "b"), \ + FOREIGN KEY ("c", "d") REFERENCES "test3Legacy"("a", "b")\ + ) + """) + + try db.create(table: "test3") { t in + t.column("c", .integer) + t.column("d", .integer) + t.foreignKey(["c", "d"], references: "test3") + t.primaryKey { pk in + pk.column("a", .integer) + pk.column("b", .integer) + } + } + assertEqualSQL(lastSQLQuery!, """ + CREATE TABLE "test3" (\ + "c" INTEGER, \ + "d" INTEGER, \ + "a" INTEGER NOT NULL, \ + "b" INTEGER NOT NULL, \ + PRIMARY KEY ("a", "b"), \ FOREIGN KEY ("c", "d") REFERENCES "test3"("a", "b")\ ) """) @@ -774,7 +845,7 @@ class TableDefinitionTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(table: "test") { t in - t.column("id", .integer).primaryKey() + t.primaryKey("id", .integer) t.column("name", .text) } XCTAssertTrue(try db.tableExists("test")) @@ -790,7 +861,7 @@ class TableDefinitionTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(table: "test") { t in - t.column("id", .integer).primaryKey() + t.primaryKey("id", .integer) t.column("a", .text) t.column("b", .text) } @@ -813,7 +884,7 @@ class TableDefinitionTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(table: "test") { t in - t.column("id", .integer).primaryKey() + t.primaryKey("id", .integer) t.column("a", .text) t.column("b", .text) } @@ -830,7 +901,7 @@ class TableDefinitionTests: GRDBTestCase { let dbQueue = try makeDatabaseQueue() try dbQueue.inDatabase { db in try db.create(table: "test") { t in - t.column("id", .integer).primaryKey() + t.primaryKey("id", .integer) t.column("name", .text) } try db.create(index: "test_on_name", on: "test", columns: ["name"])