From 49717d55a0a7aabfdb870252add92bc583ce6d4c Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 09:55:54 +0700 Subject: [PATCH 1/3] fix(data-grid): keep edits through saves, undo and paste, hold keyed writes and restores to their rows --- .../ChangeTracking/DataChangeManager.swift | 57 +++++- .../SQLStatementGenerator.swift | 3 +- .../ChangeTracking/SQLiteRowidAlias.swift | 20 ++ .../RowEditingCoordinator+SaveChanges.swift | 4 +- .../Coordinators/RowEditingCoordinator.swift | 52 ++++-- .../CrossEngine/CrossEngineDefaultValue.swift | 12 ++ .../Core/DataWrite/DataWriteExecutor.swift | 44 ++++- .../Core/DataWrite/DataWritePlan.swift | 14 +- .../Core/DataWrite/DataWriteRowCounts.swift | 12 ++ .../DataWrite/KeyedWriteVerification.swift | 6 +- .../Core/DataWrite/Rewind/RewindPlanner.swift | 45 ++++- .../RowGenerationColumnReader.swift | 6 +- .../Core/SchemaTracking/StructureRebase.swift | 30 ++- .../Services/Formatting/JsonReindenter.swift | 9 + .../Services/Query/RowOperationsManager.swift | 125 ++++++++++--- .../Core/Services/Query/RowParser.swift | 16 +- .../Models/Database/TableSchema.swift | 3 - .../Models/UI/JsonFieldEditingModel.swift | 6 +- .../Models/UI/MultiRowEditState.swift | 5 +- .../TablePro/Resources/Localizable.xcstrings | 9 + .../Views/Main/Child/DataTabGridAdapter.swift | 4 +- ...MainContentCoordinator+ResultEditing.swift | 9 +- ...MainContentCoordinator+RowOperations.swift | 4 + .../Extensions/MainContentView+Bindings.swift | 5 +- .../MainContentCommandActions+Structure.swift | 2 +- .../MainContentCommandActions+UndoState.swift | 4 + .../Views/Results/JSONEditorContentView.swift | 4 +- .../Views/Results/JSONViewerView.swift | 3 +- .../Results/JSONViewerWindowController.swift | 3 +- .../ChangeTracking/RowEditUndoStepTests.swift | 115 ++++++++++++ ...tementGeneratorBatchDeleteScaleTests.swift | 19 ++ .../SQLStatementGeneratorImportTests.swift | 3 + .../DataWriteExecutorLookupTests.swift | 34 ++++ .../DataWrite/DataWriteExecutorTests.swift | 66 ++++++- .../Core/DataWrite/RewindPlannerTests.swift | 24 +++ .../StructureChangeRebaseTests.swift | 133 +++++++++++++ .../RowOperationsFieldAbsenceTests.swift | 2 +- .../RowOperationsManagerPasteTests.swift | 176 +++++++++++++++++- .../Services/RowPasteForeignTextTests.swift | 17 +- .../UI/MultiRowEditStateJsonTests.swift | 11 ++ .../Views/Main/SaveAttributionTests.swift | 2 +- .../Main/SaveInFlightEditGateTests.swift | 75 ++++++++ .../Results/JSONViewerWindowCommitTests.swift | 12 +- 43 files changed, 1087 insertions(+), 118 deletions(-) create mode 100644 apple/TablePro/Core/ChangeTracking/SQLiteRowidAlias.swift create mode 100644 apple/TableProTests/Core/ChangeTracking/RowEditUndoStepTests.swift create mode 100644 apple/TableProTests/Views/Main/SaveInFlightEditGateTests.swift diff --git a/apple/TablePro/Core/ChangeTracking/DataChangeManager.swift b/apple/TablePro/Core/ChangeTracking/DataChangeManager.swift index 4ceae454c5..51793a5616 100644 --- a/apple/TablePro/Core/ChangeTracking/DataChangeManager.swift +++ b/apple/TablePro/Core/ChangeTracking/DataChangeManager.swift @@ -351,6 +351,13 @@ final class DataChangeManager: ObservableObject, ChangeManaging { wasExpression: pending.isExpression(rowID: rowID, columnIndex: columnIndex), isExpression: isExpression ) + // The grid's row shows the row's other pending cells; an undo that has to stage the row again needs it as read. + var absence = absence + let originalRow = originalRow.map { shown in + let image = pending.imageAsRead(ofRow: rowID, current: shown, absentColumns: absence.originalRow) + absence.originalRow = image.absentColumns + return image.row + } let action = coalescesWithPrevious ? typingRunAction() : commandAction ?? Self.newActionID() objectWillChange.send() let recorded = pending.recordCellChange( @@ -571,12 +578,49 @@ final class DataChangeManager: ObservableObject, ChangeManaging { // MARK: - Undo Operations func undoRowDeletion(rowID: RowID) { - guard pending.isRowDeleted(rowID) else { return } + restoreDeletedRows([rowID]) + } + + // A step of its own, so Undo deletes the rows again rather than replaying the delete under it the wrong way round. + func restoreDeletedRows(_ rowIDs: [RowID]) { + let restored = rowIDs.compactMap { rowID -> (rowID: RowID, originalRow: [PluginCellValue], absent: Set)? in + guard let delete = pending.change(forRow: rowID, type: .delete) else { return nil } + return (rowID, completedImage(delete.originalRow ?? [], of: rowID), delete.absentColumns) + } + guard !restored.isEmpty else { return } objectWillChange.send() - guard pending.undoRowDeletion(rowID: rowID) else { return } + for row in restored { + _ = pending.undoRowDeletion(rowID: row.rowID) + } + if restored.count == 1, let row = restored.first { + registerUndo(actionName: Self.restoreRowActionName) { target in + target.applyDataUndo(.rowDeletion(rowID: row.rowID, originalRow: row.originalRow, absentColumns: row.absent)) + } + } else { + let rows = restored.map { (rowID: $0.rowID, originalRow: $0.originalRow) } + let absentColumns = Dictionary( + restored.filter { !$0.absent.isEmpty }.map { ($0.rowID, $0.absent) }, + uniquingKeysWith: { first, _ in first } + ) + registerUndo(actionName: Self.restoreRowsActionName) { target in + target.applyDataUndo(.batchRowDeletion(rows: rows, absentColumns: absentColumns)) + } + } hasChanges = !pending.isEmpty } + private static var restoreRowActionName: String { String(localized: "Restore Row") } + private static var restoreRowsActionName: String { String(localized: "Restore Rows") } + + // A delete and a restore replay through one step, which keeps the name of the command that made it. + private func rowDeletionStepName(rowIsDeleted: Bool, batch: Bool) -> String { + let isUndoing = undoManagerProvider?()?.isUndoing ?? false + if isUndoing == rowIsDeleted { + return batch ? String(localized: "Delete Rows") : String(localized: "Delete Row") + } + return batch ? Self.restoreRowsActionName : Self.restoreRowActionName + } + func undoBatchRowInsertion(rows: [InsertedRowLocation]) { let validRows = rows.filter { pending.isRowInserted($0.rowID) } guard !validRows.isEmpty else { return } @@ -668,7 +712,8 @@ final class DataChangeManager: ObservableObject, ChangeManaging { pending.reapplyCellChange( rowID: rowID, columnIndex: columnIndex, columnName: columnName, - originalDBValue: newValue, newValue: previousValue, originalRow: originalRow, + originalDBValue: newValue, newValue: previousValue, + originalRow: originalRow.map { completedImage($0, of: rowID) }, absence: absence.reversed, expression: expression.reversed, action: actionID ) @@ -716,7 +761,8 @@ final class DataChangeManager: ObservableObject, ChangeManaging { ) { // The pending deletion's image, which a value loaded since the delete has completed. let image = completedImage(pending.change(forRow: rowID, type: .delete)?.originalRow ?? originalRow, of: rowID) - registerUndo(actionName: String(localized: "Delete Row")) { target in + let stepName = rowDeletionStepName(rowIsDeleted: pending.isRowDeleted(rowID), batch: false) + registerUndo(actionName: stepName) { target in target.applyDataUndo(.rowDeletion(rowID: rowID, originalRow: image, absentColumns: absentColumns)) } @@ -744,7 +790,8 @@ final class DataChangeManager: ObservableObject, ChangeManaging { let image = pending.change(forRow: row.rowID, type: .delete)?.originalRow ?? row.originalRow return (rowID: row.rowID, originalRow: completedImage(image, of: row.rowID)) } - registerUndo(actionName: String(localized: "Delete Rows")) { target in + let stepName = rowDeletionStepName(rowIsDeleted: rows.contains { pending.isRowDeleted($0.rowID) }, batch: true) + registerUndo(actionName: stepName) { target in target.applyDataUndo(.batchRowDeletion(rows: images, absentColumns: absentColumns)) } diff --git a/apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift b/apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift index 5583026125..026b3321d9 100644 --- a/apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift +++ b/apple/TablePro/Core/ChangeTracking/SQLStatementGenerator.swift @@ -364,8 +364,9 @@ struct SQLStatementGenerator { var maxBindParameters: Int { switch databaseType { - case .sqlite: 32_766 + case .sqlite, .libsql, .turso: 32_766 case .mssql: 2_100 + case .cloudflareD1: 100 default: 65_535 } } diff --git a/apple/TablePro/Core/ChangeTracking/SQLiteRowidAlias.swift b/apple/TablePro/Core/ChangeTracking/SQLiteRowidAlias.swift new file mode 100644 index 0000000000..52fcfe2331 --- /dev/null +++ b/apple/TablePro/Core/ChangeTracking/SQLiteRowidAlias.swift @@ -0,0 +1,20 @@ +// +// SQLiteRowidAlias.swift +// TablePro +// + +import Foundation + +// SQLite's INTEGER PRIMARY KEY is the rowid: an INSERT that leaves it out takes the next one. A WITHOUT ROWID table has none. +enum SQLiteRowidAlias { + static func isAlias( + column: String, + declaredType: String?, + primaryKey: [String], + family: SQLTypeFamily, + hasRowID: Bool = true + ) -> Bool { + guard family == .sqlite, hasRowID, primaryKey == [column], let declaredType else { return false } + return declaredType.trimmingCharacters(in: .whitespaces).uppercased() == "INTEGER" + } +} diff --git a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift index bcb80872e1..9043eef7d7 100644 --- a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift +++ b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift @@ -52,10 +52,11 @@ extension RowEditingCoordinator { return } + let savingTabId = parent.tabManager.selectedTabId /// One change manager serves the whole window, so a second save cannot start while one is /// in flight. Say so: over the slow link this guard exists for, a Save that silently does /// nothing is indistinguishable from a Save that is broken. - guard beginSaveInFlight() else { + guard beginSaveInFlight(tabId: savingTabId) else { saveChangesLogger.debug("Save already in flight, refusing the repeat") if let index = parent.tabManager.selectedTabIndex { parent.tabManager.mutate(at: index) { @@ -66,7 +67,6 @@ extension RowEditingCoordinator { return } - let savingTabId = parent.tabManager.selectedTabId guard parent.selectedTabScope != nil else { failSave(message: String(localized: "Not connected to database"), tabId: savingTabId, waiter: waiter) return diff --git a/apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift b/apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift index f43a74ee09..fa81cadf9a 100644 --- a/apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift +++ b/apple/TablePro/Core/Coordinators/RowEditingCoordinator.swift @@ -13,19 +13,29 @@ final class RowEditingCoordinator: ObservableObject { // Nothing clears the pending changes until the write returns, so a second Cmd+S in the round trip would commit twice. private(set) var isSaveInFlight = false + // The landing save clears this tab's staged edits and reloads its rows, so nothing may stage there meanwhile. + private(set) var savingTabId: UUID? init(parent: MainContentCoordinator) { self.parent = parent } - func beginSaveInFlight() -> Bool { + func beginSaveInFlight(tabId: UUID?) -> Bool { guard !isSaveInFlight else { return false } + parent.objectWillChange.send() isSaveInFlight = true + savingTabId = tabId return true } func endSaveInFlight() { + parent.objectWillChange.send() isSaveInFlight = false + savingTabId = nil + } + + func isSaving(tabId: UUID?) -> Bool { + tabId != nil && savingTabId == tabId } // A row command selects the row it made. The JSON view reads the selection as "show only these rows". @@ -104,43 +114,52 @@ final class RowEditingCoordinator: ObservableObject { guard parent.canEditActiveResult(.delete), let tabId = parent.tabManager.selectedTabId else { return } let deleted = rowIDs.filter { parent.changeManager.isRowDeleted($0) } guard !deleted.isEmpty else { return } - for rowID in deleted { - parent.changeManager.undoRowDeletion(rowID: rowID) - } + parent.changeManager.restoreDeletedRows(deleted) parent.existingGridModel(forTab: tabId)?.repaintRows(deleted) } func duplicateRow(_ rowID: RowID) { + duplicateRows([rowID]) + } + + func duplicateRows(_ rowIDs: [RowID]) { guard parent.canEditActiveResult(.insert), let (tab, tabIndex) = parent.tabManager.selectedTabAndIndex, tab.tableContext.tableName != nil else { return } let tabId = tab.id let tableRows = parent.tabSessionRegistry.tableRows(for: tabId) - guard tableRows.hasAuthoritativeSchema, - let storageIndex = tableRows.index(of: rowID) else { return } - guard parent.unloadedValuesAreLoaded(rowIDs: [rowID], retry: { [weak self] in - self?.duplicateRow(rowID) + let storageIndices = rowIDs.compactMap { tableRows.index(of: $0) } + guard tableRows.hasAuthoritativeSchema, !storageIndices.isEmpty else { return } + guard parent.unloadedValuesAreLoaded(rowIDs: rowIDs, retry: { [weak self] in + self?.duplicateRows(rowIDs) }) else { return } parent.commitActiveGridEdit() let uuidKeys = uuidKeyForms(in: tableRows, tab: tab) - var dupResult: RowOperationsManager.AddNewRowResult? + var duplicated: [RowOperationsManager.AddNewRowResult] = [] parent.mutateActiveTableRows(for: tabId) { rows in - let result = parent.rowOperationsManager.duplicateRow( - sourceRowIndex: storageIndex, + let result = parent.rowOperationsManager.duplicateRows( + sourceRowIndices: storageIndices, tableRows: &rows, uuidKeyForms: uuidKeys ) - dupResult = result - return result?.delta ?? .none + duplicated = result.rows + return result.delta } - guard let result = dupResult else { return } + guard let first = duplicated.first else { return } parent.tabManager.mutate(at: tabIndex) { $0.hasUserInteraction = true } - selectAndEditInsertedRow(result.rowID, tabId: tabId, skipping: result.prefilledColumns) + guard duplicated.count > 1 else { + selectAndEditInsertedRow(first.rowID, tabId: tabId, skipping: first.prefilledColumns) + return + } + if selectionPointsTheGrid, let model = parent.existingGridModel(forTab: tabId) { + model.selectRows(duplicated.map(\.rowID)) + model.reveal(rowID: first.rowID) + } } // The caret skips a key the row was given a fresh UUID for, so the first keystroke does not replace it. @@ -286,7 +305,8 @@ final class RowEditingCoordinator: ObservableObject { if continuity == .discrete { endInspectorEditRun() } - guard let (tab, tabIndex) = parent.tabManager.selectedTabAndIndex, !valuesByRow.isEmpty else { return } + guard !parent.isSavingSelectedTab, + let (tab, tabIndex) = parent.tabManager.selectedTabAndIndex, !valuesByRow.isEmpty else { return } let tabId = tab.id let tableRows = parent.tabSessionRegistry.tableRows(for: tabId) guard tableRows.columns.indices.contains(columnIndex) else { return } diff --git a/apple/TablePro/Core/CrossEngine/CrossEngineDefaultValue.swift b/apple/TablePro/Core/CrossEngine/CrossEngineDefaultValue.swift index f87f31e94f..0f3395d82e 100644 --- a/apple/TablePro/Core/CrossEngine/CrossEngineDefaultValue.swift +++ b/apple/TablePro/Core/CrossEngine/CrossEngineDefaultValue.swift @@ -66,6 +66,18 @@ internal enum CrossEngineDefaultValue { return head.trimmingCharacters(in: .whitespaces) } + // A default built on the current date or time, UTC forms and time zone conversions included. + internal static func readsTheClock(_ value: String) -> Bool { + let upper = value.uppercased() + return clockNames.contains { upper.contains($0) } + } + + private static let clockNames = [ + "NOW(", "CURRENT_TIMESTAMP", "LOCALTIMESTAMP", "CURRENT_DATE", "CURDATE(", "CURTIME(", "TODAY(", + "GETDATE(", "GETUTCDATE(", "SYSDATETIME(", "SYSUTCDATETIME(", "SYSDATETIMEOFFSET(", "UTC_TIMESTAMP", + "SYSDATE", "SYSTIMESTAMP", "STATEMENT_TIMESTAMP(", "TRANSACTION_TIMESTAMP(", "CLOCK_TIMESTAMP(", + ] + private static func currentTimestamp(_ upper: String, target: SQLTypeFamily) -> String? { let names = [ "CURRENT_TIMESTAMP", "CURRENT_TIMESTAMP()", "NOW()", "GETDATE()", "GETUTCDATE()", diff --git a/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift b/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift index 2dda435413..3e365d308b 100644 --- a/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift +++ b/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift @@ -11,9 +11,10 @@ // affected count against the count the plan expected is what catches it, and inside a transaction // it is caught before it lands. // -// For a keyed write the comparison is `actual > expected`, never `actual != expected`. MySQL reports -// zero affected rows for an UPDATE that writes a value a row already holds, which is a normal save, -// not a fault. +// For a keyed write the comparison is `actual > expected`. MySQL reports zero affected rows for an +// UPDATE that writes a value a row already holds, which is a normal save, not a fault. Where the +// count is the rows the UPDATE matched, a short one means the row was not written: deleted or +// rekeyed since it was read, kept out by row-level security, or skipped by a trigger. // // A keyless write is held to both ends. It finds its row by matching every column against the text // the grid read, and that match can simply find nothing: measured on MySQL 8.4.11 and OceanBase @@ -161,6 +162,10 @@ enum DataWriteExecutor { sessionState: await driver.heldSessionTransactionState() ) var results: [DataWriteStepResult] = [] + // Through a view the count is whatever its trigger or rule reports. + let countsKeyedUpdates = DataWriteRowCounts.countsMatchedRows(for: plan.databaseType) && plan.viewTarget == nil + // A delete earlier in the save can cascade to a row a later update edits, which then matches nothing. + var ranDelete = false do { if owner.opensTransaction { @@ -202,8 +207,10 @@ enum DataWriteExecutor { step, rowsAffected: result.rowsAffected, owner: owner, - countsAreMeaningful: DataWriteRowCounts.areMeaningful(for: plan.databaseType) + countsAreMeaningful: DataWriteRowCounts.areMeaningful(for: plan.databaseType), + holdsKeyedUpdates: countsKeyedUpdates && !ranDelete ) + ranDelete = ranDelete || step.rowKind == .delete || step.kind == .tableOperation results.append( DataWriteStepResult( executionTime: Date().timeIntervalSince(start), @@ -291,7 +298,13 @@ enum DataWriteExecutor { ) ) } - guard rows.count != lookup.rowCount else { return rows } + guard rows.count != lookup.rowCount else { + guard let row = rows.first, lookup.rowCount == 1, + !lookup.heldValues.allSatisfy({ row.indices.contains($0.key) && row[$0.key] == $0.value }) + else { return rows } + logger.error("Lookup on '\(table, privacy: .private(mask: .hash))' found the row changed since it was read") + throw DataWriteError.changeRefused(table: table, kind: step.rowKind, reason: rowNotFoundReason) + } logger.error( "Lookup on '\(table, privacy: .private(mask: .hash))' found \(rows.count, privacy: .public) rows, expected \(lookup.rowCount, privacy: .public)" ) @@ -300,7 +313,7 @@ enum DataWriteExecutor { // A lookup that expects no row is an insert checking its key is free. reason = String(localized: "A row with this key already exists, so the insert would not add one.") } else if rows.count < lookup.rowCount { - reason = String(localized: "No row holds the values it was loaded with. Another client may have changed or deleted it.") + reason = rowNotFoundReason } else { reason = String(localized: "Other rows hold exactly the same values, so the change cannot reach this row alone.") } @@ -324,6 +337,10 @@ enum DataWriteExecutor { ) } + private static var rowNotFoundReason: String { + String(localized: "No row holds the values it was loaded with. Another client may have changed or deleted it.") + } + private static func fetch(_ statement: ParameterizedStatement, on driver: DatabaseDriver) async throws -> QueryResult { statement.parameters.isEmpty ? try await driver.execute(query: statement.sql) @@ -334,7 +351,8 @@ enum DataWriteExecutor { _ step: DataWriteStep, rowsAffected: Int, owner: WriteTransactionOwner, - countsAreMeaningful: Bool + countsAreMeaningful: Bool, + holdsKeyedUpdates: Bool ) throws { // A row inserted between a lookup without a pin and its write can still be reached, and a real count shows it. guard let expected = step.expectedRowCount ?? step.lookup?.rowCount else { return } @@ -354,6 +372,18 @@ enum DataWriteExecutor { ) throw DataWriteError.keyNoLongerMatchesOneRow(table: table, expected: expected, actual: rowsAffected) } + if holdsKeyedUpdates, step.rowKind == .update, !step.matchesRowsWithoutKey { + logger.error( + "Keyed update on '\(table, privacy: .private(mask: .hash))' matched \(rowsAffected, privacy: .public) rows, expected \(expected, privacy: .public)" + ) + throw DataWriteError.changeRefused( + table: table, + kind: .update, + reason: String( + localized: "No row took the change. Another client may have changed or deleted it, or a security policy or trigger skipped it." + ) + ) + } guard step.matchesRowsWithoutKey else { return } logger.error( "Keyless statement on '\(table, privacy: .private(mask: .hash))' affected \(rowsAffected, privacy: .public) rows, expected \(expected, privacy: .public)" diff --git a/apple/TablePro/Core/DataWrite/DataWritePlan.swift b/apple/TablePro/Core/DataWrite/DataWritePlan.swift index f67c4c5bf5..2cc5f615a3 100644 --- a/apple/TablePro/Core/DataWrite/DataWritePlan.swift +++ b/apple/TablePro/Core/DataWrite/DataWritePlan.swift @@ -173,12 +173,21 @@ struct RowLookup: Sendable { let rowCount: Int let pinsRows: Bool let check: ParameterizedStatement? + // By position in the query's columns: what the one row found must still hold, or another writer changed it. + let heldValues: [Int: PluginCellValue] - init(query: ParameterizedStatement, rowCount: Int, pinsRows: Bool, check: ParameterizedStatement? = nil) { + init( + query: ParameterizedStatement, + rowCount: Int, + pinsRows: Bool, + check: ParameterizedStatement? = nil, + heldValues: [Int: PluginCellValue] = [:] + ) { self.query = query self.rowCount = rowCount self.pinsRows = pinsRows self.check = check + self.heldValues = heldValues } init(_ lookup: PluginRowLookup) { @@ -254,7 +263,8 @@ extension DataWriteStep { expectedRowCount: $0.rowCount, tableName: tableName, matchesRowsWithoutKey: primaryKeyColumns.isEmpty && $0.kind != .insert, - refusesSharedKey: $0.refusesSharedKey + refusesSharedKey: $0.refusesSharedKey, + rowKind: $0.kind ) } case .driverWritten(let statements): diff --git a/apple/TablePro/Core/DataWrite/DataWriteRowCounts.swift b/apple/TablePro/Core/DataWrite/DataWriteRowCounts.swift index 7c584aff47..02c0ca1b97 100644 --- a/apple/TablePro/Core/DataWrite/DataWriteRowCounts.swift +++ b/apple/TablePro/Core/DataWrite/DataWriteRowCounts.swift @@ -20,7 +20,19 @@ enum DataWriteRowCounts { .mssql, .oracle, ] + // An UPDATE's count there is the rows it matched, including rows already holding the value; the MySQL family + // reports the rows it changed, so 0 there is also an UPDATE that wrote what the row held. + private static let enginesCountingMatchedRows: Set = [ + .postgresql, .redshift, .cockroachdb, .pglite, .kingbasees, + .sqlite, .libsql, .turso, .duckdb, + .mssql, .oracle, + ] + static func areMeaningful(for databaseType: DatabaseType) -> Bool { enginesReportingRealCounts.contains(databaseType) } + + static func countsMatchedRows(for databaseType: DatabaseType) -> Bool { + enginesCountingMatchedRows.contains(databaseType) + } } diff --git a/apple/TablePro/Core/DataWrite/KeyedWriteVerification.swift b/apple/TablePro/Core/DataWrite/KeyedWriteVerification.swift index fc82d15ad3..65822b5157 100644 --- a/apple/TablePro/Core/DataWrite/KeyedWriteVerification.swift +++ b/apple/TablePro/Core/DataWrite/KeyedWriteVerification.swift @@ -2,9 +2,9 @@ // KeyedWriteVerification.swift // TablePro // -// The rule every keyed write is held to: more rows than expected is a fault, fewer is not. -// MySQL reports zero affected rows for an UPDATE that writes a value a row already holds, which -// is a normal outcome, so a keyed write is never held to `actual != expected`. +// The rule every keyed write is held to: more rows than expected is a fault. Fewer is one only +// for an UPDATE on an engine whose count is the rows it matched (`DataWriteRowCounts`): MySQL +// reports zero affected rows for an UPDATE that writes a value a row already holds. // import Foundation diff --git a/apple/TablePro/Core/DataWrite/Rewind/RewindPlanner.swift b/apple/TablePro/Core/DataWrite/Rewind/RewindPlanner.swift index 7b6b7116d3..914ea8cb13 100644 --- a/apple/TablePro/Core/DataWrite/Rewind/RewindPlanner.swift +++ b/apple/TablePro/Core/DataWrite/Rewind/RewindPlanner.swift @@ -295,6 +295,7 @@ struct RewindPlanner { var lookups: [RowLookup?] = [] var prologue: [String] = [] var epilogue: [String] = [] + var checked: Set = [] for entry in Self.runOrder(of: operations) { let operation = operations[entry.operation] let inverse: RestoreStatements @@ -304,13 +305,55 @@ struct RewindPlanner { inverse = try inverseStatements(for: operation) } statements.append(contentsOf: inverse.statements) - lookups.append(contentsOf: inverse.lookups) + // The operation's first statement to run finds the row as the save left it, so it is the one checked. + if checked.insert(entry.operation).inserted, let lookup = heldRowLookup(for: operation, inverse) { + lookups.append(lookup) + } else { + lookups.append(contentsOf: inverse.lookups) + } prologue.append(contentsOf: inverse.prologue.filter { !prologue.contains($0) }) epilogue.append(contentsOf: inverse.epilogue.filter { !epilogue.contains($0) }) } return RestoreStatements(statements: statements, lookups: lookups, prologue: prologue, epilogue: epilogue) } + // A statement with no lookup of its own finds the row by its key alone, so the sheet's check runs again inside the + // write: a row another writer changed since the review refuses the restore instead of being overwritten. + private func heldRowLookup(for operation: RowWriteOperation, _ inverse: RestoreStatements) -> RowLookup? { + guard inverse.statements.count == 1, inverse.lookups.allSatisfy({ $0 == nil }), + operation.kind == .update || operation.kind == .insert, + operation.rowLocator == nil, + !PluginManager.shared.supportsFieldRemoval(for: record.databaseType), + let image = operation.postImage, + let query = keyedRead(of: image, for: operation) + else { return nil } + var held: [Int: PluginCellValue] = [:] + for column in operation.writtenColumns { + guard let index = operation.columns.firstIndex(of: column), index < image.count else { return nil } + held[index] = image[index] + } + return RowLookup( + query: ParameterizedStatement(sql: query, parameters: []), rowCount: 1, pinsRows: false, heldValues: held + ) + } + + private func keyedRead(of image: [PluginCellValue], for operation: RowWriteOperation) -> String? { + let filters = operation.primaryKeyColumns.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.isEmpty, filters.count == operation.primaryKeyColumns.count else { return nil } + return queryBuilder.buildFilteredQuery( + tableName: record.target.table, + schemaName: record.target.schema, + filters: filters, + logicMode: .and, + columns: operation.columns, + selectColumns: operation.columns, + limit: 1 + ) + } + // Sets the step's columns back, keyed on the row as that step left it. private func inverseStatements(for operation: RowWriteOperation, step index: Int) throws -> RestoreStatements { guard let image = operation.stepImages()?[safe: index] else { return RestoreStatements(statements: []) } diff --git a/apple/TablePro/Core/RowGeneration/RowGenerationColumnReader.swift b/apple/TablePro/Core/RowGeneration/RowGenerationColumnReader.swift index beb1cf185f..3c8a962a06 100644 --- a/apple/TablePro/Core/RowGeneration/RowGenerationColumnReader.swift +++ b/apple/TablePro/Core/RowGeneration/RowGenerationColumnReader.swift @@ -111,9 +111,9 @@ internal enum RowGenerationColumnReader { CrossEngineDefaultValue.strippingCast(value).uppercased().hasPrefix("NEXTVAL(") { return .assigned } - // SQLite's INTEGER PRIMARY KEY is the rowid: left out, it takes the next one. A WITHOUT ROWID table has none. - if family == .sqlite, hasRowID, primaryKey == [info.name], - info.dataType.trimmingCharacters(in: .whitespaces).uppercased() == "INTEGER" { + if SQLiteRowidAlias.isAlias( + column: info.name, declaredType: info.dataType, primaryKey: primaryKey, family: family, hasRowID: hasRowID + ) { return .assigned } return .none diff --git a/apple/TablePro/Core/SchemaTracking/StructureRebase.swift b/apple/TablePro/Core/SchemaTracking/StructureRebase.swift index 9134449a8f..c28c9b9da7 100644 --- a/apple/TablePro/Core/SchemaTracking/StructureRebase.swift +++ b/apple/TablePro/Core/SchemaTracking/StructureRebase.swift @@ -105,18 +105,35 @@ enum StructureRebase { let entityName = entity[keyPath: name] if !entityName.isEmpty, idsBeforeSave[entityName] == nil { idsBeforeSave[entityName] = entity.id } } + let earlierByID = Dictionary(earlier.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first }) + let loadedNames = Set(loaded.map { $0[keyPath: name] }) + func reads(_ entity: Entity, as held: Entity?) -> Bool { + guard let held else { return false } + var probe = entity + probe[keyPath: id] = held.id + return probe == held + } + // A deleted definition and another one that takes its name: the drop runs first, so the table still holds the + // deleted one while a rename into the name has not run, or while it reads as it did before the save. A rename + // whose old name another definition takes leaves no such evidence, and the renamed one is matched as before. + func stillHoldsDeleted(_ entity: Entity, deleted before: UUID, taker after: UUID) -> Bool { + if let renamedFrom = earlierByID[after]?[keyPath: name], renamedFrom != entity[keyPath: name] { + return idsAfterSave[renamedFrom] == nil && loadedNames.contains(renamedFrom) + } + return reads(entity, as: earlierByID[before]) + } var used = Set() let current: [Entity] = loaded.map { entity in let entityName = entity[keyPath: name] var candidates = [idsAfterSave[entityName], idsBeforeSave[entityName]].compactMap { $0 } + if let before = idsBeforeSave[entityName], deleted.contains(before), + let after = idsAfterSave[entityName], after != before, + stillHoldsDeleted(entity, deleted: before, taker: after) { + candidates = [before, after] + } if entityName.isEmpty { // A definition with no name is the one it was when nothing else about it changed. - candidates = earlier.filter { held in - var probe = entity - probe[keyPath: id] = held.id - return probe == held - } - .map(\.id) + candidates = earlier.filter { reads(entity, as: $0) }.map(\.id) } guard let kept = candidates.first(where: { !used.contains($0) }) else { return entity } used.insert(kept) @@ -126,7 +143,6 @@ enum StructureRebase { } let currentByID = Dictionary(current.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first }) - let earlierByID = Dictionary(earlier.map { ($0.id, $0) }, uniquingKeysWith: { first, _ in first }) var rebasedWorking: [Entity] = [] var changes: [(key: SchemaChangeIdentifier, change: SchemaChange)] = [] for entity in working { diff --git a/apple/TablePro/Core/Services/Formatting/JsonReindenter.swift b/apple/TablePro/Core/Services/Formatting/JsonReindenter.swift index a34098ad0f..c6e40a8bb1 100644 --- a/apple/TablePro/Core/Services/Formatting/JsonReindenter.swift +++ b/apple/TablePro/Core/Services/Formatting/JsonReindenter.swift @@ -21,6 +21,15 @@ internal enum JsonReindenter { reindentIfValid(source, indent: indent) ?? source } + // An edit keeps the stored value's layout: a value stored on one line stays compact, a formatted one takes the + // editor's lines rather than losing every line break. + static func staged(_ edited: String, keepingLayoutOf stored: String?) -> String { + guard let stored, stored.trimmingCharacters(in: .whitespacesAndNewlines).contains(where: \.isNewline) else { + return normalize(edited) + } + return edited + } + static func normalize(_ source: String) -> String { guard (source as NSString).length <= maxLength else { return source } guard let node = JsonSyntaxParser.parse(source) else { return source } diff --git a/apple/TablePro/Core/Services/Query/RowOperationsManager.swift b/apple/TablePro/Core/Services/Query/RowOperationsManager.swift index 2a33425139..4713a5b7cd 100644 --- a/apple/TablePro/Core/Services/Query/RowOperationsManager.swift +++ b/apple/TablePro/Core/Services/Query/RowOperationsManager.swift @@ -72,15 +72,52 @@ final class RowOperationsManager { ) } - // A pending source row keeps its staged SQL, so a copy of now() is now() again, not the text. func duplicateRow( sourceRowIndex: Int, tableRows: inout TableRows, uuidKeyForms: [Int: UUIDCellForm] = [:] ) -> AddNewRowResult? { - guard sourceRowIndex >= 0, sourceRowIndex < tableRows.count else { return nil } + duplicateRows(sourceRowIndices: [sourceRowIndex], tableRows: &tableRows, uuidKeyForms: uuidKeyForms).rows.first + } + + struct DuplicateRowsResult { + let rows: [AddNewRowResult] + let delta: Delta + } - let source = tableRows.rows[sourceRowIndex] + // Every copy is one undo step, as a paste of many rows is. + func duplicateRows( + sourceRowIndices: [Int], + tableRows: inout TableRows, + uuidKeyForms: [Int: UUIDCellForm] = [:] + ) -> DuplicateRowsResult { + let firstIndex = tableRows.count + var results: [AddNewRowResult] = [] + var recorded: [DataChangeManager.InsertedRow] = [] + for sourceRowIndex in sourceRowIndices where sourceRowIndex >= 0 && sourceRowIndex < firstIndex { + let copy = duplicatedRow(of: tableRows.rows[sourceRowIndex], in: tableRows, uuidKeyForms: uuidKeyForms) + let rowID = RowID.inserted(UUID()) + let storageIndex = tableRows.count + let delta = tableRows.appendInsertedRow(id: rowID, values: copy.values, absentColumns: copy.absentColumns) + recorded.append(DataChangeManager.InsertedRow( + location: InsertedRowLocation(rowID: rowID, storageIndex: storageIndex), + values: copy.values, absentColumns: copy.absentColumns, expressionColumns: copy.expressionColumns + )) + results.append(AddNewRowResult( + rowID: rowID, values: copy.values, delta: delta, prefilledColumns: copy.prefilledColumns + )) + } + changeManager.recordBatchRowInsertion(rows: recorded) + let delta: Delta = results.isEmpty ? .none : .rowsInserted(IndexSet(integersIn: firstIndex.. RowCopy { var newValues = Array(source.values) var absentColumns = source.absentColumns var expressionColumns = changeManager.expressionColumns(forRow: source.id).filter { $0 < newValues.count } @@ -103,15 +140,18 @@ final class RowOperationsManager { } } } - return appendInsertedRow( - values: newValues, - absentColumns: absentColumns, - expressionColumns: expressionColumns, - prefilledColumns: prefilled, - to: &tableRows + return RowCopy( + values: newValues, absentColumns: absentColumns, expressionColumns: expressionColumns, prefilledColumns: prefilled ) } + private struct RowCopy { + let values: [PluginCellValue] + let absentColumns: Set + let expressionColumns: Set + let prefilledColumns: Set + } + private static func freshUUID(_ form: UUIDCellForm) -> PluginCellValue { form.value(for: UUIDValueGenerator.shared.make(.v4)) } @@ -352,7 +392,9 @@ final class RowOperationsManager { let parsedRows = Self.reconcileStructuredRows( payload, schema: schema, keepsMissingFields: keepsMissingFields ) - return insertParsedRows(parsedRows, uuidKeyForms: uuidKeyForms, into: &tableRows) + return insertParsedRows( + parsedRows, primaryKeyColumns: primaryKeyColumns, uuidKeyForms: uuidKeyForms, into: &tableRows + ) } guard let clipboardText = clipboardProvider.readText() else { @@ -363,6 +405,7 @@ final class RowOperationsManager { let parsedRows = Self.rows(fromCopiedCells: cells, schema: schema, tableRows: tableRows) return insertParsedRows( keepsMissingFields ? parsedRows.map(Self.nullsAsMissingFields) : parsedRows, + primaryKeyColumns: primaryKeyColumns, uuidKeyForms: uuidKeyForms, into: &tableRows ) @@ -373,10 +416,11 @@ final class RowOperationsManager { switch parseResult { case .success(let parsedRows): - let unfilled = Self.unfilledCellValues(columns: columns, tableRows: tableRows) + let unfilled = Self.unfilledCellValues(columns: columns, primaryKeyColumns: primaryKeyColumns, tableRows: tableRows) let filled = parsedRows.map { Self.fillingBlankCells($0, with: unfilled) } return insertParsedRows( keepsMissingFields ? filled.map(Self.nullsAsMissingFields) : filled, + primaryKeyColumns: primaryKeyColumns, uuidKeyForms: uuidKeyForms, into: &tableRows ) @@ -400,7 +444,7 @@ final class RowOperationsManager { let sourceExpressions = payload.expressionCells?[index] ?? [] var absentColumns: Set = [] var expressionColumns: Set = [] - var values: [PluginCellValue] = sourceForDestination.enumerated().map { destination, sourceIndex in + let values: [PluginCellValue] = sourceForDestination.enumerated().map { destination, sourceIndex in guard let sourceIndex, sourceIndex < row.count else { if keepsMissingFields { absentColumns.insert(destination) } return .null @@ -412,12 +456,6 @@ final class RowOperationsManager { return row[sourceIndex] } - if let pkIndex = schema.primaryKeyIndex, pkIndex < values.count { - values[pkIndex] = .columnDefault - absentColumns.remove(pkIndex) - expressionColumns.insert(pkIndex) - } - return ParsedRow( values: values, sourceLineNumber: index + 1, @@ -436,7 +474,7 @@ final class RowOperationsManager { cells.fields.enumerated().map { index, fields in let marked = cells.expressionFields?[index] ?? [] var expressionColumns: Set = [] - var values: [PluginCellValue] = (0.. (values: [PluginCellValue], defaults: Set) { var defaults: Set = [] @@ -465,7 +501,8 @@ final class RowOperationsManager { defaults.insert(position) return .columnDefault } - if tableRows.refusesNull(columnAt: index), tableRows.editingColumnType(at: index)?.hasEmptyValue == true { + if tableRows.refusesNull(columnAt: index), tableRows.editingColumnType(at: index)?.hasEmptyValue == true, + !primaryKeyColumns.contains(name) { return .text("") } return .null @@ -509,6 +546,36 @@ final class RowOperationsManager { return destination.indices.map { $0 < source.count ? $0 : nil } } + // Left out of the INSERT, the server gives the key a value of its own: an identity, a default that calls a function + // such as nextval(), SQLite's rowid, or a key the driver takes no value for, as MongoDB's _id. A constant default + // would put every pasted row under the same key. + private func serverFillsKey(_ column: String, primaryKeyColumns: [String], in tableRows: TableRows) -> Bool { + guard primaryKeyColumns.contains(column) else { return false } + if tableRows.columnIdentity[column] != nil || !changeManager.isColumnWritable(column) { return true } + if let defaultValue = tableRows.columnDefaults[column] ?? nil, Self.givesEachRowItsOwnValue(defaultValue) { + return true + } + guard let databaseType = changeManager.databaseType else { return false } + return SQLiteRowidAlias.isAlias( + column: column, + declaredType: tableRows.columnDeclaredTypes[column], + primaryKey: primaryKeyColumns, + family: SQLTypeFamily.of(databaseType) + ) + } + + // A sequence, or a function other than the clock. SQL Server wraps a default in parentheses, `((1))` or `(newid())`, + // and PostgreSQL casts it, `'a'::text`. + private static func givesEachRowItsOwnValue(_ defaultValue: String) -> Bool { + var text = defaultValue.trimmingCharacters(in: .whitespaces) + while text.hasPrefix("("), text.hasSuffix(")") { + text = String(text.dropFirst().dropLast()).trimmingCharacters(in: .whitespaces) + } + let upper = text.uppercased() + if upper.contains("NEXTVAL") || upper.contains("NEXT VALUE FOR") { return true } + return CrossEngineDefaultValue.strippingCast(text).contains("(") && !CrossEngineDefaultValue.readsTheClock(text) + } + static func detectParser(for text: String) -> RowDataParser { var containsTab = false var containsComma = false @@ -527,9 +594,11 @@ final class RowOperationsManager { return containsComma ? CSVRowParser() : TSVRowParser() } - // Every paste path resets the key to its default first; an unfilled UUID key then takes a fresh UUID per row. + // A key the server fills goes back to its default, a natural key keeps the pasted value, and an unfilled UUID key + // takes a fresh UUID per row. private func insertParsedRows( _ parsedRows: [ParsedRow], + primaryKeyColumns: [String], uuidKeyForms: [Int: UUIDCellForm], into tableRows: inout TableRows ) -> PasteRowsResult { @@ -542,6 +611,7 @@ final class RowOperationsManager { // Pasted server-owned values skip the cell-edit check and the generator drops them, so the grid showed unsaved values. let serverOwned = tableRows.columns.enumerated().filter { _, name in tableRows.generatedColumns.contains(name) || tableRows.columnIdentity[name] != nil + || serverFillsKey(name, primaryKeyColumns: primaryKeyColumns, in: tableRows) }.map(\.offset) for parsedRow in parsedRows { @@ -554,7 +624,8 @@ final class RowOperationsManager { expressionColumns.insert(index) } for (index, form) in uuidKeyForms where index < rowValues.count - && rowValues[index].isColumnDefault(isExpression: expressionColumns.contains(index)) { + && (rowValues[index].isNull + || 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 64179f3039..3284ad0c7c 100644 --- a/apple/TablePro/Core/Services/Query/RowParser.swift +++ b/apple/TablePro/Core/Services/Query/RowParser.swift @@ -19,8 +19,7 @@ protocol RowDataParser { func parse(_ text: String, schema: TableSchema) -> Result<[ParsedRow], RowParseError> } -// One line's fields fitted to the table: a short line's missing fields are blank cells, extra fields are dropped, -// and the key is left to the server. +// One line's fields fitted to the table: a short line's missing fields are blank cells, extra fields are dropped. private func fittedRow( _ fields: [String], lineNumber: Int, @@ -37,17 +36,9 @@ private func fittedRow( blankColumns.insert(values.count) values.append(nil) } - 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: cells, + values: values.map(PluginCellValue.fromOptional), sourceLineNumber: lineNumber, - expressionColumns: defaultColumns, blankColumns: blankColumns ) } @@ -237,11 +228,12 @@ struct CSVRowParser: RowDataParser { return matchCount > schema.columnCount / 2 } + // RFC 4180: spaces are part of a field. Only a blank or NULL field reads as no value. private func normalizeValue(_ rawValue: String) -> String? { let trimmed = rawValue.trimmingCharacters(in: .whitespaces) if trimmed.isEmpty || trimmed.uppercased() == "NULL" { return nil } - return trimmed + return rawValue } } diff --git a/apple/TablePro/Models/Database/TableSchema.swift b/apple/TablePro/Models/Database/TableSchema.swift index 578a8df7d4..b0229f39f7 100644 --- a/apple/TablePro/Models/Database/TableSchema.swift +++ b/apple/TablePro/Models/Database/TableSchema.swift @@ -29,9 +29,6 @@ struct TableSchema { primaryKeyColumns.compactMap { columns.firstIndex(of: $0) } } - /// Get index of first primary key column - var primaryKeyIndex: Int? { primaryKeyIndices.first } - /// Check if a column name exists func hasColumn(_ name: String) -> Bool { columns.contains(name) diff --git a/apple/TablePro/Models/UI/JsonFieldEditingModel.swift b/apple/TablePro/Models/UI/JsonFieldEditingModel.swift index 8b86f24408..bbf7ffe86f 100644 --- a/apple/TablePro/Models/UI/JsonFieldEditingModel.swift +++ b/apple/TablePro/Models/UI/JsonFieldEditingModel.swift @@ -13,9 +13,9 @@ import Foundation /// /// The rule is ``lastSynced``: the editor's text and the stored value are known to agree at that /// string, so a value arriving from the store that still means the same thing is this editor's own -/// write coming back and is ignored. What the field stages is a compacted form of what the editor -/// shows, so the two agree in meaning rather than in bytes, and a comparison against the editor's -/// displayed text cannot tell an echo from an external change on its own. +/// write coming back and is ignored. A value stored on one line is staged compacted, so the two agree +/// in meaning rather than in bytes, and a comparison against the editor's displayed text cannot tell +/// an echo from an external change on its own. /// /// Adopting an echo is what let a keystroke be overwritten by the value it had just replaced, and /// then pushed back, forever: one keystroke produced 6,944 body evaluations in 30 seconds at 100% diff --git a/apple/TablePro/Models/UI/MultiRowEditState.swift b/apple/TablePro/Models/UI/MultiRowEditState.swift index a9c0f4ae65..95c9555755 100644 --- a/apple/TablePro/Models/UI/MultiRowEditState.swift +++ b/apple/TablePro/Models/UI/MultiRowEditState.swift @@ -464,11 +464,10 @@ final class MultiRowEditState: ObservableObject { private static func resolvePendingValue(_ value: String?, original: String?, isJson: Bool) -> String? { if isJson, let value, !value.isEmpty { - let normalized = JsonReindenter.normalize(value) - if let original, JsonReindenter.normalize(original) == normalized { + if let original, JsonReindenter.normalize(original) == JsonReindenter.normalize(value) { return nil } - return normalized + return JsonReindenter.staged(value, keepingLayoutOf: original) } if value == original || (original == nil && value == "") { return nil diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 59ea7fab79..28ca3e537b 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -120564,6 +120564,9 @@ } } } + }, + "No row took the change. Another client may have changed or deleted it, or a security policy or trigger skipped it." : { + }, "No rows" : { "localizations" : { @@ -151500,6 +151503,12 @@ } } } + }, + "Restore Row" : { + + }, + "Restore Rows" : { + }, "Restore the previous values of a save" : { "localizations" : { diff --git a/apple/TablePro/Views/Main/Child/DataTabGridAdapter.swift b/apple/TablePro/Views/Main/Child/DataTabGridAdapter.swift index a53466d541..0b608aae63 100644 --- a/apple/TablePro/Views/Main/Child/DataTabGridAdapter.swift +++ b/apple/TablePro/Views/Main/Child/DataTabGridAdapter.swift @@ -105,8 +105,8 @@ final class DataTabGridAdapter: DataGridDataSource, DataGridActions { } func gridDuplicateRows(_ rowIDs: [RowID]) { - guard ownsWindowState, let first = rowIDs.first else { return } - coordinator?.duplicateRow(first) + guard ownsWindowState, !rowIDs.isEmpty else { return } + coordinator?.duplicateRows(rowIDs) } func gridCanPasteRows() -> Bool { diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ResultEditing.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ResultEditing.swift index 42a1a7eec2..2af30127ec 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ResultEditing.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ResultEditing.swift @@ -27,12 +27,19 @@ extension MainContentCoordinator { /// two. They all read this now. var canEditActiveResult: Bool { guard let tab = tabManager.selectedTab else { return false } - // A claimed execution replaces these rows and clears whatever was staged on them meanwhile, as the data files grid does when busy. + // A claimed execution or a landing save replaces these rows and clears whatever was staged on them meanwhile, as the data + // files grid does when busy. return tab.tableContext.isEditable && tab.tableContext.allowsRowEditing && !safeModeLevel.blocksAllWrites && activeResultEditRefusal == nil && !tabExecution.isExecuting(tab.id) + && !rowEditingCoordinator.isSaving(tabId: tab.id) + } + + // Undo, Redo and Discard wait too: the landing save clears the change manager they would act on. + var isSavingSelectedTab: Bool { + rowEditingCoordinator.isSaving(tabId: tabManager.selectedTabId) } // A query tab learns its result reads a view from the result's origin; a table tab from its context. diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+RowOperations.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+RowOperations.swift index 6f34f12452..1a236f244f 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+RowOperations.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+RowOperations.swift @@ -33,6 +33,10 @@ extension MainContentCoordinator { rowEditingCoordinator.duplicateRow(rowID) } + func duplicateRows(_ rowIDs: [RowID]) { + rowEditingCoordinator.duplicateRows(rowIDs) + } + func handleUndoResult(_ result: UndoResult) { rowEditingCoordinator.handleUndoResult(result) } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentView+Bindings.swift b/apple/TablePro/Views/Main/Extensions/MainContentView+Bindings.swift index 3c82d37c5b..69f32d3579 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentView+Bindings.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentView+Bindings.swift @@ -158,7 +158,8 @@ extension MainContentView { resultsViewMode: currentTab?.display.resultsViewMode ?? .data, inspectorRowSourceRevision: coordinator.inspectorRowSourceRevision, gridDisplayRevision: coordinator.gridDisplayRevision, - structureEditor: coordinator.activeStructureEditor.map { ObjectIdentifier($0) } + structureEditor: coordinator.activeStructureEditor.map { ObjectIdentifier($0) }, + isSaving: coordinator.isSavingSelectedTab ) } } @@ -172,6 +173,8 @@ struct InspectorTrigger: Equatable { let gridDisplayRevision: Int // A structure session or Create Table draft appears after its view first mounts, which moves nothing else here. var structureEditor: ObjectIdentifier? + // The fields go read-only while a save of this tab runs, and back when it ends. + var isSaving = false } enum InspectorValueDisplayFormatResolver { diff --git a/apple/TablePro/Views/Main/MainContentCommandActions+Structure.swift b/apple/TablePro/Views/Main/MainContentCommandActions+Structure.swift index e64c645fed..80643e9853 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions+Structure.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions+Structure.swift @@ -43,7 +43,7 @@ extension MainContentCommandActions { guard let tab = coordinator?.tabManager.selectedTab, tab.tabType == .table || tab.tabType == .query else { return nil } if let session = stagedStructureSession { return .structure(session) } - return hasDataWork ? .data : nil + return hasDataWork && coordinator?.isSavingSelectedTab != true ? .data : nil } private func discardDataWorkAfterConfirming() async { diff --git a/apple/TablePro/Views/Main/MainContentCommandActions+UndoState.swift b/apple/TablePro/Views/Main/MainContentCommandActions+UndoState.swift index b3f291f407..c74704fad4 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions+UndoState.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions+UndoState.swift @@ -54,6 +54,7 @@ extension MainContentCommandActions { if isUsersRolesTab { return coordinator?.usersRolesActions?.canUndo() ?? false } if isDiagramTab { return selectedDiagram?.canUndoStagedEdit ?? false } if showsStructureEditor { return structureHistory?.canUndo ?? false } + if coordinator?.isSavingSelectedTab == true { return false } if coordinator?.changeManager.hasCoalescedUndoRun == true { return true } return windowUndoManager?.canUndo ?? false } @@ -62,6 +63,7 @@ extension MainContentCommandActions { if isUsersRolesTab { return coordinator?.usersRolesActions?.canRedo() ?? false } if isDiagramTab { return selectedDiagram?.canRedoStagedEdit ?? false } if showsStructureEditor { return structureHistory?.canRedo ?? false } + if coordinator?.isSavingSelectedTab == true { return false } return windowUndoManager?.canRedo ?? false } @@ -106,6 +108,7 @@ extension MainContentCommandActions { structureHistory?.undo() return } + guard coordinator?.isSavingSelectedTab != true else { return } windowUndoManager?.undo() } @@ -123,6 +126,7 @@ extension MainContentCommandActions { structureHistory?.redo() return } + guard coordinator?.isSavingSelectedTab != true else { return } windowUndoManager?.redo() } } diff --git a/apple/TablePro/Views/Results/JSONEditorContentView.swift b/apple/TablePro/Views/Results/JSONEditorContentView.swift index 261f934501..b8540793c4 100644 --- a/apple/TablePro/Views/Results/JSONEditorContentView.swift +++ b/apple/TablePro/Views/Results/JSONEditorContentView.swift @@ -36,8 +36,8 @@ struct JSONEditorContentView: View { onDismiss: onDismiss, onCommit: { newValue in if newValue.isEmpty && initialValue == nil { return } - if newValue != JsonReindenter.normalize(initialValue ?? "") { - onCommit(newValue) + if JsonReindenter.normalize(newValue) != JsonReindenter.normalize(initialValue ?? "") { + onCommit(JsonReindenter.staged(newValue, keepingLayoutOf: initialValue)) } }, onPopOut: onPopOut diff --git a/apple/TablePro/Views/Results/JSONViewerView.swift b/apple/TablePro/Views/Results/JSONViewerView.swift index ea6bdf22c8..43cbdace66 100644 --- a/apple/TablePro/Views/Results/JSONViewerView.swift +++ b/apple/TablePro/Views/Results/JSONViewerView.swift @@ -244,8 +244,9 @@ internal struct JSONViewerView: View { commitAndClose(displayText) } + // Committed as shown: the caller knows the stored value, whose layout decides what is staged. private func commitAndClose(_ value: String) { - onCommit?(JsonReindenter.normalize(value)) + onCommit?(value) onDismiss?() } } diff --git a/apple/TablePro/Views/Results/JSONViewerWindowController.swift b/apple/TablePro/Views/Results/JSONViewerWindowController.swift index 4f7926ab6d..c2536380e6 100644 --- a/apple/TablePro/Views/Results/JSONViewerWindowController.swift +++ b/apple/TablePro/Views/Results/JSONViewerWindowController.swift @@ -55,7 +55,8 @@ internal final class JSONViewerWindowController: ValueViewerWindowController { /// Nil when Save would write back what is stored. A NULL baseline reads as empty text, so an /// empty Save leaves the cell NULL rather than writing an empty string over it. nonisolated static func valueToCommit(saved: String, baseline: String?) -> String? { - saved == JsonReindenter.normalize(baseline ?? "") ? nil : saved + guard JsonReindenter.normalize(saved) != JsonReindenter.normalize(baseline ?? "") else { return nil } + return JsonReindenter.staged(saved, keepingLayoutOf: baseline) } } diff --git a/apple/TableProTests/Core/ChangeTracking/RowEditUndoStepTests.swift b/apple/TableProTests/Core/ChangeTracking/RowEditUndoStepTests.swift new file mode 100644 index 0000000000..d49243c64e --- /dev/null +++ b/apple/TableProTests/Core/ChangeTracking/RowEditUndoStepTests.swift @@ -0,0 +1,115 @@ +// +// RowEditUndoStepTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +@MainActor +struct RowEditUndoStepTests { + private static let columns = ["code", "status"] + + private func makeManager() -> (DataChangeManager, UndoManager) { + let manager = DataChangeManager() + let undoManager = UndoManager() + undoManager.groupsByEvent = false + manager.undoManagerProvider = { undoManager } + manager.configureForTable( + tableName: "tickets", + columns: Self.columns, + primaryKeyColumns: ["code"], + databaseType: .postgresql, + generatedColumns: [] + ) + return (manager, undoManager) + } + + @Test("Undoing a cell typed back to its value stages the row as it was read") + func undoAfterTypingBackKeepsTheRowAsRead() throws { + let (manager, undoManager) = makeManager() + manager.recordCellChange( + rowID: .existing(0), columnIndex: 1, columnName: "status", + oldValue: "open", newValue: "closed", originalRow: ["A1", "open"] + ) + manager.recordCellChange( + rowID: .existing(0), columnIndex: 1, columnName: "status", + oldValue: "closed", newValue: "open", originalRow: ["A1", "closed"] + ) + #expect(manager.pendingUpdate(forRow: .existing(0)) == nil) + + undoManager.undo() + + let update = try #require(manager.pendingUpdate(forRow: .existing(0))) + #expect(update.originalRow == ["A1", "open"]) + #expect(update.cellChanges.first?.newValue == "closed") + } + + @Test("Undoing a key typed back keys the UPDATE on the key as read") + func undoAfterTypingTheKeyBackKeysOnTheReadKey() throws { + let (manager, undoManager) = makeManager() + manager.recordCellChange( + rowID: .existing(0), columnIndex: 0, columnName: "code", + oldValue: "A1", newValue: "A2", originalRow: ["A1", "open"] + ) + manager.recordCellChange( + rowID: .existing(0), columnIndex: 0, columnName: "code", + oldValue: "A2", newValue: "A1", originalRow: ["A2", "open"] + ) + + undoManager.undo() + + let update = try #require(manager.pendingUpdate(forRow: .existing(0))) + #expect(update.originalRow == ["A1", "open"]) + let statement = try #require(try manager.generateSQL().first) + let parameters = statement.parameters.compactMap { $0 as? String } + #expect(parameters == ["A2", "A1"]) + } + + @Test("Undo Delete from the row menu is its own step, so Undo deletes the row again and then restores it") + func restoringADeletedRowIsUndoable() { + let (manager, undoManager) = makeManager() + manager.recordRowDeletion(rowID: .existing(0), originalRow: ["A1", "open"]) + + manager.restoreDeletedRows([.existing(0)]) + #expect(!manager.isRowDeleted(.existing(0))) + #expect(undoManager.undoActionName == String(localized: "Restore Row")) + + undoManager.undo() + #expect(manager.isRowDeleted(.existing(0))) + #expect(undoManager.redoActionName == String(localized: "Restore Row")) + #expect(undoManager.undoActionName == String(localized: "Delete Row")) + + undoManager.undo() + #expect(!manager.isRowDeleted(.existing(0))) + #expect(!undoManager.canUndo) + #expect(!manager.hasChanges) + + undoManager.redo() + #expect(manager.isRowDeleted(.existing(0))) + undoManager.redo() + #expect(!manager.isRowDeleted(.existing(0))) + } + + @Test("Restoring several deleted rows is one step over all of them") + func restoringSeveralRowsIsOneStep() { + let (manager, undoManager) = makeManager() + manager.recordBatchRowDeletion(rows: [ + (rowID: .existing(0), originalRow: ["A1", "open"]), + (rowID: .existing(1), originalRow: ["A2", "open"]), + ]) + + manager.restoreDeletedRows([.existing(0), .existing(1)]) + #expect(manager.deletedRowIDs.isEmpty) + #expect(undoManager.undoActionName == String(localized: "Restore Rows")) + + undoManager.undo() + #expect(manager.deletedRowIDs == [.existing(0), .existing(1)]) + + undoManager.undo() + #expect(manager.deletedRowIDs.isEmpty) + #expect(!undoManager.canUndo) + } +} diff --git a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorBatchDeleteScaleTests.swift b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorBatchDeleteScaleTests.swift index 2c13f6db07..9621e1b4ed 100644 --- a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorBatchDeleteScaleTests.swift +++ b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorBatchDeleteScaleTests.swift @@ -76,6 +76,25 @@ struct SQLStatementGeneratorBatchDeleteScaleTests { #expect(statements.reduce(0) { $0 + $1.parameters.count } == cap + 1) } + @Test("Cloudflare D1 deletes in statements of at most 100 parameters") + func cloudflareD1ChunksAtItsLimit() throws { + let generator = try makeGenerator( + columns: ["id", "name"], primaryKeyColumns: ["id"], databaseType: .cloudflareD1 + ) + let changes = deleteChanges(count: 150, columns: ["id", "name"]) + + let statements = generator.generateStatements( + from: changes, + insertedRowData: [:], + deletedRowIDs: Set((0..<150).map(RowID.existing)), + insertedRowIDs: [] + ) + + #expect(statements.count == 2) + #expect(statements.allSatisfy { $0.parameters.count <= 100 }) + #expect(statements.reduce(0) { $0 + $1.parameters.count } == 150) + } + @Test("Primary-key selection exactly at the cap stays one statement") func testPKSelectionExactlyAtCapSingleStatement() throws { let generator = try makeGenerator( diff --git a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorImportTests.swift b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorImportTests.swift index 6945aab201..b08b19563b 100644 --- a/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorImportTests.swift +++ b/apple/TableProTests/Core/ChangeTracking/SQLStatementGeneratorImportTests.swift @@ -100,6 +100,9 @@ struct SQLStatementGeneratorImportTests { #expect(try makeGenerator(databaseType: .sqlite).maxBindParameters == 32_766) #expect(try makeGenerator(databaseType: .mssql).maxBindParameters == 2_100) #expect(try makeGenerator(databaseType: .mysql).maxBindParameters == 65_535) + #expect(try makeGenerator(databaseType: .libsql).maxBindParameters == 32_766) + #expect(try makeGenerator(databaseType: .turso).maxBindParameters == 32_766) + #expect(try makeGenerator(databaseType: .cloudflareD1).maxBindParameters == 100) } @Test("keyMatchCountStatement asks one count per key, tagged with its position") diff --git a/apple/TableProTests/Core/DataWrite/DataWriteExecutorLookupTests.swift b/apple/TableProTests/Core/DataWrite/DataWriteExecutorLookupTests.swift index cc8ad67a43..8815b387f0 100644 --- a/apple/TableProTests/Core/DataWrite/DataWriteExecutorLookupTests.swift +++ b/apple/TableProTests/Core/DataWrite/DataWriteExecutorLookupTests.swift @@ -97,6 +97,40 @@ struct DataWriteExecutorLookupTests { #expect(driver.trace.map { $0.query } == ["LOOKUP DELETE pinned", "LOOKUP DELETE pinned", "DELETE pinned", "CHECK DELETE pinned"]) } + private func heldStep(_ held: PluginCellValue) -> DataWriteStep { + DataWriteStep( + kind: .rowWrite, + statement: ParameterizedStatement(sql: "UPDATE held", parameters: ["a"]), + expectedRowCount: 1, + tableName: "events", + lookup: RowLookup( + query: ParameterizedStatement(sql: "LOOKUP UPDATE held", parameters: []), + rowCount: 1, + pinsRows: false, + heldValues: [0: held] + ) + ) + } + + @Test("A row that still holds the reviewed values is written") + func heldValuesThatStillMatchWrite() async throws { + let driver = LookupDriver(found: [1, 1], affected: 1) + + try await run([heldStep("part_0")], on: driver) + + #expect(driver.writes.map { $0.query } == ["UPDATE held"]) + } + + @Test("A row another writer changed since the review refuses the write before anything runs") + func changedHeldValueRefuses() async throws { + let driver = LookupDriver(found: [1, 1], affected: 1) + + await #expect(throws: DataWriteError.self) { + try await run([heldStep("reviewed")], on: driver) + } + #expect(driver.writes.isEmpty) + } + @Test("A row its lookup cannot tell apart from another refuses the save before anything is written") func identicalRowsRefuseBeforeAnyWrite() async { let driver = LookupDriver(found: [1, 2]) diff --git a/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift b/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift index 5234fead9b..188b3e6ec0 100644 --- a/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift +++ b/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift @@ -87,6 +87,7 @@ struct DataWriteExecutorTests { epilogue: [String] = [], matchesRowsWithoutKey: Bool = false, refusesSharedKey: Bool = false, + rowKind: RowWriteKind? = nil, databaseType: DatabaseType = .sqlite ) -> DataWritePlan { DataWritePlan( @@ -99,7 +100,8 @@ struct DataWriteExecutorTests { expectedRowCount: expectedRowCount, tableName: "t", matchesRowsWithoutKey: matchesRowsWithoutKey, - refusesSharedKey: refusesSharedKey + refusesSharedKey: refusesSharedKey, + rowKind: rowKind ) }, prologue: prologue, @@ -141,16 +143,74 @@ struct DataWriteExecutorTests { #expect(counting.didRollBack == false) } - @Test("Fewer rows than expected is an ordinary save, because MySQL reports zero for an unchanged value") + @Test("A keyed UPDATE short of its rows is an ordinary save on MySQL, which 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, rowKind: .update, databaseType: .mysql), + on: driver(counting) + ).results #expect(results.first?.rowsAffected == 0) #expect(counting.didCommit) #expect(counting.didRollBack == false) } + @Test("A keyed UPDATE that matched no row fails where the count is the rows matched") + func keyedUpdateThatMatchedNothingFails() async throws { + for databaseType in [DatabaseType.postgresql, .sqlite, .duckdb] { + let counting = CountingDriver(affectedRows: 0) + await #expect(throws: DataWriteError.self) { + try await DataWriteExecutor.run( + plan(expectedRowCount: 1, rowKind: .update, databaseType: databaseType), + on: driver(counting) + ) + } + #expect(counting.didRollBack) + #expect(counting.didCommit == false) + } + } + + @Test("A keyed UPDATE through a view keeps the lenient rule: its count comes from the view's trigger") + func keyedUpdateThroughAViewStaysLenient() async throws { + let counting = CountingDriver(affectedRows: 0) + var viewPlan = plan(expectedRowCount: 1, rowKind: .update, databaseType: .postgresql) + viewPlan.viewTarget = TestFixtures.viewTarget(keys: ["id"]) + + _ = try await DataWriteExecutor.run(viewPlan, on: driver(counting)) + + #expect(counting.didCommit) + } + + @Test("An UPDATE after a DELETE in the same save may find its row gone to the cascade, which is no failure") + func updateAfterADeleteStaysLenient() async throws { + let counting = CountingDriver(affectedRows: 0) + let update = plan(expectedRowCount: 1, rowKind: .update, databaseType: .postgresql) + let delete = DataWriteStep( + kind: .rowWrite, + statement: ParameterizedStatement(sql: "DELETE FROM \"t\" WHERE \"id\" = 1", parameters: []), + expectedRowCount: 1, + tableName: "t", + rowKind: .delete + ) + let cascading = DataWritePlan(scope: update.scope, databaseType: .postgresql, steps: [delete] + update.steps) + + _ = try await DataWriteExecutor.run(cascading, on: driver(counting)) + + #expect(counting.didCommit) + } + + @Test("A keyed DELETE that found its row already gone stays an ordinary save") + func keyedDeleteOfAGoneRowIsNotAFailure() async throws { + let counting = CountingDriver(affectedRows: 0) + _ = try await DataWriteExecutor.run( + plan(expectedRowCount: 1, rowKind: .delete, databaseType: .postgresql), + on: driver(counting) + ) + + #expect(counting.didCommit) + } + @Test("A keyless write that found no row is rolled back rather than reported as a save") func keylessWriteThatMatchedNothingFails() async throws { let counting = CountingDriver(affectedRows: 0) diff --git a/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift b/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift index b27435f4c6..dd88b9d3b4 100644 --- a/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift +++ b/apple/TableProTests/Core/DataWrite/RewindPlannerTests.swift @@ -134,6 +134,30 @@ struct RewindPlannerTests { #expect(plan.statements.count == 1) } + @Test("An edit's restore reads its row again inside the write and holds it to what the save left") + func restoredEditHoldsTheRowAsReviewed() throws { + let plan = try planner(operations: [ + operation(kind: .update, preImage: ["7", "Ada"], postImage: ["7", "Grace"], writtenColumns: ["name"]), + ]).plan(currentRows: [RewindCurrentRow(values: ["7", "Grace"])]) + + let lookup = try #require(plan.lookups.first ?? nil) + #expect(lookup.rowCount == 1) + #expect(lookup.heldValues == [1: "Grace"]) + #expect(lookup.query.sql.contains("\"id\"")) + #expect(lookup.query.sql.contains("'7'")) + } + + @Test("Removing an inserted row holds it to what the save wrote; restoring a deleted one does not read") + func insertAndDeleteRestoreLookups() throws { + let insert = operation(kind: .insert, preImage: nil, postImage: ["9", "Lin"], writtenColumns: columns) + let delete = operation(kind: .delete, preImage: ["7", "Ada"], postImage: nil, writtenColumns: columns) + let plan = try planner(operations: [insert, delete]).plan(currentRows: [RewindCurrentRow(values: ["9", "Lin"])]) + + #expect(plan.statements.count == 2) + let held = plan.lookups.compactMap { $0?.heldValues } + #expect(held == [[0: "9", 1: "Lin"]]) + } + @Test("A row someone else changed is left alone, and no statement is generated for it") func changedRowIsSkipped() throws { let plan = try planner(operations: [ diff --git a/apple/TableProTests/Core/SchemaTracking/StructureChangeRebaseTests.swift b/apple/TableProTests/Core/SchemaTracking/StructureChangeRebaseTests.swift index c847c5ec7d..4d35111448 100644 --- a/apple/TableProTests/Core/SchemaTracking/StructureChangeRebaseTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/StructureChangeRebaseTests.swift @@ -124,6 +124,139 @@ struct StructureChangeRebaseTests { #expect(shown == ["id", "a"]) } + private func stageDropAndReAdd(in manager: StructureChangeManager) throws { + try manager.deleteColumn(id: workingColumn("a", in: manager).id) + var readded = EditableColumnDefinition.from(textColumn("a")) + readded.id = UUID() + manager.addColumn(readded) + } + + private func textColumn(_ name: String) -> ColumnInfo { + ColumnInfo( + name: name, dataType: "TEXT", isNullable: true, isPrimaryKey: false, + defaultValue: nil, extra: nil, charset: nil, collation: nil, comment: nil + ) + } + + private func stagedColumnChanges(_ manager: StructureChangeManager) -> [String] { + manager.getChangesArray().compactMap { change -> String? in + switch change { + case .deleteColumn(let column): return "drop \(column.name) \(column.dataType)" + case .addColumn(let column): return "add \(column.name) \(column.dataType)" + case .modifyColumn(let old, let new): return "\(old.name)>\(new.name)" + default: return nil + } + }.sorted() + } + + @Test("A drop and an add of one name both stay staged when the save wrote nothing") + func dropAndReAddSurviveAnUnchangedTable() throws { + let manager = manager() + try stageDropAndReAdd(in: manager) + let before = manager.getChangesArray() + + let moved = manager.rebaseStagedChanges( + onto: StructureRebase.Baseline( + columns: ["id", "a", "x"].map { column($0) }, indexes: [], foreignKeys: [], primaryKey: ["id"] + ) + ) + + #expect(!moved) + #expect(manager.getChangesArray() == before) + } + + @Test("A drop that failed after a rename ran keeps the drop and the add of the same name") + func dropAndReAddSurviveAPartlyAppliedSave() throws { + let manager = manager() + var renamed = try workingColumn("x", in: manager) + renamed.name = "y" + manager.updateColumn(id: renamed.id, with: renamed) + try stageDropAndReAdd(in: manager) + + let moved = manager.rebaseStagedChanges( + onto: StructureRebase.Baseline( + columns: ["id", "a", "y"].map { column($0) }, indexes: [], foreignKeys: [], primaryKey: ["id"] + ) + ) + + #expect(moved) + #expect(stagedColumnChanges(manager) == ["add a TEXT", "drop a INTEGER"]) + } + + @Test("A drop and an add of one name that both ran leave nothing staged") + func dropAndReAddThatRanAreDone() throws { + let manager = manager() + var renamed = try workingColumn("x", in: manager) + renamed.name = "y" + manager.updateColumn(id: renamed.id, with: renamed) + try stageDropAndReAdd(in: manager) + + let moved = manager.rebaseStagedChanges( + onto: StructureRebase.Baseline( + columns: [column("id"), column("y"), textColumn("a")], indexes: [], foreignKeys: [], primaryKey: ["id"] + ) + ) + + #expect(moved) + #expect(stagedColumnChanges(manager).isEmpty) + } + + private func stageDropAndRenameInto(in manager: StructureChangeManager) throws { + try manager.deleteColumn(id: workingColumn("a", in: manager).id) + var renamed = try workingColumn("x", in: manager) + renamed.name = "a" + manager.updateColumn(id: renamed.id, with: renamed) + } + + @Test("A drop and a rename into the same name that both ran leave nothing staged, even with equal definitions") + func dropAndRenameIntoThatRanAreDone() throws { + let manager = manager() + try stageDropAndRenameInto(in: manager) + + let moved = manager.rebaseStagedChanges( + onto: StructureRebase.Baseline( + columns: ["id", "a"].map { column($0) }, indexes: [], foreignKeys: [], primaryKey: ["id"] + ) + ) + + #expect(moved) + #expect(stagedColumnChanges(manager).isEmpty) + } + + @Test("A drop and a rename into the same name keep both when the save wrote nothing") + func dropAndRenameIntoSurviveAnUnchangedTable() throws { + let manager = manager() + try stageDropAndRenameInto(in: manager) + let before = manager.getChangesArray() + + let moved = manager.rebaseStagedChanges( + onto: StructureRebase.Baseline( + columns: ["id", "a", "x"].map { column($0) }, indexes: [], foreignKeys: [], primaryKey: ["id"] + ) + ) + + #expect(!moved) + #expect(manager.getChangesArray() == before) + } + + @Test("A chain of renames through a dropped name that all ran leaves no drop on the renamed column") + func renameChainThatRanStagesNoDrop() throws { + let manager = manager() + try stageDropAndRenameInto(in: manager) + var added = EditableColumnDefinition.from(textColumn("x")) + added.id = UUID() + manager.addColumn(added) + + let moved = manager.rebaseStagedChanges( + onto: StructureRebase.Baseline( + columns: [column("id"), column("a"), textColumn("x")], indexes: [], foreignKeys: [], primaryKey: ["id"] + ) + ) + + #expect(moved) + #expect(!stagedColumnChanges(manager).contains { $0.hasPrefix("drop") }) + } + @Test("A rename that did not run is staged against the table as it is") func unappliedRenameStaysStaged() throws { let manager = manager() diff --git a/apple/TableProTests/Core/Services/RowOperationsFieldAbsenceTests.swift b/apple/TableProTests/Core/Services/RowOperationsFieldAbsenceTests.swift index 2823bea961..7de2740536 100644 --- a/apple/TableProTests/Core/Services/RowOperationsFieldAbsenceTests.swift +++ b/apple/TableProTests/Core/Services/RowOperationsFieldAbsenceTests.swift @@ -123,7 +123,7 @@ extension GlobalStateSuites { let (tableRows, result) = paste(clipboard, into: manager) - #expect(result.pastedRows.first?.values == [.columnDefault, "Ada", .null]) + #expect(result.pastedRows.first?.values == ["1", "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 51e4d37d67..b610767291 100644 --- a/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift +++ b/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift @@ -20,9 +20,10 @@ struct RowOperationsManagerPasteTests { return RowOperationsManager(changeManager: changeManager) } + // `id` is AUTO_INCREMENT, so the server fills it. private func emptyRows() -> TableRows { let types: [ColumnType] = Array(repeating: .text(rawType: nil), count: Self.columns.count) - return TableRows.from(queryRows: [], columns: Self.columns, columnTypes: types) + return TableRows.from(queryRows: [], columns: Self.columns, columnTypes: types, columnIdentity: ["id": .byDefault]) } private func pasteRows(_ clipboard: RecordingClipboard, manager: RowOperationsManager) -> [[PluginCellValue]] { @@ -204,6 +205,179 @@ struct RowOperationsManagerPasteTests { } } +// A key the server cannot fill keeps the pasted value: reset to DEFAULT it went out as NULL. +@MainActor +struct RowOperationsManagerPasteKeyTests { + private static let columns = ["sku", "name"] + + private func paste( + _ clipboard: RecordingClipboard, + databaseType: DatabaseType = .postgresql, + declaredTypes: [String: String] = [:], + defaults: [String: String?] = [:], + uuidKeyForms: [Int: UUIDCellForm] = [:] + ) -> [[PluginCellValue]] { + let changeManager = DataChangeManager() + changeManager.configureForTable( + tableName: "products", + columns: Self.columns, + primaryKeyColumns: ["sku"], + databaseType: databaseType, + generatedColumns: [] + ) + var rows = TableRows.from( + queryRows: [], + columns: Self.columns, + columnTypes: Array(repeating: .text(rawType: "VARCHAR"), count: Self.columns.count), + columnDefaults: defaults, + columnNullable: ["sku": false], + columnDeclaredTypes: declaredTypes + ) + let result = RowOperationsManager(changeManager: changeManager).pasteRowsFromClipboard( + columns: Self.columns, + primaryKeyColumns: ["sku"], + tableRows: &rows, + uuidKeyForms: uuidKeyForms, + clipboard: clipboard + ) + return result.pastedRows.map(\.values) + } + + @Test("Pasted text keeps a natural key") + func textKeepsNaturalKey() { + let clipboard = RecordingClipboard() + clipboard.text = "SKU-1\tLamp\nSKU-2\tDesk" + + let keys = paste(clipboard).map { $0.first } + + #expect(keys == [PluginCellValue.text("SKU-1"), PluginCellValue.text("SKU-2")]) + } + + @Test("Pasted CSV keeps a natural key") + func csvKeepsNaturalKey() { + let clipboard = RecordingClipboard() + clipboard.text = "SKU-1,Lamp" + + #expect(paste(clipboard).first?.first == .text("SKU-1")) + } + + @Test("Structured rows keep a natural key") + func structuredKeepsNaturalKey() { + let clipboard = RecordingClipboard() + clipboard.gridRows = GridRowsClipboardPayload(columns: Self.columns, rows: [[.text("SKU-1"), .text("Lamp")]]) + + #expect(paste(clipboard).first == [.text("SKU-1"), .text("Lamp")]) + } + + @Test("Copied cells keep a natural key") + func copiedCellsKeepNaturalKey() { + let clipboard = RecordingClipboard() + clipboard.writeCells(GridCellsClipboardPayload(text: "SKU-1\tLamp", fields: [["SKU-1", "Lamp"]])) + + #expect(paste(clipboard).first?.first == .text("SKU-1")) + } + + @Test("A SQLite INTEGER PRIMARY KEY is the rowid, so the server fills it") + func sqliteRowidKeyGoesToTheServer() { + let clipboard = RecordingClipboard() + clipboard.text = "7\tLamp" + + let rows = paste(clipboard, databaseType: .sqlite, declaredTypes: ["sku": "INTEGER"]) + + #expect(rows.first?.first == .columnDefault) + } + + @Test("A SQLite TEXT key is not the rowid and keeps the pasted value") + func sqliteTextKeyIsKept() { + let clipboard = RecordingClipboard() + clipboard.text = "SKU-1\tLamp" + + let rows = paste(clipboard, databaseType: .sqlite, declaredTypes: ["sku": "TEXT"]) + + #expect(rows.first?.first == .text("SKU-1")) + } + + @Test("A key whose default calls a function goes to the server; a constant default does not replace the pasted key") + func onlyAFunctionDefaultFillsTheKey() { + let clipboard = RecordingClipboard() + clipboard.text = "SKU-1\tLamp" + + #expect(paste(clipboard, defaults: ["sku": "gen_random_uuid()"]).first?.first == .columnDefault) + #expect(paste(clipboard, defaults: ["sku": "(newid())"]).first?.first == .columnDefault) + #expect(paste(clipboard, defaults: ["sku": "'X'::text"]).first?.first == .text("SKU-1")) + #expect(paste(clipboard, defaults: ["sku": "((1))"]).first?.first == .text("SKU-1")) + #expect(paste(clipboard, defaults: ["sku": "(NEXT VALUE FOR [sku_seq])"]).first?.first == .columnDefault) + #expect(paste(clipboard, defaults: ["sku": "\"SKU_SEQ\".\"NEXTVAL\""]).first?.first == .columnDefault) + #expect(paste(clipboard, defaults: ["sku": "now()"]).first?.first == .text("SKU-1")) + #expect(paste(clipboard, defaults: ["sku": "(getdate())"]).first?.first == .text("SKU-1")) + #expect(paste(clipboard, defaults: ["sku": "timezone('utc'::text, now())"]).first?.first == .text("SKU-1")) + } + + @Test("A blank key that refuses NULL stays NULL rather than an empty string") + func blankKeyIsNotEmptyText() { + let clipboard = RecordingClipboard() + clipboard.text = "\tLamp" + + #expect(paste(clipboard).first?.first == .null) + } + + @Test("A blank UUID key takes a fresh UUID and a pasted one is kept") + func uuidKeyFillsOnlyWhenBlank() throws { + let clipboard = RecordingClipboard() + clipboard.text = "\tLamp\n6f1c2b0e-3c1a-4d0e-9a7b-1f2e3d4c5b6a\tDesk" + + let rows = paste(clipboard, uuidKeyForms: [0: .hyphenated]) + + let generated = try #require(rows.first?.first?.asText) + #expect(UUID(uuidString: generated) != nil) + #expect(rows.last?.first == .text("6f1c2b0e-3c1a-4d0e-9a7b-1f2e3d4c5b6a")) + } + + @Test("CSV keeps the spaces inside a quoted field") + func csvKeepsQuotedSpaces() { + let clipboard = RecordingClipboard() + clipboard.text = "SKU-1,\" indented note \"" + + #expect(paste(clipboard).first?.last == .text(" indented note ")) + } +} + +@MainActor +struct RowOperationsManagerDuplicateRowsTests { + @Test("Duplicating several rows copies each of them in one undo step") + func duplicatesEverySelectedRow() { + let changeManager = DataChangeManager() + let undoManager = UndoManager() + undoManager.groupsByEvent = false + changeManager.undoManagerProvider = { undoManager } + changeManager.configureForTable( + tableName: "t", + columns: ["id", "name"], + primaryKeyColumns: ["id"], + databaseType: .postgresql, + generatedColumns: [] + ) + var rows = TableRows.from( + queryRows: [[.text("1"), .text("a")], [.text("2"), .text("b")], [.text("3"), .text("c")]], + columns: ["id", "name"], + columnTypes: [.text(rawType: nil), .text(rawType: nil)] + ) + + let result = RowOperationsManager(changeManager: changeManager) + .duplicateRows(sourceRowIndices: [0, 1, 2], tableRows: &rows) + + #expect(result.rows.count == 3) + #expect(result.rows.map { $0.values[1] } == [.text("a"), .text("b"), .text("c")]) + #expect(result.rows.allSatisfy { $0.values[0] == .columnDefault }) + #expect(rows.count == 6) + #expect(changeManager.insertedRowIDs.count == 3) + + undoManager.undo() + #expect(changeManager.insertedRowIDs.isEmpty) + #expect(!undoManager.canUndo) + } +} + /// A pasted row arrives whole, so it carries values for columns the server owns. Those never reach /// the cell-edit boundary that refuses them, and `SQLStatementGenerator` drops them without a word, /// so the grid showed a pasted identity value the row was never saved with. diff --git a/apple/TableProTests/Core/Services/RowPasteForeignTextTests.swift b/apple/TableProTests/Core/Services/RowPasteForeignTextTests.swift index e886b4cf83..d74fe0ddb7 100644 --- a/apple/TableProTests/Core/Services/RowPasteForeignTextTests.swift +++ b/apple/TableProTests/Core/Services/RowPasteForeignTextTests.swift @@ -22,16 +22,16 @@ struct RowPasteForeignTextTests { let rows = try parse("\tAnn\tann@x.com\n\tBob\tbob@x.com") #expect(rows == [ - [.columnDefault, .text("Ann"), .text("ann@x.com")], - [.columnDefault, .text("Bob"), .text("bob@x.com")] + [.null, .text("Ann"), .text("ann@x.com")], + [.null, .text("Bob"), .text("bob@x.com")] ]) } - @Test("An empty first cell reads as NULL when the table has no key to reset") - func leadingEmptyFieldIsNullWithoutAKey() throws { - let rows = try parse("\tAnn\tann@x.com", primaryKey: []) + @Test("The parser keeps a pasted key; only the paste knows whether the server fills it") + func parserKeepsTheKey() throws { + let rows = try parse("7\tAnn\tann@x.com") - #expect(rows == [[.null, .text("Ann"), .text("ann@x.com")]]) + #expect(rows == [[.text("7"), .text("Ann"), .text("ann@x.com")]]) } @Test("Trailing empty cells stay NULL and blank lines are not rows") @@ -67,7 +67,8 @@ struct RowPasteForeignTextTests { var rows = TableRows.from( queryRows: [], columns: Self.columns, - columnTypes: Array(repeating: .text(rawType: nil), count: Self.columns.count) + columnTypes: Array(repeating: .text(rawType: nil), count: Self.columns.count), + columnIdentity: ["id": .byDefault] ) let result = manager.pasteRowsFromClipboard( @@ -101,6 +102,7 @@ struct RowPasteForeignTextTests { ], columnDefaults: ["status": "'active'"], columnNullable: ["name": false, "status": false, "note": false, "born": false, "nickname": true], + columnIdentity: ["id": .byDefault], hasAuthoritativeSchema: true ) @@ -131,6 +133,7 @@ struct RowPasteForeignTextTests { columns: columns, columnTypes: [.integer(rawType: "INT"), .text(rawType: "VARCHAR")], columnDefaults: ["status": "'active'"], + columnIdentity: ["id": .byDefault], hasAuthoritativeSchema: true ) diff --git a/apple/TableProTests/Models/UI/MultiRowEditStateJsonTests.swift b/apple/TableProTests/Models/UI/MultiRowEditStateJsonTests.swift index fd2de9f139..72af1a5769 100644 --- a/apple/TableProTests/Models/UI/MultiRowEditStateJsonTests.swift +++ b/apple/TableProTests/Models/UI/MultiRowEditStateJsonTests.swift @@ -46,6 +46,17 @@ struct MultiRowEditStateJsonTests { #expect(state.fields[0].pendingValue == "{\"a\":2}") } + @Test("A value stored across lines keeps the typed lines; only a one-line value is compacted") + func formattedValueKeepsItsLayout() { + let stored = "{\n \"retries\": 3,\n \"host\": \"db\"\n}" + let typed = "{\n \"retries\": 5,\n \"host\": \"db\"\n}" + let state = makeState(value: stored, type: .text(rawType: "text"), column: "config") + + state.updateField(at: 0, value: typed) + + #expect(state.fields[0].pendingValue == typed) + } + @Test("Editing back to the original content clears the pending change") func jsonRevertClearsChange() { let original = "{\"a\":1}" diff --git a/apple/TableProTests/Views/Main/SaveAttributionTests.swift b/apple/TableProTests/Views/Main/SaveAttributionTests.swift index 57aea277e9..c46cdf7b6f 100644 --- a/apple/TableProTests/Views/Main/SaveAttributionTests.swift +++ b/apple/TableProTests/Views/Main/SaveAttributionTests.swift @@ -68,7 +68,7 @@ struct SaveAttributionTests { #expect(fixture.changeManager.hasChanges) #expect(fixture.tabManager.tabs.first { $0.id == other.id }?.execution.errorMessage == nil) // The save is over: a second one is not refused as already running. - #expect(fixture.coordinator.rowEditingCoordinator.beginSaveInFlight()) + #expect(fixture.coordinator.rowEditingCoordinator.beginSaveInFlight(tabId: nil)) fixture.coordinator.rowEditingCoordinator.endSaveInFlight() } diff --git a/apple/TableProTests/Views/Main/SaveInFlightEditGateTests.swift b/apple/TableProTests/Views/Main/SaveInFlightEditGateTests.swift new file mode 100644 index 0000000000..107cce1726 --- /dev/null +++ b/apple/TableProTests/Views/Main/SaveInFlightEditGateTests.swift @@ -0,0 +1,75 @@ +// +// SaveInFlightEditGateTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +// A landing save clears the tab's staged edits and reloads its rows, so an edit typed during the write was lost. +@MainActor +struct SaveInFlightEditGateTests { + private func makeCoordinator() throws -> MainContentCoordinator { + let coordinator = MainContentCoordinator( + connection: TestFixtures.makeConnection(database: "shop", type: .postgresql), + tabManager: QueryTabManager(), + changeManager: DataChangeManager(), + toolbarState: ConnectionToolbarState() + ) + coordinator.openTableTab(TableInfo(name: "orders", type: .table, rowCount: nil, schema: "public")) + let index = try #require(coordinator.tabManager.selectedTabIndex) + coordinator.tabManager.tabs[index].tableContext.isEditable = true + coordinator.tabManager.tabs[index].display.resultsViewMode = .data + coordinator.setActiveTableRows( + TableRows.from( + queryRows: [[.text("1"), .text("open")]], + columns: ["id", "status"], + columnTypes: [.text(rawType: nil), .text(rawType: nil)], + hasAuthoritativeSchema: true + ), + for: coordinator.tabManager.tabs[index].id + ) + coordinator.changeManager.configureForTable( + tableName: "orders", + columns: ["id", "status"], + primaryKeyColumns: ["id"], + databaseType: .postgresql, + generatedColumns: [] + ) + return coordinator + } + + @Test("The saving tab takes no edit until the save ends") + func savingTabIsReadOnly() throws { + let coordinator = try makeCoordinator() + defer { coordinator.teardown() } + let tabId = try #require(coordinator.tabManager.selectedTabId) + #expect(coordinator.canEditActiveResult) + + #expect(coordinator.rowEditingCoordinator.beginSaveInFlight(tabId: tabId)) + #expect(!coordinator.canEditActiveResult) + #expect(coordinator.isSavingSelectedTab) + coordinator.deleteRows([.existing(0)]) + coordinator.stageInspectorFieldEdit( + columnIndex: 1, value: .text("closed"), rowIDs: [.existing(0)], continuity: .discrete + ) + #expect(!coordinator.changeManager.hasChanges) + + coordinator.rowEditingCoordinator.endSaveInFlight() + #expect(coordinator.canEditActiveResult) + #expect(!coordinator.isSavingSelectedTab) + } + + @Test("A save running for another tab leaves this one editable") + func otherTabStaysEditable() throws { + let coordinator = try makeCoordinator() + defer { coordinator.teardown() } + + #expect(coordinator.rowEditingCoordinator.beginSaveInFlight(tabId: UUID())) + #expect(coordinator.canEditActiveResult) + #expect(!coordinator.isSavingSelectedTab) + coordinator.rowEditingCoordinator.endSaveInFlight() + } +} diff --git a/apple/TableProTests/Views/Results/JSONViewerWindowCommitTests.swift b/apple/TableProTests/Views/Results/JSONViewerWindowCommitTests.swift index d5a4ab4112..9db90b2b44 100644 --- a/apple/TableProTests/Views/Results/JSONViewerWindowCommitTests.swift +++ b/apple/TableProTests/Views/Results/JSONViewerWindowCommitTests.swift @@ -15,7 +15,7 @@ struct JSONViewerWindowCommitTests { private let edited = #"{"qty":25,"sku":"A-1"}"# private func commit(saving displayText: String, baseline: String?) -> String? { - JSONViewerWindowController.valueToCommit(saved: JsonReindenter.normalize(displayText), baseline: baseline) + JSONViewerWindowController.valueToCommit(saved: displayText, baseline: baseline) } @Test("Save commits an edit the popover made before the window opened") @@ -60,6 +60,16 @@ struct JSONViewerWindowCommitTests { #expect(commit(saving: "{}", baseline: nil) == "{}") } + @Test("A document stored across lines keeps its lines when edited, one stored on one line stays compact") + func editKeepsTheStoredLayout() { + let formatted = JsonReindenter.reindent(stored) + let windowText = JsonReindenter.reindent(edited) + + #expect(commit(saving: windowText, baseline: formatted) == windowText) + #expect(commit(saving: windowText, baseline: stored) == edited) + #expect(commit(saving: windowText, baseline: stored + "\n") == edited) + } + @Test("text that is not JSON is compared as written") func invalidTextIsComparedAsWritten() { #expect(commit(saving: "{oops", baseline: "{oops") == nil) From 09f4206b0291c8e9321eba8dd8f3781f7860e964 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 09:57:00 +0700 Subject: [PATCH 2/3] chore(changelog): add the entries for the edits lane fixes --- apple/changelog.d/4226.fixed.1.md | 1 + apple/changelog.d/4226.fixed.10.md | 1 + apple/changelog.d/4226.fixed.2.md | 1 + apple/changelog.d/4226.fixed.3.md | 1 + apple/changelog.d/4226.fixed.4.md | 1 + apple/changelog.d/4226.fixed.5.md | 1 + apple/changelog.d/4226.fixed.6.md | 1 + apple/changelog.d/4226.fixed.7.md | 1 + apple/changelog.d/4226.fixed.8.md | 1 + apple/changelog.d/4226.fixed.9.md | 1 + apple/changelog.d/4226.fixed.md | 1 + 11 files changed, 11 insertions(+) create mode 100644 apple/changelog.d/4226.fixed.1.md create mode 100644 apple/changelog.d/4226.fixed.10.md create mode 100644 apple/changelog.d/4226.fixed.2.md create mode 100644 apple/changelog.d/4226.fixed.3.md create mode 100644 apple/changelog.d/4226.fixed.4.md create mode 100644 apple/changelog.d/4226.fixed.5.md create mode 100644 apple/changelog.d/4226.fixed.6.md create mode 100644 apple/changelog.d/4226.fixed.7.md create mode 100644 apple/changelog.d/4226.fixed.8.md create mode 100644 apple/changelog.d/4226.fixed.9.md create mode 100644 apple/changelog.d/4226.fixed.md diff --git a/apple/changelog.d/4226.fixed.1.md b/apple/changelog.d/4226.fixed.1.md new file mode 100644 index 0000000000..cd7116c646 --- /dev/null +++ b/apple/changelog.d/4226.fixed.1.md @@ -0,0 +1 @@ +A save reported success for an edited row that was deleted elsewhere or hidden by a row-level security policy. diff --git a/apple/changelog.d/4226.fixed.10.md b/apple/changelog.d/4226.fixed.10.md new file mode 100644 index 0000000000..8b2a46888c --- /dev/null +++ b/apple/changelog.d/4226.fixed.10.md @@ -0,0 +1 @@ +A structure save that stopped part way lost a staged drop and re-add of a column with the same name. diff --git a/apple/changelog.d/4226.fixed.2.md b/apple/changelog.d/4226.fixed.2.md new file mode 100644 index 0000000000..7d54755eac --- /dev/null +++ b/apple/changelog.d/4226.fixed.2.md @@ -0,0 +1 @@ +Undoing a cell typed back to its value kept Restore Previous Values from bringing the old value back. diff --git a/apple/changelog.d/4226.fixed.3.md b/apple/changelog.d/4226.fixed.3.md new file mode 100644 index 0000000000..b5c1c10e16 --- /dev/null +++ b/apple/changelog.d/4226.fixed.3.md @@ -0,0 +1 @@ +Cmd+Z after Undo Delete on the row menu deleted the row again. diff --git a/apple/changelog.d/4226.fixed.4.md b/apple/changelog.d/4226.fixed.4.md new file mode 100644 index 0000000000..5f1a83ef1f --- /dev/null +++ b/apple/changelog.d/4226.fixed.4.md @@ -0,0 +1 @@ +Restore Previous Values overwrote changes made to a row after the restore was reviewed. diff --git a/apple/changelog.d/4226.fixed.5.md b/apple/changelog.d/4226.fixed.5.md new file mode 100644 index 0000000000..fe84392d51 --- /dev/null +++ b/apple/changelog.d/4226.fixed.5.md @@ -0,0 +1 @@ +Deleting more than 100 rows in one save failed part way on Cloudflare D1. diff --git a/apple/changelog.d/4226.fixed.6.md b/apple/changelog.d/4226.fixed.6.md new file mode 100644 index 0000000000..3afec1a872 --- /dev/null +++ b/apple/changelog.d/4226.fixed.6.md @@ -0,0 +1 @@ +Pasting rows replaced every pasted primary key with DEFAULT, even a key the server cannot fill. diff --git a/apple/changelog.d/4226.fixed.7.md b/apple/changelog.d/4226.fixed.7.md new file mode 100644 index 0000000000..2676f21e4a --- /dev/null +++ b/apple/changelog.d/4226.fixed.7.md @@ -0,0 +1 @@ +Editing a formatted JSON value in the row inspector or the JSON editor saved it on one line. diff --git a/apple/changelog.d/4226.fixed.8.md b/apple/changelog.d/4226.fixed.8.md new file mode 100644 index 0000000000..8768e3bcd4 --- /dev/null +++ b/apple/changelog.d/4226.fixed.8.md @@ -0,0 +1 @@ +Duplicate on the row menu copied only the first of the selected rows. diff --git a/apple/changelog.d/4226.fixed.9.md b/apple/changelog.d/4226.fixed.9.md new file mode 100644 index 0000000000..0806d5f7df --- /dev/null +++ b/apple/changelog.d/4226.fixed.9.md @@ -0,0 +1 @@ +Pasting CSV removed the spaces inside quoted fields. diff --git a/apple/changelog.d/4226.fixed.md b/apple/changelog.d/4226.fixed.md new file mode 100644 index 0000000000..c025502181 --- /dev/null +++ b/apple/changelog.d/4226.fixed.md @@ -0,0 +1 @@ +Edits typed while a save was running were wiped when the save finished. From 1cc40c8324c296c18cb0e2b11b675e297d4a0fe0 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Thu, 12 Nov 2026 15:15:24 +0700 Subject: [PATCH 3/3] fix(data-grid): give a pasted UUID key a fresh UUID again --- .../Core/Services/Query/RowOperationsManager.swift | 8 +++----- .../Services/RowOperationsManagerPasteTests.swift | 11 ++++++----- .../Services/RowOperationsManagerUUIDKeyTests.swift | 8 +++++--- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/apple/TablePro/Core/Services/Query/RowOperationsManager.swift b/apple/TablePro/Core/Services/Query/RowOperationsManager.swift index 4713a5b7cd..3058d90185 100644 --- a/apple/TablePro/Core/Services/Query/RowOperationsManager.swift +++ b/apple/TablePro/Core/Services/Query/RowOperationsManager.swift @@ -594,8 +594,8 @@ final class RowOperationsManager { return containsComma ? CSVRowParser() : TSVRowParser() } - // A key the server fills goes back to its default, a natural key keeps the pasted value, and an unfilled UUID key - // takes a fresh UUID per row. + // A key the server fills goes back to its default, a UUID key it does not fill takes a fresh UUID per row, and any + // other key keeps the pasted value. private func insertParsedRows( _ parsedRows: [ParsedRow], primaryKeyColumns: [String], @@ -623,9 +623,7 @@ final class RowOperationsManager { absentColumns.remove(index) expressionColumns.insert(index) } - for (index, form) in uuidKeyForms where index < rowValues.count - && (rowValues[index].isNull - || rowValues[index].isColumnDefault(isExpression: expressionColumns.contains(index))) { + for (index, form) in uuidKeyForms where index < rowValues.count { rowValues[index] = Self.freshUUID(form) absentColumns.remove(index) expressionColumns.remove(index) diff --git a/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift b/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift index b610767291..8c9c369b57 100644 --- a/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift +++ b/apple/TableProTests/Core/Services/RowOperationsManagerPasteTests.swift @@ -321,16 +321,17 @@ struct RowOperationsManagerPasteKeyTests { #expect(paste(clipboard).first?.first == .null) } - @Test("A blank UUID key takes a fresh UUID and a pasted one is kept") - func uuidKeyFillsOnlyWhenBlank() throws { + @Test("A UUID key the server does not fill takes a fresh UUID on every pasted row") + func uuidKeyTakesAFreshUUID() { let clipboard = RecordingClipboard() clipboard.text = "\tLamp\n6f1c2b0e-3c1a-4d0e-9a7b-1f2e3d4c5b6a\tDesk" let rows = paste(clipboard, uuidKeyForms: [0: .hyphenated]) - let generated = try #require(rows.first?.first?.asText) - #expect(UUID(uuidString: generated) != nil) - #expect(rows.last?.first == .text("6f1c2b0e-3c1a-4d0e-9a7b-1f2e3d4c5b6a")) + let keys = rows.compactMap { $0.first?.asText } + #expect(keys.count == 2) + #expect(keys.allSatisfy { UUID(uuidString: $0) != nil }) + #expect(!keys.contains("6f1c2b0e-3c1a-4d0e-9a7b-1f2e3d4c5b6a")) } @Test("CSV keeps the spaces inside a quoted field") diff --git a/apple/TableProTests/Core/Services/RowOperationsManagerUUIDKeyTests.swift b/apple/TableProTests/Core/Services/RowOperationsManagerUUIDKeyTests.swift index 4122e13d78..bfcfd74caf 100644 --- a/apple/TableProTests/Core/Services/RowOperationsManagerUUIDKeyTests.swift +++ b/apple/TableProTests/Core/Services/RowOperationsManagerUUIDKeyTests.swift @@ -176,8 +176,8 @@ struct RowOperationsManagerUUIDKeyTests { #expect(key != "0b1e7c9a-58f4-4b8e-9c55-1f1c86f0a001") } - @Test("Pasted rows on a key that is not a UUID key still hand it to the server") - func pasteWithoutUUIDKeysKeepsDefault() { + @Test("A pasted key that is neither a UUID key nor filled by the server keeps its value") + func pasteWithoutUUIDKeysKeepsTheKey() { let (manager, _) = makeManager() var rows = makeRows() let clipboard = RecordingClipboard() @@ -190,7 +190,7 @@ struct RowOperationsManagerUUIDKeyTests { clipboard: clipboard ) - #expect(result.pastedRows.first?.values[0] == .columnDefault) + #expect(result.pastedRows.first?.values[0] == .text("k1")) } } @@ -212,11 +212,13 @@ extension GlobalStateSuites { return (RowOperationsManager(changeManager: changeManager), changeManager) } + // `id` is an identity, so a pasted row hands it back to the server. private func makeRows(_ queryRows: [[PluginCellValue]] = []) -> TableRows { TableRows.from( queryRows: queryRows, columns: Self.columns, columnTypes: [.text(rawType: "text"), .text(rawType: "text"), .timestamp(rawType: "timestamptz")], + columnIdentity: ["id": .byDefault], hasAuthoritativeSchema: true ) }