mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-30 04:28:44 +00:00
Drop Record.reload()
This commit is contained in:
1 parent
e5184d23c9
commit
fd96555a79
12 files changed
+53
-423
No files matched your search
@@ -1,6 +1,12 @@
|
||||
Release Notes
|
||||
=============
|
||||
|
||||
## 0.35.0
|
||||
|
||||
**Breaking Changes**
|
||||
|
||||
- `Record.reload` has been removed.
|
||||
|
||||
## 0.34.0
|
||||
|
||||
Released December 14, 2015
|
||||
|
||||
@@ -290,59 +290,6 @@ class MinimalPrimaryKeyRowIDTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Reload
|
||||
|
||||
func testReloadWithNotNilPrimaryKeyThatDoesNotMatchAnyRowThrowsRecordNotFound() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = MinimalRowID()
|
||||
record.id = 123456
|
||||
do {
|
||||
try record.reload(db)
|
||||
XCTFail("Expected PersistenceError.NotFound")
|
||||
} catch PersistenceError.NotFound {
|
||||
// Expected PersistenceError.NotFound
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReloadWithNotNilPrimaryKeyThatMatchesARowFetchesThatRow() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = MinimalRowID()
|
||||
try record.insert(db)
|
||||
try record.reload(db)
|
||||
|
||||
let row = Row.fetchOne(db, "SELECT * FROM minimalRowIDs WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.storedDatabaseDictionary {
|
||||
if let dbv = row[key] {
|
||||
XCTAssertEqual(dbv, value?.databaseValue ?? .Null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReloadAfterDeleteThrowsRecordNotFound() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = MinimalRowID()
|
||||
try record.insert(db)
|
||||
try record.delete(db)
|
||||
do {
|
||||
try record.reload(db)
|
||||
XCTFail("Expected PersistenceError.NotFound")
|
||||
} catch PersistenceError.NotFound {
|
||||
// Expected PersistenceError.NotFound
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetch With Key
|
||||
|
||||
func testFetchWithKeys() {
|
||||
|
||||
@@ -288,61 +288,6 @@ class MinimalPrimaryKeySingleTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Reload
|
||||
|
||||
func testReloadWithNotNilPrimaryKeyThatDoesNotMatchAnyRowThrowsRecordNotFound() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = MinimalSingle()
|
||||
record.UUID = "theUUID"
|
||||
do {
|
||||
try record.reload(db)
|
||||
XCTFail("Expected PersistenceError.NotFound")
|
||||
} catch PersistenceError.NotFound {
|
||||
// Expected PersistenceError.NotFound
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReloadWithNotNilPrimaryKeyThatMatchesARowFetchesThatRow() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = MinimalSingle()
|
||||
record.UUID = "theUUID"
|
||||
try record.insert(db)
|
||||
try record.reload(db)
|
||||
|
||||
let row = Row.fetchOne(db, "SELECT * FROM minimalSingles WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.storedDatabaseDictionary {
|
||||
if let dbv = row[key] {
|
||||
XCTAssertEqual(dbv, value?.databaseValue ?? .Null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReloadAfterDeleteThrowsRecordNotFound() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = MinimalSingle()
|
||||
record.UUID = "theUUID"
|
||||
try record.insert(db)
|
||||
try record.delete(db)
|
||||
do {
|
||||
try record.reload(db)
|
||||
XCTFail("Expected PersistenceError.NotFound")
|
||||
} catch PersistenceError.NotFound {
|
||||
// Expected PersistenceError.NotFound
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetch With Key
|
||||
|
||||
func testFetchWithKeys() {
|
||||
|
||||
@@ -299,62 +299,7 @@ class PrimaryKeyMultipleTests: GRDBTestCase {
|
||||
XCTAssertFalse(deleted)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Reload
|
||||
|
||||
func testReloadWithNotNilPrimaryKeyThatDoesNotMatchAnyRowThrowsRecordNotFound() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Citizenship(personName: "Arthur", countryName: "France", native: true)
|
||||
do {
|
||||
try record.reload(db)
|
||||
XCTFail("Expected PersistenceError.NotFound")
|
||||
} catch PersistenceError.NotFound {
|
||||
// Expected PersistenceError.NotFound
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReloadWithNotNilPrimaryKeyThatMatchesARowFetchesThatRow() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Citizenship(personName: "Arthur", countryName: "France", native: true)
|
||||
try record.insert(db)
|
||||
record.native = false
|
||||
try record.reload(db)
|
||||
|
||||
let row = Row.fetchOne(db, "SELECT * FROM citizenships WHERE personName = ? AND countryName = ?", arguments: [record.personName, record.countryName])!
|
||||
for (key, value) in record.storedDatabaseDictionary {
|
||||
if let dbv = row[key] {
|
||||
XCTAssertEqual(dbv, value?.databaseValue ?? .Null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReloadAfterDeleteThrowsRecordNotFound() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Citizenship(personName: "Arthur", countryName: "France", native: true)
|
||||
try record.insert(db)
|
||||
try record.delete(db)
|
||||
do {
|
||||
try record.reload(db)
|
||||
XCTFail("Expected PersistenceError.NotFound")
|
||||
} catch PersistenceError.NotFound {
|
||||
// Expected PersistenceError.NotFound
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetch With Key
|
||||
|
||||
@@ -361,59 +361,6 @@ class PrimaryKeyRowIDTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Reload
|
||||
|
||||
func testReloadWithNotNilPrimaryKeyThatDoesNotMatchAnyRowThrowsRecordNotFound() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Person(id: 123456, name: "Arthur")
|
||||
do {
|
||||
try record.reload(db)
|
||||
XCTFail("Expected PersistenceError.NotFound")
|
||||
} catch PersistenceError.NotFound {
|
||||
// Expected PersistenceError.NotFound
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReloadWithNotNilPrimaryKeyThatMatchesARowFetchesThatRow() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Person(name: "Arthur", age: 41)
|
||||
try record.insert(db)
|
||||
record.age = record.age! + 1
|
||||
try record.reload(db)
|
||||
|
||||
let row = Row.fetchOne(db, "SELECT * FROM persons WHERE id = ?", arguments: [record.id])!
|
||||
for (key, value) in record.storedDatabaseDictionary {
|
||||
if let dbv = row[key] {
|
||||
XCTAssertEqual(dbv, value?.databaseValue ?? .Null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReloadAfterDeleteThrowsRecordNotFound() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Person(name: "Arthur")
|
||||
try record.insert(db)
|
||||
try record.delete(db)
|
||||
do {
|
||||
try record.reload(db)
|
||||
XCTFail("Expected PersistenceError.NotFound")
|
||||
} catch PersistenceError.NotFound {
|
||||
// Expected PersistenceError.NotFound
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetch With Key
|
||||
|
||||
func testFetchWithKeys() {
|
||||
|
||||
@@ -295,59 +295,6 @@ class PrimaryKeySingleTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Reload
|
||||
|
||||
func testReloadWithNotNilPrimaryKeyThatDoesNotMatchAnyRowThrowsRecordNotFound() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Pet(UUID: "BobbyUUID", name: "Bobby")
|
||||
do {
|
||||
try record.reload(db)
|
||||
XCTFail("Expected PersistenceError.NotFound")
|
||||
} catch PersistenceError.NotFound {
|
||||
// Expected PersistenceError.NotFound
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReloadWithNotNilPrimaryKeyThatMatchesARowFetchesThatRow() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Pet(UUID: "BobbyUUID", name: "Bobby")
|
||||
try record.insert(db)
|
||||
record.name = "Carl"
|
||||
try record.reload(db)
|
||||
|
||||
let row = Row.fetchOne(db, "SELECT * FROM pets WHERE UUID = ?", arguments: [record.UUID])!
|
||||
for (key, value) in record.storedDatabaseDictionary {
|
||||
if let dbv = row[key] {
|
||||
XCTAssertEqual(dbv, value?.databaseValue ?? .Null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReloadAfterDeleteThrowsRecordNotFound() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Pet(UUID: "BobbyUUID", name: "Bobby")
|
||||
try record.insert(db)
|
||||
try record.delete(db)
|
||||
do {
|
||||
try record.reload(db)
|
||||
XCTFail("Expected PersistenceError.NotFound")
|
||||
} catch PersistenceError.NotFound {
|
||||
// Expected PersistenceError.NotFound
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetch With Key
|
||||
|
||||
func testFetchWithKeys() {
|
||||
|
||||
@@ -298,61 +298,6 @@ class PrimaryKeySingleWithReplaceConflictResolutionTests: GRDBTestCase {
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Reload
|
||||
|
||||
func testReloadWithNotNilPrimaryKeyThatDoesNotMatchAnyRowThrowsRecordNotFound() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Email()
|
||||
record.email = "me@domain.com"
|
||||
do {
|
||||
try record.reload(db)
|
||||
XCTFail("Expected PersistenceError.NotFound")
|
||||
} catch PersistenceError.NotFound {
|
||||
// Expected PersistenceError.NotFound
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReloadWithNotNilPrimaryKeyThatMatchesARowFetchesThatRow() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Email()
|
||||
record.email = "me@domain.com"
|
||||
try record.insert(db)
|
||||
try record.reload(db)
|
||||
|
||||
let row = Row.fetchOne(db, "SELECT * FROM emails WHERE email = ?", arguments: [record.email])!
|
||||
for (key, value) in record.storedDatabaseDictionary {
|
||||
if let dbv = row[key] {
|
||||
XCTAssertEqual(dbv, value?.databaseValue ?? .Null)
|
||||
} else {
|
||||
XCTFail("Missing column \(key) in fetched row")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testReloadAfterDeleteThrowsRecordNotFound() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let record = Email()
|
||||
record.email = "me@domain.com"
|
||||
try record.insert(db)
|
||||
try record.delete(db)
|
||||
do {
|
||||
try record.reload(db)
|
||||
XCTFail("Expected PersistenceError.NotFound")
|
||||
} catch PersistenceError.NotFound {
|
||||
// Expected PersistenceError.NotFound
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// MARK: - Fetch With Key
|
||||
|
||||
func testFetchWithKeys() {
|
||||
|
||||
@@ -57,8 +57,6 @@ class RecordEventsTests: GRDBTestCase {
|
||||
let record = EventRecorder()
|
||||
try record.insert(db)
|
||||
XCTAssertEqual(record.awakeFromFetchCount, 0)
|
||||
try record.reload(db)
|
||||
XCTAssertEqual(record.awakeFromFetchCount, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -130,23 +130,48 @@ class RecordEditedTests: GRDBTestCase {
|
||||
// record that is edited.
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let person = Person(name: "Arthur")
|
||||
try person.insert(db)
|
||||
|
||||
XCTAssertTrue(person.name != nil)
|
||||
person.name = "Bobby" // non-nil vs. non-nil
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
try person.reload(db)
|
||||
|
||||
XCTAssertTrue(person.name != nil)
|
||||
person.name = nil // non-nil vs. nil
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
try person.reload(db)
|
||||
|
||||
XCTAssertTrue(person.age == nil)
|
||||
person.age = 41 // nil vs. non-nil
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
try person.reload(db)
|
||||
do {
|
||||
let person = Person(name: "Arthur")
|
||||
try person.insert(db)
|
||||
XCTAssertTrue(person.name != nil)
|
||||
person.name = "Bobby" // non-nil vs. non-nil
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
}
|
||||
do {
|
||||
let person = Person(name: "Arthur")
|
||||
try person.insert(db)
|
||||
XCTAssertTrue(person.name != nil)
|
||||
person.name = nil // non-nil vs. nil
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
}
|
||||
do {
|
||||
let person = Person(name: "Arthur")
|
||||
try person.insert(db)
|
||||
XCTAssertTrue(person.age == nil)
|
||||
person.age = 41 // nil vs. non-nil
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testRecordIsNotEditedAfterSameValueChange() {
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
do {
|
||||
let person = Person(name: "Arthur")
|
||||
try person.insert(db)
|
||||
XCTAssertTrue(person.name != nil)
|
||||
person.name = "Arthur" // non-nil vs. non-nil
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
}
|
||||
do {
|
||||
let person = Person(name: "Arthur")
|
||||
try person.insert(db)
|
||||
XCTAssertTrue(person.age == nil)
|
||||
person.age = nil // nil vs. nil
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -179,22 +204,6 @@ class RecordEditedTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testRecordIsNotEditedAfterReload() {
|
||||
// After reload, a record is not edited.
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let person = Person(name: "Arthur", age: 41)
|
||||
try person.insert(db)
|
||||
|
||||
person.name = "Bobby"
|
||||
XCTAssertTrue(person.databaseEdited)
|
||||
|
||||
try person.reload(db)
|
||||
XCTAssertFalse(person.databaseEdited)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testRecordIsEditedAfterPrimaryKeyChange() {
|
||||
// After reload, a record is not edited.
|
||||
assertNoError {
|
||||
@@ -407,32 +416,6 @@ class RecordEditedTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testChangesAfterReload() {
|
||||
// After reload, a record is not edited.
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
let person = Person(name: "Arthur", age: 41)
|
||||
try person.insert(db)
|
||||
|
||||
person.name = "Bobby"
|
||||
let changes = person.databaseChanges
|
||||
XCTAssertEqual(changes.count, 1)
|
||||
for (column, (old: old, new: new)) in changes {
|
||||
switch column {
|
||||
case "name":
|
||||
XCTAssertEqual(old, "Arthur".databaseValue)
|
||||
XCTAssertEqual(new, "Bobby".databaseValue)
|
||||
default:
|
||||
XCTFail("Unexpected column: \(column)")
|
||||
}
|
||||
}
|
||||
|
||||
try person.reload(db)
|
||||
XCTAssertEqual(person.databaseChanges.count, 0)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testChangesAfterPrimaryKeyChange() {
|
||||
// After reload, a record is not edited.
|
||||
assertNoError {
|
||||
|
||||
@@ -135,22 +135,6 @@ class RecordUpdateFromRowTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func testUpdateFromRowForReload() {
|
||||
let parisLatitude = 48.8534100
|
||||
let parisLongitude = 2.3488000
|
||||
assertNoError {
|
||||
try dbQueue.inDatabase { db in
|
||||
try db.execute("INSERT INTO placemarks (name, latitude, longitude) VALUES (?,?,?)", arguments: ["Paris", parisLatitude, parisLongitude])
|
||||
let paris = Placemark.fetchOne(db, "SELECT * FROM placemarks")!
|
||||
paris.coordinate = nil
|
||||
try paris.reload(db)
|
||||
XCTAssertEqual(paris.name!, "Paris")
|
||||
XCTAssertEqual(paris.coordinate!.latitude, parisLatitude)
|
||||
XCTAssertEqual(paris.coordinate!.longitude, parisLongitude)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testCopy() {
|
||||
let parisLatitude = 48.8534100
|
||||
let parisLongitude = 2.3488000
|
||||
|
||||
@@ -33,7 +33,7 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
}
|
||||
|
||||
/// Don't call this method directly. It is called after a Record has been
|
||||
/// fetched or reloaded.
|
||||
/// fetched.
|
||||
///
|
||||
/// *Important*: subclasses must invoke super's implementation.
|
||||
///
|
||||
@@ -60,8 +60,8 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
|
||||
/// Returns the name of a database table.
|
||||
///
|
||||
/// This table name is required by the insert, update, save, delete, exists
|
||||
/// and reload methods.
|
||||
/// This table name is required by the insert, update, save, delete,
|
||||
/// and exists methods.
|
||||
///
|
||||
/// class Person : Record {
|
||||
/// override class func databaseTableName() -> String {
|
||||
@@ -174,7 +174,7 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
/// been saved.
|
||||
///
|
||||
/// This flag is purely informative, and does not prevent insert(),
|
||||
/// update(), save() and reload() from performing their database queries.
|
||||
/// update(), and save() from performing their database queries.
|
||||
///
|
||||
/// A record is *edited* if its *storedDatabaseDictionary* has been changed
|
||||
/// since last database synchronization (fetch, update, insert). Comparison
|
||||
@@ -313,22 +313,6 @@ public class Record : RowConvertible, DatabaseTableMapping, DatabasePersistable
|
||||
databaseEdited = true
|
||||
return deleted
|
||||
}
|
||||
|
||||
/// Executes a SELECT statetement.
|
||||
///
|
||||
/// On success, this method sets the *databaseEdited* flag to false.
|
||||
///
|
||||
/// - parameter db: A Database.
|
||||
/// - throws: PersistenceError.NotFound is thrown if the primary key does
|
||||
/// not match any row in the database and record could not be reloaded.
|
||||
public func reload(db: Database) throws {
|
||||
let statement = DataMapper(db, self).reloadStatement()
|
||||
guard let row = Row.fetchOne(statement) else {
|
||||
throw PersistenceError.NotFound(self)
|
||||
}
|
||||
updateFromRow(row)
|
||||
awakeFromFetch(row)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -13,7 +13,6 @@
|
||||
- [ ] Row.value("named") should returns nil if column is not there.
|
||||
- [ ] Conversion should crash when type mismatch (String.fetch("SELECT 1"))
|
||||
- [ ] Record:
|
||||
- [ ] Remove reloading
|
||||
- [ ] Remove init(row)
|
||||
- [ ] Expose fromRow (fatalError("subclass must implement") in Record)
|
||||
- [ ] Expose didInsertWithRowID(_:forColumn:)
|
||||
|
||||
Reference in new issue
Block a user