diff --git a/Modules/SyncDatabase/Sources/SyncDatabase/SyncStatusTable.swift b/Modules/SyncDatabase/Sources/SyncDatabase/SyncStatusTable.swift index 0ac1d50f1..ed82bbe5a 100644 --- a/Modules/SyncDatabase/Sources/SyncDatabase/SyncStatusTable.swift +++ b/Modules/SyncDatabase/Sources/SyncDatabase/SyncStatusTable.swift @@ -35,7 +35,16 @@ struct SyncStatusTable { throw error } + // Rows are fetched during iteration, not by the query above. A failure partway + // through stops early and returns the rows read so far, which is indistinguishable + // from a complete result. let statuses = resultSet.mapToSet(statusWithRow) + guard !database.hadError() else { + let error = database.syncDatabaseError(operation: operation) + database.rollback() + throw error + } + guard !statuses.isEmpty else { database.commit() return statuses @@ -172,6 +181,10 @@ private extension SyncStatusTable { } let articleIDs = resultSet.mapToSet { $0.swiftString(forColumnIndex: 0) } + guard !database.hadError() else { + throw database.syncDatabaseError(operation: operation) + } + return articleIDs } } diff --git a/Modules/SyncDatabase/Tests/SyncDatabaseTests/SyncStatusTableTests.swift b/Modules/SyncDatabase/Tests/SyncDatabaseTests/SyncStatusTableTests.swift index 5336b7b43..8655ef7c5 100644 --- a/Modules/SyncDatabase/Tests/SyncDatabaseTests/SyncStatusTableTests.swift +++ b/Modules/SyncDatabase/Tests/SyncDatabaseTests/SyncStatusTableTests.swift @@ -59,4 +59,47 @@ import SyncDatabase let pendingStarredArticleIDs = try #require(await database.selectPendingStarredStatusArticleIDs()) #expect(pendingStarredArticleIDs == [articleID]) } + + /// The step check must not fire on a healthy read that simply found no rows. + /// A false positive there would stop syncing outright. + @Test func emptyQueueReadsCleanly() async throws { + #expect(try #require(await database.selectForProcessing()).isEmpty) + #expect(await database.selectPendingCount() == 0) + #expect(try #require(await database.selectPendingReadStatusArticleIDs()).isEmpty) + #expect(try #require(await database.selectPendingStarredStatusArticleIDs()).isEmpty) + + // And again once rows have been queued and then cleared. + let statuses = Set((0..<5).map { SyncStatus(articleID: "article-\($0)", key: .read, flag: true) }) + await database.insertStatuses(statuses) + _ = await database.selectForProcessing() + await database.deleteSelectedForProcessing(Set(statuses.map { $0.articleID }), key: .read) + + #expect(try #require(await database.selectForProcessing()).isEmpty) + #expect(await database.selectPendingCount() == 0) + } + + /// A read that stops on a SQLite error has to fail, not return the rows it got to + /// first. A short pending-statuses read is missing articleIDs that really are + /// pending, and the next refresh reverts exactly those edits. + /// + /// This covers failing on the first row. A failure partway through is the same bug + /// and the same fix, but there’s no way to force one at a chosen row. + @Test func aReadThatStopsOnAnErrorFails() async throws { + let folderURL = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) + try FileManager.default.createDirectory(at: folderURL, withIntermediateDirectories: true) + defer { + try? FileManager.default.removeItem(at: folderURL) + } + + let database = SyncDatabase(databasePath: folderURL.appendingPathComponent("Sync.sqlite3").path) + let articleIDs = (0..<10).map { "article-\($0)" } + await database.insertStatuses(Set(articleIDs.map { SyncStatus(articleID: $0, key: .read, flag: true) })) + + // Every call fails once the folder is gone, but the statements still prepare + // cleanly — the failure only shows up while stepping through the rows. + try FileManager.default.removeItem(at: folderURL) + + #expect(await database.selectForProcessing() == nil) + #expect(await database.selectPendingReadStatusArticleIDs() == nil) + } }