Rename methods that check foreign keys

This commit is contained in:
Gwendal Roué committed 2021-11-04 18:26:24 +01:00
1 parent ffdc80646e
commit 127c4e0440
5 files changed
+23 -23

No files matched your search

+5 -5
View File
@@ -162,7 +162,7 @@ The detailed sequence of operations is described below, some of them are perform
SQLite makes it [difficult](https://www.sqlite.org/lang_altertable.html) to perform some schema changes, and this creates very undesired churn w.r.t. foreign keys. GRDB makes its best to hide those problems to you, but you might have to deal with them one day, especially if your database becomes *very big*:
You'll need to read this chapter if you are looking for a mitigation to the time spent by migrations performing foreign key checks. You'll know this by instrumenting your migrations, and looking for the time spent in the `checkForeignKeyViolations` method. See [Advanced Database Schema Changes] right above to know what are those foreign key checks.
You'll need to read this chapter if you are looking for a mitigation to the time spent by migrations performing foreign key checks. You'll know this by instrumenting your migrations, and looking for the time spent in the `checkForeignKeys` method. See [Advanced Database Schema Changes] right above to know what are those foreign key checks.
**Your first mitigation technique is immediate foreign key checks.**
@@ -209,7 +209,7 @@ In order to prevent foreign key violations from being committed to disk, you can
// Throws an error and stops migrations if there exists a
// foreign key violation in the 'player' table.
try db.checkForeignKeyViolations(in: "player")
try db.checkForeignKeys(in: "player")
}
```
@@ -220,16 +220,16 @@ In order to prevent foreign key violations from being committed to disk, you can
// Throws an error if there exists any foreign key violation.
try dbQueue.read { db in
try db.checkForeignKeyViolations()
try db.checkForeignKeys()
}
```
In order to check for foreign key violations, the `checkForeignKeyViolations()` and `checkForeignKeyViolations(in:)` methods are recommended over the raw use of the [`PRAGMA foreign_key_check`](https://www.sqlite.org/pragma.html#pragma_foreign_key_check). Those methods throw a nicely detailed DatabaseError that contains a lot of debugging information:
In order to check for foreign key violations, the `checkForeignKeys()` and `checkForeignKeys(in:)` methods are recommended over the raw use of the [`PRAGMA foreign_key_check`](https://www.sqlite.org/pragma.html#pragma_foreign_key_check). Those methods throw a nicely detailed DatabaseError that contains a lot of debugging information:
```swift
// SQLite error 19: foreign key constraint failed from player(teamId) to team(id),
// in [id:1 teamId:2 name:"O'Brien" score:1000]
try db.checkForeignKeyViolations()
try db.checkForeignKeys()
```
You can also iterate a lazy [cursor](../README.md#cursors) of all individual foreign key violations found in the database:
+5 -5
View File
@@ -479,14 +479,14 @@ extension Database {
/// Throws a DatabaseError of extended code `SQLITE_CONSTRAINT_FOREIGNKEY`
/// if there exists a foreign key violation in the database.
public func checkForeignKeyViolations() throws {
try checkForeignKeyViolations(from: foreignKeyViolations())
public func checkForeignKeys() throws {
try checkForeignKeys(from: foreignKeyViolations())
}
/// Throws a DatabaseError of extended code `SQLITE_CONSTRAINT_FOREIGNKEY`
/// if there exists a foreign key violation in the table.
public func checkForeignKeyViolations(in tableName: String) throws {
try checkForeignKeyViolations(from: foreignKeyViolations(in: tableName))
public func checkForeignKeys(in tableName: String) throws {
try checkForeignKeys(from: foreignKeyViolations(in: tableName))
}
private func foreignKeyViolations(in table: TableIdentifier) throws -> RecordCursor<ForeignKeyViolation> {
@@ -495,7 +495,7 @@ extension Database {
""")
}
private func checkForeignKeyViolations(from violations: RecordCursor<ForeignKeyViolation>) throws {
private func checkForeignKeys(from violations: RecordCursor<ForeignKeyViolation>) throws {
guard let violation = try violations.next() else {
return
}
+1 -1
View File
@@ -67,7 +67,7 @@ struct Migration {
// > then run PRAGMA foreign_key_check to verify that the
// > schema change did not break any foreign
// > key constraints.
try db.checkForeignKeyViolations()
try db.checkForeignKeys()
// > 11. Commit the transaction started in step 2.
return .commit
+8 -8
View File
@@ -873,7 +873,7 @@ class DatabaseMigratorTests : GRDBTestCase {
try migrator.migrate(dbQueue)
XCTFail("Expected error")
} catch DatabaseError.SQLITE_CONSTRAINT_FOREIGNKEY { }
try dbQueue.read { try $0.checkForeignKeyViolations() }
try dbQueue.read { try $0.checkForeignKeys() }
}
do {
var migrator = DatabaseMigrator()
@@ -885,7 +885,7 @@ class DatabaseMigratorTests : GRDBTestCase {
try migrator.migrate(dbQueue)
XCTFail("Expected error")
} catch DatabaseError.SQLITE_CONSTRAINT_FOREIGNKEY { }
try dbQueue.read { try $0.checkForeignKeyViolations() }
try dbQueue.read { try $0.checkForeignKeys() }
}
// Transient foreign key violation
@@ -896,7 +896,7 @@ class DatabaseMigratorTests : GRDBTestCase {
}
let dbQueue = try makeDatabaseQueue()
try migrator.migrate(dbQueue)
try dbQueue.read { try $0.checkForeignKeyViolations() }
try dbQueue.read { try $0.checkForeignKeys() }
}
do {
var migrator = DatabaseMigrator()
@@ -908,7 +908,7 @@ class DatabaseMigratorTests : GRDBTestCase {
try migrator.migrate(dbQueue)
XCTFail("Expected error")
} catch DatabaseError.SQLITE_CONSTRAINT_FOREIGNKEY { }
try dbQueue.read { try $0.checkForeignKeyViolations() }
try dbQueue.read { try $0.checkForeignKeys() }
}
}
@@ -935,7 +935,7 @@ class DatabaseMigratorTests : GRDBTestCase {
try migrator.migrate(dbQueue)
do {
// The unique opportunity for corrupt data!
try dbQueue.read { try $0.checkForeignKeyViolations() }
try dbQueue.read { try $0.checkForeignKeys() }
XCTFail("Expected foreign key violation")
} catch DatabaseError.SQLITE_CONSTRAINT_FOREIGNKEY { }
}
@@ -949,7 +949,7 @@ class DatabaseMigratorTests : GRDBTestCase {
try migrator.migrate(dbQueue)
XCTFail("Expected error")
} catch DatabaseError.SQLITE_CONSTRAINT_FOREIGNKEY { }
try dbQueue.read { try $0.checkForeignKeyViolations() }
try dbQueue.read { try $0.checkForeignKeys() }
}
// Transient foreign key violation
@@ -960,7 +960,7 @@ class DatabaseMigratorTests : GRDBTestCase {
}
let dbQueue = try makeDatabaseQueue()
try migrator.migrate(dbQueue)
try dbQueue.read { try $0.checkForeignKeyViolations() }
try dbQueue.read { try $0.checkForeignKeys() }
}
do {
var migrator = DatabaseMigrator().disablingDeferredForeignKeyChecks()
@@ -972,7 +972,7 @@ class DatabaseMigratorTests : GRDBTestCase {
try migrator.migrate(dbQueue)
XCTFail("Expected error")
} catch DatabaseError.SQLITE_CONSTRAINT_FOREIGNKEY { }
try dbQueue.read { try $0.checkForeignKeyViolations() }
try dbQueue.read { try $0.checkForeignKeys() }
}
}
+4 -4
View File
@@ -177,7 +177,7 @@ class ForeignKeyInfoTests: GRDBTestCase {
}
}
func testCheckForeignKeyViolations() throws {
func testCheckForeignKeys() throws {
try makeDatabaseQueue().writeWithoutTransaction { db in
try db.execute(sql: """
CREATE TABLE parent(id TEXT NOT NULL PRIMARY KEY);
@@ -187,7 +187,7 @@ class ForeignKeyInfoTests: GRDBTestCase {
""")
do {
try db.checkForeignKeyViolations()
try db.checkForeignKeys()
} catch let error as DatabaseError {
XCTAssertEqual(error.resultCode, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.extendedResultCode, .SQLITE_CONSTRAINT_FOREIGNKEY)
@@ -205,7 +205,7 @@ class ForeignKeyInfoTests: GRDBTestCase {
""")
do {
try db.checkForeignKeyViolations()
try db.checkForeignKeys()
} catch let error as DatabaseError {
XCTAssertEqual(error.resultCode, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.extendedResultCode, .SQLITE_CONSTRAINT_FOREIGNKEY)
@@ -223,7 +223,7 @@ class ForeignKeyInfoTests: GRDBTestCase {
""")
do {
try db.checkForeignKeyViolations()
try db.checkForeignKeys()
} catch let error as DatabaseError {
XCTAssertEqual(error.resultCode, .SQLITE_CONSTRAINT)
XCTAssertEqual(error.extendedResultCode, .SQLITE_CONSTRAINT_FOREIGNKEY)