Use fatalError() instead of precondition() so that safety checks survive Release configuration.

This commit is contained in:
Gwendal Roué committed 2015-09-12 14:36:06 +02:00
1 parent 4cbac39dfc
commit 75bb735e98
7 files changed
+61 -45

No files matched your search

@@ -1,37 +1,37 @@
import XCTest
@testable import GRDB
class DatabaseErrorFromStatementCreationTests: GRDBTestCase {
func testDatabaseErrorThrownBySelectStatementContainSQL() {
dbQueue.inDatabase { db in
do {
let _ = try SelectStatement(database: db, sql: "SELECT * FROM blah")
XCTFail()
} catch let error as DatabaseError {
XCTAssertEqual(error.code, 1)
XCTAssertEqual(error.message!, "no such table: blah")
XCTAssertEqual(error.sql!, "SELECT * FROM blah")
XCTAssertEqual(error.description, "SQLite error 1 with statement `SELECT * FROM blah`: no such table: blah")
} catch {
XCTFail("\(error)")
}
}
}
func testDatabaseErrorThrownByUpdateStatementContainSQL() {
dbQueue.inDatabase { db in
do {
let _ = try UpdateStatement(database: db, sql: "UPDATE blah SET id = 12")
XCTFail()
} catch let error as DatabaseError {
XCTAssertEqual(error.code, 1)
XCTAssertEqual(error.message!, "no such table: blah")
XCTAssertEqual(error.sql!, "UPDATE blah SET id = 12")
XCTAssertEqual(error.description, "SQLite error 1 with statement `UPDATE blah SET id = 12`: no such table: blah")
} catch {
XCTFail("\(error)")
}
}
}
}
//import XCTest
//@testable import GRDB
//
//class DatabaseErrorFromStatementCreationTests: GRDBTestCase {
//
// func testDatabaseErrorThrownBySelectStatementContainSQL() {
// dbQueue.inDatabase { db in
// do {
// let _ = try SelectStatement(database: db, sql: "SELECT * FROM blah")
// XCTFail()
// } catch let error as DatabaseError {
// XCTAssertEqual(error.code, 1)
// XCTAssertEqual(error.message!, "no such table: blah")
// XCTAssertEqual(error.sql!, "SELECT * FROM blah")
// XCTAssertEqual(error.description, "SQLite error 1 with statement `SELECT * FROM blah`: no such table: blah")
// } catch {
// XCTFail("\(error)")
// }
// }
// }
//
// func testDatabaseErrorThrownByUpdateStatementContainSQL() {
// dbQueue.inDatabase { db in
// do {
// let _ = try UpdateStatement(database: db, sql: "UPDATE blah SET id = 12")
// XCTFail()
// } catch let error as DatabaseError {
// XCTAssertEqual(error.code, 1)
// XCTAssertEqual(error.message!, "no such table: blah")
// XCTAssertEqual(error.sql!, "UPDATE blah SET id = 12")
// XCTAssertEqual(error.description, "SQLite error 1 with statement `UPDATE blah SET id = 12`: no such table: blah")
// } catch {
// XCTFail("\(error)")
// }
// }
// }
//}
+3 -1
View File
@@ -384,7 +384,9 @@ public final class Database {
}
func assertValid() {
precondition(databaseQueueID == nil || databaseQueueID == dispatch_get_specific(DatabaseQueue.databaseQueueIDKey), "Database was not used on the correct queue. Execute your statements inside DatabaseQueue.inDatabase() or DatabaseQueue.inTransaction(). Consider using fetchAll() method if this error message happens when iterating the result of the fetch() method.")
guard databaseQueueID == nil || databaseQueueID == dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) else {
fatalError("Database was not used on the correct queue. Execute your statements inside DatabaseQueue.inDatabase() or DatabaseQueue.inTransaction(). Consider using fetchAll() method if this error message happens when iterating the result of the fetch() method.")
}
}
}
+3 -1
View File
@@ -235,7 +235,9 @@ public final class DatabaseQueue {
//
// I try not to ship half-baked solutions, so until a complete solution
// is found to this problem, I prefer totally disabling reentrancy.
precondition(databaseQueueID != dispatch_get_specific(DatabaseQueue.databaseQueueIDKey), "DatabaseQueue.inDatabase(_:) or DatabaseQueue.inTransaction(_:) was called reentrantly, which would lead to a deadlock.")
guard databaseQueueID != dispatch_get_specific(DatabaseQueue.databaseQueueIDKey) else {
fatalError("DatabaseQueue.inDatabase(_:) or DatabaseQueue.inTransaction(_:) was called reentrantly, which would lead to a deadlock.")
}
return try DatabaseQueue.dispatchSync(queue, block: block)
}
@@ -38,7 +38,9 @@ extension DatabaseTableMapping {
// Fail early if database table has not one column in its primary key
let columns = primaryKey.columns
precondition(columns.count == 1, "Primary key of table \(databaseTableName.quotedDatabaseIdentifier) is not made of a single column. See \(self).databaseTableName()")
guard columns.count == 1 else {
fatalError("Primary key of table \(databaseTableName.quotedDatabaseIdentifier) is not made of a single column. See \(self).databaseTableName()")
}
guard let primaryKeyValue = primaryKeyValue else {
return nil
@@ -64,7 +66,9 @@ extension DatabaseTableMapping {
}
// Fail early if key is empty.
precondition(dictionary.count > 0, "Invalid empty key")
guard dictionary.count > 0 else {
fatalError("Invalid empty key")
}
let whereSQL = dictionary.keys.map { column in "\(column.quotedDatabaseIdentifier)=?" }.joinWithSeparator(" AND ")
let sql = "SELECT * FROM \(databaseTableName.quotedDatabaseIdentifier) WHERE \(whereSQL)"
+6 -2
View File
@@ -56,7 +56,9 @@ public class Statement {
switch code {
case SQLITE_OK:
precondition(consumedCharactersCount == sqlCodeUnits.count, "Invalid SQL string: multiple statements found. To execute multiple statements, use Database.executeMultiStatement() instead.")
guard consumedCharactersCount == sqlCodeUnits.count else {
fatalError("Invalid SQL string: multiple statements found. To execute multiple statements, use Database.executeMultiStatement() instead.")
}
default:
throw DatabaseError(code: code, message: database.lastErrorMessage, sql: sql)
}
@@ -95,7 +97,9 @@ public class Statement {
// Exposed for StatementArguments. Don't make this one public unless we keep the arguments property in sync.
final func bind(value: DatabaseValueConvertible?, forKey key: String) {
let index = Int(sqlite3_bind_parameter_index(sqliteStatement, ":\(key)"))
precondition(index > 0, "Key not found in SQLite statement: `:\(key)`")
guard index > 0 else {
fatalError("Key not found in SQLite statement: `:\(key)`")
}
bind(value, atIndex: index)
}
+3 -1
View File
@@ -62,7 +62,9 @@ public struct DatabaseMigrator {
- parameter block: The migration block that performs SQL statements.
*/
public mutating func registerMigration(identifier: String, _ block: (db: Database) throws -> Void) {
precondition(migrations.map({ $0.identifier }).indexOf(identifier) == nil, "Already registered migration: \"\(identifier)\"")
guard migrations.map({ $0.identifier }).indexOf(identifier) == nil else {
fatalError("Already registered migration: \"\(identifier)\"")
}
migrations.append(Migration(identifier: identifier, block: block))
}
+3 -1
View File
@@ -103,7 +103,9 @@ final class DataMapper {
// Fail early if storedDatabaseDictionary is empty
let storedDatabaseDictionary = storable.storedDatabaseDictionary
precondition(storedDatabaseDictionary.count > 0, "Invalid empty dictionary returned from \(storable.dynamicType).storedDatabaseDictionary")
guard storedDatabaseDictionary.count > 0 else {
fatalError("Invalid empty dictionary returned from \(storable.dynamicType).storedDatabaseDictionary")
}
self.db = db
self.storable = storable