From 7afd5ffb318d5192593fa474753407317a3a4c9c Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 12:31:25 +0700 Subject: [PATCH 1/3] perf(ios): show table rows before the count, open the remembered PostgreSQL database in one dial, keep one row store and skip unchanged launch and sync work --- .../fix-issue/scripts/lib/platform-apple.sh | 11 +- .github/workflows/ios-tests.yml | 8 +- apple/CONTRIBUTING.md | 2 +- .../TableProMobile/AppState.swift | 19 +- .../Coordinators/ConnectionSessionModel.swift | 184 +++++++++-- .../Coordinators/SessionRedialer.swift | 33 +- .../Drivers/PostgreSQLDriver.swift | 10 +- .../TableProMobile/Helpers/IndexedRow.swift | 9 - .../TableProMobile/Localizable.xcstrings | 9 + .../TableProMobile/Models/RowWindow.swift | 13 + .../Models/StreamingResultBuffer.swift | 15 +- .../Platform/ConnectionLibraryPublisher.swift | 41 ++- .../Platform/KeychainSecureStore.swift | 48 +-- .../Platform/SpotlightConnectionIndex.swift | 58 +++- .../Sync/IOSSyncCoordinator.swift | 77 ++++- .../TableProMobile/TableProMobileApp.swift | 2 +- .../ViewModels/DataBrowserViewModel.swift | 79 +++-- .../ViewModels/QueryEditorViewModel.swift | 37 ++- .../ViewModels/RowDetailViewModel.swift | 8 +- .../Views/Components/RowGrid.swift | 36 ++- .../Views/DataBrowserView.swift | 91 ++++-- .../Views/QueryEditorView.swift | 68 ++-- .../TableProMobile/Views/RowDetailView.swift | 10 +- .../ConnectionCoordinatorStatusTests.swift | 2 + .../ConnectionCoordinatorSwitchTests.swift | 294 +++++++++++++++++- .../ConnectionLibraryPublisherTests.swift | 114 ++++++- .../DataBrowserDriverBuiltTests.swift | 50 ++- .../DataBrowserViewModelTests.swift | 97 ++++-- .../Mocks/InMemoryKeychainItems.swift | 30 +- .../Mocks/MockDatabaseDriver.swift | 19 +- .../Mocks/MockQueryBuildingDriver.swift | 2 + .../Mocks/RecordingSearchIndex.swift | 21 +- .../Mocks/RecordingSyncTransport.swift | 16 +- .../Onboarding/AppStateFixture.swift | 4 +- .../Onboarding/AppStateLibraryTests.swift | 4 +- .../Platform/KeychainSecureStoreTests.swift | 66 ++++ .../QueryEditorViewModelTests.swift | 40 ++- .../RowDetailViewModelTests.swift | 16 + .../StreamingResultBufferTests.swift | 16 +- .../Support/RowWindow+Shown.swift | 9 + .../Sync/IOSSyncActivationTests.swift | 168 ++++++++++ .../TableProWidget/Localizable.xcstrings | 286 +++++++++++++++++ apple/TableProMobile/project.yml | 2 +- apple/scripts/ci/check-string-catalogs.py | 1 + apple/scripts/localization.py | 10 +- apple/scripts/test_localization.py | 25 ++ 46 files changed, 1890 insertions(+), 270 deletions(-) create mode 100644 apple/TableProMobile/TableProMobileTests/Support/RowWindow+Shown.swift create mode 100644 apple/TableProMobile/TableProMobileTests/Sync/IOSSyncActivationTests.swift create mode 100644 apple/TableProMobile/TableProWidget/Localizable.xcstrings diff --git a/.claude/skills/fix-issue/scripts/lib/platform-apple.sh b/.claude/skills/fix-issue/scripts/lib/platform-apple.sh index a1fb1c2acd..00c039a90d 100644 --- a/.claude/skills/fix-issue/scripts/lib/platform-apple.sh +++ b/.claude/skills/fix-issue/scripts/lib/platform-apple.sh @@ -633,10 +633,13 @@ platform_step() { code=0 failed="" unbuilt="" - for target in mac ios; do - echo "== localization.py plugins --target $target" >> "$log" - (cd "$APPLE_ROOT" && python3 scripts/localization.py plugins --target "$target") >> "$log" 2>&1 \ - || { code=1; failed="$failed plugins:$target"; } + # The iOS widget extension has its own catalog and links no package. + for target in mac ios widget; do + if [ "$target" != widget ]; then + echo "== localization.py plugins --target $target" >> "$log" + (cd "$APPLE_ROOT" && python3 scripts/localization.py plugins --target "$target") >> "$log" 2>&1 \ + || { code=1; failed="$failed plugins:$target"; } + fi echo "== localization.py extracted --target $target" >> "$log" (cd "$APPLE_ROOT" && python3 scripts/localization.py extracted --target "$target") >> "$log" 2>&1 case $? in diff --git a/.github/workflows/ios-tests.yml b/.github/workflows/ios-tests.yml index 2eee442713..988ae229f0 100644 --- a/.github/workflows/ios-tests.yml +++ b/.github/workflows/ios-tests.yml @@ -79,9 +79,13 @@ jobs: key: ${{ steps.spm.outputs.cache-primary-key }} # Only Xcode adds a new string to the catalog. A command-line build extracts it and moves on. - - name: Check the app's strings reach its catalog + # The widget extension has its own catalog. + - name: Check the app's strings reach its catalogs if: ${{ !cancelled() }} - run: python3 scripts/localization.py extracted --target ios + run: | + set -euo pipefail + python3 scripts/localization.py extracted --target ios + python3 scripts/localization.py extracted --target widget # --parallel is what makes swift test write the XCTest xunit file. Without it only the Swift # Testing one appears, and TableProOracle's cases are all XCTest. diff --git a/apple/CONTRIBUTING.md b/apple/CONTRIBUTING.md index cc18b17b8f..c33139ff66 100644 --- a/apple/CONTRIBUTING.md +++ b/apple/CONTRIBUTING.md @@ -150,7 +150,7 @@ xcodebuild -importLocalizations -project TablePro.xcodeproj -localizationPath Lo scripts/localization.py status # what is translated, per language ``` -The export builds the app first, so run `scripts/download-libs.sh` and `scripts/generate-project.sh` once before it. Pass an existing language code to fill gaps, or a new one to start a language. Commit the catalog, not the export: `Localization/` is ignored. The iPhone and iPad app has its own catalog: run the same commands on `TableProMobile/TableProMobile.xcodeproj` with the `TableProMobile` scheme. Plugin and package strings, and strings a command-line build added: [`AGENTS.md`](AGENTS.md), Every change 3. +The export builds the app first, so run `scripts/download-libs.sh` and `scripts/generate-project.sh` once before it. Pass an existing language code to fill gaps, or a new one to start a language. Commit the catalog, not the export: `Localization/` is ignored. The iPhone and iPad app has its own catalogs, one for the app and one for its widget: run the same commands on `TableProMobile/TableProMobile.xcodeproj` with the `TableProMobile` scheme, and `status` with `--target ios` or `--target widget`. Plugin and package strings, and strings a command-line build added: [`AGENTS.md`](AGENTS.md), Every change 3. ## Troubleshooting diff --git a/apple/TableProMobile/TableProMobile/AppState.swift b/apple/TableProMobile/TableProMobile/AppState.swift index f3d01616b8..7520b2040e 100644 --- a/apple/TableProMobile/TableProMobile/AppState.swift +++ b/apple/TableProMobile/TableProMobile/AppState.swift @@ -73,7 +73,7 @@ final class AppState { driverFactory injectedDriverFactory: (any DriverFactory)? = nil ) { self.sampleInstaller = sampleInstaller - self.libraryPublisher = libraryPublisher ?? .live() + self.libraryPublisher = libraryPublisher ?? .live(defaults: defaults) self.localDatabaseFiles = localDatabaseFiles localDatabaseFiles.container.recordCurrentContainer() onboarding = OnboardingPreferences(defaults: defaults) @@ -118,8 +118,7 @@ final class AppState { guard !TestRuntime.isActive else { return } if loadStatus == .ready { - KeychainSecureStore.cleanOrphanedCredentials(validConnectionIds: Set(connections.map(\.id))) - CertificateMaterializer().releaseAll(except: Set(connections.map(\.id))) + removeWhatDeletedConnectionsLeft() Task { publishLibrary() } @@ -128,6 +127,20 @@ final class AppState { connectSync() } + // The Keychain and certificate folder listings block, so they run off the main actor at utility + // priority, against the library as it stands once the Keychain listing returns, not at launch. + private func removeWhatDeletedConnectionsLeft() { + Task.detached(priority: .utility) { [self] in + await KeychainSecureStore.cleanOrphanedCredentials { self.loadedConnectionIds } + guard let kept = await loadedConnectionIds else { return } + CertificateMaterializer().releaseAll(except: kept) + } + } + + private var loadedConnectionIds: Set? { + loadStatus == .ready ? Set(connections.map(\.id)) : nil + } + // Separate from init, which skips it under tests, so a test can run the wiring the app runs. func connectSync() { syncCoordinator.onConnectionsChanged = { [weak self] merged in diff --git a/apple/TableProMobile/TableProMobile/Coordinators/ConnectionSessionModel.swift b/apple/TableProMobile/TableProMobile/Coordinators/ConnectionSessionModel.swift index a1928c4a55..73fc79ed4b 100644 --- a/apple/TableProMobile/TableProMobile/Coordinators/ConnectionSessionModel.swift +++ b/apple/TableProMobile/TableProMobile/Coordinators/ConnectionSessionModel.swift @@ -5,6 +5,7 @@ import TableProConnectionLibrary import TableProDatabase import TableProLogRedaction import TableProModels +import TableProPluginKit import TableProSQLGrammar // What a window shows of its connection: the model's, or what it showed when the session moved under an unsaved edit. @@ -316,30 +317,51 @@ final class ConnectionSessionModel { Telemetry.shared.connectionAttempted(databaseType: record.type.rawValue) do { - let outcome = try await appState.sessionRedialer.redial( - record, - schema: nil, - prompter: lead(of: attempt)?.prompts, - joining: allowSignIn, - consumer: self, - stillWanted: { [weak self] in self?.isCurrent(attempt) == true } - ) - let newSession = outcome.session - let load = await loadTables(in: nil, on: newSession) - if let refusal = load.failure { throw refusal } + let saved = record + let target = openTarget(restoring: wanted) + let landed: OpenLanding + var restoring = wanted + var refused: RefusedDatabase? + do { + landed = try await dialForOpen( + target, schema: schemaRestored(by: target, restoring: wanted), by: attempt, joining: allowSignIn + ) + } catch let thrown { + let listRefused = thrown as? TableListRefused + let error = listRefused?.error ?? thrown + // A sign-in is the connection's, not the database's: the saved database would fail the same way. + guard target.database != saved.database, isCurrent(attempt), + listRefused != nil || Self.mayBlameDatabase(error), + SignInOffer.method(for: error, connection: saved, secureStore: appState.secureStore) == nil + else { throw error } + Self.logger.warning("Failed to open remembered database \(target.database, privacy: .private(mask: .hash)): \(LogRedaction.publicDescription(of: error), privacy: .public) \(error.localizedDescription, privacy: .private)") + // A dial that landed and then refused its table list leaves a session to tear down first. + await appState.connectionManager.disconnect(connectionId) + guard isCurrent(attempt) else { return } + landed = try await dialForOpen(saved, schema: nil, by: attempt, joining: false) + restoring.database = saved.database + refused = RefusedDatabase(name: target.database, error: error) + } guard isCurrent(attempt) else { return } + let newSession = landed.outcome.session let placement = adoptedPlacement(of: newSession, includingDatabase: true) enter(.adopt(newSession, by: attempt)) - commit(load, at: placement, onFailure: .keep) - if outcome.dialedHere { + commit(landed.load, at: placement, onFailure: .keep) + if landed.outcome.dialedHere { appState.bumpSessionRevision(connectionId) } - guard await finishOpen(attempt, restoring: wanted) else { return } + guard await finishOpen( + attempt, + restoring: restoring, + listedSchemas: landed.outcome.schemas, + refused: refused + ) else { return } Telemetry.shared.connectionSucceeded(databaseType: record.type.rawValue) for scene in consumers(of: attempt) { scene.navigateToPendingTable() } - } catch { + } catch let thrown { + let error = (thrown as? TableListRefused)?.error ?? thrown guard isCurrent(attempt) else { return } var failure = error // A sign-in that expired is recoverable, so offer it once and retry rather than @@ -362,11 +384,100 @@ final class ConnectionSessionModel { } } + // Where the connection was, so the first table list is already the right one. Only PostgreSQL and Redshift, which + // need a second connection to change database. The others dial the saved one and move with USE, as before, so their + // refusals, which the app cannot always tell apart from a failed sign-in, never decide between two dials. + private func openTarget(restoring wanted: Placement) -> DatabaseConnection { + guard redialsToSwitchDatabase, !wanted.database.isEmpty else { return record } + var target = record + target.database = wanted.database + return target + } + + // A remembered schema belongs to the remembered database, so the dial restores it only when it lands there. + private func schemaRestored(by target: DatabaseConnection, restoring wanted: Placement) -> String? { + guard supportsSchemas else { return nil } + guard !supportsDatabaseSwitching || wanted.database.isEmpty || target.database == wanted.database else { + return nil + } + return wanted.schema + } + + private struct OpenLanding { + let outcome: SessionRedialer.Outcome + let load: CatalogLoad + } + + // The database the open could not go back to, and why. + private struct RefusedDatabase { + let name: String + let error: any Error + } + + nonisolated private struct TableListRefused: Error { + let error: any Error + } + + // A PostgreSQL dial that failed on the way, or on the credentials, fails the same way for the saved database, and + // a second one would ask the same host key question or code again, wait out another timeout or count another failed + // login. Those come typed, or as libpq's own English text; anything else the server answered, in whatever language + // it speaks, may be about the database, so the open tries the saved one. + nonisolated static func mayBlameDatabase(_ error: any Error) -> Bool { + if error is CancellationError || error is SSHTunnelError || error is SSLHandshakeError + || error is LocalNetworkPermissionError || error is ConnectionError || error is URLError + || error is POSIXError { + return false + } + if case PostgreSQLError.unreachable = error { return false } + let message = error.localizedDescription.lowercased() + return !notAboutTheDatabase.contains { message.contains($0) } + } + + private nonisolated static let notAboutTheDatabase = [ + "password authentication failed", "authentication failed", "fe_sendauth", "no password supplied", + "could not connect", "connection refused", "timeout expired", "timed out", "could not translate host name", + "network is unreachable", "no route to host", "server closed the connection", "connection reset", + "lost connection", "ssl error", "ssl syscall", "server does not support ssl", + ] + + // A table list the new session refuses fails the dial, as it fails a database switch. + private func dialForOpen( + _ target: DatabaseConnection, + schema: String?, + by attempt: ConnectionAttempt, + joining: Bool + ) async throws -> OpenLanding { + let outcome = try await appState.sessionRedialer.redial( + target, + schema: schema, + onlyIfListed: true, + prompter: lead(of: attempt)?.prompts, + joining: joining, + consumer: self, + stillWanted: { [weak self] in self?.isCurrent(attempt) == true } + ) + let load = await loadTables(in: nil, on: outcome.session) + if let refusal = load.failure { throw TableListRefused(error: refusal) } + return OpenLanding(outcome: outcome, load: load) + } + // The open landed where the session is; this puts it back where the connection was, through the asking window. + // A fresh open dialed there already, so this only lists, unless that dial fell back to the saved database. @discardableResult - private func finishOpen(_ attempt: ConnectionAttempt, restoring wanted: Placement) async -> Bool { - await loadDatabases(restoring: wanted.database, from: lead(of: attempt), inside: attempt) - await loadSchemas(restoring: wanted.schema, from: lead(of: attempt)) + private func finishOpen( + _ attempt: ConnectionAttempt, + restoring wanted: Placement, + listedSchemas: [String]? = nil, + refused: RefusedDatabase? = nil + ) async -> Bool { + let landedOn = (driver: session.map { ObjectIdentifier($0.driver) }, database: activeDatabase) + let databases = await loadDatabases(restoring: wanted.database, from: lead(of: attempt), inside: attempt) + if let refused { + settleRefusedDatabase(refused, listed: databases, reportingTo: lead(of: attempt)) + } + // The dial's schema list belongs to the session it read, which a database switch above replaces or moves. + let stayed = session.map { ObjectIdentifier($0.driver) } == landedOn.driver && activeDatabase == landedOn.database + await loadSchemas(restoring: wanted.schema, from: lead(of: attempt), listed: stayed ? listedSchemas : nil) await refreshTransaction() // A database restore that failed both ways left the open lost, and it stays lost. guard enter(.opened(attempt)) else { return false } @@ -375,6 +486,26 @@ final class ConnectionSessionModel { return true } + // A database the server still lists failed to open, so the alert says why, as a failed switch does. One it no + // longer lists is gone: the open stays quiet, as it always did for one, and forgets it so the next open skips it. + private func settleRefusedDatabase( + _ refused: RefusedDatabase, + listed: [String]?, + reportingTo scene: ConnectionCoordinator? + ) { + guard let listed, !listed.contains(refused.name) else { + scene?.presentFailure( + title: ConnectionCoordinator.switchDatabaseFailedTitle, + message: classify(refused.error, operation: "switchDatabase").message + ) + return + } + let key = ConnectionDefaultsKey.lastDB.name(for: connectionId) + if UserDefaults.standard.string(forKey: key) == refused.name { + UserDefaults.standard.removeObject(forKey: key) + } + } + // A transaction the server aborted refuses catalog reads, yet the session still holds the user's work. private func keepsRefusingSession( _ existing: ConnectionSession, @@ -1164,12 +1295,13 @@ final class ConnectionSessionModel { // `wanted` is where the connection was; the session may have opened elsewhere, and the asking window switches back, // as a step of `open` when an open asks. + @discardableResult private func loadDatabases( restoring wanted: String, from scene: ConnectionCoordinator?, inside open: ConnectionAttempt? = nil - ) async { - guard let session, supportsDatabaseSwitching else { return } + ) async -> [String]? { + guard let session, supportsDatabaseSwitching else { return nil } do { let listed = try await session.driver.fetchDatabases() catalog.databases = listed @@ -1187,15 +1319,23 @@ final class ConnectionSessionModel { await scene?.switchDatabase(to: reported, inside: open) } } + return listed } catch { Self.logger.warning("Failed to load databases: \(LogRedaction.publicDescription(of: error), privacy: .public) \(error.localizedDescription, privacy: .private)") + return nil } } - private func loadSchemas(restoring wanted: String, from scene: ConnectionCoordinator?) async { + // `known` is a list the dial already read on this session. + private func loadSchemas(restoring wanted: String, from scene: ConnectionCoordinator?, listed known: [String]? = nil) async { guard let session, supportsSchemas else { return } do { - let listed = try await session.driver.fetchSchemas() + let listed: [String] + if let known { + listed = known + } else { + listed = try await session.driver.fetchSchemas() + } catalog.schemas = listed let currentSchema = session.driver.currentSchema ?? "public" if listed.contains(wanted), wanted != currentSchema { diff --git a/apple/TableProMobile/TableProMobile/Coordinators/SessionRedialer.swift b/apple/TableProMobile/TableProMobile/Coordinators/SessionRedialer.swift index 6c26522c89..2826f06557 100644 --- a/apple/TableProMobile/TableProMobile/Coordinators/SessionRedialer.swift +++ b/apple/TableProMobile/TableProMobile/Coordinators/SessionRedialer.swift @@ -12,11 +12,18 @@ final class SessionRedialer { struct Outcome { let session: ConnectionSession let dialedHere: Bool + // What the dial read to check the schema it was asked to restore; nil when it read nothing. + let schemas: [String]? + } + + private struct Landed: Sendable { + let session: ConnectionSession + let schemas: [String]? } private struct Dial { let id: UUID - let task: Task + let task: Task let prompter: SharedDialPrompter var consumers: Set } @@ -31,9 +38,12 @@ final class SessionRedialer { } // The schema goes back inside the dial, so a window that joins or adopts it never reads the default schema. + // `onlyIfListed` is for a schema remembered from an earlier session, which may since be dropped or belong to + // another database: PostgreSQL and SQL Server would move to it anyway and list no tables. func redial( _ connection: DatabaseConnection, schema: String?, + onlyIfListed: Bool = false, prompter: ConnectionPromptQueue?, joining: Bool = true, consumer: AnyObject? = nil, @@ -44,7 +54,8 @@ final class SessionRedialer { if joining { if let consumerId { dials[connection.id]?.consumers.insert(consumerId) } inFlight.prompter.add(consumerId, prompter) - return Outcome(session: try await inFlight.task.value, dialedHere: false) + let landed = try await inFlight.task.value + return Outcome(session: landed.session, dialedHere: false, schemas: landed.schemas) } // A database switch needs its own target, and a retry after a sign-in its new credentials. _ = try? await inFlight.task.value @@ -61,11 +72,12 @@ final class SessionRedialer { if dials[connection.id]?.id == dialId { dials[connection.id] = nil } } let session = try await manager.connect(connection, prompter: shared) - await Self.restore(schema, on: session.driver) - return session + let schemas = await Self.restore(schema, onlyIfListed: onlyIfListed, on: session.driver) + return Landed(session: session, schemas: schemas) } dials[connection.id] = Dial(id: dialId, task: task, prompter: shared, consumers: consumerId.map { [$0] } ?? []) - return Outcome(session: try await task.value, dialedHere: true) + let landed = try await task.value + return Outcome(session: landed.session, dialedHere: true, schemas: landed.schemas) } // A window that goes away stops waiting; the dial is cancelled only when no other window still waits on it, so @@ -97,13 +109,20 @@ final class SessionRedialer { } } - private static func restore(_ schema: String?, on driver: any DatabaseDriver) async { - guard let schema, let current = driver.currentSchema, current != schema else { return } + private static func restore(_ schema: String?, onlyIfListed: Bool, on driver: any DatabaseDriver) async -> [String]? { + guard let schema, let current = driver.currentSchema, current != schema else { return nil } + var listed: [String]? do { + if onlyIfListed { + let names = try await driver.fetchSchemas() + listed = names + guard names.contains(schema) else { return names } + } try await driver.switchSchema(to: schema) } catch { logger.warning("Failed to restore schema \(schema, privacy: .private(mask: .hash)): \(LogRedaction.publicDescription(of: error), privacy: .public) \(error.localizedDescription, privacy: .private)") } + return listed } } diff --git a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift index 1bf56c825c..bc1112cdaf 100644 --- a/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift +++ b/apple/TableProMobile/TableProMobile/Drivers/PostgreSQLDriver.swift @@ -395,7 +395,7 @@ private actor PostgreSQLActor { ) guard let connection = PQconnectStart(connStr) else { - throw PostgreSQLError.connectionFailed(String(localized: "Could not start a connection.")) + throw PostgreSQLError.unreachable(String(localized: "Could not start a connection.")) } var adopted = false @@ -434,14 +434,14 @@ private actor PostgreSQLActor { throw PostgreSQLError.connectionFailed(Self.message(from: connection)) } guard Date() < deadline else { - throw PostgreSQLError.connectionFailed(String(localized: "Connection timed out.")) + throw PostgreSQLError.unreachable(String(localized: "Connection timed out.")) } let events = status == PGRES_POLLING_READING ? Int16(POLLIN) : Int16(POLLOUT) var descriptor = pollfd(fd: socket, events: events, revents: 0) let ready = poll(&descriptor, 1, Self.pollSliceMilliseconds) guard ready >= 0 else { - throw PostgreSQLError.connectionFailed(String(localized: "Connection failed while waiting on the socket.")) + throw PostgreSQLError.unreachable(String(localized: "Connection failed while waiting on the socket.")) } guard ready > 0 else { continue } @@ -1200,13 +1200,15 @@ nonisolated private struct RawPGResult: Sendable { nonisolated enum PostgreSQLError: Error, LocalizedError { case connectionFailed(String) + // The connect never reached the server: it timed out or the socket failed on this device. + case unreachable(String) case notConnected case queryFailed(String) case unsupported(String) var errorDescription: String? { switch self { - case .connectionFailed(let msg): return "PostgreSQL connection failed: \(msg)" + case .connectionFailed(let msg), .unreachable(let msg): return "PostgreSQL connection failed: \(msg)" case .notConnected: return "Not connected to PostgreSQL database" case .queryFailed(let msg): return "PostgreSQL query failed: \(msg)" case .unsupported(let msg): return msg diff --git a/apple/TableProMobile/TableProMobile/Helpers/IndexedRow.swift b/apple/TableProMobile/TableProMobile/Helpers/IndexedRow.swift index c90c8ed5e9..eb06023008 100644 --- a/apple/TableProMobile/TableProMobile/Helpers/IndexedRow.swift +++ b/apple/TableProMobile/TableProMobile/Helpers/IndexedRow.swift @@ -1,15 +1,6 @@ import Foundation -/// Identifiable wrapper used by iOS lists that need both the row payload and -/// its position index. Iterating over `[IndexedRow]` instead of -/// `rows.indices` keeps SwiftUI's `ForEach` diff stable when the underlying -/// row collection shrinks mid-render. This is the pattern that prevents the -/// `Array._checkSubscript` crashes seen in release 1.0 (build 11). struct IndexedRow: Identifiable { let id: Int let values: Element - - static func wrap(_ rows: [Element]) -> [IndexedRow] { - rows.enumerated().map { IndexedRow(id: $0.offset, values: $0.element) } - } } diff --git a/apple/TableProMobile/TableProMobile/Localizable.xcstrings b/apple/TableProMobile/TableProMobile/Localizable.xcstrings index bafad10cd7..a9dae43c47 100644 --- a/apple/TableProMobile/TableProMobile/Localizable.xcstrings +++ b/apple/TableProMobile/TableProMobile/Localizable.xcstrings @@ -10906,6 +10906,9 @@ } } } + }, + "Go to First Page" : { + }, "Go to Page" : { "localizations" : { @@ -15426,6 +15429,9 @@ } } } + }, + "No Rows on This Page" : { + }, "No Tables" : { "localizations" : { @@ -27626,6 +27632,9 @@ "This Oracle server requires Native Network Encryption but did not offer it on this connection. Ask the DBA to enable it, or use TLS instead by setting an SSL mode." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "This page is past the last row." : { + }, "This query will modify data. Are you sure you want to continue?" : { "localizations" : { diff --git a/apple/TableProMobile/TableProMobile/Models/RowWindow.swift b/apple/TableProMobile/TableProMobile/Models/RowWindow.swift index 5180c3c4bc..715d89a375 100644 --- a/apple/TableProMobile/TableProMobile/Models/RowWindow.swift +++ b/apple/TableProMobile/TableProMobile/Models/RowWindow.swift @@ -5,8 +5,16 @@ struct RowWindow: Sendable { private(set) var rows: [Row] private(set) var firstAbsoluteIndex: Int private(set) var totalAppended: Int + private(set) var clears = 0 let capacity: Int + // Changes whenever the first rows may have, so a view can tell without reading them. + struct HeadIdentity: Equatable, Sendable { + let clears: Int + let firstAbsoluteIndex: Int + let count: Int + } + init(capacity: Int = 200) { self.rows = [] self.firstAbsoluteIndex = 0 @@ -37,6 +45,11 @@ struct RowWindow: Sendable { rows = [] firstAbsoluteIndex = 0 totalAppended = 0 + clears += 1 + } + + func headIdentity(count: Int) -> HeadIdentity { + HeadIdentity(clears: clears, firstAbsoluteIndex: firstAbsoluteIndex, count: min(rows.count, count)) } var isEmpty: Bool { diff --git a/apple/TableProMobile/TableProMobile/Models/StreamingResultBuffer.swift b/apple/TableProMobile/TableProMobile/Models/StreamingResultBuffer.swift index 805a260972..534cc6dab5 100644 --- a/apple/TableProMobile/TableProMobile/Models/StreamingResultBuffer.swift +++ b/apple/TableProMobile/TableProMobile/Models/StreamingResultBuffer.swift @@ -5,8 +5,8 @@ import TableProModels @Observable final class StreamingResultBuffer { private(set) var columns: [ColumnInfo] = [] + // The one copy of the rows: a list derives each row's display text when it draws it. private(set) var window: RowWindow - private(set) var legacyRows: [[String?]] = [] private(set) var rowsAffected: Int? private(set) var statusMessage: String? private(set) var truncation: TruncationReason? @@ -21,8 +21,8 @@ final class StreamingResultBuffer { self.window = RowWindow(capacity: capacity) } - var count: Int { legacyRows.count } - var isEmpty: Bool { legacyRows.isEmpty } + var count: Int { window.count } + var isEmpty: Bool { window.isEmpty } func apply(_ element: StreamElement) { switch element { @@ -51,13 +51,7 @@ final class StreamingResultBuffer { flushTask?.cancel() flushTask = nil guard !pendingRows.isEmpty else { return } - // Display text only: a long value kept whole stays whole in `window` for copy, export and the row screen. - let legacyBatch = pendingRows.map { $0.displayValues() } window.append(contentsOf: pendingRows) - legacyRows.append(contentsOf: legacyBatch) - if legacyRows.count > window.count { - legacyRows.removeFirst(legacyRows.count - window.count) - } pendingRows.removeAll(keepingCapacity: true) } @@ -68,8 +62,6 @@ final class StreamingResultBuffer { func shrink(to maxCount: Int) { window.shrink(to: maxCount) - guard legacyRows.count > maxCount else { return } - legacyRows.removeFirst(legacyRows.count - maxCount) } func reset() { @@ -77,7 +69,6 @@ final class StreamingResultBuffer { flushTask = nil columns = [] window.clear() - legacyRows.removeAll(keepingCapacity: true) rowsAffected = nil statusMessage = nil truncation = nil diff --git a/apple/TableProMobile/TableProMobile/Platform/ConnectionLibraryPublisher.swift b/apple/TableProMobile/TableProMobile/Platform/ConnectionLibraryPublisher.swift index 43de7c3d9f..b4c561c7cf 100644 --- a/apple/TableProMobile/TableProMobile/Platform/ConnectionLibraryPublisher.swift +++ b/apple/TableProMobile/TableProMobile/Platform/ConnectionLibraryPublisher.swift @@ -5,39 +5,53 @@ import TableProLogRedaction import TableProModels import WidgetKit +// The index, the widget file and the shortcut parameters all outlive the process, so each is +// rewritten only when what it holds differs from the library, not at every launch. @MainActor final class ConnectionLibraryPublisher { private static let logger = Logger(subsystem: "com.TablePro", category: "LibraryPublisher") + static let publishedShortcutsKey = "com.TablePro.publishedShortcutProjection" private let searchIndex: any ConnectionSearchIndexing + private let readWidgetItems: () -> [WidgetConnectionItem] private let writeWidgetItems: ([WidgetConnectionItem]) -> Void private let refreshShortcutParameters: () -> Void + private let defaults: UserDefaults + private let build: String - private var shortcutProjection: [SearchableConnection]? private var indexed: [SearchableConnection]? private var queued: [SearchableConnection]? private var replacing: Task? init( searchIndex: any ConnectionSearchIndexing, + readWidgetItems: @escaping () -> [WidgetConnectionItem], writeWidgetItems: @escaping ([WidgetConnectionItem]) -> Void, - refreshShortcutParameters: @escaping () -> Void + refreshShortcutParameters: @escaping () -> Void, + defaults: UserDefaults, + build: String = "" ) { self.searchIndex = searchIndex + self.readWidgetItems = readWidgetItems self.writeWidgetItems = writeWidgetItems self.refreshShortcutParameters = refreshShortcutParameters + self.defaults = defaults + self.build = build } - static func live() -> ConnectionLibraryPublisher { + static func live(defaults: UserDefaults = .standard) -> ConnectionLibraryPublisher { ConnectionLibraryPublisher( searchIndex: SpotlightConnectionIndex(), + readWidgetItems: { SharedConnectionStore.read() }, writeWidgetItems: { items in SharedConnectionStore.write(items) WidgetCenter.shared.reloadAllTimelines() }, refreshShortcutParameters: { TableProShortcuts.updateAppShortcutParameters() - } + }, + defaults: defaults, + build: Bundle.main.object(forInfoDictionaryKey: "CFBundleVersion") as? String ?? "" ) } @@ -64,11 +78,15 @@ final class ConnectionLibraryPublisher { } func publish(_ connections: [DatabaseConnection]) { - writeWidgetItems(Self.widgetItems(for: connections)) + let items = Self.widgetItems(for: connections) + if items != readWidgetItems() { + writeWidgetItems(items) + } let searchable = Self.searchableConnections(for: connections) - if searchable != shortcutProjection { - shortcutProjection = searchable + let digest = SearchableConnection.digest(of: searchable, build: build) + if digest != defaults.data(forKey: Self.publishedShortcutsKey) { refreshShortcutParameters() + defaults.set(digest, forKey: Self.publishedShortcutsKey) } queued = searchable guard replacing == nil else { return } @@ -83,8 +101,15 @@ final class ConnectionLibraryPublisher { while let next = queued { queued = nil guard next != indexed else { continue } + let state = SearchableConnection.digest(of: next, build: build) + // A new process learns what the index holds from the state the last replace stored. A state that + // cannot be read is treated as different, so the index is written rather than left as it is. + if indexed == nil, (try? await searchIndex.storedState()) == state { + indexed = next + continue + } do { - try await searchIndex.replaceConnections(with: next) + try await searchIndex.replaceConnections(with: next, state: state) indexed = next } catch { indexed = nil diff --git a/apple/TableProMobile/TableProMobile/Platform/KeychainSecureStore.swift b/apple/TableProMobile/TableProMobile/Platform/KeychainSecureStore.swift index 4ae63bc82c..fc2890837d 100644 --- a/apple/TableProMobile/TableProMobile/Platform/KeychainSecureStore.swift +++ b/apple/TableProMobile/TableProMobile/Platform/KeychainSecureStore.swift @@ -14,6 +14,8 @@ nonisolated protocol KeychainItemStore: Sendable { func update(_ query: [String: Any], attributes: [String: Any]) -> OSStatus // The first item the query matches, with its data and modification date. func copyItem(_ query: [String: Any]) -> KeychainCopiedItem + // Every item's account the query matches; none when the read fails. + func accounts(_ query: [String: Any]) -> [String] func delete(_ query: [String: Any]) -> OSStatus } @@ -41,6 +43,16 @@ nonisolated struct SystemKeychainItemStore: KeychainItemStore { ) } + func accounts(_ query: [String: Any]) -> [String] { + var query = query + query[kSecReturnAttributes as String] = true + query[kSecMatchLimit as String] = kSecMatchLimitAll + var result: AnyObject? + guard SecItemCopyMatching(query as CFDictionary, &result) == errSecSuccess, + let items = result as? [[String: Any]] else { return [] } + return items.compactMap { $0[kSecAttrAccount as String] as? String } + } + func delete(_ query: [String: Any]) -> OSStatus { SecItemDelete(query as CFDictionary) } @@ -184,38 +196,38 @@ nonisolated final class KeychainSecureStore: SecureStore { } // Device-local items only: deleting a synced one here would remove it from the Mac that wrote it. - static func cleanOrphanedCredentials(validConnectionIds: Set) { - guard !validConnectionIds.isEmpty else { return } - + // The Keychain calls block, so they run off the main actor. The library is read once the listing + // returns, and a save adds its connection before its secrets, so a connection saved meanwhile keeps them. + @concurrent + static func cleanOrphanedCredentials( + items: any KeychainItemStore = SystemKeychainItemStore(), + keeping connectionIds: @MainActor @Sendable () -> Set? + ) async { let accessGroup = resolveAccessGroup() - let query: [String: Any] = [ + let accounts = items.accounts(applying(accessGroup: accessGroup, to: [ kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: serviceName, - kSecReturnAttributes as String: true, - kSecMatchLimit as String: kSecMatchLimitAll, kSecAttrSynchronizable as String: false, kSecUseDataProtectionKeychain as String: true, - ] - var result: AnyObject? - guard SecItemCopyMatching(applying(accessGroup: accessGroup, to: query) as CFDictionary, &result) == errSecSuccess, - let items = result as? [[String: Any]] else { return } - - let accounts = items.compactMap { $0[kSecAttrAccount as String] as? String } - for account in ConnectionSecretKind.orphanedAccounts(accounts, keeping: validConnectionIds) { - deleteDeviceLocal(forKey: account, accessGroup: accessGroup) + ])) + guard !accounts.isEmpty, let kept = await connectionIds() else { return } + for account in ConnectionSecretKind.orphanedAccounts(accounts, keeping: kept) { + let status = items.delete(deviceLocalQuery(forKey: account, accessGroup: accessGroup)) + if status != errSecSuccess, status != errSecItemNotFound { + logger.error("Removing a deleted connection's secret failed: \(status)") + } } } // Unlike delete(forKey:), never matches a synced item of the same name. - private static func deleteDeviceLocal(forKey key: String, accessGroup: String?) { - let query: [String: Any] = [ + private static func deviceLocalQuery(forKey key: String, accessGroup: String?) -> [String: Any] { + applying(accessGroup: accessGroup, to: [ kSecClass as String: kSecClassGenericPassword, kSecAttrService as String: serviceName, kSecAttrAccount as String: key, kSecAttrSynchronizable as String: false, kSecUseDataProtectionKeychain as String: true, - ] - SecItemDelete(applying(accessGroup: accessGroup, to: query) as CFDictionary) + ]) } } diff --git a/apple/TableProMobile/TableProMobile/Platform/SpotlightConnectionIndex.swift b/apple/TableProMobile/TableProMobile/Platform/SpotlightConnectionIndex.swift index 0197142618..a2c7fe6b6a 100644 --- a/apple/TableProMobile/TableProMobile/Platform/SpotlightConnectionIndex.swift +++ b/apple/TableProMobile/TableProMobile/Platform/SpotlightConnectionIndex.swift @@ -1,4 +1,5 @@ import CoreSpotlight +import CryptoKit import Foundation import TableProModels @@ -19,21 +20,66 @@ nonisolated struct SearchableConnection: Equatable, Sendable { summary = [connection.type.mobileDisplayName, ConnectionDetailFormatter.detail(for: connection)] .joined(separator: ", ") } + + // The build is part of it, so an update that changes how an item is written publishes again once. + static func digest(of connections: [SearchableConnection], build: String) -> Data { + var bytes = Data() + for field in [build] + connections.flatMap({ [$0.id.uuidString, $0.title, $0.summary] }) { + let utf8 = Data(field.utf8) + withUnsafeBytes(of: UInt32(utf8.count).bigEndian) { bytes.append(contentsOf: $0) } + bytes.append(utf8) + } + return Data(SHA256.hash(data: bytes)) + } } nonisolated protocol ConnectionSearchIndexing: Sendable { - func replaceConnections(with connections: [SearchableConnection]) async throws + // The state the last finished replace stored, nil when none did. + func storedState() async throws -> Data? + func replaceConnections(with connections: [SearchableConnection], state: Data) async throws } nonisolated struct SpotlightConnectionIndex: ConnectionSearchIndexing { static let domainIdentifier = "com.TablePro.connections" + // CoreSpotlight keeps a client state only for a named index; the default one supports no batch. + static let indexName = "com.TablePro.connections" + + func storedState() async throws -> Data? { + guard CSSearchableIndex.isIndexingAvailable() else { return nil } + return try await Self.lastClientState(of: CSSearchableIndex(name: Self.indexName)) + } - func replaceConnections(with connections: [SearchableConnection]) async throws { + // Connections are all the app indexes. Deleting everything also resets the stored state, so an index a failed + // replace emptied never passes for the projection it held before. + func replaceConnections(with connections: [SearchableConnection], state: Data) async throws { guard CSSearchableIndex.isIndexingAvailable() else { return } - let index = CSSearchableIndex.default() - try await index.deleteSearchableItems(withDomainIdentifiers: [Self.domainIdentifier]) - guard !connections.isEmpty else { return } - try await index.indexSearchableItems(connections.map(Self.searchableItem(for:))) + let index = CSSearchableIndex(name: Self.indexName) + if (try? await Self.lastClientState(of: index)) == nil { + // Builds before the named index wrote through the default one. + try await CSSearchableIndex.default().deleteSearchableItems(withDomainIdentifiers: [Self.domainIdentifier]) + } + try await index.deleteAllSearchableItems() + index.beginBatch() + if !connections.isEmpty { + // Journaled with the batch, so ending the batch is the one call awaited. + index.indexSearchableItems(connections.map(Self.searchableItem(for:)), completionHandler: nil) + } + try await index.endBatch(withClientState: state) + } + + // The async form returns a non-optional Data, though the index answers nil before any batch stored a state. + private static func lastClientState(of index: CSSearchableIndex) async throws -> Data? { + let state: Data? = try await withCheckedThrowingContinuation { continuation in + index.fetchLastClientState { state, error in + if let error { + continuation.resume(throwing: error) + } else { + continuation.resume(returning: state) + } + } + } + guard let state, !state.isEmpty else { return nil } + return state } static func searchableItem(for connection: SearchableConnection) -> CSSearchableItem { diff --git a/apple/TableProMobile/TableProMobile/Sync/IOSSyncCoordinator.swift b/apple/TableProMobile/TableProMobile/Sync/IOSSyncCoordinator.swift index 9420079f9f..ffe71de5f7 100644 --- a/apple/TableProMobile/TableProMobile/Sync/IOSSyncCoordinator.swift +++ b/apple/TableProMobile/TableProMobile/Sync/IOSSyncCoordinator.swift @@ -76,6 +76,15 @@ final class IOSSyncCoordinator { @ObservationIgnored private var editGenerations: [EditKey: Int] = [:] @ObservationIgnored private var accountChangeObservation: AnyCancellable? + // A scene activation asks only for what other devices sent, so one download per interval answers every + // activation in it. The same rule as the Mac's SyncCoordinator. + nonisolated static let activationInterval: Duration = .seconds(60) + @ObservationIgnored private let activationClock: any Clock + @ObservationIgnored private var activationCooldown: Task? + @ObservationIgnored private var activationHeld = false + @ObservationIgnored private var activationAwaitsRun = false + @ObservationIgnored private var downloadIsAheadInRun = false + // Each returns whether the merged list was saved; false holds the change token for a replay. @ObservationIgnored var onConnectionsChanged: (([DatabaseConnection]) -> Bool)? @ObservationIgnored var onGroupsChanged: (([ConnectionGroup]) -> Bool)? @@ -99,13 +108,15 @@ final class IOSSyncCoordinator { makeTransport: @escaping () -> any IOSSyncTransport = { CloudKitSyncEngine() }, isEnabled: @escaping () -> Bool = { AppPreferences.isCloudSyncEnabled }, notificationCenter: NotificationCenter = .default, - networkMonitor: SyncNetworkMonitor? = SyncNetworkMonitor() + networkMonitor: SyncNetworkMonitor? = SyncNetworkMonitor(), + activationClock: any Clock = ContinuousClock() ) { self.metadata = metadata self.recordCache = recordCache self.makeTransport = makeTransport self.isEnabled = isEnabled self.networkMonitor = networkMonitor + self.activationClock = activationClock self.status = isEnabled() ? metadata.zoneState.initialStatus : .disabled(.userDisabled) self.lastSyncDate = metadata.lastSyncDate accountChangeObservation = notificationCenter.publisher(for: .CKAccountChanged) @@ -124,6 +135,7 @@ final class IOSSyncCoordinator { deinit { debounceTask?.cancel() retryTask?.cancel() + activationCooldown?.cancel() } private func currentTransport() -> any IOSSyncTransport { @@ -154,6 +166,7 @@ final class IOSSyncCoordinator { debounceTask?.cancel() debounceTask = nil pendingTriggers = [] + endActivationHold() metadata.lastSyncDate = nil lastSyncDate = nil decide(.disabled(.userDisabled)) @@ -210,6 +223,54 @@ final class IOSSyncCoordinator { await task.value } + // The scene's entry point. A background refresh and every other trigger call `sync(_:)` and are never held. + func syncOnSceneActivation() async { + if let runningSync { + // A run whose download has not started brings what the activation asks for. + guard !downloadIsAheadInRun else { return } + activationAwaitsRun = true + await runningSync.value + return + } + if activationCooldown != nil { + activationHeld = true + return + } + await sync(.activation) + } + + var isHoldingActivations: Bool { + activationCooldown != nil + } + + // After a download, activations wait for the interval to end and then run once, so a change made on + // another device still arrives. + private func startActivationCooldown() { + activationCooldown?.cancel() + activationHeld = false + let clock = activationClock + activationCooldown = Task { [weak self] in + do { + try await clock.sleep(for: Self.activationInterval) + } catch { + return + } + // A newer download may have replaced this hold after the sleep ended. + guard !Task.isCancelled, let self else { return } + activationCooldown = nil + guard activationHeld else { return } + activationHeld = false + await syncOnSceneActivation() + } + } + + private func endActivationHold() { + activationCooldown?.cancel() + activationCooldown = nil + activationHeld = false + activationAwaitsRun = false + } + private func runThenPending(_ trigger: SyncTrigger) async { var next: SyncTrigger? = trigger while let current = next { @@ -258,6 +319,16 @@ final class IOSSyncCoordinator { /// The trigger that may do the most among those that arrived during the last run. private func takePendingTrigger() -> SyncTrigger? { + if activationAwaitsRun { + activationAwaitsRun = false + // The download began before the activation and may have missed what it asks for, so it runs once the + // interval ends rather than now. + if activationCooldown != nil { + activationHeld = true + } else if !pendingTriggers.contains(.activation) { + pendingTriggers.append(.activation) + } + } guard !pendingTriggers.isEmpty else { return nil } let error = status.error /// The network coming back mid-run matters only if the run ended unable to reach iCloud. @@ -281,6 +352,8 @@ final class IOSSyncCoordinator { /// Nil when the status moved on while the run was in flight. private func perform(_ requested: SyncAdmission, generation: Int) async -> RunResult? { + downloadIsAheadInRun = true + defer { downloadIsAheadInRun = false } let transport = currentTransport() var admission = requested switch await confirmAccount(using: transport, generation: generation) { @@ -359,6 +432,7 @@ final class IOSSyncCoordinator { } let pulled: PullResult + downloadIsAheadInRun = false do { pulled = try await fetchChanges(using: transport) } catch { @@ -387,6 +461,7 @@ final class IOSSyncCoordinator { guard applied != .notSaved else { return RunResult(admission: admission, failure: .pullNotSaved) } + startActivationCooldown() // Committed before the upload, so a failed upload does not make every later run download everything again. if applied == .saved, let newToken = pulled.newToken { metadata.saveToken(newToken) diff --git a/apple/TableProMobile/TableProMobile/TableProMobileApp.swift b/apple/TableProMobile/TableProMobile/TableProMobileApp.swift index d4d054dadf..dbb9a6dd30 100644 --- a/apple/TableProMobile/TableProMobile/TableProMobileApp.swift +++ b/apple/TableProMobile/TableProMobile/TableProMobileApp.swift @@ -54,7 +54,7 @@ struct TableProMobileApp: App { if appState.onboarding.isCloudSyncEnabled && appState.loadStatus == .ready { syncTask?.cancel() syncTask = Task { - await appState.syncCoordinator.sync(.activation) + await appState.syncCoordinator.syncOnSceneActivation() } } Telemetry.shared.sceneBecameActive() diff --git a/apple/TableProMobile/TableProMobile/ViewModels/DataBrowserViewModel.swift b/apple/TableProMobile/TableProMobile/ViewModels/DataBrowserViewModel.swift index 2dafc1a8b0..4385b10da9 100644 --- a/apple/TableProMobile/TableProMobile/ViewModels/DataBrowserViewModel.swift +++ b/apple/TableProMobile/TableProMobile/ViewModels/DataBrowserViewModel.swift @@ -25,7 +25,6 @@ final class DataBrowserViewModel { var columns: [ColumnInfo] { buffer.columns } var window: RowWindow { buffer.window } - var legacyRows: [[String?]] { buffer.legacyRows } var rowsAffected: Int? { buffer.rowsAffected } var statusMessage: String? { buffer.statusMessage } @@ -56,7 +55,11 @@ final class DataBrowserViewModel { @ObservationIgnored private var keyReadTask: Task? @ObservationIgnored private var loadGeneration = 0 @ObservationIgnored private var hasSeededFilters = false - @ObservationIgnored private var singleRowMatch = SingleRowMatch.inactive + @ObservationIgnored private var foreignKeysRead = false + // Bumped when what the count counts changes; a page turn, a sort or a page size change leaves it. + @ObservationIgnored private var countEpoch = 0 + @ObservationIgnored private var countingEpoch: Int? + private var singleRowMatch = SingleRowMatch.inactive private enum SingleRowMatch { case inactive @@ -87,17 +90,20 @@ final class DataBrowserViewModel { private var rowIdentity: RowIdentity { RowLocator.identity(of: session?.driver) } var showsPaginationBar: Bool { - !legacyRows.isEmpty || hasActiveSearch || hasActiveFilters || isPageLoading + !buffer.isEmpty || hasActiveSearch || hasActiveFilters || isPageLoading || pagination.currentPage > 0 } var canGoToPreviousPage: Bool { pagination.currentPage > 0 && !isLoading } - var canGoToNextPage: Bool { pagination.hasNextPage && !isLoading } + var canGoToNextPage: Bool { pagination.hasNextPage && !isLoading && !isPastTheEnd } + + // An empty page after the first: Next or Go to Page went past the last row before the count said where it was. + var isPastTheEnd: Bool { buffer.isEmpty && pagination.currentPage > 0 } var paginationLabel: String { - guard !legacyRows.isEmpty else { return "" } + guard !buffer.isEmpty else { return "" } // A memory trim drops rows from the head of the page, so the range starts at the first row still held. let start = pagination.currentOffset + window.firstAbsoluteIndex + 1 - let end = start + legacyRows.count - 1 + let end = start + buffer.count - 1 guard let total = pagination.totalRows else { return "\(start)-\(end)" } return Self.pageRangeLabel(start: start, end: end, total: total) } @@ -119,12 +125,17 @@ final class DataBrowserViewModel { schema: String? = nil, trackRead: SessionReadTracker? = nil ) { + // Another session or schema holds other rows, so neither the total nor a count still running applies. + let movesSource = session?.driver !== self.session?.driver || schema != self.schema || table.name != self.table?.name self.session = session self.table = table self.databaseType = databaseType self.host = host self.schema = schema self.trackRead = trackRead + if movesSource { + resetTotal() + } } // Only the first call counts, so filters the user changed afterwards survive the view's task running again. @@ -136,6 +147,11 @@ final class DataBrowserViewModel { singleRowMatch = hasActiveFilters ? .awaitingFirstLoad : .inactive } + var hasSingleRowMatch: Bool { + if case .found = singleRowMatch { return true } + return false + } + func takeSingleRowMatch() -> Bool { guard case .found = singleRowMatch else { return false } singleRowMatch = .inactive @@ -160,7 +176,7 @@ final class DataBrowserViewModel { return } - if isInitial || legacyRows.isEmpty { + if isInitial || buffer.isEmpty { isLoading = true } else { isPageLoading = true @@ -194,7 +210,7 @@ final class DataBrowserViewModel { isLoading = false isPageLoading = false guard case .awaitingFirstLoad = singleRowMatch else { return } - singleRowMatch = loadError == nil && legacyRows.count == 1 ? .found : .inactive + singleRowMatch = loadError == nil && buffer.count == 1 ? .found : .inactive } private func loadRows(session: ConnectionSession, table: TableInfo, isInitial: Bool, generation: Int) async { @@ -226,19 +242,24 @@ final class DataBrowserViewModel { return } settleTotalRowsFromShortPage() + // The page is on screen and takes taps; the key links and the pager's total fill in after it. + finishLoad() - if foreignKeys.isEmpty || isInitial { + if !foreignKeysRead || isInitial { do { let fetched = try await session.driver.fetchForeignKeys(table: table.name, schema: schema) guard isCurrent(generation) else { return } foreignKeys = fetched + foreignKeysRead = true } catch { Self.logger.warning("Failed to fetch foreign keys: \(error.localizedDescription, privacy: .private)") } } - if isCurrent(generation), pagination.totalRows == nil { - await fetchTotalRows(session: session, table: table, generation: generation) + // A count already running for the same rows lands for this page too, so a page turn neither drops it nor + // starts another behind it. + if isCurrent(generation), pagination.totalRows == nil, countingEpoch != countEpoch { + await fetchTotalRows(session: session, table: table) } } catch { guard isCurrent(generation) else { return } @@ -285,10 +306,13 @@ final class DataBrowserViewModel { } } - // A page cut short by Stop, a closed screen or another statement says nothing about where the table ends. + // A page cut short by Stop, a closed screen or another statement says nothing about where the table ends, and an + // empty page after the first says only that it ends before it. private func settleTotalRowsFromShortPage() { - guard pageEndedOnItsOwn, legacyRows.count < pagination.pageSize, pagination.totalRows == nil else { return } - pagination.totalRows = pagination.currentOffset + legacyRows.count + guard pageEndedOnItsOwn, buffer.count < pagination.pageSize, pagination.totalRows == nil, !isPastTheEnd else { + return + } + pagination.totalRows = pagination.currentOffset + buffer.count } private func pageOptions(lazyContext: LazyContext?, textBudget: TextBudget? = nil) -> StreamOptions { @@ -370,11 +394,16 @@ final class DataBrowserViewModel { columns.filter { !Cell.holdsBytes(typeName: $0.typeName) } } - private func fetchTotalRows(session: ConnectionSession, table: TableInfo, generation: Int) async { + private func fetchTotalRows(session: ConnectionSession, table: TableInfo) async { + let epoch = countEpoch + countingEpoch = epoch + defer { + if countingEpoch == epoch { countingEpoch = nil } + } do { if let builder = session.driver as? any DriverQueryBuilding { let total = try await builder.rowCount(readRequest(table: table)) - guard isCurrent(generation) else { return } + guard epoch == countEpoch else { return } pagination.totalRows = total return } @@ -396,7 +425,7 @@ final class DataBrowserViewModel { countQuery = SQLBuilder.buildCount(table: table.name, schema: schema, type: databaseType) } let countResult = try await session.driver.execute(query: countQuery) - guard isCurrent(generation) else { return } + guard epoch == countEpoch else { return } if let firstRow = countResult.rows.first, let firstCol = firstRow.first { pagination.totalRows = Int(firstCol ?? "0") } @@ -430,10 +459,10 @@ final class DataBrowserViewModel { await navigatePage() } + // The page size and the order cannot change how many rows there are, so the total stays. func changePageSize(_ size: Int) async { pagination.pageSize = size pagination.currentPage = 0 - pagination.totalRows = nil await navigatePage() } @@ -445,7 +474,6 @@ final class DataBrowserViewModel { func applySort() async { pagination.currentPage = 0 - pagination.totalRows = nil await load() } @@ -454,7 +482,7 @@ final class DataBrowserViewModel { activeSearchText = trimmed guard !trimmed.isEmpty, !columns.isEmpty else { return } pagination.currentPage = 0 - pagination.totalRows = nil + resetTotal() searchTask?.cancel() let task = Task { [weak self] in guard let self else { return } @@ -467,7 +495,7 @@ final class DataBrowserViewModel { func clearSearch() async { activeSearchText = "" pagination.currentPage = 0 - pagination.totalRows = nil + resetTotal() searchTask?.cancel() let task = Task { [weak self] in guard let self else { return } @@ -479,17 +507,22 @@ final class DataBrowserViewModel { func applyFilters() async { pagination.currentPage = 0 - pagination.totalRows = nil + resetTotal() await load() } func clearFilters() async { filters.removeAll() pagination.currentPage = 0 - pagination.totalRows = nil + resetTotal() await load() } + private func resetTotal() { + pagination.totalRows = nil + countEpoch += 1 + } + // MARK: - Row Operations private static var rowNotDeleted: AppError { diff --git a/apple/TableProMobile/TableProMobile/ViewModels/QueryEditorViewModel.swift b/apple/TableProMobile/TableProMobile/ViewModels/QueryEditorViewModel.swift index 1e062df830..5b0c1b451d 100644 --- a/apple/TableProMobile/TableProMobile/ViewModels/QueryEditorViewModel.swift +++ b/apple/TableProMobile/TableProMobile/ViewModels/QueryEditorViewModel.swift @@ -75,7 +75,6 @@ final class QueryEditorViewModel { var columns: [ColumnInfo] { buffer.columns } var window: RowWindow { buffer.window } - var legacyRows: [[String?]] { buffer.legacyRows } var statusMessage: String? { buffer.statusMessage } var truncationReason: TruncationReason? { buffer.truncation } @@ -95,7 +94,7 @@ final class QueryEditorViewModel { var truncationMessage: String? { guard let truncationReason else { return nil } - let shown = buffer.legacyRows.count + let shown = buffer.count switch truncationReason { case .rowCap: return String(format: String(localized: "Showing the first %d rows. Add LIMIT to fetch more."), shown) @@ -112,9 +111,39 @@ final class QueryEditorViewModel { cutLongValues ? String(localized: "Some long values are shown in part to stay within memory.") : nil } + // Formats off the main actor: 10,000 rows of JSON take long enough to hold up the screen. + func exportResults( + as format: ExportFormat, + databaseType: DatabaseType, + driver: (any DatabaseDriver)?, + availableBytes: Int + ) async throws -> String { + try await Self.export( + rows: buffer.window.rows, columns: buffer.columns, format: format, + databaseType: databaseType, driver: driver, availableBytes: availableBytes + ) + } + + @concurrent + nonisolated private static func export( + rows: [Row], + columns: [ColumnInfo], + format: ExportFormat, + databaseType: DatabaseType, + driver: (any DatabaseDriver)?, + availableBytes: Int + ) async throws -> String { + guard resultsFitExport(rows: rows, availableBytes: availableBytes) else { + throw ClipboardExporter.TooLarge(source: .queryResult) + } + return try ClipboardExporter.exportRows( + columns: columns, rows: rows.map(\.cells), format: format, databaseType: databaseType, driver: driver + ) + } + // The export text and the pasteboard's copy of it are held beside the values already on screen. - func resultsFitExport(availableBytes: Int) -> Bool { - let textBytes = buffer.window.rows.reduce(0) { total, row in + nonisolated static func resultsFitExport(rows: [Row], availableBytes: Int) -> Bool { + let textBytes = rows.reduce(0) { total, row in row.cells.reduce(total) { sum, cell in guard case .text(let value) = cell else { return sum } return sum + value.utf8.count diff --git a/apple/TableProMobile/TableProMobile/ViewModels/RowDetailViewModel.swift b/apple/TableProMobile/TableProMobile/ViewModels/RowDetailViewModel.swift index bf6976f3df..80af225dc2 100644 --- a/apple/TableProMobile/TableProMobile/ViewModels/RowDetailViewModel.swift +++ b/apple/TableProMobile/TableProMobile/ViewModels/RowDetailViewModel.swift @@ -11,7 +11,9 @@ final class RowDetailViewModel { let columns: [ColumnInfo] let columnDetails: [ColumnInfo] - let foreignKeys: [ForeignKeyInfo] + // Read when shown: the browser reads the keys after its rows appear, so a row opened first gets them too. + @ObservationIgnored private let readForeignKeys: () -> [ForeignKeyInfo] + var foreignKeys: [ForeignKeyInfo] { readForeignKeys() } let table: TableInfo? // Decides what the row is and how it is located; writes go to `session`, which follows a redial. private let openedSession: ConnectionSession? @@ -47,7 +49,7 @@ final class RowDetailViewModel { databaseType: DatabaseType = .sqlite, schema: String? = nil, safeModeLevel: @escaping () -> SafeModeLevel = { .off }, - foreignKeys: [ForeignKeyInfo] = [], + foreignKeys: @escaping @autoclosure () -> [ForeignKeyInfo] = [], onSaved: (() -> Void)? = nil, loadFullValue: ((CellRef) async throws -> String?)? = nil, liveSession: (() -> Result)? = nil @@ -62,7 +64,7 @@ final class RowDetailViewModel { self.databaseType = databaseType self.schema = schema self.readSafeModeLevel = safeModeLevel - self.foreignKeys = foreignKeys + self.readForeignKeys = foreignKeys self.onSaved = onSaved self.loadFullValueProvider = loadFullValue } diff --git a/apple/TableProMobile/TableProMobile/Views/Components/RowGrid.swift b/apple/TableProMobile/TableProMobile/Views/Components/RowGrid.swift index 90f1c2adea..ce04f75535 100644 --- a/apple/TableProMobile/TableProMobile/Views/Components/RowGrid.swift +++ b/apple/TableProMobile/TableProMobile/Views/Components/RowGrid.swift @@ -88,11 +88,11 @@ nonisolated struct RowGridViewport: Equatable, Sendable { let leadingInset: CGFloat } -struct RowGrid: View { +struct RowGrid: View { let columns: [ColumnInfo] - let rows: [[String?]] + let window: RowWindow let keyColumn: Int? - @ViewBuilder let row: (_ index: Int, _ line: RowGridLine) -> Row + @ViewBuilder let row: (_ index: Int, _ line: RowGridLine) -> Line @Environment(\.dynamicTypeSize) private var dynamicTypeSize @Environment(\.displayScale) private var displayScale @@ -103,9 +103,12 @@ struct RowGrid: View { @State private var computedLayout: (key: LayoutKey, layout: RowGridLayout)? @State private var viewport = RowGridViewport(width: 0, leadingInset: 0) + private var rows: [Row] { window.rows } + private var layoutKey: LayoutKey { LayoutKey( columnNames: columns.map(\.name), + sampleIdentity: window.headIdentity(count: RowGridLayout.sampledRowCount), sample: rows.prefix(RowGridLayout.sampledRowCount), keyColumn: keyColumn, dynamicTypeSize: dynamicTypeSize, @@ -125,7 +128,8 @@ struct RowGrid: View { } var body: some View { - let layout = currentLayout.limitingKeyColumn(toViewportWidth: viewport.width) + let key = layoutKey + let layout = currentLayout(for: key).limitingKeyColumn(toViewportWidth: viewport.width) ScrollView([.horizontal, .vertical]) { grid(layout, width: max(layout.totalWidth, viewport.width)) } @@ -140,14 +144,13 @@ struct RowGrid: View { viewport = newViewport } // Reads only the key: the closure's other captures belong to the previous body. - .onChange(of: layoutKey, initial: true) { _, key in + .onChange(of: key, initial: true) { _, key in computedLayout = (key, Self.makeLayout(key)) } } // Until onChange stores the layout for these inputs, it is measured here, so no pass draws an empty grid. - private var currentLayout: RowGridLayout { - let key = layoutKey + private func currentLayout(for key: LayoutKey) -> RowGridLayout { if let computedLayout, computedLayout.key == key { return computedLayout.layout } return Self.makeLayout(key) } @@ -156,7 +159,7 @@ struct RowGrid: View { let measure = RowGridTextMeasure(dynamicTypeSize: key.dynamicTypeSize) return RowGridLayout.make( columnNames: key.columnNames, - rows: key.sample, + rows: key.sample.map { $0.displayValues() }, keyColumn: key.keyColumn, metrics: key.metrics, measure: measure.width(of:isHeader:) @@ -167,9 +170,10 @@ struct RowGrid: View { let style = style return LazyVStack(alignment: .leading, spacing: 0, pinnedViews: [.sectionHeaders]) { Section { - ForEach(IndexedRow.wrap(rows)) { item in - row(item.id, RowGridLine( - columns: columns, values: item.values, layout: layout, width: width, style: style + // Indices of the same array the closure reads, so a result that shrank cannot put one out of range. + ForEach(rows.indices, id: \.self) { index in + row(index, RowGridLine( + columns: columns, values: rows[index].displayValues(), layout: layout, width: width, style: style )) } } header: { @@ -202,12 +206,20 @@ struct RowGrid: View { .accessibilityAddTraits(.isHeader) } + // The sample rows are compared by where they sit in the window, so a body pass reads none of their text. private struct LayoutKey: Equatable { let columnNames: [String] - let sample: ArraySlice<[String?]> + let sampleIdentity: RowWindow.HeadIdentity + let sample: ArraySlice let keyColumn: Int? let dynamicTypeSize: DynamicTypeSize let metrics: RowGridMetrics + + static func == (lhs: LayoutKey, rhs: LayoutKey) -> Bool { + lhs.columnNames == rhs.columnNames && lhs.sampleIdentity == rhs.sampleIdentity + && lhs.keyColumn == rhs.keyColumn && lhs.dynamicTypeSize == rhs.dynamicTypeSize + && lhs.metrics == rhs.metrics + } } } diff --git a/apple/TableProMobile/TableProMobile/Views/DataBrowserView.swift b/apple/TableProMobile/TableProMobile/Views/DataBrowserView.swift index 00f7d21e11..faa7f6a31c 100644 --- a/apple/TableProMobile/TableProMobile/Views/DataBrowserView.swift +++ b/apple/TableProMobile/TableProMobile/Views/DataBrowserView.swift @@ -70,7 +70,7 @@ struct DataBrowserView: View { } private var columns: [ColumnInfo] { viewModel.columns } - private var rows: [[String?]] { viewModel.legacyRows } + private var rows: [Row] { viewModel.window.rows } private var pageReads: DataBrowserViewModel.SessionReadTracker { let viewModel = viewModel @@ -130,10 +130,11 @@ struct DataBrowserView: View { viewModel.seedFilters(filters) } await viewModel.load(isInitial: true) - guard reference != nil, !Task.isCancelled else { return } - if viewModel.takeSingleRowMatch() { - showMatchedRow = true - } + } + // Pushed when the one row lands, not after the key read and the count that follow it. + .onChange(of: viewModel.hasSingleRowMatch) { _, found in + guard found, reference != nil, viewModel.takeSingleRowMatch() else { return } + showMatchedRow = true } // A redial or another window's database switch hands this table a new session; a referenced table keeps its schema. .onChange(of: BrowseSource(session: session, schema: browserSchema)) { _, source in @@ -283,6 +284,17 @@ struct DataBrowserView: View { } else if rows.isEmpty, viewModel.isPageLoading { ProgressView() .frame(maxWidth: .infinity, maxHeight: .infinity) + } else if viewModel.isPastTheEnd { + ContentUnavailableView { + Label("No Rows on This Page", systemImage: "tray") + } description: { + Text("This page is past the last row.") + } actions: { + Button("Go to First Page") { + Task { await viewModel.goToPage(1) } + } + .buttonStyle(.borderedProminent) + } } else if rows.isEmpty, viewModel.hasActiveSearch { ContentUnavailableView.search(text: viewModel.activeSearchText) } else if rows.isEmpty { @@ -316,10 +328,10 @@ struct DataBrowserView: View { } private var rowList: some View { - let indexed = IndexedRow.wrap(rows) + let rows = rows return List { - ForEach(indexed) { item in - rowLink(index: item.id, row: item.values) + ForEach(rows.indices, id: \.self) { index in + rowLink(index: index, row: rows[index].displayValues()) } } .listStyle(.plain) @@ -329,7 +341,7 @@ struct DataBrowserView: View { let primaryKeys = primaryKeyNames return RowGrid( columns: columns, - rows: rows, + window: viewModel.window, keyColumn: RowKey.columnIndex(columns: columns, primaryKeys: primaryKeys) ) { index, line in NavigationLink { @@ -345,7 +357,7 @@ struct DataBrowserView: View { RowCard( columns: columns, columnDetails: viewModel.columnDetails, - row: rows.indices.contains(index) ? rows[index] : [], + row: rows.indices.contains(index) ? rows[index].displayValues() : [], previewFieldCount: DuoLayoutResolver.regularPreviewFieldCount ) .padding() @@ -370,7 +382,8 @@ struct DataBrowserView: View { } private func rowDetail(index: Int) -> some View { - RowDetailView( + let viewModel = viewModel + return RowDetailView( columns: columns, rows: viewModel.window.rows, initialIndex: index, @@ -450,12 +463,8 @@ struct DataBrowserView: View { Menu("Share Row") { ForEach(ExportFormat.offered(for: connection.type)) { format in Button(format.rawValue) { - exportCompleted([cells]) { exportColumns, completed in - shareText = try ClipboardExporter.exportRow( - columns: exportColumns, cells: completed[0], - format: format, tableName: table.name, - databaseType: connection.type, driver: session?.driver - ) + exportCompleted([cells], asOneRow: true, format: format) { text in + shareText = text showShareSheet = true } } @@ -465,12 +474,7 @@ struct DataBrowserView: View { Menu("Copy Row") { ForEach(ExportFormat.offered(for: connection.type)) { format in Button(format.rawValue) { - exportCompleted([cells]) { exportColumns, completed in - let text = try ClipboardExporter.exportRow( - columns: exportColumns, cells: completed[0], - format: format, tableName: table.name, - databaseType: connection.type, driver: session?.driver - ) + exportCompleted([cells], asOneRow: true, format: format) { text in ClipboardExporter.copyToClipboard(text) } } @@ -516,12 +520,7 @@ struct DataBrowserView: View { Section("Export") { ForEach(ExportFormat.offered(for: connection.type)) { format in Button { - exportCompleted(viewModel.window.rows.map(\.cells)) { exportColumns, completed in - let text = try ClipboardExporter.exportRows( - columns: exportColumns, rows: completed, - format: format, tableName: table.name, - databaseType: connection.type, driver: session?.driver - ) + exportCompleted(viewModel.window.rows.map(\.cells), asOneRow: false, format: format) { text in ClipboardExporter.copyToClipboard(text) } } label: { @@ -644,11 +643,15 @@ struct DataBrowserView: View { // Rows that cannot all be completed skip the reads: the exporter refuses them anyway. private func exportCompleted( _ pageRows: [[Cell]], - export: @escaping @MainActor ([ColumnInfo], [[Cell]]) throws -> Void + asOneRow: Bool, + format: ExportFormat, + deliver: @escaping (String) -> Void ) { guard !isCompletingCells else { return } let exportColumns = columns let driver = session?.driver + let tableName = table.name + let databaseType = connection.type isCompletingCells = true Task { defer { isCompletingCells = false } @@ -657,7 +660,10 @@ struct DataBrowserView: View { if let driver, pageRows.allSatisfy(DataBrowserViewModel.canComplete) { completed = try await viewModel.completeRows(pageRows, driver: driver) } - try export(exportColumns, completed) + deliver(try await Self.exportText( + completed, columns: exportColumns, asOneRow: asOneRow, format: format, + tableName: tableName, databaseType: databaseType, driver: driver + )) } catch let error where error is ClipboardExporter.IncompleteValues || error is ClipboardExporter.TooLarge || error is CellLookupError { viewModel.operationError = AppError( @@ -678,6 +684,29 @@ struct DataBrowserView: View { } } + // Off the main actor, so a page of 500 rows formats without holding up the screen. + @concurrent + nonisolated private static func exportText( + _ rows: [[Cell]], + columns: [ColumnInfo], + asOneRow: Bool, + format: ExportFormat, + tableName: String, + databaseType: DatabaseType, + driver: (any DatabaseDriver)? + ) async throws -> String { + if asOneRow, let cells = rows.first { + return try ClipboardExporter.exportRow( + columns: columns, cells: cells, format: format, tableName: tableName, + databaseType: databaseType, driver: driver + ) + } + return try ClipboardExporter.exportRows( + columns: columns, rows: rows, format: format, tableName: tableName, + databaseType: databaseType, driver: driver + ) + } + private func performDelete(_ pending: PendingRowDelete) async { if let refusal = coordinator.databaseMoveRefusal { viewModel.operationError = refusal diff --git a/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift b/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift index c5a1de6cef..d1e8e2284b 100644 --- a/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift +++ b/apple/TableProMobile/TableProMobile/Views/QueryEditorView.swift @@ -25,7 +25,7 @@ struct QueryEditorView: View { @State private var executeTask: Task? private var resultRowCount: Int { - viewModel.legacyRows.count + viewModel.window.count } @State private var draft = QueryDraftSaver() @State private var deferredAdds: [String] = [] @@ -45,6 +45,7 @@ struct QueryEditorView: View { @State private var showShareSheet = false @State private var shareText = "" @State private var resultsExportError: (any Error)? + @State private var isExportingResults = false @State private var hapticSuccess = false @State private var hapticError = false @State private var containerSize: CGSize = .zero @@ -413,9 +414,9 @@ struct QueryEditorView: View { } } .frame(maxWidth: .infinity, maxHeight: .infinity) - } else if viewModel.columns.isEmpty || viewModel.legacyRows.isEmpty && !runCompleted { + } else if viewModel.columns.isEmpty || viewModel.window.isEmpty && !runCompleted { commandOutcomeView - } else if viewModel.legacyRows.isEmpty { + } else if viewModel.window.isEmpty { VStack(spacing: 0) { resultNotices(includesTruncation: false) ContentUnavailableView( @@ -559,10 +560,11 @@ struct QueryEditorView: View { private var resultGrid: some View { let columns = viewModel.columns - let rows = viewModel.legacyRows + let window = viewModel.window + let rows = window.rows return RowGrid( columns: columns, - rows: rows, + window: window, keyColumn: RowKey.columnIndex(columns: columns, primaryKeys: []) ) { index, line in NavigationLink { @@ -574,7 +576,7 @@ struct QueryEditorView: View { .contextMenu { resultRowContextMenu(columns: columns, rowIndex: index) } preview: { - resultRowCard(columns: columns, row: rows.indices.contains(index) ? rows[index] : []) + resultRowCard(columns: columns, row: rows.indices.contains(index) ? rows[index].displayValues() : []) .padding() .frame(maxWidth: 360, alignment: .leading) } @@ -582,18 +584,17 @@ struct QueryEditorView: View { } private var resultList: some View { - let indexed = IndexedRow.wrap(viewModel.legacyRows) + let columns = viewModel.columns + let rows = viewModel.window.rows return List { - ForEach(indexed) { item in - let rowIndex = item.id - let row = item.values + ForEach(rows.indices, id: \.self) { rowIndex in NavigationLink { - resultRowDetail(columns: viewModel.columns, initialIndex: rowIndex) + resultRowDetail(columns: columns, initialIndex: rowIndex) } label: { - resultRowCard(columns: viewModel.columns, row: row) + resultRowCard(columns: columns, row: rows[rowIndex].displayValues()) } .contextMenu { - resultRowContextMenu(columns: viewModel.columns, rowIndex: rowIndex) + resultRowContextMenu(columns: columns, rowIndex: rowIndex) } } } @@ -711,34 +712,31 @@ struct QueryEditorView: View { } } - if !viewModel.legacyRows.isEmpty { + if !viewModel.window.isEmpty { RowLayoutPicker() Section("Share Results") { ForEach(ExportFormat.offered(for: databaseType)) { format in Button { - do { - shareText = try exportedResults(as: format) + exportResults(as: format) { text in + shareText = text showShareSheet = true - } catch { - resultsExportError = error } } label: { Label(format.rawValue, systemImage: "square.and.arrow.up") } + .disabled(isExportingResults) } } Section("Copy Results") { ForEach(ExportFormat.offered(for: databaseType)) { format in Button { - do { - let text = try exportedResults(as: format) + exportResults(as: format) { text in ClipboardExporter.copyToClipboard(text) - } catch { - resultsExportError = error } } label: { Label(format.rawValue, systemImage: "doc.on.clipboard") } + .disabled(isExportingResults) } } } @@ -769,15 +767,23 @@ struct QueryEditorView: View { } } - private func exportedResults(as format: ExportFormat) throws -> String { - guard viewModel.resultsFitExport(availableBytes: MemoryPressureMonitor.shared.availableMemoryBytes()) else { - throw ClipboardExporter.TooLarge(source: .queryResult) + private func exportResults(as format: ExportFormat, deliver: @escaping (String) -> Void) { + guard !isExportingResults else { return } + isExportingResults = true + let viewModel = viewModel + let databaseType = databaseType + let driver = coordinator.session?.driver + let availableBytes = MemoryPressureMonitor.shared.availableMemoryBytes() + Task { + defer { isExportingResults = false } + do { + deliver(try await viewModel.exportResults( + as: format, databaseType: databaseType, driver: driver, availableBytes: availableBytes + )) + } catch { + resultsExportError = error + } } - return try ClipboardExporter.exportRows( - columns: viewModel.columns, rows: viewModel.window.rows.map(\.cells), - format: format, - databaseType: databaseType, driver: coordinator.session?.driver - ) } // MARK: - Execution @@ -1024,7 +1030,7 @@ struct QueryEditorView: View { while !Task.isCancelled { try? await Task.sleep(for: .seconds(1)) if Task.isCancelled { return } - guard let count = viewModel?.legacyRows.count else { return } + guard let count = viewModel?.window.count else { return } await controller.update(token: token, rowsStreamed: count) } } diff --git a/apple/TableProMobile/TableProMobile/Views/RowDetailView.swift b/apple/TableProMobile/TableProMobile/Views/RowDetailView.swift index f034403e10..999f0b7a16 100644 --- a/apple/TableProMobile/TableProMobile/Views/RowDetailView.swift +++ b/apple/TableProMobile/TableProMobile/Views/RowDetailView.swift @@ -28,7 +28,7 @@ struct RowDetailView: View { databaseType: DatabaseType = .sqlite, schema: String? = nil, safeModeLevel: @escaping () -> SafeModeLevel = { .off }, - foreignKeys: [ForeignKeyInfo] = [], + foreignKeys: @escaping @autoclosure () -> [ForeignKeyInfo] = [], onSaved: (() -> Void)? = nil, loadFullValue: ((CellRef) async throws -> String?)? = nil, coordinator: ConnectionCoordinator? = nil @@ -44,7 +44,7 @@ struct RowDetailView: View { databaseType: databaseType, schema: schema, safeModeLevel: safeModeLevel, - foreignKeys: foreignKeys, + foreignKeys: foreignKeys(), onSaved: onSaved, loadFullValue: loadFullValue, liveSession: coordinator.map { coordinator in { coordinator.writableSession } } @@ -58,9 +58,9 @@ struct RowDetailView: View { rowContent(at: viewModel.currentIndex) } else { TabView(selection: $viewModel.currentIndex) { - ForEach(IndexedRow.wrap(viewModel.rows)) { item in - rowContent(at: item.id) - .tag(item.id) + ForEach(viewModel.rows.indices, id: \.self) { index in + rowContent(at: index) + .tag(index) } } .tabViewStyle(.page(indexDisplayMode: .never)) diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorStatusTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorStatusTests.swift index f37c63eedb..dc40e04fc1 100644 --- a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorStatusTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorStatusTests.swift @@ -550,11 +550,13 @@ struct ConnectionCoordinatorStatusTests { nonisolated enum ScriptedFailure: LocalizedError { case unreachable case refused + case noSuchDatabase var errorDescription: String? { switch self { case .unreachable: "Lost connection to the server during the query" case .refused: "The server refused the request" + case .noSuchDatabase: "FATAL: the requested database does not exist" } } } diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorSwitchTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorSwitchTests.swift index b449b2e087..8db428c9c2 100644 --- a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorSwitchTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorSwitchTests.swift @@ -3,6 +3,7 @@ import TableProConnectionLibrary import TableProDatabase @testable import TableProMobile import TableProModels +import TableProPluginKit import Testing @MainActor @@ -45,6 +46,32 @@ struct ConnectionCoordinatorSwitchTests { return refused } + private func missingDatabaseDriver() -> MockDatabaseDriver { + let refused = driver() + refused.connectError = ScriptedFailure.noSuchDatabase + return refused + } + + private func postgreSQLDriver(tables: [String]) -> MockDatabaseDriver { + let session = driver(tables: tables) + session.scriptedDatabases = ["analytics", "staging"] + session.scriptedSchemas = ["public", "sales"] + return session + } + + // Where the connection was when it last closed, which the first window restores. + private func remember(database: String, schema: String? = nil, for record: DatabaseConnection) { + UserDefaults.standard.set(database, forKey: ConnectionDefaultsKey.lastDB.name(for: record.id)) + if let schema { + UserDefaults.standard.set(schema, forKey: ConnectionDefaultsKey.lastSchema.name(for: record.id)) + } + } + + private func forget(_ record: DatabaseConnection) { + UserDefaults.standard.removeObject(forKey: ConnectionDefaultsKey.lastDB.name(for: record.id)) + UserDefaults.standard.removeObject(forKey: ConnectionDefaultsKey.lastSchema.name(for: record.id)) + } + private func attachedCoordinator(to record: DatabaseConnection, on first: MockDatabaseDriver) async throws -> ConnectionCoordinator { factory.queue(first) _ = try await appState.connectionManager.connect(record) @@ -155,34 +182,269 @@ struct ConnectionCoordinatorSwitchTests { #expect(coordinator.displayStatus == .lost(cause: ScriptedFailure.unreachable.localizedDescription)) } - @Test("An open whose saved database cannot be restored, and whose way back fails too, is lost and redials on the next trigger") - func failedRestoreDuringAnOpenIsLost() async { - let lastDB = ConnectionDefaultsKey.lastDB.name(for: warehouse.id) - UserDefaults.standard.set("staging", forKey: lastDB) - defer { UserDefaults.standard.removeObject(forKey: lastDB) } - let first = driver(tables: ["facts"]) - first.scriptedDatabases = ["analytics", "staging"] - factory.queue(first) + @Test("An open whose remembered and saved databases both refuse fails as a connect does, and the next connect dials the remembered one again") + func refusedRememberedAndSavedDatabasesFailTheOpen() async { + remember(database: "staging", for: warehouse) + defer { forget(warehouse) } + factory.queue(missingDatabaseDriver()) factory.queue(refusingDriver()) + let coordinator = ConnectionCoordinator(connection: warehouse, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["staging", "analytics"]) + #expect(coordinator.session == nil) + #expect(coordinator.displayStatus == .failed(cause: ScriptedFailure.unreachable.localizedDescription)) + #expect(!coordinator.showFailureAlert) + + let recovered = driver(tables: ["staging_facts"]) + factory.queue(recovered) + await coordinator.connect() + + #expect(factory.dialed.count == 3) + #expect(factory.dialed.last?.database == "staging") + #expect(coordinator.session?.driver === recovered) + #expect(coordinator.activeDatabase == "staging") + #expect(coordinator.displayStatus == .connected) + } + + // MARK: - Open + + @Test("A PostgreSQL open dials the remembered database once, restores the schema inside the dial and reads each list once") + func openDialsTheRememberedPlacement() async { + remember(database: "staging", schema: "sales", for: warehouse) + defer { forget(warehouse) } + let session = postgreSQLDriver(tables: ["sales_facts"]) + factory.queue(session) + let coordinator = ConnectionCoordinator(connection: warehouse, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["staging"]) + #expect(session.switchedSchemas == ["sales"]) + #expect(session.fetchTablesSchemas == [nil]) + #expect(session.fetchSchemasCalls == 1) + #expect(session.fetchDatabasesCalls == 1) + #expect(session.transactionStateReads == 1) + #expect(coordinator.session?.driver === session) + #expect(coordinator.activeDatabase == "staging") + #expect(coordinator.activeSchema == "sales") + #expect(coordinator.schemas == ["public", "sales"]) + #expect(tableNames(coordinator) == ["sales_facts"]) + #expect(!coordinator.showFailureAlert) + } + + @Test("A MySQL open dials the saved database and moves to the remembered one with USE") + func mySQLOpenUsesTheRememberedDatabase() async { + remember(database: "app_staging", for: connection) + defer { forget(connection) } + let session = driver(tables: ["staging_orders"]) + session.scriptedDatabases = ["app_prod", "app_staging"] + factory.queue(session) + let coordinator = ConnectionCoordinator(connection: connection, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["app_prod"]) + #expect(session.switchedDatabases == ["app_staging"]) + #expect(coordinator.activeDatabase == "app_staging") + #expect(coordinator.tableQualifier == "app_staging") + } + + @Test("A remembered database that refuses the open falls back to the saved one once and says why") + func refusedRememberedDatabaseFallsBackOnce() async { + remember(database: "staging", for: warehouse) + defer { forget(warehouse) } + let fallback = postgreSQLDriver(tables: ["facts"]) + factory.queue(missingDatabaseDriver()) + factory.queue(fallback) + let coordinator = ConnectionCoordinator(connection: warehouse, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["staging", "analytics"]) + #expect(coordinator.session?.driver === fallback) + #expect(coordinator.displayStatus == .connected) + #expect(coordinator.activeDatabase == "analytics") + #expect(tableNames(coordinator) == ["facts"]) + #expect(coordinator.failureAlertTitle == ConnectionCoordinator.switchDatabaseFailedTitle) + #expect(coordinator.failureAlertMessage == ScriptedFailure.noSuchDatabase.localizedDescription) + #expect(UserDefaults.standard.string(forKey: ConnectionDefaultsKey.lastDB.name(for: warehouse.id)) == "staging") + } + + @Test("A remembered database the open cannot reach fails it after one dial, without dialing the saved one") + func unreachableRememberedDatabaseDialsOnce() async { + remember(database: "staging", for: warehouse) + defer { forget(warehouse) } factory.queue(refusingDriver()) let coordinator = ConnectionCoordinator(connection: warehouse, appState: appState) coordinator.restorePersistedState() await coordinator.connect() - #expect(factory.dialed.suffix(2).map { $0.database } == ["staging", "analytics"]) + #expect(factory.dialed.map { $0.database } == ["staging"]) #expect(coordinator.session == nil) - #expect(coordinator.displayStatus == .lost(cause: ScriptedFailure.unreachable.localizedDescription)) + #expect(coordinator.displayStatus == .failed(cause: ScriptedFailure.unreachable.localizedDescription)) - let recovered = driver(tables: ["facts"]) + let recovered = driver(tables: ["staging_facts"]) factory.queue(recovered) - await coordinator.reconnectIfNeeded() + await coordinator.connect() - #expect(factory.dialed.last?.database == "analytics") + #expect(factory.dialed.map { $0.database } == ["staging", "staging"]) #expect(coordinator.session?.driver === recovered) #expect(coordinator.displayStatus == .connected) } + @Test("A PostgreSQL refusal goes to the saved database unless it came from the network, the device or the sign-in") + func fallbackFollowsTheRefusal() { + #expect(ConnectionSessionModel.mayBlameDatabase(ScriptedFailure.noSuchDatabase)) + #expect(ConnectionSessionModel.mayBlameDatabase(ScriptedFailure.refused)) + #expect(ConnectionSessionModel.mayBlameDatabase(Refusal(message: "permission denied for database \"staging\""))) + #expect(ConnectionSessionModel.mayBlameDatabase(Refusal(message: "FATAL: la base de données « staging » n'existe pas"))) + #expect(!ConnectionSessionModel.mayBlameDatabase(ScriptedFailure.unreachable)) + #expect(!ConnectionSessionModel.mayBlameDatabase(URLError(.timedOut))) + #expect(!ConnectionSessionModel.mayBlameDatabase(POSIXError(.ETIMEDOUT))) + #expect(!ConnectionSessionModel.mayBlameDatabase(PostgreSQLError.unreachable("연결 시간이 초과되었습니다."))) + #expect(!ConnectionSessionModel.mayBlameDatabase(SSHTunnelError.hostKeyRejected("db.example.com"))) + #expect(!ConnectionSessionModel.mayBlameDatabase(CancellationError())) + #expect(!ConnectionSessionModel.mayBlameDatabase(Refusal(message: "password authentication failed for user \"app\""))) + #expect(!ConnectionSessionModel.mayBlameDatabase(Refusal( + message: "password authentication failed for user \"app\"\nno pg_hba.conf entry for host \"10.0.0.2\", user \"app\", database \"staging\", no encryption" + ))) + #expect(!ConnectionSessionModel.mayBlameDatabase(Refusal(message: "could not translate host name \"db.example\" to address"))) + } + + @Test("A remembered database whose table list is refused falls back to the saved one and says why") + func refusedRememberedTableListFallsBack() async { + remember(database: "staging", for: warehouse) + defer { forget(warehouse) } + let refusing = driver() + refusing.fetchTablesError = ScriptedFailure.refused + let fallback = postgreSQLDriver(tables: ["facts"]) + factory.queue(refusing) + factory.queue(fallback) + let coordinator = ConnectionCoordinator(connection: warehouse, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["staging", "analytics"]) + #expect(appState.connectionManager.session(for: warehouse.id)?.driver === fallback) + #expect(coordinator.session?.driver === fallback) + #expect(coordinator.activeDatabase == "analytics") + #expect(coordinator.failureAlertTitle == ConnectionCoordinator.switchDatabaseFailedTitle) + #expect(coordinator.failureAlertMessage == ScriptedFailure.refused.localizedDescription) + } + + @Test("A remembered database the server no longer lists falls back quietly and is forgotten") + func droppedRememberedDatabaseIsForgotten() async { + remember(database: "archive", for: warehouse) + defer { forget(warehouse) } + let fallback = postgreSQLDriver(tables: ["facts"]) + factory.queue(missingDatabaseDriver()) + factory.queue(fallback) + let coordinator = ConnectionCoordinator(connection: warehouse, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["archive", "analytics"]) + #expect(coordinator.session?.driver === fallback) + #expect(coordinator.activeDatabase == "analytics") + #expect(!coordinator.showFailureAlert) + #expect(UserDefaults.standard.string(forKey: ConnectionDefaultsKey.lastDB.name(for: warehouse.id)) == nil) + } + + @Test("A SQL Server open that needs a sign-in dials the saved database once and offers it") + func signInOnTheRememberedDatabaseSkipsTheFallback() async throws { + let ledger = DatabaseConnection(name: "Ledger", type: .mssql, host: "sql.example.com", port: 1_433, database: "app") + remember(database: "history", for: ledger) + defer { forget(ledger) } + let expired = driver() + expired.connectError = EntraOAuthError.signInRequired + factory.queue(expired) + let coordinator = ConnectionCoordinator(connection: ledger, appState: appState) + coordinator.restorePersistedState() + + let open = Task { await coordinator.connect() } + await ObservedCondition.wait { coordinator.prompts.current != nil } + let offer = try #require(coordinator.prompts.current) + coordinator.prompts.answer(offer.id, accepted: false) + await open.value + + #expect(factory.dialed.map { $0.database } == ["app"]) + #expect(coordinator.session == nil) + #expect(!coordinator.showFailureAlert) + guard case .failed = coordinator.phase else { + Issue.record("Expected the declined sign-in to fail the open") + return + } + } + + // lastSchema is kept per connection, so it can name a schema of another database. + @Test("A remembered schema the database does not list is not restored") + func unlistedRememberedSchemaStaysUnrestored() async { + remember(database: "staging", schema: "etl", for: warehouse) + defer { forget(warehouse) } + let session = postgreSQLDriver(tables: ["staging_facts"]) + factory.queue(session) + let coordinator = ConnectionCoordinator(connection: warehouse, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["staging"]) + #expect(session.switchedSchemas.isEmpty) + #expect(session.fetchSchemasCalls == 1) + #expect(coordinator.activeSchema == "public") + #expect(tableNames(coordinator) == ["staging_facts"]) + } + + @Test("A PostgreSQL open with nothing remembered dials the saved database and switches nothing") + func openWithNothingRememberedDialsTheSavedDatabase() async { + let session = postgreSQLDriver(tables: ["facts"]) + factory.queue(session) + let coordinator = ConnectionCoordinator(connection: warehouse, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["analytics"]) + #expect(session.switchedSchemas.isEmpty) + #expect(session.fetchTablesSchemas == [nil]) + #expect(session.fetchSchemasCalls == 1) + #expect(session.fetchDatabasesCalls == 1) + #expect(session.transactionStateReads == 1) + #expect(coordinator.activeDatabase == "analytics") + #expect(coordinator.activeSchema == "public") + #expect(UserDefaults.standard.string(forKey: ConnectionDefaultsKey.lastDB.name(for: warehouse.id)) == nil) + #expect(UserDefaults.standard.string(forKey: ConnectionDefaultsKey.lastSchema.name(for: warehouse.id)) == nil) + } + + // The placement's schema reads "public" until a session says otherwise. + @Test("A SQL Server open with nothing remembered stays on the server's default schema") + func sqlServerOpenKeepsItsDefaultSchema() async { + let ledger = DatabaseConnection(name: "Ledger", type: .mssql, host: "sql.example.com", port: 1_433, database: "app") + let session = driver(tables: ["orders"]) + session.currentSchema = "dbo" + session.scriptedSchemas = ["dbo", "sales"] + factory.queue(session) + let coordinator = ConnectionCoordinator(connection: ledger, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(session.switchedSchemas.isEmpty) + #expect(session.fetchSchemasCalls == 1) + #expect(coordinator.activeSchema == "dbo") + #expect(coordinator.schemas == ["dbo", "sales"]) + #expect(tableNames(coordinator) == ["orders"]) + } + @Test("A refresh that fails on a dead session redials and shows the new list without an alert") func refreshOnADeadSessionRedials() async throws { let first = driver(tables: ["orders"]) @@ -213,3 +475,9 @@ struct ConnectionCoordinatorSwitchTests { #expect(coordinator.failureAlertMessage == ScriptedFailure.refused.localizedDescription) } } + +private struct Refusal: LocalizedError { + let message: String + + var errorDescription: String? { message } +} diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionLibrary/ConnectionLibraryPublisherTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionLibrary/ConnectionLibraryPublisherTests.swift index f4072639ef..47a486aafd 100644 --- a/apple/TableProMobile/TableProMobileTests/ConnectionLibrary/ConnectionLibraryPublisherTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ConnectionLibrary/ConnectionLibraryPublisherTests.swift @@ -7,20 +7,33 @@ import Testing @MainActor @Suite("Connection library publisher") struct ConnectionLibraryPublisherTests { + // Outlives a publisher, as the app group file and the defaults outlive a launch. private final class Recorder { var shortcutRefreshes = 0 + var widgetFile: [WidgetConnectionItem] = [] var widgetWrites: [[WidgetConnectionItem]] = [] } private let index = RecordingSearchIndex() private let recorder = Recorder() + private let defaults: UserDefaults - private func makePublisher() -> ConnectionLibraryPublisher { + init() throws { + defaults = try #require(UserDefaults(suiteName: "com.TablePro.tests.LibraryPublisher.\(UUID().uuidString)")) + } + + private func makePublisher(build: String = "1") -> ConnectionLibraryPublisher { let recorder = recorder return ConnectionLibraryPublisher( searchIndex: index, - writeWidgetItems: { recorder.widgetWrites.append($0) }, - refreshShortcutParameters: { recorder.shortcutRefreshes += 1 } + readWidgetItems: { recorder.widgetFile }, + writeWidgetItems: { items in + recorder.widgetFile = items + recorder.widgetWrites.append(items) + }, + refreshShortcutParameters: { recorder.shortcutRefreshes += 1 }, + defaults: defaults, + build: build ) } @@ -145,6 +158,101 @@ struct ConnectionLibraryPublisherTests { await publisher.settle() } + @Test("A launch over an unchanged library touches neither Spotlight, the widget nor Shortcuts") + func unchangedLibraryOnRelaunchPublishesNothing() async { + let library = [connection("A"), connection("B", sortOrder: 1)] + let first = makePublisher() + first.publish(library) + await first.settle() + #expect(index.replacements.count == 1) + #expect(recorder.widgetWrites.count == 1) + #expect(recorder.shortcutRefreshes == 1) + + let relaunched = makePublisher() + relaunched.publish(library) + await relaunched.settle() + + #expect(index.replacements.count == 1) + #expect(recorder.widgetWrites.count == 1) + #expect(recorder.shortcutRefreshes == 1) + #expect(index.stateReads == 2) + #expect(index.indexedIds == Set(library.map(\.id))) + } + + @Test("A library changed between launches is published again on the next one") + func changedLibraryOnRelaunchPublishes() async { + let kept = connection("A") + let first = makePublisher() + first.publish([kept]) + await first.settle() + + var renamed = kept + renamed.name = "Renamed" + let added = connection("B", sortOrder: 1) + let relaunched = makePublisher() + relaunched.publish([renamed, added]) + await relaunched.settle() + + #expect(index.replacements.count == 2) + #expect(index.indexedIds == [kept.id, added.id]) + #expect(index.contents.first { $0.id == kept.id }?.title == "Renamed") + #expect(recorder.widgetWrites.count == 2) + #expect(recorder.widgetFile.map(\.name) == ["Renamed", "B"]) + #expect(recorder.shortcutRefreshes == 2) + } + + @Test("A launch that cannot read the index's state writes the index rather than trust it") + func unreadableStateReindexes() async { + let library = [connection("A")] + let first = makePublisher() + first.publish(library) + await first.settle() + + index.failNextStateRead() + let relaunched = makePublisher() + relaunched.publish(library) + await relaunched.settle() + + #expect(index.replacements.count == 2) + #expect(index.indexedIds == [library[0].id]) + } + + @Test("A new build indexes and refreshes Shortcuts once, and leaves an unchanged widget file alone") + func newBuildPublishesOnce() async { + let library = [connection("A")] + let first = makePublisher(build: "1") + first.publish(library) + await first.settle() + + let updated = makePublisher(build: "2") + updated.publish(library) + await updated.settle() + let relaunched = makePublisher(build: "2") + relaunched.publish(library) + await relaunched.settle() + + #expect(index.replacements.count == 2) + #expect(recorder.shortcutRefreshes == 2) + #expect(recorder.widgetWrites.count == 1) + } + + @Test("A widget file written by an older build is rewritten once") + func olderWidgetFileIsRewritten() async { + let current = connection("A") + recorder.widgetFile = [ + WidgetConnectionItem(id: current.id, name: "A", type: current.type.rawValue, sortOrder: 0, glyph: nil) + ] + + for _ in 0..<2 { + let publisher = makePublisher() + publisher.publish([current]) + await publisher.settle() + } + + #expect(recorder.widgetWrites.count == 1) + #expect(recorder.widgetFile.first?.glyph != nil) + } + @Test("Widget items sort by order, then name, and a blank name shows the host") func widgetItemMapping() { let unnamed = DatabaseConnection(name: "", type: .mysql, host: "cache.local", sortOrder: 1) diff --git a/apple/TableProMobile/TableProMobileTests/DataBrowserDriverBuiltTests.swift b/apple/TableProMobile/TableProMobileTests/DataBrowserDriverBuiltTests.swift index 578ada74cb..c21b319ac7 100644 --- a/apple/TableProMobile/TableProMobileTests/DataBrowserDriverBuiltTests.swift +++ b/apple/TableProMobile/TableProMobileTests/DataBrowserDriverBuiltTests.swift @@ -44,7 +44,55 @@ struct DataBrowserDriverBuiltTests { #expect(page.request.searchText == nil) #expect(driver.countRequests.count == 1) #expect(viewModel.pagination.totalRows == 420) - #expect(viewModel.legacyRows.count == viewModel.pagination.pageSize) + #expect(viewModel.window.shownValues.count == viewModel.pagination.pageSize) + } + + @Test("A page turn while the count runs keeps that count instead of starting another") + func pageTurnKeepsTheRunningCount() async { + let driver = MockQueryBuildingDriver() + driver.scriptedCount = 5_000 + let counting = TestGate() + driver.beforeCount = { await counting.enter() } + let viewModel = browser(driver) + driver.scriptedRows = fullPage(viewModel) + + let firstLoad = Task { await viewModel.load(isInitial: true) } + await counting.waitUntilEntered() + #expect(viewModel.isLoading == false) + await viewModel.goToNextPage() + #expect(viewModel.pagination.currentPage == 1) + #expect(driver.countRequests.count == 1) + + await counting.open() + await firstLoad.value + #expect(viewModel.pagination.totalRows == 5_000) + #expect(driver.countRequests.count == 1) + } + + @Test("A page past the last row keeps the pager and sets no total, and the count still lands") + func pagePastTheEndSetsNoTotal() async { + let driver = MockQueryBuildingDriver() + let counting = TestGate() + driver.beforeCount = { await counting.enter() } + let viewModel = browser(driver) + let pageSize = viewModel.pagination.pageSize + driver.scriptedCount = pageSize + driver.scriptedRows = fullPage(viewModel) + + let firstLoad = Task { await viewModel.load(isInitial: true) } + await counting.waitUntilEntered() + driver.scriptedRows = [] + await viewModel.goToNextPage() + + #expect(viewModel.isPastTheEnd) + #expect(viewModel.pagination.totalRows == nil) + #expect(viewModel.showsPaginationBar) + #expect(viewModel.canGoToPreviousPage) + #expect(!viewModel.canGoToNextPage) + + await counting.open() + await firstLoad.value + #expect(viewModel.pagination.totalRows == pageSize) } @Test("Search and the enabled filters reach the driver; binary columns are not searched") diff --git a/apple/TableProMobile/TableProMobileTests/DataBrowserViewModelTests.swift b/apple/TableProMobile/TableProMobileTests/DataBrowserViewModelTests.swift index c145e8f609..45ef4d764d 100644 --- a/apple/TableProMobile/TableProMobileTests/DataBrowserViewModelTests.swift +++ b/apple/TableProMobile/TableProMobileTests/DataBrowserViewModelTests.swift @@ -54,7 +54,7 @@ struct DataBrowserViewModelTests { vm.attach(session: makeSession(driver: driver), table: TableInfo(name: "users"), databaseType: .mysql, host: "localhost") await vm.load(isInitial: true) - #expect(vm.legacyRows.count == 2) + #expect(vm.window.shownValues.count == 2) #expect(vm.columnDetails.count == 2) #expect(vm.hasPrimaryKeys == true) #expect(vm.loadError == nil) @@ -85,7 +85,7 @@ struct DataBrowserViewModelTests { #expect(log.tracked == 1) #expect(log.callsInside == driver.fetchColumnsCalls + driver.executedQueries.count) #expect(log.callsInside > 0) - #expect(vm.legacyRows.count == 1) + #expect(vm.window.shownValues.count == 1) } @Test("A page cut short is not taken as the end of the table") @@ -103,7 +103,7 @@ struct DataBrowserViewModelTests { vm.attach(session: makeSession(driver: driver), table: TableInfo(name: "users"), databaseType: .mysql, host: "localhost") await vm.load(isInitial: true) - #expect(vm.legacyRows.count == 1) + #expect(vm.window.shownValues.count == 1) #expect(vm.pagination.totalRows == 120) #expect(vm.canGoToNextPage) } @@ -150,7 +150,7 @@ struct DataBrowserViewModelTests { let vm = DataBrowserViewModel() vm.attach(session: makeSession(driver: driver), table: TableInfo(name: "users"), databaseType: .mysql, host: "localhost") await vm.load(isInitial: true) - #expect(vm.legacyRows.count == 1) + #expect(vm.window.shownValues.count == 1) #expect(vm.isLoading == false) #expect(vm.isPageLoading == false) @@ -162,7 +162,7 @@ struct DataBrowserViewModelTests { #expect(vm.isLoading == false) #expect(vm.isPageLoading == false) - #expect(vm.legacyRows.count == 2) + #expect(vm.window.shownValues.count == 2) } @Test("pagination prev/next clamps at boundaries") @@ -220,7 +220,7 @@ struct DataBrowserViewModelTests { vm.attach(session: makeSession(driver: driver), table: TableInfo(name: "customers"), databaseType: .mysql, host: "localhost") await vm.load(isInitial: true) - #expect(vm.legacyRows.count == 1) + #expect(vm.window.shownValues.count == 1) #expect(vm.rowLocator(forRowAt: 0) == nil) } @@ -411,6 +411,67 @@ struct DataBrowserViewModelTests { #expect((userDriver.executedQueries.first ?? "").contains("\"code\" IN (5, '5')")) } + private func fullPage(_ vm: DataBrowserViewModel, from start: Int = 0) -> Result { + let rows = (start.. Result { + .success(QueryResult(columns: [], rows: [[String(total)]], rowsAffected: 0, executionTime: 0)) + } + + @Test("The rows show and take taps while the row count still runs") + func rowsShowBeforeTheCount() async { + let driver = MockDatabaseDriver() + driver.scriptedColumns = makeColumns() + let gate = ExecuteGate(holding: [2]) + driver.beforeExecute = { await gate.pass() } + let vm = DataBrowserViewModel() + driver.scriptedExecuteResults = [fullPage(vm), rowCount(5_000)] + vm.attach(session: makeSession(driver: driver), table: TableInfo(name: "users"), databaseType: .mysql, host: "localhost") + + let load = Task { await vm.load(isInitial: true) } + await gate.arrival(of: 2) + + #expect(vm.isLoading == false) + #expect(vm.isPageLoading == false) + #expect(vm.window.count == vm.pagination.pageSize) + #expect(driver.fetchForeignKeysCalls == 1) + #expect(vm.pagination.totalRows == nil) + #expect(vm.canGoToNextPage) + #expect(vm.paginationLabel == "1-\(vm.pagination.pageSize)") + + await gate.release(2) + await load.value + #expect(vm.pagination.totalRows == 5_000) + } + + @Test("A sort or a new page size keeps the total, and a page turn reads the keys of a table without any only once") + func reloadsReadNoCountOrKeysAgain() async { + let driver = MockDatabaseDriver() + driver.scriptedColumns = makeColumns() + let vm = DataBrowserViewModel() + driver.scriptedExecuteResults = [fullPage(vm), rowCount(5_000)] + vm.attach(session: makeSession(driver: driver), table: TableInfo(name: "users"), databaseType: .mysql, host: "localhost") + await vm.load(isInitial: true) + #expect(driver.executedQueries.count == 2) + + driver.scriptedExecuteResults = [fullPage(vm)] + vm.sortState.columns = [SortColumn(name: "name", ascending: false)] + await vm.applySort() + #expect(driver.executedQueries.count == 3) + #expect(vm.pagination.totalRows == 5_000) + + driver.scriptedExecuteResults = [fullPage(vm, from: vm.pagination.pageSize)] + await vm.goToNextPage() + #expect(driver.executedQueries.count == 4) + #expect(driver.fetchForeignKeysCalls == 1) + + await vm.changePageSize(50) + #expect(driver.executedQueries.count == 5) + #expect(vm.pagination.totalRows == 5_000) + } + @Test("A load overtaken during its row count leaves the newer load's total and loading state alone") func overtakenCountIsDropped() async { let driver = MockDatabaseDriver() @@ -440,7 +501,7 @@ struct DataBrowserViewModelTests { await gate.release(3) await filtered.value #expect(vm.pagination.totalRows == 1) - #expect(vm.legacyRows.count == 1) + #expect(vm.window.shownValues.count == 1) #expect(vm.isLoading == false) #expect(vm.isPageLoading == false) } @@ -559,7 +620,7 @@ struct DataBrowserViewModelTests { #expect(success == false) #expect(vm.operationError?.title == String(localized: "Row Not Deleted")) #expect(driver.executedQueries.contains { $0.hasPrefix("SELECT `id` FROM `users` WHERE `id` = 1") }) - #expect(vm.legacyRows == [["1", "Alice"]]) + #expect(vm.window.shownValues == [["1", "Alice"]]) } @Test("A delete whose count reads 0, as under SET NOCOUNT ON, succeeds once the row is gone") @@ -800,7 +861,7 @@ struct DataBrowserViewModelTests { } } - @Test("changePageSize resets currentPage and totalRows") + @Test("changePageSize returns to the first page") func changePageSizeResets() async { let driver = MockDatabaseDriver() driver.scriptedColumns = makeColumns() @@ -854,7 +915,7 @@ struct DataBrowserViewModelTests { driver.scriptedExecuteResults = [emptyResult()] await vm.applySearch("nobody") - #expect(vm.legacyRows.isEmpty) + #expect(vm.window.shownValues.isEmpty) #expect(vm.showsPaginationBar) } @@ -871,7 +932,7 @@ struct DataBrowserViewModelTests { driver.scriptedExecuteResults = [emptyResult()] await vm.applyFilters() - #expect(vm.legacyRows.isEmpty) + #expect(vm.window.shownValues.isEmpty) #expect(vm.showsPaginationBar) } @@ -998,8 +1059,8 @@ struct DataBrowserViewModelTests { await vm.handlePressure(.warning) - #expect(vm.legacyRows.count == 100) - #expect(vm.legacyRows.first?.first == "901") + #expect(vm.window.shownValues.count == 100) + #expect(vm.window.shownValues.first?.first == "901") #expect(vm.paginationLabel == DataBrowserViewModel.pageRangeLabel(start: 901, end: 1_000, total: 3_503)) } @@ -1018,7 +1079,7 @@ struct DataBrowserViewModelTests { driver.scriptedExecuteResults = [emptyResult()] await vm.applyFilters() - #expect(vm.legacyRows.isEmpty) + #expect(vm.window.shownValues.isEmpty) #expect(vm.memoryWarning == nil) } @@ -1059,7 +1120,7 @@ struct DataBrowserViewModelTests { #expect(vm.pagination.totalRows == pageSize * 3) #expect(vm.columnDetails.map(\.name) == ["index", "element"]) #expect(vm.hasPrimaryKeys == false) - #expect(vm.legacyRows.first == [String(pageSize), "e\(pageSize)"]) + #expect(vm.window.shownValues.first == [String(pageSize), "e\(pageSize)"]) #expect(vm.loadError == nil) #expect(vm.isLoading == false) } @@ -1086,7 +1147,7 @@ struct DataBrowserViewModelTests { await overtaken.value #expect(driver.pageRequests.count == 2) - #expect(vm.legacyRows.first == [String(pageSize), "e\(pageSize)"]) + #expect(vm.window.shownValues.first == [String(pageSize), "e\(pageSize)"]) #expect(vm.loadError == nil) #expect(vm.isLoading == false) } @@ -1126,7 +1187,7 @@ struct DataBrowserViewModelTests { await vm.load(isInitial: true) #expect(vm.loadError?.title == String(localized: "Key Not Found")) - #expect(vm.legacyRows.isEmpty) + #expect(vm.window.shownValues.isEmpty) #expect(vm.isLoading == false) #expect(driver.executedQueries.isEmpty) } @@ -1158,7 +1219,7 @@ struct DataBrowserViewModelTests { } #expect(kept == long) #expect(DataBrowserViewModel.canComplete(vm.cells(at: 0))) - let shown = vm.legacyRows.first?.first ?? nil + let shown = vm.window.shownValues.first?.first ?? nil #expect(shown?.utf8.count == Cell.previewByteLimit) } } diff --git a/apple/TableProMobile/TableProMobileTests/Mocks/InMemoryKeychainItems.swift b/apple/TableProMobile/TableProMobileTests/Mocks/InMemoryKeychainItems.swift index 037016add4..8e2f48e929 100644 --- a/apple/TableProMobile/TableProMobileTests/Mocks/InMemoryKeychainItems.swift +++ b/apple/TableProMobile/TableProMobileTests/Mocks/InMemoryKeychainItems.swift @@ -30,6 +30,23 @@ final class InMemoryKeychainItems: KeychainItemStore, @unchecked Sendable { private var refusedAdd: OSStatus? private var refusedUpdate: OSStatus? private var writes = 0 + private var callsOnMainThread = 0 + private var listingHook: (@Sendable () -> Void)? + + // Calls made on the main thread, where a real Keychain call blocks the UI. + var mainThreadCalls: Int { + lock.withLock { callsOnMainThread } + } + + // Runs inside the next listing, before it reads the items, as a save landing mid-listing would. + func duringNextListing(_ hook: @escaping @Sendable () -> Void) { + lock.withLock { listingHook = hook } + } + + private func countIfMainThread() { + guard Thread.isMainThread else { return } + lock.withLock { callsOnMainThread += 1 } + } // Each write is one tick later than the one before, as a later save on any device would be. private func nextDate() -> Date { @@ -134,8 +151,19 @@ final class InMemoryKeychainItems: KeychainItemStore, @unchecked Sendable { } } + func accounts(_ query: [String: Any]) -> [String] { + countIfMainThread() + let hook = lock.withLock { () -> (@Sendable () -> Void)? in + defer { listingHook = nil } + return listingHook + } + hook?() + return lock.withLock { matches(query).map { items[$0].account } } + } + func delete(_ query: [String: Any]) -> OSStatus { - lock.withLock { () -> OSStatus in + countIfMainThread() + return lock.withLock { () -> OSStatus in let matched = Set(matches(query)) guard !matched.isEmpty else { return errSecItemNotFound } items = items.enumerated().filter { !matched.contains($0.offset) }.map { $0.element } diff --git a/apple/TableProMobile/TableProMobileTests/Mocks/MockDatabaseDriver.swift b/apple/TableProMobile/TableProMobileTests/Mocks/MockDatabaseDriver.swift index a6fa48dd8f..b0113e8490 100644 --- a/apple/TableProMobile/TableProMobileTests/Mocks/MockDatabaseDriver.swift +++ b/apple/TableProMobile/TableProMobileTests/Mocks/MockDatabaseDriver.swift @@ -45,6 +45,11 @@ final class MockDatabaseDriver: DatabaseDriver, @unchecked Sendable { private(set) var didRollbackTransaction = false private(set) var pingCount = 0 private(set) var switchedDatabases: [String] = [] + private(set) var switchedSchemas: [String] = [] + private(set) var fetchTablesSchemas: [String?] = [] + private(set) var fetchSchemasCalls = 0 + private(set) var fetchDatabasesCalls = 0 + private(set) var transactionStateReads = 0 var supportsSchemas: Bool = false var currentSchema: String? @@ -95,6 +100,7 @@ final class MockDatabaseDriver: DatabaseDriver, @unchecked Sendable { } func fetchTables(schema: String?) async throws -> [TableInfo] { + fetchTablesSchemas.append(schema) await beforeFetchTables?() if let fetchTablesError { throw fetchTablesError } return scriptedTables @@ -116,8 +122,15 @@ final class MockDatabaseDriver: DatabaseDriver, @unchecked Sendable { return scriptedForeignKeys } - func fetchDatabases() async throws -> [String] { scriptedDatabases } - func fetchSchemas() async throws -> [String] { scriptedSchemas } + func fetchDatabases() async throws -> [String] { + fetchDatabasesCalls += 1 + return scriptedDatabases + } + + func fetchSchemas() async throws -> [String] { + fetchSchemasCalls += 1 + return scriptedSchemas + } var currentDatabase: String? { scriptedCurrentDatabase } var refusedDatabases: Set = [] @@ -128,6 +141,7 @@ final class MockDatabaseDriver: DatabaseDriver, @unchecked Sendable { switchedDatabases.append(name) } func switchSchema(to name: String) async throws { + switchedSchemas.append(name) currentSchema = name } func beginTransaction() async throws { didBeginTransaction = true } @@ -153,6 +167,7 @@ final class MockDatabaseDriver: DatabaseDriver, @unchecked Sendable { } func sessionTransactionState() async -> DriverTransactionState { + transactionStateReads += 1 await beforeTransactionState?() guard !scriptedTransactionStates.isEmpty else { return scriptedTransactionState } return scriptedTransactionStates.removeFirst() diff --git a/apple/TableProMobile/TableProMobileTests/Mocks/MockQueryBuildingDriver.swift b/apple/TableProMobile/TableProMobileTests/Mocks/MockQueryBuildingDriver.swift index f4d22c9079..944f55f234 100644 --- a/apple/TableProMobile/TableProMobileTests/Mocks/MockQueryBuildingDriver.swift +++ b/apple/TableProMobile/TableProMobileTests/Mocks/MockQueryBuildingDriver.swift @@ -8,6 +8,7 @@ final class MockQueryBuildingDriver: DriverQueryBuilding, @unchecked Sendable { var scriptedRows: [[String?]] = [] var scriptedCount: Int? = 0 var scriptedValues: [Result] = [] + var beforeCount: (@Sendable () async -> Void)? private(set) var pageRequests: [(request: TableReadRequest, limit: Int, offset: Int)] = [] private(set) var countRequests: [TableReadRequest] = [] @@ -25,6 +26,7 @@ final class MockQueryBuildingDriver: DriverQueryBuilding, @unchecked Sendable { func rowCount(_ request: TableReadRequest) async throws -> Int? { countRequests.append(request) + await beforeCount?() return scriptedCount } diff --git a/apple/TableProMobile/TableProMobileTests/Mocks/RecordingSearchIndex.swift b/apple/TableProMobile/TableProMobileTests/Mocks/RecordingSearchIndex.swift index 3b2a8bdf14..3f24600fba 100644 --- a/apple/TableProMobile/TableProMobileTests/Mocks/RecordingSearchIndex.swift +++ b/apple/TableProMobile/TableProMobileTests/Mocks/RecordingSearchIndex.swift @@ -9,9 +9,13 @@ final class RecordingSearchIndex: ConnectionSearchIndexing { private(set) var replacements: [[SearchableConnection]] = [] private(set) var contents: [SearchableConnection] = [] + // Kept by the index across launches, as CoreSpotlight keeps a client state. + private(set) var state: Data? + private(set) var stateReads = 0 private(set) var maxConcurrent = 0 private var inFlight = 0 private var failuresRemaining = 0 + private var stateReadFailures = 0 private var holdsNext = false private var held: CheckedContinuation? private var heldWaiter: CheckedContinuation? @@ -24,6 +28,10 @@ final class RecordingSearchIndex: ConnectionSearchIndexing { failuresRemaining += 1 } + func failNextStateRead() { + stateReadFailures += 1 + } + func holdNextReplace() { holdsNext = true } @@ -39,7 +47,17 @@ final class RecordingSearchIndex: ConnectionSearchIndexing { } @MainActor - func replaceConnections(with connections: [SearchableConnection]) async throws { + func storedState() async throws -> Data? { + stateReads += 1 + if stateReadFailures > 0 { + stateReadFailures -= 1 + throw Failure.scripted + } + return state + } + + @MainActor + func replaceConnections(with connections: [SearchableConnection], state: Data) async throws { inFlight += 1 maxConcurrent = max(maxConcurrent, inFlight) defer { inFlight -= 1 } @@ -57,5 +75,6 @@ final class RecordingSearchIndex: ConnectionSearchIndexing { throw Failure.scripted } contents = connections + self.state = state } } diff --git a/apple/TableProMobile/TableProMobileTests/Mocks/RecordingSyncTransport.swift b/apple/TableProMobile/TableProMobileTests/Mocks/RecordingSyncTransport.swift index fa68820f00..5f662b47a8 100644 --- a/apple/TableProMobile/TableProMobileTests/Mocks/RecordingSyncTransport.swift +++ b/apple/TableProMobile/TableProMobileTests/Mocks/RecordingSyncTransport.swift @@ -12,6 +12,8 @@ actor RecordingSyncTransport: IOSSyncTransport { private(set) var pushedRecords: [CKRecord] = [] private(set) var pushedDeletions: [CKRecord.ID] = [] private(set) var pullCount = 0 + private var beforeAccountRead: (@Sendable () async -> Void)? + private var beforePull: (@Sendable () async -> Void)? init( remoteRecords: [CKRecord] = [], @@ -28,18 +30,30 @@ actor RecordingSyncTransport: IOSSyncTransport { remoteDeletions = deletions } + // Runs inside every account read, before any download starts. + func holdAccountChecks(_ hold: @escaping @Sendable () async -> Void) { + beforeAccountRead = hold + } + + // Runs inside every pull, once it is counted. + func holdPulls(_ hold: @escaping @Sendable () async -> Void) { + beforePull = hold + } + func accountStatus() async throws -> CKAccountStatus { .available } func currentAccountId() async throws -> String { - "recording-account" + await beforeAccountRead?() + return "recording-account" } func ensureZoneExists() async throws {} func pull(since token: CKServerChangeToken?) async throws -> PullResult { pullCount += 1 + await beforePull?() return PullResult(changedRecords: remoteRecords, deletedRecordIDs: remoteDeletions, newToken: newToken) } diff --git a/apple/TableProMobile/TableProMobileTests/Onboarding/AppStateFixture.swift b/apple/TableProMobile/TableProMobileTests/Onboarding/AppStateFixture.swift index 85d24b749d..ebb49e53dd 100644 --- a/apple/TableProMobile/TableProMobileTests/Onboarding/AppStateFixture.swift +++ b/apple/TableProMobile/TableProMobileTests/Onboarding/AppStateFixture.swift @@ -73,8 +73,10 @@ struct AppStateFixture { bookmarkStore: bookmarkStore, libraryPublisher: libraryPublisher ?? ConnectionLibraryPublisher( searchIndex: RecordingSearchIndex(), + readWidgetItems: { [] }, writeWidgetItems: { _ in }, - refreshShortcutParameters: {} + refreshShortcutParameters: {}, + defaults: defaults ), driverFactory: driverFactory ) diff --git a/apple/TableProMobile/TableProMobileTests/Onboarding/AppStateLibraryTests.swift b/apple/TableProMobile/TableProMobileTests/Onboarding/AppStateLibraryTests.swift index 17fb2705ef..f1c9db870f 100644 --- a/apple/TableProMobile/TableProMobileTests/Onboarding/AppStateLibraryTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Onboarding/AppStateLibraryTests.swift @@ -23,8 +23,10 @@ struct AppStateLibraryTests { let widgetWrites = widgetWrites return ConnectionLibraryPublisher( searchIndex: searchIndex, + readWidgetItems: { [] }, writeWidgetItems: { _ in widgetWrites.total += 1 }, - refreshShortcutParameters: {} + refreshShortcutParameters: {}, + defaults: fixture.defaults ) } diff --git a/apple/TableProMobile/TableProMobileTests/Platform/KeychainSecureStoreTests.swift b/apple/TableProMobile/TableProMobileTests/Platform/KeychainSecureStoreTests.swift index c608f163b7..d68128cece 100644 --- a/apple/TableProMobile/TableProMobileTests/Platform/KeychainSecureStoreTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Platform/KeychainSecureStoreTests.swift @@ -1,4 +1,5 @@ import Foundation +import os import Security @testable import TableProMobile import Testing @@ -122,4 +123,69 @@ struct KeychainSecureStoreTests { try store.store("new-pem", role: .clientCertificate, for: connectionId) #expect(store.pem(role: .clientCertificate, for: connectionId) == "new-pem") } + + // MARK: - Launch cleanup + + @Test("The launch cleanup removes a deleted connection's device-only secrets and keeps current, synced and Mac-owned ones") + func cleanupRemovesOnlyOrphans() async { + let kept = UUID() + let deleted = UUID() + let keptPassword = ConnectionSecretKind.password.account(for: kept) + let deletedPassword = ConnectionSecretKind.password.account(for: deleted) + let deletedSSHPassword = ConnectionSecretKind.sshPassword.account(for: deleted) + let macOwnedKey = ConnectionSecretKind.sshPrivateKey.account(for: deleted) + items.seed("kept", account: keptPassword, synchronizable: false) + items.seed("orphan", account: deletedPassword, synchronizable: false) + items.seed("orphan", account: deletedSSHPassword, synchronizable: false) + items.seed("synced", account: deletedPassword, synchronizable: true) + items.seed("mac", account: macOwnedKey, synchronizable: false) + + await KeychainSecureStore.cleanOrphanedCredentials(items: items) { Set([kept]) } + + #expect(items.value(account: keptPassword, synchronizable: false) == "kept") + #expect(items.value(account: deletedPassword, synchronizable: false) == nil) + #expect(items.value(account: deletedSSHPassword, synchronizable: false) == nil) + #expect(items.value(account: deletedPassword, synchronizable: true) == "synced") + #expect(items.value(account: macOwnedKey, synchronizable: false) == "mac") + } + + @MainActor + @Test("Started from the main actor, the launch cleanup makes no Keychain call on the main thread") + func cleanupStaysOffTheMainThread() async { + let orphan = ConnectionSecretKind.password.account(for: UUID()) + items.seed("orphan", account: orphan, synchronizable: false) + + await KeychainSecureStore.cleanOrphanedCredentials(items: items) { Set([UUID()]) } + + #expect(items.value(account: orphan, synchronizable: false) == nil) + #expect(items.mainThreadCalls == 0) + } + + @Test("A connection saved while the Keychain is listed keeps its secrets, since the library is read after the listing") + func connectionSavedDuringListingKeepsItsSecrets() async { + let existing = UUID() + let saved = UUID() + let savedPassword = ConnectionSecretKind.password.account(for: saved) + let library = OSAllocatedUnfairLock(initialState: Set([existing])) + items.duringNextListing { [items] in + // A save adds the connection, then writes its secrets. + library.withLock { _ = $0.insert(saved) } + items.seed("new", account: savedPassword, synchronizable: false) + } + + await KeychainSecureStore.cleanOrphanedCredentials(items: items) { library.withLock { $0 } } + + #expect(items.value(account: savedPassword, synchronizable: false) == "new") + } + + @Test("A library that is empty or not loaded removes nothing") + func unknownLibraryRemovesNothing() async { + let account = ConnectionSecretKind.password.account(for: UUID()) + items.seed("secret", account: account, synchronizable: false) + + await KeychainSecureStore.cleanOrphanedCredentials(items: items) { nil } + await KeychainSecureStore.cleanOrphanedCredentials(items: items) { Set() } + + #expect(items.value(account: account, synchronizable: false) == "secret") + } } diff --git a/apple/TableProMobile/TableProMobileTests/QueryEditorViewModelTests.swift b/apple/TableProMobile/TableProMobileTests/QueryEditorViewModelTests.swift index ab17c1a25a..fa118350e7 100644 --- a/apple/TableProMobile/TableProMobileTests/QueryEditorViewModelTests.swift +++ b/apple/TableProMobile/TableProMobileTests/QueryEditorViewModelTests.swift @@ -24,9 +24,9 @@ struct QueryEditorViewModelTests { let vm = QueryEditorViewModel(windowCapacity: 100) await vm.run(driver: driver, script: .statements("SELECT * FROM t"), maxRows: 3) - #expect(vm.legacyRows.count == 3) - #expect(vm.legacyRows.first?.first == "0") - #expect(vm.legacyRows.last?.first == "2") + #expect(vm.window.shownValues.count == 3) + #expect(vm.window.shownValues.first?.first == "0") + #expect(vm.window.shownValues.last?.first == "2") #expect(vm.truncationReason != nil) #expect(vm.truncationMessage != nil) if case .truncated(let reason) = vm.phase, case .rowCap(let cap) = reason { @@ -36,6 +36,23 @@ struct QueryEditorViewModelTests { } } + @Test("Share and Copy Results format the rows on screen, and refuse a result the free memory cannot hold") + func exportResultsFormatsTheRows() async throws { + let driver = MockDatabaseDriver() + driver.scriptedExecuteResults = [ + .success(QueryResult(columns: makeColumns(), rows: [["1"], ["2"]], rowsAffected: 0, executionTime: 0)) + ] + let vm = QueryEditorViewModel(windowCapacity: 100) + await vm.run(driver: driver, script: .statements("SELECT id FROM t"), maxRows: 100) + + let csv = try await vm.exportResults(as: .csv, databaseType: .mysql, driver: driver, availableBytes: 0) + #expect(csv == "id\n1\n2") + + await #expect(throws: ClipboardExporter.TooLarge(source: .queryResult)) { + try await vm.exportResults(as: .csv, databaseType: .mysql, driver: driver, availableBytes: 1) + } + } + @Test("stop marks the run cancelled rather than letting it settle as finished") func stopRecordsCancellation() async { let driver = MockDatabaseDriver() @@ -106,7 +123,7 @@ struct QueryEditorViewModelTests { let vm = QueryEditorViewModel(windowCapacity: 100) await vm.run(driver: driver, script: .statements("SELECT id FROM t"), maxRows: 100) - #expect(vm.legacyRows.count == 2) + #expect(vm.window.shownValues.count == 2) #expect(vm.truncationReason == nil) #expect(vm.truncationMessage == nil) if case .finished = vm.phase {} else { @@ -127,7 +144,7 @@ struct QueryEditorViewModelTests { await vm.handlePressure(.warning) - #expect(vm.legacyRows.count == 2) + #expect(vm.window.shownValues.count == 2) #expect(vm.truncationReason == nil) #expect(vm.truncationMessage == nil) if case .finished = vm.phase {} else { @@ -144,11 +161,11 @@ struct QueryEditorViewModelTests { let vm = QueryEditorViewModel(windowCapacity: 100) await vm.run(driver: driver, script: .statements("SELECT id FROM t"), maxRows: 100) - #expect(vm.legacyRows.count == 1) + #expect(vm.window.shownValues.count == 1) vm.reset() - #expect(vm.legacyRows.isEmpty) + #expect(vm.window.shownValues.isEmpty) #expect(vm.columns.isEmpty) if case .idle = vm.phase {} else { Issue.record("expected idle phase after reset") @@ -430,7 +447,7 @@ struct QueryEditorViewModelTests { #expect(totalBytes == 10_000) #expect(ref == nil) #expect(vm.longValuesNotice == String(localized: "Some long values are shown in part to stay within memory.")) - let shown = vm.legacyRows.first?.first ?? nil + let shown = vm.window.shownValues.first?.first ?? nil #expect(shown?.utf8.count == Cell.previewByteLimit) } @@ -472,9 +489,10 @@ struct QueryEditorViewModelTests { await vm.run(driver: driver, script: .statements("SELECT body FROM notes")) let margin = 64 * 1_024 * 1_024 - #expect(vm.resultsFitExport(availableBytes: 0)) - #expect(vm.resultsFitExport(availableBytes: margin + 40_001)) - #expect(!vm.resultsFitExport(availableBytes: margin + 40_000)) + let rows = vm.window.rows + #expect(QueryEditorViewModel.resultsFitExport(rows: rows, availableBytes: 0)) + #expect(QueryEditorViewModel.resultsFitExport(rows: rows, availableBytes: margin + 40_001)) + #expect(!QueryEditorViewModel.resultsFitExport(rows: rows, availableBytes: margin + 40_000)) } @Test("the budget is a quarter of the headroom past 64 MB, at most 64 MB, and 64 MB when the headroom is unknown") diff --git a/apple/TableProMobile/TableProMobileTests/RowDetailViewModelTests.swift b/apple/TableProMobile/TableProMobileTests/RowDetailViewModelTests.swift index 3e8167407d..005d70f295 100644 --- a/apple/TableProMobile/TableProMobileTests/RowDetailViewModelTests.swift +++ b/apple/TableProMobile/TableProMobileTests/RowDetailViewModelTests.swift @@ -26,6 +26,17 @@ struct RowDetailViewModelTests { ConnectionSession(connectionId: UUID(), driver: driver, activeDatabase: "test") } + @Test("A row opened before the browser read its foreign keys shows them once they arrive") + func foreignKeysArriveAfterTheRowOpened() { + let source = ForeignKeySource() + let vm = RowDetailViewModel(columns: makeColumns(), rows: makeRows(), initialIndex: 0, foreignKeys: source.keys) + #expect(vm.foreignKeys.isEmpty) + + source.keys = [ForeignKeyInfo(name: "fk_owner", column: "id", referencedTable: "owners", referencedColumn: "id")] + + #expect(vm.foreignKeys.map { $0.referencedTable } == ["owners"]) + } + @Test("canEdit requires session, table, primary key, and not safe-mode-blocked") func canEditPreconditions() { let driver = MockDatabaseDriver() @@ -1181,3 +1192,8 @@ struct RowDetailViewModelTests { #expect(!loadable.isShownInPart(forRow: 0, at: 2)) } } + +@MainActor +private final class ForeignKeySource { + var keys: [ForeignKeyInfo] = [] +} diff --git a/apple/TableProMobile/TableProMobileTests/StreamingResultBufferTests.swift b/apple/TableProMobile/TableProMobileTests/StreamingResultBufferTests.swift index 4e46275d7d..050ca4ec97 100644 --- a/apple/TableProMobile/TableProMobileTests/StreamingResultBufferTests.swift +++ b/apple/TableProMobile/TableProMobileTests/StreamingResultBufferTests.swift @@ -15,25 +15,25 @@ struct StreamingResultBufferTests { buffer.apply(.row(Row(cells: [.text("2")]))) buffer.flush() - #expect(buffer.legacyRows.count == 2) + #expect(buffer.window.shownValues.count == 2) #expect(buffer.window.count == 2) #expect(buffer.columns.count == 1) - #expect(buffer.legacyRows.first?.first == "1") + #expect(buffer.window.shownValues.first?.first == "1") } - @Test("shrink keeps window and legacy rows in lockstep") - func shrinkLockstep() { + @Test("shrink keeps the newest rows") + func shrinkKeepsNewest() { let buffer = StreamingResultBuffer(capacity: 100) for index in 0..<10 { buffer.apply(.row(Row(cells: [.text("\(index)")]))) } buffer.flush() - #expect(buffer.legacyRows.count == 10) + #expect(buffer.count == 10) buffer.shrink(to: 4) - #expect(buffer.legacyRows.count == 4) - #expect(buffer.window.count == 4) + #expect(buffer.count == 4) + #expect(buffer.window.shownValues.map { $0.first ?? nil } == ["6", "7", "8", "9"]) } @Test("the grid gets a preview of a long value the row keeps whole") @@ -43,7 +43,7 @@ struct StreamingResultBufferTests { buffer.apply(.row(Row(cells: [.text(long)]))) buffer.flush() - let shown = try #require(buffer.legacyRows.first?.first ?? nil) + let shown = try #require(buffer.window.shownValues.first?.first ?? nil) #expect(shown.utf8.count <= Cell.previewByteLimit) #expect(long.hasPrefix(shown)) guard case .text(let kept)? = buffer.window.rows.first?.cells.first else { diff --git a/apple/TableProMobile/TableProMobileTests/Support/RowWindow+Shown.swift b/apple/TableProMobile/TableProMobileTests/Support/RowWindow+Shown.swift new file mode 100644 index 0000000000..9c3aff9b56 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Support/RowWindow+Shown.swift @@ -0,0 +1,9 @@ +@testable import TableProMobile +import TableProModels + +extension RowWindow { + // The text each row shows, derived the way the grid and the list derive it. + var shownValues: [[String?]] { + rows.map { $0.displayValues() } + } +} diff --git a/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncActivationTests.swift b/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncActivationTests.swift new file mode 100644 index 0000000000..edaf804ef3 --- /dev/null +++ b/apple/TableProMobile/TableProMobileTests/Sync/IOSSyncActivationTests.swift @@ -0,0 +1,168 @@ +import Foundation +@testable import TableProMobile +import TableProModels +import TableProSync +import TableProSyncTransport +import TableProTestSupport +import Testing + +@MainActor +struct IOSSyncActivationTests { + private let metadata: SyncMetadataStorage + private let cacheDirectory: URL + private let clock = ManualClock() + private let transport: RecordingSyncTransport + + init() throws { + transport = RecordingSyncTransport() + let defaults = try #require(UserDefaults(suiteName: "com.TablePro.tests.IOSSyncActivation.\(UUID().uuidString)")) + metadata = SyncMetadataStorage(userDefaults: defaults) + _ = metadata.adoptReadableRecordTypes(IOSSyncCoordinator.readableRecordTypes) + cacheDirectory = FileManager.default.temporaryDirectory + .appendingPathComponent("ios-sync-activation-\(UUID().uuidString)", isDirectory: true) + } + + private func makeCoordinator() -> IOSSyncCoordinator { + let transport = transport + let coordinator = IOSSyncCoordinator( + metadata: metadata, + recordCache: SyncRecordCache(directory: cacheDirectory, defaults: nil), + makeTransport: { transport }, + isEnabled: { true }, + notificationCenter: NotificationCenter(), + networkMonitor: nil, + activationClock: clock + ) + coordinator.getCurrentState = { ([], [], []) } + coordinator.onConnectionsChanged = { _ in true } + coordinator.onGroupsChanged = { _ in true } + coordinator.onTagsChanged = { _ in true } + coordinator.getSavedQueries = { SavedQueryContents() } + coordinator.onSavedQueriesChanged = { _ in true } + return coordinator + } + + private func endInterval() async { + await clock.waitUntilSleeping() + clock.advance(by: IOSSyncCoordinator.activationInterval) + } + + @Test("Ten activations within a minute of a download start one run, and the held one runs once when the minute ends") + func activationsInsideTheIntervalRunOnce() async throws { + let coordinator = makeCoordinator() + + for _ in 0..<10 { + await coordinator.syncOnSceneActivation() + } + #expect(await transport.pullCount == 1) + #expect(coordinator.isHoldingActivations) + + await endInterval() + try await waitUntil { await transport.pullCount == 2 } + + // Waits for any run still going, so a second held run would show as a fourth pull. + await coordinator.sync(.userRequest) + #expect(await transport.pullCount == 3) + } + + @Test("With no activation held, the end of the interval starts no run") + func quietIntervalStartsNothing() async throws { + let coordinator = makeCoordinator() + await coordinator.syncOnSceneActivation() + + await endInterval() + try await waitUntil { !coordinator.isHoldingActivations } + await coordinator.sync(.userRequest) + + #expect(await transport.pullCount == 2) + } + + @Test("An edit and the person's request run at once inside the interval") + func editsAndRequestsAreNeverHeld() async { + let coordinator = makeCoordinator() + await coordinator.syncOnSceneActivation() + + await coordinator.sync(.localChange) + #expect(await transport.pullCount == 2) + await coordinator.sync(.userRequest) + #expect(await transport.pullCount == 3) + } + + @Test("A background refresh runs inside the interval") + func backgroundRefreshIsNeverHeld() async { + let coordinator = makeCoordinator() + await coordinator.syncOnSceneActivation() + + // What the BGAppRefresh task calls. + await coordinator.sync(.activation) + + #expect(await transport.pullCount == 2) + } + + @Test("An activation while a run has not started its download is answered by that run") + func activationBeforeTheDownloadIsDropped() async throws { + let coordinator = makeCoordinator() + let gate = AsyncGate() + await transport.holdAccountChecks { await gate.wait() } + let edit = Task { await coordinator.sync(.localChange) } + await gate.arrived(1) + + // Runs only once the activation below has returned or waits on the run, since both share the main actor. + let opener = Task { await gate.open() } + await coordinator.syncOnSceneActivation() + await opener.value + await edit.value + #expect(await transport.pullCount == 1) + + await endInterval() + try await waitUntil { !coordinator.isHoldingActivations } + await coordinator.sync(.userRequest) + #expect(await transport.pullCount == 2) + } + + @Test("An activation during a download runs once the interval ends, since the download began before it") + func activationDuringTheDownloadRunsAfterTheInterval() async throws { + let coordinator = makeCoordinator() + let gate = AsyncGate() + await transport.holdPulls { await gate.wait() } + let edit = Task { await coordinator.sync(.localChange) } + await gate.arrived(1) + + // Runs only once the activation below waits on the run, since both share the main actor. + let opener = Task { await gate.open() } + await coordinator.syncOnSceneActivation() + await opener.value + await edit.value + #expect(await transport.pullCount == 1) + #expect(coordinator.isHoldingActivations) + + await endInterval() + try await waitUntil { await transport.pullCount == 2 } + } + + @Test("A download that fails starts no interval, so the next activation runs") + func failedDownloadHoldsNothing() async { + let coordinator = makeCoordinator() + coordinator.onConnectionsChanged = { _ in false } + await coordinator.syncOnSceneActivation() + #expect(!coordinator.isHoldingActivations) + + coordinator.onConnectionsChanged = { _ in true } + await coordinator.syncOnSceneActivation() + + #expect(await transport.pullCount == 2) + } + + @Test("Turning sync off drops a held activation") + func turningSyncOffDropsTheHold() async throws { + let coordinator = makeCoordinator() + await coordinator.syncOnSceneActivation() + await coordinator.syncOnSceneActivation() + await clock.waitUntilSleeping() + + coordinator.setEnabled(false) + + #expect(!coordinator.isHoldingActivations) + #expect(clock.pendingSleeperCount == 0) + } +} diff --git a/apple/TableProMobile/TableProWidget/Localizable.xcstrings b/apple/TableProMobile/TableProWidget/Localizable.xcstrings new file mode 100644 index 0000000000..2abd97dc73 --- /dev/null +++ b/apple/TableProMobile/TableProWidget/Localizable.xcstrings @@ -0,0 +1,286 @@ +{ + "sourceLanguage" : "en", + "strings" : { + "%lld rows" : { + "localizations" : { + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "%lld개 행" + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "%lld dòng" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "%lld行" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "%lld 列" + } + } + } + }, + "1 row" : { + "localizations" : { + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "1개 행" + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "1 dòng" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "1 行" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "1 列" + } + } + } + }, + "Add a connection in TablePro" : { + "localizations" : { + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "TablePro에서 연결 추가" + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Thêm một kết nối trong TablePro" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "在 TablePro 中添加连接" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "在 TablePro 中新增連線" + } + } + } + }, + "Done" : { + "localizations" : { + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "완료" + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Xong" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "完成" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "完成" + } + } + } + }, + "Failed" : { + "localizations" : { + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "실패" + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Thất bại" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "失败" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "失敗" + } + } + } + }, + "Interrupted" : { + "localizations" : { + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "중단됨" + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Bị gián đoạn" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "已中断" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "已中斷" + } + } + } + }, + "Quick Connect" : { + "localizations" : { + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "빠른 연결" + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Kết nối nhanh" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "快速连接" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "快速連線" + } + } + } + }, + "Quickly connect to your databases." : { + "localizations" : { + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "데이터베이스에 빠르게 연결합니다." + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Kết nối nhanh tới các cơ sở dữ liệu của bạn." + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "快速连接到你的数据库。" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "快速連線到你的資料庫。" + } + } + } + }, + "Running" : { + "localizations" : { + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "실행 중" + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Đang chạy" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "正在运行" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "執行中" + } + } + } + }, + "Stopped" : { + "localizations" : { + "ko" : { + "stringUnit" : { + "state" : "translated", + "value" : "중지됨" + } + }, + "vi" : { + "stringUnit" : { + "state" : "translated", + "value" : "Đã dừng" + } + }, + "zh-Hans" : { + "stringUnit" : { + "state" : "translated", + "value" : "已停止" + } + }, + "zh-Hant" : { + "stringUnit" : { + "state" : "translated", + "value" : "已停止" + } + } + } + } + }, + "version" : "1.0" +} \ No newline at end of file diff --git a/apple/TableProMobile/project.yml b/apple/TableProMobile/project.yml index 9116d12957..27996ea3a8 100644 --- a/apple/TableProMobile/project.yml +++ b/apple/TableProMobile/project.yml @@ -231,11 +231,11 @@ targets: TableProWidgetExtension: type: app-extension platform: iOS + # The folder holds the widget's own Localizable.xcstrings, so the extension ships only its strings. sources: - path: TableProWidget excludes: - Info.plist - - TableProMobile/Localizable.xcstrings configFiles: Debug: ../Configs/Version-iOS.xcconfig Release: ../Configs/Version-iOS.xcconfig diff --git a/apple/scripts/ci/check-string-catalogs.py b/apple/scripts/ci/check-string-catalogs.py index 38dc17624d..9cb91e5e68 100755 --- a/apple/scripts/ci/check-string-catalogs.py +++ b/apple/scripts/ci/check-string-catalogs.py @@ -16,6 +16,7 @@ CATALOGS = { "TablePro": "TablePro/Resources/Localizable.xcstrings", "TableProMobile": "TableProMobile/TableProMobile/Localizable.xcstrings", + "TableProWidget": "TableProMobile/TableProWidget/Localizable.xcstrings", } TERMINAL_MARKS = {":": (":", ":"), "?": ("?", "?"), ".": (".", "。")} # SQL notation, written the way SQL documentation writes it, not a UI ellipsis. diff --git a/apple/scripts/localization.py b/apple/scripts/localization.py index 2c3b5ee71e..97515daf9f 100755 --- a/apple/scripts/localization.py +++ b/apple/scripts/localization.py @@ -10,6 +10,9 @@ scripts/localization.py plugins --target ios -> the same for the package targets the iOS app links scripts/localization.py extracted [--add] -> strings the app's last build extracted, missing +`--target widget` names the iOS widget extension's own catalog, for `status` and `extracted`. It +links no package, so `plugins` has nothing to check there. + Xcode extracts strings per target. Code in a plugin bundle or in a package under `Packages/` resolves `String(localized:)` against `Bundle.main`, which is the host app, so its strings must be in the app's catalog, yet no sync of the app target ever finds them in its sources. Xcode deletes @@ -35,13 +38,16 @@ CATALOGS = { "mac": Path("TablePro/Resources/Localizable.xcstrings"), "ios": Path("TableProMobile/TableProMobile/Localizable.xcstrings"), + "widget": Path("TableProMobile/TableProWidget/Localizable.xcstrings"), } +PLUGIN_TARGETS = ("mac", "ios") HIDDEN_SOURCE_ROOTS = (Path("Plugins"), Path("Packages")) IOS_PROJECT = Path("TableProMobile/project.yml") # The project, its scheme, and the targets whose sources hold the catalog. APP_BUILDS = { "mac": ("TablePro.xcodeproj", "TablePro", ("TablePro",)), - "ios": ("TableProMobile/TableProMobile.xcodeproj", "TableProMobile", ("TableProMobile", "TableProWidgetExtension")), + "ios": ("TableProMobile/TableProMobile.xcodeproj", "TableProMobile", ("TableProMobile",)), + "widget": ("TableProMobile/TableProMobile.xcodeproj", "TableProMobile", ("TableProWidgetExtension",)), } # Xcode writes a catalog through JSONSerialization with exactly these options. Writing through the @@ -332,6 +338,8 @@ def main() -> int: parser.add_argument("--derived-data", type=Path, help="for extracted: the DerivedData folder of the app's build") args = parser.parse_args() + if args.command == "plugins" and args.target not in PLUGIN_TARGETS: + parser.error(f"plugins checks {' and '.join(PLUGIN_TARGETS)}: the {args.target} target links no package") if args.command == "plugins": return plugins(args.add, args.target) if args.command == "extracted": diff --git a/apple/scripts/test_localization.py b/apple/scripts/test_localization.py index d3909b3bac..19c704a607 100644 --- a/apple/scripts/test_localization.py +++ b/apple/scripts/test_localization.py @@ -3,13 +3,17 @@ from __future__ import annotations +import contextlib import importlib.util +import io import json import os import shutil +import sys import tempfile import unittest from pathlib import Path +from unittest import mock SPEC = importlib.util.spec_from_file_location("localization", Path(__file__).with_name("localization.py")) localization = importlib.util.module_from_spec(SPEC) @@ -152,6 +156,20 @@ def test_reads_the_newest_build_of_the_catalog_targets(self): ["Open Connection", "Run Query", "Shown", "Widget text"], ) + def test_the_ios_app_and_its_widget_each_check_their_own_build(self): + view = self.source("View.swift", 'Text("App text")\n') + widget = self.source("Widget.swift", 'Text("Widget text")\n') + self.stringsdata("TableProMobile", view, [("App text", 1, "")]) + self.stringsdata("TableProWidgetExtension", widget, [("Widget text", 1, "")]) + + app = localization.extracted_strings(self.objects, localization.APP_BUILDS["ios"][2], "Localizable") + extension = localization.extracted_strings(self.objects, localization.APP_BUILDS["widget"][2], "Localizable") + + self.assertEqual(localization.extracted_keys(app, "Localizable"), ["App text"]) + self.assertEqual(localization.extracted_keys(extension, "Localizable"), ["Widget text"]) + self.assertEqual(localization.APP_BUILDS["widget"][:2], localization.APP_BUILDS["ios"][:2]) + self.assertEqual(localization.CATALOGS["widget"].stem, "Localizable") + @unittest.skipUnless(shutil.which("xcrun"), "adding a key runs xcstringstool") def test_adds_an_entry_as_xcode_writes_it(self): menu = self.source("Menu.swift", 'String(localized: "menu.edit", defaultValue: "Edit", comment: "Edit menu")\n') @@ -193,6 +211,13 @@ def test_manage_adds_missing_keys_and_keeps_translations(self): self.assertEqual(strings["Hello"]["localizations"]["fr"], unit) self.assertEqual(localization.unmanaged_keys(strings, ["Hello", "New"]), []) + def test_plugins_refuses_the_widget_which_links_no_package(self): + with mock.patch.object(sys, "argv", ["localization.py", "plugins", "--target", "widget"]): + with contextlib.redirect_stderr(io.StringIO()): + with self.assertRaises(SystemExit) as raised: + localization.main() + self.assertEqual(raised.exception.code, 2) + @unittest.skipUnless(shutil.which("xcrun"), "writing a catalog uses Foundation through xcrun swift") class WriteTests(unittest.TestCase): From 9d303d28b2cf2a626266ccb970a0d528b326a269 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 12:32:03 +0700 Subject: [PATCH 2/3] docs(changelog): add the iOS performance fragments --- apple/changelog.d/4228.changed.1.md | 1 + apple/changelog.d/4228.changed.2.md | 1 + apple/changelog.d/4228.changed.3.md | 1 + apple/changelog.d/4228.changed.4.md | 1 + apple/changelog.d/4228.changed.md | 1 + apple/changelog.d/4228.fixed.1.md | 1 + apple/changelog.d/4228.fixed.2.md | 1 + apple/changelog.d/4228.fixed.md | 1 + 8 files changed, 8 insertions(+) create mode 100644 apple/changelog.d/4228.changed.1.md create mode 100644 apple/changelog.d/4228.changed.2.md create mode 100644 apple/changelog.d/4228.changed.3.md create mode 100644 apple/changelog.d/4228.changed.4.md create mode 100644 apple/changelog.d/4228.changed.md create mode 100644 apple/changelog.d/4228.fixed.1.md create mode 100644 apple/changelog.d/4228.fixed.2.md create mode 100644 apple/changelog.d/4228.fixed.md diff --git a/apple/changelog.d/4228.changed.1.md b/apple/changelog.d/4228.changed.1.md new file mode 100644 index 0000000000..049458cd69 --- /dev/null +++ b/apple/changelog.d/4228.changed.1.md @@ -0,0 +1 @@ +On iOS, a PostgreSQL connection opens straight into the database and schema you last used. diff --git a/apple/changelog.d/4228.changed.2.md b/apple/changelog.d/4228.changed.2.md new file mode 100644 index 0000000000..22de99ce4d --- /dev/null +++ b/apple/changelog.d/4228.changed.2.md @@ -0,0 +1 @@ +Large iOS query results use less memory, so more rows fit before the app stops at its memory limit. diff --git a/apple/changelog.d/4228.changed.3.md b/apple/changelog.d/4228.changed.3.md new file mode 100644 index 0000000000..c4c25a1a3e --- /dev/null +++ b/apple/changelog.d/4228.changed.3.md @@ -0,0 +1 @@ +Returning to the iOS app syncs with iCloud at most once a minute. diff --git a/apple/changelog.d/4228.changed.4.md b/apple/changelog.d/4228.changed.4.md new file mode 100644 index 0000000000..4234868ae2 --- /dev/null +++ b/apple/changelog.d/4228.changed.4.md @@ -0,0 +1 @@ +The iOS app starts faster: it no longer rebuilds Spotlight entries, widgets and Shortcuts at every launch. diff --git a/apple/changelog.d/4228.changed.md b/apple/changelog.d/4228.changed.md new file mode 100644 index 0000000000..07f1cb4497 --- /dev/null +++ b/apple/changelog.d/4228.changed.md @@ -0,0 +1 @@ +Opening a table on iOS shows its rows before the row count finishes, and sorting no longer counts the table again. diff --git a/apple/changelog.d/4228.fixed.1.md b/apple/changelog.d/4228.fixed.1.md new file mode 100644 index 0000000000..16d899ae32 --- /dev/null +++ b/apple/changelog.d/4228.fixed.1.md @@ -0,0 +1 @@ +Going past the last page of an iOS table before its row count arrived showed it as empty with a wrong total. diff --git a/apple/changelog.d/4228.fixed.2.md b/apple/changelog.d/4228.fixed.2.md new file mode 100644 index 0000000000..ff7f77f776 --- /dev/null +++ b/apple/changelog.d/4228.fixed.2.md @@ -0,0 +1 @@ +Switching schema on iOS kept the previous schema's row count in the table pager. diff --git a/apple/changelog.d/4228.fixed.md b/apple/changelog.d/4228.fixed.md new file mode 100644 index 0000000000..2e6e2bde06 --- /dev/null +++ b/apple/changelog.d/4228.fixed.md @@ -0,0 +1 @@ +Share Results and Copy Results on iOS froze the screen while they formatted a large result. From b79bd3177f23ff4eba8317f3df6be87941f8ef7b Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 12:36:12 +0700 Subject: [PATCH 3/3] fix(ios): start the next open from the saved database after a remembered one never answered --- .../Coordinators/ConnectionSessionModel.swift | 12 ++++++++-- .../ConnectionCoordinatorSwitchTests.swift | 24 +++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/apple/TableProMobile/TableProMobile/Coordinators/ConnectionSessionModel.swift b/apple/TableProMobile/TableProMobile/Coordinators/ConnectionSessionModel.swift index 73fc79ed4b..597025a969 100644 --- a/apple/TableProMobile/TableProMobile/Coordinators/ConnectionSessionModel.swift +++ b/apple/TableProMobile/TableProMobile/Coordinators/ConnectionSessionModel.swift @@ -79,6 +79,7 @@ final class ConnectionSessionModel { @ObservationIgnored private var placementEpoch = 0 @ObservationIgnored private var switchesInFlight = 0 @ObservationIgnored private var placementRestored = false + @ObservationIgnored private var rememberedDatabaseHung = false @ObservationIgnored private var scenes: [WeakScene] = [] private let appState: AppState @@ -318,7 +319,10 @@ final class ConnectionSessionModel { do { let saved = record - let target = openTarget(restoring: wanted) + // A remembered database that never answered may hang again, so the next open starts from the saved one and + // switches after, as opens did before. + let target = rememberedDatabaseHung ? record : openTarget(restoring: wanted) + rememberedDatabaseHung = false let landed: OpenLanding var restoring = wanted var refused: RefusedDatabase? @@ -329,6 +333,9 @@ final class ConnectionSessionModel { } catch let thrown { let listRefused = thrown as? TableListRefused let error = listRefused?.error ?? thrown + if target.database != saved.database, case PostgreSQLError.unreachable = error { + rememberedDatabaseHung = true + } // A sign-in is the connection's, not the database's: the saved database would fail the same way. guard target.database != saved.database, isCurrent(attempt), listRefused != nil || Self.mayBlameDatabase(error), @@ -436,7 +443,8 @@ final class ConnectionSessionModel { private nonisolated static let notAboutTheDatabase = [ "password authentication failed", "authentication failed", "fe_sendauth", "no password supplied", "could not connect", "connection refused", "timeout expired", "timed out", "could not translate host name", - "network is unreachable", "no route to host", "server closed the connection", "connection reset", + "network is unreachable", "no route to host", "host is down", "software caused connection abort", + "server closed the connection", "connection reset", "lost connection", "ssl error", "ssl syscall", "server does not support ssl", ] diff --git a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorSwitchTests.swift b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorSwitchTests.swift index 8db428c9c2..70bdf7b059 100644 --- a/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorSwitchTests.swift +++ b/apple/TableProMobile/TableProMobileTests/ConnectionCoordinatorSwitchTests.swift @@ -276,6 +276,30 @@ struct ConnectionCoordinatorSwitchTests { #expect(UserDefaults.standard.string(forKey: ConnectionDefaultsKey.lastDB.name(for: warehouse.id)) == "staging") } + @Test("A remembered database that never answered fails the open once, and the next open starts from the saved one") + func hungRememberedDatabaseYieldsToTheSavedOne() async { + remember(database: "staging", for: warehouse) + defer { forget(warehouse) } + let hung = driver() + hung.connectError = PostgreSQLError.unreachable("Connection timed out.") + factory.queue(hung) + let coordinator = ConnectionCoordinator(connection: warehouse, appState: appState) + coordinator.restorePersistedState() + + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["staging"]) + #expect(coordinator.session == nil) + + let saved = driver(tables: ["facts"]) + factory.queue(saved) + await coordinator.connect() + + #expect(factory.dialed.map { $0.database } == ["staging", "analytics"]) + #expect(coordinator.session?.driver === saved) + #expect(coordinator.displayStatus == .connected) + } + @Test("A remembered database the open cannot reach fails it after one dial, without dialing the saved one") func unreachableRememberedDatabaseDialsOnce() async { remember(database: "staging", for: warehouse)