mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-22 04:00:39 +00:00
Failing test for upsert in a table WITHOUT ROWID
Failing test for #1855
This commit is contained in:
1 parent
36e30a6f1e
commit
c637e04133
2 files changed
+108
No files matched your search
@@ -2334,6 +2334,60 @@ extension MutablePersistableRecordTests {
|
||||
}
|
||||
}
|
||||
|
||||
func test_upsert_WITHOUT_ROWID_optimization() throws {
|
||||
#if GRDBCUSTOMSQLITE || SQLITE_HAS_CODEC
|
||||
guard Database.sqliteLibVersionNumber >= 3035000 else {
|
||||
throw XCTSkip("UPSERT is not available")
|
||||
}
|
||||
#else
|
||||
guard #available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) else {
|
||||
throw XCTSkip("UPSERT is not available")
|
||||
}
|
||||
#endif
|
||||
|
||||
struct MyRecord: Codable, FetchableRecord, MutablePersistableRecord {
|
||||
var id: String
|
||||
var name: String
|
||||
var score: Int?
|
||||
}
|
||||
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.execute(sql: """
|
||||
CREATE TABLE myRecord(
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
score INTEGER NOT NULL
|
||||
) WITHOUT ROWID;
|
||||
""")
|
||||
|
||||
do {
|
||||
var record = MyRecord(id: "1", name: "foo", score: 1000)
|
||||
try record.upsert(db)
|
||||
|
||||
XCTAssertEqual(lastSQLQuery, """
|
||||
INSERT INTO "myRecord" ("id", "name", "score") \
|
||||
VALUES ('1','foo',1000) \
|
||||
ON CONFLICT DO UPDATE SET "name" = "excluded"."name", "score" = "excluded"."score"
|
||||
""")
|
||||
}
|
||||
do {
|
||||
var record = MyRecord(id: "2", name: "foo", score: 2000)
|
||||
let upserted = try record.upsertAndFetch(db)
|
||||
|
||||
XCTAssertEqual(lastSQLQuery, """
|
||||
INSERT INTO "myRecord" ("id", "name", "score") \
|
||||
VALUES ('2','foo',2000) \
|
||||
ON CONFLICT DO UPDATE SET "name" = "excluded"."name", "score" = "excluded"."score" \
|
||||
RETURNING *
|
||||
""")
|
||||
|
||||
XCTAssertEqual(upserted.id, "1")
|
||||
XCTAssertEqual(upserted.name, "foo")
|
||||
XCTAssertEqual(upserted.score, 2000)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_upsertAndFetch_do_update_set_where_with_default_strategy() throws {
|
||||
#if GRDBCUSTOMSQLITE || SQLITE_HAS_CODEC
|
||||
guard Database.sqliteLibVersionNumber >= 3035000 else {
|
||||
|
||||
@@ -2324,6 +2324,60 @@ extension PersistableRecordTests {
|
||||
}
|
||||
}
|
||||
|
||||
func test_upsert_WITHOUT_ROWID_optimization() throws {
|
||||
#if GRDBCUSTOMSQLITE || SQLITE_HAS_CODEC
|
||||
guard Database.sqliteLibVersionNumber >= 3035000 else {
|
||||
throw XCTSkip("UPSERT is not available")
|
||||
}
|
||||
#else
|
||||
guard #available(iOS 15, macOS 12, tvOS 15, watchOS 8, *) else {
|
||||
throw XCTSkip("UPSERT is not available")
|
||||
}
|
||||
#endif
|
||||
|
||||
struct MyRecord: Codable, FetchableRecord, PersistableRecord {
|
||||
var id: String
|
||||
var name: String
|
||||
var score: Int?
|
||||
}
|
||||
|
||||
try makeDatabaseQueue().write { db in
|
||||
try db.execute(sql: """
|
||||
CREATE TABLE myRecord(
|
||||
id TEXT NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
score INTEGER NOT NULL
|
||||
) WITHOUT ROWID;
|
||||
""")
|
||||
|
||||
do {
|
||||
let record = MyRecord(id: "1", name: "foo", score: 1000)
|
||||
try record.upsert(db)
|
||||
|
||||
XCTAssertEqual(lastSQLQuery, """
|
||||
INSERT INTO "myRecord" ("id", "name", "score") \
|
||||
VALUES ('1','foo',1000) \
|
||||
ON CONFLICT DO UPDATE SET "name" = "excluded"."name", "score" = "excluded"."score"
|
||||
""")
|
||||
}
|
||||
do {
|
||||
let record = MyRecord(id: "2", name: "foo", score: 2000)
|
||||
let upserted = try record.upsertAndFetch(db)
|
||||
|
||||
XCTAssertEqual(lastSQLQuery, """
|
||||
INSERT INTO "myRecord" ("id", "name", "score") \
|
||||
VALUES ('2','foo',2000) \
|
||||
ON CONFLICT DO UPDATE SET "name" = "excluded"."name", "score" = "excluded"."score" \
|
||||
RETURNING *
|
||||
""")
|
||||
|
||||
XCTAssertEqual(upserted.id, "1")
|
||||
XCTAssertEqual(upserted.name, "foo")
|
||||
XCTAssertEqual(upserted.score, 2000)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_upsertAndFetch_do_update_set_where_with_default_strategy() throws {
|
||||
#if GRDBCUSTOMSQLITE || SQLITE_HAS_CODEC
|
||||
guard Database.sqliteLibVersionNumber >= 3035000 else {
|
||||
|
||||
Reference in new issue
Block a user