From 5f6332f97d482caf234e2dc57eb2fa47902030cc Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 14:39:10 +0700 Subject: [PATCH 01/14] fix(macos): keep T-SQL blocks and rewinds whole, stop CSV sanitizing by default, and fix Stop, defaults and gate cancels across drivers --- .../CassandraConditionalWrite.swift | 41 +++ .../RedisCommandSplitter.swift | 11 + .../SQLParenthesizedSemicolons.swift | 42 +++ .../SQLRoutineBodyTracker.swift | 124 +++++++-- .../SQLUnterminatedStatements.swift | 25 +- .../SqlBlockStructure.swift | 37 ++- .../Sources/TableProTeradataCore/Parcel.swift | 2 + .../TeradataConnection.swift | 204 +++++++++++++- .../TeradataMessages.swift | 8 + .../CassandraConditionalWriteTests.swift | 61 +++++ .../PluginParameterEscapingTests.swift | 11 + .../RedisCommandSplitterTests.swift | 12 + .../SQLParenthesizedSemicolonTests.swift | 77 ++++++ .../SQLStatementCursorIndexTests.swift | 13 +- .../TSQLControlFlowBlockTests.swift | 153 +++++++++++ .../TeradataStopTests.swift | 249 ++++++++++++++++++ .../TextViewController+EmphasizeBracket.swift | 17 +- .../Controller/TextViewControllerTests.swift | 18 ++ .../BeancountPluginDriver.swift | 6 +- .../BigtablePluginDriver+Execution.swift | 6 +- .../CSVExportPlugin/CSVExportModels.swift | 3 +- .../CSVExportOptionsView.swift | 6 +- .../CSVExportPlugin/CSVExportPlugin.swift | 95 +++++-- .../CSVImportPlugin/CSVImportPlugin.swift | 30 ++- .../CassandraPlugin.swift | 4 +- .../CassandraRowWriter.swift | 39 +-- .../DamengDriverPlugin/DamengPlugin.swift | 7 +- .../DynamoDBPluginDriver+Execution.swift | 4 +- .../EtcdDriverPlugin/EtcdPluginDriver.swift | 8 +- .../CSVExportFormulaAndDecimalTests.swift | 174 ++++++++++++ .../ExportImportRoundTripTests.swift | 17 +- .../FormatSettingsChangedMidRunTests.swift | 185 +++++++++++++ .../JSONExportPlugin/JSONExportPlugin.swift | 13 +- .../MarkdownExportPlugin.swift | 17 +- .../MongoDBConnection+SyncHelpers.swift | 5 +- .../MongoDBPluginDriver.swift | 7 +- .../MariaDBPluginConnection.swift | 36 ++- .../MariaDBStopThreadIdTests.swift | 38 +++ .../ParquetExportPlugin.swift | 9 +- .../RedisPluginDriver+Operations.swift | 5 +- .../RedisDriverPlugin/RedisPluginDriver.swift | 6 +- .../RedisConditionalWriteTests.swift | 19 ++ .../SQLImportPlugin/SQLImportPlugin.swift | 14 +- .../PluginDatabaseDriver.swift | 10 +- .../PluginExportUtilities.swift | 66 +++++ .../TableProPluginKit/PluginRowWriters.swift | 23 +- .../TeradataAsyncConnection.swift | 18 +- .../XLSXExportPlugin/XLSXExportPlugin.swift | 30 ++- .../Plugins/XLSXExportPlugin/XLSXWriter.swift | 63 +---- .../XLSXImportPlugin/XLSXImportPlugin.swift | 24 +- .../ChangeTracking/DataChangeModels.swift | 8 +- .../Core/ChangeTracking/DefaultedCells.swift | 38 +++ .../SQLStatementGenerator.swift | 7 +- .../Core/Compare/CompareSQLLiteral.swift | 22 +- .../Core/Compare/DataDiffEngine.swift | 26 +- .../Core/Compare/StreamingRowProvider.swift | 20 +- .../Coordinators/RowEditingCoordinator.swift | 6 +- .../DataWrite/Rewind/RewindExecutor.swift | 3 +- .../Core/DataWrite/Rewind/RewindPlanner.swift | 96 +++++-- .../DataWrite/RowChangeStatementFactory.swift | 111 +++++++- .../DataWrite/RowWriteOperationBuilder.swift | 9 +- .../DatabaseManager+ContainerDDL.swift | 36 +-- .../Database/DatabaseManager+Health.swift | 42 ++- .../Database/DatabaseManager+Principals.swift | 12 +- .../Database/DatabaseManager+Schema.swift | 14 +- .../Database/DatabaseManager+Sessions.swift | 5 +- .../Database/DatabaseManager+Startup.swift | 102 ++++++- .../Core/Database/DatabaseManager.swift | 4 + .../Core/Database/TriggerEditing.swift | 19 +- apple/TablePro/Core/LSP/LSPTransport.swift | 72 ++++- .../Core/ObjectCopy/ObjectCopyRowCopier.swift | 12 +- .../Core/ObjectCopy/ObjectCopyRunner.swift | 16 +- .../Plugins/ExportDataSourceAdapter.swift | 8 +- .../TablePro/Core/Plugins/ExportRowCut.swift | 11 + .../Execution/GatedOperationFailure.swift | 39 +++ .../Query/MetadataConnectionPool.swift | 18 +- .../Services/Query/RowOperationsManager.swift | 59 +++-- .../Core/Services/Query/RowParser.swift | 10 +- .../Query/SelectionSummaryCalculator.swift | 14 +- .../Shortcuts/Shared/PayloadRowPlanner.swift | 50 +++- .../Core/Shortcuts/Shared/RowPayload.swift | 20 +- .../Core/Shortcuts/ShortcutsRowWriter.swift | 5 +- .../UsersRoles/PrincipalChangeManager.swift | 17 +- .../JavaScript/QueryStatementModel.swift | 52 +++- .../Utilities/SQL/DisplayedResultReader.swift | 13 +- .../Core/Utilities/SQL/JsonRowConverter.swift | 2 +- .../Core/Utilities/SQL/QueryClassifier.swift | 13 +- .../Utilities/SQL/ResultJsonSerializer.swift | 2 + .../SQL/SQLRowToStatementConverter.swift | 14 +- .../Core/Vim/VimTextBufferAdapter.swift | 59 ----- .../PluginCellValue+DefaultMarker.swift | 19 +- .../ClickHouse/ClickHousePartInfo.swift | 4 +- .../Models/Query/SelectionSummary.swift | 10 +- .../Schema/ClickHousePartStatements.swift | 46 ++-- .../Models/UI/MultiRowEditState.swift | 8 +- .../TablePro/Resources/Localizable.xcstrings | 41 +++ .../ViewModels/SchemaEditorViewModel.swift | 2 +- .../ViewModels/UserTypeEditorModel.swift | 2 +- .../UsersRolesViewModel+Changes.swift | 2 +- .../Views/Compare/CompareRowDiffPane.swift | 15 ++ .../DataGrid/Cells/DataGridCellContent.swift | 3 +- .../Cells/DataGridCellPresenter.swift | 2 + .../Commands/DataGridCommands+Copy.swift | 1 + .../Commands/DataGridCommands+SetValue.swift | 4 +- .../DataGrid/DataGridViewController.swift | 3 +- .../CreateDatabaseSheet.swift | 2 +- .../Views/Editor/StatementRunController.swift | 50 +++- .../Views/Main/Child/DataTabSQLExporter.swift | 10 +- .../Main/Child/MainEditorContentView.swift | 1 + .../MainContentCoordinator+Navigation.swift | 11 +- ...ntentCoordinator+SafeModePromptRetry.swift | 28 ++ .../Main/MainContentCommandActions.swift | 67 +++-- .../Views/Main/MainContentCoordinator.swift | 9 +- .../ObjectCopy/CopyObjectsResultView.swift | 5 + .../UserType/CreateUserTypeSheet.swift | 9 +- .../Views/Results/ResultsJsonView.swift | 9 + .../Views/Sidebar/EditSequenceSheet.swift | 2 +- .../Views/Sidebar/ObjectCommentSheet.swift | 4 +- .../Views/Sidebar/SetSequenceValueSheet.swift | 2 +- .../Views/Structure/ClickHousePartsView.swift | 217 +++++++++------ .../Views/Structure/CreateTableDraft.swift | 15 +- .../StructureEditingSession+Apply.swift | 51 +--- ...tructureEditingSession+ColumnReorder.swift | 4 +- .../Views/Structure/TriggerDetailView.swift | 2 +- .../Views/Structure/TriggerEditorView.swift | 18 +- .../Completion/QueryCompletionCatalog.swift | 51 +++- .../QueryCompletionCatalogTests.swift | 29 ++ .../SQLStatementGeneratorBitColumnTests.swift | 4 +- .../SQLStatementGeneratorRowMatchTests.swift | 6 +- .../SQLStatementGeneratorTests.swift | 69 ++++- .../Compare/CompareDriverRowCutTests.swift | 90 +++++++ .../Core/Compare/CompareSQLLiteralTests.swift | 27 +- .../Compare/DataSyncScriptBuilderTests.swift | 2 +- .../RowEditingCoordinatorUUIDKeyTests.swift | 2 +- .../Core/DataWrite/RewindPlannerTests.swift | 81 ++++++ ...wChangeStatementFactoryCoverageTests.swift | 4 +- ...hangeStatementFactoryExpressionTests.swift | 84 +++++- .../RowWriteOperationBuilderTests.swift | 41 ++- .../Core/Database/DatabaseManagerTests.swift | 5 +- .../Database/SchemaWriteGateDenialTests.swift | 86 ++++++ .../Database/StartupCommandWarningTests.swift | 135 ++++++++++ .../Database/TriggerInfoMappingTests.swift | 86 ++++++ .../LSP/LSPTransportBrokenPipeTests.swift | 48 ++++ .../ObjectCopy/ObjectCopyRowCutTests.swift | 107 ++++++++ .../Query/MetadataConnectionPoolTests.swift | 21 +- .../SelectionSummaryCalculatorTests.swift | 17 +- .../RowOperationsFieldAbsenceTests.swift | 4 +- .../RowOperationsManagerPasteTests.swift | 18 +- .../Services/RowOperationsManagerTests.swift | 44 +++- .../RowOperationsManagerUUIDKeyTests.swift | 25 +- .../Services/RowPasteForeignTextTests.swift | 16 +- .../PayloadRowPlannerHeaderMatchTests.swift | 59 +++++ .../Shortcuts/RowPayloadFormatTests.swift | 60 +++++ .../PrincipalApplyOpenTransactionTests.swift | 44 ++++ .../PrincipalChangeManagerTests.swift | 40 +++ .../Utilities/JsonRowConverterTests.swift | 18 +- .../SQL/QueryClassifierTSQLBlockTests.swift | 39 +++ .../SQLRowToStatementConverterTests.swift | 31 ++- .../Vim/VimTextBufferAdapterPerfTests.swift | 103 ++------ .../Helpers/StubExecutionGates.swift | 9 +- .../Models/MultiRowEditStateTests.swift | 23 +- .../ClickHousePartStatementsTests.swift | 81 ++++-- .../Editor/StatementCaretTableTests.swift | 109 ++++++++ .../TableLoadAfterSafeModePromptTests.swift | 40 +++ .../WindowCloseSaveEveryConnectionTests.swift | 129 +++++++++ .../Views/Results/ResultsJsonViewTests.swift | 24 ++ .../SelectionSummaryTrackerTests.swift | 2 +- .../Structure/ClickHousePartsGridTests.swift | 101 ++++++- .../StructureListGridAccessibilityTests.swift | 3 +- .../StructureSaveConfirmationTests.swift | 10 +- .../TriggerEditorFontSizeTests.swift | 25 ++ core/crates/core/src/export/csv.rs | 55 ++-- core/crates/core/src/export/csv_options.rs | 4 +- core/crates/core/src/export/encoder/tests.rs | 6 +- core/crates/mcp/src/export_tools.rs | 10 +- .../storage/src/query_history/export.rs | 6 +- .../src/conformance/cases/large_table.rs | 9 +- .../src/conformance/cases/round_trip.rs | 8 +- .../crates/app/src/ui/export/encoding_sink.rs | 2 +- .../app/src/ui/export/export_ledger/tests.rs | 12 +- linux/crates/platform/tests/settings.rs | 2 +- .../data/app.tablepro.TablePro.gschema.xml.in | 2 +- linux/docs/storage.md | 2 +- 183 files changed, 4958 insertions(+), 1068 deletions(-) create mode 100644 apple/Packages/TableProCore/Sources/TableProCassandraCore/CassandraConditionalWrite.swift create mode 100644 apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLParenthesizedSemicolons.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProCassandraCoreTests/CassandraConditionalWriteTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLParenthesizedSemicolonTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/TSQLControlFlowBlockTests.swift create mode 100644 apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataStopTests.swift create mode 100644 apple/Plugins/FormatPluginTests/CSVExportFormulaAndDecimalTests.swift create mode 100644 apple/Plugins/FormatPluginTests/FormatSettingsChangedMidRunTests.swift create mode 100644 apple/Plugins/MySQLDriverTests/MariaDBStopThreadIdTests.swift create mode 100644 apple/TablePro/Core/ChangeTracking/DefaultedCells.swift create mode 100644 apple/TablePro/Core/Services/Execution/GatedOperationFailure.swift create mode 100644 apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SafeModePromptRetry.swift create mode 100644 apple/TableProTests/Core/Compare/CompareDriverRowCutTests.swift create mode 100644 apple/TableProTests/Core/Database/SchemaWriteGateDenialTests.swift create mode 100644 apple/TableProTests/Core/Database/StartupCommandWarningTests.swift create mode 100644 apple/TableProTests/Core/LSP/LSPTransportBrokenPipeTests.swift create mode 100644 apple/TableProTests/Core/ObjectCopy/ObjectCopyRowCutTests.swift create mode 100644 apple/TableProTests/Core/Shortcuts/PayloadRowPlannerHeaderMatchTests.swift create mode 100644 apple/TableProTests/Core/Shortcuts/RowPayloadFormatTests.swift create mode 100644 apple/TableProTests/Core/Utilities/SQL/QueryClassifierTSQLBlockTests.swift create mode 100644 apple/TableProTests/Views/Editor/StatementCaretTableTests.swift create mode 100644 apple/TableProTests/Views/Main/TableLoadAfterSafeModePromptTests.swift create mode 100644 apple/TableProTests/Views/Main/WindowCloseSaveEveryConnectionTests.swift create mode 100644 apple/TableProTests/Views/Structure/TriggerEditorFontSizeTests.swift diff --git a/apple/Packages/TableProCore/Sources/TableProCassandraCore/CassandraConditionalWrite.swift b/apple/Packages/TableProCore/Sources/TableProCassandraCore/CassandraConditionalWrite.swift new file mode 100644 index 0000000000..a4772dda72 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProCassandraCore/CassandraConditionalWrite.swift @@ -0,0 +1,41 @@ +// +// CassandraConditionalWrite.swift +// TableProCassandraCore +// + +import Foundation + +// A grid write names its row by key and is conditioned on it: an INSERT is an upsert, and an UPDATE writes its cells +// whether or not the row is there, so either would otherwise replace or recreate a row the grid never showed. +// A condition that fails answers one row whose `[applied]` is false, which the server reports as a success. +public enum CassandraConditionalWrite { + public static let appliedColumn = "[applied]" + public static let insertCondition = "IF NOT EXISTS" + public static let updateCondition = "IF EXISTS" + + public enum Refusal: Sendable, Equatable { + case keyTaken + case rowMissing + } + + public static func insertStatement(target: String, names: [String], values: [String]) -> String { + "INSERT INTO \(target) (\(names.joined(separator: ", "))) VALUES (\(values.joined(separator: ", "))) " + + insertCondition + } + + public static func updateStatement(target: String, assignments: [String], keyCondition: String) -> String { + "UPDATE \(target) SET \(assignments.joined(separator: ", ")) WHERE \(keyCondition) \(updateCondition)" + } + + public static func refusal(statement: String, firstColumn: String?, firstValue: String?) -> Refusal? { + guard firstColumn == appliedColumn, firstValue?.lowercased() == "false" else { return nil } + let normalized = statement.uppercased() + if normalized.hasPrefix("INSERT"), normalized.hasSuffix(" " + insertCondition) { + return .keyTaken + } + if normalized.hasPrefix("UPDATE"), normalized.hasSuffix(" " + updateCondition) { + return .rowMissing + } + return nil + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisCommandSplitter.swift b/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisCommandSplitter.swift index 8d42794c8d..d92e7bc4bc 100644 --- a/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisCommandSplitter.swift +++ b/apple/Packages/TableProCore/Sources/TableProRedisCore/RedisCommandSplitter.swift @@ -32,6 +32,17 @@ public enum RedisCommandSplitter { return nil } + // Every command with the last caret offset `command(at:in:)` gives it, for a caller that keeps them between + // caret moves. + public static func commandsWithCaretReach(in text: String) -> [(command: Command, reach: Int)] { + var lexer = RedisCommandLexer(text, endsCommands: true, decodesArguments: false) + var commands: [(command: Command, reach: Int)] = [] + while let found = lexer.next() { + commands.append((command: command(found, in: text), reach: found.reach)) + } + return commands + } + private static func command(_ found: RedisCommandLexer.Command, in text: String) -> Command { let range = NSRange(location: found.start, length: found.end - found.start) return Command(text: (text as NSString).substring(with: range), range: range) diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLParenthesizedSemicolons.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLParenthesizedSemicolons.swift new file mode 100644 index 0000000000..d5d1607083 --- /dev/null +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLParenthesizedSemicolons.swift @@ -0,0 +1,42 @@ +import Foundation + +// The scanner keeps the `;`s of a PostgreSQL rule's `DO ALSO (...; ...)` in its statement. A client that cuts at every +// `;` runs each piece, so a gate tiers them too: `DO ALSO (SELECT 1; DELETE FROM t)` by its `DELETE`. +enum SQLParenthesizedSemicolons { + static func pieces(in statement: String, grammar: SQLLexicalGrammar) -> [String] { + guard statement.utf16.contains(SqlLexer.semicolon) else { return [] } + let text = statement as NSString + let length = text.length + var cuts: [Int] = [] + var depth = 0 + var index = 0 + while index < length { + if let span = SQLNonCodeSpan.span(at: index, in: text, grammar: grammar) { + index = max(span.end, index + 1) + continue + } + switch text.character(at: index) { + case SqlLexer.openParen: + depth += 1 + case SqlLexer.closeParen: + depth = max(0, depth - 1) + case SqlLexer.semicolon where depth > 0: + cuts.append(index) + default: + break + } + index += 1 + } + guard !cuts.isEmpty else { return [] } + var pieces: [String] = [] + var start = 0 + for cut in cuts + [length] { + let piece = StatementBlank.trimming(text.substring(with: NSRange(location: start, length: cut - start))) + if !piece.isEmpty { + pieces.append(piece) + } + start = cut + 1 + } + return pieces + } +} diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLRoutineBodyTracker.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLRoutineBodyTracker.swift index f1596e7ceb..d4abc7cc10 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLRoutineBodyTracker.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLRoutineBodyTracker.swift @@ -2,13 +2,26 @@ import Foundation /// Statement boundaries for the dialects whose only `;`-holding construct is a routine body written `BEGIN ... END`. /// -/// A `BEGIN` opens a body only inside a statement that defines a routine, for the safety reason recorded on +/// Outside T-SQL a `BEGIN` opens a body only inside a statement that defines a routine, for the safety reason recorded on /// ``SqlBlockStructure/opensRoutineDefinition(_:)``. The `;` belongs to the statement only after a T-SQL `MERGE`, which /// ``SQLLexicalGrammar/terminatedMergeStatements`` asks for: these engines accept every other statement without it. /// /// `BEGIN`, `CASE` and `END` count only where a keyword can stand: an engine that does not reserve them takes them as -/// names, and a block a name opened would never close. A text left inside one is refused by the external gate. +/// names, and a block a name opened would never close. T-SQL reserves all three, so there a block also opens outside +/// a routine, as in `IF @x = 1 BEGIN ... END` and `BEGIN TRY ... END TRY BEGIN CATCH ... END CATCH`. A text left +/// inside one is refused by the external gate. +/// +/// A `;` inside parentheses ends the statement, so a stray `(` cannot swallow what follows it, except in a PostgreSQL +/// rule's action list, `DO ALSO (INSERT ...; INSERT ...)`, which holds its commands together. public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { + // A PostgreSQL `CREATE RULE`: its header, the `DO [ALSO | INSTEAD]` before the action list, and the list itself. + private enum RuleAction: Equatable { + case none + case header + case afterDo + case list(depth: Int) + } + private enum Header { case opener /// After `CREATE`, `ALTER` or `REPLACE`. `accountWords` is what is left of an unquoted `DEFINER = user@host`. @@ -25,6 +38,11 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { private static let statementIntroducers: Set = ["THEN", "ELSE", "DO", "LOOP", "REPEAT"] private let grammar: SQLLexicalGrammar + private let readsBlocksEverywhere: Bool + // T-SQL outside a routine: where a block's `BEGIN` can stand, as the statement's first word, after `IF`, `WHILE` + // or `ELSE`, or after a block the statement already opened. Anywhere else `begin` is in a name's place. + private var followsControlFlow = false + private var pendingBeginMayOpen = false private var header = Header.opener private var definesRoutine = false private var openedBlockBody = false @@ -34,6 +52,8 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { /// whatever word follows it. private var openCaseExpressions = 0 private var parenDepth = 0 + private var ruleAction = RuleAction.none + private var ruleActionHeldSemicolon = false private var pendingBegin = false private var pendingEnd = false private var followsName = false @@ -50,12 +70,15 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { public init(grammar: SQLLexicalGrammar) { self.grammar = grammar + self.readsBlocksEverywhere = grammar.contains(.unterminatedStatements) self.merge = grammar.contains(.terminatedMergeStatements) ? SQLMergeStatementTracker() : nil } public var needsWords: Bool { + // Inside parentheses no word opens or closes a block, and a `MERGE` there needs no `;`. + if readsBlocksEverywhere { return parenDepth == 0 } if case .read = header { - return definesRoutine || merge?.needsWords == true + return definesRoutine || ruleAction == .header || ruleAction == .afterDo || merge?.needsWords == true } return true } @@ -70,7 +93,7 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { // An `END` still waiting for the word after it closes a block when the text ends. public var holdsOpenBlock: Bool { - depth > (pendingEnd ? 1 : 0) + depth > (pendingEnd ? 1 : 0) || ruleActionHeldSemicolon } public mutating func observeWord(_ word: String) { @@ -87,10 +110,27 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { startsBodyStatement = false let statementPosition = atStatementPosition observeStatementPosition(after: word, wasAtStatementPosition: statementPosition) - guard !isMember, readHeader(word), definesRoutine, parenDepth == 0 else { return } + guard !isMember else { return } + var isFirstWord = false + if case .opener = header { + isFirstWord = true + } + let pastHeader = readHeader(word) + if pastHeader, parenDepth == 0 { + observeRuleWord(word) + } + guard parenDepth == 0, readsBlocksEverywhere || (pastHeader && definesRoutine) else { return } switch word { - case "BEGIN" where opensNestedBlock(atStatementPosition: statementPosition): + case "BEGIN" where readsBlocksEverywhere: + pendingBegin = true + pendingBeginMayOpen = opensNestedBlock(atStatementPosition: statementPosition, isFirstWord: isFirstWord) + case "BEGIN" where opensNestedBlock(atStatementPosition: statementPosition, isFirstWord: isFirstWord): pendingBegin = true + pendingBeginMayOpen = true + case "IF" where readsBlocksEverywhere, "WHILE" where readsBlocksEverywhere: + followsControlFlow = true + case "ELSE" where readsBlocksEverywhere && openCaseExpressions == 0: + followsControlFlow = true case "CASE" where !grammar.hasSingleBodyForm: depth += 1 openCaseExpressions += 1 @@ -103,6 +143,9 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { public mutating func observeSymbol(_ symbol: UInt16) { merge?.observeSymbol(symbol) + if ruleAction == .afterDo, symbol != SqlLexer.openParen { + ruleAction = .none + } let opensBlock = symbol == SqlLexer.openParen && !grammar.contains(.atomicRoutineBodies) settlePendingBeforeNonWord(opensBlock: opensBlock) followsDot = followsName && symbol == Self.dot @@ -112,9 +155,16 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { switch symbol { case SqlLexer.openParen: parenDepth += 1 + if ruleAction == .afterDo { + ruleAction = .list(depth: parenDepth) + } endName() case SqlLexer.closeParen: parenDepth = max(0, parenDepth - 1) + if case .list(let listDepth) = ruleAction, parenDepth < listDepth { + ruleAction = .none + ruleActionHeldSemicolon = false + } default: break } @@ -122,6 +172,9 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { public mutating func observeOpaqueToken() { merge?.observeOpaqueToken() + if ruleAction == .afterDo { + ruleAction = .none + } settlePendingBeforeNonWord(opensBlock: !grammar.contains(.atomicRoutineBodies)) followsName = true followsDot = false @@ -140,13 +193,27 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { pendingEnd = false closeBlock() } + if readsBlocksEverywhere { + // A T-SQL `CASE` is only an expression, which no `;` can stand inside, so one still open was never closed. + depth = max(0, depth - openCaseExpressions) + } openCaseExpressions = 0 - parenDepth = 0 followsName = false followsDot = false - let endsStatement = depth == 0 - startsBodyStatement = !endsStatement - atStatementPosition = !endsStatement + let heldByRuleAction: Bool + if case .list(let listDepth) = ruleAction { + heldByRuleAction = depth == 0 && parenDepth >= listDepth + } else { + heldByRuleAction = false + } + if heldByRuleAction { + ruleActionHeldSemicolon = true + } else { + parenDepth = 0 + } + let endsStatement = depth == 0 && !heldByRuleAction + startsBodyStatement = depth > 0 + atStatementPosition = depth > 0 labelMayFollow = false inDeclaration = false inHandlerDeclaration = false @@ -175,6 +242,9 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { } return false case .kind(let accountWords): + if word == "RULE", grammar.contains(.atomicRoutineBodies) { + ruleAction = .header + } if SqlBlockStructure.isRoutineKind(word, grammar: grammar) { definesRoutine = true header = .name @@ -207,11 +277,33 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { } } - private func opensNestedBlock(atStatementPosition: Bool) -> Bool { - guard depth > 0, !grammar.hasSingleBodyForm, !grammar.contains(.unterminatedStatements) else { return true } + private func opensNestedBlock(atStatementPosition: Bool, isFirstWord: Bool) -> Bool { + if readsBlocksEverywhere { + return depth > 0 || definesRoutine || isFirstWord || followsControlFlow || openedBlockBody + } + guard depth > 0, !grammar.hasSingleBodyForm else { return true } return atStatementPosition || inHandlerDeclaration } + private mutating func observeRuleWord(_ word: String) { + switch ruleAction { + case .header where word == "DO": + ruleAction = .afterDo + case .afterDo where word == "ALSO" || word == "INSTEAD": + break + case .afterDo: + ruleAction = .none + default: + break + } + } + + // `BEGIN TRY` and `BEGIN CATCH` are blocks wherever they stand, and nothing else follows a name that way. + private func beginOpensBlock(followedBy word: String) -> Bool { + if readsBlocksEverywhere, word == "TRY" || word == "CATCH" { return true } + return pendingBeginMayOpen && !SqlBlockStructure.beginStartsStatement(followedBy: word, grammar: grammar) + } + private mutating func endName() { if case .name = header { header = .read @@ -227,7 +319,7 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { openBlockBody() return true } - if !SqlBlockStructure.beginStartsTransaction(followedBy: word) { + if beginOpensBlock(followedBy: word) { openBlockBody() } } @@ -237,7 +329,7 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { closeBlock() return false } - switch SqlBlockStructure.endingFollowedBy(word, closesCaseExpression: openCaseExpressions > 0) { + switch SqlBlockStructure.endingFollowedBy(word, closesCaseExpression: openCaseExpressions > 0, grammar: grammar) { case .closesControlFlow: return true case .closesCaseStatement: @@ -254,7 +346,9 @@ public struct SQLRoutineBodyTracker: SQLStatementBoundaryTracking { private mutating func settlePendingBeforeNonWord(opensBlock: Bool) { if pendingBegin { pendingBegin = false - if opensBlock { + // A T-SQL `begin(` outside a routine is a name, as in `CREATE TABLE begin(a int)`. + let opensAfterName = !readsBlocksEverywhere || definesRoutine || depth > 0 + if opensBlock, pendingBeginMayOpen, opensAfterName { openBlockBody() } } diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnterminatedStatements.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnterminatedStatements.swift index 38f254c5bd..851741b05f 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnterminatedStatements.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLUnterminatedStatements.swift @@ -20,11 +20,13 @@ public enum SQLUnterminatedStatements { /// Every text a gate has to tier for `statement`: the statement itself, then, when `grammar` needs no terminator, /// the text of each statement that begins inside it, less the statements nested in that one, which come on their /// own. A grammar that takes a statement in place of a value or a table adds what ``SQLEmbeddedStatements`` finds, - /// and one with a pipe operator each statement ``SQLPipeOperatorChain`` finds. + /// and one with a pipe operator each statement ``SQLPipeOperatorChain`` finds. A grammar that needs a terminator + /// also adds the pieces a `;` inside the statement's parentheses separates, from ``SQLParenthesizedSemicolons``. public static func runnable(in statement: String, grammar: SQLLexicalGrammar) -> [String] { guard grammar.contains(.unterminatedStatements) else { return [statement] + SQLEmbeddedStatements.texts(in: statement, grammar: grammar) + SQLPipeOperatorChain.statements(in: statement, grammar: grammar) + + SQLParenthesizedSemicolons.pieces(in: statement, grammar: grammar) } let text = statement as NSString let tokens = TSQLTokens.read(text, grammar: grammar) @@ -32,6 +34,27 @@ public enum SQLUnterminatedStatements { return [statement] + ownTexts(of: statementSpans(in: tokens, length: text.length), in: text) } + // A scanner statement on T-SQL keeps a `;` outside parentheses only when a block holds it, so anything after one but + // the block's `END`, an `ELSE` or another `;` is a second statement the block runs. A stored routine runs nothing. + public static func blockHoldsSeveralStatements(_ statement: String, grammar: SQLLexicalGrammar) -> Bool { + guard grammar.contains(.unterminatedStatements) else { return false } + let tokens = TSQLTokens.read(statement as NSString, grammar: grammar) + guard !definesRoutine(tokens) else { return false } + return tokens.indices.dropLast().contains { index in + guard tokens[index].kind == .terminator, tokens[index].depth == 0 else { return false } + let next = tokens[index + 1] + guard next.kind != .terminator else { return false } + switch next.word { + case "ELSE": + return false + case "END": + return tokens.indices.contains(index + 2) && tokens[index + 2].word == "CONVERSATION" + default: + return true + } + } + } + /// The reserved keywords that begin a T-SQL statement. `ELSE` and `FETCH` are left out because they also continue /// a `CASE` and an `OFFSET` clause, and the statement after an `ELSE` begins with a keyword of its own. `WITH` is /// left out because a common table expression cannot follow a statement without a `;`, and the word also opens diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SqlBlockStructure.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SqlBlockStructure.swift index 0a11e84e6d..6968b11fef 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SqlBlockStructure.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SqlBlockStructure.swift @@ -23,7 +23,8 @@ public enum SqlBlockStructure { /// What an `END` means, given the word after it. public enum EndFollower: Equatable, Sendable { - /// `END IF`, `END LOOP` and their kin close a construct that never opened a block, and the word is theirs. + /// `END IF`, `END LOOP` and their kin close a construct that never opened a block, and T-SQL's + /// `END CONVERSATION` is a statement that closes nothing. Either way the word is theirs. case closesControlFlow /// `END CASE` closes the `CASE` that opened a block, and the word is theirs. case closesCaseStatement @@ -48,6 +49,10 @@ public enum SqlBlockStructure { /// second opener. private static let controlFlowFollowers: Set = ["IF", "LOOP", "WHILE", "FOR", "REPEAT"] + // T-SQL's `BEGIN DIALOG`, `BEGIN CONVERSATION TIMER` and `END CONVERSATION` are statements, not a block's ends. + private static let serviceBrokerFollowers: Set = ["DIALOG", conversationKeyword] + private static let conversationKeyword = "CONVERSATION" + /// The keywords a statement has to open with before a `BEGIN` inside it is read as a routine body. /// /// Only ``SQLRoutineBodyTracker`` consults this, and the reason is safety rather than grammar. An anonymous @@ -56,7 +61,9 @@ public enum SqlBlockStructure { /// single statement whose first word is `BEGIN`, and `QueryClassifier` tiers a statement by its leading keyword, /// so a swallowed `DROP` would be tiered a plain write and skip the destructive confirmation. Folding cannot /// execute anything, so it takes every block. Oracle, whose anonymous blocks are the everyday form, has its own - /// grammar in ``PLSQLUnitTracker`` and a classifier that reads the whole block. + /// grammar in ``PLSQLUnitTracker`` and a classifier that reads the whole block. T-SQL reserves `BEGIN`, so its + /// blocks open outside a routine too, and the gate tiers every statement inside one through + /// ``SQLUnterminatedStatements``. private static let routineDefinitionOpeners: Set = ["CREATE", "ALTER", "REPLACE"] /// What such a statement has to define, for the same reason. `BEGIN` is not reserved on PostgreSQL, MySQL or @@ -111,6 +118,28 @@ public enum SqlBlockStructure { return transactionFollowers.contains(keyword) } + public static func beginStartsStatement(followedBy keyword: String?, grammar: SQLLexicalGrammar) -> Bool { + if beginStartsTransaction(followedBy: keyword) { return true } + return readsTSQLBlocks(grammar) && keyword.map(serviceBrokerFollowers.contains) == true + } + + // T-SQL closes every block with a bare `END`: in `END IF @x = 1 ...` the `IF` begins the next statement. + public static func endingFollowedBy( + _ keyword: String, + closesCaseExpression: Bool, + grammar: SQLLexicalGrammar + ) -> EndFollower { + guard readsTSQLBlocks(grammar) else { + return endingFollowedBy(keyword, closesCaseExpression: closesCaseExpression) + } + return !closesCaseExpression && keyword == conversationKeyword ? .closesControlFlow : .closesBlock + } + + // The grammar whose statements need no `;` between them is T-SQL's, which reserves `BEGIN`, `CASE` and `END`. + private static func readsTSQLBlocks(_ grammar: SQLLexicalGrammar) -> Bool { + grammar.contains(.unterminatedStatements) + } + /// What an `END` followed by `keyword` closes. /// /// - Parameter closesCaseExpression: whether a `CASE` is open that has held no `;`. Only the statement form can @@ -260,7 +289,7 @@ public enum SqlBlockStructure { let cursor = skipTrivia(from: offset, in: text, length: length, grammar: grammar) guard cursor < length else { return true } guard text.character(at: cursor) != SqlLexer.semicolon else { return true } - return beginStartsTransaction(followedBy: readKeyword(text, at: cursor, length: length).text) + return beginStartsStatement(followedBy: readKeyword(text, at: cursor, length: length).text, grammar: grammar) } /// Goes by the word after the `END` alone: only folding asks, and it keeps no record of which `CASE` is open. @@ -273,7 +302,7 @@ public enum SqlBlockStructure { let cursor = skipTrivia(from: offset, in: text, length: length, grammar: grammar) guard cursor < length else { return .closesBlock(resumeAt: offset) } let follower = readKeyword(text, at: cursor, length: length) - switch endingFollowedBy(follower.text, closesCaseExpression: false) { + switch endingFollowedBy(follower.text, closesCaseExpression: false, grammar: grammar) { case .closesControlFlow: return .none case .closesCaseStatement: diff --git a/apple/Packages/TableProCore/Sources/TableProTeradataCore/Parcel.swift b/apple/Packages/TableProCore/Sources/TableProTeradataCore/Parcel.swift index e00a7551a5..cb810a30f5 100644 --- a/apple/Packages/TableProCore/Sources/TableProTeradataCore/Parcel.swift +++ b/apple/Packages/TableProCore/Sources/TableProTeradataCore/Parcel.swift @@ -4,6 +4,8 @@ import TableProWireIO enum ParcelFlavor: UInt16 { case request = 1 case response = 4 + case abort = 6 + case cancel = 7 case success = 8 case failure = 9 case record = 10 diff --git a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift index ed5608fe9f..440de15bef 100644 --- a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift +++ b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataConnection.swift @@ -1,7 +1,8 @@ import Darwin import Foundation -public final class TeradataConnection { +// One queue drives it. Only `cancel()` comes from elsewhere, and what it reads and sends is under socketLock or wireLock. +public final class TeradataConnection: @unchecked Sendable { private let config: TeradataConnectionConfig private let socketLock = NSLock() private var _socket: (any TeradataTransport)? @@ -20,13 +21,56 @@ public final class TeradataConnection { private var gssSequence: UInt64 = 0 private var confidentialityBypassed = false + // Orders every post-logon send with the counters it consumes, since Stop sends its abort from another thread. + private let wireLock = NSLock() + // Under socketLock, which is never held across I/O. + private var _phase = RequestPhase.idle + private var _stopRequested = false + private var _operationActive = false + // The request a stop is waiting on the server for, so the stop's deadline closes nothing that came after it. + private var _stoppingRequest: UInt32? + + // 3110: "The transaction was aborted by the user." + static let userAbortCode = 3_110 + private static let abortLockWait: TimeInterval = 0.25 + // A server or network that stops answering would otherwise hold a stop for the whole 20-second receive timeout. + static let defaultStopGrace: TimeInterval = 5 + private let stopGrace: TimeInterval + private let stopDeadlineQueue = DispatchQueue(label: "com.TablePro.teradata.stop-deadline") + + private enum RequestPhase: Equatable { + case idle + case connecting + case awaitingResponse(UInt32) + case fetching(UInt32) + } + + private enum StopAction { + case nothing + case abort(UInt32) + case endOutput(UInt32) + case close + } + + private enum Exchange { + case answered([Parcel]) + case notSent + case outputEnded + } + public init(config: TeradataConnectionConfig) { self.config = config + stopGrace = Self.defaultStopGrace } - init(config: TeradataConnectionConfig, transport: any TeradataTransport) { + init( + config: TeradataConnectionConfig, + transport: any TeradataTransport, + stopGrace: TimeInterval = TeradataConnection.defaultStopGrace + ) { self.config = config self._socket = transport + self.stopGrace = stopGrace confidentialityBypassed = true } @@ -49,6 +93,7 @@ public final class TeradataConnection { socketLock.withLock { _socket = socket _hasLostConnection = false + _phase = .connecting } var established = false defer { @@ -57,6 +102,7 @@ public final class TeradataConnection { self.socket = nil connectDeadline = nil } + socketLock.withLock { _phase = .idle } } serverIP = Self.resolveAddress(config.host) ?? config.host _ = try deadline.remainingMilliseconds() @@ -91,28 +137,91 @@ public final class TeradataConnection { let body = TeradataMessages.optionsParcel(returnStatementInfo: false).encoded() + TeradataMessages.requestParcel(sql: sql).encoded() + TeradataMessages.responseParcel().encoded() - let parcels: [Parcel] + let outcome: Exchange do { - parcels = try exchange(body, requestNumber: requestNumber) + outcome = try exchange(body, requestNumber: requestNumber) } catch { + endRequest() // The reply still on its way would be read as the next request's. dropTransport() throw error } - return try TeradataResultParser.parse(parcels) + let stopped = endRequest() + guard case .answered(let parcels) = outcome else { throw TeradataWireError.cancelled } + do { + return try TeradataResultParser.parse(parcels) + } catch TeradataWireError.server(let code, _) where stopped && code == Self.userAbortCode { + // The server rolled back the transaction the request ran in; the host has no way yet to say so. + throw TeradataWireError.cancelled + } } - private func exchange(_ body: [UInt8], requestNumber: UInt32) throws -> [Parcel] { - try sendEncrypted(kind: .start, body: body, requestNumber: requestNumber) + private func exchange(_ body: [UInt8], requestNumber: UInt32) throws -> Exchange { + let sent = try wireLock.withLock { () -> Bool in + // A stop that landed before the request went out refuses it, and the session stays as it was. + let refused = socketLock.withLock { () -> Bool in + guard !_stopRequested else { return true } + _phase = .awaitingResponse(requestNumber) + return false + } + guard !refused else { return false } + try sendEncryptedLocked(kind: .start, body: body, requestNumber: requestNumber) + return true + } + guard sent else { return .notSent } var parcels: [Parcel] = [] while true { let batch = try readReply() parcels.append(contentsOf: batch) - if Self.responseIsComplete(batch) { return parcels } + if Self.responseIsComplete(batch) { return .answered(parcels) } + // Once the first reply is out an abort no longer takes effect, so a stop ends the output instead. + let stopRequested = socketLock.withLock { () -> Bool in + _phase = .fetching(requestNumber) + return _stopRequested + } + if stopRequested { + try endOutput(requestNumber: requestNumber) + return .outputEnded + } try sendContinue(requestNumber: requestNumber) } } + // The Cancel parcel in place of the Response parcel deletes the request's spool, and the server answers with + // End Request. One that does not throws, and the caller gives the transport up. + private func endOutput(requestNumber: UInt32) throws { + try sendEncrypted(kind: .cont, body: TeradataMessages.cancelParcel().encoded(), requestNumber: requestNumber) + guard Self.responseIsComplete(try readReply()) else { + throw TeradataWireError.malformed("the server kept answering a cancelled request") + } + } + + // A caller's operation, so a stop that lands before its request goes out still reaches it. + public func beginOperation() { + socketLock.withLock { + _operationActive = true + _stopRequested = false + } + } + + public func endOperation() { + socketLock.withLock { + _operationActive = false + _stopRequested = false + } + } + + @discardableResult + private func endRequest() -> Bool { + socketLock.withLock { + let stopped = _stopRequested + _phase = .idle + _stopRequested = false + _stoppingRequest = nil + return stopped + } + } + private func dropTransport() { let dropped = socketLock.withLock { let transport = _socket @@ -129,19 +238,80 @@ public final class TeradataConnection { return } if sessionNumber != 0 { - let message = LanMessage( - kind: .logoff, body: TeradataMessages.logoffParcel().encoded(), - sessionNumber: sessionNumber, byteVar: 0, - authentication: TeradataMessages.authBytes(nextMessage()), hostCharSet: charsetCode) - try? socket.send(message.encoded()) + wireLock.withLock { + let message = LanMessage( + kind: .logoff, body: TeradataMessages.logoffParcel().encoded(), + sessionNumber: sessionNumber, byteVar: 0, + authentication: TeradataMessages.authBytes(nextMessage()), hostCharSet: charsetCode) + try? socket.send(message.encoded()) + } } socket.close() self.socket = nil connectDeadline = nil } + // A request the server is still working on is aborted on the session, which rolls back its transaction, answers it + // with failure 3110 and stays logged on. Past the first reply the request's own loop ends the output instead, and a + // request not yet sent is refused. A connect has no request to stop, so it gives the transport up, as does an + // abort that cannot be sent or a server that has not ended the request within the grace. public func cancel() { - socket?.cancel() + let action: StopAction = socketLock.withLock { + switch _phase { + case .idle: + if _operationActive { _stopRequested = true } + return .nothing + case .connecting: + return .close + case .awaitingResponse(let request): + _stopRequested = true + _stoppingRequest = request + return .abort(request) + case .fetching(let request): + _stopRequested = true + _stoppingRequest = request + return .endOutput(request) + } + } + switch action { + case .nothing: + return + case .close: + socket?.cancel() + case .abort(let request): + guard sendAbort(for: request) else { + socket?.cancel() + return + } + closeUnlessEnded(request) + case .endOutput(let request): + closeUnlessEnded(request) + } + } + + private func closeUnlessEnded(_ request: UInt32) { + stopDeadlineQueue.asyncAfter(deadline: .now() + stopGrace) { [weak self] in + guard let self else { return } + // Shut down under the lock, so a request that ended meanwhile cannot hand the transport to a new one. + socketLock.withLock { + guard _stoppingRequest == request else { return } + _socket?.cancel() + } + } + } + + // A long start still going out holds the lock, and the caller may be the main thread. + private func sendAbort(for request: UInt32) -> Bool { + guard wireLock.lock(before: Date(timeIntervalSinceNow: Self.abortLockWait)) else { return false } + defer { wireLock.unlock() } + // Past the first reply the request's own loop ends the output; a request already answered needs nothing. + guard socketLock.withLock({ _phase == .awaitingResponse(request) }) else { return true } + do { + try sendEncryptedLocked(kind: .abort, body: TeradataMessages.abortParcel().encoded(), requestNumber: request) + return true + } catch { + return false + } } static func responseIsComplete(_ parcels: [Parcel]) -> Bool { @@ -252,6 +422,12 @@ public final class TeradataConnection { } private func sendEncrypted(kind: MessageKind, body: [UInt8], requestNumber: UInt32) throws { + try wireLock.withLock { + try sendEncryptedLocked(kind: kind, body: body, requestNumber: requestNumber) + } + } + + private func sendEncryptedLocked(kind: MessageKind, body: [UInt8], requestNumber: UInt32) throws { let message = LanMessage( kind: kind, body: body, sessionNumber: sessionNumber, requestNumber: requestNumber, byteVar: 0, authentication: TeradataMessages.authBytes(nextMessage()), diff --git a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataMessages.swift b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataMessages.swift index 042633063c..d0ec9d359e 100644 --- a/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataMessages.swift +++ b/apple/Packages/TableProCore/Sources/TableProTeradataCore/TeradataMessages.swift @@ -119,6 +119,14 @@ enum TeradataMessages { Parcel(.logoff) } + static func abortParcel() -> Parcel { + Parcel(.abort) + } + + static func cancelParcel() -> Parcel { + Parcel(.cancel) + } + static func charsetCode(fromConfigResponse body: [UInt8], name: String) -> UInt8? { let needle = Array(name.utf8) guard !needle.isEmpty else { return nil } diff --git a/apple/Packages/TableProCore/Tests/TableProCassandraCoreTests/CassandraConditionalWriteTests.swift b/apple/Packages/TableProCore/Tests/TableProCassandraCoreTests/CassandraConditionalWriteTests.swift new file mode 100644 index 0000000000..b02a53181e --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProCassandraCoreTests/CassandraConditionalWriteTests.swift @@ -0,0 +1,61 @@ +// +// CassandraConditionalWriteTests.swift +// TableProCassandraCoreTests +// + +import TableProCassandraCore +import Testing + +struct CassandraConditionalWriteTests { + private static let update = CassandraConditionalWrite.updateStatement( + target: "\"shop\".\"orders\"", assignments: ["\"note\" = ?"], keyCondition: "\"id\" = ?" + ) + private static let insert = CassandraConditionalWrite.insertStatement( + target: "\"shop\".\"orders\"", names: ["\"id\"", "\"note\""], values: ["?", "?"] + ) + + // Without the condition CQL writes the cell anyway and recreates the row with every other column null. + @Test("A grid edit only writes a row that still exists") + func updateIsConditionedOnTheRow() { + #expect(Self.update == "UPDATE \"shop\".\"orders\" SET \"note\" = ? WHERE \"id\" = ? IF EXISTS") + #expect(Self.insert == "INSERT INTO \"shop\".\"orders\" (\"id\", \"note\") VALUES (?, ?) IF NOT EXISTS") + } + + @Test("An edit of a row deleted since it was read is refused, not reported as saved") + func updateOfAMissingRowIsRefused() { + #expect(CassandraConditionalWrite.refusal( + statement: Self.update, firstColumn: "[applied]", firstValue: "false" + ) == .rowMissing) + } + + @Test("A new row whose key is taken is refused") + func insertOfATakenKeyIsRefused() { + #expect(CassandraConditionalWrite.refusal( + statement: Self.insert, firstColumn: "[applied]", firstValue: "false" + ) == .keyTaken) + } + + @Test("A write the condition let through is no refusal") + func appliedWriteIsNotRefused() { + #expect(CassandraConditionalWrite.refusal( + statement: Self.update, firstColumn: "[applied]", firstValue: "true" + ) == nil) + #expect(CassandraConditionalWrite.refusal( + statement: Self.insert, firstColumn: "[applied]", firstValue: "true" + ) == nil) + } + + // A condition on a column answers with the row's current values, which the console shows as the server sent them. + @Test("An unapplied write with a condition of its own is left to the result") + func otherConditionsAreLeftToTheResult() { + #expect(CassandraConditionalWrite.refusal( + statement: "UPDATE t SET a = 1 WHERE k = 1 IF a = 0", firstColumn: "[applied]", firstValue: "false" + ) == nil) + #expect(CassandraConditionalWrite.refusal( + statement: "DELETE FROM t WHERE k = 1 IF EXISTS", firstColumn: "[applied]", firstValue: "false" + ) == nil) + #expect(CassandraConditionalWrite.refusal( + statement: "UPDATE t SET a = 1 WHERE k = 1", firstColumn: "a", firstValue: "false" + ) == nil) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginParameterEscapingTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginParameterEscapingTests.swift index 4f2d558573..1f17a3ac4b 100644 --- a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginParameterEscapingTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginParameterEscapingTests.swift @@ -107,6 +107,17 @@ struct IsNumericLiteralTests { #expect(!StubDriver.isNumericLiteral("12abc")) #expect(!StubDriver.isNumericLiteral("1.2.3")) } + + // Written bare, a zip or a product code is compared as a number and matches `123` and `0123` too. + @Test("Numeric-shaped text with padding zeros is bound as a string", arguments: ["00123", "-007", "0042", "00.5"]) + func paddedNumbersAreQuoted(value: String) { + #expect(StubDriver().escapedParameterValue(value).unicodeScalars.first == "'") + } + + @Test("A number that opens with a single zero is bound bare", arguments: ["0", "0.5", "-0.5", "0e3", "+0", "100"]) + func singleLeadingZeroStaysBare(value: String) { + #expect(StubDriver().escapedParameterValue(value) == value) + } } // MARK: - escapedParameterValue diff --git a/apple/Packages/TableProCore/Tests/TableProRedisCoreTests/RedisCommandSplitterTests.swift b/apple/Packages/TableProCore/Tests/TableProRedisCoreTests/RedisCommandSplitterTests.swift index 515d4828cd..5dee906b01 100644 --- a/apple/Packages/TableProCore/Tests/TableProRedisCoreTests/RedisCommandSplitterTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProRedisCoreTests/RedisCommandSplitterTests.swift @@ -103,6 +103,18 @@ struct RedisCommandSplitterTests { } } + @Test("Each command's caret reach gives every caret the command command(at:) gives it", arguments: [ + "GET a; GET b\n\nGET c \n", "GET a\n", "GET a; ", "SET k \"a\nb\"\nGET k", " GET a ;", "SET 😀 1\r\nGET 😀", "", + ]) + func caretReachMatchesCommandAt(script: String) { + let reached = RedisCommandSplitter.commandsWithCaretReach(in: script) + #expect(reached.map { $0.command } == RedisCommandSplitter.commands(in: script)) + for caret in 0...((script as NSString).length + 1) { + let viaReach = reached.first { caret <= $0.reach }?.command + #expect(viaReach == RedisCommandSplitter.command(at: caret, in: script), "caret \(caret)") + } + } + @Test("A caret after the last command, or in text with none, has no command") func caretPastEveryCommand() { #expect(command(at: 6, in: "GET a\n") == nil) diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLParenthesizedSemicolonTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLParenthesizedSemicolonTests.swift new file mode 100644 index 0000000000..2f9cf1257c --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLParenthesizedSemicolonTests.swift @@ -0,0 +1,77 @@ +import Foundation +import TableProSQLGrammar +import Testing + +struct SQLParenthesizedSemicolonTests { + private static func grammar(_ typeId: String) -> SQLLexicalGrammar { + SQLLexicalReadings.resolve(databaseTypeId: typeId, declared: nil, session: nil).execution + } + + private func sent(_ sql: String, _ grammar: SQLLexicalGrammar) -> [String] { + SQLStatementScanner.executableStatements(in: sql, grammar: grammar).map(\.sql) + } + + @Test("A PostgreSQL rule's multi-command action list stays one statement", arguments: [ + "CREATE RULE r AS ON INSERT TO t DO ALSO (INSERT INTO a VALUES (1); INSERT INTO b VALUES (2))", + "CREATE OR REPLACE RULE r AS ON UPDATE TO t WHERE (NEW.a > 0) DO INSTEAD (UPDATE a SET n = 1; NOTIFY t)", + "CREATE RULE r AS ON DELETE TO t DO (DELETE FROM a; DELETE FROM b)", + ]) + func ruleActionStaysWhole(rule: String) { + let grammar = Self.grammar("PostgreSQL") + let sql = rule + ";\nSELECT 9;" + + #expect(sent(sql, grammar) == [rule, "SELECT 9"]) + #expect(SQLStatementScanner.navigableStatements(in: sql, grammar: grammar).count == 2) + #expect(!SQLStatementScanner.endsInsideOpenBlock(sql, grammar: grammar)) + } + + @Test("A rule whose action is one command ends at its ;") + func ruleWithoutAnActionList() { + let grammar = Self.grammar("PostgreSQL") + + #expect(sent("CREATE RULE r AS ON INSERT TO t DO ALSO INSERT INTO log VALUES (NEW.id);\nSELECT 2;", grammar).count == 2) + #expect(sent("CREATE RULE r AS ON INSERT TO t DO INSTEAD NOTHING;\nSELECT 2;", grammar).count == 2) + } + + @Test("A stray ( ends at its ; and leaves every statement after it on its own", arguments: [ + "PostgreSQL", "MySQL", "SQLite", "SQL Server", "DuckDB", "ClickHouse", + ]) + func strayParenthesisEndsAtItsSemicolon(typeId: String) { + let grammar = Self.grammar(typeId) + let sql = "SELECT 1;\nSELECT count(id FROM a;\nSELECT 2;\nSELECT 3;" + let caret = (sql as NSString).range(of: "SELECT 3").location + + #expect(sent(sql, grammar) == ["SELECT 1", "SELECT count(id FROM a", "SELECT 2", "SELECT 3"]) + #expect(SQLStatementScanner.statementAtCursor(in: sql, cursorPosition: caret, grammar: grammar) == "SELECT 3") + #expect(!SQLStatementScanner.endsInsideOpenBlock(sql, grammar: grammar)) + } + + @Test("A rule's action list left open holds the rest of the text, which the gate reads as more than one statement") + func openActionListLeavesTheTextInsideABlock() { + let grammar = Self.grammar("PostgreSQL") + let sql = "CREATE RULE r AS ON INSERT TO t DO ALSO (INSERT INTO a VALUES (1);\nDROP TABLE users;" + + #expect(sent(sql, grammar).count == 1) + #expect(SQLStatementScanner.endsInsideOpenBlock(sql, grammar: grammar)) + } + + @Test("The gate tiers each command a rule's action list holds") + func gateTiersThePieces() { + let readings = SQLLexicalReadings.resolve(databaseTypeId: "PostgreSQL", declared: nil, session: nil) + + #expect(SQLStatementClassifier.deletesEveryRow( + "CREATE RULE r AS ON INSERT TO t DO ALSO (SELECT 1; DELETE FROM audit)", + databaseTypeId: "PostgreSQL", + readings: readings + )) + #expect(SQLStatementClassifier.classify( + "SELECT (1; DROP TABLE users; SELECT 1)", databaseTypeId: "PostgreSQL", readings: readings + ).tier == .destructive) + } + + @Test("Inside a routine body a parenthesis one statement leaves open does not hide the body's END") + func bodyStatementParenthesisDoesNotHideTheEnd() { + let sql = "CREATE PROCEDURE p()\nBEGIN\n SELECT (1;\n SELECT 2;\nEND;\nSELECT 3;" + #expect(sent(sql, Self.grammar("MySQL")).last == "SELECT 3") + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLStatementCursorIndexTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLStatementCursorIndexTests.swift index c63a3d7bbe..79bcaf44ed 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLStatementCursorIndexTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLStatementCursorIndexTests.swift @@ -20,6 +20,7 @@ private let cursorIndexFixtures = [ CREATE FUNCTION f() RETURNS int AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql; /* c; */ SELECT E'x' 'y';; + CREATE RULE r AS ON INSERT TO t DO ALSO (INSERT INTO a VALUES (1); INSERT INTO b VALUES (2)); SELECT 2 """), CursorIndexFixture(typeId: "MySQL", text: """ @@ -44,6 +45,15 @@ private let cursorIndexFixtures = [ GO SELECT [a;b] GO + IF @x = 1 + BEGIN + SELECT 1; + SELECT CASE WHEN @x = 1 THEN 2 END; + END + ELSE BEGIN TRY SELECT 3; END TRY BEGIN CATCH SELECT 4; END CATCH; + SELECT (5; 6); + SET XACT_ABORT ON + BEGIN TRY SELECT 7; END TRY BEGIN CATCH THROW; END CATCH """), CursorIndexFixture(typeId: "Oracle", text: """ BEGIN @@ -70,7 +80,8 @@ private let cursorIndexFixtures = [ ] private let cursorIndexInsertions = [ - ";", "'", "\nGO\n", "\n/\n", " ", "x", "$$", "BEGIN ", "END;", "--", "/*", "*/", "\n", "GO", "/", + ";", "'", "\nGO\n", "\n/\n", " ", "x", "$$", "BEGIN ", "END;", "--", "/*", "*/", "\n", "GO", "/", "(", ")", + "CASE ", "END ", ] private let longScriptRow = "SELECT id, name FROM users WHERE active = true;\n" diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/TSQLControlFlowBlockTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/TSQLControlFlowBlockTests.swift new file mode 100644 index 0000000000..271bc67937 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/TSQLControlFlowBlockTests.swift @@ -0,0 +1,153 @@ +import Foundation +import TableProSQLGrammar +import Testing + +// T-SQL reserves BEGIN, so a BEGIN ... END block holds its `;`s wherever it stands, not only in a routine's body. +struct TSQLControlFlowBlockTests { + private static let sqlServer = SQLLexicalReadings.resolve(databaseTypeId: "SQL Server", declared: nil, session: nil) + .execution + + private static let merge = "MERGE dbo.t AS t USING dbo.s AS s ON t.id = s.id WHEN MATCHED THEN DELETE" + + private func sent(_ sql: String) -> [String] { + SQLStatementScanner.executableStatements(in: sql, grammar: Self.sqlServer).map(\.sql) + } + + private func atCursor(_ sql: String, on marker: String) -> String { + let offset = (sql as NSString).range(of: marker).location + return SQLStatementScanner.statementAtCursor(in: sql, cursorPosition: offset, grammar: Self.sqlServer) + } + + @Test("A block an IF, a WHILE or a statement of its own opens stays one statement", arguments: [ + ("IF 1=1 BEGIN SELECT 1; SELECT 2; END; SELECT 3", ["IF 1=1 BEGIN SELECT 1; SELECT 2; END", "SELECT 3"]), + ( + "WHILE @i < 3\nBEGIN\n SET @i += 1;\n PRINT @i;\nEND;\nSELECT @i;", + ["WHILE @i < 3\nBEGIN\n SET @i += 1;\n PRINT @i;\nEND", "SELECT @i"] + ), + ("BEGIN\n SELECT 1;\n SELECT 2;\nEND;\nSELECT 3;", ["BEGIN\n SELECT 1;\n SELECT 2;\nEND", "SELECT 3"]), + ( + "IF @a = 1 BEGIN IF @b = 1 BEGIN SELECT 1; END ELSE BEGIN SELECT 2; END; SELECT 3; END; SELECT 4", + ["IF @a = 1 BEGIN IF @b = 1 BEGIN SELECT 1; END ELSE BEGIN SELECT 2; END; SELECT 3; END", "SELECT 4"] + ), + ( + "BEGIN TRY\n DELETE FROM a;\nEND TRY\nBEGIN CATCH\n SELECT ERROR_MESSAGE();\nEND CATCH;\nSELECT 9;", + ["BEGIN TRY\n DELETE FROM a;\nEND TRY\nBEGIN CATCH\n SELECT ERROR_MESSAGE();\nEND CATCH", "SELECT 9"] + ), + ]) + func blockHoldsItsSemicolons(sql: String, expected: [String]) { + #expect(sent(sql) == expected) + } + + @Test("Run Current anywhere in a conditional block runs the whole block") + func caretInsideTheBlockRunsTheBlock() { + let block = "IF @env = 'dev'\nBEGIN\n DELETE FROM a;\n DELETE FROM b;\nEND" + let sql = "SELECT 0;\n\(block);\nSELECT 9;" + + #expect(atCursor(sql, on: "IF @env") == block) + #expect(atCursor(sql, on: "DELETE FROM b") == block) + #expect(SQLStatementScanner.navigableStatements(in: sql, grammar: Self.sqlServer).count == 3) + } + + @Test("An END is never followed by more of its block, so the IF after it is the next statement") + func endBeforeTheNextStatementClosesTheBlock() { + let sql = "IF @a = 1 BEGIN SELECT 1; END\nIF @b = 1 BEGIN SELECT 2; END;\nSELECT 3;" + + #expect(sent(sql) == ["IF @a = 1 BEGIN SELECT 1; END\nIF @b = 1 BEGIN SELECT 2; END", "SELECT 3"]) + #expect(!SQLStatementScanner.endsInsideOpenBlock(sql, grammar: Self.sqlServer)) + } + + @Test("A transaction's BEGIN is a statement, not a block", arguments: ["TRAN", "TRANSACTION", "DISTRIBUTED TRANSACTION"]) + func transactionBeginIsAStatement(opener: String) { + #expect(sent("BEGIN \(opener);\nUPDATE t SET a = 1;\nCOMMIT;") == ["BEGIN \(opener)", "UPDATE t SET a = 1", "COMMIT"]) + } + + @Test("Service Broker's BEGIN DIALOG, BEGIN CONVERSATION TIMER and END CONVERSATION are statements") + func serviceBrokerStatementsAreNotBlocks() { + let script = """ + BEGIN DIALOG CONVERSATION @h FROM SERVICE [s] TO SERVICE 't' ON CONTRACT [c]; + BEGIN CONVERSATION TIMER (@h) TIMEOUT = 60; + END CONVERSATION @h; + SELECT 1; + """ + #expect(sent(script).count == 4) + + let inBlock = "IF @x = 1 BEGIN END CONVERSATION @h; SELECT 1; END; SELECT 2" + #expect(sent(inBlock) == ["IF @x = 1 BEGIN END CONVERSATION @h; SELECT 1; END", "SELECT 2"]) + } + + @Test("A CASE expression closes at its own END, and one left open at a ; does not hold the next statement") + func caseExpressions() { + #expect(sent("SELECT CASE WHEN a = 1 THEN 'x' END AS c FROM t;\nSELECT 2;").count == 2) + #expect(sent("IF @x = 1 BEGIN SELECT CASE WHEN @x = 1 THEN 1 END; SELECT 2; END; SELECT 3").count == 2) + #expect(sent("SELECT CASE WHEN a = 1 THEN 1;\nSELECT 2;") == ["SELECT CASE WHEN a = 1 THEN 1", "SELECT 2"]) + } + + @Test("A MERGE inside a block keeps its ; and the block's own ; is a separator") + func mergeInsideABlock() { + let block = "IF 1 = 1 BEGIN \(Self.merge); END" + #expect(sent(block + ";\nSELECT 1;") == [block, "SELECT 1"]) + } + + @Test("A block left open runs to its GO line and no further") + func goLineEndsAnOpenBlock() { + let sql = "IF 1 = 1 BEGIN SELECT 1;\nGO\nSELECT 2" + #expect(sent(sql) == ["IF 1 = 1 BEGIN SELECT 1", "SELECT 2"]) + #expect(SQLStatementScanner.endsInsideOpenBlock("IF 1 = 1 BEGIN SELECT 1;", grammar: Self.sqlServer)) + } + + @Test("The gate still tiers every statement a block holds") + func gateTiersEachStatementInsideTheBlock() { + let readings = SQLLexicalReadings.resolve(databaseTypeId: "SQL Server", declared: nil, session: nil) + let sql = "IF @env = 'dev' BEGIN SELECT 1; DROP TABLE dbo.t; END" + + #expect(SQLStatementClassifier.classify(sql, databaseTypeId: "SQL Server", readings: readings).tier == .destructive) + #expect(SQLStatementClassifier.deletesEveryRow( + "IF 1 = 1 BEGIN DELETE FROM dbo.t; END", databaseTypeId: "SQL Server", readings: readings + )) + } + + @Test("A TRY block after a statement written without a ; is still one block") + func tryBlockAfterAnUnterminatedStatement() { + let script = "SET XACT_ABORT ON\nBEGIN TRY\n DELETE FROM a;\nEND TRY\nBEGIN CATCH\n THROW;\nEND CATCH;\nSELECT 1;" + #expect(sent(script).count == 2) + #expect(sent(script).last == "SELECT 1") + } + + @Test("A begin where a name stands opens no block", arguments: [ + ("CREATE TABLE begin(a int); DROP TABLE users", 2), + ("INSERT INTO begin VALUES (1); DROP TABLE users", 2), + ("DO BEGIN SELECT 1 FROM DUMMY; SELECT 2 FROM DUMMY; END;", 3), + ]) + func nameInBeginsPlaceOpensNothing(sql: String, count: Int) { + #expect(sent(sql).count == count) + } + + @Test("A block holding more than one statement is told apart from one statement", arguments: [ + ("IF x BEGIN UPDATE a SET c = 1; UPDATE b SET c = 1; END", true), + ("BEGIN MERGE dbo.t AS t USING dbo.s AS s ON t.id = s.id WHEN MATCHED THEN DELETE; SELECT 1; END", true), + ("IF x BEGIN SELECT 1; WITH c AS (SELECT id FROM t) DELETE FROM c; END", true), + ("IF x BEGIN SELECT 1; DISABLE TRIGGER trg ON t; END", true), + ("BEGIN TRY SELECT 1; THROW 50000, 'x', 1; END TRY BEGIN CATCH SELECT 2; END CATCH", true), + ("IF x BEGIN SELECT 1; done: SELECT 2; END", true), + ("IF x BEGIN SELECT 1; (SELECT 2); END", true), + ("IF x BEGIN SELECT 1; END CONVERSATION @h; END", true), + ("IF x SELECT 1", false), + ("IF x BEGIN SELECT 1; END", false), + ("IF x BEGIN SELECT 1;; END", false), + ("BEGIN IF x SELECT 1; ELSE SELECT 2; END", false), + ("BEGIN TRY SELECT 1; END TRY BEGIN CATCH THROW; END CATCH", false), + ("CREATE PROCEDURE p AS BEGIN SELECT 1; SELECT 2; END", false), + ]) + func blockHoldingSeveralStatements(sql: String, holdsSeveral: Bool) { + #expect(SQLUnterminatedStatements.blockHoldsSeveralStatements(sql, grammar: Self.sqlServer) == holdsSeveral) + let postgreSQL = SQLLexicalReadings.resolve(databaseTypeId: "PostgreSQL", declared: nil, session: nil).execution + #expect(!SQLUnterminatedStatements.blockHoldsSeveralStatements(sql, grammar: postgreSQL)) + } + + @Test("Other engines still read BEGIN outside a routine as a word", arguments: ["PostgreSQL", "MySQL", "SQLite"]) + func otherEnginesAreUnchanged(typeId: String) { + let grammar = SQLLexicalReadings.resolve(databaseTypeId: typeId, declared: nil, session: nil).execution + let statements = SQLStatementScanner.executableStatements(in: "BEGIN\nDELETE FROM t;\nSELECT 1;", grammar: grammar) + #expect(statements.map { $0.sql } == ["BEGIN\nDELETE FROM t", "SELECT 1"]) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataStopTests.swift b/apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataStopTests.swift new file mode 100644 index 0000000000..4f19e1b292 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProTeradataCoreTests/TeradataStopTests.swift @@ -0,0 +1,249 @@ +import Foundation +@testable import TableProTeradataCore +import Testing + +// Replies are read in order; a hook keyed by a reply's index runs just before that reply is read, which is where a +// request is waiting on the server when Stop is pressed. +private final class StoppableTransport: TeradataTransport { + private var replies: [[UInt8]] + private var stream: [UInt8] = [] + private var repliesRead = 0 + var beforeReply: [Int: () -> Void] = [:] + private(set) var sent: [LanMessage] = [] + private(set) var shutDown = false + private(set) var closed = false + + var sentKinds: [UInt8] { + sent.map { $0.kind } + } + + init(replies: [[UInt8]]) { + self.replies = replies + } + + func send(_ bytes: [UInt8]) throws { + if shutDown || closed { throw TeradataWireError.cancelled } + sent.append(LanMessage.decode( + header: Array(bytes.prefix(LanMessage.headerLength)), + body: Array(bytes.dropFirst(LanMessage.headerLength)) + )) + } + + func receive(_ count: Int) throws -> [UInt8] { + if stream.isEmpty { + beforeReply.removeValue(forKey: repliesRead)?() + if shutDown || closed { throw TeradataWireError.cancelled } + guard !replies.isEmpty else { throw TeradataWireError.truncated("no reply") } + stream = replies.removeFirst() + repliesRead += 1 + } + guard stream.count >= count else { throw TeradataWireError.truncated("closed") } + defer { stream.removeFirst(count) } + return Array(stream.prefix(count)) + } + + func finishConnecting() {} + + func cancel() { + shutDown = true + } + + func close() { + closed = true + } +} + +// A server that never answers: a receive waits until the transport is shut down, which the stop's deadline does from +// its own queue. +private final class SilentTransport: TeradataTransport, @unchecked Sendable { + private let lock = NSLock() + private let shutDown = DispatchSemaphore(value: 0) + private var sent: [UInt8] = [] + private var shut = false + private var received = false + var onFirstReceive: (() -> Void)? + + var sentKinds: [UInt8] { lock.withLock { sent } } + var wasShutDown: Bool { lock.withLock { shut } } + + func send(_ bytes: [UInt8]) throws { + try lock.withLock { + if shut { throw TeradataWireError.cancelled } + sent.append(bytes[2]) + } + } + + func receive(_ count: Int) throws -> [UInt8] { + let first = lock.withLock { () -> Bool in + defer { received = true } + return !received + } + if first { onFirstReceive?() } + _ = shutDown.wait(timeout: .now() + 10) + throw TeradataWireError.cancelled + } + + func finishConnecting() {} + + func cancel() { + lock.withLock { shut = true } + shutDown.signal() + } + + func close() { + cancel() + } +} + +struct TeradataStopTests { + private static let config = TeradataConnectionConfig(host: "td.example", username: "dbc", password: "dbc") + private static let start = MessageKind.start.rawValue + private static let cont = MessageKind.cont.rawValue + private static let abort = MessageKind.abort.rawValue + + private static func reply(_ parcels: [Parcel]) -> [UInt8] { + LanMessage(kind: .start, body: parcels.flatMap { $0.encoded() }).encoded() + } + + private static func failure(code: Int, _ message: String) -> Parcel { + let text = Array(message.utf8) + let body = [UInt8](repeating: 0, count: 8) + + [UInt8(code >> 8), UInt8(code & 0xFF), UInt8(text.count >> 8), UInt8(text.count & 0xFF)] + text + return Parcel(.failure, body: body) + } + + private static let finished = reply([Parcel(.success), Parcel(.endRequest)]) + + private static func thrown(_ body: () throws -> Void) -> Error? { + do { + try body() + return nil + } catch { + return error + } + } + + private static func isCancelled(_ error: Error?) -> Bool { + guard let wire = error as? TeradataWireError, case .cancelled = wire else { return false } + return true + } + + @Test("Stop while the server works on a request aborts it on the session, which stays logged on") + func stopAbortsTheRequestAndKeepsTheSession() throws { + let aborted = Self.reply([Self.failure(code: 3_110, "The transaction was aborted by the user.")]) + let transport = StoppableTransport(replies: [aborted, Self.finished]) + let connection = TeradataConnection(config: Self.config, transport: transport) + transport.beforeReply[0] = { connection.cancel() } + + let error = Self.thrown { _ = try connection.execute("SELECT * FROM sales.orders CROSS JOIN sales.items") } + + #expect(Self.isCancelled(error)) + #expect(transport.sentKinds == [Self.start, Self.abort]) + #expect(transport.sent.last?.requestNumber == transport.sent.first?.requestNumber) + #expect(transport.sent.last?.body == TeradataMessages.abortParcel().encoded()) + #expect(!transport.shutDown) + #expect(!transport.closed) + #expect(connection.isConnected) + #expect(!connection.hasLostConnection) + + _ = try connection.execute("SELECT 1") + #expect(transport.sentKinds == [Self.start, Self.abort, Self.start]) + } + + @Test("A request that failed on its own before the abort landed keeps its own error") + func failureOtherThanTheAbortIsReported() throws { + let missing = Self.reply([Self.failure(code: 3_807, "Object 'sales.orderz' does not exist.")]) + let transport = StoppableTransport(replies: [missing]) + let connection = TeradataConnection(config: Self.config, transport: transport) + transport.beforeReply[0] = { connection.cancel() } + + let error = Self.thrown { _ = try connection.execute("SELECT * FROM sales.orderz") } + + guard let wire = error as? TeradataWireError, case .server(let code, _) = wire else { + Issue.record("expected the server's error, got \(String(describing: error))") + return + } + #expect(code == 3_807) + #expect(connection.isConnected) + } + + // An export Stop lands here: the abort does not take effect once the server has started answering. + @Test("Stop while a long answer is being fetched ends the output and keeps the session") + func stopDuringFetchEndsTheOutput() throws { + let batch = Self.reply([Parcel(.success)]) + let ended = Self.reply([Parcel(.endRequest)]) + let transport = StoppableTransport(replies: [batch, batch, ended, Self.finished]) + let connection = TeradataConnection(config: Self.config, transport: transport) + transport.beforeReply[1] = { connection.cancel() } + + let error = Self.thrown { _ = try connection.execute("SELECT * FROM sales.orders") } + + #expect(Self.isCancelled(error)) + #expect(transport.sentKinds == [Self.start, Self.cont, Self.cont]) + #expect(transport.sent.last?.body == TeradataMessages.cancelParcel().encoded()) + #expect(!transport.shutDown) + #expect(!transport.closed) + #expect(!connection.hasLostConnection) + + _ = try connection.execute("SELECT 1") + #expect(transport.sentKinds == [Self.start, Self.cont, Self.cont, Self.start]) + } + + @Test("An output the server keeps sending after the Cancel parcel gives the session up") + func unendedOutputGivesTheSessionUp() throws { + let batch = Self.reply([Parcel(.success)]) + let transport = StoppableTransport(replies: [batch, batch, batch]) + let connection = TeradataConnection(config: Self.config, transport: transport) + transport.beforeReply[1] = { connection.cancel() } + + #expect(Self.thrown { _ = try connection.execute("SELECT * FROM sales.orders") } != nil) + #expect(transport.closed) + #expect(connection.hasLostConnection) + } + + @Test("Stop in a running call before its request goes out refuses the request and keeps the session") + func stopBeforeTheRequestIsSentRefusesIt() throws { + let transport = StoppableTransport(replies: [Self.finished]) + let connection = TeradataConnection(config: Self.config, transport: transport) + + connection.beginOperation() + connection.cancel() + let error = Self.thrown { _ = try connection.execute("DELETE FROM sales.orders") } + connection.endOperation() + + #expect(Self.isCancelled(error)) + #expect(transport.sent.isEmpty) + #expect(connection.isConnected) + + connection.beginOperation() + _ = try connection.execute("SELECT 1") + connection.endOperation() + #expect(transport.sentKinds == [Self.start]) + } + + @Test("A stopped request the server never answers gives the session up after the grace", .timeLimit(.minutes(1))) + func unansweredStopGivesTheSessionUp() throws { + let transport = SilentTransport() + let connection = TeradataConnection(config: Self.config, transport: transport, stopGrace: 0.05) + transport.onFirstReceive = { connection.cancel() } + + let error = Self.thrown { _ = try connection.execute("SELECT * FROM sales.orders CROSS JOIN sales.items") } + + #expect(Self.isCancelled(error)) + #expect(transport.sentKinds == [Self.start, Self.abort]) + #expect(transport.wasShutDown) + #expect(connection.hasLostConnection) + } + + @Test("Stop with no request running sends nothing and keeps the session") + func stopWhileIdleDoesNothing() throws { + let transport = StoppableTransport(replies: [Self.finished]) + let connection = TeradataConnection(config: Self.config, transport: transport) + + connection.cancel() + _ = try connection.execute("SELECT 1") + + #expect(transport.sentKinds == [Self.start]) + #expect(!transport.shutDown) + } +} diff --git a/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+EmphasizeBracket.swift b/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+EmphasizeBracket.swift index 0d390a3be1..04429cdc55 100644 --- a/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+EmphasizeBracket.swift +++ b/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+EmphasizeBracket.swift @@ -37,7 +37,7 @@ extension TextViewController { pair.0, pair.1, from: range.location, - limit: min((textView.visibleTextRange ?? .zero).max + 4_096, textView.documentRange.max), + limit: min(max(range.location, (textView.visibleTextRange ?? .zero).max) + 4_096, textView.documentRange.max), reverse: false ) { emphasizeCharacter(characterIndex) @@ -52,7 +52,10 @@ extension TextViewController { pair.1, pair.0, from: range.location - 1, - limit: max((textView.visibleTextRange?.location ?? 0) - 4_096, textView.documentRange.location), + limit: max( + min(range.location - 1, textView.visibleTextRange?.location ?? 0) - 4_096, + textView.documentRange.location + ), reverse: true ) { emphasizeCharacter(characterIndex) @@ -91,12 +94,16 @@ extension TextViewController { if reverse { options = options.union(.reverse) } + // A caret outside the visible range can put the limit on the wrong side of `from`, and NSString raises + // an exception nothing can catch for a range past the text. + let length = textView.textStorage.length + let lower = max(0, min(reverse ? limit : from, length)) + let upper = max(0, min(reverse ? from : limit, length)) + guard upper > lower else { return nil } var closeCount = 0 var index: Int? textView.textStorage.mutableString.enumerateSubstrings( - in: reverse ? - NSRange(location: limit, length: from - limit) : - NSRange(location: from, length: limit - from), + in: NSRange(location: lower, length: upper - lower), options: options, using: { substring, range, _, stop in if substring == close { diff --git a/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/TextViewControllerTests.swift b/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/TextViewControllerTests.swift index ff714958bf..8542ff6869 100644 --- a/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/TextViewControllerTests.swift +++ b/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/TextViewControllerTests.swift @@ -330,6 +330,24 @@ final class TextViewControllerTests: XCTestCase { ) } + func test_findClosingPairWithALimitOnTheWrongSideOrPastTheText() { + controller.textView.string = "{ Lorem }" + + XCTAssertNil(controller.findClosingPair("{", "}", from: 5, limit: 2, reverse: false)) + XCTAssertNil(controller.findClosingPair("}", "{", from: 2, limit: 5, reverse: true)) + XCTAssertEqual(controller.findClosingPair("{", "}", from: 1, limit: 10_000, reverse: false), 8) + } + + func test_bracketEmphasisFarPastTheVisibleTextFindsThePair() { + controller.configuration.appearance.bracketPairEmphasis = .flash + controller.setText(String(repeating: "x", count: 60_000) + "{ a }") + + controller.setCursorPositions([CursorPosition(range: NSRange(location: 60_001, length: 0))]) + + XCTAssertEqual(controller.cursorPositions.first?.range.location, 60_001) + XCTAssertEqual(controller.findClosingPair("{", "}", from: 60_001, limit: 60_005, reverse: false), 60_004) + } + // MARK: Set Text func test_setText() { diff --git a/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift b/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift index 4d776c917f..661f20f618 100644 --- a/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift +++ b/apple/Plugins/BeancountDriverPlugin/BeancountPluginDriver.swift @@ -460,11 +460,13 @@ final class BeancountPluginDriver: PluginDatabaseDriver, @unchecked Sendable { } return try executeSQLite(query: query, parameters: []) } - continuation.yield(.header(PluginStreamHeader( + var header = PluginStreamHeader( columns: result.columns, columnTypeNames: result.columnTypeNames, estimatedRowCount: result.rows.count - ))) + ) + if result.isTruncated { header.rowLimit = result.rows.count } + continuation.yield(.header(header)) continuation.yield(.rows(result.rows)) continuation.finish() } catch { diff --git a/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift b/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift index 1b3c890c81..e362dfd30d 100644 --- a/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift +++ b/apple/Plugins/BigtableDriverPlugin/BigtablePluginDriver+Execution.swift @@ -156,9 +156,11 @@ extension BigtablePluginDriver { try await self.export(spec, continuation: continuation) } else { let result = try await self.execute(query: query) - continuation.yield(.header(PluginStreamHeader( + var header = PluginStreamHeader( columns: result.columns, columnTypeNames: result.columnTypeNames, estimatedRowCount: nil - ))) + ) + if result.isTruncated { header.rowLimit = result.rows.count } + continuation.yield(.header(header)) if !result.rows.isEmpty { continuation.yield(.rows(result.rows)) } } continuation.finish() diff --git a/apple/Plugins/CSVExportPlugin/CSVExportModels.swift b/apple/Plugins/CSVExportPlugin/CSVExportModels.swift index 2fdbd570a2..cd7942fb4b 100644 --- a/apple/Plugins/CSVExportPlugin/CSVExportModels.swift +++ b/apple/Plugins/CSVExportPlugin/CSVExportModels.swift @@ -79,7 +79,8 @@ public struct CSVExportOptions: Equatable, Codable, Sendable { public var quoteHandling: CSVQuoteHandling = .asNeeded public var lineBreak: CSVLineBreak = .lf public var decimalFormat: CSVDecimalFormat = .period - public var sanitizeFormulas: Bool = true + // Off: RFC 4180 carries a field as written, and OWASP scopes the quote prefix to files meant for a spreadsheet. + public var sanitizeFormulas: Bool = false /// What the file's bytes are written in. UTF-8 keeps every existing export byte for byte. public var encoding: PluginTextEncoding = .utf8 diff --git a/apple/Plugins/CSVExportPlugin/CSVExportOptionsView.swift b/apple/Plugins/CSVExportPlugin/CSVExportOptionsView.swift index a35bd63f80..f38791eced 100644 --- a/apple/Plugins/CSVExportPlugin/CSVExportOptionsView.swift +++ b/apple/Plugins/CSVExportPlugin/CSVExportOptionsView.swift @@ -23,7 +23,11 @@ struct CSVExportOptionsView: View { Toggle("Sanitize formula-like values", isOn: $plugin.settings.sanitizeFormulas) .toggleStyle(.checkbox) - .help("Prevent CSV formula injection by prefixing values starting with =, +, -, @ with a single quote") + .help(""" + For a file opened in a spreadsheet. Values that would run as formulas, such as those starting \ + with =, +, - or @, get a leading single quote, which stays in the value for anything that \ + imports the file. + """) } Divider() diff --git a/apple/Plugins/CSVExportPlugin/CSVExportPlugin.swift b/apple/Plugins/CSVExportPlugin/CSVExportPlugin.swift index 6f11d384b2..f4f2bf17b3 100644 --- a/apple/Plugins/CSVExportPlugin/CSVExportPlugin.swift +++ b/apple/Plugins/CSVExportPlugin/CSVExportPlugin.swift @@ -59,6 +59,7 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi let lineBreak = options.lineBreak.value let encoding = options.encoding var report = CSVEncodingReport() + var neutralisedCount = 0 if options.writesByteOrderMark, !encoding.byteOrderMark.isEmpty { try fileHandle.write(contentsOf: Data(encoding.byteOrderMark)) @@ -80,7 +81,7 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi } var isFirstBatch = true - var columns: [String] = [] + var columnNumbers: [PluginColumnNumbers] = [] let stream = dataSource.streamRows(for: table) for try await element in stream { @@ -88,17 +89,27 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi switch element { case .header(let header): - columns = header.columns + columnNumbers = header.columnTypeNames.map { PluginColumnNumbers(typeName: $0) } if isFirstBatch && options.includeFieldNames { - let headerLine = columns - .map { escapeCSVField($0, options: options) } + if options.sanitizeFormulas { + neutralisedCount += header.columns.filter(PluginRowWriters.isFormulaLike).count + } + let headerLine = header.columns + .map { escapeCSVField($0, options: options, sanitizes: options.sanitizeFormulas) } .joined(separator: options.delimiter.actualValue) try write(headerLine + lineBreak, to: fileHandle, encoding: encoding, report: &report) } isFirstBatch = false case .rows(let rows): for row in rows { - try writeCSVRow(row, options: options, to: fileHandle, report: &report) + try writeCSVRow( + row, + columnNumbers: columnNumbers, + options: options, + to: fileHandle, + report: &report, + neutralisedCount: &neutralisedCount + ) progress.incrementRow() } } @@ -114,7 +125,22 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi try PluginExportUtilities.commitAtomicWrite(from: tempURL, to: destination) committed = true progress.finalizeTable() - return ExportFormatResult(warnings: report.warnings(for: encoding)) + return ExportFormatResult(warnings: report.warnings(for: encoding) + Self.neutralisedWarnings(neutralisedCount)) + } + + // The quote is part of the value for anything that imports the file, so the export says how many it changed. + static func neutralisedWarnings(_ count: Int) -> [String] { + guard count > 0 else { return [] } + return [String( + format: String( + localized: """ + Values a spreadsheet would read as formulas, written with a leading single quote: %lld. \ + Turn off Sanitize formula-like values to export them unchanged. + """, + bundle: .main + ), + Int64(count) + )] } // MARK: - Private @@ -143,26 +169,31 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi try fileHandle.write(contentsOf: encoded.data) } + // A text column holding "1.10" is a version or a code; a column with no usable type keeps the decimal comma. private func writeCSVRow( _ row: [PluginCellValue], + columnNumbers: [PluginColumnNumbers], options: CSVExportOptions, to fileHandle: FileHandle, - report: inout CSVEncodingReport + report: inout CSVEncodingReport, + neutralisedCount: inout Int ) throws { - let delimiter = options.delimiter.actualValue - let lineBreak = options.lineBreak.value + var fields: [String] = [] + fields.reserveCapacity(row.count) - let rowLine = row.map { cell -> String in + for (index, cell) in row.enumerated() { let val: String switch cell { case .null: - return options.convertNullToEmpty ? "" : "NULL" + fields.append(options.convertNullToEmpty ? "" : "NULL") + continue case .text(let s): val = s case .bytes(let d): val = "0x" + d.map { String(format: "%02X", $0) }.joined() } + let takesDecimalComma = index >= columnNumbers.count || columnNumbers[index] != .text var processed = val // `\r\n` is one Character, so only a scalar test sees a Windows line break. let hadLineBreaks = val.unicodeScalars.contains { $0 == "\n" || $0 == "\r" } @@ -174,40 +205,54 @@ final class CSVExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlugi .replacingOccurrences(of: "\n", with: " ", options: .literal) } - if options.decimalFormat == .comma { + var isCommaDecimal = false + if options.decimalFormat == .comma, takesDecimalComma { let range = NSRange(processed.startIndex..., in: processed) if Self.decimalFormatRegex.firstMatch(in: processed, range: range) != nil { processed = processed.replacingOccurrences(of: ".", with: ",") + isCommaDecimal = true } } - return escapeCSVField(processed, options: options, originalHadLineBreaks: hadLineBreaks) - }.joined(separator: delimiter) + // `-1,5` is still a number, and the quote would turn it into text. + let sanitizes = options.sanitizeFormulas && !isCommaDecimal + if sanitizes, PluginRowWriters.isFormulaLike(processed) { + neutralisedCount += 1 + } + fields.append(escapeCSVField( + processed, options: options, sanitizes: sanitizes, originalHadLineBreaks: hadLineBreaks + )) + } - try write(rowLine + lineBreak, to: fileHandle, encoding: options.encoding, report: &report) + try write( + fields.joined(separator: options.delimiter.actualValue) + options.lineBreak.value, + to: fileHandle, + encoding: options.encoding, + report: &report + ) } - /// Escaping and quoting live in `PluginRowWriters`, so this format, the other export formats - /// and the MCP tool spell a value the same way. Only the option mapping is this plugin's own. - /// - /// `originalHadLineBreaks` says the value's breaks were already replaced with spaces upstream, - /// and it still forces quoting: the source text spanned lines, and a reader that splits on the - /// delimiter has no way to know the space it now sees was one. - private func escapeCSVField(_ field: String, options: CSVExportOptions, originalHadLineBreaks: Bool = false) -> String { - let escaped = PluginRowWriters.csvField(field, options: writeOptions(options)) + // `originalHadLineBreaks` still forces quoting after the breaks became spaces: the source text spanned lines. + private func escapeCSVField( + _ field: String, + options: CSVExportOptions, + sanitizes: Bool, + originalHadLineBreaks: Bool = false + ) -> String { + let escaped = PluginRowWriters.csvField(field, options: writeOptions(options, sanitizes: sanitizes)) guard originalHadLineBreaks, options.quoteHandling == .asNeeded, escaped.unicodeScalars.first != "\"" else { return escaped } return "\"\(escaped.replacingOccurrences(of: "\"", with: "\"\"", options: .literal))\"" } - private func writeOptions(_ options: CSVExportOptions) -> PluginCsvWriteOptions { + private func writeOptions(_ options: CSVExportOptions, sanitizes: Bool) -> PluginCsvWriteOptions { PluginCsvWriteOptions( delimiter: options.delimiter.actualValue, quoteHandling: quoteHandling(options.quoteHandling), lineEnding: options.lineBreak.value, nullAsEmpty: true, - sanitizesFormulas: options.sanitizeFormulas, + sanitizesFormulas: sanitizes, flattensLineBreaks: false ) } diff --git a/apple/Plugins/CSVImportPlugin/CSVImportPlugin.swift b/apple/Plugins/CSVImportPlugin/CSVImportPlugin.swift index 0a3b2d9ae3..c9716c5229 100644 --- a/apple/Plugins/CSVImportPlugin/CSVImportPlugin.swift +++ b/apple/Plugins/CSVImportPlugin/CSVImportPlugin.swift @@ -29,13 +29,13 @@ final class CSVImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi var fieldDetectionSignature: String { settings.detectionSignature } - var rowImportConfiguration: RowImportRunner.Configuration? { runConfiguration } + var rowImportConfiguration: RowImportRunner.Configuration? { Self.runConfiguration(settings) } - private var runConfiguration: RowImportRunner.Configuration { + private static func runConfiguration(_ options: CSVImportOptions) -> RowImportRunner.Configuration { RowImportRunner.Configuration( - errorHandling: settings.errorHandling, - wrapInTransaction: settings.wrapInTransaction, - deleteExistingRows: settings.deleteExistingRows + errorHandling: options.errorHandling, + wrapInTransaction: options.wrapInTransaction, + deleteExistingRows: options.deleteExistingRows ) } @@ -58,10 +58,12 @@ final class CSVImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi ) async throws -> PluginImportResult { let startTime = Date() let url = source.fileURL() + // One plugin instance serves every window, so a dialog in another window can change `settings` mid-import. + let options = settings let text: CSVImportText do { - text = try CSVImportText.contents(of: url, encoding: settings.encoding, isCancelled: { progress.isCancelled }) + text = try CSVImportText.contents(of: url, encoding: options.encoding, isCancelled: { progress.isCancelled }) } catch is TabularCancellation { throw PluginImportCancellationError() } catch { @@ -80,9 +82,9 @@ final class CSVImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi } let data = text.data - let dialect = CSVImportParsing.resolveDialect(in: data, options: settings) + let dialect = CSVImportParsing.resolveDialect(in: data, options: options) let parser = CSVStreamingParser(dialect: dialect) - let hasHeader = settings.hasHeaderRow + let hasHeader = options.hasHeaderRow let (dataRanges, columnNames) = indexRowsAndNames(in: data, parser: parser, hasHeader: hasHeader) guard !columnNames.isEmpty else { @@ -93,7 +95,7 @@ final class CSVImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi let lineOffset = hasHeader ? 2 : 1 var cursor = 0 - let configuration = runConfiguration + let configuration = Self.runConfiguration(options) var pendingFailure: PluginImportError? var malformedCount = 0 var malformedErrors: [PluginImportResult.ImportStatementError] = [] @@ -111,7 +113,8 @@ final class CSVImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi ranges: dataRanges[cursor..>, startIndex: Int, lineOffset: Int, - columnNames: [String] + columnNames: [String], + options: CSVImportOptions ) -> (rows: [RowImportRunner.Entry], malformed: [MalformedRow]) { data.withUnsafeBytes { raw -> (rows: [RowImportRunner.Entry], malformed: [MalformedRow]) in guard let base = raw.bindMemory(to: UInt8.self).baseAddress else { return ([], []) } @@ -185,12 +189,12 @@ final class CSVImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi if CSVImportParsing.isBlank(fields) { continue } let line = startIndex + offset + lineOffset if let error = CSVImportParsing.shapeError( - fields: fields, columnCount: columnNames.count, options: settings + fields: fields, columnCount: columnNames.count, options: options ) { malformed.append((line, error)) continue } - out.append((line, CSVImportParsing.row(fields: fields, columnNames: columnNames, options: settings))) + out.append((line, CSVImportParsing.row(fields: fields, columnNames: columnNames, options: options))) } return (out, malformed) } diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift b/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift index f59b0a903d..ff8717faec 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraPlugin.swift @@ -324,7 +324,7 @@ internal final class CassandraPluginDriver: PluginDatabaseDriver, @unchecked Sen let rawResult = try await connectionActor.executeQuery( query, consistency: consistency, cancellation: cancellation ) - if let refusal = CassandraRowWriter.unappliedInsertRefusal( + if let refusal = CassandraRowWriter.unappliedWriteRefusal( statement: query, columns: rawResult.columns, rows: rawResult.rows ) { throw CassandraPluginError.queryFailed(refusal) @@ -371,7 +371,7 @@ internal final class CassandraPluginDriver: PluginDatabaseDriver, @unchecked Sen let rawResult = try await connectionActor.executePrepared( query, parameters: parameters, consistency: consistency ) - if let refusal = CassandraRowWriter.unappliedInsertRefusal( + if let refusal = CassandraRowWriter.unappliedWriteRefusal( statement: query, columns: rawResult.columns, rows: rawResult.rows ) { throw CassandraPluginError.queryFailed(refusal) diff --git a/apple/Plugins/CassandraDriverPlugin/CassandraRowWriter.swift b/apple/Plugins/CassandraDriverPlugin/CassandraRowWriter.swift index 0c7ce00b73..ca4edf9125 100644 --- a/apple/Plugins/CassandraDriverPlugin/CassandraRowWriter.swift +++ b/apple/Plugins/CassandraDriverPlugin/CassandraRowWriter.swift @@ -4,16 +4,15 @@ // import Foundation +import TableProCassandraCore import TableProPluginKit -/// The CQL a grid save writes: one statement per row, each naming the row by its whole primary key. -/// -/// CQL has no `OR` and no parenthesised groups, so several rows cannot share one `DELETE`, and an `INSERT` is an -/// upsert, so a new row whose key is taken would silently replace the row already there. A new row is therefore -/// written `IF NOT EXISTS`, and anything CQL cannot do to a row is refused by name rather than sent. +// One statement per row, each naming the row by its whole primary key: CQL has no `OR` and no parenthesised groups. +// A new row is written `IF NOT EXISTS` and an edit `IF EXISTS` (CassandraConditionalWrite), and anything CQL cannot +// do to a row is refused by name rather than sent. enum CassandraRowWriter { static let defaultSentinel = "__DEFAULT__" - static let unappliedMarker = "[applied]" + static let unappliedMarker = CassandraConditionalWrite.appliedColumn static func rowWrites( keyspace: String?, @@ -54,15 +53,18 @@ enum CassandraRowWriter { } } - /// An `INSERT … IF NOT EXISTS` that found its key taken answers with `[applied] = false` and the row already - /// there, which the server reports as a success. - static func unappliedInsertRefusal(statement: String, columns: [String], rows: [[PluginCellValue]]) -> String? { - let normalized = statement.uppercased() - guard normalized.hasPrefix("INSERT"), normalized.hasSuffix("IF NOT EXISTS"), - columns.first == unappliedMarker, - rows.first?.first?.asText?.lowercased() == "false" - else { return nil } - return String(localized: "A row with this primary key already exists. Edit that row instead.") + static func unappliedWriteRefusal(statement: String, columns: [String], rows: [[PluginCellValue]]) -> String? { + let refusal = CassandraConditionalWrite.refusal( + statement: statement, firstColumn: columns.first, firstValue: rows.first?.first?.asText + ) + switch refusal { + case .keyTaken: + return String(localized: "A row with this primary key already exists. Edit that row instead.") + case .rowMissing: + return String(localized: "No row has this primary key, so nothing was updated. Another client may have deleted it.") + case nil: + return nil + } } private struct Writer { @@ -100,8 +102,7 @@ enum CassandraRowWriter { )) } } - let statement = "INSERT INTO \(target) (\(names.joined(separator: ", "))) " - + "VALUES (\(placeholders.joined(separator: ", "))) IF NOT EXISTS" + let statement = CassandraConditionalWrite.insertStatement(target: target, names: names, values: placeholders) return PluginRowWrite(statement: statement, parameters: parameters, rowIndices: [change.rowIndex]) } @@ -133,7 +134,9 @@ enum CassandraRowWriter { } } let key = try keyCondition(for: change) - let statement = "UPDATE \(target) SET \(assignments.joined(separator: ", ")) WHERE \(key.cql)" + let statement = CassandraConditionalWrite.updateStatement( + target: target, assignments: assignments, keyCondition: key.cql + ) return PluginRowWrite(statement: statement, parameters: parameters + key.values, rowIndices: [change.rowIndex]) } diff --git a/apple/Plugins/DamengDriverPlugin/DamengPlugin.swift b/apple/Plugins/DamengDriverPlugin/DamengPlugin.swift index cc80a87298..70b89896ec 100644 --- a/apple/Plugins/DamengDriverPlugin/DamengPlugin.swift +++ b/apple/Plugins/DamengDriverPlugin/DamengPlugin.swift @@ -323,10 +323,9 @@ final class DamengPluginDriver: PluginDatabaseDriver, @unchecked Sendable { let task = Task { do { let result = try await self.execute(query: query, context: context) - continuation.yield(.header(PluginStreamHeader( - columns: result.columns, - columnTypeNames: result.columnTypeNames - ))) + var header = PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames) + if result.isTruncated { header.rowLimit = result.rows.count } + continuation.yield(.header(header)) if !result.rows.isEmpty { continuation.yield(.rows(result.rows)) } diff --git a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift index ee013d3fe9..72389cdf12 100644 --- a/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift +++ b/apple/Plugins/DynamoDBDriverPlugin/DynamoDBPluginDriver+Execution.swift @@ -373,7 +373,9 @@ extension DynamoDBPluginDriver { source = try await spoolSource(for: statement, session: session) } catch DynamoDBError.invalidStatement(let message) where message == Self.notAReadMarker { let result = try await runStatement(text, parameters: [], rowCap: PluginRowLimits.emergencyMax) - continuation.yield(.header(PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames))) + var header = PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames) + if result.isTruncated { header.rowLimit = result.rows.count } + continuation.yield(.header(header)) if !result.rows.isEmpty { continuation.yield(.rows(result.rows)) } return } diff --git a/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift b/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift index e8d3c50f61..038ed7dbcc 100644 --- a/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift +++ b/apple/Plugins/EtcdDriverPlugin/EtcdPluginDriver.swift @@ -242,11 +242,11 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { continuation: AsyncThrowingStream.Continuation ) async throws { let matcher = try filter.matcher() - continuation.yield(.header(PluginStreamHeader( + var header = PluginStreamHeader( columns: Self.columns, columnTypeNames: Self.columnTypeNames, estimatedRowCount: nil - ))) + ) let range = Self.prefixRange(prefix) guard sortAscending else { @@ -256,6 +256,9 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { req.sortTarget = "KEY" let response = try await client.rangeRequest(req) let rows = (response.kvs ?? []).map(Self.filterRow).filter(matcher.matches).map(\.cells) + // `more` says the range held keys past the limit. + if response.more == true { header.rowLimit = rows.count } + continuation.yield(.header(header)) if !rows.isEmpty { continuation.yield(.rows(rows)) } @@ -263,6 +266,7 @@ final class EtcdPluginDriver: PluginDatabaseDriver, @unchecked Sendable { return } + continuation.yield(.header(header)) do { try await scanKeyValues(in: range, client: client) { kvs in let rows = kvs.map(Self.filterRow).filter(matcher.matches).map(\.cells) diff --git a/apple/Plugins/FormatPluginTests/CSVExportFormulaAndDecimalTests.swift b/apple/Plugins/FormatPluginTests/CSVExportFormulaAndDecimalTests.swift new file mode 100644 index 0000000000..db59df3638 --- /dev/null +++ b/apple/Plugins/FormatPluginTests/CSVExportFormulaAndDecimalTests.swift @@ -0,0 +1,174 @@ +// +// CSVExportFormulaAndDecimalTests.swift +// FormatPluginTests +// + +import Foundation +import TableProPluginKit +import Testing + +private final class TypedCSVSource: PluginExportDataSource, @unchecked Sendable { + let databaseTypeId = "PostgreSQL" + private let header: PluginStreamHeader + private let rows: [PluginRow] + + init(columns: [String], columnTypeNames: [String], rows: [PluginRow]) { + header = PluginStreamHeader(columns: columns, columnTypeNames: columnTypeNames) + self.rows = rows + } + + func streamRows(table: String, databaseName: String) -> AsyncThrowingStream { + let header = header + let rows = rows + return AsyncThrowingStream { continuation in + continuation.yield(.header(header)) + continuation.yield(.rows(rows)) + continuation.finish() + } + } + + func fetchTableDDL(table: String, databaseName: String) async throws -> String { "" } + + func execute(query: String) async throws -> PluginQueryResult { + PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) + } + + func quoteIdentifier(_ identifier: String) -> String { "\"\(identifier)\"" } + + func escapeStringLiteral(_ value: String) -> String { value } + + func fetchApproximateRowCount(table: String, databaseName: String) async throws -> Int? { nil } +} + +struct CSVExportFormulaAndDecimalTests { + private func export( + _ options: CSVExportOptions, + columns: [String], + types: [String], + rows: [PluginRow] + ) async throws -> (text: String, warnings: [String]) { + let output = try await CSVExportHarness.shared.bytes( + options: options, + tables: [PluginExportTable(name: "users", databaseName: "", tableType: "table", schema: nil, kind: .table)], + dataSource: TypedCSVSource(columns: columns, columnTypeNames: types, rows: rows) + ) + return (String(decoding: output.data, as: UTF8.self), output.result.warnings) + } + + // A database export is read back by importers, which keep the quote as part of the value. + @Test("The stock export writes a value that starts like a formula as it is") + func stockExportKeepsValues() async throws { + let output = try await export( + CSVExportOptions(), + columns: ["handle"], + types: ["text"], + rows: [["@handle"], ["+84 90 123 4567"], ["-pending"]] + ) + #expect(output.text == "handle\n@handle\n+84 90 123 4567\n-pending\n") + #expect(output.warnings.isEmpty) + } + + @Test("Sanitizing says how many values it gave a leading quote") + func sanitizingCountsWhatItChanged() async throws { + var options = CSVExportOptions() + options.sanitizeFormulas = true + let output = try await export( + options, + columns: ["handle"], + types: ["text"], + rows: [["@handle"], ["plain"], ["=1+1"], ["-5"], ["-pending"]] + ) + #expect(output.text == "handle\n\"'@handle\"\nplain\n\"'=1+1\"\n-5\n\"'-pending\"\n") + #expect(output.warnings == CSVExportPlugin.neutralisedWarnings(3)) + } + + @Test("Nothing changed means no warning about changes") + func noCountWithoutChanges() { + #expect(CSVExportPlugin.neutralisedWarnings(0).isEmpty) + } + + // A text column holding "1.10" is a version or a code, not a number. + @Test("A decimal comma leaves text columns alone and keeps columns with no usable type") + func decimalCommaSkipsTextColumns() async throws { + var options = CSVExportOptions() + options.decimalFormat = .comma + options.delimiter = .semicolon + let output = try await export( + options, + columns: ["version", "amount", "untyped"], + types: ["varchar(16)", "numeric(10,2)", ""], + rows: [["1.10", "1.10", "1.10"]] + ) + #expect(output.text == "version;amount;untyped\n1.10;1,10;1,10\n") + } + + @Test("A decimal comma reaches every engine's numeric type", arguments: [ + "Nullable(Float64)", "Nullable(Decimal(10, 2))", "money", "smallmoney", "BINARY_DOUBLE", "BINARY_FLOAT", + "BIGNUMERIC", "double precision", "NUMBER(10,2)" + ]) + func decimalCommaReachesNumericTypes(typeName: String) async throws { + var options = CSVExportOptions() + options.decimalFormat = .comma + options.delimiter = .semicolon + let output = try await export(options, columns: ["n"], types: [typeName], rows: [["1.5"]]) + #expect(output.text == "n\n1,5\n") + } + + @Test("A decimal comma never reaches a text type", arguments: [ + "varchar(16)", "String", "LowCardinality(String)", "nvarchar(10)", "TEXT", "jsonb" + ]) + func decimalCommaSkipsTextTypes(typeName: String) async throws { + var options = CSVExportOptions() + options.decimalFormat = .comma + options.delimiter = .semicolon + let output = try await export(options, columns: ["n"], types: [typeName], rows: [["1.5"]]) + #expect(output.text == "n\n1.5\n") + } + + @Test("A header name sanitizing changes is counted with the values") + func headerNamesAreCounted() async throws { + var options = CSVExportOptions() + options.sanitizeFormulas = true + let output = try await export(options, columns: ["=total"], types: ["text"], rows: [["=1"]]) + #expect(output.text == "\"'=total\"\n\"'=1\"\n") + #expect(output.warnings == CSVExportPlugin.neutralisedWarnings(2)) + } + + // `'-1,5` is text to a spreadsheet, so a negative number written with a decimal comma lost its value. + @Test("A number written with a decimal comma is never given a quote") + func commaDecimalsAreNotSanitized() async throws { + var options = CSVExportOptions() + options.sanitizeFormulas = true + options.decimalFormat = .comma + options.delimiter = .semicolon + let output = try await export( + options, + columns: ["amount", "note"], + types: ["numeric", "text"], + rows: [["-1.5", "-1.5x"]] + ) + #expect(output.text == "amount;note\n-1,5;\"'-1.5x\"\n") + #expect(output.warnings == CSVExportPlugin.neutralisedWarnings(1)) + } +} + +struct PluginRowWritersFormulaLeadTests { + // OWASP lists a leading line feed and the full-width operators, which a Japanese locale reads as formulas. + @Test("A leading line feed or a full-width operator counts as a formula lead") + func owaspLeadsAreNeutralised() { + let options = PluginCsvWriteOptions.default + #expect(PluginRowWriters.csvField("\n=1+1", options: options).hasPrefix("\"'")) + for lead in ["\u{FF1D}", "\u{FF0B}", "\u{FF0D}", "\u{FF20}"] { + #expect(PluginRowWriters.csvField("\(lead)SUM(A1)", options: options) == "\"'\(lead)SUM(A1)\"") + } + } + + @Test("Only a value that would be prefixed is formula-like") + func formulaLikeMatchesTheWriter() { + #expect(PluginRowWriters.isFormulaLike("=1+1")) + #expect(PluginRowWriters.isFormulaLike("\t=1+1")) + #expect(!PluginRowWriters.isFormulaLike("-5")) + #expect(!PluginRowWriters.isFormulaLike("plain")) + #expect(!PluginRowWriters.isFormulaLike("")) + } +} diff --git a/apple/Plugins/FormatPluginTests/ExportImportRoundTripTests.swift b/apple/Plugins/FormatPluginTests/ExportImportRoundTripTests.swift index 29503ea388..ed0cae7764 100644 --- a/apple/Plugins/FormatPluginTests/ExportImportRoundTripTests.swift +++ b/apple/Plugins/FormatPluginTests/ExportImportRoundTripTests.swift @@ -85,6 +85,14 @@ struct ExportImportRoundTripTests { try RoundTrip.expectSame(target, RoundTripFixture.table, FormatLimits.csv.carried(RoundTripFixture.rows)) } + @Test("A CSV file sanitized for a spreadsheet imports back with the quote it gave formula-like values") + func csvSanitizedImportsBackWithItsQuotes() async throws { + var options = CSVExportOptions() + options.sanitizeFormulas = true + let target = try await RoundTrip.csv(options) + try RoundTrip.expectSame(target, RoundTripFixture.table, FormatLimits.csvSanitized.carried(RoundTripFixture.rows)) + } + @Test("A CSV file written with a byte order mark imports back") func csvWithByteOrderMarkImportsBack() async throws { var options = CSVExportOptions() @@ -166,9 +174,14 @@ struct ExportImportRoundTripTests { extension FormatLimits { // CSV has no NULL and no binary: both NULL and '' are written as an empty field and read as - // NULL, and a binary value is written as hex text. A value a spreadsheet would run as a - // formula gains a leading quote. + // NULL, and a binary value is written as hex text. static let csv = FormatLimits( + emptyTextIsNull: true, + binary: { .text(FormatLimits.hex($0)) } + ) + + // Sanitizing for a spreadsheet gives a value that would run as a formula a leading quote, which the import keeps. + static let csvSanitized = FormatLimits( emptyTextIsNull: true, binary: { .text(FormatLimits.hex($0)) }, text: { FormatLimits.csvNeutralised[$0] ?? $0 } diff --git a/apple/Plugins/FormatPluginTests/FormatSettingsChangedMidRunTests.swift b/apple/Plugins/FormatPluginTests/FormatSettingsChangedMidRunTests.swift new file mode 100644 index 0000000000..f995008f40 --- /dev/null +++ b/apple/Plugins/FormatPluginTests/FormatSettingsChangedMidRunTests.swift @@ -0,0 +1,185 @@ +// +// FormatSettingsChangedMidRunTests.swift +// FormatPluginTests +// + +import Foundation +import TableProPluginKit +import Testing + +// One plugin instance serves every window, so a dialog open in another window changes `settings` while a run reads. +// Each source here changes them between its first and second batch, the way that dialog would. +private final class TwoBatchSource: PluginExportDataSource, @unchecked Sendable { + let databaseTypeId = "SQLite" + private let header: PluginStreamHeader + private let batches: [[PluginRow]] + private let betweenBatches: @Sendable () -> Void + + init(columns: [String], batches: [[PluginRow]], betweenBatches: @escaping @Sendable () -> Void) { + header = PluginStreamHeader(columns: columns, columnTypeNames: columns.map { _ in "TEXT" }) + self.batches = batches + self.betweenBatches = betweenBatches + } + + // Pulled, so the change lands only after the plugin has written the first batch. + func streamRows(table: String, databaseName: String) -> AsyncThrowingStream { + let header = header + let batches = batches + let betweenBatches = betweenBatches + let pulls = Counter() + return AsyncThrowingStream(unfolding: { + let pull = pulls.next() + if pull == 0 { return .header(header) } + guard pull <= batches.count else { return nil } + if pull == 2 { betweenBatches() } + return .rows(batches[pull - 1]) + }) + } + + func fetchTableDDL(table: String, databaseName: String) async throws -> String { "" } + + func execute(query: String) async throws -> PluginQueryResult { + PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) + } + + func quoteIdentifier(_ identifier: String) -> String { "\"\(identifier)\"" } + + func escapeStringLiteral(_ value: String) -> String { value } + + func fetchApproximateRowCount(table: String, databaseName: String) async throws -> Int? { nil } +} + +private final class Counter: @unchecked Sendable { + private let lock = NSLock() + private var value = 0 + + func next() -> Int { + lock.lock() + defer { lock.unlock() } + defer { value += 1 } + return value + } +} + +private final class ForeignKeyRecordingSink: PluginImportDataSink, @unchecked Sendable { + let databaseTypeId = "SQLite" + var onExecute: () -> Void = {} + private(set) var disabled = 0 + private(set) var enabled = 0 + + func execute(statement: String) async throws { onExecute() } + func beginTransaction() async throws {} + func commitTransaction() async throws {} + func rollbackTransaction() async throws {} + func disableForeignKeyChecks() async throws { disabled += 1 } + func enableForeignKeyChecks() async throws { enabled += 1 } +} + +private final class OneStatementSource: PluginImportSource, @unchecked Sendable { + func statements() async throws -> AsyncThrowingStream<(statement: String, lineNumber: Int), Error> { + AsyncThrowingStream { continuation in + continuation.yield((statement: "INSERT INTO t VALUES (1);", lineNumber: 1)) + continuation.finish() + } + } + + func fileURL() -> URL { URL(fileURLWithPath: "/dev/null") } + + func fileSizeBytes() -> Int64 { 0 } +} + +@Suite(.serialized) +struct FormatSettingsChangedMidRunTests { + private let table = PluginExportTable(name: "t", databaseName: "", tableType: "table", schema: nil, kind: .table) + private let rowWithNull: PluginRow = [.text("1"), .null] + + private func export( + _ plugin: any ExportFormatPlugin, + from source: TwoBatchSource, + fileExtension: String + ) async throws -> Data { + let destination = FileManager.default.temporaryDirectory + .appendingPathComponent("\(UUID().uuidString).\(fileExtension)") + defer { try? FileManager.default.removeItem(at: destination) } + _ = try await plugin.export( + tables: [table], + dataSource: source, + destination: destination, + progress: PluginExportProgress(progress: Progress(totalUnitCount: 1)) + ) + return try Data(contentsOf: destination) + } + + @Test("JSON keeps writing null keys after another window turns Include NULL values off") + func jsonKeepsItsNullChoice() async throws { + let plugin = JSONExportPlugin() + let stored = plugin.settings + defer { plugin.settings = stored } + var options = JSONExportOptions() + options.layout = .newlineDelimited + options.includeNullValues = true + plugin.settings = options + + let source = TwoBatchSource(columns: ["id", "note"], batches: [[rowWithNull], [rowWithNull]]) { + plugin.settings.includeNullValues = false + } + let text = String(decoding: try await export(plugin, from: source, fileExtension: "ndjson"), as: UTF8.self) + #expect(text == "{\"id\": \"1\", \"note\": null}\n{\"id\": \"1\", \"note\": null}\n") + } + + @Test("Markdown keeps its NULL placeholder after another window changes it") + func markdownKeepsItsPlaceholder() async throws { + let plugin = MarkdownExportPlugin() + let stored = plugin.settings + defer { plugin.settings = stored } + var options = MarkdownExportOptions() + options.alignsColumns = false + options.includesTableNames = false + options.nullPlaceholder = "NULL" + plugin.settings = options + + let source = TwoBatchSource(columns: ["id", "note"], batches: [[rowWithNull], [rowWithNull]]) { + plugin.settings.nullPlaceholder = "(none)" + } + let text = String(decoding: try await export(plugin, from: source, fileExtension: "md"), as: UTF8.self) + #expect(!text.contains("(none)")) + #expect(text.components(separatedBy: "| 1 | NULL |").count == 3) + } + + @Test("XLSX keeps writing NULL as an empty cell after another window turns the option off") + func xlsxKeepsItsNullChoice() async throws { + let plugin = XLSXExportPlugin() + let stored = plugin.settings + defer { plugin.settings = stored } + var options = XLSXExportOptions() + options.convertNullToEmpty = true + plugin.settings = options + + let source = TwoBatchSource(columns: ["id", "note"], batches: [[rowWithNull], [rowWithNull]]) { + plugin.settings.convertNullToEmpty = false + } + let written = try await export(plugin, from: source, fileExtension: "xlsx") + let text = try #require(String(bytes: written, encoding: .isoLatin1)) + #expect(!text.contains("NULL")) + } + + @Test("A SQL import turns foreign key checks back on after another window unticks the option") + func sqlImportRestoresTheChecksItTurnedOff() async throws { + let plugin = SQLImportPlugin() + let stored = plugin.settings + defer { plugin.settings = stored } + var options = SQLImportOptions() + options.disableForeignKeyChecks = true + plugin.settings = options + + let sink = ForeignKeyRecordingSink() + sink.onExecute = { plugin.settings.disableForeignKeyChecks = false } + _ = try await plugin.performImport( + source: OneStatementSource(), + sink: sink, + progress: PluginImportProgress(progress: Progress()) + ) + #expect(sink.disabled == 1) + #expect(sink.enabled == 1) + } +} diff --git a/apple/Plugins/JSONExportPlugin/JSONExportPlugin.swift b/apple/Plugins/JSONExportPlugin/JSONExportPlugin.swift index cf23a29b92..60adecb627 100644 --- a/apple/Plugins/JSONExportPlugin/JSONExportPlugin.swift +++ b/apple/Plugins/JSONExportPlugin/JSONExportPlugin.swift @@ -53,10 +53,11 @@ final class JSONExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug } } - /// NDJSON is one row per line by definition, so the pretty-print setting cannot apply to it - /// and the wrapping object has no place to go. - let isNewlineDelimited = settings.layout == .newlineDelimited - let prettyPrint = settings.prettyPrint && !isNewlineDelimited + // One plugin instance serves every window, so a dialog in another window can change `settings` mid-export. + let options = settings + // NDJSON is one row per line by definition, so pretty-printing and the wrapping object do not apply. + let isNewlineDelimited = options.layout == .newlineDelimited + let prettyPrint = options.prettyPrint && !isNewlineDelimited let indent = prettyPrint ? " " : "" let newline = prettyPrint ? "\n" : "" @@ -104,7 +105,7 @@ final class JSONExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug for (colIndex, column) in columns.enumerated() { if colIndex < row.count { let value = row[colIndex] - if settings.includeNullValues || !value.isNull { + if options.includeNullValues || !value.isNull { if !isFirstField { rowString += ", " } @@ -117,7 +118,7 @@ final class JSONExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug let jsonValue = formatJSONValue( value, columnTypeName: colTypeName, - preserveAsString: settings.preserveAllAsStrings + preserveAsString: options.preserveAllAsStrings ) rowString += "\"\(escapedKey)\": \(jsonValue)" } diff --git a/apple/Plugins/MarkdownExportPlugin/MarkdownExportPlugin.swift b/apple/Plugins/MarkdownExportPlugin/MarkdownExportPlugin.swift index 2ee2f17664..8fc08e0fd3 100644 --- a/apple/Plugins/MarkdownExportPlugin/MarkdownExportPlugin.swift +++ b/apple/Plugins/MarkdownExportPlugin/MarkdownExportPlugin.swift @@ -54,13 +54,15 @@ final class MarkdownExportPlugin: ObservableObject, ExportFormatPlugin, Settable if !committed { PluginExportUtilities.rollbackAtomicWrite(at: tempURL) } } + // One plugin instance serves every window, so a dialog in another window can change `settings` mid-export. + let options = settings for (index, table) in tables.enumerated() { try progress.checkCancellation() progress.setCurrentTable(table.qualifiedName, index: index + 1) - if settings.includesTableNames { + if options.includesTableNames { try fileHandle.write(contentsOf: "## \(table.qualifiedName)\n\n".toUTF8Data()) } - try await writeTable(table, dataSource: dataSource, to: fileHandle, progress: progress) + try await writeTable(table, options: options, dataSource: dataSource, to: fileHandle, progress: progress) try fileHandle.write(contentsOf: "\n".toUTF8Data()) } @@ -73,6 +75,7 @@ final class MarkdownExportPlugin: ObservableObject, ExportFormatPlugin, Settable private func writeTable( _ table: PluginExportTable, + options: MarkdownExportOptions, dataSource: any PluginExportDataSource, to fileHandle: FileHandle, progress: PluginExportProgress @@ -89,9 +92,9 @@ final class MarkdownExportPlugin: ObservableObject, ExportFormatPlugin, Settable columns = header.columns case .rows(let rows): for row in rows { - let cells = row.map { cellText($0) } + let cells = row.map { cellText($0, nullPlaceholder: options.nullPlaceholder) } progress.incrementRow() - guard settings.alignsColumns, !wroteHeader else { + guard options.alignsColumns, !wroteHeader else { if !wroteHeader { try writeHeader(columns, widths: nil, to: fileHandle) wroteHeader = true @@ -115,7 +118,7 @@ final class MarkdownExportPlugin: ObservableObject, ExportFormatPlugin, Settable } if !wroteHeader { - widths = settings.alignsColumns + widths = options.alignsColumns ? MarkdownTableRenderer.widths(header: columns, sample: buffered) : nil try writeHeader(columns, widths: widths, to: fileHandle) @@ -138,10 +141,10 @@ final class MarkdownExportPlugin: ObservableObject, ExportFormatPlugin, Settable ).toUTF8Data()) } - private func cellText(_ value: PluginCellValue) -> String { + private func cellText(_ value: PluginCellValue, nullPlaceholder: String) -> String { switch value { case .null: - return MarkdownTableRenderer.cell(settings.nullPlaceholder) + return MarkdownTableRenderer.cell(nullPlaceholder) case .bytes(let data): return MarkdownTableRenderer.cell("0x\(data.map { String(format: "%02X", $0) }.joined())") case .text(let text): diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift index 3be35d7dd1..55c8df7418 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBConnection+SyncHelpers.swift @@ -518,7 +518,10 @@ nonisolated extension MongoDBConnection { let documents = try MongoExportDocument.read( MongoReadDocuments(dictionaries: sample, texts: texts), representation: uuidRepresentation ) - continuation.yield(.header(projection.header)) + var header = projection.header + // iterateCursorStreaming stops there, so a stream that reaches it was cut. + header.rowLimit = PluginRowLimits.emergencyMax + continuation.yield(.header(header)) if !sample.isEmpty { let rows = BsonDocumentFlattener.flatten( documents: sample, columns: projection.columns, kinds: projection.kinds, diff --git a/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift b/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift index 5e10afbea4..05bfefbd69 100644 --- a/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift +++ b/apple/Plugins/MongoDBDriverPlugin/MongoDBPluginDriver.swift @@ -1031,10 +1031,9 @@ nonisolated final class MongoDBPluginDriver: PluginDatabaseDriver, @unchecked Se } ) if !result.columns.isEmpty { - continuation.yield(.header(PluginStreamHeader( - columns: result.columns, - columnTypeNames: result.columnTypeNames - ))) + var header = PluginStreamHeader(columns: result.columns, columnTypeNames: result.columnTypeNames) + if result.isTruncated { header.rowLimit = result.rows.count } + continuation.yield(.header(header)) } if !result.rows.isEmpty { continuation.yield(.rows(result.rows, documents: result.exportDocuments)) diff --git a/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift b/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift index edea147522..ba69a63dc3 100644 --- a/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift +++ b/apple/Plugins/MySQLDriverPlugin/MariaDBPluginConnection.swift @@ -88,7 +88,19 @@ struct MariaDBPluginQueryResult { // MARK: - Connection Class final class MariaDBPluginConnection: @unchecked Sendable { - private var mysql: UnsafeMutablePointer? + // Under stateLock, which disconnect() clears it under. Only `queue` may call into the handle: the close is queued there. + private var _mysql: UnsafeMutablePointer? + private var _serverThreadId: UInt = 0 + + private var mysql: UnsafeMutablePointer? { + stateLock.withLock { _mysql } + } + + // Read from the snapshot taken at connect: Stop runs off `queue`, where the handle may already be freed. + private var connectedThreadId: UInt? { + stateLock.withLock { _mysql == nil ? nil : _serverThreadId } + } + private let queue = DispatchQueue(label: "com.TablePro.mariadb.plugin", qos: .userInitiated) /// Serial, and separate from `queue`, which the read being cancelled is sitting on. Serial so a @@ -342,9 +354,9 @@ final class MariaDBPluginConnection: @unchecked Sendable { if let cachePath = kerberos?.cachePath { KerberosTicket.discard(cachePath) } - let handle = mysql + let handle = _mysql let cleanupQueue = queue - mysql = nil + _mysql = nil if let handle = handle { cleanupQueue.async { mysql_close(handle) @@ -390,8 +402,10 @@ final class MariaDBPluginConnection: @unchecked Sendable { self.baselineSelectLimit = nil self.hasCapturedBaselineSelectLimit = false + let threadId = mysql_thread_id(handle) self.stateLock.lock() - self.mysql = handle + self._mysql = handle + self._serverThreadId = threadId self._isConnected = true self._errorLanguage = session.errorLanguage self._sessionEndedByKill = false @@ -600,10 +614,10 @@ final class MariaDBPluginConnection: @unchecked Sendable { KerberosTicket.discard(cachePath) } - let handle = mysql - mysql = nil - stateLock.lock() + let handle = _mysql + _mysql = nil + _serverThreadId = 0 _isConnected = false stateLock.unlock() @@ -635,7 +649,7 @@ final class MariaDBPluginConnection: @unchecked Sendable { let target = killTarget guard mayEndSession || !target.endsSession else { return } - guard let mysql = mysql, let statement = target.statement(threadId: mysql_thread_id(mysql)) else { return } + guard let threadId = connectedThreadId, let statement = target.statement(threadId: threadId) else { return } cancelQueue.async { [self] in guard deliverKill(statement) else { return } if target.endsSession { @@ -657,11 +671,9 @@ final class MariaDBPluginConnection: @unchecked Sendable { recordKillDelivered(generation: generation) } - /// The server thread this connection is on, read before a statement goes out so a kill still - /// has somewhere to go once the handle itself is unusable. + // Read before a statement goes out, so a kill still has somewhere to go once the handle itself is unusable. internal var currentThreadId: UInt { - guard let mysql = self.mysql else { return 0 } - return mysql_thread_id(mysql) + connectedThreadId ?? 0 } /// The kill has to reach the server the query is running on, which means repeating the transport diff --git a/apple/Plugins/MySQLDriverTests/MariaDBStopThreadIdTests.swift b/apple/Plugins/MySQLDriverTests/MariaDBStopThreadIdTests.swift new file mode 100644 index 0000000000..52bb160f9a --- /dev/null +++ b/apple/Plugins/MySQLDriverTests/MariaDBStopThreadIdTests.swift @@ -0,0 +1,38 @@ +// +// MariaDBStopThreadIdTests.swift +// MySQLDriverTests +// + +import Foundation +import TableProPluginKit +import Testing + +// Stop runs off the statement queue, where a disconnect's queued close can free the handle under it, so the kill's +// thread id is the one the handshake gave, held beside the handle and cleared with it. +@Suite(.timeLimit(.minutes(1))) +struct MariaDBStopThreadIdTests { + @Test("The kill's thread id is the session's own, and none is left once the session is closed") + func threadIdFollowsTheSession() async throws { + let server = try PlaintextMySQLServer(asksForCleartext: false) + defer { server.close() } + async let session = server.serveOneSession() + let connection = MariaDBPluginConnection( + transport: .tcp(host: "127.0.0.1", port: UInt32(server.port)), + user: "app", + password: "app-password", + database: "", + sslConfig: SSLConfiguration(mode: .disabled), + enableCleartextPlugin: false + ) + + try await connection.connect() + let signedIn = connection.currentThreadId + connection.disconnect() + let closed = connection.currentThreadId + _ = await session + + // The fake server's handshake names connection 1. + #expect(signedIn == 1) + #expect(closed == 0) + } +} diff --git a/apple/Plugins/ParquetExportPlugin/ParquetExportPlugin.swift b/apple/Plugins/ParquetExportPlugin/ParquetExportPlugin.swift index 90eeda01e8..2495d396c6 100644 --- a/apple/Plugins/ParquetExportPlugin/ParquetExportPlugin.swift +++ b/apple/Plugins/ParquetExportPlugin/ParquetExportPlugin.swift @@ -59,10 +59,12 @@ final class ParquetExportPlugin: ObservableObject, ExportFormatPlugin, SettableP progress: PluginExportProgress ) async throws -> ExportFormatResult { guard !tables.isEmpty else { return ExportFormatResult() } + // One plugin instance serves every window, so a dialog in another window can change `settings` mid-export. + let options = settings var warnings: [String] = [] try await ParquetTableFiles.write(tables, destination: destination, progress: progress) { table, fileURL in - warnings += try await writeTable(table, dataSource: dataSource, to: fileURL, progress: progress) + warnings += try await writeTable(table, options: options, dataSource: dataSource, to: fileURL, progress: progress) } if tables.count > 1 { @@ -76,6 +78,7 @@ final class ParquetExportPlugin: ObservableObject, ExportFormatPlugin, SettableP private func writeTable( _ table: PluginExportTable, + options: ParquetExportOptions, dataSource: any PluginExportDataSource, to fileURL: URL, progress: PluginExportProgress @@ -123,8 +126,8 @@ final class ParquetExportPlugin: ObservableObject, ExportFormatPlugin, SettableP } let fallbacks = try staging.copyToParquet( fileURL: fileURL, - compression: settings.compression.duckDBValue, - rowGroupSize: settings.rowGroupSize + compression: options.compression.duckDBValue, + rowGroupSize: options.rowGroupSize ) return fallbacks.map { fallback in String( diff --git a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver+Operations.swift b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver+Operations.swift index c2f93088de..6ac131744d 100644 --- a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver+Operations.swift +++ b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver+Operations.swift @@ -141,8 +141,9 @@ extension RedisPluginDriver { if opts.xx { args.append("XX".redisArgument) } if opts.keepTTL { args.append("KEEPTTL".redisArgument) } } - try await conn.run(args) - return buildStatusResult("OK", startTime: startTime) + // NX or XX that stops the write answers nil, not an error. + let reply = try await conn.run(args) + return buildStatusResult(reply.displayText, startTime: startTime) case .del(let keys): let args = ["DEL"] + keys diff --git a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift index 43d70ee2b9..4d4ffcb41b 100644 --- a/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift +++ b/apple/Plugins/RedisDriverPlugin/RedisPluginDriver.swift @@ -561,11 +561,13 @@ final class RedisPluginDriver: PluginDatabaseDriver, @unchecked Sendable { default: let startTime = Date() let result = try await executeOperation(operation, connection: conn, startTime: startTime) - continuation.yield(.header(PluginStreamHeader( + var header = PluginStreamHeader( columns: result.columns, columnTypeNames: result.columnTypeNames, estimatedRowCount: nil - ))) + ) + if result.isTruncated { header.rowLimit = result.rows.count } + continuation.yield(.header(header)) if !result.rows.isEmpty { continuation.yield(.rows(result.rows)) } diff --git a/apple/Plugins/RedisDriverTests/RedisConditionalWriteTests.swift b/apple/Plugins/RedisDriverTests/RedisConditionalWriteTests.swift index 72e4aa3bd3..4402cf7e01 100644 --- a/apple/Plugins/RedisDriverTests/RedisConditionalWriteTests.swift +++ b/apple/Plugins/RedisDriverTests/RedisConditionalWriteTests.swift @@ -180,6 +180,25 @@ struct RedisConditionalWriteTests { #expect(result.rows.first?.first?.asText == "OK") } + @Test("A SET that NX or XX kept from writing shows nil, as the server answered") + func skippedSetShowsNil() async throws { + let channel = StubRedisChannel([.null]) + let result = try await driver().executeOperation( + try RedisCommandParser.parse("SET lock:job 1 NX"), connection: channel, startTime: Date() + ) + #expect(result.rows.first?.first?.asText == "(nil)") + #expect(channel.sentCommands == [["SET", "lock:job", "1", "NX"]]) + } + + @Test("A SET that wrote the key shows OK") + func appliedSetShowsOK() async throws { + let channel = StubRedisChannel([.status("OK")]) + let result = try await driver().executeOperation( + try RedisCommandParser.parse("SET lock:job 1 XX EX 60"), connection: channel, startTime: Date() + ) + #expect(result.rows.first?.first?.asText == "OK") + } + @Test("A conditional write the server refused outright keeps the server's message") func execKeepsServerErrorForConditionalWrite() { let reply = RedisReply.array([.error("ERR no such key")]) diff --git a/apple/Plugins/SQLImportPlugin/SQLImportPlugin.swift b/apple/Plugins/SQLImportPlugin/SQLImportPlugin.swift index 91b6a9c205..137d0a7ac3 100644 --- a/apple/Plugins/SQLImportPlugin/SQLImportPlugin.swift +++ b/apple/Plugins/SQLImportPlugin/SQLImportPlugin.swift @@ -51,15 +51,17 @@ final class SQLImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi var errors: [PluginImportResult.ImportStatementError] = [] let maxErrors = 1_000 - let errorMode = settings.errorHandling - let useTransaction = settings.wrapInTransaction && errorMode != .skipAndContinue + // One plugin instance serves every window: a checkbox unticked elsewhere mid-import left the checks off. + let options = settings + let errorMode = options.errorHandling + let useTransaction = options.wrapInTransaction && errorMode != .skipAndContinue let fileSizeBytes = source.fileSizeBytes() let estimatedTotal = max(1, Int(fileSizeBytes / 500)) progress.setEstimatedTotal(estimatedTotal) do { - if settings.disableForeignKeyChecks { + if options.disableForeignKeyChecks { try await sink.disableForeignKeyChecks() } @@ -99,7 +101,7 @@ final class SQLImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi Self.logger.warning("Failed to commit partial import") } } - if settings.disableForeignKeyChecks { + if options.disableForeignKeyChecks { do { try await sink.enableForeignKeyChecks() } catch { @@ -134,7 +136,7 @@ final class SQLImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi didCommit = true } - if settings.disableForeignKeyChecks { + if options.disableForeignKeyChecks { do { try await sink.enableForeignKeyChecks() } catch { @@ -156,7 +158,7 @@ final class SQLImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlugi } } - if settings.disableForeignKeyChecks { + if options.disableForeignKeyChecks { do { try await sink.enableForeignKeyChecks() } catch { diff --git a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift index dc8a667c2c..53d2af6929 100644 --- a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift +++ b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift @@ -1567,7 +1567,8 @@ public extension PluginDatabaseDriver { } func escapedParameterValue(_ value: String) -> String { - if Self.isNumericLiteral(value) { + // No numeric column reads as `00123` or `-007`, so that text is a code or a zip and stays a string. + if Self.isNumericLiteral(value), !opensWithPaddingZero(value) { return value } return PluginSQLQuoting.stringLiteral(value, escapes: PluginSQLQuoting.Escapes(features: literalFeatures)) @@ -1611,3 +1612,10 @@ extension PluginDatabaseDriver { return backslash ? [.backslashEscapesInSingleQuotes, .backslashEscapesInDoubleQuotes] : [] } } + +// `007`, `-0042`: an integer part that opens with 0 and goes on with another digit. +private func opensWithPaddingZero(_ value: String) -> Bool { + let unsigned = value.unicodeScalars.drop { $0 == "-" || $0 == "+" } + guard unsigned.first == "0", let next = unsigned.dropFirst().first else { return false } + return ("0"..."9").contains(next) +} diff --git a/apple/Plugins/TableProPluginKit/PluginExportUtilities.swift b/apple/Plugins/TableProPluginKit/PluginExportUtilities.swift index 726b45de69..a89463cd38 100644 --- a/apple/Plugins/TableProPluginKit/PluginExportUtilities.swift +++ b/apple/Plugins/TableProPluginKit/PluginExportUtilities.swift @@ -109,6 +109,72 @@ public enum PluginExportUtilities { } } +// Shared so XLSX number cells and the CSV decimal comma read a column's declared type the same way. +public enum PluginColumnNumbers: Equatable, Sendable { + case text + case exact + case approximate + case untyped + + public init(typeName: String, bitsAreStrings: Bool = false) { + let base = Self.baseName(typeName) + if base.isEmpty { + self = .untyped + } else if Self.bitTypes.contains(base) { + self = bitsAreStrings ? .text : .exact + } else if Self.approximateTypes.contains(base) { + self = .approximate + } else if Self.exactTypes.contains(base) { + self = .exact + } else if Self.textTypes.contains(base) || Self.textMarkers.contains(where: { base.contains($0) }) { + self = .text + } else { + self = .untyped + } + } + + public var isNumeric: Bool { + self == .exact || self == .approximate + } + + // `Nullable(UInt64)` and `LowCardinality(Nullable(Int32))` name the type inside. The base is the + // first word without its width: `int4`, `Int64` and `INT UNSIGNED` are all `int`. + private static func baseName(_ typeName: String) -> String { + var name = typeName.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() + while name.hasSuffix(")"), let open = name.firstIndex(of: "("), + ["nullable", "lowcardinality"].contains(String(name[.. = [ + "int", "uint", "integer", "bigint", "smallint", "tinyint", "mediumint", "hugeint", + "uinteger", "ubigint", "usmallint", "utinyint", "uhugeint", "decimal", "dec", "numeric", + "number", "bignumeric", "serial", "bigserial", "smallserial", "money", "smallmoney", "year", "oid" + ] + + private static let approximateTypes: Set = [ + "float", "double", "real", "binary_float", "binary_double" + ] + + private static let textTypes: Set = ["enum", "set", "json", "jsonb", "name"] + + private static let bitTypes: Set = ["bit", "varbit"] + + // PostgreSQL's `bit(n)` is a string of 0 and 1. SQL Server's is a flag and MySQL's reads as a number. + public static func bitsAreStrings(databaseTypeId: String) -> Bool { + enginesWithBitStrings.contains(databaseTypeId) + } + + private static let enginesWithBitStrings: Set = [ + "PostgreSQL", "Redshift", "CockroachDB", "PGlite", "AlloyDB", "Citus", "Greenplum", "KingbaseES" + ] + + private static let textMarkers = ["char", "text", "string", "clob"] +} + public extension String { func toUTF8Data() throws -> Data { guard let data = self.data(using: .utf8) else { diff --git a/apple/Plugins/TableProPluginKit/PluginRowWriters.swift b/apple/Plugins/TableProPluginKit/PluginRowWriters.swift index e92617afb2..d3c0be5caa 100644 --- a/apple/Plugins/TableProPluginKit/PluginRowWriters.swift +++ b/apple/Plugins/TableProPluginKit/PluginRowWriters.swift @@ -18,9 +18,7 @@ public struct PluginCsvWriteOptions: Sendable, Equatable { public let lineEnding: String public let nullAsEmpty: Bool - /// Prefixes a value opening with `=`, `+`, `-` or `@` with a quote. A spreadsheet treats a cell - /// starting with one of those as a formula, and the values in an export come from the database - /// rather than from the person opening the file. + // The quote stays in the value for any reader but a spreadsheet, so it suits files meant for one. public let sanitizesFormulas: Bool /// Replaces a line break inside a value with a space. Off means the value keeps its break and is @@ -73,9 +71,7 @@ public enum PluginRowWriters { .replacingOccurrences(of: "\r", with: " ", options: .literal) } var wasNeutralised = false - // Scalars, not Characters: `\r\n` is one Character, so a Character test misses a Windows line break. - if options.sanitizesFormulas, let first = processed.unicodeScalars.first, - isFormulaLead(Character(first)), !isPlainNumber(processed) { + if options.sanitizesFormulas, isFormulaLike(processed) { processed = "'" + processed wasNeutralised = true } @@ -95,12 +91,19 @@ public enum PluginRowWriters { private static let quotedScalars: Set = ["\"", "\n", "\r", "\t"] - /// A leading tab or carriage return is treated as a formula lead too: Excel strips it before - /// parsing the cell, so `\t=1+1` reaches the formula engine exactly as `=1+1` would. - private static func isFormulaLead(_ character: Character) -> Bool { - formulaPrefixes.contains(character) || character == "\t" || character == "\r" + // Public so a format plugin can count the values sanitizing changes. + public static func isFormulaLike(_ value: String) -> Bool { + // Scalars, not Characters: `\r\n` is one Character, so a Character test misses a Windows line break. + guard let first = value.unicodeScalars.first, formulaLeads.contains(first) else { return false } + return !isPlainNumber(value) } + // OWASP's list: the four operators, the whitespace Excel strips before parsing (so `\t=1+1` runs as `=1+1`), + // and the full-width operators a Japanese locale reads as formulas. The same set as the shared core's `FORMULA_LEADS`. + private static let formulaLeads: Set = [ + "=", "+", "-", "@", "\t", "\r", "\n", "\u{FF1D}", "\u{FF0B}", "\u{FF0D}", "\u{FF20}" + ] + // A number is never a formula, and `'-5` is text to every reader, a spreadsheet included. // The same rule as the Linux app's `is_plain_number`. private static func isPlainNumber(_ value: String) -> Bool { diff --git a/apple/Plugins/TeradataDriverPlugin/TeradataAsyncConnection.swift b/apple/Plugins/TeradataDriverPlugin/TeradataAsyncConnection.swift index ef3166e4c1..ea42a44623 100644 --- a/apple/Plugins/TeradataDriverPlugin/TeradataAsyncConnection.swift +++ b/apple/Plugins/TeradataDriverPlugin/TeradataAsyncConnection.swift @@ -4,6 +4,8 @@ import TableProTeradataCore final class TeradataAsyncConnection: @unchecked Sendable { private let connection: TeradataConnection private let queue = DispatchQueue(label: "com.TablePro.teradata.connection") + private let runningLock = NSLock() + private var running: TeradataAsyncCancellation? init(config: TeradataConnectionConfig) { connection = TeradataConnection(config: config) @@ -41,6 +43,13 @@ final class TeradataAsyncConnection: @unchecked Sendable { return try await withTaskCancellationHandler { try await withCheckedThrowingContinuation { continuation in queue.async { + // Begun before the call is marked running, so a Stop either side of the mark reaches it. + self.connection.beginOperation() + self.runningLock.withLock { self.running = cancellation } + defer { + self.runningLock.withLock { self.running = nil } + self.connection.endOperation() + } do { try cancellation.throwIfCancelled() let value = try body(self.connection) @@ -53,9 +62,12 @@ final class TeradataAsyncConnection: @unchecked Sendable { } } onCancel: { cancellation.cancel() - // TeradataConnection snapshots its transport under a lock; the transport's cancel - // path uses shutdown rather than disconnecting concurrently with protocol cleanup. - connection.cancel() + // A call still queued never starts; stopping the connection would stop the request of the one running. + // Held across the stop, so the call cannot end and hand the session to the next one meanwhile. + runningLock.withLock { + guard running === cancellation else { return } + connection.cancel() + } } } } diff --git a/apple/Plugins/XLSXExportPlugin/XLSXExportPlugin.swift b/apple/Plugins/XLSXExportPlugin/XLSXExportPlugin.swift index 63a467ef35..f773973f4a 100644 --- a/apple/Plugins/XLSXExportPlugin/XLSXExportPlugin.swift +++ b/apple/Plugins/XLSXExportPlugin/XLSXExportPlugin.swift @@ -43,6 +43,8 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug destination: URL, progress: PluginExportProgress ) async throws -> ExportFormatResult { + // One plugin instance serves every window, so a dialog in another window can change `settings` mid-export. + let options = settings let writer = XLSXWriter(databaseTypeId: dataSource.databaseTypeId) var didSplitSheets = false @@ -56,7 +58,7 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug var currentSheetRowCount = 0 var columns: [String] = [] var columnTypeNames: [String] = [] - let headerRowCount = settings.includeHeaderRow ? 1 : 0 + let headerRowCount = options.includeHeaderRow ? 1 : 0 let stream = dataSource.streamRows(for: table) for try await element in stream { @@ -70,8 +72,8 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug name: table.name, columns: columns, columnTypeNames: columnTypeNames, - includeHeader: settings.includeHeaderRow, - convertNullToEmpty: settings.convertNullToEmpty + includeHeader: options.includeHeaderRow, + convertNullToEmpty: options.convertNullToEmpty ) currentSheetRowCount = headerRowCount isFirstBatch = false @@ -83,7 +85,7 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug let remaining = Self.maxRowsPerSheet - currentSheetRowCount if rowBatch.count <= remaining { try autoreleasepool { - try writer.addRows(rowBatch, convertNullToEmpty: settings.convertNullToEmpty) + try writer.addRows(rowBatch, convertNullToEmpty: options.convertNullToEmpty) } currentSheetRowCount += rowBatch.count } else { @@ -91,7 +93,7 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug let overflow = Array(rowBatch.dropFirst(remaining)) if !fitting.isEmpty { try autoreleasepool { - try writer.addRows(fitting, convertNullToEmpty: settings.convertNullToEmpty) + try writer.addRows(fitting, convertNullToEmpty: options.convertNullToEmpty) } currentSheetRowCount += fitting.count } @@ -99,14 +101,14 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug baseName: table.name, columns: columns, columnTypeNames: columnTypeNames, - includeHeader: settings.includeHeaderRow, - convertNullToEmpty: settings.convertNullToEmpty + includeHeader: options.includeHeaderRow, + convertNullToEmpty: options.convertNullToEmpty ) didSplitSheets = true currentSheetRowCount = headerRowCount if !overflow.isEmpty { try autoreleasepool { - try writer.addRows(overflow, convertNullToEmpty: settings.convertNullToEmpty) + try writer.addRows(overflow, convertNullToEmpty: options.convertNullToEmpty) } currentSheetRowCount += overflow.count } @@ -124,7 +126,7 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug let remaining = Self.maxRowsPerSheet - currentSheetRowCount if rowBatch.count <= remaining { try autoreleasepool { - try writer.addRows(rowBatch, convertNullToEmpty: settings.convertNullToEmpty) + try writer.addRows(rowBatch, convertNullToEmpty: options.convertNullToEmpty) } currentSheetRowCount += rowBatch.count } else { @@ -132,21 +134,21 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug let overflow = Array(rowBatch.dropFirst(remaining)) if !fitting.isEmpty { try autoreleasepool { - try writer.addRows(fitting, convertNullToEmpty: settings.convertNullToEmpty) + try writer.addRows(fitting, convertNullToEmpty: options.convertNullToEmpty) } } try writer.continueSheet( baseName: table.name, columns: columns, columnTypeNames: columnTypeNames, - includeHeader: settings.includeHeaderRow, - convertNullToEmpty: settings.convertNullToEmpty + includeHeader: options.includeHeaderRow, + convertNullToEmpty: options.convertNullToEmpty ) didSplitSheets = true currentSheetRowCount = headerRowCount if !overflow.isEmpty { try autoreleasepool { - try writer.addRows(overflow, convertNullToEmpty: settings.convertNullToEmpty) + try writer.addRows(overflow, convertNullToEmpty: options.convertNullToEmpty) } currentSheetRowCount += overflow.count } @@ -163,7 +165,7 @@ final class XLSXExportPlugin: ObservableObject, ExportFormatPlugin, SettablePlug name: table.name, columns: [], includeHeader: false, - convertNullToEmpty: settings.convertNullToEmpty + convertNullToEmpty: options.convertNullToEmpty ) try writer.finishSheet() } diff --git a/apple/Plugins/XLSXExportPlugin/XLSXWriter.swift b/apple/Plugins/XLSXExportPlugin/XLSXWriter.swift index c404199eac..cdb19eb612 100644 --- a/apple/Plugins/XLSXExportPlugin/XLSXWriter.swift +++ b/apple/Plugins/XLSXExportPlugin/XLSXWriter.swift @@ -46,66 +46,7 @@ final class XLSXWriter { case empty } - enum ColumnNumbers: Equatable { - case text - case exact - case approximate - case untyped - - init(typeName: String, bitsAreStrings: Bool = false) { - let base = Self.baseName(typeName) - if base.isEmpty { - self = .untyped - } else if Self.bitTypes.contains(base) { - self = bitsAreStrings ? .text : .exact - } else if Self.approximateTypes.contains(base) { - self = .approximate - } else if Self.exactTypes.contains(base) { - self = .exact - } else if Self.textTypes.contains(base) || Self.textMarkers.contains(where: { base.contains($0) }) { - self = .text - } else { - self = .untyped - } - } - - // `Nullable(UInt64)` and `LowCardinality(Nullable(Int32))` name the type inside. The base is the - // first word without its width: `int4`, `Int64` and `INT UNSIGNED` are all `int`. - private static func baseName(_ typeName: String) -> String { - var name = typeName.trimmingCharacters(in: .whitespacesAndNewlines).lowercased() - while name.hasSuffix(")"), let open = name.firstIndex(of: "("), - ["nullable", "lowcardinality"].contains(String(name[.. = [ - "int", "uint", "integer", "bigint", "smallint", "tinyint", "mediumint", "hugeint", - "uinteger", "ubigint", "usmallint", "utinyint", "uhugeint", "decimal", "dec", "numeric", - "number", "bignumeric", "serial", "bigserial", "smallserial", "money", "smallmoney", "year", "oid" - ] - - private static let approximateTypes: Set = [ - "float", "double", "real", "binary_float", "binary_double" - ] - - private static let textTypes: Set = ["enum", "set", "json", "jsonb", "name"] - - private static let bitTypes: Set = ["bit", "varbit"] - - // PostgreSQL's `bit(n)` is a string of 0 and 1. SQL Server's is a flag and MySQL's reads as a number. - static func bitsAreStrings(databaseTypeId: String) -> Bool { - enginesWithBitStrings.contains(databaseTypeId) - } - - private static let enginesWithBitStrings: Set = [ - "PostgreSQL", "Redshift", "CockroachDB", "PGlite", "AlloyDB", "Citus", "Greenplum", "KingbaseES" - ] - - private static let textMarkers = ["char", "text", "string", "clob"] - } + typealias ColumnNumbers = PluginColumnNumbers // Excel keeps 15 significant digits of a number. private static let maximumExactDigits = 15 @@ -225,6 +166,8 @@ final class XLSXWriter { let isPlain = literal == text && !literal.contains("e") let digitCount = literal.filter { $0.isNumber }.count return isPlain && digitCount <= maximumExactDigits ? .number(literal) : .string(text) + @unknown default: + return .string(text) } } diff --git a/apple/Plugins/XLSXImportPlugin/XLSXImportPlugin.swift b/apple/Plugins/XLSXImportPlugin/XLSXImportPlugin.swift index 6f7592ab3a..d74bafbfba 100644 --- a/apple/Plugins/XLSXImportPlugin/XLSXImportPlugin.swift +++ b/apple/Plugins/XLSXImportPlugin/XLSXImportPlugin.swift @@ -39,13 +39,13 @@ final class XLSXImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlug var fieldDetectionSignature: String { settings.detectionSignature } - var rowImportConfiguration: RowImportRunner.Configuration? { runConfiguration } + var rowImportConfiguration: RowImportRunner.Configuration? { Self.runConfiguration(settings) } - private var runConfiguration: RowImportRunner.Configuration { + private static func runConfiguration(_ options: XLSXImportOptions) -> RowImportRunner.Configuration { RowImportRunner.Configuration( - errorHandling: settings.errorHandling, - wrapInTransaction: settings.wrapInTransaction, - deleteExistingRows: settings.deleteExistingRows + errorHandling: options.errorHandling, + wrapInTransaction: options.wrapInTransaction, + deleteExistingRows: options.deleteExistingRows ) } @@ -78,11 +78,13 @@ final class XLSXImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlug progress: PluginImportProgress ) async throws -> PluginImportResult { let started = Date() + // One plugin instance serves every window, so a dialog in another window can change `settings` mid-import. + let options = settings let sheet: XLSXImportSheet do { sheet = try XLSXImportSheet.read( at: source.fileURL(), - hasHeaderRow: settings.hasHeaderRow, + hasHeaderRow: options.hasHeaderRow, isCancelled: { progress.isCancelled } ) } catch is TabularCancellation { @@ -96,7 +98,7 @@ final class XLSXImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlug progress.setEstimatedTotal(max(1, sheet.rowCount)) var cursor = 0 let outcome = try await RowImportRunner.run( - configuration: runConfiguration, + configuration: Self.runConfiguration(options), sink: sink, progress: progress ) { @@ -108,7 +110,7 @@ final class XLSXImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlug if filled.count < rows.count { progress.incrementStatement(by: rows.count - filled.count) } - return filled.map { (line: $0.line, row: self.row($0.values, header: header)) } + return filled.map { (line: $0.line, row: Self.row($0.values, header: header, options: options)) } } return PluginImportResult( @@ -121,7 +123,7 @@ final class XLSXImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlug // MARK: - Private - private func row(_ values: [String?], header: [String]) -> [String: PluginCellValue] { + private static func row(_ values: [String?], header: [String], options: XLSXImportOptions) -> [String: PluginCellValue] { var mapped: [String: PluginCellValue] = [:] for (index, column) in header.enumerated() { let raw = values[safe: index] ?? nil @@ -129,10 +131,10 @@ final class XLSXImportPlugin: ObservableObject, ImportFormatPlugin, SettablePlug mapped[column] = .null continue } - let trimmed = settings.trimWhitespace + let trimmed = options.trimWhitespace ? raw.trimmingCharacters(in: .whitespacesAndNewlines) : raw - if settings.emptyAsNull, trimmed.isEmpty { + if options.emptyAsNull, trimmed.isEmpty { mapped[column] = .null } else { mapped[column] = .text(trimmed) diff --git a/apple/TablePro/Core/ChangeTracking/DataChangeModels.swift b/apple/TablePro/Core/ChangeTracking/DataChangeModels.swift index 7a795ff87d..f86e46a25c 100644 --- a/apple/TablePro/Core/ChangeTracking/DataChangeModels.swift +++ b/apple/TablePro/Core/ChangeTracking/DataChangeModels.swift @@ -49,8 +49,8 @@ struct CellChange: Identifiable, Equatable { let oldIsAbsent: Bool let newIsAbsent: Bool let oldIsExpression: Bool - /// The new value is `.text` holding SQL from `CellExpressionVocabulary`, written unquoted. Never set with - /// `newIsAbsent`. + /// The new value is `.text` holding SQL from `CellExpressionVocabulary`, or `PluginCellValue.columnDefault`, + /// written unquoted. Never set with `newIsAbsent`. let newIsExpression: Bool init( @@ -92,8 +92,8 @@ struct RowChange: Identifiable, Equatable { /// stands for an insert. Empty on every engine that cannot tell a missing field from NULL. var absentColumns: Set - /// An insert's cells that hold a SQL expression, as the row stands. An update keeps its own on each - /// `CellChange`. + /// An insert's cells that hold a SQL expression or the column default, as the row stands. An update keeps its own + /// on each `CellChange`. var expressionColumns: Set /// The order the user made this change in. diff --git a/apple/TablePro/Core/ChangeTracking/DefaultedCells.swift b/apple/TablePro/Core/ChangeTracking/DefaultedCells.swift new file mode 100644 index 0000000000..bdbb48fc6b --- /dev/null +++ b/apple/TablePro/Core/ChangeTracking/DefaultedCells.swift @@ -0,0 +1,38 @@ +// +// DefaultedCells.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +// The cells a pending insert or edit set to the column's default, by row and data index. Only those rows can hold one. +enum DefaultedCells { + static func marked( + in rows: TableRows, + rowIDs: Set, + expressionColumns: (RowID) -> Set + ) -> [RowID: Set] { + var cells: [RowID: Set] = [:] + for rowID in rowIDs { + guard let row = rows.row(withID: rowID) else { continue } + let defaulted = expressionColumns(rowID).filter { + row.values.indices.contains($0) && row.values[$0].isColumnDefault(isExpression: true) + } + if !defaulted.isEmpty { cells[rowID] = defaulted } + } + return cells + } +} + +extension ChangeManaging { + func defaultedCells(in rows: TableRows) -> [RowID: Set] { + DefaultedCells.marked(in: rows, rowIDs: insertedRowIDs.union(modifiedCells.keys)) { expressionColumns(forRow: $0) } + } +} + +extension AnyChangeManager { + func defaultedCells(in rows: TableRows) -> [RowID: Set] { + DefaultedCells.marked(in: rows, rowIDs: insertedRowIDs.union(modifiedCells.keys)) { expressionColumns(forRow: $0) } + } +} diff --git a/apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift b/apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift index 17f1871fa9..21df0ea117 100644 --- a/apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift +++ b/apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift @@ -257,7 +257,8 @@ struct SQLStatementGenerator { var bindParameters: [Any?] = [] for (index, value) in values.enumerated() { - if case .text(let s) = value, s == "__DEFAULT__" { continue } + // Left out rather than written as DEFAULT, which SQLite does not take in VALUES. + if value.isColumnDefault(isExpression: expressionColumns.contains(index)) { continue } guard index < columns.count else { continue } let columnName = columns[index] @@ -383,7 +384,7 @@ struct SQLStatementGenerator { guard !change.cellChanges.isEmpty else { return nil } let nonDefaultChanges = change.cellChanges.filter { - $0.newValue != .text("__DEFAULT__") && !generatedColumns.contains($0.columnName) + !$0.newValue.isColumnDefault(isExpression: $0.newIsExpression) && !generatedColumns.contains($0.columnName) } guard !nonDefaultChanges.isEmpty else { return nil } @@ -419,8 +420,6 @@ struct SQLStatementGenerator { var parameters: [Any?] = [] let setClauses = writableChanges.map { cellChange -> String in switch cellChange.newValue { - case .text(let s) where s == "__DEFAULT__": - return "\(quoteIdentifierFn(cellChange.columnName)) = DEFAULT" case .text(let expression) where cellChange.newIsExpression: return "\(quoteIdentifierFn(cellChange.columnName)) = \(expression)" default: diff --git a/apple/TablePro/Core/Compare/CompareSQLLiteral.swift b/apple/TablePro/Core/Compare/CompareSQLLiteral.swift index 24cc1a74a8..40fc624f42 100644 --- a/apple/TablePro/Core/Compare/CompareSQLLiteral.swift +++ b/apple/TablePro/Core/Compare/CompareSQLLiteral.swift @@ -77,17 +77,20 @@ internal enum CompareSQLLiteral { return SQLStringLiteralPrefix.forDatabaseType(databaseType) + literal } + // Shared by Compare & Sync and Copy as SQL. `SQLExportBinaryLiteral` in the SQL export plugin holds the + // measurements behind each spelling; the plugin cannot link app code, so it keeps its own copy. internal static func binaryLiteral(for data: Data, databaseType: DatabaseType) -> String? { - let hex = data.map { String(format: "%02X", $0) }.joined() + let hex = data.uppercaseHex() switch binaryStyle(for: databaseType) { case .bitString: return "X'\(hex)'" case .postgresBytea: - return "'\\x\(hex.lowercased())'::bytea" + return postgresFamilyBinaryLiteral(hex: hex, databaseType: databaseType) case .zeroX: return "0x\(hex)" case .hexToRaw: - return "HEXTORAW('\(hex)')" + // `HEXTORAW('')` stores NULL, which a `BLOB NOT NULL` refuses. + return data.isEmpty ? "EMPTY_BLOB()" : "HEXTORAW('\(hex)')" case .unhexFunction: return "unhex('\(hex)')" case .unknown: @@ -95,6 +98,19 @@ internal enum CompareSQLLiteral { } } + // `'\x…'::bytea` follows `standard_conforming_strings`: with it off the server stores the text's bytes. KingbaseES + // documents `decode` only as Oracle's DECODE, so it keeps the bytea literal; Redshift has no bytea and reads VARBYTE. + private static func postgresFamilyBinaryLiteral(hex: String, databaseType: DatabaseType) -> String { + switch databaseType { + case .kingbasees: + return "'\\x\(hex)'::bytea" + case .redshift: + return "from_hex('\(hex)')" + default: + return "decode('\(hex)', 'hex')" + } + } + internal enum BinaryStyle { case bitString case postgresBytea diff --git a/apple/TablePro/Core/Compare/DataDiffEngine.swift b/apple/TablePro/Core/Compare/DataDiffEngine.swift index 882c746b62..df96ff5b92 100644 --- a/apple/TablePro/Core/Compare/DataDiffEngine.swift +++ b/apple/TablePro/Core/Compare/DataDiffEngine.swift @@ -30,10 +30,16 @@ internal struct DataRow: Hashable, Sendable { internal protocol DataRowProviding: AnyObject { var endedAtRowLimit: Bool { get } + // The limit the driver stopped this side's stream at, once it has ended there. + var driverRowCut: Int? { get } func nextRow() async throws -> DataRow? func drain() async throws } +internal extension DataRowProviding { + var driverRowCut: Int? { nil } +} + internal protocol OneSidedRowResolving: AnyObject { func rows(matching keys: [[PluginCellValue]], on side: ComparisonSide) async throws -> [DataRow] } @@ -111,6 +117,10 @@ internal struct DataDiffSummary: Hashable, Sendable { /// as `id > ` resumes from without skipping a key or comparing one twice. internal let resumeKey: [PluginCellValue]? + // The limit a driver stopped a side's stream at, so the answer covers only the rows read before it. + internal let sourceRowCut: Int? + internal let targetRowCut: Int? + /// One comparison's own identity. Two runs that produce the same counts and the same digest are /// still two answers, and a view that caches on the answer has to be able to tell them apart: /// matching rows are counted rather than digested, so both can move while the digest stands. @@ -130,6 +140,8 @@ internal struct DataDiffSummary: Hashable, Sendable { stoppedAtRowLimit: Bool = false, differenceDigest: String = "", resumeKey: [PluginCellValue]? = nil, + sourceRowCut: Int? = nil, + targetRowCut: Int? = nil, runIdentity: UUID = UUID() ) { self.insertCount = insertCount @@ -145,6 +157,8 @@ internal struct DataDiffSummary: Hashable, Sendable { self.stoppedAtRowLimit = stoppedAtRowLimit self.differenceDigest = differenceDigest self.resumeKey = resumeKey + self.sourceRowCut = sourceRowCut + self.targetRowCut = targetRowCut self.runIdentity = runIdentity } @@ -359,7 +373,9 @@ internal struct DataDiffEngine { return accumulator.summary( comparedKeyCount: positions, stoppedAtRowLimit: stoppedAtRowLimit, - resumeKey: stoppedAtRowLimit ? lastClassifiedKey : nil + resumeKey: stoppedAtRowLimit ? lastClassifiedKey : nil, + sourceRowCut: source.driverRowCut, + targetRowCut: target.driverRowCut ) } @@ -536,7 +552,9 @@ internal extension DataDiffEngine { func summary( comparedKeyCount: Int, stoppedAtRowLimit: Bool, - resumeKey: [PluginCellValue]? = nil + resumeKey: [PluginCellValue]? = nil, + sourceRowCut: Int? = nil, + targetRowCut: Int? = nil ) -> DataDiffSummary { DataDiffSummary( insertCount: counts[.insert] ?? 0, @@ -551,7 +569,9 @@ internal extension DataDiffEngine { comparedKeyCount: comparedKeyCount, stoppedAtRowLimit: stoppedAtRowLimit, differenceDigest: digest.finalize().map { String(format: "%02x", $0) }.joined(), - resumeKey: resumeKey + resumeKey: resumeKey, + sourceRowCut: sourceRowCut, + targetRowCut: targetRowCut ) } diff --git a/apple/TablePro/Core/Compare/StreamingRowProvider.swift b/apple/TablePro/Core/Compare/StreamingRowProvider.swift index 979cabe6fb..54a4ac107f 100644 --- a/apple/TablePro/Core/Compare/StreamingRowProvider.swift +++ b/apple/TablePro/Core/Compare/StreamingRowProvider.swift @@ -13,6 +13,7 @@ // import Foundation +import os import TableProPluginKit internal struct RowReadContext: Sendable { @@ -55,6 +56,7 @@ internal final class StreamingRowProvider: DataRowProviding { private var deliveredCount = 0 private var isFinished = false private var sawRowPastTheLimit = false + private let rowCut: OSAllocatedUnfairLock internal init( stream: AsyncThrowingStream, @@ -62,17 +64,25 @@ internal final class StreamingRowProvider: DataRowProviding { rowLimit: Int? = nil, context: RowReadContext? = nil ) { - self.iterator = stream.makeAsyncIterator() + let rowCut = OSAllocatedUnfairLock(initialState: nil) + self.rowCut = rowCut + self.iterator = ExportRowCut.reporting(stream, engineCeiling: nil) { limit in + rowCut.withLock { $0 = limit } + }.makeAsyncIterator() self.columns = columns self.rowLimit = rowLimit self.context = context } - /// The read asks for one row more than the limit and never hands that row out, so this is the - /// server's own answer to whether anything was left rather than a guess from the row count: a - /// table holding exactly the limit is not a truncated read. + // The read asks for one row past the limit, so a table holding exactly the limit is not a cut read. A stream the + // driver stopped at a limit it declares ends here too: a key only the other side holds was never read on this one. internal var endedAtRowLimit: Bool { - sawRowPastTheLimit + sawRowPastTheLimit || driverRowCut != nil + } + + internal var driverRowCut: Int? { + guard isFinished else { return nil } + return rowCut.withLock { $0 } } internal func nextRow() async throws -> DataRow? { diff --git a/apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift b/apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift index ec3657620b..9cf2f91330 100644 --- a/apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift +++ b/apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift @@ -218,7 +218,7 @@ final class RowEditingCoordinator: ObservableObject { private static func sampleText(atColumn index: Int, in tableRows: TableRows) -> String? { for row in tableRows.rows where row.values.indices.contains(index) { - if case .text(let text) = row.values[index], text != "__DEFAULT__" { return text } + if case .text(let text) = row.values[index], row.values[index] != .columnDefault { return text } } return nil } @@ -430,10 +430,12 @@ final class RowEditingCoordinator: ObservableObject { self?.copyRowsAsJson(rowIDs) }) else { return } let model = parent.gridModel(forTab: tab.id) + let tableRows = parent.tabSessionRegistry.tableRows(for: tab.id) let output = ResultJsonSerializer.serialize( - tableRows: parent.tabSessionRegistry.tableRows(for: tab.id), + tableRows: tableRows, displayIDs: model.displayedRowIDs, selectedDisplayIndices: Set(rowIDs.compactMap { model.displayIndex(of: $0) }), + defaultedCells: parent.changeManager.defaultedCells(in: tableRows), columns: VisibleColumnProjection(indices: presentedColumnIndices(tabId: tab.id)) ) guard output.rowCount > 0 else { return } diff --git a/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift b/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift index a4c255ea3b..3686d2ca50 100644 --- a/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift +++ b/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift @@ -134,8 +134,9 @@ struct RewindExecutor { ) } + // In the order the restore ran them, since undoing the restore runs its record backwards. static func inverseOperations(of plan: RewindPlan) -> [RowWriteOperation] { - plan.rows.filter { $0.outcome.restores }.map { row -> RowWriteOperation in + plan.rows.filter { $0.outcome.restores }.reversed().map { row -> RowWriteOperation in let source = row.operation let kind = source.kind.inverted // The same stored refusal a save through a view writes, so a build without the view flag refuses too. diff --git a/apple/TablePro/Core/DataWrite/Rewind/RewindPlanner.swift b/apple/TablePro/Core/DataWrite/Rewind/RewindPlanner.swift index 0438224191..361fb6bc40 100644 --- a/apple/TablePro/Core/DataWrite/Rewind/RewindPlanner.swift +++ b/apple/TablePro/Core/DataWrite/Rewind/RewindPlanner.swift @@ -82,6 +82,7 @@ struct RewindPlanner { currentRow = byLocator[Array(locator.utf8)] } else { currentRow = primaryKey(of: operation).flatMap { current[$0] } + ?? rowRestoredUnderKeyBefore(operation, in: current) } let outcome = classify(operation, current: currentRow) rows.append( @@ -135,7 +136,7 @@ struct RewindPlanner { } } - /// Whether the row holds what the image says on every column the save wrote: the same value, + /// Whether the row holds what the image says on every compared column: the same value, /// and the field there or missing as it was. Set NULL on a missing field and Remove Field on a /// NULL one leave the value alone, so comparing values alone reads either as already restored. /// @@ -147,7 +148,7 @@ struct RewindPlanner { absent expectedAbsent: Set?, on operation: RowWriteOperation ) -> Bool { - operation.writtenColumns.allSatisfy { column in + comparedColumns(of: operation).allSatisfy { column in guard let index = operation.columns.firstIndex(of: column), index < current.values.count, index < expected.count, current.values[index] == expected[index] @@ -157,6 +158,14 @@ struct RewindPlanner { } } + // A row found by a key the save wrote holds that key whatever row it is, so a moved row is compared on every + // column the server does not compute; a trigger's stamp then reads as changed, which leaves the row alone. + private func comparedColumns(of operation: RowWriteOperation) -> [String] { + guard keyBeforeMove(of: operation) != nil else { return operation.writtenColumns } + let generated = Set(record.generatedColumns) + return operation.columns.filter { !generated.contains($0) } + } + // MARK: - Reading private func indexByKey(_ rows: [RewindCurrentRow]) -> [RewindRowKey: RewindCurrentRow] { @@ -194,9 +203,11 @@ struct RewindPlanner { guard !keyColumns.isEmpty else { return [] } if keyColumns.count == 1, let keyColumn = keyColumns.first { - let filters = operations.compactMap { operation -> TableFilter? in - guard let value = keyValue(of: operation, column: keyColumn) else { return nil } - return TableFilter(columnName: keyColumn, filterOperator: .equal, value: value) + let filters = operations.flatMap { operation in + readImages(of: operation).compactMap { image -> TableFilter? in + guard let value = keyValue(in: image, of: operation, column: keyColumn) else { return nil } + return TableFilter(columnName: keyColumn, filterOperator: .equal, value: value) + } } guard !filters.isEmpty, let query = queryBuilder.buildFilteredQuery( @@ -211,21 +222,23 @@ struct RewindPlanner { return [query] } - return operations.compactMap { operation in - let filters = keyColumns.compactMap { column -> TableFilter? in - guard let value = keyValue(of: operation, column: column) else { return nil } - return TableFilter(columnName: column, filterOperator: .equal, value: value) + return operations.flatMap { operation in + readImages(of: operation).compactMap { image -> String? in + let filters = keyColumns.compactMap { column -> TableFilter? in + guard let value = keyValue(in: image, of: operation, column: column) else { return nil } + return TableFilter(columnName: column, filterOperator: .equal, value: value) + } + guard filters.count == keyColumns.count else { return nil } + return queryBuilder.buildFilteredQuery( + tableName: record.target.table, + schemaName: record.target.schema, + filters: filters, + logicMode: .and, + columns: columns, + selectColumns: columns, + limit: 1 + ) } - guard filters.count == keyColumns.count else { return nil } - return queryBuilder.buildFilteredQuery( - tableName: record.target.table, - schemaName: record.target.schema, - filters: filters, - logicMode: .and, - columns: columns, - selectColumns: columns, - limit: 1 - ) } } @@ -293,12 +306,40 @@ struct RewindPlanner { // MARK: - Keys + // Where the save left the row, or for a delete where it was: an edit that rewrote the key moved it. + private func savedImage(of operation: RowWriteOperation) -> [PluginCellValue]? { + operation.postImage ?? operation.preImage + } + private func primaryKey(of operation: RowWriteOperation) -> RewindRowKey? { - let source = operation.preImage ?? operation.postImage - guard let source else { return nil } + guard let source = savedImage(of: operation) else { return nil } return primaryKey(of: source, columns: operation.columns, keyColumns: operation.primaryKeyColumns) } + private func keyBeforeMove(of operation: RowWriteOperation) -> RewindRowKey? { + guard operation.kind == .update, let preImage = operation.preImage else { return nil } + let before = primaryKey(of: preImage, columns: operation.columns, keyColumns: operation.primaryKeyColumns) + return before == primaryKey(of: operation) ? nil : before + } + + // A rewind that already ran put a moved row back under its old key, where it reads as restored. + private func readImages(of operation: RowWriteOperation) -> [[PluginCellValue]] { + guard let saved = savedImage(of: operation) else { return [] } + guard keyBeforeMove(of: operation) != nil, let preImage = operation.preImage else { return [saved] } + return [saved, preImage] + } + + // Only a row holding the old values, so another row that took the old key since is not mistaken for this one. + private func rowRestoredUnderKeyBefore( + _ operation: RowWriteOperation, + in current: [RewindRowKey: RewindCurrentRow] + ) -> RewindCurrentRow? { + guard let key = keyBeforeMove(of: operation), let row = current[key], let preImage = operation.preImage, + matches(row, preImage, absent: operation.preImageAbsentColumns, on: operation) + else { return nil } + return row + } + private func primaryKey( of row: [PluginCellValue], columns: [String], @@ -319,16 +360,17 @@ struct RewindPlanner { /// compare against, and a delete would be reported as restorable with no conflict check at all. private func keyIsComparable(_ operation: RowWriteOperation) -> Bool { !operation.primaryKeyColumns.isEmpty - && operation.primaryKeyColumns.allSatisfy { keyValue(of: operation, column: $0) != nil } + && operation.primaryKeyColumns.allSatisfy { + keyValue(in: savedImage(of: operation), of: operation, column: $0) != nil + } } - private func keyValue(of operation: RowWriteOperation, column: String) -> String? { - let source = operation.preImage ?? operation.postImage - guard let source, + private func keyValue(in image: [PluginCellValue]?, of operation: RowWriteOperation, column: String) -> String? { + guard let image, let index = operation.columns.firstIndex(of: column), - index < source.count + index < image.count else { return nil } - return source[index].asText + return image[index].asText } } diff --git a/apple/TablePro/Core/DataWrite/RowChangeStatementFactory.swift b/apple/TablePro/Core/DataWrite/RowChangeStatementFactory.swift index 968b67c8df..19517845a3 100644 --- a/apple/TablePro/Core/DataWrite/RowChangeStatementFactory.swift +++ b/apple/TablePro/Core/DataWrite/RowChangeStatementFactory.swift @@ -146,7 +146,7 @@ struct RowChangeStatementFactory { try refuseUnwritableExpressions(in: changes, insertedRowData: insertedRowData) try refuseMisdimensionedVectors(in: changes, insertedRowData: insertedRowData) let insertedRowData = insertedRowData.mapValues(leavingServerOwnedColumnsToTheServer) - let changes = changes.map(droppingServerOwnedExpressions) + let changes = changes.map { markingServerOwnedDefaults($0, in: insertedRowData) } if let driverStatements = try pluginRowWrites( for: changes, insertedRowData: insertedRowData, @@ -190,7 +190,9 @@ struct RowChangeStatementFactory { private func refuseDefaultOnUpdate(in changes: [RowChange]) throws { guard !CellExpressionVocabulary.updateTakesDefault(databaseType) else { return } for change in changes where change.type == .update { - guard let defaulted = change.cellChanges.first(where: { $0.newValue.isDefaultMarker }) else { continue } + guard let defaulted = change.cellChanges.first(where: { + $0.newValue.isColumnDefault(isExpression: $0.newIsExpression) + }) else { continue } throw DataWriteError.changeRefused( table: tableName, kind: .update, @@ -209,9 +211,10 @@ struct RowChangeStatementFactory { in changes: [RowChange], insertedRowData: [RowID: [PluginCellValue]] ) throws { + // A default reaches a driver in the spelling it reads, so only SQL proper has to be SQL this engine runs. let marked = changes.flatMap { change in Self.markedCells(of: change, insertedRowData: insertedRowData, columns: columns) - } + }.filter { $0.value != .columnDefault } guard !marked.isEmpty else { return } for cell in marked { guard case .text(let sql) = cell.value, CellExpressionVocabulary.isCanonical(sql, for: databaseType) @@ -309,21 +312,58 @@ struct RowChangeStatementFactory { guard !generatedColumns.isEmpty else { return values } return values.enumerated().map { index, value in guard columns.indices.contains(index), generatedColumns.contains(columns[index]) else { return value } - return Self.defaultMarker + return .columnDefault } } - /// The cell now holds the default marker, so it is no longer SQL to write. - private func droppingServerOwnedExpressions(_ change: RowChange) -> RowChange { - guard change.type == .insert, !change.expressionColumns.isEmpty, !generatedColumns.isEmpty else { return change } - var kept = change - kept.expressionColumns = change.expressionColumns.filter { - !(columns.indices.contains($0) && generatedColumns.contains(columns[$0])) + // Whatever SQL the cell held, it now holds the default, which only the mark makes one. + private func markingServerOwnedDefaults(_ change: RowChange, in insertedRowData: [RowID: [PluginCellValue]]) -> RowChange { + guard change.type == .insert, !generatedColumns.isEmpty else { return change } + let owned = Set(columns.indices.filter { generatedColumns.contains(columns[$0]) }) + var marked = change + marked.expressionColumns.subtract(owned) + if let values = insertedRowData[change.rowID] { + marked.expressionColumns.formUnion(owned.filter { values.indices.contains($0) }) } - return kept + return marked } - private static let defaultMarker = PluginCellValue.text("__DEFAULT__") + // A driver reads its own spelling for a default, not the mark, so a default reaches it spelled that way and unmarked. + private static func spellingDefaultsForDrivers( + _ changes: [RowChange], + insertedRowData: [RowID: [PluginCellValue]] + ) -> (changes: [RowChange], insertedRowData: [RowID: [PluginCellValue]]) { + var rows = insertedRowData + let spelledChanges = changes.map { change -> RowChange in + var spelled = change + if change.type == .insert, var values = rows[change.rowID] { + for index in change.expressionColumns where values.indices.contains(index) && values[index] == .columnDefault { + values[index] = .pluginDefaultMarker + spelled.expressionColumns.remove(index) + } + rows[change.rowID] = values + } + let defaulted = change.cellChanges.filter { $0.newValue.isColumnDefault(isExpression: $0.newIsExpression) } + guard !defaulted.isEmpty else { return spelled } + spelled.cellChanges = change.cellChanges.map { cell in + guard cell.newValue.isColumnDefault(isExpression: cell.newIsExpression) else { return cell } + return CellChange( + columnIndex: cell.columnIndex, + columnName: cell.columnName, + oldValue: cell.oldValue, + newValue: .pluginDefaultMarker, + oldIsAbsent: cell.oldIsAbsent, + newIsAbsent: cell.newIsAbsent, + oldIsExpression: cell.oldIsExpression + ) + } + if change.type == .insert, rows[change.rowID] == nil { + spelled.expressionColumns.subtract(defaulted.map(\.columnIndex)) + } + return spelled + } + return (spelledChanges, rows) + } private func rowWriteContext(for keyed: PluginKeyedChanges) -> PluginRowWriteContext { var context = PluginRowWriteContext() @@ -386,6 +426,7 @@ struct RowChangeStatementFactory { }, context: restoreContext(rowCount: rows.count) ) { + try refuseRestoredTextDriversReadAsDefault(rows, restorable: restorable) try refuseUnfilteredWrites(restored.map { $0.statement }) return RestoreStatements(statements: restored.map { ParameterizedStatement(sql: $0.statement, parameters: $0.parameters.map(\.asAny)) @@ -489,9 +530,10 @@ struct RowChangeStatementFactory { insertedRowIDs: Set ) throws -> [DriverWrittenStatement]? { guard let pluginDriver else { return nil } + let spelled = Self.spellingDefaultsForDrivers(changes, insertedRowData: insertedRowData) let keyed = PluginKeyedChanges( - changes: changes, - insertedRowData: insertedRowData, + changes: spelled.changes, + insertedRowData: spelled.insertedRowData, deletedRowIDs: deletedRowIDs, insertedRowIDs: insertedRowIDs ) @@ -524,6 +566,7 @@ struct RowChangeStatementFactory { throw DataWriteError.changeRefused(table: tableName, kind: nil, reason: error.localizedDescription) } + try refuseTextDriversReadAsDefault(in: changes, insertedRowData: insertedRowData) try refuseUnfilteredWrites(writes.map(\.statement)) let unwritten = RowWriteCoverage.unwrittenChanges( changes, @@ -547,6 +590,46 @@ struct RowChangeStatementFactory { } } + // A driver may read the text `__DEFAULT__` as the column default, and none can say whether it does until PluginKit + // carries the mark beside the value, so the text is never handed to one as a value. + private func refuseTextDriversReadAsDefault( + in changes: [RowChange], + insertedRowData: [RowID: [PluginCellValue]] + ) throws { + for change in changes where change.type != .delete { + var written = change.cellChanges.filter { !$0.newIsExpression }.map { (column: $0.columnName, value: $0.newValue) } + if change.type == .insert, let stored = insertedRowData[change.rowID] { + written = zip(columns, stored).enumerated() + .filter { !change.expressionColumns.contains($0.offset) } + .map { (column: $0.element.0, value: $0.element.1) } + } + guard let literal = written.first(where: { $0.value == .pluginDefaultMarker }) else { continue } + throw textReadAsDefault(in: literal.column, kind: change.type == .insert ? .insert : .update) + } + } + + // A rewind puts a deleted row back with the values it held, which can be that text. + private func refuseRestoredTextDriversReadAsDefault(_ rows: [[PluginCellValue]], restorable: [Int]) throws { + for row in rows { + guard let index = restorable.first(where: { row.indices.contains($0) && row[$0] == .pluginDefaultMarker }) + else { continue } + throw textReadAsDefault(in: columns[index], kind: .insert) + } + } + + private func textReadAsDefault(in column: String, kind: RowWriteKind) -> DataWriteError { + .changeRefused( + table: tableName, + kind: kind, + reason: String( + format: String( + localized: "This database's driver may read the text __DEFAULT__ in %@ as the column default, so it cannot be saved as text." + ), + column + ) + ) + } + // The host's own generator never writes one, so only a driver's statements are read. private func refuseUnfilteredWrites(_ statements: [String]) throws { guard PluginManager.shared.editorLanguage(for: databaseType) == .sql else { return } diff --git a/apple/TablePro/Core/DataWrite/RowWriteOperationBuilder.swift b/apple/TablePro/Core/DataWrite/RowWriteOperationBuilder.swift index 457424223b..11cafebc8f 100644 --- a/apple/TablePro/Core/DataWrite/RowWriteOperationBuilder.swift +++ b/apple/TablePro/Core/DataWrite/RowWriteOperationBuilder.swift @@ -34,7 +34,8 @@ enum RowWriteOperationBuilder { writesThroughView: Bool = false, rowLocators: [RowID: String] = [:] ) -> [RowWriteOperation] { - changes.compactMap { change in + // A rewind undoes them newest first, and `changes` stops being in edit order once a change is cancelled. + changes.sorted { $0.sequence < $1.sequence }.compactMap { change in switch change.type { case .update: var operation = update( @@ -111,7 +112,7 @@ enum RowWriteOperationBuilder { ) -> RewindRefusal? { if containsTableOperation { return .destructiveTableOperation } if primaryKeyColumns.isEmpty || preImage == nil { return .noPrimaryKey } - if change.cellChanges.contains(where: { $0.newIsExpression || $0.newValue.isDefaultMarker }) { + if change.cellChanges.contains(where: \.newIsExpression) { return .serverComputedValue } if change.cellChanges.contains(where: { generatedColumns.contains($0.columnName) }) { @@ -172,11 +173,11 @@ enum RowWriteOperationBuilder { return .serverAssignedKey } let value = values[index] - if value.isNull || value.isDefaultMarker || change.expressionColumns.contains(index) { + if value.isNull || change.expressionColumns.contains(index) { return .serverAssignedKey } } - if values.contains(where: \.isDefaultMarker) || !change.expressionColumns.isEmpty { + if !change.expressionColumns.isEmpty { return .serverComputedValue } if values.contains(where: exceedsCapture) { return .valueTooLarge } diff --git a/apple/TablePro/Core/Database/DatabaseManager+ContainerDDL.swift b/apple/TablePro/Core/Database/DatabaseManager+ContainerDDL.swift index 147c37dfaf..f4550da094 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+ContainerDDL.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+ContainerDDL.swift @@ -59,10 +59,7 @@ extension DatabaseManager { } } - /// Runs a container operation the driver performs itself, with no statement to show. - /// - /// `kind` is the caller's to state because there is no text to classify: a drop is - /// `.destructiveQuery` and everything else is `.schemaMutation`. + // No statement to classify, so the caller states `kind`: a drop is destructive, anything else a schema mutation. func runContainerOperation( description: String, kind: OperationKind, @@ -70,6 +67,7 @@ extension DatabaseManager { databaseType: DatabaseType, event: CatalogEvent?, isConfirmationPreCleared: Bool = false, + gate: any ExecutionGate = ExecutionGateProvider.shared, perform: @Sendable @escaping (any DatabaseDriver) async throws -> Void ) async throws { try await authorizeContainerOperation( @@ -78,7 +76,8 @@ extension DatabaseManager { connectionId: scope.connectionId, databaseType: databaseType, description: description, - isConfirmationPreCleared: isConfirmationPreCleared + isConfirmationPreCleared: isConfirmationPreCleared, + gate: gate ) try await withScopedDriver( scope: scope, @@ -90,16 +89,14 @@ extension DatabaseManager { if let event { CatalogChangeService.post(event) } } - /// Runs a container operation the driver expressed as statements. - /// - /// The kind comes from the statements rather than from the action's name, so an edit that - /// revokes a privilege is authorized at the destructive tier the same way a drop is. + // The kind comes from the statements, so an edit that revokes a privilege is authorized like a drop. func runContainerStatements( _ plan: ContainerDDLPlan, description: String, scope: DatabaseScope, databaseType: DatabaseType, - event: CatalogEvent? + event: CatalogEvent?, + gate: any ExecutionGate = ExecutionGateProvider.shared ) async throws { let statements = plan.statements guard !statements.isEmpty else { return } @@ -113,7 +110,8 @@ extension DatabaseManager { connectionId: scope.connectionId, databaseType: databaseType, description: description, - isConfirmationPreCleared: false + isConfirmationPreCleared: false, + gate: gate ) let route = schemaChangeRoute(for: scope) @@ -139,21 +137,19 @@ extension DatabaseManager { if let event { CatalogChangeService.post(event) } } - /// `isConfirmationPreCleared` is for a caller that has already shown the user a destructive - /// confirmation of its own. Without it the sidebar's Drop dialog is followed by the gate's, - /// asking twice for one gesture and once per target for a batch. It clears the confirmation - /// only: Touch ID, the read-only refusal and the audit record all still apply. + // Pre-cleared skips only a confirmation the caller already showed; Touch ID, Read-Only and the audit still apply. private func authorizeContainerOperation( sql: String?, kind: OperationKind, connectionId: UUID, databaseType: DatabaseType, description: String, - isConfirmationPreCleared: Bool + isConfirmationPreCleared: Bool, + gate: any ExecutionGate ) async throws { var capabilities = CallerCapabilities.interactiveUser if isConfirmationPreCleared { capabilities.insert(.confirmationPreCleared) } - let decision = await ExecutionGateProvider.shared.authorize( + let decision = await gate.authorize( OperationRequest( connectionId: connectionId, databaseType: databaseType, @@ -164,11 +160,7 @@ extension DatabaseManager { operationDescription: description ) ) - guard case .authorized = decision else { - throw DatabaseError.queryFailed( - decision.deniedReason ?? String(localized: "This change was not authorized") - ) - } + if let denial = decision.denialError { throw denial } } private static func execute( diff --git a/apple/TablePro/Core/Database/DatabaseManager+Health.swift b/apple/TablePro/Core/Database/DatabaseManager+Health.swift index b42ed9eae7..3f9fc5feb2 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Health.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Health.swift @@ -149,10 +149,17 @@ extension DatabaseManager { result.driver.disconnect() return .abort } + let warning = openingWarning( + driver: result.driver, + startupFailures: result.startupFailures, + restoreWarning: result.restoreWarning, + for: connectionId + ) updateSession(connectionId) { session in session.driver = result.driver session.effectiveConnection = result.effectiveConnection session.status = .connected + session.connectionWarning = warning if let schemaDriver = result.driver as? SchemaSwitchable { session.browseSchema = schemaDriver.currentSchema } @@ -233,6 +240,8 @@ extension DatabaseManager { let driver: DatabaseDriver let effectiveConnection: DatabaseConnection let cachedPassword: String? + let startupFailures: [StartupCommandFailure] + let restoreWarning: String? } // Rebuilds the tunnel for a tunneled session before connecting the new driver. Tunnels are keyed by @@ -282,13 +291,13 @@ extension DatabaseManager { throw CancellationError() } - await applyTimeoutAndStartupCommands( + let startupFailures = await applyTimeoutAndStartupCommands( on: driver, queryTimeoutSeconds: session.effectiveQueryTimeoutSeconds, startupCommands: session.connection.startupCommands, connectionName: session.connection.name ) - await restoreSchemaAndDatabase( + let restoreWarning = await restoreSchemaAndDatabase( on: driver, savedSchema: session.browseSchema, savedDatabase: databaseSwitchRequiresReconnect(session.connection) ? nil : session.browseDatabase @@ -297,7 +306,9 @@ extension DatabaseManager { return ReconnectResult( driver: driver, effectiveConnection: connectionForDriver, - cachedPassword: connectResult.cachedPassword + cachedPassword: connectResult.cachedPassword, + startupFailures: startupFailures, + restoreWarning: restoreWarning ) } @@ -306,7 +317,7 @@ extension DatabaseManager { queryTimeoutSeconds: Int, startupCommands: String?, connectionName: String - ) async { + ) async -> [StartupCommandFailure] { do { try await driver.applyQueryTimeout(queryTimeoutSeconds) } catch { @@ -315,7 +326,7 @@ extension DatabaseManager { ) } - await executeStartupCommands(startupCommands, on: driver, connectionName: connectionName) + return await executeStartupCommands(startupCommands, on: driver, connectionName: connectionName) } private func databaseSwitchRequiresReconnect(_ connection: DatabaseConnection) -> Bool { @@ -323,17 +334,23 @@ extension DatabaseManager { .capabilities.requiresReconnectForDatabaseSwitch ?? false } + // The reconnected session stays on the server's default when a restore fails, which the window would not show. func restoreSchemaAndDatabase( on driver: DatabaseDriver, savedSchema: String?, savedDatabase: String? - ) async { + ) async -> String? { + var warnings: [String?] = [] // The schema lives inside the database, and a database switch can leave the driver on none. if let savedDatabase, let adapter = driver as? PluginDriverAdapter { do { try await adapter.switchDatabase(to: savedDatabase) } catch { Self.logger.warning("Failed to restore database '\(savedDatabase)' on reconnect: \(error.localizedDescription)") + warnings.append(String( + format: String(localized: "Reconnecting could not reopen the database %1$@: %2$@"), + savedDatabase, error.localizedDescription + )) } } @@ -342,8 +359,13 @@ extension DatabaseManager { try await schemaDriver.switchSchema(to: savedSchema) } catch { Self.logger.warning("Failed to restore schema '\(savedSchema)' on reconnect: \(error.localizedDescription)") + warnings.append(String( + format: String(localized: "Reconnecting could not reopen the schema %1$@: %2$@"), + savedSchema, error.localizedDescription + )) } } + return Self.joinedWarning(warnings) } internal func stopHealthMonitor(for connectionId: UUID) async { @@ -440,13 +462,13 @@ extension DatabaseManager { } let driver = connectResult.driver - await applyTimeoutAndStartupCommands( + let startupFailures = await applyTimeoutAndStartupCommands( on: driver, queryTimeoutSeconds: session.effectiveQueryTimeoutSeconds, startupCommands: session.connection.startupCommands, connectionName: session.connection.name ) - await restoreSchemaAndDatabase( + let restoreWarning = await restoreSchemaAndDatabase( on: driver, savedSchema: activeSessions[sessionId]?.browseSchema, savedDatabase: databaseSwitchRequiresReconnect(session.connection) ? nil : activeSessions[sessionId]?.browseDatabase @@ -456,10 +478,14 @@ extension DatabaseManager { throw CancellationError() } + let warning = openingWarning( + driver: driver, startupFailures: startupFailures, restoreWarning: restoreWarning, for: sessionId + ) updateSession(sessionId) { session in session.driver = driver session.status = .connected session.effectiveConnection = effectiveConnection + session.connectionWarning = warning if let schemaDriver = driver as? SchemaSwitchable { session.browseSchema = schemaDriver.currentSchema } diff --git a/apple/TablePro/Core/Database/DatabaseManager+Principals.swift b/apple/TablePro/Core/Database/DatabaseManager+Principals.swift index 7cf505ddcd..12680f123e 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Principals.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Principals.swift @@ -14,7 +14,8 @@ extension DatabaseManager { func executePrincipalChanges( changes: [PrincipalChange], databaseType: DatabaseType, - connectionId: UUID + connectionId: UUID, + gate: any ExecutionGate = ExecutionGateProvider.shared ) async throws { guard let driver = driver(for: connectionId) else { throw DatabaseError.notConnected @@ -39,7 +40,7 @@ extension DatabaseManager { ? .destructiveQuery : .schemaMutation - let authorization = await ExecutionGateProvider.shared.authorize( + let authorization = await gate.authorize( OperationRequest( connectionId: connectionId, databaseType: databaseType, @@ -50,12 +51,7 @@ extension DatabaseManager { operationDescription: String(localized: "Apply User and Role Changes") ) ) - guard case .authorized = authorization else { - throw DatabaseError.queryFailed( - authorization.deniedReason - ?? String(localized: "User and role changes were not authorized") - ) - } + if let denial = authorization.denialError { throw denial } /// Reported whether or not every statement ran: an engine that cannot roll principal /// statements back keeps the ones before a failure, and `DROP OWNED` among them drops objects. diff --git a/apple/TablePro/Core/Database/DatabaseManager+Schema.swift b/apple/TablePro/Core/Database/DatabaseManager+Schema.swift index fd4b884352..01ab285459 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Schema.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Schema.swift @@ -226,13 +226,7 @@ extension DatabaseManager { } } - /// Run a Create Table draft's statements, on the same isolated route and in the same shape as - /// `executeSchemaChanges`. - /// - /// The route is what matters. The view used to reach for `driver(for:)`, which is the session - /// driver, so a `BEGIN` opened here joined whatever transaction a query tab had left open and - /// the matching `COMMIT` took that tab's uncommitted work with it. `schemaChangeRoute` exists - /// for exactly that, and the app's own DDL has no business on the user's connection. + // On the schema change route: a BEGIN on the session driver would join a query tab's open transaction. func executeCreateTable( statements: [String], databaseType: DatabaseType, @@ -254,11 +248,7 @@ extension DatabaseManager { operationDescription: String(localized: "Create Table") ) ) - guard case .authorized = authorization else { - throw DatabaseError.queryFailed( - authorization.deniedReason ?? String(localized: "Operation not permitted") - ) - } + if let denial = authorization.denialError { throw denial } let executionTimes: [TimeInterval] do { diff --git a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift index 2e0bfa525e..23c6f022e6 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift @@ -136,7 +136,7 @@ extension DatabaseManager { attempt: attempt, for: connection.id ) - await applyTimeoutAndStartupCommands( + let startupFailures = await applyTimeoutAndStartupCommands( on: driver, queryTimeoutSeconds: effectiveQueryTimeoutSeconds, startupCommands: resolvedConnection.startupCommands, @@ -167,7 +167,7 @@ extension DatabaseManager { session.driver = driver session.status = driver.status session.effectiveConnection = effectiveConnection - session.connectionWarning = driver.connectionWarning + session.connectionWarning = openingWarning(driver: driver, startupFailures: startupFailures, for: connection.id) // A connect answers whatever went wrong before it, including a reconnect that gave up on this entry. session.liveness = .live session.recovery = nil @@ -1172,6 +1172,7 @@ extension DatabaseManager { monitorPingedDrivers.removeValue(forKey: connectionId) sessionDriverGate.drain(connectionId: connectionId) connectionStatusVersions.removeValue(forKey: connectionId) + reportedStartupFailures.removeValue(forKey: connectionId) forgetVerification(for: connectionId) aiAccessApprovals.revoke(connectionId) AppEvents.shared.connectionStatusChanged.send( diff --git a/apple/TablePro/Core/Database/DatabaseManager+Startup.swift b/apple/TablePro/Core/Database/DatabaseManager+Startup.swift index 5f68e9c7a7..3804867891 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Startup.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Startup.swift @@ -61,26 +61,116 @@ extension DatabaseManager { return statements } - @discardableResult nonisolated internal func executeStartupCommands( _ commands: String?, on driver: DatabaseDriver, connectionName: String - ) async -> [(statement: String, error: String)] { - var failures: [(statement: String, error: String)] = [] + ) async -> [StartupCommandFailure] { + var failures: [StartupCommandFailure] = [] let databaseType = driver.connection.type let grammar = SQLLexicalResolver.readings(for: databaseType, session: driver.sessionLexicalState).execution for statement in Self.startupStatements(from: commands, databaseType: databaseType, grammar: grammar) { do { _ = try await driver.execute(query: statement) Self.startupLogger.info( - "Startup command succeeded for '\(connectionName)': \(statement)" + "Startup command succeeded for '\(connectionName, privacy: .private)': \(statement, privacy: .private)" ) } catch { Self.startupLogger.warning( - "Startup command failed for '\(connectionName)': \(statement) — \(error.localizedDescription)" + "Startup command failed for '\(connectionName, privacy: .private)': \(statement, privacy: .private): \(error.localizedDescription, privacy: .private)" ) - failures.append((statement: statement, error: error.localizedDescription)) + failures.append(StartupCommandFailure(statement: statement, error: error)) } } return failures } + + // A startup command can be a safety rail, `SET ROLE app_ro` or a read-only default, so a session that opened + // without one says so on the connection's warning rather than only in the log. + nonisolated internal static func startupWarning(for failures: [StartupCommandFailure]) -> String? { + guard let first = failures.first else { return nil } + guard failures.count > 1 else { + guard let code = first.code else { + return String( + format: String(localized: "The startup command %@ failed, so this session runs without it."), + shownStatement(first.statement) + ) + } + return String( + format: String(localized: "The startup command %1$@ failed (%2$@), so this session runs without it."), + shownStatement(first.statement), code + ) + } + let lines = failures.map { failure in + failure.code.map { "\(shownStatement(failure.statement)) (\($0))" } ?? shownStatement(failure.statement) + } + return String( + format: String(localized: "Startup commands failed, so this session runs without them:\n%@"), + lines.joined(separator: "\n") + ) + } + + // A startup command can carry a secret, a password after IDENTIFIED BY or a key in a literal, so the banner shows + // it only up to its first quote or credential clause. + nonisolated internal static func shownStatement(_ statement: String) -> String { + let quote = statement.firstIndex(where: { $0 == "'" || $0 == "\"" || $0 == "$" }) + let stops = [quote] + ["IDENTIFIED", "PASSWORD"].map { statement.range(of: $0, options: .caseInsensitive)?.lowerBound } + let cut = stops.compactMap { $0 }.min() ?? statement.endIndex + let kept = statement[.. limit else { return kept } + return String(kept.prefix(limit)) + "…" + } + + nonisolated internal static func joinedWarning(_ parts: [String?]) -> String? { + let present = parts.compactMap { $0 }.filter { !$0.isEmpty } + return present.isEmpty ? nil : present.joined(separator: "\n\n") + } + + // The warning a session opens with: the login's own, then what its setup could not do. It replaces the previous + // one, so a reconnect that ran every startup command drops a banner about the session before it, and the record + // of named failures starts over. + internal func openingWarning( + driver: DatabaseDriver, + startupFailures: [StartupCommandFailure], + restoreWarning: String? = nil, + for connectionId: UUID + ) -> String? { + reportedStartupFailures[connectionId] = Set(startupFailures.map { $0.statement }) + return Self.joinedWarning([driver.connectionWarning, Self.startupWarning(for: startupFailures), restoreWarning]) + } + + // A pool entry or a database lane opens a session of its own, which can fail a command the window's session ran. + // Each failing statement is named once per connect: the pool keeps opening entries while the window is open. + internal func reportStartupFailures(_ failures: [StartupCommandFailure], for connectionId: UUID) { + guard activeSessions[connectionId] != nil else { return } + var reported = reportedStartupFailures[connectionId] ?? [] + let unreported = failures.filter { reported.insert($0.statement).inserted } + reportedStartupFailures[connectionId] = reported + guard let warning = Self.startupWarning(for: unreported) else { return } + updateSession(connectionId) { session in + session.connectionWarning = Self.joinedWarning([session.connectionWarning, warning]) + } + } +} + +// What the banner may show of a failed startup command: the statement and the server's code, never its message, which +// can echo the values the statement carried. The message goes to the log, private. +struct StartupCommandFailure: Sendable, Equatable { + let statement: String + let code: String? + + init(statement: String, code: String?) { + self.statement = statement + self.code = code + } + + init(statement: String, error: Error) { + self.init(statement: statement, code: Self.code(of: error)) + } + + static func code(of error: Error) -> String? { + guard let driverError = error as? any PluginDriverError else { return nil } + if let state = driverError.pluginSqlState, !state.isEmpty { return state } + if let code = driverError.pluginErrorCode, code != 0 { return String(code) } + return nil + } } diff --git a/apple/TablePro/Core/Database/DatabaseManager.swift b/apple/TablePro/Core/Database/DatabaseManager.swift index 15072aca99..2dec865754 100644 --- a/apple/TablePro/Core/Database/DatabaseManager.swift +++ b/apple/TablePro/Core/Database/DatabaseManager.swift @@ -126,6 +126,10 @@ final class DatabaseManager: ObservableObject { /// for the same reason every other shared write here is generation-checked. internal var connectionStages: [UUID: ConnectionStage] = [:] + // The startup statements a connection's warning has already named since its connect, so a pool entry or lane + // opened later names only a failure the window was not told about. + internal var reportedStartupFailures: [UUID: Set] = [:] + /// Orders operations that move the shared driver, so two windows cannot interleave /// their pins and each run against the other's database. internal let sessionDriverGate = SessionDriverGate() diff --git a/apple/TablePro/Core/Database/TriggerEditing.swift b/apple/TablePro/Core/Database/TriggerEditing.swift index c3744ad7e4..8a427c1bfd 100644 --- a/apple/TablePro/Core/Database/TriggerEditing.swift +++ b/apple/TablePro/Core/Database/TriggerEditing.swift @@ -14,7 +14,6 @@ import TableProSQLGrammar enum TriggerEditingError: LocalizedError { case notConnected - case denied(String) case dropUnavailable case notCreateTrigger case definitionUnavailable @@ -23,7 +22,6 @@ enum TriggerEditingError: LocalizedError { var errorDescription: String? { switch self { case .notConnected: String(localized: "Not connected to database") - case let .denied(reason): reason case .dropUnavailable: String(localized: "This database cannot drop triggers") case .notCreateTrigger: String( @@ -121,9 +119,7 @@ enum TriggerApplyStrategy: Equatable { enum TriggerEditing { nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "TriggerEditing") - /// Runs on the schema change route, never on the session driver: the trigger's own BEGIN - /// would join a transaction a query tab left open, and its COMMIT or ROLLBACK would take - /// that tab's uncommitted work with it. + // On the schema change route: a BEGIN on the session driver would join a query tab's open transaction. static func apply( scope: DatabaseScope, connection: DatabaseConnection, @@ -162,9 +158,7 @@ enum TriggerEditing { statements: plan.gatedStatements(for: sql) ) ) - guard case .authorized = decision else { - throw TriggerEditingError.denied(decision.deniedReason ?? String(localized: "Operation not permitted")) - } + if let denial = decision.denialError { throw denial } let startedAt = Date() defer { @@ -230,10 +224,7 @@ enum TriggerEditing { name: String, gate: any ExecutionGate = ExecutionGateProvider.shared ) async throws { - /// Built on a driver in the scope that will run it, the way `apply` builds its own inside - /// the lease. The session driver is wherever the sidebar last went, and an engine that - /// qualifies its DDL with the connection's current database writes that name into a - /// statement the pooled connection then runs somewhere else. + // Built in the scope that runs it: the session driver may qualify the DDL with another database. let generated = try await DatabaseManager.shared.withMetadataDriver(scope: scope) { driver in driver.generateDropTriggerSQL(name: name, table: tableName) } @@ -252,9 +243,7 @@ enum TriggerEditing { operationDescription: String(localized: "Drop Trigger") ) ) - guard case .authorized = decision else { - throw TriggerEditingError.denied(decision.deniedReason ?? String(localized: "Operation not permitted")) - } + if let denial = decision.denialError { throw denial } let startedAt = Date() try await withSchemaChangeDriver(scope: scope) { driver in diff --git a/apple/TablePro/Core/LSP/LSPTransport.swift b/apple/TablePro/Core/LSP/LSPTransport.swift index 47e274cc6a..6677bb3748 100644 --- a/apple/TablePro/Core/LSP/LSPTransport.swift +++ b/apple/TablePro/Core/LSP/LSPTransport.swift @@ -32,6 +32,45 @@ enum LSPTransportError: Error, LocalizedError { } } +// MARK: - LSPMessageWriter + +// Writes on its own queue, so a server that stops reading blocks the queue and never the transport. +final class LSPMessageWriter: @unchecked Sendable { + private let handle: FileHandle + private let queue = DispatchQueue(label: "com.TablePro.LSPTransport.writer") + private var isClosed = false + + init(_ handle: FileHandle) throws { + // Without it, a write to a server that has exited raises SIGPIPE and ends the app. + guard fcntl(handle.fileDescriptor, F_SETNOSIGPIPE, 1) == 0 else { + throw POSIXError(POSIXErrorCode(rawValue: errno) ?? .EIO) + } + self.handle = handle + } + + func write(_ frame: Data, onFailure: @escaping @Sendable (any Error) -> Void) { + queue.async { [self] in + guard !isClosed else { + onFailure(LSPTransportError.processNotRunning) + return + } + do { + try handle.write(contentsOf: frame) + } catch { + onFailure(error) + } + } + } + + func close() { + queue.async { [self] in + guard !isClosed else { return } + isClosed = true + try? handle.close() + } + } +} + // MARK: - LSPTransport actor LSPTransport { @@ -39,6 +78,7 @@ actor LSPTransport { private var process: Process? private var stdinPipe: Pipe? + private var writer: LSPMessageWriter? private var stdoutPipe: Pipe? private var stderrPipe: Pipe? private var stderrReader: PipeReader? @@ -66,6 +106,7 @@ actor LSPTransport { proc.standardOutput = stdout proc.standardError = stderr + self.writer = try LSPMessageWriter(stdin.fileHandleForWriting) self.stdinPipe = stdin self.stdoutPipe = stdout self.stderrPipe = stderr @@ -105,9 +146,8 @@ actor LSPTransport { readerTask?.cancel() readerTask = nil - if let stdinHandle = stdinPipe?.fileHandleForWriting { - try? stdinHandle.close() - } + writer?.close() + writer = nil if let stdoutHandle = stdoutPipe?.fileHandleForReading { try? stdoutHandle.close() } @@ -144,7 +184,7 @@ actor LSPTransport { return try await withCheckedThrowingContinuation { continuation in pendingRequests[requestID] = continuation do { - try writeMessage(data) + try writeMessage(data, failingRequest: requestID) } catch { pendingRequests.removeValue(forKey: requestID) continuation.resume(throwing: error) @@ -212,19 +252,22 @@ actor LSPTransport { // MARK: - Private - private func writeMessage(_ data: Data) throws { - guard let stdinPipe else { + private func writeMessage(_ data: Data, failingRequest requestID: Int? = nil) throws { + guard let writer else { throw LSPTransportError.processNotRunning } - - let header = "Content-Length: \(data.count)\r\n\r\n" - guard let headerData = header.data(using: .utf8) else { - throw LSPTransportError.invalidResponse + let frame = Data("Content-Length: \(data.count)\r\n\r\n".utf8) + data + writer.write(frame) { [weak self] error in + Task { [weak self] in + await self?.writeFailed(error, requestID: requestID) + } } + } - let handle = stdinPipe.fileHandleForWriting - handle.write(headerData) - handle.write(data) + private func writeFailed(_ error: any Error, requestID: Int?) { + Self.logger.warning("LSP write failed: \(error.localizedDescription)") + guard let requestID, let continuation = pendingRequests.removeValue(forKey: requestID) else { return } + continuation.resume(throwing: error) } private func runReadLoop(handle: FileHandle) async { @@ -341,6 +384,9 @@ actor LSPTransport { private func handleProcessExit(code: Int32) { Self.logger.info("LSP process exited with code \(code)") + writer?.close() + writer = nil + stdinPipe = nil let pending = pendingRequests pendingRequests.removeAll() for (_, continuation) in pending { diff --git a/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift b/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift index 164f6c3613..5b6028255f 100644 --- a/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift +++ b/apple/TablePro/Core/ObjectCopy/ObjectCopyRowCopier.swift @@ -38,6 +38,8 @@ internal struct ObjectCopyRowCopier: Sendable { internal struct Outcome: Sendable { internal let inserted: Int internal let cancelled: Bool + // The limit the source's stream stopped at, so the table holds only its leading rows. + internal var rowCut: Int? } /// A table that failed after some of its batches were written. Outside a transaction those stay @@ -94,7 +96,13 @@ internal struct ObjectCopyRowCopier: Sendable { let generator = try makeGenerator(targetDriver: targetDriver) let budget = SQLWriteBatchBudget(columnCount: step.columns.count, generator: generator) var filler = SQLWriteBatchFiller<[PluginCellValue]>(budget: budget) - var stream = sourceDriver.streamRows(query: step.sourceQuery).makeAsyncIterator() + let cut = OSAllocatedUnfairLock(initialState: nil) + // The copy's SELECT states no engine ceiling, so only a limit the driver declares applies, as for its own export query. + var stream = ExportRowCut.reporting( + sourceDriver.streamRows(query: step.sourceQuery), engineCeiling: nil + ) { limit in + cut.withLock { $0 = limit } + }.makeAsyncIterator() var inserted = 0 @@ -121,7 +129,7 @@ internal struct ObjectCopyRowCopier: Sendable { inserted += try await write(batch, generator: generator, driver: targetDriver) onProgress(inserted) } - return Outcome(inserted: inserted, cancelled: Task.isCancelled) + return Outcome(inserted: inserted, cancelled: Task.isCancelled, rowCut: cut.withLock { $0 }) } catch is CancellationError { return Outcome(inserted: inserted, cancelled: true) } catch where inserted > 0 { diff --git a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift index 6cef58ed51..f8c04fd656 100644 --- a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift +++ b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift @@ -22,6 +22,7 @@ internal struct ObjectCopyObjectOutcome: Identifiable, Sendable { internal let selection: ObjectCopySelection internal let rowsCopied: Int internal let error: String? + internal var warning: String? internal var id: String { selection.id } internal var succeeded: Bool { error == nil } @@ -67,6 +68,8 @@ internal struct ObjectCopyRunResult: Sendable { internal var firstError: String? { outcomes.compactMap(\.error).first } + internal var warnings: [String] { outcomes.compactMap(\.warning) } + /// Every failure, each with an identity of its own. /// /// One object fails once per phase it reaches, so a table whose `CREATE` failed under Skip and @@ -507,9 +510,14 @@ internal struct ObjectCopyRunner { /// would send a retry over them. var rowsCopied = 0 - mutating func recordCopy(of step: ObjectCopyTableStep, inserted: Int) { + mutating func recordCopy(of step: ObjectCopyTableStep, inserted: Int, rowCut: Int? = nil) { rowsCopied += inserted - outcomes.append(ObjectCopyObjectOutcome(selection: step.selection, rowsCopied: inserted, error: nil)) + outcomes.append(ObjectCopyObjectOutcome( + selection: step.selection, + rowsCopied: inserted, + error: nil, + warning: rowCut.map { StreamRowCutWarning.text(table: step.selection.qualifiedName, limit: $0) } + )) } /// Stop reaching the driver mid-stream is the user stopping, not the copy failing, and the @@ -702,7 +710,7 @@ internal struct ObjectCopyRunner { result.recordStop(of: step, inserted: outcome.inserted, progress: progress) break } - result.recordCopy(of: step, inserted: outcome.inserted) + result.recordCopy(of: step, inserted: outcome.inserted, rowCut: outcome.rowCut) } catch is CancellationError { result.recordStop(of: step, inserted: 0, progress: progress) break @@ -748,7 +756,7 @@ internal struct ObjectCopyRunner { result.recordStop(of: step, inserted: outcome.inserted, progress: progress) break } - result.recordCopy(of: step, inserted: outcome.inserted) + result.recordCopy(of: step, inserted: outcome.inserted, rowCut: outcome.rowCut) } catch is CancellationError { result.recordStop(of: step, inserted: 0, progress: progress) break diff --git a/apple/TablePro/Core/Plugins/ExportDataSourceAdapter.swift b/apple/TablePro/Core/Plugins/ExportDataSourceAdapter.swift index 4c67711e74..5878dcda37 100644 --- a/apple/TablePro/Core/Plugins/ExportDataSourceAdapter.swift +++ b/apple/TablePro/Core/Plugins/ExportDataSourceAdapter.swift @@ -61,13 +61,7 @@ final class ExportDataSourceAdapter: PluginExportDataSource, @unchecked Sendable // One line per table whose rows stopped at a ceiling, so a partial copy is never reported as the whole table. var cappedTableWarnings: [String] { - cappedTables.withLock { $0 }.map { capped in - String( - format: String(localized: "%1$@: only the first %2$lld rows were read, the most this database returns from one query."), - capped.table, - capped.limit - ) - } + cappedTables.withLock { $0 }.map { StreamRowCutWarning.text(table: $0.table, limit: $0.limit) } } func streamRows(table: String, databaseName: String) -> AsyncThrowingStream { diff --git a/apple/TablePro/Core/Plugins/ExportRowCut.swift b/apple/TablePro/Core/Plugins/ExportRowCut.swift index 04efb07dc6..a3be667953 100644 --- a/apple/TablePro/Core/Plugins/ExportRowCut.swift +++ b/apple/TablePro/Core/Plugins/ExportRowCut.swift @@ -58,6 +58,17 @@ final class ExportRowCut: @unchecked Sendable { } } +// One wording for every reader of a cut stream: export, Table Transfer, Object Copy and Data Compare. +enum StreamRowCutWarning { + static func text(table: String, limit: Int) -> String { + String( + format: String(localized: "%1$@: only the first %2$lld rows were read, the most this database returns from one query."), + table, + limit + ) + } +} + extension ExportRowCut where Element == PluginStreamElement { static func reporting( _ upstream: AsyncThrowingStream, diff --git a/apple/TablePro/Core/Services/Execution/GatedOperationFailure.swift b/apple/TablePro/Core/Services/Execution/GatedOperationFailure.swift new file mode 100644 index 0000000000..f0e6bdaeae --- /dev/null +++ b/apple/TablePro/Core/Services/Execution/GatedOperationFailure.swift @@ -0,0 +1,39 @@ +// +// GatedOperationFailure.swift +// TablePro +// + +import Foundation + +// Cancel at the gate's confirmation is the user's own choice: it shows nothing and the sheet keeps its edits. +internal enum GatedOperationFailure: Equatable { + case cancelledByUser + case refused(String) + case failed(String) + + internal init(_ error: any Error) { + guard let gateError = error as? ExecutionGateError else { + self = .failed(error.localizedDescription) + return + } + switch gateError { + case .cancelledByUser: + self = .cancelledByUser + case .denied(let reason): + self = .refused(reason) + } + } + + internal var message: String? { + switch self { + case .cancelledByUser: nil + case .refused(let reason), .failed(let reason): reason + } + } + + // A refusal and a Cancel both stop before the first statement: nothing to report or reload. + internal var reportsFailure: Bool { + if case .failed = self { return true } + return false + } +} diff --git a/apple/TablePro/Core/Services/Query/MetadataConnectionPool.swift b/apple/TablePro/Core/Services/Query/MetadataConnectionPool.swift index 2d85197c26..c7a274f88f 100644 --- a/apple/TablePro/Core/Services/Query/MetadataConnectionPool.swift +++ b/apple/TablePro/Core/Services/Query/MetadataConnectionPool.swift @@ -591,13 +591,14 @@ final class MetadataConnectionPool { ) do { try await driver.connect() - try await Self.prepareSession( + let startupFailures = try await Self.prepareSession( driver, queryTimeoutSeconds: queryTimeout, startupCommands: session.connection.startupCommands, connectionName: session.connection.name, timeoutSeconds: operationTimeoutSeconds ) + DatabaseManager.shared.reportStartupFailures(startupFailures, for: scope.connectionId) if let database = plan.switchDatabase { try await Self.switchDatabase(driver, to: database, timeoutSeconds: operationTimeoutSeconds) } @@ -680,14 +681,14 @@ final class MetadataConnectionPool { startupCommands: String?, connectionName: String, timeoutSeconds: Double? - ) async throws { + ) async throws -> [StartupCommandFailure] { try await bounded( driver: driver, timeoutSeconds: timeoutSeconds, timeoutMessage: String(localized: "Preparing the metadata connection timed out.") ) { try? await driver.applyQueryTimeout(queryTimeoutSeconds) - await DatabaseManager.shared.executeStartupCommands( + return await DatabaseManager.shared.executeStartupCommands( startupCommands, on: driver, connectionName: connectionName ) } @@ -695,18 +696,17 @@ final class MetadataConnectionPool { /// Disconnects the driver when the deadline fires so a driver call that /// ignores task cancellation still completes and the timeout can propagate. - private static func bounded( + private static func bounded( driver: DatabaseDriver, timeoutSeconds: Double?, timeoutMessage: String, - _ operation: @escaping @Sendable () async throws -> Void - ) async throws { + _ operation: @escaping @Sendable () async throws -> T + ) async throws -> T { guard let timeoutSeconds, timeoutSeconds > 0 else { - try await operation() - return + return try await operation() } do { - try await withTimeout( + return try await withTimeout( seconds: timeoutSeconds, onTimeout: { driver.disconnect() }, operation: operation diff --git a/apple/TablePro/Core/Services/Query/RowOperationsManager.swift b/apple/TablePro/Core/Services/Query/RowOperationsManager.swift index 66f5ebf5e4..2a33425139 100644 --- a/apple/TablePro/Core/Services/Query/RowOperationsManager.swift +++ b/apple/TablePro/Core/Services/Query/RowOperationsManager.swift @@ -49,10 +49,12 @@ final class RowOperationsManager { func addNewRow(tableRows: inout TableRows, uuidKeyForms: [Int: UUIDCellForm] = [:]) -> AddNewRowResult? { var newRowValues: [PluginCellValue] = [] var absentColumns: Set = [] + var defaultColumns: Set = [] var prefilled: Set = [] for (index, column) in tableRows.columns.enumerated() { if tableRows.generatedColumns.contains(column) || tableRows.serverAssignsValue(forColumn: column) { - newRowValues.append(.text("__DEFAULT__")) + newRowValues.append(.columnDefault) + defaultColumns.insert(index) } else if let form = uuidKeyForms[index] { newRowValues.append(Self.freshUUID(form)) prefilled.insert(index) @@ -62,7 +64,11 @@ final class RowOperationsManager { } } return appendInsertedRow( - values: newRowValues, absentColumns: absentColumns, prefilledColumns: prefilled, to: &tableRows + values: newRowValues, + absentColumns: absentColumns, + expressionColumns: defaultColumns, + prefilledColumns: prefilled, + to: &tableRows ) } @@ -86,14 +92,15 @@ final class RowOperationsManager { + Array(tableRows.columnIdentity.keys) for resetColumn in resetColumns { if let index = tableRows.columns.firstIndex(of: resetColumn), index < newValues.count { + absentColumns.remove(index) if let form = uuidKeyForms[index] { newValues[index] = Self.freshUUID(form) prefilled.insert(index) + expressionColumns.remove(index) } else { - newValues[index] = .text("__DEFAULT__") + newValues[index] = .columnDefault + expressionColumns.insert(index) } - absentColumns.remove(index) - expressionColumns.remove(index) } } return appendInsertedRow( @@ -406,9 +413,9 @@ final class RowOperationsManager { } if let pkIndex = schema.primaryKeyIndex, pkIndex < values.count { - values[pkIndex] = .text("__DEFAULT__") + values[pkIndex] = .columnDefault absentColumns.remove(pkIndex) - expressionColumns.remove(pkIndex) + expressionColumns.insert(pkIndex) } return ParsedRow( @@ -438,8 +445,8 @@ final class RowOperationsManager { return .entered(text, columnType: tableRows.editingColumnType(at: column)) } if let pkIndex = schema.primaryKeyIndex, pkIndex < values.count { - values[pkIndex] = .text("__DEFAULT__") - expressionColumns.remove(pkIndex) + values[pkIndex] = .columnDefault + expressionColumns.insert(pkIndex) } return ParsedRow(values: values, sourceLineNumber: index + 1, expressionColumns: expressionColumns) } @@ -447,28 +454,39 @@ final class RowOperationsManager { // A blank cell of pasted text was left unfilled, so it takes what a new row's cell does: the server's value where // the column has a default, else NULL. A NOT NULL text column with no default takes the empty string it can hold. - private static func unfilledCellValues(columns: [String], tableRows: TableRows) -> [PluginCellValue] { - columns.map { name in + private static func unfilledCellValues( + columns: [String], + tableRows: TableRows + ) -> (values: [PluginCellValue], defaults: Set) { + var defaults: Set = [] + let values = columns.enumerated().map { position, name -> PluginCellValue in guard let index = tableRows.columns.firstIndex(of: name) else { return .null } if tableRows.generatedColumns.contains(name) || tableRows.serverAssignsValue(forColumn: name) { - return .text("__DEFAULT__") + defaults.insert(position) + return .columnDefault } if tableRows.refusesNull(columnAt: index), tableRows.editingColumnType(at: index)?.hasEmptyValue == true { return .text("") } return .null } + return (values, defaults) } - private static func fillingBlankCells(_ row: ParsedRow, with unfilled: [PluginCellValue]) -> ParsedRow { + private static func fillingBlankCells( + _ row: ParsedRow, + with unfilled: (values: [PluginCellValue], defaults: Set) + ) -> ParsedRow { guard !row.blankColumns.isEmpty else { return row } var values = row.values - for index in row.blankColumns where index < values.count && index < unfilled.count { - values[index] = unfilled[index] + var expressionColumns = row.expressionColumns + for index in row.blankColumns where index < values.count && index < unfilled.values.count { + values[index] = unfilled.values[index] + if unfilled.defaults.contains(index) { expressionColumns.insert(index) } } var filled = ParsedRow(values: values, sourceLineNumber: row.sourceLineNumber) filled.absentColumns = row.absentColumns - filled.expressionColumns = row.expressionColumns + filled.expressionColumns = expressionColumns return filled } @@ -509,7 +527,7 @@ final class RowOperationsManager { return containsComma ? CSVRowParser() : TSVRowParser() } - // Every paste path resets the key to __DEFAULT__ first; an unfilled UUID key then takes a fresh UUID per row. + // Every paste path resets the key to its default first; an unfilled UUID key then takes a fresh UUID per row. private func insertParsedRows( _ parsedRows: [ParsedRow], uuidKeyForms: [Int: UUIDCellForm], @@ -531,11 +549,12 @@ final class RowOperationsManager { var absentColumns = parsedRow.absentColumns var expressionColumns = parsedRow.expressionColumns.filter { $0 < rowValues.count } for index in serverOwned where index < rowValues.count { - rowValues[index] = .text("__DEFAULT__") + rowValues[index] = .columnDefault absentColumns.remove(index) - expressionColumns.remove(index) + expressionColumns.insert(index) } - for (index, form) in uuidKeyForms where index < rowValues.count && rowValues[index] == .text("__DEFAULT__") { + for (index, form) in uuidKeyForms where index < rowValues.count + && rowValues[index].isColumnDefault(isExpression: expressionColumns.contains(index)) { rowValues[index] = Self.freshUUID(form) absentColumns.remove(index) expressionColumns.remove(index) diff --git a/apple/TablePro/Core/Services/Query/RowParser.swift b/apple/TablePro/Core/Services/Query/RowParser.swift index cdd13c09a6..64179f3039 100644 --- a/apple/TablePro/Core/Services/Query/RowParser.swift +++ b/apple/TablePro/Core/Services/Query/RowParser.swift @@ -37,13 +37,17 @@ private func fittedRow( blankColumns.insert(values.count) values.append(nil) } - if let pkIndex = schema.primaryKeyIndex, pkIndex < values.count { - values[pkIndex] = "__DEFAULT__" + var cells = values.map(PluginCellValue.fromOptional) + var defaultColumns: Set = [] + if let pkIndex = schema.primaryKeyIndex, pkIndex < cells.count { + cells[pkIndex] = .columnDefault + defaultColumns.insert(pkIndex) blankColumns.remove(pkIndex) } return ParsedRow( - values: values.map(PluginCellValue.fromOptional), + values: cells, sourceLineNumber: lineNumber, + expressionColumns: defaultColumns, blankColumns: blankColumns ) } diff --git a/apple/TablePro/Core/Services/Query/SelectionSummaryCalculator.swift b/apple/TablePro/Core/Services/Query/SelectionSummaryCalculator.swift index 7470c95bb8..4d031fd612 100644 --- a/apple/TablePro/Core/Services/Query/SelectionSummaryCalculator.swift +++ b/apple/TablePro/Core/Services/Query/SelectionSummaryCalculator.swift @@ -61,11 +61,8 @@ actor SelectionSummaryCalculator { } private struct Tally { - private static let defaultMarkerLength = PluginCellValue.defaultMarkerText.utf8.count - let emptyTextIsEmpty: Bool - let insertedRowIDs: Set - let modifiedCells: [RowID: Set] + let defaultedCells: [RowID: Set] var valueCount = 0 var emptyCount = 0 var notANumberCount = 0 @@ -73,8 +70,7 @@ private struct Tally { init(input: SelectionSummaryInput) { emptyTextIsEmpty = input.policy.emptyTextIsEmpty - insertedRowIDs = input.insertedRowIDs - modifiedCells = input.modifiedCells + defaultedCells = input.defaultedCells } mutating func add(_ value: PluginCellValue, column: Int, rule: SelectionSummaryColumnRule, rowID: RowID) { @@ -108,13 +104,9 @@ private struct Tally { return numbers.add(text.trimmingCharacters(in: .whitespaces)) } - // The default marker is the grid's placeholder only where an edit put it; elsewhere it is text the table holds. private func isEmpty(_ text: String, column: Int, rowID: RowID) -> Bool { if text.isEmpty { return emptyTextIsEmpty } - guard text.utf8.count == Self.defaultMarkerLength, text == PluginCellValue.defaultMarkerText else { - return false - } - return insertedRowIDs.contains(rowID) || modifiedCells[rowID]?.contains(column) == true + return defaultedCells[rowID]?.contains(column) == true } func summary(coversWholeColumn: Bool) -> SelectionSummary { diff --git a/apple/TablePro/Core/Shortcuts/Shared/PayloadRowPlanner.swift b/apple/TablePro/Core/Shortcuts/Shared/PayloadRowPlanner.swift index 47d4e2da17..0065a4e3d0 100644 --- a/apple/TablePro/Core/Shortcuts/Shared/PayloadRowPlanner.swift +++ b/apple/TablePro/Core/Shortcuts/Shared/PayloadRowPlanner.swift @@ -16,17 +16,17 @@ nonisolated enum PayloadRowPlanner { dropsEmptyPrimaryKey: Bool = true ) throws -> [[(column: String, value: String?)]] { guard !columns.isEmpty else { throw IntentDataError.noColumns(table) } - let columnNames = Set(columns.map(\.name)) + let columnNames = columns.map(\.name) let primaryKeys = Set(columns.filter(\.isPrimaryKey).map(\.name)) return try rows.compactMap { row in - let unknown = row.keys.filter { !columnNames.contains($0) } - guard unknown.isEmpty else { throw IntentDataError.unknownColumns(unknown.sorted(), table) } + let matched = keysByColumn(row.keys, columns: columnNames) + guard matched.unknown.isEmpty else { throw IntentDataError.unknownColumns(matched.unknown.sorted(), table) } var values: [(column: String, value: String?)] = [] for column in columns { guard !column.isServerOwned else { continue } - guard let value = row.value(for: column.name) else { continue } + guard let key = matched.byColumn[column.name], let value = row.value(for: key) else { continue } if dropsEmptyPrimaryKey, primaryKeys.contains(column.name), value.isEmptyOrNull { continue } values.append((column: column.name, value: value.sqlValue)) } @@ -34,4 +34,46 @@ nonisolated enum PayloadRowPlanner { return values } } + + // As the import sheet matches a header: its exact spelling claims a column first, then a case-insensitive match. + // There is no sheet to map a header by hand, so SQL Server's `[name]` also finds `name`. + static func keysByColumn(_ keys: [String], columns: [String]) -> (byColumn: [String: String], unknown: [String]) { + var byColumn: [String: String] = [:] + var loose: [String] = [] + let spelled = Set(columns) + for key in keys.sorted() { + if spelled.contains(key) { + byColumn[key] = key + } else { + loose.append(key) + } + } + var unknown: [String] = [] + for key in loose { + let claimed = byColumn + guard let column = matchingColumn(for: key, in: columns, unclaimed: { claimed[$0] == nil }) else { + unknown.append(key) + continue + } + byColumn[column] = key + } + return (byColumn, unknown) + } + + private static func matchingColumn(for key: String, in columns: [String], unclaimed: (String) -> Bool) -> String? { + for name in [key, unbracketed(key)].compactMap({ $0 }) { + if columns.contains(name), unclaimed(name) { return name } + let folded = columns.filter { $0.caseInsensitiveCompare(name) == .orderedSame && unclaimed($0) } + if folded.count == 1 { return folded[0] } + } + return nil + } + + // `]` inside the brackets is doubled, so a lone one means the key is not a bracketed name. + private static func unbracketed(_ key: String) -> String? { + guard key.count > 2, key.hasPrefix("["), key.hasSuffix("]") else { return nil } + let inner = String(key.dropFirst().dropLast()) + guard !inner.replacingOccurrences(of: "]]", with: "", options: .literal).contains("]") else { return nil } + return inner.replacingOccurrences(of: "]]", with: "]", options: .literal) + } } diff --git a/apple/TablePro/Core/Shortcuts/Shared/RowPayload.swift b/apple/TablePro/Core/Shortcuts/Shared/RowPayload.swift index 99fd7d2c56..95736a027e 100644 --- a/apple/TablePro/Core/Shortcuts/Shared/RowPayload.swift +++ b/apple/TablePro/Core/Shortcuts/Shared/RowPayload.swift @@ -11,15 +11,29 @@ nonisolated enum RowPayload { let trimmed = raw.trimmingCharacters(in: .whitespacesAndNewlines) guard !trimmed.isEmpty else { throw IntentDataError.emptyPayload } - let rows = trimmed.hasPrefix("{") || trimmed.hasPrefix("[") - ? try parseJSON(trimmed) - : try parseCSV(raw) + let rows = try sniffedRows(raw: raw, trimmed: trimmed) guard !rows.isEmpty else { throw IntentDataError.emptyPayload } guard rows.count <= maxRows else { throw IntentDataError.tooManyRows(maxRows) } return rows } + // A CSV header can open with `[` or `{`, as SQL Server's `[id],[name]` does, so only text that parses as JSON is + // JSON. Text that is neither keeps the JSON error, which names what broke in a payload meant as JSON. So does JSON + // that still splits into even CSV records, one object per line with a trailing comma: its headers hold `":`. + private static func sniffedRows(raw: String, trimmed: String) throws -> [PayloadRow] { + guard trimmed.hasPrefix("{") || trimmed.hasPrefix("[") else { return try parseCSV(raw) } + do { + return try parseJSON(trimmed) + } catch IntentDataError.malformedPayload(let reason) { + guard let rows = try? parseCSV(raw), !rows.isEmpty, + !rows.contains(where: { row in row.keys.contains { $0.contains("\":") } }) else { + throw IntentDataError.malformedPayload(reason) + } + return rows + } + } + static func parseSingle(data: String?, file: IntentFile?) async throws -> [PayloadRow] { let rows = try await parse(data: data, file: file) guard rows.count == 1 else { throw IntentDataError.expectedSingleRow } diff --git a/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift b/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift index 1baee5a518..4336e6c51f 100644 --- a/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift +++ b/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift @@ -157,10 +157,11 @@ internal enum ShortcutsRowWriter { let rowID = RowID.inserted(UUID()) let byColumn = Dictionary(values.map { ($0.column, $0.value) }, uniquingKeysWith: { first, _ in first }) insertedRowData[rowID] = names.map { name in - guard let value = byColumn[name] else { return .text(PluginCellValue.defaultMarkerText) } + guard let value = byColumn[name] else { return .columnDefault } return value.map(PluginCellValue.text) ?? .null } - changes.append(RowChange(rowID: rowID, type: .insert, sequence: index)) + let defaulted = Set(names.indices.filter { byColumn[names[$0]] == nil }) + changes.append(RowChange(rowID: rowID, type: .insert, expressionColumns: defaulted, sequence: index)) } let factory = RowChangeStatementFactory( diff --git a/apple/TablePro/Core/UsersRoles/PrincipalChangeManager.swift b/apple/TablePro/Core/UsersRoles/PrincipalChangeManager.swift index 1a7be6130b..578cfc531f 100644 --- a/apple/TablePro/Core/UsersRoles/PrincipalChangeManager.swift +++ b/apple/TablePro/Core/UsersRoles/PrincipalChangeManager.swift @@ -432,14 +432,20 @@ final class PrincipalChangeManager: ObservableObject { // MARK: - Output + // A staged drop keeps the principal's other edits so undoing the drop brings them back, but only the DROP runs. func pendingChanges() -> [PrincipalChange] { var changes: [PrincipalChange] = pendingCreates.map { .create($0) } changes += pendingAlters.compactMap { ref, definition in - guard let original = principals.first(where: { $0.ref == ref }) else { return nil } + guard pendingDrops[ref] == nil, + let original = principals.first(where: { $0.ref == ref }) + else { return nil } return .alter(old: Self.definition(from: original), new: definition) } - changes += pendingPasswords.map { .setPassword(ref: $0.key, password: $0.value) } + changes += pendingPasswords.compactMap { ref, password in + guard pendingDrops[ref] == nil else { return nil } + return .setPassword(ref: ref, password: password) + } changes += grantChangeSets().map { .modifyGrants($0) } changes += pendingDrops.map { .drop(ref: $0.key, options: $0.value) } return changes @@ -531,11 +537,12 @@ final class PrincipalChangeManager: ObservableObject { } private func recomputeChangeCount() { - let grantChanges = grantDeltas.values.reduce(0) { $0 + $1.count } + let isLive = { (ref: PluginPrincipalRef) in self.pendingDrops[ref] == nil } + let grantChanges = grantDeltas.reduce(0) { isLive($1.key) ? $0 + $1.value.count : $0 } changeCount = pendingCreates.count + pendingDrops.count - + pendingPasswords.count - + pendingAlters.count + + pendingPasswords.keys.count(where: isLive) + + pendingAlters.keys.count(where: isLive) + grantChanges } diff --git a/apple/TablePro/Core/Utilities/JavaScript/QueryStatementModel.swift b/apple/TablePro/Core/Utilities/JavaScript/QueryStatementModel.swift index 5161eb1e38..a805e5be70 100644 --- a/apple/TablePro/Core/Utilities/JavaScript/QueryStatementModel.swift +++ b/apple/TablePro/Core/Utilities/JavaScript/QueryStatementModel.swift @@ -189,11 +189,11 @@ enum QueryStatementScanner { } } - private static func located(_ command: RedisCommandSplitter.Command) -> SQLStatementScanner.LocatedStatement { + fileprivate static func located(_ command: RedisCommandSplitter.Command) -> SQLStatementScanner.LocatedStatement { SQLStatementScanner.LocatedStatement(wholeText: command.text, offset: command.range.location) } - private static func located( + fileprivate static func located( _ statement: JavaScriptStatementScanner.Statement ) -> SQLStatementScanner.LocatedStatement { SQLStatementScanner.LocatedStatement( @@ -203,3 +203,51 @@ enum QueryStatementScanner { ) } } + +// A JavaScript or Redis document's statements, read once per text, so a caret move finds its statement by search and +// answers what `QueryStatementScanner.locatedStatementAtCursor` would. A SQL document uses `SQLStatementCursorIndex`. +struct QueryStatementCaretTable { + private let model: QueryStatementModel + private let statements: [SQLStatementScanner.LocatedStatement] + // The last caret offset each statement owns, in document order. + private let reaches: [Int] + + init?(text: String, model: QueryStatementModel) { + self.model = model + switch model { + case .sql: + return nil + case .javascript: + let found = JavaScriptStatementScanner.executableStatements(in: text) + statements = found.map(QueryStatementScanner.located) + reaches = found.map { NSMaxRange($0.range) } + case .redis: + let found = RedisCommandSplitter.commandsWithCaretReach(in: text) + statements = found.map { QueryStatementScanner.located($0.command) } + reaches = found.map { $0.reach } + } + } + + func statement(atCursor cursor: Int) -> SQLStatementScanner.LocatedStatement { + var low = 0 + var high = reaches.count + while low < high { + let middle = (low + high) / 2 + if reaches[middle] < cursor { + low = middle + 1 + } else { + high = middle + } + } + let nothing = SQLStatementScanner.LocatedStatement(sql: "", offset: 0, hasContent: false) + switch model { + case .redis: + return low < statements.count ? statements[low] : nothing + case .javascript, .sql: + // Off every statement, the caret takes the one before it, or the first. + guard !statements.isEmpty else { return nothing } + if low < statements.count, statements[low].offset <= cursor { return statements[low] } + return statements[max(0, low - 1)] + } + } +} diff --git a/apple/TablePro/Core/Utilities/SQL/DisplayedResultReader.swift b/apple/TablePro/Core/Utilities/SQL/DisplayedResultReader.swift index 869817eb65..a9463a1cb4 100644 --- a/apple/TablePro/Core/Utilities/SQL/DisplayedResultReader.swift +++ b/apple/TablePro/Core/Utilities/SQL/DisplayedResultReader.swift @@ -15,12 +15,14 @@ internal enum DisplayedResultReader { internal let skippedDeletedCount: Int } - // Empty selectedDisplayIndices means every displayed row. + // Empty selectedDisplayIndices means every displayed row. A cell in defaultedCells has no value until the server + // gives it one, so it reads as NULL. internal static func read( tableRows: TableRows, displayIDs: [RowID]?, selectedDisplayIndices: Set, deletedRowIDs: Set = [], + defaultedCells: [RowID: Set] = [:], columns projection: VisibleColumnProjection ) -> Output { let positions: [Int] @@ -38,7 +40,7 @@ internal enum DisplayedResultReader { skippedDeleted += 1 return nil } - return projection.values(Self.values(of: row)) + return projection.values(Self.values(of: row, defaulted: defaultedCells[row.id] ?? [])) } return Output( @@ -50,12 +52,15 @@ internal enum DisplayedResultReader { } // A value the page left on the server says so, rather than reading as NULL. - private static func values(of row: Row) -> [PluginCellValue] { - guard row.hasUnloadedValues else { return Array(row.values) } + private static func values(of row: Row, defaulted: Set) -> [PluginCellValue] { + guard row.hasUnloadedValues || !defaulted.isEmpty else { return Array(row.values) } var values = Array(row.values) for (column, byteCount) in row.unloadedColumns where values.indices.contains(column) { values[column] = .text(UnloadedValueText.notLoaded(byteCount: byteCount)) } + for column in defaulted where values.indices.contains(column) { + values[column] = .null + } return values } } diff --git a/apple/TablePro/Core/Utilities/SQL/JsonRowConverter.swift b/apple/TablePro/Core/Utilities/SQL/JsonRowConverter.swift index efc41e9320..5cb6637e19 100644 --- a/apple/TablePro/Core/Utilities/SQL/JsonRowConverter.swift +++ b/apple/TablePro/Core/Utilities/SQL/JsonRowConverter.swift @@ -40,7 +40,7 @@ internal struct JsonRowConverter { } let cell = row[colIdx] - if cell.isNull || cell.isDefaultMarker { + if cell.isNull { result.append("null") appendPropertySuffix(to: &result, colIdx: colIdx) continue diff --git a/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift b/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift index c326a505df..19d3dc08ff 100644 --- a/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift +++ b/apple/TablePro/Core/Utilities/SQL/QueryClassifier.swift @@ -102,16 +102,21 @@ enum QueryClassifier { isMultiStatement(sql, databaseType: databaseType, readings: databaseType.lexicalReadings) } - /// Whether `sql` runs more than one statement: it holds several, a `GO n` line runs its one statement n times, or it - /// ends inside a block it opened, which an engine that reads the opener as a name runs as several statements. A - /// caller that sends one statement once cannot honour the count, and dropping it runs the statement once. + /// Whether `sql` runs more than one statement: it holds several, a `GO n` line runs its one statement n times, it + /// ends inside a block it opened, which an engine that reads the opener as a name runs as several statements, or a + /// T-SQL block in it holds several. A caller that sends one statement once cannot honour the count, and dropping it + /// runs the statement once. static func isMultiStatement(_ sql: String, databaseType: DatabaseType, readings: SQLLexicalReadings) -> Bool { let model = QueryStatementModel.forDatabaseType(databaseType) guard let lexings = readings.lexings(of: sql) else { return true } return lexings.contains { lexing in let batches = QueryBatchPlanner.batches(in: lexing.text, model: model, grammar: lexing.grammar) - return batches.flatMap(\.statements).count > 1 || batches.contains { $0.repeatCount > 1 } + let statements = batches.flatMap(\.statements) + return statements.count > 1 || batches.contains { $0.repeatCount > 1 } || SQLStatementScanner.endsInsideOpenBlock(lexing.text, grammar: lexing.grammar) + || statements.contains { statement in + SQLUnterminatedStatements.blockHoldsSeveralStatements(statement.sql, grammar: lexing.grammar) + } } } diff --git a/apple/TablePro/Core/Utilities/SQL/ResultJsonSerializer.swift b/apple/TablePro/Core/Utilities/SQL/ResultJsonSerializer.swift index c2f9ead605..1bb897caff 100644 --- a/apple/TablePro/Core/Utilities/SQL/ResultJsonSerializer.swift +++ b/apple/TablePro/Core/Utilities/SQL/ResultJsonSerializer.swift @@ -36,6 +36,7 @@ internal enum ResultJsonSerializer { displayIDs: [RowID]?, selectedDisplayIndices: Set, deletedRowIDs: Set = [], + defaultedCells: [RowID: Set] = [:], columns projection: VisibleColumnProjection ) -> Output { let read = DisplayedResultReader.read( @@ -43,6 +44,7 @@ internal enum ResultJsonSerializer { displayIDs: displayIDs, selectedDisplayIndices: selectedDisplayIndices, deletedRowIDs: deletedRowIDs, + defaultedCells: defaultedCells, columns: projection ) diff --git a/apple/TablePro/Core/Utilities/SQL/SQLRowToStatementConverter.swift b/apple/TablePro/Core/Utilities/SQL/SQLRowToStatementConverter.swift index 4eac9054f4..3c763effc6 100644 --- a/apple/TablePro/Core/Utilities/SQL/SQLRowToStatementConverter.swift +++ b/apple/TablePro/Core/Utilities/SQL/SQLRowToStatementConverter.swift @@ -228,18 +228,6 @@ internal struct SQLRowToStatementConverter { } private func formatBinaryLiteral(_ data: Data) -> String { - var hex = "" - hex.reserveCapacity(data.count * 2) - for byte in data { - hex += String(format: "%02X", byte) - } - switch databaseType { - case .postgresql, .redshift, .cockroachdb, .pglite, .kingbasees: - return "'\\x\(hex)'::bytea" - case .mssql: - return "0x\(hex)" - default: - return "X'\(hex)'" - } + CompareSQLLiteral.binaryLiteral(for: data, databaseType: databaseType) ?? "X'\(data.uppercaseHex())'" } } diff --git a/apple/TablePro/Core/Vim/VimTextBufferAdapter.swift b/apple/TablePro/Core/Vim/VimTextBufferAdapter.swift index 4d012d8074..9d8bde2d77 100644 --- a/apple/TablePro/Core/Vim/VimTextBufferAdapter.swift +++ b/apple/TablePro/Core/Vim/VimTextBufferAdapter.swift @@ -51,65 +51,6 @@ final class VimTextBufferAdapter: VimTextBuffer { cachedLineCount = nil } - /// Incrementally update the cached line count based on text change delta. - /// Avoids a full O(n) recount on every keystroke. - func textDidChange(in range: NSRange, replacementLength: Int) { - guard let textView else { - cachedLineCount = nil - return - } - - guard let cached = cachedLineCount else { return } - - let nsString = textView.string as NSString - - // Pure insertion: count newlines in the new text and apply delta - if range.length == 0 { - var addedNewlines = 0 - let end = range.location + replacementLength - if replacementLength > 0 && end <= nsString.length { - for i in range.location.. 0 && range.location + range.length <= oldNs.length { - let end = range.location + range.length - for i in range.location.. 0 && replacementEnd <= nsString.length { - for i in range.location.. NSRange { guard let textView else { return NSRange(location: 0, length: 0) } let nsString = textView.string as NSString diff --git a/apple/TablePro/Extensions/PluginCellValue+DefaultMarker.swift b/apple/TablePro/Extensions/PluginCellValue+DefaultMarker.swift index 74b11c7a5b..63f9e24d11 100644 --- a/apple/TablePro/Extensions/PluginCellValue+DefaultMarker.swift +++ b/apple/TablePro/Extensions/PluginCellValue+DefaultMarker.swift @@ -7,16 +7,15 @@ import Foundation import TableProPluginKit internal extension PluginCellValue { - /// The sentinel a pending inserted row carries for a column the server fills in, written by - /// `RowOperationsManager` and stripped again by `SQLStatementGenerator`. - /// - /// It is an internal marker, never a value, so every view that renders a cell has to recognise - /// it. The grid shows it as a placeholder; anything that renders the same rows some other way - /// has to agree, or the marker leaks out as literal text. - static let defaultMarkerText = "__DEFAULT__" + // A cell left to the column's default is the SQL keyword, marked as SQL beside the value like any expression. The + // mark decides, never the text: a stored or typed value can read the same. + static let columnDefaultKeyword = "DEFAULT" + static let columnDefault = PluginCellValue.text(columnDefaultKeyword) - var isDefaultMarker: Bool { - guard case .text(let value) = self else { return false } - return value == Self.defaultMarkerText + // The spelling driver plugins read as the default in the rows the host hands them. + static let pluginDefaultMarker = PluginCellValue.text("__DEFAULT__") + + func isColumnDefault(isExpression: Bool) -> Bool { + isExpression && self == Self.columnDefault } } diff --git a/apple/TablePro/Models/ClickHouse/ClickHousePartInfo.swift b/apple/TablePro/Models/ClickHouse/ClickHousePartInfo.swift index baa347a335..afe2544e04 100644 --- a/apple/TablePro/Models/ClickHouse/ClickHousePartInfo.swift +++ b/apple/TablePro/Models/ClickHouse/ClickHousePartInfo.swift @@ -2,15 +2,13 @@ // ClickHousePartInfo.swift // TablePro // -// Model for ClickHouse partition/part information from system.parts. -// import Foundation -/// Represents a single part from the ClickHouse system.parts table struct ClickHousePartInfo: Identifiable { let id = UUID() let partition: String + let partitionId: String let name: String let rows: UInt64 let bytesOnDisk: UInt64 diff --git a/apple/TablePro/Models/Query/SelectionSummary.swift b/apple/TablePro/Models/Query/SelectionSummary.swift index af5da02cb1..923812aa44 100644 --- a/apple/TablePro/Models/Query/SelectionSummary.swift +++ b/apple/TablePro/Models/Query/SelectionSummary.swift @@ -32,8 +32,8 @@ struct SelectionSummaryInput: Sendable { let dataColumnsByDisplayPosition: [Int] let policy: SelectionSummaryColumnPolicy let deletedRowIDs: Set - let insertedRowIDs: Set - let modifiedCells: [RowID: Set] + // The cells a pending edit set to the column's default, which hold the keyword as text. + let defaultedCells: [RowID: Set] init( selection: GridSelection, @@ -42,8 +42,7 @@ struct SelectionSummaryInput: Sendable { dataColumnsByDisplayPosition: [Int], policy: SelectionSummaryColumnPolicy, deletedRowIDs: Set = [], - insertedRowIDs: Set = [], - modifiedCells: [RowID: Set] = [:] + defaultedCells: [RowID: Set] = [:] ) { self.selection = selection self.tableRows = tableRows @@ -51,8 +50,7 @@ struct SelectionSummaryInput: Sendable { self.dataColumnsByDisplayPosition = dataColumnsByDisplayPosition self.policy = policy self.deletedRowIDs = deletedRowIDs - self.insertedRowIDs = insertedRowIDs - self.modifiedCells = modifiedCells + self.defaultedCells = defaultedCells } } diff --git a/apple/TablePro/Models/Schema/ClickHousePartStatements.swift b/apple/TablePro/Models/Schema/ClickHousePartStatements.swift index 5436cfced7..c11617c1c6 100644 --- a/apple/TablePro/Models/Schema/ClickHousePartStatements.swift +++ b/apple/TablePro/Models/Schema/ClickHousePartStatements.swift @@ -2,15 +2,10 @@ // ClickHousePartStatements.swift // TablePro // -// The statements the Parts tab issues, built from the tab's own container. -// import Foundation -/// ClickHouse carries the database as a request parameter rather than in the session, and the app -/// moves that parameter whenever any tab runs somewhere else. An unqualified name therefore names -/// whichever database the connection was last pinned to, which for `DROP PARTITION` is data loss in -/// a database the user was not looking at. +// ClickHouse takes the database from a request parameter any tab can move, so every statement names its own. internal enum ClickHousePartStatements { internal static func qualifiedName( database: String, @@ -32,42 +27,49 @@ internal enum ClickHousePartStatements { internal static func dropPartition( database: String, table: String, - partition: String, - quote: (String) -> String, - escape: (String) -> String - ) -> String { + partitionId: String, + quote: (String) -> String + ) -> String? { + guard isValidPartitionId(partitionId) else { return nil } let name = qualifiedName(database: database, table: table, quote: quote) - return "ALTER TABLE \(name) DROP PARTITION '\(escape(partition))'" + return "ALTER TABLE \(name) DROP PARTITION ID '\(partitionId)'" } internal static func detachPartition( database: String, table: String, - partition: String, - quote: (String) -> String, - escape: (String) -> String - ) -> String { + partitionId: String, + quote: (String) -> String + ) -> String? { + guard isValidPartitionId(partitionId) else { return nil } let name = qualifiedName(database: database, table: table, quote: quote) - return "ALTER TABLE \(name) DETACH PARTITION '\(escape(partition))'" + return "ALTER TABLE \(name) DETACH PARTITION ID '\(partitionId)'" + } + + // The server's own rule (MergeTreePartInfo::validatePartitionID); it also keeps quotes out of the literal. + internal static func isValidPartitionId(_ id: String) -> Bool { + !id.isEmpty && id.unicodeScalars.allSatisfy { scalar in + switch scalar { + case "0"..."9", "a"..."z", "A"..."Z", "-": true + default: false + } + } } - /// A connection saved with no database of its own has none to name, and ClickHouse then resolves - /// an unqualified request against its own configured default, which `currentDatabase()` reports. - /// Comparing against an empty string instead matches nothing and empties the tab. + // With no database of its own, ClickHouse resolves a request against its configured default. private static func databasePredicate(_ database: String, escape: (String) -> String) -> String { guard !database.isEmpty else { return "currentDatabase()" } return "'\(escape(database))'" } - /// Filtered on the named database rather than `currentDatabase()`, which answers with the - /// request parameter and so listed another database's parts under this table's name. + // `partition` renders a tuple key as `(202401,'eu')` and no key as `tuple()`, so statements name `partition_id`. internal static func parts( database: String, table: String, escape: (String) -> String ) -> String { """ - SELECT partition, name, rows, bytes_on_disk, + SELECT partition, partition_id, name, rows, bytes_on_disk, toString(modification_time) AS mod_time, active FROM system.parts WHERE database = \(databasePredicate(database, escape: escape)) AND table = '\(escape(table))' diff --git a/apple/TablePro/Models/UI/MultiRowEditState.swift b/apple/TablePro/Models/UI/MultiRowEditState.swift index 26c98ce2f2..a9c0f4ae65 100644 --- a/apple/TablePro/Models/UI/MultiRowEditState.swift +++ b/apple/TablePro/Models/UI/MultiRowEditState.swift @@ -70,7 +70,7 @@ struct FieldEditState: Identifiable { var effectiveValue: String? { if isPendingDefault { - return "__DEFAULT__" + return PluginCellValue.columnDefaultKeyword } else if isPendingNull { return nil } else { @@ -244,6 +244,8 @@ final class MultiRowEditState: ObservableObject { hasCommittedEdit = true } else if originalValue == nil { isPendingNull = true + } else if expression.first == true, originalValue == PluginCellValue.columnDefaultKeyword { + isPendingDefault = true } else { pendingValue = originalValue isPendingExpression = expression.first == true @@ -508,7 +510,7 @@ final class MultiRowEditState: ObservableObject { guard index < fields.count else { return } resetPending(at: index) fields[index].isPendingDefault = true - onFieldChanged?(index, .text("__DEFAULT__"), .discrete) + onFieldExpressionChanged?(index, PluginCellValue.columnDefaultKeyword, .discrete) } func setFieldToExpression(at index: Int, expression: CellExpression) { @@ -619,7 +621,7 @@ final class MultiRowEditState: ObservableObject { columnName: field.columnName, newValue: field.effectiveValue, removesField: field.isPendingRemoval, - isExpression: field.isPendingExpression + isExpression: field.isPendingExpression || field.isPendingDefault ) } } diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 4ba0fefbde..245084252f 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -55039,6 +55039,9 @@ } } } + }, + "Couldn't Detach Partition" : { + }, "Couldn't Discard Changes" : { "localizations" : { @@ -55052,6 +55055,9 @@ }, "Couldn't download cloud-sql-proxy. The server answered with HTTP status %lld." : { + }, + "Couldn't Drop Partition" : { + }, "Couldn't Duplicate the Theme" : { @@ -55119,6 +55125,9 @@ }, "Couldn't Move the Theme to the Trash" : { + }, + "Couldn't Optimize Table" : { + }, "Couldn't publish to the team catalog" : { "localizations" : { @@ -55254,6 +55263,9 @@ }, "Couldn't Read the DDL" : { + }, + "Couldn't Read the Parts" : { + }, "Couldn't read the referenced table" : { "localizations" : { @@ -120449,6 +120461,10 @@ } } }, + "No row has this primary key, so nothing was updated. Another client may have deleted it." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "No row holds the values it was loaded with. Another client may have changed or deleted it." : { }, @@ -145185,6 +145201,12 @@ "Reconnecting (%lld of %lld)" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Reconnecting could not reopen the database %1$@: %2$@" : { + + }, + "Reconnecting could not reopen the schema %1$@: %2$@" : { + }, "Reconnecting failed after %d attempts." : { "localizations" : { @@ -174217,6 +174239,9 @@ } } } + }, + "Startup commands failed, so this session runs without them:\n%@" : { + }, "Startup SQL" : { "comment" : "A section header for the startup SQL commands of a connection.", @@ -193166,6 +193191,9 @@ } } } + }, + "The server reported a partition ID that cannot be used in a statement, so nothing was run." : { + }, "The server reported an error." : { "localizations" : { @@ -194368,6 +194396,12 @@ } } } + }, + "The startup command %@ failed, so this session runs without it." : { + + }, + "The startup command %1$@ failed (%2$@), so this session runs without it." : { + }, "The statement before it stays applied." : { "localizations" : { @@ -198452,6 +198486,9 @@ }, "This database's driver cannot write SQL into %@. Update the driver." : { + }, + "This database's driver may read the text __DEFAULT__ in %@ as the column default, so it cannot be saved as text." : { + }, "This deletes only the queries listed here, across every connection. Queries the source, date, outcome and search filters are hiding stay. You cannot undo this." : { "comment" : "Message for the \"Delete this connection's query history\" button.", @@ -213159,6 +213196,10 @@ } } }, + "Values a spreadsheet would read as formulas, written with a leading single quote: %lld. Turn off Sanitize formula-like values to export them unchanged." : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Values from %d loaded rows" : { "extractionState" : "stale", "localizations" : { diff --git a/apple/TablePro/ViewModels/SchemaEditorViewModel.swift b/apple/TablePro/ViewModels/SchemaEditorViewModel.swift index 3d86e3b1fe..5c20599902 100644 --- a/apple/TablePro/ViewModels/SchemaEditorViewModel.swift +++ b/apple/TablePro/ViewModels/SchemaEditorViewModel.swift @@ -330,7 +330,7 @@ final class SchemaEditorViewModel: ObservableObject { renameCommitted: plan.renamesFirst && committedFirst ) } catch { - failure = error.localizedDescription + failure = GatedOperationFailure(error).message return ApplyOutcome( succeeded: false, committedName: originalName ?? "", renameCommitted: false ) diff --git a/apple/TablePro/ViewModels/UserTypeEditorModel.swift b/apple/TablePro/ViewModels/UserTypeEditorModel.swift index 3d6bc764f8..ae428d77f5 100644 --- a/apple/TablePro/ViewModels/UserTypeEditorModel.swift +++ b/apple/TablePro/ViewModels/UserTypeEditorModel.swift @@ -302,7 +302,7 @@ final class UserTypeEditorModel: ObservableObject { do { outcome = try await UserTypeChangeRunner.run(plan, in: scope, connection: connection, gate: gate) } catch { - let failure = StructureApplyFailure(error) + let failure = GatedOperationFailure(error) if let message = failure.message { AlertHelper.showErrorSheet( title: String(localized: "Error Applying Changes"), diff --git a/apple/TablePro/ViewModels/UsersRolesViewModel+Changes.swift b/apple/TablePro/ViewModels/UsersRolesViewModel+Changes.swift index d54a6fc8bc..162da203eb 100644 --- a/apple/TablePro/ViewModels/UsersRolesViewModel+Changes.swift +++ b/apple/TablePro/ViewModels/UsersRolesViewModel+Changes.swift @@ -290,7 +290,7 @@ extension UsersRolesViewModel { .joined(separator: "\n") await load(forceReload: true) } catch { - applyFailure = error.localizedDescription + applyFailure = GatedOperationFailure(error).message } } diff --git a/apple/TablePro/Views/Compare/CompareRowDiffPane.swift b/apple/TablePro/Views/Compare/CompareRowDiffPane.swift index 542c2d52e9..f80cb6f1c3 100644 --- a/apple/TablePro/Views/Compare/CompareRowDiffPane.swift +++ b/apple/TablePro/Views/Compare/CompareRowDiffPane.swift @@ -208,12 +208,27 @@ internal struct CompareRowDiffPane: View { ) } + // Named by side, because the two tables usually share a name and only one database cut its stream. + private func rowCutNotices(for summary: DataDiffSummary, plan: DataComparePlan) -> [String] { + let sides: [(endpoint: DatabaseEndpoint?, cut: Int?)] = [ + (session.source, summary.sourceRowCut), (session.target, summary.targetRowCut) + ] + return sides.compactMap { side in + guard let cut = side.cut else { return nil } + let table = ([side.endpoint?.qualifiedDescription].compactMap { $0 } + [plan.table]).joined(separator: " / ") + return StreamRowCutWarning.text(table: table, limit: cut) + } + } + @ViewBuilder private func notices(for plan: DataComparePlan) -> some View { if let failure = plan.comparisonFailure { notice(failure, systemImage: "exclamationmark.octagon.fill") } if let summary = plan.summary { + ForEach(rowCutNotices(for: summary, plan: plan), id: \.self) { text in + notice(text, systemImage: "exclamationmark.triangle.fill") + } if summary.stoppedAtRowLimit { notice(rowLimitNotice(for: summary), systemImage: "info.circle.fill") } diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridCellContent.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridCellContent.swift index b32be5c107..1f918e38b5 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellContent.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellContent.swift @@ -23,16 +23,17 @@ struct DataGridCellContent { static func placeholder( for rawValue: PluginCellValue, isAbsent: Bool = false, + isExpression: Bool = false, unloadedByteCount: Int? = nil, marksEmptyText: Bool = true ) -> DataGridCellPlaceholder? { if let unloadedByteCount { return .unloaded(byteCount: unloadedByteCount) } guard !isAbsent else { return .absent } + if rawValue.isColumnDefault(isExpression: isExpression) { return .defaultMarker } switch rawValue { case .null: return .null case .text(let value): - if value == PluginCellValue.defaultMarkerText { return .defaultMarker } return value.isEmpty && marksEmptyText ? .empty : nil case .bytes: return nil diff --git a/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift b/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift index 8f7745aac0..651da4002b 100644 --- a/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift +++ b/apple/TablePro/Views/DataGrid/Cells/DataGridCellPresenter.swift @@ -250,6 +250,7 @@ final class DataGridCellPresenter { placeholder: DataGridCellContent.placeholder( for: rawValue, isAbsent: row.isAbsent(dataIndex), + isExpression: visualState.isExpression(columnIndex: dataIndex), unloadedByteCount: row.unloadedByteCount(dataIndex), marksEmptyText: pass.marksEmptyText ) @@ -297,6 +298,7 @@ final class DataGridCellPresenter { switch DataGridCellContent.placeholder( for: rawValue, isAbsent: row.isAbsent(dataIndex), + isExpression: model.changeState?.isExpression(rowID: row.id, columnIndex: dataIndex) ?? false, unloadedByteCount: row.unloadedByteCount(dataIndex), marksEmptyText: model.configuration.marksEmptyText ) { diff --git a/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+Copy.swift b/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+Copy.swift index e27584da1e..fd99df077b 100644 --- a/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+Copy.swift +++ b/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+Copy.swift @@ -133,6 +133,7 @@ extension DataGridCommands { tableRows: model.rows, displayIDs: model.order.filteredIDs, selectedDisplayIndices: displayIndices, + defaultedCells: model.changeState?.defaultedCells(in: model.rows) ?? [:], columns: projection ) guard output.rowCount > 0 else { return } diff --git a/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+SetValue.swift b/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+SetValue.swift index cc2d571948..cdfa6e7fa8 100644 --- a/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+SetValue.swift +++ b/apple/TablePro/Views/DataGrid/Commands/DataGridCommands+SetValue.swift @@ -78,7 +78,7 @@ extension DataGridCommands { private static func sampleText(ofColumn dataIndex: Int, in rows: TableRows) -> String? { for row in rows.rows where dataIndex < row.values.count { guard let text = row.values[dataIndex].asText, - !text.isEmpty, text != PluginCellValue.defaultMarkerText else { continue } + !text.isEmpty, row.values[dataIndex] != .columnDefault else { continue } return text } return nil @@ -91,7 +91,7 @@ extension DataGridCommands { case .null: return (value: .null, isExpression: false) case .defaultValue: - return (value: .text(PluginCellValue.defaultMarkerText), isExpression: false) + return (value: .columnDefault, isExpression: true) case .expression(let expression): return (value: .text(expression.sql), isExpression: true) case .uuid(let version): diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController.swift b/apple/TablePro/Views/DataGrid/DataGridViewController.swift index dd7176f7af..a3b65c5666 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController.swift @@ -388,8 +388,7 @@ final class DataGridViewController: NSViewController, DataGridMountedView { dataColumnsByDisplayPosition: model.columns.columns.map(\.dataIndex), policy: policy, deletedRowIDs: changeState?.deletedRowIDs ?? [], - insertedRowIDs: changeState?.insertedRowIDs ?? [], - modifiedCells: changeState?.modifiedCells ?? [:] + defaultedCells: changeState?.defaultedCells(in: rows) ?? [:] ) } } diff --git a/apple/TablePro/Views/DatabaseSwitcher/CreateDatabaseSheet.swift b/apple/TablePro/Views/DatabaseSwitcher/CreateDatabaseSheet.swift index 7cdc8a564d..1b6314b1ba 100644 --- a/apple/TablePro/Views/DatabaseSwitcher/CreateDatabaseSheet.swift +++ b/apple/TablePro/Views/DatabaseSwitcher/CreateDatabaseSheet.swift @@ -176,7 +176,7 @@ struct CreateDatabaseSheet: View { onCreated?(name) dismiss() } catch { - errorMessage = error.localizedDescription + errorMessage = GatedOperationFailure(error).message isCreating = false } } diff --git a/apple/TablePro/Views/Editor/StatementRunController.swift b/apple/TablePro/Views/Editor/StatementRunController.swift index 7909dcd81a..b7b0acef3c 100644 --- a/apple/TablePro/Views/Editor/StatementRunController.swift +++ b/apple/TablePro/Views/Editor/StatementRunController.swift @@ -17,7 +17,8 @@ enum StatementNavigationDirection { // The gutter's run controls and the caret's statement band both come from the scanner execution uses, so a control // never offers to run something other than what it is drawn beside. The band runs on every caret move, so a SQL -// document resolves it through `SQLStatementCursorIndex`, which rescans only from the last boundary before the caret. +// document resolves it through `SQLStatementCursorIndex`, which rescans only from the last boundary before the caret, +// and a JavaScript or Redis document through a `QueryStatementCaretTable` read once per text. // The controls need every statement, a full pass of 31ms at the size limit, so that refresh is debounced. @MainActor final class StatementRunController { @@ -44,11 +45,17 @@ final class StatementRunController { /// How the document divides into statements. JavaScript engines do not end a statement at every /// semicolon, so the gutter controls and the caret band have to ask the same splitter execution does. var statementModel: QueryStatementModel = .sql { - didSet { if statementModel != oldValue { caretIndex = nil } } + didSet { + if statementModel != oldValue { + caretIndex = nil + caretTable = nil + } + } } - // The band asks on every caret move, so a SQL document keeps its statement boundaries between edits. + // The band asks on every caret move, so a document keeps its statement boundaries between edits. private var caretIndex: SQLStatementCursorIndex? + private var caretTable: QueryStatementCaretTable? private weak var caretIndexStorage: NSTextStorage? private var caretIndexEdits: StorageEditObservation? @@ -56,6 +63,8 @@ final class StatementRunController { caretIndex?.unitsScanned ?? 0 } + private(set) var caretTableReads = 0 + /// Called with the SQL the pressed control stands for, read from the document at the moment of the press, and /// where that SQL starts in the document. /// @@ -213,6 +222,8 @@ final class StatementRunController { pendingRefresh?.cancel() pendingRefresh = nil caretIndex = nil + caretTable = nil + caretIndexStorage = nil caretIndexEdits = nil controller?.runnableStatements = [] controller?.highlightedStatementRange = nil @@ -267,7 +278,7 @@ final class StatementRunController { guard (text as NSString).length <= sizeLimit else { return nil } let statement: SQLStatementScanner.LocatedStatement - if statementModel == .sql, let storage = textView.textStorage { + if let storage = textView.textStorage { statement = indexedStatement(at: selection.range.location, in: text, storage: storage) } else { statement = QueryStatementScanner.locatedStatementAtCursor( @@ -286,19 +297,38 @@ final class StatementRunController { in text: String, storage: NSTextStorage ) -> SQLStatementScanner.LocatedStatement { - if caretIndex == nil || caretIndexStorage !== storage { - startCaretIndex(on: storage) + if caretIndexStorage !== storage { + observeEdits(on: storage) } // Inside an editing batch the text has changed but the storage has not posted its notification yet. if storage.editedMask.contains(.editedCharacters) { - caretIndex?.textDidChange(at: storage.editedRange.location) + textDidChange(at: storage.editedRange.location) + } + guard statementModel == .sql else { + if caretTable == nil { + caretTable = QueryStatementCaretTable(text: text, model: statementModel) + caretTableReads += 1 + } + return caretTable?.statement(atCursor: cursor) + ?? QueryStatementScanner.locatedStatementAtCursor( + in: text, cursorPosition: cursor, model: statementModel, grammar: grammar + ) + } + if caretIndex == nil { + caretIndex = SQLStatementCursorIndex(grammar: grammar) } return caretIndex?.statementAtCursor(cursor, in: text) ?? SQLStatementScanner.locatedStatementAtCursor(in: text, cursorPosition: cursor, grammar: grammar) } - private func startCaretIndex(on storage: NSTextStorage) { - caretIndex = SQLStatementCursorIndex(grammar: grammar) + private func textDidChange(at location: Int) { + caretIndex?.textDidChange(at: location) + caretTable = nil + } + + private func observeEdits(on storage: NSTextStorage) { + caretIndex = nil + caretTable = nil caretIndexStorage = storage // Every way the text changes passes through the storage, including the ones that skip the editor's delegate. let token = NotificationCenter.default.addObserver( @@ -313,7 +343,7 @@ final class StatementRunController { private func caretIndexStorageDidProcessEditing() { guard let storage = caretIndexStorage, storage.editedMask.contains(.editedCharacters) else { return } - caretIndex?.textDidChange(at: storage.editedRange.location) + textDidChange(at: storage.editedRange.location) } } diff --git a/apple/TablePro/Views/Main/Child/DataTabSQLExporter.swift b/apple/TablePro/Views/Main/Child/DataTabSQLExporter.swift index f9f0e6a53f..7e4e542b48 100644 --- a/apple/TablePro/Views/Main/Child/DataTabSQLExporter.swift +++ b/apple/TablePro/Views/Main/Child/DataTabSQLExporter.swift @@ -155,14 +155,10 @@ final class DataTabSQLExporter: DataGridSQLExporting { ) } - // Every marker in a row not saved yet, and in a saved row the cells a pending edit set to Default. + // The cells a pending insert or edit marked as the default. private func stagedDefaults(of row: Row, target: Target) -> Set { - let marker = PluginCellValue.text("__DEFAULT__") - if target.changeManager.insertedRowIDs.contains(row.id) { - return Set(row.values.indices.filter { row.values[$0] == marker }) - } - let pendingUpdate = target.changeManager.pendingUpdate(forRow: row.id) - return Set(pendingUpdate?.cellChanges.filter { $0.newValue == marker }.map(\.columnIndex) ?? []) + let marked = target.changeManager.expressionColumns(forRow: row.id) + return Set(marked.filter { row.values.indices.contains($0) && row.values[$0] == .columnDefault }) } private func nonEmpty(_ statements: String) -> String? { diff --git a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift index 81b48e98af..a246fc2a70 100644 --- a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift +++ b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift @@ -734,6 +734,7 @@ struct MainEditorContentView: View { selectedRowIDs: Set(model.selectedRowIDs), displayIDs: model.displayedRowIDs, deletedRowIDs: changeManager.deletedRowIDs, + defaultedCells: changeManager.defaultedCells(in: rows), valueFilter: tab.valueFilter, dataRevision: coordinator.tabSessionRegistry.session(for: tab.id)?.dataRevision ?? 0, displayRevision: coordinator.gridDisplayRevision, diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Navigation.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Navigation.swift index d7eab33615..ecd1426d4c 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Navigation.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Navigation.swift @@ -677,10 +677,13 @@ extension MainContentCoordinator { try await dropContainer(target) services.catalogChangeService.record(.containerDropped(target, connectionId: connectionId)) } catch { - navigationLogger.error( - "Failed to drop \(target.id, privacy: .public): \(error.publicLogShape, privacy: .public)" - ) - failures.append((target.name, error.localizedDescription)) + let failure = GatedOperationFailure(error) + if failure.reportsFailure { + navigationLogger.error( + "Failed to drop \(target.id, privacy: .public): \(error.publicLogShape, privacy: .public)" + ) + } + if let message = failure.message { failures.append((target.name, message)) } } } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SafeModePromptRetry.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SafeModePromptRetry.swift new file mode 100644 index 0000000000..92846198f7 --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+SafeModePromptRetry.swift @@ -0,0 +1,28 @@ +// +// MainContentCoordinator+SafeModePromptRetry.swift +// TablePro +// + +import Foundation + +extension MainContentCoordinator { + // The prompt is one per window, so a first load that meets another run's prompt waits for it to close. + func turnAwayTableLoad(tabId: UUID, trigger: TableLoadTrigger) { + traceNavigationAbandoned(tabId: tabId, outcome: .safeModePromptAlreadyOpen) + tableLoadTurnedAwayByPrompt = ParkedTableLoad(tabId: tabId, trigger: trigger) + declineTableLoad(for: tabId) + } + + func safeModePromptDidChange(wasShowing: Bool) { + guard wasShowing, !isShowingSafeModePrompt, tableLoadTurnedAwayByPrompt != nil else { return } + // Once the run that lowered it has started, so a load it starts itself is not asked for twice. + Task { @MainActor [weak self] in self?.retryTableLoadTurnedAwayByPrompt() } + } + + private func retryTableLoadTurnedAwayByPrompt() { + guard !isShowingSafeModePrompt, !isTearingDown, let turnedAway = tableLoadTurnedAwayByPrompt else { return } + tableLoadTurnedAwayByPrompt = nil + guard turnedAway.tabId == tabManager.selectedTabId else { return } + lazyLoadCurrentTabIfNeeded(trigger: turnedAway.trigger) + } +} diff --git a/apple/TablePro/Views/Main/MainContentCommandActions.swift b/apple/TablePro/Views/Main/MainContentCommandActions.swift index ec02fa09d7..0decc6483f 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions.swift @@ -66,6 +66,9 @@ final class MainContentCommandActions: ObservableObject { await RunningWorkApproval.confirm(prompt, window: window) } + // What a window close waits on while another connection's pane mounts. + var paneWaitClock: any Clock = ContinuousClock() + private var notificationTasks: [Task] = [] private var eventCancellables: Set = [] @@ -556,12 +559,7 @@ final class MainContentCommandActions: ObservableObject { // Every connection in the window: closing it closes them all, and skipping a background one is silent data loss. internal var hasUnsavedWorkInWindow: Bool { - guard let host = window?.contentViewController as? MainSplitViewController else { - return coordinator?.hasAnyUnsavedWork() ?? false - } - return host.workspaces.workspaces.contains { workspace in - workspace.sessionState?.coordinator.hasAnyUnsavedWork() == true - } + coordinatorsInWindow.contains { $0.hasAnyUnsavedWork() } } // This connection only: closing its tabs leaves another connection's pending work alone. @@ -650,13 +648,16 @@ final class MainContentCommandActions: ObservableObject { return .closed } // Work is stopped only once the close is certain: a cancelled or failed save keeps it running. - let result = await AlertHelper.confirmSaveChanges( - message: RunningWorkPrompt.saveMessage( - String(localized: "Your changes will be lost if you don't save them."), + let keepsTabsOpen = coordinatorsInWindow.contains { hosted in + hosted.tabManager.tabs.contains { hosted.savability(of: $0) == .mountedOnly } + } + let result = await confirmSaveChanges( + RunningWorkPrompt.saveMessage( + Self.unsavedWorkMessage(keepsTabsOpen: keepsTabsOpen), action: .close, alsoStopping: work ), - window: closeAnchorWindow + closeAnchorWindow ) switch result { @@ -775,13 +776,17 @@ final class MainContentCommandActions: ObservableObject { return true } + // The prompt asked about every connection in the window, so Save reaches every tab of each; one it cannot save keeps the window. private func saveAndClose(asBatchSurvivor: Bool?) async -> Bool { - guard let coordinator else { - finish(asBatchSurvivor: asBatchSurvivor) - return true + let host = window?.contentViewController as? MainSplitViewController + let shown = host?.workspaces.selectedConnectionId + for hosted in coordinatorsInWindow where hosted === coordinator || hosted.hasAnyUnsavedWork() { + // A save can raise a sheet, which has to sit over the connection it is about. + host?.workspaces.select(hosted.connectionId) + guard let actions = await shownActions(of: hosted) else { return false } + guard await actions.saveEveryTabsWork() else { return false } } - guard await applyStagedStructureEdits(in: coordinator.tabManager.tabs) else { return false } - guard await saveSelectedTabWork() else { return false } + if let shown { host?.workspaces.select(shown) } finish(asBatchSurvivor: asBatchSurvivor) return true } @@ -1407,6 +1412,38 @@ final class MainContentCommandActions: ObservableObject { } extension MainContentCommandActions { + // This connection's first. + private var coordinatorsInWindow: [MainContentCoordinator] { + guard let host = window?.contentViewController as? MainSplitViewController else { + return [coordinator].compactMap { $0 } + } + let hosted = host.workspaces.workspaces.compactMap { $0.sessionState?.coordinator } + guard let coordinator else { return hosted } + return [coordinator] + hosted.filter { $0 !== coordinator } + } + + // A connection never shown has no actions until its pane mounts, a few run loop turns after it is selected. + private func shownActions(of hosted: MainContentCoordinator) async -> MainContentCommandActions? { + if hosted === coordinator { return self } + return await Self.actions(of: hosted, waitingOn: paneWaitClock) + } + + // A cancelled close stops waiting at once rather than polling out the rest of the deadline. + private static func actions>( + of hosted: MainContentCoordinator, + waitingOn clock: C + ) async -> MainContentCommandActions? { + let deadline = clock.now.advanced(by: .seconds(2)) + while hosted.commandActions == nil, clock.now < deadline { + do { + try await clock.sleep(for: .milliseconds(20)) + } catch { + return nil + } + } + return hosted.commandActions + } + // Copy with the grid not focused takes the selected rows in the row copy format; a focused grid answers `copy:` itself. func copySelectedRowsInCopyFormat() { guard dataGridOwnsSelection, let model = coordinator?.activeGridModel else { diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index b338583e42..2c2c39815a 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -371,7 +371,12 @@ final class MainContentCoordinator: ObservableObject { // Guards re-entrant confirm dialogs (nested run loop during runModal). internal var isShowingConfirmAlert = false - internal var isShowingSafeModePrompt = false + // Lowered by whichever run raised it, so a table load it turned away is retried whatever path asked. + internal var isShowingSafeModePrompt = false { + didSet { safeModePromptDidChange(wasShowing: oldValue) } + } + + var tableLoadTurnedAwayByPrompt: ParkedTableLoad? @Published internal var rewindPlan: RewindPlan? @@ -1110,7 +1115,7 @@ final class MainContentCoordinator: ObservableObject { tab.execution.lastExecutedAt == nil { guard !isShowingSafeModePrompt else { - traceNavigationAbandoned(tabId: tab.id, outcome: .safeModePromptAlreadyOpen) + turnAwayTableLoad(tabId: tab.id, trigger: trigger) return nil } isShowingSafeModePrompt = true diff --git a/apple/TablePro/Views/ObjectCopy/CopyObjectsResultView.swift b/apple/TablePro/Views/ObjectCopy/CopyObjectsResultView.swift index 3bd997547b..38c7cf6698 100644 --- a/apple/TablePro/Views/ObjectCopy/CopyObjectsResultView.swift +++ b/apple/TablePro/Views/ObjectCopy/CopyObjectsResultView.swift @@ -41,6 +41,11 @@ internal struct CopyObjectsResultView: View { .font(.callout) .foregroundStyle(.secondary) } + ForEach(Array(result.warnings.enumerated()), id: \.offset) { _, warning in + NoticeLabel(warning, severity: .warning) + .font(.callout) + .textSelection(.enabled) + } outcomes(result) } .frame(maxWidth: .infinity, alignment: .leading) diff --git a/apple/TablePro/Views/ObjectSource/UserType/CreateUserTypeSheet.swift b/apple/TablePro/Views/ObjectSource/UserType/CreateUserTypeSheet.swift index 31526e51b4..8c0baa020c 100644 --- a/apple/TablePro/Views/ObjectSource/UserType/CreateUserTypeSheet.swift +++ b/apple/TablePro/Views/ObjectSource/UserType/CreateUserTypeSheet.swift @@ -245,11 +245,12 @@ struct CreateUserTypeSheet: View { try await UserTypeCreation.create(definition, in: scope, connection: connection) dismiss() onCreated(definition) - } catch ExecutionGateError.cancelledByUser { - return } catch { - Self.logger.error("Create Type failed: \(error.publicLogShape, privacy: .public)") - failure = error.localizedDescription + let outcome = GatedOperationFailure(error) + if outcome.reportsFailure { + Self.logger.error("Create Type failed: \(error.publicLogShape, privacy: .public)") + } + failure = outcome.message } } } diff --git a/apple/TablePro/Views/Results/ResultsJsonView.swift b/apple/TablePro/Views/Results/ResultsJsonView.swift index d8cc4d3395..6a04df3448 100644 --- a/apple/TablePro/Views/Results/ResultsJsonView.swift +++ b/apple/TablePro/Views/Results/ResultsJsonView.swift @@ -11,6 +11,7 @@ internal struct ResultsJsonView: View { let selectedRowIDs: Set let displayIDs: [RowID]? let deletedRowIDs: Set + let defaultedCells: [RowID: Set] let valueFilter: GridValueFilterState let dataRevision: Int let displayRevision: Int @@ -32,6 +33,7 @@ internal struct ResultsJsonView: View { selectedRowIDs: Set, displayIDs: [RowID]?, deletedRowIDs: Set, + defaultedCells: [RowID: Set] = [:], valueFilter: GridValueFilterState, dataRevision: Int, displayRevision: Int, @@ -41,6 +43,7 @@ internal struct ResultsJsonView: View { self.selectedRowIDs = selectedRowIDs self.displayIDs = displayIDs self.deletedRowIDs = deletedRowIDs + self.defaultedCells = defaultedCells self.valueFilter = valueFilter self.dataRevision = dataRevision self.displayRevision = displayRevision @@ -58,6 +61,7 @@ internal struct ResultsJsonView: View { let displayRevision: Int let selectedRowIDs: Set let deletedRowIDs: Set + let defaultedCells: [RowID: Set] let valueFilter: GridValueFilterState let hiddenColumns: Set let columnOrder: [String]? @@ -69,6 +73,7 @@ internal struct ResultsJsonView: View { displayRevision: displayRevision, selectedRowIDs: selectedRowIDs, deletedRowIDs: deletedRowIDs, + defaultedCells: defaultedCells, valueFilter: valueFilter, hiddenColumns: columnLayout.hiddenColumns, columnOrder: columnLayout.columnOrder @@ -202,6 +207,7 @@ internal struct ResultsJsonView: View { let ids = displayIDs let selectedIndices = Set(DataGridRowOrder(filteredIDs: ids, revision: 0).displayIndexes(of: selectedRowIDs, in: snapshot)) let deletedIDs = deletedRowIDs + let defaulted = defaultedCells let layout = columnLayout let result = await Task.detached(priority: .userInitiated) { @@ -210,6 +216,7 @@ internal struct ResultsJsonView: View { displayIDs: ids, selectedIndices: selectedIndices, deletedRowIDs: deletedIDs, + defaultedCells: defaulted, columnLayout: layout ) }.value @@ -245,6 +252,7 @@ internal struct ResultsJsonView: View { displayIDs: [RowID]?, selectedIndices: Set, deletedRowIDs: Set = [], + defaultedCells: [RowID: Set] = [:], columnLayout: ColumnLayoutState ) -> RenderedJson { let output = ResultJsonSerializer.serialize( @@ -252,6 +260,7 @@ internal struct ResultsJsonView: View { displayIDs: displayIDs, selectedDisplayIndices: selectedIndices, deletedRowIDs: deletedRowIDs, + defaultedCells: defaultedCells, columns: .fromColumnLayout(columnLayout, columns: tableRows.columns) ) return RenderedJson( diff --git a/apple/TablePro/Views/Sidebar/EditSequenceSheet.swift b/apple/TablePro/Views/Sidebar/EditSequenceSheet.swift index 11ae7629f4..c72f6f12ea 100644 --- a/apple/TablePro/Views/Sidebar/EditSequenceSheet.swift +++ b/apple/TablePro/Views/Sidebar/EditSequenceSheet.swift @@ -188,7 +188,7 @@ struct EditSequenceSheet: View { try await SequenceEditing.alter(details, to: settings, target: target, connection: connection) dismiss() } catch { - saveError = error.localizedDescription + saveError = GatedOperationFailure(error).message phase = .editing(details) } } diff --git a/apple/TablePro/Views/Sidebar/ObjectCommentSheet.swift b/apple/TablePro/Views/Sidebar/ObjectCommentSheet.swift index bdc4397a00..b73407bedf 100644 --- a/apple/TablePro/Views/Sidebar/ObjectCommentSheet.swift +++ b/apple/TablePro/Views/Sidebar/ObjectCommentSheet.swift @@ -144,10 +144,8 @@ struct ObjectCommentSheet: View { do { try await ObjectCommentEditing.setComment(draft.commentToSave, on: target, connection: connection) dismiss() - } catch ExecutionGateError.cancelledByUser { - phase = .editing } catch { - saveError = error.localizedDescription + saveError = GatedOperationFailure(error).message phase = .editing } } diff --git a/apple/TablePro/Views/Sidebar/SetSequenceValueSheet.swift b/apple/TablePro/Views/Sidebar/SetSequenceValueSheet.swift index e63e765d73..d970311881 100644 --- a/apple/TablePro/Views/Sidebar/SetSequenceValueSheet.swift +++ b/apple/TablePro/Views/Sidebar/SetSequenceValueSheet.swift @@ -176,7 +176,7 @@ struct SetSequenceValueSheet: View { try await SequenceEditing.setNextValue(nextValue, of: details, target: target, connection: connection) dismiss() } catch { - saveError = error.localizedDescription + saveError = GatedOperationFailure(error).message phase = .editing(details) } } diff --git a/apple/TablePro/Views/Structure/ClickHousePartsView.swift b/apple/TablePro/Views/Structure/ClickHousePartsView.swift index 6f37691102..f95d50739e 100644 --- a/apple/TablePro/Views/Structure/ClickHousePartsView.swift +++ b/apple/TablePro/Views/Structure/ClickHousePartsView.swift @@ -8,6 +8,56 @@ import SwiftUI import TableProPluginKit import TableProSQLGrammar +internal struct ClickHousePartsAlert: Equatable { + let title: String + let message: String +} + +// A failed or refused action never replaces the list it acted on; only a load with nothing shown does. +internal struct ClickHousePartsPaneState { + internal enum Display: Equatable { + case loading + case loadFailed(String) + case empty + case parts + } + + private(set) var parts: [ClickHousePartInfo] = [] + private(set) var isLoading = true + private(set) var loadError: String? + var alert: ClickHousePartsAlert? + + var display: Display { + guard parts.isEmpty else { return .parts } + if let loadError { return .loadFailed(loadError) } + return isLoading ? .loading : .empty + } + + mutating func beginLoad() { + isLoading = true + loadError = nil + } + + mutating func finishLoad(_ parts: [ClickHousePartInfo]) { + self.parts = parts + isLoading = false + } + + mutating func failLoad(_ message: String) { + isLoading = false + guard parts.isEmpty else { + alert = ClickHousePartsAlert(title: String(localized: "Couldn't Read the Parts"), message: message) + return + } + loadError = message + } + + mutating func actionEnded(_ failure: GatedOperationFailure, title: String) { + guard let message = failure.message else { return } + alert = ClickHousePartsAlert(title: title, message: message) + } +} + struct ClickHousePartsView: View { private static let logger = Logger(subsystem: "com.TablePro", category: "ClickHousePartsView") @@ -17,31 +67,31 @@ struct ClickHousePartsView: View { let connection: DatabaseConnection let reloadToken: Int - @State private var parts: [ClickHousePartInfo] = [] - @State private var isLoading = true - @State private var errorMessage: String? + @State private var pane = ClickHousePartsPaneState() @State private var loadedToken: Int? @StateObject private var grid = StructureListGridModel(accessibilityLabel: StructurePane.parts.displayName) @StateObject private var selection = StructureListSelection() var body: some View { VStack(spacing: 0) { - if isLoading { + switch pane.display { + case .loading: ProgressView() .frame(maxWidth: .infinity, maxHeight: .infinity) .gridStandInBackground() - } else if let error = errorMessage { - VStack(spacing: 8) { - Image(systemName: "exclamationmark.triangle") - .font(.largeTitle) - .foregroundStyle(.orange) - .accessibilityHidden(true) - RevealedTextView(error) - .foregroundStyle(.secondary) + case .loadFailed(let message): + UnavailableStateView { + Label(String(localized: "Couldn't Read the Parts"), systemImage: "exclamationmark.triangle") + } description: { + RevealedTextView(message) + } actions: { + Button(String(localized: "Try Again")) { + Task { await loadParts() } + } } .frame(maxWidth: .infinity, maxHeight: .infinity) .gridStandInBackground() - } else if parts.isEmpty { + case .empty: VStack(spacing: 8) { Image(systemName: "tray") .font(.largeTitle) @@ -52,11 +102,20 @@ struct ClickHousePartsView: View { } .frame(maxWidth: .infinity, maxHeight: .infinity) .gridStandInBackground() - } else { + case .parts: partsToolbar partsTable } } + .alert( + pane.alert?.title ?? "", + isPresented: Binding(get: { pane.alert != nil }, set: { if !$0 { pane.alert = nil } }), + presenting: pane.alert + ) { _ in + Button(String(localized: "OK"), role: .cancel) {} + } message: { alert in + Text(verbatim: RevealedText(alert.message).plainText) + } .task(id: reloadToken) { await loadPartsIfNeeded() } } @@ -92,7 +151,7 @@ struct ClickHousePartsView: View { columns: Self.columnTitles, defaultWidths: Self.columnWidths, minimumWidths: Self.columnMinimumWidths, - rows: parts.map(Self.gridRow(for:)), + rows: pane.parts.map(Self.gridRow(for:)), databaseType: connection.type, selectedIDs: selection.ids, select: { selection.ids = $0 }, @@ -130,6 +189,20 @@ struct ClickHousePartsView: View { ) } + // Column order is ClickHousePartStatements.parts. + internal static func part(from row: [PluginCellValue]) -> ClickHousePartInfo? { + guard let name = row[safe: 2]?.asText else { return nil } + return ClickHousePartInfo( + partition: row[safe: 0]?.asText ?? "", + partitionId: row[safe: 1]?.asText ?? "", + name: name, + rows: row[safe: 3]?.asText.flatMap { UInt64($0) } ?? 0, + bytesOnDisk: row[safe: 4]?.asText.flatMap { UInt64($0) } ?? 0, + modificationTime: row[safe: 5]?.asText ?? "", + active: row[safe: 6]?.asText == "1" + ) + } + // MARK: - Actions private func optimizeTable() { @@ -140,19 +213,20 @@ struct ClickHousePartsView: View { database: scope.database, table: tableName, quote: driver.quoteIdentifier ), description: String(localized: "Optimize Table"), + failureTitle: String(localized: "Couldn't Optimize Table"), kind: .maintenance ) } } private func dropSelectedPartition() { - guard let partitionValue = selectedPartitionValue() else { return } + guard let part = selectedPart() else { return } Task { @MainActor in let confirmed = await AlertHelper.confirmDestructive( title: String(localized: "Drop Partition?"), message: String( format: String(localized: "This will permanently delete all data in partition '%@'."), - partitionValue + part.partition ), confirmButton: String(localized: "Drop"), cancelButton: String(localized: "Cancel") @@ -160,27 +234,31 @@ struct ClickHousePartsView: View { guard confirmed else { return } guard let driver = DatabaseManager.shared.driver(for: scope.connectionId) else { return } - let sql = ClickHousePartStatements.dropPartition( + let failureTitle = String(localized: "Couldn't Drop Partition") + guard let sql = ClickHousePartStatements.dropPartition( database: scope.database, table: tableName, - partition: partitionValue, - quote: driver.quoteIdentifier, - escape: driver.escapeStringLiteral - ) - guard await run(sql, description: String(localized: "Drop Partition"), kind: .destructiveQuery) - else { return } + partitionId: part.partitionId, + quote: driver.quoteIdentifier + ) else { + pane.alert = ClickHousePartsAlert(title: failureTitle, message: Self.unusablePartitionIdMessage) + return + } + guard await run( + sql, description: String(localized: "Drop Partition"), failureTitle: failureTitle, kind: .destructiveQuery + ) else { return } selection.ids.removeAll() } } private func detachSelectedPartition() { - guard let partitionValue = selectedPartitionValue() else { return } + guard let part = selectedPart() else { return } Task { @MainActor in let confirmed = await AlertHelper.confirmDestructive( title: String(localized: "Detach Partition?"), message: String( format: String(localized: "This will detach partition '%@'. Data will be preserved but inaccessible until re-attached."), - partitionValue + part.partition ), confirmButton: String(localized: "Detach"), cancelButton: String(localized: "Cancel") @@ -188,22 +266,30 @@ struct ClickHousePartsView: View { guard confirmed else { return } guard let driver = DatabaseManager.shared.driver(for: scope.connectionId) else { return } - let sql = ClickHousePartStatements.detachPartition( + let failureTitle = String(localized: "Couldn't Detach Partition") + guard let sql = ClickHousePartStatements.detachPartition( database: scope.database, table: tableName, - partition: partitionValue, - quote: driver.quoteIdentifier, - escape: driver.escapeStringLiteral - ) - guard await run(sql, description: String(localized: "Detach Partition"), kind: .destructiveQuery) - else { return } + partitionId: part.partitionId, + quote: driver.quoteIdentifier + ) else { + pane.alert = ClickHousePartsAlert(title: failureTitle, message: Self.unusablePartitionIdMessage) + return + } + guard await run( + sql, description: String(localized: "Detach Partition"), failureTitle: failureTitle, kind: .destructiveQuery + ) else { return } selection.ids.removeAll() } } + private static var unusablePartitionIdMessage: String { + String(localized: "The server reported a partition ID that cannot be used in a statement, so nothing was run.") + } + @MainActor @discardableResult - private func run(_ sql: String, description: String, kind: OperationKind) async -> Bool { + private func run(_ sql: String, description: String, failureTitle: String, kind: OperationKind) async -> Bool { let scope = scope do { let decision = await ExecutionGateProvider.shared.authorize( @@ -217,10 +303,7 @@ struct ClickHousePartsView: View { operationDescription: description ) ) - guard case .authorized = decision else { - errorMessage = decision.deniedReason - return false - } + if let denial = decision.denialError { throw denial } _ = try await DatabaseManager.shared.withScopedDriver( scope: scope, route: DatabaseManager.shared.schemaChangeRoute(for: scope), @@ -228,30 +311,30 @@ struct ClickHousePartsView: View { ) { driver in try await driver.execute(query: sql) } - if kind != .maintenance { - CatalogChangeService.post( - .changed(CatalogChange(connectionId: scope.connectionId, database: scope.database, kinds: .tables)) - ) - } - await loadParts() - return true } catch { - Self.logger.error("\(description, privacy: .public) failed: \(error.publicLogShape, privacy: .public)") - errorMessage = error.localizedDescription + let failure = GatedOperationFailure(error) + if failure.reportsFailure { + Self.logger.error("\(description, privacy: .public) failed: \(error.publicLogShape, privacy: .public)") + } + pane.actionEnded(failure, title: failureTitle) return false } + if kind != .maintenance { + CatalogChangeService.post( + .changed(CatalogChange(connectionId: scope.connectionId, database: scope.database, kinds: .tables)) + ) + } + await loadParts() + return true } - private func selectedPartitionValue() -> String? { - Self.partition(of: selection.ids, in: parts) + private func selectedPart() -> ClickHousePartInfo? { + Self.selectedPart(of: selection.ids, in: pane.parts) } - internal static func partition(of selectedIDs: Set, in parts: [ClickHousePartInfo]) -> String? { - guard selectedIDs.count == 1, - let selectedId = selectedIDs.first, - let part = parts.first(where: { $0.id == selectedId }) - else { return nil } - return part.partition + internal static func selectedPart(of selectedIDs: Set, in parts: [ClickHousePartInfo]) -> ClickHousePartInfo? { + guard selectedIDs.count == 1, let selectedId = selectedIDs.first else { return nil } + return parts.first { $0.id == selectedId } } // MARK: - Data Loading @@ -263,8 +346,7 @@ struct ClickHousePartsView: View { private func loadParts() async { let token = reloadToken - isLoading = true - errorMessage = nil + pane.beginLoad() let scope = scope let tableName = tableName @@ -276,29 +358,12 @@ struct ClickHousePartsView: View { } guard !Task.isCancelled else { return } selection.ids = [] - parts = result.rows.compactMap { row -> ClickHousePartInfo? in - guard let name = row[safe: 1]?.asText else { return nil } - let partition = row[safe: 0]?.asText ?? "" - let rows = row[safe: 2]?.asText.flatMap { UInt64($0) } ?? 0 - let bytesOnDisk = row[safe: 3]?.asText.flatMap { UInt64($0) } ?? 0 - let modTime = row[safe: 4]?.asText ?? "" - let active = row[safe: 5]?.asText == "1" - return ClickHousePartInfo( - partition: partition, - name: name, - rows: rows, - bytesOnDisk: bytesOnDisk, - modificationTime: modTime, - active: active - ) - } + pane.finishLoad(result.rows.compactMap(Self.part(from:))) loadedToken = token } catch { guard !Task.isCancelled else { return } Self.logger.error("Failed to load parts: \(error.publicLogShape, privacy: .public)") - errorMessage = error.localizedDescription + pane.failLoad(error.localizedDescription) } - - isLoading = false } } diff --git a/apple/TablePro/Views/Structure/CreateTableDraft.swift b/apple/TablePro/Views/Structure/CreateTableDraft.swift index bffd4f2653..854b5fea41 100644 --- a/apple/TablePro/Views/Structure/CreateTableDraft.swift +++ b/apple/TablePro/Views/Structure/CreateTableDraft.swift @@ -469,8 +469,7 @@ internal final class CreateTableDraft: ObservableObject { coordinator: coordinator ) } catch { - Self.logger.error("Create table failed: \(error.publicLogShape, privacy: .public)") - reportFailure(error.localizedDescription, on: coordinator) + reportFailure(error: error, on: coordinator) } } } @@ -508,8 +507,7 @@ internal final class CreateTableDraft: ObservableObject { } catch let formError as PluginCreateTableFormError { form?.recordSubmissionError(formError) } catch { - Self.logger.error("Create table failed: \(error.publicLogShape, privacy: .public)") - reportFailure(error.localizedDescription, on: coordinator) + reportFailure(error: error, on: coordinator) } } } @@ -560,6 +558,15 @@ internal final class CreateTableDraft: ObservableObject { ) } + private func reportFailure(error: any Error, on coordinator: MainContentCoordinator) { + let failure = GatedOperationFailure(error) + if failure.reportsFailure { + Self.logger.error("Create table failed: \(error.publicLogShape, privacy: .public)") + } + guard let message = failure.message else { return } + reportFailure(message, on: coordinator) + } + private func reportFailure(_ message: String, on coordinator: MainContentCoordinator) { AlertHelper.showErrorSheet( title: String(localized: "Create Table Failed"), diff --git a/apple/TablePro/Views/Structure/StructureEditingSession+Apply.swift b/apple/TablePro/Views/Structure/StructureEditingSession+Apply.swift index b760819838..15048571f6 100644 --- a/apple/TablePro/Views/Structure/StructureEditingSession+Apply.swift +++ b/apple/TablePro/Views/Structure/StructureEditingSession+Apply.swift @@ -31,52 +31,13 @@ internal enum StructureSaveOutcome: Equatable { } } -/// How a save that did not apply ends, read from the error alone so it can be tested without -/// presenting anything. -/// -/// The gate's sheet is the one confirmation a save gets, so Cancel there is the ordinary way to -/// back out, and it ends quietly the way closing any confirmation does. Answering it with an -/// "Error Applying Changes" sheet put a second dialog in front of the user for the choice they had -/// just made. -internal enum StructureApplyFailure: Equatable { - case cancelledByUser - case refused(String) - case failed(String) - - internal init(_ error: any Error) { - guard let gateError = error as? ExecutionGateError else { - self = .failed(error.localizedDescription) - return - } - switch gateError { - case .cancelledByUser: - self = .cancelledByUser - case .denied(let reason): - self = .refused(reason) - } - } - - internal var outcome: StructureSaveOutcome { +internal extension GatedOperationFailure { + var outcome: StructureSaveOutcome { switch self { case .cancelledByUser, .refused: .refused case .failed(let message): .failed(message) } } - - /// What the error sheet says. Nil for a Cancel, which shows nothing. - internal var message: String? { - switch self { - case .cancelledByUser: nil - case .refused(let reason), .failed(let reason): reason - } - } - - /// A refusal and a Cancel both stop at the gate, before the first statement, so neither is a - /// failed operation to report or a reason to reload the catalog. - internal var reportsFailure: Bool { - if case .failed = self { return true } - return false - } } internal extension StructureEditingSession { @@ -159,7 +120,7 @@ internal extension StructureEditingSession { // A failed save that may have written; its error waits for the table to be read again. private struct AlterFailure { - let failure: StructureApplyFailure + let failure: GatedOperationFailure let startedAt: ContinuousClock.Instant } @@ -240,7 +201,7 @@ internal extension StructureEditingSession { report(.succeeded(OperationSummary()), startedAt: operationStart, coordinator: coordinator) return (.applied, nil) } catch { - let failure = StructureApplyFailure(error) + let failure = GatedOperationFailure(error) // A gate refusal and a driver's refusal both stop before the first statement. guard failure.reportsFailure, !(error is SchemaOperationRefusedError) else { present(failure, startedAt: operationStart, coordinator: coordinator) @@ -251,7 +212,7 @@ internal extension StructureEditingSession { } private func present( - _ failure: StructureApplyFailure, + _ failure: GatedOperationFailure, note: String? = nil, startedAt: ContinuousClock.Instant, coordinator: MainContentCoordinator? @@ -340,7 +301,7 @@ internal extension StructureEditingSession { } catch { changeManager.releaseHold(hold, written: false) isApplying = false - let failure = StructureApplyFailure(error) + let failure = GatedOperationFailure(error) if failure.reportsFailure { CatalogChangeService.post( .changed(CatalogChange(connectionId: connection.id, database: prepared.scope.database, kinds: .tables)) diff --git a/apple/TablePro/Views/Structure/StructureEditingSession+ColumnReorder.swift b/apple/TablePro/Views/Structure/StructureEditingSession+ColumnReorder.swift index 30c1ff93cc..47f269457e 100644 --- a/apple/TablePro/Views/Structure/StructureEditingSession+ColumnReorder.swift +++ b/apple/TablePro/Views/Structure/StructureEditingSession+ColumnReorder.swift @@ -65,7 +65,7 @@ internal extension StructureEditingSession { ) await finishColumnReorder(prepared, clearTarget: clearTarget) } catch { - if StructureApplyFailure(error).reportsFailure { + if GatedOperationFailure(error).reportsFailure { CatalogChangeService.post( .changed( CatalogChange( @@ -115,7 +115,7 @@ internal extension StructureEditingSession { } private func reportColumnReorderFailure(_ error: any Error) { - guard let message = StructureApplyFailure(error).message else { return } + guard let message = GatedOperationFailure(error).message else { return } AlertHelper.showErrorSheet( title: String(localized: "Column Reorder Failed"), message: message, diff --git a/apple/TablePro/Views/Structure/TriggerDetailView.swift b/apple/TablePro/Views/Structure/TriggerDetailView.swift index a0885aac43..34a9be327b 100644 --- a/apple/TablePro/Views/Structure/TriggerDetailView.swift +++ b/apple/TablePro/Views/Structure/TriggerDetailView.swift @@ -277,7 +277,7 @@ struct TriggerDetailView: View { name: trigger.name ) } catch { - actionError = error.localizedDescription + actionError = GatedOperationFailure(error).message } } } diff --git a/apple/TablePro/Views/Structure/TriggerEditorView.swift b/apple/TablePro/Views/Structure/TriggerEditorView.swift index 6dee7b596e..ef9b441168 100644 --- a/apple/TablePro/Views/Structure/TriggerEditorView.swift +++ b/apple/TablePro/Views/Structure/TriggerEditorView.swift @@ -29,7 +29,12 @@ struct TriggerEditorView: View { @State private var isApplying = false @State private var errorMessage: String? @State private var droppedDefinition: String? - @AppStorage("structureCodeFontSize", store: AppStorageEnvironment.shared.defaults) private var fontSize: Double = 13 + @AppStorage(TriggerEditorView.fontSizeKey, store: AppStorageEnvironment.shared.defaults) + private var fontSize = TriggerEditorView.defaultFontSize + + // The Structure tab's DDL pane writes its zoom here too. + static let fontSizeKey = "structureCodeFontSize" + static let defaultFontSize = 13.0 init( scope: DatabaseScope, @@ -45,7 +50,14 @@ struct TriggerEditorView: View { self.mode = mode self.onClose = onClose self._sql = State(wrappedValue: initialSQL) - self._editorConfiguration = State(wrappedValue: Self.makeConfiguration(fontSize: 13)) + self._editorConfiguration = State(wrappedValue: Self.initialConfiguration()) + } + + // The State is seeded once, here, so it reads the store; `fontSize` only follows later zooms. + static func initialConfiguration( + in defaults: UserDefaults = AppStorageEnvironment.shared.defaults + ) -> SourceEditorConfiguration { + makeConfiguration(fontSize: defaults.object(forKey: fontSizeKey) as? Double ?? defaultFontSize) } var body: some View { @@ -145,7 +157,7 @@ struct TriggerEditorView: View { if let dropped = (error as? TriggerEditingError)?.droppedDefinition { droppedDefinition = dropped } - errorMessage = error.localizedDescription + errorMessage = GatedOperationFailure(error).message } } } diff --git a/apple/TableProMobile/TableProMobile/Completion/QueryCompletionCatalog.swift b/apple/TableProMobile/TableProMobile/Completion/QueryCompletionCatalog.swift index e7c5b14de0..9b87a2bce5 100644 --- a/apple/TableProMobile/TableProMobile/Completion/QueryCompletionCatalog.swift +++ b/apple/TableProMobile/TableProMobile/Completion/QueryCompletionCatalog.swift @@ -21,22 +21,39 @@ actor QueryCompletionCatalog: SQLCompletionCatalog { let columns: [ColumnInfo] } - private var tables: [TableInfo] + private var tables: [TableInfo] { + didSet { + cachedTableItems = nil + cachedTableColumnItems = nil + } + } + // Every table-slot request reads all of them, so they are built once per table list. + private var cachedTableItems: [SQLCompletionItem]? private let databaseType: DatabaseType private let schema: String? private let readColumns: QueryColumnReader private let readCatalogColumns: QueryColumnReader? private var columnReadsAllowed = true private var columnGeneration = 0 - private var columns: [ColumnKey: CachedColumns] = [:] + private var columns: [ColumnKey: CachedColumns] = [:] { + didSet { cachedTableColumnItems = nil } + } + // Every column request before a FROM reads all of them, so they are built once per column cache, table list and + // namespace list. + private var cachedTableColumnItems: [SQLCompletionItem]? + private(set) var tableColumnItemsBuildCount = 0 private var reads: [ColumnKey: Task<[ColumnInfo]?, any Error>] = [:] private var sessionReads: [ColumnKey: Task<[ColumnInfo]?, any Error>] = [:] private let readSchemaTables: QuerySchemaTableReader? private let readCatalogSchemaTables: QuerySchemaTableReader? private var sessionSchemaReads: [String: Task<[TableInfo]?, any Error>] = [:] private var sessionLocalTables: Set = [] - private var schemas: [String] - private var databases: [String] + private var schemas: [String] { + didSet { cachedTableColumnItems = nil } + } + private var databases: [String] { + didSet { cachedTableColumnItems = nil } + } private var schemaTables: [String: (generation: Int, tables: [TableInfo])] = [:] private var schemaTableReads: [String: Task<[TableInfo]?, any Error>] = [:] @@ -123,19 +140,21 @@ actor QueryCompletionCatalog: SQLCompletionCatalog { } func tableCompletionItems() -> [SQLCompletionItem] { - tables.map { table in - let isView = table.type == .view || table.type == .materializedView - return SQLCompletionItem(label: table.name, kind: isView ? .view : .table, insertText: quoted(table.name)) - } + if let cachedTableItems { return cachedTableItems } + let items = tables.map { tableItem($0) } + cachedTableItems = items + return items + } + + private func tableItem(_ table: TableInfo) -> SQLCompletionItem { + let isView = table.type == .view || table.type == .materializedView + return SQLCompletionItem(label: table.name, kind: isView ? .view : .table, insertText: quoted(table.name)) } func tableCompletionItems(inSchema schema: String) async -> [SQLCompletionItem] { guard isKnownSchema(schema) else { return [] } guard !isActiveSchema(schema) else { return tableCompletionItems() } - return await tables(inSchema: schema).map { table in - let isView = table.type == .view || table.type == .materializedView - return SQLCompletionItem(label: table.name, kind: isView ? .view : .table, insertText: quoted(table.name)) - } + return await tables(inSchema: schema).map { tableItem($0) } } func schemaCompletionItems() -> [SQLCompletionItem] { @@ -317,6 +336,14 @@ actor QueryCompletionCatalog: SQLCompletionCatalog { } func allColumnsFromCachedTables() -> [SQLCompletionItem] { + if let cachedTableColumnItems { return cachedTableColumnItems } + let items = buildTableColumnItems() + tableColumnItemsBuildCount += 1 + cachedTableColumnItems = items + return items + } + + private func buildTableColumnItems() -> [SQLCompletionItem] { // Sorted, because the ranker keeps emission order between equal scores and a dictionary's order is per process. let cached = columns.sorted { lhs, rhs in (lhs.key.schema ?? "", lhs.key.table) < (rhs.key.schema ?? "", rhs.key.table) diff --git a/apple/TableProMobile/TableProMobileTests/Completion/QueryCompletionCatalogTests.swift b/apple/TableProMobile/TableProMobileTests/Completion/QueryCompletionCatalogTests.swift index f5322a5f2e..5dd5104935 100644 --- a/apple/TableProMobile/TableProMobileTests/Completion/QueryCompletionCatalogTests.swift +++ b/apple/TableProMobile/TableProMobileTests/Completion/QueryCompletionCatalogTests.swift @@ -400,6 +400,35 @@ struct QueryCompletionCatalogTests { #expect(await source.reads == ["orders", "customers"]) } + @Test("cached column suggestions are built once, and again only after a read, a table list or a namespace list") + func cachedColumnsAreBuiltOnce() async { + let source = CatalogColumnSource(["orders": [column("id")], "customers": [column("name")]]) + let catalog = catalog(source, tables: [table("orders"), table("customers")]) + _ = await catalog.columnCompletionItems(for: "orders", schema: nil) + + let first = await catalog.allColumnsFromCachedTables() + let second = await catalog.allColumnsFromCachedTables() + #expect(labels(second) == labels(first)) + #expect(await catalog.tableColumnItemsBuildCount == 1) + + _ = await catalog.columnCompletionItems(for: "customers", schema: nil) + let afterRead = await catalog.allColumnsFromCachedTables() + #expect(labels(afterRead) == ["name", "id"]) + #expect(await catalog.tableColumnItemsBuildCount == 2) + + let listedBefore = await catalog.tableCompletionItems() + #expect(labels(listedBefore) == ["orders", "customers"]) + await catalog.replaceTables([table("orders"), table("customers"), table("refunds")]) + _ = await catalog.allColumnsFromCachedTables() + #expect(await catalog.tableColumnItemsBuildCount == 3) + let listedAfter = await catalog.tableCompletionItems() + #expect(labels(listedAfter) == ["orders", "customers", "refunds"]) + + await catalog.replaceNamespaces(schemas: ["main"], databases: []) + _ = await catalog.allColumnsFromCachedTables() + #expect(await catalog.tableColumnItemsBuildCount == 4) + } + @Test("the catalog connection reads first and is not closed by a run; the session reads only without one") func catalogConnectionFirst() async { let catalogSource = CatalogColumnSource(["orders": [column("id")]]) diff --git a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorBitColumnTests.swift b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorBitColumnTests.swift index d3d4c2d300..aed8b0f01c 100644 --- a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorBitColumnTests.swift +++ b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorBitColumnTests.swift @@ -92,8 +92,8 @@ struct SQLStatementGeneratorBitColumnTests { let rowID = RowID.inserted(UUID()) let statements = generator.generateStatements( - from: [RowChange(rowID: rowID, type: .insert)], - insertedRowData: [rowID: ["__DEFAULT__", "5", "Ann"]], + from: [RowChange(rowID: rowID, type: .insert, expressionColumns: [0])], + insertedRowData: [rowID: [.columnDefault, "5", "Ann"]], deletedRowIDs: [], insertedRowIDs: [rowID] ) diff --git a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorRowMatchTests.swift b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorRowMatchTests.swift index 44d6a863ef..ea1bbb0f87 100644 --- a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorRowMatchTests.swift +++ b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorRowMatchTests.swift @@ -62,10 +62,12 @@ struct SQLStatementGeneratorRowMatchTests { @Test("A Databend row left entirely on defaults names one column with DEFAULT") func databendAllDefaultsInsert() throws { - let change = RowChange(rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil) + let change = RowChange( + rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil, expressionColumns: [0, 1, 2, 3] + ) let statements = try generator(excluding: []).generateStatements( from: [change], - insertedRowData: [.existing(0): [.text("__DEFAULT__"), .text("__DEFAULT__"), .text("__DEFAULT__"), .text("__DEFAULT__")]], + insertedRowData: [.existing(0): [.columnDefault, .columnDefault, .columnDefault, .columnDefault]], deletedRowIDs: [], insertedRowIDs: [.existing(0)] ) diff --git a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorTests.swift b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorTests.swift index edeb219dea..731615a286 100644 --- a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorTests.swift +++ b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorTests.swift @@ -89,14 +89,14 @@ struct SQLStatementGeneratorTests { #expect(statements[0].parameters[2] == nil) } - @Test("Insert skips __DEFAULT__ columns") + @Test("Insert leaves out a column marked as its default") func testInsertSkipsDefaultColumns() throws { let generator = try makeGenerator() let insertedRowData: [RowID: [PluginCellValue]] = [ - .existing(0): ["__DEFAULT__", "John", "john@example.com"] + .existing(0): [.columnDefault, "John", "john@example.com"] ] let changes: [RowChange] = [ - RowChange(rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil) + RowChange(rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil, expressionColumns: [0]) ] let statements = generator.generateStatements( @@ -114,11 +114,35 @@ struct SQLStatementGeneratorTests { #expect(stmt.parameters.count == 2) } + @Test("Insert binds text that only reads like the default, unmarked", arguments: ["__DEFAULT__", "DEFAULT"]) + func testInsertBindsUnmarkedDefaultText(text: String) throws { + let generator = try makeGenerator() + let insertedRowData: [RowID: [PluginCellValue]] = [ + .existing(0): [.columnDefault, .text(text), "john@example.com"] + ] + let changes: [RowChange] = [ + RowChange(rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil, expressionColumns: [0]) + ] + + let statements = generator.generateStatements( + from: changes, + insertedRowData: insertedRowData, + deletedRowIDs: [], + insertedRowIDs: [.existing(0)] + ) + + #expect(statements.count == 1) + let stmt = statements[0] + #expect(!stmt.sql.contains("`id`")) + #expect(stmt.sql.contains("`name`")) + #expect(stmt.parameters.compactMap { $0 as? String } == [text, "john@example.com"]) + } + /// This used to expect no statement at all, which dropped the row from the batch while the rest /// of the save committed and reported success. A table of nothing but an identity column and /// defaults is exactly the shape that produces it. @Test( - "Insert with all __DEFAULT__ names no column", + "Insert with every column marked as its default names no column", arguments: [ (DatabaseType.mysql, "() VALUES ()"), (DatabaseType.postgresql, "DEFAULT VALUES"), @@ -128,10 +152,10 @@ struct SQLStatementGeneratorTests { func testInsertAllDefaultNamesNoColumn(databaseType: DatabaseType, expected: String) throws { let generator = try makeGenerator(databaseType: databaseType) let insertedRowData: [RowID: [PluginCellValue]] = [ - .existing(0): ["__DEFAULT__", "__DEFAULT__", "__DEFAULT__"] + .existing(0): [.columnDefault, .columnDefault, .columnDefault] ] let changes: [RowChange] = [ - RowChange(rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil) + RowChange(rowID: .existing(0), type: .insert, cellChanges: [], originalRow: nil, expressionColumns: [0, 1, 2]) ] let statements = generator.generateStatements( @@ -378,7 +402,7 @@ struct SQLStatementGeneratorTests { #expect(statements[0].parameters[0] == nil) } - @Test("Update with __DEFAULT__ value") + @Test("Update with a cell marked as its default") func testUpdateWithDefaultValue() throws { let generator = try makeGenerator() let changes: [RowChange] = [ @@ -386,7 +410,10 @@ struct SQLStatementGeneratorTests { rowID: .existing(0), type: .update, cellChanges: [ - CellChange(columnIndex: 1, columnName: "name", oldValue: "John", newValue: "__DEFAULT__") + CellChange( + columnIndex: 1, columnName: "name", oldValue: "John", newValue: .columnDefault, + newIsExpression: true + ) ], originalRow: ["1", "John", "john@example.com"] ) @@ -405,6 +432,32 @@ struct SQLStatementGeneratorTests { #expect(stmt.parameters.count == 1) } + @Test("Update binds typed text that only reads like the default", arguments: ["__DEFAULT__", "DEFAULT"]) + func testUpdateBindsUnmarkedDefaultText(text: String) throws { + let generator = try makeGenerator() + let changes: [RowChange] = [ + RowChange( + rowID: .existing(0), + type: .update, + cellChanges: [CellChange(columnIndex: 1, columnName: "name", oldValue: "John", newValue: .text(text))], + originalRow: ["1", "John", "john@example.com"] + ) + ] + + let statements = generator.generateStatements( + from: changes, + insertedRowData: [:], + deletedRowIDs: [], + insertedRowIDs: [] + ) + + #expect(statements.count == 1) + let stmt = statements[0] + #expect(stmt.sql.contains("`name` = ?")) + #expect(!stmt.sql.contains("DEFAULT")) + #expect(stmt.parameters.compactMap { $0 as? String } == [text, "1"]) + } + @Test("Update with a cell marked as SQL inlines it") func testUpdateWithSQLFunction() throws { let generator = try makeGenerator() diff --git a/apple/TableProTests/Core/Compare/CompareDriverRowCutTests.swift b/apple/TableProTests/Core/Compare/CompareDriverRowCutTests.swift new file mode 100644 index 0000000000..1d8668db4f --- /dev/null +++ b/apple/TableProTests/Core/Compare/CompareDriverRowCutTests.swift @@ -0,0 +1,90 @@ +// +// CompareDriverRowCutTests.swift +// TableProTests +// + +import Foundation +import TableProPluginKit +import Testing + +@testable import TablePro + +struct CompareDriverRowCutTests { + private func stream(rowLimit: Int?, ids: [Int]) -> AsyncThrowingStream { + var header = PluginStreamHeader(columns: ["id", "name"], columnTypeNames: ["integer", "text"]) + header.rowLimit = rowLimit + let rows: [PluginRow] = ids.map { [.text(String($0)), .text("n")] } + return AsyncThrowingStream { continuation in + continuation.yield(.header(header)) + continuation.yield(.rows(rows)) + continuation.finish() + } + } + + private func targetRows(_ ids: [Int]) -> [DataRow] { + ids.map { DataRow(values: ["id": .text(String($0)), "name": .text("n")]) } + } + + private func engine() -> DataDiffEngine { + DataDiffEngine( + options: DataCompareOptions(), + shape: DataComparisonShape( + keyColumns: ["id"], + keyOrders: [.numeric], + comparedColumns: ["name"], + digestColumns: ["id", "name"] + ) + ) + } + + @Test("A stream that ends at the limit its driver declares reads as cut there") + func streamEndingAtTheDeclaredLimitIsCut() async throws { + let provider = StreamingRowProvider(stream: stream(rowLimit: 2, ids: [1, 2])) + while try await provider.nextRow() != nil {} + + #expect(provider.endedAtRowLimit) + #expect(provider.driverRowCut == 2) + } + + @Test("A stream that ends short of its declared limit read every row") + func streamShortOfTheDeclaredLimitIsWhole() async throws { + let provider = StreamingRowProvider(stream: stream(rowLimit: 5, ids: [1, 2])) + while try await provider.nextRow() != nil {} + + #expect(!provider.endedAtRowLimit) + #expect(provider.driverRowCut == nil) + } + + // A HANA source stops at 5,000,000 rows: every target row past them read as a delete, and a sync removed them. + @Test("A source the driver cut short reports no deletes past the cut") + func cutSourceProducesNoDeletesPastIt() async throws { + let summary = try await engine().compare( + source: StreamingRowProvider(stream: stream(rowLimit: 2, ids: [1, 2])), + target: ArrayRowProvider(rows: targetRows([1, 2, 3, 4])) + ) + + #expect(summary.deleteCount == 0) + #expect(summary.identicalCount == 2) + #expect(summary.stoppedAtRowLimit) + #expect(summary.sourceRowCut == 2) + #expect(summary.targetRowCut == nil) + #expect(summary.resumeKey == [.text("2")]) + } + + @Test("A target the driver cut short reports no inserts past the cut") + func cutTargetProducesNoInsertsPastIt() async throws { + let summary = try await engine().compare( + source: ArrayRowProvider(rows: targetRows([1, 2, 3, 4])), + target: StreamingRowProvider(stream: stream(rowLimit: 2, ids: [1, 2])) + ) + + #expect(summary.insertCount == 0) + #expect(summary.targetRowCut == 2) + #expect(summary.stoppedAtRowLimit) + } + + @Test("The cut is named in the wording the export uses") + func cutWordingMatchesTheExport() { + #expect(StreamRowCutWarning.text(table: "orders", limit: 5_000_000).hasPrefix("orders: ")) + } +} diff --git a/apple/TableProTests/Core/Compare/CompareSQLLiteralTests.swift b/apple/TableProTests/Core/Compare/CompareSQLLiteralTests.swift index d6ba71ac85..04baec1f63 100644 --- a/apple/TableProTests/Core/Compare/CompareSQLLiteralTests.swift +++ b/apple/TableProTests/Core/Compare/CompareSQLLiteralTests.swift @@ -99,14 +99,27 @@ struct CompareSQLLiteralTests { #expect(CompareSQLLiteral.prefixed("0x89504E47", databaseType: .mssql) == "0x89504E47") } + // `'\x…'::bytea` reads its backslash by `standard_conforming_strings`, and with that off a sync stored other bytes. @Test - func postgresFamilyUsesAByteaCast() throws { - for type in [DatabaseType.postgresql, .cockroachdb, .redshift, .pglite, .kingbasees] { + func postgresFamilyDecodesHex() throws { + for type in [DatabaseType.postgresql, .cockroachdb, .pglite] { let literal = try #require(CompareSQLLiteral.binaryLiteral(for: png, databaseType: type)) - #expect(literal == "'\\x89504e47'::bytea", "\(type.rawValue) uses a bytea literal") + #expect(literal == "decode('89504E47', 'hex')", "\(type.rawValue) decodes hex") } } + // KingbaseES documents `decode` only as Oracle's DECODE; its bytea decoder is not the PostgreSQL one. + @Test + func kingbaseKeepsTheByteaLiteral() { + #expect(CompareSQLLiteral.binaryLiteral(for: png, databaseType: .kingbasees) == "'\\x89504E47'::bytea") + } + + // Redshift has no bytea, and its DECODE is the CASE-like one; a binary column there is VARBYTE. + @Test + func redshiftReadsHexIntoVarbyte() { + #expect(CompareSQLLiteral.binaryLiteral(for: png, databaseType: .redshift) == "from_hex('89504E47')") + } + @Test func sQLServerUsesZeroX() throws { let literal = try #require(CompareSQLLiteral.binaryLiteral(for: png, databaseType: .mssql)) @@ -121,6 +134,12 @@ struct CompareSQLLiteralTests { #expect(literal == "HEXTORAW('89504E47')") } + // `HEXTORAW('')` stores NULL, and a `BLOB NOT NULL` refuses it. + @Test + func oracleWritesAnEmptyValueAsAnEmptyBlob() { + #expect(CompareSQLLiteral.binaryLiteral(for: Data(), databaseType: .oracle) == "EMPTY_BLOB()") + } + /// An engine this build does not name falls back to the driver's own spelling rather than /// guessing at one, which is why the helper returns nil instead of a default. @Test @@ -132,6 +151,6 @@ struct CompareSQLLiteralTests { func emptyDataStillProducesAValidLiteral() throws { let literal = try #require(CompareSQLLiteral.binaryLiteral(for: Data(), databaseType: .postgresql)) - #expect(literal == "'\\x'::bytea") + #expect(literal == "decode('', 'hex')") } } diff --git a/apple/TableProTests/Core/Compare/DataSyncScriptBuilderTests.swift b/apple/TableProTests/Core/Compare/DataSyncScriptBuilderTests.swift index 72c7c861e1..517a89829e 100644 --- a/apple/TableProTests/Core/Compare/DataSyncScriptBuilderTests.swift +++ b/apple/TableProTests/Core/Compare/DataSyncScriptBuilderTests.swift @@ -795,7 +795,7 @@ final class DataSyncScriptBuilderColumnTests: XCTestCase { XCTAssertEqual( statements.map(\.sql), - [#"INSERT INTO "public"."files" ("id", "blob") VALUES (1, '\x8950'::bytea)"#] + [#"INSERT INTO "public"."files" ("id", "blob") VALUES (1, decode('8950', 'hex'))"#] ) } diff --git a/apple/TableProTests/Core/Coordinators/RowEditingCoordinatorUUIDKeyTests.swift b/apple/TableProTests/Core/Coordinators/RowEditingCoordinatorUUIDKeyTests.swift index 71a31a2c8f..1386171d49 100644 --- a/apple/TableProTests/Core/Coordinators/RowEditingCoordinatorUUIDKeyTests.swift +++ b/apple/TableProTests/Core/Coordinators/RowEditingCoordinatorUUIDKeyTests.swift @@ -71,7 +71,7 @@ struct RowEditingCoordinatorUUIDKeyTests { coordinator.addNewRow() - #expect(try #require(lastRow(of: coordinator)).values[0] == .text("__DEFAULT__")) + #expect(try #require(lastRow(of: coordinator)).values[0] == .columnDefault) } @Test("Add Row on an integer key leaves it as before") diff --git a/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift b/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift index fb7a319753..10d189f39a 100644 --- a/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift +++ b/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift @@ -277,6 +277,87 @@ struct RewindPlannerTests { #expect(queries.first?.contains("SELECT *") == false) } + @Test("An edit that changed the key is read and restored under the key the save gave it") + func movedKeyIsFoundUnderItsNewKey() throws { + let moved = operation(kind: .update, preImage: ["k5", "Ada"], postImage: ["k6", "Ada"], writtenColumns: ["id"]) + let rewind = planner(operations: [moved]) + + let query = try #require(rewind.readQueries().first) + #expect(query.contains("k6")) + #expect(query.contains("k5")) + + let plan = try rewind.plan(currentRows: [RewindCurrentRow(values: ["k6", "Ada"])]) + #expect(plan.rows.map { $0.outcome } == [.willRestore]) + #expect(plan.statements.first?.sql.hasPrefix("UPDATE") == true) + #expect(plan.statements.first?.parameters.compactMap { $0 as? String } == ["k5", "k6"]) + } + + @Test("A moved row a rewind already put back reads as restored, and another row on its old key does not") + func movedKeyAlreadyRestored() throws { + let moved = operation( + kind: .update, preImage: ["k5", "Ada"], postImage: ["k6", "Grace"], writtenColumns: ["id", "name"] + ) + + let restored = try planner(operations: [moved]).plan(currentRows: [RewindCurrentRow(values: ["k5", "Ada"])]) + #expect(restored.rows.map { $0.outcome } == [.alreadyRestored]) + #expect(restored.statements.isEmpty) + + let taken = try planner(operations: [moved]).plan(currentRows: [RewindCurrentRow(values: ["k5", "Bo"])]) + #expect(taken.rows.map { $0.outcome } == [.rowMissing]) + #expect(taken.statements.isEmpty) + } + + @Test("A row that took the key a save moved to is left alone, though the save wrote only the key") + func rowOnTheMovedKeyIsNotRewritten() throws { + let moved = operation(kind: .update, preImage: ["k5", "Ada"], postImage: ["k6", "Ada"], writtenColumns: ["id"]) + + let plan = try planner(operations: [moved]).plan(currentRows: [RewindCurrentRow(values: ["k6", "Bo"])]) + + #expect(plan.rows.map { $0.outcome } == [.changedSinceSave]) + #expect(plan.statements.isEmpty) + } + + @Test("A moved row is not held to a column the server computes") + func movedKeyIgnoresGeneratedColumns() throws { + let columns = ["id", "name", "updated_at"] + let moved = RowWriteOperation( + kind: .update, target: target, columns: columns, primaryKeyColumns: ["id"], + preImage: ["k5", "Ada", "2026-01-01"], postImage: ["k6", "Ada", "2026-01-01"], + writtenColumns: ["id"], refusal: nil + ) + let record = RewindRecord( + id: UUID(), historyId: nil, connectionId: UUID(), databaseType: .sqlite, + target: target, capturedAt: Date(timeIntervalSince1970: 0), + generatedColumns: ["updated_at"], identityColumns: [], operations: [moved] + ) + let planner = RewindPlanner( + record: record, + factory: RowChangeStatementFactory( + tableName: target.table, schemaName: nil, columns: columns, primaryKeyColumns: ["id"], + generatedColumns: ["updated_at"], databaseType: .sqlite, pluginDriver: nil + ), + queryBuilder: sqliteQueryBuilder + ) + + let plan = try planner.plan(currentRows: [RewindCurrentRow(values: ["k6", "Ada", "2026-06-30"])]) + + #expect(plan.rows.map { $0.outcome } == [.willRestore]) + } + + @Test("Undoing a restore takes back what the restore did last first") + func inverseRecordRunsBackwards() throws { + let deleted = operation(kind: .delete, preImage: ["7", "Ada"], postImage: nil, writtenColumns: columns) + let inserted = operation(kind: .insert, preImage: nil, postImage: ["8", "Ada"], writtenColumns: columns) + let plan = try planner(operations: [deleted, inserted]) + .plan(currentRows: [RewindCurrentRow(values: ["8", "Ada"])]) + + let inverse = RewindExecutor.inverseOperations(of: plan) + #expect(inverse.map { $0.kind } == [.delete, .insert]) + + let undo = try planner(operations: inverse).plan(currentRows: [RewindCurrentRow(values: ["7", "Ada"])]) + #expect(undo.statements.map { String($0.sql.prefix(6)) } == ["DELETE", "INSERT"]) + } + @Test("The inverse runs newest first, so a value one row freed is not taken before the other gives it up") func inverseRunsInReverseOrder() throws { let first = operation(kind: .delete, preImage: ["7", "Ada"], postImage: nil, writtenColumns: columns) diff --git a/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryCoverageTests.swift b/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryCoverageTests.swift index e688dff4ff..570e20367b 100644 --- a/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryCoverageTests.swift +++ b/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryCoverageTests.swift @@ -255,8 +255,8 @@ struct RowChangeStatementFactoryCoverageTests { let engine = DatabaseType(rawValue: "ProbeEngine") let hostOnly = LegacyStatementStubDriver { _, _, _, _ in nil } let inserted = RowID.inserted(UUID()) - let allDefaults: [PluginCellValue] = [.text("__DEFAULT__"), .text("__DEFAULT__")] - let changes = [nameEdit(), RowChange(rowID: inserted, type: .insert)] + let allDefaults: [PluginCellValue] = [.columnDefault, .columnDefault] + let changes = [nameEdit(), RowChange(rowID: inserted, type: .insert, expressionColumns: [0, 1])] let generator = try SQLStatementGenerator( tableName: "t", columns: columns, primaryKeyColumns: ["_id"], databaseType: engine, diff --git a/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryExpressionTests.swift b/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryExpressionTests.swift index 93c24b8d32..0109aeb1ec 100644 --- a/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryExpressionTests.swift +++ b/apple/TableProTests/Core/DataWrite/RowChangeStatementFactoryExpressionTests.swift @@ -36,6 +36,21 @@ private final class ExpressionRecordingDriver: PluginDriverStub, @unchecked Send func quoteIdentifier(_ name: String) -> String { "[\(name)]" } } +private final class RestoringDriver: PluginDriverStub, @unchecked Sendable { + func generateIdentityPreservingInsert( + table: String, + schema: String?, + columns: [String], + primaryKeyColumns: [String], + rows: [[PluginCellValue]], + absentCells: [Int: Set] + ) -> [(statement: String, parameters: [PluginCellValue])]? { + rows.map { (statement: "INSERT INTO [events] VALUES (?, ?, ?)", parameters: $0) } + } + + func quoteIdentifier(_ name: String) -> String { "[\(name)]" } +} + @MainActor struct RowChangeStatementFactoryExpressionTests { private let columns = ["id", "name", "stamped_at"] @@ -192,7 +207,7 @@ struct RowChangeStatementFactoryExpressionTests { @Test("SQLite refuses DEFAULT on a saved row, naming the column") func sqliteDefaultUpdateIsRefused() { - let change = edit(to: .text(PluginCellValue.defaultMarkerText), isExpression: false, column: 1) + let change = edit(to: .columnDefault, isExpression: true, column: 1) let refused = refusal { _ = try factory(databaseType: .sqlite).statements(for: [change]) } #expect(refused?.kind == .update) #expect(refused?.reason.contains("name") == true) @@ -202,8 +217,8 @@ struct RowChangeStatementFactoryExpressionTests { func sqliteDefaultInsertIsWritten() throws { let rowID = RowID.inserted(UUID()) let statements = try factory(databaseType: .sqlite).statements( - for: [RowChange(rowID: rowID, type: .insert)], - insertedRowData: [rowID: ["1", .text(PluginCellValue.defaultMarkerText), "2026-01-01"]], + for: [RowChange(rowID: rowID, type: .insert, expressionColumns: [1])], + insertedRowData: [rowID: ["1", .columnDefault, "2026-01-01"]], insertedRowIDs: [rowID] ) #expect(statements.count == 1) @@ -212,8 +227,69 @@ struct RowChangeStatementFactoryExpressionTests { @Test("PostgreSQL takes DEFAULT on a saved row") func postgresDefaultUpdate() throws { - let change = edit(to: .text(PluginCellValue.defaultMarkerText), isExpression: false, column: 1) + let change = edit(to: .columnDefault, isExpression: true, column: 1) let statements = try factory().statements(for: [change]) #expect(statements.first?.sql.contains("\"name\" = DEFAULT") == true) } + + @Test("Text a row holds or a user typed that reads like the default is written as that text", arguments: [ + "__DEFAULT__", "DEFAULT", + ]) + func unmarkedDefaultTextIsAValue(text: String) throws { + let saved = try factory(databaseType: .sqlite).statements(for: [edit(to: .text(text), isExpression: false, column: 1)]) + #expect(saved.first?.sql.contains("DEFAULT") == false) + #expect(saved.first?.parameters.first as? String == text) + + let rowID = RowID.inserted(UUID()) + let inserted = try factory(databaseType: .sqlite).statements( + for: [RowChange(rowID: rowID, type: .insert)], + insertedRowData: [rowID: ["1", .text(text), "2026-01-01"]], + insertedRowIDs: [rowID] + ) + #expect(inserted.first?.parameters.compactMap { $0 as? String } == ["1", text, "2026-01-01"]) + } + + @Test("A driver is handed a default in the spelling it reads, with no SQL mark, whatever it declares") + func defaultReachesADriverInItsSpelling() throws { + let driver = ExpressionRecordingDriver(capabilities: []) + _ = try factory(databaseType: .mssql, driver: driver).statements( + for: [edit(to: .columnDefault, isExpression: true, column: 1)] + ) + #expect(driver.changes.first?.cellChanges.first?.newValue == .pluginDefaultMarker) + #expect(driver.changes.first?.expressionColumns == nil) + } + + // A driver may write DEFAULT for it, and none can say it will not, so every one that writes its own rows is refused. + @Test("Text spelled the way drivers read a default is refused by a driver that writes its own rows", arguments: [ + DatabaseType.mssql, DatabaseType.redis, + ]) + func textADriverMayReadAsDefaultIsRefused(databaseType: DatabaseType) { + let driver = ExpressionRecordingDriver(capabilities: []) + let refused = refusal { + _ = try factory(databaseType: databaseType, driver: driver).statements( + for: [edit(to: "__DEFAULT__", isExpression: false, column: 1)] + ) + } + #expect(refused?.kind == .update) + #expect(refused?.reason.contains("name") == true) + } + + @Test("A deleted row a driver puts back is refused when it holds that text", arguments: [ + DatabaseType.dameng, DatabaseType.redis, + ]) + func restoredTextADriverMayReadAsDefaultIsRefused(databaseType: DatabaseType) { + let refused = refusal { + _ = try factory(databaseType: databaseType, driver: RestoringDriver()) + .restoreStatements(rows: [["1", "__DEFAULT__", "2026-01-01"]]) + } + #expect(refused?.kind == .insert) + #expect(refused?.reason.contains("name") == true) + } + + @Test("A deleted row a driver puts back with no such text is restored") + func restoredRowWithoutThatTextIsWritten() throws { + let restored = try factory(databaseType: .dameng, driver: RestoringDriver()) + .restoreStatements(rows: [["1", "Ada", "2026-01-01"]]) + #expect(restored.statements.first?.parameters.compactMap { $0 as? String } == ["1", "Ada", "2026-01-01"]) + } } diff --git a/apple/TableProTests/Core/DataWrite/RowWriteOperationBuilderTests.swift b/apple/TableProTests/Core/DataWrite/RowWriteOperationBuilderTests.swift index d4c40a9899..fdf82b33af 100644 --- a/apple/TableProTests/Core/DataWrite/RowWriteOperationBuilderTests.swift +++ b/apple/TableProTests/Core/DataWrite/RowWriteOperationBuilderTests.swift @@ -78,14 +78,14 @@ struct RowWriteOperationBuilderTests { #expect(now.first?.refusal == .serverComputedValue) let marker = operations( - changes: [ - cellEdit( - column: "updated_at", index: 2, from: "2026-01-01", - to: .text(PluginCellValue.defaultMarkerText) - ), - ] + changes: [cellEdit(column: "updated_at", index: 2, from: "2026-01-01", to: .columnDefault, isExpression: true)] ) #expect(marker.first?.refusal == .serverComputedValue) + + let text = operations( + changes: [cellEdit(column: "updated_at", index: 2, from: "2026-01-01", to: "__DEFAULT__")] + ) + #expect(text.first?.refusal == nil) } @Test("A generated column cannot be written back") @@ -136,8 +136,8 @@ struct RowWriteOperationBuilderTests { func serverAssignedKeyIsRefused() { let change = RowChange(rowID: .existing(0), type: .insert) let withMarker = operations( - changes: [change], - insertedRowData: [.existing(0): [.text(PluginCellValue.defaultMarkerText), "Ada", "2026-01-01"]], + changes: [RowChange(rowID: .existing(0), type: .insert, expressionColumns: [0])], + insertedRowData: [.existing(0): [.columnDefault, "Ada", "2026-01-01"]], insertedRowIDs: [.existing(0)] ) #expect(withMarker.first?.refusal == .serverAssignedKey) @@ -184,6 +184,31 @@ struct RowWriteOperationBuilderTests { #expect(literal.first?.refusal == nil) } + @Test("The record keeps the order the rows were changed in after an earlier change is cancelled") + func recordFollowsEditOrder() { + var pending = PendingChanges() + pending.recordCellChange( + rowID: .existing(0), columnIndex: 1, columnName: "name", + oldValue: "Ada", newValue: "Grace", originalRow: ["7", "Ada", "2026-01-01"] + ) + pending.recordRowDeletion(rowID: .existing(1), originalRow: ["8", "Bo", "2026-01-01"]) + let inserted = RowID.inserted(UUID()) + pending.recordRowInsertion(rowID: inserted, values: ["9", "Bo", "2026-01-01"]) + pending.recordCellChange( + rowID: .existing(0), columnIndex: 1, columnName: "name", oldValue: "Grace", newValue: "Ada" + ) + #expect(pending.changes.map { $0.type } == [.insert, .delete]) + + let result = operations( + changes: pending.changes, + insertedRowData: pending.insertedRowData, + deletedRowIDs: pending.deletedRowIDs, + insertedRowIDs: pending.insertedRowIDs + ) + + #expect(result.map { $0.kind } == [.delete, .insert]) + } + @Test("An insert carrying its own key is reversible") func userSuppliedKeyIsReversible() { let result = operations( diff --git a/apple/TableProTests/Core/Database/DatabaseManagerTests.swift b/apple/TableProTests/Core/Database/DatabaseManagerTests.swift index 1d05998bfb..10989dcb28 100644 --- a/apple/TableProTests/Core/Database/DatabaseManagerTests.swift +++ b/apple/TableProTests/Core/Database/DatabaseManagerTests.swift @@ -412,8 +412,11 @@ struct DatabaseManagerDatabaseSwitchTests { let pluginDriver = DatabaseSwitchingDriver() let adapter = PluginDriverAdapter(connection: connection, pluginDriver: pluginDriver) - await DatabaseManager.shared.restoreSchemaAndDatabase(on: adapter, savedSchema: "idb", savedDatabase: "ice") + let warning = await DatabaseManager.shared.restoreSchemaAndDatabase( + on: adapter, savedSchema: "idb", savedDatabase: "ice" + ) #expect(pluginDriver.switches == ["database ice", "schema idb"]) + #expect(warning == nil) } } diff --git a/apple/TableProTests/Core/Database/SchemaWriteGateDenialTests.swift b/apple/TableProTests/Core/Database/SchemaWriteGateDenialTests.swift new file mode 100644 index 0000000000..5af1cd879d --- /dev/null +++ b/apple/TableProTests/Core/Database/SchemaWriteGateDenialTests.swift @@ -0,0 +1,86 @@ +// +// SchemaWriteGateDenialTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +// A denial came back as a failed statement, so Create Database, the schema editor and Create Table +// showed the user's own Cancel at the gate as an error. +@MainActor +struct SchemaWriteGateDenialTests { + private let scope = DatabaseScope(connectionId: UUID(), database: "app", schema: nil) + + private func gateFailure(of body: () async throws -> Void) async -> GatedOperationFailure? { + do { + try await body() + return nil + } catch { + return GatedOperationFailure(error) + } + } + + private func runCreateDatabase(gate: any ExecutionGate) async -> GatedOperationFailure? { + await gateFailure { + try await DatabaseManager.shared.runContainerOperation( + description: "Create database \"sales\"", + kind: .schemaMutation, + scope: scope, + databaseType: .postgresql, + event: nil, + gate: gate + ) { _ in + Issue.record("The driver was asked after the gate said no") + } + } + } + + @Test("A Cancel at the gate ends a database operation quietly, before the driver is asked") + func containerOperationCancelIsQuiet() async { + let failure = await runCreateDatabase(gate: AlwaysDenyGate.cancelling) + + #expect(failure == .cancelledByUser) + #expect(failure?.message == nil) + } + + @Test("A refused database operation says why and is not a failed statement") + func containerOperationRefusalExplainsItself() async { + let failure = await runCreateDatabase(gate: AlwaysDenyGate(reason: "Safe Mode is read-only")) + + #expect(failure == .refused("Safe Mode is read-only")) + #expect(failure?.reportsFailure == false) + } + + @Test("A Cancel at the gate ends a schema editor apply quietly") + func containerStatementsCancelIsQuiet() async { + let failure = await gateFailure { + try await DatabaseManager.shared.runContainerStatements( + .statements([SchemaStatement(sql: "CREATE SCHEMA sales", description: "Create", isDestructive: false)]), + description: "Create Schema", + scope: scope, + databaseType: .postgresql, + event: nil, + gate: AlwaysDenyGate.cancelling + ) + } + + #expect(failure == .cancelledByUser) + } + + @Test("A Cancel at the gate ends a Create Table quietly") + func createTableCancelIsQuiet() async { + let failure = await gateFailure { + try await DatabaseManager.shared.executeCreateTable( + statements: ["CREATE TABLE invoices (id INT)"], + databaseType: .postgresql, + scope: scope, + gate: AlwaysDenyGate.cancelling + ) + } + + #expect(failure == .cancelledByUser) + #expect(failure?.message == nil) + } +} diff --git a/apple/TableProTests/Core/Database/StartupCommandWarningTests.swift b/apple/TableProTests/Core/Database/StartupCommandWarningTests.swift new file mode 100644 index 0000000000..11fbac5867 --- /dev/null +++ b/apple/TableProTests/Core/Database/StartupCommandWarningTests.swift @@ -0,0 +1,135 @@ +// +// StartupCommandWarningTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +private struct ServerRefusal: PluginDriverError { + let pluginErrorMessage: String + let pluginSqlState: String? +} + +// A startup command can be a safety rail, so a session that opened without one says so on the connection's warning. +@MainActor +struct StartupCommandWarningTests { + @Test("A startup command the server refused is named on the session's warning, the ones that ran are not") + func refusedStartupCommandIsNamed() async { + let driver = MockDatabaseDriver() + driver.failsStatementContaining = ("SET ROLE", DatabaseError.queryFailed("role \"app_ro\" does not exist")) + + let failures = await DatabaseManager.shared.applyTimeoutAndStartupCommands( + on: driver, + queryTimeoutSeconds: 0, + startupCommands: "SET ROLE app_ro\nSET search_path = app", + connectionName: "Reporting" + ) + let warning = DatabaseManager.startupWarning(for: failures) + + #expect(failures.map { $0.statement } == ["SET ROLE app_ro"]) + #expect(warning?.contains("SET ROLE app_ro") == true) + #expect(warning?.contains("SET search_path") == false) + } + + // PostgreSQL can quote the statement's values back in its message, so only the code reaches the screen. + @Test("The warning names the server's code, never its message") + func warningShowsTheCodeNotTheMessage() { + let refusal = ServerRefusal( + pluginErrorMessage: "invalid value for parameter \"app.api_key\": \"sk-live-123\"", pluginSqlState: "22023" + ) + let failure = StartupCommandFailure(statement: "SET app.api_key = 42", error: refusal) + let warning = DatabaseManager.startupWarning(for: [failure]) + + #expect(failure.code == "22023") + #expect(warning?.contains("SET app.api_key = 42") == true) + #expect(warning?.contains("22023") == true) + #expect(warning?.contains("sk-live-123") == false) + } + + @Test("Startup commands that all ran leave no warning") + func successfulStartupCommandsLeaveNoWarning() async { + let driver = MockDatabaseDriver() + + let failures = await DatabaseManager.shared.applyTimeoutAndStartupCommands( + on: driver, queryTimeoutSeconds: 0, startupCommands: "SET search_path = app", connectionName: "Reporting" + ) + + #expect(DatabaseManager.startupWarning(for: failures) == nil) + } + + @Test("Several failed startup commands are each named") + func severalFailuresAreEachNamed() { + let warning = DatabaseManager.startupWarning(for: [ + StartupCommandFailure(statement: "SET ROLE app_ro", code: "42704"), + StartupCommandFailure(statement: "SET default_transaction_read_only = on", code: nil), + ]) + + #expect(warning?.contains("SET ROLE app_ro (42704)") == true) + #expect(warning?.contains("SET default_transaction_read_only = on") == true) + } + + // A startup command can carry a password or a key, and the banner is on screen, screen sharing included. + @Test("A failed startup command is shown only up to its first literal or credential clause") + func shownStatementStopsBeforeSecrets() { + #expect(DatabaseManager.shownStatement("SELECT set_config('app.api_key', 'sk-live-123', false)") + == "SELECT set_config(…") + #expect(DatabaseManager.shownStatement("SET ROLE reporting IDENTIFIED BY hunter2") == "SET ROLE reporting…") + #expect(DatabaseManager.shownStatement("ALTER USER app WITH PASSWORD 'x'") == "ALTER USER app WITH…") + #expect(DatabaseManager.shownStatement("SET search_path = app") == "SET search_path = app") + + let warning = DatabaseManager.startupWarning(for: [ + StartupCommandFailure(statement: "SET ROLE reporting IDENTIFIED BY hunter2", code: "28000"), + ]) + #expect(warning?.contains("hunter2") == false) + } + + @Test("A reconnect whose startup commands all ran drops the banner about the session before it") + func cleanReconnectClearsTheStartupWarning() { + let connectionId = UUID() + let manager = DatabaseManager.shared + defer { manager.reportedStartupFailures.removeValue(forKey: connectionId) } + + let failed = manager.openingWarning( + driver: MockDatabaseDriver(), + startupFailures: [StartupCommandFailure(statement: "SET ROLE app_ro", code: "42704")], + for: connectionId + ) + let clean = manager.openingWarning(driver: MockDatabaseDriver(), startupFailures: [], for: connectionId) + + #expect(failed?.contains("SET ROLE app_ro") == true) + #expect(clean == nil) + #expect(manager.reportedStartupFailures[connectionId]?.isEmpty == true) + } + + @Test("The login's warning and the startup warning are both kept") + func warningsAreJoined() { + #expect(DatabaseManager.joinedWarning(["ORA-28002", nil, "startup"]) == "ORA-28002\n\nstartup") + #expect(DatabaseManager.joinedWarning([nil, ""]) == nil) + } + + // The pool opens entries for as long as the window is open, so a failure it was already told about stays dismissed. + @Test("A pool or lane session's failed startup command reaches the window once") + func poolFailureIsReportedOnce() { + let connection = TestFixtures.makeConnection(name: "Startup rail") + let manager = DatabaseManager.shared + manager.injectSession(ConnectionSession(connection: connection), for: connection.id) + defer { manager.removeSession(for: connection.id) } + let role = StartupCommandFailure(statement: "SET ROLE app_ro", code: "42704") + let path = StartupCommandFailure(statement: "SET search_path = app", code: "3F000") + + manager.reportStartupFailures([role], for: connection.id) + #expect(manager.session(for: connection.id)?.connectionWarning?.contains("SET ROLE app_ro") == true) + + manager.dismissConnectionWarning(for: connection.id) + manager.reportStartupFailures([role], for: connection.id) + #expect(manager.session(for: connection.id)?.connectionWarning == nil) + + manager.reportStartupFailures([role, path], for: connection.id) + let warning = manager.session(for: connection.id)?.connectionWarning + #expect(warning?.contains("SET search_path = app") == true) + #expect(warning?.contains("SET ROLE") == false) + } +} diff --git a/apple/TableProTests/Core/Database/TriggerInfoMappingTests.swift b/apple/TableProTests/Core/Database/TriggerInfoMappingTests.swift index e67a5599bd..0b19025ea9 100644 --- a/apple/TableProTests/Core/Database/TriggerInfoMappingTests.swift +++ b/apple/TableProTests/Core/Database/TriggerInfoMappingTests.swift @@ -437,3 +437,89 @@ struct TriggerEditSafetyTests { #expect(requests.isEmpty) } } + +// A Cancel in the gate's confirmation reached the trigger editor as an error row under the SQL. +@MainActor +struct TriggerGateDenialTests { + private func install() async throws -> (connection: DatabaseConnection, scope: DatabaseScope, stub: StubTriggerDriver) { + let connection = TestFixtures.makeConnection(database: "app", type: .postgresql) + let stub = StubTriggerDriver() + stub.dropToReturn = "DROP TRIGGER t" + let adapter = PluginDriverAdapter(connection: connection, pluginDriver: stub) + try await adapter.connect() + DatabaseManager.shared.injectSession(ConnectionSession(connection: connection, driver: adapter), for: connection.id) + let scope = DatabaseScope(connectionId: connection.id, database: "app", schema: nil) + MetadataConnectionPool.shared.injectEntry(adapter, scope: scope) + return (connection, scope, stub) + } + + private func uninstall(_ connection: DatabaseConnection) { + MetadataConnectionPool.shared.closeAll(connectionId: connection.id) + DatabaseManager.shared.removeSession(for: connection.id) + } + + private func failure(of body: () async throws -> Void) async -> GatedOperationFailure? { + do { + try await body() + return nil + } catch { + return GatedOperationFailure(error) + } + } + + @Test("A Cancel at the gate ends a trigger save and a trigger drop quietly, with nothing run") + func cancelIsQuiet() async throws { + let installed = try await install() + defer { uninstall(installed.connection) } + + let save = await failure { + try await TriggerEditing.apply( + scope: installed.scope, + connection: installed.connection, + tableName: "orders", + sql: "CREATE TRIGGER t", + isEdit: false, + originalName: nil, + originalDefinition: nil, + gate: AlwaysDenyGate.cancelling + ) + } + let drop = await failure { + try await TriggerEditing.drop( + scope: installed.scope, + connection: installed.connection, + tableName: "orders", + name: "t", + gate: AlwaysDenyGate.cancelling + ) + } + + #expect(save == .cancelledByUser) + #expect(drop == .cancelledByUser) + #expect(save?.message == nil) + #expect(installed.stub.executedQueries.isEmpty) + } + + @Test("A refused trigger save says why and is not a failed statement") + func refusalExplainsItself() async throws { + let installed = try await install() + defer { uninstall(installed.connection) } + + let save = await failure { + try await TriggerEditing.apply( + scope: installed.scope, + connection: installed.connection, + tableName: "orders", + sql: "CREATE TRIGGER t", + isEdit: false, + originalName: nil, + originalDefinition: nil, + gate: AlwaysDenyGate(reason: "Safe Mode is read-only") + ) + } + + #expect(save == .refused("Safe Mode is read-only")) + #expect(save?.reportsFailure == false) + #expect(installed.stub.executedQueries.isEmpty) + } +} diff --git a/apple/TableProTests/Core/LSP/LSPTransportBrokenPipeTests.swift b/apple/TableProTests/Core/LSP/LSPTransportBrokenPipeTests.swift new file mode 100644 index 0000000000..7af8c30567 --- /dev/null +++ b/apple/TableProTests/Core/LSP/LSPTransportBrokenPipeTests.swift @@ -0,0 +1,48 @@ +import Foundation +@testable import TablePro +import TableProTestSupport +import Testing + +struct LSPTransportBrokenPipeTests { + static let perl = "/usr/bin/perl" + + static var hasPerl: Bool { + FileManager.default.isExecutableFile(atPath: perl) + } + + // A server on its way out: it stops reading, asks the client something, then says it has stopped. + private static let closingServer = #""" + close STDIN; $| = 1; + for my $body ('{"jsonrpc":"2.0","id":7,"method":"workspace/configuration","params":{}}', + '{"jsonrpc":"2.0","method":"test/inputClosed"}') { + print "Content-Length: " . length($body) . "\r\n\r\n" . $body; + } + sleep 30; + """# + + @Test( + "A server that stopped reading fails the request instead of ending the app", + .enabled(if: LSPTransportBrokenPipeTests.hasPerl), + .timeLimit(.minutes(1)) + ) + func closedInputFailsTheRequest() async throws { + let transport = LSPTransport() + let inputClosed = AsyncGate() + await transport.onNotification(method: "test/inputClosed") { _ in + Task { await inputClosed.open() } + } + try await transport.start(executablePath: Self.perl, arguments: ["-e", Self.closingServer]) + await inputClosed.wait() + + let error = await #expect(throws: (any Error).self) { + _ = try await transport.sendRequest(method: "test/afterClose", params: EmptyLSPParams()) + } + await transport.stop() + + let failure = error.map { $0 as NSError } + let posix = failure?.domain == NSPOSIXErrorDomain + ? failure + : failure?.underlyingErrors.first.map { $0 as NSError } + #expect(posix?.code == Int(EPIPE)) + } +} diff --git a/apple/TableProTests/Core/ObjectCopy/ObjectCopyRowCutTests.swift b/apple/TableProTests/Core/ObjectCopy/ObjectCopyRowCutTests.swift new file mode 100644 index 0000000000..c9e9c4e532 --- /dev/null +++ b/apple/TableProTests/Core/ObjectCopy/ObjectCopyRowCutTests.swift @@ -0,0 +1,107 @@ +// +// ObjectCopyRowCutTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +private final class CappedSourceDriver: PluginDriverStub, @unchecked Sendable { + let declaredLimit: Int? + let rowCount: Int + + init(declaredLimit: Int?, rowCount: Int) { + self.declaredLimit = declaredLimit + self.rowCount = rowCount + } + + func streamRows(query: String) -> AsyncThrowingStream { + var header = PluginStreamHeader(columns: ["id", "name"], columnTypeNames: []) + header.rowLimit = declaredLimit + let rows: [PluginRow] = (0 ..< rowCount).map { [.text(String($0)), .text("row\($0)")] } + return AsyncThrowingStream { continuation in + continuation.yield(.header(header)) + continuation.yield(.rows(rows)) + continuation.finish() + } + } +} + +private final class WritingTargetDriver: PluginDriverStub, @unchecked Sendable { + var parameterStyle: ParameterStyle { .questionMark } + + func executeParameterized(query: String, parameters: [PluginCellValue]) async throws -> PluginQueryResult { + PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) + } + + func quoteIdentifier(_ name: String) -> String { "`\(name)`" } +} + +struct ObjectCopyRowCutTests { + private func step() -> ObjectCopyTableStep { + ObjectCopyTableStep( + selection: ObjectCopySelection(kind: .table, name: "orders", schema: "public"), + dropStatements: [], + sequenceStatements: [], + createStatements: [], + truncateStatements: [], + columns: ["id", "name"], + targetColumnTypeNames: [:], + primaryKeyColumns: ["id"], + sourceQuery: "SELECT `id`, `name` FROM `public`.`orders`", + targetTable: "orders", + targetSchema: "public", + estimatedRows: nil, + copiesData: true, + copiesIdentityColumn: false, + coercer: nil, + serverSideInsert: nil, + note: nil + ) + } + + private func copy(from source: CappedSourceDriver) async throws -> ObjectCopyRowCopier.Outcome { + try await ObjectCopyRowCopier(step: step(), sourceDatabaseType: .mysql, targetDatabaseType: .mysql) + .copy(from: source, to: WritingTargetDriver()) { _ in } + } + + // A 6,000,000-row SAP HANA table arrived as 5,000,000 rows and the copy reported success. + @Test("A copy whose source stream stopped at the driver's limit says where") + func cutStreamIsReported() async throws { + let outcome = try await copy(from: CappedSourceDriver(declaredLimit: 3, rowCount: 3)) + + #expect(outcome.inserted == 3) + #expect(outcome.rowCut == 3) + } + + @Test("A copy that read fewer rows than the driver's limit is whole") + func shortStreamIsWhole() async throws { + let outcome = try await copy(from: CappedSourceDriver(declaredLimit: 10, rowCount: 3)) + + #expect(outcome.inserted == 3) + #expect(outcome.rowCut == nil) + } + + @Test("A cut table's outcome carries the export's wording") + func cutOutcomeCarriesTheWarning() { + let selection = ObjectCopySelection(kind: .table, name: "orders", schema: "public") + let result = ObjectCopyRunResult( + outcomes: [ + ObjectCopyObjectOutcome( + selection: selection, + rowsCopied: 3, + error: nil, + warning: StreamRowCutWarning.text(table: selection.qualifiedName, limit: 3) + ) + ], + rowsCopied: 3, + cancelled: false, + createdDatabase: nil + ) + + #expect(result.warnings == [StreamRowCutWarning.text(table: selection.qualifiedName, limit: 3)]) + #expect(result.failedCount == 0) + } +} diff --git a/apple/TableProTests/Core/Services/Query/MetadataConnectionPoolTests.swift b/apple/TableProTests/Core/Services/Query/MetadataConnectionPoolTests.swift index 0a184ad353..04b349fbe9 100644 --- a/apple/TableProTests/Core/Services/Query/MetadataConnectionPoolTests.swift +++ b/apple/TableProTests/Core/Services/Query/MetadataConnectionPoolTests.swift @@ -84,7 +84,7 @@ struct MetadataConnectionPoolTests { func prepareSessionPassesThrough() async throws { let driver = MockDatabaseDriver() - try await MetadataConnectionPool.prepareSession( + let failures = try await MetadataConnectionPool.prepareSession( driver, queryTimeoutSeconds: 30, startupCommands: nil, @@ -93,6 +93,25 @@ struct MetadataConnectionPoolTests { ) #expect(driver.applyQueryTimeoutValues == [30]) + #expect(failures.isEmpty) + } + + // A pool entry or a lane whose `SET ROLE` failed would otherwise run with the login's own rights unannounced. + @Test("session preparation hands back the startup commands that failed") + func prepareSessionReturnsFailedStartupCommands() async throws { + let driver = MockDatabaseDriver() + driver.failsStatementContaining = ("SET ROLE", DatabaseError.queryFailed("role \"app_ro\" does not exist")) + + let failures = try await MetadataConnectionPool.prepareSession( + driver, + queryTimeoutSeconds: 0, + startupCommands: "SET ROLE app_ro\nSET search_path = app", + connectionName: "test", + timeoutSeconds: 1 + ) + + #expect(failures.map { $0.statement } == ["SET ROLE app_ro"]) + #expect(driver.executedQueries == ["SET ROLE app_ro", "SET search_path = app"]) } @Test("database switch rejects a driver that cannot switch") diff --git a/apple/TableProTests/Core/Services/Query/SelectionSummaryCalculatorTests.swift b/apple/TableProTests/Core/Services/Query/SelectionSummaryCalculatorTests.swift index 2f7e00976f..953b41e5c4 100644 --- a/apple/TableProTests/Core/Services/Query/SelectionSummaryCalculatorTests.swift +++ b/apple/TableProTests/Core/Services/Query/SelectionSummaryCalculatorTests.swift @@ -25,8 +25,7 @@ struct SelectionSummaryCalculatorTests { dataColumns: [Int]? = nil, policy: SelectionSummaryColumnPolicy? = nil, deleted: Set = [], - inserted: Set = [], - modified: [RowID: Set] = [:] + defaulted: [RowID: Set] = [:] ) -> SelectionSummaryInput { SelectionSummaryInput( selection: selection(rects), @@ -39,8 +38,7 @@ struct SelectionSummaryCalculatorTests { columnCount: tableRows.columns.count ), deletedRowIDs: deleted, - insertedRowIDs: inserted, - modifiedCells: modified + defaultedCells: defaulted ) } @@ -113,25 +111,24 @@ struct SelectionSummaryCalculatorTests { #expect(summary.numbers?.sum.decimal == 1) } - @Test("NULL is empty, empty text is a value, and the default marker is empty only where an edit put it") + @Test("NULL is empty, empty text is a value, and DEFAULT is empty only where an edit marked it") func emptyCellRules() async throws { let insertedID = RowID.inserted(UUID()) var table = Self.tableRows( [ [.null, .text("")], - [.text(PluginCellValue.defaultMarkerText), .text(PluginCellValue.defaultMarkerText)], - [.text("7"), .text(PluginCellValue.defaultMarkerText)] + [.text("DEFAULT"), .text("DEFAULT")], + [.text("7"), .columnDefault] ], types: [Self.integer, Self.untyped] ) - table.appendInsertedRow(id: insertedID, values: [.text(PluginCellValue.defaultMarkerText), .text("5")]) + table.appendInsertedRow(id: insertedID, values: [.columnDefault, .text("5")]) let summary = try await Self.summarize( Self.input( table, selecting: [GridRect(rows: 0...3, columns: 0...1)], - inserted: [insertedID], - modified: [.existing(2): [1]] + defaulted: [.existing(2): [1], insertedID: [0]] ) ) diff --git a/apple/TableProTests/Core/Services/RowOperationsFieldAbsenceTests.swift b/apple/TableProTests/Core/Services/RowOperationsFieldAbsenceTests.swift index a0e0ecf3b2..2823bea961 100644 --- a/apple/TableProTests/Core/Services/RowOperationsFieldAbsenceTests.swift +++ b/apple/TableProTests/Core/Services/RowOperationsFieldAbsenceTests.swift @@ -82,7 +82,7 @@ extension GlobalStateSuites { #expect(tableRows.row(withID: nullKept.rowID)?.absentColumns.isEmpty == true) #expect(tableRows.row(withID: missingKept.rowID)?.absentColumns == [2]) #expect(changes.pending.insertedAbsentColumns(forRow: missingKept.rowID) == [2]) - #expect(missingKept.values[0] == .text("__DEFAULT__")) + #expect(missingKept.values[0] == .columnDefault) } @Test("Copying rows records which fields each lacked, in the copied column order") @@ -123,7 +123,7 @@ extension GlobalStateSuites { let (tableRows, result) = paste(clipboard, into: manager) - #expect(result.pastedRows.first?.values == [.text("__DEFAULT__"), "Ada", .null]) + #expect(result.pastedRows.first?.values == [.columnDefault, "Ada", .null]) #expect(tableRows.rows[0].absentColumns.isEmpty) } diff --git a/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift b/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift index 80819290f7..51e4d37d67 100644 --- a/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift +++ b/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift @@ -51,7 +51,7 @@ struct RowOperationsManagerPasteTests { let values = pasteValues(clipboard, manager: makeManager()) #expect(values.count == 3) - #expect(values[0] == .text("__DEFAULT__")) + #expect(values[0] == .columnDefault) #expect(values[1] == .text("Smith, John")) #expect(values[2] == .text("a@test.com")) } @@ -120,7 +120,7 @@ struct RowOperationsManagerPasteTests { let values = pasteValues(clipboard, manager: makeManager()) - #expect(values[0] == .text("__DEFAULT__")) + #expect(values[0] == .columnDefault) #expect(values[1] == .text("Jo")) #expect(values[2] == .text("e@x.com")) } @@ -136,7 +136,7 @@ struct RowOperationsManagerPasteTests { let values = pasteValues(clipboard, manager: makeManager()) #expect(values.count == 3) - #expect(values[0] == .text("__DEFAULT__")) + #expect(values[0] == .columnDefault) #expect(values[1] == .text("Jo")) #expect(values[2] == .text("e@x.com")) } @@ -151,7 +151,7 @@ struct RowOperationsManagerPasteTests { let values = pasteValues(clipboard, manager: makeManager()) - #expect(values[0] == .text("__DEFAULT__")) + #expect(values[0] == .columnDefault) #expect(values[1] == .text("2")) #expect(values[2] == .text("3")) } @@ -170,8 +170,8 @@ struct RowOperationsManagerPasteTests { let rows = pasteRows(clipboard, manager: makeManager()) #expect(rows.count == 2) - #expect(rows[0] == [.text("__DEFAULT__"), .text("Alice"), .text("a@test.com")]) - #expect(rows[1] == [.text("__DEFAULT__"), .text("Bob"), .null]) + #expect(rows[0] == [.columnDefault, .text("Alice"), .text("a@test.com")]) + #expect(rows[1] == [.columnDefault, .text("Bob"), .null]) } @Test("Plain-text TSV with commas inside a value parses as TSV, not CSV") @@ -182,7 +182,7 @@ struct RowOperationsManagerPasteTests { let values = pasteValues(clipboard, manager: makeManager()) #expect(values.count == 3) - #expect(values[0] == .text("__DEFAULT__")) + #expect(values[0] == .columnDefault) #expect(values[1] == .text("Mozilla/5.0 (KHTML, like Gecko) Safari")) #expect(values[2] == .text("a@test.com")) } @@ -257,7 +257,7 @@ struct RowOperationsManagerPasteServerOwnedTests { func resetsNonKeyIdentity() { let values = paste(payload, columnIdentity: ["code": .always]) - #expect(values[1] == .text("__DEFAULT__")) + #expect(values[1] == .columnDefault) #expect(values[2] == .text("Ada")) } @@ -265,7 +265,7 @@ struct RowOperationsManagerPasteServerOwnedTests { func resetsGeneratedColumn() { let values = paste(payload, generatedColumns: ["code"]) - #expect(values[1] == .text("__DEFAULT__")) + #expect(values[1] == .columnDefault) #expect(values[2] == .text("Ada")) } diff --git a/apple/TableProTests/Core/Services/RowOperationsManagerTests.swift b/apple/TableProTests/Core/Services/RowOperationsManagerTests.swift index 7358b7b600..c56df2acad 100644 --- a/apple/TableProTests/Core/Services/RowOperationsManagerTests.swift +++ b/apple/TableProTests/Core/Services/RowOperationsManagerTests.swift @@ -118,8 +118,8 @@ struct RowOperationsManagerTests { let result = manager.addNewRow(tableRows: &tableRows) #expect(result != nil) - #expect(result?.values[0] == "__DEFAULT__") - #expect(result?.values[2] == "__DEFAULT__") + #expect(result?.values[0] == .columnDefault) + #expect(result?.values[2] == .columnDefault) } // PostgreSQL leaves `column_default` null for an identity column, so a new row sent an explicit NULL it refuses. @@ -133,7 +133,7 @@ struct RowOperationsManagerTests { let result = manager.addNewRow(tableRows: &tableRows) - #expect(result?.values[0] == "__DEFAULT__") + #expect(result?.values[0] == .columnDefault) #expect(result?.values[1] == .null) } @@ -146,7 +146,7 @@ struct RowOperationsManagerTests { let result = manager.addNewRow(tableRows: &tableRows) - #expect(result?.values[2] == "__DEFAULT__") + #expect(result?.values[2] == .columnDefault) #expect(result?.values[1] == .null) } @@ -159,7 +159,7 @@ struct RowOperationsManagerTests { let result = manager.addNewRow(tableRows: &tableRows) #expect(result?.values[1] == .null) - #expect(result?.values[2] == "__DEFAULT__") + #expect(result?.values[2] == .columnDefault) #expect(tableRows.serverAssignsValue(forColumn: "name") == false) } @@ -246,7 +246,7 @@ struct RowOperationsManagerTests { ) #expect(result != nil) - #expect(result?.values[0] == "__DEFAULT__") + #expect(result?.values[0] == .columnDefault) if case .rowsInserted(let indices) = result?.delta { #expect(indices == IndexSet(integer: 3)) } else { @@ -263,10 +263,40 @@ struct RowOperationsManagerTests { let result = manager.duplicateRow(sourceRowIndex: 1, tableRows: &tableRows) - #expect(result?.values[2] == "__DEFAULT__") + #expect(result?.values[2] == .columnDefault) #expect(result?.values[1] == sourceValues[1]) } + @Test("The change manager names the cells a new row leaves to their default, for the JSON view and Copy as JSON") + func defaultedCellsOfANewRow() throws { + let (manager, changeManager) = makeManager() + var tableRows = emptyTableRows(columnDefaults: ["id": "auto_increment", "email": "'x'"]) + + let added = manager.addNewRow(tableRows: &tableRows) + let result = try #require(added) + + #expect(changeManager.defaultedCells(in: tableRows) == [result.rowID: [0, 2]]) + } + + @Test("A duplicate writes stored text that reads like the default as text, and leaves only its key to the server") + func duplicateRowKeepsDefaultLookingText() throws { + let (manager, changeManager) = makeManager() + var tableRows = TableRows.from( + queryRows: [["7", "__DEFAULT__", "DEFAULT"]], + columns: Self.testColumns, + columnTypes: Self.testColumnTypes + ) + + let duplicated = manager.duplicateRow(sourceRowIndex: 0, tableRows: &tableRows) + let result = try #require(duplicated) + + #expect(changeManager.expressionColumns(forRow: result.rowID) == [0]) + let statements = try changeManager.generateSQL() + #expect(statements.count == 1) + #expect(statements.first?.sql.contains("`id`") == false) + #expect(statements.first?.parameters.compactMap { $0 as? String } == ["__DEFAULT__", "DEFAULT"]) + } + @Test("duplicateRow returns nil for invalid source index") func duplicateRowReturnsNilForInvalidIndex() { let (manager, _) = makeManager() diff --git a/apple/TableProTests/Core/Services/RowOperationsManagerUUIDKeyTests.swift b/apple/TableProTests/Core/Services/RowOperationsManagerUUIDKeyTests.swift index 5601c64f35..4122e13d78 100644 --- a/apple/TableProTests/Core/Services/RowOperationsManagerUUIDKeyTests.swift +++ b/apple/TableProTests/Core/Services/RowOperationsManagerUUIDKeyTests.swift @@ -80,7 +80,7 @@ struct RowOperationsManagerUUIDKeyTests { let result = try #require(manager.addNewRow(tableRows: &rows, uuidKeyForms: Self.uuidKey)) - #expect(result.values[0] == .text("__DEFAULT__")) + #expect(result.values[0] == .columnDefault) #expect(result.prefilledColumns.isEmpty) } @@ -99,7 +99,7 @@ struct RowOperationsManagerUUIDKeyTests { #expect(copyKey != sourceKey) #expect(copy.values[1] == .text("a")) #expect(copy.prefilledColumns == [0]) - #expect(plain.values[0] == .text("__DEFAULT__")) + #expect(plain.values[0] == .columnDefault) } @Test("Duplicating a pending row keeps the SQL staged in it, except in the columns the copy resets") @@ -118,14 +118,16 @@ struct RowOperationsManagerUUIDKeyTests { #expect(uuidText(copy.values[0]) != nil) } - @Test("Duplicating a saved row stages no SQL") + @Test("Duplicating a saved row stages no SQL but the default its reset key takes") func duplicateOfASavedRowHasNoMarks() throws { let (manager, changeManager) = makeManager() var rows = makeRows([[.text("0b1e7c9a-58f4-4b8e-9c55-1f1c86f0a001"), .text("now()"), .null]]) let copy = try #require(manager.duplicateRow(sourceRowIndex: 0, tableRows: &rows)) - #expect(changeManager.expressionColumns(forRow: copy.rowID).isEmpty) + #expect(changeManager.expressionColumns(forRow: copy.rowID) == [0]) + #expect(copy.values[0] == .columnDefault) + #expect(copy.values[1] == .text("now()")) } // MARK: - Paste rows @@ -188,7 +190,7 @@ struct RowOperationsManagerUUIDKeyTests { clipboard: clipboard ) - #expect(result.pastedRows.first?.values[0] == .text("__DEFAULT__")) + #expect(result.pastedRows.first?.values[0] == .columnDefault) } } @@ -249,8 +251,8 @@ extension GlobalStateSuites { let pasted = result.pastedRows.map(\.rowID) #expect(pasted.count == 2) - #expect(changeManager.expressionColumns(forRow: pasted[0]).isEmpty) - #expect(changeManager.expressionColumns(forRow: pasted[1]) == [2]) + #expect(changeManager.expressionColumns(forRow: pasted[0]) == [0]) + #expect(changeManager.expressionColumns(forRow: pasted[1]) == [0, 2]) #expect(result.pastedRows[0].values[2] == .text("CURRENT_TIMESTAMP")) #expect(result.pastedRows[1].values[2] == .text("now()")) } @@ -268,8 +270,9 @@ extension GlobalStateSuites { let result = try paste(manager, into: &rows) - let rowID = try #require(result.pastedRows.first?.rowID) - #expect(changeManager.expressionColumns(forRow: rowID) == [2]) + let pastedRow = try #require(result.pastedRows.first) + #expect(pastedRow.values[0] == .columnDefault) + #expect(changeManager.expressionColumns(forRow: pastedRow.rowID) == [0, 2]) } @Test("Cells the grid copied as wide as the table paste as rows with their marks") @@ -287,8 +290,8 @@ extension GlobalStateSuites { let pasted = result.pastedRows.map(\.rowID) #expect(pasted.count == 2) - #expect(changeManager.expressionColumns(forRow: pasted[0]) == [2]) - #expect(changeManager.expressionColumns(forRow: pasted[1]).isEmpty) + #expect(changeManager.expressionColumns(forRow: pasted[0]) == [0, 2]) + #expect(changeManager.expressionColumns(forRow: pasted[1]) == [0]) } } } diff --git a/apple/TableProTests/Core/Services/RowPasteForeignTextTests.swift b/apple/TableProTests/Core/Services/RowPasteForeignTextTests.swift index 9c1a9b1dd4..e886b4cf83 100644 --- a/apple/TableProTests/Core/Services/RowPasteForeignTextTests.swift +++ b/apple/TableProTests/Core/Services/RowPasteForeignTextTests.swift @@ -22,8 +22,8 @@ struct RowPasteForeignTextTests { let rows = try parse("\tAnn\tann@x.com\n\tBob\tbob@x.com") #expect(rows == [ - [.text("__DEFAULT__"), .text("Ann"), .text("ann@x.com")], - [.text("__DEFAULT__"), .text("Bob"), .text("bob@x.com")] + [.columnDefault, .text("Ann"), .text("ann@x.com")], + [.columnDefault, .text("Bob"), .text("bob@x.com")] ]) } @@ -74,7 +74,8 @@ struct RowPasteForeignTextTests { columns: Self.columns, primaryKeyColumns: ["id"], tableRows: &rows, clipboard: clipboard ) - #expect(result.pastedRows.first?.values == [.text("__DEFAULT__"), .text("Ann"), .text("ann@x.com")]) + #expect(result.pastedRows.first?.values == [.columnDefault, .text("Ann"), .text("ann@x.com")]) + #expect(result.pastedRows.first.map { changeManager.expressionColumns(forRow: $0.rowID) } == [0]) } @Test("A blank pasted cell takes the column's default, an empty string where NULL is refused, else NULL") @@ -107,11 +108,12 @@ struct RowPasteForeignTextTests { columns: columns, primaryKeyColumns: ["id"], tableRows: &rows, clipboard: clipboard ) - let unfilled: [PluginCellValue] = [.text("__DEFAULT__"), .text(""), .null, .null] + let unfilled: [PluginCellValue] = [.columnDefault, .text(""), .null, .null] #expect(result.pastedRows.map { $0.values } == [ - [.text("__DEFAULT__"), .text("Ann")] + unfilled, - [.text("__DEFAULT__"), .text("Bob")] + unfilled, + [.columnDefault, .text("Ann")] + unfilled, + [.columnDefault, .text("Bob")] + unfilled, ]) + #expect(result.pastedRows.map { changeManager.expressionColumns(forRow: $0.rowID) } == [[0, 2], [0, 2]]) } @Test("A pasted NULL stays NULL in a column with a default") @@ -136,7 +138,7 @@ struct RowPasteForeignTextTests { columns: columns, primaryKeyColumns: ["id"], tableRows: &rows, clipboard: clipboard ) - #expect(result.pastedRows.first?.values == [.text("__DEFAULT__"), .null]) + #expect(result.pastedRows.first?.values == [.columnDefault, .null]) } } diff --git a/apple/TableProTests/Core/Shortcuts/PayloadRowPlannerHeaderMatchTests.swift b/apple/TableProTests/Core/Shortcuts/PayloadRowPlannerHeaderMatchTests.swift new file mode 100644 index 0000000000..a34eb3e553 --- /dev/null +++ b/apple/TableProTests/Core/Shortcuts/PayloadRowPlannerHeaderMatchTests.swift @@ -0,0 +1,59 @@ +// +// PayloadRowPlannerHeaderMatchTests.swift +// TableProTests +// + +import Foundation +import Testing + +@testable import TablePro + +struct PayloadRowPlannerHeaderMatchTests { + private func columns(_ names: [String]) -> [PayloadColumn] { + names.map { PayloadColumn(name: $0, isPrimaryKey: false, isServerOwned: false) } + } + + private func planned(_ rows: [PayloadRow], into names: [String]) throws -> [[String]] { + try PayloadRowPlanner.values(table: "people", columns: columns(names), rows: rows) + .map { row in row.map { "\($0.column)=\($0.value ?? "NULL")" } } + } + + // SQL Server exports its headers bracketed, and Shortcuts has no sheet to map them by hand. + @Test("A CSV with bracketed headers inserts into the columns they name") + func bracketedHeadersFindTheirColumns() async throws { + let rows = try await RowPayload.parse(data: "[id],[name]\n1,Bob\n", file: nil) + #expect(try planned(rows, into: ["id", "name"]) == [["id=1", "name=Bob"]]) + } + + @Test("A doubled closing bracket inside the brackets is one bracket of the name") + func doubledBracketIsUnescaped() throws { + let row = PayloadRow(values: ["[a]]b]": .text("x")]) + #expect(try planned([row], into: ["a]b"]) == [["a]b=x"]]) + } + + @Test("A lone closing bracket inside means the key is not a bracketed name") + func loneBracketIsNotUnwrapped() { + #expect(throws: IntentDataError.unknownColumns(["[a]b]"], "people")) { + _ = try planned([PayloadRow(values: ["[a]b]": .text("x")])], into: ["a]b"]) + } + } + + @Test("A key differing only in case finds its column, as the import sheet matches headers") + func caseInsensitiveMatch() throws { + #expect(try planned([PayloadRow(values: ["Name": .text("Ada")])], into: ["name"]) == [["name=Ada"]]) + } + + @Test("A case match two columns share is refused rather than guessed") + func ambiguousCaseMatchIsUnknown() { + #expect(throws: IntentDataError.unknownColumns(["NAME"], "people")) { + _ = try planned([PayloadRow(values: ["NAME": .text("Ada")])], into: ["Name", "name"]) + } + } + + @Test("A key spelled exactly claims its column before a bracketed one can") + func exactSpellingClaimsFirst() { + #expect(throws: IntentDataError.unknownColumns(["[id]"], "people")) { + _ = try planned([PayloadRow(values: ["id": .text("1"), "[id]": .text("2")])], into: ["id"]) + } + } +} diff --git a/apple/TableProTests/Core/Shortcuts/RowPayloadFormatTests.swift b/apple/TableProTests/Core/Shortcuts/RowPayloadFormatTests.swift new file mode 100644 index 0000000000..f5f9743a48 --- /dev/null +++ b/apple/TableProTests/Core/Shortcuts/RowPayloadFormatTests.swift @@ -0,0 +1,60 @@ +// +// RowPayloadFormatTests.swift +// TableProTests +// + +import Foundation +import Testing + +@testable import TablePro + +struct RowPayloadFormatTests { + // SQL Server exports its headers bracketed, so a valid CSV opens with `[`. + @Test("CSV with bracketed headers is read as CSV") + func bracketedHeaderIsCSV() async throws { + let rows = try await RowPayload.parse(data: "[id],[name]\n1,Bob\n", file: nil) + #expect(rows == [PayloadRow(values: ["[id]": .text("1"), "[name]": .text("Bob")])]) + } + + @Test("CSV whose first header opens with a brace is read as CSV") + func braceHeaderIsCSV() async throws { + let rows = try await RowPayload.parse(data: "{key},value\na,1\n", file: nil) + #expect(rows == [PayloadRow(values: ["{key}": .text("a"), "value": .text("1")])]) + } + + @Test("A JSON object and a JSON array of objects are still read as JSON") + func jsonStaysJSON() async throws { + let object = try await RowPayload.parse(data: #"{"name":"Ada"}"#, file: nil) + #expect(object == [PayloadRow(values: ["name": .text("Ada")])]) + let array = try await RowPayload.parse(data: #"[{"name":"Ada"},{"name":"Grace"}]"#, file: nil) + #expect(array.count == 2) + } + + @Test("Malformed JSON that is no CSV either reports the JSON error") + func malformedJSONKeepsItsError() async { + await #expect { + _ = try await RowPayload.parse(data: #"{"name": tru}"#, file: nil) + } throws: { error in + guard case .malformedPayload = error as? IntentDataError else { return false } + return true + } + } + + // Each line splits into the same number of CSV fields, so the text read as CSV with `{"id": 1` for a header. + @Test("JSON with a trailing comma after each line's object keeps the JSON error") + func jsonThatSplitsEvenlyKeepsItsError() async { + await #expect { + _ = try await RowPayload.parse(data: "{\"id\": 1, \"name\": \"a\"},\n{\"id\": 2, \"name\": \"b\"},\n", file: nil) + } throws: { error in + guard case .malformedPayload = error as? IntentDataError else { return false } + return true + } + } + + @Test("A JSON array of something other than objects keeps its own error") + func jsonArrayOfValuesIsNotRetriedAsCSV() async { + await #expect(throws: IntentDataError.jsonArrayContainsNonObject) { + _ = try await RowPayload.parse(data: "[1,2]", file: nil) + } + } +} diff --git a/apple/TableProTests/Core/UsersRoles/PrincipalApplyOpenTransactionTests.swift b/apple/TableProTests/Core/UsersRoles/PrincipalApplyOpenTransactionTests.swift index 4cb996f0e4..0ee582a754 100644 --- a/apple/TableProTests/Core/UsersRoles/PrincipalApplyOpenTransactionTests.swift +++ b/apple/TableProTests/Core/UsersRoles/PrincipalApplyOpenTransactionTests.swift @@ -62,3 +62,47 @@ struct PrincipalApplyOpenTransactionTests { #expect(driver.executedQueries.isEmpty) } } + +// The review sheet showed a Cancel at the gate as a failed apply. +@MainActor +struct PrincipalApplyGateDenialTests { + private func apply(gate: any ExecutionGate) async -> (failure: GatedOperationFailure?, executed: [String]) { + let driver = TransactionHoldingPrincipalDriver(rollsBack: true) + let connection = TestFixtures.makeConnection(type: .postgresql) + DatabaseManager.shared.injectSession( + ConnectionSession(connection: connection, driver: PluginDriverAdapter(connection: connection, pluginDriver: driver)), + for: connection.id + ) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + let change = PrincipalChange.create(PluginPrincipalDefinition(ref: PluginPrincipalRef(name: "X"), password: "p")) + do { + try await DatabaseManager.shared.executePrincipalChanges( + changes: [change], + databaseType: .postgresql, + connectionId: connection.id, + gate: gate + ) + return (nil, driver.executedQueries) + } catch { + return (GatedOperationFailure(error), driver.executedQueries) + } + } + + @Test("A Cancel at the gate ends the apply quietly, with nothing run") + func cancelIsQuiet() async { + let result = await apply(gate: AlwaysDenyGate.cancelling) + + #expect(result.failure == .cancelledByUser) + #expect(result.failure?.message == nil) + #expect(result.executed.isEmpty) + } + + @Test("A refused apply says why and is not a failed statement") + func refusalExplainsItself() async { + let result = await apply(gate: AlwaysDenyGate(reason: "Safe Mode is read-only")) + + #expect(result.failure == .refused("Safe Mode is read-only")) + #expect(result.executed.isEmpty) + } +} diff --git a/apple/TableProTests/Core/UsersRoles/PrincipalChangeManagerTests.swift b/apple/TableProTests/Core/UsersRoles/PrincipalChangeManagerTests.swift index fcdd2c5053..d8c628c9eb 100644 --- a/apple/TableProTests/Core/UsersRoles/PrincipalChangeManagerTests.swift +++ b/apple/TableProTests/Core/UsersRoles/PrincipalChangeManagerTests.swift @@ -329,6 +329,46 @@ struct PrincipalChangeManagerTests { #expect(manager.changeCount == 1) } + @Test("A staged drop runs only the DROP and counts as one change") + func dropSupersedesOtherEdits() { + let manager = makeManager() + manager.stageAlter(PluginPrincipalDefinition(ref: alice, canLogin: false), for: alice) + manager.stageSetPassword("s3cret", for: alice) + manager.setGranted(true, privilege: "CREATE", scope: app, for: alice) + #expect(manager.changeCount == 3) + + manager.stageDrop(alice, options: PluginPrincipalDropOptions()) + + let changes = manager.pendingChanges() + #expect(changes.count == 1) + guard case .drop(let ref, _) = changes.first else { + Issue.record("Expected only the DROP, got \(changes)") + return + } + #expect(ref == alice) + #expect(manager.changeCount == 1) + } + + @Test("Undoing a staged drop brings the principal's other edits back") + func unstagingDropRestoresEdits() { + let manager = makeManager() + manager.stageAlter(PluginPrincipalDefinition(ref: alice, canLogin: false), for: alice) + manager.stageSetPassword("s3cret", for: alice) + manager.setGranted(true, privilege: "CREATE", scope: app, for: alice) + manager.stageDrop(alice, options: PluginPrincipalDropOptions()) + + manager.unstageDrop(alice) + + let changes = manager.pendingChanges() + let dropsAnything = changes.contains { change in + if case .drop = change { return true } + return false + } + #expect(changes.count == 3) + #expect(!dropsAnything) + #expect(manager.changeCount == 3) + } + @Test("Dropping the connected account is reported as self-impact") func reportsSelfImpact() { let manager = makeManager() diff --git a/apple/TableProTests/Core/Utilities/JsonRowConverterTests.swift b/apple/TableProTests/Core/Utilities/JsonRowConverterTests.swift index 759614a413..0e3f59c5d3 100644 --- a/apple/TableProTests/Core/Utilities/JsonRowConverterTests.swift +++ b/apple/TableProTests/Core/Utilities/JsonRowConverterTests.swift @@ -384,20 +384,16 @@ struct JsonRowConverterTests { // MARK: - Default-value marker - /// A pending inserted row carries the default marker for columns the server fills in. The grid - /// draws it as a placeholder, so JSON has to agree instead of printing the internal token. - @Test("The default-value marker renders as null, not as literal text") - func defaultMarkerRendersAsNull() { + // The default is marked beside the value, so text a table holds that spells the old marker is a value. + @Test("Stored text that spells the old default marker renders as that text") + func oldMarkerTextIsAValue() { let converter = makeConverter( - columns: ["id", "name"], - columnTypes: [.integer(rawType: nil), .text(rawType: nil)] - ) - let result = converter.generateJson( - rows: [[.text(PluginCellValue.defaultMarkerText), .text("Ada")]] + columns: ["status", "name"], + columnTypes: [.text(rawType: nil), .text(rawType: nil)] ) + let result = converter.generateJson(rows: [[.text("__DEFAULT__"), .text("Ada")]]) - #expect(!result.contains(PluginCellValue.defaultMarkerText)) - #expect(result.contains("\"id\": null")) + #expect(result.contains("\"status\": \"__DEFAULT__\"")) #expect(result.contains("\"Ada\"")) } diff --git a/apple/TableProTests/Core/Utilities/SQL/QueryClassifierTSQLBlockTests.swift b/apple/TableProTests/Core/Utilities/SQL/QueryClassifierTSQLBlockTests.swift new file mode 100644 index 0000000000..86fc85825a --- /dev/null +++ b/apple/TableProTests/Core/Utilities/SQL/QueryClassifierTSQLBlockTests.swift @@ -0,0 +1,39 @@ +import Foundation +@testable import TablePro +import TableProSQLGrammar +import Testing + +// A T-SQL block runs as one statement, and a caller allowed one statement at a time is still refused one holding more. +struct QueryClassifierTSQLBlockTests { + @Test("A block holding more than one statement is multi-statement", arguments: [ + "IF @x = 1 BEGIN UPDATE a SET c = 1; UPDATE b SET c = 1; END", + "BEGIN TRY DELETE FROM a WHERE id = 1; DELETE FROM b WHERE id = 1; END TRY BEGIN CATCH SELECT 1; END CATCH", + "WHILE @i < 3 BEGIN SET @i += 1; PRINT @i; END", + "IF @x = 1 BEGIN SELECT 1; WITH c AS (SELECT id FROM t) DELETE FROM c; END", + "IF @x = 1 BEGIN SELECT 1; DISABLE TRIGGER trg ON t; END", + "BEGIN TRY SELECT 1; THROW 50000, 'x', 1; END TRY BEGIN CATCH SELECT 2; END CATCH", + "IF @x = 1 BEGIN SELECT 1; done: SELECT 2; END", + "IF @x = 1 BEGIN SELECT 1; (SELECT 2); END", + ]) + func blockOfSeveralIsMulti(sql: String) { + #expect(QueryClassifier.isMultiStatement(sql, databaseType: .mssql)) + } + + @Test("A conditional statement, or a block holding one, is a single statement", arguments: [ + "IF @x = 1 SELECT 1", + "IF @x = 1 BEGIN SELECT 1; END", + "BEGIN SELECT 1 END", + "BEGIN IF @x = 1 SELECT 1; ELSE SELECT 2; END", + "BEGIN TRY SELECT 1; END TRY BEGIN CATCH THROW; END CATCH", + ]) + func singleStatementIsNotMulti(sql: String) { + #expect(!QueryClassifier.isMultiStatement(sql, databaseType: .mssql)) + } + + @Test("A stored routine's body runs nothing, so it is one statement however many it holds") + func routineBodyIsNotMulti() { + #expect(!QueryClassifier.isMultiStatement( + "CREATE PROCEDURE dbo.p AS BEGIN UPDATE a SET c = 1; UPDATE b SET c = 1; END", databaseType: .mssql + )) + } +} diff --git a/apple/TableProTests/Core/Utilities/SQLRowToStatementConverterTests.swift b/apple/TableProTests/Core/Utilities/SQLRowToStatementConverterTests.swift index b5df30bfd0..8fc75fa0d1 100644 --- a/apple/TableProTests/Core/Utilities/SQLRowToStatementConverterTests.swift +++ b/apple/TableProTests/Core/Utilities/SQLRowToStatementConverterTests.swift @@ -384,8 +384,9 @@ struct SQLRowToStatementConverterTests { #expect(lines.count == 50_000) } - @Test("PostgreSQL: binary cell renders as bytea hex literal in INSERT") - func postgresBinaryInsertEmitsByteaLiteral() throws { + // `'\x…'::bytea` reads its backslash by `standard_conforming_strings`; `decode` means the same bytes either way. + @Test("PostgreSQL: binary cell decodes hex in INSERT") + func postgresBinaryInsertDecodesHex() throws { let converter = try SQLRowToStatementConverter( tableName: "documents", columns: ["id", "payload"], @@ -396,10 +397,28 @@ struct SQLRowToStatementConverterTests { ) let bytes = Data([0xD3, 0x8C, 0xE5, 0x66]) let result = converter.generateInserts(rows: plain([[.text("1"), .bytes(bytes)]])) - #expect(result.contains("'\\xD38CE566'::bytea")) + #expect(result.contains("decode('D38CE566', 'hex')")) #expect(!result.contains("NULL")) } + // DuckDB reads `X'…'` as the text `x…` and Oracle has no such literal. + @Test("DuckDB and Oracle binary cells use their own hex functions in INSERT", arguments: [ + (DatabaseType.duckdb, "unhex('CAFE')"), (DatabaseType.oracle, "HEXTORAW('CAFE')"), + ]) + func binaryInsertUsesTheEnginesHexFunction(databaseType: DatabaseType, literal: String) throws { + let converter = try SQLRowToStatementConverter( + tableName: "documents", + columns: ["id", "payload"], + primaryKeyColumns: ["id"], + databaseType: databaseType, + quoteIdentifier: { "\"\($0)\"" }, + escapeStringLiteral: { $0.replacingOccurrences(of: "'", with: "''") } + ) + let result = converter.generateInserts(rows: plain([[.text("1"), .bytes(Data([0xCA, 0xFE]))]])) + #expect(result.contains(literal)) + #expect(!result.contains("X'CAFE'")) + } + @Test("MySQL: binary cell renders as X'...' literal in INSERT") func mysqlBinaryInsertEmitsXLiteral() throws { let converter = try makeConverter( @@ -441,7 +460,7 @@ struct SQLRowToStatementConverterTests { ) let bytes = Data([0xAB, 0xCD]) let result = converter.generateUpdates(rows: plain([[.text("42"), .bytes(bytes)]])) - #expect(result.contains("\"payload\" = '\\xABCD'::bytea")) + #expect(result.contains("\"payload\" = decode('ABCD', 'hex')")) #expect(result.contains("WHERE \"id\" = '42'")) } @@ -656,8 +675,8 @@ struct SQLRowToStatementConverterTests { databaseType: .pglite, dialect: Self.postgresDialect ) let rows = plain([[.text("1"), .bytes(Data([0xDE, 0xAD]))]]) - #expect(converter.generateInserts(rows: rows).contains("'\\xDEAD'::bytea")) - #expect(converter.generateUpdates(rows: rows).contains("\"payload\" = '\\xDEAD'::bytea")) + #expect(converter.generateInserts(rows: rows).contains("decode('DEAD', 'hex')")) + #expect(converter.generateUpdates(rows: rows).contains("\"payload\" = decode('DEAD', 'hex')")) #expect(!converter.generateInserts(rows: rows).contains("X'DEAD'")) } } diff --git a/apple/TableProTests/Core/Vim/VimTextBufferAdapterPerfTests.swift b/apple/TableProTests/Core/Vim/VimTextBufferAdapterPerfTests.swift index 6fadfaf773..48c97aac20 100644 --- a/apple/TableProTests/Core/Vim/VimTextBufferAdapterPerfTests.swift +++ b/apple/TableProTests/Core/Vim/VimTextBufferAdapterPerfTests.swift @@ -2,8 +2,7 @@ // VimTextBufferAdapterPerfTests.swift // TableProTests // -// Regression tests for VimTextBufferAdapter incremental lineCount -// and setSelectedRange guard +// Regression tests for VimTextBufferAdapter lineCount and setSelectedRange guard // import AppKit @@ -65,83 +64,29 @@ struct VimTextBufferAdapterPerfTests { #expect(adapter.lineCount == 2) } - // MARK: - textDidChange incremental (pure insertion) - - @Test("textDidChange with pure insertion updates line count incrementally") - func insertionUpdatesLineCount() { - let (adapter, textView) = makeAdapter(string: "hello") - - // Prime the cache - let initial = adapter.lineCount - #expect(initial == 1) - - // Simulate inserting "\nworld" at offset 5 - textView.replaceCharacters(in: NSRange(location: 5, length: 0), with: "\nworld") - adapter.textDidChange(in: NSRange(location: 5, length: 0), replacementLength: 6) - - #expect(adapter.lineCount == 2) - } - - @Test("textDidChange with multi-newline insertion") - func multiNewlineInsertion() { - let (adapter, textView) = makeAdapter(string: "hello") - - _ = adapter.lineCount // prime cache - - textView.replaceCharacters(in: NSRange(location: 5, length: 0), with: "\n\n\n") - adapter.textDidChange(in: NSRange(location: 5, length: 0), replacementLength: 3) - - #expect(adapter.lineCount == 4) - } - - // MARK: - textDidChange fallback on deletion - - @Test("textDidChange with deletion invalidates cache") - func deletionInvalidatesCache() { - let (adapter, textView) = makeAdapter(string: "a\nb\nc") - - let initial = adapter.lineCount - #expect(initial == 3) - - // Simulate deleting "\nb" (range.length > 0 means it's not pure insertion) - textView.replaceCharacters(in: NSRange(location: 1, length: 2), with: "") - adapter.textDidChange(in: NSRange(location: 1, length: 2), replacementLength: 0) - - // Cache should be invalidated; next access does a full recount - #expect(adapter.lineCount == 2) - } - - // MARK: - textDidChange(oldText:...) incremental replacement - - @Test("textDidChange with oldText correctly computes delta for replacement") - func oldTextReplacementDelta() { - let originalText = "a\nb\nc" - let (adapter, textView) = makeAdapter(string: originalText) - - let initial = adapter.lineCount - #expect(initial == 3) - - // Replace "b\nc" (range 2..4, contains 1 newline) with "x" (0 newlines) - textView.replaceCharacters(in: NSRange(location: 2, length: 3), with: "x") - adapter.textDidChange(oldText: originalText, in: NSRange(location: 2, length: 3), replacementLength: 1) - - // 3 original - 1 removed newline + 0 added = 2 - #expect(adapter.lineCount == 2) - } - - @Test("textDidChange with oldText adding newlines") - func oldTextAddingNewlines() { - let originalText = "abc" - let (adapter, textView) = makeAdapter(string: originalText) - - _ = adapter.lineCount // prime: 1 - - // Replace "b" with "x\ny\nz" (adding 2 newlines) - textView.replaceCharacters(in: NSRange(location: 1, length: 1), with: "x\ny\nz") - adapter.textDidChange(oldText: originalText, in: NSRange(location: 1, length: 1), replacementLength: 5) - - // 1 original - 0 removed + 2 added = 3 - #expect(adapter.lineCount == 3) + // MARK: - Line count after an edit + + // The editor drops the cache on every edit, so the next count reads the text as it is now. + @Test("After an edit the line count matches a fresh count of the new text", arguments: [ + (NSRange(location: 5, length: 0), "\nworld", "hello", 2), + (NSRange(location: 5, length: 0), "\n\n\n", "hello", 3), + (NSRange(location: 1, length: 2), "", "a\nb\nc", 2), + (NSRange(location: 2, length: 3), "x", "a\nb\nc", 2), + (NSRange(location: 1, length: 1), "x\ny\nz", "abc", 3), + (NSRange(location: 1, length: 0), "\r", "ab", 2), + ]) + func lineCountAfterAnEdit(range: NSRange, replacement: String, original: String, expected: Int) { + let (adapter, textView) = makeAdapter(string: original) + _ = adapter.lineCount + + textView.replaceCharacters(in: range, with: replacement) + adapter.invalidateLineCache() + + let counted = withExtendedLifetime(textView) { adapter.lineCount } + let (fresh, freshTextView) = makeAdapter(string: textView.string) + let freshCount = withExtendedLifetime(freshTextView) { fresh.lineCount } + #expect(counted == expected) + #expect(counted == freshCount) } // MARK: - setSelectedRange guard diff --git a/apple/TableProTests/Helpers/StubExecutionGates.swift b/apple/TableProTests/Helpers/StubExecutionGates.swift index 83dce282ed..afcfa85d48 100644 --- a/apple/TableProTests/Helpers/StubExecutionGates.swift +++ b/apple/TableProTests/Helpers/StubExecutionGates.swift @@ -27,13 +27,18 @@ internal struct AlwaysAllowGate: ExecutionGate { internal struct AlwaysDenyGate: ExecutionGate { internal let reason: String + internal let cause: OperationDenialCause - internal init(reason: String = "Read-Only connection") { + internal init(reason: String = "Read-Only connection", cause: OperationDenialCause = .policy) { self.reason = reason + self.cause = cause } + // The user answering Cancel in the gate's confirmation. + internal static let cancelling = AlwaysDenyGate(reason: "Operation cancelled by user", cause: .cancelledByUser) + internal func authorize(_ request: OperationRequest) async -> OperationDecision { - .denied(reason: reason) + .denied(reason: reason, cause: cause) } } diff --git a/apple/TableProTests/Models/MultiRowEditStateTests.swift b/apple/TableProTests/Models/MultiRowEditStateTests.swift index ce162e198a..1cdb06f345 100644 --- a/apple/TableProTests/Models/MultiRowEditStateTests.swift +++ b/apple/TableProTests/Models/MultiRowEditStateTests.swift @@ -95,14 +95,14 @@ struct MultiRowEditStateTests { #expect(field.effectiveValue == nil) } - @Test("effectiveValue returns __DEFAULT__ when isPendingDefault") + @Test("effectiveValue returns the DEFAULT keyword when isPendingDefault") func effectiveValueReturnsDefaultWhenPendingDefault() { let field = FieldEditState( columnIndex: 0, columnName: "id", columnTypeEnum: .text(rawType: nil), isLongText: false, isJson: false, originalValue: "1", hasMultipleValues: false, pendingValue: nil, isPendingNull: false, isPendingDefault: true ) - #expect(field.effectiveValue == "__DEFAULT__") + #expect(field.effectiveValue == "DEFAULT") } @Test("effectiveValue returns nil when no edit is pending") @@ -644,13 +644,14 @@ struct MultiRowEditStateTests { #expect(edited[0].newValue == nil) } - @Test("Returns __DEFAULT__ for default edit") + @Test("Returns the DEFAULT keyword marked as SQL for a default edit") func returnsDefaultForDefaultEdit() { let sut = makeSUT() sut.setFieldToDefault(at: 0) let edited = sut.getEditedFields() #expect(edited.count == 1) - #expect(edited[0].newValue == "__DEFAULT__") + #expect(edited[0].newValue == "DEFAULT") + #expect(edited[0].isExpression) } @Test("Returns empty array when no edits") @@ -774,18 +775,22 @@ struct MultiRowEditStateTests { #expect(callbackCalls[0].value == nil) } - @Test("setFieldToDefault fires callback with __DEFAULT__") + // Through `onFieldChanged` the keyword would be a literal, which a stored value can also be. + @Test("setFieldToDefault sends the DEFAULT keyword through the expression callback") func setFieldToDefaultFiresCallback() { let sut = makeSUT() - var callbackCalls: [(index: Int, value: String?)] = [] - sut.onFieldChanged = { index, value, _ in - callbackCalls.append((index, value.asText)) + var literals: [PluginCellValue] = [] + var callbackCalls: [(index: Int, sql: String)] = [] + sut.onFieldChanged = { _, value, _ in literals.append(value) } + sut.onFieldExpressionChanged = { index, sql, _ in + callbackCalls.append((index, sql)) } sut.setFieldToDefault(at: 0) + #expect(literals.isEmpty) #expect(callbackCalls.count == 1) #expect(callbackCalls[0].index == 0) - #expect(callbackCalls[0].value == "__DEFAULT__") + #expect(callbackCalls[0].sql == "DEFAULT") } // `onFieldChanged` carries a literal, so an expression sent through it would be saved as quoted text. diff --git a/apple/TableProTests/Models/Schema/ClickHousePartStatementsTests.swift b/apple/TableProTests/Models/Schema/ClickHousePartStatementsTests.swift index cc67c4e094..cdee26b95c 100644 --- a/apple/TableProTests/Models/Schema/ClickHousePartStatementsTests.swift +++ b/apple/TableProTests/Models/Schema/ClickHousePartStatementsTests.swift @@ -2,9 +2,6 @@ // ClickHousePartStatementsTests.swift // TableProTests // -// The Parts tab's statements name the tab's own database, because ClickHouse resolves an -// unqualified one from a request parameter the app moves whenever any tab runs elsewhere. -// import Foundation @testable import TablePro @@ -14,8 +11,7 @@ struct ClickHousePartStatementsTests { private let quote: (String) -> String = { "`\($0.replacingOccurrences(of: "`", with: "``"))`" } private let escape: (String) -> String = { $0.replacingOccurrences(of: "'", with: "\\'") } - /// The defect: an unqualified DROP PARTITION deleted data from whichever database the shared - /// driver was last pinned to, while the tab reported it was acting on its own. + // An unqualified DROP PARTITION deleted data from whichever database the shared driver was last pinned to. @Test("Every statement names the database it was given") func statementsCarryTheDatabase() { #expect( @@ -24,20 +20,59 @@ struct ClickHousePartStatementsTests { ) #expect( ClickHousePartStatements.dropPartition( - database: "analytics", table: "events", partition: "202401", - quote: quote, escape: escape - ) == "ALTER TABLE `analytics`.`events` DROP PARTITION '202401'" + database: "analytics", table: "events", partitionId: "202401", quote: quote + ) == "ALTER TABLE `analytics`.`events` DROP PARTITION ID '202401'" ) #expect( ClickHousePartStatements.detachPartition( - database: "analytics", table: "events", partition: "202401", - quote: quote, escape: escape - ) == "ALTER TABLE `analytics`.`events` DETACH PARTITION '202401'" + database: "analytics", table: "events", partitionId: "202401", quote: quote + ) == "ALTER TABLE `analytics`.`events` DETACH PARTITION ID '202401'" ) } - /// `currentDatabase()` answers with the request parameter, so the list showed another database's - /// parts under this table's name. + // A tuple key's rendered value `(202401,'eu')` and an unpartitioned table's `tuple()` were each + // wrapped as one string literal, which names no partition. + @Test("Drop and Detach name the partition by its ID for every key shape") + func everyKeyShapeUsesThePartitionId() { + let shapes = [ + "202401-7", + "all", + "4f8b2b7c9a1d3e5f60718293a4b5c6d7", + "1--5", + ] + for id in shapes { + #expect( + ClickHousePartStatements.dropPartition( + database: "analytics", table: "events", partitionId: id, quote: quote + ) == "ALTER TABLE `analytics`.`events` DROP PARTITION ID '\(id)'" + ) + #expect( + ClickHousePartStatements.detachPartition( + database: "analytics", table: "events", partitionId: id, quote: quote + ) == "ALTER TABLE `analytics`.`events` DETACH PARTITION ID '\(id)'" + ) + } + } + + @Test("An ID the server would refuse is never spliced into a statement") + func unusableIdBuildsNothing() { + let unusable = ["", "it's", "a\\'b", "(202401,'eu')", "tuple()", "2024 01", "202401_1", "é"] + for id in unusable { + #expect(!ClickHousePartStatements.isValidPartitionId(id)) + #expect( + ClickHousePartStatements.dropPartition( + database: "analytics", table: "events", partitionId: id, quote: quote + ) == nil + ) + #expect( + ClickHousePartStatements.detachPartition( + database: "analytics", table: "events", partitionId: id, quote: quote + ) == nil + ) + } + } + + // `currentDatabase()` answers with the request parameter, so the list showed another database's parts. @Test("The parts read filters on the named database, never the connection's current one") func partsReadNamesTheDatabase() { let sql = ClickHousePartStatements.parts( @@ -48,9 +83,13 @@ struct ClickHousePartStatementsTests { #expect(!sql.contains("currentDatabase()")) } - /// A connection saved with no database of its own has nothing to qualify against, and an empty - /// qualifier is a statement the server refuses. The parts read has to fall back too: comparing - /// against '' matches nothing, where ClickHouse's own default database is what the user sees. + @Test("The parts read selects the partition ID beside the displayed partition value") + func partsReadSelectsThePartitionId() { + let sql = ClickHousePartStatements.parts(database: "analytics", table: "events", escape: escape) + #expect(sql.contains("SELECT partition, partition_id, name, rows, bytes_on_disk,")) + } + + // With no database the qualifier is refused by the server, and comparing against '' matches nothing. @Test("With no database the name stays unqualified and the read asks the server") func emptyDatabaseLeavesTheNameBare() { #expect( @@ -66,13 +105,11 @@ struct ClickHousePartStatementsTests { #expect(!sql.contains("database = ''")) } - @Test("A quote in a partition value is escaped, and a backtick in a name is quoted") - func valuesAreEscaped() { + @Test("A backtick in a database or table name is quoted") + func namesAreQuoted() { let sql = ClickHousePartStatements.dropPartition( - database: "we`ird", table: "ev`ents", partition: "it's", - quote: quote, escape: escape + database: "we`ird", table: "ev`ents", partitionId: "202401", quote: quote ) - #expect(sql.contains("`we``ird`.`ev``ents`")) - #expect(sql.contains("'it\\'s'")) + #expect(sql == "ALTER TABLE `we``ird`.`ev``ents` DROP PARTITION ID '202401'") } } diff --git a/apple/TableProTests/Views/Editor/StatementCaretTableTests.swift b/apple/TableProTests/Views/Editor/StatementCaretTableTests.swift new file mode 100644 index 0000000000..ba5ea78102 --- /dev/null +++ b/apple/TableProTests/Views/Editor/StatementCaretTableTests.swift @@ -0,0 +1,109 @@ +import AppKit +import Foundation +@testable import TablePro +import TableProEditorKit +import TableProGrammars +import TableProSQLGrammar +import TableProTextEngine +import Testing + +private let caretTableMongoScript = """ +db.users.find({ name: "a;b" }); +// note + +for (var i = 0; i < 3; i++) { + print(i); +} +db.orders.insertOne({ total: 1 }) +db.orders.find() +// trailing +""" + +private let caretTableRedisScript = "GET a; GET b\n\nSET k \"a\nb\"\nGET c \n " + +@MainActor +struct StatementCaretTableTests { + private struct Resolved: Equatable { + let offset: Int + let sql: String + let hasContent: Bool + let contentRange: NSRange + + init(_ statement: SQLStatementScanner.LocatedStatement) { + offset = statement.offset + sql = statement.sql + hasContent = statement.hasContent + contentRange = statement.contentRange + } + } + + @Test("Every caret resolves to what a scan of the whole document resolves to", arguments: [ + (QueryStatementModel.javascript, caretTableMongoScript), + (QueryStatementModel.redis, caretTableRedisScript), + (QueryStatementModel.javascript, ""), + (QueryStatementModel.redis, ""), + ]) + func everyCaretMatchesAFullScan(model: QueryStatementModel, text: String) throws { + let table = try #require(QueryStatementCaretTable(text: text, model: model)) + for caret in -1...((text as NSString).length + 1) { + let fresh = QueryStatementScanner.locatedStatementAtCursor( + in: text, cursorPosition: caret, model: model, grammar: .ansi + ) + #expect(Resolved(table.statement(atCursor: caret)) == Resolved(fresh), "caret \(caret)") + } + } + + @Test("A SQL document has no table, since it resumes through its own index") + func sqlHasNoTable() { + #expect(QueryStatementCaretTable(text: "SELECT 1;", model: .sql) == nil) + } + + private func makeController(text: String) -> TextViewController { + let configuration = SourceEditorConfiguration( + appearance: .init( + theme: ThemeEngine.shared.makeEditorTheme(), + font: .monospacedSystemFont(ofSize: 12, weight: .regular), + wrapLines: false + ), + peripherals: EditorPeripherals.editor(lineNumbers: true, folding: true, statementRunControls: true) + ) + let controller = TextViewController(string: text, language: .sql, configuration: configuration, cursorPositions: []) + controller.loadView() + return controller + } + + private func band(at caret: Int, in controller: TextViewController, subject: StatementRunController) -> NSRange? { + controller.setCursorPositions([CursorPosition(range: NSRange(location: caret, length: 0))]) + subject.refreshHighlight(in: controller) + return controller.highlightedStatementRange + } + + private func freshBand(at caret: Int, in controller: TextViewController) -> NSRange? { + let statement = QueryStatementScanner.locatedStatementAtCursor( + in: controller.textView.string, cursorPosition: caret, model: .javascript, grammar: .ansi + ) + guard statement.hasContent, statement.contentRange.length > 0 else { return nil } + return statement.contentRange + } + + @Test("Caret moves through a MongoDB script read it once, and an edit reads it again") + func caretMovesReadTheScriptOnce() { + let script = String(repeating: "db.orders.find({ total: 1 });\n", count: 2_000) + let controller = makeController(text: script) + let subject = StatementRunController() + subject.statementModel = .javascript + subject.install(on: controller) + let readsAtInstall = subject.caretTableReads + + for caret in stride(from: 59_000, to: 58_000, by: -7) { + #expect(band(at: caret, in: controller, subject: subject) == freshBand(at: caret, in: controller)) + } + #expect(subject.caretTableReads <= readsAtInstall + 1) + + controller.textView.replaceCharacters(in: NSRange(location: 0, length: 0), with: "db.a.find();\n") + let afterEdit = subject.caretTableReads + #expect(band(at: 100, in: controller, subject: subject) == freshBand(at: 100, in: controller)) + #expect(band(at: 58_013, in: controller, subject: subject) == freshBand(at: 58_013, in: controller)) + #expect(subject.caretTableReads == afterEdit + 1) + } +} diff --git a/apple/TableProTests/Views/Main/TableLoadAfterSafeModePromptTests.swift b/apple/TableProTests/Views/Main/TableLoadAfterSafeModePromptTests.swift new file mode 100644 index 0000000000..920544485f --- /dev/null +++ b/apple/TableProTests/Views/Main/TableLoadAfterSafeModePromptTests.swift @@ -0,0 +1,40 @@ +import Foundation +@testable import TablePro +import TableProPluginKit +import TableProTestSupport +import Testing + +@MainActor +struct TableLoadAfterSafeModePromptTests { + @Test("A table load turned away while a query waits for approval runs once that approval closes") + func turnedAwayLoadRunsAfterThePrompt() async throws { + let connection = DatabaseConnection( + name: "Confirm", database: "app", type: .postgresql, safeModeLevel: .alertFull + ) + let fixture = CoordinatorFixture( + connection: connection, + driver: ScriptAnsweringDriver(connection: connection, sendsBatchesWhole: false) + ) + fixture.coordinator.toolbarState.safeModeLevel = .alertFull + let gate = HeldExecutionGate() + fixture.coordinator.executionGate = gate + defer { fixture.coordinator.cancelAllQueryTasks() } + + fixture.addTab(QueryTab(title: "Query", query: "SELECT 1", tabType: .query)) + #expect(fixture.coordinator.runStatement("SELECT 1")) + try await waitUntil { gate.askedCount == 1 } + + var table = QueryTab(title: "users", query: "SELECT * FROM users LIMIT 1000", tabType: .table, tableName: "users") + table.pagination.isLoading = true + fixture.addTab(table) + #expect(fixture.coordinator.executeTableTabQueryDirectly(viewport: .firstRow) == nil) + #expect(fixture.tabManager.tabs.first { $0.id == table.id }?.pagination.isLoading == false) + + await gate.answer(allowing: true) + try await waitUntil { gate.askedCount == 2 } + await gate.answer(allowing: false) + try await waitUntil { + fixture.tabManager.tabs.first { $0.id == table.id }?.execution.errorMessage == "Refused by the test" + } + } +} diff --git a/apple/TableProTests/Views/Main/WindowCloseSaveEveryConnectionTests.swift b/apple/TableProTests/Views/Main/WindowCloseSaveEveryConnectionTests.swift new file mode 100644 index 0000000000..bb0a30c859 --- /dev/null +++ b/apple/TableProTests/Views/Main/WindowCloseSaveEveryConnectionTests.swift @@ -0,0 +1,129 @@ +import AppKit +import Foundation +import SwiftUI +@testable import TablePro +import TableProPluginKit +import TableProTestSupport +import Testing + +@MainActor +struct WindowCloseSaveEveryConnectionTests { + private func actions( + for coordinator: MainContentCoordinator, + in window: NSWindow, + attach: Bool = true + ) -> MainContentCommandActions { + var selectedTables: Set = [] + var pendingTruncates: Set = [] + var pendingDeletes: Set = [] + var tableOperationOptions: [DatabaseTreeTableRef: TableOperationOptions] = [:] + let actions = MainContentCommandActions( + coordinator: coordinator, + connection: coordinator.connection, + selectedTables: Binding(get: { selectedTables }, set: { selectedTables = $0 }), + pendingTruncates: Binding(get: { pendingTruncates }, set: { pendingTruncates = $0 }), + pendingDeletes: Binding(get: { pendingDeletes }, set: { pendingDeletes = $0 }), + tableOperationOptions: Binding( + get: { tableOperationOptions }, + set: { tableOperationOptions = $0 } + ), + trailingPaneState: TrailingPaneState() + ) + actions.window = window + if attach { coordinator.commandActions = actions } + return actions + } + + @Test("Save on a window close keeps the window while another connection holds edits only their tab can save") + func saveKeepsTheWindowForAnotherConnectionsEdits() async throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let front = try harness.coordinator(of: harness.foreground) + let back = try harness.coordinator(of: harness.background) + let frontActions = actions(for: front, in: harness.window) + let backActions = actions(for: back, in: harness.window) + try harness.addSelectedTab(to: harness.foreground) + try harness.addSelectedTab(to: harness.background) + + var edited = QueryTab(title: "users", query: "SELECT * FROM users", tabType: .table, tableName: "users") + edited.pendingChanges.insertedRowIDs = [.inserted(UUID())] + edited.pendingChanges.columns = ["id", "name"] + edited.pendingChanges.primaryKeyColumns = ["id"] + back.tabManager.tabs.append(edited) + frontActions.confirmSaveChanges = { _, _ in .save } + + let outcome = await frontActions.closeWindowAwaiting() + + #expect(outcome == .cancelled) + #expect(back.tabManager.tabs.contains { $0.id == edited.id }) + withExtendedLifetime(backActions) {} + } + + // Its actions arrive only once its pane mounts, after Save has shown it. + @Test("Save waits for a connection that has not shown its pane yet, then saves it and closes") + func saveReachesAConnectionNotShownYet() async throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let front = try harness.coordinator(of: harness.foreground) + let back = try harness.coordinator(of: harness.background) + front.contentWindow = harness.window + let frontActions = actions(for: front, in: harness.window) + let backActions = actions(for: back, in: harness.window, attach: false) + back.commandActions = nil + try harness.addSelectedTab(to: harness.foreground) + back.toolbarState.hasCreateTablePending = true + frontActions.confirmSaveChanges = { _, _ in + Task { @MainActor in back.commandActions = backActions } + return .save + } + + let outcome = await frontActions.closeWindowAwaiting() + + #expect(outcome == .closed) + } + + @Test("A close cancelled while it waits for a connection's pane stops after that one wait and keeps the window") + func cancelledCloseStopsWaiting() async throws { + let harness = TwoConnectionWindow() + defer { harness.tearDown() } + let front = try harness.coordinator(of: harness.foreground) + let back = try harness.coordinator(of: harness.background) + front.contentWindow = harness.window + let frontActions = actions(for: front, in: harness.window) + back.commandActions = nil + try harness.addSelectedTab(to: harness.foreground) + back.toolbarState.hasCreateTablePending = true + let clock = CountingClock() + frontActions.paneWaitClock = clock + frontActions.confirmSaveChanges = { _, _ in .save } + + let close = Task { await frontActions.closeWindowAwaiting() } + await clock.firstSleep.wait() + close.cancel() + let outcome = await close.value + + #expect(outcome == .cancelled) + #expect(clock.sleepCount == 1) + } +} + +// Time stands still, so a sleep ends only by cancellation; the first one opens a gate the test waits on. +private final class CountingClock: Clock, @unchecked Sendable { + let firstSleep = AsyncGate() + private let base = ManualClock() + private let lock = NSLock() + private var count = 0 + + var sleepCount: Int { lock.withLock { count } } + var now: ManualClock.Instant { base.now } + var minimumResolution: Duration { base.minimumResolution } + + func sleep(until deadline: ManualClock.Instant, tolerance: Duration?) async throws { + let isFirst = lock.withLock { + count += 1 + return count == 1 + } + if isFirst { await firstSleep.open() } + try await base.sleep(until: deadline, tolerance: tolerance) + } +} diff --git a/apple/TableProTests/Views/Results/ResultsJsonViewTests.swift b/apple/TableProTests/Views/Results/ResultsJsonViewTests.swift index d64d2975d0..167773b1fa 100644 --- a/apple/TableProTests/Views/Results/ResultsJsonViewTests.swift +++ b/apple/TableProTests/Views/Results/ResultsJsonViewTests.swift @@ -128,6 +128,30 @@ struct ResultsJsonViewTests { } } + @Test("A cell a pending edit set to the default renders as null, and stored text that reads DEFAULT stays text") + func pendingDefaultRendersAsNull() { + let inserted = RowID.inserted(UUID()) + let tableRows = TableRows( + rows: [ + Row(id: .existing(0), values: [.text("DEFAULT"), .text("a")]), + Row(id: inserted, values: [.columnDefault, .text("b")]), + ], + columns: ["status", "name"], + columnTypes: [.text(rawType: nil), .text(rawType: nil)] + ) + + let result = ResultsJsonView.computeJson( + tableRows: tableRows, + displayIDs: nil, + selectedIndices: [], + defaultedCells: [inserted: [0]], + columnLayout: ColumnLayoutState() + ) + + #expect(result.json.contains("\"status\": \"DEFAULT\"")) + #expect(result.json.contains("\"status\": null")) + } + @Test("an empty result renders an empty array") func emptyResultRendersEmptyArray() { let result = ResultsJsonView.computeJson( diff --git a/apple/TableProTests/Views/Results/SelectionSummaryTrackerTests.swift b/apple/TableProTests/Views/Results/SelectionSummaryTrackerTests.swift index 26110af586..926bf1f31f 100644 --- a/apple/TableProTests/Views/Results/SelectionSummaryTrackerTests.swift +++ b/apple/TableProTests/Views/Results/SelectionSummaryTrackerTests.swift @@ -176,7 +176,7 @@ struct SelectionSummaryTrackerTests { defer { grid.close() } grid.model.setSelection(Self.block(rows: 0...1, columns: 0...0)) - grid.edit(row: 1, to: .value(.text(PluginCellValue.defaultMarkerText), isExpression: false)) + grid.edit(row: 1, to: .value(.columnDefault, isExpression: true)) await grid.tracker.pendingTask?.value #expect(state.summary?.emptyCount == 1) diff --git a/apple/TableProTests/Views/Structure/ClickHousePartsGridTests.swift b/apple/TableProTests/Views/Structure/ClickHousePartsGridTests.swift index 41044fa125..fde04c7c82 100644 --- a/apple/TableProTests/Views/Structure/ClickHousePartsGridTests.swift +++ b/apple/TableProTests/Views/Structure/ClickHousePartsGridTests.swift @@ -5,12 +5,14 @@ import Foundation @testable import TablePro +import TableProPluginKit import Testing @MainActor struct ClickHousePartsGridTests { private let part = ClickHousePartInfo( partition: "202601", + partitionId: "202601", name: "202601_1_4_1", rows: 1_234_567, bytesOnDisk: 5_242_880, @@ -35,7 +37,7 @@ struct ClickHousePartsGridTests { @Test("An inactive part reads NO") func inactivePart() { let inactive = ClickHousePartInfo( - partition: "", name: "p", rows: 0, bytesOnDisk: 0, modificationTime: "", active: false + partition: "", partitionId: "", name: "p", rows: 0, bytesOnDisk: 0, modificationTime: "", active: false ) #expect(ClickHousePartsView.gridRow(for: inactive).values.last == "NO") } @@ -43,7 +45,7 @@ struct ClickHousePartsGridTests { @Test("Active reads aloud as Active or Inactive rather than YES or NO") func activeIsSpokenByName() { let inactive = ClickHousePartInfo( - partition: "", name: "p", rows: 0, bytesOnDisk: 0, modificationTime: "", active: false + partition: "", partitionId: "", name: "p", rows: 0, bytesOnDisk: 0, modificationTime: "", active: false ) let active = ClickHousePartsView.activeColumn @@ -59,23 +61,25 @@ struct ClickHousePartsGridTests { #expect(zip(ClickHousePartsView.columnMinimumWidths, ClickHousePartsView.columnWidths).allSatisfy { $0 <= $1 }) } - @Test("Drop and Detach name the one selected part's partition, and none while several are selected") + @Test("Drop and Detach act on the one selected part, and on none while several are selected") func selectedPartition() { let other = ClickHousePartInfo( - partition: "202602", name: "202602_1_1_0", rows: 1, bytesOnDisk: 1, modificationTime: "", active: true + partition: "202602", partitionId: "202602", name: "202602_1_1_0", rows: 1, bytesOnDisk: 1, + modificationTime: "", active: true ) let parts = [part, other] - #expect(ClickHousePartsView.partition(of: [other.id], in: parts) == "202602") - #expect(ClickHousePartsView.partition(of: [part.id, other.id], in: parts) == nil) - #expect(ClickHousePartsView.partition(of: [], in: parts) == nil) - #expect(ClickHousePartsView.partition(of: [UUID()], in: parts) == nil) + #expect(ClickHousePartsView.selectedPart(of: [other.id], in: parts)?.id == other.id) + #expect(ClickHousePartsView.selectedPart(of: [part.id, other.id], in: parts)?.id == nil) + #expect(ClickHousePartsView.selectedPart(of: [], in: parts)?.id == nil) + #expect(ClickHousePartsView.selectedPart(of: [UUID()], in: parts)?.id == nil) } @Test("A selected grid row names its own part") func selectedRowResolvesToItsPart() throws { let other = ClickHousePartInfo( - partition: "202602", name: "202602_1_1_0", rows: 1, bytesOnDisk: 1, modificationTime: "", active: true + partition: "202602", partitionId: "202602", name: "202602_1_1_0", rows: 1, bytesOnDisk: 1, + modificationTime: "", active: true ) let parts = [part, other] let grid = StructureListGridModel() @@ -95,7 +99,84 @@ struct ClickHousePartsGridTests { #expect(grid.id(for: second) == other.id) #expect(selected == [other.id]) - #expect(ClickHousePartsView.partition(of: selected, in: parts) == "202602") + #expect(ClickHousePartsView.selectedPart(of: selected, in: parts)?.partition == "202602") #expect(grid.model.rowID(at: 2) == nil) } + + @Test("A parts row keeps the partition ID a statement names beside the value the grid shows") + func rowCarriesThePartitionId() throws { + let row: [PluginCellValue] = [ + "(202401,'eu')", + "c1f8e6e2a7b04d4c9e1f0a2b3c4d5e6f", + "c1f8e6e2a7b04d4c9e1f0a2b3c4d5e6f_1_1_0", + "12", + "4096", + "2026-01-05 10:00:00", + "1", + ] + + let parsed = try #require(ClickHousePartsView.part(from: row)) + + #expect(parsed.partition == "(202401,'eu')") + #expect(parsed.partitionId == "c1f8e6e2a7b04d4c9e1f0a2b3c4d5e6f") + #expect(parsed.name == "c1f8e6e2a7b04d4c9e1f0a2b3c4d5e6f_1_1_0") + #expect(parsed.rows == 12) + #expect(parsed.bytesOnDisk == 4096) + #expect(parsed.modificationTime == "2026-01-05 10:00:00") + #expect(parsed.active) + #expect(ClickHousePartsView.gridRow(for: parsed).values[0] == "(202401,'eu')") + } + + // The pane used to swap the list and its toolbar for the error text until the tab was refreshed. + @Test("A refused or failed action keeps the parts list and its toolbar on screen") + func actionFailureKeepsTheList() { + var pane = ClickHousePartsPaneState() + pane.beginLoad() + pane.finishLoad([part]) + + pane.actionEnded(.refused("Safe Mode is read-only"), title: "Couldn't Drop Partition") + #expect(pane.display == .parts) + #expect(pane.alert == ClickHousePartsAlert(title: "Couldn't Drop Partition", message: "Safe Mode is read-only")) + + pane.alert = nil + pane.actionEnded(.failed("Wrong partition value"), title: "Couldn't Detach Partition") + #expect(pane.display == .parts) + #expect(pane.alert == ClickHousePartsAlert(title: "Couldn't Detach Partition", message: "Wrong partition value")) + } + + @Test("A Cancel at the gate's confirmation shows nothing") + func cancelIsQuiet() { + var pane = ClickHousePartsPaneState() + pane.finishLoad([part]) + + pane.actionEnded( + GatedOperationFailure(ExecutionGateError.cancelledByUser("Operation cancelled by user")), + title: "Couldn't Drop Partition" + ) + + #expect(pane.alert == nil) + #expect(pane.display == .parts) + } + + @Test("A load that fails with nothing shown offers Try Again, and a reload that fails keeps the list") + func loadFailure() { + var pane = ClickHousePartsPaneState() + #expect(pane.display == .loading) + + pane.failLoad("Connection refused") + #expect(pane.display == .loadFailed("Connection refused")) + #expect(pane.alert == nil) + + pane.beginLoad() + #expect(pane.display == .loading) + pane.finishLoad([]) + #expect(pane.display == .empty) + + pane.finishLoad([part]) + pane.beginLoad() + #expect(pane.display == .parts) + pane.failLoad("Timeout exceeded") + #expect(pane.display == .parts) + #expect(pane.alert?.message == "Timeout exceeded") + } } diff --git a/apple/TableProTests/Views/Structure/StructureListGridAccessibilityTests.swift b/apple/TableProTests/Views/Structure/StructureListGridAccessibilityTests.swift index b4e8440113..7910b8ae10 100644 --- a/apple/TableProTests/Views/Structure/StructureListGridAccessibilityTests.swift +++ b/apple/TableProTests/Views/Structure/StructureListGridAccessibilityTests.swift @@ -109,7 +109,8 @@ struct StructureListGridAccessibilityTests { defer { DataGridAccessibility.isActive = false } let parts = [true, false].map { active in ClickHousePartInfo( - partition: "202601", name: "p", rows: 1, bytesOnDisk: 1, modificationTime: "", active: active + partition: "202601", partitionId: "202601", name: "p", rows: 1, bytesOnDisk: 1, modificationTime: "", + active: active ) } let rows = parts.map { part in diff --git a/apple/TableProTests/Views/Structure/StructureSaveConfirmationTests.swift b/apple/TableProTests/Views/Structure/StructureSaveConfirmationTests.swift index 348093f635..fcb0310781 100644 --- a/apple/TableProTests/Views/Structure/StructureSaveConfirmationTests.swift +++ b/apple/TableProTests/Views/Structure/StructureSaveConfirmationTests.swift @@ -235,7 +235,7 @@ struct StructureSaveConfirmationTests { ).authorize(request) let error = try #require(decision.denialError) - #expect(StructureApplyFailure(error) == .cancelledByUser) + #expect(GatedOperationFailure(error) == .cancelledByUser) } @Test("A declined Touch ID is a refusal that says why, not a quiet Cancel") @@ -252,7 +252,7 @@ struct StructureSaveConfirmationTests { ).authorize(request) let error = try #require(decision.denialError) - let failure = StructureApplyFailure(error) + let failure = GatedOperationFailure(error) #expect(failure.outcome == .refused) #expect(failure.message != nil) #expect(!failure.reportsFailure) @@ -404,7 +404,7 @@ struct StructureSaveConfirmationTests { @Test("A Cancel leaves the edits staged and shows nothing") func cancelIsQuiet() { - let failure = StructureApplyFailure(ExecutionGateError.cancelledByUser("Operation cancelled by user")) + let failure = GatedOperationFailure(ExecutionGateError.cancelledByUser("Operation cancelled by user")) #expect(failure == .cancelledByUser) #expect(failure.outcome == .refused) @@ -414,7 +414,7 @@ struct StructureSaveConfirmationTests { @Test("A Safe Mode refusal leaves the edits staged and says why, and is not a failed operation") func refusalExplainsItself() { - let failure = StructureApplyFailure(ExecutionGateError.denied("Safe Mode is read-only")) + let failure = GatedOperationFailure(ExecutionGateError.denied("Safe Mode is read-only")) #expect(failure.outcome == .refused) #expect(failure.message == "Safe Mode is read-only") @@ -423,7 +423,7 @@ struct StructureSaveConfirmationTests { @Test("A statement the server rejected is a failed save") func serverRejectionIsAFailure() { - let failure = StructureApplyFailure(ServerRejection()) + let failure = GatedOperationFailure(ServerRejection()) #expect(failure.outcome == .failed("Duplicate column name 'notes'")) #expect(failure.message == "Duplicate column name 'notes'") diff --git a/apple/TableProTests/Views/Structure/TriggerEditorFontSizeTests.swift b/apple/TableProTests/Views/Structure/TriggerEditorFontSizeTests.swift new file mode 100644 index 0000000000..3745115a46 --- /dev/null +++ b/apple/TableProTests/Views/Structure/TriggerEditorFontSizeTests.swift @@ -0,0 +1,25 @@ +import AppKit +@testable import TablePro +import TableProEditorKit +import Testing + +@MainActor +struct TriggerEditorFontSizeTests { + @Test("New and Edit Trigger open at the size the DDL pane is zoomed to") + func opensAtTheStoredSize() throws { + let zoomed = try EphemeralDefaults() + zoomed.defaults.set(18.0, forKey: TriggerEditorView.fontSizeKey) + + #expect(TriggerEditorView.initialConfiguration(in: zoomed.defaults).appearance.font.pointSize == 18) + } + + @Test("Without a stored size the sheet opens at the default") + func opensAtTheDefaultSize() throws { + let fresh = try EphemeralDefaults() + + #expect( + TriggerEditorView.initialConfiguration(in: fresh.defaults).appearance.font.pointSize + == CGFloat(TriggerEditorView.defaultFontSize) + ) + } +} diff --git a/core/crates/core/src/export/csv.rs b/core/crates/core/src/export/csv.rs index f3ff38e741..55ad66485b 100644 --- a/core/crates/core/src/export/csv.rs +++ b/core/crates/core/src/export/csv.rs @@ -184,11 +184,30 @@ mod tests { } } + fn sanitized() -> CsvOptions { + CsvOptions { + sanitize_formulas: true, + ..Default::default() + } + } + + // An importer reads the quote a sanitized field gains as part of the value. #[test] - fn csv_defaults_quote_every_field_when_sanitising() { + fn csv_defaults_write_values_as_they_are() { + let columns = cols(&["id", "handle"]); + let rows = vec![ + vec![Value::Int(1), Value::Text("@handle".into())], + vec![Value::Int(2), Value::Text("-pending".into())], + ]; + let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + assert_eq!(out, "id,handle\n1,@handle\n2,-pending\n"); + } + + #[test] + fn csv_sanitising_quotes_every_field() { let columns = cols(&["id", "name"]); let rows = vec![vec![Value::Int(1), Value::Text("Alice".into())]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out, "\"id\",\"name\"\n\"1\",\"Alice\"\n"); } @@ -266,7 +285,7 @@ mod tests { let rows = vec![vec![Value::Text("=cmd".into())]]; let opts = CsvOptions { quote: CsvQuote::Never, - ..Default::default() + ..sanitized() }; let out = render_csv(&columns, &rows, &opts).unwrap(); assert_eq!(out, "\"a\"\n\"'=cmd\"\n"); @@ -277,7 +296,7 @@ mod tests { let columns = cols(&["a"]); for ch in ['=', '+', '-', '@'] { let rows = vec![vec![Value::Text(format!("{ch}cmd"))]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out, format!("\"a\"\n\"'{ch}cmd\"\n"), "prefix {ch} should be sanitized"); } } @@ -287,7 +306,7 @@ mod tests { let columns = cols(&["a"]); for lead in ['\t', '\r', '\n'] { let rows = vec![vec![Value::Text(format!("{lead}=cmd|'/C calc'!A0"))]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); let expected = format!("\"a\"\n\"'{lead}=cmd|'/C calc'!A0\"\n"); assert_eq!(out, expected, "lead {lead:?} should be sanitized and quoted"); } @@ -298,7 +317,7 @@ mod tests { let columns = cols(&["a"]); for lead in ['\u{FF1D}', '\u{FF0B}', '\u{FF0D}', '\u{FF20}'] { let rows = vec![vec![Value::Text(format!("{lead}SUM(A1)"))]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!( out, format!("\"a\"\n\"'{lead}SUM(A1)\"\n"), @@ -319,7 +338,7 @@ mod tests { fn csv_does_not_sanitize_non_formula_prefixes() { let columns = cols(&["a"]); let rows = vec![vec![Value::Text("plain text".into())]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out, "\"a\"\n\"plain text\"\n"); } @@ -347,7 +366,7 @@ mod tests { #[test] fn csv_keeps_negative_int() { let columns = cols(&["a"]); - let out = render_csv(&columns, &[vec![Value::Int(-5)]], &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &[vec![Value::Int(-5)]], &sanitized()).unwrap(); assert_eq!(out, "\"a\"\n\"-5\"\n"); } @@ -355,14 +374,14 @@ mod tests { fn csv_keeps_negative_decimal() { let columns = cols(&["a"]); let rows = vec![vec![Value::Decimal("-12.30".parse().expect("a decimal"))]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out, "\"a\"\n\"-12.30\"\n"); } #[test] fn csv_keeps_negative_float() { let columns = cols(&["a"]); - let out = render_csv(&columns, &[vec![Value::Float64(-1.5)]], &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &[vec![Value::Float64(-1.5)]], &sanitized()).unwrap(); assert_eq!(out, "\"a\"\n\"-1.5\"\n"); } @@ -371,7 +390,7 @@ mod tests { let columns = cols(&["a"]); for text in ["-12", "+1.5e3", "-.5", "1.", "-7E-3"] { let rows = vec![vec![Value::Text(text.into())]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out, format!("\"a\"\n\"{text}\"\n"), "{text} is a number, not a formula"); } } @@ -381,7 +400,7 @@ mod tests { let columns = cols(&["a"]); for text in ["-cmd", "=SUM(A1)", "+1+cmd", "-", "-.", "-1e"] { let rows = vec![vec![Value::Text(text.into())]]; - let out = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out, format!("\"a\"\n\"'{text}\"\n"), "{text} must be neutralised"); } } @@ -401,7 +420,7 @@ mod tests { other("-2+cmd"), ]; for value in neutralised { - let out = render_csv(&columns, &[vec![value.clone()]], &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &[vec![value.clone()]], &sanitized()).unwrap(); assert!(out.contains("\"'"), "{value:?} reached the sheet as a formula: {out}"); } let kept = [ @@ -412,7 +431,7 @@ mod tests { (other("+1.5e3"), "+1.5e3"), ]; for (value, text) in kept { - let out = render_csv(&columns, &[vec![value]], &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &[vec![value]], &sanitized()).unwrap(); assert_eq!(out, format!("\"a\"\n\"{text}\"\n"), "{text} is data, not a formula"); } } @@ -420,14 +439,14 @@ mod tests { #[test] fn csv_neutralises_formula_header() { let columns = cols(&["=HYPERLINK(\"x\")"]); - let out = render_csv(&columns, &[], &CsvOptions::default()).unwrap(); + let out = render_csv(&columns, &[], &sanitized()).unwrap(); assert_eq!(out, "\"'=HYPERLINK(\"\"x\"\")\"\n"); } #[test] fn text_csv_treats_every_field_as_free_text() { let records = vec![vec!["-cmd".to_string(), "12".to_string(), String::new()]]; - let out = render_text_csv(&["q", "n", "e"], &records, &CsvOptions::default()).unwrap(); + let out = render_text_csv(&["q", "n", "e"], &records, &sanitized()).unwrap(); assert_eq!(out, "\"q\",\"n\",\"e\"\n\"'-cmd\",\"12\",\"\"\n"); } @@ -449,11 +468,11 @@ mod tests { fn csv_null_modes() { let columns = cols(&["a"]); let rows = vec![vec![Value::Null]]; - let out_empty = render_csv(&columns, &rows, &CsvOptions::default()).unwrap(); + let out_empty = render_csv(&columns, &rows, &sanitized()).unwrap(); assert_eq!(out_empty, "\"a\"\n\"\"\n"); let opts = CsvOptions { null_to_empty: false, - ..Default::default() + ..sanitized() }; let out_null = render_csv(&columns, &rows, &opts).unwrap(); assert_eq!(out_null, "\"a\"\n\"NULL\"\n"); diff --git a/core/crates/core/src/export/csv_options.rs b/core/crates/core/src/export/csv_options.rs index 3ff8f7adaf..409842ab0d 100644 --- a/core/crates/core/src/export/csv_options.rs +++ b/core/crates/core/src/export/csv_options.rs @@ -68,7 +68,9 @@ impl Default for CsvOptions { null_to_empty: true, line_break_to_space: false, header_row: true, - sanitize_formulas: true, + // RFC 4180 carries a field as written, and OWASP scopes the quote prefix to files meant + // for a spreadsheet: an importer keeps the quote as part of the value. + sanitize_formulas: false, delimiter: CsvDelimiter::Comma, quote: CsvQuote::IfNeeded, line_break: CsvLineBreak::Lf, diff --git a/core/crates/core/src/export/encoder/tests.rs b/core/crates/core/src/export/encoder/tests.rs index 1e65cb7aa8..6484071e73 100644 --- a/core/crates/core/src/export/encoder/tests.rs +++ b/core/crates/core/src/export/encoder/tests.rs @@ -377,7 +377,11 @@ fn csv_neutralises_text_not_negative_numbers_through_encoder() { vec![Value::Text("-12".into())], ]; - let out = streamed(CsvEncoder::new(columns, CsvOptions::default()), &rows); + let options = CsvOptions { + sanitize_formulas: true, + ..CsvOptions::default() + }; + let out = streamed(CsvEncoder::new(columns, options), &rows); assert_eq!( out, diff --git a/core/crates/mcp/src/export_tools.rs b/core/crates/mcp/src/export_tools.rs index 971b6b73a6..bdcf434f47 100644 --- a/core/crates/mcp/src/export_tools.rs +++ b/core/crates/mcp/src/export_tools.rs @@ -158,7 +158,15 @@ fn render( rows: &[Vec], ) -> Result { let encoded = match format { - ExportFormat::Csv => render_csv(columns, rows, &CsvOptions::default()), + // A tool result has no option to turn it on, so it keeps the neutralising the macOS tool result has. + ExportFormat::Csv => render_csv( + columns, + rows, + &CsvOptions { + sanitize_formulas: true, + ..CsvOptions::default() + }, + ), ExportFormat::Json => render_json(columns, rows), ExportFormat::Sql => { let dialect = session diff --git a/core/crates/storage/src/query_history/export.rs b/core/crates/storage/src/query_history/export.rs index b9d9bacdcc..a4753f5251 100644 --- a/core/crates/storage/src/query_history/export.rs +++ b/core/crates/storage/src/query_history/export.rs @@ -17,7 +17,11 @@ const HISTORY_CSV_HEADER: [&str; 10] = [ pub(super) fn history_csv(entries: &[Entry]) -> Result { let records: Vec> = entries.iter().map(history_record).collect(); - let options = tablepro_core::export::CsvOptions::default(); + // The history export has no option of its own, so it keeps neutralising rather than follow the export default. + let options = tablepro_core::export::CsvOptions { + sanitize_formulas: true, + ..tablepro_core::export::CsvOptions::default() + }; Ok(tablepro_core::export::render_text_csv( &HISTORY_CSV_HEADER, &records, diff --git a/core/crates/test-support/src/conformance/cases/large_table.rs b/core/crates/test-support/src/conformance/cases/large_table.rs index 4b77f07bfb..5dc295e763 100644 --- a/core/crates/test-support/src/conformance/cases/large_table.rs +++ b/core/crates/test-support/src/conformance/cases/large_table.rs @@ -6,7 +6,7 @@ use std::time::{Duration, Instant}; use async_trait::async_trait; use tablepro_core::browse::{ExportRequest, PageRead, PageRequest, PagingMode, build_export, read_page}; -use tablepro_core::export::{CsvEncoder, CsvOptions, RowEncoder}; +use tablepro_core::export::{CsvEncoder, CsvOptions, CsvQuote, RowEncoder}; use tablepro_core::meta::{PagePosition, RowIdentity, TableRef}; use tablepro_core::{ ColumnInfo, Connection, FilterSet, ReadStatement, ResultEvent, ResultSink, SinkFlow, UserSql, Value, WriteKind, @@ -538,7 +538,12 @@ impl ResultSink for CsvFileSink { match event { ResultEvent::ResultSetStarted { columns } => { lock(&self.tally).result_sets += 1; - let mut encoder = CsvEncoder::new(columns, CsvOptions::default()); + // `check_file` reads every field quoted. + let options = CsvOptions { + quote: CsvQuote::Always, + ..CsvOptions::default() + }; + let mut encoder = CsvEncoder::new(columns, options); encoder.begin(&mut self.buffer); self.encoder = Some(encoder); SinkFlow::Continue diff --git a/core/crates/test-support/src/conformance/cases/round_trip.rs b/core/crates/test-support/src/conformance/cases/round_trip.rs index 9e17db85c6..94f0a82d79 100644 --- a/core/crates/test-support/src/conformance/cases/round_trip.rs +++ b/core/crates/test-support/src/conformance/cases/round_trip.rs @@ -47,7 +47,7 @@ const IMPORT_DEADLINE: Duration = Duration::from_secs(120); /// that hangs. const EXPORT_DEADLINE: Duration = Duration::from_secs(120); -/// Under the export defaults these rows of 200-character labels make a +/// With every field quoted these rows of 200-character labels make a /// file just under `MAX_CSV_FILE_BYTES`. 200 characters is the widest /// label every engine's scratch table takes. const NEAR_LIMIT_ROWS: u64 = 315_000; @@ -428,7 +428,11 @@ pub async fn csv_export_near_the_import_limit_imports_back( FILL_DEADLINE, ) .await?; - let file = export_file(connection.as_ref(), &source, csv_encoder(CsvOptions::default())).await?; + let quoted = CsvOptions { + quote: CsvQuote::Always, + ..CsvOptions::default() + }; + let file = export_file(connection.as_ref(), &source, csv_encoder(quoted)).await?; let size = file.len(); expect( NEAR_LIMIT_CASE, diff --git a/linux/crates/app/src/ui/export/encoding_sink.rs b/linux/crates/app/src/ui/export/encoding_sink.rs index 947fcb8fa8..bb1b3bb509 100644 --- a/linux/crates/app/src/ui/export/encoding_sink.rs +++ b/linux/crates/app/src/ui/export/encoding_sink.rs @@ -239,7 +239,7 @@ mod tests { assert_eq!(finished, Some(1)); let text: Vec = chunks.into_iter().flat_map(|(bytes, _)| bytes).collect(); - assert_eq!(String::from_utf8(text).expect("utf-8"), "\"x\",\"y\"\n\"1\",\"2\"\n"); + assert_eq!(String::from_utf8(text).expect("utf-8"), "x,y\n1,2\n"); } #[tokio::test(start_paused = true)] diff --git a/linux/crates/app/src/ui/export/export_ledger/tests.rs b/linux/crates/app/src/ui/export/export_ledger/tests.rs index 97e830ca17..63fdf02bb8 100644 --- a/linux/crates/app/src/ui/export/export_ledger/tests.rs +++ b/linux/crates/app/src/ui/export/export_ledger/tests.rs @@ -250,8 +250,8 @@ fn entire_table_export_streams_every_row() { let text = std::fs::read_to_string(&path).expect("the export"); let lines: Vec<&str> = text.lines().collect(); assert_eq!(lines.len(), 25_001); - assert_eq!(lines[0], "\"id\",\"name\""); - assert_eq!(lines[25_000], "\"24999\",\"user 24999\""); + assert_eq!(lines[0], "id,name"); + assert_eq!(lines[25_000], "24999,user 24999"); assert_eq!( fake.connection.observed_deadlines("stream_read"), [None], @@ -298,7 +298,7 @@ fn entire_table_export_has_no_identity_columns() { "{sql}" ); let text = std::fs::read_to_string(&path).expect("the export"); - assert_eq!(text.lines().next(), Some("\"id\",\"name\"")); + assert_eq!(text.lines().next(), Some("id,name")); } #[gtk4::test] @@ -510,11 +510,7 @@ fn snapshot_export_of_10k_rows_keeps_main_loop_responsive() { assert!(ticks.get() > 0, "the main loop never ran while the export did"); let text = std::fs::read_to_string(&path).expect("the export"); assert_eq!(text.lines().count(), 10_001); - assert!( - text.starts_with("\"id\",\"name\",\"note\"\n\"0\",\"name 0\""), - "{}", - &text[..40] - ); + assert!(text.starts_with("id,name,note\n0,name 0"), "{}", &text[..40]); assert_eq!(harness.ledger.in_flight(), 0); assert_eq!(harness.hold.live(), 0, "the export kept the process held"); } diff --git a/linux/crates/platform/tests/settings.rs b/linux/crates/platform/tests/settings.rs index e95371763f..6939de58a3 100644 --- a/linux/crates/platform/tests/settings.rs +++ b/linux/crates/platform/tests/settings.rs @@ -213,7 +213,7 @@ async fn csv_options_round_trip() -> TestResult { null_to_empty: false, line_break_to_space: true, header_row: false, - sanitize_formulas: false, + sanitize_formulas: true, delimiter: CsvDelimiter::Pipe, quote: CsvQuote::Always, line_break: CsvLineBreak::CrLf, diff --git a/linux/data/app.tablepro.TablePro.gschema.xml.in b/linux/data/app.tablepro.TablePro.gschema.xml.in index 2044a4b947..af9c9f021a 100644 --- a/linux/data/app.tablepro.TablePro.gschema.xml.in +++ b/linux/data/app.tablepro.TablePro.gschema.xml.in @@ -324,7 +324,7 @@ - true + false Neutralise spreadsheet formulas Whether a CSV export prefixes values starting with =, +, - or @ so a spreadsheet does not run them. diff --git a/linux/docs/storage.md b/linux/docs/storage.md index f190f695be..ce39a839d1 100644 --- a/linux/docs/storage.md +++ b/linux/docs/storage.md @@ -378,7 +378,7 @@ The schema id is the application id and the path follows it: `app.tablepro.Table | `csv-null-to-empty` | `b` | `true` | | | `csv-line-break-to-space` | `b` | `false` | | | `csv-header-row` | `b` | `true` | | -| `csv-sanitize-formulas` | `b` | `true` | | +| `csv-sanitize-formulas` | `b` | `false` | | | `csv-delimiter` | enum | `comma` | comma, semicolon, tab, pipe | | `csv-quote` | enum | `if-needed` | always, if-needed, never | | `csv-line-break` | enum | `lf` | lf, crlf, cr | From 5b9d13a6df2e055f899b5c0d7d08d67602e7d308 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 14:52:25 +0700 Subject: [PATCH 02/14] docs(macos): add the changelog fragments for #4185 --- apple/changelog.d/4185.fixed.1.md | 1 + apple/changelog.d/4185.fixed.10.md | 1 + apple/changelog.d/4185.fixed.11.md | 1 + apple/changelog.d/4185.fixed.12.md | 1 + apple/changelog.d/4185.fixed.13.md | 1 + apple/changelog.d/4185.fixed.14.md | 1 + apple/changelog.d/4185.fixed.15.md | 1 + apple/changelog.d/4185.fixed.16.md | 1 + apple/changelog.d/4185.fixed.17.md | 1 + apple/changelog.d/4185.fixed.18.md | 1 + apple/changelog.d/4185.fixed.19.md | 1 + apple/changelog.d/4185.fixed.2.md | 1 + apple/changelog.d/4185.fixed.20.md | 1 + apple/changelog.d/4185.fixed.21.md | 1 + apple/changelog.d/4185.fixed.22.md | 1 + apple/changelog.d/4185.fixed.23.md | 1 + apple/changelog.d/4185.fixed.24.md | 1 + apple/changelog.d/4185.fixed.25.md | 1 + apple/changelog.d/4185.fixed.26.md | 1 + apple/changelog.d/4185.fixed.27.md | 1 + apple/changelog.d/4185.fixed.28.md | 1 + apple/changelog.d/4185.fixed.29.md | 1 + apple/changelog.d/4185.fixed.3.md | 1 + apple/changelog.d/4185.fixed.30.md | 1 + apple/changelog.d/4185.fixed.31.md | 1 + apple/changelog.d/4185.fixed.32.md | 1 + apple/changelog.d/4185.fixed.4.md | 1 + apple/changelog.d/4185.fixed.5.md | 1 + apple/changelog.d/4185.fixed.6.md | 1 + apple/changelog.d/4185.fixed.7.md | 1 + apple/changelog.d/4185.fixed.8.md | 1 + apple/changelog.d/4185.fixed.9.md | 1 + apple/changelog.d/4185.fixed.md | 1 + linux/changelog.d/4185.fixed.md | 1 + 34 files changed, 34 insertions(+) create mode 100644 apple/changelog.d/4185.fixed.1.md create mode 100644 apple/changelog.d/4185.fixed.10.md create mode 100644 apple/changelog.d/4185.fixed.11.md create mode 100644 apple/changelog.d/4185.fixed.12.md create mode 100644 apple/changelog.d/4185.fixed.13.md create mode 100644 apple/changelog.d/4185.fixed.14.md create mode 100644 apple/changelog.d/4185.fixed.15.md create mode 100644 apple/changelog.d/4185.fixed.16.md create mode 100644 apple/changelog.d/4185.fixed.17.md create mode 100644 apple/changelog.d/4185.fixed.18.md create mode 100644 apple/changelog.d/4185.fixed.19.md create mode 100644 apple/changelog.d/4185.fixed.2.md create mode 100644 apple/changelog.d/4185.fixed.20.md create mode 100644 apple/changelog.d/4185.fixed.21.md create mode 100644 apple/changelog.d/4185.fixed.22.md create mode 100644 apple/changelog.d/4185.fixed.23.md create mode 100644 apple/changelog.d/4185.fixed.24.md create mode 100644 apple/changelog.d/4185.fixed.25.md create mode 100644 apple/changelog.d/4185.fixed.26.md create mode 100644 apple/changelog.d/4185.fixed.27.md create mode 100644 apple/changelog.d/4185.fixed.28.md create mode 100644 apple/changelog.d/4185.fixed.29.md create mode 100644 apple/changelog.d/4185.fixed.3.md create mode 100644 apple/changelog.d/4185.fixed.30.md create mode 100644 apple/changelog.d/4185.fixed.31.md create mode 100644 apple/changelog.d/4185.fixed.32.md create mode 100644 apple/changelog.d/4185.fixed.4.md create mode 100644 apple/changelog.d/4185.fixed.5.md create mode 100644 apple/changelog.d/4185.fixed.6.md create mode 100644 apple/changelog.d/4185.fixed.7.md create mode 100644 apple/changelog.d/4185.fixed.8.md create mode 100644 apple/changelog.d/4185.fixed.9.md create mode 100644 apple/changelog.d/4185.fixed.md create mode 100644 linux/changelog.d/4185.fixed.md diff --git a/apple/changelog.d/4185.fixed.1.md b/apple/changelog.d/4185.fixed.1.md new file mode 100644 index 0000000000..625f6be5b1 --- /dev/null +++ b/apple/changelog.d/4185.fixed.1.md @@ -0,0 +1 @@ +CSV export with a decimal comma rewrote text values that looked like decimals, such as a version 1.10. diff --git a/apple/changelog.d/4185.fixed.10.md b/apple/changelog.d/4185.fixed.10.md new file mode 100644 index 0000000000..c4812cbb1b --- /dev/null +++ b/apple/changelog.d/4185.fixed.10.md @@ -0,0 +1 @@ +Rewinding a save could fail on a unique key when an earlier edit in it had been undone before saving. diff --git a/apple/changelog.d/4185.fixed.11.md b/apple/changelog.d/4185.fixed.11.md new file mode 100644 index 0000000000..03913855f0 --- /dev/null +++ b/apple/changelog.d/4185.fixed.11.md @@ -0,0 +1 @@ +Rewind reported a row as missing when the save had changed its primary key. diff --git a/apple/changelog.d/4185.fixed.12.md b/apple/changelog.d/4185.fixed.12.md new file mode 100644 index 0000000000..427ed680e8 --- /dev/null +++ b/apple/changelog.d/4185.fixed.12.md @@ -0,0 +1 @@ +A value of __DEFAULT__ was saved as the column default when duplicating a row or typing it into a cell. diff --git a/apple/changelog.d/4185.fixed.13.md b/apple/changelog.d/4185.fixed.13.md new file mode 100644 index 0000000000..f3f1e23763 --- /dev/null +++ b/apple/changelog.d/4185.fixed.13.md @@ -0,0 +1 @@ +A table opened while another query waited for Safe Mode approval stayed empty with its loading indicator. diff --git a/apple/changelog.d/4185.fixed.14.md b/apple/changelog.d/4185.fixed.14.md new file mode 100644 index 0000000000..bfaa1727f7 --- /dev/null +++ b/apple/changelog.d/4185.fixed.14.md @@ -0,0 +1 @@ +Saving while closing a window could close it over unsaved edits in another connection or tab. diff --git a/apple/changelog.d/4185.fixed.15.md b/apple/changelog.d/4185.fixed.15.md new file mode 100644 index 0000000000..157fe467ec --- /dev/null +++ b/apple/changelog.d/4185.fixed.15.md @@ -0,0 +1 @@ +Saving a Cassandra row that another client deleted recreated it with every other column empty. diff --git a/apple/changelog.d/4185.fixed.16.md b/apple/changelog.d/4185.fixed.16.md new file mode 100644 index 0000000000..9b9cdee113 --- /dev/null +++ b/apple/changelog.d/4185.fixed.16.md @@ -0,0 +1 @@ +Running SET with NX or XX in the Redis console showed OK when the key was not written. diff --git a/apple/changelog.d/4185.fixed.17.md b/apple/changelog.d/4185.fixed.17.md new file mode 100644 index 0000000000..27e55c9db4 --- /dev/null +++ b/apple/changelog.d/4185.fixed.17.md @@ -0,0 +1 @@ +Pressing Stop as a MySQL tab closed could crash the app. diff --git a/apple/changelog.d/4185.fixed.18.md b/apple/changelog.d/4185.fixed.18.md new file mode 100644 index 0000000000..18a4d89177 --- /dev/null +++ b/apple/changelog.d/4185.fixed.18.md @@ -0,0 +1 @@ +A startup command that failed, such as SET ROLE, left the session running without it and nothing said so. diff --git a/apple/changelog.d/4185.fixed.19.md b/apple/changelog.d/4185.fixed.19.md new file mode 100644 index 0000000000..e052ba407c --- /dev/null +++ b/apple/changelog.d/4185.fixed.19.md @@ -0,0 +1 @@ +Exporting more than 5,000,000 MongoDB documents wrote the first 5,000,000 and reported success. diff --git a/apple/changelog.d/4185.fixed.2.md b/apple/changelog.d/4185.fixed.2.md new file mode 100644 index 0000000000..51fe78d8f7 --- /dev/null +++ b/apple/changelog.d/4185.fixed.2.md @@ -0,0 +1 @@ +Changing export or import options in another window changed a running export or import, or left foreign keys off. diff --git a/apple/changelog.d/4185.fixed.20.md b/apple/changelog.d/4185.fixed.20.md new file mode 100644 index 0000000000..a4aa8a48a5 --- /dev/null +++ b/apple/changelog.d/4185.fixed.20.md @@ -0,0 +1 @@ +Stopping a Teradata query ended the whole session, so volatile tables were lost and the next query failed. diff --git a/apple/changelog.d/4185.fixed.21.md b/apple/changelog.d/4185.fixed.21.md new file mode 100644 index 0000000000..21106f7c75 --- /dev/null +++ b/apple/changelog.d/4185.fixed.21.md @@ -0,0 +1 @@ +A query parameter such as zip code 00123 was sent to Trino as the number 123. diff --git a/apple/changelog.d/4185.fixed.22.md b/apple/changelog.d/4185.fixed.22.md new file mode 100644 index 0000000000..05b1cf291d --- /dev/null +++ b/apple/changelog.d/4185.fixed.22.md @@ -0,0 +1 @@ +TablePro could quit when the Copilot language server exited while TablePro was sending it a message. diff --git a/apple/changelog.d/4185.fixed.23.md b/apple/changelog.d/4185.fixed.23.md new file mode 100644 index 0000000000..244113e6d1 --- /dev/null +++ b/apple/changelog.d/4185.fixed.23.md @@ -0,0 +1 @@ +Run Current and the gutter cut a SQL Server IF, WHILE or TRY...CATCH block at each semicolon inside it. diff --git a/apple/changelog.d/4185.fixed.24.md b/apple/changelog.d/4185.fixed.24.md new file mode 100644 index 0000000000..d34f707ee7 --- /dev/null +++ b/apple/changelog.d/4185.fixed.24.md @@ -0,0 +1 @@ +Run Current split a PostgreSQL rule with several commands in parentheses at each semicolon. diff --git a/apple/changelog.d/4185.fixed.25.md b/apple/changelog.d/4185.fixed.25.md new file mode 100644 index 0000000000..4ff326bcbd --- /dev/null +++ b/apple/changelog.d/4185.fixed.25.md @@ -0,0 +1 @@ +New Trigger and Edit Trigger opened at 13 pt instead of the size the Structure tab's DDL pane was zoomed to. diff --git a/apple/changelog.d/4185.fixed.26.md b/apple/changelog.d/4185.fixed.26.md new file mode 100644 index 0000000000..65af010304 --- /dev/null +++ b/apple/changelog.d/4185.fixed.26.md @@ -0,0 +1 @@ +Moving the caret in a long MongoDB or Redis query re-read the whole document every time. diff --git a/apple/changelog.d/4185.fixed.27.md b/apple/changelog.d/4185.fixed.27.md new file mode 100644 index 0000000000..f0afd7c7e2 --- /dev/null +++ b/apple/changelog.d/4185.fixed.27.md @@ -0,0 +1 @@ +Column and table suggestions on iPhone and iPad were rebuilt on every keystroke. diff --git a/apple/changelog.d/4185.fixed.28.md b/apple/changelog.d/4185.fixed.28.md new file mode 100644 index 0000000000..27178d8fa0 --- /dev/null +++ b/apple/changelog.d/4185.fixed.28.md @@ -0,0 +1 @@ +The ClickHouse Parts tab hid its list and toolbar after a cancelled or failed partition action. diff --git a/apple/changelog.d/4185.fixed.29.md b/apple/changelog.d/4185.fixed.29.md new file mode 100644 index 0000000000..0ac47956a5 --- /dev/null +++ b/apple/changelog.d/4185.fixed.29.md @@ -0,0 +1 @@ +Dropping or detaching a ClickHouse partition failed on tables partitioned by several columns or not at all. diff --git a/apple/changelog.d/4185.fixed.3.md b/apple/changelog.d/4185.fixed.3.md new file mode 100644 index 0000000000..8fa5c94f07 --- /dev/null +++ b/apple/changelog.d/4185.fixed.3.md @@ -0,0 +1 @@ +Data sync and Copy as SQL wrote PostgreSQL binary values that stored other bytes with standard_conforming_strings off. diff --git a/apple/changelog.d/4185.fixed.30.md b/apple/changelog.d/4185.fixed.30.md new file mode 100644 index 0000000000..6be6044706 --- /dev/null +++ b/apple/changelog.d/4185.fixed.30.md @@ -0,0 +1 @@ +Cancelling the Safe Mode confirmation in an object editor or sheet showed an error. diff --git a/apple/changelog.d/4185.fixed.31.md b/apple/changelog.d/4185.fixed.31.md new file mode 100644 index 0000000000..c6d66b0771 --- /dev/null +++ b/apple/changelog.d/4185.fixed.31.md @@ -0,0 +1 @@ +Applying user changes ran ALTER and password statements for a user staged for drop, and overcounted pending changes. diff --git a/apple/changelog.d/4185.fixed.32.md b/apple/changelog.d/4185.fixed.32.md new file mode 100644 index 0000000000..98e47dea15 --- /dev/null +++ b/apple/changelog.d/4185.fixed.32.md @@ -0,0 +1 @@ +Moving the caret next to a bracket far below the visible text could crash the editor. diff --git a/apple/changelog.d/4185.fixed.4.md b/apple/changelog.d/4185.fixed.4.md new file mode 100644 index 0000000000..a1b1cd6aa7 --- /dev/null +++ b/apple/changelog.d/4185.fixed.4.md @@ -0,0 +1 @@ +Copy as SQL wrote DuckDB and Oracle binary values that DuckDB stored as text and Oracle rejected. diff --git a/apple/changelog.d/4185.fixed.5.md b/apple/changelog.d/4185.fixed.5.md new file mode 100644 index 0000000000..2aea41ab0b --- /dev/null +++ b/apple/changelog.d/4185.fixed.5.md @@ -0,0 +1 @@ +Data sync wrote an empty Oracle BLOB as NULL, which a NOT NULL column refused. diff --git a/apple/changelog.d/4185.fixed.6.md b/apple/changelog.d/4185.fixed.6.md new file mode 100644 index 0000000000..77fcfcfd1f --- /dev/null +++ b/apple/changelog.d/4185.fixed.6.md @@ -0,0 +1 @@ +Data sync and Copy as SQL wrote Redshift binary values in a form Redshift refused. diff --git a/apple/changelog.d/4185.fixed.7.md b/apple/changelog.d/4185.fixed.7.md new file mode 100644 index 0000000000..2b5c350f28 --- /dev/null +++ b/apple/changelog.d/4185.fixed.7.md @@ -0,0 +1 @@ +Add Rows in Shortcuts refused CSV with bracketed headers such as [id], as SQL Server exports them. diff --git a/apple/changelog.d/4185.fixed.8.md b/apple/changelog.d/4185.fixed.8.md new file mode 100644 index 0000000000..88dc60ec1c --- /dev/null +++ b/apple/changelog.d/4185.fixed.8.md @@ -0,0 +1 @@ +Add Rows in Shortcuts refused a header that differed from its column's name only in case. diff --git a/apple/changelog.d/4185.fixed.9.md b/apple/changelog.d/4185.fixed.9.md new file mode 100644 index 0000000000..081e98b9ba --- /dev/null +++ b/apple/changelog.d/4185.fixed.9.md @@ -0,0 +1 @@ +Copy to another database and Data Compare missed rows past a database's row limit without saying so. diff --git a/apple/changelog.d/4185.fixed.md b/apple/changelog.d/4185.fixed.md new file mode 100644 index 0000000000..a90c14e4ae --- /dev/null +++ b/apple/changelog.d/4185.fixed.md @@ -0,0 +1 @@ +CSV export put a single quote before values starting with =, +, - or @ by default, which then stayed in imports. diff --git a/linux/changelog.d/4185.fixed.md b/linux/changelog.d/4185.fixed.md new file mode 100644 index 0000000000..a90c14e4ae --- /dev/null +++ b/linux/changelog.d/4185.fixed.md @@ -0,0 +1 @@ +CSV export put a single quote before values starting with =, +, - or @ by default, which then stayed in imports. From 4bd9bc7dc60094b452cd050f80a2a5d907a6ae24 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 17:26:04 +0700 Subject: [PATCH 03/14] build(pluginkit): raise the kit to 44 for the additive API --- apple/Configs/PluginKit.xcconfig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apple/Configs/PluginKit.xcconfig b/apple/Configs/PluginKit.xcconfig index fef1b5c3ce..207ba58516 100644 --- a/apple/Configs/PluginKit.xcconfig +++ b/apple/Configs/PluginKit.xcconfig @@ -1,3 +1,3 @@ // When to raise each, and how the build stamps them: .claude/rules/plugin-system.md, ABI. -TABLEPRO_PLUGINKIT_VERSION = 43 +TABLEPRO_PLUGINKIT_VERSION = 44 TABLEPRO_PLUGINKIT_MINIMUM_VERSION = 19 From 8a8ba11be6e3864f71e79d66731a469b5befdca8 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 21:22:56 +0700 Subject: [PATCH 04/14] feat(macos): add manual commit, table auto refresh, query tab history, server object kinds and composite ER keys --- .claude/rules/data-sync-storage.md | 1 + .../ConnectionFieldTrust.swift | 1 + .../MySQLObjectQueries.swift | 17 + .../PostgreSQLCapabilities.swift | 2 + .../TableProSQLGrammar/SQLCatalogEffect.swift | 6 +- .../MySQLObjectQueryTests.swift | 42 ++ .../PluginServerObjectDefaultsTests.swift | 97 ++++ .../SQLCatalogEffectClassifierTests.swift | 11 + .../TextViewController+UnfilteredEdits.swift | 15 + .../ReplaceTextAsOneEditTests.swift | 49 ++ .../KingbaseESPluginDriver.swift | 3 + .../MySQLPluginDriver+ServerObjects.swift | 143 ++++++ .../MySQLEventServerObjectTests.swift | 144 ++++++ .../PostgreSQLExtensionQueries.swift | 97 ++++ .../PostgreSQLPluginDriver+Extensions.swift | 38 ++ .../PostgreSQLPluginDriver.swift | 5 + .../PostgreSQLExtensionQueryTests.swift | 123 +++++ .../PluginDatabaseDriver.swift | 19 + .../PluginServerObject.swift | 68 +++ .../QueryExecutionCoordinator+Batches.swift | 16 +- .../QueryExecutionCoordinator+Helpers.swift | 60 ++- ...QueryExecutionCoordinator+Parameters.swift | 37 +- .../QueryExecutionCoordinator.swift | 26 +- .../RowEditingCoordinator+Discard.swift | 3 +- .../RowEditingCoordinator+SaveChanges.swift | 26 +- .../Core/DataGrid/DataGridModel.swift | 17 + .../Engine/GridSelectionSnapshot.swift | 29 ++ .../Core/DataGrid/GridViewportResolver.swift | 91 +++- .../Core/DataWrite/DataWriteExecutor.swift | 31 +- .../DataWrite/Rewind/RewindExecutor.swift | 3 +- .../Access/AgentTransactionGuard.swift | 128 ++++++ .../Access/DatabaseAccessBridge+Scripts.swift | 32 +- .../Access/DatabaseAccessBridge.swift | 45 +- .../Core/Database/DatabaseDriver.swift | 16 + .../Database/DatabaseManager+Metadata.swift | 34 +- .../DatabaseManager+OpenTransactions.swift | 13 +- .../Database/DatabaseManager+Principals.swift | 11 +- .../Database/DatabaseManager+Schema.swift | 15 +- .../DatabaseManager+SchemaChangeHold.swift | 40 ++ .../DatabaseManager+ScopedDriver.swift | 32 +- .../Database/DatabaseManager+Sessions.swift | 18 + .../Database/SessionTransactionTracker.swift | 83 +++- .../Core/Database/TriggerEditing.swift | 26 +- apple/TablePro/Core/Events/AppEvents.swift | 71 +-- .../Core/Events/SucceededStatements.swift | 11 +- .../ConfirmDestructiveOperationTool.swift | 3 +- .../MCP/Protocol/Tools/ExecuteQueryTool.swift | 6 +- .../MCP/Protocol/Tools/MaintenanceTools.swift | 4 +- .../Core/Menu/DatabaseMenuBuilder.swift | 46 +- .../TablePro/Core/Menu/FileMenuBuilder.swift | 46 +- .../Menu/QueryTabRevertMenuDelegate.swift | 67 +++ .../Menu/TableAutoRefreshMenuDelegate.swift | 38 ++ .../Core/ObjectCopy/ObjectCopyRunner.swift | 8 +- .../Core/Plugins/ImportDataSinkAdapter.swift | 34 +- .../TablePro/Core/Plugins/ImportSinkRun.swift | 85 +++- .../PluginDriverAdapter+ServerObjects.swift | 36 ++ .../StructureChangeManager.swift | 7 + .../Execution/BatchStatementRun.swift | 24 +- .../Execution/BatchTransactionPlan.swift | 35 +- .../Services/Execution/ManualCommit.swift | 384 ++++++++++++++++ .../Execution/MultiStatementFailure.swift | 2 +- .../Execution/TransactionEngineFamily.swift | 15 + .../Execution/WriteTransactionOwner.swift | 36 +- .../Core/Services/Export/ImportService.swift | 9 +- .../MainSplitViewController+AutoRefresh.swift | 24 + ...itViewController+DatabaseMenuActions.swift | 54 +++ ...inSplitViewController+MenuValidation.swift | 64 ++- ...SplitViewController+QueryTabVersions.swift | 23 + .../Infrastructure/QuitApproval.swift | 46 +- ...aseTreeMetadataService+CatalogChange.swift | 15 +- .../Query/DatabaseTreeMetadataService.swift | 134 +++++- .../Query/SchemaService+ServerObjects.swift | 60 +++ .../Core/Services/Query/SchemaService.swift | 140 +++++- .../Core/Shortcuts/ShortcutsRowWriter.swift | 3 +- .../Core/Storage/ConnectionLocalState.swift | 45 +- .../Core/Storage/QueryTabVersionStorage.swift | 433 ++++++++++++++++++ .../LinkedFileVersionHistoryProvider.swift | 2 +- .../QueryTabVersionHistoryProvider.swift | 85 ++++ .../QueryTabVersionRecorder.swift | 28 ++ .../SavedQueryVersionHistoryProvider.swift | 2 +- .../VersionHistory/VersionHistoryModels.swift | 6 + .../Connection/ConnectionCommitMode.swift | 11 + .../Models/Connection/ConnectionSession.swift | 2 + .../Connection/DatabaseConnection.swift | 14 + .../Models/Query/DatabaseObjectRef.swift | 100 +++- apple/TablePro/Models/Query/QueryTab.swift | 5 + .../Models/Query/QueryTabManager.swift | 7 +- .../Models/Query/ResultStatusModel.swift | 3 + .../Models/Query/ServerObjectInfo.swift | 90 ++++ .../Models/Query/TableAutoRefreshGate.swift | 96 ++++ .../Query/TableAutoRefreshInterval.swift | 38 ++ .../Models/Query/TableLoadTrigger.swift | 25 + .../ServerDashboardModels.swift | 2 +- .../Models/Sidebar/SidebarObjectKind.swift | 72 ++- .../Sidebar/SidebarServerObjectKind.swift | 83 ++++ .../Models/UI/KeyboardShortcutModels.swift | 9 +- .../Models/UI/WindowSidebarState.swift | 7 +- .../ERDiagramViewModel+Editing.swift | 178 ++++++- .../ViewModels/SidebarViewModel.swift | 35 +- .../ViewModels/VersionHistoryViewModel.swift | 3 +- .../ConnectionFormCoordinator.swift | 1 + .../Panes/OptionsPaneView.swift | 9 + .../Support/ConnectionFormEdits.swift | 3 + .../CustomizationPaneViewModel.swift | 2 + .../DataGridViewController+Changes.swift | 4 +- .../DataGridViewController+Selection.swift | 2 + ...DataGridViewController+TableDelegate.swift | 15 +- .../DataGrid/DataGridViewController.swift | 23 +- .../Views/ERDiagram/ERDiagramEditSheets.swift | 170 ++++++- .../Views/ERDiagram/ERDiagramView.swift | 2 +- .../Views/Editor/EditorEventRouter.swift | 42 +- .../Child/ConnectionTransactionActions.swift | 90 ++++ .../Main/Child/ConnectionTransactionBar.swift | 67 +-- .../Main/Child/MainEditorContentView.swift | 13 +- ...ainContentCoordinator+AIQueryActions.swift | 9 +- .../MainContentCoordinator+AutoRefresh.swift | 170 +++++++ .../MainContentCoordinator+DraftSave.swift | 57 +++ ...ontentCoordinator+EditorQueryLoading.swift | 99 ++++ .../MainContentCoordinator+Explain.swift | 19 +- .../MainContentCoordinator+Favorites.swift | 17 +- .../MainContentCoordinator+GridViewport.swift | 6 +- .../MainContentCoordinator+ManualCommit.swift | 18 + .../MainContentCoordinator+Phase2Launch.swift | 41 ++ .../MainContentCoordinator+QueryHelpers.swift | 20 +- .../MainContentCoordinator+Refresh.swift | 4 +- ...ainContentCoordinator+VersionHistory.swift | 52 +++ .../Main/MainContentCommandActions.swift | 22 +- .../Views/Main/MainContentCoordinator.swift | 156 ++----- .../TablePro/Views/Main/MainContentView.swift | 3 +- .../ObjectSource/ObjectSourceTabView.swift | 18 +- .../Views/Results/ResultStatusBar.swift | 50 ++ .../Views/Results/ResultStatusInputs.swift | 15 + .../Results/TextViewerWindowController.swift | 4 +- .../InspectorPopOutRegistry.swift | 32 ++ .../Views/RowInspector/RowInspectorView.swift | 6 +- .../Settings/Sections/HistorySection.swift | 7 +- .../Views/Sidebar/DatabaseTreeCellView.swift | 2 + .../DatabaseTreeDoubleClickIntent.swift | 6 +- .../Views/Sidebar/DatabaseTreeFilter.swift | 44 +- .../Views/Sidebar/DatabaseTreeNode.swift | 63 +-- ...baseTreeOutlineCoordinator+Expansion.swift | 25 +- ...seTreeOutlineCoordinator+FolderNodes.swift | 2 +- .../DatabaseTreeOutlineCoordinator+Menu.swift | 2 +- ...DatabaseTreeOutlineCoordinator+Nodes.swift | 136 +++++- .../DatabaseTreeOutlineCoordinator.swift | 31 +- .../Sidebar/DatabaseTreeRenderInputs.swift | 16 + .../Views/Sidebar/DatabaseTreeRowView.swift | 2 + .../Views/Sidebar/DatabaseTreeSelection.swift | 2 +- .../Sidebar/DatabaseTreeTypeSelect.swift | 2 + .../Views/Sidebar/DatabaseTreeView.swift | 15 + .../Sidebar/Menu/DatabaseTreeMenuSpec.swift | 17 +- .../Views/Sidebar/ServerObjectRowView.swift | 79 ++++ .../Views/Sidebar/SidebarPersistenceKey.swift | 16 +- .../TablePro/Views/Sidebar/SidebarView.swift | 9 +- .../StructureRebuildPlanRunner.swift | 12 +- .../VersionHistoryFactory.swift | 9 + .../VersionHistoryFormatting.swift | 13 + .../DataGrid/GridViewportResolverTests.swift | 80 +++- .../DataWriteExecutorLookupTests.swift | 2 +- .../DataWrite/DataWriteExecutorTests.swift | 206 +++++++-- .../DatabaseAccessBridgeScriptTests.swift | 5 +- .../SchemaChangeHeldTransactionTests.swift | 162 +++++++ .../Core/Database/SessionLanesTests.swift | 26 ++ .../SessionTransactionTrackerTests.swift | 344 +++++++++++++- .../Core/Menu/ManualCommitMenuTests.swift | 89 ++++ .../Core/Menu/QueryTabRevertMenuTests.swift | 89 ++++ .../ObjectCopyTransactionTests.swift | 17 + .../ConnectionFieldTrustCoverageTests.swift | 1 + .../Plugins/ImportManualCommitTests.swift | 179 ++++++++ .../StructureChangeValidationTests.swift | 19 + .../Execution/BatchStatementRunTests.swift | 49 +- .../Execution/BatchTransactionPlanTests.swift | 57 ++- .../Execution/ManualCommitTests.swift | 264 +++++++++++ .../MultiStatementFailureTests.swift | 4 +- .../QuitTransactionPromptTests.swift | 56 +++ .../DatabaseTreeCatalogRefreshPlanTests.swift | 70 +++ .../ConnectionLocalStatePurgeTests.swift | 25 + .../Storage/QueryTabVersionStorageTests.swift | 255 +++++++++++ .../QueryTabVersionHistoryProviderTests.swift | 158 +++++++ .../QueryTabVersionSubjectTests.swift | 66 +++ .../Models/DatabaseObjectRefCodingTests.swift | 98 ++++ .../ERDiagramStagedOverlayTests.swift | 20 + .../Query/TableAutoRefreshGateTests.swift | 83 ++++ .../Query/TableAutoRefreshIntervalTests.swift | 47 ++ .../Models/ResultStatusModelTests.swift | 10 + .../Sidebar/SidebarObjectKindTests.swift | 78 +++- .../SchemaServiceSideObjectsTests.swift | 104 +++++ .../ERDiagramForeignKeyDraftTests.swift | 133 ++++++ .../ERDiagramSchemaEditsTests.swift | 28 ++ .../ERDiagramViewModelForeignKeyTests.swift | 73 +++ .../ViewModels/SidebarViewModelTests.swift | 50 ++ .../VersionHistoryViewModelTests.swift | 13 + .../ViewModels/WindowSidebarStateTests.swift | 35 ++ .../DataGridContextMenuTransientTests.swift | 43 ++ .../DataGrid/DataGridViewportTests.swift | 20 + .../Views/Main/GridReloadViewportTests.swift | 21 + ...inContentCoordinatorAutoRefreshTests.swift | 406 ++++++++++++++++ .../Main/QueryTabVersionTriggerTests.swift | 165 +++++++ .../Main/ResultStatusBarLayoutTests.swift | 47 +- .../ObjectSourceLoaderTests.swift | 64 +++ .../Sidebar/DatabaseTreeFilterTests.swift | 62 ++- .../Sidebar/DatabaseTreeFlatRootTests.swift | 94 +++- .../Sidebar/DatabaseTreeMenuSpecTests.swift | 49 ++ .../Views/Sidebar/DatabaseTreeNodeTests.swift | 83 ++++ .../Sidebar/ServerObjectRowLogicTests.swift | 67 +++ .../TableProUITests/ManualCommitUITests.swift | 28 ++ .../TableAutoRefreshUITests.swift | 26 ++ 207 files changed, 9742 insertions(+), 823 deletions(-) create mode 100644 apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginServerObjectDefaultsTests.swift create mode 100644 apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/ReplaceTextAsOneEditTests.swift create mode 100644 apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver+ServerObjects.swift create mode 100644 apple/Plugins/MySQLDriverTests/MySQLEventServerObjectTests.swift create mode 100644 apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLExtensionQueries.swift create mode 100644 apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Extensions.swift create mode 100644 apple/Plugins/PostgreSQLDriverTests/PostgreSQLExtensionQueryTests.swift create mode 100644 apple/Plugins/TableProPluginKit/PluginServerObject.swift create mode 100644 apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift create mode 100644 apple/TablePro/Core/Database/DatabaseManager+SchemaChangeHold.swift create mode 100644 apple/TablePro/Core/Menu/QueryTabRevertMenuDelegate.swift create mode 100644 apple/TablePro/Core/Menu/TableAutoRefreshMenuDelegate.swift create mode 100644 apple/TablePro/Core/Plugins/PluginDriverAdapter+ServerObjects.swift create mode 100644 apple/TablePro/Core/Services/Execution/ManualCommit.swift create mode 100644 apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+AutoRefresh.swift create mode 100644 apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+QueryTabVersions.swift create mode 100644 apple/TablePro/Core/Services/Query/SchemaService+ServerObjects.swift create mode 100644 apple/TablePro/Core/Storage/QueryTabVersionStorage.swift create mode 100644 apple/TablePro/Core/VersionHistory/QueryTabVersionHistoryProvider.swift create mode 100644 apple/TablePro/Core/VersionHistory/QueryTabVersionRecorder.swift create mode 100644 apple/TablePro/Models/Connection/ConnectionCommitMode.swift create mode 100644 apple/TablePro/Models/Query/ServerObjectInfo.swift create mode 100644 apple/TablePro/Models/Query/TableAutoRefreshGate.swift create mode 100644 apple/TablePro/Models/Query/TableAutoRefreshInterval.swift create mode 100644 apple/TablePro/Models/Sidebar/SidebarServerObjectKind.swift create mode 100644 apple/TablePro/Views/Main/Child/ConnectionTransactionActions.swift create mode 100644 apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AutoRefresh.swift create mode 100644 apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DraftSave.swift create mode 100644 apple/TablePro/Views/Main/Extensions/MainContentCoordinator+EditorQueryLoading.swift create mode 100644 apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ManualCommit.swift create mode 100644 apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Phase2Launch.swift create mode 100644 apple/TablePro/Views/RowInspector/InspectorPopOutRegistry.swift create mode 100644 apple/TablePro/Views/Sidebar/ServerObjectRowView.swift create mode 100644 apple/TableProTests/Core/Database/SchemaChangeHeldTransactionTests.swift create mode 100644 apple/TableProTests/Core/Menu/ManualCommitMenuTests.swift create mode 100644 apple/TableProTests/Core/Menu/QueryTabRevertMenuTests.swift create mode 100644 apple/TableProTests/Core/Plugins/ImportManualCommitTests.swift create mode 100644 apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift create mode 100644 apple/TableProTests/Core/Services/Infrastructure/QuitTransactionPromptTests.swift create mode 100644 apple/TableProTests/Core/Storage/QueryTabVersionStorageTests.swift create mode 100644 apple/TableProTests/Core/VersionHistory/QueryTabVersionHistoryProviderTests.swift create mode 100644 apple/TableProTests/Core/VersionHistory/QueryTabVersionSubjectTests.swift create mode 100644 apple/TableProTests/Models/Query/TableAutoRefreshGateTests.swift create mode 100644 apple/TableProTests/Models/Query/TableAutoRefreshIntervalTests.swift create mode 100644 apple/TableProTests/ViewModels/ERDiagramForeignKeyDraftTests.swift create mode 100644 apple/TableProTests/ViewModels/ERDiagramViewModelForeignKeyTests.swift create mode 100644 apple/TableProTests/Views/DataGrid/DataGridContextMenuTransientTests.swift create mode 100644 apple/TableProTests/Views/Main/MainContentCoordinatorAutoRefreshTests.swift create mode 100644 apple/TableProTests/Views/Main/QueryTabVersionTriggerTests.swift create mode 100644 apple/TableProTests/Views/Sidebar/ServerObjectRowLogicTests.swift create mode 100644 apple/TableProUITests/ManualCommitUITests.swift create mode 100644 apple/TableProUITests/TableAutoRefreshUITests.swift diff --git a/.claude/rules/data-sync-storage.md b/.claude/rules/data-sync-storage.md index 49b33f7ff5..7fae1e0eb8 100644 --- a/.claude/rules/data-sync-storage.md +++ b/.claude/rules/data-sync-storage.md @@ -159,6 +159,7 @@ This path holds user data. A silent wrong answer here looks exactly like an empt | Light and dark theme ids (iCloud-synced, category `appearance`) | UserDefaults | `AppearanceSettings` through `AppSettingsManager` | | User themes (iCloud-synced, one `AppSettings` record `theme.` each) | JSON, one file per theme | `ThemeCatalog` (sync rules: `.claude/rules/theme.md`, Sync) | | Query history | SQLite FTS5 | `QueryHistoryStorage` | +| Query tab edit history: up to 50 versions per tab, kept 30 days or the shorter Query History window, after the tab closes too; purged with the connection and by Clear History | SQLite `query_tab_versions.db`, device-local, never synced | `QueryTabVersionStorage` | | Tab state | JSON | `TabPersistenceCoordinator`, `TabDiskActor` | | Filter defaults and presets | UserDefaults | `FilterSettingsStorage`, `FilterPresetStorage` | | Per-table filters | JSON, one file per table | `FilterSettingsStorage` | diff --git a/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/ConnectionFieldTrust.swift b/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/ConnectionFieldTrust.swift index b99565d3d9..dbb106f145 100644 --- a/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/ConnectionFieldTrust.swift +++ b/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/ConnectionFieldTrust.swift @@ -130,6 +130,7 @@ public enum ConnectionFieldCatalog { "bqMaxBytesBilled": .setting, "cassandraConsistency": .setting, "cassandraSerialConsistency": .setting, + "commitMode": .setting, "completionIdentifierCase": .setting, "connectionOptions": .setting, "connectTimeoutSeconds": .setting, diff --git a/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLObjectQueries.swift b/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLObjectQueries.swift index 5932916173..b174813116 100644 --- a/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLObjectQueries.swift +++ b/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLObjectQueries.swift @@ -256,6 +256,23 @@ public enum MySQLObjectQueries { """ } + // The sidebar's read, apart from export's `eventList`, which keeps the four columns every server answers. + // A name narrows it to the one event a source tab reads again. + public static func eventDetails(schema: String, name: String? = nil, exactSchema: Bool) -> String { + let namePredicate = name.map { "\n AND EVENT_NAME = '\(escapeLiteral($0))'" } ?? "" + return """ + SELECT EVENT_NAME, EVENT_TYPE, STATUS, EVENT_SCHEMA, + EXECUTE_AT, INTERVAL_VALUE, INTERVAL_FIELD, STARTS, ENDS, + ON_COMPLETION, LAST_EXECUTED, DEFINER, EVENT_COMMENT + FROM information_schema.EVENTS + WHERE \(schemaMatch("EVENT_SCHEMA", schema, exact: exactSchema))\(namePredicate) + ORDER BY EVENT_NAME + """ + } + + // Its own statement, so a server without the variable loses that one attribute rather than the list. + public static let eventScheduler = "SELECT @@global.event_scheduler" + /// Qualified with the schema. Unqualified, the server resolves the name against the session /// database instead of the one being browsed, and returns a different routine's body or none. public static func routineDefinition(kind: String, schema: String?, name: String) -> String { diff --git a/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCapabilities.swift b/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCapabilities.swift index 5e38c4ccb6..8fd987c88d 100644 --- a/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCapabilities.swift +++ b/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCapabilities.swift @@ -30,6 +30,8 @@ nonisolated public struct PostgreSQLCapabilities: Sendable, Equatable { /// Every range gained a companion multirange in 14, with a name the creator may choose. public var hasMultirangeTypes: Bool { serverVersion >= 140_000 } public var hasForeignTablesCatalog: Bool { serverVersion >= 90_100 } + // `pg_extension` and CREATE EXTENSION landed in 9.1. + public var hasExtensionsCatalog: Bool { serverVersion >= 90_100 } public var hasSequencesCatalog: Bool { serverVersion >= 100_000 } public var hasBypassRLS: Bool { serverVersion >= 90_500 } public var hasProcedureKind: Bool { serverVersion >= 110_000 } diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift index 2e44afde28..5b233db5ff 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift @@ -13,8 +13,10 @@ public struct SQLCatalogObjectKinds: OptionSet, Sendable, Hashable { public static let types = SQLCatalogObjectKinds(rawValue: 1 << 3) public static let schemas = SQLCatalogObjectKinds(rawValue: 1 << 4) public static let databases = SQLCatalogObjectKinds(rawValue: 1 << 5) + // The kinds a driver declares for the sidebar, such as MySQL events and PostgreSQL extensions. + public static let serverObjects = SQLCatalogObjectKinds(rawValue: 1 << 6) - public static let objects: SQLCatalogObjectKinds = [.tables, .routines, .triggers, .types] + public static let objects: SQLCatalogObjectKinds = [.tables, .routines, .triggers, .types, .serverObjects] public static let everything: SQLCatalogObjectKinds = [.objects, .schemas, .databases] } @@ -200,7 +202,7 @@ public enum SQLCatalogEffectClassifier { "SYNONYM": .tables, "FIELD": .tables, "FUNCTION": .routines, "PROCEDURE": .routines, "PROC": .routines, "ROUTINE": .routines, "AGGREGATE": .routines, "MACRO": .routines, "PACKAGE": .routines, "OPERATOR": .routines, - "TRIGGER": .triggers, "EVENT": .triggers, + "TRIGGER": .triggers, "EVENT": [.triggers, .serverObjects], "TYPE": .types, "DOMAIN": .types, "ENUM": .types, "SCHEMA": [.schemas, .objects], "NAMESPACE": [.schemas, .objects], "OWNED": [.schemas, .objects], "DATABASE": .everything, "KEYSPACE": .everything, "CATALOG": .everything, diff --git a/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLObjectQueryTests.swift b/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLObjectQueryTests.swift index 8775894a00..475c977295 100644 --- a/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLObjectQueryTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLObjectQueryTests.swift @@ -303,6 +303,48 @@ struct MySQLObjectQueryTests { #expect(sql.hasSuffix("ORDER BY EVENT_NAME")) } + private func selectedColumns(_ sql: String) throws -> [String] { + let select = try #require(sql.components(separatedBy: "FROM").first) + return select + .replacingOccurrences(of: "SELECT", with: "") + .split(separator: ",") + .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } + } + + // Export reads these four by position on every flavor, including ones the sidebar leaves out. + @Test("Export's event list keeps the four columns it shipped with") + func eventListKeepsItsShippedProjection() throws { + let sql = MySQLObjectQueries.eventList(schema: "app", exactSchema: false) + #expect(try selectedColumns(sql) == ["EVENT_NAME", "EVENT_TYPE", "STATUS", "EVENT_SCHEMA"]) + #expect(!sql.contains("@@")) + } + + @Test("The sidebar's event read starts with export's columns and appends the schedule columns in order") + func eventDetailsAppendsScheduleColumns() throws { + let sql = MySQLObjectQueries.eventDetails(schema: "app", exactSchema: true) + #expect(try selectedColumns(sql) == [ + "EVENT_NAME", "EVENT_TYPE", "STATUS", "EVENT_SCHEMA", + "EXECUTE_AT", "INTERVAL_VALUE", "INTERVAL_FIELD", "STARTS", "ENDS", + "ON_COMPLETION", "LAST_EXECUTED", "DEFINER", "EVENT_COMMENT", + ]) + #expect(sql.contains("EVENT_SCHEMA = 'app' AND CAST(EVENT_SCHEMA AS BINARY) = CAST('app' AS BINARY)")) + #expect(!sql.contains("EVENT_NAME =")) + #expect(!sql.contains("@@")) + #expect(sql.hasSuffix("ORDER BY EVENT_NAME")) + } + + @Test("A re-read of one event names it as an escaped literal") + func eventDetailsForOneEvent() { + let sql = MySQLObjectQueries.eventDetails(schema: "app", name: "it's", exactSchema: false) + #expect(sql.contains("AND EVENT_NAME = 'it''s'")) + #expect(sql.contains("EVENT_SCHEMA = 'app'")) + } + + @Test("The event scheduler is read in a statement of its own") + func eventSchedulerIsItsOwnStatement() { + #expect(MySQLObjectQueries.eventScheduler == "SELECT @@global.event_scheduler") + } + @Test("A routine's recorded mode is read by schema, name and type, with the name selected for an exact match") func routineCreationSettings() { let sql = MySQLObjectQueries.routineCreationSettings( diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginServerObjectDefaultsTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginServerObjectDefaultsTests.swift new file mode 100644 index 0000000000..9be7b2e5b9 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginServerObjectDefaultsTests.swift @@ -0,0 +1,97 @@ +import Foundation +import TableProPluginKit +import Testing + +private final class StubServerObjectDriver: PluginDatabaseDriver, @unchecked Sendable { + var supportsSchemas: Bool { false } + var supportsTransactions: Bool { false } + var currentSchema: String? { nil } + var serverVersion: String? { nil } + + func connect() async throws {} + func disconnect() {} + func ping() async throws {} + func execute(query: String) async throws -> PluginQueryResult { + PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) + } + + func fetchTables(schema: String?) async throws -> [PluginTableInfo] { [] } + func fetchColumns(table: String, schema: String?) async throws -> [PluginColumnInfo] { [] } + func fetchIndexes(table: String, schema: String?) async throws -> [PluginIndexInfo] { [] } + func fetchForeignKeys(table: String, schema: String?) async throws -> [PluginForeignKeyInfo] { [] } + func fetchTableDDL(table: String, schema: String?) async throws -> String { "" } + func fetchViewDefinition(view: String, schema: String?) async throws -> String { "" } + func fetchTableMetadata(table: String, schema: String?) async throws -> PluginTableMetadata { + PluginTableMetadata(tableName: table) + } + + func fetchDatabases() async throws -> [String] { [] } + func fetchDatabaseMetadata(_ database: String) async throws -> PluginDatabaseMetadata { + PluginDatabaseMetadata(name: database) + } +} + +struct PluginServerObjectDefaultsTests { + @Test("A driver that implements nothing declares no kinds and lists no objects") + func defaultsDeclareNothing() async throws { + let driver = StubServerObjectDriver() + #expect(driver.serverObjectKinds.isEmpty) + #expect(try await driver.fetchServerObjects(ofKind: "event", schema: "app").isEmpty) + #expect(try await driver.fetchServerObjects(ofKind: "extension", schema: nil).isEmpty) + } + + @Test("Reading an object again returns it when the listing carried its definition") + func rereadKeepsListedDefinition() async throws { + let listed = PluginServerObjectInfo(kindId: "event", name: "nightly", definition: "CREATE EVENT nightly") + let read = try await StubServerObjectDriver().fetchServerObject(listed) + #expect(read.name == "nightly") + #expect(read.definition == "CREATE EVENT nightly") + } + + @Test("Reading an object again without a definition is unsupported, not missing") + func rereadWithoutDefinitionIsUnsupported() async { + let listed = PluginServerObjectInfo(kindId: "event", name: "nightly", definition: "") + var refusal: Error? + do { + _ = try await StubServerObjectDriver().fetchServerObject(listed) + } catch { + refusal = error + } + guard let sourceError = refusal as? PluginObjectSourceError, case .unsupported(let name) = sourceError else { + Issue.record("expected unsupported, got \(String(describing: refusal))") + return + } + #expect(name == "nightly") + } + + @Test("A kind is listed per container unless it says otherwise") + func kindScopeDefaultsToContainer() { + let kind = PluginServerObjectKind(id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar") + #expect(kind.scope == .container) + } + + @Test("Kinds and objects round-trip through Codable") + func codableRoundTrip() throws { + let kind = PluginServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", + iconName: "puzzlepiece.extension", scope: .database + ) + let decodedKind = try JSONDecoder().decode(PluginServerObjectKind.self, from: JSONEncoder().encode(kind)) + #expect(decodedKind == kind) + + let object = PluginServerObjectInfo( + kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384", detail: "1.3", + isEnabled: true, definition: "CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;", + attributes: [PluginObjectAttribute(label: "Version", value: "1.3")] + ) + let decoded = try JSONDecoder().decode(PluginServerObjectInfo.self, from: JSONEncoder().encode(object)) + #expect(decoded.kindId == object.kindId) + #expect(decoded.name == object.name) + #expect(decoded.schema == object.schema) + #expect(decoded.identity == object.identity) + #expect(decoded.detail == object.detail) + #expect(decoded.isEnabled == object.isEnabled) + #expect(decoded.definition == object.definition) + #expect(decoded.attributes == object.attributes) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift index 1e65ebbaf7..25e49d5127 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift @@ -21,6 +21,17 @@ struct SQLCatalogEffectClassifierTests { #expect(kinds("CREATE TABLE copy AS SELECT * FROM t") == .tables) } + @Test("An event changes the triggers and the server objects, an extension reaches the server objects") + func serverObjects() { + #expect(kinds("CREATE EVENT nightly ON SCHEDULE EVERY 1 DAY DO DELETE FROM log", "MySQL") == [.triggers, .serverObjects]) + #expect(kinds("ALTER EVENT nightly DISABLE", "MySQL") == [.triggers, .serverObjects]) + #expect(kinds("DROP EVENT IF EXISTS nightly", "MySQL") == [.triggers, .serverObjects]) + #expect(kinds("CREATE EXTENSION IF NOT EXISTS pgcrypto").contains(.serverObjects)) + #expect(kinds("DROP EXTENSION pgcrypto").contains(.serverObjects)) + #expect(SQLCatalogObjectKinds.objects.contains(.serverObjects)) + #expect(!kinds("CREATE TABLE t (id int)").contains(.serverObjects)) + } + @Test("Row work and reads leave the catalog alone, except SELECT INTO") func rowsAndReads() { #expect(kinds("INSERT INTO t VALUES (1)").isEmpty) diff --git a/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+UnfilteredEdits.swift b/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+UnfilteredEdits.swift index 933c02074b..0b13653b05 100644 --- a/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+UnfilteredEdits.swift +++ b/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+UnfilteredEdits.swift @@ -26,4 +26,19 @@ public extension TextViewController { defer { isApplyingUnfilteredEdits = false } textView.replaceCharacters(in: span, with: rebuilt, skipUpdateSelection: true) } + + // A command's replacement, unlike `setText`, keeps the undo history: one undo group, no typing filters, no + // suggestion triggers. The binding gets the text before this returns, so a run right after reads it. + @discardableResult + func replaceTextAsOneEdit(in range: NSRange, with string: String) -> Bool { + guard let textView, textView.isEditable, NSMaxRange(range) <= textView.textStorage.length else { return false } + guard !range.isEmpty || !string.isEmpty else { return true } + textView.undoManager?.beginUndoGrouping() + isApplyingUnfilteredEdits = true + textView.replaceCharacters(in: range, with: string) + isApplyingUnfilteredEdits = false + textView.undoManager?.endUndoGrouping() + flushPendingTextWriteback() + return true + } } diff --git a/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/ReplaceTextAsOneEditTests.swift b/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/ReplaceTextAsOneEditTests.swift new file mode 100644 index 0000000000..54718ec320 --- /dev/null +++ b/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/ReplaceTextAsOneEditTests.swift @@ -0,0 +1,49 @@ +import AppKit +@testable import TableProEditorKit +import TableProTextEngine +import Testing + +@MainActor +struct ReplaceTextAsOneEditTests { + @Test("A command's replacement keeps the undo history and undoes in one step") + func replacementIsUndoable() { + let (window, controller) = Mock.focusedTextViewController(string: "SELECT 1") + defer { window.orderOut(nil) } + let whole = NSRange(location: 0, length: controller.textView.textStorage.length) + + #expect(controller.replaceTextAsOneEdit(in: whole, with: "SELECT * FROM users")) + #expect(controller.textView.string == "SELECT * FROM users") + #expect(controller.textView.undoManager?.canUndo == true) + + controller.textView.undoManager?.undo() + #expect(controller.textView.string == "SELECT 1") + } + + @Test("A command's replacement skips the typing filters a typed edit runs") + func replacementSkipsTypingFilters() { + let typed = Mock.loadedTextViewController(string: "") + typed.textView.replaceCharacters(in: NSRange(location: 0, length: 0), with: "\t") + #expect(typed.textView.string == " ") + + let controller = Mock.loadedTextViewController(string: "") + #expect(controller.replaceTextAsOneEdit(in: NSRange(location: 0, length: 0), with: "\t")) + #expect(controller.textView.string == "\t") + } + + @Test("An editor that takes no edits refuses, so the caller writes the text itself") + func uneditableEditorRefuses() { + let controller = Mock.loadedTextViewController(string: "SELECT 1") + controller.textView.isEditable = false + + #expect(!controller.replaceTextAsOneEdit(in: NSRange(location: 0, length: 8), with: "SELECT 2")) + #expect(controller.textView.string == "SELECT 1") + } + + @Test("A range past the end of the text is refused") + func rangePastTheTextIsRefused() { + let controller = Mock.loadedTextViewController(string: "SELECT 1") + + #expect(!controller.replaceTextAsOneEdit(in: NSRange(location: 4, length: 20), with: "x")) + #expect(controller.textView.string == "SELECT 1") + } +} diff --git a/apple/Plugins/KingbaseESDriverPlugin/KingbaseESPluginDriver.swift b/apple/Plugins/KingbaseESDriverPlugin/KingbaseESPluginDriver.swift index 19e459e8d6..5d99829c88 100644 --- a/apple/Plugins/KingbaseESDriverPlugin/KingbaseESPluginDriver.swift +++ b/apple/Plugins/KingbaseESDriverPlugin/KingbaseESPluginDriver.swift @@ -14,6 +14,9 @@ final class KingbaseESPluginDriver: PostgreSQLPluginDriver, @unchecked Sendable ) } + // Not measured against a KingbaseES catalog yet, so no Extensions group. + override var serverObjectKinds: [PluginServerObjectKind] { [] } + override func probeAfterConnect() async throws { let result = try await catalogRead(KingbaseESCompatibilityMode.probeQuery) let detected = try KingbaseESCompatibilityMode(setting: result.rows.first?.first?.asText) diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver+ServerObjects.swift b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver+ServerObjects.swift new file mode 100644 index 0000000000..fb6ccc9b69 --- /dev/null +++ b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver+ServerObjects.swift @@ -0,0 +1,143 @@ +// +// MySQLPluginDriver+ServerObjects.swift +// MySQLDriverPlugin +// + +import Foundation +import TableProMySQLCore +import TableProPluginKit + +nonisolated internal enum MySQLEvents { + static let kindId = "event" + + static var kind: PluginServerObjectKind { + PluginServerObjectKind( + id: kindId, + displayName: String(localized: "Event"), + pluralDisplayName: String(localized: "Events"), + iconName: "calendar.badge.clock", + scope: .container + ) + } + + // TiDB lists `EVENTS` with no rows and OceanBase is not established, so only these two declare it. + static func isDeclared(flavor: MySQLServerFlavor, banner: String?) -> Bool { + guard flavor == .mysql || flavor == .mariadb else { return false } + return MySQLServerVersion.hasPartitionAndEventCatalogs(banner: banner, flavor: flavor) + } +} + +// Reads `MySQLObjectQueries.eventDetails` rows: name, type, status, schema, then the schedule columns. +nonisolated internal enum MySQLEventRows { + static func objects(from rows: [[PluginCellValue]], scheduler: String? = nil) -> [PluginServerObjectInfo] { + rows.compactMap { object(from: $0, scheduler: scheduler) } + } + + // `scheduler` is `@@global.event_scheduler`, read once beside the rows; nil leaves the attribute out. + static func object(from row: [PluginCellValue], scheduler: String? = nil) -> PluginServerObjectInfo? { + guard let name = text(row, 0) else { return nil } + let isRecurring = text(row, 1)?.uppercased() == "RECURRING" + let status = text(row, 2)?.uppercased() + var attributes: [PluginObjectAttribute] = [] + func add(_ label: String, _ value: String?) { + guard let value, !value.isEmpty else { return } + attributes.append(PluginObjectAttribute(label: label, value: value)) + } + add(String(localized: "Schedule"), schedule( + isRecurring: isRecurring, executeAt: text(row, 4), intervalValue: text(row, 5), intervalField: text(row, 6) + )) + add(String(localized: "Status"), status.map(statusLabel)) + add(String(localized: "Starts"), text(row, 7)) + add(String(localized: "Ends"), text(row, 8)) + add(String(localized: "On Completion"), text(row, 9)) + add(String(localized: "Last Executed"), text(row, 10)) + add(String(localized: "Definer"), text(row, 11)) + add(String(localized: "Comment"), text(row, 12)) + add(String(localized: "Event Scheduler"), scheduler) + return PluginServerObjectInfo( + kindId: MySQLEvents.kindId, + name: name, + schema: text(row, 3), + detail: isRecurring ? String(localized: "Recurring") : String(localized: "One Time"), + isEnabled: status.map { $0 == "ENABLED" }, + attributes: attributes + ) + } + + // The schedule in the clause `CREATE EVENT` takes, quoting a composite interval as the server does. + static func schedule(isRecurring: Bool, executeAt: String?, intervalValue: String?, intervalField: String?) -> String? { + guard isRecurring else { return executeAt.map { "AT '\($0)'" } } + guard let intervalValue, let intervalField else { return nil } + let value = intervalValue.allSatisfy(\.isNumber) ? intervalValue : "'\(intervalValue)'" + return "EVERY \(value) \(intervalField)" + } + + // 8.2 renamed SLAVESIDE_DISABLED to REPLICA_SIDE_DISABLED; both mean the replica does not run it. + static func statusLabel(_ status: String) -> String { + switch status { + case "ENABLED": return String(localized: "Enabled") + case "DISABLED": return String(localized: "Disabled") + case "SLAVESIDE_DISABLED", "REPLICA_SIDE_DISABLED": return String(localized: "Disabled on Replica") + default: return status + } + } + + // The catalog compares names without case, so an exact spelling wins over another the read also matched. + static func row(named name: String, in rows: [[PluginCellValue]]) -> [PluginCellValue]? { + rows.first { $0[safe: 0]?.asText == name } ?? rows.first + } + + private static func text(_ row: [PluginCellValue], _ index: Int) -> String? { + guard let value = row[safe: index]?.asText, !value.isEmpty else { return nil } + return value + } +} + +internal extension MySQLPluginDriver { + var serverObjectKinds: [PluginServerObjectKind] { + let identity = serverIdentity + return MySQLEvents.isDeclared(flavor: identity.flavor, banner: identity.banner) ? [MySQLEvents.kind] : [] + } + + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [PluginServerObjectInfo] { + let identity = serverIdentity + guard kindId == MySQLEvents.kindId, MySQLEvents.isDeclared(flavor: identity.flavor, banner: identity.banner) else { + return [] + } + let exactSchema = await schemaNamesAreCaseSensitive() + let result = try await execute( + ownStatement: MySQLObjectQueries.eventDetails(schema: effectiveSchema(schema), exactSchema: exactSchema) + ) + guard !result.rows.isEmpty else { return [] } + return MySQLEventRows.objects(from: result.rows, scheduler: await eventScheduler()) + } + + // The tab reads the event's row again, so its attributes follow an ALTER EVENT as its DDL does. + func fetchServerObject(_ object: PluginServerObjectInfo) async throws -> PluginServerObjectInfo { + guard object.kindId == MySQLEvents.kindId else { throw PluginObjectSourceError.unsupported(object.name) } + let exactSchema = await schemaNamesAreCaseSensitive() + let result = try await execute(ownStatement: MySQLObjectQueries.eventDetails( + schema: effectiveSchema(object.schema), name: object.name, exactSchema: exactSchema + )) + guard let row = MySQLEventRows.row(named: object.name, in: result.rows), + let fresh = MySQLEventRows.object(from: row, scheduler: await eventScheduler()) + else { throw PluginObjectSourceError.notFound(object.name) } + let definition = try await fetchEventDDL(PluginEventInfo(name: fresh.name, schema: fresh.schema)) + return PluginServerObjectInfo( + kindId: fresh.kindId, + name: fresh.name, + schema: fresh.schema, + identity: object.identity, + detail: fresh.detail, + isEnabled: fresh.isEnabled, + definition: definition, + attributes: fresh.attributes + ) + } + + // A server or proxy without the variable loses this one attribute, never the list. + private func eventScheduler() async -> String? { + guard let result = try? await execute(ownStatement: MySQLObjectQueries.eventScheduler) else { return nil } + return result.rows.first?[safe: 0]?.asText + } +} diff --git a/apple/Plugins/MySQLDriverTests/MySQLEventServerObjectTests.swift b/apple/Plugins/MySQLDriverTests/MySQLEventServerObjectTests.swift new file mode 100644 index 0000000000..3704a927b3 --- /dev/null +++ b/apple/Plugins/MySQLDriverTests/MySQLEventServerObjectTests.swift @@ -0,0 +1,144 @@ +// +// MySQLEventServerObjectTests.swift +// MySQLDriverTests +// + +import Foundation +import TableProPluginKit +import Testing + +struct MySQLEventServerObjectTests { + private func row(_ cells: String?...) -> [PluginCellValue] { + cells.map(PluginCellValue.fromOptional) + } + + private func attribute(_ object: PluginServerObjectInfo?, _ label: String) -> String? { + object?.attributes.first { $0.label == label }?.value + } + + @Test("MySQL and MariaDB declare Events; the other flavors sharing the driver declare none") + func kindsPerFlavor() { + #expect(MySQLEvents.isDeclared(flavor: .mysql, banner: "8.0.36")) + #expect(MySQLEvents.isDeclared(flavor: .mariadb, banner: "10.11.6-MariaDB")) + #expect(MySQLEvents.isDeclared(flavor: .mysql, banner: nil)) + #expect(!MySQLEvents.isDeclared(flavor: .tidb(version: nil), banner: "8.0.11-TiDB-v7.5.0")) + #expect(!MySQLEvents.isDeclared(flavor: .databend, banner: nil)) + #expect(!MySQLEvents.isDeclared(flavor: .oceanbase(version: nil), banner: nil)) + #expect(!MySQLEvents.isDeclared(flavor: .starrocks, banner: nil)) + } + + @Test("A server older than 5.1.6 has no events catalog and declares no Events") + func kindsFollowTheEventsFloor() { + #expect(!MySQLEvents.isDeclared(flavor: .mysql, banner: "5.0.96-log")) + #expect(!MySQLEvents.isDeclared(flavor: .mysql, banner: "4.1.22-standard")) + #expect(MySQLEvents.isDeclared(flavor: .mysql, banner: "5.1.6")) + } + + @Test("The Events kind is listed per database, under its own symbol") + func eventKindDescriptor() { + let kind = MySQLEvents.kind + #expect(kind.id == "event") + #expect(kind.scope == .container) + #expect(kind.iconName == "calendar.badge.clock") + #expect(kind.pluralDisplayName == String(localized: "Events")) + } + + @Test("A recurring event reads its interval as the schedule and counts as enabled") + func recurringEvent() { + let object = MySQLEventRows.object( + from: row( + "nightly_cleanup", "RECURRING", "ENABLED", "shop", + nil, "1", "DAY", "2026-01-01 03:00:00", nil, + "PRESERVE", "2026-11-10 03:00:00", "app@%", "Purges the log" + ), + scheduler: "ON" + ) + + #expect(object?.kindId == "event") + #expect(object?.name == "nightly_cleanup") + #expect(object?.schema == "shop") + #expect(object?.identity == nil) + #expect(object?.detail == String(localized: "Recurring")) + #expect(object?.isEnabled == true) + #expect(attribute(object, String(localized: "Schedule")) == "EVERY 1 DAY") + #expect(attribute(object, String(localized: "Status")) == String(localized: "Enabled")) + #expect(attribute(object, String(localized: "Starts")) == "2026-01-01 03:00:00") + #expect(attribute(object, String(localized: "Ends")) == nil) + #expect(attribute(object, String(localized: "On Completion")) == "PRESERVE") + #expect(attribute(object, String(localized: "Last Executed")) == "2026-11-10 03:00:00") + #expect(attribute(object, String(localized: "Definer")) == "app@%") + #expect(attribute(object, String(localized: "Comment")) == "Purges the log") + #expect(attribute(object, String(localized: "Event Scheduler")) == "ON") + } + + @Test("A one-time event reads its run time as the schedule") + func oneTimeEvent() { + let object = MySQLEventRows.object( + from: row( + "close_quarter", "ONE TIME", "DISABLED", "shop", + "2026-12-31 23:59:00", nil, nil, nil, nil, + "NOT PRESERVE", nil, "root@localhost", nil + ), + scheduler: "OFF" + ) + + #expect(object?.detail == String(localized: "One Time")) + #expect(object?.isEnabled == false) + #expect(attribute(object, String(localized: "Schedule")) == "AT '2026-12-31 23:59:00'") + #expect(attribute(object, String(localized: "Status")) == String(localized: "Disabled")) + #expect(attribute(object, String(localized: "Last Executed")) == nil) + #expect(attribute(object, String(localized: "Event Scheduler")) == "OFF") + } + + @Test("An event disabled on a replica says so under both spellings the server has used") + func replicaDisabledEvent() { + for status in ["SLAVESIDE_DISABLED", "REPLICA_SIDE_DISABLED"] { + let object = MySQLEventRows.object(from: row( + "sync_totals", "RECURRING", status, "shop", nil, "1", "HOUR", nil, nil, "PRESERVE", nil, nil, nil + )) + #expect(object?.isEnabled == false, "\(status)") + #expect(attribute(object, String(localized: "Status")) == String(localized: "Disabled on Replica"), "\(status)") + } + } + + @Test("A composite interval is quoted the way CREATE EVENT spells it") + func compositeInterval() { + #expect(MySQLEventRows.schedule(isRecurring: true, executeAt: nil, intervalValue: "1:30", intervalField: "HOUR_MINUTE") + == "EVERY '1:30' HOUR_MINUTE") + #expect(MySQLEventRows.schedule(isRecurring: true, executeAt: nil, intervalValue: nil, intervalField: "DAY") == nil) + #expect(MySQLEventRows.schedule(isRecurring: false, executeAt: nil, intervalValue: "1", intervalField: "DAY") == nil) + } + + @Test("A scheduler the server would not report leaves only that attribute out") + func missingSchedulerDropsOneAttribute() { + let object = MySQLEventRows.object(from: row( + "nightly", "RECURRING", "ENABLED", "shop", nil, "1", "DAY", nil, nil, "PRESERVE", nil, "app@%", nil + )) + + #expect(attribute(object, String(localized: "Event Scheduler")) == nil) + #expect(attribute(object, String(localized: "Schedule")) == "EVERY 1 DAY") + #expect(attribute(object, String(localized: "Definer")) == "app@%") + } + + @Test("A re-read picks the row spelled exactly as the tab names it") + func rereadPicksTheExactSpelling() { + let rows = [row("Nightly", "RECURRING", "ENABLED", "shop"), row("nightly", "ONE TIME", "DISABLED", "shop")] + + #expect(MySQLEventRows.row(named: "nightly", in: rows)?.first == PluginCellValue.fromOptional("nightly")) + #expect(MySQLEventRows.row(named: "NIGHTLY", in: rows)?.first == PluginCellValue.fromOptional("Nightly")) + #expect(MySQLEventRows.row(named: "nightly", in: []) == nil) + } + + @Test("Four columns alone still map, and a row with no name is dropped") + func shortAndNamelessRows() { + let objects = MySQLEventRows.objects(from: [ + row("legacy", "RECURRING", "ENABLED", "shop"), + row(nil, "RECURRING", "ENABLED", "shop") + ]) + + #expect(objects.count == 1) + #expect(objects.first?.name == "legacy") + #expect(objects.first?.isEnabled == true) + #expect(attribute(objects.first, String(localized: "Schedule")) == nil) + } +} diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLExtensionQueries.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLExtensionQueries.swift new file mode 100644 index 0000000000..228f666e0d --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLExtensionQueries.swift @@ -0,0 +1,97 @@ +// +// PostgreSQLExtensionQueries.swift +// PostgreSQLDriverPlugin +// + +import Foundation +import TableProPluginKit +import TableProPostgreSQLCore + +nonisolated enum PostgreSQLExtensions { + static let kindId = "extension" + + static var kind: PluginServerObjectKind { + PluginServerObjectKind( + id: kindId, + displayName: String(localized: "Extension"), + pluralDisplayName: String(localized: "Extensions"), + iconName: "puzzlepiece.extension", + scope: .database + ) + } + + static func isDeclared(capabilities: PostgreSQLCapabilities) -> Bool { + capabilities.hasExtensionsCatalog + } +} + +// Every column a 9.1 server has; the default version reads control files on disk, so only one object asks for it. +nonisolated enum PostgreSQLExtensionQueries { + private static let projection = """ + SELECT e.oid::pg_catalog.text, e.extname, n.nspname, e.extversion, e.extrelocatable, + pg_catalog.pg_get_userbyid(e.extowner), + pg_catalog.obj_description(e.oid, 'pg_extension') + FROM pg_catalog.pg_extension e + JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace + """ + + static let list = projection + "\nORDER BY e.extname" + + static func one(oid: UInt32) -> String { + projection + "\nWHERE e.oid = \(oid)" + } + + static func defaultVersion(name: String) -> String { + """ + SELECT default_version FROM pg_catalog.pg_available_extensions + WHERE name = \(PostgreSQLObjectQueries.quoteLiteral(name)) + """ + } + + // pg_dump's own form: it names the schema and intentionally leaves the version out. + static func definition(name: String, schema: String, comment: String?) -> String { + var text = "CREATE EXTENSION IF NOT EXISTS \(PostgreSQLObjectQueries.quoteIdentifier(name)) " + + "WITH SCHEMA \(PostgreSQLObjectQueries.quoteIdentifier(schema));" + if let comment, !comment.isEmpty { + text += "\n\nCOMMENT ON EXTENSION \(PostgreSQLObjectQueries.quoteIdentifier(name)) " + + "IS \(PostgreSQLObjectQueries.quoteLiteral(comment));" + } + return text + } + + static func object(from row: [PluginCellValue], defaultVersion: String? = nil) -> PluginServerObjectInfo? { + guard let oid = text(row, 0), UInt32(oid) != nil, let name = text(row, 1) else { return nil } + let schema = text(row, 2) + let version = text(row, 3) + var attributes: [PluginObjectAttribute] = [] + func add(_ label: String, _ value: String?) { + guard let value, !value.isEmpty else { return } + attributes.append(PluginObjectAttribute(label: label, value: value)) + } + add(String(localized: "Version"), version) + add(String(localized: "Default Version"), defaultVersion) + add(String(localized: "Schema"), schema) + add(String(localized: "Owner"), text(row, 5)) + add(String(localized: "Relocatable"), text(row, 4).map { isTrue($0) ? String(localized: "Yes") : String(localized: "No") }) + add(String(localized: "Comment"), text(row, 6)) + return PluginServerObjectInfo( + kindId: PostgreSQLExtensions.kindId, + name: name, + schema: schema, + identity: oid, + detail: version, + definition: schema.map { definition(name: name, schema: $0, comment: text(row, 6)) }, + attributes: attributes + ) + } + + // A boolean cell arrives as `true`/`false` once decoded, and as `t`/`f` from a text read. + private static func isTrue(_ value: String) -> Bool { + value == "true" || value == "t" + } + + private static func text(_ row: [PluginCellValue], _ index: Int) -> String? { + guard let value = row[safe: index]?.asText, !value.isEmpty else { return nil } + return value + } +} diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Extensions.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Extensions.swift new file mode 100644 index 0000000000..64243d9326 --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Extensions.swift @@ -0,0 +1,38 @@ +// +// PostgreSQLPluginDriver+Extensions.swift +// PostgreSQLDriverPlugin +// + +import Foundation +import TableProPluginKit +import TableProPostgreSQLCore + +extension PostgreSQLPluginDriver { + // `serverObjectKinds` is dynamically dispatched, so a subclass that declares none reads nothing here. + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [PluginServerObjectInfo] { + guard kindId == PostgreSQLExtensions.kindId, serverObjectKinds.contains(where: { $0.id == kindId }) else { + return [] + } + let result = try await catalogRead(PostgreSQLExtensionQueries.list) + return result.rows.compactMap { PostgreSQLExtensionQueries.object(from: $0) } + } + + // Read again by the oid this driver handed out, which a rename or a schema move keeps; anything + // else is refused rather than widened into a lookup by name. + func fetchServerObject(_ object: PluginServerObjectInfo) async throws -> PluginServerObjectInfo { + guard object.kindId == PostgreSQLExtensions.kindId else { throw PluginObjectSourceError.unsupported(object.name) } + guard let identity = object.identity, let oid = UInt32(identity) else { + throw PluginObjectSourceError.notFound(object.name) + } + let result = try await catalogRead(PostgreSQLExtensionQueries.one(oid: oid)) + guard let row = result.rows.first(where: { $0[safe: 0]?.asText == String(oid) }), + let name = row[safe: 1]?.asText + else { throw PluginObjectSourceError.notFound(object.name) } + let defaultVersion = try? await catalogRead(PostgreSQLExtensionQueries.defaultVersion(name: name)) + guard let fresh = PostgreSQLExtensionQueries.object( + from: row, + defaultVersion: defaultVersion?.rows.first?[safe: 0]?.asText + ) else { throw PluginObjectSourceError.notFound(object.name) } + return fresh + } +} diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift index 332e0df495..b86fd5edb0 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift @@ -59,6 +59,11 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { var canDebugRoutines: Bool { true } var canFollowNotifications: Bool { true } + // In the class body so KingbaseES, whose catalog is not established, can answer none. + var serverObjectKinds: [PluginServerObjectKind] { + PostgreSQLExtensions.isDeclared(capabilities: catalogCapabilities) ? [PostgreSQLExtensions.kind] : [] + } + init(config: DriverConnectionConfig, singleConnectionMode: Bool = false) { self.core = LibPQDriverCore(config: config, singleConnectionMode: singleConnectionMode) } diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLExtensionQueryTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLExtensionQueryTests.swift new file mode 100644 index 0000000000..35fd9ff50e --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLExtensionQueryTests.swift @@ -0,0 +1,123 @@ +// +// PostgreSQLExtensionQueryTests.swift +// PostgreSQLDriverTests +// + +import Foundation +import TableProPluginKit +import TableProPostgreSQLCore +import Testing + +struct PostgreSQLExtensionQueryTests { + private func row(_ cells: String?...) -> [PluginCellValue] { + cells.map(PluginCellValue.fromOptional) + } + + private func attribute(_ object: PluginServerObjectInfo?, _ label: String) -> String? { + object?.attributes.first { $0.label == label }?.value + } + + private static let constructsMissingBefore91 = [ + "to_regclass", "LATERAL", "WITH ORDINALITY", "json", "array_remove", "array_position", "FILTER (", "unnest(" + ] + + @Test("The extension reads stay inside what PostgreSQL 9.1 parses") + func readsArePortable() { + let queries = [ + PostgreSQLExtensionQueries.list, + PostgreSQLExtensionQueries.one(oid: 16_384), + PostgreSQLExtensionQueries.defaultVersion(name: "pgcrypto") + ] + for sql in queries { + #expect(Self.constructsMissingBefore91.filter { sql.contains($0) }.isEmpty, "\(sql)") + } + } + + @Test("The listing reads every extension once, by name, without the control files") + func listingIsOneCatalogRead() { + let sql = PostgreSQLExtensionQueries.list + #expect(sql.contains("FROM pg_catalog.pg_extension e")) + #expect(sql.contains("JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace")) + #expect(sql.hasSuffix("ORDER BY e.extname")) + #expect(!sql.contains("pg_available_extensions")) + #expect(!sql.contains("WHERE")) + } + + @Test("One extension is read again by its oid alone") + func rereadIsByOid() { + let sql = PostgreSQLExtensionQueries.one(oid: 16_384) + #expect(sql.hasSuffix("WHERE e.oid = 16384")) + #expect(!sql.contains("extname =")) + } + + @Test("The default version read quotes its name as a literal") + func defaultVersionQuotesTheName() { + #expect(PostgreSQLExtensionQueries.defaultVersion(name: "o'brien").contains("WHERE name = 'o''brien'")) + } + + @Test("A plain extension's definition is pg_dump's, with no version") + func plainDefinition() { + #expect(PostgreSQLExtensionQueries.definition(name: "pgcrypto", schema: "public", comment: nil) + == #"CREATE EXTENSION IF NOT EXISTS "pgcrypto" WITH SCHEMA "public";"#) + } + + @Test("A name or schema that needs quoting is quoted as an identifier") + func quotedDefinition() { + #expect(PostgreSQLExtensionQueries.definition(name: "uuid-ossp", schema: "My Schema", comment: "") + == #"CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA "My Schema";"#) + } + + @Test("A comment follows as COMMENT ON EXTENSION, its quote doubled") + func commentWithAQuote() { + let text = PostgreSQLExtensionQueries.definition(name: "hstore", schema: "public", comment: "data type for key/value's") + #expect(text.hasPrefix(#"CREATE EXTENSION IF NOT EXISTS "hstore" WITH SCHEMA "public";"#)) + #expect(text.hasSuffix(#"COMMENT ON EXTENSION "hstore" IS 'data type for key/value''s';"#)) + } + + @Test("A listed row carries its oid, version and definition") + func rowMapping() { + let object = PostgreSQLExtensionQueries.object( + from: row("16384", "pgcrypto", "public", "1.3", "true", "postgres", "cryptographic functions") + ) + + #expect(object?.kindId == "extension") + #expect(object?.identity == "16384") + #expect(object?.name == "pgcrypto") + #expect(object?.schema == "public") + #expect(object?.detail == "1.3") + #expect(object?.isEnabled == nil) + #expect(attribute(object, String(localized: "Version")) == "1.3") + #expect(attribute(object, String(localized: "Owner")) == "postgres") + #expect(attribute(object, String(localized: "Relocatable")) == String(localized: "Yes")) + #expect(attribute(object, String(localized: "Default Version")) == nil) + #expect(object?.definition?.contains("COMMENT ON EXTENSION \"pgcrypto\"") == true) + } + + @Test("The re-read adds the default version, and a non-relocatable extension says No") + func rereadRowMapping() { + let object = PostgreSQLExtensionQueries.object( + from: row("13000", "plpgsql", "pg_catalog", "1.0", "f", "postgres", nil), + defaultVersion: "1.0" + ) + + #expect(attribute(object, String(localized: "Default Version")) == "1.0") + #expect(attribute(object, String(localized: "Relocatable")) == String(localized: "No")) + #expect(attribute(object, String(localized: "Comment")) == nil) + #expect(object?.definition == #"CREATE EXTENSION IF NOT EXISTS "plpgsql" WITH SCHEMA "pg_catalog";"#) + } + + @Test("A row without an oid is not an extension this driver can read again") + func rowWithoutOidIsDropped() { + #expect(PostgreSQLExtensionQueries.object(from: row(nil, "pgcrypto", "public", "1.3", "t", nil, nil)) == nil) + #expect(PostgreSQLExtensionQueries.object(from: row("pgcrypto", "pgcrypto", "public", "1.3", "t", nil, nil)) == nil) + } + + @Test("Extensions are declared from 9.1, at database scope") + func declaredFromNinePointOne() { + #expect(!PostgreSQLExtensions.isDeclared(capabilities: PostgreSQLCapabilities(serverVersion: 90_024))) + #expect(PostgreSQLExtensions.isDeclared(capabilities: PostgreSQLCapabilities(serverVersion: 90_100))) + #expect(PostgreSQLExtensions.isDeclared(capabilities: .assumingModernWhenUnknown(0))) + #expect(PostgreSQLExtensions.kind.scope == .database) + #expect(PostgreSQLExtensions.kind.id == "extension") + } +} diff --git a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift index 53d2af6929..eda4295d3f 100644 --- a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift +++ b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift @@ -200,6 +200,14 @@ public protocol PluginDatabaseDriver: AnyObject, Sendable { func fetchEvents(schema: String?) async throws -> [PluginEventInfo] func fetchEventDDL(_ event: PluginEventInfo) async throws -> String + // Asked of the live driver, so one plugin can answer per server flavor and version. + var serverObjectKinds: [PluginServerObjectKind] { get } + // `schema` is the container for a `.container` kind and nil for a `.database` kind. A server + // without the catalog answers empty rather than throwing. + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [PluginServerObjectInfo] + // Reads one listed object again with its definition; `identity` is the driver's own key. + func fetchServerObject(_ object: PluginServerObjectInfo) async throws -> PluginServerObjectInfo + /// The settings the server recorded for an object when it was created and applies whenever it runs, such as a /// MySQL routine's `sql_mode`, as statements a script writes around the object's `CREATE`. Nil when there are none. func fetchRoutineCreationScope(_ routine: PluginRoutineInfo) async throws -> PluginStatementScope? @@ -824,6 +832,17 @@ public extension PluginDatabaseDriver { return definition } + var serverObjectKinds: [PluginServerObjectKind] { [] } + + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [PluginServerObjectInfo] { [] } + + func fetchServerObject(_ object: PluginServerObjectInfo) async throws -> PluginServerObjectInfo { + guard let definition = object.definition, !definition.isEmpty else { + throw PluginObjectSourceError.unsupported(object.name) + } + return object + } + func fetchRoutineCreationScope(_ routine: PluginRoutineInfo) async throws -> PluginStatementScope? { nil } func fetchTriggerCreationScope(_ trigger: PluginTriggerInfo) async throws -> PluginStatementScope? { nil } diff --git a/apple/Plugins/TableProPluginKit/PluginServerObject.swift b/apple/Plugins/TableProPluginKit/PluginServerObject.swift new file mode 100644 index 0000000000..460117f92f --- /dev/null +++ b/apple/Plugins/TableProPluginKit/PluginServerObject.swift @@ -0,0 +1,68 @@ +// +// PluginServerObject.swift +// TableProPluginKit +// + +import Foundation + +public enum PluginServerObjectScope: String, Codable, Sendable { + // Listed in every container the engine lists tables in: a MySQL database, a PostgreSQL schema. + case container + // Listed once per database, beside its schemas. + case database +} + +public struct PluginServerObjectKind: Codable, Sendable, Hashable { + public let id: String + public let displayName: String + public let pluralDisplayName: String + public let iconName: String + public let scope: PluginServerObjectScope + + public init( + id: String, + displayName: String, + pluralDisplayName: String, + iconName: String, + scope: PluginServerObjectScope = .container + ) { + self.id = id + self.displayName = displayName + self.pluralDisplayName = pluralDisplayName + self.iconName = iconName + self.scope = scope + } +} + +public struct PluginServerObjectInfo: Codable, Sendable { + public let kindId: String + public let name: String + public let schema: String? + // The driver's own key for reading this object again, opaque to the app. + public let identity: String? + // One short label drawn beside the name, such as "Recurring" or "1.3". + public let detail: String? + public let isEnabled: Bool? + public let definition: String? + public let attributes: [PluginObjectAttribute] + + public init( + kindId: String, + name: String, + schema: String? = nil, + identity: String? = nil, + detail: String? = nil, + isEnabled: Bool? = nil, + definition: String? = nil, + attributes: [PluginObjectAttribute] = [] + ) { + self.kindId = kindId + self.name = name + self.schema = schema + self.identity = identity + self.detail = detail + self.isEnabled = isEnabled + self.definition = definition + self.attributes = attributes + } +} diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift index 12236dcb7f..15c3d75743 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift @@ -103,6 +103,7 @@ extension QueryExecutionCoordinator { /// The app opens no transaction around the run, as SQL Server's own tools do not: a batch carries on past most /// errors, so an app-owned rollback would take back statements that ran after the failure, and the script's own /// `BEGIN TRAN`, `TRY...CATCH` and procedures are what decide. A transaction the script leaves open is reported. + /// Manual Commit is the exception: the run is held, and a failure or a Stop rolls it back. func executeBatches( _ batches: [ExecutableBatch], parameters: [QueryParameter], @@ -250,6 +251,10 @@ extension QueryExecutionCoordinator { lease: DriverLeaseOwner ) async -> BatchRun { let failureOutput = ServerOutputBox() + let commitMode = DatabaseManager.shared.commitMode(for: scope.connectionId) + let databaseType = parent.connection.type + let grammar = parent.lexicalGrammar + let statementTexts = prepared.flatMap { $0.batch.statements.map(\.sql) } do { var run = try await DatabaseManager.shared.withScopedDriver( scope: scope, @@ -257,7 +262,16 @@ extension QueryExecutionCoordinator { cancellation: .cancellableRead(lease) ) { driver in let startState = await driver.heldSessionTransactionState() - let plan = BatchTransactionPlan.autocommit.joining(startState) + let manual = ManualCommit.decision( + commitMode: commitMode, + statements: statementTexts, + databaseType: databaseType, + grammar: grammar, + supportsTransactions: driver.supportsTransactions, + sessionState: startState + ) + if let refusal = manual.refusal { throw refusal } + let plan = ManualCommit.runPlan(text: .autocommit, decision: manual, sessionState: startState) let outcome = await BatchStatementRun.run( prepared, plan: plan, diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift index ead6c8e0e9..04b2242bc3 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift @@ -224,7 +224,8 @@ extension QueryExecutionCoordinator { absentCells: [Int: Set] = [:], unloadedCells: [Int: [Int: Int]] = [:], columnOrigins: [PluginColumnOrigin?]? = nil, - prepared: TableRows.PreparedRows? = nil + prepared: TableRows.PreparedRows? = nil, + trigger: TableLoadTrigger = .userInitiated ) { guard let idx = parent.tabManager.tabs.firstIndex(where: { $0.id == tabId }) else { return } @@ -383,20 +384,12 @@ extension QueryExecutionCoordinator { } parent.dropParkedEdits(ofBackgroundTab: tabId) - recordHistory( - QueryHistoryRecordRequest( - query: historySQL ?? sql, - connectionId: conn.id, - databaseName: historyDatabaseName(tabId: tabId), - databaseType: conn.type, - schemaName: historySchemaName(tabId: tabId), - source: historySource(tabId: tabId), - executionTime: executionTime, - rowCount: rows.count, - wasSuccessful: true, - timing: timing + if trigger.recordsHistory { + recordSuccessfulRun( + query: historySQL ?? sql, tabId: tabId, connection: conn, + executionTime: executionTime, rowCount: rows.count, timing: timing ) - ) + } if parent.tabManager.selectedTabId == tabId, isEditable, !parent.changeManager.hasChanges { parent.changeManager.clearChangesAndUndoHistory() @@ -479,11 +472,36 @@ extension QueryExecutionCoordinator { ) } + private func recordSuccessfulRun( + query: String, + tabId: UUID, + connection conn: DatabaseConnection, + executionTime: TimeInterval, + rowCount: Int, + timing: PluginQueryTiming? + ) { + recordHistory( + QueryHistoryRecordRequest( + query: query, + connectionId: conn.id, + databaseName: historyDatabaseName(tabId: tabId), + databaseType: conn.type, + schemaName: historySchemaName(tabId: tabId), + source: historySource(tabId: tabId), + executionTime: executionTime, + rowCount: rowCount, + wasSuccessful: true, + timing: timing + ) + ) + } + func launchPhase2Work( tableName: String, tabId: UUID, connectionType: DatabaseType, - schemaTask: Task? + schemaTask: Task?, + countsRows: Bool = true ) { let isNonSQL = PluginManager.shared.editorLanguage(for: connectionType) != .sql let contentEpoch = parent.tabExecution.contentEpoch(for: tabId) @@ -491,7 +509,7 @@ extension QueryExecutionCoordinator { Task(priority: .utility) { [weak self, parent] in // The caller marked the total pending; every exit that skips `resolveRowCount` must release it, or Count Exactly never returns. guard let self else { - parent.releaseCountPending(for: tabId) + if countsRows { parent.releaseCountPending(for: tabId) } return } guard !parent.isTearingDown else { return } @@ -503,14 +521,14 @@ extension QueryExecutionCoordinator { await MainActor.run { [weak self] in guard let self else { - parent.releaseCountPending(for: tabId) + if countsRows { parent.releaseCountPending(for: tabId) } return } if let schema { applySchemaMetadata(schema, tabId: tabId, tableName: tableName, resultSetId: resultSetId) } // A retarget or newer run owns the tab now with its own pending mark; clearing here would release the successor's. - guard parent.tabExecution.isSameContent(contentEpoch, for: tabId) else { return } + guard countsRows, parent.tabExecution.isSameContent(contentEpoch, for: tabId) else { return } resolveRowCount( tableName: tableName, tabId: tabId, @@ -785,7 +803,7 @@ extension QueryExecutionCoordinator { case .clear: return .clear case .approximate: - guard let count = try? await DatabaseManager.shared.withMetadataDriver(scope: scope, { driver in + guard let count = try? await DatabaseManager.shared.withMetadataRead(scope: scope, workload: .interactive, { driver in try await driver.fetchApproximateRowCount(table: tableName, schema: scope.schema) }) else { return nil } return .count(count, isApproximate: true) @@ -860,7 +878,8 @@ extension QueryExecutionCoordinator { sql: String, tabId: UUID, connection conn: DatabaseConnection, - serverOutput: PluginServerOutput = .none + serverOutput: PluginServerOutput = .none, + trigger: TableLoadTrigger = .userInitiated ) { let diagnosis = DatabaseWriteRejectionDiagnosis.formatted(error) let message = ServerOutputCapture.failureMessage(diagnosis, output: serverOutput) @@ -873,6 +892,7 @@ extension QueryExecutionCoordinator { tab.execution.executionTime = nil } + guard trigger.recordsHistory else { return } recordHistory( QueryHistoryRecordRequest( query: sql, diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift index c0bcf998c7..d65d426f29 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift @@ -202,6 +202,7 @@ extension QueryExecutionCoordinator { let boundValues = BoundParameterValues(values: parameters) let failureOutput = ServerOutputBox() let grammar = parent.lexicalGrammar + let held = parent.manualCommitRun(skipped: tab.tabType != .query) let parameterizedTask = Task { [weak self, parent] in guard let self else { return } @@ -215,13 +216,15 @@ extension QueryExecutionCoordinator { route: DatabaseManager.shared.executionRoute(for: scope), cancellation: .cancellableRead(lease) ) { [queryExecutor = parent.queryExecutor, boundValues] driver in - let fetched = try await queryExecutor.executeQuery( - driver: driver, - sql: statement.sql, - parameters: boundValues.values, - rowCap: rowCap, - capturingOutputInto: failureOutput - ) + let fetched = try await held.running([statement.sql], on: driver) { + try await queryExecutor.executeQuery( + driver: driver, + sql: statement.sql, + parameters: boundValues.values, + rowCap: rowCap, + capturingOutputInto: failureOutput + ) + } let edits = await SucceededStatements.single( statement.sql, scope: scope, databaseType: conn.type, grammar: grammar, ranOn: driver ) @@ -359,6 +362,7 @@ extension QueryExecutionCoordinator { let transactionKind = OperationKind.worst(of: statementTexts, databaseType: conn.type) let grammar = parent.lexicalGrammar let plan = BatchTransactionPolicy.plan(for: statementTexts, databaseType: conn.type, grammar: grammar) + let commitMode = DatabaseManager.shared.commitMode(for: conn.id) let prepared = statements.map { statement in prepareStatement( statement: statement, @@ -398,13 +402,14 @@ extension QueryExecutionCoordinator { scope: scope, mode: transactionKind.transactionAccessMode, plan: plan, + commitMode: commitMode, asksEndState: asksEndState, claim: claim, lease: lease ) let outcome = run.outcome let sent = running.prepared - let sessionNotice = run.plan == .sessionTransaction ? run.sessionState.openTransactionNotice : nil + let sessionNotice = run.plan.readsSessionEndState ? run.sessionState.openTransactionNotice : nil let ranStatements: [String] switch outcome { @@ -520,6 +525,7 @@ extension QueryExecutionCoordinator { scope: DatabaseScope, mode: PluginTransactionAccessMode, plan: BatchTransactionPlan, + commitMode: ConnectionCommitMode, asksEndState: Bool, claim: TabExecutionClaim, lease: DriverLeaseOwner @@ -527,6 +533,8 @@ extension QueryExecutionCoordinator { let failureOutput = ServerOutputBox() let commitOutput = ServerOutputBox() let databaseType = parent.connection.type + let grammar = parent.lexicalGrammar + let statementTexts = prepared.map(\.sentSQL) do { var run = try await DatabaseManager.shared.withScopedDriver( scope: scope, @@ -534,7 +542,16 @@ extension QueryExecutionCoordinator { cancellation: .cancellableRead(lease) ) { driver in let startState = await driver.heldSessionTransactionState() - let sessionPlan = plan.joining(startState) + let manual = ManualCommit.decision( + commitMode: commitMode, + statements: statementTexts, + databaseType: databaseType, + grammar: grammar, + supportsTransactions: driver.supportsTransactions, + sessionState: startState + ) + if let refusal = manual.refusal { throw refusal } + let sessionPlan = ManualCommit.runPlan(text: plan, decision: manual, sessionState: startState) let outcome = await BatchStatementRun.run( prepared, plan: sessionPlan, @@ -563,7 +580,7 @@ extension QueryExecutionCoordinator { ) } } - guard sessionPlan == .sessionTransaction else { + guard sessionPlan.readsSessionEndState else { let endState: PluginSessionTransactionState = asksEndState ? await driver.heldSessionTransactionState() : .unknown diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift index 9911aa248a..82e3ff91a8 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift @@ -3,6 +3,7 @@ // TablePro // +import AppKit import Combine import Foundation import TableProSQLGrammar @@ -32,6 +33,7 @@ final class QueryExecutionCoordinator: ObservableObject { let batches = executionBatches(in: tab.content.query) let statements = batches.flatMap(\.statements) guard !statements.isEmpty else { return } + parent.tabVersions.record(tab, connectionId: parent.connectionId, reason: .run) if AppSettingsManager.shared.editor.queryParametersEnabled, parent.bindsNamedParameters { let combinedSQL = SQLParameterExtractor.parameterSource(of: statements) @@ -76,8 +78,10 @@ final class QueryExecutionCoordinator: ObservableObject { statements: batches.flatMap(\.statements), extraCapabilities: extraCapabilities ) + let statementTexts = batches.flatMap(\.statements).map(\.sql) Task { [parent] in defer { parent.isShowingSafeModePrompt = false } + guard await confirmsImplicitCommit(of: statementTexts, onTab: target.tabId) else { return } switch await parent.executionGate.authorize(request) { case .authorized: guard parent.rereadCanStart(install.source), stillHolds(target) else { return } @@ -129,6 +133,24 @@ final class QueryExecutionCoordinator: ObservableObject { return true } + // Asked before the gate, for any open transaction, Manual Commit or a typed BEGIN. + private func confirmsImplicitCommit(of statements: [String], onTab tabId: UUID) async -> Bool { + let databaseType = parent.connection.type + let commitMode = DatabaseManager.shared.commitMode(for: parent.connectionId) + // A run Manual Commit refuses is refused in the lease, so it is never offered Commit and Run. + guard ManualCommit.asksBeforeImplicitCommit( + statements, databaseType: databaseType, grammar: parent.lexicalGrammar, commitMode: commitMode + ), + let tab = parent.tabManager.tabs.first(where: { $0.id == tabId }), + let scope = parent.scope(for: tab), + DatabaseManager.shared.sessionTransactions.holdsTransaction( + connectionId: parent.connectionId, container: scope.database + ) + else { return true } + let name = scope.database.isEmpty ? parent.connection.name : scope.database + return await ConnectionTransactionActions.confirmImplicitCommit(on: name, window: parent.contentWindow) + } + private func makeExecuteRequest( statements: [SQLStatementScanner.ExecutableStatement], extraCapabilities: CallerCapabilities = [] @@ -160,8 +182,10 @@ final class QueryExecutionCoordinator: ObservableObject { statements: batches.flatMap(\.statements), extraCapabilities: extraCapabilities ) + let statementTexts = batches.flatMap(\.statements).map(\.sql) Task { [parent] in defer { parent.isShowingSafeModePrompt = false } + guard await confirmsImplicitCommit(of: statementTexts, onTab: target.tabId) else { return } switch await parent.executionGate.authorize(request) { case .authorized: guard parent.rereadCanStart(install.source), stillHolds(target) else { return } @@ -206,7 +230,7 @@ final class QueryExecutionCoordinator: ObservableObject { } } - /// A run the driver cannot carry out as written says so before anything is sent, rather than running part of it. + // A run the driver cannot carry out as written says so before anything is sent, rather than running part of it. private func refuses(_ route: QueryExecutionRoute, tabIndex index: Int) -> Bool { guard case .needsBatchDriver = route else { return false } parent.tabManager.mutate(at: index) { diff --git a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+Discard.swift b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+Discard.swift index b74650db33..fca7c1b6b6 100644 --- a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+Discard.swift +++ b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+Discard.swift @@ -36,12 +36,13 @@ extension RowEditingCoordinator { let mode: PluginTransactionAccessMode = kind.declaresWrite ? .readWrite : .serverDefault let plan = DataWritePlan(scope: scope, databaseType: parent.connection.type, steps: steps) + let commitMode = DatabaseManager.shared.commitMode(for: parent.connectionId) _ = try await DatabaseManager.shared.withScopedDriver( scope: scope, route: DatabaseManager.shared.executionRoute(for: scope), cancellation: .protectedWrite ) { driver in - _ = try await DataWriteExecutor.run(plan, mode: mode, on: driver) + _ = try await DataWriteExecutor.run(plan, mode: mode, commitMode: commitMode, on: driver) } } diff --git a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift index bcb80872e1..02e710469c 100644 --- a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift +++ b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift @@ -260,6 +260,7 @@ extension RowEditingCoordinator { } let route = DatabaseManager.shared.executionRoute(for: scope) + let commitMode = DatabaseManager.shared.commitMode(for: conn.id) let serverOwned = serverOwnedColumns(of: savingTabId) let columnTypeNames = parent.changeManager.columnTypeNames @@ -268,6 +269,14 @@ extension RowEditingCoordinator { waiter?.resume(returning: false) return } + if clearTableOps, await !confirmsImplicitCommit(of: plan) { + restorePendingTableOperations( + connectionId: conn.id, truncates: truncatedTables, deletes: deletedTables, options: capturedOptions + ) + endSaveInFlight() + waiter?.resume(returning: false) + return + } let overallStartTime = Date() let operationStart = ContinuousClock.Instant.now @@ -277,7 +286,7 @@ extension RowEditingCoordinator { route: route, cancellation: .protectedWrite ) { driver in - try await DataWriteExecutor.run(plan, mode: .readWrite, on: driver) + try await DataWriteExecutor.run(plan, mode: .readWrite, commitMode: commitMode, on: driver) } let history = recordSuccessHistory( @@ -686,6 +695,21 @@ extension RowEditingCoordinator { ) } + // A staged TRUNCATE or DROP on MySQL or Oracle commits the transaction the bar shows before it runs. + private func confirmsImplicitCommit(of plan: DataWritePlan) async -> Bool { + let statements = plan.steps.map(\.statement.sql) + guard ManualCommit.asksBeforeImplicitCommit( + statements, databaseType: parent.connection.type, grammar: parent.lexicalGrammar, + commitMode: DatabaseManager.shared.commitMode(for: parent.connectionId) + ), + DatabaseManager.shared.sessionTransactions.holdsTransaction( + connectionId: parent.connectionId, container: plan.scope.database + ) + else { return true } + let name = plan.scope.database.isEmpty ? parent.connection.name : plan.scope.database + return await ConnectionTransactionActions.confirmImplicitCommit(on: name, window: parent.contentWindow) + } + // Written to the tab that started the save: the one in front may be another by the time it fails. private func failSave(message: String, tabId: UUID?, waiter: CheckedContinuation?) { endSaveInFlight() diff --git a/apple/TablePro/Core/DataGrid/DataGridModel.swift b/apple/TablePro/Core/DataGrid/DataGridModel.swift index fa3d21b8a6..86fb966261 100644 --- a/apple/TablePro/Core/DataGrid/DataGridModel.swift +++ b/apple/TablePro/Core/DataGrid/DataGridModel.swift @@ -15,6 +15,10 @@ protocol DataGridMountedView: AnyObject { func viewportSample() -> (row: Int, offset: CGFloat)? @discardableResult func beginEditing(row: Int, displayColumn: Int) -> Bool var isEditing: Bool { get } + // A viewer, the value filter or the foreign key preview: a reload closes or repoints each of them. + var holdsTransientUI: Bool { get } + // A live scroll or a selection drag, which a reload would move rows under. + var isTrackingInteraction: Bool { get } func commitEditing() -> Bool func discardEditing() @discardableResult func focus() -> Bool @@ -22,6 +26,11 @@ protocol DataGridMountedView: AnyObject { func anchor(forColumn displayColumn: Int) -> (view: NSView, rect: NSRect)? } +extension DataGridMountedView { + var holdsTransientUI: Bool { false } + var isTrackingInteraction: Bool { false } +} + @MainActor final class DataGridModel: ObservableObject { let id = UUID() @@ -240,6 +249,12 @@ final class DataGridModel: ObservableObject { setSelection(GridSelection(rectangles: rects, activeCell: active, anchor: active, mode: rects.isEmpty ? .cells : .rows)) } + // Takes back a selection kept by identity, so a cell cursor stays a cursor on its own column. + func select(_ snapshot: GridSelectionSnapshot) { + let resolved = snapshot.resolved(order: order, rows: rows, columns: columns) + setSelection(resolved.rectangles.isEmpty && resolved.activeCell == nil ? .empty : resolved) + } + func clearSelection() { setSelection(.empty) } @@ -578,6 +593,8 @@ final class DataGridModel: ObservableObject { } var isEditing: Bool { mountedView?.isEditing ?? false } + var holdsTransientUI: Bool { mountedView?.holdsTransientUI ?? false } + var isTrackingInteraction: Bool { mountedView?.isTrackingInteraction ?? false } // Owners that deferred work while an editor was open resume here. var onEditingEnded: (@MainActor () -> Void)? diff --git a/apple/TablePro/Core/DataGrid/Engine/GridSelectionSnapshot.swift b/apple/TablePro/Core/DataGrid/Engine/GridSelectionSnapshot.swift index 14e8eb047c..507d2d4f46 100644 --- a/apple/TablePro/Core/DataGrid/Engine/GridSelectionSnapshot.swift +++ b/apple/TablePro/Core/DataGrid/Engine/GridSelectionSnapshot.swift @@ -57,6 +57,35 @@ struct GridSelectionSnapshot: Equatable { self.mode = selection.mode } + var namedRowIDs: Set { + var named = Set(rowIDs) + for rectangle in rectangles { named.formUnion(rectangle.rowIDs) } + if let activeCell { named.insert(activeCell.rowID) } + if let anchor { named.insert(anchor.rowID) } + return named + } + + // A row the transform cannot place leaves, and a cell on it goes with it; the mode and columns stay. + func mappingRows(_ transform: (RowID) -> RowID?) -> GridSelectionSnapshot { + func cell(_ cell: Cell?) -> Cell? { + guard let cell, let rowID = transform(cell.rowID) else { return nil } + return Cell(rowID: rowID, columnID: cell.columnID) + } + var mapped = GridSelectionSnapshot( + rowIDs: rowIDs.compactMap(transform), + rectangles: rectangles.compactMap { rectangle in + let rows = rectangle.rowIDs.compactMap(transform) + return rows.isEmpty ? nil : Rectangle(rowIDs: rows, columnIDs: rectangle.columnIDs) + }, + activeCell: cell(activeCell), + anchor: cell(anchor) + ) + guard !mapped.isEmpty else { return .empty } + mapped.pickedColumnIDs = pickedColumnIDs + mapped.mode = mode + return mapped + } + func resolved(order: DataGridRowOrder, rows: TableRows, columns: DataGridColumnSet) -> GridSelection { func coord(_ cell: Cell?) -> GridCoord? { guard let cell, diff --git a/apple/TablePro/Core/DataGrid/GridViewportResolver.swift b/apple/TablePro/Core/DataGrid/GridViewportResolver.swift index 91dd9d2d77..ddfb7daee0 100644 --- a/apple/TablePro/Core/DataGrid/GridViewportResolver.swift +++ b/apple/TablePro/Core/DataGrid/GridViewportResolver.swift @@ -11,37 +11,74 @@ internal struct GridViewportSnapshot: Equatable { let firstVisiblePosition: Int let firstVisibleOffset: CGFloat let firstVisibleKey: [String: String]? - var selectedKeys: [[String: String]] = [] + // Row ids are positional per page, so each row the selection names travels as its key. + var selection: GridSelectionSnapshot = .empty + var selectionKeys: [RowID: [String: String]] = [:] static let top = GridViewportSnapshot(firstVisiblePosition: 0, firstVisibleOffset: 0, firstVisibleKey: nil) + var selectedKeys: [[String: String]] { + selection.rowIDs.compactMap { selectionKeys[$0] } + } + // For a re-read that starts at the first row but keeps what is selected. var selectionOnly: GridViewportSnapshot { - GridViewportSnapshot(firstVisiblePosition: 0, firstVisibleOffset: 0, firstVisibleKey: nil, selectedKeys: selectedKeys) + GridViewportSnapshot( + firstVisiblePosition: 0, + firstVisibleOffset: 0, + firstVisibleKey: nil, + selection: selection, + selectionKeys: selectionKeys + ) } } internal struct GridViewportPlacement: Equatable { let firstVisibleRow: RowID? let firstVisibleOffset: CGFloat - let selectedRows: [RowID] + let selection: GridSelectionSnapshot let revealsSelection: Bool + init( + firstVisibleRow: RowID?, + firstVisibleOffset: CGFloat, + selection: GridSelectionSnapshot, + revealsSelection: Bool + ) { + self.firstVisibleRow = firstVisibleRow + self.firstVisibleOffset = firstVisibleOffset + self.selection = selection + self.revealsSelection = revealsSelection + } + + init(firstVisibleRow: RowID?, firstVisibleOffset: CGFloat, selectedRows: [RowID], revealsSelection: Bool) { + self.init( + firstVisibleRow: firstVisibleRow, + firstVisibleOffset: firstVisibleOffset, + selection: selectedRows.isEmpty + ? .empty + : GridSelectionSnapshot(rowIDs: selectedRows, rectangles: [], activeCell: nil, anchor: nil), + revealsSelection: revealsSelection + ) + } + + var selectedRows: [RowID] { selection.rowIDs } + static let firstRow = GridViewportPlacement( firstVisibleRow: nil, firstVisibleOffset: 0, - selectedRows: [], + selection: .empty, revealsSelection: false ) } extension GridViewportPlacement { - func selecting(_ rows: [RowID]) -> GridViewportPlacement { - guard !rows.isEmpty else { return self } + func selecting(_ selection: GridSelectionSnapshot) -> GridViewportPlacement { + guard selection != .empty else { return self } return GridViewportPlacement( firstVisibleRow: firstVisibleRow, firstVisibleOffset: firstVisibleOffset, - selectedRows: rows, + selection: selection, revealsSelection: revealsSelection ) } @@ -60,21 +97,27 @@ internal enum GridViewportResolver { displayIDs: [RowID]?, firstVisibleDisplayRow: Int, firstVisibleOffset: CGFloat, - selectedDisplayRows: [Int] = [], + selection: GridSelectionSnapshot = .empty, keyColumns: [String], isCellModified: (RowID, Int) -> Bool = { _, _ in false } ) -> GridViewportSnapshot { let isKeyed = !keyColumns.isEmpty && tableRows.count <= keyedRowLimit let order = DataGridRowOrder(filteredIDs: displayIDs, revision: 0) - func key(_ displayRow: Int) -> [String: String]? { - order.row(at: displayRow, in: tableRows) - .flatMap { savedKey(of: $0, in: tableRows, keyColumns: keyColumns, isCellModified: isCellModified) } + func key(of row: Row?) -> [String: String]? { + row.flatMap { savedKey(of: $0, in: tableRows, keyColumns: keyColumns, isCellModified: isCellModified) } + } + var selectionKeys: [RowID: [String: String]] = [:] + if isKeyed { + for rowID in selection.namedRowIDs { + selectionKeys[rowID] = key(of: tableRows.row(withID: rowID)) + } } return GridViewportSnapshot( firstVisiblePosition: max(0, firstVisibleDisplayRow), firstVisibleOffset: max(0, firstVisibleOffset), - firstVisibleKey: isKeyed ? key(firstVisibleDisplayRow) : nil, - selectedKeys: isKeyed ? selectedDisplayRows.sorted().compactMap { key($0) } : [] + firstVisibleKey: isKeyed ? key(of: order.row(at: firstVisibleDisplayRow, in: tableRows)) : nil, + selection: isKeyed ? selection : .empty, + selectionKeys: selectionKeys ) } @@ -89,10 +132,10 @@ internal enum GridViewportResolver { switch intent { case .firstRow: return GridViewportPlacement.firstRow - .selecting(rows(matching: snapshot.selectedKeys, in: incoming, keyColumns: keyColumns)) + .selecting(remappedSelection(snapshot, in: incoming, keyColumns: keyColumns)) case .keepPlace: return keptPlace(from: snapshot, in: incoming, displayIDs: displayIDs, keyColumns: keyColumns) - .selecting(rows(matching: snapshot.selectedKeys, in: incoming, keyColumns: keyColumns)) + .selecting(remappedSelection(snapshot, in: incoming, keyColumns: keyColumns)) case .restoreRow(let anchor): guard let row = uniqueRow(matching: anchor, in: incoming) else { return .firstRow } return GridViewportPlacement( @@ -153,14 +196,14 @@ internal enum GridViewportResolver { } // One pass, so a large selection does not rescan per row; a repeated key matches nothing, as in uniqueRow(matching:in:). - private static func rows( - matching keys: [[String: String]], + private static func remappedSelection( + _ snapshot: GridViewportSnapshot, in incoming: TableRows, keyColumns: [String] - ) -> [RowID] { - guard !keys.isEmpty, !keyColumns.isEmpty, incoming.count <= keyedRowLimit else { return [] } + ) -> GridSelectionSnapshot { + guard !snapshot.selectionKeys.isEmpty, !keyColumns.isEmpty, incoming.count <= keyedRowLimit else { return .empty } let indices = keyColumns.compactMap { incoming.columns.firstIndex(of: $0) } - guard indices.count == keyColumns.count else { return [] } + guard indices.count == keyColumns.count else { return .empty } var rowByKey: [[String]: RowID] = [:] var repeated = Set<[String]>() @@ -171,11 +214,13 @@ internal enum GridViewportResolver { repeated.insert(values) } } - return keys.compactMap { key in + var incomingRows: [RowID: RowID] = [:] + for (outgoing, key) in snapshot.selectionKeys { let values = keyColumns.compactMap { key[$0] } - guard values.count == keyColumns.count, !repeated.contains(values) else { return nil } - return rowByKey[values] + guard values.count == keyColumns.count, !repeated.contains(values), let row = rowByKey[values] else { continue } + incomingRows[outgoing] = row } + return snapshot.selection.mappingRows { incomingRows[$0] } } private static func uniqueRow(matching values: [String: String], in incoming: TableRows) -> RowID? { diff --git a/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift b/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift index 2dda435413..9c9b765f8d 100644 --- a/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift +++ b/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift @@ -103,12 +103,25 @@ enum DataWriteExecutor { nonisolated static func run( _ plan: DataWritePlan, mode: PluginTransactionAccessMode = .readWrite, + commitMode: ConnectionCommitMode, on driver: DatabaseDriver, context: PluginStatementContext? = nil ) async throws -> DataWriteRun { let steps = plan.steps.filter { !$0.statement.sql.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } guard !steps.isEmpty || !plan.prologue.isEmpty else { return DataWriteRun(results: [], sideStatements: []) } + // A write Manual Commit can't hold would commit with no Commit pressed, so it is refused before anything runs. + if commitMode == .manual { + guard plan.prologue.isEmpty, plan.epilogue.isEmpty else { throw ManualCommitSessionOptionsRefusal() } + // On MySQL and Oracle a staged TRUNCATE or DROP commits the cell edits saved beside it. + let sql = steps.map(\.statement.sql) + if ManualCommit.mixesImplicitCommit(sql, databaseType: plan.databaseType, grammar: plan.databaseType.lexicalGrammar) { + throw ManualCommitMixedRefusal() + } + let state = await driver.heldSessionTransactionState() + try ManualCommit.refuseUnheldWrite(supportsTransactions: driver.supportsTransactions, sessionState: state) + } + // A row a lookup cannot find on its own refuses the save before anything is written. Each step reads its // lookup again right before it runs, since the steps ahead of it can move or change its rows. for step in steps { @@ -154,17 +167,18 @@ enum DataWriteExecutor { } } - /// Asked inside the same lease that runs the statements, so nothing can open a transaction - /// between the answer and the first write. + // Asked inside the lease that runs the statements, so nothing opens a transaction before the first write. let owner = WriteTransactionOwner.resolve( supportsTransactions: driver.supportsTransactions, - sessionState: await driver.heldSessionTransactionState() + sessionState: await driver.heldSessionTransactionState(), + commitMode: commitMode ) var results: [DataWriteStepResult] = [] do { - if owner.opensTransaction { + if owner.sendsBegin { try await driver.beginTransaction(mode: mode) + if owner == .held { ManualCommit.turnHold?.mark(true) } } for step in steps { @@ -217,14 +231,15 @@ enum DataWriteExecutor { } } - if owner.opensTransaction { + if owner.commitsAtEnd { try await driver.commitTransaction() } } catch { var rollbackSucceeded = owner.canRollBack - if owner.opensTransaction { + if owner.canRollBack { do { try await driver.rollbackTransaction() + if owner == .held { ManualCommit.turnHold?.mark(false) } } catch { rollbackSucceeded = false logger.error("Rollback failed: \(error.publicLogShape, privacy: .public)") @@ -242,7 +257,7 @@ enum DataWriteExecutor { committed: results, totalStatements: steps.count, engine: plan.databaseType.rawValue, - disposition: owner == .session ? .pendingInSessionTransaction : .written, + disposition: owner == .session || owner == .held ? .pendingInSessionTransaction : .written, underlying: error ) } @@ -370,7 +385,7 @@ enum DataWriteExecutor { actual: Int ) -> DataWriteError { switch owner { - case .app: + case .app, .held: return .tooManyRowsAffected(table: table, expected: expected, actual: actual) case .session: return .tooManyRowsAffectedInSessionTransaction(table: table, expected: expected, actual: actual) diff --git a/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift b/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift index 3686d2ca50..a9ffd233ec 100644 --- a/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift +++ b/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift @@ -178,6 +178,7 @@ struct RewindExecutor { ) let route = DatabaseManager.shared.executionRoute(for: scope) + let commitMode = DatabaseManager.shared.commitMode(for: connection.id) let run = try await ExecutionGateProvider.shared.authorizing( OperationRequest( connectionId: connection.id, @@ -194,7 +195,7 @@ struct RewindExecutor { route: route, cancellation: .protectedWrite ) { driver in - try await DataWriteExecutor.run(writePlan, on: driver) + try await DataWriteExecutor.run(writePlan, commitMode: commitMode, on: driver) } } diff --git a/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift b/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift new file mode 100644 index 0000000000..1003f7dae2 --- /dev/null +++ b/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift @@ -0,0 +1,128 @@ +// +// AgentTransactionGuard.swift +// TablePro +// + +import Foundation +import os +import TableProPluginKit +import TableProSQLGrammar + +// What an agent's statement or script may do to the transaction its session holds, decided inside the lease. +internal struct AgentTransactionGuard: Sendable { + nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "AgentTransactionGuard") + internal static let savepointStatement = "SAVEPOINT tablepro_agent" + internal static let releaseStatement = "RELEASE SAVEPOINT tablepro_agent" + internal static let rollbackStatement = "ROLLBACK TO SAVEPOINT tablepro_agent" + + let commitMode: ConnectionCommitMode + let databaseType: DatabaseType + let grammar: SQLLexicalGrammar + let connectionId: UUID + + internal enum Protection: Equatable { + case none + case savepoint + case refuse + } + + // A failed statement inside an open block aborts it on these engines, which takes the user's pending work with it. + nonisolated internal static func protection( + family: TransactionEngineFamily, + sessionState: PluginSessionTransactionState, + controlsTransaction: Bool + ) -> Protection { + guard !controlsTransaction, sessionState == .inTransaction, family.failedStatementAbortsTransaction else { + return .none + } + return family.takesBackAFailedStatement ? .savepoint : .refuse + } + + // Runs `body`, told whether this call opened the hold, and answers what the reply should say about the transaction. + nonisolated func running( + _ statements: [String], + writes: Bool, + on driver: DatabaseDriver, + _ body: (_ openedHold: Bool) async throws -> T + ) async throws -> (value: T, notice: String?) { + let controls = ManualCommit.controlsTransaction(statements, databaseType: databaseType, grammar: grammar) + if commitMode == .manual, controls { throw ManualCommitTransactionControlRefusal() } + let family = TransactionEngineFamily.of(databaseType) + let unseen = ManualCommit.mayEndTransactionUnseen(statements, databaseType: databaseType, grammar: grammar) + let readsState = commitMode == .manual || controls || unseen || family.failedStatementAbortsTransaction + let startState: PluginSessionTransactionState = readsState ? await driver.heldSessionTransactionState() : .unknown + // MySQL's START TRANSACTION commits the transaction already open; PostgreSQL's BEGIN warns and joins it. + if controls, startState.holdsUserTransaction, + ManualCommit.startsTransaction(statements, databaseType: databaseType, grammar: grammar) { + throw OpenTransactionRefusal() + } + if commitMode == .manual, startState.holdsUserTransaction, unseen { + throw ManualCommitTransactionControlRefusal() + } + // An agent cannot answer the prompt the editor shows before an implicit commit. + if commitMode == .manual, startState.holdsUserTransaction, + ManualCommit.commitsHeldTransaction(statements, databaseType: databaseType, grammar: grammar) { + throw ManualCommitDDLRefusal() + } + let protection = Self.protection(family: family, sessionState: startState, controlsTransaction: controls) + if protection == .refuse { throw AbortingTransactionRefusal() } + + let opened = try await ManualCommit.openIfNeeded( + on: driver, statements: statements, databaseType: databaseType, grammar: grammar, commitMode: commitMode + ) + let value: T + do { + if protection == .savepoint { + value = try await savepointed(on: driver) { try await body(opened) } + } else { + value = try await body(opened) + } + } catch { + if opened { await ManualCommit.rollBackOpened(on: driver, connectionId: connectionId) } + throw error + } + let pendingMatters = commitMode == .manual && writes + guard pendingMatters || startState.holdsUserTransaction else { return (value, nil) } + let endState = await driver.heldSessionTransactionState() + return (value, Self.notice(startState: startState, endState: endState, pendingMatters: pendingMatters)) + } + + nonisolated internal static func notice( + startState: PluginSessionTransactionState, + endState: PluginSessionTransactionState, + pendingMatters: Bool + ) -> String? { + if startState.holdsUserTransaction, endState == .idle { return ManualCommit.transactionEndedNotice } + return pendingMatters && endState.holdsUserTransaction ? ManualCommit.pendingNotice : nil + } + + nonisolated private func savepointed(on driver: DatabaseDriver, _ body: () async throws -> T) async throws -> T { + _ = try await driver.execute(query: Self.savepointStatement) + let value: T + do { + value = try await body() + } catch { + await Self.rollBackToSavepoint(on: driver, connectionId: connectionId) + throw error + } + do { + _ = try await driver.execute(query: Self.releaseStatement) + } catch { + Self.logger.warning("Releasing the agent savepoint failed: \(error.publicLogShape, privacy: .public)") + } + return value + } + + @MainActor + private static func rollBackToSavepoint(on driver: DatabaseDriver, connectionId: UUID) async { + let token = DatabaseManager.shared.beginProtectedWrite(on: driver, for: connectionId) + defer { DatabaseManager.shared.endProtectedWrite(token, for: connectionId) } + do { + try await TaskCancellationShield.run { @Sendable in + _ = try await driver.execute(query: Self.rollbackStatement) + } + } catch { + logger.error("Rolling back to the agent savepoint failed: \(error.publicLogShape, privacy: .public)") + } + } +} diff --git a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift index fb726e0bd5..8e615c0a02 100644 --- a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift +++ b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift @@ -114,6 +114,13 @@ extension DatabaseAccessBridge { let manager = await databaseManager let route = await MainActor.run { manager.executionRoute(for: scope) } + let commitMode = await MainActor.run { manager.commitMode(for: connectionId) } + let statementTexts = batches.flatMap { $0.statements.map(\.sql) } + let agentGuard = AgentTransactionGuard( + commitMode: commitMode, databaseType: databaseType, grammar: databaseType.lexicalGrammar, + connectionId: connectionId + ) + let writes = classification.tier != .safe let startLines = BatchErrorText.lines(of: batches.map(\.range.location), in: text) let startTime = CFAbsoluteTimeGetCurrent() let statementsRan = CatalogEvent.statementsRan( @@ -134,9 +141,15 @@ extension DatabaseAccessBridge { onLateCompletion: { CatalogChangeService.post(statementsRan) } ) { driver in reach.mark() - return try await ScriptBatchRun.run( - batches, startLines: startLines, rowCap: rowCap, driver: driver, progress: progress, context: context - ) + let ran = try await agentGuard.running(statementTexts, writes: writes, on: driver) { opened in + try await ScriptBatchRun.run( + batches, startLines: startLines, rowCap: rowCap, driver: driver, progress: progress, + context: context, commitMode: commitMode, heldByApp: opened + ) + } + var run = ran.value + run.transactionNotice = ran.notice + return run } } catch { if classification.tier != .safe { @@ -222,6 +235,8 @@ final class ScriptBatchProgress: Sendable { struct ScriptBatchRun: Sendable { let answers: [QueryBatchResult] let sessionState: PluginSessionTransactionState + // What the agent is told instead of "commit or roll it back": the work is pending, or the transaction ended. + var transactionNotice: String? /// The session is asked what it holds before and after, as the editor asks: the first answer decides how a /// failure reads, and the second whether the script left a transaction open. A batch that raised an error ends the @@ -232,11 +247,15 @@ struct ScriptBatchRun: Sendable { rowCap: Int, driver: DatabaseDriver, progress: ScriptBatchProgress, - context: PluginStatementContext? = nil + context: PluginStatementContext? = nil, + commitMode: ConnectionCommitMode = .automatic, + heldByApp: Bool = false ) async throws -> ScriptBatchRun { let startState = await driver.heldSessionTransactionState() progress.begin(in: startState) - let plan = BatchTransactionPlan.autocommit.joining(startState) + let plan: BatchTransactionPlan = heldByApp + ? .heldTransaction + : BatchTransactionPlan.autocommit.joining(startState, commitMode: commitMode) var answers: [QueryBatchResult] = [] for (batch, startLine) in zip(batches, startLines) { try Task.checkCancellation() @@ -298,9 +317,10 @@ struct ScriptBatchRun: Sendable { primary.columnOrigins = first?.columnOrigins let notes = [ first?.statusMessage, + transactionNotice, BatchRunNotice.text( discardedResultSetCount: answers.reduce(0) { $0 + $1.discardedResultSetCount }, - sessionState: sessionState + sessionState: transactionNotice == nil ? sessionState : .idle ) ].compactMap { $0 } primary.statusMessage = notes.isEmpty ? nil : notes.joined(separator: " ") diff --git a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift index d3577f51c1..2504c3ca6c 100644 --- a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift +++ b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift @@ -397,16 +397,22 @@ internal actor DatabaseAccessBridge { let manager = await databaseManager let route = await MainActor.run { manager.executionRoute(for: scope) } + let commitMode = await MainActor.run { manager.commitMode(for: connectionId) } + let agentGuard = AgentTransactionGuard( + commitMode: commitMode, databaseType: databaseType, grammar: grammar, connectionId: connectionId + ) + let writes = classification.tier != .safe let startTime = CFAbsoluteTimeGetCurrent() let statementRan = CatalogEvent.statementsRan( connectionId: connectionId, statements: [sql], databaseType: databaseType ) // A write that timed out or failed may still have committed, so the catalog is refreshed rather than trusted. - let result: QueryResult + var result: QueryResult let tableEdits: SucceededStatements? + let transactionNotice: String? do { - (result, tableEdits) = try await runRacingTimeout( + (result, tableEdits, transactionNotice) = try await runRacingTimeout( scope: scope, route: route, policy: policy, @@ -415,16 +421,18 @@ internal actor DatabaseAccessBridge { onLateCompletion: { CatalogChangeService.post(statementRan) } ) { driver in reach.mark() - let answer = try await driver.executeUserQuery( - query: sql, - rowCap: fetchCap, - parameters: statement.parameters.isEmpty ? nil : statement.boundValues, - context: context - ) + let ran = try await agentGuard.running([sql], writes: writes, on: driver) { _ in + try await driver.executeUserQuery( + query: sql, + rowCap: fetchCap, + parameters: statement.parameters.isEmpty ? nil : statement.boundValues, + context: context + ) + } let edits = await SucceededStatements.single( sql, scope: scope, databaseType: databaseType, grammar: grammar, ranOn: driver ) - return (answer, edits) + return (ran.value, edits, ran.notice) } } catch { if classification.tier != .safe { @@ -437,6 +445,9 @@ internal actor DatabaseAccessBridge { if let tableEdits { CatalogChangeService.post(.statementsSucceeded(tableEdits)) } + if let transactionNotice { + result.statusMessage = [result.statusMessage, transactionNotice].compactMap { $0 }.joined(separator: " ") + } let executionTimeMs = (CFAbsoluteTimeGetCurrent() - startTime) * 1_000 let rules = await maskingPolicy(for: connectionId) let applied = await MainActor.run { masking.resolved(in: scope, databaseType: databaseType) } @@ -630,6 +641,10 @@ internal actor DatabaseAccessBridge { guard supported else { throw DatabaseAccessError.dataSourceError(String(localized: "This engine does not support transactions.")) } + // The user commits a Manual Commit connection's work; an agent's begin, commit or rollback would end or bury it. + guard await MainActor.run(body: { manager.commitMode(for: scope.connectionId) }) == .automatic else { + throw DatabaseAccessError.dataSourceError(ManualCommit.transactionControlRefusalMessage) + } try await manager.withScopedDriver( scope: scope, route: .sessionDriver, @@ -637,7 +652,12 @@ internal actor DatabaseAccessBridge { ) { driver in reach.mark() switch action { - case "begin": try await driver.beginTransaction(mode: .readWrite) + case "begin": + // MySQL's START TRANSACTION commits a transaction already open; PostgreSQL's BEGIN warns and joins it. + guard await driver.heldSessionTransactionState() == .idle else { + throw DatabaseAccessError.dataSourceError(ManualCommit.openTransactionRefusalMessage) + } + try await driver.beginTransaction(mode: .readWrite) case "commit": try await driver.commitTransaction() case "rollback": try await driver.rollbackTransaction() default: @@ -729,7 +749,7 @@ internal actor DatabaseAccessBridge { ) { driver in reach.mark() try await before(driver, ownsConnection) - return try await DataWriteExecutor.run(plan, on: driver, context: context) + return try await DataWriteExecutor.run(plan, commitMode: .automatic, on: driver, context: context) } } @@ -968,7 +988,8 @@ internal enum ServerErrorText { // The app's own refusals, timeouts and cancels name no value; whatever a driver or a server wrote might. static func mayRepeatAValue(_ error: Error) -> Bool { switch error { - case is CancellationError, is DatabaseAccessError, is ExternalStatementGateError, is ShortcutsActionError: + case is CancellationError, is DatabaseAccessError, is ExternalStatementGateError, is ShortcutsActionError, + is any ManualCommitRefusing: return false case let failure as DatabaseError: if case .queryFailed = failure { return true } diff --git a/apple/TablePro/Core/Database/DatabaseDriver.swift b/apple/TablePro/Core/Database/DatabaseDriver.swift index 5bae9e93a9..7b0a216512 100644 --- a/apple/TablePro/Core/Database/DatabaseDriver.swift +++ b/apple/TablePro/Core/Database/DatabaseDriver.swift @@ -348,6 +348,11 @@ protocol DatabaseDriver: AnyObject, Sendable { /// Fetch the source of one trigger. func fetchTriggerDDL(_ trigger: TriggerInfo) async throws -> String + // Mirrors `PluginDatabaseDriver.serverObjectKinds`: asked of the live driver, never of the type. + var serverObjectKinds: [PluginServerObjectKind] { get } + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [ServerObjectInfo] + func fetchServerObject(_ object: ServerObjectInfo) async throws -> ServerObjectInfo + /// Fetch metadata for a specific database (table count, size, etc.) func fetchDatabaseMetadata(_ database: String) async throws -> DatabaseMetadata @@ -1032,6 +1037,17 @@ extension DatabaseDriver { throw PluginObjectSourceError.unsupported(trigger.name) } + var serverObjectKinds: [PluginServerObjectKind] { [] } + + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [ServerObjectInfo] { [] } + + func fetchServerObject(_ object: ServerObjectInfo) async throws -> ServerObjectInfo { + guard let definition = object.definition, !definition.isEmpty else { + throw PluginObjectSourceError.unsupported(object.name) + } + return object + } + var supportsTransactions: Bool { true } var hasLostConnection: Bool { false } diff --git a/apple/TablePro/Core/Database/DatabaseManager+Metadata.swift b/apple/TablePro/Core/Database/DatabaseManager+Metadata.swift index d10ea0b38b..643ff155dc 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Metadata.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Metadata.swift @@ -16,6 +16,12 @@ protocol ScopedMetadataProviding: AnyObject, Sendable { _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> T + func withMetadataRead( + scope: DatabaseScope, + workload: MetadataConnectionPool.Workload, + _ body: @Sendable @escaping (DatabaseDriver) async throws -> T + ) async throws -> T + func browseScope(for connectionId: UUID) -> DatabaseScope? } @@ -27,6 +33,15 @@ extension ScopedMetadataProviding { try await withMetadataDriver(scope: scope, workload: .interactive, body) } + // A conformer with no session turns has no transaction revision to keep. + func withMetadataRead( + scope: DatabaseScope, + workload: MetadataConnectionPool.Workload, + _ body: @Sendable @escaping (DatabaseDriver) async throws -> T + ) async throws -> T { + try await withMetadataDriver(scope: scope, workload: workload, body) + } + /// For reads that belong to the sidebar rather than to a tab: the object list, the /// database and quick switchers, and the AI schema context. Completion is not one of /// them any more: a query tab completes against its own scope. @@ -38,7 +53,7 @@ extension ScopedMetadataProviding { guard let scope = browseScope(for: connectionId) else { throw DatabaseError.notConnected } - return try await withMetadataDriver(scope: scope, workload: workload, body) + return try await withMetadataRead(scope: scope, workload: workload, body) } } @@ -59,4 +74,21 @@ extension DatabaseManager { body ) } + + // Reads only: on the session driver its turn leaves an open transaction's revision alone, unless the state moved. + func withMetadataRead( + scope: DatabaseScope, + workload: MetadataConnectionPool.Workload = .interactive, + _ body: @Sendable @escaping (DatabaseDriver) async throws -> T + ) async throws -> T { + try await withScopedDriver( + scope: scope, + route: metadataRoute(for: scope), + workload: workload, + cancellation: .untracked, + ownsSessionState: false, + readsOnly: true, + body + ) + } } diff --git a/apple/TablePro/Core/Database/DatabaseManager+OpenTransactions.swift b/apple/TablePro/Core/Database/DatabaseManager+OpenTransactions.swift index cdd8268df7..6739a615f7 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+OpenTransactions.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+OpenTransactions.swift @@ -8,10 +8,8 @@ import Foundation import TableProPluginKit extension DatabaseManager { - /// A connection running a turn is skipped, because asking it waits for that turn. One that has - /// not answered within a second counts as holding nothing, so a close never hangs on a busy or - /// dead server. An aborted transaction counts: a savepoint taken before the failure can still - /// bring its earlier work back to a commit. + // A busy lane is not asked and a slow one counts as idle, so a close never hangs; what the tracker + // last read covers both. An aborted transaction counts: a savepoint can still bring earlier work back. internal func databasesHoldingTransaction( for connectionId: UUID, among databases: Set? = nil, @@ -37,6 +35,7 @@ extension DatabaseManager { state: Task { await Self.boundedTransactionState(of: candidate.driver, clock: clock) } ) } + let browsedDatabase = session.resolvedBrowseDatabase var holding: [String] = [] for read in reads { let state = await read.state.value @@ -44,6 +43,12 @@ extension DatabaseManager { holding.append(read.database) } } + let tracked = sessionTransactions.holds(for: connectionId) + .map { $0.key.lane ?? browsedDatabase } + .filter { databases?.contains($0) ?? true } + for database in tracked where !holding.contains(database) { + holding.append(database) + } return holding } diff --git a/apple/TablePro/Core/Database/DatabaseManager+Principals.swift b/apple/TablePro/Core/Database/DatabaseManager+Principals.swift index 12680f123e..d3b63817f1 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Principals.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Principals.swift @@ -79,15 +79,14 @@ extension DatabaseManager { rollsBack: Bool, connectionId: UUID ) async throws { - /// No transaction is opened over one the session already holds: on this shared session an - /// app-owned `COMMIT` commits the user's pending work, and MySQL's `START TRANSACTION` - /// commits it implicitly. Joining it instead leaves the statements pending, which is what - /// the failure then reports. + // Joins a transaction the session holds: an app COMMIT, or MySQL's START TRANSACTION, would commit the user's work. + // `.automatic`: this runs outside a session turn, so a hold it opened would never reach the bar. let owner = WriteTransactionOwner.resolve( supportsTransactions: driver.supportsTransactions, - sessionState: await driver.heldSessionTransactionState() + sessionState: await driver.heldSessionTransactionState(), + commitMode: .automatic ) - let useTransaction = owner.opensTransaction && rollsBack + let useTransaction = owner.sendsBegin && owner.commitsAtEnd && rollsBack if useTransaction { try await driver.beginTransaction(mode: .readWrite) } diff --git a/apple/TablePro/Core/Database/DatabaseManager+Schema.swift b/apple/TablePro/Core/Database/DatabaseManager+Schema.swift index 01ab285459..f08b0e99c2 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Schema.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Schema.swift @@ -42,7 +42,8 @@ extension DatabaseManager { _ script: SchemaChangeScript, databaseType: DatabaseType, scope: DatabaseScope, - gate: any ExecutionGate = ExecutionGateProvider.shared + gate: any ExecutionGate = ExecutionGateProvider.shared, + holdConfirmation: SchemaChangeHoldConfirmation = DatabaseManager.confirmSchemaChangeBesideHold ) async throws { let route = schemaChangeRoute(for: scope) let statements = script.statements @@ -52,6 +53,8 @@ extension DatabaseManager { if let denial = await gate.authorize(request).denialError { throw denial } + try await confirmSchemaChange(on: route, connectionId: scope.connectionId, confirmation: holdConfirmation) + let isPooled = route.isPooled let executionTimes: [TimeInterval] do { @@ -61,7 +64,7 @@ extension DatabaseManager { cancellation: .protectedWrite ) { driver in try await Self.refuseBeforeWriting(script, scope: scope, on: driver) - let useTransaction = Self.wrapsDDLInTransaction(driver) + let useTransaction = await Self.wrapsSchemaChange(on: driver, isPooled: isPooled) if useTransaction { try await driver.beginTransaction(mode: schemaKind.declaresWrite ? .readWrite : .serverDefault) } @@ -231,10 +234,12 @@ extension DatabaseManager { statements: [String], databaseType: DatabaseType, scope: DatabaseScope, - gate: any ExecutionGate = ExecutionGateProvider.shared + gate: any ExecutionGate = ExecutionGateProvider.shared, + holdConfirmation: SchemaChangeHoldConfirmation = DatabaseManager.confirmSchemaChangeBesideHold ) async throws { guard !statements.isEmpty else { return } let route = schemaChangeRoute(for: scope) + let isPooled = route.isPooled let script = statements.map { $0.hasSuffix(";") ? $0 : $0 + ";" }.joined(separator: "\n\n") let authorization = await gate.authorize( @@ -249,6 +254,7 @@ extension DatabaseManager { ) ) if let denial = authorization.denialError { throw denial } + try await confirmSchemaChange(on: route, connectionId: scope.connectionId, confirmation: holdConfirmation) let executionTimes: [TimeInterval] do { @@ -257,7 +263,8 @@ extension DatabaseManager { route: route, cancellation: .protectedWrite ) { driver in - let useTransaction = Self.wrapsDDLInTransaction(driver) && statements.count > 1 + let wraps = await Self.wrapsSchemaChange(on: driver, isPooled: isPooled) + let useTransaction = wraps && statements.count > 1 if useTransaction { try await driver.beginTransaction(mode: .readWrite) } diff --git a/apple/TablePro/Core/Database/DatabaseManager+SchemaChangeHold.swift b/apple/TablePro/Core/Database/DatabaseManager+SchemaChangeHold.swift new file mode 100644 index 0000000000..69fd63fac1 --- /dev/null +++ b/apple/TablePro/Core/Database/DatabaseManager+SchemaChangeHold.swift @@ -0,0 +1,40 @@ +// +// DatabaseManager+SchemaChangeHold.swift +// TablePro +// + +import AppKit +import Foundation + +typealias SchemaChangeHoldConfirmation = @MainActor @Sendable () async -> Bool + +extension DatabaseManager { + // On its own connection a schema change queues behind the open transaction's locks, and Stop cannot reach it. + static let confirmSchemaChangeBesideHold: SchemaChangeHoldConfirmation = { + await AlertHelper.confirm( + title: String(localized: "A Transaction Is Open"), + message: String( + localized: "This change runs on its own connection and can wait until you commit or roll back the open transaction." + ), + confirmButton: String(localized: "Continue") + ) + } + + func confirmSchemaChange( + on route: ScopedDriverRoute, + connectionId: UUID, + confirmation: SchemaChangeHoldConfirmation + ) async throws { + guard route.isPooled, !sessionTransactions.holds(for: connectionId).isEmpty else { return } + guard await confirmation() else { + throw ExecutionGateError.cancelledByUser(String(localized: "The change did not run.")) + } + } + + // A BEGIN on the session driver joins what a tab left open and its COMMIT ends that, so the DDL joins it instead. + nonisolated static func wrapsSchemaChange(on driver: DatabaseDriver, isPooled: Bool) async -> Bool { + guard wrapsDDLInTransaction(driver) else { return false } + guard !isPooled else { return true } + return await driver.heldSessionTransactionState().permitsAppTransaction + } +} diff --git a/apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift b/apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift index ad1ba9c387..4ba30ec5a3 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift @@ -116,6 +116,7 @@ extension DatabaseManager { workload: MetadataConnectionPool.Workload = .interactive, cancellation: DriverCancellationPolicy, ownsSessionState: Bool = true, + readsOnly: Bool = false, _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> T { let leased = trackedLease(for: scope.connectionId, cancellation: cancellation, body) @@ -127,7 +128,9 @@ extension DatabaseManager { scope: scope, workload: workload, leased ) case .sessionDriver: - return try await withPinnedSessionDriver(scope: scope, ownsSessionState: ownsSessionState, leased) + return try await withPinnedSessionDriver( + scope: scope, ownsSessionState: ownsSessionState, readsOnly: readsOnly, leased + ) } } @@ -138,6 +141,7 @@ extension DatabaseManager { route: ScopedDriverRoute, workload: MetadataConnectionPool.Workload = .interactive, ownsSessionState: Bool = true, + readsOnly: Bool = false, _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> T { let owner = DriverLeaseOwner() @@ -149,6 +153,7 @@ extension DatabaseManager { workload: workload, cancellation: .cancellableRead(owner), ownsSessionState: ownsSessionState, + readsOnly: readsOnly, body ) } onCancel: { @@ -173,7 +178,7 @@ extension DatabaseManager { guard rowCountRunsWithPage(scope, filtered: filtered) else { return try await withMetadataDriver(scope: scope, workload: .bulk, body) } - return try await withCancellableRead(scope: scope, route: tableReadRoute(for: scope), body) + return try await withCancellableRead(scope: scope, route: tableReadRoute(for: scope), readsOnly: true, body) } /// A table tab's read is a SELECT the app built from the tab's own table, so it depends on @@ -372,9 +377,10 @@ extension DatabaseManager { private func withPinnedSessionDriver( scope: DatabaseScope, ownsSessionState: Bool, + readsOnly: Bool, _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> T { - try await withSessionDriverTurn(scope: scope, ownsSessionState: ownsSessionState) { driver in + try await withSessionDriverTurn(scope: scope, ownsSessionState: ownsSessionState, readsOnly: readsOnly) { driver in try await pin(driver, to: scope) return try await body(driver) } @@ -387,7 +393,7 @@ extension DatabaseManager { scope: DatabaseScope, _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> TableReadTurn { - try await withSessionDriverTurn(scope: scope, ownsSessionState: false) { driver in + try await withSessionDriverTurn(scope: scope, ownsSessionState: false, readsOnly: true) { driver in let route = tableReadRoute(for: scope) guard route == .sessionDriver else { return .moved(route) } try await pin(driver, to: scope) @@ -401,6 +407,7 @@ extension DatabaseManager { private func withSessionDriverTurn( scope: DatabaseScope, ownsSessionState: Bool, + readsOnly: Bool = false, _ turn: (DatabaseDriver) async throws -> R ) async throws -> R { let connectionId = scope.connectionId @@ -442,7 +449,9 @@ extension DatabaseManager { if ownsSessionState, let lost = sessionLanes.takeAnyTransactionLoss(for: connectionId) { throw DatabaseError.connectionFailed(Self.parkedTransactionLostMessage(database: lost)) } - return try await recordingTransaction(on: browsedDriver, for: .init(connectionId: connectionId, lane: nil)) { + return try await recordingTransaction( + on: browsedDriver, for: .init(connectionId: connectionId, lane: nil), readsOnly: readsOnly + ) { try await turn(browsedDriver) } } @@ -464,7 +473,9 @@ extension DatabaseManager { } sessionLanes.noteOpenedUse(database: lane, for: connectionId) defer { sessionLanes.noteOpenedUse(database: lane, for: connectionId) } - return try await recordingTransaction(on: laneDriver, for: .init(connectionId: connectionId, lane: lane)) { + return try await recordingTransaction( + on: laneDriver, for: .init(connectionId: connectionId, lane: lane), readsOnly: readsOnly + ) { try await turn(laneDriver) } } @@ -477,17 +488,20 @@ extension DatabaseManager { private func recordingTransaction( on driver: DatabaseDriver, for key: SessionTransactionTracker.Key, + readsOnly: Bool, _ work: () async throws -> R ) async throws -> R { guard driver.supportsTransactions else { return try await work() } + let turnHold = ManualCommitTurnHold() let outcome: Result do { - outcome = .success(try await work()) + outcome = .success(try await ManualCommit.$turnHold.withValue(turnHold) { try await work() }) } catch { outcome = .failure(error) } - let state = await Self.boundedTransactionState(of: driver, clock: ContinuousClock()) - sessionTransactions.record(state, on: driver, for: key) + let read = await Self.boundedTransactionState(of: driver, clock: ContinuousClock()) + let state = ManualCommit.recordedState(read: read, turnOpenedHold: turnHold.isHeld) + sessionTransactions.record(state, on: driver, for: key, readsOnly: readsOnly) return try outcome.get() } diff --git a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift index 66dcd2fad0..ea925a1f50 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift @@ -151,6 +151,7 @@ extension DatabaseManager { from: connection, preservingSessionFieldsFrom: session.connection ) + session.commitMode = session.connection.commitMode session.driver = driver session.status = driver.status session.effectiveConnection = effectiveConnection @@ -345,6 +346,7 @@ extension DatabaseManager { guard let stored = connectionStorage.loadConnection(id: connection.id) else { return connection } var resolved = connection resolved.preferredSafeModeLevel = stored.preferredSafeModeLevel + resolved.commitMode = stored.commitMode return resolved } @@ -913,6 +915,7 @@ extension DatabaseManager { let stored = connectionStorage.loadConnection(id: id) else { continue } adoptDisplayFields(from: stored, into: session, for: id) setSafeModeLevel(stored.preferredSafeModeLevel, for: id) + setCommitMode(stored.commitMode, for: id) } } @@ -946,6 +949,7 @@ extension DatabaseManager { ) -> DatabaseConnection { var adopted = connection(attempted, adoptingDisplayFieldsFrom: current) adopted.preferredSafeModeLevel = current.preferredSafeModeLevel + adopted.commitMode = current.commitMode return adopted } @@ -1001,6 +1005,20 @@ extension DatabaseManager { _ = connectionStorage.updateSafeModeLevel(level, for: connectionId) } + // Written to the connection, as the Safe Mode level is, so the form and the Database menu are one setting. + func setCommitMode(_ mode: ConnectionCommitMode, for connectionId: UUID) { + guard var session = activeSessions[connectionId] else { return } + guard session.connection.commitMode != mode || session.commitMode != mode else { return } + session.connection.commitMode = mode + session.commitMode = mode + setSession(session, for: connectionId) + connectionStorage.mutateConnections(ids: [connectionId]) { $0.commitMode = mode } + } + + func commitMode(for connectionId: UUID) -> ConnectionCommitMode { + activeSessions[connectionId]?.commitMode ?? .automatic + } + internal func setSession(_ session: ConnectionSession, for connectionId: UUID) { /// A session created while a window is already in Agent mode takes the level stored on the /// connection, which is the user's own and may be Silent. Applying the floor only when the diff --git a/apple/TablePro/Core/Database/SessionTransactionTracker.swift b/apple/TablePro/Core/Database/SessionTransactionTracker.swift index 1cd91819ea..1471a74411 100644 --- a/apple/TablePro/Core/Database/SessionTransactionTracker.swift +++ b/apple/TablePro/Core/Database/SessionTransactionTracker.swift @@ -28,8 +28,7 @@ internal final class SessionTransactionTracker: ObservableObject { internal struct Hold: Identifiable, Equatable { let key: Key let state: State - /// Moves with every turn recorded on the lane, so an action confirmed after other work ran - /// there is refused rather than ending a transaction the user has not seen. + // Moves with every turn that could have changed the transaction, so a confirmation made before it is refused. let revision: Int var id: Key { key } @@ -43,10 +42,17 @@ internal final class SessionTransactionTracker: ObservableObject { var state: State } + // Whether a lane's driver ever answered with a definite state; a timed-out read keeps an earlier yes. + private struct Reader { + weak var driver: AnyObject? + var answers: Bool + } + @Published private(set) var holds: [Key: Hold] = [:] private var drivers: [Key: Entry] = [:] private var revisions: [Key: Int] = [:] + private var readers: [Key: Reader] = [:] private let isLive: @MainActor (Key, DatabaseDriver) -> Bool internal init(isLive: @escaping @MainActor (Key, DatabaseDriver) -> Bool) { @@ -64,6 +70,10 @@ internal final class SessionTransactionTracker: ObservableObject { transactionState(connectionId: connectionId, container: container) != nil } + internal func hold(connectionId: UUID, container: String) -> Hold? { + holds(for: connectionId).first { $0.key.lane == nil || $0.key.lane == container } + } + // An aborted hold wins, because it is the one that blocks every statement until a rollback. internal func transactionState(connectionId: UUID, container: String) -> State? { let states = holds.values @@ -73,7 +83,7 @@ internal final class SessionTransactionTracker: ObservableObject { return states.isEmpty ? nil : .open } - // Moves with every turn recorded on any of the connection's lanes. + // Moves with every turn on any of the connection's lanes, except a read-only one that changed nothing. internal func turnRevision(for connectionId: UUID) -> Int { revisions.reduce(0) { total, entry in entry.key.connectionId == connectionId ? total &+ entry.value : total @@ -89,33 +99,70 @@ internal final class SessionTransactionTracker: ObservableObject { holds[hold.key] == hold } - /// An engine that cannot say keeps what an earlier answer from the same driver said, since a - /// refresh never clears what it refreshes; from another driver it says nothing at all. - internal func record(_ state: PluginSessionTransactionState, on driver: DatabaseDriver, for key: Key) { + // An engine that cannot say keeps what an earlier answer from the same driver said. A read-only turn + // moves the revision only when the state it read differs, so a background read never voids a confirmation. + internal func record( + _ state: PluginSessionTransactionState, + on driver: DatabaseDriver, + for key: Key, + readsOnly: Bool = false + ) { // A turn can outlive its session's teardown; its late answer must not touch the replacement's hold. guard isLive(key, driver) else { if drivers[key]?.driver === driver { clear(key) } return } - revisions[key, default: 0] &+= 1 + noteReader(driver, answered: state != .unknown, for: key) + let next = heldState(after: state, from: driver, for: key) + let unchanged = next == drivers[key]?.state && (next == nil || drivers[key]?.driver === driver) + if !(readsOnly && unchanged) { + revisions[key, default: 0] &+= 1 + } + if let next { + set(next, on: driver, for: key) + } else { + clear(key) + } + } + + // True once the live driver of a lane the container runs on has only ever answered `.unknown`. + internal func cannotReportState(connectionId: UUID, container: String) -> Bool { + readers.contains { key, reader in + guard key.connectionId == connectionId, key.lane == nil || key.lane == container, + let driver = reader.driver as? DatabaseDriver, isLive(key, driver) + else { return false } + return !reader.answers + } + } + + private func heldState( + after state: PluginSessionTransactionState, + from driver: DatabaseDriver, + for key: Key + ) -> State? { switch state { case .inTransaction: - set(.open, on: driver, for: key) + return .open case .abortedTransaction: - set(.aborted, on: driver, for: key) + return .aborted case .idle, .holdsSessionLocks: - clear(key) + return nil case .unknown: - if drivers[key]?.driver !== driver { - clear(key) - } else if let entry = drivers[key] { - set(entry.state, on: driver, for: key) - } + guard let entry = drivers[key], entry.driver === driver else { return nil } + return entry.state @unknown default: - clear(key) + return nil } } + private func noteReader(_ driver: DatabaseDriver, answered: Bool, for key: Key) { + if let reader = readers[key], reader.driver === driver { + if answered { readers[key]?.answers = true } + return + } + readers[key] = Reader(driver: driver, answers: answered) + } + /// Drops every hold whose connection is not the lane's live one any more: a disconnect, a /// reconnect onto a new driver, a closed entry or a lost parked connection all end the /// transaction on the server. @@ -124,6 +171,10 @@ internal final class SessionTransactionTracker: ObservableObject { if let driver = entry.driver as? DatabaseDriver, isLive(key, driver) { continue } clear(key) } + for (key, reader) in readers where key.connectionId == connectionId { + if let driver = reader.driver as? DatabaseDriver, isLive(key, driver) { continue } + readers.removeValue(forKey: key) + } } private func set(_ state: State, on driver: DatabaseDriver, for key: Key) { diff --git a/apple/TablePro/Core/Database/TriggerEditing.swift b/apple/TablePro/Core/Database/TriggerEditing.swift index 8a427c1bfd..4f7130d2db 100644 --- a/apple/TablePro/Core/Database/TriggerEditing.swift +++ b/apple/TablePro/Core/Database/TriggerEditing.swift @@ -128,7 +128,8 @@ enum TriggerEditing { isEdit: Bool, originalName: String?, originalDefinition: String?, - gate: any ExecutionGate = ExecutionGateProvider.shared + gate: any ExecutionGate = ExecutionGateProvider.shared, + holdConfirmation: SchemaChangeHoldConfirmation = DatabaseManager.confirmSchemaChangeBesideHold ) async throws { let plan = try await DatabaseManager.shared.withMetadataDriver(scope: scope) { driver in TriggerApplyPlan( @@ -159,6 +160,11 @@ enum TriggerEditing { ) ) if let denial = decision.denialError { throw denial } + let route = DatabaseManager.shared.schemaChangeRoute(for: scope) + try await DatabaseManager.shared.confirmSchemaChange( + on: route, connectionId: connection.id, confirmation: holdConfirmation + ) + let isPooled = route.isPooled let startedAt = Date() defer { @@ -167,10 +173,13 @@ enum TriggerEditing { ) } do { - try await withSchemaChangeDriver(scope: scope) { driver in + try await withSchemaChangeDriver(scope: scope, route: route) { driver in switch plan.strategy { case let .transactional(dropFirst): - try await runInTransaction(driver: driver, dropSQL: dropFirst ? plan.dropSQL : nil, sql: sql) + let wraps = await DatabaseManager.wrapsSchemaChange(on: driver, isPooled: isPooled) + try await runInTransaction( + driver: driver, dropSQL: dropFirst ? plan.dropSQL : nil, sql: sql, wraps: wraps + ) case .dropThenCreate: guard let dropSQL = plan.dropSQL, let restore else { throw TriggerEditingError.dropUnavailable } try await runDropThenCreate(driver: driver, dropSQL: dropSQL, sql: sql, rollback: restore) @@ -260,17 +269,24 @@ enum TriggerEditing { private static func withSchemaChangeDriver( scope: DatabaseScope, + route: ScopedDriverRoute? = nil, _ body: @Sendable @escaping (DatabaseDriver) async throws -> Void ) async throws { try await DatabaseManager.shared.withScopedDriver( scope: scope, - route: DatabaseManager.shared.schemaChangeRoute(for: scope), + route: route ?? DatabaseManager.shared.schemaChangeRoute(for: scope), cancellation: .protectedWrite, body ) } - static func runInTransaction(driver: DatabaseDriver, dropSQL: String?, sql: String) async throws { + // Unwrapped, the edit joins the transaction the session holds and is taken back with it. + static func runInTransaction(driver: DatabaseDriver, dropSQL: String?, sql: String, wraps: Bool = true) async throws { + guard wraps else { + if let dropSQL { _ = try await driver.execute(query: dropSQL) } + _ = try await driver.execute(query: sql) + return + } try await driver.beginTransaction(mode: .readWrite) do { if let dropSQL { _ = try await driver.execute(query: dropSQL) } diff --git a/apple/TablePro/Core/Events/AppEvents.swift b/apple/TablePro/Core/Events/AppEvents.swift index eb2894539b..7ecc6291ca 100644 --- a/apple/TablePro/Core/Events/AppEvents.swift +++ b/apple/TablePro/Core/Events/AppEvents.swift @@ -13,32 +13,26 @@ final class AppEvents { // MARK: - Theme & Accessibility - /// Carries the palette revision and the effective appearance. A consumer that snapshots - /// colours or fonts needs both: a forced Light/Dark switch never changes the theme, and a - /// theme switch never changes the appearance. Subscribing reaches a view that is alive but - /// detached from every window, which a walk of the window tree would miss. + // Both the revision and the appearance: a forced Light/Dark switch never changes the theme, and the reverse. let themeChanged = PassthroughSubject() let accessibilityTextSizeChanged = PassthroughSubject() // MARK: - System Environment - /// The Mac's time zone moved. Formatted text that was derived in the old one is now wrong, and - /// nothing else re-derives it: a grid keeps its cached strings until the result is re-fetched. + // A grid keeps text formatted in the old zone until its result is fetched again. let systemTimeZoneChanged = PassthroughSubject() // MARK: - Settings let editorSettingsChanged = PassthroughSubject() - /// A live session's health monitor is started once, at connect, so a change to how often - /// TablePro checks its connections reaches nothing already open without this. + // A session's health monitor starts once, at connect. let connectionHealthCheckChanged = PassthroughSubject() let dataGridSettingsChanged = PassthroughSubject() - /// The menu bar re-syncs itself through `MainMenuBuilder`, but a window's toolbar advertises - /// the same shortcuts in its tooltips and overflow menu and AppKit never revisits either. + // The toolbar's tooltips and overflow menu show shortcuts too, and AppKit never revisits them. let keyboardSettingsChanged = PassthroughSubject() let currentSchemaChanged = PassthroughSubject() @@ -48,21 +42,16 @@ final class AppEvents { // MARK: - Connections let connectionStatusChanged = PassthroughSubject() - /// A workspace's window phase changed. Carries the connection id; the phase lives on the workspace. + // Carries the connection id; the phase lives on the workspace. let connectionPhaseChanged = PassthroughSubject() // A driver's Vault lease can no longer be renewed, so the driver has to sign in again. let credentialLeaseEnding = PassthroughSubject() - /// The step a connection attempt is currently on. Presentational detail inside the - /// connecting phase, never a second source of truth for which pane a window shows. + // Detail inside the connecting phase, never what decides which pane a window shows. let connectionStageChanged = PassthroughSubject() - /// Connection metadata changed (name, color, group, type, etc.). - /// Payload is the affected connection's id, or `nil` for bulk updates - /// (sync pull, multi-import) where the sender doesn't track individual ids. - /// Subscribers scoped to a single connection should filter `payload == id`; - /// list-level subscribers refresh on every event regardless. + // The connection's id, or nil for a sync pull or a multi-import. let connectionUpdated = PassthroughSubject() let databaseDidConnect = PassthroughSubject() @@ -73,66 +62,47 @@ final class AppEvents { let mainWindowWillClose = PassthroughSubject() - /// A connection window was registered, closed, or became key. - /// Subscribers that present the set of open connections, or which window - /// stands for each one, refresh on every event. + // A connection window was registered, closed, or became key. let connectionWindowsChanged = PassthroughSubject() - /// The user reordered the workspace rail. Every open rail shares one - /// arrangement, so all of them reload. + // Every open rail shares one arrangement. let workspaceRailOrderChanged = PassthroughSubject() - /// The workspace rail was shown or hidden. Every window carries its own - /// rail, so all of them follow the one setting. + // Every window's rail follows the one setting. let workspaceRailVisibilityChanged = PassthroughSubject() - /// A window's tabs changed in a way that changes which containers they hold - /// open. Fired only when the set of held containers can differ, not on every - /// keystroke, so the rail reloads when a workspace appears or goes away. + // Only when the set of containers the tabs hold open can differ, never per keystroke. let workspaceTabsChanged = PassthroughSubject() - /// A connection's browse cursor moved to another database or schema. - /// Payload is the connection's id. + // The connection's id. let browseContainerChanged = PassthroughSubject() - /// A window put another connections-strip entry in front, so the strip's highlight moves. let visibleContainerChanged = PassthroughSubject() - /// A session connection started or stopped holding a transaction. Payload is the connection's id. + // The connection's id. let sessionTransactionsChanged = PassthroughSubject() // MARK: - Data Sources - /// Query history changed (entry added, deleted, or cleared). - /// Payload is the affected connection's id, or `nil` for cross-connection - /// operations (delete-by-id without connection lookup, clear-all). - /// Per-connection subscribers should refresh on `payload == nil || payload == self.connectionId`. + // The connection's id, or nil for a change across connections, such as Clear History. let queryHistoryDidUpdate = PassthroughSubject() - /// SQL favorites or favorite folders changed. - /// Payload is the connection whose list changed, or `nil` when every connection's did. - /// Per-connection subscribers should refresh on `payload == nil || payload == self.connectionId`. - /// - /// A global favorite is part of every connection's list, so a record moving into or out of - /// global scope changes them all and is sent as `nil`, as are bulk operations and any delete. - /// Only the sender can know this: the scope it moved from is no longer in the record by the - /// time a subscriber sees the event. `SQLFavoriteManager.scopeToAnnounce(for:newConnectionId:)` - /// is where that is decided. + // Nil when every connection's list changed, which only the sender knows: `SQLFavoriteManager.scopeToAnnounce`. let sqlFavoritesDidUpdate = PassthroughSubject() let linkedFoldersDidUpdate = PassthroughSubject() let teamLibraryDidUpdate = PassthroughSubject() - /// Linked SQL folder rescan completed; cached file index changed. - /// Senders are bulk rescans across all enabled folders, so payload is always `nil`. - /// The shape is kept consistent with `sqlFavoritesDidUpdate` so subscribers can - /// uniformly handle "this update may affect me" via `payload == nil || payload == self.connectionId`. + // Always nil: a rescan covers every folder. Shaped like `sqlFavoritesDidUpdate` for shared subscribers. let linkedSQLFoldersDidUpdate = PassthroughSubject() // Refresh on a tab that loads its own content rather than through the coordinator, by tab id. let tabRefreshRequested = PassthroughSubject() + // A query tab's edit history was written. The tab's id, or nil after a purge or Clear History. + let queryTabVersionsDidUpdate = PassthroughSubject() + // MARK: - License & Sync let licenseStatusDidChange = PassthroughSubject() @@ -147,8 +117,7 @@ final class AppEvents { let pluginsRejected = PassthroughSubject<[RejectedPlugin], Never>() - /// Not private so a test can hand an isolated bus to the object under test. App code uses - /// `shared`, which is the only instance anything observes. + // Not private, so a test can hand an isolated bus to the object under test. init() {} } diff --git a/apple/TablePro/Core/Events/SucceededStatements.swift b/apple/TablePro/Core/Events/SucceededStatements.swift index 156a74b96e..8f8cdd8f0b 100644 --- a/apple/TablePro/Core/Events/SucceededStatements.swift +++ b/apple/TablePro/Core/Events/SucceededStatements.swift @@ -35,8 +35,15 @@ enum StatementCommitEvidence: Sendable, Equatable { plan: BatchTransactionPlan, completed: Bool ) -> StatementCommitEvidence { - guard plan.opensTransaction else { return .runStartedIn(start, endedIn: end, appTransaction: .none) } - return .runStartedIn(start, endedIn: end, appTransaction: completed ? .committed : .rolledBack) + switch plan { + case .appTransaction: + return .runStartedIn(start, endedIn: end, appTransaction: completed ? .committed : .rolledBack) + // A held run sent no COMMIT, so what it ran is pending until the user ends the transaction. + case .heldTransaction: + return .runStartedIn(start, endedIn: end, appTransaction: completed ? .none : .rolledBack) + case .scriptTransaction, .autocommit, .sessionTransaction: + return .runStartedIn(start, endedIn: end, appTransaction: .none) + } } } diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/ConfirmDestructiveOperationTool.swift b/apple/TablePro/Core/MCP/Protocol/Tools/ConfirmDestructiveOperationTool.swift index cf0418a0da..1e675ad88c 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/ConfirmDestructiveOperationTool.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/ConfirmDestructiveOperationTool.swift @@ -11,7 +11,8 @@ public struct ConfirmDestructiveOperationTool: MCPToolImplementation { with no WHERE: one statement, or on SQL Server a whole script. Anything else is refused. The user \ approves it before it runs: through your elicitation prompt when your client supports elicitation, \ otherwise through TablePro's own confirmation dialog on the user's Mac. Needs tools:write and a \ - connection the user left writable for external clients. + connection the user left writable for external clients. The transaction rules of execute_query apply, \ + Manual Commit's among them. """ ) public static let requiredScopes: Set = [.toolsWrite] diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/ExecuteQueryTool.swift b/apple/TablePro/Core/MCP/Protocol/Tools/ExecuteQueryTool.swift index 87522b7b7a..98dee6bb14 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/ExecuteQueryTool.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/ExecuteQueryTool.swift @@ -13,7 +13,11 @@ public struct ExecuteQueryTool: MCPToolImplementation { Destructive statements, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE, go through \ confirm_destructive_operation instead. Statements that read or write files, or run server-side code, \ are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at \ - its GO lines and result_sets lists every result set when there is more than one. + its GO lines and result_sets lists every result set when there is more than one. A statement that starts \ + a transaction while one is open is refused. With Manual Commit on, a write stays pending until the user \ + commits it in TablePro, and statements that begin, commit or roll back a transaction are refused. While a \ + transaction is open on PostgreSQL or CockroachDB a statement runs inside a savepoint, so its failure leaves \ + the transaction usable; on Redshift and DuckDB it is refused. """ ) public static let requiredScopes: Set = [.toolsRead] diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/MaintenanceTools.swift b/apple/TablePro/Core/MCP/Protocol/Tools/MaintenanceTools.swift index 832833c5bb..9e022fc2c8 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/MaintenanceTools.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/MaintenanceTools.swift @@ -236,7 +236,9 @@ public struct TransactionControlTool: MCPToolImplementation { localized: """ Begin, commit, or roll back a transaction on the connection's shared session. The transaction \ stays open across calls until committed or rolled back, and it is the same session the user's \ - own tabs run on, so leave nothing open. Needs tools:write. + own tabs run on, so leave nothing open. Begin is refused while a transaction is already open, \ + and every action is refused on a connection with Manual Commit on, where the user commits in \ + TablePro. Needs tools:write. """ ) public static let requiredScopes: Set = [.toolsWrite] diff --git a/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift b/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift index f1e617b865..af0ebfa94b 100644 --- a/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift +++ b/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift @@ -10,6 +10,7 @@ import AppKit enum DatabaseMenuBuilder { static let identifier = NSUserInterfaceItemIdentifier("menu.database") static let liveMessagesIdentifier = NSUserInterfaceItemIdentifier("menu.database.liveMessages") + static let manualCommitFootnoteIdentifier = NSUserInterfaceItemIdentifier("menu.database.manualCommitFootnote") static func build(keyboard: KeyboardSettings, modules: Set = []) -> NSMenuItem { let item = MenuItemFactory.menu(String(localized: "Database"), items: [ @@ -38,6 +39,7 @@ enum DatabaseMenuBuilder { shortcut: .refresh, keyboard: keyboard ), + autoRefreshSubmenu(), MenuItemFactory.separator, MenuItemFactory.item( String(localized: "New Database…"), @@ -109,7 +111,8 @@ enum DatabaseMenuBuilder { ), schemaSubmenu(), sessionContextSubmenu(), - safeModeSubmenu(), + safeModeSubmenu() + ] + transactionItems(keyboard: keyboard) + [ favoriteDatabaseSubmenu(), maintenanceSubmenu(), MenuItemFactory.item( @@ -278,6 +281,47 @@ enum DatabaseMenuBuilder { return container } + private static func transactionItems(keyboard: KeyboardSettings) -> [NSMenuItem] { + [ + MenuItemFactory.item( + String(localized: "Manual Commit"), + action: #selector(MainSplitViewController.toggleManualCommit(_:)) + ), + manualCommitFootnote(), + MenuItemFactory.item( + String(localized: "Commit Transaction"), + action: #selector(MainSplitViewController.commitOpenTransaction(_:)), + shortcut: .commitTransaction, + keyboard: keyboard + ), + MenuItemFactory.item( + String(localized: "Roll Back Transaction"), + action: #selector(MainSplitViewController.rollBackOpenTransaction(_:)), + shortcut: .rollbackTransaction, + keyboard: keyboard + ) + ] + } + + // Validation shows it under Manual Commit, which it shares an action with so it always validates as disabled. + private static func manualCommitFootnote() -> NSMenuItem { + let item = MenuFootnote.item( + String(localized: "This database can't report an open transaction, so it commits each change.") + ) + item.action = #selector(MainSplitViewController.toggleManualCommit(_:)) + item.identifier = manualCommitFootnoteIdentifier + item.isHidden = true + return item + } + + private static let autoRefreshDelegate = TableAutoRefreshMenuDelegate() + + private static func autoRefreshSubmenu() -> NSMenuItem { + let container = MenuItemFactory.submenu(String(localized: "Auto Refresh"), items: []) + container.submenu?.delegate = autoRefreshDelegate + return container + } + private static let safeModeDelegate = SafeModeMenuDelegate() /// The same list the toolbar's Safe Mode control opens, so every toolbar item keeps a menu-bar diff --git a/apple/TablePro/Core/Menu/FileMenuBuilder.swift b/apple/TablePro/Core/Menu/FileMenuBuilder.swift index 888331b772..2ce0f5d02b 100644 --- a/apple/TablePro/Core/Menu/FileMenuBuilder.swift +++ b/apple/TablePro/Core/Menu/FileMenuBuilder.swift @@ -7,11 +7,12 @@ import AppKit @MainActor enum FileMenuBuilder { - /// Retained for the menu's lifetime, which is the app's: `NSMenu.delegate` is unowned. + // Retained for the app's lifetime: `NSMenu.delegate` is unowned. private static let closeTitleDelegate = CloseCommandMenuDelegate() private static let importFormatDelegate = ImportFormatMenuDelegate() private static let agentSessionDelegate = AgentSessionMenuDelegate() private static let conversationHistoryDelegate = ConversationHistoryMenuDelegate() + private static let revertToDelegate = QueryTabRevertMenuDelegate() static func build(keyboard: KeyboardSettings) -> NSMenuItem { let file = MenuItemFactory.menu(String(localized: "File"), items: [ @@ -84,14 +85,9 @@ enum FileMenuBuilder { shortcut: .saveAs, keyboard: keyboard ), + revertToSubmenu(), MenuItemFactory.separator, - /// `performClose:` is the close command every `NSWindow` implements and validates, so - /// Command W reaches Settings, the integrations activity window and every viewer the - /// app opens. An app-specific selector here reached only the editor window, and the - /// windows that missed out had to answer it by hand. `EditorWindow` overrides it to - /// close the front tab, which is where a window that draws its own tabs says so. - /// Built with the title the resolver gives a window with no tabs; the File menu's - /// delegate resolves it from the key window from then on. + // `performClose:` reaches every window, not only the editor; `EditorWindow` overrides it to close the front tab. MenuItemFactory.item( CloseCommandTitleResolver.windowTitle, action: #selector(NSWindow.performClose(_:)), @@ -143,14 +139,8 @@ enum FileMenuBuilder { return file } - /// Agent mode's four session commands and the assistant's three conversation commands, which - /// between them had no menu-bar home at all: the rail's buttons and the pane header's menu were - /// the only routes, so none of them could be found by search, rebound, or reached by a user who - /// had the rail collapsed. They sit in File because a session and a conversation are things this - /// window opens, closes and throws away, which is what the rest of this menu is about. - /// - /// Each command has exactly one item here, and each item leaves `target` nil, so the window - /// validates it through the responder chain and dims what the mode cannot run. + // A session and a conversation are things this window opens and closes. Each item leaves `target` nil, so the + // window validates it through the responder chain. private static func sessionSubmenu(keyboard: KeyboardSettings) -> NSMenuItem { MenuItemFactory.submenu(String(localized: "Session"), items: [ MenuItemFactory.item( @@ -159,11 +149,7 @@ enum FileMenuBuilder { shortcut: .newAgentSession, keyboard: keyboard ), - /// Opens the session the rail has highlighted, which is what Return on the rail does. - /// A separate leaf rather than the list's own row: AppKit ignores a key equivalent on an - /// item that owns a submenu, so making this the list would hand Settings a binding that - /// records, reads back and never fires. That is the same trap Import Data… is split - /// around, and it is why the list is a row of its own below. + // A leaf beside the list: AppKit ignores a key equivalent on an item that owns a submenu. MenuItemFactory.item( String(localized: "Open Session"), action: #selector(MainSplitViewController.openAgentSession(_:)), @@ -198,23 +184,27 @@ enum FileMenuBuilder { ]) } - /// Every session the connection on screen owns, latest first, filled when it opens. A session - /// list built at menu-build time would be one window's sessions frozen at launch. + // Filled when it opens; a list built with the menu bar would be frozen at launch. private static func recentSessionsSubmenu() -> NSMenuItem { let container = MenuItemFactory.submenu(String(localized: "Recent Sessions"), items: []) container.submenu?.delegate = agentSessionDelegate return container } - /// The assistant's stored conversations, filled when it opens for the same reason: the set - /// changes with every reply. The pane header's own menu offers the same list, and both put the - /// choice through the window so neither can act on a connection the other is showing. + // Filled when it opens, since the set changes with every reply. private static func conversationHistorySubmenu() -> NSMenuItem { let container = MenuItemFactory.submenu(String(localized: "Conversation History"), items: []) container.submenu?.delegate = conversationHistoryDelegate return container } + // The selected query tab's recent versions, filled when it opens. No shortcut: AppKit ignores one on this row. + private static func revertToSubmenu() -> NSMenuItem { + let container = MenuItemFactory.submenu(String(localized: "Revert To"), items: []) + container.submenu?.delegate = revertToDelegate + return container + } + private static func importSubmenu(keyboard: KeyboardSettings) -> NSMenuItem { let container = MenuItemFactory.submenu(String(localized: "Import"), items: [ MenuItemFactory.item( @@ -253,9 +243,7 @@ enum FileMenuBuilder { return container } - /// Every format the connection imports from, for naming one a file's extension does not. - /// Import Data… above it reads the format off the file instead. The Actions pull-down offers - /// the same list under the same title, and the two are filled by the same class when they open. + // For a format the file's extension does not name; the Actions pull-down fills the same list with the same class. private static func importFormatsSubmenu() -> NSMenuItem { let container = MenuItemFactory.submenu(String(localized: "Import Data From"), items: []) container.submenu?.delegate = importFormatDelegate diff --git a/apple/TablePro/Core/Menu/QueryTabRevertMenuDelegate.swift b/apple/TablePro/Core/Menu/QueryTabRevertMenuDelegate.swift new file mode 100644 index 0000000000..0dc068c075 --- /dev/null +++ b/apple/TablePro/Core/Menu/QueryTabRevertMenuDelegate.swift @@ -0,0 +1,67 @@ +// +// QueryTabRevertMenuDelegate.swift +// TablePro +// + +import AppKit + +internal struct QueryTabRevertChoice: Equatable { + let tabId: UUID + let versionId: Int64 +} + +// File > Revert To, filled for the selected query tab when it opens. Items carry no target, so the window validates them. +@MainActor +internal final class QueryTabRevertMenuDelegate: NSObject, NSMenuDelegate { + static let revertAction = #selector(MainSplitViewController.revertToQueryTabVersion(_:)) + static let browseAction = #selector(MainSplitViewController.browseQueryTabVersions(_:)) + static let recentVersionLimit = 10 + + // Resolved when the menu opens, so building the menu bar touches no store. + private let storage: QueryTabVersionStorage? + + init(storage: QueryTabVersionStorage? = nil) { + self.storage = storage + } + + func menuNeedsUpdate(_ menu: NSMenu) { + let controller = NSApp.target(forAction: Self.browseAction, to: nil, from: nil) as? MainSplitViewController + let tabId = controller?.selectedQueryTabId + let store = storage ?? .shared + let summaries = tabId.map { store.summaries(tabId: $0, limit: Self.recentVersionLimit) } ?? [] + Self.fill(menu, tabId: tabId, summaries: summaries) + } + + static func fill(_ menu: NSMenu, tabId: UUID?, summaries: [QueryTabVersionSummary]) { + menu.removeAllItems() + if let tabId, !summaries.isEmpty { + for summary in summaries { + menu.addItem(item(for: summary, tabId: tabId)) + } + } else { + menu.addItem(MenuPlaceholder.item()) + } + menu.addItem(.separator()) + let browse = NSMenuItem(title: String(localized: "Browse All Versions…"), action: browseAction, keyEquivalent: "") + browse.target = nil + menu.addItem(browse) + } + + static func item(for summary: QueryTabVersionSummary, tabId: UUID) -> NSMenuItem { + let entry = QueryTabVersionHistoryProvider.entry(for: summary) + let item = NSMenuItem(title: VersionHistoryFormatting.menuTitle(for: entry), action: revertAction, keyEquivalent: "") + item.target = nil + item.representedObject = QueryTabRevertChoice(tabId: tabId, versionId: summary.id) + return item + } + + // Keeps AppKit's key-equivalent search from reading the store on every modified keystroke. + func menuHasKeyEquivalent( + _ menu: NSMenu, + for event: NSEvent, + target: AutoreleasingUnsafeMutablePointer, + action: UnsafeMutablePointer + ) -> Bool { + false + } +} diff --git a/apple/TablePro/Core/Menu/TableAutoRefreshMenuDelegate.swift b/apple/TablePro/Core/Menu/TableAutoRefreshMenuDelegate.swift new file mode 100644 index 0000000000..f7df6b2695 --- /dev/null +++ b/apple/TablePro/Core/Menu/TableAutoRefreshMenuDelegate.swift @@ -0,0 +1,38 @@ +// +// TableAutoRefreshMenuDelegate.swift +// TablePro +// + +import AppKit + +// Filled when it opens, so the checkmark names the selected tab's interval; validation dims the items off a table tab. +@MainActor +final class TableAutoRefreshMenuDelegate: NSObject, NSMenuDelegate { + static let action = #selector(MainSplitViewController.setTableAutoRefreshInterval(_:)) + + func menuNeedsUpdate(_ menu: NSMenu) { + let controller = NSApp.target(forAction: Self.action, to: nil, from: nil) as? MainSplitViewController + Self.populate(menu, current: controller?.tableAutoRefreshInterval) + } + + internal static func populate(_ menu: NSMenu, current: TableAutoRefreshInterval?) { + menu.removeAllItems() + for interval in TableAutoRefreshInterval.allCases { + let item = NSMenuItem(title: interval.title, action: action, keyEquivalent: "") + item.target = nil + item.representedObject = interval.rawValue + item.state = interval == current ? .on : .off + menu.addItem(item) + } + } + + // Keeps AppKit's key-equivalent search from rebuilding the menu on every modified keystroke. + func menuHasKeyEquivalent( + _ menu: NSMenu, + for event: NSEvent, + target: AutoreleasingUnsafeMutablePointer, + action: UnsafeMutablePointer + ) -> Bool { + false + } +} diff --git a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift index f8c04fd656..8aeeb4b339 100644 --- a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift +++ b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift @@ -115,9 +115,11 @@ internal struct ObjectCopyRunner { /// pending work. `DataWriteExecutor`, `DatabaseManager+Principals` and /// `StructureRebuildPlanRunner` ask the same question for the same reason. nonisolated private static func transactionOwner(of driver: DatabaseDriver) async -> WriteTransactionOwner { + // `.automatic`: a pooled lease is closed while idle, which would roll a held copy back after it reported success. WriteTransactionOwner.resolve( supportsTransactions: driver.supportsTransactions, - sessionState: await driver.heldSessionTransactionState() + sessionState: await driver.heldSessionTransactionState(), + commitMode: .automatic ) } @@ -272,7 +274,7 @@ internal struct ObjectCopyRunner { let owner = await Self.transactionOwner(of: driver) // The other two settings keep what ran, and PostgreSQL commits an aborted transaction as a ROLLBACK. let usesTransaction = hasCleanup && errorHandling == .stopAndRollback - && plugin.supportsTransactionalDDL && owner.opensTransaction + && plugin.supportsTransactionalDDL && owner.sendsBegin && owner.commitsAtEnd if usesTransaction { try await plugin.beginTransaction(mode: .readWrite) } var result: DDLResult @@ -600,7 +602,7 @@ internal struct ObjectCopyRunner { throw ObjectCopyError.refused(Self.noTargetDriver) } let owner = await Self.transactionOwner(of: targetDriver) - let usesTransaction = owner.opensTransaction + let usesTransaction = owner.sendsBegin && owner.commitsAtEnd if usesTransaction { try await targetPlugin.beginTransaction(mode: .readWrite) } var result = DataResult() diff --git a/apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift b/apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift index cb350fb44f..5bc8d241e6 100644 --- a/apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift +++ b/apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift @@ -40,7 +40,10 @@ final class ImportDataSinkAdapter: PluginImportDataSink, @unchecked Sendable { private let tableEditDialect: TableEditDialect? private let nameHazards = OSAllocatedUnfairLock<[String]>(initialState: []) private let ownsTransaction = OSAllocatedUnfairLock(initialState: false) + private let leavesTransactionOpen = OSAllocatedUnfairLock(initialState: false) + private let disabledForeignKeys = OSAllocatedUnfairLock(initialState: false) private let joinsTransaction: Bool + private let commitMode: ConnectionCommitMode private let transactionRecord = OSAllocatedUnfairLock(initialState: TransactionRecord()) private struct TransactionRecord { @@ -72,14 +75,17 @@ final class ImportDataSinkAdapter: PluginImportDataSink, @unchecked Sendable { sourceFields: Set = [], rowWriting: ImportRowWriting = .insert, joinsTransaction: Bool = false, + commitMode: ConnectionCommitMode = .automatic, importRun: ImportRun? = nil, fileIndex: Int? = 0, isCancelled: @escaping @Sendable () -> Bool = { false } ) { self.importControl = importRun.map { ImportSinkRun(run: $0, driver: driver, databaseType: databaseType, fileIndex: fileIndex, - targetTable: targetTable, targetSchema: targetSchema, joinsTransaction: joinsTransaction) + targetTable: targetTable, targetSchema: targetSchema, joinsTransaction: joinsTransaction, + commitMode: commitMode) } + self.commitMode = commitMode self.isCancelled = isCancelled self.rowWriting = rowWriting self.tally = OSAllocatedUnfairLock(initialState: ImportRowTally(mode: rowWriting.mode)) @@ -564,19 +570,32 @@ final class ImportDataSinkAdapter: PluginImportDataSink, @unchecked Sendable { func beginTransaction() async throws { if let importControl { return try await importControl.begin() } guard !joinsTransaction else { return } + let state = await driver.heldSessionTransactionState() + if commitMode == .manual { + guard !disabledForeignKeys.withLock({ $0 }) else { throw ManualCommitForeignKeyRefusal() } + try ManualCommit.refuseUnheldWrite(supportsTransactions: driver.supportsTransactions, sessionState: state) + } let owner = WriteTransactionOwner.resolve( supportsTransactions: driver.supportsTransactions, - sessionState: await driver.heldSessionTransactionState() + sessionState: state, + commitMode: commitMode ) - ownsTransaction.withLock { $0 = owner.opensTransaction } + ownsTransaction.withLock { $0 = owner.sendsBegin } + leavesTransactionOpen.withLock { $0 = owner.sendsBegin && !owner.commitsAtEnd } transactionRecord.withLock { $0 = TransactionRecord() } - guard owner.opensTransaction else { return } + guard owner.sendsBegin else { return } try await driver.beginTransaction(mode: .readWrite) + if owner == .held { ManualCommit.turnHold?.mark(true) } } func commitTransaction() async throws { if let importControl { return try await importControl.commit() } guard ownsTransaction.withLock({ $0 }) else { return } + // Manual Commit: the imported rows stay pending in the transaction the user commits. + guard !leavesTransactionOpen.withLock({ $0 }) else { + ownsTransaction.withLock { $0 = false } + return + } try await driver.commitTransaction() ownsTransaction.withLock { $0 = false } transactionRecord.withLock { $0.ending = .committed } @@ -642,6 +661,8 @@ final class ImportDataSinkAdapter: PluginImportDataSink, @unchecked Sendable { _ = try await self.driver.execute(query: stmt) } } + disabledForeignKeys.withLock { $0 = true } + importControl?.noteForeignKeysDisabled() } func enableForeignKeyChecks() async throws { @@ -659,7 +680,10 @@ final class ImportDataSinkAdapter: PluginImportDataSink, @unchecked Sendable { } func prepareImportRun() async throws { - try await importControl?.prepare() + if let importControl { return try await importControl.prepare() } + guard commitMode == .manual else { return } + let state = await driver.heldSessionTransactionState() + try ManualCommit.refuseUnheldWrite(supportsTransactions: driver.supportsTransactions, sessionState: state) } func importCheckpoint() async throws { diff --git a/apple/TablePro/Core/Plugins/ImportSinkRun.swift b/apple/TablePro/Core/Plugins/ImportSinkRun.swift index bbad43e321..22bdd9089d 100644 --- a/apple/TablePro/Core/Plugins/ImportSinkRun.swift +++ b/apple/TablePro/Core/Plugins/ImportSinkRun.swift @@ -15,8 +15,14 @@ internal final class ImportSinkRun: @unchecked Sendable { private let targetTable: String? private let targetSchema: String? private let joinsTransaction: Bool + private let commitMode: ConnectionCommitMode private let lock = NSLock() private var ownsTransaction = false + private var leavesTransactionOpen = false + private var disabledForeignKeys = false + private var writesOutsideTransaction: Bool? + private var heldChanges = false + private var holdEndedByFile = false private var finalized = false private var acknowledgedEnding: ImportTransactionOutcome = .untracked private var fileCommittedEarly = false @@ -38,7 +44,8 @@ internal final class ImportSinkRun: @unchecked Sendable { fileIndex: Int?, targetTable: String?, targetSchema: String?, - joinsTransaction: Bool + joinsTransaction: Bool, + commitMode: ConnectionCommitMode = .automatic ) { self.run = run self.driver = driver @@ -47,10 +54,15 @@ internal final class ImportSinkRun: @unchecked Sendable { self.targetTable = targetTable self.targetSchema = targetSchema self.joinsTransaction = joinsTransaction + self.commitMode = commitMode } internal func prepare() async throws { try await admitted(.begin) { + if self.commitMode == .manual { + let state = await self.driver.heldSessionTransactionState() + try ManualCommit.refuseUnheldWrite(supportsTransactions: self.driver.supportsTransactions, sessionState: state) + } if !self.run.snapshot.prepared { let state = await self.sessionState() self.run.record { @@ -93,6 +105,10 @@ internal final class ImportSinkRun: @unchecked Sendable { operation: () async throws -> T ) async throws -> T { try await admitted(.data) { + if writes, self.commitMode == .manual { + try self.refuseWriteOutsideTransaction() + if let statement { try self.refuseCommittingStatement(statement) } + } if let statement { self.noteStatement(statement) } if writes, let index = self.fileIndex { self.run.record { $0.effects[index]?.admittedWrite = true } @@ -110,6 +126,7 @@ internal final class ImportSinkRun: @unchecked Sendable { } await self.observe() if let statement { self.noteCompletedStatement(statement, before: before, wasOwned: hadAuthority) } + if writes, self.commitMode == .manual, let statement { self.noteHeldStatement(statement) } return value } catch { self.run.record { @@ -122,6 +139,41 @@ internal final class ImportSinkRun: @unchecked Sendable { } } + // Decided at the first write, before a GO line resets the holder: outside a transaction each statement commits. + private func refuseWriteOutsideTransaction() throws { + let holder = run.snapshot.holder + let outside = lock.withLock { () -> Bool in + if writesOutsideTransaction == nil { writesOutsideTransaction = holder == .none } + return writesOutsideTransaction == true + } + if outside { throw ManualCommitUnwrappedImportRefusal() } + } + + // MySQL and Oracle commit a held import's rows at its next DDL, and the rows after that DDL would autocommit. + private func refuseCommittingStatement(_ statement: String) throws { + guard let commits = heldImplicitCommit(statement) else { return } + let (changed, ended) = lock.withLock { (heldChanges, holdEndedByFile) } + if commits ? changed : ended { throw ManualCommitMixedRefusal() } + } + + private func noteHeldStatement(_ statement: String) { + guard let commits = heldImplicitCommit(statement) else { return } + let changes = !commits && OperationKind.worst(of: [statement], databaseType: databaseType).declaresWrite + lock.withLock { + if commits { holdEndedByFile = true } + if changes { heldChanges = true } + } + } + + // Nil unless this import holds its transaction on an engine whose DDL commits it. + private func heldImplicitCommit(_ statement: String) -> Bool? { + guard lock.withLock({ leavesTransactionOpen }), + TableEditDialect.of(databaseType)?.commitsDDLImplicitly == true + else { return nil } + let grammar = SQLLexicalResolver.readings(for: databaseType, session: driver.sessionLexicalState).execution + return ManualCommit.commitsHeldTransaction([statement], databaseType: databaseType, grammar: grammar) + } + private func noteStatement(_ statement: String) { let grammar = SQLLexicalResolver.readings(for: databaseType, session: driver.sessionLexicalState).execution if grammar.contains(.batchSeparatorLines) { @@ -171,17 +223,34 @@ internal final class ImportSinkRun: @unchecked Sendable { try await admitted(.data) {} } + // SQLite ignores the re-enable inside a held transaction, and a Roll Back undoes PostgreSQL's. + internal func noteForeignKeysDisabled() { + lock.withLock { disabledForeignKeys = true } + } + internal func begin() async throws { guard !joinsTransaction else { return } try await admitted(.begin) { let state = await self.driver.heldSessionTransactionState() - let owner = WriteTransactionOwner.resolve(supportsTransactions: self.driver.supportsTransactions, sessionState: state) - guard owner.opensTransaction else { + if self.commitMode == .manual { + guard !self.lock.withLock({ self.disabledForeignKeys }) else { throw ManualCommitForeignKeyRefusal() } + try ManualCommit.refuseUnheldWrite(supportsTransactions: self.driver.supportsTransactions, sessionState: state) + } + let owner = WriteTransactionOwner.resolve( + supportsTransactions: self.driver.supportsTransactions, + sessionState: state, + commitMode: self.commitMode + ) + guard owner.sendsBegin else { self.run.record { $0.currentState = ImportSessionState(state) } return } try await self.driver.beginTransaction(mode: .readWrite) - self.lock.withLock { self.ownsTransaction = true } + if owner == .held { ManualCommit.turnHold?.mark(true) } + self.lock.withLock { + self.ownsTransaction = true + self.leavesTransactionOpen = !owner.commitsAtEnd + } self.run.record { $0.holder = .owned; $0.ending = nil } await self.observe() } @@ -203,6 +272,13 @@ internal final class ImportSinkRun: @unchecked Sendable { markFinalized() return } + // Manual Commit: the rows stay pending in a transaction that is now the user's, as a joined one is. + if lock.withLock({ leavesTransactionOpen }) { + lock.withLock { ownsTransaction = false } + run.record { $0.holder = .joinedUser; $0.currentState = state } + markFinalized() + return + } do { try await driver.commitTransaction() await recordEnding(.committed) @@ -243,6 +319,7 @@ internal final class ImportSinkRun: @unchecked Sendable { } do { try await driver.rollbackTransaction() + ManualCommit.turnHold?.mark(false) await recordEnding(.rolledBack) lock.withLock { acknowledgedEnding = .rolledBack } markFinalized() diff --git a/apple/TablePro/Core/Plugins/PluginDriverAdapter+ServerObjects.swift b/apple/TablePro/Core/Plugins/PluginDriverAdapter+ServerObjects.swift new file mode 100644 index 0000000000..1994fe8817 --- /dev/null +++ b/apple/TablePro/Core/Plugins/PluginDriverAdapter+ServerObjects.swift @@ -0,0 +1,36 @@ +// +// PluginDriverAdapter+ServerObjects.swift +// TablePro +// + +import Foundation +import os +import TableProPluginKit + +extension PluginDriverAdapter { + private static let serverObjectsLogger = Logger(subsystem: "com.TablePro", category: "PluginDriverAdapter") + + var serverObjectKinds: [PluginServerObjectKind] { schemaPluginDriver.serverObjectKinds } + + // A container kind is stamped with the schema it was read from, as routines are; a database kind + // names no schema and reads with none. + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [ServerObjectInfo] { + let isDatabaseScoped = schemaPluginDriver.serverObjectKinds.first { $0.id == kindId }?.scope == .database + let resolvedSchema = isDatabaseScoped ? nil : (schema ?? schemaPluginDriver.currentSchema) + do { + return try await schemaPluginDriver.fetchServerObjects(ofKind: kindId, schema: resolvedSchema) + .filter { $0.kindId == kindId } + .map { ServerObjectInfo($0).adoptingSchema(resolvedSchema) } + .sorted { ($0.schema ?? "", $0.name) < ($1.schema ?? "", $1.name) } + } catch { + Self.serverObjectsLogger.warning( + "fetchServerObjects failed kind=\(kindId, privacy: .public) error=\(error.publicLogShape, privacy: .public)" + ) + throw error + } + } + + func fetchServerObject(_ object: ServerObjectInfo) async throws -> ServerObjectInfo { + ServerObjectInfo(try await schemaPluginDriver.fetchServerObject(object.pluginObject)) + } +} diff --git a/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift b/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift index 87b4c54dc8..90985b1650 100644 --- a/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift +++ b/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift @@ -589,6 +589,13 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { } for fk in workingForeignKeys where isStaged(.foreignKey(fk.id)) && fk.isValid { + // Create Table refuses this too; otherwise the server does, after a MySQL save's earlier DDL already applied. + guard fk.columns.count == fk.referencedColumns.count else { + validationErrors[.foreignKey(fk.id)] = String( + localized: "A foreign key needs as many referenced columns as it has columns." + ) + continue + } for columnName in fk.columns where !namesAColumn(columnName, in: columnNames) { validationErrors[.foreignKey(fk.id)] = String( format: String(localized: "Foreign key references a column that does not exist: %@"), columnName diff --git a/apple/TablePro/Core/Services/Execution/BatchStatementRun.swift b/apple/TablePro/Core/Services/Execution/BatchStatementRun.swift index 3f3c01bb6d..fb6aca5752 100644 --- a/apple/TablePro/Core/Services/Execution/BatchStatementRun.swift +++ b/apple/TablePro/Core/Services/Execution/BatchStatementRun.swift @@ -80,10 +80,11 @@ internal enum BatchStatementRun { commitOutput: ServerOutputBox = ServerOutputBox(), execute: @escaping @MainActor @Sendable (Statement) async throws -> Output ) async -> BatchStatementOutcome { - let opensTransaction = plan.opensTransaction && driver.supportsTransactions - if opensTransaction { + let sentBegin = plan.sendsBegin && driver.supportsTransactions + if sentBegin { do { try await driver.beginTransaction(mode: mode) + if plan == .heldTransaction { ManualCommit.turnHold?.mark(true) } } catch { return .failed(results: [], failure: .transactionStart, errorDescription: error.localizedDescription) } @@ -95,7 +96,7 @@ internal enum BatchStatementRun { while position < queue.count { let statement = queue[position] guard !Task.isCancelled, gate.isCurrent() else { - await rollback(driver: driver, connectionId: connectionId, plan: plan, opensTransaction: opensTransaction) + await rollback(driver: driver, connectionId: connectionId, plan: plan, sentBegin: sentBegin) return .cancelled(results: plan.keepsExecutedStatements ? results : []) } let ran = await runStatement( @@ -108,12 +109,12 @@ internal enum BatchStatementRun { ) switch ran { case .stopped: - await rollback(driver: driver, connectionId: connectionId, plan: plan, opensTransaction: opensTransaction) + await rollback(driver: driver, connectionId: connectionId, plan: plan, sentBegin: sentBegin) return .cancelled(results: plan.keepsExecutedStatements ? results : []) case .committed(let result): results.append(result) if let errorDescription = serverError(result) { - await rollback(driver: driver, connectionId: connectionId, plan: plan, opensTransaction: opensTransaction) + await rollback(driver: driver, connectionId: connectionId, plan: plan, sentBegin: sentBegin) return .failed( results: results, failure: .batch(sql: failureSQL(statement)), @@ -127,7 +128,7 @@ internal enum BatchStatementRun { errorDescription: failure.errorDescription ) case .failed(let failure): - await rollback(driver: driver, connectionId: connectionId, plan: plan, opensTransaction: opensTransaction) + await rollback(driver: driver, connectionId: connectionId, plan: plan, sentBegin: sentBegin) return .failed( results: results, failure: .statement(sql: failureSQL(statement)), @@ -140,7 +141,7 @@ internal enum BatchStatementRun { } } - guard opensTransaction else { return .completed(results: results) } + guard plan.commitsAtEnd, sentBegin else { return .completed(results: results) } return await commit( results: results, driver: driver, connectionId: connectionId, gate: gate, plan: plan, output: commitOutput ) @@ -198,7 +199,7 @@ internal enum BatchStatementRun { switch committed { case .stopped: - await rollback(driver: driver, connectionId: connectionId, plan: plan, opensTransaction: true) + await rollback(driver: driver, connectionId: connectionId, plan: plan, sentBegin: true) return .cancelled(results: plan.keepsExecutedStatements ? results : []) case .committed: output.store(await ServerOutputCapture.drain(driver)) @@ -211,7 +212,7 @@ internal enum BatchStatementRun { results: results, failure: .commitOutcomeUnknown, errorDescription: failure.errorDescription ) } - await rollback(driver: driver, connectionId: connectionId, plan: plan, opensTransaction: true) + await rollback(driver: driver, connectionId: connectionId, plan: plan, sentBegin: true) return .failed(results: results, failure: .commit, errorDescription: failure.errorDescription) } } @@ -229,15 +230,16 @@ internal enum BatchStatementRun { driver: DatabaseDriver, connectionId: UUID, plan: BatchTransactionPlan, - opensTransaction: Bool + sentBegin: Bool ) async { guard plan.rollsBackAfterStop, driver.supportsTransactions else { return } let token = DatabaseManager.shared.beginProtectedWrite(on: driver, for: connectionId) defer { DatabaseManager.shared.endProtectedWrite(token, for: connectionId) } do { try await TaskCancellationShield.run { @Sendable in try await driver.rollbackTransaction() } + if plan == .heldTransaction { ManualCommit.turnHold?.mark(false) } } catch { - guard opensTransaction else { + guard sentBegin else { batchLog.debug("No open script transaction to roll back: \(error.publicLogShape, privacy: .public)") return } diff --git a/apple/TablePro/Core/Services/Execution/BatchTransactionPlan.swift b/apple/TablePro/Core/Services/Execution/BatchTransactionPlan.swift index cb9d4df71f..13f736b9f3 100644 --- a/apple/TablePro/Core/Services/Execution/BatchTransactionPlan.swift +++ b/apple/TablePro/Core/Services/Execution/BatchTransactionPlan.swift @@ -13,14 +13,17 @@ import TableProPluginKit /// `PRAGMA foreign_keys`. Such a batch opens nothing, commits nothing, and rolls nothing back, /// because there is no transaction of its own to roll back to. /// -/// `.sessionTransaction` is the answer the text cannot reach: it comes from the driver, once the -/// lease is held, through ``joining(_:)``. +/// `.sessionTransaction` and `.heldTransaction` are the answers the text cannot reach: they come +/// from the driver and the connection's commit mode, once the lease is held, through +/// ``joining(_:commitMode:)``. /// /// The switches over this are exhaustive on purpose. No case may inherit `.appTransaction`'s begin /// or `.scriptTransaction`'s rollback by falling into a `default:` arm. internal enum BatchTransactionPlan: Equatable, Sendable { /// The app opens the transaction, commits it, and rolls it back on a failure or a Stop. case appTransaction + // Manual Commit: the app opens the transaction and leaves it for the user to commit; a failure or a Stop rolls it back. + case heldTransaction /// The script opens its own transaction. The app opens none and still rolls back what a failed /// script left open. case scriptTransaction @@ -31,24 +34,43 @@ internal enum BatchTransactionPlan: Equatable, Sendable { /// ending in `COMMIT` commits. case sessionTransaction - internal var opensTransaction: Bool { + internal var sendsBegin: Bool { switch self { - case .appTransaction: + case .appTransaction, .heldTransaction: return true case .scriptTransaction, .autocommit, .sessionTransaction: return false } } + internal var commitsAtEnd: Bool { + switch self { + case .appTransaction: + return true + case .heldTransaction, .scriptTransaction, .autocommit, .sessionTransaction: + return false + } + } + internal var rollsBackAfterStop: Bool { switch self { - case .appTransaction, .scriptTransaction: + case .appTransaction, .heldTransaction, .scriptTransaction: return true case .autocommit, .sessionTransaction: return false } } + // What the session holds once the run ends is the user's to see: the transaction is still open. + internal var readsSessionEndState: Bool { + switch self { + case .heldTransaction, .sessionTransaction: + return true + case .appTransaction, .scriptTransaction, .autocommit: + return false + } + } + /// Whether the statements that ran before a failure or a Stop stay in place. The failure banner /// says so, and a stopped run keeps their results and their history rather than dropping them. internal var keepsExecutedStatements: Bool { @@ -65,8 +87,9 @@ internal enum BatchTransactionPlan: Equatable, Sendable { /// A session that answers `.unknown` keeps the wrap for a plain batch, but a self-managed /// script stops being rolled back: the transaction its text left open may predate the run, and /// rolling that back discards work the run never did. - internal func joining(_ session: PluginSessionTransactionState) -> BatchTransactionPlan { + internal func joining(_ session: PluginSessionTransactionState, commitMode: ConnectionCommitMode) -> BatchTransactionPlan { guard session.permitsAppTransaction else { return .sessionTransaction } + if self == .appTransaction, commitMode == .manual, session == .idle { return .heldTransaction } guard session == .unknown, self == .scriptTransaction else { return self } return .sessionTransaction } diff --git a/apple/TablePro/Core/Services/Execution/ManualCommit.swift b/apple/TablePro/Core/Services/Execution/ManualCommit.swift new file mode 100644 index 0000000000..61d2a30d55 --- /dev/null +++ b/apple/TablePro/Core/Services/Execution/ManualCommit.swift @@ -0,0 +1,384 @@ +// +// ManualCommit.swift +// TablePro +// + +import Foundation +import os +import TableProPluginKit +import TableProSQLGrammar +import TableProUtilities + +// A refusal an external caller reads as it is: it quotes nothing the server said. +internal protocol ManualCommitRefusing: LocalizedError {} + +internal struct ManualCommitRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "This connection can't hold this change until you commit, so it did not run. Turn off Manual Commit in the Database menu to run it." + ) + } +} + +internal struct ManualCommitMixedRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "This run mixes changes with a statement that commits them or can't run inside a transaction, so they can't wait for Commit. Run that statement on its own, or turn off Manual Commit in the Database menu." + ) + } +} + +internal struct ManualCommitSessionOptionsRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "This change sets session options around its statements, so it can't wait for Commit. Turn off Manual Commit in the Database menu to run it." + ) + } +} + +internal struct ManualCommitForeignKeyRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "This import turns off foreign key checks, which can't wait for Commit. Turn off Disable foreign key checks or Manual Commit to run it." + ) + } +} + +internal struct ManualCommitUnwrappedImportRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "This import runs each statement on its own, so its changes can't wait for Commit. Turn on Wrap in transaction (BEGIN/COMMIT) or turn off Manual Commit to run it." + ) + } +} + +internal struct ManualCommitDDLRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "This statement commits the transaction open on this connection before it runs. Commit or roll back in TablePro first." + ) + } +} + +internal struct ManualCommitTransactionControlRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { ManualCommit.transactionControlRefusalMessage } +} + +internal struct OpenTransactionRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { ManualCommit.openTransactionRefusalMessage } +} + +internal struct AbortingTransactionRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "A transaction is open on this connection, and a failed statement would abort it. Commit or roll back in TablePro first." + ) + } +} + +// Set by `openIfNeeded` for the session turn it runs in, so the turn records the hold its BEGIN opened. +internal final class ManualCommitTurnHold: @unchecked Sendable { + private let lock = NSLock() + private var held = false + + var isHeld: Bool { lock.withLock { held } } + + func mark(_ value: Bool) { + lock.withLock { held = value } + } +} + +// One statement's run under its connection's commit mode, read on the main actor before the lease. +internal struct ManualCommitRun: Sendable { + let commitMode: ConnectionCommitMode + let databaseType: DatabaseType + let grammar: SQLLexicalGrammar + let connectionId: UUID + + nonisolated func running( + _ statements: [String], + on driver: DatabaseDriver, + _ body: () async throws -> T + ) async throws -> T { + let opened = try await ManualCommit.openIfNeeded( + on: driver, statements: statements, databaseType: databaseType, grammar: grammar, commitMode: commitMode + ) + do { + return try await body() + } catch { + if opened { await ManualCommit.rollBackOpened(on: driver, connectionId: connectionId) } + throw error + } + } +} + +// The hold a Manual Commit connection opens before the first write the user or an agent sends: BEGIN, never COMMIT. +internal enum ManualCommit { + nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "ManualCommit") + + @TaskLocal internal static var turnHold: ManualCommitTurnHold? + + internal enum Decision: Equatable { + case runAsIs + case hold + case refuse + case refuseMixed + + var refusal: (any Error)? { + switch self { + case .runAsIs, .hold: return nil + case .refuse: return ManualCommitRefusal() + case .refuseMixed: return ManualCommitMixedRefusal() + } + } + } + + // Exactly `.idle` holds: `.unknown` and a table lock would let the write commit on its own. + nonisolated internal static func decision( + commitMode: ConnectionCommitMode, + statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar, + supportsTransactions: Bool, + sessionState: PluginSessionTransactionState + ) -> Decision { + guard commitMode == .manual, writes(statements, databaseType: databaseType) else { return .runAsIs } + let family = TransactionEngineFamily.of(databaseType) + guard supportsTransactions, family.wrapsBatchInTransaction else { return .refuse } + switch BatchTransactionPolicy.plan(for: statements, databaseType: databaseType, grammar: grammar) { + case .appTransaction, .heldTransaction: + break + case .autocommit: + let mixed = statements.contains { statement in + !AutocommitOnlyStatement.matches(statement, family: family, grammar: grammar) + && writes([statement], databaseType: databaseType) + } + return mixed ? .refuseMixed : .runAsIs + case .scriptTransaction, .sessionTransaction: + return .runAsIs + } + if commitsHeldTransaction(statements, databaseType: databaseType, grammar: grammar) { + return mixesImplicitCommit(statements, databaseType: databaseType, grammar: grammar) ? .refuseMixed : .runAsIs + } + switch sessionState { + case .idle: + return .hold + case .inTransaction, .abortedTransaction: + return .runAsIs + case .holdsSessionLocks, .unknown: + return .refuse + @unknown default: + return .refuse + } + } + + // A held decision runs held; anything else keeps the plan an automatic connection would use. + nonisolated internal static func runPlan( + text plan: BatchTransactionPlan, + decision: Decision, + sessionState: PluginSessionTransactionState + ) -> BatchTransactionPlan { + decision == .hold ? .heldTransaction : plan.joining(sessionState, commitMode: .automatic) + } + + // Inside the caller's lease, so nothing runs between the state read and the BEGIN. True when it sent the BEGIN. + nonisolated internal static func openIfNeeded( + on driver: DatabaseDriver, + statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar, + commitMode: ConnectionCommitMode + ) async throws -> Bool { + guard commitMode == .manual else { return false } + let state = await driver.heldSessionTransactionState() + let decided = decision( + commitMode: commitMode, + statements: statements, + databaseType: databaseType, + grammar: grammar, + supportsTransactions: driver.supportsTransactions, + sessionState: state + ) + if let refusal = decided.refusal { throw refusal } + guard decided == .hold else { return false } + let mode = OperationKind.worst(of: statements, databaseType: databaseType).transactionAccessMode + try await driver.beginTransaction(mode: mode) + turnHold?.mark(true) + return true + } + + // The app's own writes on a Manual Commit session: one the session can't hold is refused before it runs. + nonisolated internal static func refuseUnheldWrite( + supportsTransactions: Bool, + sessionState: PluginSessionTransactionState + ) throws { + guard supportsTransactions else { throw ManualCommitRefusal() } + switch sessionState { + case .idle, .inTransaction, .abortedTransaction: + return + case .holdsSessionLocks, .unknown: + throw ManualCommitRefusal() + @unknown default: + throw ManualCommitRefusal() + } + } + + // A timed-out read after the turn's own BEGIN still means the transaction it opened. + nonisolated internal static func recordedState( + read: PluginSessionTransactionState, + turnOpenedHold: Bool + ) -> PluginSessionTransactionState { + read == .unknown && turnOpenedHold ? .inTransaction : read + } + + // A failed first write leaves nothing worth keeping, and on PostgreSQL an aborted block that needs a Roll Back. + @MainActor + internal static func rollBackOpened(on driver: DatabaseDriver, connectionId: UUID) async { + let token = DatabaseManager.shared.beginProtectedWrite(on: driver, for: connectionId) + defer { DatabaseManager.shared.endProtectedWrite(token, for: connectionId) } + do { + try await TaskCancellationShield.run { @Sendable in try await driver.rollbackTransaction() } + turnHold?.mark(false) + } catch { + logger.error("Rolling back the held transaction failed: \(error.publicLogShape, privacy: .public)") + } + } + + // DDL on an engine that commits it implicitly ends a transaction already open on the session. + nonisolated internal static func commitsHeldTransaction( + _ statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar + ) -> Bool { + guard TableEditDialect.of(databaseType)?.commitsDDLImplicitly == true else { return false } + return statements.contains { commitsImplicitly($0, databaseType: databaseType, grammar: grammar) } + } + + // On a `commitsDDLImplicitly` engine, an implicit-commit statement beside another write commits that write. + nonisolated internal static func mixesImplicitCommit( + _ statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar + ) -> Bool { + guard commitsHeldTransaction(statements, databaseType: databaseType, grammar: grammar) else { return false } + return statements.contains { statement in + !commitsImplicitly(statement, databaseType: databaseType, grammar: grammar) + && writes([statement], databaseType: databaseType) + } + } + + // The editor asks before an implicit commit only for a run Manual Commit would then let through. + nonisolated internal static func asksBeforeImplicitCommit( + _ statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar, + commitMode: ConnectionCommitMode + ) -> Bool { + guard commitsHeldTransaction(statements, databaseType: databaseType, grammar: grammar) else { return false } + let decided = decision( + commitMode: commitMode, statements: statements, databaseType: databaseType, grammar: grammar, + supportsTransactions: true, sessionState: .inTransaction + ) + return decided.refusal == nil + } + + // A procedure, a dynamic batch or a T-SQL `IF ... ROLLBACK` can end the transaction without leading with the keyword. + nonisolated internal static func mayEndTransactionUnseen( + _ statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar + ) -> Bool { + statements.contains { statement in + CatalogChangeClassifier.effect(of: statement, databaseType: databaseType, grammar: grammar).runsUnseenStatements + || mentionsTransactionControl(statement, grammar: grammar) + } + } + + // END is left out: it closes every T-SQL block and every CASE. + nonisolated private static func mentionsTransactionControl(_ statement: String, grammar: SQLLexicalGrammar) -> Bool { + let text = statement as NSString + var start = 0 + while start < text.length { + var cursor = SQLTokenCursor(text.substring(from: start) as NSString, grammar: grammar) + var previous: String? + while let token = cursor.next() { + guard let word = token.word else { + previous = nil + continue + } + if controlWords.contains(word) { return true } + if word == "TRAN" || word == "TRANSACTION", let previous, transactionOpeners.contains(previous) { + return true + } + previous = word + } + start += cursor.location + 1 + } + return false + } + + private static let controlWords: Set = ["COMMIT", "ROLLBACK", "SAVEPOINT", "RELEASE", "ABORT", "XA"] + private static let transactionOpeners: Set = ["BEGIN", "SAVE", "START", "DISTRIBUTED"] + + // BEGIN and START are read the way a batch is planned, so a PL/SQL or T-SQL block is not taken for one. + nonisolated internal static func controlsTransaction( + _ statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar + ) -> Bool { + if startsTransaction(statements, databaseType: databaseType, grammar: grammar) { return true } + return statements.contains { statement in + if CatalogChangeClassifier.effect(of: statement, databaseType: databaseType, grammar: grammar).endsTransaction { + return true + } + return transactionControlKeywords.contains(QueryClassifier.leadingKeyword(of: statement, grammar: grammar)) + } + } + + nonisolated internal static func startsTransaction( + _ statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar + ) -> Bool { + BatchTransactionPolicy.plan(for: statements, databaseType: databaseType, grammar: grammar) == .scriptTransaction + } + + nonisolated private static func writes(_ statements: [String], databaseType: DatabaseType) -> Bool { + OperationKind.worst(of: statements, databaseType: databaseType).declaresWrite + } + + nonisolated private static func commitsImplicitly( + _ statement: String, + databaseType: DatabaseType, + grammar: SQLLexicalGrammar + ) -> Bool { + let effect = CatalogChangeClassifier.effect(of: statement, databaseType: databaseType, grammar: grammar) + if !effect.kinds.isEmpty, !effect.runsUnseenStatements { return true } + return implicitCommitKeywords.contains(QueryClassifier.leadingKeyword(of: statement, grammar: grammar)) + } + + // Changes no catalog the sidebar lists, and still commits on MySQL and Oracle. + private static let implicitCommitKeywords: Set = ["TRUNCATE", "RENAME", "GRANT", "REVOKE", "LOCK"] + + private static let transactionControlKeywords: Set = [ + "COMMIT", "ROLLBACK", "END", "ABORT", "SAVEPOINT", "RELEASE", "XA", + ] + + internal static var transactionEndedNotice: String { + String(localized: "The open transaction on this connection ended while this statement ran.") + } + + internal static var pendingNotice: String { + String(localized: "Manual Commit is on for this connection, so this change is pending until the user commits it in TablePro.") + } + + internal static var transactionControlRefusalMessage: String { + String(localized: "Manual Commit is on for this connection. Commit or roll back in TablePro.") + } + + internal static var openTransactionRefusalMessage: String { + String( + localized: "A transaction is already open on this connection, or it cannot say whether one is. Commit or roll it back first." + ) + } +} diff --git a/apple/TablePro/Core/Services/Execution/MultiStatementFailure.swift b/apple/TablePro/Core/Services/Execution/MultiStatementFailure.swift index d4b7700a1e..1c94bf5b64 100644 --- a/apple/TablePro/Core/Services/Execution/MultiStatementFailure.swift +++ b/apple/TablePro/Core/Services/Execution/MultiStatementFailure.swift @@ -133,7 +133,7 @@ internal struct MultiStatementFailureContext: Equatable, Sendable { /// What became of the statements that ran before the failure. private func standingWorkNote(position: Int) -> String? { switch plan { - case .appTransaction, .scriptTransaction: + case .appTransaction, .heldTransaction, .scriptTransaction: return nil case .autocommit: return appliedWorkNote(position: position) diff --git a/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift b/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift index bc761bd61d..1f5c76f366 100644 --- a/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift +++ b/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift @@ -64,6 +64,21 @@ internal enum TransactionEngineFamily: String, Hashable, Sendable, CaseIterable self == .oracle } + // After a failed statement the block takes nothing but a rollback, so one error costs every pending change. + internal var failedStatementAbortsTransaction: Bool { + switch self { + case .postgres, .redshift, .cockroach, .duckdb: + return true + case .mysql, .sqlite, .sqlServer, .oracle, .redis, .other: + return false + } + } + + // Redshift and DuckDB have no savepoint to take a failed statement back to. + internal var takesBackAFailedStatement: Bool { + self == .postgres || self == .cockroach + } + private static let familiesByTypeId: [String: TransactionEngineFamily] = [ "PostgreSQL": .postgres, "PGlite": .postgres, diff --git a/apple/TablePro/Core/Services/Execution/WriteTransactionOwner.swift b/apple/TablePro/Core/Services/Execution/WriteTransactionOwner.swift index 74cd43e9b4..e0f2587eb6 100644 --- a/apple/TablePro/Core/Services/Execution/WriteTransactionOwner.swift +++ b/apple/TablePro/Core/Services/Execution/WriteTransactionOwner.swift @@ -14,31 +14,55 @@ import TableProPluginKit /// transaction of its own then commits their pending work on PostgreSQL, implicitly commits it on /// MySQL and aborts it on DuckDB, with nothing raised and the write reporting success. /// -/// This is the one place that decision is made. ``BatchTransactionPlan/joining(_:)`` is the same -/// decision for a multi-statement run, where the statement text has a say as well. +/// This is the one place that decision is made. ``BatchTransactionPlan/joining(_:commitMode:)`` is +/// the same decision for a multi-statement run, where the statement text has a say as well. internal enum WriteTransactionOwner: Equatable { /// The app opens the transaction, verifies inside it, and commits or rolls it back. case app + // Manual Commit: the app opens the transaction and leaves it open for the user to commit, rolling back only a failure. + case held /// The session already holds one. The write joins it and sends no `BEGIN`, `COMMIT` or /// `ROLLBACK`: ending it belongs to whoever opened it. case session /// The engine has no transactions, so every statement is on the server as it runs. case none + // No default for the mode: a writer off the session's turn must say `.automatic`, or its hold is never seen. internal static func resolve( supportsTransactions: Bool, - sessionState: PluginSessionTransactionState + sessionState: PluginSessionTransactionState, + commitMode: ConnectionCommitMode ) -> WriteTransactionOwner { guard supportsTransactions else { return .none } + if commitMode == .manual, sessionState == .idle { return .held } return sessionState.permitsAppTransaction ? .app : .session } - internal var opensTransaction: Bool { - self == .app + internal var sendsBegin: Bool { + switch self { + case .app, .held: + return true + case .session, .none: + return false + } + } + + internal var commitsAtEnd: Bool { + switch self { + case .app: + return true + case .held, .session, .none: + return false + } } /// Whether the statements that already ran can still be taken back by the app. internal var canRollBack: Bool { - self == .app + switch self { + case .app, .held: + return true + case .session, .none: + return false + } } } diff --git a/apple/TablePro/Core/Services/Export/ImportService.swift b/apple/TablePro/Core/Services/Export/ImportService.swift index 3f0e86c491..ed91c4351f 100644 --- a/apple/TablePro/Core/Services/Export/ImportService.swift +++ b/apple/TablePro/Core/Services/Export/ImportService.swift @@ -413,7 +413,13 @@ final class ImportService: ObservableObject { policy: RowImportBatchPolicy ) async throws -> RowImportBatchReport { let started = Date() - let owner = ImportDataSinkAdapter(driver: driver, databaseType: connection.type, importRun: run, fileIndex: nil) + let owner = ImportDataSinkAdapter( + driver: driver, + databaseType: connection.type, + commitMode: DatabaseManager.shared.commitMode(for: connection.id), + importRun: run, + fileIndex: nil + ) var sessionScope: PluginSessionScope? var outcomes = [RowImportBatchReport.FileOutcome](repeating: .notRun, count: files.count) var ending = RowImportBatchReport.Ending.completed @@ -644,6 +650,7 @@ final class ImportService: ObservableObject { let sink = ImportDataSinkAdapter( driver: driver, databaseType: connection.type, targetTable: targetTable, columnMapping: columnMapping, sourceFields: sourceFields, rowWriting: rowWriting, + commitMode: DatabaseManager.shared.commitMode(for: connection.id), importRun: run, isCancelled: { progress.isCancelled } ) defer { diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+AutoRefresh.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+AutoRefresh.swift new file mode 100644 index 0000000000..d560a463f7 --- /dev/null +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+AutoRefresh.swift @@ -0,0 +1,24 @@ +// +// MainSplitViewController+AutoRefresh.swift +// TablePro +// + +import AppKit + +extension MainSplitViewController { + @objc func setTableAutoRefreshInterval(_ sender: Any?) { + guard let raw = (sender as? NSMenuItem)?.representedObject as? Int, + let interval = TableAutoRefreshInterval(rawValue: raw) else { return } + commandActions?.setTableAutoRefreshInterval(interval) + } + + // Nil off a table tab, so the submenu's checkmark never names another tab's interval. + var tableAutoRefreshInterval: TableAutoRefreshInterval? { + guard contentMode == .browse, isConnected else { return nil } + return commandActions?.tableAutoRefreshInterval + } + + var canSetTableAutoRefresh: Bool { + tableAutoRefreshInterval != nil && !(isViewLoaded && view.window?.attachedSheet != nil) + } +} diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+DatabaseMenuActions.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+DatabaseMenuActions.swift index ce3c3232e6..4ba661bcc9 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+DatabaseMenuActions.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+DatabaseMenuActions.swift @@ -124,6 +124,60 @@ extension MainSplitViewController { workspaces.selected?.sessionState?.coordinator.safeModeStatus } + // Turning it off leaves a transaction already held open, and the bar with it. + @objc func toggleManualCommit(_ sender: Any?) { + guard let connectionId = workspaces.selectedConnectionId, + let session = DatabaseManager.shared.session(for: connectionId) else { return } + DatabaseManager.shared.setCommitMode(session.commitMode == .manual ? .automatic : .manual, for: connectionId) + } + + @objc func commitOpenTransaction(_ sender: Any?) { + guard let hold = selectedTransactionHold else { return } + let window = view.window + Task { @MainActor in + await ConnectionTransactionActions.perform(window: window) { + try await ConnectionTransactionActions.commit(hold) + } + } + } + + @objc func rollBackOpenTransaction(_ sender: Any?) { + guard let hold = selectedTransactionHold else { return } + let window = view.window + Task { @MainActor in + await ConnectionTransactionActions.perform(window: window) { + try await ConnectionTransactionActions.rollBack(hold, window: window) + } + } + } + + // The hold the selected tab's database runs on: the connection's one driver, or that database's own lane. + var selectedTransactionHold: SessionTransactionTracker.Hold? { + guard let coordinator = workspaces.selected?.sessionState?.coordinator else { return nil } + let container = coordinator.tabManager.selectedTab.flatMap { coordinator.scope(for: $0)?.database } + ?? coordinator.browseDatabaseName + return DatabaseManager.shared.sessionTransactions.hold(connectionId: coordinator.connectionId, container: container) + } + + var transactionMenuState: TransactionMenuState { + guard let coordinator = workspaces.selected?.sessionState?.coordinator, + let session = DatabaseManager.shared.session(for: coordinator.connectionId) + else { return TransactionMenuState() } + let container = coordinator.tabManager.selectedTab.flatMap { coordinator.scope(for: $0)?.database } + ?? coordinator.browseDatabaseName + let reportsState = (session.driver?.supportsTransactions ?? false) + && !DatabaseManager.shared.sessionTransactions.cannotReportState( + connectionId: coordinator.connectionId, container: container + ) + let hold = selectedTransactionHold + return TransactionMenuState( + isManualCommitOn: session.commitMode == .manual, + canHoldTransactions: reportsState, + holdsTransaction: hold != nil, + canCommit: hold.map { ConnectionTransactionActions.offersCommit($0, safeModeLevel: session.safeModeLevel) } ?? false + ) + } + @objc func switchSessionContext(_ sender: Any?) { guard let selection = (sender as? NSMenuItem)?.representedObject as? SessionContextSelection, let coordinator = commandActions?.coordinator else { return } diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift index af0799bf23..a85ba0b621 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift @@ -6,6 +6,15 @@ import AppKit import TableProPluginKit +// Read only when a transaction command is validated, as the Debug menu's state is. +struct TransactionMenuState: Equatable { + var isManualCommitOn = false + // False for an engine with no transactions, or one whose live driver has only answered that it cannot say. + var canHoldTransactions = false + var holdsTransaction = false + var canCommit = false +} + // A plain value keeps `isEnabled` pure and testable, the same split `ToolbarContextResolver` uses for the toolbar. struct MenuValidationContext: Equatable { // True for any connection on screen, connected or not, so a pane that failed to dial can still be dismissed. @@ -140,6 +149,7 @@ struct MenuValidationContext: Equatable { var hasSchemaFilter = false // Filled only when a Debug menu item is validated; the menu exists only while the module is on. var routineDebug = RoutineDebugMenuState() + var transaction = TransactionMenuState() var canOpenLiveMessages = false var canDisconnect = false var isPresentingSheet = false @@ -358,10 +368,35 @@ extension MainSplitViewController: NSMenuItemValidation { case #selector(showAllSchemas(_:)): return context.isConnected && context.canFilterSchemas && context.hasSchemaFilter default: - return nil + return isTransactionCommandEnabled(selector, context: context) } } + // Manual Commit stays enabled while on, so a connection that cannot hold can still turn it off. + private static func isTransactionCommandEnabled( + _ selector: Selector, + context: MenuValidationContext + ) -> Bool? { + guard transactionSelectors.contains(selector) else { return nil } + guard context.isConnected else { return false } + switch selector { + case #selector(toggleManualCommit(_:)): + return context.transaction.isManualCommitOn || context.transaction.canHoldTransactions + case #selector(commitOpenTransaction(_:)): + return context.transaction.canCommit + case #selector(rollBackOpenTransaction(_:)): + return context.transaction.holdsTransaction + default: + return false + } + } + + static let transactionSelectors: Set = [ + #selector(toggleManualCommit(_:)), + #selector(commitOpenTransaction(_:)), + #selector(rollBackOpenTransaction(_:)), + ] + // Agent mode does not draw the debug tab these act on. private static func isRoutineDebugCommandEnabled( _ selector: Selector, @@ -471,6 +506,9 @@ extension MainSplitViewController: NSMenuItemValidation { #selector(closeConnection(_:)), #selector(requestDisconnect), #selector(setSafeModeLevel(_:)), + #selector(toggleManualCommit(_:)), + #selector(commitOpenTransaction(_:)), + #selector(rollBackOpenTransaction(_:)), #selector(releaseFileLock(_:)), #selector(openContainerSwitcher(_:)), #selector(openSchemaSwitcher(_:)), @@ -539,6 +577,8 @@ extension MainSplitViewController: NSMenuItemValidation { #selector(closeAgentSession(_:)), #selector(deleteAgentSession(_:)), #selector(clearAIConversations(_:)), + #selector(commitOpenTransaction(_:)), + #selector(rollBackOpenTransaction(_:)), ] // A command this window does not own stays enabled, which keeps `performClose:` and the system's own items working. @@ -596,6 +636,9 @@ extension MainSplitViewController: NSMenuItemValidation { return context.canClearResults case #selector(saveAsFavorite(_:)): return context.canSaveAsFavorite + // The versions are on this Mac, so a tab whose connection dropped can still go back. + case #selector(revertToQueryTabVersion(_:)), #selector(browseQueryTabVersions(_:)): + return context.isQueryTab default: return nil } @@ -766,12 +809,14 @@ extension MainSplitViewController: NSMenuItemValidation { func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { applyDynamicTitle(to: menuItem) guard let action = menuItem.action else { return false } + if menuItem.identifier == DatabaseMenuBuilder.manualCommitFootnoteIdentifier { return false } // AppKit asks this for the View menu and `validateUserInterfaceItem` for the rest, so a rule must live in both. if action == #selector(toggleSidebar(_:)) { return true } if action == #selector(toggleInspector(_:)) { return canToggleTrailingPane } if action == #selector(toggleAssistant(_:)) { return canToggleAssistant } if action == #selector(setResultView(_:)) { return canShowResultView(menuItem) } if action == #selector(setSafeModeLevel(_:)) { return canChooseSafeModeLevel(menuItem) } + if action == #selector(setTableAutoRefreshInterval(_:)) { return canSetTableAutoRefresh } if action == #selector(retryConnection) { return canReconnect } if let zoom = FontSizeZoom.editor.enablement(action, typography: AppSettingsManager.shared.typography) { return zoom @@ -792,6 +837,9 @@ extension MainSplitViewController: NSMenuItemValidation { if action == #selector(openLiveMessages(_:)) { context.canOpenLiveMessages = canOpenLiveMessages } + if Self.transactionSelectors.contains(action) { + context.transaction = transactionMenuState + } guard Self.agentSessionSelectors.contains(action) else { return context } context.agentSessionTarget = agentSessionTarget(for: menuItem)?.status return context @@ -867,6 +915,8 @@ extension MainSplitViewController: NSMenuItemValidation { setState(isCurrentResultView(menuItem) ? .on : .off, on: menuItem) case #selector(setContentModeFromMenu(_:)): setState(isCurrentContentMode(menuItem) ? .on : .off, on: menuItem) + case #selector(toggleManualCommit(_:)): + applyManualCommitState(to: menuItem) case #selector(useFlatSidebarLayout(_:)): setState(commandActions?.sidebarLayout == .flat ? .on : .off, on: menuItem) case #selector(useTreeSidebarLayout(_:)): @@ -880,6 +930,18 @@ extension MainSplitViewController: NSMenuItemValidation { } } + private func applyManualCommitState(to menuItem: NSMenuItem) { + guard menuItem.identifier != DatabaseMenuBuilder.manualCommitFootnoteIdentifier else { return } + let state = transactionMenuState + setState(state.isManualCommitOn ? .on : .off, on: menuItem) + guard let footnote = menuItem.menu?.items.first(where: { + $0.identifier == DatabaseMenuBuilder.manualCommitFootnoteIdentifier + }) else { return } + // While on, a write is refused rather than committed, which the footnote's wording does not cover. + let hidden = !isConnected || state.canHoldTransactions || state.isManualCommitOn + if footnote.isHidden != hidden { footnote.isHidden = hidden } + } + // The mode is in `representedObject`, so enablement needs the item, not the selector the shared table keys on. private func canShowResultView(_ menuItem: NSMenuItem) -> Bool { guard contentMode == .browse else { return false } diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+QueryTabVersions.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+QueryTabVersions.swift new file mode 100644 index 0000000000..a36806943f --- /dev/null +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+QueryTabVersions.swift @@ -0,0 +1,23 @@ +// +// MainSplitViewController+QueryTabVersions.swift +// TablePro +// + +import AppKit + +extension MainSplitViewController { + @objc func revertToQueryTabVersion(_ sender: Any?) { + guard let choice = (sender as? NSMenuItem)?.representedObject as? QueryTabRevertChoice else { return } + commandActions?.coordinator?.revertQueryTab(choice.tabId, toVersion: choice.versionId) + } + + @objc func browseQueryTabVersions(_ sender: Any?) { + guard let tabId = selectedQueryTabId else { return } + commandActions?.coordinator?.showVersionHistory(ofQueryTab: tabId) + } + + var selectedQueryTabId: UUID? { + guard let tab = commandActions?.coordinator?.tabManager.selectedTab, tab.tabType == .query else { return nil } + return tab.id + } +} diff --git a/apple/TablePro/Core/Services/Infrastructure/QuitApproval.swift b/apple/TablePro/Core/Services/Infrastructure/QuitApproval.swift index 9d14e0be65..9750866d0c 100644 --- a/apple/TablePro/Core/Services/Infrastructure/QuitApproval.swift +++ b/apple/TablePro/Core/Services/Infrastructure/QuitApproval.swift @@ -27,15 +27,59 @@ internal enum QuitApproval { EditorEventRouter.shared.flushAllPendingText() let work = registry.items(in: .app) let hasUnsavedTabs = MainContentCoordinator.hasAnyUnsavedChanges() - let losses = hasUnsavedTabs ? [String(localized: "Unsaved edits in open tabs")] : [] + let transactions = openTransactionNames( + holds: Array(DatabaseManager.shared.sessionTransactions.holds.values), + connectionName: { DatabaseManager.shared.session(for: $0)?.connection.name } + ) + let losses = (hasUnsavedTabs ? [String(localized: "Unsaved edits in open tabs")] : []) + + RunningWorkPrompt.transactionLines(for: transactions) if let prompt = RunningWorkPrompt.make(action: action, work: work, otherLosses: losses) { guard RunningWorkApproval.confirmModally(prompt) else { return false } if stopsWork { registry.cancel(work) } return true } + if let prompt = transactionPrompt(action: action, connections: transactions, hasUnsavedTabs: hasUnsavedTabs) { + return RunningWorkApproval.confirmModally(prompt) + } return !hasUnsavedTabs || approvesDiscardingUnsavedTabs() } + // The server rolls back what a quit leaves open, so a hold is asked about like unsaved work. + internal static func openTransactionNames( + holds: [SessionTransactionTracker.Hold], + connectionName: (UUID) -> String? + ) -> [String] { + var names: [String] = [] + for hold in holds { + guard let name = connectionName(hold.connectionId), !names.contains(name) else { continue } + names.append(name) + } + return names.sorted { $0.localizedStandardCompare($1) == .orderedAscending } + } + + internal static func transactionPrompt( + action: RunningWorkAction, + connections: [String], + hasUnsavedTabs: Bool + ) -> RunningWorkPrompt? { + guard let first = connections.first else { return nil } + let rolledBack = connections.count == 1 + ? String(format: String(localized: "Uncommitted changes on “%@” are rolled back when TablePro quits."), first) + : String( + format: String(localized: "Uncommitted changes on %@ are rolled back when TablePro quits."), + ListFormatter.localizedString(byJoining: connections.map { "“\($0)”" }) + ) + let unsaved = String(localized: "Some tabs have unsaved edits. Quitting will discard these changes.") + let relaunches = action == .relaunch + return RunningWorkPrompt( + title: relaunches + ? String(localized: "Relaunch with an Open Transaction?") + : String(localized: "Quit with an Open Transaction?"), + message: hasUnsavedTabs ? "\(rolledBack)\n\n\(unsaved)" : rolledBack, + confirmButton: relaunches ? String(localized: "Relaunch Anyway") : String(localized: "Quit Anyway") + ) + } + private static func approvesDiscardingUnsavedTabs() -> Bool { // A quit can come from outside the app, and a background process has no Dock icon to reach the alert by. AppActivationPolicyController.shared.activate(ignoringOtherApps: true) diff --git a/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService+CatalogChange.swift b/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService+CatalogChange.swift index 4e47982744..00b95434c5 100644 --- a/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService+CatalogChange.swift +++ b/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService+CatalogChange.swift @@ -18,11 +18,13 @@ struct CatalogTreeRefreshPlan: Equatable, Sendable { var routines: Set = [] var triggers: Set = [] var types: Set = [] + var serverObjects: Set = [] var allSchemaTables: Set = [] var isEmpty: Bool { !refreshesDatabaseList && schemaLists.isEmpty && tables.isEmpty && partitions.isEmpty - && routines.isEmpty && triggers.isEmpty && types.isEmpty && allSchemaTables.isEmpty + && routines.isEmpty && triggers.isEmpty && types.isEmpty && serverObjects.isEmpty + && allSchemaTables.isEmpty } } @@ -43,6 +45,7 @@ extension DatabaseTreeMetadataService { routineKeys: routinesState.keys, triggerKeys: triggersState.keys, typeKeys: typesState.keys, + serverObjectKeys: serverObjectsState.keys, allSchemaTableKeys: allSchemaTablesState.keys ) guard !plan.isEmpty else { return } @@ -65,6 +68,7 @@ extension DatabaseTreeMetadataService { routineKeys: routinesState.keys, triggerKeys: triggersState.keys, typeKeys: typesState.keys, + serverObjectKeys: serverObjectsState.keys, allSchemaTableKeys: allSchemaTablesState.keys ) markAllSchemaTablesChanged(plan.allSchemaTables) @@ -80,6 +84,7 @@ extension DatabaseTreeMetadataService { routineKeys: some Sequence, triggerKeys: some Sequence, typeKeys: some Sequence, + serverObjectKeys: some Sequence = EmptyCollection(), allSchemaTableKeys: some Sequence = EmptyCollection() ) -> CatalogTreeRefreshPlan { func reached(_ key: ObjectsKey) -> Bool { @@ -108,6 +113,14 @@ extension DatabaseTreeMetadataService { plan.routines = objectKeys(routineKeys, for: .routines) plan.triggers = objectKeys(triggerKeys, for: .triggers) plan.types = objectKeys(typeKeys, for: .types) + // A key with no schema lists a whole database, which a change in any of its schemas reaches. + if change.kinds.contains(.serverObjects) { + plan.serverObjects = Set(serverObjectKeys.filter { key in + guard key.connectionId == change.connectionId else { return false } + guard let schema = key.schema else { return change.reaches(database: key.database) } + return change.reaches(database: key.database, schema: schema) + }) + } if !change.kinds.isDisjoint(with: [.tables, .schemas]) { plan.allSchemaTables = Set(allSchemaTableKeys.filter { key in key.connectionId == change.connectionId && change.reaches(database: key.database) diff --git a/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService.swift b/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService.swift index f7e894e09b..5897d2c968 100644 --- a/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService.swift +++ b/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService.swift @@ -26,6 +26,14 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { let schema: String? } + // A `.database` kind, and every kind on an engine without schemas, is keyed with no schema. + struct ServerObjectsKey: Hashable, Sendable { + let connectionId: UUID + let database: String + let schema: String? + let kindId: String + } + struct PartitionsKey: Hashable, Sendable { let connectionId: UUID let database: String @@ -66,6 +74,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { @Published private(set) var routinesState: [ObjectsKey: MetadataLoadState<[RoutineInfo]>] = [:] @Published private(set) var triggersState: [ObjectsKey: MetadataLoadState<[TriggerInfo]>] = [:] @Published private(set) var typesState: [ObjectsKey: MetadataLoadState<[UserDefinedTypeInfo]>] = [:] + @Published private(set) var serverObjectsState: [ServerObjectsKey: MetadataLoadState<[ServerObjectInfo]>] = [:] @Published private(set) var partitionsState: [PartitionsKey: MetadataLoadState<[PartitionInfo]>] = [:] @Published private(set) var allSchemaTablesState: [DatabaseKey: MetadataLoadState] = [:] @Published private(set) var listsRevisions: [UUID: Int] = [:] @@ -76,6 +85,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { private let routinesDedup = OnceTask, [RoutineInfo]>() private let triggersDedup = OnceTask, [TriggerInfo]>() private let typesDedup = OnceTask, [UserDefinedTypeInfo]>() + private let serverObjectsDedup = OnceTask, [ServerObjectInfo]>() private let partitionsDedup = OnceTask, [PartitionInfo]>() private let allSchemaTablesDedup = OnceTask() @@ -85,6 +95,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { private var routinesCommit = ListCommitState() private var triggersCommit = ListCommitState() private var typesCommit = ListCommitState() + private var serverObjectsCommit = ListCommitState() private var partitionsCommit = ListCommitState() private var allSchemaTablesFence = CommitFence() private var allSchemaTablesFreshness = CatalogFreshness() @@ -117,6 +128,12 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { TreeList(state: \.typesState, commit: \.typesCommit, dedup: typesDedup, label: "types") } + private var serverObjectLists: TreeList { + TreeList( + state: \.serverObjectsState, commit: \.serverObjectsCommit, dedup: serverObjectsDedup, label: "server objects" + ) + } + private var partitionLists: TreeList { TreeList(state: \.partitionsState, commit: \.partitionsCommit, dedup: partitionsDedup, label: "partitions") } @@ -173,6 +190,45 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { typesState[Self.objectsKey(connectionId: connectionId, database: database, schema: schema)]?.value ?? [] } + func serverObjectsLoadState( + connectionId: UUID, database: String, schema: String?, kindId: String + ) -> MetadataLoadState<[ServerObjectInfo]> { + serverObjectsState[Self.serverObjectsKey(connectionId: connectionId, database: database, schema: schema, kindId: kindId)] + ?? .idle + } + + func serverObjects(connectionId: UUID, database: String, schema: String?, kindId: String) -> [ServerObjectInfo] { + serverObjectsLoadState(connectionId: connectionId, database: database, schema: schema, kindId: kindId).value ?? [] + } + + func isServerObjectsCurrent(connectionId: UUID, database: String, schema: String?, kindId: String) -> Bool { + isConnected(connectionId) && serverObjectsCommit.freshness.isCurrent( + Self.serverObjectsKey(connectionId: connectionId, database: database, schema: schema, kindId: kindId) + ) + } + + // A container lists the kinds of its own level: a schema its `.container` kinds, a schema engine's + // database its `.database` kinds, and a database without schemas every kind. + nonisolated static func serverKinds( + declared: [SidebarServerObjectKind], + schema: String?, + listsTablesPerSchema: Bool + ) -> [SidebarServerObjectKind] { + if schema?.isEmpty == false { return declared.filter { $0.scope == .container } } + return listsTablesPerSchema ? declared.filter { $0.scope == .database } : declared + } + + func serverKinds(connectionId: UUID, database: String, schema: String?) -> [SidebarServerObjectKind] { + guard let session = DatabaseManager.shared.session(for: connectionId) else { return [] } + return Self.serverKinds( + declared: SidebarServerObjectKind.declared(by: DatabaseManager.shared.driver(for: connectionId)), + schema: schema, + listsTablesPerSchema: Self.listsTablesPerSchema( + PluginManager.shared.databaseGroupingStrategy(for: session.connection.type) + ) + ) + } + func partitionsLoadState( connectionId: UUID, database: String, schema: String?, table: String ) -> MetadataLoadState<[PartitionInfo]> { @@ -235,6 +291,12 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { .isEmpty } + func serverObjectsNeedFetch(connectionId: UUID, database: String, schema: String?) -> Bool { + guard isConnected(connectionId) else { return false } + return !serverKindsNeedingFetch(Self.objectsKey(connectionId: connectionId, database: database, schema: schema)) + .isEmpty + } + func partitionsNeedFetch(connectionId: UUID, database: String, schema: String?, table: String) -> Bool { isConnected(connectionId) && partitionsCommit.freshness.needsFetch( Self.partitionsKey(connectionId: connectionId, database: database, schema: schema, table: table) @@ -272,6 +334,10 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { typesCommit.freshness.markChanged(key) marked = true } + for key in plan.serverObjects { + serverObjectsCommit.freshness.markChanged(key) + marked = true + } guard marked else { return } listsRevisions[connectionId, default: 0] &+= 1 } @@ -282,9 +348,16 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { if routinesCommit.freshness.needsFetch(key) { kinds.insert(.routines) } if browsesTriggers(key.connectionId), triggersCommit.freshness.needsFetch(key) { kinds.insert(.triggers) } if browsesUserDefinedTypes(key.connectionId), typesCommit.freshness.needsFetch(key) { kinds.insert(.types) } + if !serverKindsNeedingFetch(key).isEmpty { kinds.insert(.serverObjects) } return kinds } + private func serverKindsNeedingFetch(_ key: ObjectsKey) -> [SidebarServerObjectKind] { + serverKinds(connectionId: key.connectionId, database: key.database, schema: key.schema).filter { + serverObjectsCommit.freshness.needsFetch(serverObjectsKey(key, kindId: $0.id)) + } + } + // MARK: - All-schema tables /// Read when next asked for, never on a catalog change itself: a COMMIT is one too, and relisting @@ -532,9 +605,29 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { await self.loadUserDefinedTypes(connectionId: connectionId, database: database, schema: schema) } } + if kinds.contains(.serverObjects) { + group.addTask { await self.loadServerObjects(connectionId: connectionId, database: database, schema: schema) } + } } } + // Only the kinds of this container's level, and only those a change overtook or nobody has read. + func loadServerObjects(connectionId: UUID, database: String, schema: String?) async { + guard isConnected(connectionId) else { return } + let container = Self.objectsKey(connectionId: connectionId, database: database, schema: schema) + let kinds = serverKindsNeedingFetch(container) + await withTaskGroup(of: Void.self) { group in + for kind in kinds { + let key = serverObjectsKey(container, kindId: kind.id) + group.addTask { await self.loadServerObjectList(key) } + } + } + } + + private func loadServerObjectList(_ key: ServerObjectsKey) async { + await read(key, connectionId: key.connectionId, in: serverObjectLists, fetch: serverObjectListFetch(key)) + } + // MARK: - Refresh /// Fetches first and commits over the old list, so a refresh never empties the tree @@ -573,7 +666,18 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { async let types: Void = refreshUserDefinedTypeObjects( connectionId: connectionId, database: database, schema: schema ) - _ = await (tables, routines, triggers, types) + async let serverObjects: Void = refreshServerObjects(connectionId: connectionId, database: database, schema: schema) + _ = await (tables, routines, triggers, types, serverObjects) + } + + func refreshServerObjects(connectionId: UUID, database: String, schema: String?, kindId: String? = nil) async { + let container = Self.objectsKey(connectionId: connectionId, database: database, schema: schema) + let kinds = serverKinds(connectionId: connectionId, database: database, schema: schema) + .filter { kindId == nil || $0.id == kindId } + for kind in kinds { + let key = serverObjectsKey(container, kindId: kind.id) + await reread(key, connectionId: connectionId, in: serverObjectLists, fetch: serverObjectListFetch(key)) + } } /// A row refreshes only the fetch its kind comes from. Partitions ride with the tables, because @@ -766,6 +870,16 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { } } + private func serverObjectListFetch(_ key: ServerObjectsKey) -> @Sendable () async throws -> [ServerObjectInfo] { + let schema = key.schema + let kindId = key.kindId + return { [self] in + try await withDriver(connectionId: key.connectionId, database: key.database, workload: .bulk) { driver in + try await driver.fetchServerObjects(ofKind: kindId, schema: schema) + } + } + } + private func partitionListFetch(_ key: PartitionsKey) -> @Sendable () async throws -> [PartitionInfo] { let schema = key.schema let table = key.table @@ -800,6 +914,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { routinesCommit.freshness.removeAll { $0.connectionId == connectionId } triggersCommit.freshness.removeAll { $0.connectionId == connectionId } typesCommit.freshness.removeAll { $0.connectionId == connectionId } + serverObjectsCommit.freshness.removeAll { $0.connectionId == connectionId } partitionsCommit.freshness.removeAll { $0.connectionId == connectionId } allSchemaTablesFreshness.removeAll { $0.connectionId == connectionId } allSchemaTablesRequested = allSchemaTablesRequested.filter { $0.key.connectionId != connectionId } @@ -812,6 +927,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { routinesState = routinesState.filter { $0.key.connectionId != connectionId } triggersState = triggersState.filter { $0.key.connectionId != connectionId } typesState = typesState.filter { $0.key.connectionId != connectionId } + serverObjectsState = serverObjectsState.filter { $0.key.connectionId != connectionId } partitionsState = partitionsState.filter { $0.key.connectionId != connectionId } } @@ -835,6 +951,9 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { triggersCommit.fence.supersede(key) typesCommit.fence.supersede(key) } + for key in serverObjectsState.keys where key.connectionId == connectionId { + serverObjectsCommit.fence.supersede(key) + } for key in connectionPartitionKeys(connectionId) { partitionsCommit.fence.supersede(key) } @@ -850,6 +969,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { await routinesDedup.cancel { $0.key.connectionId == connectionId } await triggersDedup.cancel { $0.key.connectionId == connectionId } await typesDedup.cancel { $0.key.connectionId == connectionId } + await serverObjectsDedup.cancel { $0.key.connectionId == connectionId } await partitionsDedup.cancel { $0.key.connectionId == connectionId } await allSchemaTablesDedup.cancel { $0.database.connectionId == connectionId } } @@ -863,6 +983,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { idlePending(\.routinesState) { $0.connectionId == connectionId } idlePending(\.triggersState) { $0.connectionId == connectionId } idlePending(\.typesState) { $0.connectionId == connectionId } + idlePending(\.serverObjectsState) { $0.connectionId == connectionId } idlePending(\.partitionsState) { $0.connectionId == connectionId } idlePending(\.allSchemaTablesState) { $0.connectionId == connectionId } } @@ -922,6 +1043,17 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { return ObjectsKey(connectionId: connectionId, database: database, schema: normalized) } + private static func serverObjectsKey( + connectionId: UUID, database: String, schema: String?, kindId: String + ) -> ServerObjectsKey { + let normalized: String? = (schema?.isEmpty == true) ? nil : schema + return ServerObjectsKey(connectionId: connectionId, database: database, schema: normalized, kindId: kindId) + } + + private func serverObjectsKey(_ container: ObjectsKey, kindId: String) -> ServerObjectsKey { + ServerObjectsKey(connectionId: container.connectionId, database: container.database, schema: container.schema, kindId: kindId) + } + private static func partitionsKey( connectionId: UUID, database: String, schema: String?, table: String ) -> PartitionsKey { diff --git a/apple/TablePro/Core/Services/Query/SchemaService+ServerObjects.swift b/apple/TablePro/Core/Services/Query/SchemaService+ServerObjects.swift new file mode 100644 index 0000000000..e8493eb51a --- /dev/null +++ b/apple/TablePro/Core/Services/Query/SchemaService+ServerObjects.swift @@ -0,0 +1,60 @@ +// +// SchemaService+ServerObjects.swift +// TablePro +// + +import Foundation + +extension SchemaService { + func serverObjects(for connectionId: UUID, kindId: String) -> [ServerObjectInfo] { + serverObjectsLoadState(for: connectionId, kindId: kindId).value ?? [] + } + + func serverObjectsLoadState(for connectionId: UUID, kindId: String) -> MetadataLoadState<[ServerObjectInfo]> { + sideObjects[shownKey(connectionId)]?.serverObjects[kindId] ?? .idle + } + + func serverObjectStates(for connectionId: UUID) -> [String: MetadataLoadState<[ServerObjectInfo]>] { + sideObjects[shownKey(connectionId)]?.serverObjects ?? [:] + } + + func serverObjects(for connectionId: UUID, schema: String, kindId: String) -> [ServerObjectInfo] { + serverObjectsLoadState(for: connectionId, schema: schema, kindId: kindId).value ?? [] + } + + func serverObjectsByKind(for connectionId: UUID, schema: String) -> [String: [ServerObjectInfo]] { + (perSchemaSideObjects[catalogKey(connectionId, schema: schema)]?.serverObjects ?? [:]).compactMapValues(\.value) + } + + func serverObjectsLoadState( + for connectionId: UUID, + schema: String, + kindId: String + ) -> MetadataLoadState<[ServerObjectInfo]> { + perSchemaSideObjects[catalogKey(connectionId, schema: schema)]?.serverObjects[kindId] ?? .idle + } + + @discardableResult + func reloadServerObjects( + connectionId: UUID, + driver: DatabaseDriver, + scope: DatabaseScope?, + kindId: String + ) async -> Bool { + let key = reloadKey(connectionId, scope: scope, driver: driver) + updateSideObjects(key) { $0.serverObjects[kindId] = ($0.serverObjects[kindId] ?? .idle).enteringLoad } + bumpGeneration(connectionId) + let outcome = await Self.fetchObjectsSafely( + key: ServerObjectLoadKey(load: LoadKey(connectionId: connectionId, scope: scope), kindId: kindId), + connectionId: connectionId, + label: "server objects", + dedup: serverObjectsDedup, + fetch: { try await driver.fetchServerObjects(ofKind: kindId, schema: nil) } + ) + updateKeptSideObjects(key) { + $0.serverObjects[kindId] = ($0.serverObjects[kindId] ?? .idle).settled(by: outcome, discardingValue: false) + } + bumpGeneration(connectionId) + return outcome.didFetch + } +} diff --git a/apple/TablePro/Core/Services/Query/SchemaService.swift b/apple/TablePro/Core/Services/Query/SchemaService.swift index 5cf3d3befd..0eedb01eed 100644 --- a/apple/TablePro/Core/Services/Query/SchemaService.swift +++ b/apple/TablePro/Core/Services/Query/SchemaService.swift @@ -18,6 +18,8 @@ final class SchemaService: ObservableObject { var routines: MetadataLoadState<[RoutineInfo]> = .idle var triggers: MetadataLoadState<[TriggerInfo]> = .idle var userDefinedTypes: MetadataLoadState<[UserDefinedTypeInfo]> = .idle + // Keyed by the declared kind's id. + var serverObjects: [String: MetadataLoadState<[ServerObjectInfo]>] = [:] } @Published private(set) var states: [CatalogKey: SchemaState] = [:] @@ -51,7 +53,7 @@ final class SchemaService: ObservableObject { generations[connectionId] ?? 0 } - private func bumpGeneration(_ connectionId: UUID) { + func bumpGeneration(_ connectionId: UUID) { generations[connectionId, default: 0] &+= 1 } @@ -64,6 +66,18 @@ final class SchemaService: ObservableObject { private let perSchemaRoutinesDedup = OnceTask() private let perSchemaTriggersDedup = OnceTask() private let perSchemaTypesDedup = OnceTask() + let serverObjectsDedup = OnceTask() + private let perSchemaServerObjectsDedup = OnceTask() + + struct ServerObjectLoadKey: Hashable, Sendable { + let load: LoadKey + let kindId: String + } + + struct ServerObjectSchemaFetchKey: Hashable, Sendable { + let fetch: SchemaFetchKey + let kindId: String + } /// A schema is named inside a database, and an engine that changes database on a live /// connection reaches a `PUBLIC` in every one of them. @@ -94,11 +108,15 @@ final class SchemaService: ObservableObject { private struct SideKinds { let triggers: Bool let types: Bool + let serverKinds: [SidebarServerObjectKind] - init(_ type: DatabaseType) { + init(_ type: DatabaseType, serverKinds: [SidebarServerObjectKind] = []) { triggers = type.supportsDatabaseTriggerBrowse types = type.supportsUserDefinedTypeBrowse + self.serverKinds = serverKinds } + + var serverKindIds: [String] { serverKinds.map(\.id) } } private struct RefreshWaiter { @@ -138,7 +156,7 @@ final class SchemaService: ObservableObject { !unrefreshedCatalogs.contains(shownKey(connectionId)) } - private func shownKey(_ connectionId: UUID) -> CatalogKey { + func shownKey(_ connectionId: UUID) -> CatalogKey { shownCatalogs[connectionId]?.key ?? CatalogKey(connectionId: connectionId, database: "", schema: nil) } @@ -337,7 +355,7 @@ final class SchemaService: ObservableObject { /// Every per-schema read answers for the database `schemas(for:)` was listed from, so the /// schema rows and the objects under them describe one database even while a switch settles. - private func catalogKey(_ connectionId: UUID, schema: String) -> SchemaKey { + func catalogKey(_ connectionId: UUID, schema: String) -> SchemaKey { SchemaKey(connectionId: connectionId, database: catalogDatabase(connectionId), schema: schema) } @@ -380,7 +398,8 @@ final class SchemaService: ObservableObject { let key = catalogKey(connectionId, schema: schema) guard hasLoadedContent(key) else { return false } let side = perSchemaSideObjects[key] ?? SideObjects() - return [side.routines.erased, side.triggers.erased, side.userDefinedTypes.erased] + return ([side.routines.erased, side.triggers.erased, side.userDefinedTypes.erased] + + side.serverObjects.values.map(\.erased)) .allSatisfy { $0 == .loaded || $0 == .idle } } @@ -551,7 +570,10 @@ final class SchemaService: ObservableObject { nextLoadGeneration += 1 let generation = nextLoadGeneration schemaLoadGenerations[key] = generation - let kinds = SideKinds(driver.connection.type) + let kinds = SideKinds( + driver.connection.type, + serverKinds: SidebarServerObjectKind.declared(by: driver).filter { $0.scope == .container } + ) if !hasLoadedContent(key) { setPerSchemaState(.loading, key: key) @@ -588,6 +610,16 @@ final class SchemaService: ObservableObject { fetch: { try await driver.fetchUserDefinedTypes(schema: schema) } ) : nil + async let serverObjectsTask = fetchServerObjectsSafely( + kinds: kinds.serverKindIds, + keys: Dictionary(uniqueKeysWithValues: kinds.serverKindIds.map { + ($0, ServerObjectSchemaFetchKey(fetch: fetchKey, kindId: $0)) + }), + connectionId: connectionId, + dedup: perSchemaServerObjectsDedup, + schema: schema, + driver: driver + ) let tablesOutcome: MetadataFetchOutcome<[TableInfo]> do { @@ -606,10 +638,14 @@ final class SchemaService: ObservableObject { let routinesOutcome = await routinesTask let triggersOutcome = await triggersTask let typesOutcome = await typesTask + let serverObjectsOutcomes = await serverObjectsTask guard schemaLoadGenerations[key] == generation else { return } schemaLoadGenerations.removeValue(forKey: key) updateSchemaSideObjects(key) { side in - side = Self.settled(side, routines: routinesOutcome, triggers: triggersOutcome, types: typesOutcome) + side = Self.settled( + side, routines: routinesOutcome, triggers: triggersOutcome, types: typesOutcome, + serverObjects: serverObjectsOutcomes + ) } bumpGeneration(connectionId) } @@ -643,9 +679,10 @@ final class SchemaService: ObservableObject { await perSchemaRoutinesDedup.cancel(where: shouldCancel) await perSchemaTriggersDedup.cancel(where: shouldCancel) await perSchemaTypesDedup.cancel(where: shouldCancel) + await perSchemaServerObjectsDedup.cancel { shouldCancel($0.fetch) } } - private func updateSideObjects(_ key: CatalogKey, _ change: (inout SideObjects) -> Void) { + func updateSideObjects(_ key: CatalogKey, _ change: (inout SideObjects) -> Void) { var side = sideObjects[key] ?? SideObjects() change(&side) sideObjects[key] = side @@ -653,14 +690,14 @@ final class SchemaService: ObservableObject { /// The catalog of the scope the reload reads, not of whatever is shown when it starts: a switch /// while it waits for a connection would file one database's objects under another. - private func reloadKey(_ connectionId: UUID, scope: DatabaseScope?, driver: DatabaseDriver) -> CatalogKey { + func reloadKey(_ connectionId: UUID, scope: DatabaseScope?, driver: DatabaseDriver) -> CatalogKey { guard let scope else { return shownKey(connectionId) } return catalogKey(for: scope, type: driver.connection.type) } /// A reload that lands after `forget` dropped its catalog writes nothing, rather than bringing /// back objects of a database whose entry closed. - private func updateKeptSideObjects(_ key: CatalogKey, _ change: (inout SideObjects) -> Void) { + func updateKeptSideObjects(_ key: CatalogKey, _ change: (inout SideObjects) -> Void) { guard var side = sideObjects[key] else { return } change(&side) sideObjects[key] = side @@ -676,10 +713,11 @@ final class SchemaService: ObservableObject { _ key: CatalogKey, routines: MetadataFetchOutcome<[RoutineInfo]>, triggers: MetadataFetchOutcome<[TriggerInfo]>?, - types: MetadataFetchOutcome<[UserDefinedTypeInfo]>? + types: MetadataFetchOutcome<[UserDefinedTypeInfo]>?, + serverObjects: [String: MetadataFetchOutcome<[ServerObjectInfo]>] = [:] ) { updateSideObjects(key) { side in - side = Self.settled(side, routines: routines, triggers: triggers, types: types) + side = Self.settled(side, routines: routines, triggers: triggers, types: types, serverObjects: serverObjects) } } @@ -698,6 +736,7 @@ final class SchemaService: ObservableObject { side.routines = side.routines.settled(by: .cancelled, discardingValue: false) side.triggers = side.triggers.settled(by: .cancelled, discardingValue: false) side.userDefinedTypes = side.userDefinedTypes.settled(by: .cancelled, discardingValue: false) + side.serverObjects = side.serverObjects.mapValues { $0.settled(by: .cancelled, discardingValue: false) } } bumpGeneration(key.connectionId) } @@ -707,6 +746,9 @@ final class SchemaService: ObservableObject { next.routines = side.routines.enteringLoad if kinds.triggers { next.triggers = side.triggers.enteringLoad } if kinds.types { next.userDefinedTypes = side.userDefinedTypes.enteringLoad } + for id in kinds.serverKindIds { + next.serverObjects[id] = (side.serverObjects[id] ?? .idle).enteringLoad + } return next } @@ -715,7 +757,8 @@ final class SchemaService: ObservableObject { _ side: SideObjects, routines: MetadataFetchOutcome<[RoutineInfo]>, triggers: MetadataFetchOutcome<[TriggerInfo]>?, - types: MetadataFetchOutcome<[UserDefinedTypeInfo]>? + types: MetadataFetchOutcome<[UserDefinedTypeInfo]>?, + serverObjects: [String: MetadataFetchOutcome<[ServerObjectInfo]>] = [:] ) -> SideObjects { var next = side next.routines = side.routines.settled(by: routines, discardingValue: false) @@ -725,6 +768,9 @@ final class SchemaService: ObservableObject { if let types { next.userDefinedTypes = side.userDefinedTypes.settled(by: types, discardingValue: false) } + for (id, outcome) in serverObjects { + next.serverObjects[id] = (side.serverObjects[id] ?? .idle).settled(by: outcome, discardingValue: false) + } return next } @@ -818,6 +864,7 @@ final class SchemaService: ObservableObject { await routinesDedup.cancel { $0.connectionId == connectionId } await triggersDedup.cancel { $0.connectionId == connectionId } await typesDedup.cancel { $0.connectionId == connectionId } + await serverObjectsDedup.cancel { $0.load.connectionId == connectionId } await schemasDedup.cancel { $0.connectionId == connectionId } await cancelSchemaLoads { $0.schemaKey.connectionId == connectionId } } @@ -885,13 +932,17 @@ final class SchemaService: ObservableObject { } } if settleTablesFailed(key, message: message) { - let kinds = type.map(SideKinds.init) + let kinds = type.map { SideKinds($0) } + let serverKindIds = SidebarServerObjectKind.declared(by: DatabaseManager.shared.driver(for: connectionId)).map(\.id) updateSideObjects(key) { side in side = Self.settled( side, routines: .failed(message), triggers: kinds?.triggers == true ? .failed(message) : nil, - types: kinds?.types == true ? .failed(message) : nil + types: kinds?.types == true ? .failed(message) : nil, + serverObjects: Dictionary(uniqueKeysWithValues: serverKindIds.map { + ($0, MetadataFetchOutcome<[ServerObjectInfo]>.failed(message)) + }) ) } } else if !moved { @@ -924,7 +975,12 @@ final class SchemaService: ObservableObject { if !hasLoadedContent(key) { states[key] = .loading } - let kinds = SideKinds(type) + let grouping = PluginManager.shared.databaseGroupingStrategy(for: type) + // A hierarchical engine lists its declared kinds per schema, in `runSchemaLoad`. + let kinds = SideKinds( + type, + serverKinds: grouping == .hierarchicalSchema ? [] : SidebarServerObjectKind.declared(by: driver) + ) updateSideObjects(key) { $0 = Self.enteringLoad($0, kinds: kinds) } bumpGeneration(connectionId) @@ -938,7 +994,6 @@ final class SchemaService: ObservableObject { } let loadKey = LoadKey(connectionId: connectionId, scope: scope) - let grouping = PluginManager.shared.databaseGroupingStrategy(for: type) if grouping == .hierarchicalSchema { await runHierarchicalLoad( key: key, @@ -979,6 +1034,16 @@ final class SchemaService: ObservableObject { fetch: { try await driver.fetchUserDefinedTypes(schema: nil) } ) : nil + async let serverObjectsTask = fetchServerObjectsSafely( + kinds: kinds.serverKindIds, + keys: Dictionary(uniqueKeysWithValues: kinds.serverKindIds.map { + ($0, ServerObjectLoadKey(load: loadKey, kindId: $0)) + }), + connectionId: connectionId, + dedup: serverObjectsDedup, + schema: nil, + driver: driver + ) async let schemasTask: [String]? = supportsSchemas || listsAttached ? Self.fetchSchemasSafely( key: loadKey, @@ -1021,10 +1086,14 @@ final class SchemaService: ObservableObject { let routinesOutcome = await routinesTask let triggersOutcome = await triggersTask let typesOutcome = await typesTask + let serverObjectsOutcomes = await serverObjectsTask guard isCurrentLoadGeneration(generation, for: key, phase: "side-objects-loaded") else { return } - commitSideObjects(key, routines: routinesOutcome, triggers: triggersOutcome, types: typesOutcome) + commitSideObjects( + key, routines: routinesOutcome, triggers: triggersOutcome, types: typesOutcome, + serverObjects: serverObjectsOutcomes + ) if let loadedSchemas = await schemasTask { guard isCurrentLoadGeneration(generation, for: key, phase: "schemas-loaded") else { @@ -1198,11 +1267,44 @@ final class SchemaService: ObservableObject { } } + // One fetch per declared kind, side by side; each settles its own group. + private func fetchServerObjectsSafely( + kinds: [String], + keys: [String: Key], + connectionId: UUID, + dedup: OnceTask, + schema: String?, + driver: DatabaseDriver + ) async -> [String: MetadataFetchOutcome<[ServerObjectInfo]>] { + guard !kinds.isEmpty else { return [:] } + return await withTaskGroup(of: (String, MetadataFetchOutcome<[ServerObjectInfo]>)?.self) { group in + for kindId in kinds { + guard let key = keys[kindId] else { continue } + group.addTask { + let outcome = await Self.fetchObjectsSafely( + key: key, + connectionId: connectionId, + label: "server objects", + dedup: dedup, + fetch: { try await driver.fetchServerObjects(ofKind: kindId, schema: schema) } + ) + return (kindId, outcome) + } + } + var outcomes: [String: MetadataFetchOutcome<[ServerObjectInfo]>] = [:] + for await result in group { + guard let result else { continue } + outcomes[result.0] = result.1 + } + return outcomes + } + } + /// A failure comes back as a failure, never as an empty list. An empty list made a refresh that /// failed indistinguishable from a database with no routines, and the caller committed it over /// the loaded one: a single dropped connection emptied the sidebar's procedures and functions /// while the refresh reported success, with nothing scheduled to put them back. - private static func fetchObjectsSafely( + static func fetchObjectsSafely( key: Key, connectionId: UUID, label: String, diff --git a/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift b/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift index 4336e6c51f..9d189e0fae 100644 --- a/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift +++ b/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift @@ -196,7 +196,8 @@ internal enum ShortcutsRowWriter { private static func refuseHeldTransaction(on driver: DatabaseDriver, ownsConnection: Bool) async throws { let owner = WriteTransactionOwner.resolve( supportsTransactions: driver.supportsTransactions, - sessionState: await driver.heldSessionTransactionState() + sessionState: await driver.heldSessionTransactionState(), + commitMode: .automatic ) guard owner == .session else { return } guard ownsConnection else { diff --git a/apple/TablePro/Core/Storage/ConnectionLocalState.swift b/apple/TablePro/Core/Storage/ConnectionLocalState.swift index aa36566e31..a39458b3ca 100644 --- a/apple/TablePro/Core/Storage/ConnectionLocalState.swift +++ b/apple/TablePro/Core/Storage/ConnectionLocalState.swift @@ -6,18 +6,12 @@ import Foundation import os -/// Everything a deleted connection leaves behind on this device, cleaned up in one place. -/// -/// The list used to be written out at each of the three delete sites, which is how they drifted: -/// the remote-deletion path cleared two stores where the local one cleared nine, and no site had -/// ever removed a single `SidebarPersistenceKey`. A store added here is cleaned up everywhere. +// Everything a deleted connection leaves on this device, in one place: three delete sites each kept a list and drifted. @MainActor internal enum ConnectionLocalState { nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "ConnectionLocalState") - /// Who deleted the connection. A local delete leaves tombstones so the other devices follow; - /// a remote delete must not, or it pushes back a deletion the sender already made, and neither - /// may the local delete of a connection kept off iCloud. + // Only a local delete leaves tombstones; a remote one would push back the sender's own deletion. internal enum Origin { case local case localOnly @@ -89,27 +83,14 @@ internal enum ConnectionLocalState { } } - /// The two stores that can only be reached with `await`, so `purge` fires them and does not - /// wait. They belong here for the reason everything else does: written out at the delete sites, - /// the query history clear reached the two local ones and never the remote one, so a connection - /// deleted on another Mac left every statement it had ever run, with its literals, in the - /// history on this one. - /// - /// Separate from `purge` so a test can await what `purge` cannot. - /// - /// `origin` splits the SQL favorites the way `purgeFavorites` splits the table ones, and for the - /// same reason. `SyncCoordinator.applyRemoteChanges` suppresses the change tracker only for the - /// length of its own synchronous body, and this runs from a `Task` that starts after that body - /// has returned and the suppression has been reset, so a remote delete really did write - /// tombstones and push the sender's own deletion back at it. - /// - /// Query history takes no origin: it is device-local and never synced, so a remote delete - /// should forget this device's copy and has no tombstone to leave either way. + // Separate from `purge` so a test can await it. Saved queries split by origin themselves: this runs after + // `applyRemoteChanges` stopped suppressing the tracker. Query history and tab versions are device-local. internal static func purgeAsyncStores( _ connectionIds: Set, origin: Origin, sqlFavorites: SQLFavoriteManager = .shared, queryHistory: QueryHistoryManager = .shared, + queryTabVersions: QueryTabVersionStorage = .shared, syncTracker: SyncChangeTracker = .shared ) async { for connectionId in connectionIds { @@ -128,16 +109,15 @@ internal enum ConnectionLocalState { "Query history for a deleted connection could not be cleared: \(connectionId, privacy: .public)" ) } + if await !queryTabVersions.removeVersions(forConnection: connectionId) { + logger.error( + "Tab versions for a deleted connection could not be cleared: \(connectionId, privacy: .public)" + ) + } } } - /// The trailing pane's two keys, which are written straight onto the defaults object rather - /// than through a store with a `remove(for:)` of its own, so `purge`'s list of stores never - /// reached them. A deleted connection left the surface it was last showing and its inspector's - /// view mode behind, and a connection later given the same id inherited both. - /// - /// Separate from `purge` for the reason `purgeAsyncStores` is: `purge` reaches nine shared - /// singletons and a test cannot call it, while this takes the one thing it writes to. + // Written straight onto the defaults, with no store of their own; separate so a test can pass its own defaults. internal static func purgeTrailingPaneKeys( _ connectionIds: Set, defaults: UserDefaults = AppStorageEnvironment.shared.defaults @@ -148,8 +128,7 @@ internal enum ConnectionLocalState { } } - /// The in-memory registries go first. A live `SharedSidebarState` for this connection rewrites - /// its own defaults keys on the next mutation, so removing the keys under it achieves nothing. + // First: a live `SharedSidebarState` rewrites its defaults keys on its next mutation. private static func purgeLiveState(_ connectionId: UUID) { SharedSidebarState.removeConnection(connectionId) SidebarViewModel.removeConnection(connectionId) diff --git a/apple/TablePro/Core/Storage/QueryTabVersionStorage.swift b/apple/TablePro/Core/Storage/QueryTabVersionStorage.swift new file mode 100644 index 0000000000..2bf78efbc2 --- /dev/null +++ b/apple/TablePro/Core/Storage/QueryTabVersionStorage.swift @@ -0,0 +1,433 @@ +// +// QueryTabVersionStorage.swift +// TablePro +// + +import Combine +import Foundation +import os +import SQLite3 + +internal enum QueryTabVersionReason: Int, Sendable, CaseIterable { + case run = 0 + case idle = 1 + case beforeReplace = 2 + case beforeRestore = 3 +} + +internal struct QueryTabVersionSummary: Sendable, Equatable, Identifiable { + let id: Int64 + let reason: QueryTabVersionReason + let savedAt: Date +} + +internal struct QueryTabVersion: Sendable, Equatable { + let summary: QueryTabVersionSummary + let query: String +} + +// Device-local and never synced. Rows outlive a closed tab, since Reopen Closed Tab brings the same tab id back. +internal actor QueryTabVersionStorage { + static let shared = QueryTabVersionStorage() + static let retainedVersionCount = 50 + static let maximumRetentionDays = 30 + static let idleCoalescingWindow: TimeInterval = 5 * 60 + + private static let logger = Logger(subsystem: "com.TablePro", category: "QueryTabVersionStorage") + private static let secondsPerDay: TimeInterval = 86_400 + + private struct DatabaseHandle: @unchecked Sendable { + var pointer: OpaquePointer? + } + + private struct NewestRow { + let id: Int64 + let reason: QueryTabVersionReason? + let idleSince: Date? + let hasSameText: Bool + } + + private var dbHandle = DatabaseHandle() + private var isPrepared = false + private let databaseURL: URL + private let removeDatabaseOnDeinit: Bool + private let now: @Sendable () -> Date + private let historyMaxDays: @Sendable () -> Int + private let announce: @Sendable (UUID?) -> Void + private let reader: QueryTabVersionReader + + private var db: OpaquePointer? { + if !isPrepared { + isPrepared = true + setupDatabase() + } + return dbHandle.pointer + } + + init( + databaseURL: URL = QueryTabVersionStorage.defaultDatabaseURL(), + now: @escaping @Sendable () -> Date = Date.init, + historyMaxDays: @escaping @Sendable () -> Int = { AppSettingsStorage.shared.loadHistory().maxDays }, + announce: @escaping @Sendable (UUID?) -> Void = { tabId in + Task { @MainActor in AppEvents.shared.queryTabVersionsDidUpdate.send(tabId) } + }, + removeDatabaseOnDeinit: Bool = false + ) { + self.databaseURL = databaseURL + self.now = now + self.historyMaxDays = historyMaxDays + self.announce = announce + self.removeDatabaseOnDeinit = removeDatabaseOnDeinit + reader = QueryTabVersionReader(databaseURL: databaseURL) + } + + static func defaultDatabaseURL() -> URL { + let directory = AppStorageEnvironment.shared.applicationSupportRoot.appendingPathComponent("TablePro") + try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + return directory.appendingPathComponent("query_tab_versions.db") + } + + deinit { + if let pointer = dbHandle.pointer { + sqlite3_close_v2(pointer) + } + reader.close() + if removeDatabaseOnDeinit { + let path = databaseURL.path(percentEncoded: false) + try? FileManager.default.removeItem(atPath: path) + for suffix in ["-wal", "-shm"] { + try? FileManager.default.removeItem(atPath: path + suffix) + } + } + } + + // A shorter Query History window applies here too: this store keeps text the user deleted. + static func retentionDays(historyMaxDays: Int) -> Int { + historyMaxDays > 0 ? min(maximumRetentionDays, historyMaxDays) : maximumRetentionDays + } + + static func isRecordable(_ text: String) -> Bool { + (text as NSString).length <= TabQueryContent.maxPersistableQuerySize + && !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + } + + nonisolated var retentionDays: Int { + Self.retentionDays(historyMaxDays: historyMaxDays()) + } + + nonisolated func summaries( + tabId: UUID, + limit: Int = QueryTabVersionStorage.retainedVersionCount + ) -> [QueryTabVersionSummary] { + reader.summaries(tabId: tabId, savedSince: cutoff(before: now()), limit: limit) + } + + nonisolated func version(id: Int64, tabId: UUID) -> QueryTabVersion? { + reader.version(id: id, tabId: tabId, savedSince: cutoff(before: now())) + } + + @discardableResult + func record(tabId: UUID, connectionId: UUID, text: String, reason: QueryTabVersionReason) -> Bool { + guard Self.isRecordable(text), let db else { return false } + let savedAt = now() + let keptSince = cutoff(before: savedAt) + let newest = newestRow(tabId: tabId, comparedWith: text, savedSince: keptSince, db: db) + if let newest, newest.hasSameText { + // The idle row already holds this text; sealing it keeps the next pause from overwriting it. + guard reason != .idle, newest.reason == .idle else { return false } + return sealRow(newest.id, tabId: tabId, reason: reason, db: db) + } + + let written: Bool + if reason == .idle, let newest, newest.reason == .idle, let idleSince = newest.idleSince, + savedAt.timeIntervalSince(idleSince) < Self.idleCoalescingWindow { + written = replaceRow(newest.id, connectionId: connectionId, text: text, savedAt: savedAt, db: db) + } else { + written = insertRow( + tabId: tabId, + connectionId: connectionId, + text: text, + reason: reason, + savedAt: savedAt, + db: db + ) + } + guard written else { + Self.logger.error("A query tab version could not be written") + return false + } + trim(tabId: tabId, savedBefore: keptSince, db: db) + announce(tabId) + return true + } + + @discardableResult + func removeVersions(forConnection connectionId: UUID) -> Bool { + guard let db else { return false } + let removed = run("DELETE FROM tab_versions WHERE connection_id = ?;", db: db) { statement in + Self.bind(connectionId.uuidString, to: statement, at: 1) + } + if removed { truncateLog(db) } + if removed { announce(nil) } + return removed + } + + @discardableResult + func removeAll() -> Bool { + guard let db else { return false } + let removed = run("DELETE FROM tab_versions;", db: db) { _ in } + if removed { truncateLog(db) } + if removed { announce(nil) } + return removed + } + + // A checkpoint alone leaves deleted text in the -wal file until its frames are reused. + private func truncateLog(_ db: OpaquePointer) { + _ = run("PRAGMA wal_checkpoint(TRUNCATE);", db: db) { _ in } + } + + // MARK: - Setup + + private static let schemaStatements = [ + "PRAGMA journal_mode=WAL;", + "PRAGMA synchronous=NORMAL;", + // Clear History is a privacy command, so deleted text is overwritten rather than left in free pages. + "PRAGMA secure_delete=ON;", + """ + CREATE TABLE IF NOT EXISTS tab_versions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + tab_id TEXT NOT NULL, + connection_id TEXT NOT NULL, + query TEXT NOT NULL, + reason INTEGER NOT NULL, + saved_at REAL NOT NULL, + idle_since REAL + ); + """, + "CREATE INDEX IF NOT EXISTS idx_tab_versions_tab ON tab_versions(tab_id, id);", + "CREATE INDEX IF NOT EXISTS idx_tab_versions_connection ON tab_versions(connection_id);", + "CREATE INDEX IF NOT EXISTS idx_tab_versions_saved ON tab_versions(saved_at);", + ] + + private func setupDatabase() { + try? FileManager.default.createDirectory( + at: databaseURL.deletingLastPathComponent(), + withIntermediateDirectories: true + ) + guard case .opened(let pointer, let setAside) = LocalSQLiteStoreOpener.open(at: databaseURL, now: now()) else { + Self.logger.error("The query tab versions database could not be opened") + return + } + dbHandle.pointer = pointer + // The reader may hold the file a damaged store was moved to. + reader.close() + // Snapshots of text the tabs still hold, so a damaged copy is not worth keeping. + if setAside != nil { + LocalSQLiteStoreOpener.removeSetAsideCopies(of: databaseURL) + } + for statement in Self.schemaStatements { + _ = run(statement, db: pointer) { _ in } + } + trim(tabId: nil, savedBefore: cutoff(before: now()), db: pointer) + } + + // MARK: - Writes + + private func newestRow(tabId: UUID, comparedWith text: String, savedSince cutoff: Date, db: OpaquePointer) -> NewestRow? { + let sql = """ + SELECT id, reason, idle_since, query = ? FROM tab_versions + WHERE tab_id = ? AND saved_at >= ? ORDER BY id DESC LIMIT 1; + """ + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { return nil } + Self.bind(text, to: statement, at: 1) + Self.bind(tabId.uuidString, to: statement, at: 2) + sqlite3_bind_double(statement, 3, cutoff.timeIntervalSince1970) + guard sqlite3_step(statement) == SQLITE_ROW else { return nil } + let idleSince = sqlite3_column_type(statement, 2) == SQLITE_NULL + ? nil + : Date(timeIntervalSince1970: sqlite3_column_double(statement, 2)) + return NewestRow( + id: sqlite3_column_int64(statement, 0), + reason: QueryTabVersionReason(rawValue: Int(sqlite3_column_int(statement, 1))), + idleSince: idleSince, + hasSameText: sqlite3_column_int(statement, 3) != 0 + ) + } + + // The coalescing window counts from the first snapshot of the run of idle pauses, so steady typing still leaves a + // version every few minutes. + private func replaceRow(_ id: Int64, connectionId: UUID, text: String, savedAt: Date, db: OpaquePointer) -> Bool { + run("UPDATE tab_versions SET query = ?, saved_at = ?, connection_id = ? WHERE id = ?;", db: db) { statement in + Self.bind(text, to: statement, at: 1) + sqlite3_bind_double(statement, 2, savedAt.timeIntervalSince1970) + Self.bind(connectionId.uuidString, to: statement, at: 3) + sqlite3_bind_int64(statement, 4, id) + } + } + + private func sealRow(_ id: Int64, tabId: UUID, reason: QueryTabVersionReason, db: OpaquePointer) -> Bool { + let sealed = run("UPDATE tab_versions SET reason = ?, idle_since = NULL WHERE id = ?;", db: db) { statement in + sqlite3_bind_int(statement, 1, Int32(reason.rawValue)) + sqlite3_bind_int64(statement, 2, id) + } + if sealed { announce(tabId) } + return sealed + } + + private func insertRow( + tabId: UUID, + connectionId: UUID, + text: String, + reason: QueryTabVersionReason, + savedAt: Date, + db: OpaquePointer + ) -> Bool { + let sql = """ + INSERT INTO tab_versions (tab_id, connection_id, query, reason, saved_at, idle_since) + VALUES (?, ?, ?, ?, ?, ?); + """ + return run(sql, db: db) { statement in + Self.bind(tabId.uuidString, to: statement, at: 1) + Self.bind(connectionId.uuidString, to: statement, at: 2) + Self.bind(text, to: statement, at: 3) + sqlite3_bind_int(statement, 4, Int32(reason.rawValue)) + sqlite3_bind_double(statement, 5, savedAt.timeIntervalSince1970) + if reason == .idle { + sqlite3_bind_double(statement, 6, savedAt.timeIntervalSince1970) + } else { + sqlite3_bind_null(statement, 6) + } + } + } + + private func trim(tabId: UUID?, savedBefore cutoff: Date, db: OpaquePointer) { + _ = run("DELETE FROM tab_versions WHERE saved_at < ?;", db: db) { statement in + sqlite3_bind_double(statement, 1, cutoff.timeIntervalSince1970) + } + guard let tabId else { return } + let sql = """ + DELETE FROM tab_versions WHERE tab_id = ?1 AND id NOT IN ( + SELECT id FROM tab_versions WHERE tab_id = ?1 ORDER BY id DESC LIMIT \(Self.retainedVersionCount) + ); + """ + _ = run(sql, db: db) { statement in + Self.bind(tabId.uuidString, to: statement, at: 1) + } + } + + // MARK: - Helpers + + nonisolated private func cutoff(before date: Date) -> Date { + date.addingTimeInterval(-TimeInterval(retentionDays) * Self.secondsPerDay) + } + + private func run(_ sql: String, db: OpaquePointer, bind: (OpaquePointer?) -> Void) -> Bool { + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { return false } + bind(statement) + let result = sqlite3_step(statement) + return result == SQLITE_DONE || result == SQLITE_ROW + } + + fileprivate static func bind(_ text: String, to statement: OpaquePointer?, at index: Int32) { + let transient = unsafeBitCast(-1, to: sqlite3_destructor_type.self) + sqlite3_bind_text(statement, index, text, -1, transient) + } +} + +// Menus fill synchronously, so reads take their own connection instead of a hop to the actor. +internal final class QueryTabVersionReader: @unchecked Sendable { + private let lock = NSLock() + private let databaseURL: URL + private var handle: OpaquePointer? + + init(databaseURL: URL) { + self.databaseURL = databaseURL + } + + deinit { + if let handle { + sqlite3_close_v2(handle) + } + } + + func close() { + lock.withLock { + if let handle { + sqlite3_close_v2(handle) + } + handle = nil + } + } + + func summaries(tabId: UUID, savedSince cutoff: Date, limit: Int) -> [QueryTabVersionSummary] { + let sql = """ + SELECT id, reason, saved_at FROM tab_versions + WHERE tab_id = ? AND saved_at >= ? ORDER BY id DESC LIMIT ?; + """ + return lock.withLock { + guard let db = openIfNeeded() else { return [] } + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { return [] } + QueryTabVersionStorage.bind(tabId.uuidString, to: statement, at: 1) + sqlite3_bind_double(statement, 2, cutoff.timeIntervalSince1970) + sqlite3_bind_int(statement, 3, Int32(clamping: limit)) + var rows: [QueryTabVersionSummary] = [] + while sqlite3_step(statement) == SQLITE_ROW { + if let summary = Self.summary(from: statement) { + rows.append(summary) + } + } + return rows + } + } + + func version(id: Int64, tabId: UUID, savedSince cutoff: Date) -> QueryTabVersion? { + let sql = """ + SELECT id, reason, saved_at, query FROM tab_versions + WHERE id = ? AND tab_id = ? AND saved_at >= ? LIMIT 1; + """ + return lock.withLock { + guard let db = openIfNeeded() else { return nil } + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { return nil } + sqlite3_bind_int64(statement, 1, id) + QueryTabVersionStorage.bind(tabId.uuidString, to: statement, at: 2) + sqlite3_bind_double(statement, 3, cutoff.timeIntervalSince1970) + guard sqlite3_step(statement) == SQLITE_ROW, + let summary = Self.summary(from: statement), + let text = sqlite3_column_text(statement, 3) else { return nil } + return QueryTabVersion(summary: summary, query: String(cString: text)) + } + } + + // Opened without create: before the first write there is nothing to read. + private func openIfNeeded() -> OpaquePointer? { + if let handle { return handle } + let path = databaseURL.path(percentEncoded: false) + guard FileManager.default.fileExists(atPath: path) else { return nil } + var opened: OpaquePointer? + guard sqlite3_open_v2(path, &opened, SQLITE_OPEN_READWRITE, nil) == SQLITE_OK, let opened else { + sqlite3_close_v2(opened) + return nil + } + sqlite3_busy_timeout(opened, 1_000) + sqlite3_exec(opened, "PRAGMA query_only = 1;", nil, nil, nil) + handle = opened + return opened + } + + private static func summary(from statement: OpaquePointer?) -> QueryTabVersionSummary? { + guard let reason = QueryTabVersionReason(rawValue: Int(sqlite3_column_int(statement, 1))) else { return nil } + return QueryTabVersionSummary( + id: sqlite3_column_int64(statement, 0), + reason: reason, + savedAt: Date(timeIntervalSince1970: sqlite3_column_double(statement, 2)) + ) + } +} diff --git a/apple/TablePro/Core/VersionHistory/LinkedFileVersionHistoryProvider.swift b/apple/TablePro/Core/VersionHistory/LinkedFileVersionHistoryProvider.swift index 9010bba72d..ba84225491 100644 --- a/apple/TablePro/Core/VersionHistory/LinkedFileVersionHistoryProvider.swift +++ b/apple/TablePro/Core/VersionHistory/LinkedFileVersionHistoryProvider.swift @@ -70,7 +70,7 @@ internal struct LinkedFileVersionHistoryProvider: VersionHistoryProvider { throw VersionHistoryError.undecodableContent } return content - case .savedQueryVersion: + case .savedQueryVersion, .tabVersion: throw VersionHistoryError.versionNotFound } } diff --git a/apple/TablePro/Core/VersionHistory/QueryTabVersionHistoryProvider.swift b/apple/TablePro/Core/VersionHistory/QueryTabVersionHistoryProvider.swift new file mode 100644 index 0000000000..132e22be01 --- /dev/null +++ b/apple/TablePro/Core/VersionHistory/QueryTabVersionHistoryProvider.swift @@ -0,0 +1,85 @@ +// +// QueryTabVersionHistoryProvider.swift +// TablePro +// + +import Foundation + +internal struct LiveQueryTab: Sendable, Equatable { + let connectionId: UUID + let text: String +} + +internal struct QueryTabVersionHistoryProvider: VersionHistoryProvider { + let tabId: UUID + let storage: QueryTabVersionStorage + // Nil once the tab is closed. + let liveTab: @MainActor @Sendable () -> LiveQueryTab? + // False when the tab is gone by the time the text arrives. + let write: @MainActor @Sendable (String) -> Bool + var isCapturePaused: @MainActor @Sendable () -> Bool = { QueryHistoryCaptureStore.isPaused } + + func loadHistory() async throws -> VersionHistoryPage { + guard await liveTab() != nil else { throw VersionHistoryError.tabClosed } + let past = storage.summaries(tabId: tabId).map { Self.entry(for: $0) } + return VersionHistoryPage( + entries: [VersionHistoryEntry(reference: .current)] + past, + notice: .keepsRecentVersions(count: QueryTabVersionStorage.retainedVersionCount, days: storage.retentionDays) + ) + } + + func content(of reference: VersionHistoryReference) async throws -> String { + switch reference { + case .current: + guard let live = await liveTab() else { throw VersionHistoryError.tabClosed } + return live.text + case .tabVersion(let id): + guard let version = storage.version(id: id, tabId: tabId) else { + throw VersionHistoryError.versionNotFound + } + return version.query + case .savedQueryVersion, .gitRevision: + throw VersionHistoryError.versionNotFound + } + } + + // Restore is recoverable through the version it records first, since the target editor is rarely mounted. + func prepareRestore(_ reference: VersionHistoryReference) async throws -> VersionRestorePlan { + guard case .tabVersion(let id) = reference, let version = storage.version(id: id, tabId: tabId) else { + throw VersionHistoryError.versionNotFound + } + guard await liveTab() != nil else { throw VersionHistoryError.tabClosed } + return VersionRestorePlan(replacesUncommittedChanges: false) { + guard let live = await liveTab() else { throw VersionHistoryError.tabClosed } + if await !isCapturePaused() { + await storage.record( + tabId: tabId, + connectionId: live.connectionId, + text: live.text, + reason: .beforeRestore + ) + } + guard await write(version.query) else { throw VersionHistoryError.tabClosed } + } + } + + static func entry(for summary: QueryTabVersionSummary) -> VersionHistoryEntry { + VersionHistoryEntry( + reference: .tabVersion(id: summary.id), + summary: summary.reason.summary, + date: summary.savedAt + ) + } +} + +extension QueryTabVersionReason { + // An idle snapshot has no label, so its title is its date. + var summary: String? { + switch self { + case .run: String(localized: "Run") + case .idle: nil + case .beforeReplace: String(localized: "Before Replace") + case .beforeRestore: String(localized: "Before Restore") + } + } +} diff --git a/apple/TablePro/Core/VersionHistory/QueryTabVersionRecorder.swift b/apple/TablePro/Core/VersionHistory/QueryTabVersionRecorder.swift new file mode 100644 index 0000000000..31a456e4b5 --- /dev/null +++ b/apple/TablePro/Core/VersionHistory/QueryTabVersionRecorder.swift @@ -0,0 +1,28 @@ +// +// QueryTabVersionRecorder.swift +// TablePro +// + +import Foundation + +@MainActor +internal struct QueryTabVersionRecorder { + let storage: QueryTabVersionStorage + // The Query History pause means "record nothing I run on this Mac", and a tab's drafts are the same text. + var isCapturePaused: @MainActor @Sendable () -> Bool + + static let live = QueryTabVersionRecorder(storage: .shared, isCapturePaused: { QueryHistoryCaptureStore.isPaused }) + + // The text is read now, so a later edit cannot change what this version holds. + @discardableResult + func record(_ tab: QueryTab?, connectionId: UUID, reason: QueryTabVersionReason) -> Task? { + guard let tab, tab.tabType == .query, !isCapturePaused() else { return nil } + let text = tab.content.query + guard QueryTabVersionStorage.isRecordable(text) else { return nil } + let storage = storage + let tabId = tab.id + return Task { + await storage.record(tabId: tabId, connectionId: connectionId, text: text, reason: reason) + } + } +} diff --git a/apple/TablePro/Core/VersionHistory/SavedQueryVersionHistoryProvider.swift b/apple/TablePro/Core/VersionHistory/SavedQueryVersionHistoryProvider.swift index 7e4cfc4571..6b6abf5d2a 100644 --- a/apple/TablePro/Core/VersionHistory/SavedQueryVersionHistoryProvider.swift +++ b/apple/TablePro/Core/VersionHistory/SavedQueryVersionHistoryProvider.swift @@ -38,7 +38,7 @@ internal struct SavedQueryVersionHistoryProvider: VersionHistoryProvider { return favorite.query case .savedQueryVersion(let id): return try await version(id: id).query - case .gitRevision: + case .gitRevision, .tabVersion: throw VersionHistoryError.versionNotFound } } diff --git a/apple/TablePro/Core/VersionHistory/VersionHistoryModels.swift b/apple/TablePro/Core/VersionHistory/VersionHistoryModels.swift index a394028ad3..63fe7e18b2 100644 --- a/apple/TablePro/Core/VersionHistory/VersionHistoryModels.swift +++ b/apple/TablePro/Core/VersionHistory/VersionHistoryModels.swift @@ -8,12 +8,14 @@ import Foundation internal enum VersionHistorySubject: Codable, Hashable, Sendable { case savedQuery(id: UUID) case linkedFile(url: URL) + case queryTab(id: UUID) } internal enum VersionHistoryReference: Hashable, Sendable { case current case savedQueryVersion(id: Int64) case gitRevision(commit: String, path: String) + case tabVersion(id: Int64) var shortRevision: String? { guard case .gitRevision(let commit, _) = self else { return nil } @@ -35,6 +37,7 @@ internal struct VersionHistoryEntry: Identifiable, Hashable, Sendable { internal enum VersionHistoryNotice: Hashable, Sendable { case keepsLatestVersions(Int) case showsLatestCommits(Int) + case keepsRecentVersions(count: Int, days: Int) } internal struct VersionHistoryPage: Equatable, Sendable { @@ -64,6 +67,7 @@ internal enum VersionHistoryError: LocalizedError, Equatable { case storedInLargeFileStorage case fileChangedBeforeWriting case restoreFailed(String) + case tabClosed var errorDescription: String? { switch self { @@ -87,6 +91,8 @@ internal enum VersionHistoryError: LocalizedError, Equatable { return String(localized: "The file or its staged version changed before it could be written. Nothing was replaced.") case .restoreFailed(let message): return String(format: String(localized: "The version could not be restored: %@"), message) + case .tabClosed: + return String(localized: "This tab is closed. Reopen it to see its history.") } } } diff --git a/apple/TablePro/Models/Connection/ConnectionCommitMode.swift b/apple/TablePro/Models/Connection/ConnectionCommitMode.swift new file mode 100644 index 0000000000..8245a4edb7 --- /dev/null +++ b/apple/TablePro/Models/Connection/ConnectionCommitMode.swift @@ -0,0 +1,11 @@ +// +// ConnectionCommitMode.swift +// TablePro +// + +import Foundation + +enum ConnectionCommitMode: String, Sendable, Equatable, CaseIterable { + case automatic + case manual +} diff --git a/apple/TablePro/Models/Connection/ConnectionSession.swift b/apple/TablePro/Models/Connection/ConnectionSession.swift index bfac87017e..d576075016 100644 --- a/apple/TablePro/Models/Connection/ConnectionSession.swift +++ b/apple/TablePro/Models/Connection/ConnectionSession.swift @@ -24,6 +24,7 @@ struct ConnectionSession: Identifiable { // The level in force; the toolbar changes it, so `connection.safeModeLevel` goes stale. var safeModeLevel: SafeModeLevel + var commitMode: ConnectionCommitMode // Per-connection state var selectedTables: Set = [] @@ -64,6 +65,7 @@ struct ConnectionSession: Identifiable { globalSeconds: 0 ) self.safeModeLevel = connection.safeModeLevel + self.commitMode = connection.commitMode self.connectedAt = Date() self.lastActiveAt = Date() } diff --git a/apple/TablePro/Models/Connection/DatabaseConnection.swift b/apple/TablePro/Models/Connection/DatabaseConnection.swift index f981693eb3..3b90b24275 100644 --- a/apple/TablePro/Models/Connection/DatabaseConnection.swift +++ b/apple/TablePro/Models/Connection/DatabaseConnection.swift @@ -156,6 +156,7 @@ struct DatabaseConnection: Identifiable, Hashable { static let postDisconnectScriptKey = "postDisconnectScript" static let scriptTimeoutSecondsKey = "scriptTimeoutSeconds" static let completionIdentifierCaseKey = "completionIdentifierCase" + static let commitModeKey = "commitMode" static let binaryInlineLimitKilobytesKey = "binaryInlineLimitKB" static let binaryInlineLimitKilobytesRange = 1 ... 1_048_576 @@ -354,6 +355,19 @@ struct DatabaseConnection: Identifiable, Hashable { } } + var commitMode: ConnectionCommitMode { + get { + additionalFields[Self.commitModeKey].flatMap(ConnectionCommitMode.init(rawValue:)) ?? .automatic + } + set { + if newValue == .automatic { + additionalFields.removeValue(forKey: Self.commitModeKey) + } else { + additionalFields[Self.commitModeKey] = newValue.rawValue + } + } + } + /// A socket path stands in for the built-in Host and Port, so only a `.network` driver can be /// reached through one. A server named in plugin fields, like DuckDB's Quack, listens on TCP /// alone, and a path stored for it would send the tunnel somewhere nothing answers. diff --git a/apple/TablePro/Models/Query/DatabaseObjectRef.swift b/apple/TablePro/Models/Query/DatabaseObjectRef.swift index 0c0814ec70..dd35c068ea 100644 --- a/apple/TablePro/Models/Query/DatabaseObjectRef.swift +++ b/apple/TablePro/Models/Query/DatabaseObjectRef.swift @@ -14,8 +14,10 @@ enum DatabaseObjectKind: String, Codable, Sendable, Hashable { case userType case view case materializedView + // A kind the driver declares; the ref's `serverKind` says which one. + case serverObject - var sidebarObjectKind: SidebarObjectKind { + var sidebarObjectKind: SidebarObjectKind? { switch self { case .procedure: return .procedure case .function: return .function @@ -23,6 +25,7 @@ enum DatabaseObjectKind: String, Codable, Sendable, Hashable { case .userType: return .type case .view: return .view case .materializedView: return .materializedView + case .serverObject: return nil } } @@ -37,11 +40,30 @@ enum DatabaseObjectKind: String, Codable, Sendable, Hashable { } var displayName: String { - sidebarObjectKind.displayName + sidebarObjectKind?.displayName ?? String(localized: "Object") } var iconName: String { - sidebarObjectKind.iconName + sidebarObjectKind?.iconName ?? "shippingbox" + } +} + +struct ServerObjectKindRef: Hashable, Codable, Sendable { + let id: String + let displayName: String + let iconName: String + // Optional so a ref written without it decodes, and reads as a container kind. + let scope: SidebarServerObjectKind.Scope? + + init(id: String, displayName: String, iconName: String, scope: SidebarServerObjectKind.Scope? = nil) { + self.id = id + self.displayName = displayName + self.iconName = iconName + self.scope = scope + } + + init(_ kind: SidebarServerObjectKind) { + self.init(id: kind.id, displayName: kind.displayName, iconName: kind.iconName, scope: kind.scope) } } @@ -69,6 +91,9 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { /// re-list an entire schema to recover it, and short enough to persist with the tab. let attributes: [ObjectAttribute] + // Server objects only, and optional so a tab persisted before them still decodes. + let serverKind: ServerObjectKindRef? + init( kind: DatabaseObjectKind, name: String, @@ -78,7 +103,8 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { identity: String? = nil, argumentSignature: String? = nil, typeKind: UserDefinedTypeInfo.Kind? = nil, - attributes: [ObjectAttribute] = [] + attributes: [ObjectAttribute] = [], + serverKind: ServerObjectKindRef? = nil ) { self.kind = kind self.name = name @@ -89,6 +115,7 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { self.argumentSignature = argumentSignature self.typeKind = typeKind self.attributes = attributes + self.serverKind = serverKind } init(routine: RoutineInfo, database: String) { @@ -132,8 +159,19 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { ) } - /// What the tab is titled and what the viewer's header shows: enough to tell two overloads - /// apart, and enough to tell two same-named triggers on different tables apart. + init(serverObject: ServerObjectInfo, kind: SidebarServerObjectKind, database: String) { + self.init( + kind: .serverObject, + name: serverObject.name, + database: database, + schema: serverObject.schema, + identity: serverObject.identity, + attributes: serverObject.attributes, + serverKind: ServerObjectKindRef(kind) + ) + } + + // Enough to tell two overloads, or two same-named triggers on different tables, apart. var displayIdentity: String { switch kind { case .procedure, .function: @@ -144,19 +182,31 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { return String(format: String(localized: "%1$@ on %2$@"), qualifiedName, table) case .userType, .view, .materializedView: return qualifiedName + case .serverObject: + return isDatabaseScoped ? name : qualifiedName } } - /// The kind's name for the header capsule. A type says which kind of type it is, because an - /// enum and a domain are edited differently and the reader should know which one opened. + // A database-wide object's schema is where it keeps its members, never part of its name. + var isDatabaseScoped: Bool { + kind == .serverObject && serverKind?.scope == .database + } + + // A type names its own kind, since an enum and a domain are edited differently. var kindDisplayName: String { - guard kind == .userType else { return kind.displayName } - return (typeKind ?? .other).displayName + switch kind { + case .userType: return (typeKind ?? .other).displayName + case .serverObject: return serverKind?.displayName ?? kind.displayName + case .procedure, .function, .trigger, .view, .materializedView: return kind.displayName + } } var kindIconName: String { - guard kind == .userType else { return kind.iconName } - return (typeKind ?? .other).iconName + switch kind { + case .userType: return (typeKind ?? .other).iconName + case .serverObject: return serverKind?.iconName ?? kind.iconName + case .procedure, .function, .trigger, .view, .materializedView: return kind.iconName + } } var qualifiedName: String { @@ -164,8 +214,7 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { return "\(schema).\(name)" } - /// A ref built where no database was selected carries an empty one, and an empty database is - /// server-scoped. Resolving it once keeps the tab-dedup key and the loader's scope agreeing. + // An empty database is server-scoped; resolving it once keeps the tab-dedup key and the loader's scope agreeing. func resolvingDatabase(_ fallback: String) -> DatabaseObjectRef { guard database.isEmpty, !fallback.isEmpty else { return self } return DatabaseObjectRef( @@ -177,7 +226,8 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { identity: identity, argumentSignature: argumentSignature, typeKind: typeKind, - attributes: attributes + attributes: attributes, + serverKind: serverKind ) } @@ -191,7 +241,7 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { argumentSignature: argumentSignature, identity: identity ) - case .trigger, .userType, .view, .materializedView: + case .trigger, .userType, .view, .materializedView, .serverObject: return nil } } @@ -212,16 +262,26 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { ) } - /// Whether two refs name one object. The attributes are what a listing learned and change with - /// a comment edit, so they are left out. An identity is only compared, never trusted alone: some - /// drivers fill it with the object's kind or its signature, which many objects share. + var serverObject: ServerObjectInfo? { + guard kind == .serverObject, let serverKind else { return nil } + return ServerObjectInfo( + kindId: serverKind.id, + name: name, + schema: schema, + identity: identity, + attributes: attributes + ) + } + + // Attributes change with a comment edit, and some drivers fill identity with a value many objects share. func namesSameObject(as other: DatabaseObjectRef) -> Bool { guard kind == other.kind, database == other.database, name == other.name, schema == other.schema, table == other.table, - argumentSignature == other.argumentSignature + argumentSignature == other.argumentSignature, + serverKind?.id == other.serverKind?.id else { return false } guard let identity, let otherIdentity = other.identity else { return true } return identity == otherIdentity diff --git a/apple/TablePro/Models/Query/QueryTab.swift b/apple/TablePro/Models/Query/QueryTab.swift index f5f32b7f12..314d4336f9 100644 --- a/apple/TablePro/Models/Query/QueryTab.swift +++ b/apple/TablePro/Models/Query/QueryTab.swift @@ -66,6 +66,9 @@ struct QueryTab: Identifiable, Equatable { var sessionHighlightRules: [HighlightRule] = [] // Set only by Reveal, after the user authenticated; never saved, so a reopened tab masks again. var revealsMaskedValues = false + // Never saved: a relaunch must not start polling a server with nobody watching. + var autoRefreshInterval: TableAutoRefreshInterval = .off + var autoRefreshPause: TableAutoRefreshPause? var pagination: PaginationState var chartConfiguration: ResultChartConfiguration var mapConfiguration: ResultMapConfiguration @@ -375,6 +378,8 @@ struct QueryTab: Identifiable, Equatable { && lhs.sortState == rhs.sortState && lhs.valueFilter == rhs.valueFilter && lhs.sessionHighlightRules == rhs.sessionHighlightRules + && lhs.autoRefreshInterval == rhs.autoRefreshInterval + && lhs.autoRefreshPause == rhs.autoRefreshPause && lhs.chartConfiguration == rhs.chartConfiguration && lhs.mapConfiguration == rhs.mapConfiguration && lhs.display == rhs.display diff --git a/apple/TablePro/Models/Query/QueryTabManager.swift b/apple/TablePro/Models/Query/QueryTabManager.swift index 42d045985b..02dd5453ad 100644 --- a/apple/TablePro/Models/Query/QueryTabManager.swift +++ b/apple/TablePro/Models/Query/QueryTabManager.swift @@ -526,7 +526,8 @@ final class QueryTabManager: ObservableObject { var newTab = QueryTab(title: Self.objectSourceTitle(for: objectRef), tabType: .objectSource) newTab.tableContext.isEditable = false newTab.tableContext.databaseName = objectRef.database - newTab.tableContext.schemaName = objectRef.schema + // A database-wide object's schema is where it installs its objects, not a namespace to browse. + newTab.tableContext.schemaName = objectRef.isDatabaseScoped ? nil : objectRef.schema newTab.display.objectRef = objectRef newTab.hasUserInteraction = true tabs.append(newTab) @@ -639,6 +640,8 @@ final class QueryTabManager: ObservableObject { case .userType: format = String(localized: "Type: %@") case .view: format = String(localized: "View: %@") case .materializedView: format = String(localized: "Materialized View: %@") + case .serverObject: + return String(format: String(localized: "%1$@: %2$@"), objectRef.kindDisplayName, objectRef.displayIdentity) } return String(format: format, objectRef.displayIdentity) } @@ -697,6 +700,8 @@ final class QueryTabManager: ObservableObject { tab.restoredSortSource = .unset tab.selectionSnapshot = .empty tab.pendingChanges = TabChangeSnapshot() + tab.autoRefreshInterval = .off + tab.autoRefreshPause = nil tab.hasUserInteraction = false tab.filterState = TabFilterState() tab.valueFilter = GridValueFilterState() diff --git a/apple/TablePro/Models/Query/ResultStatusModel.swift b/apple/TablePro/Models/Query/ResultStatusModel.swift index 665075df18..b7efcf26d7 100644 --- a/apple/TablePro/Models/Query/ResultStatusModel.swift +++ b/apple/TablePro/Models/Query/ResultStatusModel.swift @@ -44,6 +44,7 @@ struct ResultStatusControls: Equatable { var showsColumns = false var showsHighlightRules = false var showsFilters = false + var showsAutoRefresh = false var showsPagination = false /// First, Previous, Next, Last and the page number, which an engine that cannot skip rows has /// no use for. The rows-per-page menu stays, because it still sets how many leading rows load. @@ -130,6 +131,8 @@ struct ResultStatusModel: Equatable { controls.showsHighlightRules = viewMode == .data && describesAResult controls.showsSelectionSummary = viewMode == .data && describesAResult controls.showsFilters = viewMode.showsRowFilters && isTable && snapshot.hasTableName + // Data only, the one mode a tick runs in; a query tab's SQL may write, so it never re-runs on a timer. + controls.showsAutoRefresh = viewMode == .data && isTable && snapshot.hasTableName controls.showsPagination = viewMode.showsResultScope && isTable && snapshot.hasTableName controls.showsPageNavigation = controls.showsPagination && snapshot.paginationCapability.allowsSeeking diff --git a/apple/TablePro/Models/Query/ServerObjectInfo.swift b/apple/TablePro/Models/Query/ServerObjectInfo.swift new file mode 100644 index 0000000000..1aa231984e --- /dev/null +++ b/apple/TablePro/Models/Query/ServerObjectInfo.swift @@ -0,0 +1,90 @@ +// +// ServerObjectInfo.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +// One object of a kind the driver declares. Equality follows `id`, so a definition read later never +// makes a reload stop matching the row it replaces. +struct ServerObjectInfo: Identifiable, Hashable, Sendable { + let kindId: String + let name: String + let schema: String? + let identity: String? + let detail: String? + let isEnabled: Bool? + let definition: String? + let attributes: [ObjectAttribute] + + init( + kindId: String, + name: String, + schema: String? = nil, + identity: String? = nil, + detail: String? = nil, + isEnabled: Bool? = nil, + definition: String? = nil, + attributes: [ObjectAttribute] = [] + ) { + self.kindId = kindId + self.name = name + self.schema = schema + self.identity = identity + self.detail = detail + self.isEnabled = isEnabled + self.definition = definition + self.attributes = attributes + } + + var id: String { + let path = IdentityPath.qualified(name: name, schema: schema) + guard let identity, !identity.isEmpty else { return "\(kindId)_\(path)" } + return "\(kindId)_\(path)_\(identity)" + } + + static func == (lhs: ServerObjectInfo, rhs: ServerObjectInfo) -> Bool { + lhs.id == rhs.id + } + + func hash(into hasher: inout Hasher) { + hasher.combine(id) + } + + func adoptingSchema(_ fallback: String?) -> ServerObjectInfo { + guard schema?.isEmpty != false, let fallback, !fallback.isEmpty else { return self } + return ServerObjectInfo( + kindId: kindId, name: name, schema: fallback, identity: identity, detail: detail, + isEnabled: isEnabled, definition: definition, attributes: attributes + ) + } +} + +extension ServerObjectInfo { + init(_ object: PluginServerObjectInfo) { + self.init( + kindId: object.kindId, + name: object.name, + schema: object.schema, + identity: object.identity, + detail: object.detail, + isEnabled: object.isEnabled, + definition: object.definition, + attributes: object.attributes.map(ObjectAttribute.init) + ) + } + + var pluginObject: PluginServerObjectInfo { + PluginServerObjectInfo( + kindId: kindId, + name: name, + schema: schema, + identity: identity, + detail: detail, + isEnabled: isEnabled, + definition: definition, + attributes: attributes.map(\.pluginAttribute) + ) + } +} diff --git a/apple/TablePro/Models/Query/TableAutoRefreshGate.swift b/apple/TablePro/Models/Query/TableAutoRefreshGate.swift new file mode 100644 index 0000000000..11a638ee1b --- /dev/null +++ b/apple/TablePro/Models/Query/TableAutoRefreshGate.swift @@ -0,0 +1,96 @@ +// +// TableAutoRefreshGate.swift +// TablePro +// + +import Foundation + +enum TableAutoRefreshPause: Equatable, Sendable { + case notVisible + case disconnected + case otherViewMode + case openTransaction + case unsavedChanges + case editing + case interacting + case valueWindowOpen + case loading + case safeModePrompt + case unkeyedSelection + + // Nil where nobody can read the bar's help anyway, or the pause lasts only as long as a gesture. + var help: String? { + switch self { + case .disconnected: + String(localized: "Auto Refresh waits until the connection is back.") + case .openTransaction: + String(localized: "Auto Refresh waits while a transaction is open.") + case .unsavedChanges: + String(localized: "Auto Refresh waits while the table has unsaved changes.") + case .editing: + String(localized: "Auto Refresh waits while a cell is being edited.") + case .valueWindowOpen: + String(localized: "Auto Refresh waits while a value is open in its own window.") + case .loading: + String(localized: "Auto Refresh waits while rows load.") + case .unkeyedSelection: + String(localized: "Auto Refresh waits while rows are selected in a table without a primary key.") + case .notVisible, .otherViewMode, .interacting, .safeModePrompt: + nil + } + } +} + +enum TableAutoRefreshDecision: Equatable, Sendable { + case refresh + case wait(TableAutoRefreshPause) + case stop +} + +struct TableAutoRefreshGate: Equatable, Sendable { + enum Load: Equatable, Sendable { + case idle + case busy + } + + var isTableTab = true + var previousTickFailed = false + var hasLoaded = true + var windowIsVisible = true + var isSelectedInWindow = true + var isConnected = true + var viewMode: ResultsViewMode = .data + var holdsTransaction = false + var holdsUnsavedWork = false + var hasSidebarDataWork = false + var isEditingCell = false + var gridHoldsTransientUI = false + var gridIsTrackingInteraction = false + var inspectorHoldsValueWindow = false + var load: Load = .idle + var isCountPending = false + var isLoadingMore = false + var showsSafeModePrompt = false + var hasSelection = false + var hasPrimaryKey = true + + // Never the app being active: watching a jobs table while working in another app is the point. + var decision: TableAutoRefreshDecision { + guard isTableTab, !previousTickFailed else { return .stop } + guard windowIsVisible, isSelectedInWindow else { return .wait(.notVisible) } + guard isConnected else { return .wait(.disconnected) } + guard viewMode == .data else { return .wait(.otherViewMode) } + guard !holdsTransaction else { return .wait(.openTransaction) } + // Ahead of unsaved work, which counts an open editor too. + guard !isEditingCell else { return .wait(.editing) } + guard !holdsUnsavedWork, !hasSidebarDataWork else { return .wait(.unsavedChanges) } + // A reload closes the grid's viewers and the value filter, and moves rows under a drag or a scroll. + guard !gridHoldsTransientUI, !gridIsTrackingInteraction else { return .wait(.interacting) } + guard !inspectorHoldsValueWindow else { return .wait(.valueWindowOpen) } + guard hasLoaded, load == .idle, !isCountPending, !isLoadingMore else { return .wait(.loading) } + guard !showsSafeModePrompt else { return .wait(.safeModePrompt) } + // Rows are kept across a reload by primary key, so without one the selection would be dropped. + guard !hasSelection || hasPrimaryKey else { return .wait(.unkeyedSelection) } + return .refresh + } +} diff --git a/apple/TablePro/Models/Query/TableAutoRefreshInterval.swift b/apple/TablePro/Models/Query/TableAutoRefreshInterval.swift new file mode 100644 index 0000000000..b41b60ff92 --- /dev/null +++ b/apple/TablePro/Models/Query/TableAutoRefreshInterval.swift @@ -0,0 +1,38 @@ +// +// TableAutoRefreshInterval.swift +// TablePro +// + +import Foundation + +enum TableAutoRefreshInterval: Int, CaseIterable, Identifiable, Sendable { + case off = 0 + case fiveSeconds = 5 + case tenSeconds = 10 + case thirtySeconds = 30 + case oneMinute = 60 + case fiveMinutes = 300 + + var id: Int { rawValue } + + var isOn: Bool { self != .off } + + var duration: Duration { .seconds(rawValue) } + + var title: String { + switch self { + case .off: String(localized: "Off") + case .fiveSeconds: String(localized: "Every 5 Seconds") + case .tenSeconds: String(localized: "Every 10 Seconds") + case .thirtySeconds: String(localized: "Every 30 Seconds") + case .oneMinute: String(localized: "Every Minute") + case .fiveMinutes: String(localized: "Every 5 Minutes") + } + } + + // The status bar's regular tier names the interval beside the glyph, so it has to stay short in every language. + var shortTitle: String { + guard isOn else { return String(localized: "Off") } + return duration.formatted(.units(allowed: [.minutes, .seconds], width: .narrow)) + } +} diff --git a/apple/TablePro/Models/Query/TableLoadTrigger.swift b/apple/TablePro/Models/Query/TableLoadTrigger.swift index 744f343fd2..13067926e3 100644 --- a/apple/TablePro/Models/Query/TableLoadTrigger.swift +++ b/apple/TablePro/Models/Query/TableLoadTrigger.swift @@ -8,6 +8,31 @@ import Foundation internal enum TableLoadTrigger { case userInitiated case restore + case autoRefresh + + // A timer's read at 5 s would write 720 rows an hour per tab. + var recordsHistory: Bool { + switch self { + case .userInitiated, .restore: true + case .autoRefresh: false + } + } + + // At 5 s a tick's trace would push every real load out of the diagnostics store within hours. + var tracesLoad: Bool { + switch self { + case .userInitiated, .restore: true + case .autoRefresh: false + } + } + + // A count slower than the interval would otherwise run on every tick and never land; only a manual Refresh recounts. + var countsRows: Bool { + switch self { + case .userInitiated, .restore: true + case .autoRefresh: false + } + } } // Keyed by tab: a window-wide trigger was consumed against whatever tab was selected when the connection came back. diff --git a/apple/TablePro/Models/ServerDashboard/ServerDashboardModels.swift b/apple/TablePro/Models/ServerDashboard/ServerDashboardModels.swift index d21d7ca284..9f5b9d63b6 100644 --- a/apple/TablePro/Models/ServerDashboard/ServerDashboardModels.swift +++ b/apple/TablePro/Models/ServerDashboard/ServerDashboardModels.swift @@ -28,7 +28,7 @@ enum DashboardRefreshInterval: Double, CaseIterable, Identifiable { var displayLabel: String { switch self { case .off: return String(localized: "Off") - default: return "\(Int(rawValue))s" + default: return Duration.seconds(rawValue).formatted(.units(allowed: [.seconds], width: .narrow)) } } } diff --git a/apple/TablePro/Models/Sidebar/SidebarObjectKind.swift b/apple/TablePro/Models/Sidebar/SidebarObjectKind.swift index 3ce26cf891..56b5962f37 100644 --- a/apple/TablePro/Models/Sidebar/SidebarObjectKind.swift +++ b/apple/TablePro/Models/Sidebar/SidebarObjectKind.swift @@ -6,16 +6,16 @@ struct DatabaseTreeObjectGroup: Hashable, Sendable { let kind: SidebarObjectKind } -/// Which model a kind's rows are drawn from. Every helper that used to ask `isRoutine` and pick -/// one of two buckets asks this instead, so a kind added later cannot silently read the wrong one. +// Which model a kind's rows are drawn from, so a kind added later cannot read the wrong bucket. enum SidebarObjectCategory: Sendable, Hashable { case table case routine case trigger case type + case server } -enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { +enum SidebarObjectKind: Sendable, Hashable { case table case view case materializedView @@ -25,6 +25,50 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { case function case trigger case type + case server(SidebarServerObjectKind) + + static let builtIn: [SidebarObjectKind] = [ + .table, .view, .materializedView, .foreignTable, .sequence, .procedure, .function, .trigger, .type, + ] + + static func ordered(declared: [SidebarServerObjectKind]) -> [SidebarObjectKind] { + builtIn + declared.map { SidebarObjectKind.server($0) } + } + + private static let serverPersistencePrefix = "server." + + // Built-in spellings are shipped in UserDefaults keys, the tree expansion JSON and node ids. + var persistenceName: String { + switch self { + case .table: return "table" + case .view: return "view" + case .materializedView: return "materializedView" + case .foreignTable: return "foreignTable" + case .sequence: return "sequence" + case .procedure: return "procedure" + case .function: return "function" + case .trigger: return "trigger" + case .type: return "type" + case .server(let kind): return Self.serverPersistencePrefix + kind.id + } + } + + // A declared kind decodes before any driver exists, so it carries only its id. + init?(persistenceName: String) { + if let builtIn = Self.builtIn.first(where: { $0.persistenceName == persistenceName }) { + self = builtIn + return + } + guard persistenceName.hasPrefix(Self.serverPersistencePrefix) else { return nil } + let id = String(persistenceName.dropFirst(Self.serverPersistencePrefix.count)) + guard !id.isEmpty else { return nil } + self = .server(SidebarServerObjectKind(id: id)) + } + + var serverKind: SidebarServerObjectKind? { + if case .server(let kind) = self { return kind } + return nil + } var displayName: String { switch self { @@ -37,6 +81,7 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { case .trigger: return String(localized: "Trigger") case .type: return String(localized: "Type") case .sequence: return String(localized: "Sequence") + case .server(let kind): return kind.displayName } } @@ -51,6 +96,7 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { case .trigger: return String(localized: "Triggers") case .type: return String(localized: "Types") case .sequence: return String(localized: "Sequences") + case .server(let kind): return kind.pluralDisplayName } } @@ -65,6 +111,7 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { case .trigger: return String(localized: "No triggers") case .type: return String(localized: "No types") case .sequence: return String(localized: "No sequences") + case .server: return String(localized: "No items") } } @@ -87,6 +134,7 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { case .trigger: return "bolt" case .type: return "cube" case .sequence: return "number" + case .server(let kind): return kind.iconName } } @@ -96,6 +144,7 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { case .procedure, .function: return .routine case .trigger: return .trigger case .type: return .type + case .server: return .server } } @@ -113,22 +162,13 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { self == .table } - /// Which kinds a container lists, in declaration order: the kinds holding objects, and the kinds in - /// `keeping`. Both sidebar layouts ask the same question, so toggling the layout never changes which - /// kinds are on screen. - /// - /// A kind is listed for what its driver returned, never for what the plugin declares. Gating on a - /// capability flag hid objects that came back, and listing every declared kind filled the sidebar - /// with Procedures, Triggers and Types sections that held nothing. - /// - /// `keeping` is for a section with something to say and no rows to say it with: a table kind whose - /// objects all sit in folders, which is where a drag takes one back out, and a kind whose fetch - /// failed, which shows the error where its rows would be. + // Listed for its rows or for `keeping` (filed into folders, or a failed fetch), never for being declared. static func visible( itemCounts: [SidebarObjectKind: Int], - keeping: Set = [] + keeping: Set = [], + declared: [SidebarServerObjectKind] = [] ) -> [SidebarObjectKind] { - allCases.filter { kind in + ordered(declared: declared).filter { kind in itemCounts[kind, default: 0] > 0 || keeping.contains(kind) } } diff --git a/apple/TablePro/Models/Sidebar/SidebarServerObjectKind.swift b/apple/TablePro/Models/Sidebar/SidebarServerObjectKind.swift new file mode 100644 index 0000000000..ebf242fbff --- /dev/null +++ b/apple/TablePro/Models/Sidebar/SidebarServerObjectKind.swift @@ -0,0 +1,83 @@ +// +// SidebarServerObjectKind.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +// An object kind a driver declares, such as MySQL events. The app reads it as an opaque descriptor and +// compares it by id alone, so a kind decoded from storage with no driver equals the declared one. +struct SidebarServerObjectKind: Sendable, Hashable { + enum Scope: String, Sendable, Hashable, Codable { + case container + case database + } + + let id: String + let displayName: String + let pluralDisplayName: String + let iconName: String + let scope: Scope + + init(id: String, displayName: String, pluralDisplayName: String, iconName: String, scope: Scope) { + self.id = id + self.displayName = displayName + self.pluralDisplayName = pluralDisplayName + self.iconName = iconName + self.scope = scope + } + + init(id: String) { + self.init(id: id, displayName: id, pluralDisplayName: id, iconName: "shippingbox", scope: .container) + } + + init(_ kind: PluginServerObjectKind) { + let scope: Scope + switch kind.scope { + case .container: scope = .container + case .database: scope = .database + @unknown default: scope = .container + } + self.init( + id: kind.id, + displayName: kind.displayName, + pluralDisplayName: kind.pluralDisplayName, + iconName: kind.iconName, + scope: scope + ) + } + + static func == (lhs: SidebarServerObjectKind, rhs: SidebarServerObjectKind) -> Bool { + lhs.id == rhs.id + } + + func hash(into hasher: inout Hasher) { + hasher.combine(id) + } + + // Menu validation and every redraw ask, and a driver answers the same for its whole life. + @MainActor private static let probed = NSMapTable.weakToStrongObjects() + + @MainActor + static func declared(by driver: DatabaseDriver?) -> [SidebarServerObjectKind] { + guard let driver else { return [] } + if let known = probed.object(forKey: driver) { + return known.kinds + } + var seen = Set() + let kinds = driver.serverObjectKinds + .map(SidebarServerObjectKind.init) + .filter { !$0.id.isEmpty && seen.insert($0.id).inserted } + probed.setObject(DeclaredKinds(kinds), forKey: driver) + return kinds + } + + private final class DeclaredKinds { + let kinds: [SidebarServerObjectKind] + + init(_ kinds: [SidebarServerObjectKind]) { + self.kinds = kinds + } + } +} diff --git a/apple/TablePro/Models/UI/KeyboardShortcutModels.swift b/apple/TablePro/Models/UI/KeyboardShortcutModels.swift index 81f7d56b94..ee2a1a2244 100644 --- a/apple/TablePro/Models/UI/KeyboardShortcutModels.swift +++ b/apple/TablePro/Models/UI/KeyboardShortcutModels.swift @@ -68,6 +68,8 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable { case moveTabToConnection case showServerDashboard case showQueryInsights + case commitTransaction + case rollbackTransaction // Editor & Query case openFile @@ -197,7 +199,7 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable { var category: ShortcutCategory { switch self { case .manageConnections, .newConnection, .openDatabase, .switchConnection, .moveTabToConnection, - .showServerDashboard, .showQueryInsights: + .showServerDashboard, .showQueryInsights, .commitTransaction, .rollbackTransaction: return .connections case .openFile, .saveChanges, .saveAs, .copyPath, .copyRelativePath, .executeQuery, .executeAllStatements, .executeQueryWithoutLimit, .cancelQuery, .explainQuery, .formatQuery, @@ -307,6 +309,8 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable { case .moveTabToConnection: return String(localized: "Move Tab to Connection") case .showServerDashboard: return String(localized: "Server Dashboard") case .showQueryInsights: return String(localized: "Query Insights") + case .commitTransaction: return String(localized: "Commit Transaction") + case .rollbackTransaction: return String(localized: "Roll Back Transaction") case .saveChanges: return String(localized: "Save Changes") case .saveAs: return String(localized: "Save As") case .copyPath: return String(localized: "Copy Path") @@ -660,7 +664,8 @@ struct KeyboardSettings: Codable, Equatable { // MARK: - Default Shortcuts - // An action absent here shows as unassigned until the user binds it. + // An action absent here shows as unassigned until the user binds it. Commit and Roll Back Transaction ship + // unbound: they are infrequent, and a stray Roll Back discards work. static let defaultShortcuts: [ShortcutAction: BoundKey] = [ // Connections .newConnection: .character("n", command: true), diff --git a/apple/TablePro/Models/UI/WindowSidebarState.swift b/apple/TablePro/Models/UI/WindowSidebarState.swift index 5dc1aed215..571c1f9bcd 100644 --- a/apple/TablePro/Models/UI/WindowSidebarState.swift +++ b/apple/TablePro/Models/UI/WindowSidebarState.swift @@ -111,8 +111,7 @@ internal final class WindowSidebarState: ObservableObject { isLoaded = true } - /// The kind rides as its raw string. Decoding it as `SidebarObjectKind` throws on a value this - /// build does not know, and one throw inside this blob discards every other expansion set. + // The kind rides as its persistence name, so a value this build does not know drops only that entry. private struct PersistedObjectGroup: Codable { let database: String let schema: String? @@ -176,7 +175,7 @@ internal final class WindowSidebarState: ObservableObject { from persisted: [PersistedObjectGroup] ) -> [DatabaseTreeObjectGroup: Bool] { persisted.reduce(into: [:]) { result, entry in - guard let kind = SidebarObjectKind(rawValue: entry.kind) else { return } + guard let kind = SidebarObjectKind(persistenceName: entry.kind) else { return } let group = DatabaseTreeObjectGroup(database: entry.database, schema: entry.schema, kind: kind) result[group] = entry.expanded } @@ -189,7 +188,7 @@ internal final class WindowSidebarState: ObservableObject { PersistedObjectGroup( database: group.database, schema: group.schema, - kind: group.kind.rawValue, + kind: group.kind.persistenceName, expanded: expanded ) } diff --git a/apple/TablePro/ViewModels/ERDiagramViewModel+Editing.swift b/apple/TablePro/ViewModels/ERDiagramViewModel+Editing.swift index 3332224271..3989cce6c4 100644 --- a/apple/TablePro/ViewModels/ERDiagramViewModel+Editing.swift +++ b/apple/TablePro/ViewModels/ERDiagramViewModel+Editing.swift @@ -22,13 +22,160 @@ struct ERDiagramColumnHit: Equatable { } struct ERDiagramForeignKeyDraft: Equatable { + struct ColumnPair: Identifiable, Equatable { + let id: UUID + var column: String + var referencedColumn: String + + init(id: UUID = UUID(), column: String, referencedColumn: String) { + self.id = id + self.column = column + self.referencedColumn = referencedColumn + } + } + + enum Issue: Equatable { + case noPairs + case incompletePair + case repeatedColumn(String) + case repeatedReferencedColumn(String) + + var message: String { + switch self { + case .noPairs, .incompletePair: + String(localized: "Choose a column and a referenced column in every row.") + case .repeatedColumn(let name), .repeatedReferencedColumn(let name): + String(format: String(localized: "“%@” is used more than once."), name) + } + } + } + let table: String - var column: String + var pairs: [ColumnPair] var referencedTable: String - var referencedColumn: String var name: String var onDelete: EditableForeignKeyDefinition.ReferentialAction = .noAction var onUpdate: EditableForeignKeyDefinition.ReferentialAction = .noAction + + var issue: Issue? { + guard !pairs.isEmpty else { return .noPairs } + guard !pairs.contains(where: { $0.column.isEmpty || $0.referencedColumn.isEmpty }) else { return .incompletePair } + if let repeated = Self.firstRepeated(pairs.map(\.column)) { return .repeatedColumn(repeated) } + if let repeated = Self.firstRepeated(pairs.map(\.referencedColumn)) { return .repeatedReferencedColumn(repeated) } + return nil + } + + // Pair order is key order on both sides, which is what the database matches the referenced key by. + var foreignKey: EditableForeignKeyDefinition { + var foreignKey = EditableForeignKeyDefinition.placeholder() + foreignKey.name = name.trimmingCharacters(in: .whitespacesAndNewlines) + foreignKey.columns = pairs.map(\.column) + foreignKey.referencedTable = referencedTable + foreignKey.referencedColumns = pairs.map(\.referencedColumn) + foreignKey.onDelete = onDelete + foreignKey.onUpdate = onUpdate + return foreignKey + } + + // A drop on a member of a composite key offers the whole key, with the dragged pair at that member's position. + static func starting( + table: String, + column: String, + referencedTable: String, + keyColumns: [String], + target: String?, + sourceColumns: [String], + name: String + ) -> ERDiagramForeignKeyDraft { + let pairs: [ColumnPair] + if let target, !keyColumns.contains(target) { + pairs = [ColumnPair(column: column, referencedColumn: target)] + } else if keyColumns.isEmpty { + pairs = [ColumnPair(column: column, referencedColumn: target ?? "")] + } else { + let position = target.flatMap { keyColumns.firstIndex(of: $0) } + pairs = guessedPairs(for: keyColumns, keeping: column, at: position, from: sourceColumns) + } + return ERDiagramForeignKeyDraft(table: table, pairs: pairs, referencedTable: referencedTable, name: name) + } + + func settingColumn(_ column: String, of id: UUID) -> ERDiagramForeignKeyDraft { + var draft = self + guard let index = draft.pairs.firstIndex(where: { $0.id == id }) else { return self } + draft.pairs[index].column = column + return draft + } + + func settingReferencedColumn(_ column: String, of id: UUID) -> ERDiagramForeignKeyDraft { + var draft = self + guard let index = draft.pairs.firstIndex(where: { $0.id == id }) else { return self } + draft.pairs[index].referencedColumn = column + return draft + } + + func addingPair() -> ERDiagramForeignKeyDraft { + var draft = self + draft.pairs.append(ColumnPair(column: "", referencedColumn: "")) + return draft + } + + // The last pair stays: a key needs at least one. + func removingPairs(_ ids: Set) -> ERDiagramForeignKeyDraft { + let kept = pairs.filter { !ids.contains($0.id) } + guard !kept.isEmpty else { return self } + var draft = self + draft.pairs = kept + return draft + } + + func movingPairs(fromOffsets source: IndexSet, toOffset destination: Int) -> ERDiagramForeignKeyDraft { + let moving = source.filter { pairs.indices.contains($0) }.map { pairs[$0] } + var remaining = pairs.enumerated().filter { !source.contains($0.offset) }.map(\.element) + let insertion = destination - source.filter { $0 < destination }.count + remaining.insert(contentsOf: moving, at: min(max(insertion, 0), remaining.count)) + var draft = self + draft.pairs = remaining + return draft + } + + // The referenced side follows the referenced table's key, so both change in one write. + func referencing(_ table: String, keyColumns: [String], sourceColumns: [String]) -> ERDiagramForeignKeyDraft { + var draft = self + draft.referencedTable = table + let first = pairs.first?.column ?? "" + draft.pairs = keyColumns.isEmpty + ? [ColumnPair(column: first, referencedColumn: "")] + : Self.guessedPairs(for: keyColumns, keeping: first, at: nil, from: sourceColumns) + return draft + } + + // Unplaced, the kept column takes a key column no other column is named after, else the last: a table's own id shares the referenced id's name. + private static func guessedPairs( + for keyColumns: [String], + keeping column: String, + at position: Int?, + from sourceColumns: [String] + ) -> [ColumnPair] { + let others = sourceColumns.filter { $0 != column } + func namesake(of key: String, excluding used: Set) -> String? { + others.first { !used.contains($0) && $0.caseInsensitiveCompare(key) == .orderedSame } + } + let slot = position + ?? keyColumns.firstIndex { namesake(of: $0, excluding: []) == nil } + ?? max(keyColumns.count - 1, 0) + var used: Set = [] + return keyColumns.enumerated().map { offset, key in + guard offset != slot else { return ColumnPair(column: column, referencedColumn: key) } + let match = namesake(of: key, excluding: used) ?? "" + if !match.isEmpty { used.insert(match) } + return ColumnPair(column: match, referencedColumn: key) + } + } + + private static func firstRepeated(_ names: [String]) -> String? { + var seen = Set() + return names.first { !seen.insert($0).inserted } + } } enum ERDiagramEditSheet: Identifiable, Equatable { @@ -38,7 +185,7 @@ enum ERDiagramEditSheet: Identifiable, Equatable { var id: String { switch self { - case .addForeignKey(let draft): "fk:\(draft.table).\(draft.column)" + case .addForeignKey(let draft): "fk:\(draft.table).\(draft.pairs.first?.column ?? "")" case .addColumn(let table): "add:\(table)" case .renameColumn(let table, let column): "rename:\(table).\(column)" } @@ -306,12 +453,13 @@ extension ERDiagramViewModel { let referencedTable = target?.tableName ?? tables.first { $0 != table } ?? table - let referencedColumn = target?.columnName ?? defaultReferencedColumn(in: referencedTable) - editSheet = .addForeignKey(ERDiagramForeignKeyDraft( + editSheet = .addForeignKey(.starting( table: table, column: column, referencedTable: referencedTable, - referencedColumn: referencedColumn, + keyColumns: referencedKeyColumns(in: referencedTable), + target: target?.columnName, + sourceColumns: columnNames(of: table), name: defaultForeignKeyName(table: table, column: column) )) } @@ -327,12 +475,14 @@ extension ERDiagramViewModel { return node.columns.filter { $0.stagedEdit != .dropped }.map(\.name) } - func defaultReferencedColumn(in table: String) -> String { + // In table order, which is the key's own order unless it was declared in another; the reader can reorder the pairs. + func referencedKeyColumns(in table: String) -> [String] { guard let id = stagedFullGraph.nodeIndex[table], let node = stagedFullGraph.nodes.first(where: { $0.id == id }) - else { return "" } - let keys = node.columns.filter { $0.isPrimaryKey && $0.stagedEdit != .dropped } - return (keys.count == 1 ? keys.first : node.columns.first { $0.stagedEdit != .dropped })?.name ?? "" + else { return [] } + let kept = node.columns.filter { $0.stagedEdit != .dropped } + let keys = kept.filter(\.isPrimaryKey).map(\.name) + return keys.isEmpty ? kept.prefix(1).map(\.name) : keys } // MySQL wants a foreign key's name unique in the whole database, so it is checked against every key drawn. @@ -360,13 +510,7 @@ extension ERDiagramViewModel { } func stageForeignKey(_ draft: ERDiagramForeignKeyDraft) { - var foreignKey = EditableForeignKeyDefinition.placeholder() - foreignKey.name = draft.name.trimmingCharacters(in: .whitespacesAndNewlines) - foreignKey.columns = [draft.column] - foreignKey.referencedTable = draft.referencedTable - foreignKey.referencedColumns = [draft.referencedColumn] - foreignKey.onDelete = draft.onDelete - foreignKey.onUpdate = draft.onUpdate + let foreignKey = draft.foreignKey stage { try await $0.addForeignKey(foreignKey, to: draft.table) } } diff --git a/apple/TablePro/ViewModels/SidebarViewModel.swift b/apple/TablePro/ViewModels/SidebarViewModel.swift index 08ecd41429..5515d5ea79 100644 --- a/apple/TablePro/ViewModels/SidebarViewModel.swift +++ b/apple/TablePro/ViewModels/SidebarViewModel.swift @@ -293,9 +293,14 @@ final class SidebarViewModel: ObservableObject { private static func loadInitialExpansion(connectionId: UUID, defaults: UserDefaults) -> ExpansionState { var values: [SidebarObjectKind: Bool] = [:] - for kind in SidebarObjectKind.allCases { + for kind in SidebarObjectKind.builtIn { values[kind] = loadKindExpansion(connectionId: connectionId, kind: kind, defaults: defaults) } + let serverKey = SidebarPersistenceKey.serverKindsExpanded(connectionId: connectionId) + for (id, expanded) in defaults.dictionary(forKey: serverKey) ?? [:] { + guard let expanded = expanded as? Bool, !id.isEmpty else { continue } + values[.server(SidebarServerObjectKind(id: id))] = expanded + } return ExpansionState(values: values) } @@ -338,12 +343,20 @@ final class SidebarViewModel: ObservableObject { } private func persistExpansion(oldValue: ExpansionState) { - for kind in SidebarObjectKind.allCases where oldValue[kind] != expanded[kind] { + for kind in SidebarObjectKind.builtIn where oldValue[kind] != expanded[kind] { defaults.set( expanded[kind], forKey: SidebarPersistenceKey.expanded(connectionId: connectionId, kind: kind) ) } + let serverKinds = Set(oldValue.values.keys).union(expanded.values.keys).compactMap(\.serverKind) + guard serverKinds.contains(where: { oldValue[.server($0)] != expanded[.server($0)] }) else { return } + var stored: [String: Bool] = [:] + for (kind, isExpanded) in expanded.values { + guard let serverKind = kind.serverKind else { continue } + stored[serverKind.id] = isExpanded + } + defaults.set(stored, forKey: SidebarPersistenceKey.serverKindsExpanded(connectionId: connectionId)) } // MARK: - Batch Operations @@ -472,6 +485,7 @@ final class SidebarViewModel: ObservableObject { private var cachedFilteredTriggersFingerprint: (count: Int, generation: Int, query: String)? private var cachedFilteredUserTypes: [UserDefinedTypeInfo] = [] private var cachedFilteredUserTypesFingerprint: (count: Int, generation: Int, query: String)? + private var cachedFilteredServerObjects: [String: (count: Int, generation: Int, query: String, objects: [ServerObjectInfo])] = [:] private var schemaGeneration: Int { SchemaService.shared.generationToken(for: connectionId) @@ -494,7 +508,7 @@ final class SidebarViewModel: ObservableObject { if cachedFilteredByKindFingerprint?.count != fingerprint.count || cachedFilteredByKindFingerprint?.generation != fingerprint.generation || cachedFilteredByKindFingerprint?.query != fingerprint.query { - for tableKind in SidebarObjectKind.allCases where tableKind.category == .table { + for tableKind in SidebarObjectKind.builtIn where tableKind.category == .table { cachedFilteredByKind[tableKind] = applyQuery(query, to: self.tables(of: tableKind, from: tables)) } cachedFilteredByKindFingerprint = fingerprint @@ -550,6 +564,18 @@ final class SidebarViewModel: ObservableObject { return cachedFilteredUserTypes } + func filteredServerObjects(kindId: String, from objects: [ServerObjectInfo]) -> [ServerObjectInfo] { + let query = filterQuery + let generation = schemaGeneration + if let cached = cachedFilteredServerObjects[kindId], + cached.count == objects.count, cached.generation == generation, cached.query == query { + return cached.objects + } + let filtered = DatabaseTreeFilter.filteredServerObjects(objects, searchText: query, database: browsedDatabase) + cachedFilteredServerObjects[kindId] = (objects.count, generation, query, filtered) + return filtered + } + func effectiveExpanded(kind: SidebarObjectKind, hasMatches: Bool) -> Bool { if !filterQuery.isEmpty && hasMatches { return true } return expanded[kind] @@ -660,7 +686,7 @@ final class SidebarViewModel: ObservableObject { private func rebuildKindBuckets(from tables: [TableInfo]) { var buckets: [SidebarObjectKind: [TableInfo]] = [:] - for kind in SidebarObjectKind.allCases { + for kind in SidebarObjectKind.builtIn { buckets[kind] = [] } for table in tables { @@ -675,6 +701,7 @@ final class SidebarViewModel: ObservableObject { cachedFilteredByKindFingerprint = nil cachedFilteredRoutines = [:] cachedFilteredRoutinesFingerprint = nil + cachedFilteredServerObjects = [:] } /// Clearing the field, or typing the first character into an empty one, changes what the list diff --git a/apple/TablePro/ViewModels/VersionHistoryViewModel.swift b/apple/TablePro/ViewModels/VersionHistoryViewModel.swift index 6d275ca2b5..d213ba80d1 100644 --- a/apple/TablePro/ViewModels/VersionHistoryViewModel.swift +++ b/apple/TablePro/ViewModels/VersionHistoryViewModel.swift @@ -117,7 +117,8 @@ internal final class VersionHistoryViewModel: ObservableObject { } catch { guard !Task.isCancelled else { return } Self.logger.warning("Version history failed to load: \(error.publicLogShape, privacy: .public)") - let subjectIsGone = (error as? VersionHistoryError) == .subjectNotFound + let failure = error as? VersionHistoryError + let subjectIsGone = failure == .subjectNotFound || failure == .tabClosed guard page.entries.isEmpty || subjectIsGone else { return } page = .empty selection = nil diff --git a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift index 3be561720c..a9c0791106 100644 --- a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift +++ b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift @@ -371,6 +371,7 @@ final class ConnectionFormCoordinator: ObservableObject { queryTimeoutSeconds: customization.queryTimeoutSeconds, binaryInlineLimitKilobytes: customization.binaryInlineLimitKilobytes, completionIdentifierCase: customization.completionIdentifierCase, + commitMode: customization.commitMode, redisDatabase: advanced.additionalFieldValues[RedisDatabaseIndex.fieldName].map { RedisDatabaseIndex.parse($0) ?? 0 }, diff --git a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift index 84c5296869..17ae32741e 100644 --- a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift +++ b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift @@ -355,6 +355,7 @@ struct OptionsPaneView: View { private var safetySection: some View { Section { safeModeRow + Toggle(String(localized: "Manual Commit"), isOn: manualCommitBinding) if aiIsEnabled { Picker(String(localized: "AI Policy"), selection: $coordinator.advanced.aiPolicy) { Text(String(localized: "Use Default")) @@ -392,12 +393,20 @@ struct OptionsPaneView: View { } } + private var manualCommitBinding: Binding { + Binding( + get: { coordinator.customization.commitMode == .manual }, + set: { coordinator.customization.commitMode = $0 ? .manual : .automatic } + ) + } + @ViewBuilder private var accessFooter: some View { VStack(alignment: .leading, spacing: 4) { if let floor = coordinator.safeModeFloor { Text(floor.explanation) } + Text(String(localized: "Your first change opens a transaction, and nothing is saved until you commit. Some databases commit schema changes at once.")) if aiIsEnabled { // swiftlint:disable:next line_length Text(String(localized: "AI Policy controls in-app AI agents. External Clients controls Raycast, Cursor, Claude Desktop, other MCP clients, AppleScript, and Shortcuts. Effective scope is the minimum of the requesting token's scope and the External Clients level.")) diff --git a/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift b/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift index 21fcb4ee5f..5e5e16a5cb 100644 --- a/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift +++ b/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift @@ -35,6 +35,7 @@ struct ConnectionFormEdits: Equatable { var queryTimeoutSeconds: Int? var binaryInlineLimitKilobytes: Int? var completionIdentifierCase: SQLIdentifierCase = .default + var commitMode: ConnectionCommitMode = .automatic var redisDatabase: Int? var startupCommands: String? var localOnly: Bool @@ -51,6 +52,7 @@ struct ConnectionFormEdits: Equatable { DatabaseConnection.queryTimeoutSecondsKey, DatabaseConnection.binaryInlineLimitKilobytesKey, DatabaseConnection.completionIdentifierCaseKey, + DatabaseConnection.commitModeKey, DatabaseConnection.maskingRulesKey, DatabaseConnection.sshForwardUnixSocketPathKey, MySQLLocalSocket.fieldKey @@ -89,6 +91,7 @@ struct ConnectionFormEdits: Equatable { result.queryTimeoutSeconds = queryTimeoutSeconds result.binaryInlineLimitKilobytes = binaryInlineLimitKilobytes result.completionIdentifierCase = completionIdentifierCase + result.commitMode = commitMode result.maskingRules = maskingRules return result } diff --git a/apple/TablePro/Views/ConnectionForm/ViewModels/CustomizationPaneViewModel.swift b/apple/TablePro/Views/ConnectionForm/ViewModels/CustomizationPaneViewModel.swift index c255b0a0c3..548285cfac 100644 --- a/apple/TablePro/Views/ConnectionForm/ViewModels/CustomizationPaneViewModel.swift +++ b/apple/TablePro/Views/ConnectionForm/ViewModels/CustomizationPaneViewModel.swift @@ -19,6 +19,7 @@ final class CustomizationPaneViewModel: ObservableObject { @Published var queryTimeoutSeconds: Int? @Published var binaryInlineLimitKilobytes: Int? @Published var completionIdentifierCase: SQLIdentifierCase = .default + @Published var commitMode: ConnectionCommitMode = .automatic @Published var coordinator: WeakCoordinatorRef? @@ -52,5 +53,6 @@ final class CustomizationPaneViewModel: ObservableObject { queryTimeoutSeconds = connection.queryTimeoutSeconds binaryInlineLimitKilobytes = connection.binaryInlineLimitKilobytes completionIdentifierCase = connection.completionIdentifierCase + commitMode = connection.commitMode } } diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift b/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift index ec17de9e21..8884781785 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift @@ -259,11 +259,11 @@ extension DataGridViewController { } else { table.scroll(NSPoint(x: x, y: -headerInset)) } - guard !placement.selectedRows.isEmpty else { return } + guard placement.selection != .empty else { return } // Selecting publishes the model, which a SwiftUI update or layout pass refuses, so it runs on the next turn. DispatchQueue.main.async { [weak self] in guard let self, self.model.mountedView === self else { return } - self.model.selectRows(placement.selectedRows) + self.model.select(placement.selection) guard placement.revealsSelection, let row = self.model.selection.affectedRows.first else { return } self.reveal(row: row, displayColumn: nil) } diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController+Selection.swift b/apple/TablePro/Views/DataGrid/DataGridViewController+Selection.swift index f3fb692a25..b39acdb327 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController+Selection.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController+Selection.swift @@ -337,6 +337,8 @@ extension DataGridViewController { @discardableResult private func trackDrag(_ drag: GridSelectionDrag, from start: GridCoord) -> Bool { guard let window = view.window else { return false } + isTrackingSelectionDrag = true + defer { isTrackingSelectionDrag = false } var moved = false while let event = window.nextEvent(matching: [.leftMouseDragged, .leftMouseUp]) { if event.type == .leftMouseUp { break } diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController+TableDelegate.swift b/apple/TablePro/Views/DataGrid/DataGridViewController+TableDelegate.swift index 8ad4588ea3..1fd673b3e2 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController+TableDelegate.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController+TableDelegate.swift @@ -154,8 +154,9 @@ extension DataGridViewController: NSTableViewDataSource, NSTableViewDelegate { extension DataGridViewController: NSMenuDelegate { // Built from clickedRow and clickedColumn, so AppKit draws the clicked-row ring and the selection does not move. func menuNeedsUpdate(_ menu: NSMenu) { - menu.removeAllItems() + // The header menu comes built, and has this delegate only so its opening is seen. guard let pane = panes.first(where: { $0.tableView.menu === menu }) else { return } + menu.removeAllItems() let table = pane.tableView let row = table.clickedRow let built: NSMenu? @@ -178,6 +179,16 @@ extension DataGridViewController: NSMenuDelegate { func headerMenu(forTableColumn tableColumn: NSTableColumn, in kind: DataGridPane.Kind) -> NSMenu? { let pane = container.pane(kind) guard let id = pane.columnID(of: tableColumn), let column = model.columns.column(id: id) else { return nil } - return headerMenu(for: column) + let menu = headerMenu(for: column) + menu?.delegate = self + return menu + } + + func menuWillOpen(_ menu: NSMenu) { + isShowingContextMenu = true + } + + func menuDidClose(_ menu: NSMenu) { + isShowingContextMenu = false } } diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController.swift b/apple/TablePro/Views/DataGrid/DataGridViewController.swift index a3b65c5666..b05d5135b8 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController.swift @@ -39,6 +39,9 @@ final class DataGridViewController: NSViewController, DataGridMountedView { var isFittingHeightScheduled = false var pendingPageReveal: NSRect? var isRevealingInGrid = false + var isLiveScrolling = false + var isTrackingSelectionDrag = false + var isShowingContextMenu = false private var observers: [NSObjectProtocol] = [] private var cancellables: Set = [] private var themeCancellable: AnyCancellable? @@ -170,6 +173,16 @@ final class DataGridViewController: NSViewController, DataGridMountedView { var isEditing: Bool { isEditingCell } + // An open context menu acts on the row ids it was built from, which a reload hands to other rows. + var holdsTransientUI: Bool { + let state = editingState + return isShowingContextMenu || state.textPopover != nil || state.typedPopover != nil + || state.poppedOutEditor != nil || state.foreignKeyPreview?.isShown == true + || menuState.valueFilterPopover != nil + } + + var isTrackingInteraction: Bool { isLiveScrolling || isTrackingSelectionDrag } + @discardableResult func beginEditing(row: Int, displayColumn: Int) -> Bool { beginEditingCell(row: row, displayColumn: displayColumn) @@ -221,10 +234,16 @@ final class DataGridViewController: NSViewController, DataGridMountedView { }) let scrollView = main.scrollView observers.append(center.addObserver(forName: NSScrollView.willStartLiveScrollNotification, object: scrollView, queue: .main) { [weak self] _ in - MainActor.assumeIsolated { self?.pausePrewarm() } + MainActor.assumeIsolated { + self?.isLiveScrolling = true + self?.pausePrewarm() + } }) observers.append(center.addObserver(forName: NSScrollView.didEndLiveScrollNotification, object: scrollView, queue: .main) { [weak self] _ in - MainActor.assumeIsolated { self?.schedulePrewarmResume() } + MainActor.assumeIsolated { + self?.isLiveScrolling = false + self?.schedulePrewarmResume() + } }) } diff --git a/apple/TablePro/Views/ERDiagram/ERDiagramEditSheets.swift b/apple/TablePro/Views/ERDiagram/ERDiagramEditSheets.swift index 45a9fdc6c5..206e1e56b3 100644 --- a/apple/TablePro/Views/ERDiagram/ERDiagramEditSheets.swift +++ b/apple/TablePro/Views/ERDiagram/ERDiagramEditSheets.swift @@ -5,21 +5,12 @@ import SwiftUI -extension ERDiagramForeignKeyDraft { - // The referenced column follows the referenced table, so both change in one write. - func referencing(_ table: String, column: String) -> ERDiagramForeignKeyDraft { - var draft = self - draft.referencedTable = table - draft.referencedColumn = column - return draft - } -} - struct ERDiagramForeignKeySheet: View { @State private var draft: ERDiagramForeignKeyDraft + @State private var selection: Set = [] private let tables: [String] private let columns: (String) -> [String] - private let defaultReferencedColumn: (String) -> String + private let referencedKeyColumns: (String) -> [String] private let isNameTaken: (String) -> Bool private let dialect: ForeignKeyDialect private let onAdd: (ERDiagramForeignKeyDraft) -> Void @@ -29,7 +20,7 @@ struct ERDiagramForeignKeySheet: View { draft: ERDiagramForeignKeyDraft, tables: [String], columns: @escaping (String) -> [String], - defaultReferencedColumn: @escaping (String) -> String, + referencedKeyColumns: @escaping (String) -> [String], isNameTaken: @escaping (String) -> Bool, dialect: ForeignKeyDialect, onAdd: @escaping (ERDiagramForeignKeyDraft) -> Void, @@ -38,7 +29,7 @@ struct ERDiagramForeignKeySheet: View { _draft = State(initialValue: draft) self.tables = tables self.columns = columns - self.defaultReferencedColumn = defaultReferencedColumn + self.referencedKeyColumns = referencedKeyColumns self.isNameTaken = isNameTaken self.dialect = dialect self.onAdd = onAdd @@ -54,14 +45,18 @@ struct ERDiagramForeignKeySheet: View { } private var canAdd: Bool { - !draft.column.isEmpty && !draft.referencedTable.isEmpty && !draft.referencedColumn.isEmpty - && !trimmedName.isEmpty && !nameIsTaken + draft.issue == nil && !draft.referencedTable.isEmpty && !trimmedName.isEmpty && !nameIsTaken } private var referencedTable: Binding { Binding( get: { draft.referencedTable }, - set: { draft = draft.referencing($0, column: defaultReferencedColumn($0)) } + set: { table in + draft = draft.referencing( + table, keyColumns: referencedKeyColumns(table), sourceColumns: columns(draft.table) + ) + selection = [] + } ) } @@ -74,16 +69,20 @@ struct ERDiagramForeignKeySheet: View { Form { Section { LabeledContent(String(localized: "Table"), value: draft.table) - Picker(String(localized: "Column"), selection: $draft.column) { - ForEach(columns(draft.table), id: \.self) { Text($0).tag($0) } - } } Section(String(localized: "References")) { Picker(String(localized: "Table"), selection: referencedTable) { ForEach(tables, id: \.self) { Text($0).tag($0) } } - Picker(String(localized: "Column"), selection: $draft.referencedColumn) { - ForEach(columns(draft.referencedTable), id: \.self) { Text($0).tag($0) } + } + Section { + pairList + } header: { + Text(String(localized: "Column → Referenced Column")) + } footer: { + if let issue = draft.issue { + Text(issue.message) + .foregroundStyle(.secondary) } } Section { @@ -121,10 +120,137 @@ struct ERDiagramForeignKeySheet: View { } .padding() } - .frame(width: 440) + .frame(width: 480) .accessibilityElement(children: .contain) .accessibilityIdentifier("er-diagram-foreign-key-sheet") } + + // MARK: - Column pairs + + private var pairList: some View { + List(selection: $selection) { + ForEach(draft.pairs) { pair in + ERDiagramColumnPairRow( + column: columnBinding(for: pair.id), + referencedColumn: referencedColumnBinding(for: pair.id), + columns: columns(draft.table), + referencedColumns: columns(draft.referencedTable), + onMoveUp: { move(pair.id, by: -1) }, + onMoveDown: { move(pair.id, by: 1) } + ) + .tag(pair.id) + } + .onMove { source, destination in + draft = draft.movingPairs(fromOffsets: source, toOffset: destination) + } + } + .listStyle(.bordered(alternatesRowBackgrounds: false)) + .frame(height: listHeight) + .onDeleteCommand { removeSelection() } + .safeAreaInset(edge: .bottom, spacing: 0) { + VStack(spacing: 0) { + Divider() + HStack(spacing: 0) { + AddRemoveControlGroup( + addLabel: String(localized: "Add Column Pair"), + removeLabel: String(localized: "Remove Column Pair"), + canRemove: canRemoveSelection, + addIdentifier: "er-diagram-foreign-key-add-pair", + removeIdentifier: "er-diagram-foreign-key-remove-pair", + onAdd: addPair, + onRemove: removeSelection + ) + Spacer() + } + .padding(.horizontal, 4) + .padding(.vertical, 2) + } + .background(.bar) + } + } + + private let rowHeight: CGFloat = 26 + private let buttonBarHeight: CGFloat = 32 + + // No cap: a List in a grouped Form never scrolls, so it shows every pair and the Form scrolls. + private var listHeight: CGFloat { + CGFloat(max(draft.pairs.count, 2)) * rowHeight + buttonBarHeight + 8 + } + + private var canRemoveSelection: Bool { + !selection.isEmpty && selection.count < draft.pairs.count + } + + private func columnBinding(for id: UUID) -> Binding { + Binding( + get: { draft.pairs.first { $0.id == id }?.column ?? "" }, + set: { draft = draft.settingColumn($0, of: id) } + ) + } + + private func referencedColumnBinding(for id: UUID) -> Binding { + Binding( + get: { draft.pairs.first { $0.id == id }?.referencedColumn ?? "" }, + set: { draft = draft.settingReferencedColumn($0, of: id) } + ) + } + + private func addPair() { + draft = draft.addingPair() + if let added = draft.pairs.last?.id { + selection = [added] + } + } + + private func removeSelection() { + guard canRemoveSelection else { return } + draft = draft.removingPairs(selection) + selection = [] + } + + private func move(_ id: UUID, by offset: Int) { + guard let index = draft.pairs.firstIndex(where: { $0.id == id }) else { return } + let target = index + offset + guard draft.pairs.indices.contains(target) else { return } + draft = draft.movingPairs(fromOffsets: IndexSet(integer: index), toOffset: offset > 0 ? target + 1 : target) + } +} + +private struct ERDiagramColumnPairRow: View { + @Binding var column: String + @Binding var referencedColumn: String + let columns: [String] + let referencedColumns: [String] + let onMoveUp: () -> Void + let onMoveDown: () -> Void + + var body: some View { + HStack(spacing: 6) { + picker(String(localized: "Column"), selection: $column, options: columns) + Image(systemName: "arrow.right") + .foregroundStyle(.secondary) + .accessibilityHidden(true) + picker(String(localized: "Referenced Column"), selection: $referencedColumn, options: referencedColumns) + } + .accessibilityElement(children: .contain) + .accessibilityActions { + Button(String(localized: "Move Up"), action: onMoveUp) + Button(String(localized: "Move Down"), action: onMoveDown) + } + } + + // A blank side names nothing until it is chosen, so it gets an item of its own rather than an invalid selection. + private func picker(_ title: String, selection: Binding, options: [String]) -> some View { + Picker(title, selection: selection) { + if selection.wrappedValue.isEmpty { + Text(String(localized: "None")).tag("") + } + ForEach(options, id: \.self) { Text($0).tag($0) } + } + .labelsHidden() + .accessibilityLabel(title) + .frame(maxWidth: .infinity) + } } struct ERDiagramColumnSheet: View { diff --git a/apple/TablePro/Views/ERDiagram/ERDiagramView.swift b/apple/TablePro/Views/ERDiagram/ERDiagramView.swift index ef7abaf74e..3d9f47f281 100644 --- a/apple/TablePro/Views/ERDiagram/ERDiagramView.swift +++ b/apple/TablePro/Views/ERDiagram/ERDiagramView.swift @@ -71,7 +71,7 @@ struct ERDiagramView: View { draft: draft, tables: viewModel.referenceableTables, columns: { viewModel.columnNames(of: $0) }, - defaultReferencedColumn: { viewModel.defaultReferencedColumn(in: $0) }, + referencedKeyColumns: { viewModel.referencedKeyColumns(in: $0) }, isNameTaken: { viewModel.isForeignKeyNameTaken($0) }, dialect: ForeignKeyDialect.forType(viewModel.editingConnection?.type ?? .mysql), onAdd: { draft in diff --git a/apple/TablePro/Views/Editor/EditorEventRouter.swift b/apple/TablePro/Views/Editor/EditorEventRouter.swift index a6a2ed5293..187d6ffbff 100644 --- a/apple/TablePro/Views/Editor/EditorEventRouter.swift +++ b/apple/TablePro/Views/Editor/EditorEventRouter.swift @@ -2,9 +2,6 @@ // EditorEventRouter.swift // TablePro // -// Registry of the live SQL editors, used to dispatch window-scoped commands -// to the editor in the key window. -// @preconcurrency import AppKit import TableProEditorKit @@ -81,9 +78,7 @@ internal final class EditorEventRouter { // MARK: - Public API - /// Whether the key window has a SQL editor a find can fall back to. A focused editor answers the - /// Find commands itself through the responder chain, so this only decides whether the window's - /// last-resort handler has anything to search once nothing nearer has claimed them. + // A focused editor answers Find itself, so this decides only whether the window's fallback has anything to search. internal var keyWindowHasEditor: Bool { editor(for: NSApp.keyWindow) != nil } @@ -98,8 +93,6 @@ internal final class EditorEventRouter { coordinator.showFindAndReplacePanel() } - /// Whether the key window's editor has a selection to search for, so the menu can disable - /// Use Selection for Find rather than offering a command that would do nothing. internal var keyWindowEditorHasSelectionForFind: Bool { editor(for: NSApp.keyWindow)?.0.hasSelectionForFind ?? false } @@ -149,10 +142,7 @@ internal final class EditorEventRouter { coordinator.moveCursorToStatement(direction) } - /// Runs the statement the caret is in and advances, entirely inside the editor this resolves to. - /// - /// Handing the SQL back for someone else to execute would let the text and the connection come from two different - /// editors, which a window hosting more than one connection can genuinely do. + // Inside the one editor, so the text and the connection cannot come from two editors in a multi-connection window. internal func runStatementAtCursorAndAdvanceForKeyWindow() { guard let (coordinator, _) = editor(for: NSApp.keyWindow) else { return } coordinator.runStatementAtCursorAndAdvance() @@ -173,16 +163,18 @@ internal final class EditorEventRouter { } } - /// Called when the Edit menu's "Clear Selection" item is chosen with the pointer. - /// - /// Only with the pointer. AppKit never dispatches an unmodified Escape as a key equivalent: - /// measured on macOS 26, `NSMenu.performKeyEquivalent` is not called at all for key code 53, - /// which instead reaches `keyDown:` and `cancelOperation(_:)` through the responder chain, as - /// `StandardKeyBinding.dict` maps 0x1B to. The item carries no Escape key equivalent anyway, - /// because `KeyboardSettings.menuKeyEquivalent(for:)` refuses a bare key. Routes the command to - /// the key window's editor so it can dismiss its completion popup, hand the escape to Vim, and - /// restore first responder. Returns whether the editor consumed it so the caller skips its - /// cancelOperation fallback (the data grid's Clear Selection). + // False when the tab has no mounted editor, or it refused the edit: the caller writes the tab instead. + internal func replaceText(forTab tabId: UUID, connectionId: UUID, in range: NSRange, with string: String) -> Bool { + for ref in editors.values { + guard let coordinator = ref.coordinator, let controller = coordinator.controller, + coordinator.tabID == tabId, coordinator.connectionId == connectionId else { continue } + if controller.replaceTextAsOneEdit(in: range, with: string) { return true } + } + return false + } + + // Clear Selection chosen with the pointer: AppKit never sends a bare Escape as a key equivalent (measured, macOS 26). + // True when the editor consumed it, so the caller skips the grid's fallback. @discardableResult internal func handleEscapeFromMenu() -> Bool { guard let (coordinator, _) = editor(for: NSApp.keyWindow) else { return false } @@ -191,10 +183,8 @@ internal final class EditorEventRouter { // MARK: - Lookup - /// Window-scoped on purpose: every caller is a command the window's front content owns rather than - /// whatever holds first responder, and the escape and find routes exist precisely because focus is - /// somewhere else. A tab switch can leave the outgoing editor registered for a moment, and the - /// dictionary has no order, so the focused editor wins before the remaining candidate is used. + // Window-scoped: these routes exist because focus is elsewhere. A switch can leave the outgoing editor registered, + // so the focused one wins. private func editor(for window: NSWindow?) -> (SQLEditorCoordinator, TextView)? { guard let window else { return nil } var unfocused: (SQLEditorCoordinator, TextView)? diff --git a/apple/TablePro/Views/Main/Child/ConnectionTransactionActions.swift b/apple/TablePro/Views/Main/Child/ConnectionTransactionActions.swift new file mode 100644 index 0000000000..eb4836c9b3 --- /dev/null +++ b/apple/TablePro/Views/Main/Child/ConnectionTransactionActions.swift @@ -0,0 +1,90 @@ +// +// ConnectionTransactionActions.swift +// TablePro +// + +import AppKit + +// One flow for the bar's buttons and the Database menu's commands, so both ask and refuse the same way. +@MainActor +internal enum ConnectionTransactionActions { + // Never gated by Safe Mode: rolling back keeps nothing, so it is the way out at every level. + static func rollBack( + _ hold: SessionTransactionTracker.Hold, + databaseManager: DatabaseManager = .shared, + window: NSWindow? + ) async throws { + let confirmed = await AlertHelper.confirmDestructive( + title: String(localized: "Roll Back the Transaction?"), + message: String(localized: "Its uncommitted changes are discarded."), + confirmButton: String(localized: "Roll Back"), + window: window + ) + guard confirmed else { return } + try await databaseManager.endTransaction(hold, committing: false) + } + + // A COMMIT is a write, so it asks what a typed one asks: nothing at Silent, a confirmation at Alert, Touch ID at Safe Mode. + static func commit( + _ hold: SessionTransactionTracker.Hold, + databaseManager: DatabaseManager = .shared, + gate: any ExecutionGate = ExecutionGateProvider.shared + ) async throws { + guard let session = databaseManager.session(for: hold.connectionId) else { return } + let decision = await gate.authorize( + OperationRequest( + connectionId: hold.connectionId, + databaseType: session.connection.type, + sql: "COMMIT", + kind: .writeQuery, + caller: .userInterface, + capabilities: .interactiveUser, + operationDescription: String(localized: "Commit the transaction") + ) + ) + switch decision { + case .authorized: + try await databaseManager.endTransaction(hold, committing: true) + case .denied(_, .cancelledByUser): + return + case .denied(let reason, .policy): + throw SessionTransactionDenial(reason: reason) + } + } + + static func offersCommit(_ hold: SessionTransactionTracker.Hold, safeModeLevel: SafeModeLevel) -> Bool { + hold.state == .open && !safeModeLevel.blocksAllWrites + } + + static func perform(window: NSWindow?, _ action: @MainActor () async throws -> Void) async { + do { + try await action() + } catch is CancellationError { + return + } catch { + AlertHelper.showErrorSheet( + title: String(localized: "Could Not End the Transaction"), + message: error.localizedDescription, + window: window + ) + } + } + + // MySQL and Oracle commit before DDL runs, so the transaction the bar shows ends with no Commit pressed. + static func confirmImplicitCommit(on name: String, window: NSWindow?) async -> Bool { + await AlertHelper.confirm( + title: String(localized: "Commit the Open Transaction?"), + message: String( + format: String(localized: "This statement commits the transaction open on “%@” before it runs."), name + ), + confirmButton: String(localized: "Commit and Run"), + window: window + ) + } +} + +private struct SessionTransactionDenial: LocalizedError { + let reason: String + + var errorDescription: String? { reason } +} diff --git a/apple/TablePro/Views/Main/Child/ConnectionTransactionBar.swift b/apple/TablePro/Views/Main/Child/ConnectionTransactionBar.swift index 6106744d43..53a5c82487 100644 --- a/apple/TablePro/Views/Main/Child/ConnectionTransactionBar.swift +++ b/apple/TablePro/Views/Main/Child/ConnectionTransactionBar.swift @@ -51,6 +51,8 @@ internal struct ConnectionTransactionBar: View { let connectionId: UUID @ObservedObject var transactions: SessionTransactionTracker let databaseManager: DatabaseManager + // Passed from the toolbar state the window observes: the session's own level publishes nothing. + let safeModeLevel: SafeModeLevel @State private var actingOn: SessionTransactionTracker.Key? @@ -73,7 +75,7 @@ internal struct ConnectionTransactionBar: View { isAborted: hold.state == .aborted, connectionName: session.connection.name, database: hold.key.lane ?? namedDatabase(of: session), - safeModeLevel: session.safeModeLevel + safeModeLevel: safeModeLevel ) } @@ -109,11 +111,15 @@ internal struct ConnectionTransactionBar: View { Spacer(minLength: 8) Button(String(localized: "Roll Back")) { - act(on: hold) { try await rollBack(hold) } + act(on: hold) { + try await ConnectionTransactionActions.rollBack( + hold, databaseManager: databaseManager, window: NSApp.keyWindow + ) + } } if content.offersCommit { Button(String(localized: "Commit")) { - act(on: hold) { try await commit(hold) } + act(on: hold) { try await ConnectionTransactionActions.commit(hold, databaseManager: databaseManager) } } } } @@ -124,64 +130,11 @@ internal struct ConnectionTransactionBar: View { .paneBarBackground() } - // Never gated by Safe Mode: rolling back keeps nothing, so it is the way out at every level. - private func rollBack(_ hold: SessionTransactionTracker.Hold) async throws { - let confirmed = await AlertHelper.confirmDestructive( - title: String(localized: "Roll Back the Transaction?"), - message: String(localized: "Its uncommitted changes are discarded."), - confirmButton: String(localized: "Roll Back"), - window: NSApp.keyWindow - ) - guard confirmed else { return } - try await databaseManager.endTransaction(hold, committing: false) - } - - // A COMMIT is a write, so it asks what a typed one asks: nothing at Silent, a confirmation at - // Alert, Touch ID at Safe Mode. - private func commit(_ hold: SessionTransactionTracker.Hold) async throws { - guard let session = databaseManager.session(for: connectionId) else { return } - let decision = await ExecutionGateProvider.shared.authorize( - OperationRequest( - connectionId: connectionId, - databaseType: session.connection.type, - sql: "COMMIT", - kind: .writeQuery, - caller: .userInterface, - capabilities: .interactiveUser, - operationDescription: String(localized: "Commit the transaction") - ) - ) - switch decision { - case .authorized: - try await databaseManager.endTransaction(hold, committing: true) - case .denied(_, .cancelledByUser): - return - case .denied(let reason, .policy): - throw SessionTransactionDenial(reason: reason) - } - } - private func act(on hold: SessionTransactionTracker.Hold, _ action: @escaping @MainActor () async throws -> Void) { actingOn = hold.key Task { defer { actingOn = nil } - do { - try await action() - } catch is CancellationError { - return - } catch { - AlertHelper.showErrorSheet( - title: String(localized: "Could Not End the Transaction"), - message: error.localizedDescription, - window: NSApp.keyWindow - ) - } + await ConnectionTransactionActions.perform(window: NSApp.keyWindow, action) } } } - -private struct SessionTransactionDenial: LocalizedError { - let reason: String - - var errorDescription: String? { reason } -} diff --git a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift index a246fc2a70..bed971af0e 100644 --- a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift +++ b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift @@ -269,7 +269,7 @@ struct MainEditorContentView: View { switch subject { case .linkedFile(let url): return url.lastPathComponent - case .savedQuery: + case .savedQuery, .queryTab: return "query.sql" } } @@ -511,7 +511,7 @@ struct MainEditorContentView: View { // By ID: mid-switch, flushTextUpdate() fires for the old tab while selectedTabIndex already points to the new one. guard tabManager.mutate(tabId: tabId, { $0.content.query = newValue }) else { return } - coordinator.scheduleDraftSave() + coordinator.scheduleDraftSave(editedTab: tabId) // Scratch tabs dirty too. Only this tab's window: a background window tab's editor stays mounted and can fire here. guard tabId == tabManager.selectedTabId, @@ -986,7 +986,14 @@ struct MainEditorContentView: View { resultSetMenu: resultSetMenuModel(for: tab, placedIn: .statusBar), resultSetActions: resultSetActions(for: tab), onToggleFilters: { coordinator.toggleFilterPanel() }, - onFetchAll: { coordinator.fetchAllRows() } + onFetchAll: { coordinator.fetchAllRows() }, + autoRefresh: StatusBarAutoRefreshState( + interval: tab.autoRefreshInterval, + pause: tab.autoRefreshPause, + onChange: { [coordinator, tabId = tab.id] interval in + coordinator.setAutoRefreshInterval(interval, forTab: tabId) + } + ) ) } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AIQueryActions.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AIQueryActions.swift index 750e98c33f..8217b45be3 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AIQueryActions.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AIQueryActions.swift @@ -146,13 +146,8 @@ extension MainContentCoordinator { } switch WalkthroughApplyPlan.resolve(beforeSQL: beforeSQL, source: source, tabText: tabText) { case .replace(let tabId, let range): - guard let owner else { return } - owner.tabManager.mutate(tabId: tabId) { tab in - let text = tab.content.query as NSString - guard NSMaxRange(range) <= text.length else { return } - tab.content.query = text.replacingCharacters(in: range, with: afterSQL) - tab.hasUserInteraction = true - } + guard let owner, owner.replaceQueryText(inTab: tabId, range: range, with: afterSQL) else { return } + owner.tabManager.mutate(tabId: tabId) { $0.hasUserInteraction = true } case .insertAsNewQuery: insertQueryFromAI(afterSQL) } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AutoRefresh.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AutoRefresh.swift new file mode 100644 index 0000000000..474224b02a --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AutoRefresh.swift @@ -0,0 +1,170 @@ +// +// MainContentCoordinator+AutoRefresh.swift +// TablePro +// + +import Combine +import Foundation + +// One per window: only the selected table tab ticks, so a switch, a close or a move ends one wait and starts the next. +@MainActor +final class TableAutoRefreshSchedule { + var clock: any Clock = ContinuousClock() + var visibility: @MainActor (OperationOwner, UUID) -> ResultVisibility = { owner, connectionId in + ResultVisibilityResolver.resolve(owner: owner, connectionId: connectionId) + } + fileprivate(set) var task: Task? + fileprivate var selectionCancellable: AnyCancellable? + + var isRunning: Bool { task != nil } +} + +extension MainContentCoordinator { + func observeSelectionForAutoRefresh() { + autoRefresh.selectionCancellable = tabManager.$selectedTabId + .removeDuplicates() + .dropFirst() + .receive(on: RunLoop.main) + .sink { [weak self] _ in self?.restartAutoRefresh() } + } + + func setAutoRefreshInterval(_ interval: TableAutoRefreshInterval, forTab tabId: UUID) { + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), tab.tabType == .table else { return } + if tab.autoRefreshInterval != interval || tab.autoRefreshPause != nil { + tabManager.mutate(tabId: tabId) { tab in + tab.autoRefreshInterval = interval + tab.autoRefreshPause = nil + } + } + guard tabId == tabManager.selectedTabId else { return } + restartAutoRefresh() + } + + func restartAutoRefresh() { + autoRefresh.task?.cancel() + autoRefresh.task = nil + guard !isTearingDown, + let tab = tabManager.selectedTab, + tab.tabType == .table, + tab.autoRefreshInterval.isOn else { return } + let tabId = tab.id + let clock = autoRefresh.clock + // Weak between ticks, so a wait never keeps a closed window's coordinator alive. + autoRefresh.task = Task { [weak self] in + while !Task.isCancelled { + await self?.awaitAutoRefreshLoad(tabId) + guard let interval = self?.autoRefreshInterval(ofTab: tabId), interval.isOn else { return } + do { + try await clock.sleep(for: interval.duration) + } catch { + return + } + guard let self, !Task.isCancelled else { return } + guard await runAutoRefreshTick(tabId) else { return } + } + } + } + + func stopAutoRefresh() { + autoRefresh.task?.cancel() + autoRefresh.task = nil + autoRefresh.selectionCancellable = nil + } + + func autoRefreshGate(forTab tabId: UUID) -> TableAutoRefreshGate { + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }) else { + return TableAutoRefreshGate(isTableTab: false) + } + let visibility = autoRefresh.visibility(operationOwner(tabId: tabId), connectionId) + let model = existingGridModel(forTab: tabId) + let database = scope(for: tab)?.database ?? browseDatabaseName + var gate = TableAutoRefreshGate() + gate.isTableTab = tab.tabType == .table && tab.tableContext.tableName != nil + gate.hasLoaded = tab.execution.lastExecutedAt != nil + gate.windowIsVisible = visibility.ownerWindowIsVisible + gate.isSelectedInWindow = visibility.ownerIsSelectedInWindow && tabManager.selectedTabId == tabId + gate.isConnected = services.databaseManager.session(for: connectionId)?.reportedStatus == .connected + gate.viewMode = tab.display.resultsViewMode + // Read only: a read inside the user's transaction sees its snapshot, and a failure aborts it on PostgreSQL. + gate.holdsTransaction = services.databaseManager.sessionTransactions + .transactionState(connectionId: connectionId, container: database) != nil + gate.holdsUnsavedWork = holdsUnsavedWork(tab) + gate.hasSidebarDataWork = commandActions?.hasDataWork ?? false + gate.isEditingCell = model?.isEditing ?? false + gate.gridHoldsTransientUI = model?.holdsTransientUI ?? false + gate.gridIsTrackingInteraction = model?.isTrackingInteraction ?? false + gate.inspectorHoldsValueWindow = trailingPaneState.map { InspectorPopOutRegistry.holdsWindow(openedFrom: $0) } ?? false + gate.load = tabExecution.isBusy(tabId) || tableLoadTasks[tabId] != nil ? .busy : .idle + gate.isCountPending = tab.pagination.isCountPending + gate.isLoadingMore = tab.pagination.isLoadingMore + gate.showsSafeModePrompt = isShowingSafeModePrompt + gate.hasSelection = model.map { !$0.selection.affectedRows.isEmpty } ?? false + gate.hasPrimaryKey = !tab.tableContext.primaryKeyColumns.isEmpty + return gate + } + + func autoRefreshHeldByTransientUI(_ tabId: UUID) -> Bool { + let gate = autoRefreshGate(forTab: tabId) + return gate.gridHoldsTransientUI || gate.inspectorHoldsValueWindow || gate.isEditingCell + } + + private func autoRefreshInterval(ofTab tabId: UUID) -> TableAutoRefreshInterval? { + guard tabManager.selectedTabId == tabId else { return nil } + return tabManager.tabs.first { $0.id == tabId }?.autoRefreshInterval + } + + // Once, not in a loop: a finished task its owner has not retired yet would spin the main actor. + private func awaitAutoRefreshLoad(_ tabId: UUID) async { + if let scheduled = tableLoadTasks[tabId]?.task { + await scheduled.value + } + if let running = queryTasks.task(for: tabId) { + await running.value + } + } + + // False ends the schedule: the tab is gone or no longer selected, or the gate stopped it. + private func runAutoRefreshTick(_ tabId: UUID) async -> Bool { + // A retarget keeps the tab id and turns the interval off while this wait slept. + guard autoRefreshInterval(ofTab: tabId)?.isOn == true else { return false } + switch autoRefreshGate(forTab: tabId).decision { + case .stop: + endAutoRefresh(forTab: tabId) + return false + case .wait(let pause): + recordAutoRefreshPause(pause.help == nil ? nil : pause, forTab: tabId) + return true + case .refresh: + recordAutoRefreshPause(nil, forTab: tabId) + } + // Not runQuery, which commits an open edit, nor reloadTableTab, which retires the count. + if let load = executeTableTabQueryDirectly(trigger: .autoRefresh, viewport: .keepPlace) + ?? queryTasks.task(for: tabId) { + await load.value + } + guard !Task.isCancelled else { return false } + var settled = autoRefreshGate(forTab: tabId) + settled.previousTickFailed = tabManager.tabs.first { $0.id == tabId }?.execution.errorMessage != nil + guard settled.decision != .stop else { + endAutoRefresh(forTab: tabId) + return false + } + return true + } + + // The tab keeps its own inline error; only the interval goes. + private func endAutoRefresh(forTab tabId: UUID) { + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), + tab.autoRefreshInterval.isOn || tab.autoRefreshPause != nil else { return } + tabManager.mutate(tabId: tabId) { tab in + tab.autoRefreshInterval = .off + tab.autoRefreshPause = nil + } + } + + // Written only on change: every tab write re-renders the window. + private func recordAutoRefreshPause(_ pause: TableAutoRefreshPause?, forTab tabId: UUID) { + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), tab.autoRefreshPause != pause else { return } + tabManager.mutate(tabId: tabId) { $0.autoRefreshPause = pause } + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DraftSave.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DraftSave.swift new file mode 100644 index 0000000000..74c62ffb1b --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DraftSave.swift @@ -0,0 +1,57 @@ +// +// MainContentCoordinator+DraftSave.swift +// TablePro +// + +import Foundation + +extension MainContentCoordinator { + static let periodicSaveInterval: Duration = .seconds(30) + static let draftSaveDebounce: Duration = .seconds(1) + + // Typing changes no tab identity, so without this a scratch query lives only in memory until the 30s timer or quit flush. + // Any window may save: `saveAggregated` writes every window's tabs for the connection. + func scheduleDraftSave(editedTab tabId: UUID? = nil) { + guard !Self.isAppTerminating, !isTearingDown else { return } + if let tabId { draftSaveTabIds.insert(tabId) } + draftSaveTask?.cancel() + let clock = draftSaveClock + draftSaveTask = Task { [weak self] in + try? await clock.sleep(for: Self.draftSaveDebounce) + guard let self, !Task.isCancelled, !Self.isAppTerminating, !self.isTearingDown else { return } + self.recordIdleVersions() + self.persistence.saveAggregated() + } + } + + func startPeriodicSave() { + guard periodicSaveTask == nil else { return } + periodicSaveTask = Task { [weak self] in + while !Task.isCancelled { + try? await Task.sleep(for: Self.periodicSaveInterval) + guard let self, !Task.isCancelled, !Self.isAppTerminating, !self.isTearingDown else { return } + guard self.isFirstCoordinatorForConnection() else { continue } + self.persistence.saveAggregated() + } + } + } + + static func aggregatedSelectedTabId(for connectionId: UUID) -> UUID? { + activeCoordinators.values + .first { $0.connectionId == connectionId && $0.tabManager.selectedTabId != nil }? + .tabManager.selectedTabId + } + + private func isFirstCoordinatorForConnection() -> Bool { + Self.activeCoordinators.values + .first { $0.connectionId == self.connectionId } === self + } + + private func recordIdleVersions() { + let tabIds = draftSaveTabIds + draftSaveTabIds.removeAll() + for tabId in tabIds { + tabVersions.record(tabManager.tabs.first { $0.id == tabId }, connectionId: connectionId, reason: .idle) + } + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+EditorQueryLoading.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+EditorQueryLoading.swift new file mode 100644 index 0000000000..9a1b94e87e --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+EditorQueryLoading.swift @@ -0,0 +1,99 @@ +// +// MainContentCoordinator+EditorQueryLoading.swift +// TablePro +// + +import Foundation + +extension MainContentCoordinator { + @discardableResult + func loadQueryIntoEditor( + _ query: String, + databaseName: String? = nil, + forceNewTab: Bool = false + ) -> WindowTabOpenDisposition { + let targetDatabaseName = databaseName ?? newTabDatabaseName + if !forceNewTab, + let (tab, tabIndex) = tabManager.selectedTabAndIndex, + tab.tabType == .query, + databaseName == nil + || tab.tableContext.resolvedDatabaseName(browsing: browseDatabaseName) == targetDatabaseName { + replaceQueryText(inTab: tab.id, with: query) + tabManager.mutate(at: tabIndex) { $0.hasUserInteraction = true } + return .currentCoordinator + } + + if !forceNewTab, tabManager.tabs.isEmpty { + tabManager.addTab(initialQuery: query, databaseName: targetDatabaseName) + return .currentCoordinator + } + + let payload = EditorTabPayload( + connectionId: connection.id, + tabType: .query, + databaseName: targetDatabaseName, + initialQuery: query + ) + openTabInNewWindow(payload) + return .focusedElsewhere + } + + var aiInsertReusesSelectedQueryTab: Bool { + guard let (tab, _) = tabManager.selectedTabAndIndex, tab.tabType == .query else { return false } + return tab.content.query.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + } + + func insertQueryFromAI(_ query: String) { + if aiInsertReusesSelectedQueryTab, let (tab, tabIndex) = tabManager.selectedTabAndIndex { + replaceQueryText(inTab: tab.id, with: query) + tabManager.mutate(at: tabIndex) { $0.hasUserInteraction = true } + } else if tabManager.tabs.isEmpty { + tabManager.addTab(initialQuery: query, databaseName: newTabDatabaseName) + } else { + let payload = EditorTabPayload( + connectionId: connection.id, + tabType: .query, + initialQuery: query + ) + WindowManager.shared.openTab(payload: payload) + } + } + + func clearSelectedQuery() { + guard let tab = tabManager.selectedTab, tab.tabType == .query else { return } + replaceQueryText(inTab: tab.id, with: "") + } + + // Through the mounted editor the change is one undoable edit; with no editor mounted the model takes the text, + // and the version recorded first is the way back. Nil `reason` is for a caller that recorded already. + @discardableResult + func replaceQueryText( + inTab tabId: UUID, + range: NSRange? = nil, + with text: String, + recording reason: QueryTabVersionReason? = .beforeReplace + ) -> Bool { + flushEditorText(forTab: tabId) + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), tab.tabType == .query else { return false } + let current = tab.content.query as NSString + let target = range ?? NSRange(location: 0, length: current.length) + guard NSMaxRange(target) <= current.length else { return false } + if let reason { + tabVersions.record(tab, connectionId: connectionId, reason: reason) + } + let replacedInEditor = EditorEventRouter.shared.replaceText( + forTab: tabId, + connectionId: connectionId, + in: target, + with: text + ) + if !replacedInEditor { + let replaced = current.replacingCharacters(in: target, with: text) + tabManager.mutate(tabId: tabId) { $0.content.query = replaced } + } + scheduleDraftSave(editedTab: tabId) + syncQueryToolbarStateForSelectedTab() + refreshUnsavedIndicator() + return true + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift index 067f3496e6..b79ca304b9 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift @@ -119,6 +119,7 @@ extension MainContentCoordinator { tabManager.mutate(tabId: tabId) { $0.execution.errorMessage = nil } // EXPLAIN ANALYZE and an actual plan run the statement, and a CREATE TABLE AS among them changes the catalog. + // Under Manual Commit such a run is held, as the write itself would be. let ranWrite = request.operationKind(databaseType: conn.type).declaresWrite let failureOutput = ServerOutputBox() let explainTask = Task { [weak self] in @@ -128,16 +129,18 @@ extension MainContentCoordinator { scope: scope, route: services.databaseManager.executionRoute(for: scope), cancellation: .cancellableRead(lease) - ) { [queryExecutor] driver in - try await ServerOutputCapture.running(on: driver, failureOutput: failureOutput) { () async throws -> QueryFetchResult in - if let step = request.driverStep { - return try await QueryExecutor.fetchExplainPlan( - driver: driver, statement: request.subjectSQL, variantId: step.variantId + ) { [queryExecutor, held = manualCommitRun(skipped: !ranWrite)] driver in + try await held.running([request.gatedSQL(databaseType: conn.type)], on: driver) { + try await ServerOutputCapture.running(on: driver, failureOutput: failureOutput) { () async throws -> QueryFetchResult in + if let step = request.driverStep { + return try await QueryExecutor.fetchExplainPlan( + driver: driver, statement: request.subjectSQL, variantId: step.variantId + ) + } + return try await queryExecutor.executeQuery( + driver: driver, sql: request.sql, parameters: nil, rowCap: nil ) } - return try await queryExecutor.executeQuery( - driver: driver, sql: request.sql, parameters: nil, rowCap: nil - ) } } if ranWrite { Self.postStatementRan(request.subjectSQL, on: conn) } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Favorites.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Favorites.swift index bec094a2e0..f2876bd7a1 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Favorites.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Favorites.swift @@ -18,13 +18,15 @@ extension MainContentCoordinator { return } - if let (tab, tabIndex) = tabManager.selectedTabAndIndex, - tab.tabType == .query { - let existing = tab.content.query.trimmingCharacters(in: .whitespacesAndNewlines) - if existing.isEmpty { - tabManager.mutate(at: tabIndex) { $0.content.query = favorite.query } + if let selected = tabManager.selectedTab, selected.tabType == .query { + // Flushed first: on a large document the tab's text trails the editor, and the end must be the editor's. + flushEditorText(forTab: selected.id) + let existing = tabManager.selectedTab?.content.query ?? "" + if existing.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { + replaceQueryText(inTab: selected.id, with: favorite.query) } else { - tabManager.mutate(at: tabIndex) { $0.content.query += "\n\n" + favorite.query } + let end = NSRange(location: (existing as NSString).length, length: 0) + replaceQueryText(inTab: selected.id, range: end, with: "\n\n" + favorite.query) } } else { runFavoriteInNewTab(favorite) @@ -43,8 +45,7 @@ extension MainContentCoordinator { func openLinkedFavorite(_ favorite: LinkedSQLFavorite) { guard let loaded = FileTextLoader.load(favorite.fileURL) else { return } - /// Any connection's tab, not only this window's selected one: the file can be open in a - /// workspace the window is not showing, and opening it here too made two buffers on one file. + // Any connection's tab: the file can be open in a workspace this window is not showing. if hostedTabRouting.revealTab(editing: favorite.fileURL) { AppActivationPolicyController.shared.activate(ignoringOtherApps: true) return diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridViewport.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridViewport.swift index fffc3eb546..1cbad5583c 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridViewport.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridViewport.swift @@ -41,12 +41,16 @@ extension MainContentCoordinator { let sample = intent == .keepPlace ? viewportSample(of: model) : nil guard sample != nil || keepsSelection else { return .top } + // Mode, columns and active cell too, or a cell cursor comes back as whole rows on column 0. + let selection = model.map { + GridSelectionSnapshot($0.selection, order: $0.order, rows: tableRows, columns: $0.columns) + } let snapshot = GridViewportResolver.snapshot( of: tableRows, displayIDs: model?.displayedRowIDs, firstVisibleDisplayRow: sample?.row ?? 0, firstVisibleOffset: sample?.offset ?? 0, - selectedDisplayRows: keepsSelection ? Array(model?.selection.affectedRows ?? IndexSet()) : [], + selection: keepsSelection ? selection ?? .empty : .empty, keyColumns: keyColumns, isCellModified: { [changeManager] rowID, column in changeManager.isCellModified(rowID: rowID, columnIndex: column) diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ManualCommit.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ManualCommit.swift new file mode 100644 index 0000000000..94593f8a72 --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ManualCommit.swift @@ -0,0 +1,18 @@ +// +// MainContentCoordinator+ManualCommit.swift +// TablePro +// + +import Foundation + +extension MainContentCoordinator { + // A table tab's read and an auto-load are the app's own SELECTs, so they never hold anything. + func manualCommitRun(skipped: Bool) -> ManualCommitRun { + ManualCommitRun( + commitMode: skipped ? .automatic : services.databaseManager.commitMode(for: connectionId), + databaseType: connection.type, + grammar: lexicalGrammar, + connectionId: connectionId + ) + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Phase2Launch.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Phase2Launch.swift new file mode 100644 index 0000000000..d3e7b515a4 --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Phase2Launch.swift @@ -0,0 +1,41 @@ +// +// MainContentCoordinator+Phase2Launch.swift +// TablePro +// + +import Foundation + +extension MainContentCoordinator { + func launchPhase2AfterApply( + tableName: String?, + isEditable: Bool, + tabId: UUID, + trigger: TableLoadTrigger, + needsMetadataFetch: Bool, + schemaTask: Task?, + claim: TabExecutionClaim + ) { + if isEditable, let tableName, trigger.countsRows { + // Marked pending here: phase 1 clears `isLoading` first, so without it the tab looks settled and offers Count Exactly early. + tabManager.mutate(tabId: tabId) { $0.pagination.isCountPending = true } + launchPhase2( + tableName: tableName, + tabId: tabId, + connectionType: connection.type, + needsMetadataFetch: needsMetadataFetch, + schemaTask: schemaTask + ) + } else if isEditable, let tableName, needsMetadataFetch { + // A tick keeps the count it has; only a manual Refresh recounts. + launchPhase2Work( + tableName: tableName, + tabId: tabId, + connectionType: connection.type, + schemaTask: schemaTask, + countsRows: false + ) + } else if !isEditable || tableName == nil { + clearChangesIfCurrent(claim: claim) + } + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QueryHelpers.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QueryHelpers.swift index 48861aa5be..940fb82661 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QueryHelpers.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QueryHelpers.swift @@ -79,7 +79,7 @@ extension MainContentCoordinator { parkedTableLoad = ParkedTableLoad(tabId: tabId, trigger: trigger) return } - handleQueryExecutionError(error, sql: sql, tabId: tabId, connection: conn, serverOutput: serverOutput) + handleQueryExecutionError(error, sql: sql, tabId: tabId, connection: conn, serverOutput: serverOutput, trigger: trigger) reportQueryOperation( claim: claim, trigger: trigger, outcome: .failed(reason: error.localizedDescription) ) @@ -137,7 +137,8 @@ extension MainContentCoordinator { absentCells: [Int: Set] = [:], unloadedCells: [Int: [Int: Int]] = [:], columnOrigins: [PluginColumnOrigin?]? = nil, - prepared: TableRows.PreparedRows? = nil + prepared: TableRows.PreparedRows? = nil, + trigger: TableLoadTrigger = .userInitiated ) { queryExecutionCoordinator.applyPhase1Result( tabId: tabId, @@ -166,7 +167,8 @@ extension MainContentCoordinator { absentCells: absentCells, unloadedCells: unloadedCells, columnOrigins: columnOrigins, - prepared: prepared + prepared: prepared, + trigger: trigger ) } @@ -231,13 +233,15 @@ extension MainContentCoordinator { tableName: String, tabId: UUID, connectionType: DatabaseType, - schemaTask: Task? + schemaTask: Task?, + countsRows: Bool = true ) { queryExecutionCoordinator.launchPhase2Work( tableName: tableName, tabId: tabId, connectionType: connectionType, - schemaTask: schemaTask + schemaTask: schemaTask, + countsRows: countsRows ) } @@ -258,14 +262,16 @@ extension MainContentCoordinator { sql: String, tabId: UUID, connection conn: DatabaseConnection, - serverOutput: PluginServerOutput = .none + serverOutput: PluginServerOutput = .none, + trigger: TableLoadTrigger = .userInitiated ) { queryExecutionCoordinator.handleQueryExecutionError( error, sql: sql, tabId: tabId, connection: conn, - serverOutput: serverOutput + serverOutput: serverOutput, + trigger: trigger ) } } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift index e5dcc43095..33edc43c0c 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift @@ -2,8 +2,6 @@ // MainContentCoordinator+Refresh.swift // TablePro // -// Refresh handling operations for MainContentCoordinator -// import AppKit import Combine @@ -102,5 +100,7 @@ extension MainContentCoordinator { tabManager.mutate(at: tabIndex) { $0.pagination.retireDerivedRowCount() } rebuildTableQuery(at: tabIndex) runQuery(viewport: .keepPlace) + // The next tick counts from this load settling, not from the last one. + restartAutoRefresh() } } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+VersionHistory.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+VersionHistory.swift index 0d8313d732..7d30e1a0f2 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+VersionHistory.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+VersionHistory.swift @@ -17,6 +17,58 @@ extension MainContentCoordinator { openVersionHistory(subject: .linkedFile(url: linked.fileURL), name: linked.fileURL.lastPathComponent) } + func showVersionHistory(ofQueryTab tabId: UUID) { + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), tab.tabType == .query else { return } + openVersionHistory(subject: .queryTab(id: tabId), name: tab.title) + } + + func revertQueryTab(_ tabId: UUID, toVersion versionId: Int64) { + let provider = queryTabVersionProvider(for: tabId) + Task { + do { + try await provider.prepareRestore(.tabVersion(id: versionId)).apply() + } catch { + Self.versionHistoryLogger.error("Reverting a query tab failed: \(error.publicLogShape, privacy: .public)") + presentError(String(localized: "Couldn't Restore Version"), error.localizedDescription, contentWindow) + } + } + } + + func queryTabVersionProvider(for tabId: UUID) -> QueryTabVersionHistoryProvider { + let recorder = tabVersions + return QueryTabVersionHistoryProvider( + tabId: tabId, + storage: recorder.storage, + liveTab: { [weak self] in self?.liveQueryTab(tabId) }, + write: { [weak self] text in self?.replaceQueryText(inTab: tabId, with: text, recording: nil) ?? false }, + isCapturePaused: recorder.isCapturePaused + ) + } + + // The History tab can outlive a move of the tab it shows, so each call finds the tab's current owner. + static func hostedQueryTabVersionProvider(for tabId: UUID) -> QueryTabVersionHistoryProvider { + QueryTabVersionHistoryProvider( + tabId: tabId, + storage: .shared, + liveTab: { Self.owner(ofQueryTab: tabId)?.liveQueryTab(tabId) }, + write: { text in + Self.owner(ofQueryTab: tabId)?.replaceQueryText(inTab: tabId, with: text, recording: nil) ?? false + } + ) + } + + func liveQueryTab(_ tabId: UUID) -> LiveQueryTab? { + flushEditorText(forTab: tabId) + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), tab.tabType == .query else { return nil } + return LiveQueryTab(connectionId: connectionId, text: tab.content.query) + } + + private static func owner(ofQueryTab tabId: UUID) -> MainContentCoordinator? { + HostedTabRouting.live.allCoordinators().first { coordinator in + coordinator.tabManager.tabs.contains { $0.id == tabId && $0.tabType == .query } + } + } + func openVersionInEditor(_ content: String) { let payload = EditorTabPayload( connectionId: connectionId, diff --git a/apple/TablePro/Views/Main/MainContentCommandActions.swift b/apple/TablePro/Views/Main/MainContentCommandActions.swift index 0decc6483f..be1c6cca5d 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions.swift @@ -1131,14 +1131,7 @@ final class MainContentCommandActions: ObservableObject { } func clearQuery() { - guard let coordinator, - let (tab, tabIndex) = coordinator.tabManager.selectedTabAndIndex, - tab.tabType == .query else { return } - coordinator.tabManager.mutate(at: tabIndex) { $0.content.query = "" } - coordinator.toolbarState.hasQueryText = false - coordinator.scheduleDraftSave() - // Emptying from a command bypasses the editor binding that recomputes the dirty indicator on each keystroke. - coordinator.refreshUnsavedIndicator() + coordinator?.clearSelectedQuery() } var canClearQuery: Bool { @@ -1289,6 +1282,19 @@ final class MainContentCommandActions: ObservableObject { ) } + // Nil off a table tab, which the Database menu's Auto Refresh items dim for. + var tableAutoRefreshInterval: TableAutoRefreshInterval? { + guard let tab = coordinator?.tabManager.selectedTab, + tab.tabType == .table, + tab.tableContext.tableName != nil else { return nil } + return tab.autoRefreshInterval + } + + func setTableAutoRefreshInterval(_ interval: TableAutoRefreshInterval) { + guard let coordinator, let tabId = coordinator.tabManager.selectedTabId else { return } + coordinator.setAutoRefreshInterval(interval, forTab: tabId) + } + private var hasPendingTableOps: Bool { !pendingTruncates.wrappedValue.isEmpty || !pendingDeletes.wrappedValue.isEmpty } diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index 2c2c39815a..16ac2865d0 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -330,8 +330,11 @@ final class MainContentCoordinator: ObservableObject { // Not on `QueryTab`: that is the persisted shape, and staying out of its `Equatable` keeps a push from re-publishing tabs. internal var navigationHistories: [UUID: TabNavigationHistory] = [:] internal var redisDatabaseSwitchTask: Task? - private var periodicSaveTask: Task? - private var draftSaveTask: Task? + var periodicSaveTask: Task? + var draftSaveTask: Task? + var draftSaveTabIds: Set = [] + var draftSaveClock: any Clock = ContinuousClock() + var tabVersions = QueryTabVersionRecorder.live private var terminationObserver: NSObjectProtocol? internal var postConnectCancellable: AnyCancellable? // Owned here, not by the view: a connection switch cancels the view's `task`, and a cancelled first load never reran. @@ -393,6 +396,7 @@ final class MainContentCoordinator: ObservableObject { var refreshCoalesceTask: Task? var refreshPendingTrailing = false + let autoRefresh = TableAutoRefreshSchedule() // SwiftUI creates and discards coordinators during body re-evaluation; those never activate and need no teardown warning. private let _didActivate = OSAllocatedUnfairLock(initialState: false) @@ -566,44 +570,6 @@ final class MainContentCoordinator: ObservableObject { } } - private static let periodicSaveInterval: Duration = .seconds(30) - private static let draftSaveDebounce: Duration = .seconds(1) - - // Typing changes no tab identity, so without this a scratch query lives only in memory until the 30s timer or quit flush. - func scheduleDraftSave() { - guard !Self.isAppTerminating, !isTearingDown else { return } - draftSaveTask?.cancel() - draftSaveTask = Task { [weak self] in - try? await Task.sleep(for: Self.draftSaveDebounce) - guard let self, !Task.isCancelled, !Self.isAppTerminating, !self.isTearingDown else { return } - guard self.isFirstCoordinatorForConnection() else { return } - self.persistence.saveAggregated() - } - } - - private func startPeriodicSave() { - guard periodicSaveTask == nil else { return } - periodicSaveTask = Task { [weak self] in - while !Task.isCancelled { - try? await Task.sleep(for: Self.periodicSaveInterval) - guard let self, !Task.isCancelled, !Self.isAppTerminating, !self.isTearingDown else { return } - guard self.isFirstCoordinatorForConnection() else { continue } - self.persistence.saveAggregated() - } - } - } - - static func aggregatedSelectedTabId(for connectionId: UUID) -> UUID? { - activeCoordinators.values - .first { $0.connectionId == connectionId && $0.tabManager.selectedTabId != nil }? - .tabManager.selectedTabId - } - - private func isFirstCoordinatorForConnection() -> Bool { - Self.activeCoordinators.values - .first { $0.connectionId == self.connectionId } === self - } - private static let registerTerminationObserver: Void = { NotificationCenter.default.addObserver( forName: NSApplication.willTerminateNotification, @@ -709,6 +675,7 @@ final class MainContentCoordinator: ObservableObject { clearedEditsCancellable = resumeWhenEditsClear() gridRepaintCancellable = observeChangeManagerForGridRepaint() maskingRulesCancellable = observeMaskingRuleChanges() + observeSelectionForAutoRefresh() Self.lifecycleLogger.info( "[open] MainContentCoordinator.init done connId=\(connection.id, privacy: .public) elapsedMs=\(Int(Date().timeIntervalSince(initStart) * 1_000))" @@ -854,7 +821,7 @@ final class MainContentCoordinator: ObservableObject { connectionId: connectionId, tabType: .query, databaseName: resolved.database, - schemaName: resolved.schema, + schemaName: resolved.isDatabaseScoped ? nil : resolved.schema, initialQuery: source, skipAutoExecute: true, tabTitle: resolved.displayIdentity @@ -898,6 +865,7 @@ final class MainContentCoordinator: ObservableObject { reportEndedExecutions(tabExecution.invalidateAll(reason: .sessionEnded)) refreshCoalesceTask?.cancel() refreshCoalesceTask = nil + stopAutoRefresh() for entry in tableLoadTasks.values { entry.task.cancel() } tableLoadTasks.removeAll() cancelAllRowCountTasks() @@ -1061,6 +1029,7 @@ final class MainContentCoordinator: ObservableObject { let batches = queryExecutionCoordinator.executionBatches(in: sql, sourceOffset: sourceOffset ?? 0) let statements = batches.flatMap(\.statements) guard !statements.isEmpty else { return false } + tabVersions.record(tabManager.tabs[index], connectionId: connectionId, reason: .run) if services.appSettings.editor.queryParametersEnabled, bindsNamedParameters { let combinedSQL = SQLParameterExtractor.parameterSource(of: statements) @@ -1163,65 +1132,6 @@ final class MainContentCoordinator: ObservableObject { return nil } - // MARK: - Editor Query Loading - - @discardableResult - func loadQueryIntoEditor( - _ query: String, - databaseName: String? = nil, - forceNewTab: Bool = false - ) -> WindowTabOpenDisposition { - let targetDatabaseName = databaseName ?? newTabDatabaseName - if !forceNewTab, - let (tab, tabIndex) = tabManager.selectedTabAndIndex, - tab.tabType == .query, - databaseName == nil - || tab.tableContext.resolvedDatabaseName(browsing: browseDatabaseName) == targetDatabaseName { - tabManager.mutate(at: tabIndex) { - $0.content.query = query - $0.hasUserInteraction = true - } - return .currentCoordinator - } - - if !forceNewTab, tabManager.tabs.isEmpty { - tabManager.addTab(initialQuery: query, databaseName: targetDatabaseName) - return .currentCoordinator - } - - let payload = EditorTabPayload( - connectionId: connection.id, - tabType: .query, - databaseName: targetDatabaseName, - initialQuery: query - ) - openTabInNewWindow(payload) - return .focusedElsewhere - } - - var aiInsertReusesSelectedQueryTab: Bool { - guard let (tab, _) = tabManager.selectedTabAndIndex, tab.tabType == .query else { return false } - return tab.content.query.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty - } - - func insertQueryFromAI(_ query: String) { - if aiInsertReusesSelectedQueryTab, let (_, tabIndex) = tabManager.selectedTabAndIndex { - tabManager.mutate(at: tabIndex) { mutTab in - mutTab.content.query = query - mutTab.hasUserInteraction = true - } - } else if tabManager.tabs.isEmpty { - tabManager.addTab(initialQuery: query, databaseName: newTabDatabaseName) - } else { - let payload = EditorTabPayload( - connectionId: connection.id, - tabType: .query, - initialQuery: query - ) - WindowManager.shared.openTab(payload: payload) - } - } - internal func executeQueryInternal( _ sql: String, isAutoLoad: Bool = false, @@ -1245,7 +1155,7 @@ final class MainContentCoordinator: ObservableObject { let conn = connection let tabId = tabManager.tabs[index].id - let traceToken = adoptOrBeginExecutionTrace(tabId: tabId) + let traceToken = trigger.tracesLoad ? adoptOrBeginExecutionTrace(tabId: tabId) : nil traceExecutionStarted(traceToken, epoch: claim.epoch, isAutoLoad: isAutoLoad) let statement = resolveStatement(sql: sql, tabType: tab.tabType, bypassLimit: bypassRowLimit) @@ -1275,6 +1185,7 @@ final class MainContentCoordinator: ObservableObject { let isTableTab = tab.tabType == .table let readsView = tab.tableContext.knowsView(named: tableName) let (grammar, lengthColumns) = (lexicalGrammar, unloadedValueLengthColumns(for: tab)) + let held = manualCommitRun(skipped: isTableTab || isAutoLoad) let failureOutput = ServerOutputBox() let queryTask = Task { [weak self] in @@ -1293,13 +1204,15 @@ final class MainContentCoordinator: ObservableObject { isTableTab: isTableTab, lease: lease ) { [queryExecutor] driver in - let fetched = try await queryExecutor.executeQuery( - driver: driver, - sql: statement.sql, - parameters: nil, - rowCap: rowCap, - capturingOutputInto: isTableTab ? nil : failureOutput - ) + let fetched = try await held.running([statement.sql], on: driver) { + try await queryExecutor.executeQuery( + driver: driver, + sql: statement.sql, + parameters: nil, + rowCap: rowCap, + capturingOutputInto: isTableTab ? nil : failureOutput + ) + } let edits = isAutoLoad ? nil : await SucceededStatements.single( statement.sql, scope: scope, databaseType: conn.type, grammar: grammar, ranOn: driver ) @@ -1336,6 +1249,8 @@ final class MainContentCoordinator: ObservableObject { traceStaleResultDropped(traceToken) return false } + // A menu or value window opened during the read names rows by position, so a tick's rows wait for the next one. + guard trigger != .autoRefresh || !autoRefreshHeldByTransientUI(tabId) else { return false } toolbarState.recordQueryTiming(fetchResult.resolvedTiming, for: tabId) traceApplyingResult(traceToken, tabId: tabId) @@ -1367,7 +1282,8 @@ final class MainContentCoordinator: ObservableObject { absentCells: fetchResult.absentCells, unloadedCells: fetchResult.unloadedCells, columnOrigins: fetchResult.columnOrigins, - prepared: prepared + prepared: prepared, + trigger: trigger ) scheduleTraceCompletion(traceToken, outcome: .completed) @@ -1380,19 +1296,15 @@ final class MainContentCoordinator: ObservableObject { return } - if isEditable, let tableName { - // Marked pending here: phase 1 clears `isLoading` first, so without it the tab looks settled and offers Count Exactly early. - tabManager.mutate(tabId: tabId) { $0.pagination.isCountPending = true } - launchPhase2( - tableName: tableName, - tabId: tabId, - connectionType: conn.type, - needsMetadataFetch: needsMetadataFetch, - schemaTask: schemaTask - ) - } else if !isEditable || tableName == nil { - clearChangesIfCurrent(claim: claim) - } + launchPhase2AfterApply( + tableName: tableName, + isEditable: isEditable, + tabId: tabId, + trigger: trigger, + needsMetadataFetch: needsMetadataFetch, + schemaTask: schemaTask, + claim: claim + ) } catch { schemaTask?.cancel() if !isAutoLoad { Self.postStatementRan(statement.sql, on: conn) } diff --git a/apple/TablePro/Views/Main/MainContentView.swift b/apple/TablePro/Views/Main/MainContentView.swift index 82eade5715..c1e5df3ff7 100644 --- a/apple/TablePro/Views/Main/MainContentView.swift +++ b/apple/TablePro/Views/Main/MainContentView.swift @@ -512,7 +512,8 @@ struct MainContentView: View { ConnectionTransactionBar( connectionId: connection.id, transactions: DatabaseManager.shared.sessionTransactions, - databaseManager: DatabaseManager.shared + databaseManager: DatabaseManager.shared, + safeModeLevel: toolbarState.safeModeLevel ) editorContentView } diff --git a/apple/TablePro/Views/ObjectSource/ObjectSourceTabView.swift b/apple/TablePro/Views/ObjectSource/ObjectSourceTabView.swift index 289b6e133e..67d6514169 100644 --- a/apple/TablePro/Views/ObjectSource/ObjectSourceTabView.swift +++ b/apple/TablePro/Views/ObjectSource/ObjectSourceTabView.swift @@ -2,7 +2,8 @@ // ObjectSourceTabView.swift // TablePro // -// Tab showing the source of one stored procedure, function, trigger, user-defined type or view. +// Tab showing the source of one stored procedure, function, trigger, user-defined type, view or +// driver-declared server object. // import Combine @@ -73,14 +74,12 @@ final class ObjectSourceLoader: ObservableObject { await load() } - /// One round trip. Re-listing the schema to recover the attributes cost a full catalog scan - /// per open and per reload, for values the sidebar's listing already carried into the ref. A - /// type is the exception: its definition and attributes are read fresh. + // One round trip, with the attributes the listing carried; a type and a server object read theirs fresh. private func fetch() async throws -> Fetched { let scope = DatabaseScope( connectionId: connectionId, database: objectRef.database, - schema: objectRef.schema + schema: objectRef.isDatabaseScoped ? nil : objectRef.schema ) return try await metadata.withMetadataDriver(scope: scope) { [objectRef] driver in switch objectRef.kind { @@ -105,6 +104,15 @@ final class ObjectSourceLoader: ObservableObject { throw PluginObjectSourceError.unsupported(objectRef.name) } return Fetched(source: source, attributes: fetched.attributes) + case .serverObject: + guard let object = objectRef.serverObject else { + throw PluginObjectSourceError.unsupported(objectRef.name) + } + let fetched = try await driver.fetchServerObject(object) + guard let source = fetched.definition, !source.isEmpty else { + throw PluginObjectSourceError.unsupported(objectRef.name) + } + return Fetched(source: source, attributes: fetched.attributes.isEmpty ? objectRef.attributes : fetched.attributes) case .view, .materializedView: let source = try await TableDDLComposer.fetchDDL( for: objectRef.name, diff --git a/apple/TablePro/Views/Results/ResultStatusBar.swift b/apple/TablePro/Views/Results/ResultStatusBar.swift index 205be5bc3f..601444a947 100644 --- a/apple/TablePro/Views/Results/ResultStatusBar.swift +++ b/apple/TablePro/Views/Results/ResultStatusBar.swift @@ -3,6 +3,7 @@ // TablePro // +import AppKit import SwiftUI /// The window's bottom bar: what the result is, and the controls that change what of it you see. @@ -50,6 +51,7 @@ struct ResultStatusBar: View { let resultSetActions: ResultSetActions let onToggleFilters: () -> Void let onFetchAll: (() -> Void)? + var autoRefresh: StatusBarAutoRefreshState = .unavailable @State private var showColumnPopover = false @State private var showHighlightPopover = false @@ -290,6 +292,9 @@ struct ResultStatusBar: View { if model.controls.showsHighlightRules { highlightButton } + if model.controls.showsAutoRefresh { + autoRefreshMenu(presentation) + } if model.controls.showsFilters { filtersToggle(presentation) } @@ -402,6 +407,51 @@ struct ResultStatusBar: View { return String(format: String(localized: "%d rules"), count) } + // A Menu around an inline Picker: a pop-up button always spells out its choice, so it could not go to a glyph. + private func autoRefreshMenu(_ presentation: ResultStatusPresentation) -> some View { + Menu { + Picker( + String(localized: "Auto Refresh"), + selection: Binding(get: { autoRefresh.interval }, set: { autoRefresh.onChange($0) }) + ) { + ForEach(TableAutoRefreshInterval.allCases) { interval in + Text(interval.title).tag(interval) + } + } + .pickerStyle(.inline) + .labelsHidden() + } label: { + Label { + Text(autoRefresh.interval.shortTitle) + } icon: { + (Self.autoRefreshSymbol(isOn: autoRefresh.interval.isOn).map(Image.init(nsImage:)) + ?? Image(systemName: Self.autoRefreshSymbolName)) + .statusBarControlIcon(besideTitle: presentation.showsControlTitles) + } + } + .menuStyle(.button) + .menuIndicator(presentation.showsControlTitles ? .visible : .hidden) + .statusBarLabelStyle(showsTitle: presentation.showsControlTitles) + .controlSize(.small) + .fixedSize() + .help(autoRefresh.help) + .accessibilityLabel(String(localized: "Auto Refresh")) + .accessibilityValue(autoRefresh.interval.title) + .accessibilityIdentifier("result-status-auto-refresh") + } + + static let autoRefreshSymbolName = "clock.arrow.circlepath" + + // A menu draws a SwiftUI symbol as a template and drops its tint, so the on state is an accent palette image. + static func autoRefreshSymbol(isOn: Bool) -> NSImage? { + let size = NSImage.SymbolConfiguration(pointSize: NSFont.systemFontSize(for: .small), weight: .regular) + let configuration = isOn ? size.applying(NSImage.SymbolConfiguration(paletteColors: [.controlAccentColor])) : size + guard let image = NSImage(systemSymbolName: autoRefreshSymbolName, accessibilityDescription: nil)? + .withSymbolConfiguration(configuration) else { return nil } + image.isTemplate = !isOn + return image + } + private func filtersToggle(_ presentation: ResultStatusPresentation) -> some View { Toggle(isOn: Binding(get: { filterState.isVisible }, set: { _ in onToggleFilters() })) { Label { diff --git a/apple/TablePro/Views/Results/ResultStatusInputs.swift b/apple/TablePro/Views/Results/ResultStatusInputs.swift index af51da33e7..f0382654f5 100644 --- a/apple/TablePro/Views/Results/ResultStatusInputs.swift +++ b/apple/TablePro/Views/Results/ResultStatusInputs.swift @@ -34,6 +34,21 @@ struct StatusBarHighlightState { } } +struct StatusBarAutoRefreshState { + let interval: TableAutoRefreshInterval + let pause: TableAutoRefreshPause? + let onChange: (TableAutoRefreshInterval) -> Void + + static var unavailable: StatusBarAutoRefreshState { + StatusBarAutoRefreshState(interval: .off, pause: nil, onChange: { _ in }) + } + + var help: String { + if interval.isOn, let reason = pause?.help { return reason } + return String(format: String(localized: "Auto Refresh: %@"), interval.title) + } +} + struct StatusBarColumnState { let hidden: Set let columns: [GridColumnEntry] diff --git a/apple/TablePro/Views/Results/TextViewerWindowController.swift b/apple/TablePro/Views/Results/TextViewerWindowController.swift index bca006ed2c..b9a073d875 100644 --- a/apple/TablePro/Views/Results/TextViewerWindowController.swift +++ b/apple/TablePro/Views/Results/TextViewerWindowController.swift @@ -8,12 +8,13 @@ import SwiftUI @MainActor internal final class TextViewerWindowController: ValueViewerWindowController { + @discardableResult static func open( text: String?, columnName: String?, isEditable: Bool, onCommit: ((String) -> Void)? - ) { + ) -> TextViewerWindowController { let title: String if let columnName { title = String(format: String(localized: "Text: %@"), columnName) @@ -34,6 +35,7 @@ internal final class TextViewerWindowController: ValueViewerWindowController { onDismiss: dismiss ) } + return controller } } diff --git a/apple/TablePro/Views/RowInspector/InspectorPopOutRegistry.swift b/apple/TablePro/Views/RowInspector/InspectorPopOutRegistry.swift new file mode 100644 index 0000000000..b62bb99911 --- /dev/null +++ b/apple/TablePro/Views/RowInspector/InspectorPopOutRegistry.swift @@ -0,0 +1,32 @@ +// +// InspectorPopOutRegistry.swift +// TablePro +// + +import Foundation + +// An editable pop-out commits to the row ids it opened on, which a reload hands to other rows, so Auto Refresh waits on it. +@MainActor +internal enum InspectorPopOutRegistry { + private struct Entry { + weak var owner: TrailingPaneState? + weak var window: ValueViewerWindowController? + } + + private static var entries: [Entry] = [] + + // Weak both ways: the window controller lives exactly as long as its window is open. + internal static func record(_ window: ValueViewerWindowController, openedFrom owner: TrailingPaneState) { + prune() + entries.append(Entry(owner: owner, window: window)) + } + + internal static func holdsWindow(openedFrom owner: TrailingPaneState) -> Bool { + prune() + return entries.contains { $0.owner === owner } + } + + private static func prune() { + entries.removeAll { $0.owner == nil || $0.window == nil } + } +} diff --git a/apple/TablePro/Views/RowInspector/RowInspectorView.swift b/apple/TablePro/Views/RowInspector/RowInspectorView.swift index 112c74b661..988d7da378 100644 --- a/apple/TablePro/Views/RowInspector/RowInspectorView.swift +++ b/apple/TablePro/Views/RowInspector/RowInspectorView.swift @@ -149,12 +149,13 @@ internal struct RowInspectorView: View { switch InspectorPopOutWindow.resolve(for: kind) { case .json: - JSONViewerWindowController.open( + let window = JSONViewerWindowController.open( text: text, columnName: field.columnName, isEditable: isEditable, onCommit: commit ) + if commit != nil { InspectorPopOutRegistry.record(window, openedFrom: paneState) } case .php: PhpViewerWindowController.open(text: text, columnName: field.columnName) case .binary(let format): @@ -165,12 +166,13 @@ internal struct RowInspectorView: View { columnName: field.columnName ) case .text: - TextViewerWindowController.open( + let window = TextViewerWindowController.open( text: text, columnName: field.columnName, isEditable: isEditable, onCommit: commit ) + if commit != nil { InspectorPopOutRegistry.record(window, openedFrom: paneState) } } } } diff --git a/apple/TablePro/Views/Settings/Sections/HistorySection.swift b/apple/TablePro/Views/Settings/Sections/HistorySection.swift index addecec3d7..222f8a544f 100644 --- a/apple/TablePro/Views/Settings/Sections/HistorySection.swift +++ b/apple/TablePro/Views/Settings/Sections/HistorySection.swift @@ -35,7 +35,11 @@ struct HistorySection: View { let confirmed = await AlertHelper.confirmDestructive( title: String(localized: "Clear All History?"), message: String( - localized: "This will permanently delete all query history entries, and the saved changes kept for restoring. This action cannot be undone." + localized: """ + This will permanently delete all query history entries, the saved changes kept \ + for restoring, and the earlier versions of your query tabs. This action cannot \ + be undone. + """ ), confirmButton: String(localized: "Clear"), cancelButton: String(localized: "Cancel") @@ -44,6 +48,7 @@ struct HistorySection: View { Task { _ = await QueryHistoryManager.shared.clearEverything() _ = await QueryHistoryManager.shared.clearRewindSnapshots() + _ = await QueryTabVersionStorage.shared.removeAll() } } } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeCellView.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeCellView.swift index e30599e652..32fbe661df 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeCellView.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeCellView.swift @@ -40,6 +40,8 @@ final class DatabaseTreeCellView: RenamableSidebarCellView return metadata.isSystemDatabase ? "gearshape" : "cylinder" case .schema, .tableFolder: return "folder" + case .serverObject(let ref): + return ref.kind.iconName case .routine, .trigger, .userType, .status, .recentSection, .temporarySection, .foldersSection, .objectKindSection, .containerObjectKindSection, .hierarchicalSchemaSection, .redisKeysSection, .redisNode: return "tablecells" diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeDoubleClickIntent.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeDoubleClickIntent.swift index b1808d59ad..dae589edb4 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeDoubleClickIntent.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeDoubleClickIntent.swift @@ -23,9 +23,7 @@ internal enum DatabaseTreeDoubleClickIntent: Equatable { } internal enum DatabaseTreeDoubleClickResolver { - /// Being a table wins over being expandable. A partitioned table is both, and the HIG puts - /// disclosure on the triangle and the Right and Left arrows, so the row's own double-click is - /// free to mean what it means everywhere else: open the thing you clicked. + // Being a table wins over being expandable: the HIG puts disclosure on the triangle and the arrow keys. internal static func resolve(node: DatabaseTreeNode) -> DatabaseTreeDoubleClickIntent { if let ref = DatabaseTreeSelection.tableRef(of: node) { return .openPermanently(ref) @@ -37,6 +35,8 @@ internal enum DatabaseTreeDoubleClickResolver { return .openObjectSource(ref.objectRef) case .userType(let ref): return .openObjectSource(ref.objectRef) + case .serverObject(let ref): + return .openObjectSource(ref.objectRef) default: return node.isExpandable ? .toggleDisclosure : .ignore } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift index adbf4d9282..a280e732b0 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift @@ -37,6 +37,7 @@ struct DatabaseTreeBucketSource: Equatable { let routines: SameRows let triggers: SameRows let userTypes: SameRows + let serverObjects: [String: SameRows] let searchText: String let database: String? @@ -45,6 +46,7 @@ struct DatabaseTreeBucketSource: Equatable { routines: [RoutineInfo], triggers: [TriggerInfo], userTypes: [UserDefinedTypeInfo], + serverObjects: [String: [ServerObjectInfo]] = [:], searchText: String, database: String? ) { @@ -52,6 +54,7 @@ struct DatabaseTreeBucketSource: Equatable { self.routines = SameRows(rows: routines) self.triggers = SameRows(rows: triggers) self.userTypes = SameRows(rows: userTypes) + self.serverObjects = serverObjects.mapValues { SameRows(rows: $0) } self.searchText = searchText self.database = database } @@ -62,6 +65,7 @@ struct DatabaseTreeBucketSource: Equatable { routines: routines.rows, triggers: triggers.rows, userTypes: userTypes.rows, + serverObjects: serverObjects.mapValues(\.rows), searchText: searchText, database: database ) @@ -87,25 +91,27 @@ struct DatabaseTreeBucketMemo { } } -/// One filtered pass over a container's objects, split by kind. The container row needs the counts -/// and each group row under it needs one bucket, so both read this rather than filtering the whole -/// list again per group. +// One filtered pass per container, read by the container row and every group under it. struct DatabaseTreeObjectBuckets { let tables: [SidebarObjectKind: [TableInfo]] let routines: [SidebarObjectKind: [RoutineInfo]] let triggers: [TriggerInfo] let userTypes: [UserDefinedTypeInfo] + // Keyed by the declared kind's id. + let serverObjects: [String: [ServerObjectInfo]] init( tables: [SidebarObjectKind: [TableInfo]], routines: [SidebarObjectKind: [RoutineInfo]], triggers: [TriggerInfo], - userTypes: [UserDefinedTypeInfo] = [] + userTypes: [UserDefinedTypeInfo] = [], + serverObjects: [String: [ServerObjectInfo]] = [:] ) { self.tables = tables self.routines = routines self.triggers = triggers self.userTypes = userTypes + self.serverObjects = serverObjects } var isEmpty: Bool { @@ -113,8 +119,10 @@ struct DatabaseTreeObjectBuckets { && routines.values.allSatisfy(\.isEmpty) && triggers.isEmpty && userTypes.isEmpty + && serverObjects.values.allSatisfy(\.isEmpty) } + // A declared kind compares by id, so the key built here finds the declared descriptor. var itemCounts: [SidebarObjectKind: Int] { var counts = tables.mapValues(\.count) for (kind, list) in routines { @@ -124,6 +132,9 @@ struct DatabaseTreeObjectBuckets { if !userTypes.isEmpty { counts[.type, default: 0] += userTypes.count } + for (kindId, list) in serverObjects where !list.isEmpty { + counts[.server(SidebarServerObjectKind(id: kindId)), default: 0] += list.count + } return counts } } @@ -230,11 +241,21 @@ enum DatabaseTreeFilter { return deduplicated(matched, by: \.id) } + static func filteredServerObjects( + _ objects: [ServerObjectInfo], + searchText: String, + database: String? = nil + ) -> [ServerObjectInfo] { + let matched = SidebarSearch(searchText).ranked(objects, name: { $0.name }, database: database, schema: { $0.schema }) + return deduplicated(matched, by: \.id) + } + static func objectBuckets( tables: [TableInfo], routines: [RoutineInfo], triggers: [TriggerInfo], userTypes: [UserDefinedTypeInfo] = [], + serverObjects: [String: [ServerObjectInfo]] = [:], searchText: String, database: String? = nil ) -> DatabaseTreeObjectBuckets { @@ -250,7 +271,10 @@ enum DatabaseTreeFilter { tables: tableBuckets, routines: routineBuckets, triggers: filteredTriggers(triggers, searchText: searchText, database: database), - userTypes: filteredUserTypes(userTypes, searchText: searchText, database: database) + userTypes: filteredUserTypes(userTypes, searchText: searchText, database: database), + serverObjects: serverObjects.mapValues { + filteredServerObjects($0, searchText: searchText, database: database) + } ) } @@ -310,6 +334,7 @@ enum DatabaseTreeFilter { routines: service.routines(for: connectionId, schema: schema), triggers: service.triggers(for: connectionId, schema: schema), userTypes: service.userDefinedTypes(for: connectionId, schema: schema), + serverObjects: service.serverObjectsByKind(for: connectionId, schema: schema), searchText: searchText, database: database )), @@ -324,14 +349,14 @@ enum DatabaseTreeFilter { return kept } - /// A schema the search matched by name shows everything inside it. Filtering its objects by the - /// same query leaves the matched schema reporting no items. + // A schema matched by name shows everything inside it, or it would report no items. static func hierarchicalBucketSource( schema: String, tables: [TableInfo], routines: [RoutineInfo], triggers: [TriggerInfo], userTypes: [UserDefinedTypeInfo], + serverObjects: [String: [ServerObjectInfo]] = [:], searchText: String, database: String? = nil ) -> DatabaseTreeBucketSource { @@ -341,6 +366,7 @@ enum DatabaseTreeFilter { routines: routines, triggers: triggers, userTypes: userTypes, + serverObjects: serverObjects, searchText: matchesSchema ? "" : searchText, database: database ) @@ -557,11 +583,15 @@ enum DatabaseTreeFilter { !service.isTablesCurrent(connectionId: connectionId, database: database, schema: schema) { tables = listingMatches.rowsBySchema[schema] ?? [] } + let serverKinds = service.serverKinds(connectionId: connectionId, database: database, schema: schema) return objectBuckets( tables: tables, routines: service.routines(connectionId: connectionId, database: database, schema: schema), triggers: service.triggers(connectionId: connectionId, database: database, schema: schema), userTypes: service.userDefinedTypes(connectionId: connectionId, database: database, schema: schema), + serverObjects: Dictionary(uniqueKeysWithValues: serverKinds.map { kind in + (kind.id, service.serverObjects(connectionId: connectionId, database: database, schema: schema, kindId: kind.id)) + }), searchText: searchText, database: database ) diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift index 0a8aaa293c..a8957add7d 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift @@ -5,16 +5,16 @@ import Foundation -/// Which groups a container shows. A group stands for objects the container holds, and a container -/// with nothing to list answers with its own status row instead. +// A group stands for objects the container holds; a container with nothing to list shows its own status row. internal enum DatabaseTreeObjectGroupResolver { internal static func groups( database: String, schema: String?, itemCounts: [SidebarObjectKind: Int], - keeping: Set = [] + keeping: Set = [], + declared: [SidebarServerObjectKind] = [] ) -> [DatabaseTreeObjectGroup] { - SidebarObjectKind.visible(itemCounts: itemCounts, keeping: keeping) + SidebarObjectKind.visible(itemCounts: itemCounts, keeping: keeping, declared: declared) .map { DatabaseTreeObjectGroup(database: database, schema: schema, kind: $0) } } } @@ -31,36 +31,48 @@ internal extension DatabaseTreeNode.Status { } } -/// Where each side kind's fetch stands for one container. A kind the engine does not declare has no -/// phase, because a fetch the engine never runs stays idle and would read as loading forever. +// An undeclared kind has no phase: a fetch the engine never runs stays idle and would read as loading forever. internal struct DatabaseTreeSidePhases: Equatable { let routines: MetadataLoadPhase let triggers: MetadataLoadPhase? let types: MetadataLoadPhase? + // Keyed by kind id, holding only the declared kinds this container lists. + let serverObjects: [String: MetadataLoadPhase] + + init( + routines: MetadataLoadPhase, + triggers: MetadataLoadPhase?, + types: MetadataLoadPhase?, + serverObjects: [String: MetadataLoadPhase] = [:] + ) { + self.routines = routines + self.triggers = triggers + self.types = types + self.serverObjects = serverObjects + } var all: [MetadataLoadPhase] { - [routines] + [triggers, types].compactMap { $0 } + [routines] + [triggers, types].compactMap { $0 } + serverObjects.keys.sorted().compactMap { serverObjects[$0] } } - func phase(for category: SidebarObjectCategory) -> MetadataLoadPhase { - switch category { + func phase(for kind: SidebarObjectKind) -> MetadataLoadPhase { + switch kind.category { case .table: return .loaded case .routine: return routines case .trigger: return triggers ?? .loaded case .type: return types ?? .loaded + case .server: return kind.serverKind.flatMap { serverObjects[$0.id] } ?? .loaded } } - /// The declared kinds whose fetch failed. Each keeps its group, empty of rows, so the failure shows - /// where the rows would be: hiding the group would make a list the server refused read as empty. + // A failed kind keeps its group, so a list the server refused never reads as empty. func failedKinds(among declared: Set) -> Set { - declared.filter { phase(for: $0.category).failureMessage != nil } + declared.filter { phase(for: $0).failureMessage != nil } } - /// The row a group with no rows shows: the failure, or nothing, since a group without rows is - /// listed only to show a failure or to take filed objects back out of their folders. + // A group without rows is listed only for a failure or to take filed objects back out of their folders. func placeholder(for kind: SidebarObjectKind) -> DatabaseTreeNode.Status? { - phase(for: kind.category).failureMessage.map(DatabaseTreeNode.Status.error) + phase(for: kind).failureMessage.map(DatabaseTreeNode.Status.error) } } @@ -94,6 +106,7 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { case routine(DatabaseTreeRoutineRef) case trigger(DatabaseTreeTriggerRef) case userType(DatabaseTreeUserTypeRef) + case serverObject(DatabaseTreeServerObjectRef) case status(Status) /// Flat shape: one collapsible section per object kind. @@ -128,7 +141,7 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { case .redisNode(let node): guard case .namespace = node else { return false } return true - case .recentTable, .temporaryTable, .routine, .trigger, .userType, .status: + case .recentTable, .temporaryTable, .routine, .trigger, .userType, .serverObject, .status: return false } } @@ -156,19 +169,14 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { return nil } - /// A source-list group row: chrome the app invented to bucket objects, not an object the - /// database has. AppKit draws these itself once `isGroupItem` says so, and it stops indenting - /// their children, which is what puts a table at the same depth as a database in the tree. - /// - /// A schema is deliberately not one. It is a real object with its own menu and its own - /// children, so it stays an ordinary container row the way a folder does in Xcode's navigator. + // Chrome the app invented to bucket objects; a schema is a real object, so it stays an ordinary container row. var isGroupRow: Bool { switch kind { case .recentSection, .temporarySection, .foldersSection, .objectKindSection, .redisKeysSection: return true case .database, .schema, .containerObjectKindSection, .tableFolder, .hierarchicalSchemaSection, .recentTable, .temporaryTable, .table, .partition, - .routine, .trigger, .userType, .status, .redisNode: + .routine, .trigger, .userType, .serverObject, .status, .redisNode: return false } } @@ -178,7 +186,7 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { case .database, .schema: return true case .recentSection, .recentTable, .temporarySection, .temporaryTable, .foldersSection, .tableFolder, - .table, .partition, .routine, .trigger, .userType, .status, .objectKindSection, + .table, .partition, .routine, .trigger, .userType, .serverObject, .status, .objectKindSection, .containerObjectKindSection, .hierarchicalSchemaSection, .redisKeysSection, .redisNode: return false } @@ -191,7 +199,7 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { case .schema(let database, let schema): return .schema(database: database, schema: schema, isSystem: systemSchemas.contains(schema)) case .recentSection, .recentTable, .temporarySection, .temporaryTable, .foldersSection, .tableFolder, - .table, .partition, .routine, .trigger, .userType, .status, .objectKindSection, + .table, .partition, .routine, .trigger, .userType, .serverObject, .status, .objectKindSection, .containerObjectKindSection, .hierarchicalSchemaSection, .redisKeysSection, .redisNode: return nil } @@ -256,6 +264,7 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { static func routineId(_ ref: DatabaseTreeRoutineRef) -> String { "routine\u{1}\(ref.id)" } static func triggerId(_ ref: DatabaseTreeTriggerRef) -> String { "trigger\u{1}\(ref.id)" } static func userTypeId(_ ref: DatabaseTreeUserTypeRef) -> String { "usertype\u{1}\(ref.id)" } + static func serverObjectId(_ ref: DatabaseTreeServerObjectRef) -> String { "serverobject\u{1}\(ref.id)" } static func statusId(parentId: String, status: Status) -> String { switch status { case .loading: return "\(parentId)\u{1}status.loading" @@ -265,9 +274,9 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { } } - static func objectKindSectionId(_ kind: SidebarObjectKind) -> String { "kindSection\u{1}\(kind.rawValue)" } + static func objectKindSectionId(_ kind: SidebarObjectKind) -> String { "kindSection\u{1}\(kind.persistenceName)" } static func containerObjectKindSectionId(_ group: DatabaseTreeObjectGroup) -> String { - "containerKindSection\u{1}\(group.database)\u{1}\(group.schema ?? "")\u{1}\(group.kind.rawValue)" + "containerKindSection\u{1}\(group.database)\u{1}\(group.schema ?? "")\u{1}\(group.kind.persistenceName)" } static func hierarchicalSchemaSectionId(_ schema: String) -> String { "hschema\u{1}\(schema)" } static let redisKeysSectionId = "redis-keys-section" diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Expansion.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Expansion.swift index c161f342db..e591b4ba23 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Expansion.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Expansion.swift @@ -87,6 +87,7 @@ extension DatabaseTreeOutlineCoordinator { restoreObjectGroupExpansion(under: schemaNode) } } + restoreObjectGroupExpansion(under: databaseNode) } } @@ -182,24 +183,21 @@ extension DatabaseTreeOutlineCoordinator { } } - /// The cascade only re-asserts disclosure the store already holds, so it runs behind the flag - /// `applyDesiredExpansion` uses. Recording each row it opens rewrote the whole expansion - /// snapshot to `UserDefaults`, and re-entered this restore once per row. + // Behind the `applyDesiredExpansion` flag: recording each row rewrote the expansion snapshot and re-entered this. internal func restoreDescendantExpansion(afterExpanding node: DatabaseTreeNode) { let wasApplyingExpansion = isApplyingExpansion isApplyingExpansion = true defer { isApplyingExpansion = wasApplyingExpansion } switch node.kind { - case .database where !supportsSchemaLevel: - restoreObjectGroupExpansion(under: node) - case .schema, .hierarchicalSchemaSection: + // A schema engine's database holds its `.database` kind groups beside the schemas. + case .database, .schema, .hierarchicalSchemaSection: restoreObjectGroupExpansion(under: node) case .foldersSection: restoreFolderExpansion(under: node) case .objectKindSection, .containerObjectKindSection, .tableFolder, .table, .partition: restorePartitionExpansion(under: node) - case .recentSection, .recentTable, .temporarySection, .temporaryTable, .database, .routine, .trigger, - .userType, .status, .redisKeysSection, .redisNode: + case .recentSection, .recentTable, .temporarySection, .temporaryTable, .routine, .trigger, + .userType, .serverObject, .status, .redisKeysSection, .redisNode: break } } @@ -257,7 +255,7 @@ extension DatabaseTreeOutlineCoordinator { case .partition(let ref): guard let tableRef = ref.tableRef else { break } recordTableExpansion(tableRef, expanded: expanded) - case .recentTable, .temporaryTable, .routine, .trigger, .userType, .status, .redisNode: + case .recentTable, .temporaryTable, .routine, .trigger, .userType, .serverObject, .status, .redisNode: break } } @@ -269,6 +267,7 @@ extension DatabaseTreeOutlineCoordinator { if service.schemaListNeedsFetch(connectionId: connectionId, database: metadata.name) { Task { await service.loadSchemas(connectionId: connectionId, database: metadata.name) } } + loadDatabaseServerObjects(database: metadata.name) loadExternalSchemaNames(database: metadata.name) } else { loadObjects(database: metadata.name, schema: nil) @@ -288,12 +287,18 @@ extension DatabaseTreeOutlineCoordinator { else { return } loadHierarchicalSchemaObjects(schema) case .recentSection, .recentTable, .temporarySection, .temporaryTable, .foldersSection, .tableFolder, - .routine, .trigger, .userType, .status, .objectKindSection, .containerObjectKindSection, + .routine, .trigger, .userType, .serverObject, .status, .objectKindSection, .containerObjectKindSection, .redisKeysSection, .redisNode: break } } + private func loadDatabaseServerObjects(database: String) { + guard service.serverObjectsNeedFetch(connectionId: connectionId, database: database, schema: nil) else { return } + let connectionId = connectionId + Task { await service.loadServerObjects(connectionId: connectionId, database: database, schema: nil) } + } + /// A schema the user has open keeps reading the lists a change overtook while a search runs; one /// the search opened shows the listing's tables instead. internal func isOpenedByUser(database: String, schema: String) -> Bool { diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+FolderNodes.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+FolderNodes.swift index 74b3fa8fa7..a7560cd73c 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+FolderNodes.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+FolderNodes.swift @@ -13,7 +13,7 @@ import TableProPluginKit /// its children. Either way an object sits in one row only, so a filed table is drawn inside its /// folder and left out of its kind section. extension DatabaseTreeOutlineCoordinator { - internal static let tableKinds = SidebarObjectKind.allCases.filter { $0.category == .table } + internal static let tableKinds = SidebarObjectKind.builtIn.filter { $0.category == .table } internal func folderScope(for container: TableFolderContainer) -> DatabaseScope? { switch container { diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Menu.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Menu.swift index d48854232f..b0bbb8a0b4 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Menu.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Menu.swift @@ -173,7 +173,7 @@ extension DatabaseTreeOutlineCoordinator: NSMenuDelegate { private func objectKindTitles() -> [SidebarObjectKind: String] { let tableEntityName = PluginManager.shared.tableEntityName(for: databaseType) var titles: [SidebarObjectKind: String] = [:] - for kind in SidebarObjectKind.allCases { + for kind in SidebarObjectKind.ordered(declared: declaredServerKinds) { titles[kind] = kind.title(tableEntityName: tableEntityName) } return titles diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Nodes.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Nodes.swift index 665e162416..191aa1ff86 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Nodes.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Nodes.swift @@ -58,7 +58,7 @@ extension DatabaseTreeOutlineCoordinator { return folderMemberNodes(of: ref) case .database(let metadata): return supportsSchemaLevel - ? schemaNodes(database: metadata.name) + ? databaseLevelNodes(database: metadata.name) : objectNodes(database: metadata.name, schema: nil) case .schema(let database, let schema): return objectNodes(database: database, schema: schema) @@ -78,7 +78,7 @@ extension DatabaseTreeOutlineCoordinator { return redisChildren(of: nil) case .redisNode(let redisNode): return redisChildren(of: redisNode) - case .recentTable, .temporaryTable, .routine, .trigger, .userType, .status: + case .recentTable, .temporaryTable, .routine, .trigger, .userType, .serverObject, .status: return [] } } @@ -208,16 +208,17 @@ extension DatabaseTreeOutlineCoordinator { .map { node(id: DatabaseTreeNode.schemaId(database: database, schema: $0), kind: .schema(database: database, schema: $0)) } } - /// A torn-down sidebar has no view model and lists nothing at all. Counting its way to a root of - /// failed sections would fill a pane that is on its way out. + // A torn-down sidebar has no view model and lists nothing, rather than a root of failed sections. private func visibleObjectKinds() -> [SidebarObjectKind] { guard viewModel != nil else { return [] } - let itemCounts = SidebarObjectKind.allCases.reduce(into: [SidebarObjectKind: Int]()) { + let declared = flatServerKinds + let itemCounts = SidebarObjectKind.ordered(declared: declared).reduce(into: [SidebarObjectKind: Int]()) { $0[$1] = flatItemCount(for: $1) } return SidebarObjectKind.visible( itemCounts: itemCounts, - keeping: flatPlan().filedKinds.union(flatSidePhases.failedKinds(among: declaredObjectKinds)) + keeping: flatPlan().filedKinds.union(flatSidePhases.failedKinds(among: declaredObjectKinds)), + declared: declared ) } @@ -228,10 +229,29 @@ extension DatabaseTreeOutlineCoordinator { return .emptyContainer(sideStates: flatSidePhases.all) } - /// What the engine says it fetches. It decides which fetches a container waits on before saying - /// it is empty, and which failures keep a section; it never lists a section on its own. + // Decides which fetches a container waits on and which failures keep a section; it never lists one on its own. internal var declaredObjectKinds: Set { - databaseType.declaredObjectKinds + databaseType.declaredObjectKinds.union(declaredServerKinds.map { SidebarObjectKind.server($0) }) + } + + // Asked of the live driver, which answers per server flavor and version. + internal var declaredServerKinds: [SidebarServerObjectKind] { + SidebarServerObjectKind.declared(by: DatabaseManager.shared.driver(for: connectionId)) + } + + // A hierarchical engine lists its kinds per schema, never in the browsed catalog. + internal var flatServerKinds: [SidebarServerObjectKind] { + PluginManager.shared.databaseGroupingStrategy(for: databaseType) == .hierarchicalSchema ? [] : declaredServerKinds + } + + private var containerServerKinds: [SidebarServerObjectKind] { + declaredServerKinds.filter { $0.scope == .container } + } + + // The tree reads the kinds of the container's own level; a schema-grouped shape lists `.container` kinds alone. + private func serverKinds(database: String, schema: String?) -> [SidebarServerObjectKind] { + if rootShape == .hierarchicalSchema { return containerServerKinds } + return service.serverKinds(connectionId: connectionId, database: database, schema: schema) } internal func flatItemCount(for kind: SidebarObjectKind) -> Int { @@ -245,6 +265,12 @@ extension DatabaseTreeOutlineCoordinator { return viewModel.filteredTriggers(from: schemaService.triggers(for: connectionId)).count case .type: return viewModel.filteredUserTypes(from: schemaService.userDefinedTypes(for: connectionId)).count + case .server: + guard let serverKind = kind.serverKind else { return 0 } + return viewModel.filteredServerObjects( + kindId: serverKind.id, + from: schemaService.serverObjects(for: connectionId, kindId: serverKind.id) + ).count } } @@ -258,14 +284,16 @@ extension DatabaseTreeOutlineCoordinator { return [statusNode(parentId: DatabaseTreeNode.objectKindSectionId(kind), status: placeholder)] } - /// Only the kinds the engine declares take part, the rule `sideLoadStates` follows for a tree - /// container, so a fetch the engine never runs cannot hold the root on a spinner. + // Declared kinds only, as `sideLoadStates`, so a fetch the engine never runs cannot hold the root on a spinner. private var flatSidePhases: DatabaseTreeSidePhases { let declared = declaredObjectKinds return DatabaseTreeSidePhases( routines: schemaService.routinesLoadState(for: connectionId).erased, triggers: declared.contains(.trigger) ? schemaService.triggersLoadState(for: connectionId).erased : nil, - types: declared.contains(.type) ? schemaService.userDefinedTypesLoadState(for: connectionId).erased : nil + types: declared.contains(.type) ? schemaService.userDefinedTypesLoadState(for: connectionId).erased : nil, + serverObjects: Dictionary(uniqueKeysWithValues: flatServerKinds.map { kind in + (kind.id, schemaService.serverObjectsLoadState(for: connectionId, kindId: kind.id).erased) + }) ) } @@ -295,6 +323,13 @@ extension DatabaseTreeOutlineCoordinator { let ref = DatabaseTreeUserTypeRef(database: database, schema: type.schema, type: type) return node(id: DatabaseTreeNode.userTypeId(ref), kind: .userType(ref)) } + case .server: + guard let serverKind = kind.serverKind else { return [] } + let objects = viewModel.filteredServerObjects( + kindId: serverKind.id, + from: schemaService.serverObjects(for: connectionId, kindId: serverKind.id) + ) + return serverObjectNodes(objects, kind: serverKind, database: database, schema: { $0.schema }) } } @@ -414,7 +449,10 @@ extension DatabaseTreeOutlineCoordinator { : nil, types: declared.contains(.type) ? schemaService.userDefinedTypesLoadState(for: connectionId, schema: schema).erased - : nil + : nil, + serverObjects: Dictionary(uniqueKeysWithValues: containerServerKinds.map { kind in + (kind.id, schemaService.serverObjectsLoadState(for: connectionId, schema: schema, kindId: kind.id).erased) + }) ) } @@ -435,6 +473,9 @@ extension DatabaseTreeOutlineCoordinator { routines: shown(schemaService.routines(for: connectionId, schema: schema)), triggers: shown(schemaService.triggers(for: connectionId, schema: schema)), userTypes: shown(schemaService.userDefinedTypes(for: connectionId, schema: schema)), + serverObjects: Dictionary(uniqueKeysWithValues: containerServerKinds.map { kind in + (kind.id, shown(schemaService.serverObjects(for: connectionId, schema: schema, kindId: kind.id))) + }), searchText: searchText, database: browsingDatabase ) @@ -536,6 +577,30 @@ extension DatabaseTreeOutlineCoordinator { DatabaseTreeMetadataService.listsTablesPerSchema(PluginManager.shared.databaseGroupingStrategy(for: databaseType)) } + // A schema engine's database lists its schemas, then the kinds it declares for the whole database. + private func databaseLevelNodes(database: String) -> [DatabaseTreeNode] { + let schemas = schemaNodes(database: database) + let groups = databaseScopedGroupNodes(database: database) + guard !groups.isEmpty else { return schemas } + if schemas.count == 1, case .status(.empty) = schemas[0].kind { return groups } + return schemas + groups + } + + private func databaseScopedGroupNodes(database: String) -> [DatabaseTreeNode] { + let kinds = serverKinds(database: database, schema: nil) + guard !kinds.isEmpty else { return [] } + let phases = sideLoadStates(database: database, schema: nil) + return DatabaseTreeObjectGroupResolver.groups( + database: database, + schema: nil, + itemCounts: objectBuckets(database: database, schema: nil).itemCounts, + keeping: phases.failedKinds(among: Set(kinds.map { SidebarObjectKind.server($0) })), + declared: kinds + ) + .filter { $0.kind.category == .server } + .map { node(id: DatabaseTreeNode.containerObjectKindSectionId($0), kind: .containerObjectKindSection($0)) } + } + private func schemaNodes(database: String) -> [DatabaseTreeNode] { let parentId = DatabaseTreeNode.databaseId(database) switch service.schemaListState(connectionId: connectionId, database: database) { @@ -632,6 +697,14 @@ extension DatabaseTreeOutlineCoordinator { func shown(_ items: [Item], isCurrent: Bool) -> [Item] { showsOvertaken || isCurrent ? items : [] } + let serverObjects = Dictionary(uniqueKeysWithValues: serverKinds(database: database, schema: schema).map { kind in + (kind.id, shown( + service.serverObjects(connectionId: connectionId, database: database, schema: schema, kindId: kind.id), + isCurrent: service.isServerObjectsCurrent( + connectionId: connectionId, database: database, schema: schema, kindId: kind.id + ) + )) + }) return DatabaseTreeBucketSource( tables: tables, routines: shown( @@ -646,6 +719,7 @@ extension DatabaseTreeOutlineCoordinator { service.userDefinedTypes(connectionId: connectionId, database: database, schema: schema), isCurrent: service.isTypesCurrent(connectionId: connectionId, database: database, schema: schema) ), + serverObjects: serverObjects, searchText: searchText, database: database ) @@ -666,9 +740,7 @@ extension DatabaseTreeOutlineCoordinator { return objectBuckets(database: database, schema: schema) } - /// A fetch the engine never runs stays idle for good, and idle is not loaded: counting it - /// would hold every empty container on a spinner for a list that is never coming. So only the - /// kinds this engine declares take part in deciding between "empty" and "loading". + // Declared kinds only: an idle fetch the engine never runs would hold every empty container on a spinner. private func sideLoadStates(database: String, schema: String?) -> DatabaseTreeSidePhases { let declared = declaredObjectKinds return DatabaseTreeSidePhases( @@ -678,7 +750,12 @@ extension DatabaseTreeOutlineCoordinator { : nil, types: declared.contains(.type) ? service.typesLoadState(connectionId: connectionId, database: database, schema: schema).erased - : nil + : nil, + serverObjects: Dictionary(uniqueKeysWithValues: serverKinds(database: database, schema: schema).map { kind in + (kind.id, service.serverObjectsLoadState( + connectionId: connectionId, database: database, schema: schema, kindId: kind.id + ).erased) + }) ) } @@ -712,7 +789,8 @@ extension DatabaseTreeOutlineCoordinator { database: database, schema: schema, itemCounts: looseItemCounts(buckets.itemCounts, plan: plan), - keeping: plan.filedKinds.union(sidePhases.failedKinds(among: declaredObjectKinds)) + keeping: plan.filedKinds.union(sidePhases.failedKinds(among: declaredObjectKinds)), + declared: serverKinds(database: database, schema: schema) ) guard !groups.isEmpty || !folders.isEmpty else { return [statusNode(parentId: parentId, status: .emptyContainer(sideStates: sidePhases.all))] @@ -728,8 +806,7 @@ extension DatabaseTreeOutlineCoordinator { private func containerObjectNodes(for group: DatabaseTreeObjectGroup) -> [DatabaseTreeNode] { let buckets = objectBuckets(database: group.database, schema: group.schema) let emptyId = DatabaseTreeNode.containerObjectKindSectionId(group) - /// A schema-grouped group carries no database, and a reference naming an empty one would - /// switch the session to a database called "" the moment the row opened. + // An empty database name would switch the session to a database called "" the moment the row opened. let database: String? = group.database.isEmpty ? nil : group.database let placeholder = sidePhases(for: group).placeholder(for: group.kind).map { [statusNode(parentId: emptyId, status: $0)] @@ -756,6 +833,25 @@ extension DatabaseTreeOutlineCoordinator { let ref = DatabaseTreeUserTypeRef(database: database, schema: group.schema, type: type) return node(id: DatabaseTreeNode.userTypeId(ref), kind: .userType(ref)) } + case .server: + guard let serverKind = group.kind.serverKind else { return placeholder } + let objects = buckets.serverObjects[serverKind.id] ?? [] + guard !objects.isEmpty else { return placeholder } + // A `.database` kind's object keeps its own schema, which is not the group's. + let fixedSchema = group.schema + return serverObjectNodes(objects, kind: serverKind, database: database, schema: { fixedSchema ?? $0.schema }) + } + } + + private func serverObjectNodes( + _ objects: [ServerObjectInfo], + kind: SidebarServerObjectKind, + database: String?, + schema: (ServerObjectInfo) -> String? + ) -> [DatabaseTreeNode] { + objects.map { object in + let ref = DatabaseTreeServerObjectRef(database: database, schema: schema(object), kind: kind, object: object) + return node(id: DatabaseTreeNode.serverObjectId(ref), kind: .serverObject(ref)) } } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift index c5afbe0e81..7c40f92912 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift @@ -561,7 +561,14 @@ final class DatabaseTreeOutlineCoordinator: NSObject, NSTextFieldDelegate { private func refreshDatabase(_ database: String) { if supportsSchemaLevel { - Task { await service.refreshSchemas(connectionId: connectionId, database: database) } + let connectionId = connectionId + Task { + async let schemas: Void = service.refreshSchemas(connectionId: connectionId, database: database) + async let serverObjects: Void = service.refreshServerObjects( + connectionId: connectionId, database: database, schema: nil + ) + _ = await (schemas, serverObjects) + } } else { Task { await service.refreshObjects(connectionId: connectionId, database: database, schema: nil) } } @@ -648,8 +655,7 @@ final class DatabaseTreeOutlineCoordinator: NSObject, NSTextFieldDelegate { } } - /// A table kind is refreshed as a catalog change, as Cmd+R is, so the partition lists the tree - /// holds under the section are marked stale along with the browsed list. + // A table kind refreshes as a catalog change, as Cmd+R does, so the partition lists under it go stale too. internal func refreshObjectKind(_ kind: SidebarObjectKind) { guard let mainCoordinator else { return } switch kind { @@ -662,6 +668,18 @@ final class DatabaseTreeOutlineCoordinator: NSObject, NSTextFieldDelegate { database: mainCoordinator.browseDatabaseName, kinds: .tables ))) + case .server(let serverKind): + let connectionId = connectionId + let manager = DatabaseManager.shared + let scope = manager.browseScope(for: connectionId) + let kindId = serverKind.id + Task { [schemaService] in + try? await manager.withBrowseMetadataDriver(connectionId: connectionId) { driver in + _ = await schemaService.reloadServerObjects( + connectionId: connectionId, driver: driver, scope: scope, kindId: kindId + ) + } + } } } @@ -697,6 +715,13 @@ final class DatabaseTreeOutlineCoordinator: NSObject, NSTextFieldDelegate { database: group.database, schema: group.schema ) + case .server: + await service.refreshServerObjects( + connectionId: connectionId, + database: group.database, + schema: group.schema, + kindId: group.kind.serverKind?.id + ) } } } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeRenderInputs.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeRenderInputs.swift index 6dbbfc1048..b7c9f74179 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeRenderInputs.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeRenderInputs.swift @@ -10,6 +10,7 @@ import Foundation struct DatabaseTreeRenderInputs: Equatable { typealias DatabaseKey = DatabaseTreeMetadataService.DatabaseKey typealias ObjectsKey = DatabaseTreeMetadataService.ObjectsKey + typealias ServerObjectsKey = DatabaseTreeMetadataService.ServerObjectsKey struct Rows: Equatable { let phase: MetadataLoadPhase @@ -77,7 +78,9 @@ struct DatabaseTreeRenderInputs: Equatable { let routines: [ObjectsKey: Rows] let triggers: [ObjectsKey: Rows] let types: [ObjectsKey: Rows] + let serverObjects: [ServerObjectsKey: Rows] let currency: [ObjectsKey: Currency] + let serverObjectsCurrency: [ServerObjectsKey: Bool] let partitions: [DatabaseTreeMetadataService.PartitionsKey: Rows] let listings: [DatabaseKey: Listing] let listsRevision: Int @@ -87,6 +90,7 @@ struct DatabaseTreeRenderInputs: Equatable { let catalogRoutines: Rows let catalogTriggers: Rows let catalogTypes: Rows + let catalogServerObjects: [String: Rows] let temporaryTables: [TemporaryTableStore.Key: TemporaryTables] let sidebar: Sidebar? @@ -105,6 +109,15 @@ struct DatabaseTreeRenderInputs: Equatable { let routines = Self.slice(treeMetadata.routinesState, where: ownsObjects) { Rows($0) } let triggers = Self.slice(treeMetadata.triggersState, where: ownsObjects) { Rows($0) } let types = Self.slice(treeMetadata.typesState, where: ownsObjects) { Rows($0) } + let serverObjects = Self.slice(treeMetadata.serverObjectsState, where: { $0.connectionId == connectionId }) { + Rows($0) + } + var serverObjectsCurrency: [ServerObjectsKey: Bool] = [:] + for key in serverObjects.keys { + serverObjectsCurrency[key] = treeMetadata.isServerObjectsCurrent( + connectionId: connectionId, database: key.database, schema: key.schema, kindId: key.kindId + ) + } var currency: [ObjectsKey: Currency] = [:] for key in Set(tables.keys).union(routines.keys).union(triggers.keys).union(types.keys) { currency[key] = Currency( @@ -131,7 +144,9 @@ struct DatabaseTreeRenderInputs: Equatable { self.routines = routines self.triggers = triggers self.types = types + self.serverObjects = serverObjects self.currency = currency + self.serverObjectsCurrency = serverObjectsCurrency self.partitions = Self.slice(treeMetadata.partitionsState, where: { $0.connectionId == connectionId }) { Rows($0) } self.listings = listings self.listsRevision = treeMetadata.listsRevision(for: connectionId) @@ -140,6 +155,7 @@ struct DatabaseTreeRenderInputs: Equatable { self.catalogRoutines = Rows(schemaService.routinesLoadState(for: connectionId)) self.catalogTriggers = Rows(schemaService.triggersLoadState(for: connectionId)) self.catalogTypes = Rows(schemaService.userDefinedTypesLoadState(for: connectionId)) + self.catalogServerObjects = schemaService.serverObjectStates(for: connectionId).mapValues { Rows($0) } self.temporaryTables = Self.slice(temporaryTables.listings, where: { $0.connectionId == connectionId }) { TemporaryTables(tables: SameRows(rows: $0.tables), session: $0.session) } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeRowView.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeRowView.swift index ea0b5d864a..85da448667 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeRowView.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeRowView.swift @@ -125,6 +125,8 @@ struct DatabaseTreeRowView: View { TriggerRowView(trigger: ref.trigger) case .userType(let ref): UserTypeRowView(type: ref.type) + case .serverObject(let ref): + ServerObjectRowView(object: ref.object, kind: ref.kind) case .status(let status): statusRow(status) case .objectKindSection(let kind): diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeSelection.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeSelection.swift index 85fa896b1f..a85dc4ca84 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeSelection.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeSelection.swift @@ -18,7 +18,7 @@ internal enum DatabaseTreeSelection { case .status, .recentSection, .temporarySection, .foldersSection, .objectKindSection, .redisKeysSection: return false case .database, .schema, .hierarchicalSchemaSection, .table, .partition, .routine, .trigger, .userType, - .recentTable, .temporaryTable, .containerObjectKindSection, .tableFolder, .redisNode: + .serverObject, .recentTable, .temporaryTable, .containerObjectKindSection, .tableFolder, .redisNode: return true } } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeTypeSelect.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeTypeSelect.swift index 55f3ed21e8..2e1bd67a02 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeTypeSelect.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeTypeSelect.swift @@ -72,6 +72,8 @@ internal enum DatabaseTreeTypeSelect { return ref.trigger.name case .userType(let ref): return ref.type.name + case .serverObject(let ref): + return ref.object.name case .hierarchicalSchemaSection(let schema): return schema case .redisNode(let node): diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeView.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeView.swift index e1aaf317a4..01010cd35d 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeView.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeView.swift @@ -48,6 +48,21 @@ struct DatabaseTreeUserTypeRef: Identifiable, Equatable { } } +struct DatabaseTreeServerObjectRef: Identifiable, Equatable { + let database: String? + let schema: String? + let kind: SidebarServerObjectKind + let object: ServerObjectInfo + + var id: String { + IdentityPath.joined([database ?? "", schema ?? "", object.id], separator: "|") + } + + var objectRef: DatabaseObjectRef { + DatabaseObjectRef(serverObject: object, kind: kind, database: database ?? "") + } +} + struct DatabaseTreeView: View { @ObservedObject private var databaseManager = DatabaseManager.shared @ObservedObject private var treeService = DatabaseTreeMetadataService.shared diff --git a/apple/TablePro/Views/Sidebar/Menu/DatabaseTreeMenuSpec.swift b/apple/TablePro/Views/Sidebar/Menu/DatabaseTreeMenuSpec.swift index 39d1569256..3318610d63 100644 --- a/apple/TablePro/Views/Sidebar/Menu/DatabaseTreeMenuSpec.swift +++ b/apple/TablePro/Views/Sidebar/Menu/DatabaseTreeMenuSpec.swift @@ -7,12 +7,7 @@ import Foundation import TableProPluginKit internal enum DatabaseTreeMenuSpec { - /// A menu is a list of groups, and every group here is one intent: reach the object, note it, - /// move its data, change it. The HIG asks for a small number of items in about three groups and - /// for commands relevant to the clicked object only, so nothing connection-wide belongs on a - /// row. View Options used to be appended to every menu including every object row, and View ER - /// Diagram sat in the middle of a table row's clipboard and export commands; both now live on - /// the empty-area menu, where the thing they act on is the sidebar rather than an object. + // One intent per group, and only commands for the clicked object; connection-wide ones live on the empty-area menu. internal static func sections(for context: DatabaseTreeMenuContext) -> [DatabaseTreeMenuSection] { guard let clicked = context.clicked else { return backgroundSections(context) } switch clicked { @@ -46,6 +41,8 @@ internal enum DatabaseTreeMenuSpec { return triggerSections(ref) case .userType(let ref): return userTypeSections(ref, context: context) + case .serverObject(let ref): + return serverObjectSections(ref) case .objectKindSection(let kind): return objectKindSections(kind, context: context) case .containerObjectKindSection(let group): @@ -336,6 +333,14 @@ internal enum DatabaseTreeMenuSpec { ] } + // List plus DDL only: the row offers nothing that writes. + private static func serverObjectSections(_ ref: DatabaseTreeServerObjectRef) -> [DatabaseTreeMenuSection] { + [ + DatabaseTreeMenuSection([.command(String(localized: "Copy Name"), .copyText(ref.object.name))]), + DatabaseTreeMenuSection([.command(String(localized: "Show DDL"), .showObjectSource(ref.objectRef))]) + ] + } + private static func userTypeSections( _ ref: DatabaseTreeUserTypeRef, context: DatabaseTreeMenuContext diff --git a/apple/TablePro/Views/Sidebar/ServerObjectRowView.swift b/apple/TablePro/Views/Sidebar/ServerObjectRowView.swift new file mode 100644 index 0000000000..cd9b583400 --- /dev/null +++ b/apple/TablePro/Views/Sidebar/ServerObjectRowView.swift @@ -0,0 +1,79 @@ +// +// ServerObjectRowView.swift +// TablePro +// + +import SwiftUI + +enum ServerObjectRowLogic { + static func displayName(for object: ServerObjectInfo, kind: SidebarServerObjectKind) -> String { + kind.scope == .database ? object.name : IdentityPath.qualified(name: object.name, schema: object.schema) + } + + static func accessibilityLabel(for object: ServerObjectInfo, kind: SidebarServerObjectKind) -> String { + let base = String(format: String(localized: "%1$@: %2$@"), kind.displayName, object.name) + guard let detail = object.detail, !detail.isEmpty else { return base } + return base + ", " + detail + } + + // The driver states the object's state in its attributes, in its own words, so it is not repeated here. + static func tooltip(for object: ServerObjectInfo, kind: SidebarServerObjectKind) -> String { + var lines: [String] = [displayName(for: object, kind: kind)] + if let detail = object.detail, !detail.isEmpty { lines.append(detail) } + lines.append(contentsOf: object.attributes.map { "\($0.label): \($0.value)" }) + return lines.joined(separator: "\n") + } +} + +// Plain drawn values: `ServerObjectInfo` compares by id, so a refreshed version or state would read as unchanged. +struct ServerObjectRowView: View { + @ObservedObject private var settingsManager = AppSettingsManager.shared + let name: String + let detail: String? + let iconName: String + let tooltip: String + let accessibilityLabel: String + + init(name: String, detail: String?, iconName: String, tooltip: String, accessibilityLabel: String) { + self.name = name + self.detail = detail + self.iconName = iconName + self.tooltip = tooltip + self.accessibilityLabel = accessibilityLabel + } + + init(object: ServerObjectInfo, kind: SidebarServerObjectKind) { + self.init( + name: object.name, + detail: object.detail, + iconName: kind.iconName, + tooltip: ServerObjectRowLogic.tooltip(for: object, kind: kind), + accessibilityLabel: ServerObjectRowLogic.accessibilityLabel(for: object, kind: kind) + ) + } + + var body: some View { + Label { + HStack(spacing: 6) { + Text(name) + .lineLimit(1) + .truncationMode(.tail) + if let detail, !detail.isEmpty { + Text(detail) + .font(.caption) + .foregroundStyle(.secondary) + .lineLimit(1) + .truncationMode(.tail) + } + } + } icon: { + Image(systemName: iconName) + .selectionAwareTint(Color.accentColor) + .frame(width: 16) + } + .sidebarRowIcon(visible: settingsManager.general.showObjectIcons) + .accessibilityElement(children: .combine) + .accessibilityLabel(accessibilityLabel) + .help(tooltip) + } +} diff --git a/apple/TablePro/Views/Sidebar/SidebarPersistenceKey.swift b/apple/TablePro/Views/Sidebar/SidebarPersistenceKey.swift index 98ac303f63..17d3e1d637 100644 --- a/apple/TablePro/Views/Sidebar/SidebarPersistenceKey.swift +++ b/apple/TablePro/Views/Sidebar/SidebarPersistenceKey.swift @@ -42,12 +42,17 @@ enum SidebarPersistenceKey { "sidebar.layout.\(connectionId.uuidString)" } + // Built-in kinds only; a declared kind's state lives under `serverKindsExpanded`. static func expanded(connectionId: UUID, kind: SidebarObjectKind) -> String { - "sidebar.\(connectionId.uuidString).\(kind.rawValue).expanded" + "sidebar.\(connectionId.uuidString).\(kind.persistenceName).expanded" } - /// Every per-connection key this type can produce. A key added above and forgotten here outlives - /// the connection it belongs to for the life of the install. + // One key for every declared kind, since their ids are open-ended and `all` has to name each key. + static func serverKindsExpanded(connectionId: UUID) -> String { + "sidebar.\(connectionId.uuidString).serverKinds.expanded" + } + + // A key missing here outlives its connection for the life of the install. static func all(connectionId: UUID) -> [String] { [ tablesExpanded(connectionId: connectionId), @@ -58,8 +63,9 @@ enum SidebarPersistenceKey { selectedTab(connectionId: connectionId), selectedFavorite(connectionId: connectionId), favoriteDatabaseEnvironmentFilter(connectionId: connectionId), - layout(connectionId: connectionId) - ] + SidebarObjectKind.allCases.map { expanded(connectionId: connectionId, kind: $0) } + layout(connectionId: connectionId), + serverKindsExpanded(connectionId: connectionId) + ] + SidebarObjectKind.builtIn.map { expanded(connectionId: connectionId, kind: $0) } } static func removeAll( diff --git a/apple/TablePro/Views/Sidebar/SidebarView.swift b/apple/TablePro/Views/Sidebar/SidebarView.swift index ec55eab1ab..df0d5d2914 100644 --- a/apple/TablePro/Views/Sidebar/SidebarView.swift +++ b/apple/TablePro/Views/Sidebar/SidebarView.swift @@ -47,7 +47,8 @@ struct SidebarView: View { } private var hasAnyMatch: Bool { - SidebarObjectKind.allCases.contains { kind in + let declared = SidebarServerObjectKind.declared(by: DatabaseManager.shared.driver(for: connectionId)) + return SidebarObjectKind.ordered(declared: declared).contains { kind in countFor(kind: kind) > 0 } || !otherSchemaMatches.isEmpty || !attachedSchemaMatches.isEmpty || hasTemporaryMatch } @@ -493,6 +494,12 @@ struct SidebarView: View { case .routine: return viewModel.filteredRoutines(of: kind, from: routines).count case .trigger: return viewModel.filteredTriggers(from: triggers).count case .type: return viewModel.filteredUserTypes(from: userDefinedTypes).count + case .server: + guard let serverKind = kind.serverKind else { return 0 } + return viewModel.filteredServerObjects( + kindId: serverKind.id, + from: schemaService.serverObjects(for: connectionId, kindId: serverKind.id) + ).count } } } diff --git a/apple/TablePro/Views/Structure/StructureRebuildPlanRunner.swift b/apple/TablePro/Views/Structure/StructureRebuildPlanRunner.swift index 4524bc9ac7..8eaa7cd55f 100644 --- a/apple/TablePro/Views/Structure/StructureRebuildPlanRunner.swift +++ b/apple/TablePro/Views/Structure/StructureRebuildPlanRunner.swift @@ -146,14 +146,12 @@ enum StructureRebuildPlanRunner { throw RunError.schemaChanged } - /// Only the transaction this plan opened is ever rolled back, and none is opened over a - /// transaction the session already holds: on the same shared session an app-owned - /// `COMMIT` commits the user's pending work, and MySQL's `START TRANSACTION` commits it - /// implicitly. A plan that cannot open one falls to its own compensation statements, - /// which is what an engine whose DDL commits as it runs already relies on. + // Never opens a transaction over one the session holds; a plan that cannot open one falls to its compensation statements. + // `.automatic`: the epilogue runs inside the transaction, where SQLite ignores `PRAGMA foreign_keys = on`. let owner = WriteTransactionOwner.resolve( supportsTransactions: driver.supportsTransactions, - sessionState: await driver.heldSessionTransactionState() + sessionState: await driver.heldSessionTransactionState(), + commitMode: .automatic ) // Inside a transaction SQLite ignores `PRAGMA foreign_keys = off`, so the rebuild's DROP TABLE cascades. if !plan.prologue.isEmpty, plan.isTransactional, owner == .session { @@ -164,7 +162,7 @@ enum StructureRebuildPlanRunner { _ = try? await driver.execute(query: sql) } - let usesTransaction = plan.isTransactional && owner.opensTransaction + let usesTransaction = plan.isTransactional && owner.sendsBegin && owner.commitsAtEnd if usesTransaction { try await driver.beginTransaction(mode: .readWrite) } diff --git a/apple/TablePro/Views/VersionHistory/VersionHistoryFactory.swift b/apple/TablePro/Views/VersionHistory/VersionHistoryFactory.swift index cd08b4b162..17070e411d 100644 --- a/apple/TablePro/Views/VersionHistory/VersionHistoryFactory.swift +++ b/apple/TablePro/Views/VersionHistory/VersionHistoryFactory.swift @@ -36,6 +36,15 @@ internal enum VersionHistoryFactory { confirmButton: String(localized: "Restore") ) } + case .queryTab(let id): + viewModel = VersionHistoryViewModel( + subject: subject, + provider: MainContentCoordinator.hostedQueryTabVersionProvider(for: id), + refreshSignal: AppEvents.shared.queryTabVersionsDidUpdate + .filter { $0 == nil || $0 == id } + .map { _ in () } + .eraseToAnyPublisher() + ) } viewModel.reportRestoreFailure = { message in AlertHelper.showErrorSheet( diff --git a/apple/TablePro/Views/VersionHistory/VersionHistoryFormatting.swift b/apple/TablePro/Views/VersionHistory/VersionHistoryFormatting.swift index 4663c05bcc..d1855d56b7 100644 --- a/apple/TablePro/Views/VersionHistory/VersionHistoryFormatting.swift +++ b/apple/TablePro/Views/VersionHistory/VersionHistoryFormatting.swift @@ -71,9 +71,22 @@ internal enum VersionHistoryFormatting { return String(format: String(localized: "Keeps the last %d versions on this Mac."), count) case .showsLatestCommits(let count): return String(format: String(localized: "Showing the %d most recent commits."), count) + case .keepsRecentVersions(let count, let days): + return String( + format: String(localized: "Keeps up to %1$d versions from the last %2$d days on this Mac."), + count, + days + ) } } + // The Revert To menu lists only past versions, so each row leads with its date. + static func menuTitle(for entry: VersionHistoryEntry) -> String { + guard let date = dateText(for: entry) else { return title(for: entry) } + guard let summary = entry.summary, !summary.isBlank else { return date } + return String(format: String(localized: "%1$@ (%2$@)"), date, summary) + } + private static func dateText(for entry: VersionHistoryEntry) -> String? { entry.date?.formatted(date: .abbreviated, time: .shortened) } diff --git a/apple/TableProTests/Core/DataGrid/GridViewportResolverTests.swift b/apple/TableProTests/Core/DataGrid/GridViewportResolverTests.swift index 13773fdddc..f14023d787 100644 --- a/apple/TableProTests/Core/DataGrid/GridViewportResolverTests.swift +++ b/apple/TableProTests/Core/DataGrid/GridViewportResolverTests.swift @@ -28,18 +28,53 @@ struct GridViewportResolverTests { selectedRows: [Int] = [], keyColumns: [String] = keyColumns, isCellModified: (RowID, Int) -> Bool = { _, _ in false } + ) -> GridViewportSnapshot { + let order = DataGridRowOrder(filteredIDs: displayIDs, revision: 0) + let selection = selectedRows.isEmpty + ? GridSelectionSnapshot.empty + : GridSelectionSnapshot( + rowIDs: selectedRows.compactMap { order.rowID(at: $0, in: tableRows) }, + rectangles: [], + activeCell: nil, + anchor: nil + ) + return snapshot( + of: tableRows, + displayIDs: displayIDs, + firstVisibleRow: firstVisibleRow, + offset: offset, + selection: selection, + keyColumns: keyColumns, + isCellModified: isCellModified + ) + } + + private static func snapshot( + of tableRows: TableRows, + displayIDs: [RowID]? = nil, + firstVisibleRow: Int, + offset: CGFloat = 0, + selection: GridSelectionSnapshot, + keyColumns: [String] = keyColumns, + isCellModified: (RowID, Int) -> Bool = { _, _ in false } ) -> GridViewportSnapshot { GridViewportResolver.snapshot( of: tableRows, displayIDs: displayIDs, firstVisibleDisplayRow: firstVisibleRow, firstVisibleOffset: offset, - selectedDisplayRows: selectedRows, + selection: selection, keyColumns: keyColumns, isCellModified: isCellModified ) } + private static let nameColumn = DataGridColumnID(name: "name", occurrence: 0) + + private static func cell(_ row: Int) -> GridSelectionSnapshot.Cell { + GridSelectionSnapshot.Cell(rowID: .existing(row), columnID: nameColumn) + } + private static func placement( _ intent: GridReloadIntent, from snapshot: GridViewportSnapshot, @@ -259,6 +294,49 @@ struct GridViewportResolverTests { #expect(Self.placement(.firstRow, from: snapshot, in: Self.rows(ids: [7, 7, 8])).selectedRows.isEmpty) } + @Test("A re-read keeps a cell cursor a cursor, on its own column, wherever its row lands") + func keepPlaceKeepsTheCursorAndItsColumn() { + var cursor = GridSelectionSnapshot(rowIDs: [.existing(2)], rectangles: [], activeCell: Self.cell(2), anchor: Self.cell(2)) + cursor.mode = .cursor + let snapshot = Self.snapshot(of: Self.rows(ids: Array(1 ... 10)), firstVisibleRow: 0, selection: cursor) + + let placement = Self.placement(.keepPlace, from: snapshot, in: Self.rows(ids: Array((1 ... 10).reversed()))) + + #expect(placement.selection.mode == .cursor) + #expect(placement.selection.rowIDs == [.existing(7)]) + #expect(placement.selection.activeCell == Self.cell(7)) + } + + @Test("A re-read keeps a cell range's columns rather than widening it to whole rows") + func keepPlaceKeepsACellRange() { + let idColumn = DataGridColumnID(name: "id", occurrence: 0) + let range = GridSelectionSnapshot( + rowIDs: [.existing(3), .existing(4)], + rectangles: [GridSelectionSnapshot.Rectangle(rowIDs: [.existing(3), .existing(4)], columnIDs: [Self.nameColumn])], + activeCell: Self.cell(3), + anchor: Self.cell(3) + ) + let snapshot = Self.snapshot(of: Self.rows(ids: Array(1 ... 10)), firstVisibleRow: 0, selection: range) + + let placement = Self.placement(.keepPlace, from: snapshot, in: Self.rows(ids: Array(0 ... 10))) + + #expect(placement.selection.mode == .cells) + #expect(placement.selection.rectangles == [ + GridSelectionSnapshot.Rectangle(rowIDs: [.existing(4), .existing(5)], columnIDs: [Self.nameColumn]) + ]) + #expect(placement.selection.activeCell == Self.cell(4)) + #expect(!placement.selection.rectangles.contains { $0.columnIDs.contains(idColumn) }) + } + + @Test("A cursor whose row is gone leaves nothing selected") + func cursorOnAGoneRowSelectsNothing() { + var cursor = GridSelectionSnapshot(rowIDs: [.existing(5)], rectangles: [], activeCell: Self.cell(5), anchor: Self.cell(5)) + cursor.mode = .cursor + let snapshot = Self.snapshot(of: Self.rows(ids: Array(1 ... 10)), firstVisibleRow: 0, selection: cursor) + + #expect(Self.placement(.keepPlace, from: snapshot, in: Self.rows(ids: [1, 2, 3])).selection == .empty) + } + @Test("Without a key the position is read through the incoming display order") func keepPlaceFallbackUsesTheDisplayOrder() { let snapshot = Self.snapshot(of: Self.rows(ids: Array(1 ... 100)), firstVisibleRow: 2, offset: 4, keyColumns: []) diff --git a/apple/TableProTests/Core/DataWrite/DataWriteExecutorLookupTests.swift b/apple/TableProTests/Core/DataWrite/DataWriteExecutorLookupTests.swift index cc8ad67a43..853f9acf5e 100644 --- a/apple/TableProTests/Core/DataWrite/DataWriteExecutorLookupTests.swift +++ b/apple/TableProTests/Core/DataWrite/DataWriteExecutorLookupTests.swift @@ -83,7 +83,7 @@ struct DataWriteExecutorLookupTests { steps: steps ) let driver = PluginDriverAdapter(connection: TestFixtures.makeConnection(type: .clickhouse), pluginDriver: lookup) - _ = try await DataWriteExecutor.run(plan, on: driver) + _ = try await DataWriteExecutor.run(plan, commitMode: .automatic, on: driver) } @Test("A write runs on the rows its lookup found, their values bound after its own") diff --git a/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift b/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift index 5234fead9b..7a23eb604f 100644 --- a/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift +++ b/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift @@ -114,7 +114,7 @@ struct DataWriteExecutorTests { @Test("The driver's real affected-row count reaches the caller") func reportsRealRowCount() async throws { let counting = CountingDriver(affectedRows: 42) - let results = try await DataWriteExecutor.run(plan(expectedRowCount: 50), on: driver(counting)).results + let results = try await DataWriteExecutor.run(plan(expectedRowCount: 50), commitMode: .automatic, on: driver(counting)).results #expect(results.first?.rowsAffected == 42) #expect(counting.didCommit) @@ -124,7 +124,7 @@ struct DataWriteExecutorTests { func tooManyRowsRollsBack() async throws { let counting = CountingDriver(affectedRows: 2) await #expect(throws: DataWriteError.tooManyRowsAffected(table: "t", expected: 1, actual: 2)) { - try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)) + try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)) } #expect(counting.didRollBack) #expect(counting.didCommit == false) @@ -136,7 +136,7 @@ struct DataWriteExecutorTests { await #expect( throws: DataWriteError.tooManyRowsAffectedUnrecoverable(table: "t", expected: 1, actual: 2) ) { - try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)) + try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)) } #expect(counting.didRollBack == false) } @@ -144,7 +144,7 @@ struct DataWriteExecutorTests { @Test("Fewer rows than expected is an ordinary save, because MySQL reports zero for an unchanged value") func fewerRowsIsNotAFailure() async throws { let counting = CountingDriver(affectedRows: 0) - let results = try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)).results + let results = try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)).results #expect(results.first?.rowsAffected == 0) #expect(counting.didCommit) @@ -157,7 +157,7 @@ struct DataWriteExecutorTests { await #expect(throws: DataWriteError.rowsNoLongerMatch(table: "t", expected: 1, actual: 0)) { try await DataWriteExecutor.run( plan(expectedRowCount: 1, matchesRowsWithoutKey: true), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) } #expect(counting.didRollBack) @@ -171,7 +171,7 @@ struct DataWriteExecutorTests { await #expect(throws: DataWriteError.keyNoLongerMatchesOneRow(table: "t", expected: 1, actual: 0)) { try await DataWriteExecutor.run( plan(expectedRowCount: 1, refusesSharedKey: true), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) } #expect(counting.didRollBack) @@ -184,7 +184,7 @@ struct DataWriteExecutorTests { await #expect(throws: DataWriteError.rowsNoLongerMatch(table: "t", expected: 3, actual: 2)) { try await DataWriteExecutor.run( plan(expectedRowCount: 3, matchesRowsWithoutKey: true), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) } #expect(counting.didRollBack) @@ -196,7 +196,7 @@ struct DataWriteExecutorTests { await #expect(throws: DataWriteError.rowsNoLongerMatch(table: "t", expected: 1, actual: 0)) { try await DataWriteExecutor.run( plan(expectedRowCount: 1, matchesRowsWithoutKey: true, databaseType: .kingbasees), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) } #expect(counting.didRollBack) @@ -207,7 +207,7 @@ struct DataWriteExecutorTests { let counting = CountingDriver(affectedRows: 0) let results = try await DataWriteExecutor.run( plan(expectedRowCount: 1, matchesRowsWithoutKey: true, databaseType: .clickhouse), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ).results #expect(results.first?.rowsAffected == 0) @@ -224,7 +224,7 @@ struct DataWriteExecutorTests { prologue: ["PRAGMA foreign_keys = OFF"], epilogue: ["PRAGMA foreign_keys = ON"] ), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) #expect(counting.trace == [ @@ -248,7 +248,7 @@ struct DataWriteExecutorTests { prologue: ["PRAGMA foreign_keys = OFF"], epilogue: ["PRAGMA foreign_keys = ON"] ), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) } @@ -269,7 +269,7 @@ struct DataWriteExecutorTests { prologue: ["SET session_replication_role = replica"], epilogue: ["SET session_replication_role = origin"] ), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) #expect(!counting.trace.contains("open preserve")) @@ -279,7 +279,7 @@ struct DataWriteExecutorTests { @Test("A plan without a prologue leaves the session's settings alone") func planWithoutPrologueOpensNoScope() async throws { let counting = CountingDriver(affectedRows: 1) - _ = try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)) + _ = try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)) #expect(counting.trace == ["BEGIN", "UPDATE \"t\" SET \"b\" = 0", "COMMIT"]) } @@ -296,7 +296,7 @@ struct DataWriteExecutorTests { prologueIsRequired: true ) await #expect(throws: (any Error).self) { - try await DataWriteExecutor.run(required, on: driver(counting)) + try await DataWriteExecutor.run(required, commitMode: .automatic, on: driver(counting)) } #expect(counting.trace == [ @@ -312,7 +312,7 @@ struct DataWriteExecutorTests { func anOptionalPrologueThatFailsLetsTheStepsRun() async throws { let counting = CountingDriver(affectedRows: 1, failOnStatement: 1) _ = try await DataWriteExecutor.run( - plan(expectedRowCount: 1, prologue: ["PRAGMA foreign_keys = OFF"]), on: driver(counting) + plan(expectedRowCount: 1, prologue: ["PRAGMA foreign_keys = OFF"]), commitMode: .automatic, on: driver(counting) ) #expect(counting.didCommit) @@ -323,7 +323,7 @@ struct DataWriteExecutorTests { let counting = CountingDriver(affectedRows: 1) let run = try await DataWriteExecutor.run( plan(expectedRowCount: 1, prologue: ["A"], epilogue: ["B"]), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) #expect(run.sideStatements == ["A", "B"]) @@ -335,7 +335,7 @@ struct DataWriteExecutorTests { do { _ = try await DataWriteExecutor.run( - plan(expectedRowCount: 1, statementCount: 3), on: driver(counting) + plan(expectedRowCount: 1, statementCount: 3), commitMode: .automatic, on: driver(counting) ) Issue.record("expected the run to throw") } catch let error as DataWritePartialCommitError { @@ -350,7 +350,7 @@ struct DataWriteExecutorTests { do { _ = try await DataWriteExecutor.run( - plan(expectedRowCount: 1, statementCount: 3), on: driver(counting) + plan(expectedRowCount: 1, statementCount: 3), commitMode: .automatic, on: driver(counting) ) Issue.record("expected the run to throw") } catch is DataWritePartialCommitError { @@ -368,7 +368,7 @@ struct DataWriteExecutorTests { do { _ = try await DataWriteExecutor.run( - plan(expectedRowCount: 1, statementCount: 3), on: driver(counting) + plan(expectedRowCount: 1, statementCount: 3), commitMode: .automatic, on: driver(counting) ) Issue.record("expected the run to throw") } catch let error as DataWritePartialCommitError { @@ -379,7 +379,7 @@ struct DataWriteExecutorTests { @Test("A statement whose row count carries no meaning is not held to one") func unverifiedStepIsNotChecked() async throws { let counting = CountingDriver(affectedRows: 9_999) - let results = try await DataWriteExecutor.run(plan(expectedRowCount: nil), on: driver(counting)).results + let results = try await DataWriteExecutor.run(plan(expectedRowCount: nil), commitMode: .automatic, on: driver(counting)).results #expect(results.first?.wasVerified == false) #expect(counting.didCommit) @@ -391,7 +391,7 @@ struct DataWriteExecutorTests { ) func saveJoinsTheSessionTransaction(state: PluginSessionTransactionState) async throws { let counting = CountingDriver(affectedRows: 1, sessionState: state) - let results = try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)).results + let results = try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)).results #expect(results.first?.rowsAffected == 1) #expect(counting.trace == ["UPDATE \"t\" SET \"b\" = 0"]) @@ -401,7 +401,7 @@ struct DataWriteExecutorTests { func saveWrapsWhenNothingIsOpen() async throws { for state in [PluginSessionTransactionState.idle, .unknown] { let counting = CountingDriver(affectedRows: 1, sessionState: state) - _ = try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)) + _ = try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)) #expect(counting.trace.first == "BEGIN") #expect(counting.didCommit) } @@ -413,7 +413,7 @@ struct DataWriteExecutorTests { do { _ = try await DataWriteExecutor.run( - plan(expectedRowCount: 1, statementCount: 3), on: driver(counting) + plan(expectedRowCount: 1, statementCount: 3), commitMode: .automatic, on: driver(counting) ) Issue.record("expected the run to throw") } catch let error as DataWritePartialCommitError { @@ -430,7 +430,7 @@ struct DataWriteExecutorTests { await #expect( throws: DataWriteError.tooManyRowsAffectedInSessionTransaction(table: "t", expected: 1, actual: 2) ) { - try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)) + try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)) } #expect(counting.didRollBack == false) } @@ -442,10 +442,12 @@ struct WriteTransactionOwnerTests { for state in [ PluginSessionTransactionState.idle, .inTransaction, .abortedTransaction, .holdsSessionLocks, .unknown, ] { - let owner = WriteTransactionOwner.resolve(supportsTransactions: false, sessionState: state) - #expect(owner == WriteTransactionOwner.none) - #expect(owner.opensTransaction == false) - #expect(owner.canRollBack == false) + for mode in ConnectionCommitMode.allCases { + let owner = WriteTransactionOwner.resolve(supportsTransactions: false, sessionState: state, commitMode: mode) + #expect(owner == WriteTransactionOwner.none) + #expect(owner.sendsBegin == false) + #expect(owner.canRollBack == false) + } } } @@ -454,19 +456,155 @@ struct WriteTransactionOwnerTests { arguments: [PluginSessionTransactionState.inTransaction, .abortedTransaction, .holdsSessionLocks] ) func sessionOwnsWhatItHolds(state: PluginSessionTransactionState) { - let owner = WriteTransactionOwner.resolve(supportsTransactions: true, sessionState: state) - #expect(owner == .session) - #expect(owner.opensTransaction == false) - #expect(owner.canRollBack == false) + for mode in ConnectionCommitMode.allCases { + let owner = WriteTransactionOwner.resolve(supportsTransactions: true, sessionState: state, commitMode: mode) + #expect(owner == .session) + #expect(owner.sendsBegin == false) + #expect(owner.canRollBack == false) + } } @Test("The app owns the transaction when nothing is open, and when the driver cannot say") func appOwnsTheRest() { for state in [PluginSessionTransactionState.idle, .unknown] { - let owner = WriteTransactionOwner.resolve(supportsTransactions: true, sessionState: state) + let owner = WriteTransactionOwner.resolve(supportsTransactions: true, sessionState: state, commitMode: .automatic) #expect(owner == .app) - #expect(owner.opensTransaction) + #expect(owner.sendsBegin) + #expect(owner.commitsAtEnd) #expect(owner.canRollBack) } } + + @Test("Manual Commit holds an idle session's write: BEGIN, no COMMIT, a rollback on failure") + func manualCommitHoldsAnIdleSession() { + let owner = WriteTransactionOwner.resolve(supportsTransactions: true, sessionState: .idle, commitMode: .manual) + #expect(owner == .held) + #expect(owner.sendsBegin) + #expect(owner.commitsAtEnd == false) + #expect(owner.canRollBack) + #expect(WriteTransactionOwner.resolve(supportsTransactions: true, sessionState: .unknown, commitMode: .manual) == .app) + } +} + +struct DataWriteExecutorHeldTests { + private func plan(prologue: [String] = []) -> DataWritePlan { + DataWritePlan( + scope: DatabaseScope(connectionId: UUID(), database: "shop", schema: nil), + databaseType: .sqlite, + steps: [ + DataWriteStep( + kind: .rowWrite, + statement: ParameterizedStatement(sql: "UPDATE \"t\" SET \"b\" = 1", parameters: []), + expectedRowCount: 1, + tableName: "t" + ), + ], + prologue: prologue + ) + } + + private func driver(_ counting: CountingDriver) -> DatabaseDriver { + PluginDriverAdapter(connection: TestFixtures.makeConnection(type: .sqlite), pluginDriver: counting) + } + + @Test("A held save sends BEGIN and the write, and leaves the transaction open") + func heldSaveLeavesTheTransactionOpen() async throws { + let counting = CountingDriver(affectedRows: 1) + _ = try await DataWriteExecutor.run(plan(), commitMode: .manual, on: driver(counting)) + #expect(counting.trace == ["BEGIN", "UPDATE \"t\" SET \"b\" = 1"]) + } + + @Test("A held save that fails rolls back the transaction it opened") + func heldSaveRollsBackAFailure() async { + let counting = CountingDriver(affectedRows: 1, failOnStatement: 1) + await #expect(throws: (any Error).self) { + try await DataWriteExecutor.run(plan(), commitMode: .manual, on: driver(counting)) + } + #expect(counting.trace.first == "BEGIN") + #expect(counting.didRollBack) + #expect(counting.didCommit == false) + } + + @Test("A save with session toggles is refused in Manual Commit before anything runs") + func toggledSaveIsRefused() async { + let counting = CountingDriver(affectedRows: 1) + await #expect(throws: ManualCommitSessionOptionsRefusal()) { + try await DataWriteExecutor.run( + plan(prologue: ["PRAGMA foreign_keys = OFF"]), commitMode: .manual, on: driver(counting) + ) + } + #expect(counting.trace.isEmpty) + } + + @Test( + "A save the session can't hold is refused in Manual Commit before anything runs", + arguments: [PluginSessionTransactionState.unknown, .holdsSessionLocks] + ) + func unholdableSaveIsRefused(state: PluginSessionTransactionState) async { + let counting = CountingDriver(affectedRows: 1, sessionState: state) + await #expect(throws: ManualCommitRefusal()) { + try await DataWriteExecutor.run(plan(), commitMode: .manual, on: driver(counting)) + } + #expect(counting.trace.isEmpty) + } + + @Test("A save on an engine without transactions is refused in Manual Commit") + func saveWithoutTransactionsIsRefused() async { + let counting = CountingDriver(affectedRows: 1, transactional: false) + await #expect(throws: ManualCommitRefusal()) { + try await DataWriteExecutor.run(plan(), commitMode: .manual, on: driver(counting)) + } + #expect(counting.trace.isEmpty) + } + + // MySQL commits the cell edit implicitly before the TRUNCATE runs. + @Test("A save mixing a cell edit with a staged TRUNCATE on MySQL is refused in Manual Commit") + func mixedImplicitCommitSaveIsRefused() async { + let counting = CountingDriver(affectedRows: 1) + let mixed = DataWritePlan( + scope: DatabaseScope(connectionId: UUID(), database: "shop", schema: nil), + databaseType: .mysql, + steps: [ + DataWriteStep( + kind: .rowWrite, + statement: ParameterizedStatement(sql: "UPDATE `orders` SET `b` = 1", parameters: []), + expectedRowCount: 1, + tableName: "orders" + ), + DataWriteStep( + kind: .rowWrite, + statement: ParameterizedStatement(sql: "TRUNCATE TABLE `audit_log`", parameters: []), + tableName: "audit_log" + ), + ] + ) + await #expect(throws: ManualCommitMixedRefusal()) { + try await DataWriteExecutor.run(mixed, commitMode: .manual, on: driver(counting)) + } + #expect(counting.trace.isEmpty) + } + + @Test("A held save marks the turn's hold, and a rolled-back one clears it") + func heldSaveMarksTheTurn() async throws { + let held = ManualCommitTurnHold() + _ = try await ManualCommit.$turnHold.withValue(held) { + try await DataWriteExecutor.run(plan(), commitMode: .manual, on: driver(CountingDriver(affectedRows: 1))) + } + #expect(held.isHeld) + + let failed = ManualCommitTurnHold() + await ManualCommit.$turnHold.withValue(failed) { + _ = try? await DataWriteExecutor.run( + plan(), commitMode: .manual, on: driver(CountingDriver(affectedRows: 1, failOnStatement: 1)) + ) + } + #expect(!failed.isHeld) + } + + @Test("Manual Commit joins a transaction already open and sends no BEGIN") + func heldSaveJoinsAnOpenTransaction() async throws { + let counting = CountingDriver(affectedRows: 1, sessionState: .inTransaction) + _ = try await DataWriteExecutor.run(plan(), commitMode: .manual, on: driver(counting)) + #expect(counting.trace == ["UPDATE \"t\" SET \"b\" = 1"]) + } } diff --git a/apple/TableProTests/Core/Database/DatabaseAccessBridgeScriptTests.swift b/apple/TableProTests/Core/Database/DatabaseAccessBridgeScriptTests.swift index 6b6d12a5d8..2d630470e2 100644 --- a/apple/TableProTests/Core/Database/DatabaseAccessBridgeScriptTests.swift +++ b/apple/TableProTests/Core/Database/DatabaseAccessBridgeScriptTests.swift @@ -53,12 +53,14 @@ struct DatabaseAccessBridgeScriptTests { private func makeDriver( sendsBatchesWhole: Bool = true, transactionState: PluginSessionTransactionState = .idle, + transactionStateAfterBatches: PluginSessionTransactionState? = nil, answer: @escaping @Sendable (String) -> QueryBatchResult = { _ in .empty } ) -> ScriptAnsweringDriver { ScriptAnsweringDriver( connection: TestFixtures.makeConnection(database: "warehouse", type: .mssql), sendsBatchesWhole: sendsBatchesWhole, transactionState: transactionState, + transactionStateAfterBatches: transactionStateAfterBatches, answer: answer ) } @@ -134,7 +136,8 @@ struct DatabaseAccessBridgeScriptTests { @Test("Rows the script changed and a transaction it left open are reported on the first result") func scriptTotalsAndNoticesAreReported() async throws { let notice = try #require(PluginSessionTransactionState.inTransaction.openTransactionNotice) - let driver = makeDriver(transactionState: .inTransaction) { _ in + // Idle before the script, as a session is; a BEGIN TRAN over an open transaction is refused. + let driver = makeDriver(transactionStateAfterBatches: .inTransaction) { _ in ScriptAnsweringDriver.batch( [ScriptAnsweringDriver.resultSet(columns: ["id"], rows: [["1"]], isTruncated: true)], rowsAffected: 3 diff --git a/apple/TableProTests/Core/Database/SchemaChangeHeldTransactionTests.swift b/apple/TableProTests/Core/Database/SchemaChangeHeldTransactionTests.swift new file mode 100644 index 0000000000..e3b682aabd --- /dev/null +++ b/apple/TableProTests/Core/Database/SchemaChangeHeldTransactionTests.swift @@ -0,0 +1,162 @@ +// +// SchemaChangeHeldTransactionTests.swift +// TableProTests +// +// On the session driver an app-owned BEGIN joins what a tab left open and its COMMIT ends it, so a +// schema change there runs inside that transaction. On a pooled connection it can wait on the +// transaction's locks, so it asks first. +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +private final class HeldDDLDriver: PluginDriverStub, @unchecked Sendable { + private let lock = NSLock() + private var current: PluginSessionTransactionState = .idle + private var sent: [String] = [] + + var supportsSchemas: Bool { false } + var currentSchema: String? { nil } + var serverVersion: String? { nil } + var supportsTransactions: Bool { true } + var supportsTransactionalDDL: Bool { true } + + var state: PluginSessionTransactionState { + get { lock.withLock { current } } + set { lock.withLock { current = newValue } } + } + + var statements: [String] { lock.withLock { sent } } + + func execute(query: String) async throws -> PluginQueryResult { + lock.withLock { + sent.append(query) + switch query.uppercased() { + case "BEGIN": current = .inTransaction + case "COMMIT", "ROLLBACK": current = .idle + default: break + } + } + return .empty + } + + func sessionTransactionState() async -> PluginSessionTransactionState { state } + + func switchDatabase(to database: String) async throws {} +} + +@Suite("Schema changes beside an open transaction", .serialized) +@MainActor +struct SchemaChangeHeldTransactionTests { + private static let first = "ALTER TABLE orders ADD COLUMN a INT;" + private static let second = "ALTER TABLE orders ADD COLUMN b INT;" + + private static func injectSession(type: DatabaseType) -> (DatabaseConnection, HeldDDLDriver, DatabaseDriver) { + let connection = TestFixtures.makeConnection(database: "orders", type: type) + let driver = HeldDDLDriver() + let adapter = PluginDriverAdapter(connection: connection, pluginDriver: driver) + var session = ConnectionSession(connection: connection, driver: adapter) + session.browseDatabase = "orders" + session.status = .connected + DatabaseManager.shared.injectSession(session, for: connection.id) + return (connection, driver, adapter) + } + + private static func tearDown(_ connection: DatabaseConnection) { + MetadataConnectionPool.shared.closeAll(connectionId: connection.id) + DatabaseManager.shared.removeSession(for: connection.id) + } + + private static func scope(_ connection: DatabaseConnection) -> DatabaseScope { + DatabaseScope(connectionId: connection.id, database: "orders", schema: nil) + } + + private static func script() -> SchemaChangeScript { + SchemaChangeScript( + tableName: "orders", + statements: [first, second].map { SchemaStatement(sql: $0, description: $0, isDestructive: false) }, + operations: [], + review: PluginSchemaChangeReview() + ) + } + + @Test("On the session driver a Structure save inside an open transaction joins it") + func sessionRouteSaveJoinsTheOpenTransaction() async throws { + let (connection, driver, _) = Self.injectSession(type: .duckdb) + defer { Self.tearDown(connection) } + driver.state = .inTransaction + + try await DatabaseManager.shared.executeSchemaChanges( + Self.script(), databaseType: .duckdb, scope: Self.scope(connection), gate: AlwaysAllowGate() + ) + + #expect(driver.statements == [Self.first, Self.second]) + #expect(driver.state == .inTransaction) + } + + @Test("On the session driver an idle Structure save keeps its own transaction") + func sessionRouteIdleSaveWraps() async throws { + let (connection, driver, _) = Self.injectSession(type: .duckdb) + defer { Self.tearDown(connection) } + + try await DatabaseManager.shared.executeSchemaChanges( + Self.script(), databaseType: .duckdb, scope: Self.scope(connection), gate: AlwaysAllowGate() + ) + + #expect(driver.statements == ["BEGIN", Self.first, Self.second, "COMMIT"]) + } + + @Test("On the session driver Create Table inside an open transaction joins it") + func sessionRouteCreateTableJoins() async throws { + let (connection, driver, _) = Self.injectSession(type: .duckdb) + defer { Self.tearDown(connection) } + driver.state = .inTransaction + + try await DatabaseManager.shared.executeCreateTable( + statements: [Self.first, Self.second], databaseType: .duckdb, scope: Self.scope(connection), + gate: AlwaysAllowGate() + ) + + #expect(driver.statements == [Self.first, Self.second]) + } + + @Test("A trigger edit that joins an open transaction sends no BEGIN or COMMIT") + func joinedTriggerEditSendsNoTransaction() async throws { + let connection = TestFixtures.makeConnection(type: .duckdb) + let driver = HeldDDLDriver() + let adapter = PluginDriverAdapter(connection: connection, pluginDriver: driver) + + try await TriggerEditing.runInTransaction(driver: adapter, dropSQL: "DROP TRIGGER t", sql: "CREATE TRIGGER t", wraps: false) + + #expect(driver.statements == ["DROP TRIGGER t", "CREATE TRIGGER t"]) + } + + @Test("A pooled save beside an open transaction asks first, and a Cancel runs nothing") + func pooledSaveAsksBesideAHold() async throws { + let (connection, _, sessionAdapter) = Self.injectSession(type: .mysql) + defer { Self.tearDown(connection) } + let pooled = HeldDDLDriver() + let pooledAdapter = PluginDriverAdapter(connection: connection, pluginDriver: pooled) + try await pooledAdapter.connect() + MetadataConnectionPool.shared.injectEntry(pooledAdapter, scope: Self.scope(connection)) + DatabaseManager.shared.sessionTransactions.record( + .inTransaction, on: sessionAdapter, for: .init(connectionId: connection.id, lane: nil) + ) + + await #expect(throws: ExecutionGateError.self) { + try await DatabaseManager.shared.executeSchemaChanges( + Self.script(), databaseType: .mysql, scope: Self.scope(connection), gate: AlwaysAllowGate(), + holdConfirmation: { false } + ) + } + #expect(pooled.statements.isEmpty) + + try await DatabaseManager.shared.executeSchemaChanges( + Self.script(), databaseType: .mysql, scope: Self.scope(connection), gate: AlwaysAllowGate(), + holdConfirmation: { true } + ) + #expect(pooled.statements == ["BEGIN", Self.first, Self.second, "COMMIT"]) + } +} diff --git a/apple/TableProTests/Core/Database/SessionLanesTests.swift b/apple/TableProTests/Core/Database/SessionLanesTests.swift index 4b4e31a158..f694b031cb 100644 --- a/apple/TableProTests/Core/Database/SessionLanesTests.swift +++ b/apple/TableProTests/Core/Database/SessionLanesTests.swift @@ -764,6 +764,32 @@ struct SessionLanesTests { #expect(holding.isEmpty) } + @Test("A transaction the tracker holds is named even while its connection runs a turn") + func trackedHoldOnABusyConnectionIsNamed() async throws { + let harness = makeHarness() + defer { cleanUp(harness) } + DatabaseManager.shared.sessionTransactions.record( + .inTransaction, on: harness.home, for: .init(connectionId: harness.connection.id, lane: "app") + ) + let release = AsyncGate() + let acquired = AsyncGate() + let holder = Task { @MainActor in + try await DatabaseManager.shared.sessionDriverGate.withExclusiveAccess( + SessionDriverGate.Key(connectionId: harness.connection.id, database: "app") + ) { + await acquired.open() + await release.wait() + } + } + await acquired.wait() + + let holding = await DatabaseManager.shared.databasesHoldingTransaction(for: harness.connection.id) + + await release.open() + try await holder.value + #expect(holding == ["app"]) + } + @Test("A connection that does not answer counts as holding nothing once the bound passes") func silentConnectionIsBounded() async { let harness = makeHarness() diff --git a/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift b/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift index fcf531d914..06f7f62f44 100644 --- a/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift +++ b/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift @@ -16,9 +16,12 @@ import Testing @MainActor struct SessionTransactionTrackerTests { private static func seed( - database: String = "app" + database: String = "app", + type: DatabaseType = .mysql, + commitMode: ConnectionCommitMode = .automatic ) -> (connection: DatabaseConnection, driver: TransactionScriptedDriver) { - let connection = TestFixtures.makeConnection(database: database, type: .mysql) + var connection = TestFixtures.makeConnection(database: database, type: type) + connection.commitMode = commitMode let driver = TransactionScriptedDriver() var session = ConnectionSession( connection: connection, @@ -41,6 +44,26 @@ struct SessionTransactionTrackerTests { } } + // The editor's single-statement path: the hold is opened inside the same turn as the write. + private static func runInMode(_ mode: ConnectionCommitMode, _ sql: String, on connectionId: UUID) async throws { + let scope = DatabaseScope(connectionId: connectionId, database: "app", schema: nil) + _ = try await DatabaseManager.shared.withScopedDriver( + scope: scope, + route: .sessionDriver, + cancellation: .protectedWrite + ) { driver in + _ = try await ManualCommit.openIfNeeded( + on: driver, statements: [sql], databaseType: .mysql, + grammar: DatabaseType.mysql.lexicalGrammar, commitMode: mode + ) + return try await driver.execute(query: sql) + } + } + + private static func scope(_ connectionId: UUID) -> DatabaseScope { + DatabaseScope(connectionId: connectionId, database: "app", schema: nil) + } + private static var tracker: SessionTransactionTracker { DatabaseManager.shared.sessionTransactions } private static func states(_ connectionId: UUID, in tracker: SessionTransactionTracker? = nil) -> [SessionTransactionTracker.State] { @@ -239,6 +262,313 @@ struct SessionTransactionTrackerTests { #expect(tracker.holdsTransaction(connectionId: connectionId, container: "hr")) } + @Test("In Manual Commit an UPDATE sends BEGIN first and leaves the transaction open") + func manualCommitOpensTheHold() async throws { + let (connection, driver) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + try await Self.runInMode(.manual, "UPDATE t SET a = 1", on: connection.id) + + #expect(driver.statements == ["BEGIN", "UPDATE t SET a = 1"]) + #expect(Self.states(connection.id) == [.open]) + } + + @Test("Manual Commit sends no BEGIN for a read, nor a second one for a later write") + func manualCommitBeginsOnce() async throws { + let (connection, driver) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + try await Self.runInMode(.manual, "SELECT * FROM t", on: connection.id) + #expect(driver.statements == ["SELECT * FROM t"]) + #expect(Self.tracker.holds(for: connection.id).isEmpty) + + try await Self.runInMode(.manual, "UPDATE t SET a = 1", on: connection.id) + try await Self.runInMode(.manual, "UPDATE t SET a = 2", on: connection.id) + #expect(driver.statements == ["SELECT * FROM t", "BEGIN", "UPDATE t SET a = 1", "UPDATE t SET a = 2"]) + } + + @Test("Automatic commit sends no BEGIN") + func automaticCommitOpensNothing() async throws { + let (connection, driver) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + try await Self.runInMode(.automatic, "UPDATE t SET a = 1", on: connection.id) + + #expect(driver.statements == ["UPDATE t SET a = 1"]) + #expect(Self.tracker.holds(for: connection.id).isEmpty) + } + + @Test( + "A session that cannot report, or holds only a lock, refuses the write and sends nothing", + arguments: [PluginSessionTransactionState.unknown, .holdsSessionLocks] + ) + func unholdableSessionRefuses(state: PluginSessionTransactionState) async throws { + let (connection, driver) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + driver.state = state + + await #expect(throws: ManualCommitRefusal()) { + try await Self.runInMode(.manual, "UPDATE t SET a = 1", on: connection.id) + } + #expect(driver.statements.isEmpty) + } + + @Test("A driver that only answers that it cannot say is reported, and one earlier answer outlasts a later unknown") + func readerThatCannotReportIsRemembered() async throws { + let (connection, driver) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + driver.state = .unknown + try await Self.run("SELECT 1", on: connection.id) + #expect(Self.tracker.cannotReportState(connectionId: connection.id, container: "app")) + + driver.state = .idle + try await Self.run("SELECT 1", on: connection.id) + driver.state = .unknown + try await Self.run("SELECT 1", on: connection.id) + #expect(!Self.tracker.cannotReportState(connectionId: connection.id, container: "app")) + } + + @Test("A table read leaves the hold's revision alone, and a write lease that owns no session state moves it") + func readOnlyTurnKeepsTheRevision() async throws { + let (connection, _) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let hold = try #require(Self.tracker.holds(for: connection.id).first) + + _ = try await DatabaseManager.shared.withTableReadDriver(scope: Self.scope(connection.id), cancellation: .untracked) { driver in + try await driver.execute(query: "SELECT * FROM t") + } + _ = try await DatabaseManager.shared.withScopedDriver( + scope: Self.scope(connection.id), route: .sessionDriver, cancellation: .untracked, + ownsSessionState: false, readsOnly: true + ) { driver in + try await driver.execute(query: "SELECT 1") + } + #expect(Self.tracker.isCurrent(hold)) + + _ = try await DatabaseManager.shared.withScopedDriver( + scope: Self.scope(connection.id), route: .sessionDriver, cancellation: .untracked, ownsSessionState: false + ) { driver in + try await driver.execute(query: "INSERT INTO t VALUES (1)") + } + #expect(!Self.tracker.isCurrent(hold)) + } + + @Test("A read-only turn that finds the transaction failed still moves the hold") + func readOnlyTurnStillReportsAFailure() async throws { + let (connection, driver) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let hold = try #require(Self.tracker.holds(for: connection.id).first) + driver.failsNextStatementAborting = true + + await #expect(throws: (any Error).self) { + try await DatabaseManager.shared.withTableReadDriver(scope: Self.scope(connection.id), cancellation: .untracked) { driver in + try await driver.execute(query: "SELECT * FROM missing") + } + } + + #expect(!Self.tracker.isCurrent(hold)) + #expect(Self.states(connection.id) == [.aborted]) + } + + @Test("MCP begin is refused over a transaction already open and sends nothing") + func mcpBeginRefusedOverAnOpenTransaction() async throws { + let (connection, driver) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let sent = driver.statements.count + let operation = try await ApprovedOperationFixture.approve( + [], scope: Self.scope(connection.id), databaseType: .mysql, kind: .writeQuery + ) + + await #expect(throws: DatabaseAccessError.self) { + _ = try await MCPConnectionBridge().transactionControl(operation, action: "begin") + } + #expect(driver.statements.count == sent) + } + + @Test("Manual Commit refuses every MCP transaction action", arguments: ["begin", "commit", "rollback"]) + func mcpTransactionControlRefusedInManualCommit(action: String) async throws { + let (connection, driver) = Self.seed(commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let operation = try await ApprovedOperationFixture.approve( + [], scope: Self.scope(connection.id), databaseType: .mysql, kind: .writeQuery + ) + + await #expect(throws: DatabaseAccessError.self) { + _ = try await MCPConnectionBridge().transactionControl(operation, action: action) + } + #expect(driver.statements.isEmpty) + } + + @Test("An agent's write in Manual Commit is held, and its reply says the change is pending") + func mcpWriteIsHeldAndSaysSo() async throws { + let (connection, driver) = Self.seed(commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + let outcome = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "UPDATE t SET a = 1", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + + let begin = try #require(driver.statements.firstIndex(of: "BEGIN")) + let update = try #require(driver.statements.firstIndex(of: "UPDATE t SET a = 1")) + #expect(begin < update) + #expect(!driver.statements.contains("COMMIT")) + #expect(outcome.result.statusMessage?.contains(ManualCommit.pendingNotice) == true) + #expect(Self.states(connection.id) == [.open]) + } + + @Test("An agent's DDL over an open transaction in Manual Commit is refused, since MySQL would commit it") + func mcpDDLRefusedOverAHold() async throws { + let (connection, driver) = Self.seed(commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let sent = driver.statements.count + + let error = await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "ALTER TABLE t ADD c INT", maxRows: 10, timeoutSeconds: 30, + cancellation: nil + ) + } + #expect(error?.localizedDescription == ManualCommitDDLRefusal().errorDescription) + #expect(driver.statements.count == sent) + } + + @Test("An agent's COMMIT, ROLLBACK or BEGIN in Manual Commit is refused and sends nothing", arguments: ["COMMIT", "ROLLBACK", "BEGIN"]) + func mcpTransactionStatementRefusedInManualCommit(sql: String) async throws { + let (connection, driver) = Self.seed(commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.runInMode(.manual, "UPDATE t SET a = 1", on: connection.id) + let sent = driver.statements.count + + let error = await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: sql, maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + } + #expect(error?.localizedDescription == ManualCommit.transactionControlRefusalMessage) + #expect(driver.statements.count == sent) + #expect(Self.states(connection.id) == [.open]) + } + + @Test( + "With Manual Commit holding a transaction, an agent statement that can end it unseen is refused", + arguments: [ + (DatabaseType.mssql, "UPDATE t SET a = 1 COMMIT"), + (DatabaseType.mssql, "EXEC dbo.p"), + (DatabaseType.mysql, "CALL p()"), + ] + ) + func mcpUnseenTransactionControlRefused(type: DatabaseType, sql: String) async throws { + let (connection, driver) = Self.seed(type: type, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let sent = driver.statements.count + + let error = await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: sql, maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + } + #expect(error?.localizedDescription == ManualCommit.transactionControlRefusalMessage) + #expect(driver.statements.count == sent) + #expect(Self.states(connection.id) == [.open]) + } + + @Test("An agent statement that ended the open transaction says so in its reply") + func mcpReplySaysTheTransactionEnded() async throws { + let (connection, _) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + + let outcome = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "COMMIT", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + + #expect(outcome.result.statusMessage?.contains(ManualCommit.transactionEndedNotice) == true) + #expect(Self.tracker.holds(for: connection.id).isEmpty) + } + + @Test("An agent's BEGIN over a transaction already open is refused, whatever the commit mode") + func mcpBeginStatementRefusedOverAnOpenTransaction() async throws { + let (connection, driver) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let sent = driver.statements.count + + let error = await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "START TRANSACTION", maxRows: 10, timeoutSeconds: 30, + cancellation: nil + ) + } + #expect(error?.localizedDescription == ManualCommit.openTransactionRefusalMessage) + #expect(driver.statements.count == sent) + } + + @Test("An agent statement over an open PostgreSQL transaction runs inside a savepoint it releases") + func agentStatementTakesASavepoint() async throws { + let (connection, driver) = Self.seed(type: .postgresql) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "UPDATE t SET a = 1", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + + #expect(driver.statements.suffix(3) == [ + AgentTransactionGuard.savepointStatement, "UPDATE t SET a = 1", AgentTransactionGuard.releaseStatement, + ]) + } + + @Test("An agent statement that fails inside an open PostgreSQL transaction is rolled back to its savepoint") + func failedAgentStatementRollsBackToItsSavepoint() async throws { + let (connection, driver) = Self.seed(type: .postgresql) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + driver.failingStatement = "UPDATE t SET a = 1 / 0" + + await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "UPDATE t SET a = 1 / 0", maxRows: 10, timeoutSeconds: 30, + cancellation: nil + ) + } + + #expect(driver.statements.suffix(3) == [ + AgentTransactionGuard.savepointStatement, "UPDATE t SET a = 1 / 0", AgentTransactionGuard.rollbackStatement, + ]) + } + + @Test("An agent statement over an open DuckDB transaction is refused, since a failure would abort it") + func agentStatementRefusedWithoutSavepoints() async throws { + let (connection, driver) = Self.seed(type: .duckdb) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let sent = driver.statements.count + + let error = await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "SELECT 1", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + } + #expect(error?.localizedDescription == AbortingTransactionRefusal().errorDescription) + #expect(driver.statements.count == sent) + } + + @Test("Turning Manual Commit on writes the session and the connection it was opened from") + func menuToggleWritesTheSession() { + let (connection, _) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + DatabaseManager.shared.setCommitMode(.manual, for: connection.id) + + #expect(DatabaseManager.shared.commitMode(for: connection.id) == .manual) + #expect(DatabaseManager.shared.session(for: connection.id)?.connection.commitMode == .manual) + } + @Test("Read-only hides Commit, an aborted transaction offers only Roll Back") func barContent() { let open = ConnectionTransactionBarContent( @@ -293,10 +623,18 @@ private final class TransactionScriptedDriver: PluginDriverStub, @unchecked Send set { lock.withLock { abortsNext = newValue } } } + private var failing: String? + + // That statement fails and aborts the transaction, the way PostgreSQL answers a division by zero. + var failingStatement: String? { + get { lock.withLock { failing } } + set { lock.withLock { failing = newValue } } + } + func execute(query: String) async throws -> PluginQueryResult { let failure: Bool = lock.withLock { sent.append(query) - if abortsNext { + if abortsNext || query == failing { abortsNext = false current = .abortedTransaction return true diff --git a/apple/TableProTests/Core/Menu/ManualCommitMenuTests.swift b/apple/TableProTests/Core/Menu/ManualCommitMenuTests.swift new file mode 100644 index 0000000000..240d025ce9 --- /dev/null +++ b/apple/TableProTests/Core/Menu/ManualCommitMenuTests.swift @@ -0,0 +1,89 @@ +// +// ManualCommitMenuTests.swift +// TableProTests +// + +import AppKit +@testable import TablePro +import Testing + +@MainActor +struct ManualCommitMenuTests { + private func databaseMenu() throws -> NSMenu { + let bar = MainMenuBuilder.build(keyboard: KeyboardSettings()).bar + return try #require(bar.items.first { $0.title == String(localized: "Database") }?.submenu) + } + + private func enabled(_ selector: Selector, _ context: MenuValidationContext) -> Bool { + MainSplitViewController.isEnabled(selector, context: context) + } + + @Test("Manual Commit, its footnote, Commit Transaction and Roll Back Transaction follow Safe Mode Level") + func itemsFollowSafeModeLevel() throws { + let items = try databaseMenu().items + let safeMode = try #require(items.firstIndex { $0.title == String(localized: "Safe Mode Level") }) + try #require(safeMode + 4 < items.count) + + #expect(items[safeMode + 1].title == String(localized: "Manual Commit")) + #expect(items[safeMode + 1].action == #selector(MainSplitViewController.toggleManualCommit(_:))) + #expect(items[safeMode + 2].identifier == DatabaseMenuBuilder.manualCommitFootnoteIdentifier) + #expect(items[safeMode + 2].isHidden) + #expect(items[safeMode + 3].title == String(localized: "Commit Transaction")) + #expect(items[safeMode + 3].action == #selector(MainSplitViewController.commitOpenTransaction(_:))) + #expect(items[safeMode + 4].title == String(localized: "Roll Back Transaction")) + #expect(items[safeMode + 4].action == #selector(MainSplitViewController.rollBackOpenTransaction(_:))) + } + + @Test("Commit and Roll Back Transaction are Keyboard rows with no default chord") + func transactionCommandsShipUnbound() throws { + for action in [ShortcutAction.commitTransaction, .rollbackTransaction] { + #expect(KeyboardSettings.defaultShortcuts[action] == nil) + let item = try #require(try databaseMenu().items.first { $0.identifier == MenuItemFactory.identifier(for: action) }) + #expect(item.keyEquivalent.isEmpty) + } + } + + @Test("Manual Commit needs a connection that can hold a transaction, and stays enabled while on") + func manualCommitEnablement() { + let toggle = #selector(MainSplitViewController.toggleManualCommit(_:)) + var context = MenuValidationContext() + context.transaction.canHoldTransactions = true + #expect(!enabled(toggle, context)) + + context.isConnected = true + #expect(enabled(toggle, context)) + + context.transaction.canHoldTransactions = false + #expect(!enabled(toggle, context)) + + context.transaction.isManualCommitOn = true + #expect(enabled(toggle, context)) + } + + @Test("Commit needs an open transaction it may commit; Roll Back needs any held transaction") + func commitAndRollBackEnablement() { + let commit = #selector(MainSplitViewController.commitOpenTransaction(_:)) + let rollBack = #selector(MainSplitViewController.rollBackOpenTransaction(_:)) + var context = MenuValidationContext() + context.isConnected = true + #expect(!enabled(commit, context)) + #expect(!enabled(rollBack, context)) + + context.transaction.holdsTransaction = true + #expect(!enabled(commit, context)) + #expect(enabled(rollBack, context)) + + context.transaction.canCommit = true + #expect(enabled(commit, context)) + } + + @Test("Commit is offered for an open hold below Read-Only, never for an aborted one") + func commitFollowsSafeModeAndState() { + let key = SessionTransactionTracker.Key(connectionId: UUID(), lane: nil) + let open = SessionTransactionTracker.Hold(key: key, state: .open, revision: 1) + let aborted = SessionTransactionTracker.Hold(key: key, state: .aborted, revision: 1) + #expect(ConnectionTransactionActions.offersCommit(open, safeModeLevel: .silent)) + #expect(!ConnectionTransactionActions.offersCommit(open, safeModeLevel: .readOnly)) + #expect(!ConnectionTransactionActions.offersCommit(aborted, safeModeLevel: .silent)) + } +} diff --git a/apple/TableProTests/Core/Menu/QueryTabRevertMenuTests.swift b/apple/TableProTests/Core/Menu/QueryTabRevertMenuTests.swift new file mode 100644 index 0000000000..2152832a19 --- /dev/null +++ b/apple/TableProTests/Core/Menu/QueryTabRevertMenuTests.swift @@ -0,0 +1,89 @@ +// +// QueryTabRevertMenuTests.swift +// TableProTests +// + +import AppKit +@testable import TablePro +import Testing + +@MainActor +struct QueryTabRevertMenuTests { + private static let revert = #selector(MainSplitViewController.revertToQueryTabVersion(_:)) + private static let browse = #selector(MainSplitViewController.browseQueryTabVersions(_:)) + + private func fileMenu() throws -> NSMenu { + let bar = MainMenuBuilder.build(keyboard: KeyboardSettings()).bar + return try #require(bar.items.first { $0.title == String(localized: "File") }?.submenu) + } + + private func summary(_ id: Int64, _ reason: QueryTabVersionReason) -> QueryTabVersionSummary { + QueryTabVersionSummary(id: id, reason: reason, savedAt: Date(timeIntervalSince1970: 1_800_000_000 + Double(id))) + } + + @Test("Revert To follows Save As in File, fills when it opens and carries no shortcut") + func revertToFollowsSaveAs() throws { + let file = try fileMenu() + let saveAs = try #require(file.items.firstIndex { + $0.action == #selector(MainSplitViewController.saveDocumentAs(_:)) + }) + let revertTo = file.items[saveAs + 1] + + #expect(revertTo.title == String(localized: "Revert To")) + #expect(revertTo.keyEquivalent.isEmpty) + #expect(revertTo.submenu?.delegate is QueryTabRevertMenuDelegate) + #expect(revertTo.submenu?.items.isEmpty == true) + } + + @Test("The list is the tab's versions, then Browse All Versions…") + func listsVersionsThenBrowse() throws { + let menu = NSMenu() + let tabId = UUID() + + QueryTabRevertMenuDelegate.fill(menu, tabId: tabId, summaries: [summary(2, .run), summary(1, .idle)]) + + #expect(menu.items.count == 4) + #expect(menu.items[0].action == Self.revert) + #expect(menu.items[0].target == nil) + #expect(menu.items[0].representedObject as? QueryTabRevertChoice == QueryTabRevertChoice(tabId: tabId, versionId: 2)) + #expect(menu.items[1].representedObject as? QueryTabRevertChoice == QueryTabRevertChoice(tabId: tabId, versionId: 1)) + #expect(menu.items[0].title.contains(String(localized: "Run"))) + #expect(menu.items[2].isSeparatorItem) + #expect(menu.items[3].title == String(localized: "Browse All Versions…")) + #expect(menu.items[3].action == Self.browse) + #expect(menu.items[3].target == nil) + } + + @Test("With no versions the list says so and still offers Browse All Versions…") + func emptyListShowsThePlaceholder() { + let menu = NSMenu() + menu.addItem(withTitle: "stale", action: nil, keyEquivalent: "") + + QueryTabRevertMenuDelegate.fill(menu, tabId: UUID(), summaries: []) + + #expect(menu.items.map { $0.title } == [ + String(localized: "None Available"), "", String(localized: "Browse All Versions…"), + ]) + #expect(menu.items[0].isEnabled == false) + } + + @Test("Revert To and Browse All Versions need a query tab, and no connection") + func commandsNeedAQueryTab() { + for selector in [Self.revert, Self.browse] { + var context = MenuValidationContext() + #expect(MainSplitViewController.resolvedEnablement(selector, context: context) == false) + + context.isQueryTab = true + #expect(MainSplitViewController.resolvedEnablement(selector, context: context) == true) + + context.isAgentMode = true + #expect(MainSplitViewController.resolvedEnablement(selector, context: context) == false) + } + } + + @Test("The window answers both commands") + func windowImplementsBoth() { + #expect(MainSplitViewController.instancesRespond(to: Self.revert)) + #expect(MainSplitViewController.instancesRespond(to: Self.browse)) + } +} diff --git a/apple/TableProTests/Core/ObjectCopy/ObjectCopyTransactionTests.swift b/apple/TableProTests/Core/ObjectCopy/ObjectCopyTransactionTests.swift index ab8b8205a5..73d2b74ef2 100644 --- a/apple/TableProTests/Core/ObjectCopy/ObjectCopyTransactionTests.swift +++ b/apple/TableProTests/Core/ObjectCopy/ObjectCopyTransactionTests.swift @@ -286,6 +286,23 @@ struct ObjectCopyTransactionTests { #expect(result.pendingInSessionTransaction == false) } + // A held copy on a pooled lease would be rolled back when the pool closes it, after the sheet reported success. + @Test("Copy To commits its own transaction even on a connection in Manual Commit") + func manualCommitConnectionStillCommitsTheCopy() async throws { + var connection = TestFixtures.makeConnection(id: targetScope.connectionId, type: targetType) + connection.commitMode = .manual + DatabaseManager.shared.injectSession(ConnectionSession(connection: connection), for: connection.id) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let log = CopyEventLog() + + _ = try await run( + replacementPlan(), source: RecordingCopyDriver("source", log: log), target: RecordingCopyDriver("target", log: log) + ) + + #expect(log.events.first == "target begin") + #expect(log.events.last == "target commit") + } + /// The statements still run: they join the transaction the user opened, and only the user can /// end it. Opening one over it is what aborted it on DuckDB. @Test("A session holding a transaction is joined rather than wrapped, with its keys as they are") diff --git a/apple/TableProTests/Core/Plugins/ConnectionFieldTrustCoverageTests.swift b/apple/TableProTests/Core/Plugins/ConnectionFieldTrustCoverageTests.swift index 8a84c3f491..e3143eda6a 100644 --- a/apple/TableProTests/Core/Plugins/ConnectionFieldTrustCoverageTests.swift +++ b/apple/TableProTests/Core/Plugins/ConnectionFieldTrustCoverageTests.swift @@ -10,6 +10,7 @@ private let hostOwnedKeys: [String] = [ DatabaseConnection.postDisconnectScriptKey, DatabaseConnection.scriptTimeoutSecondsKey, DatabaseConnection.completionIdentifierCaseKey, + DatabaseConnection.commitModeKey, DatabaseConnection.binaryInlineLimitKilobytesKey, DatabaseConnection.maskingRulesKey, DatabaseConnection.sshForwardUnixSocketPathKey, diff --git a/apple/TableProTests/Core/Plugins/ImportManualCommitTests.swift b/apple/TableProTests/Core/Plugins/ImportManualCommitTests.swift new file mode 100644 index 0000000000..c217178d55 --- /dev/null +++ b/apple/TableProTests/Core/Plugins/ImportManualCommitTests.swift @@ -0,0 +1,179 @@ +// +// ImportManualCommitTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +private final class ImportHoldDriver: PluginDriverStub, @unchecked Sendable { + private let lock = NSLock() + private var current: PluginSessionTransactionState + private var sent: [String] = [] + private let ddlCommits: Bool + + // `ddlCommits` answers as MySQL does: DDL and LOCK TABLES commit the open transaction. + init(state: PluginSessionTransactionState = .idle, ddlCommits: Bool = false) { + current = state + self.ddlCommits = ddlCommits + } + + var supportsSchemas: Bool { false } + var currentSchema: String? { nil } + var serverVersion: String? { nil } + + var statements: [String] { lock.withLock { sent } } + + func execute(query: String) async throws -> PluginQueryResult { + lock.withLock { + sent.append(query) + let upper = query.uppercased() + switch upper { + case "BEGIN": current = .inTransaction + case "COMMIT", "ROLLBACK": current = .idle + default: + let head = upper.split(separator: " ").first.map(String.init) ?? "" + if ddlCommits, ["DROP", "CREATE", "ALTER", "LOCK"].contains(head) { current = .idle } + } + } + return .empty + } + + func sessionTransactionState() async -> PluginSessionTransactionState { lock.withLock { current } } + + func foreignKeyDisableStatements() -> [String]? { ["PRAGMA foreign_keys = OFF"] } + func foreignKeyEnableStatements() -> [String]? { ["PRAGMA foreign_keys = ON"] } +} + +struct ImportManualCommitTests { + private func sink( + on driver: ImportHoldDriver, + type: DatabaseType = .sqlite, + commitMode: ConnectionCommitMode = .manual + ) -> ImportDataSinkAdapter { + let adapter = PluginDriverAdapter(connection: TestFixtures.makeConnection(type: type), pluginDriver: driver) + return ImportDataSinkAdapter(driver: adapter, databaseType: type, commitMode: commitMode, importRun: ImportRun()) + } + + @Test("A Manual Commit import holds its transaction: BEGIN, and no COMMIT at the end") + func importIsHeld() async throws { + let driver = ImportHoldDriver() + let sink = sink(on: driver) + + try await sink.prepareImportRun() + try await sink.beginTransaction() + try await sink.execute(statement: "INSERT INTO t VALUES (1)", line: 1) + try await sink.commitTransaction() + + #expect(driver.statements == ["BEGIN", "INSERT INTO t VALUES (1)"]) + } + + @Test( + "A Manual Commit import the session can't hold is refused before anything runs", + arguments: [PluginSessionTransactionState.unknown, .holdsSessionLocks] + ) + func unholdableImportIsRefused(state: PluginSessionTransactionState) async { + let driver = ImportHoldDriver(state: state) + let sink = sink(on: driver) + + await #expect(throws: ManualCommitRefusal()) { + try await sink.prepareImportRun() + } + #expect(driver.statements.isEmpty) + } + + // SQLite ignores the re-enable inside a held transaction, so the checks would stay off for the session. + @Test("A Manual Commit import that turned foreign key checks off is refused before its first write") + func foreignKeyToggledImportIsRefused() async throws { + let driver = ImportHoldDriver() + let sink = sink(on: driver) + + try await sink.prepareImportRun() + try await sink.disableForeignKeyChecks() + await #expect(throws: ManualCommitForeignKeyRefusal()) { + try await sink.beginTransaction() + } + try await sink.enableForeignKeyChecks() + + #expect(driver.statements == ["PRAGMA foreign_keys = OFF", "PRAGMA foreign_keys = ON"]) + } + + // Skip and Continue goes on after a refusal, so every later write is refused as well. + @Test("A Manual Commit import that never begins a transaction is refused at each write, before it runs") + func unwrappedImportIsRefused() async throws { + let driver = ImportHoldDriver() + let sink = sink(on: driver) + + try await sink.prepareImportRun() + await #expect(throws: ManualCommitUnwrappedImportRefusal()) { + try await sink.execute(statement: "INSERT INTO t VALUES (1)", line: 1) + } + await #expect(throws: ManualCommitUnwrappedImportRefusal()) { + try await sink.execute(statement: "INSERT INTO t VALUES (2)", line: 2) + } + + #expect(driver.statements.isEmpty) + } + + // A mysqldump drops and creates each table before its rows; after that DDL the rows would autocommit. + @Test("A held MySQL import refuses the first row after the file's own DDL ended the hold") + func dumpRowsAfterDDLAreRefused() async throws { + let driver = ImportHoldDriver(ddlCommits: true) + let sink = sink(on: driver, type: .mysql) + try await sink.prepareImportRun() + try await sink.beginTransaction() + + for (line, statement) in ["DROP TABLE IF EXISTS t", "CREATE TABLE t (id INT)", "LOCK TABLES t WRITE"].enumerated() { + try await sink.execute(statement: statement, line: line + 1) + } + await #expect(throws: ManualCommitMixedRefusal()) { + try await sink.execute(statement: "INSERT INTO t VALUES (1)", line: 4) + } + + #expect(driver.statements == ["BEGIN", "DROP TABLE IF EXISTS t", "CREATE TABLE t (id INT)", "LOCK TABLES t WRITE"]) + } + + @Test("A held MySQL import refuses DDL once it has written rows, which stay held") + func ddlAfterHeldRowsIsRefused() async throws { + let driver = ImportHoldDriver(ddlCommits: true) + let sink = sink(on: driver, type: .mysql) + try await sink.prepareImportRun() + try await sink.beginTransaction() + + try await sink.execute(statement: "INSERT INTO a VALUES (1)", line: 1) + await #expect(throws: ManualCommitMixedRefusal()) { + try await sink.execute(statement: "DROP TABLE IF EXISTS b", line: 2) + } + + #expect(driver.statements == ["BEGIN", "INSERT INTO a VALUES (1)"]) + } + + @Test("A held import marks the turn's hold at its BEGIN") + func heldImportMarksTheTurn() async throws { + let driver = ImportHoldDriver() + let sink = sink(on: driver) + let held = ManualCommitTurnHold() + + try await ManualCommit.$turnHold.withValue(held) { + try await sink.prepareImportRun() + try await sink.beginTransaction() + } + + #expect(held.isHeld) + } + + @Test("An automatic import with foreign key checks off still wraps and commits") + func automaticImportWithToggleCommits() async throws { + let driver = ImportHoldDriver() + let sink = sink(on: driver, commitMode: .automatic) + + try await sink.prepareImportRun() + try await sink.disableForeignKeyChecks() + try await sink.beginTransaction() + try await sink.commitTransaction() + + #expect(driver.statements == ["PRAGMA foreign_keys = OFF", "BEGIN", "COMMIT"]) + } +} diff --git a/apple/TableProTests/Core/SchemaTracking/StructureChangeValidationTests.swift b/apple/TableProTests/Core/SchemaTracking/StructureChangeValidationTests.swift index e05e11ea1f..a325c60432 100644 --- a/apple/TableProTests/Core/SchemaTracking/StructureChangeValidationTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/StructureChangeValidationTests.swift @@ -83,6 +83,25 @@ struct StructureChangeValidationTests { #expect(manager.validationSummary.isEmpty) } + @Test("A key with more columns than referenced columns blocks the save, with Create Table's reason") + func unequalColumnCountsBlockTheSave() throws { + let manager = loadedManager() + let foreignKey = validForeignKey(columns: ["id", "customer_id"], referencedColumns: ["id"]) + manager.addForeignKey(foreignKey) + + #expect(!manager.canCommit) + let staged = try #require(manager.workingForeignKeys.last) + #expect(manager.validationErrors[.foreignKey(staged.id)] + == "A foreign key needs as many referenced columns as it has columns.") + } + + @Test("A key with as many referenced columns as columns can be saved") + func equalColumnCountsAllowTheSave() { + let manager = loadedManager() + manager.addForeignKey(validForeignKey(columns: ["id", "customer_id"], referencedColumns: ["tenant_id", "id"])) + #expect(manager.canCommit) + } + @Test("A key on a column the table does not have blocks the save") func foreignKeyOnMissingColumnBlocksCommit() { let manager = loadedManager() diff --git a/apple/TableProTests/Core/Services/Execution/BatchStatementRunTests.swift b/apple/TableProTests/Core/Services/Execution/BatchStatementRunTests.swift index b20e731cd0..d29ac4858a 100644 --- a/apple/TableProTests/Core/Services/Execution/BatchStatementRunTests.swift +++ b/apple/TableProTests/Core/Services/Execution/BatchStatementRunTests.swift @@ -117,6 +117,53 @@ struct BatchStatementRunTests { } } + @Test("A held run sends BEGIN and leaves the transaction open for the user to commit") + func heldRunBeginsAndNeverCommits() async { + let driver = TransactionRecordingDriver() + let outcome = await Self.run(plan: .heldTransaction, driver: driver) + #expect(driver.events == [.begin(mode: .readWrite)]) + guard case .completed(let results) = outcome else { + Issue.record("expected a completed run, got \(outcome)") + return + } + #expect(results.count == 3) + } + + @Test("A held run marks the turn's hold, and one rolled back clears it") + func heldRunMarksTheTurn() async { + let held = ManualCommitTurnHold() + _ = await ManualCommit.$turnHold.withValue(held) { + await Self.run(plan: .heldTransaction, driver: TransactionRecordingDriver()) + } + #expect(held.isHeld) + + let failed = ManualCommitTurnHold() + _ = await ManualCommit.$turnHold.withValue(failed) { + await Self.run(plan: .heldTransaction, driver: TransactionRecordingDriver(), failing: "VACUUM") + } + #expect(!failed.isHeld) + } + + @Test("A held run rolls back what it opened on a failure and on a Stop") + func heldRunRollsBackAFailureAndAStop() async { + let failing = TransactionRecordingDriver() + let failed = await Self.run(plan: .heldTransaction, driver: failing, failing: "VACUUM") + #expect(failing.events == [.begin(mode: .readWrite), .rollback]) + guard case .failed = failed else { + Issue.record("expected a failed run, got \(failed)") + return + } + + let stopped = TransactionRecordingDriver() + let outcome = await Self.run(plan: .heldTransaction, driver: stopped, stopsAfter: 1) + #expect(stopped.events == [.begin(mode: .readWrite), .rollback]) + guard case .cancelled(let results) = outcome else { + Issue.record("expected a cancelled run, got \(outcome)") + return + } + #expect(results.isEmpty) + } + @Test("The app's own transaction is rolled back on a failure, and its results are dropped") func appTransactionRollsBackAFailure() async { let driver = TransactionRecordingDriver() @@ -228,7 +275,7 @@ struct BatchStatementRunTests { @Test( "A driver without transactions is never asked to begin, commit or roll back", - arguments: [BatchTransactionPlan.appTransaction, .scriptTransaction, .autocommit, .sessionTransaction] + arguments: [BatchTransactionPlan.appTransaction, .heldTransaction, .scriptTransaction, .autocommit, .sessionTransaction] ) func driversWithoutTransactionsAreLeftAlone(plan: BatchTransactionPlan) async { let driver = TransactionRecordingDriver(supportsTransactions: false) diff --git a/apple/TableProTests/Core/Services/Execution/BatchTransactionPlanTests.swift b/apple/TableProTests/Core/Services/Execution/BatchTransactionPlanTests.swift index 185bd773ad..49eb84b51e 100644 --- a/apple/TableProTests/Core/Services/Execution/BatchTransactionPlanTests.swift +++ b/apple/TableProTests/Core/Services/Execution/BatchTransactionPlanTests.swift @@ -11,12 +11,16 @@ import Testing struct BatchTransactionPlanTests { private static let textPlans: [BatchTransactionPlan] = [.appTransaction, .scriptTransaction, .autocommit] - @Test("Only the app's own plan opens a transaction") - func onlyTheAppPlanOpensOne() { - #expect(BatchTransactionPlan.appTransaction.opensTransaction) - #expect(BatchTransactionPlan.scriptTransaction.opensTransaction == false) - #expect(BatchTransactionPlan.autocommit.opensTransaction == false) - #expect(BatchTransactionPlan.sessionTransaction.opensTransaction == false) + @Test("The app's own plan and a held one send BEGIN, and only the app's own sends COMMIT") + func onlyTheAppPlanCommits() { + #expect(BatchTransactionPlan.appTransaction.sendsBegin) + #expect(BatchTransactionPlan.appTransaction.commitsAtEnd) + #expect(BatchTransactionPlan.heldTransaction.sendsBegin) + #expect(BatchTransactionPlan.heldTransaction.commitsAtEnd == false) + for plan in [BatchTransactionPlan.scriptTransaction, .autocommit, .sessionTransaction] { + #expect(plan.sendsBegin == false) + #expect(plan.commitsAtEnd == false) + } } @Test("A joined run rolls nothing back and keeps what ran, exactly as autocommit does") @@ -25,31 +29,48 @@ struct BatchTransactionPlanTests { #expect(BatchTransactionPlan.sessionTransaction.keepsExecutedStatements) } + @Test("A held run rolls back on a failure or a Stop and reads what it left open") + func heldRunRollsBackAndReadsItsEnd() { + #expect(BatchTransactionPlan.heldTransaction.rollsBackAfterStop) + #expect(BatchTransactionPlan.heldTransaction.keepsExecutedStatements == false) + #expect(BatchTransactionPlan.heldTransaction.readsSessionEndState) + #expect(BatchTransactionPlan.appTransaction.readsSessionEndState == false) + } + @Test( "A session holding a transaction or a lock takes the plan over, whatever the text said", arguments: [PluginSessionTransactionState.inTransaction, .abortedTransaction, .holdsSessionLocks] ) func heldSessionTakesOver(state: PluginSessionTransactionState) { for plan in Self.textPlans { - #expect(plan.joining(state) == .sessionTransaction) + #expect(plan.joining(state, commitMode: .automatic) == .sessionTransaction) + #expect(plan.joining(state, commitMode: .manual) == .sessionTransaction) } } @Test("A session with nothing open leaves the plan the text decided") func idleSessionChangesNothing() { for plan in Self.textPlans { - #expect(plan.joining(.idle) == plan) + #expect(plan.joining(.idle, commitMode: .automatic) == plan) } } + @Test("Manual Commit holds a wrapped batch on an idle session, and nothing else") + func manualCommitHoldsOnlyAWrappedBatchOnAnIdleSession() { + #expect(BatchTransactionPlan.appTransaction.joining(.idle, commitMode: .manual) == .heldTransaction) + #expect(BatchTransactionPlan.appTransaction.joining(.unknown, commitMode: .manual) == .appTransaction) + #expect(BatchTransactionPlan.scriptTransaction.joining(.idle, commitMode: .manual) == .scriptTransaction) + #expect(BatchTransactionPlan.autocommit.joining(.idle, commitMode: .manual) == .autocommit) + } + /// The wrap stays for a plain batch, because its atomicity is worth more than a transaction that /// may not be there. A self-managed script stops being rolled back, because the transaction its /// text left open may predate the run. @Test("A session that cannot say keeps the wrap but stops the rollback of a self-managed script") func unknownSessionKeepsTheWrapButNotTheRollback() { - #expect(BatchTransactionPlan.appTransaction.joining(.unknown) == .appTransaction) - #expect(BatchTransactionPlan.autocommit.joining(.unknown) == .autocommit) - #expect(BatchTransactionPlan.scriptTransaction.joining(.unknown) == .sessionTransaction) + #expect(BatchTransactionPlan.appTransaction.joining(.unknown, commitMode: .automatic) == .appTransaction) + #expect(BatchTransactionPlan.autocommit.joining(.unknown, commitMode: .automatic) == .autocommit) + #expect(BatchTransactionPlan.scriptTransaction.joining(.unknown, commitMode: .automatic) == .sessionTransaction) } @Test("Joining is settled once: a joined plan stays joined") @@ -57,9 +78,21 @@ struct BatchTransactionPlanTests { for state in [ PluginSessionTransactionState.idle, .inTransaction, .abortedTransaction, .holdsSessionLocks, .unknown, ] { - #expect(BatchTransactionPlan.sessionTransaction.joining(state) == .sessionTransaction) + #expect(BatchTransactionPlan.sessionTransaction.joining(state, commitMode: .manual) == .sessionTransaction) } } + + @Test("A held run is never recorded as committed") + func heldRunIsNotCommitted() { + let completed = StatementCommitEvidence.run( + startedIn: .idle, endedIn: .inTransaction, plan: .heldTransaction, completed: true + ) + let failed = StatementCommitEvidence.run( + startedIn: .idle, endedIn: .idle, plan: .heldTransaction, completed: false + ) + #expect(completed == .runStartedIn(.idle, endedIn: .inTransaction, appTransaction: .none)) + #expect(failed == .runStartedIn(.idle, endedIn: .idle, appTransaction: .rolledBack)) + } } struct SessionTransactionStateTests { diff --git a/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift b/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift new file mode 100644 index 0000000000..8833f54ced --- /dev/null +++ b/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift @@ -0,0 +1,264 @@ +// +// ManualCommitTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import TableProSSHConfig +import Testing + +struct ManualCommitTests { + private func decision( + _ sql: String, + mode: ConnectionCommitMode = .manual, + type: DatabaseType = .postgresql, + supportsTransactions: Bool = true, + state: PluginSessionTransactionState = .idle + ) -> ManualCommit.Decision { + decision([sql], mode: mode, type: type, supportsTransactions: supportsTransactions, state: state) + } + + private func decision( + _ statements: [String], + mode: ConnectionCommitMode = .manual, + type: DatabaseType = .postgresql, + supportsTransactions: Bool = true, + state: PluginSessionTransactionState = .idle + ) -> ManualCommit.Decision { + ManualCommit.decision( + commitMode: mode, + statements: statements, + databaseType: type, + grammar: type.lexicalGrammar, + supportsTransactions: supportsTransactions, + sessionState: state + ) + } + + @Test("A write on an idle session is held in Manual Commit and runs as typed otherwise") + func writeOnAnIdleSessionIsHeld() { + #expect(decision("UPDATE t SET a = 1") == .hold) + #expect(decision("UPDATE t SET a = 1", mode: .automatic) == .runAsIs) + } + + @Test("A read opens nothing") + func readOpensNothing() { + #expect(decision("SELECT * FROM t") == .runAsIs) + } + + @Test("A write joins a transaction already open, aborted or not") + func writeJoinsAnOpenTransaction() { + #expect(decision("UPDATE t SET a = 1", state: .inTransaction) == .runAsIs) + #expect(decision("UPDATE t SET a = 1", state: .abortedTransaction) == .runAsIs) + } + + @Test( + "A write the session cannot hold is refused rather than committed", + arguments: [PluginSessionTransactionState.unknown, .holdsSessionLocks] + ) + func unholdableStateRefuses(state: PluginSessionTransactionState) { + #expect(decision("UPDATE t SET a = 1", state: state) == .refuse) + } + + @Test("An engine without transactions refuses a write in Manual Commit") + func engineWithoutTransactionsRefuses() { + #expect(decision("UPDATE t SET a = 1", supportsTransactions: false) == .refuse) + #expect(decision("SELECT 1", supportsTransactions: false) == .runAsIs) + } + + @Test("Transaction control and statements refused inside a block run as typed") + func transactionControlAndAutocommitOnlyRunAsTyped() { + #expect(decision("BEGIN") == .runAsIs) + #expect(decision("VACUUM") == .runAsIs) + } + + @Test("DDL on an engine that commits it implicitly ends the open transaction, elsewhere it does not") + func implicitCommitDDL() { + let mysql = DatabaseType.mysql + #expect(ManualCommit.commitsHeldTransaction(["ALTER TABLE t ADD c INT"], databaseType: mysql, grammar: mysql.lexicalGrammar)) + #expect(ManualCommit.commitsHeldTransaction(["TRUNCATE TABLE t"], databaseType: mysql, grammar: mysql.lexicalGrammar)) + #expect(!ManualCommit.commitsHeldTransaction(["UPDATE t SET a = 1"], databaseType: mysql, grammar: mysql.lexicalGrammar)) + let postgres = DatabaseType.postgresql + #expect(!ManualCommit.commitsHeldTransaction( + ["ALTER TABLE t ADD c INT"], databaseType: postgres, grammar: postgres.lexicalGrammar + )) + } + + @Test("A run that mixes a write with a statement that can't run inside a transaction is refused") + func autocommitOnlyBesideAWriteRefuses() { + #expect(decision(["UPDATE t SET a = 1", "VACUUM t"]) == .refuseMixed) + #expect(decision(["DELETE FROM t", "CREATE INDEX CONCURRENTLY i ON t (a)"]) == .refuseMixed) + #expect(decision(["VACUUM t"]) == .runAsIs) + } + + @Test("On MySQL a run that mixes a write with DDL is refused, and DDL alone runs as typed") + func implicitCommitBesideAWriteRefuses() { + #expect(decision(["UPDATE t SET a = 1", "ALTER TABLE u ADD c INT"], type: .mysql) == .refuseMixed) + #expect(decision(["ALTER TABLE u ADD c INT"], type: .mysql) == .runAsIs) + #expect(decision(["UPDATE t SET a = 1", "ALTER TABLE u ADD c INT"], type: .postgresql) == .hold) + } + + @Test("Two reads in Manual Commit are not held: the run keeps the plan an automatic connection uses") + func readOnlyRunIsNotHeld() { + let reads = ["SELECT 1", "SELECT 2"] + let decided = decision(reads) + #expect(decided == .runAsIs) + #expect(ManualCommit.runPlan(text: .appTransaction, decision: decided, sessionState: .idle) == .appTransaction) + #expect(ManualCommit.runPlan(text: .appTransaction, decision: .hold, sessionState: .idle) == .heldTransaction) + #expect(ManualCommit.runPlan(text: .autocommit, decision: .runAsIs, sessionState: .inTransaction) == .sessionTransaction) + } + + @Test("An EXPLAIN ANALYZE of a write is held like the write") + func explainAnalyzeOfAWriteIsHeld() { + #expect(decision("EXPLAIN ANALYZE DELETE FROM orders WHERE id = 1") == .hold) + #expect(decision("EXPLAIN SELECT * FROM orders") == .runAsIs) + } + + @Test("A read that timed out after the turn's own BEGIN records the hold it opened") + func timedOutReadAfterTheTurnsBeginIsOpen() { + #expect(ManualCommit.recordedState(read: .unknown, turnOpenedHold: true) == .inTransaction) + #expect(ManualCommit.recordedState(read: .unknown, turnOpenedHold: false) == .unknown) + #expect(ManualCommit.recordedState(read: .idle, turnOpenedHold: true) == .idle) + } + + @Test("COMMIT, ROLLBACK, BEGIN, SAVEPOINT and END control the transaction; a PL/SQL block does not") + func transactionControlIsRecognised() { + let postgres = DatabaseType.postgresql + for sql in ["COMMIT", "ROLLBACK", "ROLLBACK TO SAVEPOINT a", "BEGIN", "START TRANSACTION", "SAVEPOINT a", "END"] { + #expect(ManualCommit.controlsTransaction([sql], databaseType: postgres, grammar: postgres.lexicalGrammar), "\(sql)") + } + #expect(!ManualCommit.controlsTransaction(["UPDATE t SET a = 1"], databaseType: postgres, grammar: postgres.lexicalGrammar)) + let oracle = DatabaseType.oracle + #expect(!ManualCommit.controlsTransaction( + ["BEGIN UPDATE t SET a = 1; END;"], databaseType: oracle, grammar: oracle.lexicalGrammar + )) + } + + @Test("Over an open transaction, an agent statement takes a savepoint where one exists and is refused where none does") + func agentProtectionFollowsTheFamily() { + #expect(AgentTransactionGuard.protection(family: .postgres, sessionState: .inTransaction, controlsTransaction: false) == .savepoint) + #expect(AgentTransactionGuard.protection(family: .cockroach, sessionState: .inTransaction, controlsTransaction: false) == .savepoint) + #expect(AgentTransactionGuard.protection(family: .duckdb, sessionState: .inTransaction, controlsTransaction: false) == .refuse) + #expect(AgentTransactionGuard.protection(family: .redshift, sessionState: .inTransaction, controlsTransaction: false) == .refuse) + #expect(AgentTransactionGuard.protection(family: .mysql, sessionState: .inTransaction, controlsTransaction: false) == .none) + #expect(AgentTransactionGuard.protection(family: .postgres, sessionState: .idle, controlsTransaction: false) == .none) + #expect(AgentTransactionGuard.protection(family: .postgres, sessionState: .inTransaction, controlsTransaction: true) == .none) + } + + @Test("Unseen transaction control: a T-SQL COMMIT inside a batch, an IF, a procedure call") + func unseenTransactionControlIsRecognised() { + let mssql = DatabaseType.mssql + for sql in ["UPDATE t SET a = 1 COMMIT", "IF @@TRANCOUNT > 0 ROLLBACK", "EXEC dbo.p", "SAVE TRAN s"] { + #expect(ManualCommit.mayEndTransactionUnseen([sql], databaseType: mssql, grammar: mssql.lexicalGrammar), "\(sql)") + } + let mysql = DatabaseType.mysql + #expect(ManualCommit.mayEndTransactionUnseen(["CALL p()"], databaseType: mysql, grammar: mysql.lexicalGrammar)) + for sql in ["UPDATE t SET a = CASE WHEN b = 1 THEN 1 ELSE 2 END", "SELECT 'commit'", "UPDATE t SET a = 1"] { + #expect(!ManualCommit.mayEndTransactionUnseen([sql], databaseType: mysql, grammar: mysql.lexicalGrammar), "\(sql)") + } + } + + @Test("The editor never offers Commit and Run for a run Manual Commit refuses") + func noPromptForARefusedRun() { + let mysql = DatabaseType.mysql + let mixed = ["UPDATE a SET x = 1", "ALTER TABLE b ADD c INT"] + #expect(!ManualCommit.asksBeforeImplicitCommit(mixed, databaseType: mysql, grammar: mysql.lexicalGrammar, commitMode: .manual)) + #expect(ManualCommit.asksBeforeImplicitCommit(mixed, databaseType: mysql, grammar: mysql.lexicalGrammar, commitMode: .automatic)) + #expect(ManualCommit.asksBeforeImplicitCommit( + ["ALTER TABLE b ADD c INT"], databaseType: mysql, grammar: mysql.lexicalGrammar, commitMode: .manual + )) + #expect(!ManualCommit.asksBeforeImplicitCommit( + ["UPDATE a SET x = 1"], databaseType: mysql, grammar: mysql.lexicalGrammar, commitMode: .automatic + )) + } + + @Test("A reply says the transaction ended when it did, and that a held change is pending otherwise") + func agentNoticeFollowsTheEndState() { + #expect(AgentTransactionGuard.notice(startState: .inTransaction, endState: .idle, pendingMatters: true) + == ManualCommit.transactionEndedNotice) + #expect(AgentTransactionGuard.notice(startState: .inTransaction, endState: .idle, pendingMatters: false) + == ManualCommit.transactionEndedNotice) + #expect(AgentTransactionGuard.notice(startState: .idle, endState: .inTransaction, pendingMatters: true) + == ManualCommit.pendingNotice) + #expect(AgentTransactionGuard.notice(startState: .inTransaction, endState: .inTransaction, pendingMatters: false) == nil) + } + + @Test("The refusal says how to run the change") + func refusalNamesTheWayOut() { + #expect( + ManualCommitRefusal().errorDescription + == "This connection can't hold this change until you commit, so it did not run. Turn off Manual Commit in the Database menu to run it." + ) + #expect(ManualCommit.Decision.refuseMixed.refusal?.localizedDescription == ManualCommitMixedRefusal().errorDescription) + } +} + +struct ConnectionCommitModeStorageTests { + @Test("Manual Commit round-trips through the connection's fields, and Automatic removes the key") + func commitModeRoundTrips() { + var connection = TestFixtures.makeConnection(type: .postgresql) + #expect(connection.commitMode == .automatic) + #expect(connection.additionalFields[DatabaseConnection.commitModeKey] == nil) + + connection.commitMode = .manual + #expect(connection.additionalFields[DatabaseConnection.commitModeKey] == "manual") + #expect(connection.commitMode == .manual) + + connection.commitMode = .automatic + #expect(connection.additionalFields[DatabaseConnection.commitModeKey] == nil) + } + + @Test("A session starts in the connection's commit mode") + func sessionSeedsFromTheConnection() { + var connection = TestFixtures.makeConnection(type: .postgresql) + connection.commitMode = .manual + #expect(ConnectionSession(connection: connection).commitMode == .manual) + } + + @Test("The form writes Manual Commit into the saved fields, and turning it off removes the key") + func formEditsCarryTheMode() { + var base = TestFixtures.makeConnection(type: .postgresql) + base.commitMode = .manual + #expect(ConnectionFormEditsSample.edits(commitMode: .automatic).applied(to: base).commitMode == .automatic) + let saved = ConnectionFormEditsSample.edits(commitMode: .manual).applied(to: TestFixtures.makeConnection()) + #expect(saved.additionalFields[DatabaseConnection.commitModeKey] == "manual") + } +} + +private enum ConnectionFormEditsSample { + static func edits(commitMode: ConnectionCommitMode) -> ConnectionFormEdits { + ConnectionFormEdits( + name: "Edited", + host: "db.example.com", + port: 6_000, + database: "edited_db", + username: "edited_user", + type: .postgresql, + sshConfig: SSHConfiguration(), + sslConfig: SSLConfiguration(), + color: .blue, + iconName: nil, + tagIds: [], + groupId: nil, + sshProfileId: nil, + sshTunnelMode: .disabled, + credentialMode: .inline, + cloudflareTunnelMode: .disabled, + cloudSQLProxyMode: .disabled, + socksProxyMode: .disabled, + tunnelCommandMode: .disabled, + safeModeLevel: .silent, + aiPolicy: nil, + aiRules: nil, + externalAccess: .readOnly, + commitMode: commitMode, + redisDatabase: nil, + startupCommands: nil, + localOnly: false, + additionalFields: [:], + ownedAdditionalFieldIDs: ConnectionFormEdits.appManagedAdditionalFieldIDs + ) + } +} diff --git a/apple/TableProTests/Core/Services/Execution/MultiStatementFailureTests.swift b/apple/TableProTests/Core/Services/Execution/MultiStatementFailureTests.swift index 6b31814b93..8e464169e4 100644 --- a/apple/TableProTests/Core/Services/Execution/MultiStatementFailureTests.swift +++ b/apple/TableProTests/Core/Services/Execution/MultiStatementFailureTests.swift @@ -107,7 +107,7 @@ struct MultiStatementFailureTests { ) #expect(firstStatement.message == "Statement 1/3 failed: database is locked") - for plan in [BatchTransactionPlan.appTransaction, .scriptTransaction] { + for plan in [BatchTransactionPlan.appTransaction, .heldTransaction, .scriptTransaction] { let rolledBack = Self.report( .statement(sql: "VACUUM"), executed: 2, @@ -228,7 +228,7 @@ struct MultiStatementFailureTests { /// `.appTransaction` and a script's own commit under `.scriptTransaction` are both unanswered. @Test( "The unknown-outcome wording does not change with the plan", - arguments: [BatchTransactionPlan.appTransaction, .scriptTransaction, .sessionTransaction, .autocommit] + arguments: [BatchTransactionPlan.appTransaction, .heldTransaction, .scriptTransaction, .sessionTransaction, .autocommit] ) func unknownCommitOutcomeReadsTheSameUnderEveryPlan(plan: BatchTransactionPlan) { let report = Self.report( diff --git a/apple/TableProTests/Core/Services/Infrastructure/QuitTransactionPromptTests.swift b/apple/TableProTests/Core/Services/Infrastructure/QuitTransactionPromptTests.swift new file mode 100644 index 0000000000..d2d1425135 --- /dev/null +++ b/apple/TableProTests/Core/Services/Infrastructure/QuitTransactionPromptTests.swift @@ -0,0 +1,56 @@ +// +// QuitTransactionPromptTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +@MainActor +struct QuitTransactionPromptTests { + @Test("Quit names the connection holding a transaction and offers Quit Anyway") + func quitNamesTheOpenTransaction() throws { + let prompt = try #require(QuitApproval.transactionPrompt(action: .quit, connections: ["Prod"], hasUnsavedTabs: false)) + #expect(prompt.title == "Quit with an Open Transaction?") + #expect(prompt.message == "Uncommitted changes on “Prod” are rolled back when TablePro quits.") + #expect(prompt.confirmButton == "Quit Anyway") + } + + @Test("Unsaved tabs ride along on the transaction question rather than asking again") + func unsavedTabsRideAlong() throws { + let prompt = try #require(QuitApproval.transactionPrompt(action: .quit, connections: ["Prod"], hasUnsavedTabs: true)) + #expect(prompt.message.hasSuffix("Some tabs have unsaved edits. Quitting will discard these changes.")) + } + + @Test("No open transaction asks nothing") + func noTransactionAsksNothing() { + #expect(QuitApproval.transactionPrompt(action: .quit, connections: [], hasUnsavedTabs: true) == nil) + } + + @Test("Each connection holding a transaction is named once, whatever its lanes") + func holdsAreNamedOncePerConnection() { + let prod = UUID() + let staging = UUID() + let driver = PluginDriverAdapter(connection: TestFixtures.makeConnection(), pluginDriver: QuitStubDriver()) + let tracker = SessionTransactionTracker { _, _ in true } + tracker.record(.inTransaction, on: driver, for: .init(connectionId: prod, lane: "sales")) + tracker.record(.abortedTransaction, on: driver, for: .init(connectionId: prod, lane: "hr")) + tracker.record(.inTransaction, on: driver, for: .init(connectionId: staging, lane: nil)) + let names = [prod: "Prod", staging: "Staging"] + + let named = QuitApproval.openTransactionNames( + holds: Array(tracker.holds.values), + connectionName: { names[$0] } + ) + + #expect(named == ["Prod", "Staging"]) + } +} + +private final class QuitStubDriver: PluginDriverStub, @unchecked Sendable { + var supportsSchemas: Bool { false } + var currentSchema: String? { nil } + var serverVersion: String? { nil } +} diff --git a/apple/TableProTests/Core/Services/Query/DatabaseTreeCatalogRefreshPlanTests.swift b/apple/TableProTests/Core/Services/Query/DatabaseTreeCatalogRefreshPlanTests.swift index 4acaa2c342..5c17abcde2 100644 --- a/apple/TableProTests/Core/Services/Query/DatabaseTreeCatalogRefreshPlanTests.swift +++ b/apple/TableProTests/Core/Services/Query/DatabaseTreeCatalogRefreshPlanTests.swift @@ -200,4 +200,74 @@ struct DatabaseTreeCatalogRefreshPlanTests { #expect(result.allSchemaTables.isEmpty) #expect(result.isEmpty) } + + private typealias ServerObjectsKey = DatabaseTreeMetadataService.ServerObjectsKey + + private func serverObjectPlan(_ change: CatalogChange, keys: [ServerObjectsKey]) -> CatalogTreeRefreshPlan { + DatabaseTreeMetadataService.catalogRefreshPlan( + for: change, + hasDatabaseList: false, + schemaListKeys: [], + tableKeys: [], + routineKeys: [], + triggerKeys: [], + typeKeys: [], + serverObjectKeys: keys + ) + } + + @Test("An event change marks the container's events stale and leaves another database current") + func eventChangeReachesItsContainer() { + let shopEvents = ServerObjectsKey(connectionId: connectionId, database: "shop", schema: nil, kindId: "event") + let warehouseEvents = ServerObjectsKey(connectionId: connectionId, database: "warehouse", schema: nil, kindId: "event") + let otherConnection = ServerObjectsKey(connectionId: UUID(), database: "shop", schema: nil, kindId: "event") + + let result = serverObjectPlan( + CatalogChange(connectionId: connectionId, database: "shop", kinds: [.triggers, .serverObjects]), + keys: [shopEvents, warehouseEvents, otherConnection] + ) + + #expect(result.serverObjects == [shopEvents]) + #expect(!result.isEmpty) + } + + @Test("A database-wide list is reached by a change made in any of its schemas") + func databaseScopedListIsReachedFromASchema() { + let extensions = ServerObjectsKey(connectionId: connectionId, database: "app", schema: nil, kindId: "extension") + let auditEvents = ServerObjectsKey(connectionId: connectionId, database: "app", schema: "audit", kindId: "event") + + let result = serverObjectPlan( + CatalogChange(connectionId: connectionId, database: "app", schema: "public", kinds: .objects), + keys: [extensions, auditEvents] + ) + + #expect(result.serverObjects == [extensions]) + } + + @Test("A container lists the declared kinds of its own level") + func serverKindsFollowTheContainerLevel() { + let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + let extensions = SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", iconName: "puzzlepiece.extension", + scope: .database + ) + let declared = [events, extensions] + + #expect(DatabaseTreeMetadataService.serverKinds(declared: declared, schema: "public", listsTablesPerSchema: true) == [events]) + #expect(DatabaseTreeMetadataService.serverKinds(declared: declared, schema: nil, listsTablesPerSchema: true) == [extensions]) + #expect(DatabaseTreeMetadataService.serverKinds(declared: declared, schema: nil, listsTablesPerSchema: false) == declared) + #expect(DatabaseTreeMetadataService.serverKinds(declared: declared, schema: "", listsTablesPerSchema: true) == [extensions]) + } + + @Test("A change that names no server object kind leaves those lists current") + func unrelatedChangeLeavesServerObjects() { + let shopEvents = ServerObjectsKey(connectionId: connectionId, database: "shop", schema: nil, kindId: "event") + + let result = serverObjectPlan(CatalogChange(connectionId: connectionId, database: "shop", kinds: .tables), keys: [shopEvents]) + + #expect(result.serverObjects.isEmpty) + #expect(result.isEmpty) + } } diff --git a/apple/TableProTests/Core/Storage/ConnectionLocalStatePurgeTests.swift b/apple/TableProTests/Core/Storage/ConnectionLocalStatePurgeTests.swift index ee2543d04e..206db0c446 100644 --- a/apple/TableProTests/Core/Storage/ConnectionLocalStatePurgeTests.swift +++ b/apple/TableProTests/Core/Storage/ConnectionLocalStatePurgeTests.swift @@ -256,4 +256,29 @@ struct ConnectionLocalStatePurgeTests { defaults.string(forKey: RowInspectorState.viewModeKey(kept)) == InspectorViewMode.json.rawValue ) } + + @Test("Purging a connection removes its query tabs' versions and leaves another's") + func purgeClearsQueryTabVersions() async { + let (history, _) = makeHistory() + let versions = QueryTabVersionStorage( + databaseURL: FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("purge_tab_versions_\(UUID().uuidString).db"), + announce: { _ in }, + removeDatabaseOnDeinit: true + ) + let deleted = UUID() + let kept = UUID() + let deletedTab = UUID() + let keptTab = UUID() + await versions.record(tabId: deletedTab, connectionId: deleted, text: "SELECT secret", reason: .run) + await versions.record(tabId: keptTab, connectionId: kept, text: "SELECT 1", reason: .run) + + await ConnectionLocalState.purgeAsyncStores( + [deleted], origin: .remote, queryHistory: history, queryTabVersions: versions + ) + + #expect(versions.summaries(tabId: deletedTab).isEmpty) + #expect(versions.summaries(tabId: keptTab).count == 1) + } } diff --git a/apple/TableProTests/Core/Storage/QueryTabVersionStorageTests.swift b/apple/TableProTests/Core/Storage/QueryTabVersionStorageTests.swift new file mode 100644 index 0000000000..b6d8f3c451 --- /dev/null +++ b/apple/TableProTests/Core/Storage/QueryTabVersionStorageTests.swift @@ -0,0 +1,255 @@ +// +// QueryTabVersionStorageTests.swift +// TableProTests +// + +import Foundation +import os +@testable import TablePro +import Testing + +struct QueryTabVersionStorageTests { + private static let start = Date(timeIntervalSince1970: 1_800_000_000) + + private let url: URL + private let now = OSAllocatedUnfairLock(initialState: QueryTabVersionStorageTests.start) + private let historyMaxDays = OSAllocatedUnfairLock(initialState: 90) + private let announced = OSAllocatedUnfairLock(initialState: [UUID?]()) + private let storage: QueryTabVersionStorage + private let tabId = UUID() + private let connectionId = UUID() + + init() { + let url = FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("query_tab_versions_\(UUID().uuidString).db") + self.url = url + storage = Self.makeStorage( + url: url, + now: now, + historyMaxDays: historyMaxDays, + announced: announced, + removeDatabaseOnDeinit: true + ) + } + + private static func makeStorage( + url: URL, + now: OSAllocatedUnfairLock, + historyMaxDays: OSAllocatedUnfairLock, + announced: OSAllocatedUnfairLock<[UUID?]>, + removeDatabaseOnDeinit: Bool + ) -> QueryTabVersionStorage { + QueryTabVersionStorage( + databaseURL: url, + now: { now.withLock { $0 } }, + historyMaxDays: { historyMaxDays.withLock { $0 } }, + announce: { tabId in announced.withLock { $0.append(tabId) } }, + removeDatabaseOnDeinit: removeDatabaseOnDeinit + ) + } + + private func reopenedStorage() -> QueryTabVersionStorage { + Self.makeStorage( + url: url, + now: now, + historyMaxDays: historyMaxDays, + announced: announced, + removeDatabaseOnDeinit: false + ) + } + + private func advance(by seconds: TimeInterval) { + now.withLock { $0 = $0.addingTimeInterval(seconds) } + } + + @discardableResult + private func record(_ text: String, _ reason: QueryTabVersionReason = .run, tab: UUID? = nil) async -> Bool { + await storage.record(tabId: tab ?? tabId, connectionId: connectionId, text: text, reason: reason) + } + + private func texts(tab: UUID? = nil) -> [String] { + let id = tab ?? tabId + return storage.summaries(tabId: id, limit: 200).compactMap { storage.version(id: $0.id, tabId: id)?.query } + } + + @Test("Blank text is not recorded") + func blankTextIsSkipped() async { + #expect(await record(" \n\t ") == false) + #expect(storage.summaries(tabId: tabId).isEmpty) + } + + @Test("Text larger than a tab can persist is not recorded") + func oversizeTextIsSkipped() async { + let oversize = String(repeating: "x", count: TabQueryContent.maxPersistableQuerySize + 1) + #expect(await record(oversize) == false) + #expect(storage.summaries(tabId: tabId).isEmpty) + } + + @Test("Text identical to the tab's newest version is not recorded again") + func duplicateOfNewestIsSkipped() async { + #expect(await record("SELECT 1")) + #expect(await record("SELECT 1", .beforeReplace) == false) + #expect(await record("SELECT 2")) + #expect(await record("SELECT 1")) + #expect(texts() == ["SELECT 1", "SELECT 2", "SELECT 1"]) + } + + @Test("A tab keeps its 50 newest versions") + func capKeepsTheNewest() async { + for index in 1...55 { + await record("SELECT \(index)") + } + let kept = texts() + #expect(kept.count == QueryTabVersionStorage.retainedVersionCount) + #expect(kept.first == "SELECT 55") + #expect(kept.last == "SELECT 6") + } + + @Test("The cap counts each tab on its own") + func capIsPerTab() async { + let other = UUID() + for index in 1...55 { + await record("SELECT \(index)") + } + await record("SELECT other", tab: other) + #expect(texts(tab: other) == ["SELECT other"]) + } + + @Test("Versions older than 30 days are gone, read or written") + func versionsExpireAfterThirtyDays() async { + await record("SELECT 1") + advance(by: 29 * 86_400) + await record("SELECT 2") + #expect(texts() == ["SELECT 2", "SELECT 1"]) + + advance(by: 2 * 86_400) + #expect(texts() == ["SELECT 2"]) + + await record("SELECT 3") + let reopened = reopenedStorage() + #expect(reopened.summaries(tabId: tabId).count == 2) + } + + @Test("A shorter Query History window shortens the retention", arguments: [(7, 7), (90, 30), (0, 30)]) + func historyWindowCapsRetention(historyDays: Int, expected: Int) async { + historyMaxDays.withLock { $0 = historyDays } + #expect(storage.retentionDays == expected) + + await record("SELECT 1") + advance(by: TimeInterval(expected) * 86_400 + 1) + #expect(storage.summaries(tabId: tabId).isEmpty) + } + + @Test("Idle snapshots within five minutes of the first become one version holding the latest text") + func idleSnapshotsCoalesce() async throws { + await record("SELECT 1", .idle) + advance(by: 60) + await record("SELECT 12", .idle) + advance(by: 200) + await record("SELECT 123", .idle) + + let summaries = storage.summaries(tabId: tabId) + let only = try #require(summaries.first) + #expect(summaries.count == 1) + #expect(only.reason == .idle) + #expect(only.savedAt == Self.start.addingTimeInterval(260)) + #expect(texts() == ["SELECT 123"]) + } + + @Test("Steady typing still leaves a version every five minutes") + func idleWindowCountsFromTheFirstSnapshot() async { + await record("SELECT 1", .idle) + for step in 2...4 { + advance(by: 120) + await record("SELECT \(step)", .idle) + } + #expect(texts() == ["SELECT 4", "SELECT 3"]) + } + + @Test("A run between idle snapshots keeps both sides of it") + func runBreaksTheIdleWindow() async { + await record("SELECT 1", .idle) + advance(by: 10) + await record("SELECT 2", .run) + advance(by: 10) + await record("SELECT 3", .idle) + #expect(texts() == ["SELECT 3", "SELECT 2", "SELECT 1"]) + #expect(storage.summaries(tabId: tabId).map { $0.reason } == [.idle, .run, .idle]) + } + + @Test("A restore of the idle text seals it, so the next pause cannot overwrite it") + func restoreSealsTheIdleVersion() async { + await record("SELECT before", .idle) + advance(by: 10) + await record("SELECT before", .beforeRestore) + advance(by: 10) + await record("SELECT restored", .idle) + #expect(texts() == ["SELECT restored", "SELECT before"]) + #expect(storage.summaries(tabId: tabId).map { $0.reason } == [.idle, .beforeRestore]) + } + + @Test("A coalesced pause takes the connection the tab moved to") + func coalescedPauseFollowsTheConnection() async { + let moved = UUID() + await record("SELECT 1", .idle) + advance(by: 10) + _ = await storage.record(tabId: tabId, connectionId: moved, text: "SELECT 2", reason: .idle) + #expect(await storage.removeVersions(forConnection: moved)) + #expect(storage.summaries(tabId: tabId).isEmpty) + } + + @Test("A run, a replace and a restore always add a version") + func otherReasonsAlwaysInsert() async { + await record("SELECT 1", .run) + await record("SELECT 2", .beforeReplace) + await record("SELECT 3", .beforeRestore) + #expect(storage.summaries(tabId: tabId).map { $0.reason } == [.beforeRestore, .beforeReplace, .run]) + } + + @Test("Deleting a connection removes its tabs' versions and leaves another's") + func purgeByConnection() async { + let otherTab = UUID() + await record("SELECT 1") + _ = await storage.record(tabId: otherTab, connectionId: UUID(), text: "SELECT kept", reason: .run) + + #expect(await storage.removeVersions(forConnection: connectionId)) + #expect(storage.summaries(tabId: tabId).isEmpty) + #expect(texts(tab: otherTab) == ["SELECT kept"]) + } + + @Test("Clear History removes every version") + func removeAllClearsEveryTab() async { + let otherTab = UUID() + await record("SELECT 1") + await record("SELECT 2", tab: otherTab) + + #expect(await storage.removeAll()) + #expect(storage.summaries(tabId: tabId).isEmpty) + #expect(storage.summaries(tabId: otherTab).isEmpty) + } + + @Test("Each write is announced with its tab, and a purge with none") + func writesAreAnnounced() async { + await record("SELECT 1") + await record("SELECT 1") + await storage.removeAll() + #expect(announced.withLock { $0 } == [tabId, nil]) + } + + @Test("A second store on the same file reads what the first wrote") + func secondInstanceReadsTheRows() async throws { + await record("SELECT 1") + let second = reopenedStorage() + + let summary = try #require(second.summaries(tabId: tabId).first) + #expect(second.version(id: summary.id, tabId: tabId)?.query == "SELECT 1") + #expect(second.version(id: summary.id, tabId: UUID()) == nil) + } + + @Test("Before the first write there is nothing to read and no file is made") + func readBeforeAnyWriteIsEmpty() { + #expect(storage.summaries(tabId: tabId).isEmpty) + #expect(!FileManager.default.fileExists(atPath: url.path(percentEncoded: false))) + } +} diff --git a/apple/TableProTests/Core/VersionHistory/QueryTabVersionHistoryProviderTests.swift b/apple/TableProTests/Core/VersionHistory/QueryTabVersionHistoryProviderTests.swift new file mode 100644 index 0000000000..cc68ed7f09 --- /dev/null +++ b/apple/TableProTests/Core/VersionHistory/QueryTabVersionHistoryProviderTests.swift @@ -0,0 +1,158 @@ +// +// QueryTabVersionHistoryProviderTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +@MainActor +private final class FakeQueryTab { + let connectionId = UUID() + var text: String? + var isCapturePaused = false + private(set) var writes: [String] = [] + private(set) var reasonsAtWrite: [[QueryTabVersionReason]] = [] + + init(text: String?) { + self.text = text + } + + func write(_ newText: String, storage: QueryTabVersionStorage, tabId: UUID) -> Bool { + guard text != nil else { return false } + reasonsAtWrite.append(storage.summaries(tabId: tabId).map { $0.reason }) + writes.append(newText) + text = newText + return true + } +} + +@MainActor +struct QueryTabVersionHistoryProviderTests { + private let storage = QueryTabVersionStorage( + databaseURL: FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("query_tab_provider_\(UUID().uuidString).db"), + announce: { _ in }, + removeDatabaseOnDeinit: true + ) + private let tabId = UUID() + + private func provider(for tab: FakeQueryTab) -> QueryTabVersionHistoryProvider { + QueryTabVersionHistoryProvider( + tabId: tabId, + storage: storage, + liveTab: { tab.text.map { LiveQueryTab(connectionId: tab.connectionId, text: $0) } }, + write: { tab.write($0, storage: storage, tabId: tabId) }, + isCapturePaused: { tab.isCapturePaused } + ) + } + + private func recordPast(_ texts: [String], connectionId: UUID) async { + for text in texts { + await storage.record(tabId: tabId, connectionId: connectionId, text: text, reason: .run) + } + } + + @Test("The page is the current text, then each version newest first, with the retention notice") + func pageListsCurrentThenNewestFirst() async throws { + let tab = FakeQueryTab(text: "SELECT 3") + await recordPast(["SELECT 1", "SELECT 2"], connectionId: tab.connectionId) + + let page = try await provider(for: tab).loadHistory() + + #expect(page.entries.count == 3) + #expect(page.entries.first?.reference == .current) + #expect(page.entries.dropFirst().allSatisfy { $0.summary == String(localized: "Run") }) + #expect(page.notice == .keepsRecentVersions(count: 50, days: storage.retentionDays)) + let newest = try #require(page.entries.dropFirst().first) + #expect(try await provider(for: tab).content(of: newest.reference) == "SELECT 2") + } + + @Test("The current entry reads the tab's live text") + func currentContentIsTheLiveText() async throws { + let tab = FakeQueryTab(text: "SELECT live") + #expect(try await provider(for: tab).content(of: .current) == "SELECT live") + } + + @Test("A version of another tab, or one that is gone, is not found") + func foreignVersionIsNotFound() async throws { + let tab = FakeQueryTab(text: "SELECT 1") + let otherTab = UUID() + await storage.record(tabId: otherTab, connectionId: tab.connectionId, text: "SELECT other", reason: .run) + let otherId = try #require(storage.summaries(tabId: otherTab).first?.id) + + await #expect(throws: VersionHistoryError.versionNotFound) { + try await provider(for: tab).content(of: .tabVersion(id: otherId)) + } + await #expect(throws: VersionHistoryError.versionNotFound) { + try await provider(for: tab).prepareRestore(.tabVersion(id: 999)) + } + } + + @Test("Restore records the text it replaces first, then writes the version into the tab") + func restoreRecordsThenWrites() async throws { + let tab = FakeQueryTab(text: "SELECT 1") + await recordPast(["SELECT 1"], connectionId: tab.connectionId) + let version = try #require(storage.summaries(tabId: tabId).first) + tab.text = "SELECT typed since" + + let plan = try await provider(for: tab).prepareRestore(.tabVersion(id: version.id)) + #expect(plan.replacesUncommittedChanges == false) + try await plan.apply() + + #expect(tab.writes == ["SELECT 1"]) + #expect(tab.reasonsAtWrite == [[.beforeRestore, .run]]) + let replaced = try #require(storage.summaries(tabId: tabId).first) + #expect(storage.version(id: replaced.id, tabId: tabId)?.query == "SELECT typed since") + } + + @Test("With capture paused, restore writes the version and records nothing") + func pausedRestoreRecordsNothing() async throws { + let tab = FakeQueryTab(text: "SELECT 1") + await recordPast(["SELECT 1"], connectionId: tab.connectionId) + let version = try #require(storage.summaries(tabId: tabId).first) + tab.text = "SELECT typed since" + tab.isCapturePaused = true + + try await provider(for: tab).prepareRestore(.tabVersion(id: version.id)).apply() + + #expect(tab.writes == ["SELECT 1"]) + #expect(storage.summaries(tabId: tabId).map { $0.id } == [version.id]) + } + + @Test("A closed tab says so for its list, its text and a restore") + func closedTabThrowsTabClosed() async throws { + let tab = FakeQueryTab(text: nil) + await recordPast(["SELECT 1"], connectionId: tab.connectionId) + let version = try #require(storage.summaries(tabId: tabId).first) + + await #expect(throws: VersionHistoryError.tabClosed) { try await provider(for: tab).loadHistory() } + await #expect(throws: VersionHistoryError.tabClosed) { try await provider(for: tab).content(of: .current) } + await #expect(throws: VersionHistoryError.tabClosed) { + try await provider(for: tab).prepareRestore(.tabVersion(id: version.id)) + } + #expect(try await provider(for: tab).content(of: .tabVersion(id: version.id)) == "SELECT 1") + } + + @Test("A tab that closes between prepare and apply is not written") + func tabClosedBeforeApplyIsNotWritten() async throws { + let tab = FakeQueryTab(text: "SELECT 2") + await recordPast(["SELECT 1"], connectionId: tab.connectionId) + let version = try #require(storage.summaries(tabId: tabId).first) + let plan = try await provider(for: tab).prepareRestore(.tabVersion(id: version.id)) + tab.text = nil + + await #expect(throws: VersionHistoryError.tabClosed) { try await plan.apply() } + #expect(tab.writes.isEmpty) + } + + @Test("Each reason names its version, and an idle snapshot is titled by its date") + func reasonSummaries() { + #expect(QueryTabVersionReason.run.summary == String(localized: "Run")) + #expect(QueryTabVersionReason.beforeReplace.summary == String(localized: "Before Replace")) + #expect(QueryTabVersionReason.beforeRestore.summary == String(localized: "Before Restore")) + #expect(QueryTabVersionReason.idle.summary == nil) + } +} diff --git a/apple/TableProTests/Core/VersionHistory/QueryTabVersionSubjectTests.swift b/apple/TableProTests/Core/VersionHistory/QueryTabVersionSubjectTests.swift new file mode 100644 index 0000000000..b1ad41bca3 --- /dev/null +++ b/apple/TableProTests/Core/VersionHistory/QueryTabVersionSubjectTests.swift @@ -0,0 +1,66 @@ +// +// QueryTabVersionSubjectTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct QueryTabVersionSubjectTests { + @Test("A history tab for a query tab round-trips the tab it shows") + func queryTabSubjectRoundTrips() throws { + let subject = VersionHistorySubject.queryTab(id: UUID()) + let tab = PersistedTab( + id: UUID(), + title: "History: Query 1", + query: "", + tabType: .versionHistory, + tableName: nil, + versionHistorySubject: subject + ) + + let decoded = try JSONDecoder().decode(PersistedTab.self, from: try JSONEncoder().encode(tab)) + + #expect(decoded.tabType == .versionHistory) + #expect(decoded.versionHistorySubject == subject) + } + + @Test("The saved query and file subjects keep the encoding older builds wrote") + func existingSubjectsDecodeUnchanged() throws { + let favoriteId = UUID() + let json = Data(#"{"savedQuery":{"id":"\#(favoriteId.uuidString)"}}"#.utf8) + #expect(try JSONDecoder().decode(VersionHistorySubject.self, from: json) == .savedQuery(id: favoriteId)) + } + + @Test("The notice names both the count and the days") + func noticeNamesCountAndDays() { + let text = VersionHistoryFormatting.noticeText(.keepsRecentVersions(count: 50, days: 7)) + #expect(text == String( + format: String(localized: "Keeps up to %1$d versions from the last %2$d days on this Mac."), + 50, + 7 + )) + } + + @Test("A Revert To row leads with the date, then names a run") + func menuTitleLeadsWithTheDate() { + let date = Date(timeIntervalSince1970: 1_800_000_000) + let dateText = date.formatted(date: .abbreviated, time: .shortened) + let run = VersionHistoryEntry(reference: .tabVersion(id: 1), summary: String(localized: "Run"), date: date) + let idle = VersionHistoryEntry(reference: .tabVersion(id: 2), summary: nil, date: date) + + #expect(VersionHistoryFormatting.menuTitle(for: run) == String( + format: String(localized: "%1$@ (%2$@)"), + dateText, + String(localized: "Run") + )) + #expect(VersionHistoryFormatting.menuTitle(for: idle) == dateText) + } + + @Test("A closed tab's message tells the reader how to get the history back") + func closedTabMessage() { + #expect(VersionHistoryError.tabClosed.errorDescription + == String(localized: "This tab is closed. Reopen it to see its history.")) + } +} diff --git a/apple/TableProTests/Models/DatabaseObjectRefCodingTests.swift b/apple/TableProTests/Models/DatabaseObjectRefCodingTests.swift index 65ee4448c4..c7c843e7b3 100644 --- a/apple/TableProTests/Models/DatabaseObjectRefCodingTests.swift +++ b/apple/TableProTests/Models/DatabaseObjectRefCodingTests.swift @@ -32,4 +32,102 @@ struct DatabaseObjectRefCodingTests { #expect(ref.resolvingDatabase("shop").typeKind == .range) #expect(ref.resolvingDatabase("shop").database == "shop") } + + private var eventKind: SidebarServerObjectKind { + SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + } + + @Test("A ref written before server objects decodes with no server kind") + func legacyRefHasNoServerKind() throws { + let json = """ + {"kind":"trigger","name":"audit","database":"shop","schema":"public","table":"orders","attributes":[]} + """ + let decoded = try JSONDecoder().decode(DatabaseObjectRef.self, from: Data(json.utf8)) + #expect(decoded.serverKind == nil) + #expect(decoded.serverObject == nil) + } + + @Test("A server object ref round-trips its kind and reads back as the object") + func serverObjectRefRoundTrips() throws { + let object = ServerObjectInfo( + kindId: "event", name: "nightly", schema: "shop", detail: "Recurring", + attributes: [ObjectAttribute(label: "Schedule", value: "EVERY 1 DAY")] + ) + let ref = DatabaseObjectRef(serverObject: object, kind: eventKind, database: "shop") + let decoded = try JSONDecoder().decode(DatabaseObjectRef.self, from: JSONEncoder().encode(ref)) + #expect(decoded == ref) + #expect(decoded.kind == .serverObject) + #expect(decoded.serverKind?.id == "event") + #expect(decoded.kindDisplayName == "Event") + #expect(decoded.kindIconName == "calendar.badge.clock") + #expect(decoded.serverObject?.kindId == "event") + #expect(decoded.serverObject?.name == "nightly") + #expect(decoded.displayIdentity == "shop.nightly") + } + + @Test("Resolving the database keeps the server kind, and the tab is titled with the kind") + @MainActor + func serverObjectRefKeepsKind() { + let ref = DatabaseObjectRef(serverObject: ServerObjectInfo(kindId: "event", name: "nightly"), kind: eventKind, database: "") + #expect(ref.resolvingDatabase("shop").serverKind?.id == "event") + #expect(QueryTabManager.objectSourceTitle(for: ref) == String(format: String(localized: "%1$@: %2$@"), "Event", "nightly")) + } + + private var extensionKind: SidebarServerObjectKind { + SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", iconName: "puzzlepiece.extension", + scope: .database + ) + } + + @Test("A database-wide object is titled by its bare name, its schema left to the attributes") + @MainActor + func databaseScopedObjectHasABareName() throws { + let object = ServerObjectInfo(kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384") + let ref = DatabaseObjectRef(serverObject: object, kind: extensionKind, database: "app") + + #expect(ref.isDatabaseScoped) + #expect(ref.displayIdentity == "pgcrypto") + #expect(QueryTabManager.objectSourceTitle(for: ref) == String(format: String(localized: "%1$@: %2$@"), "Extension", "pgcrypto")) + #expect(ref.schema == "public") + + let decoded = try JSONDecoder().decode(DatabaseObjectRef.self, from: JSONEncoder().encode(ref)) + #expect(decoded.serverKind?.scope == .database) + #expect(decoded.displayIdentity == "pgcrypto") + } + + @Test("A container object keeps its schema in its name") + func containerObjectIsQualified() { + let ref = DatabaseObjectRef(serverObject: ServerObjectInfo(kindId: "event", name: "nightly", schema: "shop"), kind: eventKind, database: "shop") + + #expect(!ref.isDatabaseScoped) + #expect(ref.displayIdentity == "shop.nightly") + } + + @Test("A server kind written without its scope decodes, and reads as a container kind") + func serverKindWithoutScopeDecodes() throws { + let json = """ + {"kind":"serverObject","name":"nightly","database":"shop","schema":"shop","attributes":[],\ + "serverKind":{"id":"event","displayName":"Event","iconName":"calendar.badge.clock"}} + """ + let decoded = try JSONDecoder().decode(DatabaseObjectRef.self, from: Data(json.utf8)) + + #expect(decoded.serverKind?.scope == nil) + #expect(!decoded.isDatabaseScoped) + #expect(decoded.displayIdentity == "shop.nightly") + } + + @Test("Two server objects of different kinds are different objects") + func serverKindSeparatesObjects() { + let extensionKind = SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", iconName: "puzzlepiece.extension", + scope: .database + ) + let event = DatabaseObjectRef(serverObject: ServerObjectInfo(kindId: "event", name: "x"), kind: eventKind, database: "shop") + let ext = DatabaseObjectRef(serverObject: ServerObjectInfo(kindId: "extension", name: "x"), kind: extensionKind, database: "shop") + #expect(!event.namesSameObject(as: ext)) + #expect(event.namesSameObject(as: event)) + } } diff --git a/apple/TableProTests/Models/ERDiagram/ERDiagramStagedOverlayTests.swift b/apple/TableProTests/Models/ERDiagram/ERDiagramStagedOverlayTests.swift index 1fc9fd2f95..ab99fb6d4c 100644 --- a/apple/TableProTests/Models/ERDiagram/ERDiagramStagedOverlayTests.swift +++ b/apple/TableProTests/Models/ERDiagram/ERDiagramStagedOverlayTests.swift @@ -85,6 +85,26 @@ struct ERDiagramStagedOverlayTests { #expect(dropped.id == saved.id) } + @Test("A staged composite key draws one edge per column pair, each marked added") + func stagedCompositeKeyDrawsEachPair() { + let graph = savedGraph() + let orders = manager(for: "orders", in: graph) + var foreignKey = EditableForeignKeyDefinition.placeholder() + foreignKey.name = "fk_orders_customer" + foreignKey.columns = ["customer_id", "shipper_id"] + foreignKey.referencedTable = "customers" + foreignKey.referencedColumns = ["id", "email"] + orders.addForeignKey(foreignKey) + + let overlaid = ERDiagramStagedOverlay.apply( + ["orders": ERDiagramStagedOverlay.TableEdits(orders)], to: graph + ) + + let edges = overlaid.edges.filter { $0.fkName == "fk_orders_customer" } + #expect(edges.map { "\($0.fromColumn)->\($0.toColumn)" } == ["customer_id->id", "shipper_id->email"]) + #expect(edges.allSatisfy { $0.stagedEdit == .added && $0.toTable == "customers" }) + } + @Test("Added, renamed and dropped columns are marked on their table") func stagedColumnsAreMarked() throws { let graph = savedGraph() diff --git a/apple/TableProTests/Models/Query/TableAutoRefreshGateTests.swift b/apple/TableProTests/Models/Query/TableAutoRefreshGateTests.swift new file mode 100644 index 0000000000..438f644dbc --- /dev/null +++ b/apple/TableProTests/Models/Query/TableAutoRefreshGateTests.swift @@ -0,0 +1,83 @@ +// +// TableAutoRefreshGateTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct TableAutoRefreshGateTests { + @Test("A visible, idle, loaded table tab refreshes") + func idleTabRefreshes() { + #expect(TableAutoRefreshGate().decision == .refresh) + } + + @Test("Each state that a reload would break waits, with its own reason") + func eachPauseStateWaits() { + let cases: [(String, (inout TableAutoRefreshGate) -> Void, TableAutoRefreshPause)] = [ + ("window hidden", { $0.windowIsVisible = false }, .notVisible), + ("tab not selected", { $0.isSelectedInWindow = false }, .notVisible), + ("disconnected", { $0.isConnected = false }, .disconnected), + ("JSON mode", { $0.viewMode = .json }, .otherViewMode), + ("structure mode", { $0.viewMode = .structure }, .otherViewMode), + ("open transaction", { $0.holdsTransaction = true }, .openTransaction), + ("unsaved edits", { $0.holdsUnsavedWork = true }, .unsavedChanges), + ("sidebar staged drops", { $0.hasSidebarDataWork = true }, .unsavedChanges), + ("open cell editor", { $0.isEditingCell = true }, .editing), + ("grid popover or context menu", { $0.gridHoldsTransientUI = true }, .interacting), + ("inspector pop-out window", { $0.inspectorHoldsValueWindow = true }, .valueWindowOpen), + ("live scroll or drag", { $0.gridIsTrackingInteraction = true }, .interacting), + ("running load", { $0.load = .busy }, .loading), + ("pending count", { $0.isCountPending = true }, .loading), + ("load more", { $0.isLoadingMore = true }, .loading), + ("never loaded", { $0.hasLoaded = false }, .loading), + ("Safe Mode prompt", { $0.showsSafeModePrompt = true }, .safeModePrompt), + ("selection without a key", { gate in + gate.hasSelection = true + gate.hasPrimaryKey = false + }, .unkeyedSelection), + ] + for (name, configure, pause) in cases { + var gate = TableAutoRefreshGate() + configure(&gate) + #expect(gate.decision == .wait(pause), "\(name)") + } + } + + @Test("An open editor names the editor, not the unsaved work it also counts as") + func editorWinsOverUnsavedWork() { + var gate = TableAutoRefreshGate() + gate.isEditingCell = true + gate.holdsUnsavedWork = true + #expect(gate.decision == .wait(.editing)) + } + + @Test("A selection in a keyed table still refreshes, since the key brings it back") + func keyedSelectionRefreshes() { + var gate = TableAutoRefreshGate() + gate.hasSelection = true + #expect(gate.decision == .refresh) + } + + @Test("A failed tick and a tab that is no longer a table stop the schedule") + func failureAndNonTableStop() { + var failed = TableAutoRefreshGate() + failed.previousTickFailed = true + var query = TableAutoRefreshGate() + query.isTableTab = false + + #expect(failed.decision == .stop) + #expect(query.decision == .stop) + } + + @Test("Only the pauses a reader can act on name themselves in the help") + func pauseHelp() { + #expect(TableAutoRefreshPause.unsavedChanges.help == "Auto Refresh waits while the table has unsaved changes.") + #expect(TableAutoRefreshPause.disconnected.help == "Auto Refresh waits until the connection is back.") + #expect(TableAutoRefreshPause.openTransaction.help == "Auto Refresh waits while a transaction is open.") + #expect(TableAutoRefreshPause.valueWindowOpen.help == "Auto Refresh waits while a value is open in its own window.") + #expect(TableAutoRefreshPause.notVisible.help == nil) + #expect(TableAutoRefreshPause.interacting.help == nil) + } +} diff --git a/apple/TableProTests/Models/Query/TableAutoRefreshIntervalTests.swift b/apple/TableProTests/Models/Query/TableAutoRefreshIntervalTests.swift new file mode 100644 index 0000000000..68b519805f --- /dev/null +++ b/apple/TableProTests/Models/Query/TableAutoRefreshIntervalTests.swift @@ -0,0 +1,47 @@ +// +// TableAutoRefreshIntervalTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct TableAutoRefreshIntervalTests { + @Test("The presets run from five seconds to five minutes, after Off") + func presets() { + let seconds = TableAutoRefreshInterval.allCases.map { $0.rawValue } + #expect(seconds == [0, 5, 10, 30, 60, 300]) + #expect(TableAutoRefreshInterval.allCases.filter { !$0.isOn } == [.off]) + #expect(TableAutoRefreshInterval.oneMinute.duration == .seconds(60)) + } + + @Test("Each preset has its own title") + func titles() { + let titles = TableAutoRefreshInterval.allCases.map { $0.title } + #expect(titles == [ + "Off", "Every 5 Seconds", "Every 10 Seconds", "Every 30 Seconds", "Every Minute", "Every 5 Minutes", + ]) + } + + @Test("An auto refresh writes no history and keeps the count; every other load does both") + func autoRefreshTriggerSkipsHistoryAndCount() { + #expect(!TableLoadTrigger.autoRefresh.recordsHistory) + #expect(!TableLoadTrigger.autoRefresh.countsRows) + #expect(TableLoadTrigger.userInitiated.recordsHistory) + #expect(TableLoadTrigger.restore.countsRows) + } + + @Test("A table tab never saves its interval, so a reopened tab starts with it off") + func intervalIsSessionOnly() { + var tab = QueryTab(title: "jobs", query: "SELECT * FROM jobs", tabType: .table, tableName: "jobs") + tab.autoRefreshInterval = .fiveSeconds + + let reopened = QueryTab(from: tab.toPersistedTab(), defaultPageSize: 1_000) + var stopped = tab + stopped.autoRefreshInterval = .off + + #expect(reopened.autoRefreshInterval == .off) + #expect(tab != stopped, "a view keyed on the tab has to see the interval change") + } +} diff --git a/apple/TableProTests/Models/ResultStatusModelTests.swift b/apple/TableProTests/Models/ResultStatusModelTests.swift index f5a1cc3f23..19304f3637 100644 --- a/apple/TableProTests/Models/ResultStatusModelTests.swift +++ b/apple/TableProTests/Models/ResultStatusModelTests.swift @@ -290,6 +290,16 @@ struct ResultStatusModelTests { #expect(!structure.controls.showsPagination) } + @Test("Auto Refresh is offered on a table tab's data grid only") + func autoRefreshFollowsTheTableGrid() { + let table = makeSnapshot(rowCount: 10) + #expect(model(table, viewMode: .data).controls.showsAutoRefresh) + #expect(!model(table, viewMode: .json).controls.showsAutoRefresh) + #expect(!model(table, viewMode: .structure).controls.showsAutoRefresh) + #expect(!model(makeSnapshot(tabType: .query, rowCount: 10, hasTableName: false)).controls.showsAutoRefresh) + #expect(!model(makeSnapshot(rowCount: 10, hasTableName: false)).controls.showsAutoRefresh) + } + @Test("Highlight rules are offered only where the data grid draws the result") func highlightRulesFollowTheDataGrid() { let table = makeSnapshot(rowCount: 10) diff --git a/apple/TableProTests/Models/Sidebar/SidebarObjectKindTests.swift b/apple/TableProTests/Models/Sidebar/SidebarObjectKindTests.swift index 2915e371b1..5295c6c304 100644 --- a/apple/TableProTests/Models/Sidebar/SidebarObjectKindTests.swift +++ b/apple/TableProTests/Models/Sidebar/SidebarObjectKindTests.swift @@ -69,12 +69,82 @@ struct SidebarObjectKindTests { @Test("Every kind belongs to exactly one category") func categoriesPartitionTheKinds() { - #expect(SidebarObjectKind.allCases.filter { $0.category == .routine } == [.procedure, .function]) - #expect(SidebarObjectKind.allCases.filter { $0.category == .trigger } == [.trigger]) - #expect(SidebarObjectKind.allCases.filter { $0.category == .type } == [.type]) + #expect(SidebarObjectKind.builtIn.filter { $0.category == .routine } == [.procedure, .function]) + #expect(SidebarObjectKind.builtIn.filter { $0.category == .trigger } == [.trigger]) + #expect(SidebarObjectKind.builtIn.filter { $0.category == .type } == [.type]) #expect( - SidebarObjectKind.allCases.filter { $0.category == .table } + SidebarObjectKind.builtIn.filter { $0.category == .table } == [.table, .view, .materializedView, .foreignTable, .sequence] ) + #expect(!SidebarObjectKind.builtIn.contains { $0.category == .server }) + #expect(SidebarObjectKind.server(events).category == .server) + } + + private let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + private let extensions = SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", iconName: "puzzlepiece.extension", + scope: .database + ) + + /// UserDefaults keys, the tree expansion JSON and node ids all spell these, so they never move. + @Test("A built-in kind persists under the name it shipped with") + func builtInPersistenceNamesAreShipped() { + #expect(SidebarObjectKind.builtIn.map { $0.persistenceName } == [ + "table", "view", "materializedView", "foreignTable", "sequence", "procedure", "function", "trigger", "type", + ]) + for kind in SidebarObjectKind.builtIn { + #expect(SidebarObjectKind(persistenceName: kind.persistenceName) == kind) + } + } + + @Test("A declared kind persists by its id and decodes without the driver that declared it") + func declaredKindRoundTripsByIdAlone() { + let kind = SidebarObjectKind.server(events) + #expect(kind.persistenceName == "server.event") + let decoded = SidebarObjectKind(persistenceName: "server.event") + #expect(decoded == kind) + #expect(decoded?.hashValue == kind.hashValue) + #expect(decoded?.serverKind?.id == "event") + } + + @Test("An unknown or empty name decodes to nothing") + func unknownPersistenceNameIsSkipped() { + #expect(SidebarObjectKind(persistenceName: "dictionary") == nil) + #expect(SidebarObjectKind(persistenceName: "server.") == nil) + #expect(SidebarObjectKind(persistenceName: "") == nil) + } + + @Test("Declared kinds follow the built-ins, in the order the driver declared them") + func declaredKindsFollowBuiltIns() { + let ordered = SidebarObjectKind.ordered(declared: [events, extensions]) + #expect(Array(ordered.prefix(SidebarObjectKind.builtIn.count)) == SidebarObjectKind.builtIn) + #expect(Array(ordered.suffix(2)) == [.server(events), .server(extensions)]) + } + + @Test("A declared kind is listed only for its rows or a kept failure, never for being declared") + func declaredKindVisibility() { + let counted: [SidebarObjectKind: Int] = [.trigger: 1, .server(SidebarServerObjectKind(id: "event")): 2] + #expect( + SidebarObjectKind.visible(itemCounts: counted, declared: [events, extensions]) + == [.trigger, .server(events)] + ) + #expect( + SidebarObjectKind.visible(itemCounts: [:], keeping: [.server(extensions)], declared: [events, extensions]) + == [.server(extensions)] + ) + #expect(SidebarObjectKind.visible(itemCounts: counted).isEmpty == false) + #expect(SidebarObjectKind.visible(itemCounts: counted) == [.trigger]) + } + + @Test("A declared kind reads its names and symbol from the driver's descriptor") + func declaredKindDisplay() { + let kind = SidebarObjectKind.server(extensions) + #expect(kind.displayName == "Extension") + #expect(kind.pluralDisplayName == "Extensions") + #expect(kind.iconName == "puzzlepiece.extension") + #expect(kind.title(tableEntityName: "Collections") == "Extensions") + #expect(!kind.isExpandedByDefault) } } diff --git a/apple/TableProTests/Services/SchemaServiceSideObjectsTests.swift b/apple/TableProTests/Services/SchemaServiceSideObjectsTests.swift index 8b86086c7d..c75a18f94f 100644 --- a/apple/TableProTests/Services/SchemaServiceSideObjectsTests.swift +++ b/apple/TableProTests/Services/SchemaServiceSideObjectsTests.swift @@ -23,6 +23,10 @@ final class SideObjectsMockDriver: DatabaseDriver, @unchecked Sendable { var schemasToReturn: [String] = [] var routineSchemaRequests: [String?] = [] var triggerSchemaRequests: [String?] = [] + var declaredServerKinds: [PluginServerObjectKind] = [] + var serverObjectsByKind: [String: [ServerObjectInfo]] = [:] + var serverObjectsError: Error? + var serverObjectRequests: [String] = [] init(connection: DatabaseConnection) { self.connection = connection @@ -94,6 +98,14 @@ final class SideObjectsMockDriver: DatabaseDriver, @unchecked Sendable { triggerSchemaRequests.append(schema) return triggersBySchema[schema ?? ""] ?? [] } + + var serverObjectKinds: [PluginServerObjectKind] { declaredServerKinds } + + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [ServerObjectInfo] { + serverObjectRequests.append("\(kindId)@\(schema ?? "")") + if let serverObjectsError { throw serverObjectsError } + return serverObjectsByKind[kindId] ?? [] + } } @MainActor @@ -316,4 +328,96 @@ struct SchemaServiceSideObjectsTests { #expect(service.routinesLoadState(for: connectionId, schema: "hr") == .idle) #expect(!service.isSchemaSettled(for: connectionId, schema: "hr")) } + + // MARK: - Server objects + + private var eventKind: PluginServerObjectKind { + PluginServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock" + ) + } + + private func event(_ name: String) -> ServerObjectInfo { + ServerObjectInfo(kindId: "event", name: name, schema: "shop", detail: "Recurring") + } + + @Test("A connection-wide load reads each declared kind beside the routines") + func loadReadsDeclaredKinds() async { + let (connectionId, connection, driver) = makeDriver() + driver.declaredServerKinds = [eventKind] + driver.serverObjectsByKind["event"] = [event("nightly")] + + let service = SchemaService() + await service.load(connectionId: connectionId, driver: driver, connection: connection) + + #expect(service.serverObjectsLoadState(for: connectionId, kindId: "event") == .loaded([event("nightly")])) + #expect(service.serverObjects(for: connectionId, kindId: "event").map { $0.name } == ["nightly"]) + #expect(driver.serverObjectRequests == ["event@"]) + } + + @Test("A driver that declares no kinds is never asked for server objects") + func undeclaredKindsAreNotFetched() async { + let (connectionId, connection, driver) = makeDriver() + + let service = SchemaService() + await service.load(connectionId: connectionId, driver: driver, connection: connection) + + #expect(driver.serverObjectRequests.isEmpty) + #expect(service.serverObjectsLoadState(for: connectionId, kindId: "event") == .idle) + } + + @Test("A server object fetch that fails is reported, and the tables stay loaded") + func failedServerObjectsAreReported() async { + let (connectionId, connection, driver) = makeDriver() + driver.declaredServerKinds = [eventKind] + driver.serverObjectsError = boom + driver.tablesToReturn = [TestFixtures.makeTableInfo(name: "orders", schema: "public")] + + let service = SchemaService() + await service.load(connectionId: connectionId, driver: driver, connection: connection) + + #expect(service.serverObjectsLoadState(for: connectionId, kindId: "event") == .failed("boom")) + #expect(service.tables(for: connectionId).map { $0.name } == ["orders"]) + } + + @Test("A failed server object reload keeps the objects already loaded") + func failedServerObjectReloadKeepsObjects() async { + let (connectionId, connection, driver) = makeDriver() + driver.declaredServerKinds = [eventKind] + driver.serverObjectsByKind["event"] = [event("nightly")] + let service = SchemaService() + await service.load(connectionId: connectionId, driver: driver, connection: connection) + + driver.serverObjectsError = boom + let reloaded = await service.reloadServerObjects(connectionId: connectionId, driver: driver, scope: nil, kindId: "event") + + #expect(!reloaded) + #expect(service.serverObjects(for: connectionId, kindId: "event").map { $0.name } == ["nightly"]) + + driver.serverObjectsError = nil + driver.serverObjectsByKind["event"] = [event("nightly"), event("hourly")] + #expect(await service.reloadServerObjects(connectionId: connectionId, driver: driver, scope: nil, kindId: "event")) + #expect(service.serverObjects(for: connectionId, kindId: "event").count == 2) + } + + @Test("Expanding a schema reads its container kinds, and a database kind is left to the database") + func schemaLoadReadsContainerKindsOnly() async { + let (connectionId, _, driver) = makeDriver() + driver.declaredServerKinds = [ + eventKind, + PluginServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", + iconName: "puzzlepiece.extension", scope: .database + ) + ] + driver.serverObjectsByKind["event"] = [event("nightly")] + let service = SchemaService() + + await service.loadSchemaObjects(schema: "hr", in: unnamedDatabase(connectionId), driver: driver) + + #expect(driver.serverObjectRequests == ["event@hr"]) + #expect(service.serverObjects(for: connectionId, schema: "hr", kindId: "event").map { $0.name } == ["nightly"]) + #expect(service.serverObjectsByKind(for: connectionId, schema: "hr").keys.sorted() == ["event"]) + #expect(service.isSchemaSettled(for: connectionId, schema: "hr")) + } } diff --git a/apple/TableProTests/ViewModels/ERDiagramForeignKeyDraftTests.swift b/apple/TableProTests/ViewModels/ERDiagramForeignKeyDraftTests.swift new file mode 100644 index 0000000000..b01bed83a8 --- /dev/null +++ b/apple/TableProTests/ViewModels/ERDiagramForeignKeyDraftTests.swift @@ -0,0 +1,133 @@ +// +// ERDiagramForeignKeyDraftTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct ERDiagramForeignKeyDraftTests { + private typealias Pair = ERDiagramForeignKeyDraft.ColumnPair + + private func draft(_ pairs: [(String, String)]) -> ERDiagramForeignKeyDraft { + ERDiagramForeignKeyDraft( + table: "line_items", + pairs: pairs.map { Pair(column: $0.0, referencedColumn: $0.1) }, + referencedTable: "orders", + name: "fk_line_items_tenant_id" + ) + } + + @Test("The pairs map to the key's two column lists in pair order") + func pairOrderMapsToTheKey() { + var composite = draft([("tenant_id", "tenant_id"), ("order_id", "id")]) + composite.onDelete = .cascade + + let key = composite.foreignKey + + #expect(key.columns == ["tenant_id", "order_id"]) + #expect(key.referencedColumns == ["tenant_id", "id"]) + #expect(key.referencedTable == "orders") + #expect(key.name == "fk_line_items_tenant_id") + #expect(key.onDelete == .cascade) + } + + @Test("A drop on a member of a two-column key offers the whole key, the dragged pair in its place") + func dropOnACompositeMemberOffersTheWholeKey() { + let started = ERDiagramForeignKeyDraft.starting( + table: "line_items", + column: "order_id", + referencedTable: "orders", + keyColumns: ["tenant_id", "id"], + target: "id", + sourceColumns: ["id", "Tenant_ID", "order_id"], + name: "fk_line_items_order_id" + ) + + #expect(started.pairs.map { $0.referencedColumn } == ["tenant_id", "id"]) + #expect(started.pairs.map { $0.column } == ["Tenant_ID", "order_id"]) + #expect(started.issue == nil) + } + + @Test("A drop on a column outside the key keeps that one pair") + func dropOutsideTheKeyIsOnePair() { + let started = ERDiagramForeignKeyDraft.starting( + table: "line_items", + column: "order_code", + referencedTable: "orders", + keyColumns: ["tenant_id", "id"], + target: "code", + sourceColumns: ["order_code"], + name: "fk" + ) + + #expect(started.pairs.map { $0.column } == ["order_code"]) + #expect(started.pairs.map { $0.referencedColumn } == ["code"]) + } + + @Test("Choosing another referenced table rebuilds its side from that table's key and keeps the chosen column") + func referencingRebuildsFromTheKey() { + let single = draft([("order_id", "id")]) + let sources = ["id", "tenant_id", "order_id"] + + let composite = single.referencing("shipments", keyColumns: ["tenant_id", "id"], sourceColumns: sources) + + #expect(composite.referencedTable == "shipments") + #expect(composite.pairs.map { $0.column } == ["tenant_id", "order_id"]) + #expect(composite.pairs.map { $0.referencedColumn } == ["tenant_id", "id"]) + } + + @Test("A table's own id is never taken for the referenced id") + func ownIdIsNotAGuess() { + let single = draft([("order_id", "id")]) + .referencing("orders", keyColumns: ["id"], sourceColumns: ["id", "order_id"]) + + #expect(single.pairs.map { $0.column } == ["order_id"]) + } + + @Test("A column with no namesake stays blank, and the draft says a row is incomplete") + func unguessedColumnIsBlank() { + let composite = draft([("order_id", "id")]) + .referencing("shipments", keyColumns: ["id", "region"], sourceColumns: ["order_id"]) + + #expect(composite.pairs.map { $0.column } == ["order_id", ""]) + #expect(composite.issue == .incompletePair) + } + + @Test("A repeated column on either side is named") + func repeatsAreNamed() { + #expect(draft([("a", "x"), ("a", "y")]).issue == .repeatedColumn("a")) + #expect(draft([("a", "x"), ("b", "x")]).issue == .repeatedReferencedColumn("x")) + #expect(draft([("a", "x"), ("b", "y")]).issue == nil) + } + + @Test("Each whole-value edit changes only the pair it names") + func editsNameTheirPair() throws { + let composite = draft([("a", "x"), ("b", "y")]) + let second = try #require(composite.pairs.last?.id) + + let edited = composite.settingColumn("c", of: second).settingReferencedColumn("z", of: second) + + #expect(edited.pairs.map { $0.column } == ["a", "c"]) + #expect(edited.pairs.map { $0.referencedColumn } == ["x", "z"]) + } + + @Test("Adding, moving and removing pairs keep the key's order, and the last pair stays") + func addMoveRemove() throws { + let composite = draft([("a", "x"), ("b", "y")]).addingPair() + #expect(composite.pairs.count == 3) + #expect(composite.issue == .incompletePair) + + let moved = composite.movingPairs(fromOffsets: IndexSet(integer: 0), toOffset: 2) + #expect(moved.pairs.map { $0.column } == ["b", "a", ""]) + + let first = try #require(moved.pairs.first?.id) + let blank = try #require(moved.pairs.last?.id) + let trimmed = moved.removingPairs([first, blank]) + #expect(trimmed.pairs.map { $0.column } == ["a"]) + + let everything = Set(trimmed.pairs.map { $0.id }) + #expect(trimmed.removingPairs(everything) == trimmed) + } +} diff --git a/apple/TableProTests/ViewModels/ERDiagramSchemaEditsTests.swift b/apple/TableProTests/ViewModels/ERDiagramSchemaEditsTests.swift index e62e4676ea..b52fb16d24 100644 --- a/apple/TableProTests/ViewModels/ERDiagramSchemaEditsTests.swift +++ b/apple/TableProTests/ViewModels/ERDiagramSchemaEditsTests.swift @@ -193,6 +193,34 @@ struct ERDiagramSchemaEditsTests { #expect(diagram == structureTab) } + @Test("A composite foreign key from the diagram composes what the Structure tab composes") + func compositeForeignKeyMatchesTheStructureTab() async throws { + let fixture = try await Self.makeFixture() + defer { fixture.tearDown() } + let draft = ERDiagramForeignKeyDraft( + table: "orders", + pairs: [ + .init(column: "customer_id", referencedColumn: "id"), + .init(column: "shipper_id", referencedColumn: "email") + ], + referencedTable: "customers", + name: "fk_orders_customer", + onDelete: .cascade + ) + + try await fixture.edits.addForeignKey(draft.foreignKey, to: "orders") + let tab = try await Self.structureTabSession(fixture, table: "orders") + tab.changeManager.addForeignKey(draft.foreignKey) + + let diagram = await fixture.edits.previewStatements() + #expect(diagram == [ + "ALTER TABLE \"public\".\"orders\" ADD CONSTRAINT \"fk_orders_customer\" FOREIGN KEY (\"customer_id\", \"shipper_id\") " + + "REFERENCES \"customers\" (\"id\", \"email\") ON DELETE CASCADE;" + ]) + let structureTab = try await Self.statements(of: tab) + #expect(diagram == structureTab) + } + @Test("Dropping a drawn foreign key drops the constraint the Structure tab would drop") func droppedForeignKeyMatchesTheStructureTab() async throws { let fixture = try await Self.makeFixture() diff --git a/apple/TableProTests/ViewModels/ERDiagramViewModelForeignKeyTests.swift b/apple/TableProTests/ViewModels/ERDiagramViewModelForeignKeyTests.swift new file mode 100644 index 0000000000..0651eb860e --- /dev/null +++ b/apple/TableProTests/ViewModels/ERDiagramViewModelForeignKeyTests.swift @@ -0,0 +1,73 @@ +// +// ERDiagramViewModelForeignKeyTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct ERDiagramViewModelForeignKeyTests { + private func loadedFixture() async -> ERDiagramLoadFixture { + let fixture = ERDiagramLoadFixture(tableCount: 1, columnsPerTable: 3) + fixture.driver.addTable("accounts", columns: [ + TestFixtures.makeColumnInfo(name: "tenant_id", isPrimaryKey: true), + TestFixtures.makeColumnInfo(name: "id", isPrimaryKey: true), + TestFixtures.makeColumnInfo(name: "name", dataType: "TEXT", isPrimaryKey: false) + ]) + await fixture.viewModel.loadDiagram() + return fixture + } + + private func draft(of viewModel: ERDiagramViewModel) -> ERDiagramForeignKeyDraft? { + guard case .addForeignKey(let draft) = viewModel.editSheet else { return nil } + return draft + } + + @Test("A drop on a member of a two-column key offers both key columns, the dragged pair in its place") + func dropOnACompositeKeyOffersBothColumns() async throws { + let fixture = await loadedFixture() + defer { fixture.tearDown() } + let target = ERDiagramColumnHit( + nodeId: UUID(), tableName: "accounts", columnName: "id", rowRect: .zero, isDropped: false + ) + + fixture.viewModel.beginForeignKey(from: "table_0", column: "column_1", to: target) + + let started = try #require(draft(of: fixture.viewModel)) + #expect(started.referencedTable == "accounts") + #expect(started.pairs.map { $0.referencedColumn } == ["tenant_id", "id"]) + #expect(started.pairs.last?.column == "column_1") + #expect(started.foreignKey.referencedColumns == ["tenant_id", "id"]) + } + + @Test("Add Foreign Key from a column prefills the referenced table's whole primary key") + func menuPrefillsTheWholeKey() async throws { + let fixture = await loadedFixture() + defer { fixture.tearDown() } + + fixture.viewModel.beginForeignKey(from: "table_0", column: "column_2", to: nil) + + let started = try #require(draft(of: fixture.viewModel)) + #expect(started.referencedTable == "accounts") + #expect(fixture.viewModel.referencedKeyColumns(in: "accounts") == ["tenant_id", "id"]) + #expect(started.pairs.map { $0.referencedColumn } == ["tenant_id", "id"]) + #expect(started.pairs.contains { $0.column == "column_2" }) + } + + @Test("A table with a single-column key still starts with one pair") + func singleKeyIsOnePair() async throws { + let fixture = await loadedFixture() + defer { fixture.tearDown() } + let target = ERDiagramColumnHit( + nodeId: UUID(), tableName: "table_0", columnName: "column_0", rowRect: .zero, isDropped: false + ) + + fixture.viewModel.beginForeignKey(from: "accounts", column: "name", to: target) + + let started = try #require(draft(of: fixture.viewModel)) + #expect(started.pairs.map { $0.column } == ["name"]) + #expect(started.pairs.map { $0.referencedColumn } == ["column_0"]) + } +} diff --git a/apple/TableProTests/ViewModels/SidebarViewModelTests.swift b/apple/TableProTests/ViewModels/SidebarViewModelTests.swift index d6689d4181..ea043ab89d 100644 --- a/apple/TableProTests/ViewModels/SidebarViewModelTests.swift +++ b/apple/TableProTests/ViewModels/SidebarViewModelTests.swift @@ -434,6 +434,56 @@ struct SidebarViewModelMultiSectionTests { #expect(defaults.bool(forKey: key) == true) } + @Test("A declared kind's flat expansion persists under the one per-connection key and restores by id") + func declaredKindExpansionPersistsUnderOneKey() { + let defaults = suite.defaults + let connectionId = UUID() + let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + let vm = makeViewModel(connectionId: connectionId, databaseType: .mysql, defaults: defaults) + #expect(vm.expanded[.server(events)] == false) + + vm.expanded[.server(events)] = true + + let key = SidebarPersistenceKey.serverKindsExpanded(connectionId: connectionId) + #expect(defaults.dictionary(forKey: key)?["event"] as? Bool == true) + #expect(defaults.object(forKey: "sidebar.\(connectionId.uuidString).server.event.expanded") == nil) + #expect(SidebarPersistenceKey.all(connectionId: connectionId).contains(key)) + + let restored = makeViewModel(connectionId: connectionId, databaseType: .mysql, defaults: defaults) + #expect(restored.expanded[.server(SidebarServerObjectKind(id: "event"))] == true) + + SidebarPersistenceKey.removeAll(connectionId: connectionId, defaults: defaults) + #expect(defaults.object(forKey: key) == nil) + } + + @Test("Every built-in kind's expansion key is listed for cleanup") + func builtInExpansionKeysAreListed() { + let connectionId = UUID() + let all = SidebarPersistenceKey.all(connectionId: connectionId) + for kind in SidebarObjectKind.builtIn { + #expect(all.contains(SidebarPersistenceKey.expanded(connectionId: connectionId, kind: kind))) + } + #expect(SidebarPersistenceKey.expanded(connectionId: connectionId, kind: .materializedView) + == "sidebar.\(connectionId.uuidString).materializedView.expanded") + } + + @Test("filteredServerObjects narrows by the search and follows each kind on its own") + func filteredServerObjectsBySearch() { + let vm = makeViewModel(databaseType: .mysql, defaults: suite.defaults) + let events = [ + ServerObjectInfo(kindId: "event", name: "nightly_cleanup", schema: "shop"), + ServerObjectInfo(kindId: "event", name: "hourly_rollup", schema: "shop") + ] + #expect(vm.filteredServerObjects(kindId: "event", from: events).count == 2) + + vm.searchText = "nightly" + + #expect(vm.filteredServerObjects(kindId: "event", from: events).map { $0.name } == ["nightly_cleanup"]) + #expect(vm.filteredServerObjects(kindId: "extension", from: []).isEmpty) + } + @Test("recents expansion defaults to expanded and persists across init") func recentsExpansionPersists() { let defaults = suite.defaults diff --git a/apple/TableProTests/ViewModels/VersionHistoryViewModelTests.swift b/apple/TableProTests/ViewModels/VersionHistoryViewModelTests.swift index 89a29599c9..c0f502b491 100644 --- a/apple/TableProTests/ViewModels/VersionHistoryViewModelTests.swift +++ b/apple/TableProTests/ViewModels/VersionHistoryViewModelTests.swift @@ -236,6 +236,19 @@ struct VersionHistoryViewModelTests { #expect(viewModel.page.entries.isEmpty) #expect(viewModel.listState == .failed(VersionHistoryError.subjectNotFound.localizedDescription)) } + + @Test("A query tab that closed clears the list and says how to get it back") + func closedTabClearsTheList() async throws { + let provider = makeProvider() + let viewModel = makeViewModel(provider) + await viewModel.loadList() + await viewModel.detailTask?.value + + await provider.setLoadError(.tabClosed) + await viewModel.loadList() + #expect(viewModel.page.entries.isEmpty) + #expect(viewModel.listState == .failed(VersionHistoryError.tabClosed.localizedDescription)) + } } struct VersionComparisonTests { diff --git a/apple/TableProTests/ViewModels/WindowSidebarStateTests.swift b/apple/TableProTests/ViewModels/WindowSidebarStateTests.swift index 131a852862..3b72dee2d0 100644 --- a/apple/TableProTests/ViewModels/WindowSidebarStateTests.swift +++ b/apple/TableProTests/ViewModels/WindowSidebarStateTests.swift @@ -214,6 +214,41 @@ struct WindowSidebarStateTests { ) } + @Test("A declared kind's group expansion persists by id and restores before any driver exists") + func declaredKindGroupExpansionRoundTrips() throws { + let defaults = try makeDefaults() + let connectionId = UUID() + let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + let group = DatabaseTreeObjectGroup(database: "shop", schema: nil, kind: .server(events)) + + let state = WindowSidebarState(connectionId: connectionId, defaults: defaults) + #expect(!state.isTreeObjectGroupExpanded(group)) + state.setTreeObjectGroup(group, expanded: true) + + let stored = try #require(defaults.data(forKey: "com.TablePro.sidebar.treeExpansion.\(connectionId.uuidString)")) + #expect(String(decoding: stored, as: UTF8.self).contains("\"server.event\"")) + + let restored = WindowSidebarState(connectionId: connectionId, defaults: defaults) + #expect(restored.isTreeObjectGroupExpanded(group)) + let decodedOnly = DatabaseTreeObjectGroup( + database: "shop", schema: nil, kind: .server(SidebarServerObjectKind(id: "event")) + ) + #expect(restored.isTreeObjectGroupExpanded(decodedOnly)) + } + + @Test("A built-in group still persists under the kind name it shipped with") + func builtInGroupKeepsShippedName() throws { + let defaults = try makeDefaults() + let connectionId = UUID() + let state = WindowSidebarState(connectionId: connectionId, defaults: defaults) + state.setTreeObjectGroup(DatabaseTreeObjectGroup(database: "shop", schema: "public", kind: .materializedView), expanded: true) + + let stored = try #require(defaults.data(forKey: "com.TablePro.sidebar.treeExpansion.\(connectionId.uuidString)")) + #expect(String(decoding: stored, as: UTF8.self).contains("\"materializedView\"")) + } + @Test("A window without a connection does not persist") func nilConnectionDoesNotPersist() throws { let defaults = try makeDefaults() diff --git a/apple/TableProTests/Views/DataGrid/DataGridContextMenuTransientTests.swift b/apple/TableProTests/Views/DataGrid/DataGridContextMenuTransientTests.swift new file mode 100644 index 0000000000..4196cd69e4 --- /dev/null +++ b/apple/TableProTests/Views/DataGrid/DataGridContextMenuTransientTests.swift @@ -0,0 +1,43 @@ +// +// DataGridContextMenuTransientTests.swift +// TableProTests +// + +import AppKit +@testable import TablePro +import Testing + +@MainActor +struct DataGridContextMenuTransientTests { + @Test("An open row menu holds the grid until it closes") + func rowMenuHoldsTheGrid() throws { + let grid = HostedDataGrid(rows: GridFixtureRows.people) + defer { grid.close() } + let menu = try #require(grid.main.menu) + + #expect(!grid.model.holdsTransientUI) + grid.controller.menuWillOpen(menu) + #expect(grid.model.holdsTransientUI) + grid.controller.menuDidClose(menu) + #expect(!grid.model.holdsTransientUI) + } + + @Test("The header menu reports its opening to the grid and keeps the items it was built with") + func headerMenuHoldsTheGrid() throws { + let grid = HostedDataGrid(rows: GridFixtureRows.people) + defer { grid.close() } + let column = try #require(grid.tableColumn(named: "name")) + let menu = try #require(grid.controller.headerMenu(forTableColumn: column, in: .main)) + let items = menu.items.count + #expect(items > 0) + #expect(menu.delegate === grid.controller) + + grid.controller.menuNeedsUpdate(menu) + #expect(menu.items.count == items) + + grid.controller.menuWillOpen(menu) + #expect(grid.model.holdsTransientUI) + grid.controller.menuDidClose(menu) + #expect(!grid.model.holdsTransientUI) + } +} diff --git a/apple/TableProTests/Views/DataGrid/DataGridViewportTests.swift b/apple/TableProTests/Views/DataGrid/DataGridViewportTests.swift index b5ada9e63f..3494536405 100644 --- a/apple/TableProTests/Views/DataGrid/DataGridViewportTests.swift +++ b/apple/TableProTests/Views/DataGrid/DataGridViewportTests.swift @@ -126,6 +126,26 @@ struct DataGridViewportTests { #expect(NSLocationInRange(250, grid.main.rows(in: grid.main.visibleRect))) } + @Test("A placement keeps a cell cursor on its column rather than selecting the whole row on column 0") + func placedCursorKeepsItsColumn() async throws { + let grid = makeGrid() + defer { grid.close() } + let column = DataGridColumnID(name: "c3", occurrence: 0) + let cell = GridSelectionSnapshot.Cell(rowID: .existing(40), columnID: column) + var cursor = GridSelectionSnapshot(rowIDs: [.existing(40)], rectangles: [], activeCell: cell, anchor: cell) + cursor.mode = .cursor + + grid.model.store.viewportAnchor = GridViewportPlacement( + firstVisibleRow: .existing(30), firstVisibleOffset: 0, selection: cursor, revealsSelection: false + ) + grid.controller.applyStagedViewport() + + try await waitUntil { grid.model.selection.mode == .cursor } + let position = try #require(grid.model.columns.displayPosition(of: column)) + #expect(grid.model.selection.activeCell == GridCoord(row: 40, displayColumn: position)) + #expect(grid.model.selectedRowIDs == [.existing(40)]) + } + @Test("Revealing a row scrolls the frozen pane with it") func revealScrollsBothPanes() async throws { let grid = makeGrid() diff --git a/apple/TableProTests/Views/Main/GridReloadViewportTests.swift b/apple/TableProTests/Views/Main/GridReloadViewportTests.swift index 7de3db2103..c85b15082f 100644 --- a/apple/TableProTests/Views/Main/GridReloadViewportTests.swift +++ b/apple/TableProTests/Views/Main/GridReloadViewportTests.swift @@ -147,6 +147,27 @@ struct GridReloadViewportTests { withExtendedLifetime(fixture) {} } + @Test("Reading the same table again keeps a cell cursor a cursor, on its own column") + func sameSourceKeepsTheCursorAndItsColumn() throws { + let fixture = makeFixture() + defer { fixture.coordinator.teardown() } + fixture.model.selectCell(GridCoord(row: 2, displayColumn: 1)) + let column = try #require(fixture.model.activeCell?.column.id) + + fixture.coordinator.setActiveTableRows( + Self.rows(ids: Array((1 ... 10).reversed())), + for: fixture.tabId, + viewport: .keepPlace, + source: .sameSource + ) + + let selection = try #require(fixture.takePlacement()?.selection) + #expect(selection.mode == .cursor) + #expect(selection.rowIDs == [.existing(7)]) + #expect(selection.activeCell == GridSelectionSnapshot.Cell(rowID: .existing(7), columnID: column)) + withExtendedLifetime(fixture) {} + } + @Test("A new result does not carry the selection over") func newSourceRestoresNoSelection() { let fixture = makeFixture() diff --git a/apple/TableProTests/Views/Main/MainContentCoordinatorAutoRefreshTests.swift b/apple/TableProTests/Views/Main/MainContentCoordinatorAutoRefreshTests.swift new file mode 100644 index 0000000000..e24ae9d7f1 --- /dev/null +++ b/apple/TableProTests/Views/Main/MainContentCoordinatorAutoRefreshTests.swift @@ -0,0 +1,406 @@ +// +// MainContentCoordinatorAutoRefreshTests.swift +// TableProTests +// + +import Foundation +import os +@testable import TablePro +import TableProPluginKit +import TableProTestSupport +import Testing + +@MainActor +struct MainContentCoordinatorAutoRefreshTests { + @Test("A tick re-reads the selected table tab and writes no history row") + func tickRereadsWithoutHistory() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + + await harness.tickOnce() + + #expect(harness.tableReads == 1) + let tab = try #require(harness.tab) + #expect(tab.execution.errorMessage == nil) + #expect(tab.autoRefreshInterval == .fiveSeconds) + await harness.expectOnlyTheMarkerIsRecorded() + } + + @Test("A manual load records its row, so the marker check would see a tick's") + func manualLoadRecordsHistory() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + + harness.coordinator.executeTableTabQueryDirectly(viewport: .keepPlace) + await harness.coordinator.queryTasks.task(for: harness.tabId)?.value + await harness.recordMarker() + + #expect(harness.recordAttempts == 2) + try await waitUntil { await harness.historyCount == 2 } + } + + @Test("A tick keeps the derived count rather than counting again") + func tickKeepsTheCount() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + harness.coordinator.tabManager.mutate(tabId: harness.tabId) { tab in + tab.pagination.applyDerivedRowCount(42, isApproximate: false) + } + + await harness.tickOnce() + + let tab = try #require(harness.tab) + #expect(harness.tableReads == 1) + #expect(tab.pagination.totalRowCount == 42) + #expect(!tab.pagination.isCountPending) + } + + @Test("An open cell editor holds the tick: no query, no commit, and the bar says why") + func openEditorPausesWithoutCommitting() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + let model = harness.coordinator.gridModel(forTab: harness.tabId) + let mount = DataTabGridMountStub() + mount.isEditing = true + model.attach(mount) + + await harness.tickOnce() + + #expect(harness.tableReads == 0) + #expect(mount.commitCount == 0) + #expect(harness.tab?.autoRefreshPause == .editing) + #expect(harness.tab?.autoRefreshInterval == .fiveSeconds) + withExtendedLifetime(mount) {} + } + + @Test("An editable inspector pop-out holds the tick until its window goes") + func inspectorPopOutPauses() async { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + let paneState = TrailingPaneState() + harness.coordinator.trailingPaneState = paneState + var popOut: JSONViewerWindowController? = JSONViewerWindowController() + if let popOut { InspectorPopOutRegistry.record(popOut, openedFrom: paneState) } + + await harness.tickOnce() + + #expect(harness.tableReads == 0) + #expect(harness.tab?.autoRefreshPause == .valueWindowOpen) + + popOut = nil + #expect(!harness.coordinator.autoRefreshGate(forTab: harness.tabId).inspectorHoldsValueWindow) + withExtendedLifetime(paneState) {} + } + + @Test("A tick waits while the tab's database holds an open transaction") + func openTransactionPauses() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + let tracker = DatabaseManager.shared.sessionTransactions + let key = SessionTransactionTracker.Key(connectionId: harness.connection.id, lane: nil) + tracker.record(.inTransaction, on: harness.driver, for: key) + defer { tracker.record(.idle, on: harness.driver, for: key) } + + await harness.tickOnce() + + #expect(harness.tableReads == 0) + #expect(harness.tab?.autoRefreshPause == .openTransaction) + } + + @Test("A disconnected session runs nothing and waits for the connection") + func disconnectedSessionWaits() async throws { + let harness = AutoRefreshHarness(status: .disconnected) + defer { harness.tearDown() } + + await harness.tickOnce() + + #expect(harness.tableReads == 0) + #expect(harness.tab?.autoRefreshPause == .disconnected) + } + + @Test("A failed tick turns Auto Refresh off and leaves the tab's error on screen") + func failedTickStops() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + harness.driver.failReads() + + harness.start() + await harness.clock.waitUntilSleeping() + harness.clock.advance(by: .seconds(5)) + try await waitUntil { harness.tab?.autoRefreshInterval == .off } + + #expect(harness.tab?.execution.errorMessage != nil) + #expect(harness.clock.pendingSleeperCount == 0) + await harness.expectOnlyTheMarkerIsRecorded() + } + + @Test("A tab that is not selected never ticks") + func backgroundTabNeverRuns() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + harness.coordinator.observeSelectionForAutoRefresh() + harness.start() + await harness.clock.waitUntilSleeping() + + let other = QueryTab(title: "Query", query: "SELECT 1", tabType: .query) + harness.coordinator.tabManager.tabs.append(other) + harness.coordinator.tabManager.selectedTabId = other.id + try await waitUntil { !harness.coordinator.autoRefresh.isRunning } + harness.clock.advance(by: .seconds(5)) + + #expect(harness.tableReads == 0) + #expect(harness.clock.pendingSleeperCount == 0) + } +} + +// MARK: - Harness + +@MainActor +private final class AutoRefreshHarness { + let connection: DatabaseConnection + let driver: AutoRefreshDriver + let coordinator: MainContentCoordinator + let clock = ManualClock() + let tabId: UUID + private let history: QueryHistoryManager + private let attempts: RecordAttemptCounter + + init(status: ConnectionStatus = .connected) { + connection = DatabaseConnection(name: "Auto Refresh", database: "app", type: .postgresql) + driver = AutoRefreshDriver(connection: connection) + var session = ConnectionSession(connection: connection, driver: driver) + session.status = status + DatabaseManager.shared.injectSession(session, for: connection.id) + + let url = FileManager.default.temporaryDirectory + .appendingPathComponent("auto-refresh-\(UUID().uuidString).db") + // Asked once per write, first thing, on the task that records it. + let attempts = RecordAttemptCounter() + self.attempts = attempts + history = QueryHistoryManager( + storage: QueryHistoryStorage(databaseURL: url, removeDatabaseOnDeinit: true), + isCapturePaused: { + attempts.increment() + return false + } + ) + coordinator = MainContentCoordinator( + connection: connection, + tabManager: QueryTabManager(), + changeManager: DataChangeManager(), + toolbarState: ConnectionToolbarState(), + services: AppServices.live.replacingQueryHistory(history) + ) + coordinator.executionGate = AlwaysAllowGate() + coordinator.autoRefresh.clock = clock + coordinator.autoRefresh.visibility = { _, _ in .onScreen } + + var tab = QueryTab(title: "jobs", query: "SELECT * FROM jobs LIMIT 1000", tabType: .table, tableName: "jobs") + tab.tableContext.primaryKeyColumns = ["id"] + tab.execution.lastExecutedAt = Date() + coordinator.tabManager.tabs.append(tab) + coordinator.tabManager.selectedTabId = tab.id + tabId = tab.id + coordinator.setActiveTableRows( + TableRows.from( + queryRows: [[.text("1"), .text("first")]], + columns: ["id", "name"], + columnTypes: [.text(rawType: "INTEGER"), .text(rawType: "TEXT")] + ), + for: tab.id + ) + } + + var tab: QueryTab? { + coordinator.tabManager.tabs.first { $0.id == tabId } + } + + var tableReads: Int { + driver.sentStatements.filter { $0.contains("jobs") }.count + } + + var historyCount: Int { + get async { await history.count(scope: .connection(connection.id)) } + } + + var recordAttempts: Int { attempts.value } + + // Through the same manager and store after the load, so a row the load recorded is written before it. + func recordMarker() async { + await history.record(QueryHistoryRecordRequest( + query: "SELECT 'marker'", + connectionId: connection.id, + databaseName: connection.database, + databaseType: connection.type, + source: .editor, + executionTime: 0, + rowCount: 0, + wasSuccessful: true + )) + } + + func expectOnlyTheMarkerIsRecorded(sourceLocation: SourceLocation = #_sourceLocation) async { + await recordMarker() + #expect(recordAttempts == 1, "the tick asked to record \(recordAttempts - 1) rows", sourceLocation: sourceLocation) + #expect(await historyCount == 1, sourceLocation: sourceLocation) + } + + func start() { + coordinator.setAutoRefreshInterval(.fiveSeconds, forTab: tabId) + } + + // One interval passes, and the schedule is back to waiting once the tick it ran has settled. + func tickOnce() async { + start() + await clock.waitUntilSleeping() + clock.advance(by: .seconds(5)) + await clock.waitUntilSleeping() + } + + func tearDown() { + coordinator.stopAutoRefresh() + coordinator.cancelAllQueryTasks() + DatabaseManager.shared.removeSession(for: connection.id) + coordinator.teardown() + } +} + +private final class RecordAttemptCounter: Sendable { + private let count = OSAllocatedUnfairLock(initialState: 0) + + var value: Int { count.withLock { $0 } } + + func increment() { + count.withLock { $0 += 1 } + } +} + +private struct AutoRefreshReadFailure: LocalizedError { + var errorDescription: String? { "relation \"jobs\" does not exist" } +} + +private final class AutoRefreshDriver: DatabaseDriver, @unchecked Sendable { + let connection: DatabaseConnection + var status: ConnectionStatus = .connected + var serverVersion: String? { nil } + + private let lock = NSLock() + private var statements: [String] = [] + private var fails = false + + init(connection: DatabaseConnection) { + self.connection = connection + } + + var sentStatements: [String] { + lock.withLock { statements } + } + + func failReads() { + lock.withLock { fails = true } + } + + private func answer(_ query: String) throws -> QueryResult { + let failing = lock.withLock { () -> Bool in + statements.append(query) + return fails && query.contains("jobs") + } + if failing { throw AutoRefreshReadFailure() } + return ScriptAnsweringDriver.resultSet(columns: ["id", "name"], rows: [["1", "first"], ["2", "second"]]) + } + + func execute(query: String) async throws -> QueryResult { try answer(query) } + func executeParameterized(query: String, parameters: [Any?]) async throws -> QueryResult { try answer(query) } + func executeUserQuery(query: String, rowCap: Int?, parameters: [Any?]?) async throws -> QueryResult { + try answer(query) + } + + func connect() async throws {} + func disconnect() {} + func testConnection() async throws -> Bool { true } + func ping() async throws {} + func cancelQuery() throws {} + func applyQueryTimeout(_ seconds: Int) async throws {} + + func fetchTables() async throws -> [TableInfo] { [] } + func fetchTables(schema: String?) async throws -> [TableInfo] { [] } + func fetchColumns(table: String) async throws -> [ColumnInfo] { [] } + func fetchAllColumns() async throws -> [String: [ColumnInfo]] { [:] } + func fetchIndexes(table: String) async throws -> [IndexInfo] { [] } + func fetchForeignKeys(table: String) async throws -> [ForeignKeyInfo] { [] } + func fetchApproximateRowCount(table: String) async throws -> Int? { nil } + func fetchDatabases() async throws -> [String] { [] } + func fetchDatabaseMetadata(_ database: String) async throws -> DatabaseMetadata { + DatabaseMetadata( + id: database, + name: database, + tableCount: nil, + sizeBytes: nil, + lastAccessed: nil, + isSystemDatabase: false, + icon: "cylinder" + ) + } + + func fetchTableDDL(table: String) async throws -> String { "" } + func fetchTableMetadata(tableName: String) async throws -> TableMetadata { + TableMetadata( + tableName: tableName, + dataSize: nil, + indexSize: nil, + totalSize: nil, + avgRowLength: nil, + rowCount: nil, + comment: nil, + engine: nil, + collation: nil, + createTime: nil, + updateTime: nil + ) + } + + func fetchViewDefinition(view: String) async throws -> String { "" } + func beginTransaction() async throws {} + func commitTransaction() async throws {} + func rollbackTransaction() async throws {} +} + +@MainActor +private extension AppServices { + func replacingQueryHistory(_ manager: QueryHistoryManager) -> AppServices { + AppServices( + appEvents: appEvents, + appSettings: appSettings, + appSettingsStorage: appSettingsStorage, + connectionStorage: connectionStorage, + databaseManager: databaseManager, + pluginManager: pluginManager, + schemaService: schemaService, + schemaRefreshService: schemaRefreshService, + schemaProviderRegistry: schemaProviderRegistry, + catalogChangeService: catalogChangeService, + sqlFavoriteManager: sqlFavoriteManager, + favoriteTablesStorage: favoriteTablesStorage, + favoriteDatabasesStorage: favoriteDatabasesStorage, + tableFolderStorage: tableFolderStorage, + aiChatStorage: aiChatStorage, + aiKeyStorage: aiKeyStorage, + aiAccessApprovals: aiAccessApprovals, + groupStorage: groupStorage, + tagStorage: tagStorage, + sshProfileStorage: sshProfileStorage, + credentialProfileStorage: credentialProfileStorage, + featureAccess: featureAccess, + syncMetadataStorage: syncMetadataStorage, + favoritesExpansionState: favoritesExpansionState, + linkedFolderWatcher: linkedFolderWatcher, + queryHistoryManager: manager, + dateFormattingService: dateFormattingService, + copilotService: copilotService, + mcpServerManager: mcpServerManager, + syncTracker: syncTracker, + welcomeRouter: welcomeRouter, + telemetry: telemetry + ) + } +} diff --git a/apple/TableProTests/Views/Main/QueryTabVersionTriggerTests.swift b/apple/TableProTests/Views/Main/QueryTabVersionTriggerTests.swift new file mode 100644 index 0000000000..1a0301d4ba --- /dev/null +++ b/apple/TableProTests/Views/Main/QueryTabVersionTriggerTests.swift @@ -0,0 +1,165 @@ +// +// QueryTabVersionTriggerTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProTestSupport +import Testing + +@MainActor +private final class CaptureSwitch { + var isPaused = false +} + +@MainActor +struct QueryTabVersionTriggerTests { + private let storage = QueryTabVersionStorage( + databaseURL: FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("query_tab_triggers_\(UUID().uuidString).db"), + announce: { _ in }, + removeDatabaseOnDeinit: true + ) + private let capture = CaptureSwitch() + + // The draft-save clock never moves unless a case advances it, so no idle version lands mid-case. + private func makeFixture( + query: String = "SELECT 1", + connection: DatabaseConnection = TestFixtures.makeConnection() + ) -> (CoordinatorFixture, QueryTab, ManualClock) { + let fixture = CoordinatorFixture(connection: connection) + let clock = ManualClock() + fixture.coordinator.draftSaveClock = clock + fixture.coordinator.executionGate = AlwaysDenyGate() + fixture.coordinator.openTabInNewWindow = { _ in } + let capture = capture + fixture.coordinator.tabVersions = QueryTabVersionRecorder( + storage: storage, + isCapturePaused: { capture.isPaused } + ) + let tab = fixture.addTab(QueryTab(title: "Query 1", query: query, tabType: .query)) + return (fixture, tab, clock) + } + + private func versions(of tabId: UUID) -> [(reason: QueryTabVersionReason, text: String?)] { + storage.summaries(tabId: tabId).map { ($0.reason, storage.version(id: $0.id, tabId: tabId)?.query) } + } + + @Test("Running a query tab records its text") + func runRecordsTheText() async throws { + let (fixture, tab, _) = makeFixture(query: "SELECT 1") + + fixture.coordinator.runQuery(viewport: .firstRow) + + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + let recorded = versions(of: tab.id) + #expect(recorded.map { $0.reason } == [.run]) + #expect(recorded.first?.text == "SELECT 1") + } + + @Test("Run All records the tab's text") + func runAllRecordsTheText() async throws { + let (fixture, tab, _) = makeFixture(query: "SELECT 1; SELECT 2") + + fixture.coordinator.runAllStatements() + + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + #expect(versions(of: tab.id).first?.text == "SELECT 1; SELECT 2") + } + + @Test("Clear Query keeps the text it removes as a version") + func clearQueryRecordsTheReplacedText() async throws { + let (fixture, tab, _) = makeFixture(query: "SELECT long_draft FROM t") + + fixture.coordinator.clearSelectedQuery() + + #expect(fixture.tabManager.selectedTab?.content.query == "") + #expect(!fixture.coordinator.toolbarState.hasQueryText) + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + let recorded = versions(of: tab.id) + #expect(recorded.first?.reason == .beforeReplace) + #expect(recorded.first?.text == "SELECT long_draft FROM t") + } + + @Test("Loading a query into the current tab keeps the text it replaces as a version") + func loadIntoEditorRecordsTheReplacedText() async throws { + let (fixture, tab, _) = makeFixture(query: "SELECT typed") + + fixture.coordinator.loadQueryIntoEditor("SELECT * FROM users") + + #expect(fixture.tabManager.selectedTab?.content.query == "SELECT * FROM users") + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + #expect(versions(of: tab.id).first?.reason == .beforeReplace) + #expect(versions(of: tab.id).first?.text == "SELECT typed") + } + + @Test("Inserting a saved query appends it and keeps the text before it") + func insertFavoriteAppendsAndRecords() async throws { + let (fixture, tab, _) = makeFixture(query: "SELECT 1") + + fixture.coordinator.insertFavorite(SQLFavorite(name: "Users", query: "SELECT * FROM users")) + + #expect(fixture.tabManager.selectedTab?.content.query == "SELECT 1\n\nSELECT * FROM users") + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + #expect(versions(of: tab.id).first?.text == "SELECT 1") + } + + // Any window may record and save: the old first-coordinator check left a second window's typing unsaved for 30s. + @Test("An idle pause records a version from a window that is not the connection's first") + func idlePauseRecordsFromAnyWindow() async throws { + let (fixture, tab, clock) = makeFixture(query: "SELECT draft") + let firstWindow = CoordinatorFixture(connection: fixture.connection) + firstWindow.coordinator.registerEagerly() + #expect(MainContentCoordinator.activeCoordinators[fixture.coordinator.instanceId] == nil) + + fixture.coordinator.scheduleDraftSave(editedTab: tab.id) + await clock.waitUntilSleeping() + clock.advance(by: MainContentCoordinator.draftSaveDebounce) + + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + #expect(versions(of: tab.id).first?.reason == .idle) + #expect(versions(of: tab.id).first?.text == "SELECT draft") + } + + @Test("With Query History capture paused, nothing a tab runs or replaces is kept") + func pausedCaptureRecordsNothing() async throws { + let (fixture, tab, clock) = makeFixture(query: "SELECT secret") + capture.isPaused = true + + fixture.coordinator.runQuery(viewport: .firstRow) + fixture.coordinator.loadQueryIntoEditor("SELECT other") + #expect(fixture.coordinator.tabVersions.record(tab, connectionId: fixture.connection.id, reason: .run) == nil) + + // A version recorded after the pause lifts is the only one, so nothing earlier was queued. + capture.isPaused = false + fixture.coordinator.scheduleDraftSave(editedTab: tab.id) + await clock.waitUntilSleeping() + clock.advance(by: MainContentCoordinator.draftSaveDebounce) + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + #expect(versions(of: tab.id).map { $0.reason } == [.idle]) + #expect(versions(of: tab.id).first?.text == "SELECT other") + } + + @Test("Revert To writes the chosen version into the tab and keeps the text it replaced") + func revertRestoresAndRecords() async throws { + let (fixture, tab, _) = makeFixture(query: "SELECT first") + await storage.record(tabId: tab.id, connectionId: fixture.connection.id, text: "SELECT first", reason: .run) + let version = try #require(storage.summaries(tabId: tab.id).first) + fixture.tabManager.mutate(tabId: tab.id) { $0.content.query = "SELECT second" } + + fixture.coordinator.revertQueryTab(tab.id, toVersion: version.id) + + try await waitUntil { fixture.tabManager.selectedTab?.content.query == "SELECT first" } + #expect(versions(of: tab.id).first?.reason == .beforeRestore) + #expect(versions(of: tab.id).first?.text == "SELECT second") + } + + @Test("A table tab records nothing") + func tableTabRecordsNothing() { + let (fixture, _, _) = makeFixture() + let table = QueryTab(title: "users", query: "SELECT * FROM users", tabType: .table, tableName: "users") + #expect(fixture.coordinator.tabVersions.record(table, connectionId: fixture.connection.id, reason: .run) == nil) + } +} diff --git a/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift b/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift index 6afd854e6a..02d5af2e59 100644 --- a/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift +++ b/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift @@ -29,7 +29,8 @@ struct ResultStatusBarLayoutTests { pagination: PaginationState = PaginationState(), statusMessage: String? = nil, selectionSummary: SelectionSummaryState? = nil, - lastTiming: PluginQueryTiming? = nil + lastTiming: PluginQueryTiming? = nil, + autoRefresh: StatusBarAutoRefreshState = .unavailable ) -> ResultStatusBar { let snapshot = StatusBarSnapshot( tabId: UUID(), @@ -92,7 +93,8 @@ struct ResultStatusBarLayoutTests { resultSetMenu: ResultSetMenuModel(entries: [], activeOrdinal: 0, total: 0), resultSetActions: .none, onToggleFilters: {}, - onFetchAll: {} + onFetchAll: {}, + autoRefresh: autoRefresh ) } @@ -629,6 +631,47 @@ struct ResultStatusBarLayoutTests { expectFills(bar, at: pane) } + private static var autoRefreshOn: StatusBarAutoRefreshState { + StatusBarAutoRefreshState(interval: .fiveSeconds, pause: nil, onChange: { _ in }) + } + + @Test("A table tab with Auto Refresh on is never wider than its host", arguments: statusBarHostWidths) + func autoRefreshBarNeverExceedsItsHost(width: CGFloat) { + let bar = makeBar( + rowCount: 1_000, + hasColumns: true, + tabType: .table, + viewMode: .data, + pagination: PaginationState(totalRowCount: 5_000, pageSize: 1_000), + autoRefresh: Self.autoRefreshOn + ) + #expect(bar.model.controls.showsAutoRefresh) + expectFills(bar, at: width) + } + + @Test("The Auto Refresh glyph keeps its own color only while an interval runs") + func autoRefreshGlyphShowsTheOnState() throws { + let on = try #require(ResultStatusBar.autoRefreshSymbol(isOn: true)) + let off = try #require(ResultStatusBar.autoRefreshSymbol(isOn: false)) + + #expect(!on.isTemplate, "a template image draws in the control's color and hides the on state") + #expect(off.isTemplate) + #expect(on.size == off.size, "the glyph must not change width when the interval turns on") + } + + @Test("The narrowest tier still holds 280pt with Auto Refresh on the bar") + func narrowestTierHoldsWithAutoRefresh() { + let bar = makeBar( + rowCount: 1_000, + hasColumns: true, + tabType: .table, + viewMode: .data, + pagination: PaginationState(totalRowCount: 5_000, pageSize: 1_000), + autoRefresh: Self.autoRefreshOn + ) + expectFills(bar, at: 280) + } + /// The grid shares a `VStack` with the bar, closed by a `.frame(maxWidth: .infinity)` that can /// never report less than its widest child, which is how the bar's width reached the grid at /// all. Measuring the column rather than the bar alone is what pins that path. diff --git a/apple/TableProTests/Views/ObjectSource/ObjectSourceLoaderTests.swift b/apple/TableProTests/Views/ObjectSource/ObjectSourceLoaderTests.swift index a9c3151c1f..5e2029f58a 100644 --- a/apple/TableProTests/Views/ObjectSource/ObjectSourceLoaderTests.swift +++ b/apple/TableProTests/Views/ObjectSource/ObjectSourceLoaderTests.swift @@ -117,6 +117,68 @@ struct ObjectSourceLoaderTests { #expect(loader.source == definition) #expect(driver.recordedCalls == [fetchCall]) } + + @Test("A server object reads its definition and fresh attributes through the driver") + func serverObjectLoadsThroughDriver() async { + let driver = ServerObjectSourceDriver() + driver.reread = PluginServerObjectInfo( + kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384", + definition: "CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;", + attributes: [PluginObjectAttribute(label: "Default Version", value: "1.3")] + ) + let kind = SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", + iconName: "puzzlepiece.extension", scope: .database + ) + let object = ServerObjectInfo(kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384") + let ref = DatabaseObjectRef(serverObject: object, kind: kind, database: "shop") + let adapter = PluginDriverAdapter(connection: TestFixtures.makeConnection(type: .postgresql), pluginDriver: driver) + let metadata = ScriptedSourceMetadata(driver: adapter) + let loader = ObjectSourceLoader(connectionId: UUID(), objectRef: ref, metadata: metadata) + + await loader.load() + + // A database-wide object is read with no schema: `public` is only where its members live. + #expect(metadata.scopes.map { $0.schema } == [nil]) + #expect(metadata.scopes.map { $0.database } == ["shop"]) + #expect(loader.source == "CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;") + #expect(loader.attributes == [ObjectAttribute(label: "Default Version", value: "1.3")]) + let requested = driver.requested + #expect(requested.map { $0.kindId } == ["extension"]) + #expect(requested.map { $0.identity } == ["16384"]) + } + + @Test("A server object the driver cannot read reports the failure") + func serverObjectWithoutDefinitionFails() async { + let driver = ServerObjectSourceDriver() + let kind = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + let ref = DatabaseObjectRef(serverObject: ServerObjectInfo(kindId: "event", name: "nightly"), kind: kind, database: "shop") + let adapter = PluginDriverAdapter(connection: TestFixtures.makeConnection(type: .mysql), pluginDriver: driver) + let loader = ObjectSourceLoader(connectionId: UUID(), objectRef: ref, metadata: ScriptedSourceMetadata(driver: adapter)) + + await loader.load() + + guard case .failed = loader.state else { + Issue.record("expected the load to fail") + return + } + } +} + +private final class ServerObjectSourceDriver: PluginDriverStub, @unchecked Sendable { + private let lock = NSLock() + private var log: [PluginServerObjectInfo] = [] + var reread: PluginServerObjectInfo? + + var requested: [PluginServerObjectInfo] { lock.withLock { log } } + + func fetchServerObject(_ object: PluginServerObjectInfo) async throws -> PluginServerObjectInfo { + lock.withLock { log.append(object) } + guard let reread else { throw PluginObjectSourceError.unsupported(object.name) } + return reread + } } /// Throws `CancellationError` once on request, the way a metadata read does when the task that @@ -125,6 +187,7 @@ struct ObjectSourceLoaderTests { private final class ScriptedSourceMetadata: ScopedMetadataProviding { private let driver: DatabaseDriver var cancelsNextRead = false + private(set) var scopes: [DatabaseScope] = [] init(driver: DatabaseDriver) { self.driver = driver @@ -135,6 +198,7 @@ private final class ScriptedSourceMetadata: ScopedMetadataProviding { workload: MetadataConnectionPool.Workload, _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> T { + scopes.append(scope) if cancelsNextRead { cancelsNextRead = false throw CancellationError() diff --git a/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterTests.swift b/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterTests.swift index 21e52a2e21..433a1c4796 100644 --- a/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterTests.swift +++ b/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterTests.swift @@ -132,9 +132,67 @@ struct DatabaseTreeFilterTests { #expect(SidebarObjectKind.visible(itemCounts: buckets.itemCounts) == [.table]) } - @Test("Types is the last kind listed") + @Test("Types is the last built-in kind listed, and declared kinds follow it") func typesIsTheLastKind() { - #expect(SidebarObjectKind.allCases.last == .type) + #expect(SidebarObjectKind.builtIn.last == .type) + let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + #expect(SidebarObjectKind.ordered(declared: [events]).suffix(2) == [.type, .server(events)]) + } + + private var eventKind: SidebarServerObjectKind { + SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + } + + private func event(_ name: String, schema: String = "shop") -> ServerObjectInfo { + ServerObjectInfo(kindId: "event", name: name, schema: schema, detail: "Recurring") + } + + @Test("Server objects are filtered by the search and counted under their declared kind") + func serverObjectsFilteredAndCounted() { + let buckets = DatabaseTreeFilter.objectBuckets( + tables: [table("users")], + routines: [], + triggers: [], + serverObjects: ["event": [event("nightly_cleanup"), event("hourly_rollup")]], + searchText: "nightly" + ) + #expect(buckets.serverObjects["event"]?.map { $0.name } == ["nightly_cleanup"]) + #expect(buckets.itemCounts[.server(eventKind)] == 1) + #expect(SidebarObjectKind.visible(itemCounts: buckets.itemCounts, declared: [eventKind]) == [.server(eventKind)]) + #expect(!buckets.isEmpty) + } + + @Test("A search matching no server object leaves its kind with no section") + func serverObjectsWithoutMatchHaveNoSection() { + let buckets = DatabaseTreeFilter.objectBuckets( + tables: [table("users")], + routines: [], + triggers: [], + serverObjects: ["event": [event("nightly_cleanup")]], + searchText: "users" + ) + #expect(buckets.serverObjects["event"]?.isEmpty == true) + #expect(SidebarObjectKind.visible(itemCounts: buckets.itemCounts, declared: [eventKind]) == [.table]) + } + + @Test("Only server objects keep a container from reading as empty") + func serverObjectsAloneAreContent() { + let buckets = DatabaseTreeFilter.objectBuckets( + tables: [], routines: [], triggers: [], serverObjects: ["event": [event("nightly")]], searchText: "" + ) + #expect(!buckets.isEmpty) + let none = DatabaseTreeFilter.objectBuckets(tables: [], routines: [], triggers: [], serverObjects: ["event": []], searchText: "") + #expect(none.isEmpty) + } + + @Test("One server object listed twice is drawn once") + func serverObjectsDeduplicated() { + let objects = [event("nightly"), event("nightly"), event("nightly", schema: "audit")] + #expect(DatabaseTreeFilter.filteredServerObjects(objects, searchText: "").count == 2) } @Test("visibleSchemas drops system schemas and deduplicates") diff --git a/apple/TableProTests/Views/Sidebar/DatabaseTreeFlatRootTests.swift b/apple/TableProTests/Views/Sidebar/DatabaseTreeFlatRootTests.swift index bc8187afbd..f2f058fcc9 100644 --- a/apple/TableProTests/Views/Sidebar/DatabaseTreeFlatRootTests.swift +++ b/apple/TableProTests/Views/Sidebar/DatabaseTreeFlatRootTests.swift @@ -26,10 +26,17 @@ struct DatabaseTreeFlatRootTests { private let boom = NSError(domain: "test", code: 1, userInfo: [NSLocalizedDescriptionKey: "permission denied"]) - private func makeFixture(_ configure: (SideObjectsMockDriver) -> Void) async throws -> Fixture { + private func makeFixture( + injectsSession: Bool = false, + _ configure: (SideObjectsMockDriver) -> Void + ) async throws -> Fixture { let connection = TestFixtures.makeConnection(database: "shop", type: .postgresql) let driver = SideObjectsMockDriver(connection: connection) configure(driver) + // The outline asks the session's driver which kinds it declares. + if injectsSession { + DatabaseManager.shared.injectSession(ConnectionSession(connection: connection, driver: driver), for: connection.id) + } await SchemaService.shared.load(connectionId: connection.id, driver: driver, connection: connection) let scrollView = SidebarOutlineScaffold.makeScrollView( @@ -69,6 +76,9 @@ struct DatabaseTreeFlatRootTests { } private func tearDown(_ fixture: Fixture) async { + if DatabaseManager.shared.session(for: fixture.connectionId) != nil { + DatabaseManager.shared.removeSession(for: fixture.connectionId) + } await SchemaService.shared.invalidate(connectionId: fixture.connectionId) SharedSidebarState.removeConnection(fixture.connectionId) _ = fixture.scrollView @@ -142,4 +152,86 @@ struct DatabaseTreeFlatRootTests { #expect(rootStatuses(fixture).isEmpty) await tearDown(fixture) } + + private var extensionKind: PluginServerObjectKind { + PluginServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", + iconName: "puzzlepiece.extension", scope: .database + ) + } + + private var extensions: SidebarObjectKind { + .server(SidebarServerObjectKind(extensionKind)) + } + + @Test("A declared kind with objects gets a section after the built-in kinds") + func declaredKindIsListedAfterBuiltIns() async throws { + let fixture = try await makeFixture(injectsSession: true) { driver in + driver.tablesToReturn = [TestFixtures.makeTableInfo(name: "orders", schema: "public")] + driver.declaredServerKinds = [extensionKind] + driver.serverObjectsByKind["extension"] = [ + ServerObjectInfo(kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384", detail: "1.3") + ] + } + + #expect(rootSections(fixture) == [.table, extensions]) + let section = try #require(section(fixture, extensions)) + let rows = fixture.coordinator.resolvedChildren(of: section) + guard case .serverObject(let ref) = rows.first?.kind else { + Issue.record("The Extensions section lists no extension row") + await tearDown(fixture) + return + } + #expect(rows.count == 1) + #expect(ref.object.name == "pgcrypto") + #expect(ref.kind.pluralDisplayName == "Extensions") + #expect(ref.objectRef.kind == .serverObject) + await tearDown(fixture) + } + + @Test("A declared kind that returned nothing gets no section") + func emptyDeclaredKindIsOmitted() async throws { + let fixture = try await makeFixture(injectsSession: true) { driver in + driver.tablesToReturn = [TestFixtures.makeTableInfo(name: "orders", schema: "public")] + driver.declaredServerKinds = [extensionKind] + } + + #expect(rootSections(fixture) == [.table]) + await tearDown(fixture) + } + + @Test("A declared kind whose fetch failed keeps its section, showing the error") + func failedDeclaredKindKeepsItsSection() async throws { + let fixture = try await makeFixture(injectsSession: true) { driver in + driver.tablesToReturn = [TestFixtures.makeTableInfo(name: "orders", schema: "public")] + driver.declaredServerKinds = [extensionKind] + driver.serverObjectsError = boom + } + + #expect(rootSections(fixture) == [.table, extensions]) + let section = try #require(section(fixture, extensions)) + let rows = fixture.coordinator.resolvedChildren(of: section) + guard case .status(let status) = rows.first?.kind else { + Issue.record("The failed section shows no status row") + await tearDown(fixture) + return + } + #expect(status == .error("permission denied")) + await tearDown(fixture) + } + + @Test("A search that matches only a server object keeps its section") + func searchMatchingOnlyAServerObject() async throws { + let fixture = try await makeFixture(injectsSession: true) { driver in + driver.tablesToReturn = [TestFixtures.makeTableInfo(name: "orders", schema: "public")] + driver.declaredServerKinds = [extensionKind] + driver.serverObjectsByKind["extension"] = [ServerObjectInfo(kindId: "extension", name: "pgcrypto", schema: "public")] + } + fixture.viewModel.searchText = "crypto" + fixture.coordinator.searchText = "crypto" + fixture.coordinator.refresh() + + #expect(rootSections(fixture) == [extensions]) + await tearDown(fixture) + } } diff --git a/apple/TableProTests/Views/Sidebar/DatabaseTreeMenuSpecTests.swift b/apple/TableProTests/Views/Sidebar/DatabaseTreeMenuSpecTests.swift index 37660fb554..7e0d7ea310 100644 --- a/apple/TableProTests/Views/Sidebar/DatabaseTreeMenuSpecTests.swift +++ b/apple/TableProTests/Views/Sidebar/DatabaseTreeMenuSpecTests.swift @@ -1041,6 +1041,55 @@ struct DatabaseTreeMenuSpecTests { } } + // MARK: - Server objects + + private var eventKind: SidebarServerObjectKind { + SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + } + + private func serverObjectRef(_ name: String) -> DatabaseTreeServerObjectRef { + DatabaseTreeServerObjectRef( + database: "shop", schema: "shop", kind: eventKind, + object: ServerObjectInfo(kindId: "event", name: name, schema: "shop", detail: "Recurring") + ) + } + + @Test("A server object row copies its name and shows its DDL, and offers nothing that writes") + func serverObjectRowItems() { + let ref = serverObjectRef("nightly_cleanup") + let sections = DatabaseTreeMenuSpec.sections(for: context(clicked: .serverObject(ref))) + + #expect(commands(sections) == [.copyText("nightly_cleanup"), .showObjectSource(ref.objectRef)]) + #expect(titles(sections) == [String(localized: "Copy Name"), String(localized: "Show DDL")]) + #expect(ref.objectRef.kind == .serverObject) + #expect(ref.objectRef.serverKind?.id == "event") + } + + @Test("A server object kind's group offers Refresh and no New Folder or New Type") + func serverObjectGroupItems() { + let group = DatabaseTreeObjectGroup(database: "shop", schema: nil, kind: .server(eventKind)) + let sections = DatabaseTreeMenuSpec.sections(for: context(clicked: .containerObjectKindSection(group))) + + #expect(commands(sections) == [.refreshContainerObjectKind(group)]) + } + + @Test("A flat server object section offers Refresh alone") + func flatServerObjectSectionItems() { + let sections = DatabaseTreeMenuSpec.sections(for: context(clicked: .objectKindSection(.server(eventKind)))) + + #expect(commands(sections) == [.refreshObjectKind(.server(eventKind))]) + } + + @Test("A read-only connection keeps the server object row's menu") + func serverObjectRowReadOnly() { + let ref = serverObjectRef("nightly_cleanup") + let sections = DatabaseTreeMenuSpec.sections(for: context(clicked: .serverObject(ref), isReadOnly: true)) + + #expect(commands(sections) == [.copyText("nightly_cleanup"), .showObjectSource(ref.objectRef)]) + } + // MARK: - Types private func userTypeRef(_ name: String, schema: String? = "public") -> DatabaseTreeUserTypeRef { diff --git a/apple/TableProTests/Views/Sidebar/DatabaseTreeNodeTests.swift b/apple/TableProTests/Views/Sidebar/DatabaseTreeNodeTests.swift index bd0cb34067..427f6f3421 100644 --- a/apple/TableProTests/Views/Sidebar/DatabaseTreeNodeTests.swift +++ b/apple/TableProTests/Views/Sidebar/DatabaseTreeNodeTests.swift @@ -333,4 +333,87 @@ struct DatabaseTreeNodeTests { #expect(groups.map(\.kind) == [.procedure]) #expect(groups.allSatisfy { $0.schema == "HR" }) } + + // MARK: - Server objects + + private let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + private let extensions = SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", iconName: "puzzlepiece.extension", + scope: .database + ) + + @Test("A container kind's group follows Types in a container and carries the declared descriptor") + func containerKindGroupFollowsTypes() { + let groups = DatabaseTreeObjectGroupResolver.groups( + database: "shop", + schema: nil, + itemCounts: [.table: 2, .trigger: 1, .type: 1, .server(SidebarServerObjectKind(id: "event")): 3], + declared: [events] + ) + + #expect(groups.map { $0.kind } == [.table, .trigger, .type, .server(events)]) + #expect(groups.last?.kind.pluralDisplayName == "Events") + } + + @Test("A database kind's group lists beside the schemas only when it holds rows or failed") + func databaseKindGroupNeedsRowsOrFailure() { + let empty = DatabaseTreeObjectGroupResolver.groups(database: "app", schema: nil, itemCounts: [:], declared: [extensions]) + #expect(empty.isEmpty) + + let phases = DatabaseTreeSidePhases( + routines: .idle, triggers: nil, types: nil, serverObjects: ["extension": .failed("permission denied")] + ) + let failed = DatabaseTreeObjectGroupResolver.groups( + database: "app", + schema: nil, + itemCounts: [:], + keeping: phases.failedKinds(among: [.server(extensions)]), + declared: [extensions] + ) + #expect(failed.map { $0.kind } == [.server(extensions)]) + #expect(phases.placeholder(for: .server(extensions)) == .error("permission denied")) + } + + @Test("A server kind's phase is its own fetch's, and an undeclared kind reads as loaded") + func serverKindPhases() { + let phases = DatabaseTreeSidePhases( + routines: .loaded, triggers: .loaded, types: nil, serverObjects: ["event": .loading] + ) + + #expect(phases.phase(for: .server(events)) == .loading) + #expect(phases.phase(for: .server(extensions)) == .loaded) + #expect(phases.all == [.loaded, .loaded, .loading]) + #expect(DatabaseTreeNode.Status.emptyContainer(sideStates: phases.all) == .loading) + #expect(phases.placeholder(for: .server(events)) == nil) + } + + @Test("A server object row is a selectable leaf with an id of its own") + func serverObjectRowIdentity() { + let object = ServerObjectInfo(kindId: "event", name: "nightly", schema: "shop") + let ref = DatabaseTreeServerObjectRef(database: "shop", schema: "shop", kind: events, object: object) + let node = DatabaseTreeNode(id: DatabaseTreeNode.serverObjectId(ref), kind: .serverObject(ref)) + let trigger = DatabaseTreeTriggerRef( + database: "shop", schema: "shop", + trigger: TriggerInfo(name: "nightly", timing: "AFTER", event: "INSERT", statement: "") + ) + + #expect(!node.isExpandable) + #expect(!node.isGroupRow) + #expect(!node.isContainer) + #expect(DatabaseTreeSelection.isSelectable(node.kind)) + #expect(DatabaseTreeNode.serverObjectId(ref) != DatabaseTreeNode.triggerId(trigger)) + #expect(DatabaseTreeDoubleClickResolver.resolve(node: node) == .openObjectSource(ref.objectRef)) + } + + @Test("A declared kind's group id spells its persistence name, and built-in ids are unchanged") + func serverGroupIdsUsePersistenceName() { + #expect(DatabaseTreeNode.objectKindSectionId(.table) == "kindSection\u{1}table") + #expect( + DatabaseTreeNode.containerObjectKindSectionId(objectGroup(kind: .materializedView)) + == "containerKindSection\u{1}shop\u{1}public\u{1}materializedView" + ) + #expect(DatabaseTreeNode.objectKindSectionId(.server(events)) == "kindSection\u{1}server.event") + } } diff --git a/apple/TableProTests/Views/Sidebar/ServerObjectRowLogicTests.swift b/apple/TableProTests/Views/Sidebar/ServerObjectRowLogicTests.swift new file mode 100644 index 0000000000..f25dc8f89e --- /dev/null +++ b/apple/TableProTests/Views/Sidebar/ServerObjectRowLogicTests.swift @@ -0,0 +1,67 @@ +// +// ServerObjectRowLogicTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct ServerObjectRowLogicTests { + private let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + private let extensions = SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", iconName: "puzzlepiece.extension", + scope: .database + ) + + private func event(enabled: Bool?, status: String) -> ServerObjectInfo { + ServerObjectInfo( + kindId: "event", name: "nightly", schema: "shop", detail: "Recurring", isEnabled: enabled, + attributes: [ObjectAttribute(label: "Status", value: status), ObjectAttribute(label: "Schedule", value: "EVERY 1 DAY")] + ) + } + + @Test("The tooltip names the state once, in the driver's own words") + func tooltipNamesStateOnce() { + let tooltip = ServerObjectRowLogic.tooltip(for: event(enabled: false, status: "Disabled on Replica"), kind: events) + + #expect(tooltip == "shop.nightly\nRecurring\nStatus: Disabled on Replica\nSchedule: EVERY 1 DAY") + } + + @Test("A database-wide object's tooltip opens with its bare name") + func databaseScopedTooltipIsBare() { + let object = ServerObjectInfo( + kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384", detail: "1.3", + attributes: [ObjectAttribute(label: "Schema", value: "public")] + ) + + #expect(ServerObjectRowLogic.tooltip(for: object, kind: extensions) == "pgcrypto\n1.3\nSchema: public") + #expect(ServerObjectRowLogic.displayName(for: object, kind: extensions) == "pgcrypto") + #expect(ServerObjectRowLogic.displayName(for: event(enabled: true, status: "Enabled"), kind: events) == "shop.nightly") + } + + @Test("The accessibility label reads kind, name and detail") + func accessibilityLabel() { + let label = ServerObjectRowLogic.accessibilityLabel(for: event(enabled: true, status: "Enabled"), kind: events) + #expect(label == String(format: String(localized: "%1$@: %2$@"), "Event", "nightly") + ", Recurring") + } + + // `ServerObjectInfo` is equal by id, so a row holding it would not redraw a new version or state. + @Test("A refreshed object with the same id hands the row new drawn values") + func refreshedObjectChangesTheDrawnValues() { + let before = ServerObjectInfo(kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384", detail: "1.3") + let after = ServerObjectInfo(kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384", detail: "1.4") + #expect(before == after) + + let oldRow = ServerObjectRowView(object: before, kind: extensions) + let newRow = ServerObjectRowView(object: after, kind: extensions) + + #expect(oldRow.detail == "1.3") + #expect(newRow.detail == "1.4") + #expect(oldRow.tooltip != newRow.tooltip) + #expect(newRow.iconName == "puzzlepiece.extension") + } +} diff --git a/apple/TableProUITests/ManualCommitUITests.swift b/apple/TableProUITests/ManualCommitUITests.swift new file mode 100644 index 0000000000..f355a330eb --- /dev/null +++ b/apple/TableProUITests/ManualCommitUITests.swift @@ -0,0 +1,28 @@ +import XCTest + +final class ManualCommitUITests: UITestCase { + func testManualCommitHoldsAnUpdateUntilItIsRolledBack() throws { + let launch = try launchWithFixture() + let window = launch.window + let app = launch.app + XCTAssertTrue(app.menuBars.firstMatch.waitToExist(timeout: 20), "The app must publish its menu bar") + + menuItem("Manual Commit", under: "Database", in: app).click() + typeQuery("UPDATE note SET body = 'Held' WHERE id = 1;", in: window) + app.typeShortcut(.runQuery) + + let bar = window.descendants(matching: .any)["connection-transaction-bar"].firstMatch + XCTAssertTrue(bar.waitToExist(timeout: 20), "A write in Manual Commit must leave its transaction open") + + let rollBack = bar.buttons["Roll Back"].firstMatch + XCTAssertTrue(waitUntilHittable(rollBack, timeout: 10), "The bar must offer Roll Back") + rollBack.click() + let confirm = window.sheets.buttons["Roll Back"].firstMatch + XCTAssertTrue(waitUntilHittable(confirm, timeout: 10), "Roll Back must ask first") + confirm.click() + + XCTAssertTrue(bar.waitForNonExistence(timeout: 20), "Rolling back must end the transaction the bar shows") + let rows = FixtureFile.rows("SELECT body FROM note WHERE id = 1", in: launch.database) + XCTAssertEqual(rows, [["Buy milk"]], "The rolled-back UPDATE must leave the row as it was") + } +} diff --git a/apple/TableProUITests/TableAutoRefreshUITests.swift b/apple/TableProUITests/TableAutoRefreshUITests.swift new file mode 100644 index 0000000000..53bbf96412 --- /dev/null +++ b/apple/TableProUITests/TableAutoRefreshUITests.swift @@ -0,0 +1,26 @@ +import XCTest + +final class TableAutoRefreshUITests: UITestCase { + func testAutoRefreshShowsAWriteMadeElsewhereWithoutARefresh() throws { + let (launch, grid) = try launchWithFixtureTable("document") + let window = launch.window + XCTAssertTrue(grid.waitForValue("First", row: 1, column: 2), "document must open on its first row") + + let control = window.descendants(matching: .any).matching(identifier: "result-status-auto-refresh").firstMatch + XCTAssertTrue(waitUntilHittable(control, timeout: 20), "A table tab's status bar must offer Auto Refresh") + control.click() + let everyFiveSeconds = control.menuItems["Every 5 Seconds"].firstMatch + XCTAssertTrue(everyFiveSeconds.waitToExist(timeout: 10), "Auto Refresh must list Every 5 Seconds") + everyFiveSeconds.click() + + XCTAssertTrue( + FixtureFile.execute("UPDATE document SET label = 'Written Elsewhere' WHERE id = 1", in: launch.database), + "The fixture file must take the write" + ) + XCTAssertTrue( + grid.waitForValue("Written Elsewhere", row: 1, column: 2, timeout: 30), + "Auto Refresh must show the write with no Refresh, the grid shows " + + "'\(grid.value(row: 1, column: 2) ?? "nil")'" + ) + } +} From 61987e44c242a1f22755a5ad5da7b36291657357 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 21:47:38 +0700 Subject: [PATCH 05/14] fix(macos): keep manual commit to engines whose DDL waits for commit --- .../QueryExecutionCoordinator.swift | 23 ----- .../RowEditingCoordinator+SaveChanges.swift | 23 ----- .../Core/DataWrite/DataWriteExecutor.swift | 5 -- .../Access/AgentTransactionGuard.swift | 18 ++-- .../Database/DatabaseManager+Sessions.swift | 4 +- .../Core/Menu/DatabaseMenuBuilder.swift | 17 ++-- .../TablePro/Core/Plugins/ImportSinkRun.swift | 33 +------ .../Services/Execution/ManualCommit.swift | 89 +++++-------------- ...itViewController+DatabaseMenuActions.swift | 6 +- ...inSplitViewController+MenuValidation.swift | 34 +++++-- .../TablePro/Resources/Localizable.xcstrings | 70 ++++++++++++++- .../ConnectionFormCoordinator+SafeMode.swift | 14 ++- .../Panes/OptionsPaneView.swift | 16 ++-- .../Child/ConnectionTransactionActions.swift | 12 --- .../DataWrite/DataWriteExecutorTests.swift | 27 ------ .../SessionTransactionTrackerTests.swift | 85 +++++++++++------- .../Core/Menu/ManualCommitMenuTests.swift | 43 +++++++-- .../ObjectCopyTransactionTests.swift | 3 +- .../Plugins/ImportManualCommitTests.swift | 47 +++------- .../Execution/ManualCommitTests.swift | 84 ++++++++++------- ...inContentCoordinatorAutoRefreshTests.swift | 55 ++++++++++-- 21 files changed, 369 insertions(+), 339 deletions(-) diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift index 82e3ff91a8..69afa07217 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift @@ -3,7 +3,6 @@ // TablePro // -import AppKit import Combine import Foundation import TableProSQLGrammar @@ -78,10 +77,8 @@ final class QueryExecutionCoordinator: ObservableObject { statements: batches.flatMap(\.statements), extraCapabilities: extraCapabilities ) - let statementTexts = batches.flatMap(\.statements).map(\.sql) Task { [parent] in defer { parent.isShowingSafeModePrompt = false } - guard await confirmsImplicitCommit(of: statementTexts, onTab: target.tabId) else { return } switch await parent.executionGate.authorize(request) { case .authorized: guard parent.rereadCanStart(install.source), stillHolds(target) else { return } @@ -133,24 +130,6 @@ final class QueryExecutionCoordinator: ObservableObject { return true } - // Asked before the gate, for any open transaction, Manual Commit or a typed BEGIN. - private func confirmsImplicitCommit(of statements: [String], onTab tabId: UUID) async -> Bool { - let databaseType = parent.connection.type - let commitMode = DatabaseManager.shared.commitMode(for: parent.connectionId) - // A run Manual Commit refuses is refused in the lease, so it is never offered Commit and Run. - guard ManualCommit.asksBeforeImplicitCommit( - statements, databaseType: databaseType, grammar: parent.lexicalGrammar, commitMode: commitMode - ), - let tab = parent.tabManager.tabs.first(where: { $0.id == tabId }), - let scope = parent.scope(for: tab), - DatabaseManager.shared.sessionTransactions.holdsTransaction( - connectionId: parent.connectionId, container: scope.database - ) - else { return true } - let name = scope.database.isEmpty ? parent.connection.name : scope.database - return await ConnectionTransactionActions.confirmImplicitCommit(on: name, window: parent.contentWindow) - } - private func makeExecuteRequest( statements: [SQLStatementScanner.ExecutableStatement], extraCapabilities: CallerCapabilities = [] @@ -182,10 +161,8 @@ final class QueryExecutionCoordinator: ObservableObject { statements: batches.flatMap(\.statements), extraCapabilities: extraCapabilities ) - let statementTexts = batches.flatMap(\.statements).map(\.sql) Task { [parent] in defer { parent.isShowingSafeModePrompt = false } - guard await confirmsImplicitCommit(of: statementTexts, onTab: target.tabId) else { return } switch await parent.executionGate.authorize(request) { case .authorized: guard parent.rereadCanStart(install.source), stillHolds(target) else { return } diff --git a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift index 02e710469c..417aaabfab 100644 --- a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift +++ b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift @@ -269,14 +269,6 @@ extension RowEditingCoordinator { waiter?.resume(returning: false) return } - if clearTableOps, await !confirmsImplicitCommit(of: plan) { - restorePendingTableOperations( - connectionId: conn.id, truncates: truncatedTables, deletes: deletedTables, options: capturedOptions - ) - endSaveInFlight() - waiter?.resume(returning: false) - return - } let overallStartTime = Date() let operationStart = ContinuousClock.Instant.now @@ -695,21 +687,6 @@ extension RowEditingCoordinator { ) } - // A staged TRUNCATE or DROP on MySQL or Oracle commits the transaction the bar shows before it runs. - private func confirmsImplicitCommit(of plan: DataWritePlan) async -> Bool { - let statements = plan.steps.map(\.statement.sql) - guard ManualCommit.asksBeforeImplicitCommit( - statements, databaseType: parent.connection.type, grammar: parent.lexicalGrammar, - commitMode: DatabaseManager.shared.commitMode(for: parent.connectionId) - ), - DatabaseManager.shared.sessionTransactions.holdsTransaction( - connectionId: parent.connectionId, container: plan.scope.database - ) - else { return true } - let name = plan.scope.database.isEmpty ? parent.connection.name : plan.scope.database - return await ConnectionTransactionActions.confirmImplicitCommit(on: name, window: parent.contentWindow) - } - // Written to the tab that started the save: the one in front may be another by the time it fails. private func failSave(message: String, tabId: UUID?, waiter: CheckedContinuation?) { endSaveInFlight() diff --git a/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift b/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift index 9c9b765f8d..102221c892 100644 --- a/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift +++ b/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift @@ -113,11 +113,6 @@ enum DataWriteExecutor { // A write Manual Commit can't hold would commit with no Commit pressed, so it is refused before anything runs. if commitMode == .manual { guard plan.prologue.isEmpty, plan.epilogue.isEmpty else { throw ManualCommitSessionOptionsRefusal() } - // On MySQL and Oracle a staged TRUNCATE or DROP commits the cell edits saved beside it. - let sql = steps.map(\.statement.sql) - if ManualCommit.mixesImplicitCommit(sql, databaseType: plan.databaseType, grammar: plan.databaseType.lexicalGrammar) { - throw ManualCommitMixedRefusal() - } let state = await driver.heldSessionTransactionState() try ManualCommit.refuseUnheldWrite(supportsTransactions: driver.supportsTransactions, sessionState: state) } diff --git a/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift b/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift index 1003f7dae2..3ef116bd13 100644 --- a/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift +++ b/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift @@ -56,14 +56,10 @@ internal struct AgentTransactionGuard: Sendable { ManualCommit.startsTransaction(statements, databaseType: databaseType, grammar: grammar) { throw OpenTransactionRefusal() } - if commitMode == .manual, startState.holdsUserTransaction, unseen { + // On an idle session the statement's own COMMIT would end the hold this call opens for its write. + if commitMode == .manual, unseen, writes || startState.holdsUserTransaction { throw ManualCommitTransactionControlRefusal() } - // An agent cannot answer the prompt the editor shows before an implicit commit. - if commitMode == .manual, startState.holdsUserTransaction, - ManualCommit.commitsHeldTransaction(statements, databaseType: databaseType, grammar: grammar) { - throw ManualCommitDDLRefusal() - } let protection = Self.protection(family: family, sessionState: startState, controlsTransaction: controls) if protection == .refuse { throw AbortingTransactionRefusal() } @@ -84,15 +80,19 @@ internal struct AgentTransactionGuard: Sendable { let pendingMatters = commitMode == .manual && writes guard pendingMatters || startState.holdsUserTransaction else { return (value, nil) } let endState = await driver.heldSessionTransactionState() - return (value, Self.notice(startState: startState, endState: endState, pendingMatters: pendingMatters)) + let notice = Self.notice( + startState: startState, endState: endState, pendingMatters: pendingMatters, openedHold: opened + ) + return (value, notice) } nonisolated internal static func notice( startState: PluginSessionTransactionState, endState: PluginSessionTransactionState, - pendingMatters: Bool + pendingMatters: Bool, + openedHold: Bool ) -> String? { - if startState.holdsUserTransaction, endState == .idle { return ManualCommit.transactionEndedNotice } + if startState.holdsUserTransaction || openedHold, endState == .idle { return ManualCommit.transactionEndedNotice } return pendingMatters && endState.holdsUserTransaction ? ManualCommit.pendingNotice : nil } diff --git a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift index ea925a1f50..00f04b9f91 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift @@ -1015,8 +1015,10 @@ extension DatabaseManager { connectionStorage.mutateConnections(ids: [connectionId]) { $0.commitMode = mode } } + // What writes run under: a stored Manual Commit on an engine that can't hold runs as automatic. func commitMode(for connectionId: UUID) -> ConnectionCommitMode { - activeSessions[connectionId]?.commitMode ?? .automatic + guard let session = activeSessions[connectionId] else { return .automatic } + return ManualCommit.effectiveMode(session.commitMode, databaseType: session.connection.type) } internal func setSession(_ session: ConnectionSession, for connectionId: UUID) { diff --git a/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift b/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift index af0ebfa94b..167aff86f2 100644 --- a/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift +++ b/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift @@ -11,6 +11,9 @@ enum DatabaseMenuBuilder { static let identifier = NSUserInterfaceItemIdentifier("menu.database") static let liveMessagesIdentifier = NSUserInterfaceItemIdentifier("menu.database.liveMessages") static let manualCommitFootnoteIdentifier = NSUserInterfaceItemIdentifier("menu.database.manualCommitFootnote") + static let manualCommitUnavailableFootnoteIdentifier = NSUserInterfaceItemIdentifier( + "menu.database.manualCommitUnavailableFootnote" + ) static func build(keyboard: KeyboardSettings, modules: Set = []) -> NSMenuItem { let item = MenuItemFactory.menu(String(localized: "Database"), items: [ @@ -287,7 +290,11 @@ enum DatabaseMenuBuilder { String(localized: "Manual Commit"), action: #selector(MainSplitViewController.toggleManualCommit(_:)) ), - manualCommitFootnote(), + manualCommitFootnote( + String(localized: "This database can't report an open transaction, so it commits each change."), + identifier: manualCommitFootnoteIdentifier + ), + manualCommitFootnote(ManualCommit.unavailableExplanation, identifier: manualCommitUnavailableFootnoteIdentifier), MenuItemFactory.item( String(localized: "Commit Transaction"), action: #selector(MainSplitViewController.commitOpenTransaction(_:)), @@ -304,12 +311,10 @@ enum DatabaseMenuBuilder { } // Validation shows it under Manual Commit, which it shares an action with so it always validates as disabled. - private static func manualCommitFootnote() -> NSMenuItem { - let item = MenuFootnote.item( - String(localized: "This database can't report an open transaction, so it commits each change.") - ) + private static func manualCommitFootnote(_ text: String, identifier: NSUserInterfaceItemIdentifier) -> NSMenuItem { + let item = MenuFootnote.item(text) item.action = #selector(MainSplitViewController.toggleManualCommit(_:)) - item.identifier = manualCommitFootnoteIdentifier + item.identifier = identifier item.isHidden = true return item } diff --git a/apple/TablePro/Core/Plugins/ImportSinkRun.swift b/apple/TablePro/Core/Plugins/ImportSinkRun.swift index 22bdd9089d..528c131a3b 100644 --- a/apple/TablePro/Core/Plugins/ImportSinkRun.swift +++ b/apple/TablePro/Core/Plugins/ImportSinkRun.swift @@ -21,8 +21,6 @@ internal final class ImportSinkRun: @unchecked Sendable { private var leavesTransactionOpen = false private var disabledForeignKeys = false private var writesOutsideTransaction: Bool? - private var heldChanges = false - private var holdEndedByFile = false private var finalized = false private var acknowledgedEnding: ImportTransactionOutcome = .untracked private var fileCommittedEarly = false @@ -105,10 +103,7 @@ internal final class ImportSinkRun: @unchecked Sendable { operation: () async throws -> T ) async throws -> T { try await admitted(.data) { - if writes, self.commitMode == .manual { - try self.refuseWriteOutsideTransaction() - if let statement { try self.refuseCommittingStatement(statement) } - } + if writes, self.commitMode == .manual { try self.refuseWriteOutsideTransaction() } if let statement { self.noteStatement(statement) } if writes, let index = self.fileIndex { self.run.record { $0.effects[index]?.admittedWrite = true } @@ -126,7 +121,6 @@ internal final class ImportSinkRun: @unchecked Sendable { } await self.observe() if let statement { self.noteCompletedStatement(statement, before: before, wasOwned: hadAuthority) } - if writes, self.commitMode == .manual, let statement { self.noteHeldStatement(statement) } return value } catch { self.run.record { @@ -149,31 +143,6 @@ internal final class ImportSinkRun: @unchecked Sendable { if outside { throw ManualCommitUnwrappedImportRefusal() } } - // MySQL and Oracle commit a held import's rows at its next DDL, and the rows after that DDL would autocommit. - private func refuseCommittingStatement(_ statement: String) throws { - guard let commits = heldImplicitCommit(statement) else { return } - let (changed, ended) = lock.withLock { (heldChanges, holdEndedByFile) } - if commits ? changed : ended { throw ManualCommitMixedRefusal() } - } - - private func noteHeldStatement(_ statement: String) { - guard let commits = heldImplicitCommit(statement) else { return } - let changes = !commits && OperationKind.worst(of: [statement], databaseType: databaseType).declaresWrite - lock.withLock { - if commits { holdEndedByFile = true } - if changes { heldChanges = true } - } - } - - // Nil unless this import holds its transaction on an engine whose DDL commits it. - private func heldImplicitCommit(_ statement: String) -> Bool? { - guard lock.withLock({ leavesTransactionOpen }), - TableEditDialect.of(databaseType)?.commitsDDLImplicitly == true - else { return nil } - let grammar = SQLLexicalResolver.readings(for: databaseType, session: driver.sessionLexicalState).execution - return ManualCommit.commitsHeldTransaction([statement], databaseType: databaseType, grammar: grammar) - } - private func noteStatement(_ statement: String) { let grammar = SQLLexicalResolver.readings(for: databaseType, session: driver.sessionLexicalState).execution if grammar.contains(.batchSeparatorLines) { diff --git a/apple/TablePro/Core/Services/Execution/ManualCommit.swift b/apple/TablePro/Core/Services/Execution/ManualCommit.swift index 61d2a30d55..24570fdbc1 100644 --- a/apple/TablePro/Core/Services/Execution/ManualCommit.swift +++ b/apple/TablePro/Core/Services/Execution/ManualCommit.swift @@ -23,7 +23,7 @@ internal struct ManualCommitRefusal: ManualCommitRefusing, Equatable { internal struct ManualCommitMixedRefusal: ManualCommitRefusing, Equatable { var errorDescription: String? { String( - localized: "This run mixes changes with a statement that commits them or can't run inside a transaction, so they can't wait for Commit. Run that statement on its own, or turn off Manual Commit in the Database menu." + localized: "This run mixes changes with a statement that can't run in a transaction. Run that statement on its own, or turn off Manual Commit." ) } } @@ -47,15 +47,7 @@ internal struct ManualCommitForeignKeyRefusal: ManualCommitRefusing, Equatable { internal struct ManualCommitUnwrappedImportRefusal: ManualCommitRefusing, Equatable { var errorDescription: String? { String( - localized: "This import runs each statement on its own, so its changes can't wait for Commit. Turn on Wrap in transaction (BEGIN/COMMIT) or turn off Manual Commit to run it." - ) - } -} - -internal struct ManualCommitDDLRefusal: ManualCommitRefusing, Equatable { - var errorDescription: String? { - String( - localized: "This statement commits the transaction open on this connection before it runs. Commit or roll back in TablePro first." + localized: "This import runs each statement on its own, so nothing can wait for Commit. Turn on Wrap in transaction or turn off Manual Commit." ) } } @@ -133,6 +125,22 @@ internal enum ManualCommit { } } + // MySQL, Oracle and ClickHouse commit schema and maintenance statements on their own, so nothing can wait there. + nonisolated internal static func isAvailable(for databaseType: DatabaseType) -> Bool { + TableEditDialect.of(databaseType)?.commitsDDLImplicitly != true + } + + nonisolated internal static func effectiveMode( + _ stored: ConnectionCommitMode, + databaseType: DatabaseType + ) -> ConnectionCommitMode { + isAvailable(for: databaseType) ? stored : .automatic + } + + internal static var unavailableExplanation: String { + String(localized: "This database commits schema and maintenance statements on its own, so changes can't wait for Commit.") + } + // Exactly `.idle` holds: `.unknown` and a table lock would let the write commit on its own. nonisolated internal static func decision( commitMode: ConnectionCommitMode, @@ -157,9 +165,6 @@ internal enum ManualCommit { case .scriptTransaction, .sessionTransaction: return .runAsIs } - if commitsHeldTransaction(statements, databaseType: databaseType, grammar: grammar) { - return mixesImplicitCommit(statements, databaseType: databaseType, grammar: grammar) ? .refuseMixed : .runAsIs - } switch sessionState { case .idle: return .hold @@ -244,53 +249,16 @@ internal enum ManualCommit { } } - // DDL on an engine that commits it implicitly ends a transaction already open on the session. - nonisolated internal static func commitsHeldTransaction( - _ statements: [String], - databaseType: DatabaseType, - grammar: SQLLexicalGrammar - ) -> Bool { - guard TableEditDialect.of(databaseType)?.commitsDDLImplicitly == true else { return false } - return statements.contains { commitsImplicitly($0, databaseType: databaseType, grammar: grammar) } - } - - // On a `commitsDDLImplicitly` engine, an implicit-commit statement beside another write commits that write. - nonisolated internal static func mixesImplicitCommit( - _ statements: [String], - databaseType: DatabaseType, - grammar: SQLLexicalGrammar - ) -> Bool { - guard commitsHeldTransaction(statements, databaseType: databaseType, grammar: grammar) else { return false } - return statements.contains { statement in - !commitsImplicitly(statement, databaseType: databaseType, grammar: grammar) - && writes([statement], databaseType: databaseType) - } - } - - // The editor asks before an implicit commit only for a run Manual Commit would then let through. - nonisolated internal static func asksBeforeImplicitCommit( - _ statements: [String], - databaseType: DatabaseType, - grammar: SQLLexicalGrammar, - commitMode: ConnectionCommitMode - ) -> Bool { - guard commitsHeldTransaction(statements, databaseType: databaseType, grammar: grammar) else { return false } - let decided = decision( - commitMode: commitMode, statements: statements, databaseType: databaseType, grammar: grammar, - supportsTransactions: true, sessionState: .inTransaction - ) - return decided.refusal == nil - } - - // A procedure, a dynamic batch or a T-SQL `IF ... ROLLBACK` can end the transaction without leading with the keyword. + // A procedure or dynamic batch can end the transaction unseen, and so can a T-SQL batch, which needs no `;` before COMMIT. nonisolated internal static func mayEndTransactionUnseen( _ statements: [String], databaseType: DatabaseType, grammar: SQLLexicalGrammar ) -> Bool { - statements.contains { statement in + let scansTokens = TransactionEngineFamily.of(databaseType) == .sqlServer + return statements.contains { statement in CatalogChangeClassifier.effect(of: statement, databaseType: databaseType, grammar: grammar).runsUnseenStatements - || mentionsTransactionControl(statement, grammar: grammar) + || (scansTokens && mentionsTransactionControl(statement, grammar: grammar)) } } @@ -347,19 +315,6 @@ internal enum ManualCommit { OperationKind.worst(of: statements, databaseType: databaseType).declaresWrite } - nonisolated private static func commitsImplicitly( - _ statement: String, - databaseType: DatabaseType, - grammar: SQLLexicalGrammar - ) -> Bool { - let effect = CatalogChangeClassifier.effect(of: statement, databaseType: databaseType, grammar: grammar) - if !effect.kinds.isEmpty, !effect.runsUnseenStatements { return true } - return implicitCommitKeywords.contains(QueryClassifier.leadingKeyword(of: statement, grammar: grammar)) - } - - // Changes no catalog the sidebar lists, and still commits on MySQL and Oracle. - private static let implicitCommitKeywords: Set = ["TRUNCATE", "RENAME", "GRANT", "REVOKE", "LOCK"] - private static let transactionControlKeywords: Set = [ "COMMIT", "ROLLBACK", "END", "ABORT", "SAVEPOINT", "RELEASE", "XA", ] diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+DatabaseMenuActions.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+DatabaseMenuActions.swift index 4ba661bcc9..0a425d1a55 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+DatabaseMenuActions.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+DatabaseMenuActions.swift @@ -127,7 +127,8 @@ extension MainSplitViewController { // Turning it off leaves a transaction already held open, and the bar with it. @objc func toggleManualCommit(_ sender: Any?) { guard let connectionId = workspaces.selectedConnectionId, - let session = DatabaseManager.shared.session(for: connectionId) else { return } + let session = DatabaseManager.shared.session(for: connectionId), + ManualCommit.isAvailable(for: session.connection.type) else { return } DatabaseManager.shared.setCommitMode(session.commitMode == .manual ? .automatic : .manual, for: connectionId) } @@ -171,7 +172,8 @@ extension MainSplitViewController { ) let hold = selectedTransactionHold return TransactionMenuState( - isManualCommitOn: session.commitMode == .manual, + isAvailable: ManualCommit.isAvailable(for: session.connection.type), + isManualCommitOn: DatabaseManager.shared.commitMode(for: coordinator.connectionId) == .manual, canHoldTransactions: reportsState, holdsTransaction: hold != nil, canCommit: hold.map { ConnectionTransactionActions.offersCommit($0, safeModeLevel: session.safeModeLevel) } ?? false diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift index a85ba0b621..a63ab93d2c 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift @@ -8,6 +8,8 @@ import TableProPluginKit // Read only when a transaction command is validated, as the Debug menu's state is. struct TransactionMenuState: Equatable { + // False on an engine that commits schema and maintenance statements on its own. + var isAvailable = false var isManualCommitOn = false // False for an engine with no transactions, or one whose live driver has only answered that it cannot say. var canHoldTransactions = false @@ -381,7 +383,8 @@ extension MainSplitViewController: NSMenuItemValidation { guard context.isConnected else { return false } switch selector { case #selector(toggleManualCommit(_:)): - return context.transaction.isManualCommitOn || context.transaction.canHoldTransactions + return context.transaction.isAvailable + && (context.transaction.isManualCommitOn || context.transaction.canHoldTransactions) case #selector(commitOpenTransaction(_:)): return context.transaction.canCommit case #selector(rollBackOpenTransaction(_:)): @@ -809,7 +812,7 @@ extension MainSplitViewController: NSMenuItemValidation { func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { applyDynamicTitle(to: menuItem) guard let action = menuItem.action else { return false } - if menuItem.identifier == DatabaseMenuBuilder.manualCommitFootnoteIdentifier { return false } + if let identifier = menuItem.identifier, Self.manualCommitFootnotes.contains(identifier) { return false } // AppKit asks this for the View menu and `validateUserInterfaceItem` for the rest, so a rule must live in both. if action == #selector(toggleSidebar(_:)) { return true } if action == #selector(toggleInspector(_:)) { return canToggleTrailingPane } @@ -931,15 +934,28 @@ extension MainSplitViewController: NSMenuItemValidation { } private func applyManualCommitState(to menuItem: NSMenuItem) { - guard menuItem.identifier != DatabaseMenuBuilder.manualCommitFootnoteIdentifier else { return } + guard !Self.manualCommitFootnotes.contains(where: { $0 == menuItem.identifier }) else { return } let state = transactionMenuState setState(state.isManualCommitOn ? .on : .off, on: menuItem) - guard let footnote = menuItem.menu?.items.first(where: { - $0.identifier == DatabaseMenuBuilder.manualCommitFootnoteIdentifier - }) else { return } - // While on, a write is refused rather than committed, which the footnote's wording does not cover. - let hidden = !isConnected || state.canHoldTransactions || state.isManualCommitOn - if footnote.isHidden != hidden { footnote.isHidden = hidden } + let shown = Self.shownManualCommitFootnote(state, isConnected: isConnected) + for footnote in menuItem.menu?.items ?? [] { + guard let identifier = footnote.identifier, Self.manualCommitFootnotes.contains(identifier) else { continue } + let hidden = identifier != shown + if footnote.isHidden != hidden { footnote.isHidden = hidden } + } + } + + static let manualCommitFootnotes = [ + DatabaseMenuBuilder.manualCommitFootnoteIdentifier, + DatabaseMenuBuilder.manualCommitUnavailableFootnoteIdentifier, + ] + + // While on, a write is refused rather than committed, which the reporting footnote's wording does not cover. + static func shownManualCommitFootnote(_ state: TransactionMenuState, isConnected: Bool) -> NSUserInterfaceItemIdentifier? { + guard isConnected else { return nil } + guard state.isAvailable else { return DatabaseMenuBuilder.manualCommitUnavailableFootnoteIdentifier } + guard !state.canHoldTransactions, !state.isManualCommitOn else { return nil } + return DatabaseMenuBuilder.manualCommitFootnoteIdentifier } // The mode is in `representedObject`, so enablement needs the item, not the selector the shared table keys on. diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index ebbd35aef7..0935c27129 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -62015,6 +62015,10 @@ } } }, + "Default Version" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Default view:" : { "localizations" : { "fr" : { @@ -62106,6 +62110,10 @@ } } }, + "Definer" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Definition" : { "localizations" : { "fr" : { @@ -65476,6 +65484,10 @@ } } }, + "Disabled on Replica" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Discard" : { "localizations" : { "fr" : { @@ -72061,6 +72073,8 @@ } }, "Enabled" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -72367,6 +72381,10 @@ } } }, + "Ends" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Ends on %@" : { }, @@ -74624,6 +74642,8 @@ "generatesSymbol" : false }, "Event" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -74663,6 +74683,10 @@ } } }, + "Event Scheduler" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Event Streaming" : { "localizations" : { "fr" : { @@ -74744,6 +74768,8 @@ } }, "Events" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -79239,6 +79265,10 @@ } } }, + "Extension" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Extension File" : { "localizations" : { "fr" : { @@ -101062,6 +101092,10 @@ } } }, + "Last Executed" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Last Hour" : { "localizations" : { "fr" : { @@ -116128,7 +116162,8 @@ } }, "No" : { - + "extractionState" : "manual", + "generatesSymbol" : false }, "No %@" : { "localizations" : { @@ -126456,6 +126491,10 @@ }, "On a partitioned table the drop cannot run concurrently, and it removes the index from every partition." : { + }, + "On Completion" : { + "extractionState" : "manual", + "generatesSymbol" : false }, "On Delete" : { "localizations" : { @@ -127075,6 +127114,10 @@ } } }, + "One Time" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "One username and password, shared by any number of connections. Change it here and every connection using it signs in with the new one." : { "localizations" : { "fr" : { @@ -131410,6 +131453,8 @@ } }, "Owner" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -145375,6 +145420,10 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "Recurring" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Red" : { "localizations" : { "fr" : { @@ -146978,6 +147027,10 @@ } } }, + "Relocatable" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Remote (Quack, experimental)" : { "extractionState" : "manual", "generatesSymbol" : false, @@ -158553,6 +158606,10 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "Schedule" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Schema" : { "extractionState" : "manual", "generatesSymbol" : false, @@ -174160,6 +174217,10 @@ } } }, + "Starts" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "starts with" : { "localizations" : { "fr" : { @@ -175022,6 +175083,8 @@ } }, "Status" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -213892,6 +213955,8 @@ } }, "Version" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -218428,7 +218493,8 @@ } }, "Yes" : { - + "extractionState" : "manual", + "generatesSymbol" : false }, "Yesterday" : { "localizations" : { diff --git a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator+SafeMode.swift b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator+SafeMode.swift index 551b1e9c6f..d88f931b4e 100644 --- a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator+SafeMode.swift +++ b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator+SafeMode.swift @@ -16,8 +16,7 @@ extension ConnectionFormCoordinator { ) } - /// The level the connection will run at, which is what the form shows. Picking the level - /// already shown keeps the user's saved choice, which comes back once the floor lifts. + // Picking the level already shown keeps the saved choice, which comes back once the floor lifts. var effectiveSafeModeLevel: SafeModeLevel { get { safeModeFloor?.raising(customization.safeModeLevel) ?? customization.safeModeLevel } set { @@ -25,4 +24,15 @@ extension ConnectionFormCoordinator { customization.safeModeLevel = newValue } } + + var isManualCommitAvailable: Bool { ManualCommit.isAvailable(for: network.type) } + + // Off where the engine can't hold, since a stored Manual Commit runs as automatic there. + var effectiveManualCommit: Bool { + get { isManualCommitAvailable && customization.commitMode == .manual } + set { + guard isManualCommitAvailable else { return } + customization.commitMode = newValue ? .manual : .automatic + } + } } diff --git a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift index 17ae32741e..fc47a474ab 100644 --- a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift +++ b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift @@ -355,7 +355,8 @@ struct OptionsPaneView: View { private var safetySection: some View { Section { safeModeRow - Toggle(String(localized: "Manual Commit"), isOn: manualCommitBinding) + Toggle(String(localized: "Manual Commit"), isOn: $coordinator.effectiveManualCommit) + .disabled(!coordinator.isManualCommitAvailable) if aiIsEnabled { Picker(String(localized: "AI Policy"), selection: $coordinator.advanced.aiPolicy) { Text(String(localized: "Use Default")) @@ -393,20 +394,17 @@ struct OptionsPaneView: View { } } - private var manualCommitBinding: Binding { - Binding( - get: { coordinator.customization.commitMode == .manual }, - set: { coordinator.customization.commitMode = $0 ? .manual : .automatic } - ) - } - @ViewBuilder private var accessFooter: some View { VStack(alignment: .leading, spacing: 4) { if let floor = coordinator.safeModeFloor { Text(floor.explanation) } - Text(String(localized: "Your first change opens a transaction, and nothing is saved until you commit. Some databases commit schema changes at once.")) + if coordinator.isManualCommitAvailable { + Text(String(localized: "Your first change opens a transaction, and nothing is saved until you commit.")) + } else { + Text(ManualCommit.unavailableExplanation) + } if aiIsEnabled { // swiftlint:disable:next line_length Text(String(localized: "AI Policy controls in-app AI agents. External Clients controls Raycast, Cursor, Claude Desktop, other MCP clients, AppleScript, and Shortcuts. Effective scope is the minimum of the requesting token's scope and the External Clients level.")) diff --git a/apple/TablePro/Views/Main/Child/ConnectionTransactionActions.swift b/apple/TablePro/Views/Main/Child/ConnectionTransactionActions.swift index eb4836c9b3..a1d5d3b0d6 100644 --- a/apple/TablePro/Views/Main/Child/ConnectionTransactionActions.swift +++ b/apple/TablePro/Views/Main/Child/ConnectionTransactionActions.swift @@ -69,18 +69,6 @@ internal enum ConnectionTransactionActions { ) } } - - // MySQL and Oracle commit before DDL runs, so the transaction the bar shows ends with no Commit pressed. - static func confirmImplicitCommit(on name: String, window: NSWindow?) async -> Bool { - await AlertHelper.confirm( - title: String(localized: "Commit the Open Transaction?"), - message: String( - format: String(localized: "This statement commits the transaction open on “%@” before it runs."), name - ), - confirmButton: String(localized: "Commit and Run"), - window: window - ) - } } private struct SessionTransactionDenial: LocalizedError { diff --git a/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift b/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift index 7a23eb604f..9d386a97cd 100644 --- a/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift +++ b/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift @@ -557,33 +557,6 @@ struct DataWriteExecutorHeldTests { #expect(counting.trace.isEmpty) } - // MySQL commits the cell edit implicitly before the TRUNCATE runs. - @Test("A save mixing a cell edit with a staged TRUNCATE on MySQL is refused in Manual Commit") - func mixedImplicitCommitSaveIsRefused() async { - let counting = CountingDriver(affectedRows: 1) - let mixed = DataWritePlan( - scope: DatabaseScope(connectionId: UUID(), database: "shop", schema: nil), - databaseType: .mysql, - steps: [ - DataWriteStep( - kind: .rowWrite, - statement: ParameterizedStatement(sql: "UPDATE `orders` SET `b` = 1", parameters: []), - expectedRowCount: 1, - tableName: "orders" - ), - DataWriteStep( - kind: .rowWrite, - statement: ParameterizedStatement(sql: "TRUNCATE TABLE `audit_log`", parameters: []), - tableName: "audit_log" - ), - ] - ) - await #expect(throws: ManualCommitMixedRefusal()) { - try await DataWriteExecutor.run(mixed, commitMode: .manual, on: driver(counting)) - } - #expect(counting.trace.isEmpty) - } - @Test("A held save marks the turn's hold, and a rolled-back one clears it") func heldSaveMarksTheTurn() async throws { let held = ManualCommitTurnHold() diff --git a/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift b/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift index 06f7f62f44..0286936d63 100644 --- a/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift +++ b/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift @@ -45,7 +45,12 @@ struct SessionTransactionTrackerTests { } // The editor's single-statement path: the hold is opened inside the same turn as the write. - private static func runInMode(_ mode: ConnectionCommitMode, _ sql: String, on connectionId: UUID) async throws { + private static func runInMode( + _ mode: ConnectionCommitMode, + _ sql: String, + on connectionId: UUID, + type: DatabaseType = .postgresql + ) async throws { let scope = DatabaseScope(connectionId: connectionId, database: "app", schema: nil) _ = try await DatabaseManager.shared.withScopedDriver( scope: scope, @@ -53,8 +58,7 @@ struct SessionTransactionTrackerTests { cancellation: .protectedWrite ) { driver in _ = try await ManualCommit.openIfNeeded( - on: driver, statements: [sql], databaseType: .mysql, - grammar: DatabaseType.mysql.lexicalGrammar, commitMode: mode + on: driver, statements: [sql], databaseType: type, grammar: type.lexicalGrammar, commitMode: mode ) return try await driver.execute(query: sql) } @@ -264,7 +268,7 @@ struct SessionTransactionTrackerTests { @Test("In Manual Commit an UPDATE sends BEGIN first and leaves the transaction open") func manualCommitOpensTheHold() async throws { - let (connection, driver) = Self.seed() + let (connection, driver) = Self.seed(type: .postgresql) defer { DatabaseManager.shared.removeSession(for: connection.id) } try await Self.runInMode(.manual, "UPDATE t SET a = 1", on: connection.id) @@ -275,7 +279,7 @@ struct SessionTransactionTrackerTests { @Test("Manual Commit sends no BEGIN for a read, nor a second one for a later write") func manualCommitBeginsOnce() async throws { - let (connection, driver) = Self.seed() + let (connection, driver) = Self.seed(type: .postgresql) defer { DatabaseManager.shared.removeSession(for: connection.id) } try await Self.runInMode(.manual, "SELECT * FROM t", on: connection.id) @@ -289,7 +293,7 @@ struct SessionTransactionTrackerTests { @Test("Automatic commit sends no BEGIN") func automaticCommitOpensNothing() async throws { - let (connection, driver) = Self.seed() + let (connection, driver) = Self.seed(type: .postgresql) defer { DatabaseManager.shared.removeSession(for: connection.id) } try await Self.runInMode(.automatic, "UPDATE t SET a = 1", on: connection.id) @@ -303,7 +307,7 @@ struct SessionTransactionTrackerTests { arguments: [PluginSessionTransactionState.unknown, .holdsSessionLocks] ) func unholdableSessionRefuses(state: PluginSessionTransactionState) async throws { - let (connection, driver) = Self.seed() + let (connection, driver) = Self.seed(type: .postgresql) defer { DatabaseManager.shared.removeSession(for: connection.id) } driver.state = state @@ -390,10 +394,10 @@ struct SessionTransactionTrackerTests { @Test("Manual Commit refuses every MCP transaction action", arguments: ["begin", "commit", "rollback"]) func mcpTransactionControlRefusedInManualCommit(action: String) async throws { - let (connection, driver) = Self.seed(commitMode: .manual) + let (connection, driver) = Self.seed(type: .postgresql, commitMode: .manual) defer { DatabaseManager.shared.removeSession(for: connection.id) } let operation = try await ApprovedOperationFixture.approve( - [], scope: Self.scope(connection.id), databaseType: .mysql, kind: .writeQuery + [], scope: Self.scope(connection.id), databaseType: .postgresql, kind: .writeQuery ) await #expect(throws: DatabaseAccessError.self) { @@ -404,7 +408,7 @@ struct SessionTransactionTrackerTests { @Test("An agent's write in Manual Commit is held, and its reply says the change is pending") func mcpWriteIsHeldAndSaysSo() async throws { - let (connection, driver) = Self.seed(commitMode: .manual) + let (connection, driver) = Self.seed(type: .postgresql, commitMode: .manual) defer { DatabaseManager.shared.removeSession(for: connection.id) } let outcome = try await DatabaseAccessBridge().runStatement( @@ -419,26 +423,9 @@ struct SessionTransactionTrackerTests { #expect(Self.states(connection.id) == [.open]) } - @Test("An agent's DDL over an open transaction in Manual Commit is refused, since MySQL would commit it") - func mcpDDLRefusedOverAHold() async throws { - let (connection, driver) = Self.seed(commitMode: .manual) - defer { DatabaseManager.shared.removeSession(for: connection.id) } - try await Self.run("BEGIN", on: connection.id) - let sent = driver.statements.count - - let error = await #expect(throws: (any Error).self) { - _ = try await DatabaseAccessBridge().runStatement( - scope: Self.scope(connection.id), query: "ALTER TABLE t ADD c INT", maxRows: 10, timeoutSeconds: 30, - cancellation: nil - ) - } - #expect(error?.localizedDescription == ManualCommitDDLRefusal().errorDescription) - #expect(driver.statements.count == sent) - } - @Test("An agent's COMMIT, ROLLBACK or BEGIN in Manual Commit is refused and sends nothing", arguments: ["COMMIT", "ROLLBACK", "BEGIN"]) func mcpTransactionStatementRefusedInManualCommit(sql: String) async throws { - let (connection, driver) = Self.seed(commitMode: .manual) + let (connection, driver) = Self.seed(type: .postgresql, commitMode: .manual) defer { DatabaseManager.shared.removeSession(for: connection.id) } try await Self.runInMode(.manual, "UPDATE t SET a = 1", on: connection.id) let sent = driver.statements.count @@ -458,7 +445,7 @@ struct SessionTransactionTrackerTests { arguments: [ (DatabaseType.mssql, "UPDATE t SET a = 1 COMMIT"), (DatabaseType.mssql, "EXEC dbo.p"), - (DatabaseType.mysql, "CALL p()"), + (DatabaseType.postgresql, "CALL p()"), ] ) func mcpUnseenTransactionControlRefused(type: DatabaseType, sql: String) async throws { @@ -477,6 +464,27 @@ struct SessionTransactionTrackerTests { #expect(Self.states(connection.id) == [.open]) } + // Its own COMMIT would end the hold this call opens for its write. + @Test( + "On an idle Manual Commit session, an agent write that can end a transaction unseen is refused", + arguments: [ + (DatabaseType.mssql, "UPDATE t SET a = 1 COMMIT"), + (DatabaseType.postgresql, "CALL p()"), + ] + ) + func mcpUnseenWriteRefusedOnAnIdleSession(type: DatabaseType, sql: String) async throws { + let (connection, driver) = Self.seed(type: type, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + let error = await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: sql, maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + } + #expect(error?.localizedDescription == ManualCommit.transactionControlRefusalMessage) + #expect(driver.statements.isEmpty) + } + @Test("An agent statement that ended the open transaction says so in its reply") func mcpReplySaysTheTransactionEnded() async throws { let (connection, _) = Self.seed() @@ -560,7 +568,7 @@ struct SessionTransactionTrackerTests { @Test("Turning Manual Commit on writes the session and the connection it was opened from") func menuToggleWritesTheSession() { - let (connection, _) = Self.seed() + let (connection, _) = Self.seed(type: .postgresql) defer { DatabaseManager.shared.removeSession(for: connection.id) } DatabaseManager.shared.setCommitMode(.manual, for: connection.id) @@ -569,6 +577,23 @@ struct SessionTransactionTrackerTests { #expect(DatabaseManager.shared.session(for: connection.id)?.connection.commitMode == .manual) } + @Test("A Manual Commit stored on a MySQL connection runs as automatic: an agent's write sends no BEGIN") + func storedManualCommitOnMySQLRunsAutomatic() async throws { + let (connection, driver) = Self.seed(type: .mysql, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + #expect(DatabaseManager.shared.session(for: connection.id)?.connection.commitMode == .manual) + #expect(DatabaseManager.shared.commitMode(for: connection.id) == .automatic) + + let outcome = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "UPDATE t SET a = 1", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + + #expect(driver.statements.contains("UPDATE t SET a = 1")) + #expect(!driver.statements.contains("BEGIN")) + #expect(outcome.result.statusMessage?.contains(ManualCommit.pendingNotice) != true) + #expect(Self.tracker.holds(for: connection.id).isEmpty) + } + @Test("Read-only hides Commit, an aborted transaction offers only Roll Back") func barContent() { let open = ConnectionTransactionBarContent( diff --git a/apple/TableProTests/Core/Menu/ManualCommitMenuTests.swift b/apple/TableProTests/Core/Menu/ManualCommitMenuTests.swift index 240d025ce9..57a454cc2a 100644 --- a/apple/TableProTests/Core/Menu/ManualCommitMenuTests.swift +++ b/apple/TableProTests/Core/Menu/ManualCommitMenuTests.swift @@ -18,20 +18,24 @@ struct ManualCommitMenuTests { MainSplitViewController.isEnabled(selector, context: context) } - @Test("Manual Commit, its footnote, Commit Transaction and Roll Back Transaction follow Safe Mode Level") + @Test("Manual Commit, its footnotes, Commit Transaction and Roll Back Transaction follow Safe Mode Level") func itemsFollowSafeModeLevel() throws { let items = try databaseMenu().items let safeMode = try #require(items.firstIndex { $0.title == String(localized: "Safe Mode Level") }) - try #require(safeMode + 4 < items.count) + try #require(safeMode + 5 < items.count) #expect(items[safeMode + 1].title == String(localized: "Manual Commit")) #expect(items[safeMode + 1].action == #selector(MainSplitViewController.toggleManualCommit(_:))) #expect(items[safeMode + 2].identifier == DatabaseMenuBuilder.manualCommitFootnoteIdentifier) #expect(items[safeMode + 2].isHidden) - #expect(items[safeMode + 3].title == String(localized: "Commit Transaction")) - #expect(items[safeMode + 3].action == #selector(MainSplitViewController.commitOpenTransaction(_:))) - #expect(items[safeMode + 4].title == String(localized: "Roll Back Transaction")) - #expect(items[safeMode + 4].action == #selector(MainSplitViewController.rollBackOpenTransaction(_:))) + #expect(items[safeMode + 3].identifier == DatabaseMenuBuilder.manualCommitUnavailableFootnoteIdentifier) + #expect(items[safeMode + 3].attributedTitle?.string.replacingOccurrences(of: "\n", with: " ") + == ManualCommit.unavailableExplanation) + #expect(items[safeMode + 3].isHidden) + #expect(items[safeMode + 4].title == String(localized: "Commit Transaction")) + #expect(items[safeMode + 4].action == #selector(MainSplitViewController.commitOpenTransaction(_:))) + #expect(items[safeMode + 5].title == String(localized: "Roll Back Transaction")) + #expect(items[safeMode + 5].action == #selector(MainSplitViewController.rollBackOpenTransaction(_:))) } @Test("Commit and Roll Back Transaction are Keyboard rows with no default chord") @@ -47,6 +51,7 @@ struct ManualCommitMenuTests { func manualCommitEnablement() { let toggle = #selector(MainSplitViewController.toggleManualCommit(_:)) var context = MenuValidationContext() + context.transaction.isAvailable = true context.transaction.canHoldTransactions = true #expect(!enabled(toggle, context)) @@ -60,6 +65,32 @@ struct ManualCommitMenuTests { #expect(enabled(toggle, context)) } + @Test("Where DDL commits on its own, Manual Commit is disabled with its footnote, even when stored on") + func manualCommitUnavailable() { + let toggle = #selector(MainSplitViewController.toggleManualCommit(_:)) + var context = MenuValidationContext() + context.isConnected = true + context.transaction.canHoldTransactions = true + context.transaction.isManualCommitOn = true + #expect(!enabled(toggle, context)) + #expect(MainSplitViewController.shownManualCommitFootnote(context.transaction, isConnected: true) + == DatabaseMenuBuilder.manualCommitUnavailableFootnoteIdentifier) + #expect(MainSplitViewController.shownManualCommitFootnote(context.transaction, isConnected: false) == nil) + } + + @Test("The reporting footnote shows only where the engine can hold but its driver can't report, while off") + func reportingFootnote() { + var state = TransactionMenuState() + state.isAvailable = true + #expect(MainSplitViewController.shownManualCommitFootnote(state, isConnected: true) + == DatabaseMenuBuilder.manualCommitFootnoteIdentifier) + state.canHoldTransactions = true + #expect(MainSplitViewController.shownManualCommitFootnote(state, isConnected: true) == nil) + state.canHoldTransactions = false + state.isManualCommitOn = true + #expect(MainSplitViewController.shownManualCommitFootnote(state, isConnected: true) == nil) + } + @Test("Commit needs an open transaction it may commit; Roll Back needs any held transaction") func commitAndRollBackEnablement() { let commit = #selector(MainSplitViewController.commitOpenTransaction(_:)) diff --git a/apple/TableProTests/Core/ObjectCopy/ObjectCopyTransactionTests.swift b/apple/TableProTests/Core/ObjectCopy/ObjectCopyTransactionTests.swift index 73d2b74ef2..77179ab158 100644 --- a/apple/TableProTests/Core/ObjectCopy/ObjectCopyTransactionTests.swift +++ b/apple/TableProTests/Core/ObjectCopy/ObjectCopyTransactionTests.swift @@ -289,7 +289,8 @@ struct ObjectCopyTransactionTests { // A held copy on a pooled lease would be rolled back when the pool closes it, after the sheet reported success. @Test("Copy To commits its own transaction even on a connection in Manual Commit") func manualCommitConnectionStillCommitsTheCopy() async throws { - var connection = TestFixtures.makeConnection(id: targetScope.connectionId, type: targetType) + // MySQL runs a stored Manual Commit as automatic, so the session is on an engine that holds. + var connection = TestFixtures.makeConnection(id: targetScope.connectionId, type: .postgresql) connection.commitMode = .manual DatabaseManager.shared.injectSession(ConnectionSession(connection: connection), for: connection.id) defer { DatabaseManager.shared.removeSession(for: connection.id) } diff --git a/apple/TableProTests/Core/Plugins/ImportManualCommitTests.swift b/apple/TableProTests/Core/Plugins/ImportManualCommitTests.swift index c217178d55..0faf907b45 100644 --- a/apple/TableProTests/Core/Plugins/ImportManualCommitTests.swift +++ b/apple/TableProTests/Core/Plugins/ImportManualCommitTests.swift @@ -12,12 +12,9 @@ private final class ImportHoldDriver: PluginDriverStub, @unchecked Sendable { private let lock = NSLock() private var current: PluginSessionTransactionState private var sent: [String] = [] - private let ddlCommits: Bool - // `ddlCommits` answers as MySQL does: DDL and LOCK TABLES commit the open transaction. - init(state: PluginSessionTransactionState = .idle, ddlCommits: Bool = false) { + init(state: PluginSessionTransactionState = .idle) { current = state - self.ddlCommits = ddlCommits } var supportsSchemas: Bool { false } @@ -29,13 +26,10 @@ private final class ImportHoldDriver: PluginDriverStub, @unchecked Sendable { func execute(query: String) async throws -> PluginQueryResult { lock.withLock { sent.append(query) - let upper = query.uppercased() - switch upper { + switch query.uppercased() { case "BEGIN": current = .inTransaction case "COMMIT", "ROLLBACK": current = .idle - default: - let head = upper.split(separator: " ").first.map(String.init) ?? "" - if ddlCommits, ["DROP", "CREATE", "ALTER", "LOCK"].contains(head) { current = .idle } + default: break } } return .empty @@ -117,37 +111,18 @@ struct ImportManualCommitTests { #expect(driver.statements.isEmpty) } - // A mysqldump drops and creates each table before its rows; after that DDL the rows would autocommit. - @Test("A held MySQL import refuses the first row after the file's own DDL ended the hold") - func dumpRowsAfterDDLAreRefused() async throws { - let driver = ImportHoldDriver(ddlCommits: true) - let sink = sink(on: driver, type: .mysql) - try await sink.prepareImportRun() - try await sink.beginTransaction() - - for (line, statement) in ["DROP TABLE IF EXISTS t", "CREATE TABLE t (id INT)", "LOCK TABLES t WRITE"].enumerated() { - try await sink.execute(statement: statement, line: line + 1) - } - await #expect(throws: ManualCommitMixedRefusal()) { - try await sink.execute(statement: "INSERT INTO t VALUES (1)", line: 4) - } - - #expect(driver.statements == ["BEGIN", "DROP TABLE IF EXISTS t", "CREATE TABLE t (id INT)", "LOCK TABLES t WRITE"]) - } - - @Test("A held MySQL import refuses DDL once it has written rows, which stay held") - func ddlAfterHeldRowsIsRefused() async throws { - let driver = ImportHoldDriver(ddlCommits: true) - let sink = sink(on: driver, type: .mysql) + @Test("A held import runs a dump's DDL inside its transaction, with no COMMIT") + func heldImportRunsDDL() async throws { + let driver = ImportHoldDriver() + let sink = sink(on: driver) try await sink.prepareImportRun() try await sink.beginTransaction() - try await sink.execute(statement: "INSERT INTO a VALUES (1)", line: 1) - await #expect(throws: ManualCommitMixedRefusal()) { - try await sink.execute(statement: "DROP TABLE IF EXISTS b", line: 2) - } + try await sink.execute(statement: "DROP TABLE IF EXISTS t", line: 1) + try await sink.execute(statement: "INSERT INTO t VALUES (1)", line: 2) + try await sink.commitTransaction() - #expect(driver.statements == ["BEGIN", "INSERT INTO a VALUES (1)"]) + #expect(driver.statements == ["BEGIN", "DROP TABLE IF EXISTS t", "INSERT INTO t VALUES (1)"]) } @Test("A held import marks the turn's hold at its BEGIN") diff --git a/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift b/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift index 8833f54ced..514497da07 100644 --- a/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift +++ b/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift @@ -74,16 +74,20 @@ struct ManualCommitTests { #expect(decision("VACUUM") == .runAsIs) } - @Test("DDL on an engine that commits it implicitly ends the open transaction, elsewhere it does not") - func implicitCommitDDL() { - let mysql = DatabaseType.mysql - #expect(ManualCommit.commitsHeldTransaction(["ALTER TABLE t ADD c INT"], databaseType: mysql, grammar: mysql.lexicalGrammar)) - #expect(ManualCommit.commitsHeldTransaction(["TRUNCATE TABLE t"], databaseType: mysql, grammar: mysql.lexicalGrammar)) - #expect(!ManualCommit.commitsHeldTransaction(["UPDATE t SET a = 1"], databaseType: mysql, grammar: mysql.lexicalGrammar)) - let postgres = DatabaseType.postgresql - #expect(!ManualCommit.commitsHeldTransaction( - ["ALTER TABLE t ADD c INT"], databaseType: postgres, grammar: postgres.lexicalGrammar - )) + @Test("Manual Commit is offered where DDL waits for Commit", arguments: [ + DatabaseType.postgresql, .sqlite, .mssql, .duckdb, .redshift, + ]) + func availableWhereDDLWaits(type: DatabaseType) { + #expect(ManualCommit.isAvailable(for: type)) + #expect(ManualCommit.effectiveMode(.manual, databaseType: type) == .manual) + } + + @Test("Manual Commit is not offered where DDL commits on its own, and a stored one runs as automatic", arguments: [ + DatabaseType.mysql, .mariadb, .oracle, .clickhouse, + ]) + func unavailableWhereDDLCommits(type: DatabaseType) { + #expect(!ManualCommit.isAvailable(for: type)) + #expect(ManualCommit.effectiveMode(.manual, databaseType: type) == .automatic) } @Test("A run that mixes a write with a statement that can't run inside a transaction is refused") @@ -93,11 +97,12 @@ struct ManualCommitTests { #expect(decision(["VACUUM t"]) == .runAsIs) } - @Test("On MySQL a run that mixes a write with DDL is refused, and DDL alone runs as typed") - func implicitCommitBesideAWriteRefuses() { - #expect(decision(["UPDATE t SET a = 1", "ALTER TABLE u ADD c INT"], type: .mysql) == .refuseMixed) - #expect(decision(["ALTER TABLE u ADD c INT"], type: .mysql) == .runAsIs) - #expect(decision(["UPDATE t SET a = 1", "ALTER TABLE u ADD c INT"], type: .postgresql) == .hold) + @Test("DDL beside a write is held like the write where DDL waits for Commit", arguments: [ + DatabaseType.postgresql, .sqlite, .mssql, + ]) + func ddlBesideAWriteIsHeld(type: DatabaseType) { + #expect(decision(["UPDATE t SET a = 1", "ALTER TABLE u ADD c INT"], type: type) == .hold) + #expect(decision(["ALTER TABLE u ADD c INT"], type: type) == .hold) } @Test("Two reads in Manual Commit are not held: the run keeps the plan an automatic connection uses") @@ -160,29 +165,28 @@ struct ManualCommitTests { } } - @Test("The editor never offers Commit and Run for a run Manual Commit refuses") - func noPromptForARefusedRun() { - let mysql = DatabaseType.mysql - let mixed = ["UPDATE a SET x = 1", "ALTER TABLE b ADD c INT"] - #expect(!ManualCommit.asksBeforeImplicitCommit(mixed, databaseType: mysql, grammar: mysql.lexicalGrammar, commitMode: .manual)) - #expect(ManualCommit.asksBeforeImplicitCommit(mixed, databaseType: mysql, grammar: mysql.lexicalGrammar, commitMode: .automatic)) - #expect(ManualCommit.asksBeforeImplicitCommit( - ["ALTER TABLE b ADD c INT"], databaseType: mysql, grammar: mysql.lexicalGrammar, commitMode: .manual - )) - #expect(!ManualCommit.asksBeforeImplicitCommit( - ["UPDATE a SET x = 1"], databaseType: mysql, grammar: mysql.lexicalGrammar, commitMode: .automatic - )) + @Test("Away from SQL Server, a column named like a transaction word is not transaction control", arguments: [ + DatabaseType.postgresql, .mysql, + ]) + func transactionWordsAsColumnsPass(type: DatabaseType) { + let sql = ["SELECT release, commit FROM t"] + #expect(!ManualCommit.mayEndTransactionUnseen(sql, databaseType: type, grammar: type.lexicalGrammar)) + #expect(!ManualCommit.controlsTransaction(sql, databaseType: type, grammar: type.lexicalGrammar)) } @Test("A reply says the transaction ended when it did, and that a held change is pending otherwise") func agentNoticeFollowsTheEndState() { - #expect(AgentTransactionGuard.notice(startState: .inTransaction, endState: .idle, pendingMatters: true) + #expect(AgentTransactionGuard.notice(startState: .inTransaction, endState: .idle, pendingMatters: true, openedHold: false) + == ManualCommit.transactionEndedNotice) + #expect(AgentTransactionGuard.notice(startState: .inTransaction, endState: .idle, pendingMatters: false, openedHold: false) == ManualCommit.transactionEndedNotice) - #expect(AgentTransactionGuard.notice(startState: .inTransaction, endState: .idle, pendingMatters: false) + #expect(AgentTransactionGuard.notice(startState: .idle, endState: .idle, pendingMatters: true, openedHold: true) == ManualCommit.transactionEndedNotice) - #expect(AgentTransactionGuard.notice(startState: .idle, endState: .inTransaction, pendingMatters: true) + #expect(AgentTransactionGuard.notice(startState: .idle, endState: .inTransaction, pendingMatters: true, openedHold: true) == ManualCommit.pendingNotice) - #expect(AgentTransactionGuard.notice(startState: .inTransaction, endState: .inTransaction, pendingMatters: false) == nil) + #expect(AgentTransactionGuard.notice( + startState: .inTransaction, endState: .inTransaction, pendingMatters: false, openedHold: false + ) == nil) } @Test("The refusal says how to run the change") @@ -217,6 +221,24 @@ struct ConnectionCommitModeStorageTests { #expect(ConnectionSession(connection: connection).commitMode == .manual) } + @MainActor + @Test("The form's Manual Commit toggle is off and refuses a change where the engine can't hold") + func formToggleIsDisabledWhereUnavailable() { + let coordinator = ConnectionFormCoordinator(connectionId: nil) + coordinator.network.type = .mysql + coordinator.customization.commitMode = .manual + #expect(!coordinator.isManualCommitAvailable) + #expect(!coordinator.effectiveManualCommit) + coordinator.effectiveManualCommit = false + #expect(coordinator.customization.commitMode == .manual) + + coordinator.network.type = .postgresql + #expect(coordinator.isManualCommitAvailable) + #expect(coordinator.effectiveManualCommit) + coordinator.effectiveManualCommit = false + #expect(coordinator.customization.commitMode == .automatic) + } + @Test("The form writes Manual Commit into the saved fields, and turning it off removes the key") func formEditsCarryTheMode() { var base = TestFixtures.makeConnection(type: .postgresql) diff --git a/apple/TableProTests/Views/Main/MainContentCoordinatorAutoRefreshTests.swift b/apple/TableProTests/Views/Main/MainContentCoordinatorAutoRefreshTests.swift index e24ae9d7f1..e314bd5c04 100644 --- a/apple/TableProTests/Views/Main/MainContentCoordinatorAutoRefreshTests.swift +++ b/apple/TableProTests/Views/Main/MainContentCoordinatorAutoRefreshTests.swift @@ -92,6 +92,37 @@ struct MainContentCoordinatorAutoRefreshTests { withExtendedLifetime(paneState) {} } + @Test("A tick's rows are dropped when a value window opens during its read, and the next tick applies its own") + func tickRowsWaitForAValueWindowOpenedMidRead() async { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + let paneState = TrailingPaneState() + harness.coordinator.trailingPaneState = paneState + let read = AsyncGate() + harness.driver.holdReads(on: read) + + harness.start() + await harness.clock.waitUntilSleeping() + harness.clock.advance(by: .seconds(5)) + await read.arrived(1) + var popOut: JSONViewerWindowController? = JSONViewerWindowController() + if let popOut { InspectorPopOutRegistry.record(popOut, openedFrom: paneState) } + await read.open() + await harness.clock.waitUntilSleeping() + + #expect(harness.tableReads == 1) + #expect(harness.loadedRowCount == 1, "the held tick's two rows must not replace the one on screen") + #expect(harness.tab?.autoRefreshInterval == .fiveSeconds) + + popOut = nil + harness.clock.advance(by: .seconds(5)) + await harness.clock.waitUntilSleeping() + + #expect(harness.tableReads == 2) + #expect(harness.loadedRowCount == 2, "with the window gone the next tick's rows land") + withExtendedLifetime(paneState) {} + } + @Test("A tick waits while the tab's database holds an open transaction") func openTransactionPauses() async throws { let harness = AutoRefreshHarness() @@ -219,6 +250,10 @@ private final class AutoRefreshHarness { driver.sentStatements.filter { $0.contains("jobs") }.count } + var loadedRowCount: Int { + coordinator.tabSessionRegistry.tableRows(for: tabId).rows.count + } + var historyCount: Int { get async { await history.count(scope: .connection(connection.id)) } } @@ -287,6 +322,7 @@ private final class AutoRefreshDriver: DatabaseDriver, @unchecked Sendable { private let lock = NSLock() private var statements: [String] = [] private var fails = false + private var readGate: AsyncGate? init(connection: DatabaseConnection) { self.connection = connection @@ -300,19 +336,26 @@ private final class AutoRefreshDriver: DatabaseDriver, @unchecked Sendable { lock.withLock { fails = true } } - private func answer(_ query: String) throws -> QueryResult { - let failing = lock.withLock { () -> Bool in + // Every later read of the table waits on the gate until the test opens it. + func holdReads(on gate: AsyncGate) { + lock.withLock { readGate = gate } + } + + private func answer(_ query: String) async throws -> QueryResult { + let (failing, gate) = lock.withLock { () -> (Bool, AsyncGate?) in statements.append(query) - return fails && query.contains("jobs") + let readsTable = query.contains("jobs") + return (fails && readsTable, readsTable ? readGate : nil) } + if let gate { await gate.wait() } if failing { throw AutoRefreshReadFailure() } return ScriptAnsweringDriver.resultSet(columns: ["id", "name"], rows: [["1", "first"], ["2", "second"]]) } - func execute(query: String) async throws -> QueryResult { try answer(query) } - func executeParameterized(query: String, parameters: [Any?]) async throws -> QueryResult { try answer(query) } + func execute(query: String) async throws -> QueryResult { try await answer(query) } + func executeParameterized(query: String, parameters: [Any?]) async throws -> QueryResult { try await answer(query) } func executeUserQuery(query: String, rowCap: Int?, parameters: [Any?]?) async throws -> QueryResult { - try answer(query) + try await answer(query) } func connect() async throws {} From f614b994591d86e9435e39f89f911fd6394bcf76 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Wed, 11 Nov 2026 21:53:45 +0700 Subject: [PATCH 06/14] fix(macos): hold MCP row inserts and keep manual commit off Redshift and CockroachDB --- .../Access/DatabaseAccessBridge.swift | 30 +++++++++++++------ .../Core/MCP/Protocol/Tools/DataTools.swift | 3 +- .../Services/Execution/ManualCommit.swift | 7 +++-- .../Panes/OptionsPaneView.swift | 3 +- .../SessionTransactionTrackerTests.swift | 20 +++++++++++++ .../Execution/ManualCommitTests.swift | 4 +-- 6 files changed, 52 insertions(+), 15 deletions(-) diff --git a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift index 2504c3ca6c..058266ea44 100644 --- a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift +++ b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift @@ -555,6 +555,14 @@ internal actor DatabaseAccessBridge { let manager = await databaseManager // No cancel handler: the inserts run under a `.protectedWrite` lease, which nothing may abort. let route = await MainActor.run { manager.executionRoute(for: scope) } + let agentGuard = await MainActor.run { + AgentTransactionGuard( + commitMode: manager.commitMode(for: scope.connectionId), + databaseType: databaseType, + grammar: SQLLexicalResolver.executionGrammar(for: databaseType, connectionId: scope.connectionId), + connectionId: scope.connectionId + ) + } let statements = operation.statements let context = operation.context return try await manager.withScopedDriver( @@ -563,16 +571,20 @@ internal actor DatabaseAccessBridge { cancellation: .protectedWrite ) { driver -> Int in reach.mark() - var affected = 0 - for statement in statements { - let result = try await driver.executeParameterized( - query: statement.sql, - parameters: statement.boundValues, - context: context - ) - affected += max(result.rowsAffected, 0) + // The hold and savepoint execute_query gets, so a Manual Commit insert waits for Commit too. + let ran = try await agentGuard.running(statements.map(\.sql), writes: true, on: driver) { _ in + var affected = 0 + for statement in statements { + let result = try await driver.executeParameterized( + query: statement.sql, + parameters: statement.boundValues, + context: context + ) + affected += max(result.rowsAffected, 0) + } + return affected } - return affected + return ran.value } } diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/DataTools.swift b/apple/TablePro/Core/MCP/Protocol/Tools/DataTools.swift index 771dea9de4..de2f24e50e 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/DataTools.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/DataTools.swift @@ -311,7 +311,8 @@ public struct InsertRowsTool: MCPToolImplementation { public static let description = String( localized: """ Insert rows into a table using bound parameters, so values are never spliced into SQL text. \ - Needs tools:write and goes through the connection's Safe Mode approval. + Needs tools:write and goes through the connection's Safe Mode approval. With Manual Commit on, the rows \ + stay pending until the user commits in TablePro. """ ) public static let requiredScopes: Set = [.toolsWrite] diff --git a/apple/TablePro/Core/Services/Execution/ManualCommit.swift b/apple/TablePro/Core/Services/Execution/ManualCommit.swift index 24570fdbc1..41289f10d5 100644 --- a/apple/TablePro/Core/Services/Execution/ManualCommit.swift +++ b/apple/TablePro/Core/Services/Execution/ManualCommit.swift @@ -125,9 +125,12 @@ internal enum ManualCommit { } } - // MySQL, Oracle and ClickHouse commit schema and maintenance statements on their own, so nothing can wait there. + // These commit schema and maintenance statements on their own, so nothing can wait there; Redshift's TRUNCATE + // and CockroachDB's DDL commit the open transaction too. nonisolated internal static func isAvailable(for databaseType: DatabaseType) -> Bool { - TableEditDialect.of(databaseType)?.commitsDDLImplicitly != true + guard TableEditDialect.of(databaseType)?.commitsDDLImplicitly != true else { return false } + let family = TransactionEngineFamily.of(databaseType) + return family != .redshift && family != .cockroach } nonisolated internal static func effectiveMode( diff --git a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift index fc47a474ab..7ca0269a96 100644 --- a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift +++ b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift @@ -401,7 +401,8 @@ struct OptionsPaneView: View { Text(floor.explanation) } if coordinator.isManualCommitAvailable { - Text(String(localized: "Your first change opens a transaction, and nothing is saved until you commit.")) + // swiftlint:disable:next line_length + Text(String(localized: "Your first change opens a transaction, and nothing is saved until you commit. Structure changes, Users & Roles, Copy To and Generate Rows still save at once.")) } else { Text(ManualCommit.unavailableExplanation) } diff --git a/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift b/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift index 0286936d63..4edbfeb9c8 100644 --- a/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift +++ b/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift @@ -423,6 +423,26 @@ struct SessionTransactionTrackerTests { #expect(Self.states(connection.id) == [.open]) } + @Test("An agent's insert_rows in Manual Commit is held: BEGIN first, and no COMMIT") + func mcpInsertRowsIsHeld() async throws { + let (connection, driver) = Self.seed(type: .postgresql, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let rows = ["INSERT INTO t VALUES (1)", "INSERT INTO t VALUES (2)"] + let operation = try await ApprovedOperationFixture.approve( + rows.map { OperationStatement(sql: $0) }, scope: Self.scope(connection.id), databaseType: .postgresql, + kind: .writeQuery + ) + + _ = try await DatabaseAccessBridge().runInserts(operation) + + let begin = try #require(driver.statements.firstIndex(of: "BEGIN")) + let first = try #require(driver.statements.firstIndex(of: rows[0])) + #expect(begin < first) + #expect(driver.statements.contains(rows[1])) + #expect(!driver.statements.contains("COMMIT")) + #expect(Self.states(connection.id) == [.open]) + } + @Test("An agent's COMMIT, ROLLBACK or BEGIN in Manual Commit is refused and sends nothing", arguments: ["COMMIT", "ROLLBACK", "BEGIN"]) func mcpTransactionStatementRefusedInManualCommit(sql: String) async throws { let (connection, driver) = Self.seed(type: .postgresql, commitMode: .manual) diff --git a/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift b/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift index 514497da07..ab83f3e138 100644 --- a/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift +++ b/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift @@ -75,7 +75,7 @@ struct ManualCommitTests { } @Test("Manual Commit is offered where DDL waits for Commit", arguments: [ - DatabaseType.postgresql, .sqlite, .mssql, .duckdb, .redshift, + DatabaseType.postgresql, .sqlite, .mssql, .duckdb, ]) func availableWhereDDLWaits(type: DatabaseType) { #expect(ManualCommit.isAvailable(for: type)) @@ -83,7 +83,7 @@ struct ManualCommitTests { } @Test("Manual Commit is not offered where DDL commits on its own, and a stored one runs as automatic", arguments: [ - DatabaseType.mysql, .mariadb, .oracle, .clickhouse, + DatabaseType.mysql, .mariadb, .oracle, .clickhouse, .redshift, .cockroachdb, ]) func unavailableWhereDDLCommits(type: DatabaseType) { #expect(!ManualCommit.isAvailable(for: type)) From 1ebbc62d165ae713711847406ef5fafc2d784aa8 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 02:18:22 +0700 Subject: [PATCH 07/14] fix(macos): leave auto refresh to the menu in the narrow status bar and keep trigger saves on their plan --- .../Core/Database/DatabaseManager+SchemaChangeHold.swift | 9 +++++++-- apple/TablePro/Core/Database/TriggerEditing.swift | 4 +++- .../TablePro/Models/Query/ResultStatusPresentation.swift | 5 +++++ apple/TablePro/Views/Results/ResultStatusBar.swift | 2 +- .../Models/ResultStatusPresentationTests.swift | 7 +++++++ apple/TableProTests/Models/ShortcutUniquenessTests.swift | 1 + .../ViewModels/ERDiagramViewModelForeignKeyTests.swift | 1 + 7 files changed, 25 insertions(+), 4 deletions(-) diff --git a/apple/TablePro/Core/Database/DatabaseManager+SchemaChangeHold.swift b/apple/TablePro/Core/Database/DatabaseManager+SchemaChangeHold.swift index 69fd63fac1..3b5711b168 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+SchemaChangeHold.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+SchemaChangeHold.swift @@ -32,8 +32,13 @@ extension DatabaseManager { } // A BEGIN on the session driver joins what a tab left open and its COMMIT ends that, so the DDL joins it instead. - nonisolated static func wrapsSchemaChange(on driver: DatabaseDriver, isPooled: Bool) async -> Bool { - guard wrapsDDLInTransaction(driver) else { return false } + // A trigger plan already chose its transactional strategy, so it skips the DDL check. + nonisolated static func wrapsSchemaChange( + on driver: DatabaseDriver, + isPooled: Bool, + checksTransactionalDDL: Bool = true + ) async -> Bool { + if checksTransactionalDDL, !wrapsDDLInTransaction(driver) { return false } guard !isPooled else { return true } return await driver.heldSessionTransactionState().permitsAppTransaction } diff --git a/apple/TablePro/Core/Database/TriggerEditing.swift b/apple/TablePro/Core/Database/TriggerEditing.swift index 4f7130d2db..69942fbfcd 100644 --- a/apple/TablePro/Core/Database/TriggerEditing.swift +++ b/apple/TablePro/Core/Database/TriggerEditing.swift @@ -176,7 +176,9 @@ enum TriggerEditing { try await withSchemaChangeDriver(scope: scope, route: route) { driver in switch plan.strategy { case let .transactional(dropFirst): - let wraps = await DatabaseManager.wrapsSchemaChange(on: driver, isPooled: isPooled) + let wraps = await DatabaseManager.wrapsSchemaChange( + on: driver, isPooled: isPooled, checksTransactionalDDL: false + ) try await runInTransaction( driver: driver, dropSQL: dropFirst ? plan.dropSQL : nil, sql: sql, wraps: wraps ) diff --git a/apple/TablePro/Models/Query/ResultStatusPresentation.swift b/apple/TablePro/Models/Query/ResultStatusPresentation.swift index 54c3832824..1b34bb2bf7 100644 --- a/apple/TablePro/Models/Query/ResultStatusPresentation.swift +++ b/apple/TablePro/Models/Query/ResultStatusPresentation.swift @@ -52,6 +52,8 @@ struct ResultStatusPresentation: Equatable { /// ("2/4"). It never leaves the bar: Pin and Close live in its menu and have no other one-click /// route, which is the same reason nothing that opens a popover may leave. let resultSetMenuIsSpelledOut: Bool + // Opens a menu, not a popover, and Database > Auto Refresh carries the same choice. + let showsAutoRefresh: Bool init(tier: StatusBarTier) { switch tier { @@ -61,18 +63,21 @@ struct ResultStatusPresentation: Equatable { showsEdgePageButtons = true pageSizeIsInline = true resultSetMenuIsSpelledOut = true + showsAutoRefresh = true case .compact: showsControlTitles = false modeSwitcherIsSegmented = true showsEdgePageButtons = false pageSizeIsInline = true resultSetMenuIsSpelledOut = false + showsAutoRefresh = true case .narrow: showsControlTitles = false modeSwitcherIsSegmented = false showsEdgePageButtons = false pageSizeIsInline = false resultSetMenuIsSpelledOut = false + showsAutoRefresh = false } } } diff --git a/apple/TablePro/Views/Results/ResultStatusBar.swift b/apple/TablePro/Views/Results/ResultStatusBar.swift index 601444a947..dbea74b74b 100644 --- a/apple/TablePro/Views/Results/ResultStatusBar.swift +++ b/apple/TablePro/Views/Results/ResultStatusBar.swift @@ -292,7 +292,7 @@ struct ResultStatusBar: View { if model.controls.showsHighlightRules { highlightButton } - if model.controls.showsAutoRefresh { + if model.controls.showsAutoRefresh, presentation.showsAutoRefresh { autoRefreshMenu(presentation) } if model.controls.showsFilters { diff --git a/apple/TableProTests/Models/ResultStatusPresentationTests.swift b/apple/TableProTests/Models/ResultStatusPresentationTests.swift index f94955df69..2ff0e29653 100644 --- a/apple/TableProTests/Models/ResultStatusPresentationTests.swift +++ b/apple/TableProTests/Models/ResultStatusPresentationTests.swift @@ -35,6 +35,13 @@ struct ResultStatusPresentationTests { #expect(!presentation(.narrow).pageSizeIsInline) } + @Test("Only the narrowest tier leaves Auto Refresh to the Database menu") + func narrowLeavesAutoRefreshToTheMenu() { + #expect(presentation(.regular).showsAutoRefresh) + #expect(presentation(.compact).showsAutoRefresh) + #expect(!presentation(.narrow).showsAutoRefresh) + } + @Test("The narrowest tier swaps the segmented switcher for a pull-down") func narrowUsesAMenuSwitcher() { #expect(!presentation(.narrow).modeSwitcherIsSegmented) diff --git a/apple/TableProTests/Models/ShortcutUniquenessTests.swift b/apple/TableProTests/Models/ShortcutUniquenessTests.swift index 56c209ebbd..0011b93bda 100644 --- a/apple/TableProTests/Models/ShortcutUniquenessTests.swift +++ b/apple/TableProTests/Models/ShortcutUniquenessTests.swift @@ -60,6 +60,7 @@ struct ShortcutUniquenessTests { .newAgentSession, .openAgentSession, .closeAgentSession, .deleteAgentSession, .newAIConversation, .showPreviousWindowTab, .showNextWindowTab, .toggleBreakpoint, + .commitTransaction, .rollbackTransaction, ] let unbound = Set(ShortcutAction.allCases.filter { KeyboardSettings.defaultShortcuts[$0] == nil }) let unlisted = unbound.subtracting(listed).map(\.rawValue).sorted() diff --git a/apple/TableProTests/ViewModels/ERDiagramViewModelForeignKeyTests.swift b/apple/TableProTests/ViewModels/ERDiagramViewModelForeignKeyTests.swift index 0651eb860e..be40451d62 100644 --- a/apple/TableProTests/ViewModels/ERDiagramViewModelForeignKeyTests.swift +++ b/apple/TableProTests/ViewModels/ERDiagramViewModelForeignKeyTests.swift @@ -3,6 +3,7 @@ // TableProTests // +import CoreGraphics import Foundation @testable import TablePro import Testing From 658a16c31a014e1333831969b0e56f102043a1e8 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 02:24:04 +0700 Subject: [PATCH 08/14] refactor(macos): read the commit mode inside the parameterized multi-statement run --- .../Coordinators/QueryExecutionCoordinator+Parameters.swift | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift index 97efe7d187..7597fc3493 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift @@ -368,7 +368,6 @@ extension QueryExecutionCoordinator { let transactionKind = OperationKind.worst(of: statementTexts, databaseType: conn.type) let grammar = parent.lexicalGrammar let plan = BatchTransactionPolicy.plan(for: statementTexts, databaseType: conn.type, grammar: grammar) - let commitMode = DatabaseManager.shared.commitMode(for: conn.id) let prepared = statements.map { statement in prepareStatement( statement: statement, @@ -408,7 +407,6 @@ extension QueryExecutionCoordinator { scope: scope, mode: transactionKind.transactionAccessMode, plan: plan, - commitMode: commitMode, asksEndState: asksEndState, claim: claim, lease: lease @@ -537,11 +535,11 @@ extension QueryExecutionCoordinator { scope: DatabaseScope, mode: PluginTransactionAccessMode, plan: BatchTransactionPlan, - commitMode: ConnectionCommitMode, asksEndState: Bool, claim: TabExecutionClaim, lease: DriverLeaseOwner ) async -> MultiStatementRun { + let commitMode = DatabaseManager.shared.commitMode(for: scope.connectionId) let failureOutput = ServerOutputBox() let commitOutput = ServerOutputBox() let databaseType = parent.connection.type From 1a0747532e80cb7651404167e58d0655a65cb78f Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 02:28:25 +0700 Subject: [PATCH 09/14] chore(macos): add the new strings to the catalog --- .../TablePro/Resources/Localizable.xcstrings | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 559ced0f1b..51af3cc9ce 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -1072,6 +1072,9 @@ } } } + }, + "“%@” is used more than once." : { + }, "“%@” isn't a port or a range of ports, such as 4000-4999." : { "extractionState" : "manual", @@ -17582,6 +17585,9 @@ } } } + }, + "A transaction is already open on this connection, or it cannot say whether one is. Commit or roll it back first." : { + }, "A transaction is already open. Commit or roll it back first." : { "extractionState" : "manual", @@ -17594,9 +17600,15 @@ } } } + }, + "A Transaction Is Open" : { + }, "A transaction is open on %@. Switching databases reconnects, and the server would roll it back. Commit or roll back the transaction, then switch again." : { + }, + "A transaction is open on this connection, and a failed statement would abort it. Commit or roll back in TablePro first." : { + }, "A transaction is open on this connection, and its uncommitted rows aren't in these counts." : { @@ -19732,6 +19744,9 @@ }, "Add column “%@”" : { + }, + "Add Column Pair" : { + }, "Add Column…" : { "localizations" : { @@ -29711,6 +29726,33 @@ } } } + }, + "Auto Refresh" : { + + }, + "Auto Refresh waits until the connection is back." : { + + }, + "Auto Refresh waits while a cell is being edited." : { + + }, + "Auto Refresh waits while a transaction is open." : { + + }, + "Auto Refresh waits while a value is open in its own window." : { + + }, + "Auto Refresh waits while rows are selected in a table without a primary key." : { + + }, + "Auto Refresh waits while rows load." : { + + }, + "Auto Refresh waits while the table has unsaved changes." : { + + }, + "Auto Refresh: %@" : { + }, "AUTO_INCREMENT columns whose next value has reached 75% of the largest value their type holds." : { @@ -31547,6 +31589,12 @@ }, "Before" : { + }, + "Before Replace" : { + + }, + "Before Restore" : { + }, "BEFORE, AFTER, or INSTEAD OF" : { "localizations" : { @@ -31587,6 +31635,9 @@ } } } + }, + "Begin, commit, or roll back a transaction on the connection's shared session. The transaction stays open across calls until committed or rolled back, and it is the same session the user's own tabs run on, so leave nothing open. Begin is refused while a transaction is already open, and every action is refused on a connection with Manual Commit on, where the user commits in TablePro. Needs tools:write." : { + }, "Begin, commit, or roll back a transaction on the connection's shared session. The transaction stays open across calls until committed or rolled back, and it is the same session the user's own tabs run on, so leave nothing open. Needs tools:write." : { "localizations" : { @@ -32519,6 +32570,9 @@ } } } + }, + "Browse All Versions…" : { + }, "Browse needs a TableName" : { "extractionState" : "manual", @@ -37998,6 +38052,9 @@ }, "Choose a character set first" : { + }, + "Choose a column and a referenced column in every row." : { + }, "Choose a connection" : { "localizations" : { @@ -44102,6 +44159,9 @@ } } } + }, + "Column → Referenced Column" : { + }, "Column comment" : { "extractionState" : "manual", @@ -45572,6 +45632,9 @@ }, "Commit the transaction" : { + }, + "Commit Transaction" : { + }, "Commit…" : { @@ -74860,6 +74923,12 @@ }, "Every 5 Minutes" : { + }, + "Every 5 Seconds" : { + + }, + "Every 10 Seconds" : { + }, "Every 15 minutes" : { "localizations" : { @@ -74883,6 +74952,9 @@ } } } + }, + "Every 30 Seconds" : { + }, "Every call that reaches a connection asks first." : { "localizations" : { @@ -95906,6 +95978,9 @@ } } } + }, + "Insert rows into a table using bound parameters, so values are never spliced into SQL text. Needs tools:write and goes through the connection's Safe Mode approval. With Manual Commit on, the rows stay pending until the user commits in TablePro." : { + }, "Insert rows the target is missing" : { "localizations" : { @@ -99440,6 +99515,9 @@ } } } + }, + "Keeps up to %1$d versions from the last %2$d days on this Mac." : { + }, "Keeps what rows looked like before each save, on this Mac only, for 7 days. It is encrypted, and never synced." : { "localizations" : { @@ -107789,6 +107867,15 @@ } } } + }, + "Manual Commit" : { + + }, + "Manual Commit is on for this connection, so this change is pending until the user commits it in TablePro." : { + + }, + "Manual Commit is on for this connection. Commit or roll back in TablePro." : { + }, "Many Databases, One App" : { "localizations" : { @@ -142621,6 +142708,9 @@ }, "Quit while work is still running?" : { + }, + "Quit with an Open Transaction?" : { + }, "Quit Without Saving" : { @@ -145972,6 +146062,9 @@ } } } + }, + "Referenced Column" : { + }, "Referenced row not found" : { "localizations" : { @@ -146709,6 +146802,9 @@ }, "Relaunch while work is still running?" : { + }, + "Relaunch with an Open Transaction?" : { + }, "Release" : { "localizations" : { @@ -147660,6 +147756,9 @@ } } } + }, + "Remove Column Pair" : { + }, "Remove Comment" : { "localizations" : { @@ -152817,6 +152916,9 @@ }, "Revert Changes" : { + }, + "Revert To" : { + }, "review" : { "extractionState" : "stale", @@ -153479,6 +153581,9 @@ }, "Roll Back the Transaction?" : { + }, + "Roll Back Transaction" : { + }, "Roll it back to continue." : { @@ -155894,6 +155999,9 @@ }, "Run one statement, or on SQL Server a whole script. Reads need tools:read; anything that writes needs tools:write and is subject to the connection's Safe Mode, which asks the user to approve it. Destructive statements, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE, go through confirm_destructive_operation instead. Statements that read or write files, or run server-side code, are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at its GO lines and result_sets lists every result set when there is more than one." : { + }, + "Run one statement, or on SQL Server a whole script. Reads need tools:read; anything that writes needs tools:write and is subject to the connection's Safe Mode, which asks the user to approve it. Destructive statements, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE, go through confirm_destructive_operation instead. Statements that read or write files, or run server-side code, are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at its GO lines and result_sets lists every result set when there is more than one. A statement that starts a transaction while one is open is refused. With Manual Commit on, a write stays pending until the user commits it in TablePro, and statements that begin, commit or roll back a transaction are refused. While a transaction is open on PostgreSQL or CockroachDB a statement runs inside a savepoint, so its failure leaves the transaction usable; on Redshift and DuckDB it is refused." : { + }, "Run one statement, or on SQL Server a whole script. Reads need tools:read; anything that writes needs tools:write and is subject to the connection's Safe Mode, which asks the user to approve it. DROP and TRUNCATE go through confirm_destructive_operation instead. Statements that read or write files, or run server-side code, are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at its GO lines and result_sets lists every result set when there is more than one." : { "localizations" : { @@ -156330,6 +156438,9 @@ }, "Run what execute_query refuses as destructive, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE: one statement, or on SQL Server a whole script. Anything else is refused. The user approves it before it runs: through your elicitation prompt when your client supports elicitation, otherwise through TablePro's own confirmation dialog on the user's Mac. Needs tools:write and a connection the user left writable for external clients." : { + }, + "Run what execute_query refuses as destructive, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE: one statement, or on SQL Server a whole script. Anything else is refused. The user approves it before it runs: through your elicitation prompt when your client supports elicitation, otherwise through TablePro's own confirmation dialog on the user's Mac. Needs tools:write and a connection the user left writable for external clients. The transaction rules of execute_query apply, Manual Commit's among them." : { + }, "Run Without Limit" : { "localizations" : { @@ -186469,6 +186580,9 @@ } } } + }, + "The change did not run." : { + }, "The change to this table's structure will be lost." : { @@ -190673,6 +190787,9 @@ }, "The open transaction failed. Roll it back yourself." : { + }, + "The open transaction on this connection ended while this statement ran." : { + }, "The operation failed." : { "localizations" : { @@ -197296,6 +197413,12 @@ } } } + }, + "This change runs on its own connection and can wait until you commit or roll back the open transaction." : { + + }, + "This change sets session options around its statements, so it can't wait for Commit. Turn off Manual Commit in the Database menu to run it." : { + }, "This change was not authorized" : { "localizations" : { @@ -197493,6 +197616,9 @@ } } } + }, + "This connection can't hold this change until you commit, so it did not run. Turn off Manual Commit in the Database menu to run it." : { + }, "This connection can't open the separate session live messages need." : { @@ -198257,6 +198383,9 @@ } } } + }, + "This database can't report an open transaction, so it commits each change." : { + }, "This database cannot be renamed" : { "extractionState" : "manual", @@ -198458,6 +198587,9 @@ }, "This database cannot store a table comment." : { + }, + "This database commits schema and maintenance statements on its own, so changes can't wait for Commit." : { + }, "This database did not return a query plan for the statement." : { "comment" : "Text displayed in a description when the database did not return a query plan for the statement.", @@ -200068,6 +200200,12 @@ } } } + }, + "This import runs each statement on its own, so nothing can wait for Commit. Turn on Wrap in transaction or turn off Manual Commit." : { + + }, + "This import turns off foreign key checks, which can't wait for Commit. Turn off Disable foreign key checks or Manual Commit to run it." : { + }, "This index needs a name." : { "localizations" : { @@ -201916,6 +202054,9 @@ } } } + }, + "This run mixes changes with a statement that can't run in a transaction. Run that statement on its own, or turn off Manual Commit." : { + }, "This save also edits key columns, and the counts don't include those edits." : { @@ -202514,6 +202655,9 @@ }, "This Tab" : { + }, + "This tab is closed. Reopen it to see its history." : { + }, "This tab is on %@. Switch the connection to that database to run it." : { "localizations" : { @@ -203256,6 +203400,9 @@ } } } + }, + "This will permanently delete all query history entries, the saved changes kept for restoring, and the earlier versions of your query tabs. This action cannot be undone." : { + }, "This will reset all settings across every section to their default values." : { "localizations" : { @@ -207766,6 +207913,12 @@ } } } + }, + "Uncommitted changes on “%@” are rolled back when TablePro quits." : { + + }, + "Uncommitted changes on %@ are rolled back when TablePro quits." : { + }, "Under MCP Servers click \"+ Add Custom Server\", select the Local tab, paste the JSON below, then click Add Server" : { "localizations" : { @@ -219155,6 +219308,9 @@ } } } + }, + "Your first change opens a transaction, and nothing is saved until you commit. Structure changes, Users & Roles, Copy To and Generate Rows still save at once." : { + }, "Your license has expired" : { "localizations" : { From dd45c00a1230d10c519b5d5bc5b4c12ebf8574b0 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 02:43:08 +0700 Subject: [PATCH 10/14] fix(macos): take back a whole insert_rows call that fails inside an open transaction and name Shortcuts in the Manual Commit footer --- .../Access/AgentTransactionGuard.swift | 71 ++++++++++++++++--- .../Access/DatabaseAccessBridge.swift | 4 +- .../Panes/OptionsPaneView.swift | 2 +- .../SessionTransactionTrackerTests.swift | 38 ++++++++++ .../Execution/ManualCommitTests.swift | 19 +++++ 5 files changed, 123 insertions(+), 11 deletions(-) diff --git a/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift b/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift index 3ef116bd13..2b9ae43802 100644 --- a/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift +++ b/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift @@ -26,6 +26,48 @@ internal struct AgentTransactionGuard: Sendable { case refuse } + internal struct Savepoint: Equatable, Sendable { + let open: String + let release: String? + let rollback: String + + static let standard = Savepoint(open: savepointStatement, release: releaseStatement, rollback: rollbackStatement) + + // SQL Server and Oracle have no RELEASE; a savepoint lasts until the transaction ends. + nonisolated static func of(_ family: TransactionEngineFamily) -> Savepoint? { + switch family { + case .postgres, .cockroach, .mysql, .sqlite: + return .standard + case .sqlServer: + return Savepoint( + open: "SAVE TRANSACTION tablepro_agent", release: nil, rollback: "ROLLBACK TRANSACTION tablepro_agent" + ) + case .oracle: + return Savepoint(open: savepointStatement, release: nil, rollback: rollbackStatement) + case .redshift, .duckdb, .redis, .other: + return nil + } + } + } + + // A call of several writes joining the user's open transaction is taken back whole when one fails, so no half + // of it waits for the user's Commit. + nonisolated internal static func savepoint( + family: TransactionEngineFamily, + protection: Protection, + sessionState: PluginSessionTransactionState, + asOneUnit: Bool + ) -> Savepoint? { + switch protection { + case .savepoint: + return Savepoint.of(family) + case .refuse: + return nil + case .none: + return asOneUnit && sessionState == .inTransaction ? Savepoint.of(family) : nil + } + } + // A failed statement inside an open block aborts it on these engines, which takes the user's pending work with it. nonisolated internal static func protection( family: TransactionEngineFamily, @@ -42,6 +84,7 @@ internal struct AgentTransactionGuard: Sendable { nonisolated func running( _ statements: [String], writes: Bool, + asOneUnit: Bool = false, on driver: DatabaseDriver, _ body: (_ openedHold: Bool) async throws -> T ) async throws -> (value: T, notice: String?) { @@ -49,7 +92,8 @@ internal struct AgentTransactionGuard: Sendable { if commitMode == .manual, controls { throw ManualCommitTransactionControlRefusal() } let family = TransactionEngineFamily.of(databaseType) let unseen = ManualCommit.mayEndTransactionUnseen(statements, databaseType: databaseType, grammar: grammar) - let readsState = commitMode == .manual || controls || unseen || family.failedStatementAbortsTransaction + let readsState = asOneUnit || commitMode == .manual || controls || unseen + || family.failedStatementAbortsTransaction let startState: PluginSessionTransactionState = readsState ? await driver.heldSessionTransactionState() : .unknown // MySQL's START TRANSACTION commits the transaction already open; PostgreSQL's BEGIN warns and joins it. if controls, startState.holdsUserTransaction, @@ -63,13 +107,17 @@ internal struct AgentTransactionGuard: Sendable { let protection = Self.protection(family: family, sessionState: startState, controlsTransaction: controls) if protection == .refuse { throw AbortingTransactionRefusal() } + let savepoint = Self.savepoint( + family: family, protection: protection, sessionState: startState, asOneUnit: asOneUnit + ) + let opened = try await ManualCommit.openIfNeeded( on: driver, statements: statements, databaseType: databaseType, grammar: grammar, commitMode: commitMode ) let value: T do { - if protection == .savepoint { - value = try await savepointed(on: driver) { try await body(opened) } + if let savepoint { + value = try await savepointed(savepoint, on: driver) { try await body(opened) } } else { value = try await body(opened) } @@ -96,17 +144,22 @@ internal struct AgentTransactionGuard: Sendable { return pendingMatters && endState.holdsUserTransaction ? ManualCommit.pendingNotice : nil } - nonisolated private func savepointed(on driver: DatabaseDriver, _ body: () async throws -> T) async throws -> T { - _ = try await driver.execute(query: Self.savepointStatement) + nonisolated private func savepointed( + _ savepoint: Savepoint, + on driver: DatabaseDriver, + _ body: () async throws -> T + ) async throws -> T { + _ = try await driver.execute(query: savepoint.open) let value: T do { value = try await body() } catch { - await Self.rollBackToSavepoint(on: driver, connectionId: connectionId) + await Self.rollBack(to: savepoint, on: driver, connectionId: connectionId) throw error } + guard let release = savepoint.release else { return value } do { - _ = try await driver.execute(query: Self.releaseStatement) + _ = try await driver.execute(query: release) } catch { Self.logger.warning("Releasing the agent savepoint failed: \(error.publicLogShape, privacy: .public)") } @@ -114,12 +167,12 @@ internal struct AgentTransactionGuard: Sendable { } @MainActor - private static func rollBackToSavepoint(on driver: DatabaseDriver, connectionId: UUID) async { + private static func rollBack(to savepoint: Savepoint, on driver: DatabaseDriver, connectionId: UUID) async { let token = DatabaseManager.shared.beginProtectedWrite(on: driver, for: connectionId) defer { DatabaseManager.shared.endProtectedWrite(token, for: connectionId) } do { try await TaskCancellationShield.run { @Sendable in - _ = try await driver.execute(query: Self.rollbackStatement) + _ = try await driver.execute(query: savepoint.rollback) } } catch { logger.error("Rolling back to the agent savepoint failed: \(error.publicLogShape, privacy: .public)") diff --git a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift index 52ddc993ce..39315ccd3c 100644 --- a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift +++ b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift @@ -574,7 +574,9 @@ internal actor DatabaseAccessBridge { ) { driver -> Int in reach.mark() // The hold and savepoint execute_query gets, so a Manual Commit insert waits for Commit too. - let ran = try await agentGuard.running(statements.map(\.sql), writes: true, on: driver) { _ in + let ran = try await agentGuard.running( + statements.map(\.sql), writes: true, asOneUnit: statements.count > 1, on: driver + ) { _ in var affected = 0 for statement in statements { let result = try await driver.executeParameterized( diff --git a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift index ce20b4c40c..db0f8cd57e 100644 --- a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift +++ b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift @@ -402,7 +402,7 @@ struct OptionsPaneView: View { } if coordinator.isManualCommitAvailable { // swiftlint:disable:next line_length - Text(String(localized: "Your first change opens a transaction, and nothing is saved until you commit. Structure changes, Users & Roles, Copy To and Generate Rows still save at once.")) + Text(String(localized: "Your first change opens a transaction, and nothing is saved until you commit. Structure changes, Users & Roles, Copy To, Generate Rows and Shortcuts actions still save at once.")) } else { Text(ManualCommit.unavailableExplanation) } diff --git a/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift b/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift index 4edbfeb9c8..784e91df77 100644 --- a/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift +++ b/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift @@ -443,6 +443,44 @@ struct SessionTransactionTrackerTests { #expect(Self.states(connection.id) == [.open]) } + @Test("An insert_rows of several rows joining an open SQLite transaction is taken back whole when a row fails") + func mcpInsertRowsJoiningATransactionIsOneUnit() async throws { + let (connection, driver) = Self.seed(type: .sqlite, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let rows = ["INSERT INTO t VALUES (1)", "INSERT INTO t VALUES (2)"] + driver.failingStatement = rows[1] + let operation = try await ApprovedOperationFixture.approve( + rows.map { OperationStatement(sql: $0) }, scope: Self.scope(connection.id), databaseType: .sqlite, + kind: .writeQuery + ) + + await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runInserts(operation) + } + + #expect(driver.statements.suffix(4) == [ + AgentTransactionGuard.savepointStatement, rows[0], rows[1], AgentTransactionGuard.rollbackStatement, + ]) + #expect(!driver.statements.contains("COMMIT")) + } + + @Test("An insert_rows joining an open SQL Server transaction takes SAVE TRANSACTION, which has no release") + func mcpInsertRowsOnSQLServerTakesItsSavepoint() async throws { + let (connection, driver) = Self.seed(type: .mssql) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let rows = ["INSERT INTO t VALUES (1)", "INSERT INTO t VALUES (2)"] + let operation = try await ApprovedOperationFixture.approve( + rows.map { OperationStatement(sql: $0) }, scope: Self.scope(connection.id), databaseType: .mssql, + kind: .writeQuery + ) + + _ = try await DatabaseAccessBridge().runInserts(operation) + + #expect(driver.statements.suffix(3) == ["SAVE TRANSACTION tablepro_agent", rows[0], rows[1]]) + } + @Test("An agent's COMMIT, ROLLBACK or BEGIN in Manual Commit is refused and sends nothing", arguments: ["COMMIT", "ROLLBACK", "BEGIN"]) func mcpTransactionStatementRefusedInManualCommit(sql: String) async throws { let (connection, driver) = Self.seed(type: .postgresql, commitMode: .manual) diff --git a/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift b/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift index ab83f3e138..d3ff9b353b 100644 --- a/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift +++ b/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift @@ -152,6 +152,25 @@ struct ManualCommitTests { #expect(AgentTransactionGuard.protection(family: .postgres, sessionState: .inTransaction, controlsTransaction: true) == .none) } + @Test("A call of several writes joining an open transaction takes the engine's savepoint, and nothing else does") + func oneUnitSavepointFollowsTheFamily() { + func savepoint(_ family: TransactionEngineFamily, _ state: PluginSessionTransactionState, unit: Bool) + -> AgentTransactionGuard.Savepoint? { + let protection = AgentTransactionGuard.protection(family: family, sessionState: state, controlsTransaction: false) + return AgentTransactionGuard.savepoint(family: family, protection: protection, sessionState: state, asOneUnit: unit) + } + #expect(savepoint(.sqlite, .inTransaction, unit: true) == .standard) + #expect(savepoint(.mysql, .inTransaction, unit: true) == .standard) + #expect(savepoint(.sqlServer, .inTransaction, unit: true)?.open == "SAVE TRANSACTION tablepro_agent") + #expect(savepoint(.sqlServer, .inTransaction, unit: true)?.release == nil) + #expect(savepoint(.oracle, .inTransaction, unit: true)?.release == nil) + #expect(savepoint(.sqlite, .inTransaction, unit: false) == nil) + #expect(savepoint(.sqlite, .idle, unit: true) == nil) + #expect(savepoint(.other, .inTransaction, unit: true) == nil) + #expect(savepoint(.postgres, .inTransaction, unit: false) == .standard) + #expect(savepoint(.duckdb, .inTransaction, unit: true) == nil) + } + @Test("Unseen transaction control: a T-SQL COMMIT inside a batch, an IF, a procedure call") func unseenTransactionControlIsRecognised() { let mssql = DatabaseType.mssql From 502c7979be6f0d7236fb9e5e3932b50c230e1040 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 02:43:14 +0700 Subject: [PATCH 11/14] chore(macos): drop the catalog additions before extracting them again --- .../TablePro/Resources/Localizable.xcstrings | 156 ------------------ 1 file changed, 156 deletions(-) diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 51af3cc9ce..559ced0f1b 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -1072,9 +1072,6 @@ } } } - }, - "“%@” is used more than once." : { - }, "“%@” isn't a port or a range of ports, such as 4000-4999." : { "extractionState" : "manual", @@ -17585,9 +17582,6 @@ } } } - }, - "A transaction is already open on this connection, or it cannot say whether one is. Commit or roll it back first." : { - }, "A transaction is already open. Commit or roll it back first." : { "extractionState" : "manual", @@ -17600,15 +17594,9 @@ } } } - }, - "A Transaction Is Open" : { - }, "A transaction is open on %@. Switching databases reconnects, and the server would roll it back. Commit or roll back the transaction, then switch again." : { - }, - "A transaction is open on this connection, and a failed statement would abort it. Commit or roll back in TablePro first." : { - }, "A transaction is open on this connection, and its uncommitted rows aren't in these counts." : { @@ -19744,9 +19732,6 @@ }, "Add column “%@”" : { - }, - "Add Column Pair" : { - }, "Add Column…" : { "localizations" : { @@ -29726,33 +29711,6 @@ } } } - }, - "Auto Refresh" : { - - }, - "Auto Refresh waits until the connection is back." : { - - }, - "Auto Refresh waits while a cell is being edited." : { - - }, - "Auto Refresh waits while a transaction is open." : { - - }, - "Auto Refresh waits while a value is open in its own window." : { - - }, - "Auto Refresh waits while rows are selected in a table without a primary key." : { - - }, - "Auto Refresh waits while rows load." : { - - }, - "Auto Refresh waits while the table has unsaved changes." : { - - }, - "Auto Refresh: %@" : { - }, "AUTO_INCREMENT columns whose next value has reached 75% of the largest value their type holds." : { @@ -31589,12 +31547,6 @@ }, "Before" : { - }, - "Before Replace" : { - - }, - "Before Restore" : { - }, "BEFORE, AFTER, or INSTEAD OF" : { "localizations" : { @@ -31635,9 +31587,6 @@ } } } - }, - "Begin, commit, or roll back a transaction on the connection's shared session. The transaction stays open across calls until committed or rolled back, and it is the same session the user's own tabs run on, so leave nothing open. Begin is refused while a transaction is already open, and every action is refused on a connection with Manual Commit on, where the user commits in TablePro. Needs tools:write." : { - }, "Begin, commit, or roll back a transaction on the connection's shared session. The transaction stays open across calls until committed or rolled back, and it is the same session the user's own tabs run on, so leave nothing open. Needs tools:write." : { "localizations" : { @@ -32570,9 +32519,6 @@ } } } - }, - "Browse All Versions…" : { - }, "Browse needs a TableName" : { "extractionState" : "manual", @@ -38052,9 +37998,6 @@ }, "Choose a character set first" : { - }, - "Choose a column and a referenced column in every row." : { - }, "Choose a connection" : { "localizations" : { @@ -44159,9 +44102,6 @@ } } } - }, - "Column → Referenced Column" : { - }, "Column comment" : { "extractionState" : "manual", @@ -45632,9 +45572,6 @@ }, "Commit the transaction" : { - }, - "Commit Transaction" : { - }, "Commit…" : { @@ -74923,12 +74860,6 @@ }, "Every 5 Minutes" : { - }, - "Every 5 Seconds" : { - - }, - "Every 10 Seconds" : { - }, "Every 15 minutes" : { "localizations" : { @@ -74952,9 +74883,6 @@ } } } - }, - "Every 30 Seconds" : { - }, "Every call that reaches a connection asks first." : { "localizations" : { @@ -95978,9 +95906,6 @@ } } } - }, - "Insert rows into a table using bound parameters, so values are never spliced into SQL text. Needs tools:write and goes through the connection's Safe Mode approval. With Manual Commit on, the rows stay pending until the user commits in TablePro." : { - }, "Insert rows the target is missing" : { "localizations" : { @@ -99515,9 +99440,6 @@ } } } - }, - "Keeps up to %1$d versions from the last %2$d days on this Mac." : { - }, "Keeps what rows looked like before each save, on this Mac only, for 7 days. It is encrypted, and never synced." : { "localizations" : { @@ -107867,15 +107789,6 @@ } } } - }, - "Manual Commit" : { - - }, - "Manual Commit is on for this connection, so this change is pending until the user commits it in TablePro." : { - - }, - "Manual Commit is on for this connection. Commit or roll back in TablePro." : { - }, "Many Databases, One App" : { "localizations" : { @@ -142708,9 +142621,6 @@ }, "Quit while work is still running?" : { - }, - "Quit with an Open Transaction?" : { - }, "Quit Without Saving" : { @@ -146062,9 +145972,6 @@ } } } - }, - "Referenced Column" : { - }, "Referenced row not found" : { "localizations" : { @@ -146802,9 +146709,6 @@ }, "Relaunch while work is still running?" : { - }, - "Relaunch with an Open Transaction?" : { - }, "Release" : { "localizations" : { @@ -147756,9 +147660,6 @@ } } } - }, - "Remove Column Pair" : { - }, "Remove Comment" : { "localizations" : { @@ -152916,9 +152817,6 @@ }, "Revert Changes" : { - }, - "Revert To" : { - }, "review" : { "extractionState" : "stale", @@ -153581,9 +153479,6 @@ }, "Roll Back the Transaction?" : { - }, - "Roll Back Transaction" : { - }, "Roll it back to continue." : { @@ -155999,9 +155894,6 @@ }, "Run one statement, or on SQL Server a whole script. Reads need tools:read; anything that writes needs tools:write and is subject to the connection's Safe Mode, which asks the user to approve it. Destructive statements, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE, go through confirm_destructive_operation instead. Statements that read or write files, or run server-side code, are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at its GO lines and result_sets lists every result set when there is more than one." : { - }, - "Run one statement, or on SQL Server a whole script. Reads need tools:read; anything that writes needs tools:write and is subject to the connection's Safe Mode, which asks the user to approve it. Destructive statements, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE, go through confirm_destructive_operation instead. Statements that read or write files, or run server-side code, are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at its GO lines and result_sets lists every result set when there is more than one. A statement that starts a transaction while one is open is refused. With Manual Commit on, a write stays pending until the user commits it in TablePro, and statements that begin, commit or roll back a transaction are refused. While a transaction is open on PostgreSQL or CockroachDB a statement runs inside a savepoint, so its failure leaves the transaction usable; on Redshift and DuckDB it is refused." : { - }, "Run one statement, or on SQL Server a whole script. Reads need tools:read; anything that writes needs tools:write and is subject to the connection's Safe Mode, which asks the user to approve it. DROP and TRUNCATE go through confirm_destructive_operation instead. Statements that read or write files, or run server-side code, are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at its GO lines and result_sets lists every result set when there is more than one." : { "localizations" : { @@ -156438,9 +156330,6 @@ }, "Run what execute_query refuses as destructive, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE: one statement, or on SQL Server a whole script. Anything else is refused. The user approves it before it runs: through your elicitation prompt when your client supports elicitation, otherwise through TablePro's own confirmation dialog on the user's Mac. Needs tools:write and a connection the user left writable for external clients." : { - }, - "Run what execute_query refuses as destructive, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE: one statement, or on SQL Server a whole script. Anything else is refused. The user approves it before it runs: through your elicitation prompt when your client supports elicitation, otherwise through TablePro's own confirmation dialog on the user's Mac. Needs tools:write and a connection the user left writable for external clients. The transaction rules of execute_query apply, Manual Commit's among them." : { - }, "Run Without Limit" : { "localizations" : { @@ -186580,9 +186469,6 @@ } } } - }, - "The change did not run." : { - }, "The change to this table's structure will be lost." : { @@ -190787,9 +190673,6 @@ }, "The open transaction failed. Roll it back yourself." : { - }, - "The open transaction on this connection ended while this statement ran." : { - }, "The operation failed." : { "localizations" : { @@ -197413,12 +197296,6 @@ } } } - }, - "This change runs on its own connection and can wait until you commit or roll back the open transaction." : { - - }, - "This change sets session options around its statements, so it can't wait for Commit. Turn off Manual Commit in the Database menu to run it." : { - }, "This change was not authorized" : { "localizations" : { @@ -197616,9 +197493,6 @@ } } } - }, - "This connection can't hold this change until you commit, so it did not run. Turn off Manual Commit in the Database menu to run it." : { - }, "This connection can't open the separate session live messages need." : { @@ -198383,9 +198257,6 @@ } } } - }, - "This database can't report an open transaction, so it commits each change." : { - }, "This database cannot be renamed" : { "extractionState" : "manual", @@ -198587,9 +198458,6 @@ }, "This database cannot store a table comment." : { - }, - "This database commits schema and maintenance statements on its own, so changes can't wait for Commit." : { - }, "This database did not return a query plan for the statement." : { "comment" : "Text displayed in a description when the database did not return a query plan for the statement.", @@ -200200,12 +200068,6 @@ } } } - }, - "This import runs each statement on its own, so nothing can wait for Commit. Turn on Wrap in transaction or turn off Manual Commit." : { - - }, - "This import turns off foreign key checks, which can't wait for Commit. Turn off Disable foreign key checks or Manual Commit to run it." : { - }, "This index needs a name." : { "localizations" : { @@ -202054,9 +201916,6 @@ } } } - }, - "This run mixes changes with a statement that can't run in a transaction. Run that statement on its own, or turn off Manual Commit." : { - }, "This save also edits key columns, and the counts don't include those edits." : { @@ -202655,9 +202514,6 @@ }, "This Tab" : { - }, - "This tab is closed. Reopen it to see its history." : { - }, "This tab is on %@. Switch the connection to that database to run it." : { "localizations" : { @@ -203400,9 +203256,6 @@ } } } - }, - "This will permanently delete all query history entries, the saved changes kept for restoring, and the earlier versions of your query tabs. This action cannot be undone." : { - }, "This will reset all settings across every section to their default values." : { "localizations" : { @@ -207913,12 +207766,6 @@ } } } - }, - "Uncommitted changes on “%@” are rolled back when TablePro quits." : { - - }, - "Uncommitted changes on %@ are rolled back when TablePro quits." : { - }, "Under MCP Servers click \"+ Add Custom Server\", select the Local tab, paste the JSON below, then click Add Server" : { "localizations" : { @@ -219308,9 +219155,6 @@ } } } - }, - "Your first change opens a transaction, and nothing is saved until you commit. Structure changes, Users & Roles, Copy To and Generate Rows still save at once." : { - }, "Your license has expired" : { "localizations" : { From 1fcd698721cb9c05b8680e0141fe2c4b48ec558c Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 03:46:03 +0700 Subject: [PATCH 12/14] chore(macos): add the new strings to the catalog --- .../TablePro/Resources/Localizable.xcstrings | 156 ++++++++++++++++++ 1 file changed, 156 insertions(+) diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 559ced0f1b..6a3a11adff 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -1072,6 +1072,9 @@ } } } + }, + "“%@” is used more than once." : { + }, "“%@” isn't a port or a range of ports, such as 4000-4999." : { "extractionState" : "manual", @@ -17582,6 +17585,9 @@ } } } + }, + "A transaction is already open on this connection, or it cannot say whether one is. Commit or roll it back first." : { + }, "A transaction is already open. Commit or roll it back first." : { "extractionState" : "manual", @@ -17594,9 +17600,15 @@ } } } + }, + "A Transaction Is Open" : { + }, "A transaction is open on %@. Switching databases reconnects, and the server would roll it back. Commit or roll back the transaction, then switch again." : { + }, + "A transaction is open on this connection, and a failed statement would abort it. Commit or roll back in TablePro first." : { + }, "A transaction is open on this connection, and its uncommitted rows aren't in these counts." : { @@ -19732,6 +19744,9 @@ }, "Add column “%@”" : { + }, + "Add Column Pair" : { + }, "Add Column…" : { "localizations" : { @@ -29711,6 +29726,33 @@ } } } + }, + "Auto Refresh" : { + + }, + "Auto Refresh waits until the connection is back." : { + + }, + "Auto Refresh waits while a cell is being edited." : { + + }, + "Auto Refresh waits while a transaction is open." : { + + }, + "Auto Refresh waits while a value is open in its own window." : { + + }, + "Auto Refresh waits while rows are selected in a table without a primary key." : { + + }, + "Auto Refresh waits while rows load." : { + + }, + "Auto Refresh waits while the table has unsaved changes." : { + + }, + "Auto Refresh: %@" : { + }, "AUTO_INCREMENT columns whose next value has reached 75% of the largest value their type holds." : { @@ -31547,6 +31589,12 @@ }, "Before" : { + }, + "Before Replace" : { + + }, + "Before Restore" : { + }, "BEFORE, AFTER, or INSTEAD OF" : { "localizations" : { @@ -31587,6 +31635,9 @@ } } } + }, + "Begin, commit, or roll back a transaction on the connection's shared session. The transaction stays open across calls until committed or rolled back, and it is the same session the user's own tabs run on, so leave nothing open. Begin is refused while a transaction is already open, and every action is refused on a connection with Manual Commit on, where the user commits in TablePro. Needs tools:write." : { + }, "Begin, commit, or roll back a transaction on the connection's shared session. The transaction stays open across calls until committed or rolled back, and it is the same session the user's own tabs run on, so leave nothing open. Needs tools:write." : { "localizations" : { @@ -32519,6 +32570,9 @@ } } } + }, + "Browse All Versions…" : { + }, "Browse needs a TableName" : { "extractionState" : "manual", @@ -37998,6 +38052,9 @@ }, "Choose a character set first" : { + }, + "Choose a column and a referenced column in every row." : { + }, "Choose a connection" : { "localizations" : { @@ -44102,6 +44159,9 @@ } } } + }, + "Column → Referenced Column" : { + }, "Column comment" : { "extractionState" : "manual", @@ -45572,6 +45632,9 @@ }, "Commit the transaction" : { + }, + "Commit Transaction" : { + }, "Commit…" : { @@ -74860,6 +74923,12 @@ }, "Every 5 Minutes" : { + }, + "Every 5 Seconds" : { + + }, + "Every 10 Seconds" : { + }, "Every 15 minutes" : { "localizations" : { @@ -74883,6 +74952,9 @@ } } } + }, + "Every 30 Seconds" : { + }, "Every call that reaches a connection asks first." : { "localizations" : { @@ -95906,6 +95978,9 @@ } } } + }, + "Insert rows into a table using bound parameters, so values are never spliced into SQL text. Needs tools:write and goes through the connection's Safe Mode approval. With Manual Commit on, the rows stay pending until the user commits in TablePro." : { + }, "Insert rows the target is missing" : { "localizations" : { @@ -99440,6 +99515,9 @@ } } } + }, + "Keeps up to %1$d versions from the last %2$d days on this Mac." : { + }, "Keeps what rows looked like before each save, on this Mac only, for 7 days. It is encrypted, and never synced." : { "localizations" : { @@ -107789,6 +107867,15 @@ } } } + }, + "Manual Commit" : { + + }, + "Manual Commit is on for this connection, so this change is pending until the user commits it in TablePro." : { + + }, + "Manual Commit is on for this connection. Commit or roll back in TablePro." : { + }, "Many Databases, One App" : { "localizations" : { @@ -142621,6 +142708,9 @@ }, "Quit while work is still running?" : { + }, + "Quit with an Open Transaction?" : { + }, "Quit Without Saving" : { @@ -145972,6 +146062,9 @@ } } } + }, + "Referenced Column" : { + }, "Referenced row not found" : { "localizations" : { @@ -146709,6 +146802,9 @@ }, "Relaunch while work is still running?" : { + }, + "Relaunch with an Open Transaction?" : { + }, "Release" : { "localizations" : { @@ -147660,6 +147756,9 @@ } } } + }, + "Remove Column Pair" : { + }, "Remove Comment" : { "localizations" : { @@ -152817,6 +152916,9 @@ }, "Revert Changes" : { + }, + "Revert To" : { + }, "review" : { "extractionState" : "stale", @@ -153479,6 +153581,9 @@ }, "Roll Back the Transaction?" : { + }, + "Roll Back Transaction" : { + }, "Roll it back to continue." : { @@ -155894,6 +155999,9 @@ }, "Run one statement, or on SQL Server a whole script. Reads need tools:read; anything that writes needs tools:write and is subject to the connection's Safe Mode, which asks the user to approve it. Destructive statements, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE, go through confirm_destructive_operation instead. Statements that read or write files, or run server-side code, are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at its GO lines and result_sets lists every result set when there is more than one." : { + }, + "Run one statement, or on SQL Server a whole script. Reads need tools:read; anything that writes needs tools:write and is subject to the connection's Safe Mode, which asks the user to approve it. Destructive statements, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE, go through confirm_destructive_operation instead. Statements that read or write files, or run server-side code, are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at its GO lines and result_sets lists every result set when there is more than one. A statement that starts a transaction while one is open is refused. With Manual Commit on, a write stays pending until the user commits it in TablePro, and statements that begin, commit or roll back a transaction are refused. While a transaction is open on PostgreSQL or CockroachDB a statement runs inside a savepoint, so its failure leaves the transaction usable; on Redshift and DuckDB it is refused." : { + }, "Run one statement, or on SQL Server a whole script. Reads need tools:read; anything that writes needs tools:write and is subject to the connection's Safe Mode, which asks the user to approve it. DROP and TRUNCATE go through confirm_destructive_operation instead. Statements that read or write files, or run server-side code, are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at its GO lines and result_sets lists every result set when there is more than one." : { "localizations" : { @@ -156330,6 +156438,9 @@ }, "Run what execute_query refuses as destructive, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE: one statement, or on SQL Server a whole script. Anything else is refused. The user approves it before it runs: through your elicitation prompt when your client supports elicitation, otherwise through TablePro's own confirmation dialog on the user's Mac. Needs tools:write and a connection the user left writable for external clients." : { + }, + "Run what execute_query refuses as destructive, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE: one statement, or on SQL Server a whole script. Anything else is refused. The user approves it before it runs: through your elicitation prompt when your client supports elicitation, otherwise through TablePro's own confirmation dialog on the user's Mac. Needs tools:write and a connection the user left writable for external clients. The transaction rules of execute_query apply, Manual Commit's among them." : { + }, "Run Without Limit" : { "localizations" : { @@ -186469,6 +186580,9 @@ } } } + }, + "The change did not run." : { + }, "The change to this table's structure will be lost." : { @@ -190673,6 +190787,9 @@ }, "The open transaction failed. Roll it back yourself." : { + }, + "The open transaction on this connection ended while this statement ran." : { + }, "The operation failed." : { "localizations" : { @@ -197296,6 +197413,12 @@ } } } + }, + "This change runs on its own connection and can wait until you commit or roll back the open transaction." : { + + }, + "This change sets session options around its statements, so it can't wait for Commit. Turn off Manual Commit in the Database menu to run it." : { + }, "This change was not authorized" : { "localizations" : { @@ -197493,6 +197616,9 @@ } } } + }, + "This connection can't hold this change until you commit, so it did not run. Turn off Manual Commit in the Database menu to run it." : { + }, "This connection can't open the separate session live messages need." : { @@ -198257,6 +198383,9 @@ } } } + }, + "This database can't report an open transaction, so it commits each change." : { + }, "This database cannot be renamed" : { "extractionState" : "manual", @@ -198458,6 +198587,9 @@ }, "This database cannot store a table comment." : { + }, + "This database commits schema and maintenance statements on its own, so changes can't wait for Commit." : { + }, "This database did not return a query plan for the statement." : { "comment" : "Text displayed in a description when the database did not return a query plan for the statement.", @@ -200068,6 +200200,12 @@ } } } + }, + "This import runs each statement on its own, so nothing can wait for Commit. Turn on Wrap in transaction or turn off Manual Commit." : { + + }, + "This import turns off foreign key checks, which can't wait for Commit. Turn off Disable foreign key checks or Manual Commit to run it." : { + }, "This index needs a name." : { "localizations" : { @@ -201916,6 +202054,9 @@ } } } + }, + "This run mixes changes with a statement that can't run in a transaction. Run that statement on its own, or turn off Manual Commit." : { + }, "This save also edits key columns, and the counts don't include those edits." : { @@ -202514,6 +202655,9 @@ }, "This Tab" : { + }, + "This tab is closed. Reopen it to see its history." : { + }, "This tab is on %@. Switch the connection to that database to run it." : { "localizations" : { @@ -203256,6 +203400,9 @@ } } } + }, + "This will permanently delete all query history entries, the saved changes kept for restoring, and the earlier versions of your query tabs. This action cannot be undone." : { + }, "This will reset all settings across every section to their default values." : { "localizations" : { @@ -207766,6 +207913,12 @@ } } } + }, + "Uncommitted changes on “%@” are rolled back when TablePro quits." : { + + }, + "Uncommitted changes on %@ are rolled back when TablePro quits." : { + }, "Under MCP Servers click \"+ Add Custom Server\", select the Local tab, paste the JSON below, then click Add Server" : { "localizations" : { @@ -219155,6 +219308,9 @@ } } } + }, + "Your first change opens a transaction, and nothing is saved until you commit. Structure changes, Users & Roles, Copy To, Generate Rows and Shortcuts actions still save at once." : { + }, "Your license has expired" : { "localizations" : { From 1b722cd1c5fc6f9095b11ce323be4f854d9acc56 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 04:47:21 +0700 Subject: [PATCH 13/14] docs(macos): add the changelog fragments for #4214 --- apple/changelog.d/4214.added.1.md | 1 + apple/changelog.d/4214.added.2.md | 1 + apple/changelog.d/4214.added.3.md | 1 + apple/changelog.d/4214.added.4.md | 1 + apple/changelog.d/4214.added.md | 1 + apple/changelog.d/4214.fixed.1.md | 1 + apple/changelog.d/4214.fixed.10.md | 1 + apple/changelog.d/4214.fixed.2.md | 1 + apple/changelog.d/4214.fixed.3.md | 1 + apple/changelog.d/4214.fixed.4.md | 1 + apple/changelog.d/4214.fixed.5.md | 1 + apple/changelog.d/4214.fixed.6.md | 1 + apple/changelog.d/4214.fixed.7.md | 1 + apple/changelog.d/4214.fixed.8.md | 1 + apple/changelog.d/4214.fixed.9.md | 1 + apple/changelog.d/4214.fixed.md | 1 + 16 files changed, 16 insertions(+) create mode 100644 apple/changelog.d/4214.added.1.md create mode 100644 apple/changelog.d/4214.added.2.md create mode 100644 apple/changelog.d/4214.added.3.md create mode 100644 apple/changelog.d/4214.added.4.md create mode 100644 apple/changelog.d/4214.added.md create mode 100644 apple/changelog.d/4214.fixed.1.md create mode 100644 apple/changelog.d/4214.fixed.10.md create mode 100644 apple/changelog.d/4214.fixed.2.md create mode 100644 apple/changelog.d/4214.fixed.3.md create mode 100644 apple/changelog.d/4214.fixed.4.md create mode 100644 apple/changelog.d/4214.fixed.5.md create mode 100644 apple/changelog.d/4214.fixed.6.md create mode 100644 apple/changelog.d/4214.fixed.7.md create mode 100644 apple/changelog.d/4214.fixed.8.md create mode 100644 apple/changelog.d/4214.fixed.9.md create mode 100644 apple/changelog.d/4214.fixed.md diff --git a/apple/changelog.d/4214.added.1.md b/apple/changelog.d/4214.added.1.md new file mode 100644 index 0000000000..626ed8ec13 --- /dev/null +++ b/apple/changelog.d/4214.added.1.md @@ -0,0 +1 @@ +A table tab can refresh itself on an interval, set from its status bar or Database > Auto Refresh. diff --git a/apple/changelog.d/4214.added.2.md b/apple/changelog.d/4214.added.2.md new file mode 100644 index 0000000000..83152ab3c9 --- /dev/null +++ b/apple/changelog.d/4214.added.2.md @@ -0,0 +1 @@ +Query tabs keep earlier versions of their text, listed under File > Revert To. diff --git a/apple/changelog.d/4214.added.3.md b/apple/changelog.d/4214.added.3.md new file mode 100644 index 0000000000..e9ea1e5804 --- /dev/null +++ b/apple/changelog.d/4214.added.3.md @@ -0,0 +1 @@ +The sidebar lists MySQL and MariaDB events and PostgreSQL extensions, and opens their DDL. diff --git a/apple/changelog.d/4214.added.4.md b/apple/changelog.d/4214.added.4.md new file mode 100644 index 0000000000..9d91cb3400 --- /dev/null +++ b/apple/changelog.d/4214.added.4.md @@ -0,0 +1 @@ +The ER diagram's Add Foreign Key sheet builds keys over several columns. diff --git a/apple/changelog.d/4214.added.md b/apple/changelog.d/4214.added.md new file mode 100644 index 0000000000..3b8c703b40 --- /dev/null +++ b/apple/changelog.d/4214.added.md @@ -0,0 +1 @@ +Manual Commit keeps a connection's changes in a transaction until you commit or roll back. diff --git a/apple/changelog.d/4214.fixed.1.md b/apple/changelog.d/4214.fixed.1.md new file mode 100644 index 0000000000..3e96339e89 --- /dev/null +++ b/apple/changelog.d/4214.fixed.1.md @@ -0,0 +1 @@ +Closing, disconnecting or quitting did not mention an open transaction it was about to roll back. diff --git a/apple/changelog.d/4214.fixed.10.md b/apple/changelog.d/4214.fixed.10.md new file mode 100644 index 0000000000..67cc9c96bb --- /dev/null +++ b/apple/changelog.d/4214.fixed.10.md @@ -0,0 +1 @@ +An MCP insert_rows call that failed partway inside an open transaction left its earlier rows pending. diff --git a/apple/changelog.d/4214.fixed.2.md b/apple/changelog.d/4214.fixed.2.md new file mode 100644 index 0000000000..2015bc0a7e --- /dev/null +++ b/apple/changelog.d/4214.fixed.2.md @@ -0,0 +1 @@ +Commit and Roll Back in the transaction bar refused after any background read on the connection. diff --git a/apple/changelog.d/4214.fixed.3.md b/apple/changelog.d/4214.fixed.3.md new file mode 100644 index 0000000000..443c047748 --- /dev/null +++ b/apple/changelog.d/4214.fixed.3.md @@ -0,0 +1 @@ +The transaction bar's Commit button did not follow a Safe Mode change until the next statement. diff --git a/apple/changelog.d/4214.fixed.4.md b/apple/changelog.d/4214.fixed.4.md new file mode 100644 index 0000000000..349037c127 --- /dev/null +++ b/apple/changelog.d/4214.fixed.4.md @@ -0,0 +1 @@ +An MCP client could start a transaction over an open one, which on MySQL commits the open one. diff --git a/apple/changelog.d/4214.fixed.5.md b/apple/changelog.d/4214.fixed.5.md new file mode 100644 index 0000000000..873610b77c --- /dev/null +++ b/apple/changelog.d/4214.fixed.5.md @@ -0,0 +1 @@ +A refresh turned a selected cell or cell range into whole selected rows. diff --git a/apple/changelog.d/4214.fixed.6.md b/apple/changelog.d/4214.fixed.6.md new file mode 100644 index 0000000000..520d92aff3 --- /dev/null +++ b/apple/changelog.d/4214.fixed.6.md @@ -0,0 +1 @@ +Clear Query, loading a query, AI apply and inserting a saved query could not be undone. diff --git a/apple/changelog.d/4214.fixed.7.md b/apple/changelog.d/4214.fixed.7.md new file mode 100644 index 0000000000..b49cce4f28 --- /dev/null +++ b/apple/changelog.d/4214.fixed.7.md @@ -0,0 +1 @@ +Typing in a second window of a connection waited up to 30 seconds to be saved. diff --git a/apple/changelog.d/4214.fixed.8.md b/apple/changelog.d/4214.fixed.8.md new file mode 100644 index 0000000000..6b69890e33 --- /dev/null +++ b/apple/changelog.d/4214.fixed.8.md @@ -0,0 +1 @@ +The Structure tab accepted a foreign key whose column and referenced column counts differ. diff --git a/apple/changelog.d/4214.fixed.9.md b/apple/changelog.d/4214.fixed.9.md new file mode 100644 index 0000000000..32f12e10e1 --- /dev/null +++ b/apple/changelog.d/4214.fixed.9.md @@ -0,0 +1 @@ +The Server Dashboard's refresh interval was not shown in your language. diff --git a/apple/changelog.d/4214.fixed.md b/apple/changelog.d/4214.fixed.md new file mode 100644 index 0000000000..0019928d55 --- /dev/null +++ b/apple/changelog.d/4214.fixed.md @@ -0,0 +1 @@ +On DuckDB and PGlite, saving a structure, a new table or a trigger committed a transaction left open. From 98e12f4e71220fa819a81a597350febaeba15352 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 06:27:35 +0700 Subject: [PATCH 14/14] test(macos): turn Auto Refresh on from the Database menu in its UI flow --- apple/TableProUITests/TableAutoRefreshUITests.swift | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/apple/TableProUITests/TableAutoRefreshUITests.swift b/apple/TableProUITests/TableAutoRefreshUITests.swift index 53bbf96412..fa6980e740 100644 --- a/apple/TableProUITests/TableAutoRefreshUITests.swift +++ b/apple/TableProUITests/TableAutoRefreshUITests.swift @@ -3,14 +3,14 @@ import XCTest final class TableAutoRefreshUITests: UITestCase { func testAutoRefreshShowsAWriteMadeElsewhereWithoutARefresh() throws { let (launch, grid) = try launchWithFixtureTable("document") - let window = launch.window + let app = launch.app XCTAssertTrue(grid.waitForValue("First", row: 1, column: 2), "document must open on its first row") - let control = window.descendants(matching: .any).matching(identifier: "result-status-auto-refresh").firstMatch - XCTAssertTrue(waitUntilHittable(control, timeout: 20), "A table tab's status bar must offer Auto Refresh") - control.click() - let everyFiveSeconds = control.menuItems["Every 5 Seconds"].firstMatch - XCTAssertTrue(everyFiveSeconds.waitToExist(timeout: 10), "Auto Refresh must list Every 5 Seconds") + // The 1024x768 runner gives the status bar its narrowest tier, which leaves Auto Refresh to this menu. + let autoRefresh = menuItem("Auto Refresh", under: "Database", in: app) + autoRefresh.click() + let everyFiveSeconds = autoRefresh.menuItems["Every 5 Seconds"].firstMatch + XCTAssertTrue(everyFiveSeconds.waitToExist(timeout: 10), "Database > Auto Refresh must list Every 5 Seconds") everyFiveSeconds.click() XCTAssertTrue(