From eb4690293cf98bf6ee4a3c74a1c474a72e88c53a Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 06:10:21 +0700 Subject: [PATCH] fix(macos): keep the last results of a SQL Server batch and fix the tests #4200 left red --- .../QueryExecutionCoordinator+Batches.swift | 15 ++++++++--- .../Models/Query/QueryBatchResult.swift | 26 +++++++++++++++++++ .../Execution/QueryBatchResultTests.swift | 17 ++++++++++-- .../Helpers/FakeMSSQLPlugin.swift | 3 +++ .../ConnectionFormPortTLSTests.swift | 7 +++-- 5 files changed, 61 insertions(+), 7 deletions(-) diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift index 3072505f7c..b15c7901a0 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift @@ -26,6 +26,7 @@ private struct BatchOutput: Sendable { let rowsAffected: Int let errors: [PluginBatchError] let discardedResultSetCount: Int + let supersededResultSetCount: Int let executionTime: TimeInterval let errorDescription: String? var serverOutput: PluginServerOutput = .none @@ -308,7 +309,8 @@ extension QueryExecutionCoordinator { driver: DatabaseDriver, failureOutput: ServerOutputBox ) async throws -> BatchOutput { - var combined = QueryBatchResult.empty + var earliest = QueryBatchResult.empty + var latest = QueryBatchResult.empty var printed = BatchPrintedOutput() for _ in 0.. QueryBatchResult { + let combined = resultSets + next.resultSets + let superseded = max(combined.count - Self.keptResultSetLimit, 0) + let shifted = { (error: PluginBatchError, preceding: Int) in + PluginBatchError( + message: error.message, + code: error.code, + line: error.line, + procedure: error.procedure, + precedingResultSetCount: max(preceding - superseded, 0) + ) + } + return QueryBatchResult( + resultSets: Array(combined.suffix(Self.keptResultSetLimit)), + rowsAffected: rowsAffected + next.rowsAffected, + errors: errors.map { shifted($0, $0.precedingResultSetCount) } + + next.errors.map { shifted($0, resultSets.count + $0.precedingResultSetCount) }, + discardedResultSetCount: discardedResultSetCount + next.discardedResultSetCount, + executionTime: executionTime + next.executionTime, + supersededResultSetCount: supersededResultSetCount + next.supersededResultSetCount + superseded + ) + } } diff --git a/apple/TableProTests/Core/Services/Execution/QueryBatchResultTests.swift b/apple/TableProTests/Core/Services/Execution/QueryBatchResultTests.swift index f52a2d2e5d..4722b88946 100644 --- a/apple/TableProTests/Core/Services/Execution/QueryBatchResultTests.swift +++ b/apple/TableProTests/Core/Services/Execution/QueryBatchResultTests.swift @@ -2,8 +2,8 @@ // QueryBatchResultTests.swift // TableProTests // -// `GO n` sends a batch n times, and the editor and every external caller fold the answers into one with -// `followed(by:)`, so the ceiling, the counts and the place of each error are decided in one place. +// `GO n` sends a batch n times, and every caller folds the answers into one here, so the ceiling, the counts and +// the place of each error are decided in one place. The editor keeps the newest result sets past the ceiling. // import Foundation @@ -51,4 +51,17 @@ struct QueryBatchResultTests { #expect(combined.errors.map(\.precedingResultSetCount) == [3]) } + + @Test("Keeping the latest, result sets past the ceiling push out the oldest and are counted as superseded") + func latestPastTheCeiling() { + let limit = QueryBatchResult.keptResultSetLimit + let combined = answer(resultSets: limit - 1).followedKeepingLatest(by: answer(resultSets: 3, errorAfter: 3)) + + #expect(combined.resultSets.count == limit) + #expect(combined.resultSets.last?.columns == ["c2"]) + #expect(combined.resultSets.dropLast(3).last?.columns == ["c\(limit - 2)"]) + #expect(combined.supersededResultSetCount == 2) + #expect(combined.discardedResultSetCount == 0) + #expect(combined.errors.map(\.precedingResultSetCount) == [limit]) + } } diff --git a/apple/TableProTests/Helpers/FakeMSSQLPlugin.swift b/apple/TableProTests/Helpers/FakeMSSQLPlugin.swift index 9a3a6f9a6c..a21c207a64 100644 --- a/apple/TableProTests/Helpers/FakeMSSQLPlugin.swift +++ b/apple/TableProTests/Helpers/FakeMSSQLPlugin.swift @@ -193,6 +193,9 @@ final class FakeMSSQLPluginDriver: PluginDriverStub, @unchecked Sendable { } func disconnect() { disconnectCallCount += 1 } + // SQL Server moves a live session with USE, so a statement pinned to a database after a reconnect lands. + func switchDatabase(to database: String) async throws {} + func applyQueryTimeout(_ seconds: Int) async throws { queryTimeoutValues.withLock { $0.append(seconds) } } diff --git a/apple/TableProTests/ViewModels/ConnectionFormPortTLSTests.swift b/apple/TableProTests/ViewModels/ConnectionFormPortTLSTests.swift index f4865d2306..a1fbcb95d6 100644 --- a/apple/TableProTests/ViewModels/ConnectionFormPortTLSTests.swift +++ b/apple/TableProTests/ViewModels/ConnectionFormPortTLSTests.swift @@ -118,10 +118,13 @@ struct ConnectionFormPortTLSTests { #expect(coordinator.ssl.validationIssues.isEmpty) } - @Test("PostgreSQL Verify Identity still asks for a CA certificate") - func postgresStillRequiresCA() { + @Test("PostgreSQL Verify Identity saves without a CA certificate, and Verify CA still asks for one") + func postgresNeedsACAOnlyForVerifyCA() { let coordinator = newConnection(.postgresql) coordinator.ssl.select(.verifyIdentity) + #expect(coordinator.ssl.validationIssues.isEmpty) + + coordinator.ssl.select(.verifyCa) #expect(!coordinator.ssl.validationIssues.isEmpty) } }