diff --git a/.claude/rules/data-sync-storage.md b/.claude/rules/data-sync-storage.md index c38348fc51..1df20d4f16 100644 --- a/.claude/rules/data-sync-storage.md +++ b/.claude/rules/data-sync-storage.md @@ -159,6 +159,7 @@ This path holds user data. A silent wrong answer here looks exactly like an empt | Light and dark theme ids (iCloud-synced, category `appearance`) | UserDefaults | `AppearanceSettings` through `AppSettingsManager` | | User themes (iCloud-synced, one `AppSettings` record `theme.` each) | JSON, one file per theme | `ThemeCatalog` (sync rules: `.claude/rules/theme.md`, Sync) | | Query history | SQLite FTS5 | `QueryHistoryStorage` | +| Query tab edit history: up to 50 versions per tab, kept 30 days or the shorter Query History window, after the tab closes too; purged with the connection and by Clear History | SQLite `query_tab_versions.db`, device-local, never synced | `QueryTabVersionStorage` | | Tab state | JSON | `TabPersistenceCoordinator`, `TabDiskActor` | | Filter defaults and presets | UserDefaults | `FilterSettingsStorage`, `FilterPresetStorage` | | Per-table filters | JSON, one file per table | `FilterSettingsStorage` | diff --git a/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/ConnectionFieldTrust.swift b/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/ConnectionFieldTrust.swift index b99565d3d9..dbb106f145 100644 --- a/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/ConnectionFieldTrust.swift +++ b/apple/Packages/TableProCore/Sources/TableProConnectionLibrary/ConnectionFieldTrust.swift @@ -130,6 +130,7 @@ public enum ConnectionFieldCatalog { "bqMaxBytesBilled": .setting, "cassandraConsistency": .setting, "cassandraSerialConsistency": .setting, + "commitMode": .setting, "completionIdentifierCase": .setting, "connectionOptions": .setting, "connectTimeoutSeconds": .setting, diff --git a/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLObjectQueries.swift b/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLObjectQueries.swift index 5932916173..b174813116 100644 --- a/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLObjectQueries.swift +++ b/apple/Packages/TableProCore/Sources/TableProMySQLCore/MySQLObjectQueries.swift @@ -256,6 +256,23 @@ public enum MySQLObjectQueries { """ } + // The sidebar's read, apart from export's `eventList`, which keeps the four columns every server answers. + // A name narrows it to the one event a source tab reads again. + public static func eventDetails(schema: String, name: String? = nil, exactSchema: Bool) -> String { + let namePredicate = name.map { "\n AND EVENT_NAME = '\(escapeLiteral($0))'" } ?? "" + return """ + SELECT EVENT_NAME, EVENT_TYPE, STATUS, EVENT_SCHEMA, + EXECUTE_AT, INTERVAL_VALUE, INTERVAL_FIELD, STARTS, ENDS, + ON_COMPLETION, LAST_EXECUTED, DEFINER, EVENT_COMMENT + FROM information_schema.EVENTS + WHERE \(schemaMatch("EVENT_SCHEMA", schema, exact: exactSchema))\(namePredicate) + ORDER BY EVENT_NAME + """ + } + + // Its own statement, so a server without the variable loses that one attribute rather than the list. + public static let eventScheduler = "SELECT @@global.event_scheduler" + /// Qualified with the schema. Unqualified, the server resolves the name against the session /// database instead of the one being browsed, and returns a different routine's body or none. public static func routineDefinition(kind: String, schema: String?, name: String) -> String { diff --git a/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCapabilities.swift b/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCapabilities.swift index 5e38c4ccb6..8fd987c88d 100644 --- a/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCapabilities.swift +++ b/apple/Packages/TableProCore/Sources/TableProPostgreSQLCore/PostgreSQLCapabilities.swift @@ -30,6 +30,8 @@ nonisolated public struct PostgreSQLCapabilities: Sendable, Equatable { /// Every range gained a companion multirange in 14, with a name the creator may choose. public var hasMultirangeTypes: Bool { serverVersion >= 140_000 } public var hasForeignTablesCatalog: Bool { serverVersion >= 90_100 } + // `pg_extension` and CREATE EXTENSION landed in 9.1. + public var hasExtensionsCatalog: Bool { serverVersion >= 90_100 } public var hasSequencesCatalog: Bool { serverVersion >= 100_000 } public var hasBypassRLS: Bool { serverVersion >= 90_500 } public var hasProcedureKind: Bool { serverVersion >= 110_000 } diff --git a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift index 2e44afde28..5b233db5ff 100644 --- a/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift +++ b/apple/Packages/TableProCore/Sources/TableProSQLGrammar/SQLCatalogEffect.swift @@ -13,8 +13,10 @@ public struct SQLCatalogObjectKinds: OptionSet, Sendable, Hashable { public static let types = SQLCatalogObjectKinds(rawValue: 1 << 3) public static let schemas = SQLCatalogObjectKinds(rawValue: 1 << 4) public static let databases = SQLCatalogObjectKinds(rawValue: 1 << 5) + // The kinds a driver declares for the sidebar, such as MySQL events and PostgreSQL extensions. + public static let serverObjects = SQLCatalogObjectKinds(rawValue: 1 << 6) - public static let objects: SQLCatalogObjectKinds = [.tables, .routines, .triggers, .types] + public static let objects: SQLCatalogObjectKinds = [.tables, .routines, .triggers, .types, .serverObjects] public static let everything: SQLCatalogObjectKinds = [.objects, .schemas, .databases] } @@ -200,7 +202,7 @@ public enum SQLCatalogEffectClassifier { "SYNONYM": .tables, "FIELD": .tables, "FUNCTION": .routines, "PROCEDURE": .routines, "PROC": .routines, "ROUTINE": .routines, "AGGREGATE": .routines, "MACRO": .routines, "PACKAGE": .routines, "OPERATOR": .routines, - "TRIGGER": .triggers, "EVENT": .triggers, + "TRIGGER": .triggers, "EVENT": [.triggers, .serverObjects], "TYPE": .types, "DOMAIN": .types, "ENUM": .types, "SCHEMA": [.schemas, .objects], "NAMESPACE": [.schemas, .objects], "OWNED": [.schemas, .objects], "DATABASE": .everything, "KEYSPACE": .everything, "CATALOG": .everything, diff --git a/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLObjectQueryTests.swift b/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLObjectQueryTests.swift index 8775894a00..475c977295 100644 --- a/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLObjectQueryTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProMySQLCoreTests/MySQLObjectQueryTests.swift @@ -303,6 +303,48 @@ struct MySQLObjectQueryTests { #expect(sql.hasSuffix("ORDER BY EVENT_NAME")) } + private func selectedColumns(_ sql: String) throws -> [String] { + let select = try #require(sql.components(separatedBy: "FROM").first) + return select + .replacingOccurrences(of: "SELECT", with: "") + .split(separator: ",") + .map { $0.trimmingCharacters(in: .whitespacesAndNewlines) } + } + + // Export reads these four by position on every flavor, including ones the sidebar leaves out. + @Test("Export's event list keeps the four columns it shipped with") + func eventListKeepsItsShippedProjection() throws { + let sql = MySQLObjectQueries.eventList(schema: "app", exactSchema: false) + #expect(try selectedColumns(sql) == ["EVENT_NAME", "EVENT_TYPE", "STATUS", "EVENT_SCHEMA"]) + #expect(!sql.contains("@@")) + } + + @Test("The sidebar's event read starts with export's columns and appends the schedule columns in order") + func eventDetailsAppendsScheduleColumns() throws { + let sql = MySQLObjectQueries.eventDetails(schema: "app", exactSchema: true) + #expect(try selectedColumns(sql) == [ + "EVENT_NAME", "EVENT_TYPE", "STATUS", "EVENT_SCHEMA", + "EXECUTE_AT", "INTERVAL_VALUE", "INTERVAL_FIELD", "STARTS", "ENDS", + "ON_COMPLETION", "LAST_EXECUTED", "DEFINER", "EVENT_COMMENT", + ]) + #expect(sql.contains("EVENT_SCHEMA = 'app' AND CAST(EVENT_SCHEMA AS BINARY) = CAST('app' AS BINARY)")) + #expect(!sql.contains("EVENT_NAME =")) + #expect(!sql.contains("@@")) + #expect(sql.hasSuffix("ORDER BY EVENT_NAME")) + } + + @Test("A re-read of one event names it as an escaped literal") + func eventDetailsForOneEvent() { + let sql = MySQLObjectQueries.eventDetails(schema: "app", name: "it's", exactSchema: false) + #expect(sql.contains("AND EVENT_NAME = 'it''s'")) + #expect(sql.contains("EVENT_SCHEMA = 'app'")) + } + + @Test("The event scheduler is read in a statement of its own") + func eventSchedulerIsItsOwnStatement() { + #expect(MySQLObjectQueries.eventScheduler == "SELECT @@global.event_scheduler") + } + @Test("A routine's recorded mode is read by schema, name and type, with the name selected for an exact match") func routineCreationSettings() { let sql = MySQLObjectQueries.routineCreationSettings( diff --git a/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginServerObjectDefaultsTests.swift b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginServerObjectDefaultsTests.swift new file mode 100644 index 0000000000..9be7b2e5b9 --- /dev/null +++ b/apple/Packages/TableProCore/Tests/TableProPluginKitTests/PluginServerObjectDefaultsTests.swift @@ -0,0 +1,97 @@ +import Foundation +import TableProPluginKit +import Testing + +private final class StubServerObjectDriver: PluginDatabaseDriver, @unchecked Sendable { + var supportsSchemas: Bool { false } + var supportsTransactions: Bool { false } + var currentSchema: String? { nil } + var serverVersion: String? { nil } + + func connect() async throws {} + func disconnect() {} + func ping() async throws {} + func execute(query: String) async throws -> PluginQueryResult { + PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) + } + + func fetchTables(schema: String?) async throws -> [PluginTableInfo] { [] } + func fetchColumns(table: String, schema: String?) async throws -> [PluginColumnInfo] { [] } + func fetchIndexes(table: String, schema: String?) async throws -> [PluginIndexInfo] { [] } + func fetchForeignKeys(table: String, schema: String?) async throws -> [PluginForeignKeyInfo] { [] } + func fetchTableDDL(table: String, schema: String?) async throws -> String { "" } + func fetchViewDefinition(view: String, schema: String?) async throws -> String { "" } + func fetchTableMetadata(table: String, schema: String?) async throws -> PluginTableMetadata { + PluginTableMetadata(tableName: table) + } + + func fetchDatabases() async throws -> [String] { [] } + func fetchDatabaseMetadata(_ database: String) async throws -> PluginDatabaseMetadata { + PluginDatabaseMetadata(name: database) + } +} + +struct PluginServerObjectDefaultsTests { + @Test("A driver that implements nothing declares no kinds and lists no objects") + func defaultsDeclareNothing() async throws { + let driver = StubServerObjectDriver() + #expect(driver.serverObjectKinds.isEmpty) + #expect(try await driver.fetchServerObjects(ofKind: "event", schema: "app").isEmpty) + #expect(try await driver.fetchServerObjects(ofKind: "extension", schema: nil).isEmpty) + } + + @Test("Reading an object again returns it when the listing carried its definition") + func rereadKeepsListedDefinition() async throws { + let listed = PluginServerObjectInfo(kindId: "event", name: "nightly", definition: "CREATE EVENT nightly") + let read = try await StubServerObjectDriver().fetchServerObject(listed) + #expect(read.name == "nightly") + #expect(read.definition == "CREATE EVENT nightly") + } + + @Test("Reading an object again without a definition is unsupported, not missing") + func rereadWithoutDefinitionIsUnsupported() async { + let listed = PluginServerObjectInfo(kindId: "event", name: "nightly", definition: "") + var refusal: Error? + do { + _ = try await StubServerObjectDriver().fetchServerObject(listed) + } catch { + refusal = error + } + guard let sourceError = refusal as? PluginObjectSourceError, case .unsupported(let name) = sourceError else { + Issue.record("expected unsupported, got \(String(describing: refusal))") + return + } + #expect(name == "nightly") + } + + @Test("A kind is listed per container unless it says otherwise") + func kindScopeDefaultsToContainer() { + let kind = PluginServerObjectKind(id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar") + #expect(kind.scope == .container) + } + + @Test("Kinds and objects round-trip through Codable") + func codableRoundTrip() throws { + let kind = PluginServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", + iconName: "puzzlepiece.extension", scope: .database + ) + let decodedKind = try JSONDecoder().decode(PluginServerObjectKind.self, from: JSONEncoder().encode(kind)) + #expect(decodedKind == kind) + + let object = PluginServerObjectInfo( + kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384", detail: "1.3", + isEnabled: true, definition: "CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;", + attributes: [PluginObjectAttribute(label: "Version", value: "1.3")] + ) + let decoded = try JSONDecoder().decode(PluginServerObjectInfo.self, from: JSONEncoder().encode(object)) + #expect(decoded.kindId == object.kindId) + #expect(decoded.name == object.name) + #expect(decoded.schema == object.schema) + #expect(decoded.identity == object.identity) + #expect(decoded.detail == object.detail) + #expect(decoded.isEnabled == object.isEnabled) + #expect(decoded.definition == object.definition) + #expect(decoded.attributes == object.attributes) + } +} diff --git a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift index 1e65ebbaf7..25e49d5127 100644 --- a/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift +++ b/apple/Packages/TableProCore/Tests/TableProSQLGrammarTests/SQLCatalogEffectClassifierTests.swift @@ -21,6 +21,17 @@ struct SQLCatalogEffectClassifierTests { #expect(kinds("CREATE TABLE copy AS SELECT * FROM t") == .tables) } + @Test("An event changes the triggers and the server objects, an extension reaches the server objects") + func serverObjects() { + #expect(kinds("CREATE EVENT nightly ON SCHEDULE EVERY 1 DAY DO DELETE FROM log", "MySQL") == [.triggers, .serverObjects]) + #expect(kinds("ALTER EVENT nightly DISABLE", "MySQL") == [.triggers, .serverObjects]) + #expect(kinds("DROP EVENT IF EXISTS nightly", "MySQL") == [.triggers, .serverObjects]) + #expect(kinds("CREATE EXTENSION IF NOT EXISTS pgcrypto").contains(.serverObjects)) + #expect(kinds("DROP EXTENSION pgcrypto").contains(.serverObjects)) + #expect(SQLCatalogObjectKinds.objects.contains(.serverObjects)) + #expect(!kinds("CREATE TABLE t (id int)").contains(.serverObjects)) + } + @Test("Row work and reads leave the catalog alone, except SELECT INTO") func rowsAndReads() { #expect(kinds("INSERT INTO t VALUES (1)").isEmpty) diff --git a/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+UnfilteredEdits.swift b/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+UnfilteredEdits.swift index 933c02074b..0b13653b05 100644 --- a/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+UnfilteredEdits.swift +++ b/apple/Packages/TableProEditor/Sources/TableProEditorKit/Controller/TextViewController+UnfilteredEdits.swift @@ -26,4 +26,19 @@ public extension TextViewController { defer { isApplyingUnfilteredEdits = false } textView.replaceCharacters(in: span, with: rebuilt, skipUpdateSelection: true) } + + // A command's replacement, unlike `setText`, keeps the undo history: one undo group, no typing filters, no + // suggestion triggers. The binding gets the text before this returns, so a run right after reads it. + @discardableResult + func replaceTextAsOneEdit(in range: NSRange, with string: String) -> Bool { + guard let textView, textView.isEditable, NSMaxRange(range) <= textView.textStorage.length else { return false } + guard !range.isEmpty || !string.isEmpty else { return true } + textView.undoManager?.beginUndoGrouping() + isApplyingUnfilteredEdits = true + textView.replaceCharacters(in: range, with: string) + isApplyingUnfilteredEdits = false + textView.undoManager?.endUndoGrouping() + flushPendingTextWriteback() + return true + } } diff --git a/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/ReplaceTextAsOneEditTests.swift b/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/ReplaceTextAsOneEditTests.swift new file mode 100644 index 0000000000..54718ec320 --- /dev/null +++ b/apple/Packages/TableProEditor/Tests/TableProEditorKitTests/Controller/ReplaceTextAsOneEditTests.swift @@ -0,0 +1,49 @@ +import AppKit +@testable import TableProEditorKit +import TableProTextEngine +import Testing + +@MainActor +struct ReplaceTextAsOneEditTests { + @Test("A command's replacement keeps the undo history and undoes in one step") + func replacementIsUndoable() { + let (window, controller) = Mock.focusedTextViewController(string: "SELECT 1") + defer { window.orderOut(nil) } + let whole = NSRange(location: 0, length: controller.textView.textStorage.length) + + #expect(controller.replaceTextAsOneEdit(in: whole, with: "SELECT * FROM users")) + #expect(controller.textView.string == "SELECT * FROM users") + #expect(controller.textView.undoManager?.canUndo == true) + + controller.textView.undoManager?.undo() + #expect(controller.textView.string == "SELECT 1") + } + + @Test("A command's replacement skips the typing filters a typed edit runs") + func replacementSkipsTypingFilters() { + let typed = Mock.loadedTextViewController(string: "") + typed.textView.replaceCharacters(in: NSRange(location: 0, length: 0), with: "\t") + #expect(typed.textView.string == " ") + + let controller = Mock.loadedTextViewController(string: "") + #expect(controller.replaceTextAsOneEdit(in: NSRange(location: 0, length: 0), with: "\t")) + #expect(controller.textView.string == "\t") + } + + @Test("An editor that takes no edits refuses, so the caller writes the text itself") + func uneditableEditorRefuses() { + let controller = Mock.loadedTextViewController(string: "SELECT 1") + controller.textView.isEditable = false + + #expect(!controller.replaceTextAsOneEdit(in: NSRange(location: 0, length: 8), with: "SELECT 2")) + #expect(controller.textView.string == "SELECT 1") + } + + @Test("A range past the end of the text is refused") + func rangePastTheTextIsRefused() { + let controller = Mock.loadedTextViewController(string: "SELECT 1") + + #expect(!controller.replaceTextAsOneEdit(in: NSRange(location: 4, length: 20), with: "x")) + #expect(controller.textView.string == "SELECT 1") + } +} diff --git a/apple/Plugins/KingbaseESDriverPlugin/KingbaseESPluginDriver.swift b/apple/Plugins/KingbaseESDriverPlugin/KingbaseESPluginDriver.swift index 19e459e8d6..5d99829c88 100644 --- a/apple/Plugins/KingbaseESDriverPlugin/KingbaseESPluginDriver.swift +++ b/apple/Plugins/KingbaseESDriverPlugin/KingbaseESPluginDriver.swift @@ -14,6 +14,9 @@ final class KingbaseESPluginDriver: PostgreSQLPluginDriver, @unchecked Sendable ) } + // Not measured against a KingbaseES catalog yet, so no Extensions group. + override var serverObjectKinds: [PluginServerObjectKind] { [] } + override func probeAfterConnect() async throws { let result = try await catalogRead(KingbaseESCompatibilityMode.probeQuery) let detected = try KingbaseESCompatibilityMode(setting: result.rows.first?.first?.asText) diff --git a/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver+ServerObjects.swift b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver+ServerObjects.swift new file mode 100644 index 0000000000..fb6ccc9b69 --- /dev/null +++ b/apple/Plugins/MySQLDriverPlugin/MySQLPluginDriver+ServerObjects.swift @@ -0,0 +1,143 @@ +// +// MySQLPluginDriver+ServerObjects.swift +// MySQLDriverPlugin +// + +import Foundation +import TableProMySQLCore +import TableProPluginKit + +nonisolated internal enum MySQLEvents { + static let kindId = "event" + + static var kind: PluginServerObjectKind { + PluginServerObjectKind( + id: kindId, + displayName: String(localized: "Event"), + pluralDisplayName: String(localized: "Events"), + iconName: "calendar.badge.clock", + scope: .container + ) + } + + // TiDB lists `EVENTS` with no rows and OceanBase is not established, so only these two declare it. + static func isDeclared(flavor: MySQLServerFlavor, banner: String?) -> Bool { + guard flavor == .mysql || flavor == .mariadb else { return false } + return MySQLServerVersion.hasPartitionAndEventCatalogs(banner: banner, flavor: flavor) + } +} + +// Reads `MySQLObjectQueries.eventDetails` rows: name, type, status, schema, then the schedule columns. +nonisolated internal enum MySQLEventRows { + static func objects(from rows: [[PluginCellValue]], scheduler: String? = nil) -> [PluginServerObjectInfo] { + rows.compactMap { object(from: $0, scheduler: scheduler) } + } + + // `scheduler` is `@@global.event_scheduler`, read once beside the rows; nil leaves the attribute out. + static func object(from row: [PluginCellValue], scheduler: String? = nil) -> PluginServerObjectInfo? { + guard let name = text(row, 0) else { return nil } + let isRecurring = text(row, 1)?.uppercased() == "RECURRING" + let status = text(row, 2)?.uppercased() + var attributes: [PluginObjectAttribute] = [] + func add(_ label: String, _ value: String?) { + guard let value, !value.isEmpty else { return } + attributes.append(PluginObjectAttribute(label: label, value: value)) + } + add(String(localized: "Schedule"), schedule( + isRecurring: isRecurring, executeAt: text(row, 4), intervalValue: text(row, 5), intervalField: text(row, 6) + )) + add(String(localized: "Status"), status.map(statusLabel)) + add(String(localized: "Starts"), text(row, 7)) + add(String(localized: "Ends"), text(row, 8)) + add(String(localized: "On Completion"), text(row, 9)) + add(String(localized: "Last Executed"), text(row, 10)) + add(String(localized: "Definer"), text(row, 11)) + add(String(localized: "Comment"), text(row, 12)) + add(String(localized: "Event Scheduler"), scheduler) + return PluginServerObjectInfo( + kindId: MySQLEvents.kindId, + name: name, + schema: text(row, 3), + detail: isRecurring ? String(localized: "Recurring") : String(localized: "One Time"), + isEnabled: status.map { $0 == "ENABLED" }, + attributes: attributes + ) + } + + // The schedule in the clause `CREATE EVENT` takes, quoting a composite interval as the server does. + static func schedule(isRecurring: Bool, executeAt: String?, intervalValue: String?, intervalField: String?) -> String? { + guard isRecurring else { return executeAt.map { "AT '\($0)'" } } + guard let intervalValue, let intervalField else { return nil } + let value = intervalValue.allSatisfy(\.isNumber) ? intervalValue : "'\(intervalValue)'" + return "EVERY \(value) \(intervalField)" + } + + // 8.2 renamed SLAVESIDE_DISABLED to REPLICA_SIDE_DISABLED; both mean the replica does not run it. + static func statusLabel(_ status: String) -> String { + switch status { + case "ENABLED": return String(localized: "Enabled") + case "DISABLED": return String(localized: "Disabled") + case "SLAVESIDE_DISABLED", "REPLICA_SIDE_DISABLED": return String(localized: "Disabled on Replica") + default: return status + } + } + + // The catalog compares names without case, so an exact spelling wins over another the read also matched. + static func row(named name: String, in rows: [[PluginCellValue]]) -> [PluginCellValue]? { + rows.first { $0[safe: 0]?.asText == name } ?? rows.first + } + + private static func text(_ row: [PluginCellValue], _ index: Int) -> String? { + guard let value = row[safe: index]?.asText, !value.isEmpty else { return nil } + return value + } +} + +internal extension MySQLPluginDriver { + var serverObjectKinds: [PluginServerObjectKind] { + let identity = serverIdentity + return MySQLEvents.isDeclared(flavor: identity.flavor, banner: identity.banner) ? [MySQLEvents.kind] : [] + } + + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [PluginServerObjectInfo] { + let identity = serverIdentity + guard kindId == MySQLEvents.kindId, MySQLEvents.isDeclared(flavor: identity.flavor, banner: identity.banner) else { + return [] + } + let exactSchema = await schemaNamesAreCaseSensitive() + let result = try await execute( + ownStatement: MySQLObjectQueries.eventDetails(schema: effectiveSchema(schema), exactSchema: exactSchema) + ) + guard !result.rows.isEmpty else { return [] } + return MySQLEventRows.objects(from: result.rows, scheduler: await eventScheduler()) + } + + // The tab reads the event's row again, so its attributes follow an ALTER EVENT as its DDL does. + func fetchServerObject(_ object: PluginServerObjectInfo) async throws -> PluginServerObjectInfo { + guard object.kindId == MySQLEvents.kindId else { throw PluginObjectSourceError.unsupported(object.name) } + let exactSchema = await schemaNamesAreCaseSensitive() + let result = try await execute(ownStatement: MySQLObjectQueries.eventDetails( + schema: effectiveSchema(object.schema), name: object.name, exactSchema: exactSchema + )) + guard let row = MySQLEventRows.row(named: object.name, in: result.rows), + let fresh = MySQLEventRows.object(from: row, scheduler: await eventScheduler()) + else { throw PluginObjectSourceError.notFound(object.name) } + let definition = try await fetchEventDDL(PluginEventInfo(name: fresh.name, schema: fresh.schema)) + return PluginServerObjectInfo( + kindId: fresh.kindId, + name: fresh.name, + schema: fresh.schema, + identity: object.identity, + detail: fresh.detail, + isEnabled: fresh.isEnabled, + definition: definition, + attributes: fresh.attributes + ) + } + + // A server or proxy without the variable loses this one attribute, never the list. + private func eventScheduler() async -> String? { + guard let result = try? await execute(ownStatement: MySQLObjectQueries.eventScheduler) else { return nil } + return result.rows.first?[safe: 0]?.asText + } +} diff --git a/apple/Plugins/MySQLDriverTests/MySQLEventServerObjectTests.swift b/apple/Plugins/MySQLDriverTests/MySQLEventServerObjectTests.swift new file mode 100644 index 0000000000..3704a927b3 --- /dev/null +++ b/apple/Plugins/MySQLDriverTests/MySQLEventServerObjectTests.swift @@ -0,0 +1,144 @@ +// +// MySQLEventServerObjectTests.swift +// MySQLDriverTests +// + +import Foundation +import TableProPluginKit +import Testing + +struct MySQLEventServerObjectTests { + private func row(_ cells: String?...) -> [PluginCellValue] { + cells.map(PluginCellValue.fromOptional) + } + + private func attribute(_ object: PluginServerObjectInfo?, _ label: String) -> String? { + object?.attributes.first { $0.label == label }?.value + } + + @Test("MySQL and MariaDB declare Events; the other flavors sharing the driver declare none") + func kindsPerFlavor() { + #expect(MySQLEvents.isDeclared(flavor: .mysql, banner: "8.0.36")) + #expect(MySQLEvents.isDeclared(flavor: .mariadb, banner: "10.11.6-MariaDB")) + #expect(MySQLEvents.isDeclared(flavor: .mysql, banner: nil)) + #expect(!MySQLEvents.isDeclared(flavor: .tidb(version: nil), banner: "8.0.11-TiDB-v7.5.0")) + #expect(!MySQLEvents.isDeclared(flavor: .databend, banner: nil)) + #expect(!MySQLEvents.isDeclared(flavor: .oceanbase(version: nil), banner: nil)) + #expect(!MySQLEvents.isDeclared(flavor: .starrocks, banner: nil)) + } + + @Test("A server older than 5.1.6 has no events catalog and declares no Events") + func kindsFollowTheEventsFloor() { + #expect(!MySQLEvents.isDeclared(flavor: .mysql, banner: "5.0.96-log")) + #expect(!MySQLEvents.isDeclared(flavor: .mysql, banner: "4.1.22-standard")) + #expect(MySQLEvents.isDeclared(flavor: .mysql, banner: "5.1.6")) + } + + @Test("The Events kind is listed per database, under its own symbol") + func eventKindDescriptor() { + let kind = MySQLEvents.kind + #expect(kind.id == "event") + #expect(kind.scope == .container) + #expect(kind.iconName == "calendar.badge.clock") + #expect(kind.pluralDisplayName == String(localized: "Events")) + } + + @Test("A recurring event reads its interval as the schedule and counts as enabled") + func recurringEvent() { + let object = MySQLEventRows.object( + from: row( + "nightly_cleanup", "RECURRING", "ENABLED", "shop", + nil, "1", "DAY", "2026-01-01 03:00:00", nil, + "PRESERVE", "2026-11-10 03:00:00", "app@%", "Purges the log" + ), + scheduler: "ON" + ) + + #expect(object?.kindId == "event") + #expect(object?.name == "nightly_cleanup") + #expect(object?.schema == "shop") + #expect(object?.identity == nil) + #expect(object?.detail == String(localized: "Recurring")) + #expect(object?.isEnabled == true) + #expect(attribute(object, String(localized: "Schedule")) == "EVERY 1 DAY") + #expect(attribute(object, String(localized: "Status")) == String(localized: "Enabled")) + #expect(attribute(object, String(localized: "Starts")) == "2026-01-01 03:00:00") + #expect(attribute(object, String(localized: "Ends")) == nil) + #expect(attribute(object, String(localized: "On Completion")) == "PRESERVE") + #expect(attribute(object, String(localized: "Last Executed")) == "2026-11-10 03:00:00") + #expect(attribute(object, String(localized: "Definer")) == "app@%") + #expect(attribute(object, String(localized: "Comment")) == "Purges the log") + #expect(attribute(object, String(localized: "Event Scheduler")) == "ON") + } + + @Test("A one-time event reads its run time as the schedule") + func oneTimeEvent() { + let object = MySQLEventRows.object( + from: row( + "close_quarter", "ONE TIME", "DISABLED", "shop", + "2026-12-31 23:59:00", nil, nil, nil, nil, + "NOT PRESERVE", nil, "root@localhost", nil + ), + scheduler: "OFF" + ) + + #expect(object?.detail == String(localized: "One Time")) + #expect(object?.isEnabled == false) + #expect(attribute(object, String(localized: "Schedule")) == "AT '2026-12-31 23:59:00'") + #expect(attribute(object, String(localized: "Status")) == String(localized: "Disabled")) + #expect(attribute(object, String(localized: "Last Executed")) == nil) + #expect(attribute(object, String(localized: "Event Scheduler")) == "OFF") + } + + @Test("An event disabled on a replica says so under both spellings the server has used") + func replicaDisabledEvent() { + for status in ["SLAVESIDE_DISABLED", "REPLICA_SIDE_DISABLED"] { + let object = MySQLEventRows.object(from: row( + "sync_totals", "RECURRING", status, "shop", nil, "1", "HOUR", nil, nil, "PRESERVE", nil, nil, nil + )) + #expect(object?.isEnabled == false, "\(status)") + #expect(attribute(object, String(localized: "Status")) == String(localized: "Disabled on Replica"), "\(status)") + } + } + + @Test("A composite interval is quoted the way CREATE EVENT spells it") + func compositeInterval() { + #expect(MySQLEventRows.schedule(isRecurring: true, executeAt: nil, intervalValue: "1:30", intervalField: "HOUR_MINUTE") + == "EVERY '1:30' HOUR_MINUTE") + #expect(MySQLEventRows.schedule(isRecurring: true, executeAt: nil, intervalValue: nil, intervalField: "DAY") == nil) + #expect(MySQLEventRows.schedule(isRecurring: false, executeAt: nil, intervalValue: "1", intervalField: "DAY") == nil) + } + + @Test("A scheduler the server would not report leaves only that attribute out") + func missingSchedulerDropsOneAttribute() { + let object = MySQLEventRows.object(from: row( + "nightly", "RECURRING", "ENABLED", "shop", nil, "1", "DAY", nil, nil, "PRESERVE", nil, "app@%", nil + )) + + #expect(attribute(object, String(localized: "Event Scheduler")) == nil) + #expect(attribute(object, String(localized: "Schedule")) == "EVERY 1 DAY") + #expect(attribute(object, String(localized: "Definer")) == "app@%") + } + + @Test("A re-read picks the row spelled exactly as the tab names it") + func rereadPicksTheExactSpelling() { + let rows = [row("Nightly", "RECURRING", "ENABLED", "shop"), row("nightly", "ONE TIME", "DISABLED", "shop")] + + #expect(MySQLEventRows.row(named: "nightly", in: rows)?.first == PluginCellValue.fromOptional("nightly")) + #expect(MySQLEventRows.row(named: "NIGHTLY", in: rows)?.first == PluginCellValue.fromOptional("Nightly")) + #expect(MySQLEventRows.row(named: "nightly", in: []) == nil) + } + + @Test("Four columns alone still map, and a row with no name is dropped") + func shortAndNamelessRows() { + let objects = MySQLEventRows.objects(from: [ + row("legacy", "RECURRING", "ENABLED", "shop"), + row(nil, "RECURRING", "ENABLED", "shop") + ]) + + #expect(objects.count == 1) + #expect(objects.first?.name == "legacy") + #expect(objects.first?.isEnabled == true) + #expect(attribute(objects.first, String(localized: "Schedule")) == nil) + } +} diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLExtensionQueries.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLExtensionQueries.swift new file mode 100644 index 0000000000..228f666e0d --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLExtensionQueries.swift @@ -0,0 +1,97 @@ +// +// PostgreSQLExtensionQueries.swift +// PostgreSQLDriverPlugin +// + +import Foundation +import TableProPluginKit +import TableProPostgreSQLCore + +nonisolated enum PostgreSQLExtensions { + static let kindId = "extension" + + static var kind: PluginServerObjectKind { + PluginServerObjectKind( + id: kindId, + displayName: String(localized: "Extension"), + pluralDisplayName: String(localized: "Extensions"), + iconName: "puzzlepiece.extension", + scope: .database + ) + } + + static func isDeclared(capabilities: PostgreSQLCapabilities) -> Bool { + capabilities.hasExtensionsCatalog + } +} + +// Every column a 9.1 server has; the default version reads control files on disk, so only one object asks for it. +nonisolated enum PostgreSQLExtensionQueries { + private static let projection = """ + SELECT e.oid::pg_catalog.text, e.extname, n.nspname, e.extversion, e.extrelocatable, + pg_catalog.pg_get_userbyid(e.extowner), + pg_catalog.obj_description(e.oid, 'pg_extension') + FROM pg_catalog.pg_extension e + JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace + """ + + static let list = projection + "\nORDER BY e.extname" + + static func one(oid: UInt32) -> String { + projection + "\nWHERE e.oid = \(oid)" + } + + static func defaultVersion(name: String) -> String { + """ + SELECT default_version FROM pg_catalog.pg_available_extensions + WHERE name = \(PostgreSQLObjectQueries.quoteLiteral(name)) + """ + } + + // pg_dump's own form: it names the schema and intentionally leaves the version out. + static func definition(name: String, schema: String, comment: String?) -> String { + var text = "CREATE EXTENSION IF NOT EXISTS \(PostgreSQLObjectQueries.quoteIdentifier(name)) " + + "WITH SCHEMA \(PostgreSQLObjectQueries.quoteIdentifier(schema));" + if let comment, !comment.isEmpty { + text += "\n\nCOMMENT ON EXTENSION \(PostgreSQLObjectQueries.quoteIdentifier(name)) " + + "IS \(PostgreSQLObjectQueries.quoteLiteral(comment));" + } + return text + } + + static func object(from row: [PluginCellValue], defaultVersion: String? = nil) -> PluginServerObjectInfo? { + guard let oid = text(row, 0), UInt32(oid) != nil, let name = text(row, 1) else { return nil } + let schema = text(row, 2) + let version = text(row, 3) + var attributes: [PluginObjectAttribute] = [] + func add(_ label: String, _ value: String?) { + guard let value, !value.isEmpty else { return } + attributes.append(PluginObjectAttribute(label: label, value: value)) + } + add(String(localized: "Version"), version) + add(String(localized: "Default Version"), defaultVersion) + add(String(localized: "Schema"), schema) + add(String(localized: "Owner"), text(row, 5)) + add(String(localized: "Relocatable"), text(row, 4).map { isTrue($0) ? String(localized: "Yes") : String(localized: "No") }) + add(String(localized: "Comment"), text(row, 6)) + return PluginServerObjectInfo( + kindId: PostgreSQLExtensions.kindId, + name: name, + schema: schema, + identity: oid, + detail: version, + definition: schema.map { definition(name: name, schema: $0, comment: text(row, 6)) }, + attributes: attributes + ) + } + + // A boolean cell arrives as `true`/`false` once decoded, and as `t`/`f` from a text read. + private static func isTrue(_ value: String) -> Bool { + value == "true" || value == "t" + } + + private static func text(_ row: [PluginCellValue], _ index: Int) -> String? { + guard let value = row[safe: index]?.asText, !value.isEmpty else { return nil } + return value + } +} diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Extensions.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Extensions.swift new file mode 100644 index 0000000000..64243d9326 --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver+Extensions.swift @@ -0,0 +1,38 @@ +// +// PostgreSQLPluginDriver+Extensions.swift +// PostgreSQLDriverPlugin +// + +import Foundation +import TableProPluginKit +import TableProPostgreSQLCore + +extension PostgreSQLPluginDriver { + // `serverObjectKinds` is dynamically dispatched, so a subclass that declares none reads nothing here. + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [PluginServerObjectInfo] { + guard kindId == PostgreSQLExtensions.kindId, serverObjectKinds.contains(where: { $0.id == kindId }) else { + return [] + } + let result = try await catalogRead(PostgreSQLExtensionQueries.list) + return result.rows.compactMap { PostgreSQLExtensionQueries.object(from: $0) } + } + + // Read again by the oid this driver handed out, which a rename or a schema move keeps; anything + // else is refused rather than widened into a lookup by name. + func fetchServerObject(_ object: PluginServerObjectInfo) async throws -> PluginServerObjectInfo { + guard object.kindId == PostgreSQLExtensions.kindId else { throw PluginObjectSourceError.unsupported(object.name) } + guard let identity = object.identity, let oid = UInt32(identity) else { + throw PluginObjectSourceError.notFound(object.name) + } + let result = try await catalogRead(PostgreSQLExtensionQueries.one(oid: oid)) + guard let row = result.rows.first(where: { $0[safe: 0]?.asText == String(oid) }), + let name = row[safe: 1]?.asText + else { throw PluginObjectSourceError.notFound(object.name) } + let defaultVersion = try? await catalogRead(PostgreSQLExtensionQueries.defaultVersion(name: name)) + guard let fresh = PostgreSQLExtensionQueries.object( + from: row, + defaultVersion: defaultVersion?.rows.first?[safe: 0]?.asText + ) else { throw PluginObjectSourceError.notFound(object.name) } + return fresh + } +} diff --git a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift index 332e0df495..b86fd5edb0 100644 --- a/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift +++ b/apple/Plugins/PostgreSQLDriverPlugin/PostgreSQLPluginDriver.swift @@ -59,6 +59,11 @@ class PostgreSQLPluginDriver: LibPQBackedDriver, @unchecked Sendable { var canDebugRoutines: Bool { true } var canFollowNotifications: Bool { true } + // In the class body so KingbaseES, whose catalog is not established, can answer none. + var serverObjectKinds: [PluginServerObjectKind] { + PostgreSQLExtensions.isDeclared(capabilities: catalogCapabilities) ? [PostgreSQLExtensions.kind] : [] + } + init(config: DriverConnectionConfig, singleConnectionMode: Bool = false) { self.core = LibPQDriverCore(config: config, singleConnectionMode: singleConnectionMode) } diff --git a/apple/Plugins/PostgreSQLDriverTests/PostgreSQLExtensionQueryTests.swift b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLExtensionQueryTests.swift new file mode 100644 index 0000000000..35fd9ff50e --- /dev/null +++ b/apple/Plugins/PostgreSQLDriverTests/PostgreSQLExtensionQueryTests.swift @@ -0,0 +1,123 @@ +// +// PostgreSQLExtensionQueryTests.swift +// PostgreSQLDriverTests +// + +import Foundation +import TableProPluginKit +import TableProPostgreSQLCore +import Testing + +struct PostgreSQLExtensionQueryTests { + private func row(_ cells: String?...) -> [PluginCellValue] { + cells.map(PluginCellValue.fromOptional) + } + + private func attribute(_ object: PluginServerObjectInfo?, _ label: String) -> String? { + object?.attributes.first { $0.label == label }?.value + } + + private static let constructsMissingBefore91 = [ + "to_regclass", "LATERAL", "WITH ORDINALITY", "json", "array_remove", "array_position", "FILTER (", "unnest(" + ] + + @Test("The extension reads stay inside what PostgreSQL 9.1 parses") + func readsArePortable() { + let queries = [ + PostgreSQLExtensionQueries.list, + PostgreSQLExtensionQueries.one(oid: 16_384), + PostgreSQLExtensionQueries.defaultVersion(name: "pgcrypto") + ] + for sql in queries { + #expect(Self.constructsMissingBefore91.filter { sql.contains($0) }.isEmpty, "\(sql)") + } + } + + @Test("The listing reads every extension once, by name, without the control files") + func listingIsOneCatalogRead() { + let sql = PostgreSQLExtensionQueries.list + #expect(sql.contains("FROM pg_catalog.pg_extension e")) + #expect(sql.contains("JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace")) + #expect(sql.hasSuffix("ORDER BY e.extname")) + #expect(!sql.contains("pg_available_extensions")) + #expect(!sql.contains("WHERE")) + } + + @Test("One extension is read again by its oid alone") + func rereadIsByOid() { + let sql = PostgreSQLExtensionQueries.one(oid: 16_384) + #expect(sql.hasSuffix("WHERE e.oid = 16384")) + #expect(!sql.contains("extname =")) + } + + @Test("The default version read quotes its name as a literal") + func defaultVersionQuotesTheName() { + #expect(PostgreSQLExtensionQueries.defaultVersion(name: "o'brien").contains("WHERE name = 'o''brien'")) + } + + @Test("A plain extension's definition is pg_dump's, with no version") + func plainDefinition() { + #expect(PostgreSQLExtensionQueries.definition(name: "pgcrypto", schema: "public", comment: nil) + == #"CREATE EXTENSION IF NOT EXISTS "pgcrypto" WITH SCHEMA "public";"#) + } + + @Test("A name or schema that needs quoting is quoted as an identifier") + func quotedDefinition() { + #expect(PostgreSQLExtensionQueries.definition(name: "uuid-ossp", schema: "My Schema", comment: "") + == #"CREATE EXTENSION IF NOT EXISTS "uuid-ossp" WITH SCHEMA "My Schema";"#) + } + + @Test("A comment follows as COMMENT ON EXTENSION, its quote doubled") + func commentWithAQuote() { + let text = PostgreSQLExtensionQueries.definition(name: "hstore", schema: "public", comment: "data type for key/value's") + #expect(text.hasPrefix(#"CREATE EXTENSION IF NOT EXISTS "hstore" WITH SCHEMA "public";"#)) + #expect(text.hasSuffix(#"COMMENT ON EXTENSION "hstore" IS 'data type for key/value''s';"#)) + } + + @Test("A listed row carries its oid, version and definition") + func rowMapping() { + let object = PostgreSQLExtensionQueries.object( + from: row("16384", "pgcrypto", "public", "1.3", "true", "postgres", "cryptographic functions") + ) + + #expect(object?.kindId == "extension") + #expect(object?.identity == "16384") + #expect(object?.name == "pgcrypto") + #expect(object?.schema == "public") + #expect(object?.detail == "1.3") + #expect(object?.isEnabled == nil) + #expect(attribute(object, String(localized: "Version")) == "1.3") + #expect(attribute(object, String(localized: "Owner")) == "postgres") + #expect(attribute(object, String(localized: "Relocatable")) == String(localized: "Yes")) + #expect(attribute(object, String(localized: "Default Version")) == nil) + #expect(object?.definition?.contains("COMMENT ON EXTENSION \"pgcrypto\"") == true) + } + + @Test("The re-read adds the default version, and a non-relocatable extension says No") + func rereadRowMapping() { + let object = PostgreSQLExtensionQueries.object( + from: row("13000", "plpgsql", "pg_catalog", "1.0", "f", "postgres", nil), + defaultVersion: "1.0" + ) + + #expect(attribute(object, String(localized: "Default Version")) == "1.0") + #expect(attribute(object, String(localized: "Relocatable")) == String(localized: "No")) + #expect(attribute(object, String(localized: "Comment")) == nil) + #expect(object?.definition == #"CREATE EXTENSION IF NOT EXISTS "plpgsql" WITH SCHEMA "pg_catalog";"#) + } + + @Test("A row without an oid is not an extension this driver can read again") + func rowWithoutOidIsDropped() { + #expect(PostgreSQLExtensionQueries.object(from: row(nil, "pgcrypto", "public", "1.3", "t", nil, nil)) == nil) + #expect(PostgreSQLExtensionQueries.object(from: row("pgcrypto", "pgcrypto", "public", "1.3", "t", nil, nil)) == nil) + } + + @Test("Extensions are declared from 9.1, at database scope") + func declaredFromNinePointOne() { + #expect(!PostgreSQLExtensions.isDeclared(capabilities: PostgreSQLCapabilities(serverVersion: 90_024))) + #expect(PostgreSQLExtensions.isDeclared(capabilities: PostgreSQLCapabilities(serverVersion: 90_100))) + #expect(PostgreSQLExtensions.isDeclared(capabilities: .assumingModernWhenUnknown(0))) + #expect(PostgreSQLExtensions.kind.scope == .database) + #expect(PostgreSQLExtensions.kind.id == "extension") + } +} diff --git a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift index b1f03c7154..6d05a666dc 100644 --- a/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift +++ b/apple/Plugins/TableProPluginKit/PluginDatabaseDriver.swift @@ -210,6 +210,14 @@ public protocol PluginDatabaseDriver: AnyObject, Sendable { func fetchEvents(schema: String?) async throws -> [PluginEventInfo] func fetchEventDDL(_ event: PluginEventInfo) async throws -> String + // Asked of the live driver, so one plugin can answer per server flavor and version. + var serverObjectKinds: [PluginServerObjectKind] { get } + // `schema` is the container for a `.container` kind and nil for a `.database` kind. A server + // without the catalog answers empty rather than throwing. + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [PluginServerObjectInfo] + // Reads one listed object again with its definition; `identity` is the driver's own key. + func fetchServerObject(_ object: PluginServerObjectInfo) async throws -> PluginServerObjectInfo + /// The settings the server recorded for an object when it was created and applies whenever it runs, such as a /// MySQL routine's `sql_mode`, as statements a script writes around the object's `CREATE`. Nil when there are none. func fetchRoutineCreationScope(_ routine: PluginRoutineInfo) async throws -> PluginStatementScope? @@ -834,6 +842,17 @@ public extension PluginDatabaseDriver { return definition } + var serverObjectKinds: [PluginServerObjectKind] { [] } + + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [PluginServerObjectInfo] { [] } + + func fetchServerObject(_ object: PluginServerObjectInfo) async throws -> PluginServerObjectInfo { + guard let definition = object.definition, !definition.isEmpty else { + throw PluginObjectSourceError.unsupported(object.name) + } + return object + } + func fetchRoutineCreationScope(_ routine: PluginRoutineInfo) async throws -> PluginStatementScope? { nil } func fetchTriggerCreationScope(_ trigger: PluginTriggerInfo) async throws -> PluginStatementScope? { nil } diff --git a/apple/Plugins/TableProPluginKit/PluginServerObject.swift b/apple/Plugins/TableProPluginKit/PluginServerObject.swift new file mode 100644 index 0000000000..460117f92f --- /dev/null +++ b/apple/Plugins/TableProPluginKit/PluginServerObject.swift @@ -0,0 +1,68 @@ +// +// PluginServerObject.swift +// TableProPluginKit +// + +import Foundation + +public enum PluginServerObjectScope: String, Codable, Sendable { + // Listed in every container the engine lists tables in: a MySQL database, a PostgreSQL schema. + case container + // Listed once per database, beside its schemas. + case database +} + +public struct PluginServerObjectKind: Codable, Sendable, Hashable { + public let id: String + public let displayName: String + public let pluralDisplayName: String + public let iconName: String + public let scope: PluginServerObjectScope + + public init( + id: String, + displayName: String, + pluralDisplayName: String, + iconName: String, + scope: PluginServerObjectScope = .container + ) { + self.id = id + self.displayName = displayName + self.pluralDisplayName = pluralDisplayName + self.iconName = iconName + self.scope = scope + } +} + +public struct PluginServerObjectInfo: Codable, Sendable { + public let kindId: String + public let name: String + public let schema: String? + // The driver's own key for reading this object again, opaque to the app. + public let identity: String? + // One short label drawn beside the name, such as "Recurring" or "1.3". + public let detail: String? + public let isEnabled: Bool? + public let definition: String? + public let attributes: [PluginObjectAttribute] + + public init( + kindId: String, + name: String, + schema: String? = nil, + identity: String? = nil, + detail: String? = nil, + isEnabled: Bool? = nil, + definition: String? = nil, + attributes: [PluginObjectAttribute] = [] + ) { + self.kindId = kindId + self.name = name + self.schema = schema + self.identity = identity + self.detail = detail + self.isEnabled = isEnabled + self.definition = definition + self.attributes = attributes + } +} diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift index b15c7901a0..6beada3865 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Batches.swift @@ -106,6 +106,7 @@ extension QueryExecutionCoordinator { /// The app opens no transaction around the run, as SQL Server's own tools do not: a batch carries on past most /// errors, so an app-owned rollback would take back statements that ran after the failure, and the script's own /// `BEGIN TRAN`, `TRY...CATCH` and procedures are what decide. A transaction the script leaves open is reported. + /// Manual Commit is the exception: the run is held, and a failure or a Stop rolls it back. func executeBatches( _ batches: [ExecutableBatch], parameters: [QueryParameter], @@ -253,6 +254,10 @@ extension QueryExecutionCoordinator { lease: DriverLeaseOwner ) async -> BatchRun { let failureOutput = ServerOutputBox() + let commitMode = DatabaseManager.shared.commitMode(for: scope.connectionId) + let databaseType = parent.connection.type + let grammar = parent.lexicalGrammar + let statementTexts = prepared.flatMap { $0.batch.statements.map(\.sql) } do { var run = try await DatabaseManager.shared.withScopedDriver( scope: scope, @@ -260,7 +265,16 @@ extension QueryExecutionCoordinator { cancellation: .cancellableRead(lease) ) { driver in let startState = await driver.heldSessionTransactionState() - let plan = BatchTransactionPlan.autocommit.joining(startState) + let manual = ManualCommit.decision( + commitMode: commitMode, + statements: statementTexts, + databaseType: databaseType, + grammar: grammar, + supportsTransactions: driver.supportsTransactions, + sessionState: startState + ) + if let refusal = manual.refusal { throw refusal } + let plan = ManualCommit.runPlan(text: .autocommit, decision: manual, sessionState: startState) let outcome = await BatchStatementRun.run( prepared, plan: plan, diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift index ead6c8e0e9..04b2242bc3 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Helpers.swift @@ -224,7 +224,8 @@ extension QueryExecutionCoordinator { absentCells: [Int: Set] = [:], unloadedCells: [Int: [Int: Int]] = [:], columnOrigins: [PluginColumnOrigin?]? = nil, - prepared: TableRows.PreparedRows? = nil + prepared: TableRows.PreparedRows? = nil, + trigger: TableLoadTrigger = .userInitiated ) { guard let idx = parent.tabManager.tabs.firstIndex(where: { $0.id == tabId }) else { return } @@ -383,20 +384,12 @@ extension QueryExecutionCoordinator { } parent.dropParkedEdits(ofBackgroundTab: tabId) - recordHistory( - QueryHistoryRecordRequest( - query: historySQL ?? sql, - connectionId: conn.id, - databaseName: historyDatabaseName(tabId: tabId), - databaseType: conn.type, - schemaName: historySchemaName(tabId: tabId), - source: historySource(tabId: tabId), - executionTime: executionTime, - rowCount: rows.count, - wasSuccessful: true, - timing: timing + if trigger.recordsHistory { + recordSuccessfulRun( + query: historySQL ?? sql, tabId: tabId, connection: conn, + executionTime: executionTime, rowCount: rows.count, timing: timing ) - ) + } if parent.tabManager.selectedTabId == tabId, isEditable, !parent.changeManager.hasChanges { parent.changeManager.clearChangesAndUndoHistory() @@ -479,11 +472,36 @@ extension QueryExecutionCoordinator { ) } + private func recordSuccessfulRun( + query: String, + tabId: UUID, + connection conn: DatabaseConnection, + executionTime: TimeInterval, + rowCount: Int, + timing: PluginQueryTiming? + ) { + recordHistory( + QueryHistoryRecordRequest( + query: query, + connectionId: conn.id, + databaseName: historyDatabaseName(tabId: tabId), + databaseType: conn.type, + schemaName: historySchemaName(tabId: tabId), + source: historySource(tabId: tabId), + executionTime: executionTime, + rowCount: rowCount, + wasSuccessful: true, + timing: timing + ) + ) + } + func launchPhase2Work( tableName: String, tabId: UUID, connectionType: DatabaseType, - schemaTask: Task? + schemaTask: Task?, + countsRows: Bool = true ) { let isNonSQL = PluginManager.shared.editorLanguage(for: connectionType) != .sql let contentEpoch = parent.tabExecution.contentEpoch(for: tabId) @@ -491,7 +509,7 @@ extension QueryExecutionCoordinator { Task(priority: .utility) { [weak self, parent] in // The caller marked the total pending; every exit that skips `resolveRowCount` must release it, or Count Exactly never returns. guard let self else { - parent.releaseCountPending(for: tabId) + if countsRows { parent.releaseCountPending(for: tabId) } return } guard !parent.isTearingDown else { return } @@ -503,14 +521,14 @@ extension QueryExecutionCoordinator { await MainActor.run { [weak self] in guard let self else { - parent.releaseCountPending(for: tabId) + if countsRows { parent.releaseCountPending(for: tabId) } return } if let schema { applySchemaMetadata(schema, tabId: tabId, tableName: tableName, resultSetId: resultSetId) } // A retarget or newer run owns the tab now with its own pending mark; clearing here would release the successor's. - guard parent.tabExecution.isSameContent(contentEpoch, for: tabId) else { return } + guard countsRows, parent.tabExecution.isSameContent(contentEpoch, for: tabId) else { return } resolveRowCount( tableName: tableName, tabId: tabId, @@ -785,7 +803,7 @@ extension QueryExecutionCoordinator { case .clear: return .clear case .approximate: - guard let count = try? await DatabaseManager.shared.withMetadataDriver(scope: scope, { driver in + guard let count = try? await DatabaseManager.shared.withMetadataRead(scope: scope, workload: .interactive, { driver in try await driver.fetchApproximateRowCount(table: tableName, schema: scope.schema) }) else { return nil } return .count(count, isApproximate: true) @@ -860,7 +878,8 @@ extension QueryExecutionCoordinator { sql: String, tabId: UUID, connection conn: DatabaseConnection, - serverOutput: PluginServerOutput = .none + serverOutput: PluginServerOutput = .none, + trigger: TableLoadTrigger = .userInitiated ) { let diagnosis = DatabaseWriteRejectionDiagnosis.formatted(error) let message = ServerOutputCapture.failureMessage(diagnosis, output: serverOutput) @@ -873,6 +892,7 @@ extension QueryExecutionCoordinator { tab.execution.executionTime = nil } + guard trigger.recordsHistory else { return } recordHistory( QueryHistoryRecordRequest( query: sql, diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift index 0520265783..7597fc3493 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator+Parameters.swift @@ -202,6 +202,7 @@ extension QueryExecutionCoordinator { let boundValues = BoundParameterValues(values: parameters) let failureOutput = ServerOutputBox() let grammar = parent.lexicalGrammar + let held = parent.manualCommitRun(skipped: tab.tabType != .query) let parameterizedTask = Task { [weak self, parent] in guard let self else { return } @@ -215,15 +216,17 @@ extension QueryExecutionCoordinator { route: DatabaseManager.shared.executionRoute(for: scope), cancellation: .cancellableRead(lease) ) { [queryExecutor = parent.queryExecutor, boundValues] driver in - // Before it runs, so a statement that fails or a connection lost under it is still noted. - await DatabaseManager.shared.noteSessionStatements([statement.sql], ranOn: driver, scope: scope) - let fetched = try await queryExecutor.executeQuery( - driver: driver, - sql: statement.sql, - parameters: boundValues.values, - rowCap: rowCap, - capturingOutputInto: failureOutput - ) + let fetched = try await held.running([statement.sql], on: driver) { + // Before it runs, so a statement that fails or a connection lost under it is still noted. + await DatabaseManager.shared.noteSessionStatements([statement.sql], ranOn: driver, scope: scope) + return try await queryExecutor.executeQuery( + driver: driver, + sql: statement.sql, + parameters: boundValues.values, + rowCap: rowCap, + capturingOutputInto: failureOutput + ) + } let edits = await SucceededStatements.single( statement.sql, scope: scope, databaseType: conn.type, grammar: grammar, ranOn: driver ) @@ -410,7 +413,7 @@ extension QueryExecutionCoordinator { ) let outcome = run.outcome let sent = running.prepared - let sessionNotice = run.plan == .sessionTransaction ? run.sessionState.openTransactionNotice : nil + let sessionNotice = run.plan.readsSessionEndState ? run.sessionState.openTransactionNotice : nil let ranStatements: [String] switch outcome { @@ -536,9 +539,12 @@ extension QueryExecutionCoordinator { claim: TabExecutionClaim, lease: DriverLeaseOwner ) async -> MultiStatementRun { + let commitMode = DatabaseManager.shared.commitMode(for: scope.connectionId) let failureOutput = ServerOutputBox() let commitOutput = ServerOutputBox() let databaseType = parent.connection.type + let grammar = parent.lexicalGrammar + let statementTexts = prepared.map(\.sentSQL) do { var run = try await DatabaseManager.shared.withScopedDriver( scope: scope, @@ -546,7 +552,16 @@ extension QueryExecutionCoordinator { cancellation: .cancellableRead(lease) ) { driver in let startState = await driver.heldSessionTransactionState() - let sessionPlan = plan.joining(startState) + let manual = ManualCommit.decision( + commitMode: commitMode, + statements: statementTexts, + databaseType: databaseType, + grammar: grammar, + supportsTransactions: driver.supportsTransactions, + sessionState: startState + ) + if let refusal = manual.refusal { throw refusal } + let sessionPlan = ManualCommit.runPlan(text: plan, decision: manual, sessionState: startState) let outcome = await BatchStatementRun.run( prepared, plan: sessionPlan, @@ -579,7 +594,7 @@ extension QueryExecutionCoordinator { await DatabaseManager.shared.noteSessionStatements( sent.prefix(outcome.ranCount(of: sent.count)).map(\.sentSQL), ranOn: driver, scope: scope ) - guard sessionPlan == .sessionTransaction else { + guard sessionPlan.readsSessionEndState else { let endState: PluginSessionTransactionState = asksEndState ? await driver.heldSessionTransactionState() : .unknown diff --git a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift index 9911aa248a..69afa07217 100644 --- a/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift +++ b/apple/TablePro/Core/Coordinators/QueryExecutionCoordinator.swift @@ -32,6 +32,7 @@ final class QueryExecutionCoordinator: ObservableObject { let batches = executionBatches(in: tab.content.query) let statements = batches.flatMap(\.statements) guard !statements.isEmpty else { return } + parent.tabVersions.record(tab, connectionId: parent.connectionId, reason: .run) if AppSettingsManager.shared.editor.queryParametersEnabled, parent.bindsNamedParameters { let combinedSQL = SQLParameterExtractor.parameterSource(of: statements) @@ -206,7 +207,7 @@ final class QueryExecutionCoordinator: ObservableObject { } } - /// A run the driver cannot carry out as written says so before anything is sent, rather than running part of it. + // A run the driver cannot carry out as written says so before anything is sent, rather than running part of it. private func refuses(_ route: QueryExecutionRoute, tabIndex index: Int) -> Bool { guard case .needsBatchDriver = route else { return false } parent.tabManager.mutate(at: index) { diff --git a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+Discard.swift b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+Discard.swift index b74650db33..fca7c1b6b6 100644 --- a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+Discard.swift +++ b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+Discard.swift @@ -36,12 +36,13 @@ extension RowEditingCoordinator { let mode: PluginTransactionAccessMode = kind.declaresWrite ? .readWrite : .serverDefault let plan = DataWritePlan(scope: scope, databaseType: parent.connection.type, steps: steps) + let commitMode = DatabaseManager.shared.commitMode(for: parent.connectionId) _ = try await DatabaseManager.shared.withScopedDriver( scope: scope, route: DatabaseManager.shared.executionRoute(for: scope), cancellation: .protectedWrite ) { driver in - _ = try await DataWriteExecutor.run(plan, mode: mode, on: driver) + _ = try await DataWriteExecutor.run(plan, mode: mode, commitMode: commitMode, on: driver) } } diff --git a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift index bcb80872e1..417aaabfab 100644 --- a/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift +++ b/apple/TablePro/Core/Coordinators/RowEditingCoordinator+SaveChanges.swift @@ -260,6 +260,7 @@ extension RowEditingCoordinator { } let route = DatabaseManager.shared.executionRoute(for: scope) + let commitMode = DatabaseManager.shared.commitMode(for: conn.id) let serverOwned = serverOwnedColumns(of: savingTabId) let columnTypeNames = parent.changeManager.columnTypeNames @@ -277,7 +278,7 @@ extension RowEditingCoordinator { route: route, cancellation: .protectedWrite ) { driver in - try await DataWriteExecutor.run(plan, mode: .readWrite, on: driver) + try await DataWriteExecutor.run(plan, mode: .readWrite, commitMode: commitMode, on: driver) } let history = recordSuccessHistory( diff --git a/apple/TablePro/Core/DataGrid/DataGridModel.swift b/apple/TablePro/Core/DataGrid/DataGridModel.swift index fa3d21b8a6..86fb966261 100644 --- a/apple/TablePro/Core/DataGrid/DataGridModel.swift +++ b/apple/TablePro/Core/DataGrid/DataGridModel.swift @@ -15,6 +15,10 @@ protocol DataGridMountedView: AnyObject { func viewportSample() -> (row: Int, offset: CGFloat)? @discardableResult func beginEditing(row: Int, displayColumn: Int) -> Bool var isEditing: Bool { get } + // A viewer, the value filter or the foreign key preview: a reload closes or repoints each of them. + var holdsTransientUI: Bool { get } + // A live scroll or a selection drag, which a reload would move rows under. + var isTrackingInteraction: Bool { get } func commitEditing() -> Bool func discardEditing() @discardableResult func focus() -> Bool @@ -22,6 +26,11 @@ protocol DataGridMountedView: AnyObject { func anchor(forColumn displayColumn: Int) -> (view: NSView, rect: NSRect)? } +extension DataGridMountedView { + var holdsTransientUI: Bool { false } + var isTrackingInteraction: Bool { false } +} + @MainActor final class DataGridModel: ObservableObject { let id = UUID() @@ -240,6 +249,12 @@ final class DataGridModel: ObservableObject { setSelection(GridSelection(rectangles: rects, activeCell: active, anchor: active, mode: rects.isEmpty ? .cells : .rows)) } + // Takes back a selection kept by identity, so a cell cursor stays a cursor on its own column. + func select(_ snapshot: GridSelectionSnapshot) { + let resolved = snapshot.resolved(order: order, rows: rows, columns: columns) + setSelection(resolved.rectangles.isEmpty && resolved.activeCell == nil ? .empty : resolved) + } + func clearSelection() { setSelection(.empty) } @@ -578,6 +593,8 @@ final class DataGridModel: ObservableObject { } var isEditing: Bool { mountedView?.isEditing ?? false } + var holdsTransientUI: Bool { mountedView?.holdsTransientUI ?? false } + var isTrackingInteraction: Bool { mountedView?.isTrackingInteraction ?? false } // Owners that deferred work while an editor was open resume here. var onEditingEnded: (@MainActor () -> Void)? diff --git a/apple/TablePro/Core/DataGrid/Engine/GridSelectionSnapshot.swift b/apple/TablePro/Core/DataGrid/Engine/GridSelectionSnapshot.swift index 14e8eb047c..507d2d4f46 100644 --- a/apple/TablePro/Core/DataGrid/Engine/GridSelectionSnapshot.swift +++ b/apple/TablePro/Core/DataGrid/Engine/GridSelectionSnapshot.swift @@ -57,6 +57,35 @@ struct GridSelectionSnapshot: Equatable { self.mode = selection.mode } + var namedRowIDs: Set { + var named = Set(rowIDs) + for rectangle in rectangles { named.formUnion(rectangle.rowIDs) } + if let activeCell { named.insert(activeCell.rowID) } + if let anchor { named.insert(anchor.rowID) } + return named + } + + // A row the transform cannot place leaves, and a cell on it goes with it; the mode and columns stay. + func mappingRows(_ transform: (RowID) -> RowID?) -> GridSelectionSnapshot { + func cell(_ cell: Cell?) -> Cell? { + guard let cell, let rowID = transform(cell.rowID) else { return nil } + return Cell(rowID: rowID, columnID: cell.columnID) + } + var mapped = GridSelectionSnapshot( + rowIDs: rowIDs.compactMap(transform), + rectangles: rectangles.compactMap { rectangle in + let rows = rectangle.rowIDs.compactMap(transform) + return rows.isEmpty ? nil : Rectangle(rowIDs: rows, columnIDs: rectangle.columnIDs) + }, + activeCell: cell(activeCell), + anchor: cell(anchor) + ) + guard !mapped.isEmpty else { return .empty } + mapped.pickedColumnIDs = pickedColumnIDs + mapped.mode = mode + return mapped + } + func resolved(order: DataGridRowOrder, rows: TableRows, columns: DataGridColumnSet) -> GridSelection { func coord(_ cell: Cell?) -> GridCoord? { guard let cell, diff --git a/apple/TablePro/Core/DataGrid/GridViewportResolver.swift b/apple/TablePro/Core/DataGrid/GridViewportResolver.swift index 91dd9d2d77..ddfb7daee0 100644 --- a/apple/TablePro/Core/DataGrid/GridViewportResolver.swift +++ b/apple/TablePro/Core/DataGrid/GridViewportResolver.swift @@ -11,37 +11,74 @@ internal struct GridViewportSnapshot: Equatable { let firstVisiblePosition: Int let firstVisibleOffset: CGFloat let firstVisibleKey: [String: String]? - var selectedKeys: [[String: String]] = [] + // Row ids are positional per page, so each row the selection names travels as its key. + var selection: GridSelectionSnapshot = .empty + var selectionKeys: [RowID: [String: String]] = [:] static let top = GridViewportSnapshot(firstVisiblePosition: 0, firstVisibleOffset: 0, firstVisibleKey: nil) + var selectedKeys: [[String: String]] { + selection.rowIDs.compactMap { selectionKeys[$0] } + } + // For a re-read that starts at the first row but keeps what is selected. var selectionOnly: GridViewportSnapshot { - GridViewportSnapshot(firstVisiblePosition: 0, firstVisibleOffset: 0, firstVisibleKey: nil, selectedKeys: selectedKeys) + GridViewportSnapshot( + firstVisiblePosition: 0, + firstVisibleOffset: 0, + firstVisibleKey: nil, + selection: selection, + selectionKeys: selectionKeys + ) } } internal struct GridViewportPlacement: Equatable { let firstVisibleRow: RowID? let firstVisibleOffset: CGFloat - let selectedRows: [RowID] + let selection: GridSelectionSnapshot let revealsSelection: Bool + init( + firstVisibleRow: RowID?, + firstVisibleOffset: CGFloat, + selection: GridSelectionSnapshot, + revealsSelection: Bool + ) { + self.firstVisibleRow = firstVisibleRow + self.firstVisibleOffset = firstVisibleOffset + self.selection = selection + self.revealsSelection = revealsSelection + } + + init(firstVisibleRow: RowID?, firstVisibleOffset: CGFloat, selectedRows: [RowID], revealsSelection: Bool) { + self.init( + firstVisibleRow: firstVisibleRow, + firstVisibleOffset: firstVisibleOffset, + selection: selectedRows.isEmpty + ? .empty + : GridSelectionSnapshot(rowIDs: selectedRows, rectangles: [], activeCell: nil, anchor: nil), + revealsSelection: revealsSelection + ) + } + + var selectedRows: [RowID] { selection.rowIDs } + static let firstRow = GridViewportPlacement( firstVisibleRow: nil, firstVisibleOffset: 0, - selectedRows: [], + selection: .empty, revealsSelection: false ) } extension GridViewportPlacement { - func selecting(_ rows: [RowID]) -> GridViewportPlacement { - guard !rows.isEmpty else { return self } + func selecting(_ selection: GridSelectionSnapshot) -> GridViewportPlacement { + guard selection != .empty else { return self } return GridViewportPlacement( firstVisibleRow: firstVisibleRow, firstVisibleOffset: firstVisibleOffset, - selectedRows: rows, + selection: selection, revealsSelection: revealsSelection ) } @@ -60,21 +97,27 @@ internal enum GridViewportResolver { displayIDs: [RowID]?, firstVisibleDisplayRow: Int, firstVisibleOffset: CGFloat, - selectedDisplayRows: [Int] = [], + selection: GridSelectionSnapshot = .empty, keyColumns: [String], isCellModified: (RowID, Int) -> Bool = { _, _ in false } ) -> GridViewportSnapshot { let isKeyed = !keyColumns.isEmpty && tableRows.count <= keyedRowLimit let order = DataGridRowOrder(filteredIDs: displayIDs, revision: 0) - func key(_ displayRow: Int) -> [String: String]? { - order.row(at: displayRow, in: tableRows) - .flatMap { savedKey(of: $0, in: tableRows, keyColumns: keyColumns, isCellModified: isCellModified) } + func key(of row: Row?) -> [String: String]? { + row.flatMap { savedKey(of: $0, in: tableRows, keyColumns: keyColumns, isCellModified: isCellModified) } + } + var selectionKeys: [RowID: [String: String]] = [:] + if isKeyed { + for rowID in selection.namedRowIDs { + selectionKeys[rowID] = key(of: tableRows.row(withID: rowID)) + } } return GridViewportSnapshot( firstVisiblePosition: max(0, firstVisibleDisplayRow), firstVisibleOffset: max(0, firstVisibleOffset), - firstVisibleKey: isKeyed ? key(firstVisibleDisplayRow) : nil, - selectedKeys: isKeyed ? selectedDisplayRows.sorted().compactMap { key($0) } : [] + firstVisibleKey: isKeyed ? key(of: order.row(at: firstVisibleDisplayRow, in: tableRows)) : nil, + selection: isKeyed ? selection : .empty, + selectionKeys: selectionKeys ) } @@ -89,10 +132,10 @@ internal enum GridViewportResolver { switch intent { case .firstRow: return GridViewportPlacement.firstRow - .selecting(rows(matching: snapshot.selectedKeys, in: incoming, keyColumns: keyColumns)) + .selecting(remappedSelection(snapshot, in: incoming, keyColumns: keyColumns)) case .keepPlace: return keptPlace(from: snapshot, in: incoming, displayIDs: displayIDs, keyColumns: keyColumns) - .selecting(rows(matching: snapshot.selectedKeys, in: incoming, keyColumns: keyColumns)) + .selecting(remappedSelection(snapshot, in: incoming, keyColumns: keyColumns)) case .restoreRow(let anchor): guard let row = uniqueRow(matching: anchor, in: incoming) else { return .firstRow } return GridViewportPlacement( @@ -153,14 +196,14 @@ internal enum GridViewportResolver { } // One pass, so a large selection does not rescan per row; a repeated key matches nothing, as in uniqueRow(matching:in:). - private static func rows( - matching keys: [[String: String]], + private static func remappedSelection( + _ snapshot: GridViewportSnapshot, in incoming: TableRows, keyColumns: [String] - ) -> [RowID] { - guard !keys.isEmpty, !keyColumns.isEmpty, incoming.count <= keyedRowLimit else { return [] } + ) -> GridSelectionSnapshot { + guard !snapshot.selectionKeys.isEmpty, !keyColumns.isEmpty, incoming.count <= keyedRowLimit else { return .empty } let indices = keyColumns.compactMap { incoming.columns.firstIndex(of: $0) } - guard indices.count == keyColumns.count else { return [] } + guard indices.count == keyColumns.count else { return .empty } var rowByKey: [[String]: RowID] = [:] var repeated = Set<[String]>() @@ -171,11 +214,13 @@ internal enum GridViewportResolver { repeated.insert(values) } } - return keys.compactMap { key in + var incomingRows: [RowID: RowID] = [:] + for (outgoing, key) in snapshot.selectionKeys { let values = keyColumns.compactMap { key[$0] } - guard values.count == keyColumns.count, !repeated.contains(values) else { return nil } - return rowByKey[values] + guard values.count == keyColumns.count, !repeated.contains(values), let row = rowByKey[values] else { continue } + incomingRows[outgoing] = row } + return snapshot.selection.mappingRows { incomingRows[$0] } } private static func uniqueRow(matching values: [String: String], in incoming: TableRows) -> RowID? { diff --git a/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift b/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift index 2dda435413..102221c892 100644 --- a/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift +++ b/apple/TablePro/Core/DataWrite/DataWriteExecutor.swift @@ -103,12 +103,20 @@ enum DataWriteExecutor { nonisolated static func run( _ plan: DataWritePlan, mode: PluginTransactionAccessMode = .readWrite, + commitMode: ConnectionCommitMode, on driver: DatabaseDriver, context: PluginStatementContext? = nil ) async throws -> DataWriteRun { let steps = plan.steps.filter { !$0.statement.sql.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty } guard !steps.isEmpty || !plan.prologue.isEmpty else { return DataWriteRun(results: [], sideStatements: []) } + // A write Manual Commit can't hold would commit with no Commit pressed, so it is refused before anything runs. + if commitMode == .manual { + guard plan.prologue.isEmpty, plan.epilogue.isEmpty else { throw ManualCommitSessionOptionsRefusal() } + let state = await driver.heldSessionTransactionState() + try ManualCommit.refuseUnheldWrite(supportsTransactions: driver.supportsTransactions, sessionState: state) + } + // A row a lookup cannot find on its own refuses the save before anything is written. Each step reads its // lookup again right before it runs, since the steps ahead of it can move or change its rows. for step in steps { @@ -154,17 +162,18 @@ enum DataWriteExecutor { } } - /// Asked inside the same lease that runs the statements, so nothing can open a transaction - /// between the answer and the first write. + // Asked inside the lease that runs the statements, so nothing opens a transaction before the first write. let owner = WriteTransactionOwner.resolve( supportsTransactions: driver.supportsTransactions, - sessionState: await driver.heldSessionTransactionState() + sessionState: await driver.heldSessionTransactionState(), + commitMode: commitMode ) var results: [DataWriteStepResult] = [] do { - if owner.opensTransaction { + if owner.sendsBegin { try await driver.beginTransaction(mode: mode) + if owner == .held { ManualCommit.turnHold?.mark(true) } } for step in steps { @@ -217,14 +226,15 @@ enum DataWriteExecutor { } } - if owner.opensTransaction { + if owner.commitsAtEnd { try await driver.commitTransaction() } } catch { var rollbackSucceeded = owner.canRollBack - if owner.opensTransaction { + if owner.canRollBack { do { try await driver.rollbackTransaction() + if owner == .held { ManualCommit.turnHold?.mark(false) } } catch { rollbackSucceeded = false logger.error("Rollback failed: \(error.publicLogShape, privacy: .public)") @@ -242,7 +252,7 @@ enum DataWriteExecutor { committed: results, totalStatements: steps.count, engine: plan.databaseType.rawValue, - disposition: owner == .session ? .pendingInSessionTransaction : .written, + disposition: owner == .session || owner == .held ? .pendingInSessionTransaction : .written, underlying: error ) } @@ -370,7 +380,7 @@ enum DataWriteExecutor { actual: Int ) -> DataWriteError { switch owner { - case .app: + case .app, .held: return .tooManyRowsAffected(table: table, expected: expected, actual: actual) case .session: return .tooManyRowsAffectedInSessionTransaction(table: table, expected: expected, actual: actual) diff --git a/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift b/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift index dfca08ec8c..f1f1e8c49d 100644 --- a/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift +++ b/apple/TablePro/Core/DataWrite/Rewind/RewindExecutor.swift @@ -190,6 +190,7 @@ struct RewindExecutor { ) let route = DatabaseManager.shared.executionRoute(for: scope) + let commitMode = DatabaseManager.shared.commitMode(for: connection.id) let run = try await ExecutionGateProvider.shared.authorizing( OperationRequest( connectionId: connection.id, @@ -206,7 +207,7 @@ struct RewindExecutor { route: route, cancellation: .protectedWrite ) { driver in - try await DataWriteExecutor.run(writePlan, on: driver) + try await DataWriteExecutor.run(writePlan, commitMode: commitMode, on: driver) } } diff --git a/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift b/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift new file mode 100644 index 0000000000..2b9ae43802 --- /dev/null +++ b/apple/TablePro/Core/Database/Access/AgentTransactionGuard.swift @@ -0,0 +1,181 @@ +// +// AgentTransactionGuard.swift +// TablePro +// + +import Foundation +import os +import TableProPluginKit +import TableProSQLGrammar + +// What an agent's statement or script may do to the transaction its session holds, decided inside the lease. +internal struct AgentTransactionGuard: Sendable { + nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "AgentTransactionGuard") + internal static let savepointStatement = "SAVEPOINT tablepro_agent" + internal static let releaseStatement = "RELEASE SAVEPOINT tablepro_agent" + internal static let rollbackStatement = "ROLLBACK TO SAVEPOINT tablepro_agent" + + let commitMode: ConnectionCommitMode + let databaseType: DatabaseType + let grammar: SQLLexicalGrammar + let connectionId: UUID + + internal enum Protection: Equatable { + case none + case savepoint + case refuse + } + + internal struct Savepoint: Equatable, Sendable { + let open: String + let release: String? + let rollback: String + + static let standard = Savepoint(open: savepointStatement, release: releaseStatement, rollback: rollbackStatement) + + // SQL Server and Oracle have no RELEASE; a savepoint lasts until the transaction ends. + nonisolated static func of(_ family: TransactionEngineFamily) -> Savepoint? { + switch family { + case .postgres, .cockroach, .mysql, .sqlite: + return .standard + case .sqlServer: + return Savepoint( + open: "SAVE TRANSACTION tablepro_agent", release: nil, rollback: "ROLLBACK TRANSACTION tablepro_agent" + ) + case .oracle: + return Savepoint(open: savepointStatement, release: nil, rollback: rollbackStatement) + case .redshift, .duckdb, .redis, .other: + return nil + } + } + } + + // A call of several writes joining the user's open transaction is taken back whole when one fails, so no half + // of it waits for the user's Commit. + nonisolated internal static func savepoint( + family: TransactionEngineFamily, + protection: Protection, + sessionState: PluginSessionTransactionState, + asOneUnit: Bool + ) -> Savepoint? { + switch protection { + case .savepoint: + return Savepoint.of(family) + case .refuse: + return nil + case .none: + return asOneUnit && sessionState == .inTransaction ? Savepoint.of(family) : nil + } + } + + // A failed statement inside an open block aborts it on these engines, which takes the user's pending work with it. + nonisolated internal static func protection( + family: TransactionEngineFamily, + sessionState: PluginSessionTransactionState, + controlsTransaction: Bool + ) -> Protection { + guard !controlsTransaction, sessionState == .inTransaction, family.failedStatementAbortsTransaction else { + return .none + } + return family.takesBackAFailedStatement ? .savepoint : .refuse + } + + // Runs `body`, told whether this call opened the hold, and answers what the reply should say about the transaction. + nonisolated func running( + _ statements: [String], + writes: Bool, + asOneUnit: Bool = false, + on driver: DatabaseDriver, + _ body: (_ openedHold: Bool) async throws -> T + ) async throws -> (value: T, notice: String?) { + let controls = ManualCommit.controlsTransaction(statements, databaseType: databaseType, grammar: grammar) + if commitMode == .manual, controls { throw ManualCommitTransactionControlRefusal() } + let family = TransactionEngineFamily.of(databaseType) + let unseen = ManualCommit.mayEndTransactionUnseen(statements, databaseType: databaseType, grammar: grammar) + let readsState = asOneUnit || commitMode == .manual || controls || unseen + || family.failedStatementAbortsTransaction + let startState: PluginSessionTransactionState = readsState ? await driver.heldSessionTransactionState() : .unknown + // MySQL's START TRANSACTION commits the transaction already open; PostgreSQL's BEGIN warns and joins it. + if controls, startState.holdsUserTransaction, + ManualCommit.startsTransaction(statements, databaseType: databaseType, grammar: grammar) { + throw OpenTransactionRefusal() + } + // On an idle session the statement's own COMMIT would end the hold this call opens for its write. + if commitMode == .manual, unseen, writes || startState.holdsUserTransaction { + throw ManualCommitTransactionControlRefusal() + } + let protection = Self.protection(family: family, sessionState: startState, controlsTransaction: controls) + if protection == .refuse { throw AbortingTransactionRefusal() } + + let savepoint = Self.savepoint( + family: family, protection: protection, sessionState: startState, asOneUnit: asOneUnit + ) + + let opened = try await ManualCommit.openIfNeeded( + on: driver, statements: statements, databaseType: databaseType, grammar: grammar, commitMode: commitMode + ) + let value: T + do { + if let savepoint { + value = try await savepointed(savepoint, on: driver) { try await body(opened) } + } else { + value = try await body(opened) + } + } catch { + if opened { await ManualCommit.rollBackOpened(on: driver, connectionId: connectionId) } + throw error + } + let pendingMatters = commitMode == .manual && writes + guard pendingMatters || startState.holdsUserTransaction else { return (value, nil) } + let endState = await driver.heldSessionTransactionState() + let notice = Self.notice( + startState: startState, endState: endState, pendingMatters: pendingMatters, openedHold: opened + ) + return (value, notice) + } + + nonisolated internal static func notice( + startState: PluginSessionTransactionState, + endState: PluginSessionTransactionState, + pendingMatters: Bool, + openedHold: Bool + ) -> String? { + if startState.holdsUserTransaction || openedHold, endState == .idle { return ManualCommit.transactionEndedNotice } + return pendingMatters && endState.holdsUserTransaction ? ManualCommit.pendingNotice : nil + } + + nonisolated private func savepointed( + _ savepoint: Savepoint, + on driver: DatabaseDriver, + _ body: () async throws -> T + ) async throws -> T { + _ = try await driver.execute(query: savepoint.open) + let value: T + do { + value = try await body() + } catch { + await Self.rollBack(to: savepoint, on: driver, connectionId: connectionId) + throw error + } + guard let release = savepoint.release else { return value } + do { + _ = try await driver.execute(query: release) + } catch { + Self.logger.warning("Releasing the agent savepoint failed: \(error.publicLogShape, privacy: .public)") + } + return value + } + + @MainActor + private static func rollBack(to savepoint: Savepoint, on driver: DatabaseDriver, connectionId: UUID) async { + let token = DatabaseManager.shared.beginProtectedWrite(on: driver, for: connectionId) + defer { DatabaseManager.shared.endProtectedWrite(token, for: connectionId) } + do { + try await TaskCancellationShield.run { @Sendable in + _ = try await driver.execute(query: savepoint.rollback) + } + } catch { + logger.error("Rolling back to the agent savepoint failed: \(error.publicLogShape, privacy: .public)") + } + } +} diff --git a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift index dc9e221372..9f1efbed3c 100644 --- a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift +++ b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge+Scripts.swift @@ -114,6 +114,13 @@ extension DatabaseAccessBridge { let manager = await databaseManager let route = await MainActor.run { manager.executionRoute(for: scope) } + let commitMode = await MainActor.run { manager.commitMode(for: connectionId) } + let statementTexts = batches.flatMap { $0.statements.map(\.sql) } + let agentGuard = AgentTransactionGuard( + commitMode: commitMode, databaseType: databaseType, grammar: databaseType.lexicalGrammar, + connectionId: connectionId + ) + let writes = classification.tier != .safe let startLines = BatchErrorText.lines(of: batches.map(\.range.location), in: text) let startTime = CFAbsoluteTimeGetCurrent() let statementsRan = CatalogEvent.statementsRan( @@ -134,16 +141,22 @@ extension DatabaseAccessBridge { onLateCompletion: { CatalogChangeService.post(statementsRan) } ) { driver in reach.mark() - do { - let answered = try await ScriptBatchRun.run( - batches, startLines: startLines, rowCap: rowCap, driver: driver, progress: progress, context: context - ) - await manager.noteSessionStatements(Self.ranStatements(of: batches, progress), ranOn: driver, scope: scope) - return answered - } catch { - await manager.noteSessionStatements(Self.ranStatements(of: batches, progress), ranOn: driver, scope: scope) - throw error + let ran = try await agentGuard.running(statementTexts, writes: writes, on: driver) { opened in + do { + let answered = try await ScriptBatchRun.run( + batches, startLines: startLines, rowCap: rowCap, driver: driver, progress: progress, + context: context, commitMode: commitMode, heldByApp: opened + ) + await manager.noteSessionStatements(Self.ranStatements(of: batches, progress), ranOn: driver, scope: scope) + return answered + } catch { + await manager.noteSessionStatements(Self.ranStatements(of: batches, progress), ranOn: driver, scope: scope) + throw error + } } + var run = ran.value + run.transactionNotice = ran.notice + return run } } catch { if classification.tier != .safe { @@ -234,6 +247,8 @@ final class ScriptBatchProgress: Sendable { struct ScriptBatchRun: Sendable { let answers: [QueryBatchResult] let sessionState: PluginSessionTransactionState + // What the agent is told instead of "commit or roll it back": the work is pending, or the transaction ended. + var transactionNotice: String? /// The session is asked what it holds before and after, as the editor asks: the first answer decides how a /// failure reads, and the second whether the script left a transaction open. A batch that raised an error ends the @@ -244,11 +259,15 @@ struct ScriptBatchRun: Sendable { rowCap: Int, driver: DatabaseDriver, progress: ScriptBatchProgress, - context: PluginStatementContext? = nil + context: PluginStatementContext? = nil, + commitMode: ConnectionCommitMode = .automatic, + heldByApp: Bool = false ) async throws -> ScriptBatchRun { let startState = await driver.heldSessionTransactionState() progress.begin(in: startState) - let plan = BatchTransactionPlan.autocommit.joining(startState) + let plan: BatchTransactionPlan = heldByApp + ? .heldTransaction + : BatchTransactionPlan.autocommit.joining(startState, commitMode: commitMode) var answers: [QueryBatchResult] = [] for (batch, startLine) in zip(batches, startLines) { try Task.checkCancellation() @@ -310,9 +329,10 @@ struct ScriptBatchRun: Sendable { primary.columnOrigins = first?.columnOrigins let notes = [ first?.statusMessage, + transactionNotice, BatchRunNotice.text( discardedResultSetCount: answers.reduce(0) { $0 + $1.discardedResultSetCount }, - sessionState: sessionState + sessionState: transactionNotice == nil ? sessionState : .idle ) ].compactMap { $0 } primary.statusMessage = notes.isEmpty ? nil : notes.joined(separator: " ") diff --git a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift index 58b00eb116..39315ccd3c 100644 --- a/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift +++ b/apple/TablePro/Core/Database/Access/DatabaseAccessBridge.swift @@ -397,16 +397,22 @@ internal actor DatabaseAccessBridge { let manager = await databaseManager let route = await MainActor.run { manager.executionRoute(for: scope) } + let commitMode = await MainActor.run { manager.commitMode(for: connectionId) } + let agentGuard = AgentTransactionGuard( + commitMode: commitMode, databaseType: databaseType, grammar: grammar, connectionId: connectionId + ) + let writes = classification.tier != .safe let startTime = CFAbsoluteTimeGetCurrent() let statementRan = CatalogEvent.statementsRan( connectionId: connectionId, statements: [sql], databaseType: databaseType ) // A write that timed out or failed may still have committed, so the catalog is refreshed rather than trusted. - let result: QueryResult + var result: QueryResult let tableEdits: SucceededStatements? + let transactionNotice: String? do { - (result, tableEdits) = try await runRacingTimeout( + (result, tableEdits, transactionNotice) = try await runRacingTimeout( scope: scope, route: route, policy: policy, @@ -415,18 +421,20 @@ internal actor DatabaseAccessBridge { onLateCompletion: { CatalogChangeService.post(statementRan) } ) { driver in reach.mark() - // Before it runs, so a statement that fails or a connection lost under it is still noted. - await manager.noteSessionStatements([sql], ranOn: driver, scope: scope) - let answer = try await driver.executeUserQuery( - query: sql, - rowCap: fetchCap, - parameters: statement.parameters.isEmpty ? nil : statement.boundValues, - context: context - ) + let ran = try await agentGuard.running([sql], writes: writes, on: driver) { _ in + // Before it runs, so a statement that fails or a connection lost under it is still noted. + await manager.noteSessionStatements([sql], ranOn: driver, scope: scope) + return try await driver.executeUserQuery( + query: sql, + rowCap: fetchCap, + parameters: statement.parameters.isEmpty ? nil : statement.boundValues, + context: context + ) + } let edits = await SucceededStatements.single( sql, scope: scope, databaseType: databaseType, grammar: grammar, ranOn: driver ) - return (answer, edits) + return (ran.value, edits, ran.notice) } } catch { if classification.tier != .safe { @@ -439,6 +447,9 @@ internal actor DatabaseAccessBridge { if let tableEdits { CatalogChangeService.post(.statementsSucceeded(tableEdits)) } + if let transactionNotice { + result.statusMessage = [result.statusMessage, transactionNotice].compactMap { $0 }.joined(separator: " ") + } let executionTimeMs = (CFAbsoluteTimeGetCurrent() - startTime) * 1_000 let rules = await maskingPolicy(for: connectionId) let applied = await MainActor.run { masking.resolved(in: scope, databaseType: databaseType) } @@ -546,6 +557,14 @@ internal actor DatabaseAccessBridge { let manager = await databaseManager // No cancel handler: the inserts run under a `.protectedWrite` lease, which nothing may abort. let route = await MainActor.run { manager.executionRoute(for: scope) } + let agentGuard = await MainActor.run { + AgentTransactionGuard( + commitMode: manager.commitMode(for: scope.connectionId), + databaseType: databaseType, + grammar: SQLLexicalResolver.executionGrammar(for: databaseType, connectionId: scope.connectionId), + connectionId: scope.connectionId + ) + } let statements = operation.statements let context = operation.context return try await manager.withScopedDriver( @@ -554,16 +573,22 @@ internal actor DatabaseAccessBridge { cancellation: .protectedWrite ) { driver -> Int in reach.mark() - var affected = 0 - for statement in statements { - let result = try await driver.executeParameterized( - query: statement.sql, - parameters: statement.boundValues, - context: context - ) - affected += max(result.rowsAffected, 0) + // The hold and savepoint execute_query gets, so a Manual Commit insert waits for Commit too. + let ran = try await agentGuard.running( + statements.map(\.sql), writes: true, asOneUnit: statements.count > 1, on: driver + ) { _ in + var affected = 0 + for statement in statements { + let result = try await driver.executeParameterized( + query: statement.sql, + parameters: statement.boundValues, + context: context + ) + affected += max(result.rowsAffected, 0) + } + return affected } - return affected + return ran.value } } @@ -632,6 +657,10 @@ internal actor DatabaseAccessBridge { guard supported else { throw DatabaseAccessError.dataSourceError(String(localized: "This engine does not support transactions.")) } + // The user commits a Manual Commit connection's work; an agent's begin, commit or rollback would end or bury it. + guard await MainActor.run(body: { manager.commitMode(for: scope.connectionId) }) == .automatic else { + throw DatabaseAccessError.dataSourceError(ManualCommit.transactionControlRefusalMessage) + } try await manager.withScopedDriver( scope: scope, route: .sessionDriver, @@ -639,7 +668,12 @@ internal actor DatabaseAccessBridge { ) { driver in reach.mark() switch action { - case "begin": try await driver.beginTransaction(mode: .readWrite) + case "begin": + // MySQL's START TRANSACTION commits a transaction already open; PostgreSQL's BEGIN warns and joins it. + guard await driver.heldSessionTransactionState() == .idle else { + throw DatabaseAccessError.dataSourceError(ManualCommit.openTransactionRefusalMessage) + } + try await driver.beginTransaction(mode: .readWrite) case "commit": try await driver.commitTransaction() case "rollback": try await driver.rollbackTransaction() default: @@ -731,7 +765,7 @@ internal actor DatabaseAccessBridge { ) { driver in reach.mark() try await before(driver, ownsConnection) - return try await DataWriteExecutor.run(plan, on: driver, context: context) + return try await DataWriteExecutor.run(plan, commitMode: .automatic, on: driver, context: context) } } @@ -970,7 +1004,8 @@ internal enum ServerErrorText { // The app's own refusals, timeouts and cancels name no value; whatever a driver or a server wrote might. static func mayRepeatAValue(_ error: Error) -> Bool { switch error { - case is CancellationError, is DatabaseAccessError, is ExternalStatementGateError, is ShortcutsActionError: + case is CancellationError, is DatabaseAccessError, is ExternalStatementGateError, is ShortcutsActionError, + is any ManualCommitRefusing: return false case let failure as DatabaseError: if case .queryFailed = failure { return true } diff --git a/apple/TablePro/Core/Database/DatabaseDriver.swift b/apple/TablePro/Core/Database/DatabaseDriver.swift index 5bae9e93a9..7b0a216512 100644 --- a/apple/TablePro/Core/Database/DatabaseDriver.swift +++ b/apple/TablePro/Core/Database/DatabaseDriver.swift @@ -348,6 +348,11 @@ protocol DatabaseDriver: AnyObject, Sendable { /// Fetch the source of one trigger. func fetchTriggerDDL(_ trigger: TriggerInfo) async throws -> String + // Mirrors `PluginDatabaseDriver.serverObjectKinds`: asked of the live driver, never of the type. + var serverObjectKinds: [PluginServerObjectKind] { get } + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [ServerObjectInfo] + func fetchServerObject(_ object: ServerObjectInfo) async throws -> ServerObjectInfo + /// Fetch metadata for a specific database (table count, size, etc.) func fetchDatabaseMetadata(_ database: String) async throws -> DatabaseMetadata @@ -1032,6 +1037,17 @@ extension DatabaseDriver { throw PluginObjectSourceError.unsupported(trigger.name) } + var serverObjectKinds: [PluginServerObjectKind] { [] } + + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [ServerObjectInfo] { [] } + + func fetchServerObject(_ object: ServerObjectInfo) async throws -> ServerObjectInfo { + guard let definition = object.definition, !definition.isEmpty else { + throw PluginObjectSourceError.unsupported(object.name) + } + return object + } + var supportsTransactions: Bool { true } var hasLostConnection: Bool { false } diff --git a/apple/TablePro/Core/Database/DatabaseManager+Metadata.swift b/apple/TablePro/Core/Database/DatabaseManager+Metadata.swift index d10ea0b38b..643ff155dc 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Metadata.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Metadata.swift @@ -16,6 +16,12 @@ protocol ScopedMetadataProviding: AnyObject, Sendable { _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> T + func withMetadataRead( + scope: DatabaseScope, + workload: MetadataConnectionPool.Workload, + _ body: @Sendable @escaping (DatabaseDriver) async throws -> T + ) async throws -> T + func browseScope(for connectionId: UUID) -> DatabaseScope? } @@ -27,6 +33,15 @@ extension ScopedMetadataProviding { try await withMetadataDriver(scope: scope, workload: .interactive, body) } + // A conformer with no session turns has no transaction revision to keep. + func withMetadataRead( + scope: DatabaseScope, + workload: MetadataConnectionPool.Workload, + _ body: @Sendable @escaping (DatabaseDriver) async throws -> T + ) async throws -> T { + try await withMetadataDriver(scope: scope, workload: workload, body) + } + /// For reads that belong to the sidebar rather than to a tab: the object list, the /// database and quick switchers, and the AI schema context. Completion is not one of /// them any more: a query tab completes against its own scope. @@ -38,7 +53,7 @@ extension ScopedMetadataProviding { guard let scope = browseScope(for: connectionId) else { throw DatabaseError.notConnected } - return try await withMetadataDriver(scope: scope, workload: workload, body) + return try await withMetadataRead(scope: scope, workload: workload, body) } } @@ -59,4 +74,21 @@ extension DatabaseManager { body ) } + + // Reads only: on the session driver its turn leaves an open transaction's revision alone, unless the state moved. + func withMetadataRead( + scope: DatabaseScope, + workload: MetadataConnectionPool.Workload = .interactive, + _ body: @Sendable @escaping (DatabaseDriver) async throws -> T + ) async throws -> T { + try await withScopedDriver( + scope: scope, + route: metadataRoute(for: scope), + workload: workload, + cancellation: .untracked, + ownsSessionState: false, + readsOnly: true, + body + ) + } } diff --git a/apple/TablePro/Core/Database/DatabaseManager+OpenTransactions.swift b/apple/TablePro/Core/Database/DatabaseManager+OpenTransactions.swift index cdd8268df7..6739a615f7 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+OpenTransactions.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+OpenTransactions.swift @@ -8,10 +8,8 @@ import Foundation import TableProPluginKit extension DatabaseManager { - /// A connection running a turn is skipped, because asking it waits for that turn. One that has - /// not answered within a second counts as holding nothing, so a close never hangs on a busy or - /// dead server. An aborted transaction counts: a savepoint taken before the failure can still - /// bring its earlier work back to a commit. + // A busy lane is not asked and a slow one counts as idle, so a close never hangs; what the tracker + // last read covers both. An aborted transaction counts: a savepoint can still bring earlier work back. internal func databasesHoldingTransaction( for connectionId: UUID, among databases: Set? = nil, @@ -37,6 +35,7 @@ extension DatabaseManager { state: Task { await Self.boundedTransactionState(of: candidate.driver, clock: clock) } ) } + let browsedDatabase = session.resolvedBrowseDatabase var holding: [String] = [] for read in reads { let state = await read.state.value @@ -44,6 +43,12 @@ extension DatabaseManager { holding.append(read.database) } } + let tracked = sessionTransactions.holds(for: connectionId) + .map { $0.key.lane ?? browsedDatabase } + .filter { databases?.contains($0) ?? true } + for database in tracked where !holding.contains(database) { + holding.append(database) + } return holding } diff --git a/apple/TablePro/Core/Database/DatabaseManager+Principals.swift b/apple/TablePro/Core/Database/DatabaseManager+Principals.swift index 12680f123e..d3b63817f1 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Principals.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Principals.swift @@ -79,15 +79,14 @@ extension DatabaseManager { rollsBack: Bool, connectionId: UUID ) async throws { - /// No transaction is opened over one the session already holds: on this shared session an - /// app-owned `COMMIT` commits the user's pending work, and MySQL's `START TRANSACTION` - /// commits it implicitly. Joining it instead leaves the statements pending, which is what - /// the failure then reports. + // Joins a transaction the session holds: an app COMMIT, or MySQL's START TRANSACTION, would commit the user's work. + // `.automatic`: this runs outside a session turn, so a hold it opened would never reach the bar. let owner = WriteTransactionOwner.resolve( supportsTransactions: driver.supportsTransactions, - sessionState: await driver.heldSessionTransactionState() + sessionState: await driver.heldSessionTransactionState(), + commitMode: .automatic ) - let useTransaction = owner.opensTransaction && rollsBack + let useTransaction = owner.sendsBegin && owner.commitsAtEnd && rollsBack if useTransaction { try await driver.beginTransaction(mode: .readWrite) } diff --git a/apple/TablePro/Core/Database/DatabaseManager+Schema.swift b/apple/TablePro/Core/Database/DatabaseManager+Schema.swift index 01ab285459..f08b0e99c2 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Schema.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Schema.swift @@ -42,7 +42,8 @@ extension DatabaseManager { _ script: SchemaChangeScript, databaseType: DatabaseType, scope: DatabaseScope, - gate: any ExecutionGate = ExecutionGateProvider.shared + gate: any ExecutionGate = ExecutionGateProvider.shared, + holdConfirmation: SchemaChangeHoldConfirmation = DatabaseManager.confirmSchemaChangeBesideHold ) async throws { let route = schemaChangeRoute(for: scope) let statements = script.statements @@ -52,6 +53,8 @@ extension DatabaseManager { if let denial = await gate.authorize(request).denialError { throw denial } + try await confirmSchemaChange(on: route, connectionId: scope.connectionId, confirmation: holdConfirmation) + let isPooled = route.isPooled let executionTimes: [TimeInterval] do { @@ -61,7 +64,7 @@ extension DatabaseManager { cancellation: .protectedWrite ) { driver in try await Self.refuseBeforeWriting(script, scope: scope, on: driver) - let useTransaction = Self.wrapsDDLInTransaction(driver) + let useTransaction = await Self.wrapsSchemaChange(on: driver, isPooled: isPooled) if useTransaction { try await driver.beginTransaction(mode: schemaKind.declaresWrite ? .readWrite : .serverDefault) } @@ -231,10 +234,12 @@ extension DatabaseManager { statements: [String], databaseType: DatabaseType, scope: DatabaseScope, - gate: any ExecutionGate = ExecutionGateProvider.shared + gate: any ExecutionGate = ExecutionGateProvider.shared, + holdConfirmation: SchemaChangeHoldConfirmation = DatabaseManager.confirmSchemaChangeBesideHold ) async throws { guard !statements.isEmpty else { return } let route = schemaChangeRoute(for: scope) + let isPooled = route.isPooled let script = statements.map { $0.hasSuffix(";") ? $0 : $0 + ";" }.joined(separator: "\n\n") let authorization = await gate.authorize( @@ -249,6 +254,7 @@ extension DatabaseManager { ) ) if let denial = authorization.denialError { throw denial } + try await confirmSchemaChange(on: route, connectionId: scope.connectionId, confirmation: holdConfirmation) let executionTimes: [TimeInterval] do { @@ -257,7 +263,8 @@ extension DatabaseManager { route: route, cancellation: .protectedWrite ) { driver in - let useTransaction = Self.wrapsDDLInTransaction(driver) && statements.count > 1 + let wraps = await Self.wrapsSchemaChange(on: driver, isPooled: isPooled) + let useTransaction = wraps && statements.count > 1 if useTransaction { try await driver.beginTransaction(mode: .readWrite) } diff --git a/apple/TablePro/Core/Database/DatabaseManager+SchemaChangeHold.swift b/apple/TablePro/Core/Database/DatabaseManager+SchemaChangeHold.swift new file mode 100644 index 0000000000..3b5711b168 --- /dev/null +++ b/apple/TablePro/Core/Database/DatabaseManager+SchemaChangeHold.swift @@ -0,0 +1,45 @@ +// +// DatabaseManager+SchemaChangeHold.swift +// TablePro +// + +import AppKit +import Foundation + +typealias SchemaChangeHoldConfirmation = @MainActor @Sendable () async -> Bool + +extension DatabaseManager { + // On its own connection a schema change queues behind the open transaction's locks, and Stop cannot reach it. + static let confirmSchemaChangeBesideHold: SchemaChangeHoldConfirmation = { + await AlertHelper.confirm( + title: String(localized: "A Transaction Is Open"), + message: String( + localized: "This change runs on its own connection and can wait until you commit or roll back the open transaction." + ), + confirmButton: String(localized: "Continue") + ) + } + + func confirmSchemaChange( + on route: ScopedDriverRoute, + connectionId: UUID, + confirmation: SchemaChangeHoldConfirmation + ) async throws { + guard route.isPooled, !sessionTransactions.holds(for: connectionId).isEmpty else { return } + guard await confirmation() else { + throw ExecutionGateError.cancelledByUser(String(localized: "The change did not run.")) + } + } + + // A BEGIN on the session driver joins what a tab left open and its COMMIT ends that, so the DDL joins it instead. + // A trigger plan already chose its transactional strategy, so it skips the DDL check. + nonisolated static func wrapsSchemaChange( + on driver: DatabaseDriver, + isPooled: Bool, + checksTransactionalDDL: Bool = true + ) async -> Bool { + if checksTransactionalDDL, !wrapsDDLInTransaction(driver) { return false } + guard !isPooled else { return true } + return await driver.heldSessionTransactionState().permitsAppTransaction + } +} diff --git a/apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift b/apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift index 2189070fbf..eba21431b8 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift @@ -115,6 +115,7 @@ extension DatabaseManager { workload: MetadataConnectionPool.Workload = .interactive, cancellation: DriverCancellationPolicy, ownsSessionState: Bool = true, + readsOnly: Bool = false, _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> T { let leased = trackedLease(for: scope.connectionId, cancellation: cancellation, body) @@ -126,7 +127,9 @@ extension DatabaseManager { scope: scope, workload: workload, leased ) case .sessionDriver: - return try await withPinnedSessionDriver(scope: scope, ownsSessionState: ownsSessionState, leased) + return try await withPinnedSessionDriver( + scope: scope, ownsSessionState: ownsSessionState, readsOnly: readsOnly, leased + ) } } @@ -137,6 +140,7 @@ extension DatabaseManager { route: ScopedDriverRoute, workload: MetadataConnectionPool.Workload = .interactive, ownsSessionState: Bool = true, + readsOnly: Bool = false, _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> T { let owner = DriverLeaseOwner() @@ -148,6 +152,7 @@ extension DatabaseManager { workload: workload, cancellation: .cancellableRead(owner), ownsSessionState: ownsSessionState, + readsOnly: readsOnly, body ) } onCancel: { @@ -172,7 +177,7 @@ extension DatabaseManager { guard rowCountRunsWithPage(scope, filtered: filtered) else { return try await withMetadataDriver(scope: scope, workload: .bulk, body) } - return try await withCancellableRead(scope: scope, route: tableReadRoute(for: scope), body) + return try await withCancellableRead(scope: scope, route: tableReadRoute(for: scope), readsOnly: true, body) } /// A table tab's read is a SELECT the app built from the tab's own table, so it depends on @@ -371,9 +376,10 @@ extension DatabaseManager { private func withPinnedSessionDriver( scope: DatabaseScope, ownsSessionState: Bool, + readsOnly: Bool, _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> T { - try await withSessionDriverTurn(scope: scope, ownsSessionState: ownsSessionState) { driver in + try await withSessionDriverTurn(scope: scope, ownsSessionState: ownsSessionState, readsOnly: readsOnly) { driver in try await pin(driver, to: scope) return try await body(driver) } @@ -386,7 +392,7 @@ extension DatabaseManager { scope: DatabaseScope, _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> TableReadTurn { - try await withSessionDriverTurn(scope: scope, ownsSessionState: false) { driver in + try await withSessionDriverTurn(scope: scope, ownsSessionState: false, readsOnly: true) { driver in let route = tableReadRoute(for: scope) guard route == .sessionDriver else { return .moved(route) } try await pin(driver, to: scope) @@ -400,6 +406,7 @@ extension DatabaseManager { private func withSessionDriverTurn( scope: DatabaseScope, ownsSessionState: Bool, + readsOnly: Bool = false, _ turn: (DatabaseDriver) async throws -> R ) async throws -> R { let connectionId = scope.connectionId @@ -441,7 +448,9 @@ extension DatabaseManager { if ownsSessionState, let lost = sessionLanes.takeAnyLoss(for: connectionId) { throw DatabaseError.connectionFailed(Self.sessionLostMessage(database: lost.database, loss: lost.loss)) } - return try await recordingTransaction(on: browsedDriver, for: .init(connectionId: connectionId, lane: nil)) { + return try await recordingTransaction( + on: browsedDriver, for: .init(connectionId: connectionId, lane: nil), readsOnly: readsOnly + ) { try await turn(browsedDriver) } } @@ -463,7 +472,9 @@ extension DatabaseManager { } sessionLanes.noteOpenedUse(database: lane, for: connectionId) defer { sessionLanes.noteOpenedUse(database: lane, for: connectionId) } - return try await recordingTransaction(on: laneDriver, for: .init(connectionId: connectionId, lane: lane)) { + return try await recordingTransaction( + on: laneDriver, for: .init(connectionId: connectionId, lane: lane), readsOnly: readsOnly + ) { try await turn(laneDriver) } } @@ -476,17 +487,20 @@ extension DatabaseManager { private func recordingTransaction( on driver: DatabaseDriver, for key: SessionTransactionTracker.Key, + readsOnly: Bool, _ work: () async throws -> R ) async throws -> R { guard driver.supportsTransactions else { return try await work() } + let turnHold = ManualCommitTurnHold() let outcome: Result do { - outcome = .success(try await work()) + outcome = .success(try await ManualCommit.$turnHold.withValue(turnHold) { try await work() }) } catch { outcome = .failure(error) } - let state = await Self.boundedTransactionState(of: driver, clock: ContinuousClock()) - sessionTransactions.record(state, on: driver, for: key) + let read = await Self.boundedTransactionState(of: driver, clock: ContinuousClock()) + let state = ManualCommit.recordedState(read: read, turnOpenedHold: turnHold.isHeld) + sessionTransactions.record(state, on: driver, for: key, readsOnly: readsOnly) return try outcome.get() } diff --git a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift index 36c19de2ac..344b433c13 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+Sessions.swift @@ -151,6 +151,7 @@ extension DatabaseManager { from: connection, preservingSessionFieldsFrom: session.connection ) + session.commitMode = session.connection.commitMode session.driver = driver session.status = driver.status session.effectiveConnection = effectiveConnection @@ -345,6 +346,7 @@ extension DatabaseManager { guard let stored = connectionStorage.loadConnection(id: connection.id) else { return connection } var resolved = connection resolved.preferredSafeModeLevel = stored.preferredSafeModeLevel + resolved.commitMode = stored.commitMode return resolved } @@ -916,6 +918,7 @@ extension DatabaseManager { let stored = connectionStorage.loadConnection(id: id) else { continue } adoptDisplayFields(from: stored, into: session, for: id) setSafeModeLevel(stored.preferredSafeModeLevel, for: id) + setCommitMode(stored.commitMode, for: id) } } @@ -949,6 +952,7 @@ extension DatabaseManager { ) -> DatabaseConnection { var adopted = connection(attempted, adoptingDisplayFieldsFrom: current) adopted.preferredSafeModeLevel = current.preferredSafeModeLevel + adopted.commitMode = current.commitMode return adopted } @@ -1004,6 +1008,22 @@ extension DatabaseManager { _ = connectionStorage.updateSafeModeLevel(level, for: connectionId) } + // Written to the connection, as the Safe Mode level is, so the form and the Database menu are one setting. + func setCommitMode(_ mode: ConnectionCommitMode, for connectionId: UUID) { + guard var session = activeSessions[connectionId] else { return } + guard session.connection.commitMode != mode || session.commitMode != mode else { return } + session.connection.commitMode = mode + session.commitMode = mode + setSession(session, for: connectionId) + connectionStorage.mutateConnections(ids: [connectionId]) { $0.commitMode = mode } + } + + // What writes run under: a stored Manual Commit on an engine that can't hold runs as automatic. + func commitMode(for connectionId: UUID) -> ConnectionCommitMode { + guard let session = activeSessions[connectionId] else { return .automatic } + return ManualCommit.effectiveMode(session.commitMode, databaseType: session.connection.type) + } + internal func setSession(_ session: ConnectionSession, for connectionId: UUID) { /// A session created while a window is already in Agent mode takes the level stored on the /// connection, which is the user's own and may be Silent. Applying the floor only when the diff --git a/apple/TablePro/Core/Database/SessionTransactionTracker.swift b/apple/TablePro/Core/Database/SessionTransactionTracker.swift index 1cd91819ea..1471a74411 100644 --- a/apple/TablePro/Core/Database/SessionTransactionTracker.swift +++ b/apple/TablePro/Core/Database/SessionTransactionTracker.swift @@ -28,8 +28,7 @@ internal final class SessionTransactionTracker: ObservableObject { internal struct Hold: Identifiable, Equatable { let key: Key let state: State - /// Moves with every turn recorded on the lane, so an action confirmed after other work ran - /// there is refused rather than ending a transaction the user has not seen. + // Moves with every turn that could have changed the transaction, so a confirmation made before it is refused. let revision: Int var id: Key { key } @@ -43,10 +42,17 @@ internal final class SessionTransactionTracker: ObservableObject { var state: State } + // Whether a lane's driver ever answered with a definite state; a timed-out read keeps an earlier yes. + private struct Reader { + weak var driver: AnyObject? + var answers: Bool + } + @Published private(set) var holds: [Key: Hold] = [:] private var drivers: [Key: Entry] = [:] private var revisions: [Key: Int] = [:] + private var readers: [Key: Reader] = [:] private let isLive: @MainActor (Key, DatabaseDriver) -> Bool internal init(isLive: @escaping @MainActor (Key, DatabaseDriver) -> Bool) { @@ -64,6 +70,10 @@ internal final class SessionTransactionTracker: ObservableObject { transactionState(connectionId: connectionId, container: container) != nil } + internal func hold(connectionId: UUID, container: String) -> Hold? { + holds(for: connectionId).first { $0.key.lane == nil || $0.key.lane == container } + } + // An aborted hold wins, because it is the one that blocks every statement until a rollback. internal func transactionState(connectionId: UUID, container: String) -> State? { let states = holds.values @@ -73,7 +83,7 @@ internal final class SessionTransactionTracker: ObservableObject { return states.isEmpty ? nil : .open } - // Moves with every turn recorded on any of the connection's lanes. + // Moves with every turn on any of the connection's lanes, except a read-only one that changed nothing. internal func turnRevision(for connectionId: UUID) -> Int { revisions.reduce(0) { total, entry in entry.key.connectionId == connectionId ? total &+ entry.value : total @@ -89,33 +99,70 @@ internal final class SessionTransactionTracker: ObservableObject { holds[hold.key] == hold } - /// An engine that cannot say keeps what an earlier answer from the same driver said, since a - /// refresh never clears what it refreshes; from another driver it says nothing at all. - internal func record(_ state: PluginSessionTransactionState, on driver: DatabaseDriver, for key: Key) { + // An engine that cannot say keeps what an earlier answer from the same driver said. A read-only turn + // moves the revision only when the state it read differs, so a background read never voids a confirmation. + internal func record( + _ state: PluginSessionTransactionState, + on driver: DatabaseDriver, + for key: Key, + readsOnly: Bool = false + ) { // A turn can outlive its session's teardown; its late answer must not touch the replacement's hold. guard isLive(key, driver) else { if drivers[key]?.driver === driver { clear(key) } return } - revisions[key, default: 0] &+= 1 + noteReader(driver, answered: state != .unknown, for: key) + let next = heldState(after: state, from: driver, for: key) + let unchanged = next == drivers[key]?.state && (next == nil || drivers[key]?.driver === driver) + if !(readsOnly && unchanged) { + revisions[key, default: 0] &+= 1 + } + if let next { + set(next, on: driver, for: key) + } else { + clear(key) + } + } + + // True once the live driver of a lane the container runs on has only ever answered `.unknown`. + internal func cannotReportState(connectionId: UUID, container: String) -> Bool { + readers.contains { key, reader in + guard key.connectionId == connectionId, key.lane == nil || key.lane == container, + let driver = reader.driver as? DatabaseDriver, isLive(key, driver) + else { return false } + return !reader.answers + } + } + + private func heldState( + after state: PluginSessionTransactionState, + from driver: DatabaseDriver, + for key: Key + ) -> State? { switch state { case .inTransaction: - set(.open, on: driver, for: key) + return .open case .abortedTransaction: - set(.aborted, on: driver, for: key) + return .aborted case .idle, .holdsSessionLocks: - clear(key) + return nil case .unknown: - if drivers[key]?.driver !== driver { - clear(key) - } else if let entry = drivers[key] { - set(entry.state, on: driver, for: key) - } + guard let entry = drivers[key], entry.driver === driver else { return nil } + return entry.state @unknown default: - clear(key) + return nil } } + private func noteReader(_ driver: DatabaseDriver, answered: Bool, for key: Key) { + if let reader = readers[key], reader.driver === driver { + if answered { readers[key]?.answers = true } + return + } + readers[key] = Reader(driver: driver, answers: answered) + } + /// Drops every hold whose connection is not the lane's live one any more: a disconnect, a /// reconnect onto a new driver, a closed entry or a lost parked connection all end the /// transaction on the server. @@ -124,6 +171,10 @@ internal final class SessionTransactionTracker: ObservableObject { if let driver = entry.driver as? DatabaseDriver, isLive(key, driver) { continue } clear(key) } + for (key, reader) in readers where key.connectionId == connectionId { + if let driver = reader.driver as? DatabaseDriver, isLive(key, driver) { continue } + readers.removeValue(forKey: key) + } } private func set(_ state: State, on driver: DatabaseDriver, for key: Key) { diff --git a/apple/TablePro/Core/Database/TriggerEditing.swift b/apple/TablePro/Core/Database/TriggerEditing.swift index 8a427c1bfd..69942fbfcd 100644 --- a/apple/TablePro/Core/Database/TriggerEditing.swift +++ b/apple/TablePro/Core/Database/TriggerEditing.swift @@ -128,7 +128,8 @@ enum TriggerEditing { isEdit: Bool, originalName: String?, originalDefinition: String?, - gate: any ExecutionGate = ExecutionGateProvider.shared + gate: any ExecutionGate = ExecutionGateProvider.shared, + holdConfirmation: SchemaChangeHoldConfirmation = DatabaseManager.confirmSchemaChangeBesideHold ) async throws { let plan = try await DatabaseManager.shared.withMetadataDriver(scope: scope) { driver in TriggerApplyPlan( @@ -159,6 +160,11 @@ enum TriggerEditing { ) ) if let denial = decision.denialError { throw denial } + let route = DatabaseManager.shared.schemaChangeRoute(for: scope) + try await DatabaseManager.shared.confirmSchemaChange( + on: route, connectionId: connection.id, confirmation: holdConfirmation + ) + let isPooled = route.isPooled let startedAt = Date() defer { @@ -167,10 +173,15 @@ enum TriggerEditing { ) } do { - try await withSchemaChangeDriver(scope: scope) { driver in + try await withSchemaChangeDriver(scope: scope, route: route) { driver in switch plan.strategy { case let .transactional(dropFirst): - try await runInTransaction(driver: driver, dropSQL: dropFirst ? plan.dropSQL : nil, sql: sql) + let wraps = await DatabaseManager.wrapsSchemaChange( + on: driver, isPooled: isPooled, checksTransactionalDDL: false + ) + try await runInTransaction( + driver: driver, dropSQL: dropFirst ? plan.dropSQL : nil, sql: sql, wraps: wraps + ) case .dropThenCreate: guard let dropSQL = plan.dropSQL, let restore else { throw TriggerEditingError.dropUnavailable } try await runDropThenCreate(driver: driver, dropSQL: dropSQL, sql: sql, rollback: restore) @@ -260,17 +271,24 @@ enum TriggerEditing { private static func withSchemaChangeDriver( scope: DatabaseScope, + route: ScopedDriverRoute? = nil, _ body: @Sendable @escaping (DatabaseDriver) async throws -> Void ) async throws { try await DatabaseManager.shared.withScopedDriver( scope: scope, - route: DatabaseManager.shared.schemaChangeRoute(for: scope), + route: route ?? DatabaseManager.shared.schemaChangeRoute(for: scope), cancellation: .protectedWrite, body ) } - static func runInTransaction(driver: DatabaseDriver, dropSQL: String?, sql: String) async throws { + // Unwrapped, the edit joins the transaction the session holds and is taken back with it. + static func runInTransaction(driver: DatabaseDriver, dropSQL: String?, sql: String, wraps: Bool = true) async throws { + guard wraps else { + if let dropSQL { _ = try await driver.execute(query: dropSQL) } + _ = try await driver.execute(query: sql) + return + } try await driver.beginTransaction(mode: .readWrite) do { if let dropSQL { _ = try await driver.execute(query: dropSQL) } diff --git a/apple/TablePro/Core/Events/AppEvents.swift b/apple/TablePro/Core/Events/AppEvents.swift index 084d9ef454..04c5dfa6a5 100644 --- a/apple/TablePro/Core/Events/AppEvents.swift +++ b/apple/TablePro/Core/Events/AppEvents.swift @@ -102,6 +102,9 @@ final class AppEvents { // Refresh on a tab that loads its own content rather than through the coordinator, by tab id. let tabRefreshRequested = PassthroughSubject() + // A query tab's edit history was written. The tab's id, or nil after a purge or Clear History. + let queryTabVersionsDidUpdate = PassthroughSubject() + // MARK: - License & Sync let licenseStatusDidChange = PassthroughSubject() diff --git a/apple/TablePro/Core/Events/SucceededStatements.swift b/apple/TablePro/Core/Events/SucceededStatements.swift index 156a74b96e..8f8cdd8f0b 100644 --- a/apple/TablePro/Core/Events/SucceededStatements.swift +++ b/apple/TablePro/Core/Events/SucceededStatements.swift @@ -35,8 +35,15 @@ enum StatementCommitEvidence: Sendable, Equatable { plan: BatchTransactionPlan, completed: Bool ) -> StatementCommitEvidence { - guard plan.opensTransaction else { return .runStartedIn(start, endedIn: end, appTransaction: .none) } - return .runStartedIn(start, endedIn: end, appTransaction: completed ? .committed : .rolledBack) + switch plan { + case .appTransaction: + return .runStartedIn(start, endedIn: end, appTransaction: completed ? .committed : .rolledBack) + // A held run sent no COMMIT, so what it ran is pending until the user ends the transaction. + case .heldTransaction: + return .runStartedIn(start, endedIn: end, appTransaction: completed ? .none : .rolledBack) + case .scriptTransaction, .autocommit, .sessionTransaction: + return .runStartedIn(start, endedIn: end, appTransaction: .none) + } } } diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/ConfirmDestructiveOperationTool.swift b/apple/TablePro/Core/MCP/Protocol/Tools/ConfirmDestructiveOperationTool.swift index cf0418a0da..1e675ad88c 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/ConfirmDestructiveOperationTool.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/ConfirmDestructiveOperationTool.swift @@ -11,7 +11,8 @@ public struct ConfirmDestructiveOperationTool: MCPToolImplementation { with no WHERE: one statement, or on SQL Server a whole script. Anything else is refused. The user \ approves it before it runs: through your elicitation prompt when your client supports elicitation, \ otherwise through TablePro's own confirmation dialog on the user's Mac. Needs tools:write and a \ - connection the user left writable for external clients. + connection the user left writable for external clients. The transaction rules of execute_query apply, \ + Manual Commit's among them. """ ) public static let requiredScopes: Set = [.toolsWrite] diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/DataTools.swift b/apple/TablePro/Core/MCP/Protocol/Tools/DataTools.swift index 771dea9de4..de2f24e50e 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/DataTools.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/DataTools.swift @@ -311,7 +311,8 @@ public struct InsertRowsTool: MCPToolImplementation { public static let description = String( localized: """ Insert rows into a table using bound parameters, so values are never spliced into SQL text. \ - Needs tools:write and goes through the connection's Safe Mode approval. + Needs tools:write and goes through the connection's Safe Mode approval. With Manual Commit on, the rows \ + stay pending until the user commits in TablePro. """ ) public static let requiredScopes: Set = [.toolsWrite] diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/ExecuteQueryTool.swift b/apple/TablePro/Core/MCP/Protocol/Tools/ExecuteQueryTool.swift index 87522b7b7a..98dee6bb14 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/ExecuteQueryTool.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/ExecuteQueryTool.swift @@ -13,7 +13,11 @@ public struct ExecuteQueryTool: MCPToolImplementation { Destructive statements, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE, go through \ confirm_destructive_operation instead. Statements that read or write files, or run server-side code, \ are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at \ - its GO lines and result_sets lists every result set when there is more than one. + its GO lines and result_sets lists every result set when there is more than one. A statement that starts \ + a transaction while one is open is refused. With Manual Commit on, a write stays pending until the user \ + commits it in TablePro, and statements that begin, commit or roll back a transaction are refused. While a \ + transaction is open on PostgreSQL or CockroachDB a statement runs inside a savepoint, so its failure leaves \ + the transaction usable; on Redshift and DuckDB it is refused. """ ) public static let requiredScopes: Set = [.toolsRead] diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/MaintenanceTools.swift b/apple/TablePro/Core/MCP/Protocol/Tools/MaintenanceTools.swift index 832833c5bb..9e022fc2c8 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/MaintenanceTools.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/MaintenanceTools.swift @@ -236,7 +236,9 @@ public struct TransactionControlTool: MCPToolImplementation { localized: """ Begin, commit, or roll back a transaction on the connection's shared session. The transaction \ stays open across calls until committed or rolled back, and it is the same session the user's \ - own tabs run on, so leave nothing open. Needs tools:write. + own tabs run on, so leave nothing open. Begin is refused while a transaction is already open, \ + and every action is refused on a connection with Manual Commit on, where the user commits in \ + TablePro. Needs tools:write. """ ) public static let requiredScopes: Set = [.toolsWrite] diff --git a/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift b/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift index f1e617b865..167aff86f2 100644 --- a/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift +++ b/apple/TablePro/Core/Menu/DatabaseMenuBuilder.swift @@ -10,6 +10,10 @@ import AppKit enum DatabaseMenuBuilder { static let identifier = NSUserInterfaceItemIdentifier("menu.database") static let liveMessagesIdentifier = NSUserInterfaceItemIdentifier("menu.database.liveMessages") + static let manualCommitFootnoteIdentifier = NSUserInterfaceItemIdentifier("menu.database.manualCommitFootnote") + static let manualCommitUnavailableFootnoteIdentifier = NSUserInterfaceItemIdentifier( + "menu.database.manualCommitUnavailableFootnote" + ) static func build(keyboard: KeyboardSettings, modules: Set = []) -> NSMenuItem { let item = MenuItemFactory.menu(String(localized: "Database"), items: [ @@ -38,6 +42,7 @@ enum DatabaseMenuBuilder { shortcut: .refresh, keyboard: keyboard ), + autoRefreshSubmenu(), MenuItemFactory.separator, MenuItemFactory.item( String(localized: "New Database…"), @@ -109,7 +114,8 @@ enum DatabaseMenuBuilder { ), schemaSubmenu(), sessionContextSubmenu(), - safeModeSubmenu(), + safeModeSubmenu() + ] + transactionItems(keyboard: keyboard) + [ favoriteDatabaseSubmenu(), maintenanceSubmenu(), MenuItemFactory.item( @@ -278,6 +284,49 @@ enum DatabaseMenuBuilder { return container } + private static func transactionItems(keyboard: KeyboardSettings) -> [NSMenuItem] { + [ + MenuItemFactory.item( + String(localized: "Manual Commit"), + action: #selector(MainSplitViewController.toggleManualCommit(_:)) + ), + manualCommitFootnote( + String(localized: "This database can't report an open transaction, so it commits each change."), + identifier: manualCommitFootnoteIdentifier + ), + manualCommitFootnote(ManualCommit.unavailableExplanation, identifier: manualCommitUnavailableFootnoteIdentifier), + MenuItemFactory.item( + String(localized: "Commit Transaction"), + action: #selector(MainSplitViewController.commitOpenTransaction(_:)), + shortcut: .commitTransaction, + keyboard: keyboard + ), + MenuItemFactory.item( + String(localized: "Roll Back Transaction"), + action: #selector(MainSplitViewController.rollBackOpenTransaction(_:)), + shortcut: .rollbackTransaction, + keyboard: keyboard + ) + ] + } + + // Validation shows it under Manual Commit, which it shares an action with so it always validates as disabled. + private static func manualCommitFootnote(_ text: String, identifier: NSUserInterfaceItemIdentifier) -> NSMenuItem { + let item = MenuFootnote.item(text) + item.action = #selector(MainSplitViewController.toggleManualCommit(_:)) + item.identifier = identifier + item.isHidden = true + return item + } + + private static let autoRefreshDelegate = TableAutoRefreshMenuDelegate() + + private static func autoRefreshSubmenu() -> NSMenuItem { + let container = MenuItemFactory.submenu(String(localized: "Auto Refresh"), items: []) + container.submenu?.delegate = autoRefreshDelegate + return container + } + private static let safeModeDelegate = SafeModeMenuDelegate() /// The same list the toolbar's Safe Mode control opens, so every toolbar item keeps a menu-bar diff --git a/apple/TablePro/Core/Menu/FileMenuBuilder.swift b/apple/TablePro/Core/Menu/FileMenuBuilder.swift index 888331b772..2ce0f5d02b 100644 --- a/apple/TablePro/Core/Menu/FileMenuBuilder.swift +++ b/apple/TablePro/Core/Menu/FileMenuBuilder.swift @@ -7,11 +7,12 @@ import AppKit @MainActor enum FileMenuBuilder { - /// Retained for the menu's lifetime, which is the app's: `NSMenu.delegate` is unowned. + // Retained for the app's lifetime: `NSMenu.delegate` is unowned. private static let closeTitleDelegate = CloseCommandMenuDelegate() private static let importFormatDelegate = ImportFormatMenuDelegate() private static let agentSessionDelegate = AgentSessionMenuDelegate() private static let conversationHistoryDelegate = ConversationHistoryMenuDelegate() + private static let revertToDelegate = QueryTabRevertMenuDelegate() static func build(keyboard: KeyboardSettings) -> NSMenuItem { let file = MenuItemFactory.menu(String(localized: "File"), items: [ @@ -84,14 +85,9 @@ enum FileMenuBuilder { shortcut: .saveAs, keyboard: keyboard ), + revertToSubmenu(), MenuItemFactory.separator, - /// `performClose:` is the close command every `NSWindow` implements and validates, so - /// Command W reaches Settings, the integrations activity window and every viewer the - /// app opens. An app-specific selector here reached only the editor window, and the - /// windows that missed out had to answer it by hand. `EditorWindow` overrides it to - /// close the front tab, which is where a window that draws its own tabs says so. - /// Built with the title the resolver gives a window with no tabs; the File menu's - /// delegate resolves it from the key window from then on. + // `performClose:` reaches every window, not only the editor; `EditorWindow` overrides it to close the front tab. MenuItemFactory.item( CloseCommandTitleResolver.windowTitle, action: #selector(NSWindow.performClose(_:)), @@ -143,14 +139,8 @@ enum FileMenuBuilder { return file } - /// Agent mode's four session commands and the assistant's three conversation commands, which - /// between them had no menu-bar home at all: the rail's buttons and the pane header's menu were - /// the only routes, so none of them could be found by search, rebound, or reached by a user who - /// had the rail collapsed. They sit in File because a session and a conversation are things this - /// window opens, closes and throws away, which is what the rest of this menu is about. - /// - /// Each command has exactly one item here, and each item leaves `target` nil, so the window - /// validates it through the responder chain and dims what the mode cannot run. + // A session and a conversation are things this window opens and closes. Each item leaves `target` nil, so the + // window validates it through the responder chain. private static func sessionSubmenu(keyboard: KeyboardSettings) -> NSMenuItem { MenuItemFactory.submenu(String(localized: "Session"), items: [ MenuItemFactory.item( @@ -159,11 +149,7 @@ enum FileMenuBuilder { shortcut: .newAgentSession, keyboard: keyboard ), - /// Opens the session the rail has highlighted, which is what Return on the rail does. - /// A separate leaf rather than the list's own row: AppKit ignores a key equivalent on an - /// item that owns a submenu, so making this the list would hand Settings a binding that - /// records, reads back and never fires. That is the same trap Import Data… is split - /// around, and it is why the list is a row of its own below. + // A leaf beside the list: AppKit ignores a key equivalent on an item that owns a submenu. MenuItemFactory.item( String(localized: "Open Session"), action: #selector(MainSplitViewController.openAgentSession(_:)), @@ -198,23 +184,27 @@ enum FileMenuBuilder { ]) } - /// Every session the connection on screen owns, latest first, filled when it opens. A session - /// list built at menu-build time would be one window's sessions frozen at launch. + // Filled when it opens; a list built with the menu bar would be frozen at launch. private static func recentSessionsSubmenu() -> NSMenuItem { let container = MenuItemFactory.submenu(String(localized: "Recent Sessions"), items: []) container.submenu?.delegate = agentSessionDelegate return container } - /// The assistant's stored conversations, filled when it opens for the same reason: the set - /// changes with every reply. The pane header's own menu offers the same list, and both put the - /// choice through the window so neither can act on a connection the other is showing. + // Filled when it opens, since the set changes with every reply. private static func conversationHistorySubmenu() -> NSMenuItem { let container = MenuItemFactory.submenu(String(localized: "Conversation History"), items: []) container.submenu?.delegate = conversationHistoryDelegate return container } + // The selected query tab's recent versions, filled when it opens. No shortcut: AppKit ignores one on this row. + private static func revertToSubmenu() -> NSMenuItem { + let container = MenuItemFactory.submenu(String(localized: "Revert To"), items: []) + container.submenu?.delegate = revertToDelegate + return container + } + private static func importSubmenu(keyboard: KeyboardSettings) -> NSMenuItem { let container = MenuItemFactory.submenu(String(localized: "Import"), items: [ MenuItemFactory.item( @@ -253,9 +243,7 @@ enum FileMenuBuilder { return container } - /// Every format the connection imports from, for naming one a file's extension does not. - /// Import Data… above it reads the format off the file instead. The Actions pull-down offers - /// the same list under the same title, and the two are filled by the same class when they open. + // For a format the file's extension does not name; the Actions pull-down fills the same list with the same class. private static func importFormatsSubmenu() -> NSMenuItem { let container = MenuItemFactory.submenu(String(localized: "Import Data From"), items: []) container.submenu?.delegate = importFormatDelegate diff --git a/apple/TablePro/Core/Menu/QueryTabRevertMenuDelegate.swift b/apple/TablePro/Core/Menu/QueryTabRevertMenuDelegate.swift new file mode 100644 index 0000000000..0dc068c075 --- /dev/null +++ b/apple/TablePro/Core/Menu/QueryTabRevertMenuDelegate.swift @@ -0,0 +1,67 @@ +// +// QueryTabRevertMenuDelegate.swift +// TablePro +// + +import AppKit + +internal struct QueryTabRevertChoice: Equatable { + let tabId: UUID + let versionId: Int64 +} + +// File > Revert To, filled for the selected query tab when it opens. Items carry no target, so the window validates them. +@MainActor +internal final class QueryTabRevertMenuDelegate: NSObject, NSMenuDelegate { + static let revertAction = #selector(MainSplitViewController.revertToQueryTabVersion(_:)) + static let browseAction = #selector(MainSplitViewController.browseQueryTabVersions(_:)) + static let recentVersionLimit = 10 + + // Resolved when the menu opens, so building the menu bar touches no store. + private let storage: QueryTabVersionStorage? + + init(storage: QueryTabVersionStorage? = nil) { + self.storage = storage + } + + func menuNeedsUpdate(_ menu: NSMenu) { + let controller = NSApp.target(forAction: Self.browseAction, to: nil, from: nil) as? MainSplitViewController + let tabId = controller?.selectedQueryTabId + let store = storage ?? .shared + let summaries = tabId.map { store.summaries(tabId: $0, limit: Self.recentVersionLimit) } ?? [] + Self.fill(menu, tabId: tabId, summaries: summaries) + } + + static func fill(_ menu: NSMenu, tabId: UUID?, summaries: [QueryTabVersionSummary]) { + menu.removeAllItems() + if let tabId, !summaries.isEmpty { + for summary in summaries { + menu.addItem(item(for: summary, tabId: tabId)) + } + } else { + menu.addItem(MenuPlaceholder.item()) + } + menu.addItem(.separator()) + let browse = NSMenuItem(title: String(localized: "Browse All Versions…"), action: browseAction, keyEquivalent: "") + browse.target = nil + menu.addItem(browse) + } + + static func item(for summary: QueryTabVersionSummary, tabId: UUID) -> NSMenuItem { + let entry = QueryTabVersionHistoryProvider.entry(for: summary) + let item = NSMenuItem(title: VersionHistoryFormatting.menuTitle(for: entry), action: revertAction, keyEquivalent: "") + item.target = nil + item.representedObject = QueryTabRevertChoice(tabId: tabId, versionId: summary.id) + return item + } + + // Keeps AppKit's key-equivalent search from reading the store on every modified keystroke. + func menuHasKeyEquivalent( + _ menu: NSMenu, + for event: NSEvent, + target: AutoreleasingUnsafeMutablePointer, + action: UnsafeMutablePointer + ) -> Bool { + false + } +} diff --git a/apple/TablePro/Core/Menu/TableAutoRefreshMenuDelegate.swift b/apple/TablePro/Core/Menu/TableAutoRefreshMenuDelegate.swift new file mode 100644 index 0000000000..f7df6b2695 --- /dev/null +++ b/apple/TablePro/Core/Menu/TableAutoRefreshMenuDelegate.swift @@ -0,0 +1,38 @@ +// +// TableAutoRefreshMenuDelegate.swift +// TablePro +// + +import AppKit + +// Filled when it opens, so the checkmark names the selected tab's interval; validation dims the items off a table tab. +@MainActor +final class TableAutoRefreshMenuDelegate: NSObject, NSMenuDelegate { + static let action = #selector(MainSplitViewController.setTableAutoRefreshInterval(_:)) + + func menuNeedsUpdate(_ menu: NSMenu) { + let controller = NSApp.target(forAction: Self.action, to: nil, from: nil) as? MainSplitViewController + Self.populate(menu, current: controller?.tableAutoRefreshInterval) + } + + internal static func populate(_ menu: NSMenu, current: TableAutoRefreshInterval?) { + menu.removeAllItems() + for interval in TableAutoRefreshInterval.allCases { + let item = NSMenuItem(title: interval.title, action: action, keyEquivalent: "") + item.target = nil + item.representedObject = interval.rawValue + item.state = interval == current ? .on : .off + menu.addItem(item) + } + } + + // Keeps AppKit's key-equivalent search from rebuilding the menu on every modified keystroke. + func menuHasKeyEquivalent( + _ menu: NSMenu, + for event: NSEvent, + target: AutoreleasingUnsafeMutablePointer, + action: UnsafeMutablePointer + ) -> Bool { + false + } +} diff --git a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift index f8c04fd656..8aeeb4b339 100644 --- a/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift +++ b/apple/TablePro/Core/ObjectCopy/ObjectCopyRunner.swift @@ -115,9 +115,11 @@ internal struct ObjectCopyRunner { /// pending work. `DataWriteExecutor`, `DatabaseManager+Principals` and /// `StructureRebuildPlanRunner` ask the same question for the same reason. nonisolated private static func transactionOwner(of driver: DatabaseDriver) async -> WriteTransactionOwner { + // `.automatic`: a pooled lease is closed while idle, which would roll a held copy back after it reported success. WriteTransactionOwner.resolve( supportsTransactions: driver.supportsTransactions, - sessionState: await driver.heldSessionTransactionState() + sessionState: await driver.heldSessionTransactionState(), + commitMode: .automatic ) } @@ -272,7 +274,7 @@ internal struct ObjectCopyRunner { let owner = await Self.transactionOwner(of: driver) // The other two settings keep what ran, and PostgreSQL commits an aborted transaction as a ROLLBACK. let usesTransaction = hasCleanup && errorHandling == .stopAndRollback - && plugin.supportsTransactionalDDL && owner.opensTransaction + && plugin.supportsTransactionalDDL && owner.sendsBegin && owner.commitsAtEnd if usesTransaction { try await plugin.beginTransaction(mode: .readWrite) } var result: DDLResult @@ -600,7 +602,7 @@ internal struct ObjectCopyRunner { throw ObjectCopyError.refused(Self.noTargetDriver) } let owner = await Self.transactionOwner(of: targetDriver) - let usesTransaction = owner.opensTransaction + let usesTransaction = owner.sendsBegin && owner.commitsAtEnd if usesTransaction { try await targetPlugin.beginTransaction(mode: .readWrite) } var result = DataResult() diff --git a/apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift b/apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift index cb350fb44f..5bc8d241e6 100644 --- a/apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift +++ b/apple/TablePro/Core/Plugins/ImportDataSinkAdapter.swift @@ -40,7 +40,10 @@ final class ImportDataSinkAdapter: PluginImportDataSink, @unchecked Sendable { private let tableEditDialect: TableEditDialect? private let nameHazards = OSAllocatedUnfairLock<[String]>(initialState: []) private let ownsTransaction = OSAllocatedUnfairLock(initialState: false) + private let leavesTransactionOpen = OSAllocatedUnfairLock(initialState: false) + private let disabledForeignKeys = OSAllocatedUnfairLock(initialState: false) private let joinsTransaction: Bool + private let commitMode: ConnectionCommitMode private let transactionRecord = OSAllocatedUnfairLock(initialState: TransactionRecord()) private struct TransactionRecord { @@ -72,14 +75,17 @@ final class ImportDataSinkAdapter: PluginImportDataSink, @unchecked Sendable { sourceFields: Set = [], rowWriting: ImportRowWriting = .insert, joinsTransaction: Bool = false, + commitMode: ConnectionCommitMode = .automatic, importRun: ImportRun? = nil, fileIndex: Int? = 0, isCancelled: @escaping @Sendable () -> Bool = { false } ) { self.importControl = importRun.map { ImportSinkRun(run: $0, driver: driver, databaseType: databaseType, fileIndex: fileIndex, - targetTable: targetTable, targetSchema: targetSchema, joinsTransaction: joinsTransaction) + targetTable: targetTable, targetSchema: targetSchema, joinsTransaction: joinsTransaction, + commitMode: commitMode) } + self.commitMode = commitMode self.isCancelled = isCancelled self.rowWriting = rowWriting self.tally = OSAllocatedUnfairLock(initialState: ImportRowTally(mode: rowWriting.mode)) @@ -564,19 +570,32 @@ final class ImportDataSinkAdapter: PluginImportDataSink, @unchecked Sendable { func beginTransaction() async throws { if let importControl { return try await importControl.begin() } guard !joinsTransaction else { return } + let state = await driver.heldSessionTransactionState() + if commitMode == .manual { + guard !disabledForeignKeys.withLock({ $0 }) else { throw ManualCommitForeignKeyRefusal() } + try ManualCommit.refuseUnheldWrite(supportsTransactions: driver.supportsTransactions, sessionState: state) + } let owner = WriteTransactionOwner.resolve( supportsTransactions: driver.supportsTransactions, - sessionState: await driver.heldSessionTransactionState() + sessionState: state, + commitMode: commitMode ) - ownsTransaction.withLock { $0 = owner.opensTransaction } + ownsTransaction.withLock { $0 = owner.sendsBegin } + leavesTransactionOpen.withLock { $0 = owner.sendsBegin && !owner.commitsAtEnd } transactionRecord.withLock { $0 = TransactionRecord() } - guard owner.opensTransaction else { return } + guard owner.sendsBegin else { return } try await driver.beginTransaction(mode: .readWrite) + if owner == .held { ManualCommit.turnHold?.mark(true) } } func commitTransaction() async throws { if let importControl { return try await importControl.commit() } guard ownsTransaction.withLock({ $0 }) else { return } + // Manual Commit: the imported rows stay pending in the transaction the user commits. + guard !leavesTransactionOpen.withLock({ $0 }) else { + ownsTransaction.withLock { $0 = false } + return + } try await driver.commitTransaction() ownsTransaction.withLock { $0 = false } transactionRecord.withLock { $0.ending = .committed } @@ -642,6 +661,8 @@ final class ImportDataSinkAdapter: PluginImportDataSink, @unchecked Sendable { _ = try await self.driver.execute(query: stmt) } } + disabledForeignKeys.withLock { $0 = true } + importControl?.noteForeignKeysDisabled() } func enableForeignKeyChecks() async throws { @@ -659,7 +680,10 @@ final class ImportDataSinkAdapter: PluginImportDataSink, @unchecked Sendable { } func prepareImportRun() async throws { - try await importControl?.prepare() + if let importControl { return try await importControl.prepare() } + guard commitMode == .manual else { return } + let state = await driver.heldSessionTransactionState() + try ManualCommit.refuseUnheldWrite(supportsTransactions: driver.supportsTransactions, sessionState: state) } func importCheckpoint() async throws { diff --git a/apple/TablePro/Core/Plugins/ImportSinkRun.swift b/apple/TablePro/Core/Plugins/ImportSinkRun.swift index bbad43e321..528c131a3b 100644 --- a/apple/TablePro/Core/Plugins/ImportSinkRun.swift +++ b/apple/TablePro/Core/Plugins/ImportSinkRun.swift @@ -15,8 +15,12 @@ internal final class ImportSinkRun: @unchecked Sendable { private let targetTable: String? private let targetSchema: String? private let joinsTransaction: Bool + private let commitMode: ConnectionCommitMode private let lock = NSLock() private var ownsTransaction = false + private var leavesTransactionOpen = false + private var disabledForeignKeys = false + private var writesOutsideTransaction: Bool? private var finalized = false private var acknowledgedEnding: ImportTransactionOutcome = .untracked private var fileCommittedEarly = false @@ -38,7 +42,8 @@ internal final class ImportSinkRun: @unchecked Sendable { fileIndex: Int?, targetTable: String?, targetSchema: String?, - joinsTransaction: Bool + joinsTransaction: Bool, + commitMode: ConnectionCommitMode = .automatic ) { self.run = run self.driver = driver @@ -47,10 +52,15 @@ internal final class ImportSinkRun: @unchecked Sendable { self.targetTable = targetTable self.targetSchema = targetSchema self.joinsTransaction = joinsTransaction + self.commitMode = commitMode } internal func prepare() async throws { try await admitted(.begin) { + if self.commitMode == .manual { + let state = await self.driver.heldSessionTransactionState() + try ManualCommit.refuseUnheldWrite(supportsTransactions: self.driver.supportsTransactions, sessionState: state) + } if !self.run.snapshot.prepared { let state = await self.sessionState() self.run.record { @@ -93,6 +103,7 @@ internal final class ImportSinkRun: @unchecked Sendable { operation: () async throws -> T ) async throws -> T { try await admitted(.data) { + if writes, self.commitMode == .manual { try self.refuseWriteOutsideTransaction() } if let statement { self.noteStatement(statement) } if writes, let index = self.fileIndex { self.run.record { $0.effects[index]?.admittedWrite = true } @@ -122,6 +133,16 @@ internal final class ImportSinkRun: @unchecked Sendable { } } + // Decided at the first write, before a GO line resets the holder: outside a transaction each statement commits. + private func refuseWriteOutsideTransaction() throws { + let holder = run.snapshot.holder + let outside = lock.withLock { () -> Bool in + if writesOutsideTransaction == nil { writesOutsideTransaction = holder == .none } + return writesOutsideTransaction == true + } + if outside { throw ManualCommitUnwrappedImportRefusal() } + } + private func noteStatement(_ statement: String) { let grammar = SQLLexicalResolver.readings(for: databaseType, session: driver.sessionLexicalState).execution if grammar.contains(.batchSeparatorLines) { @@ -171,17 +192,34 @@ internal final class ImportSinkRun: @unchecked Sendable { try await admitted(.data) {} } + // SQLite ignores the re-enable inside a held transaction, and a Roll Back undoes PostgreSQL's. + internal func noteForeignKeysDisabled() { + lock.withLock { disabledForeignKeys = true } + } + internal func begin() async throws { guard !joinsTransaction else { return } try await admitted(.begin) { let state = await self.driver.heldSessionTransactionState() - let owner = WriteTransactionOwner.resolve(supportsTransactions: self.driver.supportsTransactions, sessionState: state) - guard owner.opensTransaction else { + if self.commitMode == .manual { + guard !self.lock.withLock({ self.disabledForeignKeys }) else { throw ManualCommitForeignKeyRefusal() } + try ManualCommit.refuseUnheldWrite(supportsTransactions: self.driver.supportsTransactions, sessionState: state) + } + let owner = WriteTransactionOwner.resolve( + supportsTransactions: self.driver.supportsTransactions, + sessionState: state, + commitMode: self.commitMode + ) + guard owner.sendsBegin else { self.run.record { $0.currentState = ImportSessionState(state) } return } try await self.driver.beginTransaction(mode: .readWrite) - self.lock.withLock { self.ownsTransaction = true } + if owner == .held { ManualCommit.turnHold?.mark(true) } + self.lock.withLock { + self.ownsTransaction = true + self.leavesTransactionOpen = !owner.commitsAtEnd + } self.run.record { $0.holder = .owned; $0.ending = nil } await self.observe() } @@ -203,6 +241,13 @@ internal final class ImportSinkRun: @unchecked Sendable { markFinalized() return } + // Manual Commit: the rows stay pending in a transaction that is now the user's, as a joined one is. + if lock.withLock({ leavesTransactionOpen }) { + lock.withLock { ownsTransaction = false } + run.record { $0.holder = .joinedUser; $0.currentState = state } + markFinalized() + return + } do { try await driver.commitTransaction() await recordEnding(.committed) @@ -243,6 +288,7 @@ internal final class ImportSinkRun: @unchecked Sendable { } do { try await driver.rollbackTransaction() + ManualCommit.turnHold?.mark(false) await recordEnding(.rolledBack) lock.withLock { acknowledgedEnding = .rolledBack } markFinalized() diff --git a/apple/TablePro/Core/Plugins/PluginDriverAdapter+ServerObjects.swift b/apple/TablePro/Core/Plugins/PluginDriverAdapter+ServerObjects.swift new file mode 100644 index 0000000000..1994fe8817 --- /dev/null +++ b/apple/TablePro/Core/Plugins/PluginDriverAdapter+ServerObjects.swift @@ -0,0 +1,36 @@ +// +// PluginDriverAdapter+ServerObjects.swift +// TablePro +// + +import Foundation +import os +import TableProPluginKit + +extension PluginDriverAdapter { + private static let serverObjectsLogger = Logger(subsystem: "com.TablePro", category: "PluginDriverAdapter") + + var serverObjectKinds: [PluginServerObjectKind] { schemaPluginDriver.serverObjectKinds } + + // A container kind is stamped with the schema it was read from, as routines are; a database kind + // names no schema and reads with none. + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [ServerObjectInfo] { + let isDatabaseScoped = schemaPluginDriver.serverObjectKinds.first { $0.id == kindId }?.scope == .database + let resolvedSchema = isDatabaseScoped ? nil : (schema ?? schemaPluginDriver.currentSchema) + do { + return try await schemaPluginDriver.fetchServerObjects(ofKind: kindId, schema: resolvedSchema) + .filter { $0.kindId == kindId } + .map { ServerObjectInfo($0).adoptingSchema(resolvedSchema) } + .sorted { ($0.schema ?? "", $0.name) < ($1.schema ?? "", $1.name) } + } catch { + Self.serverObjectsLogger.warning( + "fetchServerObjects failed kind=\(kindId, privacy: .public) error=\(error.publicLogShape, privacy: .public)" + ) + throw error + } + } + + func fetchServerObject(_ object: ServerObjectInfo) async throws -> ServerObjectInfo { + ServerObjectInfo(try await schemaPluginDriver.fetchServerObject(object.pluginObject)) + } +} diff --git a/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift b/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift index 87b4c54dc8..90985b1650 100644 --- a/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift +++ b/apple/TablePro/Core/SchemaTracking/StructureChangeManager.swift @@ -589,6 +589,13 @@ final class StructureChangeManager: ObservableObject, ChangeManaging { } for fk in workingForeignKeys where isStaged(.foreignKey(fk.id)) && fk.isValid { + // Create Table refuses this too; otherwise the server does, after a MySQL save's earlier DDL already applied. + guard fk.columns.count == fk.referencedColumns.count else { + validationErrors[.foreignKey(fk.id)] = String( + localized: "A foreign key needs as many referenced columns as it has columns." + ) + continue + } for columnName in fk.columns where !namesAColumn(columnName, in: columnNames) { validationErrors[.foreignKey(fk.id)] = String( format: String(localized: "Foreign key references a column that does not exist: %@"), columnName diff --git a/apple/TablePro/Core/Services/Execution/BatchStatementRun.swift b/apple/TablePro/Core/Services/Execution/BatchStatementRun.swift index 3f3c01bb6d..fb6aca5752 100644 --- a/apple/TablePro/Core/Services/Execution/BatchStatementRun.swift +++ b/apple/TablePro/Core/Services/Execution/BatchStatementRun.swift @@ -80,10 +80,11 @@ internal enum BatchStatementRun { commitOutput: ServerOutputBox = ServerOutputBox(), execute: @escaping @MainActor @Sendable (Statement) async throws -> Output ) async -> BatchStatementOutcome { - let opensTransaction = plan.opensTransaction && driver.supportsTransactions - if opensTransaction { + let sentBegin = plan.sendsBegin && driver.supportsTransactions + if sentBegin { do { try await driver.beginTransaction(mode: mode) + if plan == .heldTransaction { ManualCommit.turnHold?.mark(true) } } catch { return .failed(results: [], failure: .transactionStart, errorDescription: error.localizedDescription) } @@ -95,7 +96,7 @@ internal enum BatchStatementRun { while position < queue.count { let statement = queue[position] guard !Task.isCancelled, gate.isCurrent() else { - await rollback(driver: driver, connectionId: connectionId, plan: plan, opensTransaction: opensTransaction) + await rollback(driver: driver, connectionId: connectionId, plan: plan, sentBegin: sentBegin) return .cancelled(results: plan.keepsExecutedStatements ? results : []) } let ran = await runStatement( @@ -108,12 +109,12 @@ internal enum BatchStatementRun { ) switch ran { case .stopped: - await rollback(driver: driver, connectionId: connectionId, plan: plan, opensTransaction: opensTransaction) + await rollback(driver: driver, connectionId: connectionId, plan: plan, sentBegin: sentBegin) return .cancelled(results: plan.keepsExecutedStatements ? results : []) case .committed(let result): results.append(result) if let errorDescription = serverError(result) { - await rollback(driver: driver, connectionId: connectionId, plan: plan, opensTransaction: opensTransaction) + await rollback(driver: driver, connectionId: connectionId, plan: plan, sentBegin: sentBegin) return .failed( results: results, failure: .batch(sql: failureSQL(statement)), @@ -127,7 +128,7 @@ internal enum BatchStatementRun { errorDescription: failure.errorDescription ) case .failed(let failure): - await rollback(driver: driver, connectionId: connectionId, plan: plan, opensTransaction: opensTransaction) + await rollback(driver: driver, connectionId: connectionId, plan: plan, sentBegin: sentBegin) return .failed( results: results, failure: .statement(sql: failureSQL(statement)), @@ -140,7 +141,7 @@ internal enum BatchStatementRun { } } - guard opensTransaction else { return .completed(results: results) } + guard plan.commitsAtEnd, sentBegin else { return .completed(results: results) } return await commit( results: results, driver: driver, connectionId: connectionId, gate: gate, plan: plan, output: commitOutput ) @@ -198,7 +199,7 @@ internal enum BatchStatementRun { switch committed { case .stopped: - await rollback(driver: driver, connectionId: connectionId, plan: plan, opensTransaction: true) + await rollback(driver: driver, connectionId: connectionId, plan: plan, sentBegin: true) return .cancelled(results: plan.keepsExecutedStatements ? results : []) case .committed: output.store(await ServerOutputCapture.drain(driver)) @@ -211,7 +212,7 @@ internal enum BatchStatementRun { results: results, failure: .commitOutcomeUnknown, errorDescription: failure.errorDescription ) } - await rollback(driver: driver, connectionId: connectionId, plan: plan, opensTransaction: true) + await rollback(driver: driver, connectionId: connectionId, plan: plan, sentBegin: true) return .failed(results: results, failure: .commit, errorDescription: failure.errorDescription) } } @@ -229,15 +230,16 @@ internal enum BatchStatementRun { driver: DatabaseDriver, connectionId: UUID, plan: BatchTransactionPlan, - opensTransaction: Bool + sentBegin: Bool ) async { guard plan.rollsBackAfterStop, driver.supportsTransactions else { return } let token = DatabaseManager.shared.beginProtectedWrite(on: driver, for: connectionId) defer { DatabaseManager.shared.endProtectedWrite(token, for: connectionId) } do { try await TaskCancellationShield.run { @Sendable in try await driver.rollbackTransaction() } + if plan == .heldTransaction { ManualCommit.turnHold?.mark(false) } } catch { - guard opensTransaction else { + guard sentBegin else { batchLog.debug("No open script transaction to roll back: \(error.publicLogShape, privacy: .public)") return } diff --git a/apple/TablePro/Core/Services/Execution/BatchTransactionPlan.swift b/apple/TablePro/Core/Services/Execution/BatchTransactionPlan.swift index cb9d4df71f..13f736b9f3 100644 --- a/apple/TablePro/Core/Services/Execution/BatchTransactionPlan.swift +++ b/apple/TablePro/Core/Services/Execution/BatchTransactionPlan.swift @@ -13,14 +13,17 @@ import TableProPluginKit /// `PRAGMA foreign_keys`. Such a batch opens nothing, commits nothing, and rolls nothing back, /// because there is no transaction of its own to roll back to. /// -/// `.sessionTransaction` is the answer the text cannot reach: it comes from the driver, once the -/// lease is held, through ``joining(_:)``. +/// `.sessionTransaction` and `.heldTransaction` are the answers the text cannot reach: they come +/// from the driver and the connection's commit mode, once the lease is held, through +/// ``joining(_:commitMode:)``. /// /// The switches over this are exhaustive on purpose. No case may inherit `.appTransaction`'s begin /// or `.scriptTransaction`'s rollback by falling into a `default:` arm. internal enum BatchTransactionPlan: Equatable, Sendable { /// The app opens the transaction, commits it, and rolls it back on a failure or a Stop. case appTransaction + // Manual Commit: the app opens the transaction and leaves it for the user to commit; a failure or a Stop rolls it back. + case heldTransaction /// The script opens its own transaction. The app opens none and still rolls back what a failed /// script left open. case scriptTransaction @@ -31,24 +34,43 @@ internal enum BatchTransactionPlan: Equatable, Sendable { /// ending in `COMMIT` commits. case sessionTransaction - internal var opensTransaction: Bool { + internal var sendsBegin: Bool { switch self { - case .appTransaction: + case .appTransaction, .heldTransaction: return true case .scriptTransaction, .autocommit, .sessionTransaction: return false } } + internal var commitsAtEnd: Bool { + switch self { + case .appTransaction: + return true + case .heldTransaction, .scriptTransaction, .autocommit, .sessionTransaction: + return false + } + } + internal var rollsBackAfterStop: Bool { switch self { - case .appTransaction, .scriptTransaction: + case .appTransaction, .heldTransaction, .scriptTransaction: return true case .autocommit, .sessionTransaction: return false } } + // What the session holds once the run ends is the user's to see: the transaction is still open. + internal var readsSessionEndState: Bool { + switch self { + case .heldTransaction, .sessionTransaction: + return true + case .appTransaction, .scriptTransaction, .autocommit: + return false + } + } + /// Whether the statements that ran before a failure or a Stop stay in place. The failure banner /// says so, and a stopped run keeps their results and their history rather than dropping them. internal var keepsExecutedStatements: Bool { @@ -65,8 +87,9 @@ internal enum BatchTransactionPlan: Equatable, Sendable { /// A session that answers `.unknown` keeps the wrap for a plain batch, but a self-managed /// script stops being rolled back: the transaction its text left open may predate the run, and /// rolling that back discards work the run never did. - internal func joining(_ session: PluginSessionTransactionState) -> BatchTransactionPlan { + internal func joining(_ session: PluginSessionTransactionState, commitMode: ConnectionCommitMode) -> BatchTransactionPlan { guard session.permitsAppTransaction else { return .sessionTransaction } + if self == .appTransaction, commitMode == .manual, session == .idle { return .heldTransaction } guard session == .unknown, self == .scriptTransaction else { return self } return .sessionTransaction } diff --git a/apple/TablePro/Core/Services/Execution/ManualCommit.swift b/apple/TablePro/Core/Services/Execution/ManualCommit.swift new file mode 100644 index 0000000000..41289f10d5 --- /dev/null +++ b/apple/TablePro/Core/Services/Execution/ManualCommit.swift @@ -0,0 +1,342 @@ +// +// ManualCommit.swift +// TablePro +// + +import Foundation +import os +import TableProPluginKit +import TableProSQLGrammar +import TableProUtilities + +// A refusal an external caller reads as it is: it quotes nothing the server said. +internal protocol ManualCommitRefusing: LocalizedError {} + +internal struct ManualCommitRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "This connection can't hold this change until you commit, so it did not run. Turn off Manual Commit in the Database menu to run it." + ) + } +} + +internal struct ManualCommitMixedRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "This run mixes changes with a statement that can't run in a transaction. Run that statement on its own, or turn off Manual Commit." + ) + } +} + +internal struct ManualCommitSessionOptionsRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "This change sets session options around its statements, so it can't wait for Commit. Turn off Manual Commit in the Database menu to run it." + ) + } +} + +internal struct ManualCommitForeignKeyRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "This import turns off foreign key checks, which can't wait for Commit. Turn off Disable foreign key checks or Manual Commit to run it." + ) + } +} + +internal struct ManualCommitUnwrappedImportRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "This import runs each statement on its own, so nothing can wait for Commit. Turn on Wrap in transaction or turn off Manual Commit." + ) + } +} + +internal struct ManualCommitTransactionControlRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { ManualCommit.transactionControlRefusalMessage } +} + +internal struct OpenTransactionRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { ManualCommit.openTransactionRefusalMessage } +} + +internal struct AbortingTransactionRefusal: ManualCommitRefusing, Equatable { + var errorDescription: String? { + String( + localized: "A transaction is open on this connection, and a failed statement would abort it. Commit or roll back in TablePro first." + ) + } +} + +// Set by `openIfNeeded` for the session turn it runs in, so the turn records the hold its BEGIN opened. +internal final class ManualCommitTurnHold: @unchecked Sendable { + private let lock = NSLock() + private var held = false + + var isHeld: Bool { lock.withLock { held } } + + func mark(_ value: Bool) { + lock.withLock { held = value } + } +} + +// One statement's run under its connection's commit mode, read on the main actor before the lease. +internal struct ManualCommitRun: Sendable { + let commitMode: ConnectionCommitMode + let databaseType: DatabaseType + let grammar: SQLLexicalGrammar + let connectionId: UUID + + nonisolated func running( + _ statements: [String], + on driver: DatabaseDriver, + _ body: () async throws -> T + ) async throws -> T { + let opened = try await ManualCommit.openIfNeeded( + on: driver, statements: statements, databaseType: databaseType, grammar: grammar, commitMode: commitMode + ) + do { + return try await body() + } catch { + if opened { await ManualCommit.rollBackOpened(on: driver, connectionId: connectionId) } + throw error + } + } +} + +// The hold a Manual Commit connection opens before the first write the user or an agent sends: BEGIN, never COMMIT. +internal enum ManualCommit { + nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "ManualCommit") + + @TaskLocal internal static var turnHold: ManualCommitTurnHold? + + internal enum Decision: Equatable { + case runAsIs + case hold + case refuse + case refuseMixed + + var refusal: (any Error)? { + switch self { + case .runAsIs, .hold: return nil + case .refuse: return ManualCommitRefusal() + case .refuseMixed: return ManualCommitMixedRefusal() + } + } + } + + // These commit schema and maintenance statements on their own, so nothing can wait there; Redshift's TRUNCATE + // and CockroachDB's DDL commit the open transaction too. + nonisolated internal static func isAvailable(for databaseType: DatabaseType) -> Bool { + guard TableEditDialect.of(databaseType)?.commitsDDLImplicitly != true else { return false } + let family = TransactionEngineFamily.of(databaseType) + return family != .redshift && family != .cockroach + } + + nonisolated internal static func effectiveMode( + _ stored: ConnectionCommitMode, + databaseType: DatabaseType + ) -> ConnectionCommitMode { + isAvailable(for: databaseType) ? stored : .automatic + } + + internal static var unavailableExplanation: String { + String(localized: "This database commits schema and maintenance statements on its own, so changes can't wait for Commit.") + } + + // Exactly `.idle` holds: `.unknown` and a table lock would let the write commit on its own. + nonisolated internal static func decision( + commitMode: ConnectionCommitMode, + statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar, + supportsTransactions: Bool, + sessionState: PluginSessionTransactionState + ) -> Decision { + guard commitMode == .manual, writes(statements, databaseType: databaseType) else { return .runAsIs } + let family = TransactionEngineFamily.of(databaseType) + guard supportsTransactions, family.wrapsBatchInTransaction else { return .refuse } + switch BatchTransactionPolicy.plan(for: statements, databaseType: databaseType, grammar: grammar) { + case .appTransaction, .heldTransaction: + break + case .autocommit: + let mixed = statements.contains { statement in + !AutocommitOnlyStatement.matches(statement, family: family, grammar: grammar) + && writes([statement], databaseType: databaseType) + } + return mixed ? .refuseMixed : .runAsIs + case .scriptTransaction, .sessionTransaction: + return .runAsIs + } + switch sessionState { + case .idle: + return .hold + case .inTransaction, .abortedTransaction: + return .runAsIs + case .holdsSessionLocks, .unknown: + return .refuse + @unknown default: + return .refuse + } + } + + // A held decision runs held; anything else keeps the plan an automatic connection would use. + nonisolated internal static func runPlan( + text plan: BatchTransactionPlan, + decision: Decision, + sessionState: PluginSessionTransactionState + ) -> BatchTransactionPlan { + decision == .hold ? .heldTransaction : plan.joining(sessionState, commitMode: .automatic) + } + + // Inside the caller's lease, so nothing runs between the state read and the BEGIN. True when it sent the BEGIN. + nonisolated internal static func openIfNeeded( + on driver: DatabaseDriver, + statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar, + commitMode: ConnectionCommitMode + ) async throws -> Bool { + guard commitMode == .manual else { return false } + let state = await driver.heldSessionTransactionState() + let decided = decision( + commitMode: commitMode, + statements: statements, + databaseType: databaseType, + grammar: grammar, + supportsTransactions: driver.supportsTransactions, + sessionState: state + ) + if let refusal = decided.refusal { throw refusal } + guard decided == .hold else { return false } + let mode = OperationKind.worst(of: statements, databaseType: databaseType).transactionAccessMode + try await driver.beginTransaction(mode: mode) + turnHold?.mark(true) + return true + } + + // The app's own writes on a Manual Commit session: one the session can't hold is refused before it runs. + nonisolated internal static func refuseUnheldWrite( + supportsTransactions: Bool, + sessionState: PluginSessionTransactionState + ) throws { + guard supportsTransactions else { throw ManualCommitRefusal() } + switch sessionState { + case .idle, .inTransaction, .abortedTransaction: + return + case .holdsSessionLocks, .unknown: + throw ManualCommitRefusal() + @unknown default: + throw ManualCommitRefusal() + } + } + + // A timed-out read after the turn's own BEGIN still means the transaction it opened. + nonisolated internal static func recordedState( + read: PluginSessionTransactionState, + turnOpenedHold: Bool + ) -> PluginSessionTransactionState { + read == .unknown && turnOpenedHold ? .inTransaction : read + } + + // A failed first write leaves nothing worth keeping, and on PostgreSQL an aborted block that needs a Roll Back. + @MainActor + internal static func rollBackOpened(on driver: DatabaseDriver, connectionId: UUID) async { + let token = DatabaseManager.shared.beginProtectedWrite(on: driver, for: connectionId) + defer { DatabaseManager.shared.endProtectedWrite(token, for: connectionId) } + do { + try await TaskCancellationShield.run { @Sendable in try await driver.rollbackTransaction() } + turnHold?.mark(false) + } catch { + logger.error("Rolling back the held transaction failed: \(error.publicLogShape, privacy: .public)") + } + } + + // A procedure or dynamic batch can end the transaction unseen, and so can a T-SQL batch, which needs no `;` before COMMIT. + nonisolated internal static func mayEndTransactionUnseen( + _ statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar + ) -> Bool { + let scansTokens = TransactionEngineFamily.of(databaseType) == .sqlServer + return statements.contains { statement in + CatalogChangeClassifier.effect(of: statement, databaseType: databaseType, grammar: grammar).runsUnseenStatements + || (scansTokens && mentionsTransactionControl(statement, grammar: grammar)) + } + } + + // END is left out: it closes every T-SQL block and every CASE. + nonisolated private static func mentionsTransactionControl(_ statement: String, grammar: SQLLexicalGrammar) -> Bool { + let text = statement as NSString + var start = 0 + while start < text.length { + var cursor = SQLTokenCursor(text.substring(from: start) as NSString, grammar: grammar) + var previous: String? + while let token = cursor.next() { + guard let word = token.word else { + previous = nil + continue + } + if controlWords.contains(word) { return true } + if word == "TRAN" || word == "TRANSACTION", let previous, transactionOpeners.contains(previous) { + return true + } + previous = word + } + start += cursor.location + 1 + } + return false + } + + private static let controlWords: Set = ["COMMIT", "ROLLBACK", "SAVEPOINT", "RELEASE", "ABORT", "XA"] + private static let transactionOpeners: Set = ["BEGIN", "SAVE", "START", "DISTRIBUTED"] + + // BEGIN and START are read the way a batch is planned, so a PL/SQL or T-SQL block is not taken for one. + nonisolated internal static func controlsTransaction( + _ statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar + ) -> Bool { + if startsTransaction(statements, databaseType: databaseType, grammar: grammar) { return true } + return statements.contains { statement in + if CatalogChangeClassifier.effect(of: statement, databaseType: databaseType, grammar: grammar).endsTransaction { + return true + } + return transactionControlKeywords.contains(QueryClassifier.leadingKeyword(of: statement, grammar: grammar)) + } + } + + nonisolated internal static func startsTransaction( + _ statements: [String], + databaseType: DatabaseType, + grammar: SQLLexicalGrammar + ) -> Bool { + BatchTransactionPolicy.plan(for: statements, databaseType: databaseType, grammar: grammar) == .scriptTransaction + } + + nonisolated private static func writes(_ statements: [String], databaseType: DatabaseType) -> Bool { + OperationKind.worst(of: statements, databaseType: databaseType).declaresWrite + } + + private static let transactionControlKeywords: Set = [ + "COMMIT", "ROLLBACK", "END", "ABORT", "SAVEPOINT", "RELEASE", "XA", + ] + + internal static var transactionEndedNotice: String { + String(localized: "The open transaction on this connection ended while this statement ran.") + } + + internal static var pendingNotice: String { + String(localized: "Manual Commit is on for this connection, so this change is pending until the user commits it in TablePro.") + } + + internal static var transactionControlRefusalMessage: String { + String(localized: "Manual Commit is on for this connection. Commit or roll back in TablePro.") + } + + internal static var openTransactionRefusalMessage: String { + String( + localized: "A transaction is already open on this connection, or it cannot say whether one is. Commit or roll it back first." + ) + } +} diff --git a/apple/TablePro/Core/Services/Execution/MultiStatementFailure.swift b/apple/TablePro/Core/Services/Execution/MultiStatementFailure.swift index d4b7700a1e..1c94bf5b64 100644 --- a/apple/TablePro/Core/Services/Execution/MultiStatementFailure.swift +++ b/apple/TablePro/Core/Services/Execution/MultiStatementFailure.swift @@ -133,7 +133,7 @@ internal struct MultiStatementFailureContext: Equatable, Sendable { /// What became of the statements that ran before the failure. private func standingWorkNote(position: Int) -> String? { switch plan { - case .appTransaction, .scriptTransaction: + case .appTransaction, .heldTransaction, .scriptTransaction: return nil case .autocommit: return appliedWorkNote(position: position) diff --git a/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift b/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift index bc761bd61d..1f5c76f366 100644 --- a/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift +++ b/apple/TablePro/Core/Services/Execution/TransactionEngineFamily.swift @@ -64,6 +64,21 @@ internal enum TransactionEngineFamily: String, Hashable, Sendable, CaseIterable self == .oracle } + // After a failed statement the block takes nothing but a rollback, so one error costs every pending change. + internal var failedStatementAbortsTransaction: Bool { + switch self { + case .postgres, .redshift, .cockroach, .duckdb: + return true + case .mysql, .sqlite, .sqlServer, .oracle, .redis, .other: + return false + } + } + + // Redshift and DuckDB have no savepoint to take a failed statement back to. + internal var takesBackAFailedStatement: Bool { + self == .postgres || self == .cockroach + } + private static let familiesByTypeId: [String: TransactionEngineFamily] = [ "PostgreSQL": .postgres, "PGlite": .postgres, diff --git a/apple/TablePro/Core/Services/Execution/WriteTransactionOwner.swift b/apple/TablePro/Core/Services/Execution/WriteTransactionOwner.swift index 74cd43e9b4..e0f2587eb6 100644 --- a/apple/TablePro/Core/Services/Execution/WriteTransactionOwner.swift +++ b/apple/TablePro/Core/Services/Execution/WriteTransactionOwner.swift @@ -14,31 +14,55 @@ import TableProPluginKit /// transaction of its own then commits their pending work on PostgreSQL, implicitly commits it on /// MySQL and aborts it on DuckDB, with nothing raised and the write reporting success. /// -/// This is the one place that decision is made. ``BatchTransactionPlan/joining(_:)`` is the same -/// decision for a multi-statement run, where the statement text has a say as well. +/// This is the one place that decision is made. ``BatchTransactionPlan/joining(_:commitMode:)`` is +/// the same decision for a multi-statement run, where the statement text has a say as well. internal enum WriteTransactionOwner: Equatable { /// The app opens the transaction, verifies inside it, and commits or rolls it back. case app + // Manual Commit: the app opens the transaction and leaves it open for the user to commit, rolling back only a failure. + case held /// The session already holds one. The write joins it and sends no `BEGIN`, `COMMIT` or /// `ROLLBACK`: ending it belongs to whoever opened it. case session /// The engine has no transactions, so every statement is on the server as it runs. case none + // No default for the mode: a writer off the session's turn must say `.automatic`, or its hold is never seen. internal static func resolve( supportsTransactions: Bool, - sessionState: PluginSessionTransactionState + sessionState: PluginSessionTransactionState, + commitMode: ConnectionCommitMode ) -> WriteTransactionOwner { guard supportsTransactions else { return .none } + if commitMode == .manual, sessionState == .idle { return .held } return sessionState.permitsAppTransaction ? .app : .session } - internal var opensTransaction: Bool { - self == .app + internal var sendsBegin: Bool { + switch self { + case .app, .held: + return true + case .session, .none: + return false + } + } + + internal var commitsAtEnd: Bool { + switch self { + case .app: + return true + case .held, .session, .none: + return false + } } /// Whether the statements that already ran can still be taken back by the app. internal var canRollBack: Bool { - self == .app + switch self { + case .app, .held: + return true + case .session, .none: + return false + } } } diff --git a/apple/TablePro/Core/Services/Export/ImportService.swift b/apple/TablePro/Core/Services/Export/ImportService.swift index 3f0e86c491..ed91c4351f 100644 --- a/apple/TablePro/Core/Services/Export/ImportService.swift +++ b/apple/TablePro/Core/Services/Export/ImportService.swift @@ -413,7 +413,13 @@ final class ImportService: ObservableObject { policy: RowImportBatchPolicy ) async throws -> RowImportBatchReport { let started = Date() - let owner = ImportDataSinkAdapter(driver: driver, databaseType: connection.type, importRun: run, fileIndex: nil) + let owner = ImportDataSinkAdapter( + driver: driver, + databaseType: connection.type, + commitMode: DatabaseManager.shared.commitMode(for: connection.id), + importRun: run, + fileIndex: nil + ) var sessionScope: PluginSessionScope? var outcomes = [RowImportBatchReport.FileOutcome](repeating: .notRun, count: files.count) var ending = RowImportBatchReport.Ending.completed @@ -644,6 +650,7 @@ final class ImportService: ObservableObject { let sink = ImportDataSinkAdapter( driver: driver, databaseType: connection.type, targetTable: targetTable, columnMapping: columnMapping, sourceFields: sourceFields, rowWriting: rowWriting, + commitMode: DatabaseManager.shared.commitMode(for: connection.id), importRun: run, isCancelled: { progress.isCancelled } ) defer { diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+AutoRefresh.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+AutoRefresh.swift new file mode 100644 index 0000000000..d560a463f7 --- /dev/null +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+AutoRefresh.swift @@ -0,0 +1,24 @@ +// +// MainSplitViewController+AutoRefresh.swift +// TablePro +// + +import AppKit + +extension MainSplitViewController { + @objc func setTableAutoRefreshInterval(_ sender: Any?) { + guard let raw = (sender as? NSMenuItem)?.representedObject as? Int, + let interval = TableAutoRefreshInterval(rawValue: raw) else { return } + commandActions?.setTableAutoRefreshInterval(interval) + } + + // Nil off a table tab, so the submenu's checkmark never names another tab's interval. + var tableAutoRefreshInterval: TableAutoRefreshInterval? { + guard contentMode == .browse, isConnected else { return nil } + return commandActions?.tableAutoRefreshInterval + } + + var canSetTableAutoRefresh: Bool { + tableAutoRefreshInterval != nil && !(isViewLoaded && view.window?.attachedSheet != nil) + } +} diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+DatabaseMenuActions.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+DatabaseMenuActions.swift index ce3c3232e6..0a425d1a55 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+DatabaseMenuActions.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+DatabaseMenuActions.swift @@ -124,6 +124,62 @@ extension MainSplitViewController { workspaces.selected?.sessionState?.coordinator.safeModeStatus } + // Turning it off leaves a transaction already held open, and the bar with it. + @objc func toggleManualCommit(_ sender: Any?) { + guard let connectionId = workspaces.selectedConnectionId, + let session = DatabaseManager.shared.session(for: connectionId), + ManualCommit.isAvailable(for: session.connection.type) else { return } + DatabaseManager.shared.setCommitMode(session.commitMode == .manual ? .automatic : .manual, for: connectionId) + } + + @objc func commitOpenTransaction(_ sender: Any?) { + guard let hold = selectedTransactionHold else { return } + let window = view.window + Task { @MainActor in + await ConnectionTransactionActions.perform(window: window) { + try await ConnectionTransactionActions.commit(hold) + } + } + } + + @objc func rollBackOpenTransaction(_ sender: Any?) { + guard let hold = selectedTransactionHold else { return } + let window = view.window + Task { @MainActor in + await ConnectionTransactionActions.perform(window: window) { + try await ConnectionTransactionActions.rollBack(hold, window: window) + } + } + } + + // The hold the selected tab's database runs on: the connection's one driver, or that database's own lane. + var selectedTransactionHold: SessionTransactionTracker.Hold? { + guard let coordinator = workspaces.selected?.sessionState?.coordinator else { return nil } + let container = coordinator.tabManager.selectedTab.flatMap { coordinator.scope(for: $0)?.database } + ?? coordinator.browseDatabaseName + return DatabaseManager.shared.sessionTransactions.hold(connectionId: coordinator.connectionId, container: container) + } + + var transactionMenuState: TransactionMenuState { + guard let coordinator = workspaces.selected?.sessionState?.coordinator, + let session = DatabaseManager.shared.session(for: coordinator.connectionId) + else { return TransactionMenuState() } + let container = coordinator.tabManager.selectedTab.flatMap { coordinator.scope(for: $0)?.database } + ?? coordinator.browseDatabaseName + let reportsState = (session.driver?.supportsTransactions ?? false) + && !DatabaseManager.shared.sessionTransactions.cannotReportState( + connectionId: coordinator.connectionId, container: container + ) + let hold = selectedTransactionHold + return TransactionMenuState( + isAvailable: ManualCommit.isAvailable(for: session.connection.type), + isManualCommitOn: DatabaseManager.shared.commitMode(for: coordinator.connectionId) == .manual, + canHoldTransactions: reportsState, + holdsTransaction: hold != nil, + canCommit: hold.map { ConnectionTransactionActions.offersCommit($0, safeModeLevel: session.safeModeLevel) } ?? false + ) + } + @objc func switchSessionContext(_ sender: Any?) { guard let selection = (sender as? NSMenuItem)?.representedObject as? SessionContextSelection, let coordinator = commandActions?.coordinator else { return } diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift index af0799bf23..a63ab93d2c 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift @@ -6,6 +6,17 @@ import AppKit import TableProPluginKit +// Read only when a transaction command is validated, as the Debug menu's state is. +struct TransactionMenuState: Equatable { + // False on an engine that commits schema and maintenance statements on its own. + var isAvailable = false + var isManualCommitOn = false + // False for an engine with no transactions, or one whose live driver has only answered that it cannot say. + var canHoldTransactions = false + var holdsTransaction = false + var canCommit = false +} + // A plain value keeps `isEnabled` pure and testable, the same split `ToolbarContextResolver` uses for the toolbar. struct MenuValidationContext: Equatable { // True for any connection on screen, connected or not, so a pane that failed to dial can still be dismissed. @@ -140,6 +151,7 @@ struct MenuValidationContext: Equatable { var hasSchemaFilter = false // Filled only when a Debug menu item is validated; the menu exists only while the module is on. var routineDebug = RoutineDebugMenuState() + var transaction = TransactionMenuState() var canOpenLiveMessages = false var canDisconnect = false var isPresentingSheet = false @@ -358,10 +370,36 @@ extension MainSplitViewController: NSMenuItemValidation { case #selector(showAllSchemas(_:)): return context.isConnected && context.canFilterSchemas && context.hasSchemaFilter default: - return nil + return isTransactionCommandEnabled(selector, context: context) + } + } + + // Manual Commit stays enabled while on, so a connection that cannot hold can still turn it off. + private static func isTransactionCommandEnabled( + _ selector: Selector, + context: MenuValidationContext + ) -> Bool? { + guard transactionSelectors.contains(selector) else { return nil } + guard context.isConnected else { return false } + switch selector { + case #selector(toggleManualCommit(_:)): + return context.transaction.isAvailable + && (context.transaction.isManualCommitOn || context.transaction.canHoldTransactions) + case #selector(commitOpenTransaction(_:)): + return context.transaction.canCommit + case #selector(rollBackOpenTransaction(_:)): + return context.transaction.holdsTransaction + default: + return false } } + static let transactionSelectors: Set = [ + #selector(toggleManualCommit(_:)), + #selector(commitOpenTransaction(_:)), + #selector(rollBackOpenTransaction(_:)), + ] + // Agent mode does not draw the debug tab these act on. private static func isRoutineDebugCommandEnabled( _ selector: Selector, @@ -471,6 +509,9 @@ extension MainSplitViewController: NSMenuItemValidation { #selector(closeConnection(_:)), #selector(requestDisconnect), #selector(setSafeModeLevel(_:)), + #selector(toggleManualCommit(_:)), + #selector(commitOpenTransaction(_:)), + #selector(rollBackOpenTransaction(_:)), #selector(releaseFileLock(_:)), #selector(openContainerSwitcher(_:)), #selector(openSchemaSwitcher(_:)), @@ -539,6 +580,8 @@ extension MainSplitViewController: NSMenuItemValidation { #selector(closeAgentSession(_:)), #selector(deleteAgentSession(_:)), #selector(clearAIConversations(_:)), + #selector(commitOpenTransaction(_:)), + #selector(rollBackOpenTransaction(_:)), ] // A command this window does not own stays enabled, which keeps `performClose:` and the system's own items working. @@ -596,6 +639,9 @@ extension MainSplitViewController: NSMenuItemValidation { return context.canClearResults case #selector(saveAsFavorite(_:)): return context.canSaveAsFavorite + // The versions are on this Mac, so a tab whose connection dropped can still go back. + case #selector(revertToQueryTabVersion(_:)), #selector(browseQueryTabVersions(_:)): + return context.isQueryTab default: return nil } @@ -766,12 +812,14 @@ extension MainSplitViewController: NSMenuItemValidation { func validateMenuItem(_ menuItem: NSMenuItem) -> Bool { applyDynamicTitle(to: menuItem) guard let action = menuItem.action else { return false } + if let identifier = menuItem.identifier, Self.manualCommitFootnotes.contains(identifier) { return false } // AppKit asks this for the View menu and `validateUserInterfaceItem` for the rest, so a rule must live in both. if action == #selector(toggleSidebar(_:)) { return true } if action == #selector(toggleInspector(_:)) { return canToggleTrailingPane } if action == #selector(toggleAssistant(_:)) { return canToggleAssistant } if action == #selector(setResultView(_:)) { return canShowResultView(menuItem) } if action == #selector(setSafeModeLevel(_:)) { return canChooseSafeModeLevel(menuItem) } + if action == #selector(setTableAutoRefreshInterval(_:)) { return canSetTableAutoRefresh } if action == #selector(retryConnection) { return canReconnect } if let zoom = FontSizeZoom.editor.enablement(action, typography: AppSettingsManager.shared.typography) { return zoom @@ -792,6 +840,9 @@ extension MainSplitViewController: NSMenuItemValidation { if action == #selector(openLiveMessages(_:)) { context.canOpenLiveMessages = canOpenLiveMessages } + if Self.transactionSelectors.contains(action) { + context.transaction = transactionMenuState + } guard Self.agentSessionSelectors.contains(action) else { return context } context.agentSessionTarget = agentSessionTarget(for: menuItem)?.status return context @@ -867,6 +918,8 @@ extension MainSplitViewController: NSMenuItemValidation { setState(isCurrentResultView(menuItem) ? .on : .off, on: menuItem) case #selector(setContentModeFromMenu(_:)): setState(isCurrentContentMode(menuItem) ? .on : .off, on: menuItem) + case #selector(toggleManualCommit(_:)): + applyManualCommitState(to: menuItem) case #selector(useFlatSidebarLayout(_:)): setState(commandActions?.sidebarLayout == .flat ? .on : .off, on: menuItem) case #selector(useTreeSidebarLayout(_:)): @@ -880,6 +933,31 @@ extension MainSplitViewController: NSMenuItemValidation { } } + private func applyManualCommitState(to menuItem: NSMenuItem) { + guard !Self.manualCommitFootnotes.contains(where: { $0 == menuItem.identifier }) else { return } + let state = transactionMenuState + setState(state.isManualCommitOn ? .on : .off, on: menuItem) + let shown = Self.shownManualCommitFootnote(state, isConnected: isConnected) + for footnote in menuItem.menu?.items ?? [] { + guard let identifier = footnote.identifier, Self.manualCommitFootnotes.contains(identifier) else { continue } + let hidden = identifier != shown + if footnote.isHidden != hidden { footnote.isHidden = hidden } + } + } + + static let manualCommitFootnotes = [ + DatabaseMenuBuilder.manualCommitFootnoteIdentifier, + DatabaseMenuBuilder.manualCommitUnavailableFootnoteIdentifier, + ] + + // While on, a write is refused rather than committed, which the reporting footnote's wording does not cover. + static func shownManualCommitFootnote(_ state: TransactionMenuState, isConnected: Bool) -> NSUserInterfaceItemIdentifier? { + guard isConnected else { return nil } + guard state.isAvailable else { return DatabaseMenuBuilder.manualCommitUnavailableFootnoteIdentifier } + guard !state.canHoldTransactions, !state.isManualCommitOn else { return nil } + return DatabaseMenuBuilder.manualCommitFootnoteIdentifier + } + // The mode is in `representedObject`, so enablement needs the item, not the selector the shared table keys on. private func canShowResultView(_ menuItem: NSMenuItem) -> Bool { guard contentMode == .browse else { return false } diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+QueryTabVersions.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+QueryTabVersions.swift new file mode 100644 index 0000000000..a36806943f --- /dev/null +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+QueryTabVersions.swift @@ -0,0 +1,23 @@ +// +// MainSplitViewController+QueryTabVersions.swift +// TablePro +// + +import AppKit + +extension MainSplitViewController { + @objc func revertToQueryTabVersion(_ sender: Any?) { + guard let choice = (sender as? NSMenuItem)?.representedObject as? QueryTabRevertChoice else { return } + commandActions?.coordinator?.revertQueryTab(choice.tabId, toVersion: choice.versionId) + } + + @objc func browseQueryTabVersions(_ sender: Any?) { + guard let tabId = selectedQueryTabId else { return } + commandActions?.coordinator?.showVersionHistory(ofQueryTab: tabId) + } + + var selectedQueryTabId: UUID? { + guard let tab = commandActions?.coordinator?.tabManager.selectedTab, tab.tabType == .query else { return nil } + return tab.id + } +} diff --git a/apple/TablePro/Core/Services/Infrastructure/QuitApproval.swift b/apple/TablePro/Core/Services/Infrastructure/QuitApproval.swift index 9d14e0be65..9750866d0c 100644 --- a/apple/TablePro/Core/Services/Infrastructure/QuitApproval.swift +++ b/apple/TablePro/Core/Services/Infrastructure/QuitApproval.swift @@ -27,15 +27,59 @@ internal enum QuitApproval { EditorEventRouter.shared.flushAllPendingText() let work = registry.items(in: .app) let hasUnsavedTabs = MainContentCoordinator.hasAnyUnsavedChanges() - let losses = hasUnsavedTabs ? [String(localized: "Unsaved edits in open tabs")] : [] + let transactions = openTransactionNames( + holds: Array(DatabaseManager.shared.sessionTransactions.holds.values), + connectionName: { DatabaseManager.shared.session(for: $0)?.connection.name } + ) + let losses = (hasUnsavedTabs ? [String(localized: "Unsaved edits in open tabs")] : []) + + RunningWorkPrompt.transactionLines(for: transactions) if let prompt = RunningWorkPrompt.make(action: action, work: work, otherLosses: losses) { guard RunningWorkApproval.confirmModally(prompt) else { return false } if stopsWork { registry.cancel(work) } return true } + if let prompt = transactionPrompt(action: action, connections: transactions, hasUnsavedTabs: hasUnsavedTabs) { + return RunningWorkApproval.confirmModally(prompt) + } return !hasUnsavedTabs || approvesDiscardingUnsavedTabs() } + // The server rolls back what a quit leaves open, so a hold is asked about like unsaved work. + internal static func openTransactionNames( + holds: [SessionTransactionTracker.Hold], + connectionName: (UUID) -> String? + ) -> [String] { + var names: [String] = [] + for hold in holds { + guard let name = connectionName(hold.connectionId), !names.contains(name) else { continue } + names.append(name) + } + return names.sorted { $0.localizedStandardCompare($1) == .orderedAscending } + } + + internal static func transactionPrompt( + action: RunningWorkAction, + connections: [String], + hasUnsavedTabs: Bool + ) -> RunningWorkPrompt? { + guard let first = connections.first else { return nil } + let rolledBack = connections.count == 1 + ? String(format: String(localized: "Uncommitted changes on “%@” are rolled back when TablePro quits."), first) + : String( + format: String(localized: "Uncommitted changes on %@ are rolled back when TablePro quits."), + ListFormatter.localizedString(byJoining: connections.map { "“\($0)”" }) + ) + let unsaved = String(localized: "Some tabs have unsaved edits. Quitting will discard these changes.") + let relaunches = action == .relaunch + return RunningWorkPrompt( + title: relaunches + ? String(localized: "Relaunch with an Open Transaction?") + : String(localized: "Quit with an Open Transaction?"), + message: hasUnsavedTabs ? "\(rolledBack)\n\n\(unsaved)" : rolledBack, + confirmButton: relaunches ? String(localized: "Relaunch Anyway") : String(localized: "Quit Anyway") + ) + } + private static func approvesDiscardingUnsavedTabs() -> Bool { // A quit can come from outside the app, and a background process has no Dock icon to reach the alert by. AppActivationPolicyController.shared.activate(ignoringOtherApps: true) diff --git a/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService+CatalogChange.swift b/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService+CatalogChange.swift index 4e47982744..00b95434c5 100644 --- a/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService+CatalogChange.swift +++ b/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService+CatalogChange.swift @@ -18,11 +18,13 @@ struct CatalogTreeRefreshPlan: Equatable, Sendable { var routines: Set = [] var triggers: Set = [] var types: Set = [] + var serverObjects: Set = [] var allSchemaTables: Set = [] var isEmpty: Bool { !refreshesDatabaseList && schemaLists.isEmpty && tables.isEmpty && partitions.isEmpty - && routines.isEmpty && triggers.isEmpty && types.isEmpty && allSchemaTables.isEmpty + && routines.isEmpty && triggers.isEmpty && types.isEmpty && serverObjects.isEmpty + && allSchemaTables.isEmpty } } @@ -43,6 +45,7 @@ extension DatabaseTreeMetadataService { routineKeys: routinesState.keys, triggerKeys: triggersState.keys, typeKeys: typesState.keys, + serverObjectKeys: serverObjectsState.keys, allSchemaTableKeys: allSchemaTablesState.keys ) guard !plan.isEmpty else { return } @@ -65,6 +68,7 @@ extension DatabaseTreeMetadataService { routineKeys: routinesState.keys, triggerKeys: triggersState.keys, typeKeys: typesState.keys, + serverObjectKeys: serverObjectsState.keys, allSchemaTableKeys: allSchemaTablesState.keys ) markAllSchemaTablesChanged(plan.allSchemaTables) @@ -80,6 +84,7 @@ extension DatabaseTreeMetadataService { routineKeys: some Sequence, triggerKeys: some Sequence, typeKeys: some Sequence, + serverObjectKeys: some Sequence = EmptyCollection(), allSchemaTableKeys: some Sequence = EmptyCollection() ) -> CatalogTreeRefreshPlan { func reached(_ key: ObjectsKey) -> Bool { @@ -108,6 +113,14 @@ extension DatabaseTreeMetadataService { plan.routines = objectKeys(routineKeys, for: .routines) plan.triggers = objectKeys(triggerKeys, for: .triggers) plan.types = objectKeys(typeKeys, for: .types) + // A key with no schema lists a whole database, which a change in any of its schemas reaches. + if change.kinds.contains(.serverObjects) { + plan.serverObjects = Set(serverObjectKeys.filter { key in + guard key.connectionId == change.connectionId else { return false } + guard let schema = key.schema else { return change.reaches(database: key.database) } + return change.reaches(database: key.database, schema: schema) + }) + } if !change.kinds.isDisjoint(with: [.tables, .schemas]) { plan.allSchemaTables = Set(allSchemaTableKeys.filter { key in key.connectionId == change.connectionId && change.reaches(database: key.database) diff --git a/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService.swift b/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService.swift index dd23d2eac5..bda60636ef 100644 --- a/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService.swift +++ b/apple/TablePro/Core/Services/Query/DatabaseTreeMetadataService.swift @@ -26,6 +26,14 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { let schema: String? } + // A `.database` kind, and every kind on an engine without schemas, is keyed with no schema. + struct ServerObjectsKey: Hashable, Sendable { + let connectionId: UUID + let database: String + let schema: String? + let kindId: String + } + struct PartitionsKey: Hashable, Sendable { let connectionId: UUID let database: String @@ -70,6 +78,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { @Published private(set) var routinesState: [ObjectsKey: MetadataLoadState<[RoutineInfo]>] = [:] @Published private(set) var triggersState: [ObjectsKey: MetadataLoadState<[TriggerInfo]>] = [:] @Published private(set) var typesState: [ObjectsKey: MetadataLoadState<[UserDefinedTypeInfo]>] = [:] + @Published private(set) var serverObjectsState: [ServerObjectsKey: MetadataLoadState<[ServerObjectInfo]>] = [:] @Published private(set) var partitionsState: [PartitionsKey: MetadataLoadState<[PartitionInfo]>] = [:] @Published private(set) var allSchemaTablesState: [DatabaseKey: MetadataLoadState] = [:] @Published private(set) var listsRevisions: [UUID: Int] = [:] @@ -80,6 +89,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { private let routinesDedup = OnceTask, [RoutineInfo]>() private let triggersDedup = OnceTask, [TriggerInfo]>() private let typesDedup = OnceTask, [UserDefinedTypeInfo]>() + private let serverObjectsDedup = OnceTask, [ServerObjectInfo]>() private let partitionsDedup = OnceTask, [PartitionInfo]>() private let allSchemaTablesDedup = OnceTask() @@ -89,6 +99,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { private var routinesCommit = ListCommitState() private var triggersCommit = ListCommitState() private var typesCommit = ListCommitState() + private var serverObjectsCommit = ListCommitState() private var partitionsCommit = ListCommitState() private var allSchemaTablesFence = CommitFence() private var allSchemaTablesFreshness = CatalogFreshness() @@ -121,6 +132,16 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { TreeList(state: \.typesState, commit: \.typesCommit, dedup: typesDedup, label: "types", workload: .bulk) } + private var serverObjectLists: TreeList { + TreeList( + state: \.serverObjectsState, + commit: \.serverObjectsCommit, + dedup: serverObjectsDedup, + label: "server objects", + workload: .bulk + ) + } + private var partitionLists: TreeList { TreeList(state: \.partitionsState, commit: \.partitionsCommit, dedup: partitionsDedup, label: "partitions", workload: .interactive) } @@ -177,6 +198,45 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { typesState[Self.objectsKey(connectionId: connectionId, database: database, schema: schema)]?.value ?? [] } + func serverObjectsLoadState( + connectionId: UUID, database: String, schema: String?, kindId: String + ) -> MetadataLoadState<[ServerObjectInfo]> { + serverObjectsState[Self.serverObjectsKey(connectionId: connectionId, database: database, schema: schema, kindId: kindId)] + ?? .idle + } + + func serverObjects(connectionId: UUID, database: String, schema: String?, kindId: String) -> [ServerObjectInfo] { + serverObjectsLoadState(connectionId: connectionId, database: database, schema: schema, kindId: kindId).value ?? [] + } + + func isServerObjectsCurrent(connectionId: UUID, database: String, schema: String?, kindId: String) -> Bool { + isConnected(connectionId) && serverObjectsCommit.freshness.isCurrent( + Self.serverObjectsKey(connectionId: connectionId, database: database, schema: schema, kindId: kindId) + ) + } + + // A container lists the kinds of its own level: a schema its `.container` kinds, a schema engine's + // database its `.database` kinds, and a database without schemas every kind. + nonisolated static func serverKinds( + declared: [SidebarServerObjectKind], + schema: String?, + listsTablesPerSchema: Bool + ) -> [SidebarServerObjectKind] { + if schema?.isEmpty == false { return declared.filter { $0.scope == .container } } + return listsTablesPerSchema ? declared.filter { $0.scope == .database } : declared + } + + func serverKinds(connectionId: UUID, database: String, schema: String?) -> [SidebarServerObjectKind] { + guard let session = DatabaseManager.shared.session(for: connectionId) else { return [] } + return Self.serverKinds( + declared: SidebarServerObjectKind.declared(by: DatabaseManager.shared.driver(for: connectionId)), + schema: schema, + listsTablesPerSchema: Self.listsTablesPerSchema( + PluginManager.shared.databaseGroupingStrategy(for: session.connection.type) + ) + ) + } + func partitionsLoadState( connectionId: UUID, database: String, schema: String?, table: String ) -> MetadataLoadState<[PartitionInfo]> { @@ -239,6 +299,12 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { .isEmpty } + func serverObjectsNeedFetch(connectionId: UUID, database: String, schema: String?) -> Bool { + guard isConnected(connectionId) else { return false } + return !serverKindsNeedingFetch(Self.objectsKey(connectionId: connectionId, database: database, schema: schema)) + .isEmpty + } + func partitionsNeedFetch(connectionId: UUID, database: String, schema: String?, table: String) -> Bool { isConnected(connectionId) && partitionsCommit.freshness.needsFetch( Self.partitionsKey(connectionId: connectionId, database: database, schema: schema, table: table) @@ -276,6 +342,10 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { typesCommit.freshness.markChanged(key) marked = true } + for key in plan.serverObjects { + serverObjectsCommit.freshness.markChanged(key) + marked = true + } guard marked else { return } listsRevisions[connectionId, default: 0] &+= 1 } @@ -286,9 +356,16 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { if routinesCommit.freshness.needsFetch(key) { kinds.insert(.routines) } if browsesTriggers(key.connectionId), triggersCommit.freshness.needsFetch(key) { kinds.insert(.triggers) } if browsesUserDefinedTypes(key.connectionId), typesCommit.freshness.needsFetch(key) { kinds.insert(.types) } + if !serverKindsNeedingFetch(key).isEmpty { kinds.insert(.serverObjects) } return kinds } + private func serverKindsNeedingFetch(_ key: ObjectsKey) -> [SidebarServerObjectKind] { + serverKinds(connectionId: key.connectionId, database: key.database, schema: key.schema).filter { + serverObjectsCommit.freshness.needsFetch(serverObjectsKey(key, kindId: $0.id)) + } + } + // MARK: - All-schema tables /// Read when next asked for, never on a catalog change itself: a COMMIT is one too, and relisting @@ -540,9 +617,29 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { await self.loadUserDefinedTypes(connectionId: connectionId, database: database, schema: schema) } } + if kinds.contains(.serverObjects) { + group.addTask { await self.loadServerObjects(connectionId: connectionId, database: database, schema: schema) } + } } } + // Only the kinds of this container's level, and only those a change overtook or nobody has read. + func loadServerObjects(connectionId: UUID, database: String, schema: String?) async { + guard isConnected(connectionId) else { return } + let container = Self.objectsKey(connectionId: connectionId, database: database, schema: schema) + let kinds = serverKindsNeedingFetch(container) + await withTaskGroup(of: Void.self) { group in + for kind in kinds { + let key = serverObjectsKey(container, kindId: kind.id) + group.addTask { await self.loadServerObjectList(key) } + } + } + } + + private func loadServerObjectList(_ key: ServerObjectsKey) async { + await read(key, connectionId: key.connectionId, in: serverObjectLists, fetch: serverObjectListFetch(key)) + } + // MARK: - Refresh /// Fetches first and commits over the old list, so a refresh never empties the tree @@ -581,7 +678,18 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { async let types: Void = refreshUserDefinedTypeObjects( connectionId: connectionId, database: database, schema: schema ) - _ = await (tables, routines, triggers, types) + async let serverObjects: Void = refreshServerObjects(connectionId: connectionId, database: database, schema: schema) + _ = await (tables, routines, triggers, types, serverObjects) + } + + func refreshServerObjects(connectionId: UUID, database: String, schema: String?, kindId: String? = nil) async { + let container = Self.objectsKey(connectionId: connectionId, database: database, schema: schema) + let kinds = serverKinds(connectionId: connectionId, database: database, schema: schema) + .filter { kindId == nil || $0.id == kindId } + for kind in kinds { + let key = serverObjectsKey(container, kindId: kind.id) + await reread(key, connectionId: connectionId, in: serverObjectLists, fetch: serverObjectListFetch(key)) + } } /// A row refreshes only the fetch its kind comes from. Partitions ride with the tables, because @@ -783,6 +891,16 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { } } + private func serverObjectListFetch(_ key: ServerObjectsKey) -> ListFetch<[ServerObjectInfo]> { + let schema = key.schema + let kindId = key.kindId + return { [self] workload in + try await withDriver(connectionId: key.connectionId, database: key.database, workload: workload) { driver in + try await driver.fetchServerObjects(ofKind: kindId, schema: schema) + } + } + } + private func partitionListFetch(_ key: PartitionsKey) -> ListFetch<[PartitionInfo]> { let schema = key.schema let table = key.table @@ -817,6 +935,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { routinesCommit.freshness.removeAll { $0.connectionId == connectionId } triggersCommit.freshness.removeAll { $0.connectionId == connectionId } typesCommit.freshness.removeAll { $0.connectionId == connectionId } + serverObjectsCommit.freshness.removeAll { $0.connectionId == connectionId } partitionsCommit.freshness.removeAll { $0.connectionId == connectionId } allSchemaTablesFreshness.removeAll { $0.connectionId == connectionId } allSchemaTablesRequested = allSchemaTablesRequested.filter { $0.key.connectionId != connectionId } @@ -829,6 +948,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { routinesState = routinesState.filter { $0.key.connectionId != connectionId } triggersState = triggersState.filter { $0.key.connectionId != connectionId } typesState = typesState.filter { $0.key.connectionId != connectionId } + serverObjectsState = serverObjectsState.filter { $0.key.connectionId != connectionId } partitionsState = partitionsState.filter { $0.key.connectionId != connectionId } } @@ -852,6 +972,9 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { triggersCommit.fence.supersede(key) typesCommit.fence.supersede(key) } + for key in serverObjectsState.keys where key.connectionId == connectionId { + serverObjectsCommit.fence.supersede(key) + } for key in connectionPartitionKeys(connectionId) { partitionsCommit.fence.supersede(key) } @@ -867,6 +990,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { await routinesDedup.cancel { $0.key.connectionId == connectionId } await triggersDedup.cancel { $0.key.connectionId == connectionId } await typesDedup.cancel { $0.key.connectionId == connectionId } + await serverObjectsDedup.cancel { $0.key.connectionId == connectionId } await partitionsDedup.cancel { $0.key.connectionId == connectionId } await allSchemaTablesDedup.cancel { $0.database.connectionId == connectionId } } @@ -880,6 +1004,7 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { idlePending(\.routinesState) { $0.connectionId == connectionId } idlePending(\.triggersState) { $0.connectionId == connectionId } idlePending(\.typesState) { $0.connectionId == connectionId } + idlePending(\.serverObjectsState) { $0.connectionId == connectionId } idlePending(\.partitionsState) { $0.connectionId == connectionId } idlePending(\.allSchemaTablesState) { $0.connectionId == connectionId } } @@ -939,6 +1064,17 @@ final class DatabaseTreeMetadataService: ObservableObject, CatalogChangeTarget { return ObjectsKey(connectionId: connectionId, database: database, schema: normalized) } + private static func serverObjectsKey( + connectionId: UUID, database: String, schema: String?, kindId: String + ) -> ServerObjectsKey { + let normalized: String? = (schema?.isEmpty == true) ? nil : schema + return ServerObjectsKey(connectionId: connectionId, database: database, schema: normalized, kindId: kindId) + } + + private func serverObjectsKey(_ container: ObjectsKey, kindId: String) -> ServerObjectsKey { + ServerObjectsKey(connectionId: container.connectionId, database: container.database, schema: container.schema, kindId: kindId) + } + private static func partitionsKey( connectionId: UUID, database: String, schema: String?, table: String ) -> PartitionsKey { diff --git a/apple/TablePro/Core/Services/Query/SchemaService+ServerObjects.swift b/apple/TablePro/Core/Services/Query/SchemaService+ServerObjects.swift new file mode 100644 index 0000000000..e8493eb51a --- /dev/null +++ b/apple/TablePro/Core/Services/Query/SchemaService+ServerObjects.swift @@ -0,0 +1,60 @@ +// +// SchemaService+ServerObjects.swift +// TablePro +// + +import Foundation + +extension SchemaService { + func serverObjects(for connectionId: UUID, kindId: String) -> [ServerObjectInfo] { + serverObjectsLoadState(for: connectionId, kindId: kindId).value ?? [] + } + + func serverObjectsLoadState(for connectionId: UUID, kindId: String) -> MetadataLoadState<[ServerObjectInfo]> { + sideObjects[shownKey(connectionId)]?.serverObjects[kindId] ?? .idle + } + + func serverObjectStates(for connectionId: UUID) -> [String: MetadataLoadState<[ServerObjectInfo]>] { + sideObjects[shownKey(connectionId)]?.serverObjects ?? [:] + } + + func serverObjects(for connectionId: UUID, schema: String, kindId: String) -> [ServerObjectInfo] { + serverObjectsLoadState(for: connectionId, schema: schema, kindId: kindId).value ?? [] + } + + func serverObjectsByKind(for connectionId: UUID, schema: String) -> [String: [ServerObjectInfo]] { + (perSchemaSideObjects[catalogKey(connectionId, schema: schema)]?.serverObjects ?? [:]).compactMapValues(\.value) + } + + func serverObjectsLoadState( + for connectionId: UUID, + schema: String, + kindId: String + ) -> MetadataLoadState<[ServerObjectInfo]> { + perSchemaSideObjects[catalogKey(connectionId, schema: schema)]?.serverObjects[kindId] ?? .idle + } + + @discardableResult + func reloadServerObjects( + connectionId: UUID, + driver: DatabaseDriver, + scope: DatabaseScope?, + kindId: String + ) async -> Bool { + let key = reloadKey(connectionId, scope: scope, driver: driver) + updateSideObjects(key) { $0.serverObjects[kindId] = ($0.serverObjects[kindId] ?? .idle).enteringLoad } + bumpGeneration(connectionId) + let outcome = await Self.fetchObjectsSafely( + key: ServerObjectLoadKey(load: LoadKey(connectionId: connectionId, scope: scope), kindId: kindId), + connectionId: connectionId, + label: "server objects", + dedup: serverObjectsDedup, + fetch: { try await driver.fetchServerObjects(ofKind: kindId, schema: nil) } + ) + updateKeptSideObjects(key) { + $0.serverObjects[kindId] = ($0.serverObjects[kindId] ?? .idle).settled(by: outcome, discardingValue: false) + } + bumpGeneration(connectionId) + return outcome.didFetch + } +} diff --git a/apple/TablePro/Core/Services/Query/SchemaService.swift b/apple/TablePro/Core/Services/Query/SchemaService.swift index 5cf3d3befd..0eedb01eed 100644 --- a/apple/TablePro/Core/Services/Query/SchemaService.swift +++ b/apple/TablePro/Core/Services/Query/SchemaService.swift @@ -18,6 +18,8 @@ final class SchemaService: ObservableObject { var routines: MetadataLoadState<[RoutineInfo]> = .idle var triggers: MetadataLoadState<[TriggerInfo]> = .idle var userDefinedTypes: MetadataLoadState<[UserDefinedTypeInfo]> = .idle + // Keyed by the declared kind's id. + var serverObjects: [String: MetadataLoadState<[ServerObjectInfo]>] = [:] } @Published private(set) var states: [CatalogKey: SchemaState] = [:] @@ -51,7 +53,7 @@ final class SchemaService: ObservableObject { generations[connectionId] ?? 0 } - private func bumpGeneration(_ connectionId: UUID) { + func bumpGeneration(_ connectionId: UUID) { generations[connectionId, default: 0] &+= 1 } @@ -64,6 +66,18 @@ final class SchemaService: ObservableObject { private let perSchemaRoutinesDedup = OnceTask() private let perSchemaTriggersDedup = OnceTask() private let perSchemaTypesDedup = OnceTask() + let serverObjectsDedup = OnceTask() + private let perSchemaServerObjectsDedup = OnceTask() + + struct ServerObjectLoadKey: Hashable, Sendable { + let load: LoadKey + let kindId: String + } + + struct ServerObjectSchemaFetchKey: Hashable, Sendable { + let fetch: SchemaFetchKey + let kindId: String + } /// A schema is named inside a database, and an engine that changes database on a live /// connection reaches a `PUBLIC` in every one of them. @@ -94,11 +108,15 @@ final class SchemaService: ObservableObject { private struct SideKinds { let triggers: Bool let types: Bool + let serverKinds: [SidebarServerObjectKind] - init(_ type: DatabaseType) { + init(_ type: DatabaseType, serverKinds: [SidebarServerObjectKind] = []) { triggers = type.supportsDatabaseTriggerBrowse types = type.supportsUserDefinedTypeBrowse + self.serverKinds = serverKinds } + + var serverKindIds: [String] { serverKinds.map(\.id) } } private struct RefreshWaiter { @@ -138,7 +156,7 @@ final class SchemaService: ObservableObject { !unrefreshedCatalogs.contains(shownKey(connectionId)) } - private func shownKey(_ connectionId: UUID) -> CatalogKey { + func shownKey(_ connectionId: UUID) -> CatalogKey { shownCatalogs[connectionId]?.key ?? CatalogKey(connectionId: connectionId, database: "", schema: nil) } @@ -337,7 +355,7 @@ final class SchemaService: ObservableObject { /// Every per-schema read answers for the database `schemas(for:)` was listed from, so the /// schema rows and the objects under them describe one database even while a switch settles. - private func catalogKey(_ connectionId: UUID, schema: String) -> SchemaKey { + func catalogKey(_ connectionId: UUID, schema: String) -> SchemaKey { SchemaKey(connectionId: connectionId, database: catalogDatabase(connectionId), schema: schema) } @@ -380,7 +398,8 @@ final class SchemaService: ObservableObject { let key = catalogKey(connectionId, schema: schema) guard hasLoadedContent(key) else { return false } let side = perSchemaSideObjects[key] ?? SideObjects() - return [side.routines.erased, side.triggers.erased, side.userDefinedTypes.erased] + return ([side.routines.erased, side.triggers.erased, side.userDefinedTypes.erased] + + side.serverObjects.values.map(\.erased)) .allSatisfy { $0 == .loaded || $0 == .idle } } @@ -551,7 +570,10 @@ final class SchemaService: ObservableObject { nextLoadGeneration += 1 let generation = nextLoadGeneration schemaLoadGenerations[key] = generation - let kinds = SideKinds(driver.connection.type) + let kinds = SideKinds( + driver.connection.type, + serverKinds: SidebarServerObjectKind.declared(by: driver).filter { $0.scope == .container } + ) if !hasLoadedContent(key) { setPerSchemaState(.loading, key: key) @@ -588,6 +610,16 @@ final class SchemaService: ObservableObject { fetch: { try await driver.fetchUserDefinedTypes(schema: schema) } ) : nil + async let serverObjectsTask = fetchServerObjectsSafely( + kinds: kinds.serverKindIds, + keys: Dictionary(uniqueKeysWithValues: kinds.serverKindIds.map { + ($0, ServerObjectSchemaFetchKey(fetch: fetchKey, kindId: $0)) + }), + connectionId: connectionId, + dedup: perSchemaServerObjectsDedup, + schema: schema, + driver: driver + ) let tablesOutcome: MetadataFetchOutcome<[TableInfo]> do { @@ -606,10 +638,14 @@ final class SchemaService: ObservableObject { let routinesOutcome = await routinesTask let triggersOutcome = await triggersTask let typesOutcome = await typesTask + let serverObjectsOutcomes = await serverObjectsTask guard schemaLoadGenerations[key] == generation else { return } schemaLoadGenerations.removeValue(forKey: key) updateSchemaSideObjects(key) { side in - side = Self.settled(side, routines: routinesOutcome, triggers: triggersOutcome, types: typesOutcome) + side = Self.settled( + side, routines: routinesOutcome, triggers: triggersOutcome, types: typesOutcome, + serverObjects: serverObjectsOutcomes + ) } bumpGeneration(connectionId) } @@ -643,9 +679,10 @@ final class SchemaService: ObservableObject { await perSchemaRoutinesDedup.cancel(where: shouldCancel) await perSchemaTriggersDedup.cancel(where: shouldCancel) await perSchemaTypesDedup.cancel(where: shouldCancel) + await perSchemaServerObjectsDedup.cancel { shouldCancel($0.fetch) } } - private func updateSideObjects(_ key: CatalogKey, _ change: (inout SideObjects) -> Void) { + func updateSideObjects(_ key: CatalogKey, _ change: (inout SideObjects) -> Void) { var side = sideObjects[key] ?? SideObjects() change(&side) sideObjects[key] = side @@ -653,14 +690,14 @@ final class SchemaService: ObservableObject { /// The catalog of the scope the reload reads, not of whatever is shown when it starts: a switch /// while it waits for a connection would file one database's objects under another. - private func reloadKey(_ connectionId: UUID, scope: DatabaseScope?, driver: DatabaseDriver) -> CatalogKey { + func reloadKey(_ connectionId: UUID, scope: DatabaseScope?, driver: DatabaseDriver) -> CatalogKey { guard let scope else { return shownKey(connectionId) } return catalogKey(for: scope, type: driver.connection.type) } /// A reload that lands after `forget` dropped its catalog writes nothing, rather than bringing /// back objects of a database whose entry closed. - private func updateKeptSideObjects(_ key: CatalogKey, _ change: (inout SideObjects) -> Void) { + func updateKeptSideObjects(_ key: CatalogKey, _ change: (inout SideObjects) -> Void) { guard var side = sideObjects[key] else { return } change(&side) sideObjects[key] = side @@ -676,10 +713,11 @@ final class SchemaService: ObservableObject { _ key: CatalogKey, routines: MetadataFetchOutcome<[RoutineInfo]>, triggers: MetadataFetchOutcome<[TriggerInfo]>?, - types: MetadataFetchOutcome<[UserDefinedTypeInfo]>? + types: MetadataFetchOutcome<[UserDefinedTypeInfo]>?, + serverObjects: [String: MetadataFetchOutcome<[ServerObjectInfo]>] = [:] ) { updateSideObjects(key) { side in - side = Self.settled(side, routines: routines, triggers: triggers, types: types) + side = Self.settled(side, routines: routines, triggers: triggers, types: types, serverObjects: serverObjects) } } @@ -698,6 +736,7 @@ final class SchemaService: ObservableObject { side.routines = side.routines.settled(by: .cancelled, discardingValue: false) side.triggers = side.triggers.settled(by: .cancelled, discardingValue: false) side.userDefinedTypes = side.userDefinedTypes.settled(by: .cancelled, discardingValue: false) + side.serverObjects = side.serverObjects.mapValues { $0.settled(by: .cancelled, discardingValue: false) } } bumpGeneration(key.connectionId) } @@ -707,6 +746,9 @@ final class SchemaService: ObservableObject { next.routines = side.routines.enteringLoad if kinds.triggers { next.triggers = side.triggers.enteringLoad } if kinds.types { next.userDefinedTypes = side.userDefinedTypes.enteringLoad } + for id in kinds.serverKindIds { + next.serverObjects[id] = (side.serverObjects[id] ?? .idle).enteringLoad + } return next } @@ -715,7 +757,8 @@ final class SchemaService: ObservableObject { _ side: SideObjects, routines: MetadataFetchOutcome<[RoutineInfo]>, triggers: MetadataFetchOutcome<[TriggerInfo]>?, - types: MetadataFetchOutcome<[UserDefinedTypeInfo]>? + types: MetadataFetchOutcome<[UserDefinedTypeInfo]>?, + serverObjects: [String: MetadataFetchOutcome<[ServerObjectInfo]>] = [:] ) -> SideObjects { var next = side next.routines = side.routines.settled(by: routines, discardingValue: false) @@ -725,6 +768,9 @@ final class SchemaService: ObservableObject { if let types { next.userDefinedTypes = side.userDefinedTypes.settled(by: types, discardingValue: false) } + for (id, outcome) in serverObjects { + next.serverObjects[id] = (side.serverObjects[id] ?? .idle).settled(by: outcome, discardingValue: false) + } return next } @@ -818,6 +864,7 @@ final class SchemaService: ObservableObject { await routinesDedup.cancel { $0.connectionId == connectionId } await triggersDedup.cancel { $0.connectionId == connectionId } await typesDedup.cancel { $0.connectionId == connectionId } + await serverObjectsDedup.cancel { $0.load.connectionId == connectionId } await schemasDedup.cancel { $0.connectionId == connectionId } await cancelSchemaLoads { $0.schemaKey.connectionId == connectionId } } @@ -885,13 +932,17 @@ final class SchemaService: ObservableObject { } } if settleTablesFailed(key, message: message) { - let kinds = type.map(SideKinds.init) + let kinds = type.map { SideKinds($0) } + let serverKindIds = SidebarServerObjectKind.declared(by: DatabaseManager.shared.driver(for: connectionId)).map(\.id) updateSideObjects(key) { side in side = Self.settled( side, routines: .failed(message), triggers: kinds?.triggers == true ? .failed(message) : nil, - types: kinds?.types == true ? .failed(message) : nil + types: kinds?.types == true ? .failed(message) : nil, + serverObjects: Dictionary(uniqueKeysWithValues: serverKindIds.map { + ($0, MetadataFetchOutcome<[ServerObjectInfo]>.failed(message)) + }) ) } } else if !moved { @@ -924,7 +975,12 @@ final class SchemaService: ObservableObject { if !hasLoadedContent(key) { states[key] = .loading } - let kinds = SideKinds(type) + let grouping = PluginManager.shared.databaseGroupingStrategy(for: type) + // A hierarchical engine lists its declared kinds per schema, in `runSchemaLoad`. + let kinds = SideKinds( + type, + serverKinds: grouping == .hierarchicalSchema ? [] : SidebarServerObjectKind.declared(by: driver) + ) updateSideObjects(key) { $0 = Self.enteringLoad($0, kinds: kinds) } bumpGeneration(connectionId) @@ -938,7 +994,6 @@ final class SchemaService: ObservableObject { } let loadKey = LoadKey(connectionId: connectionId, scope: scope) - let grouping = PluginManager.shared.databaseGroupingStrategy(for: type) if grouping == .hierarchicalSchema { await runHierarchicalLoad( key: key, @@ -979,6 +1034,16 @@ final class SchemaService: ObservableObject { fetch: { try await driver.fetchUserDefinedTypes(schema: nil) } ) : nil + async let serverObjectsTask = fetchServerObjectsSafely( + kinds: kinds.serverKindIds, + keys: Dictionary(uniqueKeysWithValues: kinds.serverKindIds.map { + ($0, ServerObjectLoadKey(load: loadKey, kindId: $0)) + }), + connectionId: connectionId, + dedup: serverObjectsDedup, + schema: nil, + driver: driver + ) async let schemasTask: [String]? = supportsSchemas || listsAttached ? Self.fetchSchemasSafely( key: loadKey, @@ -1021,10 +1086,14 @@ final class SchemaService: ObservableObject { let routinesOutcome = await routinesTask let triggersOutcome = await triggersTask let typesOutcome = await typesTask + let serverObjectsOutcomes = await serverObjectsTask guard isCurrentLoadGeneration(generation, for: key, phase: "side-objects-loaded") else { return } - commitSideObjects(key, routines: routinesOutcome, triggers: triggersOutcome, types: typesOutcome) + commitSideObjects( + key, routines: routinesOutcome, triggers: triggersOutcome, types: typesOutcome, + serverObjects: serverObjectsOutcomes + ) if let loadedSchemas = await schemasTask { guard isCurrentLoadGeneration(generation, for: key, phase: "schemas-loaded") else { @@ -1198,11 +1267,44 @@ final class SchemaService: ObservableObject { } } + // One fetch per declared kind, side by side; each settles its own group. + private func fetchServerObjectsSafely( + kinds: [String], + keys: [String: Key], + connectionId: UUID, + dedup: OnceTask, + schema: String?, + driver: DatabaseDriver + ) async -> [String: MetadataFetchOutcome<[ServerObjectInfo]>] { + guard !kinds.isEmpty else { return [:] } + return await withTaskGroup(of: (String, MetadataFetchOutcome<[ServerObjectInfo]>)?.self) { group in + for kindId in kinds { + guard let key = keys[kindId] else { continue } + group.addTask { + let outcome = await Self.fetchObjectsSafely( + key: key, + connectionId: connectionId, + label: "server objects", + dedup: dedup, + fetch: { try await driver.fetchServerObjects(ofKind: kindId, schema: schema) } + ) + return (kindId, outcome) + } + } + var outcomes: [String: MetadataFetchOutcome<[ServerObjectInfo]>] = [:] + for await result in group { + guard let result else { continue } + outcomes[result.0] = result.1 + } + return outcomes + } + } + /// A failure comes back as a failure, never as an empty list. An empty list made a refresh that /// failed indistinguishable from a database with no routines, and the caller committed it over /// the loaded one: a single dropped connection emptied the sidebar's procedures and functions /// while the refresh reported success, with nothing scheduled to put them back. - private static func fetchObjectsSafely( + static func fetchObjectsSafely( key: Key, connectionId: UUID, label: String, diff --git a/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift b/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift index 4336e6c51f..9d189e0fae 100644 --- a/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift +++ b/apple/TablePro/Core/Shortcuts/ShortcutsRowWriter.swift @@ -196,7 +196,8 @@ internal enum ShortcutsRowWriter { private static func refuseHeldTransaction(on driver: DatabaseDriver, ownsConnection: Bool) async throws { let owner = WriteTransactionOwner.resolve( supportsTransactions: driver.supportsTransactions, - sessionState: await driver.heldSessionTransactionState() + sessionState: await driver.heldSessionTransactionState(), + commitMode: .automatic ) guard owner == .session else { return } guard ownsConnection else { diff --git a/apple/TablePro/Core/Storage/ConnectionLocalState.swift b/apple/TablePro/Core/Storage/ConnectionLocalState.swift index aa36566e31..a39458b3ca 100644 --- a/apple/TablePro/Core/Storage/ConnectionLocalState.swift +++ b/apple/TablePro/Core/Storage/ConnectionLocalState.swift @@ -6,18 +6,12 @@ import Foundation import os -/// Everything a deleted connection leaves behind on this device, cleaned up in one place. -/// -/// The list used to be written out at each of the three delete sites, which is how they drifted: -/// the remote-deletion path cleared two stores where the local one cleared nine, and no site had -/// ever removed a single `SidebarPersistenceKey`. A store added here is cleaned up everywhere. +// Everything a deleted connection leaves on this device, in one place: three delete sites each kept a list and drifted. @MainActor internal enum ConnectionLocalState { nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "ConnectionLocalState") - /// Who deleted the connection. A local delete leaves tombstones so the other devices follow; - /// a remote delete must not, or it pushes back a deletion the sender already made, and neither - /// may the local delete of a connection kept off iCloud. + // Only a local delete leaves tombstones; a remote one would push back the sender's own deletion. internal enum Origin { case local case localOnly @@ -89,27 +83,14 @@ internal enum ConnectionLocalState { } } - /// The two stores that can only be reached with `await`, so `purge` fires them and does not - /// wait. They belong here for the reason everything else does: written out at the delete sites, - /// the query history clear reached the two local ones and never the remote one, so a connection - /// deleted on another Mac left every statement it had ever run, with its literals, in the - /// history on this one. - /// - /// Separate from `purge` so a test can await what `purge` cannot. - /// - /// `origin` splits the SQL favorites the way `purgeFavorites` splits the table ones, and for the - /// same reason. `SyncCoordinator.applyRemoteChanges` suppresses the change tracker only for the - /// length of its own synchronous body, and this runs from a `Task` that starts after that body - /// has returned and the suppression has been reset, so a remote delete really did write - /// tombstones and push the sender's own deletion back at it. - /// - /// Query history takes no origin: it is device-local and never synced, so a remote delete - /// should forget this device's copy and has no tombstone to leave either way. + // Separate from `purge` so a test can await it. Saved queries split by origin themselves: this runs after + // `applyRemoteChanges` stopped suppressing the tracker. Query history and tab versions are device-local. internal static func purgeAsyncStores( _ connectionIds: Set, origin: Origin, sqlFavorites: SQLFavoriteManager = .shared, queryHistory: QueryHistoryManager = .shared, + queryTabVersions: QueryTabVersionStorage = .shared, syncTracker: SyncChangeTracker = .shared ) async { for connectionId in connectionIds { @@ -128,16 +109,15 @@ internal enum ConnectionLocalState { "Query history for a deleted connection could not be cleared: \(connectionId, privacy: .public)" ) } + if await !queryTabVersions.removeVersions(forConnection: connectionId) { + logger.error( + "Tab versions for a deleted connection could not be cleared: \(connectionId, privacy: .public)" + ) + } } } - /// The trailing pane's two keys, which are written straight onto the defaults object rather - /// than through a store with a `remove(for:)` of its own, so `purge`'s list of stores never - /// reached them. A deleted connection left the surface it was last showing and its inspector's - /// view mode behind, and a connection later given the same id inherited both. - /// - /// Separate from `purge` for the reason `purgeAsyncStores` is: `purge` reaches nine shared - /// singletons and a test cannot call it, while this takes the one thing it writes to. + // Written straight onto the defaults, with no store of their own; separate so a test can pass its own defaults. internal static func purgeTrailingPaneKeys( _ connectionIds: Set, defaults: UserDefaults = AppStorageEnvironment.shared.defaults @@ -148,8 +128,7 @@ internal enum ConnectionLocalState { } } - /// The in-memory registries go first. A live `SharedSidebarState` for this connection rewrites - /// its own defaults keys on the next mutation, so removing the keys under it achieves nothing. + // First: a live `SharedSidebarState` rewrites its defaults keys on its next mutation. private static func purgeLiveState(_ connectionId: UUID) { SharedSidebarState.removeConnection(connectionId) SidebarViewModel.removeConnection(connectionId) diff --git a/apple/TablePro/Core/Storage/QueryTabVersionStorage.swift b/apple/TablePro/Core/Storage/QueryTabVersionStorage.swift new file mode 100644 index 0000000000..2bf78efbc2 --- /dev/null +++ b/apple/TablePro/Core/Storage/QueryTabVersionStorage.swift @@ -0,0 +1,433 @@ +// +// QueryTabVersionStorage.swift +// TablePro +// + +import Combine +import Foundation +import os +import SQLite3 + +internal enum QueryTabVersionReason: Int, Sendable, CaseIterable { + case run = 0 + case idle = 1 + case beforeReplace = 2 + case beforeRestore = 3 +} + +internal struct QueryTabVersionSummary: Sendable, Equatable, Identifiable { + let id: Int64 + let reason: QueryTabVersionReason + let savedAt: Date +} + +internal struct QueryTabVersion: Sendable, Equatable { + let summary: QueryTabVersionSummary + let query: String +} + +// Device-local and never synced. Rows outlive a closed tab, since Reopen Closed Tab brings the same tab id back. +internal actor QueryTabVersionStorage { + static let shared = QueryTabVersionStorage() + static let retainedVersionCount = 50 + static let maximumRetentionDays = 30 + static let idleCoalescingWindow: TimeInterval = 5 * 60 + + private static let logger = Logger(subsystem: "com.TablePro", category: "QueryTabVersionStorage") + private static let secondsPerDay: TimeInterval = 86_400 + + private struct DatabaseHandle: @unchecked Sendable { + var pointer: OpaquePointer? + } + + private struct NewestRow { + let id: Int64 + let reason: QueryTabVersionReason? + let idleSince: Date? + let hasSameText: Bool + } + + private var dbHandle = DatabaseHandle() + private var isPrepared = false + private let databaseURL: URL + private let removeDatabaseOnDeinit: Bool + private let now: @Sendable () -> Date + private let historyMaxDays: @Sendable () -> Int + private let announce: @Sendable (UUID?) -> Void + private let reader: QueryTabVersionReader + + private var db: OpaquePointer? { + if !isPrepared { + isPrepared = true + setupDatabase() + } + return dbHandle.pointer + } + + init( + databaseURL: URL = QueryTabVersionStorage.defaultDatabaseURL(), + now: @escaping @Sendable () -> Date = Date.init, + historyMaxDays: @escaping @Sendable () -> Int = { AppSettingsStorage.shared.loadHistory().maxDays }, + announce: @escaping @Sendable (UUID?) -> Void = { tabId in + Task { @MainActor in AppEvents.shared.queryTabVersionsDidUpdate.send(tabId) } + }, + removeDatabaseOnDeinit: Bool = false + ) { + self.databaseURL = databaseURL + self.now = now + self.historyMaxDays = historyMaxDays + self.announce = announce + self.removeDatabaseOnDeinit = removeDatabaseOnDeinit + reader = QueryTabVersionReader(databaseURL: databaseURL) + } + + static func defaultDatabaseURL() -> URL { + let directory = AppStorageEnvironment.shared.applicationSupportRoot.appendingPathComponent("TablePro") + try? FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + return directory.appendingPathComponent("query_tab_versions.db") + } + + deinit { + if let pointer = dbHandle.pointer { + sqlite3_close_v2(pointer) + } + reader.close() + if removeDatabaseOnDeinit { + let path = databaseURL.path(percentEncoded: false) + try? FileManager.default.removeItem(atPath: path) + for suffix in ["-wal", "-shm"] { + try? FileManager.default.removeItem(atPath: path + suffix) + } + } + } + + // A shorter Query History window applies here too: this store keeps text the user deleted. + static func retentionDays(historyMaxDays: Int) -> Int { + historyMaxDays > 0 ? min(maximumRetentionDays, historyMaxDays) : maximumRetentionDays + } + + static func isRecordable(_ text: String) -> Bool { + (text as NSString).length <= TabQueryContent.maxPersistableQuerySize + && !text.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + } + + nonisolated var retentionDays: Int { + Self.retentionDays(historyMaxDays: historyMaxDays()) + } + + nonisolated func summaries( + tabId: UUID, + limit: Int = QueryTabVersionStorage.retainedVersionCount + ) -> [QueryTabVersionSummary] { + reader.summaries(tabId: tabId, savedSince: cutoff(before: now()), limit: limit) + } + + nonisolated func version(id: Int64, tabId: UUID) -> QueryTabVersion? { + reader.version(id: id, tabId: tabId, savedSince: cutoff(before: now())) + } + + @discardableResult + func record(tabId: UUID, connectionId: UUID, text: String, reason: QueryTabVersionReason) -> Bool { + guard Self.isRecordable(text), let db else { return false } + let savedAt = now() + let keptSince = cutoff(before: savedAt) + let newest = newestRow(tabId: tabId, comparedWith: text, savedSince: keptSince, db: db) + if let newest, newest.hasSameText { + // The idle row already holds this text; sealing it keeps the next pause from overwriting it. + guard reason != .idle, newest.reason == .idle else { return false } + return sealRow(newest.id, tabId: tabId, reason: reason, db: db) + } + + let written: Bool + if reason == .idle, let newest, newest.reason == .idle, let idleSince = newest.idleSince, + savedAt.timeIntervalSince(idleSince) < Self.idleCoalescingWindow { + written = replaceRow(newest.id, connectionId: connectionId, text: text, savedAt: savedAt, db: db) + } else { + written = insertRow( + tabId: tabId, + connectionId: connectionId, + text: text, + reason: reason, + savedAt: savedAt, + db: db + ) + } + guard written else { + Self.logger.error("A query tab version could not be written") + return false + } + trim(tabId: tabId, savedBefore: keptSince, db: db) + announce(tabId) + return true + } + + @discardableResult + func removeVersions(forConnection connectionId: UUID) -> Bool { + guard let db else { return false } + let removed = run("DELETE FROM tab_versions WHERE connection_id = ?;", db: db) { statement in + Self.bind(connectionId.uuidString, to: statement, at: 1) + } + if removed { truncateLog(db) } + if removed { announce(nil) } + return removed + } + + @discardableResult + func removeAll() -> Bool { + guard let db else { return false } + let removed = run("DELETE FROM tab_versions;", db: db) { _ in } + if removed { truncateLog(db) } + if removed { announce(nil) } + return removed + } + + // A checkpoint alone leaves deleted text in the -wal file until its frames are reused. + private func truncateLog(_ db: OpaquePointer) { + _ = run("PRAGMA wal_checkpoint(TRUNCATE);", db: db) { _ in } + } + + // MARK: - Setup + + private static let schemaStatements = [ + "PRAGMA journal_mode=WAL;", + "PRAGMA synchronous=NORMAL;", + // Clear History is a privacy command, so deleted text is overwritten rather than left in free pages. + "PRAGMA secure_delete=ON;", + """ + CREATE TABLE IF NOT EXISTS tab_versions ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + tab_id TEXT NOT NULL, + connection_id TEXT NOT NULL, + query TEXT NOT NULL, + reason INTEGER NOT NULL, + saved_at REAL NOT NULL, + idle_since REAL + ); + """, + "CREATE INDEX IF NOT EXISTS idx_tab_versions_tab ON tab_versions(tab_id, id);", + "CREATE INDEX IF NOT EXISTS idx_tab_versions_connection ON tab_versions(connection_id);", + "CREATE INDEX IF NOT EXISTS idx_tab_versions_saved ON tab_versions(saved_at);", + ] + + private func setupDatabase() { + try? FileManager.default.createDirectory( + at: databaseURL.deletingLastPathComponent(), + withIntermediateDirectories: true + ) + guard case .opened(let pointer, let setAside) = LocalSQLiteStoreOpener.open(at: databaseURL, now: now()) else { + Self.logger.error("The query tab versions database could not be opened") + return + } + dbHandle.pointer = pointer + // The reader may hold the file a damaged store was moved to. + reader.close() + // Snapshots of text the tabs still hold, so a damaged copy is not worth keeping. + if setAside != nil { + LocalSQLiteStoreOpener.removeSetAsideCopies(of: databaseURL) + } + for statement in Self.schemaStatements { + _ = run(statement, db: pointer) { _ in } + } + trim(tabId: nil, savedBefore: cutoff(before: now()), db: pointer) + } + + // MARK: - Writes + + private func newestRow(tabId: UUID, comparedWith text: String, savedSince cutoff: Date, db: OpaquePointer) -> NewestRow? { + let sql = """ + SELECT id, reason, idle_since, query = ? FROM tab_versions + WHERE tab_id = ? AND saved_at >= ? ORDER BY id DESC LIMIT 1; + """ + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { return nil } + Self.bind(text, to: statement, at: 1) + Self.bind(tabId.uuidString, to: statement, at: 2) + sqlite3_bind_double(statement, 3, cutoff.timeIntervalSince1970) + guard sqlite3_step(statement) == SQLITE_ROW else { return nil } + let idleSince = sqlite3_column_type(statement, 2) == SQLITE_NULL + ? nil + : Date(timeIntervalSince1970: sqlite3_column_double(statement, 2)) + return NewestRow( + id: sqlite3_column_int64(statement, 0), + reason: QueryTabVersionReason(rawValue: Int(sqlite3_column_int(statement, 1))), + idleSince: idleSince, + hasSameText: sqlite3_column_int(statement, 3) != 0 + ) + } + + // The coalescing window counts from the first snapshot of the run of idle pauses, so steady typing still leaves a + // version every few minutes. + private func replaceRow(_ id: Int64, connectionId: UUID, text: String, savedAt: Date, db: OpaquePointer) -> Bool { + run("UPDATE tab_versions SET query = ?, saved_at = ?, connection_id = ? WHERE id = ?;", db: db) { statement in + Self.bind(text, to: statement, at: 1) + sqlite3_bind_double(statement, 2, savedAt.timeIntervalSince1970) + Self.bind(connectionId.uuidString, to: statement, at: 3) + sqlite3_bind_int64(statement, 4, id) + } + } + + private func sealRow(_ id: Int64, tabId: UUID, reason: QueryTabVersionReason, db: OpaquePointer) -> Bool { + let sealed = run("UPDATE tab_versions SET reason = ?, idle_since = NULL WHERE id = ?;", db: db) { statement in + sqlite3_bind_int(statement, 1, Int32(reason.rawValue)) + sqlite3_bind_int64(statement, 2, id) + } + if sealed { announce(tabId) } + return sealed + } + + private func insertRow( + tabId: UUID, + connectionId: UUID, + text: String, + reason: QueryTabVersionReason, + savedAt: Date, + db: OpaquePointer + ) -> Bool { + let sql = """ + INSERT INTO tab_versions (tab_id, connection_id, query, reason, saved_at, idle_since) + VALUES (?, ?, ?, ?, ?, ?); + """ + return run(sql, db: db) { statement in + Self.bind(tabId.uuidString, to: statement, at: 1) + Self.bind(connectionId.uuidString, to: statement, at: 2) + Self.bind(text, to: statement, at: 3) + sqlite3_bind_int(statement, 4, Int32(reason.rawValue)) + sqlite3_bind_double(statement, 5, savedAt.timeIntervalSince1970) + if reason == .idle { + sqlite3_bind_double(statement, 6, savedAt.timeIntervalSince1970) + } else { + sqlite3_bind_null(statement, 6) + } + } + } + + private func trim(tabId: UUID?, savedBefore cutoff: Date, db: OpaquePointer) { + _ = run("DELETE FROM tab_versions WHERE saved_at < ?;", db: db) { statement in + sqlite3_bind_double(statement, 1, cutoff.timeIntervalSince1970) + } + guard let tabId else { return } + let sql = """ + DELETE FROM tab_versions WHERE tab_id = ?1 AND id NOT IN ( + SELECT id FROM tab_versions WHERE tab_id = ?1 ORDER BY id DESC LIMIT \(Self.retainedVersionCount) + ); + """ + _ = run(sql, db: db) { statement in + Self.bind(tabId.uuidString, to: statement, at: 1) + } + } + + // MARK: - Helpers + + nonisolated private func cutoff(before date: Date) -> Date { + date.addingTimeInterval(-TimeInterval(retentionDays) * Self.secondsPerDay) + } + + private func run(_ sql: String, db: OpaquePointer, bind: (OpaquePointer?) -> Void) -> Bool { + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { return false } + bind(statement) + let result = sqlite3_step(statement) + return result == SQLITE_DONE || result == SQLITE_ROW + } + + fileprivate static func bind(_ text: String, to statement: OpaquePointer?, at index: Int32) { + let transient = unsafeBitCast(-1, to: sqlite3_destructor_type.self) + sqlite3_bind_text(statement, index, text, -1, transient) + } +} + +// Menus fill synchronously, so reads take their own connection instead of a hop to the actor. +internal final class QueryTabVersionReader: @unchecked Sendable { + private let lock = NSLock() + private let databaseURL: URL + private var handle: OpaquePointer? + + init(databaseURL: URL) { + self.databaseURL = databaseURL + } + + deinit { + if let handle { + sqlite3_close_v2(handle) + } + } + + func close() { + lock.withLock { + if let handle { + sqlite3_close_v2(handle) + } + handle = nil + } + } + + func summaries(tabId: UUID, savedSince cutoff: Date, limit: Int) -> [QueryTabVersionSummary] { + let sql = """ + SELECT id, reason, saved_at FROM tab_versions + WHERE tab_id = ? AND saved_at >= ? ORDER BY id DESC LIMIT ?; + """ + return lock.withLock { + guard let db = openIfNeeded() else { return [] } + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { return [] } + QueryTabVersionStorage.bind(tabId.uuidString, to: statement, at: 1) + sqlite3_bind_double(statement, 2, cutoff.timeIntervalSince1970) + sqlite3_bind_int(statement, 3, Int32(clamping: limit)) + var rows: [QueryTabVersionSummary] = [] + while sqlite3_step(statement) == SQLITE_ROW { + if let summary = Self.summary(from: statement) { + rows.append(summary) + } + } + return rows + } + } + + func version(id: Int64, tabId: UUID, savedSince cutoff: Date) -> QueryTabVersion? { + let sql = """ + SELECT id, reason, saved_at, query FROM tab_versions + WHERE id = ? AND tab_id = ? AND saved_at >= ? LIMIT 1; + """ + return lock.withLock { + guard let db = openIfNeeded() else { return nil } + var statement: OpaquePointer? + defer { sqlite3_finalize(statement) } + guard sqlite3_prepare_v2(db, sql, -1, &statement, nil) == SQLITE_OK else { return nil } + sqlite3_bind_int64(statement, 1, id) + QueryTabVersionStorage.bind(tabId.uuidString, to: statement, at: 2) + sqlite3_bind_double(statement, 3, cutoff.timeIntervalSince1970) + guard sqlite3_step(statement) == SQLITE_ROW, + let summary = Self.summary(from: statement), + let text = sqlite3_column_text(statement, 3) else { return nil } + return QueryTabVersion(summary: summary, query: String(cString: text)) + } + } + + // Opened without create: before the first write there is nothing to read. + private func openIfNeeded() -> OpaquePointer? { + if let handle { return handle } + let path = databaseURL.path(percentEncoded: false) + guard FileManager.default.fileExists(atPath: path) else { return nil } + var opened: OpaquePointer? + guard sqlite3_open_v2(path, &opened, SQLITE_OPEN_READWRITE, nil) == SQLITE_OK, let opened else { + sqlite3_close_v2(opened) + return nil + } + sqlite3_busy_timeout(opened, 1_000) + sqlite3_exec(opened, "PRAGMA query_only = 1;", nil, nil, nil) + handle = opened + return opened + } + + private static func summary(from statement: OpaquePointer?) -> QueryTabVersionSummary? { + guard let reason = QueryTabVersionReason(rawValue: Int(sqlite3_column_int(statement, 1))) else { return nil } + return QueryTabVersionSummary( + id: sqlite3_column_int64(statement, 0), + reason: reason, + savedAt: Date(timeIntervalSince1970: sqlite3_column_double(statement, 2)) + ) + } +} diff --git a/apple/TablePro/Core/VersionHistory/LinkedFileVersionHistoryProvider.swift b/apple/TablePro/Core/VersionHistory/LinkedFileVersionHistoryProvider.swift index 9010bba72d..ba84225491 100644 --- a/apple/TablePro/Core/VersionHistory/LinkedFileVersionHistoryProvider.swift +++ b/apple/TablePro/Core/VersionHistory/LinkedFileVersionHistoryProvider.swift @@ -70,7 +70,7 @@ internal struct LinkedFileVersionHistoryProvider: VersionHistoryProvider { throw VersionHistoryError.undecodableContent } return content - case .savedQueryVersion: + case .savedQueryVersion, .tabVersion: throw VersionHistoryError.versionNotFound } } diff --git a/apple/TablePro/Core/VersionHistory/QueryTabVersionHistoryProvider.swift b/apple/TablePro/Core/VersionHistory/QueryTabVersionHistoryProvider.swift new file mode 100644 index 0000000000..132e22be01 --- /dev/null +++ b/apple/TablePro/Core/VersionHistory/QueryTabVersionHistoryProvider.swift @@ -0,0 +1,85 @@ +// +// QueryTabVersionHistoryProvider.swift +// TablePro +// + +import Foundation + +internal struct LiveQueryTab: Sendable, Equatable { + let connectionId: UUID + let text: String +} + +internal struct QueryTabVersionHistoryProvider: VersionHistoryProvider { + let tabId: UUID + let storage: QueryTabVersionStorage + // Nil once the tab is closed. + let liveTab: @MainActor @Sendable () -> LiveQueryTab? + // False when the tab is gone by the time the text arrives. + let write: @MainActor @Sendable (String) -> Bool + var isCapturePaused: @MainActor @Sendable () -> Bool = { QueryHistoryCaptureStore.isPaused } + + func loadHistory() async throws -> VersionHistoryPage { + guard await liveTab() != nil else { throw VersionHistoryError.tabClosed } + let past = storage.summaries(tabId: tabId).map { Self.entry(for: $0) } + return VersionHistoryPage( + entries: [VersionHistoryEntry(reference: .current)] + past, + notice: .keepsRecentVersions(count: QueryTabVersionStorage.retainedVersionCount, days: storage.retentionDays) + ) + } + + func content(of reference: VersionHistoryReference) async throws -> String { + switch reference { + case .current: + guard let live = await liveTab() else { throw VersionHistoryError.tabClosed } + return live.text + case .tabVersion(let id): + guard let version = storage.version(id: id, tabId: tabId) else { + throw VersionHistoryError.versionNotFound + } + return version.query + case .savedQueryVersion, .gitRevision: + throw VersionHistoryError.versionNotFound + } + } + + // Restore is recoverable through the version it records first, since the target editor is rarely mounted. + func prepareRestore(_ reference: VersionHistoryReference) async throws -> VersionRestorePlan { + guard case .tabVersion(let id) = reference, let version = storage.version(id: id, tabId: tabId) else { + throw VersionHistoryError.versionNotFound + } + guard await liveTab() != nil else { throw VersionHistoryError.tabClosed } + return VersionRestorePlan(replacesUncommittedChanges: false) { + guard let live = await liveTab() else { throw VersionHistoryError.tabClosed } + if await !isCapturePaused() { + await storage.record( + tabId: tabId, + connectionId: live.connectionId, + text: live.text, + reason: .beforeRestore + ) + } + guard await write(version.query) else { throw VersionHistoryError.tabClosed } + } + } + + static func entry(for summary: QueryTabVersionSummary) -> VersionHistoryEntry { + VersionHistoryEntry( + reference: .tabVersion(id: summary.id), + summary: summary.reason.summary, + date: summary.savedAt + ) + } +} + +extension QueryTabVersionReason { + // An idle snapshot has no label, so its title is its date. + var summary: String? { + switch self { + case .run: String(localized: "Run") + case .idle: nil + case .beforeReplace: String(localized: "Before Replace") + case .beforeRestore: String(localized: "Before Restore") + } + } +} diff --git a/apple/TablePro/Core/VersionHistory/QueryTabVersionRecorder.swift b/apple/TablePro/Core/VersionHistory/QueryTabVersionRecorder.swift new file mode 100644 index 0000000000..31a456e4b5 --- /dev/null +++ b/apple/TablePro/Core/VersionHistory/QueryTabVersionRecorder.swift @@ -0,0 +1,28 @@ +// +// QueryTabVersionRecorder.swift +// TablePro +// + +import Foundation + +@MainActor +internal struct QueryTabVersionRecorder { + let storage: QueryTabVersionStorage + // The Query History pause means "record nothing I run on this Mac", and a tab's drafts are the same text. + var isCapturePaused: @MainActor @Sendable () -> Bool + + static let live = QueryTabVersionRecorder(storage: .shared, isCapturePaused: { QueryHistoryCaptureStore.isPaused }) + + // The text is read now, so a later edit cannot change what this version holds. + @discardableResult + func record(_ tab: QueryTab?, connectionId: UUID, reason: QueryTabVersionReason) -> Task? { + guard let tab, tab.tabType == .query, !isCapturePaused() else { return nil } + let text = tab.content.query + guard QueryTabVersionStorage.isRecordable(text) else { return nil } + let storage = storage + let tabId = tab.id + return Task { + await storage.record(tabId: tabId, connectionId: connectionId, text: text, reason: reason) + } + } +} diff --git a/apple/TablePro/Core/VersionHistory/SavedQueryVersionHistoryProvider.swift b/apple/TablePro/Core/VersionHistory/SavedQueryVersionHistoryProvider.swift index 7e4cfc4571..6b6abf5d2a 100644 --- a/apple/TablePro/Core/VersionHistory/SavedQueryVersionHistoryProvider.swift +++ b/apple/TablePro/Core/VersionHistory/SavedQueryVersionHistoryProvider.swift @@ -38,7 +38,7 @@ internal struct SavedQueryVersionHistoryProvider: VersionHistoryProvider { return favorite.query case .savedQueryVersion(let id): return try await version(id: id).query - case .gitRevision: + case .gitRevision, .tabVersion: throw VersionHistoryError.versionNotFound } } diff --git a/apple/TablePro/Core/VersionHistory/VersionHistoryModels.swift b/apple/TablePro/Core/VersionHistory/VersionHistoryModels.swift index a394028ad3..63fe7e18b2 100644 --- a/apple/TablePro/Core/VersionHistory/VersionHistoryModels.swift +++ b/apple/TablePro/Core/VersionHistory/VersionHistoryModels.swift @@ -8,12 +8,14 @@ import Foundation internal enum VersionHistorySubject: Codable, Hashable, Sendable { case savedQuery(id: UUID) case linkedFile(url: URL) + case queryTab(id: UUID) } internal enum VersionHistoryReference: Hashable, Sendable { case current case savedQueryVersion(id: Int64) case gitRevision(commit: String, path: String) + case tabVersion(id: Int64) var shortRevision: String? { guard case .gitRevision(let commit, _) = self else { return nil } @@ -35,6 +37,7 @@ internal struct VersionHistoryEntry: Identifiable, Hashable, Sendable { internal enum VersionHistoryNotice: Hashable, Sendable { case keepsLatestVersions(Int) case showsLatestCommits(Int) + case keepsRecentVersions(count: Int, days: Int) } internal struct VersionHistoryPage: Equatable, Sendable { @@ -64,6 +67,7 @@ internal enum VersionHistoryError: LocalizedError, Equatable { case storedInLargeFileStorage case fileChangedBeforeWriting case restoreFailed(String) + case tabClosed var errorDescription: String? { switch self { @@ -87,6 +91,8 @@ internal enum VersionHistoryError: LocalizedError, Equatable { return String(localized: "The file or its staged version changed before it could be written. Nothing was replaced.") case .restoreFailed(let message): return String(format: String(localized: "The version could not be restored: %@"), message) + case .tabClosed: + return String(localized: "This tab is closed. Reopen it to see its history.") } } } diff --git a/apple/TablePro/Models/Connection/ConnectionCommitMode.swift b/apple/TablePro/Models/Connection/ConnectionCommitMode.swift new file mode 100644 index 0000000000..8245a4edb7 --- /dev/null +++ b/apple/TablePro/Models/Connection/ConnectionCommitMode.swift @@ -0,0 +1,11 @@ +// +// ConnectionCommitMode.swift +// TablePro +// + +import Foundation + +enum ConnectionCommitMode: String, Sendable, Equatable, CaseIterable { + case automatic + case manual +} diff --git a/apple/TablePro/Models/Connection/ConnectionSession.swift b/apple/TablePro/Models/Connection/ConnectionSession.swift index bfac87017e..d576075016 100644 --- a/apple/TablePro/Models/Connection/ConnectionSession.swift +++ b/apple/TablePro/Models/Connection/ConnectionSession.swift @@ -24,6 +24,7 @@ struct ConnectionSession: Identifiable { // The level in force; the toolbar changes it, so `connection.safeModeLevel` goes stale. var safeModeLevel: SafeModeLevel + var commitMode: ConnectionCommitMode // Per-connection state var selectedTables: Set = [] @@ -64,6 +65,7 @@ struct ConnectionSession: Identifiable { globalSeconds: 0 ) self.safeModeLevel = connection.safeModeLevel + self.commitMode = connection.commitMode self.connectedAt = Date() self.lastActiveAt = Date() } diff --git a/apple/TablePro/Models/Connection/DatabaseConnection.swift b/apple/TablePro/Models/Connection/DatabaseConnection.swift index f981693eb3..3b90b24275 100644 --- a/apple/TablePro/Models/Connection/DatabaseConnection.swift +++ b/apple/TablePro/Models/Connection/DatabaseConnection.swift @@ -156,6 +156,7 @@ struct DatabaseConnection: Identifiable, Hashable { static let postDisconnectScriptKey = "postDisconnectScript" static let scriptTimeoutSecondsKey = "scriptTimeoutSeconds" static let completionIdentifierCaseKey = "completionIdentifierCase" + static let commitModeKey = "commitMode" static let binaryInlineLimitKilobytesKey = "binaryInlineLimitKB" static let binaryInlineLimitKilobytesRange = 1 ... 1_048_576 @@ -354,6 +355,19 @@ struct DatabaseConnection: Identifiable, Hashable { } } + var commitMode: ConnectionCommitMode { + get { + additionalFields[Self.commitModeKey].flatMap(ConnectionCommitMode.init(rawValue:)) ?? .automatic + } + set { + if newValue == .automatic { + additionalFields.removeValue(forKey: Self.commitModeKey) + } else { + additionalFields[Self.commitModeKey] = newValue.rawValue + } + } + } + /// A socket path stands in for the built-in Host and Port, so only a `.network` driver can be /// reached through one. A server named in plugin fields, like DuckDB's Quack, listens on TCP /// alone, and a path stored for it would send the tunnel somewhere nothing answers. diff --git a/apple/TablePro/Models/Query/DatabaseObjectRef.swift b/apple/TablePro/Models/Query/DatabaseObjectRef.swift index 0c0814ec70..dd35c068ea 100644 --- a/apple/TablePro/Models/Query/DatabaseObjectRef.swift +++ b/apple/TablePro/Models/Query/DatabaseObjectRef.swift @@ -14,8 +14,10 @@ enum DatabaseObjectKind: String, Codable, Sendable, Hashable { case userType case view case materializedView + // A kind the driver declares; the ref's `serverKind` says which one. + case serverObject - var sidebarObjectKind: SidebarObjectKind { + var sidebarObjectKind: SidebarObjectKind? { switch self { case .procedure: return .procedure case .function: return .function @@ -23,6 +25,7 @@ enum DatabaseObjectKind: String, Codable, Sendable, Hashable { case .userType: return .type case .view: return .view case .materializedView: return .materializedView + case .serverObject: return nil } } @@ -37,11 +40,30 @@ enum DatabaseObjectKind: String, Codable, Sendable, Hashable { } var displayName: String { - sidebarObjectKind.displayName + sidebarObjectKind?.displayName ?? String(localized: "Object") } var iconName: String { - sidebarObjectKind.iconName + sidebarObjectKind?.iconName ?? "shippingbox" + } +} + +struct ServerObjectKindRef: Hashable, Codable, Sendable { + let id: String + let displayName: String + let iconName: String + // Optional so a ref written without it decodes, and reads as a container kind. + let scope: SidebarServerObjectKind.Scope? + + init(id: String, displayName: String, iconName: String, scope: SidebarServerObjectKind.Scope? = nil) { + self.id = id + self.displayName = displayName + self.iconName = iconName + self.scope = scope + } + + init(_ kind: SidebarServerObjectKind) { + self.init(id: kind.id, displayName: kind.displayName, iconName: kind.iconName, scope: kind.scope) } } @@ -69,6 +91,9 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { /// re-list an entire schema to recover it, and short enough to persist with the tab. let attributes: [ObjectAttribute] + // Server objects only, and optional so a tab persisted before them still decodes. + let serverKind: ServerObjectKindRef? + init( kind: DatabaseObjectKind, name: String, @@ -78,7 +103,8 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { identity: String? = nil, argumentSignature: String? = nil, typeKind: UserDefinedTypeInfo.Kind? = nil, - attributes: [ObjectAttribute] = [] + attributes: [ObjectAttribute] = [], + serverKind: ServerObjectKindRef? = nil ) { self.kind = kind self.name = name @@ -89,6 +115,7 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { self.argumentSignature = argumentSignature self.typeKind = typeKind self.attributes = attributes + self.serverKind = serverKind } init(routine: RoutineInfo, database: String) { @@ -132,8 +159,19 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { ) } - /// What the tab is titled and what the viewer's header shows: enough to tell two overloads - /// apart, and enough to tell two same-named triggers on different tables apart. + init(serverObject: ServerObjectInfo, kind: SidebarServerObjectKind, database: String) { + self.init( + kind: .serverObject, + name: serverObject.name, + database: database, + schema: serverObject.schema, + identity: serverObject.identity, + attributes: serverObject.attributes, + serverKind: ServerObjectKindRef(kind) + ) + } + + // Enough to tell two overloads, or two same-named triggers on different tables, apart. var displayIdentity: String { switch kind { case .procedure, .function: @@ -144,19 +182,31 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { return String(format: String(localized: "%1$@ on %2$@"), qualifiedName, table) case .userType, .view, .materializedView: return qualifiedName + case .serverObject: + return isDatabaseScoped ? name : qualifiedName } } - /// The kind's name for the header capsule. A type says which kind of type it is, because an - /// enum and a domain are edited differently and the reader should know which one opened. + // A database-wide object's schema is where it keeps its members, never part of its name. + var isDatabaseScoped: Bool { + kind == .serverObject && serverKind?.scope == .database + } + + // A type names its own kind, since an enum and a domain are edited differently. var kindDisplayName: String { - guard kind == .userType else { return kind.displayName } - return (typeKind ?? .other).displayName + switch kind { + case .userType: return (typeKind ?? .other).displayName + case .serverObject: return serverKind?.displayName ?? kind.displayName + case .procedure, .function, .trigger, .view, .materializedView: return kind.displayName + } } var kindIconName: String { - guard kind == .userType else { return kind.iconName } - return (typeKind ?? .other).iconName + switch kind { + case .userType: return (typeKind ?? .other).iconName + case .serverObject: return serverKind?.iconName ?? kind.iconName + case .procedure, .function, .trigger, .view, .materializedView: return kind.iconName + } } var qualifiedName: String { @@ -164,8 +214,7 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { return "\(schema).\(name)" } - /// A ref built where no database was selected carries an empty one, and an empty database is - /// server-scoped. Resolving it once keeps the tab-dedup key and the loader's scope agreeing. + // An empty database is server-scoped; resolving it once keeps the tab-dedup key and the loader's scope agreeing. func resolvingDatabase(_ fallback: String) -> DatabaseObjectRef { guard database.isEmpty, !fallback.isEmpty else { return self } return DatabaseObjectRef( @@ -177,7 +226,8 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { identity: identity, argumentSignature: argumentSignature, typeKind: typeKind, - attributes: attributes + attributes: attributes, + serverKind: serverKind ) } @@ -191,7 +241,7 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { argumentSignature: argumentSignature, identity: identity ) - case .trigger, .userType, .view, .materializedView: + case .trigger, .userType, .view, .materializedView, .serverObject: return nil } } @@ -212,16 +262,26 @@ struct DatabaseObjectRef: Hashable, Codable, Sendable { ) } - /// Whether two refs name one object. The attributes are what a listing learned and change with - /// a comment edit, so they are left out. An identity is only compared, never trusted alone: some - /// drivers fill it with the object's kind or its signature, which many objects share. + var serverObject: ServerObjectInfo? { + guard kind == .serverObject, let serverKind else { return nil } + return ServerObjectInfo( + kindId: serverKind.id, + name: name, + schema: schema, + identity: identity, + attributes: attributes + ) + } + + // Attributes change with a comment edit, and some drivers fill identity with a value many objects share. func namesSameObject(as other: DatabaseObjectRef) -> Bool { guard kind == other.kind, database == other.database, name == other.name, schema == other.schema, table == other.table, - argumentSignature == other.argumentSignature + argumentSignature == other.argumentSignature, + serverKind?.id == other.serverKind?.id else { return false } guard let identity, let otherIdentity = other.identity else { return true } return identity == otherIdentity diff --git a/apple/TablePro/Models/Query/QueryTab.swift b/apple/TablePro/Models/Query/QueryTab.swift index f5f32b7f12..314d4336f9 100644 --- a/apple/TablePro/Models/Query/QueryTab.swift +++ b/apple/TablePro/Models/Query/QueryTab.swift @@ -66,6 +66,9 @@ struct QueryTab: Identifiable, Equatable { var sessionHighlightRules: [HighlightRule] = [] // Set only by Reveal, after the user authenticated; never saved, so a reopened tab masks again. var revealsMaskedValues = false + // Never saved: a relaunch must not start polling a server with nobody watching. + var autoRefreshInterval: TableAutoRefreshInterval = .off + var autoRefreshPause: TableAutoRefreshPause? var pagination: PaginationState var chartConfiguration: ResultChartConfiguration var mapConfiguration: ResultMapConfiguration @@ -375,6 +378,8 @@ struct QueryTab: Identifiable, Equatable { && lhs.sortState == rhs.sortState && lhs.valueFilter == rhs.valueFilter && lhs.sessionHighlightRules == rhs.sessionHighlightRules + && lhs.autoRefreshInterval == rhs.autoRefreshInterval + && lhs.autoRefreshPause == rhs.autoRefreshPause && lhs.chartConfiguration == rhs.chartConfiguration && lhs.mapConfiguration == rhs.mapConfiguration && lhs.display == rhs.display diff --git a/apple/TablePro/Models/Query/QueryTabManager.swift b/apple/TablePro/Models/Query/QueryTabManager.swift index 42d045985b..02dd5453ad 100644 --- a/apple/TablePro/Models/Query/QueryTabManager.swift +++ b/apple/TablePro/Models/Query/QueryTabManager.swift @@ -526,7 +526,8 @@ final class QueryTabManager: ObservableObject { var newTab = QueryTab(title: Self.objectSourceTitle(for: objectRef), tabType: .objectSource) newTab.tableContext.isEditable = false newTab.tableContext.databaseName = objectRef.database - newTab.tableContext.schemaName = objectRef.schema + // A database-wide object's schema is where it installs its objects, not a namespace to browse. + newTab.tableContext.schemaName = objectRef.isDatabaseScoped ? nil : objectRef.schema newTab.display.objectRef = objectRef newTab.hasUserInteraction = true tabs.append(newTab) @@ -639,6 +640,8 @@ final class QueryTabManager: ObservableObject { case .userType: format = String(localized: "Type: %@") case .view: format = String(localized: "View: %@") case .materializedView: format = String(localized: "Materialized View: %@") + case .serverObject: + return String(format: String(localized: "%1$@: %2$@"), objectRef.kindDisplayName, objectRef.displayIdentity) } return String(format: format, objectRef.displayIdentity) } @@ -697,6 +700,8 @@ final class QueryTabManager: ObservableObject { tab.restoredSortSource = .unset tab.selectionSnapshot = .empty tab.pendingChanges = TabChangeSnapshot() + tab.autoRefreshInterval = .off + tab.autoRefreshPause = nil tab.hasUserInteraction = false tab.filterState = TabFilterState() tab.valueFilter = GridValueFilterState() diff --git a/apple/TablePro/Models/Query/ResultStatusModel.swift b/apple/TablePro/Models/Query/ResultStatusModel.swift index 665075df18..b7efcf26d7 100644 --- a/apple/TablePro/Models/Query/ResultStatusModel.swift +++ b/apple/TablePro/Models/Query/ResultStatusModel.swift @@ -44,6 +44,7 @@ struct ResultStatusControls: Equatable { var showsColumns = false var showsHighlightRules = false var showsFilters = false + var showsAutoRefresh = false var showsPagination = false /// First, Previous, Next, Last and the page number, which an engine that cannot skip rows has /// no use for. The rows-per-page menu stays, because it still sets how many leading rows load. @@ -130,6 +131,8 @@ struct ResultStatusModel: Equatable { controls.showsHighlightRules = viewMode == .data && describesAResult controls.showsSelectionSummary = viewMode == .data && describesAResult controls.showsFilters = viewMode.showsRowFilters && isTable && snapshot.hasTableName + // Data only, the one mode a tick runs in; a query tab's SQL may write, so it never re-runs on a timer. + controls.showsAutoRefresh = viewMode == .data && isTable && snapshot.hasTableName controls.showsPagination = viewMode.showsResultScope && isTable && snapshot.hasTableName controls.showsPageNavigation = controls.showsPagination && snapshot.paginationCapability.allowsSeeking diff --git a/apple/TablePro/Models/Query/ResultStatusPresentation.swift b/apple/TablePro/Models/Query/ResultStatusPresentation.swift index 54c3832824..1b34bb2bf7 100644 --- a/apple/TablePro/Models/Query/ResultStatusPresentation.swift +++ b/apple/TablePro/Models/Query/ResultStatusPresentation.swift @@ -52,6 +52,8 @@ struct ResultStatusPresentation: Equatable { /// ("2/4"). It never leaves the bar: Pin and Close live in its menu and have no other one-click /// route, which is the same reason nothing that opens a popover may leave. let resultSetMenuIsSpelledOut: Bool + // Opens a menu, not a popover, and Database > Auto Refresh carries the same choice. + let showsAutoRefresh: Bool init(tier: StatusBarTier) { switch tier { @@ -61,18 +63,21 @@ struct ResultStatusPresentation: Equatable { showsEdgePageButtons = true pageSizeIsInline = true resultSetMenuIsSpelledOut = true + showsAutoRefresh = true case .compact: showsControlTitles = false modeSwitcherIsSegmented = true showsEdgePageButtons = false pageSizeIsInline = true resultSetMenuIsSpelledOut = false + showsAutoRefresh = true case .narrow: showsControlTitles = false modeSwitcherIsSegmented = false showsEdgePageButtons = false pageSizeIsInline = false resultSetMenuIsSpelledOut = false + showsAutoRefresh = false } } } diff --git a/apple/TablePro/Models/Query/ServerObjectInfo.swift b/apple/TablePro/Models/Query/ServerObjectInfo.swift new file mode 100644 index 0000000000..1aa231984e --- /dev/null +++ b/apple/TablePro/Models/Query/ServerObjectInfo.swift @@ -0,0 +1,90 @@ +// +// ServerObjectInfo.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +// One object of a kind the driver declares. Equality follows `id`, so a definition read later never +// makes a reload stop matching the row it replaces. +struct ServerObjectInfo: Identifiable, Hashable, Sendable { + let kindId: String + let name: String + let schema: String? + let identity: String? + let detail: String? + let isEnabled: Bool? + let definition: String? + let attributes: [ObjectAttribute] + + init( + kindId: String, + name: String, + schema: String? = nil, + identity: String? = nil, + detail: String? = nil, + isEnabled: Bool? = nil, + definition: String? = nil, + attributes: [ObjectAttribute] = [] + ) { + self.kindId = kindId + self.name = name + self.schema = schema + self.identity = identity + self.detail = detail + self.isEnabled = isEnabled + self.definition = definition + self.attributes = attributes + } + + var id: String { + let path = IdentityPath.qualified(name: name, schema: schema) + guard let identity, !identity.isEmpty else { return "\(kindId)_\(path)" } + return "\(kindId)_\(path)_\(identity)" + } + + static func == (lhs: ServerObjectInfo, rhs: ServerObjectInfo) -> Bool { + lhs.id == rhs.id + } + + func hash(into hasher: inout Hasher) { + hasher.combine(id) + } + + func adoptingSchema(_ fallback: String?) -> ServerObjectInfo { + guard schema?.isEmpty != false, let fallback, !fallback.isEmpty else { return self } + return ServerObjectInfo( + kindId: kindId, name: name, schema: fallback, identity: identity, detail: detail, + isEnabled: isEnabled, definition: definition, attributes: attributes + ) + } +} + +extension ServerObjectInfo { + init(_ object: PluginServerObjectInfo) { + self.init( + kindId: object.kindId, + name: object.name, + schema: object.schema, + identity: object.identity, + detail: object.detail, + isEnabled: object.isEnabled, + definition: object.definition, + attributes: object.attributes.map(ObjectAttribute.init) + ) + } + + var pluginObject: PluginServerObjectInfo { + PluginServerObjectInfo( + kindId: kindId, + name: name, + schema: schema, + identity: identity, + detail: detail, + isEnabled: isEnabled, + definition: definition, + attributes: attributes.map(\.pluginAttribute) + ) + } +} diff --git a/apple/TablePro/Models/Query/TableAutoRefreshGate.swift b/apple/TablePro/Models/Query/TableAutoRefreshGate.swift new file mode 100644 index 0000000000..11a638ee1b --- /dev/null +++ b/apple/TablePro/Models/Query/TableAutoRefreshGate.swift @@ -0,0 +1,96 @@ +// +// TableAutoRefreshGate.swift +// TablePro +// + +import Foundation + +enum TableAutoRefreshPause: Equatable, Sendable { + case notVisible + case disconnected + case otherViewMode + case openTransaction + case unsavedChanges + case editing + case interacting + case valueWindowOpen + case loading + case safeModePrompt + case unkeyedSelection + + // Nil where nobody can read the bar's help anyway, or the pause lasts only as long as a gesture. + var help: String? { + switch self { + case .disconnected: + String(localized: "Auto Refresh waits until the connection is back.") + case .openTransaction: + String(localized: "Auto Refresh waits while a transaction is open.") + case .unsavedChanges: + String(localized: "Auto Refresh waits while the table has unsaved changes.") + case .editing: + String(localized: "Auto Refresh waits while a cell is being edited.") + case .valueWindowOpen: + String(localized: "Auto Refresh waits while a value is open in its own window.") + case .loading: + String(localized: "Auto Refresh waits while rows load.") + case .unkeyedSelection: + String(localized: "Auto Refresh waits while rows are selected in a table without a primary key.") + case .notVisible, .otherViewMode, .interacting, .safeModePrompt: + nil + } + } +} + +enum TableAutoRefreshDecision: Equatable, Sendable { + case refresh + case wait(TableAutoRefreshPause) + case stop +} + +struct TableAutoRefreshGate: Equatable, Sendable { + enum Load: Equatable, Sendable { + case idle + case busy + } + + var isTableTab = true + var previousTickFailed = false + var hasLoaded = true + var windowIsVisible = true + var isSelectedInWindow = true + var isConnected = true + var viewMode: ResultsViewMode = .data + var holdsTransaction = false + var holdsUnsavedWork = false + var hasSidebarDataWork = false + var isEditingCell = false + var gridHoldsTransientUI = false + var gridIsTrackingInteraction = false + var inspectorHoldsValueWindow = false + var load: Load = .idle + var isCountPending = false + var isLoadingMore = false + var showsSafeModePrompt = false + var hasSelection = false + var hasPrimaryKey = true + + // Never the app being active: watching a jobs table while working in another app is the point. + var decision: TableAutoRefreshDecision { + guard isTableTab, !previousTickFailed else { return .stop } + guard windowIsVisible, isSelectedInWindow else { return .wait(.notVisible) } + guard isConnected else { return .wait(.disconnected) } + guard viewMode == .data else { return .wait(.otherViewMode) } + guard !holdsTransaction else { return .wait(.openTransaction) } + // Ahead of unsaved work, which counts an open editor too. + guard !isEditingCell else { return .wait(.editing) } + guard !holdsUnsavedWork, !hasSidebarDataWork else { return .wait(.unsavedChanges) } + // A reload closes the grid's viewers and the value filter, and moves rows under a drag or a scroll. + guard !gridHoldsTransientUI, !gridIsTrackingInteraction else { return .wait(.interacting) } + guard !inspectorHoldsValueWindow else { return .wait(.valueWindowOpen) } + guard hasLoaded, load == .idle, !isCountPending, !isLoadingMore else { return .wait(.loading) } + guard !showsSafeModePrompt else { return .wait(.safeModePrompt) } + // Rows are kept across a reload by primary key, so without one the selection would be dropped. + guard !hasSelection || hasPrimaryKey else { return .wait(.unkeyedSelection) } + return .refresh + } +} diff --git a/apple/TablePro/Models/Query/TableAutoRefreshInterval.swift b/apple/TablePro/Models/Query/TableAutoRefreshInterval.swift new file mode 100644 index 0000000000..b41b60ff92 --- /dev/null +++ b/apple/TablePro/Models/Query/TableAutoRefreshInterval.swift @@ -0,0 +1,38 @@ +// +// TableAutoRefreshInterval.swift +// TablePro +// + +import Foundation + +enum TableAutoRefreshInterval: Int, CaseIterable, Identifiable, Sendable { + case off = 0 + case fiveSeconds = 5 + case tenSeconds = 10 + case thirtySeconds = 30 + case oneMinute = 60 + case fiveMinutes = 300 + + var id: Int { rawValue } + + var isOn: Bool { self != .off } + + var duration: Duration { .seconds(rawValue) } + + var title: String { + switch self { + case .off: String(localized: "Off") + case .fiveSeconds: String(localized: "Every 5 Seconds") + case .tenSeconds: String(localized: "Every 10 Seconds") + case .thirtySeconds: String(localized: "Every 30 Seconds") + case .oneMinute: String(localized: "Every Minute") + case .fiveMinutes: String(localized: "Every 5 Minutes") + } + } + + // The status bar's regular tier names the interval beside the glyph, so it has to stay short in every language. + var shortTitle: String { + guard isOn else { return String(localized: "Off") } + return duration.formatted(.units(allowed: [.minutes, .seconds], width: .narrow)) + } +} diff --git a/apple/TablePro/Models/Query/TableLoadTrigger.swift b/apple/TablePro/Models/Query/TableLoadTrigger.swift index 744f343fd2..13067926e3 100644 --- a/apple/TablePro/Models/Query/TableLoadTrigger.swift +++ b/apple/TablePro/Models/Query/TableLoadTrigger.swift @@ -8,6 +8,31 @@ import Foundation internal enum TableLoadTrigger { case userInitiated case restore + case autoRefresh + + // A timer's read at 5 s would write 720 rows an hour per tab. + var recordsHistory: Bool { + switch self { + case .userInitiated, .restore: true + case .autoRefresh: false + } + } + + // At 5 s a tick's trace would push every real load out of the diagnostics store within hours. + var tracesLoad: Bool { + switch self { + case .userInitiated, .restore: true + case .autoRefresh: false + } + } + + // A count slower than the interval would otherwise run on every tick and never land; only a manual Refresh recounts. + var countsRows: Bool { + switch self { + case .userInitiated, .restore: true + case .autoRefresh: false + } + } } // Keyed by tab: a window-wide trigger was consumed against whatever tab was selected when the connection came back. diff --git a/apple/TablePro/Models/ServerDashboard/ServerDashboardModels.swift b/apple/TablePro/Models/ServerDashboard/ServerDashboardModels.swift index d21d7ca284..9f5b9d63b6 100644 --- a/apple/TablePro/Models/ServerDashboard/ServerDashboardModels.swift +++ b/apple/TablePro/Models/ServerDashboard/ServerDashboardModels.swift @@ -28,7 +28,7 @@ enum DashboardRefreshInterval: Double, CaseIterable, Identifiable { var displayLabel: String { switch self { case .off: return String(localized: "Off") - default: return "\(Int(rawValue))s" + default: return Duration.seconds(rawValue).formatted(.units(allowed: [.seconds], width: .narrow)) } } } diff --git a/apple/TablePro/Models/Sidebar/SidebarObjectKind.swift b/apple/TablePro/Models/Sidebar/SidebarObjectKind.swift index 3ce26cf891..56b5962f37 100644 --- a/apple/TablePro/Models/Sidebar/SidebarObjectKind.swift +++ b/apple/TablePro/Models/Sidebar/SidebarObjectKind.swift @@ -6,16 +6,16 @@ struct DatabaseTreeObjectGroup: Hashable, Sendable { let kind: SidebarObjectKind } -/// Which model a kind's rows are drawn from. Every helper that used to ask `isRoutine` and pick -/// one of two buckets asks this instead, so a kind added later cannot silently read the wrong one. +// Which model a kind's rows are drawn from, so a kind added later cannot read the wrong bucket. enum SidebarObjectCategory: Sendable, Hashable { case table case routine case trigger case type + case server } -enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { +enum SidebarObjectKind: Sendable, Hashable { case table case view case materializedView @@ -25,6 +25,50 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { case function case trigger case type + case server(SidebarServerObjectKind) + + static let builtIn: [SidebarObjectKind] = [ + .table, .view, .materializedView, .foreignTable, .sequence, .procedure, .function, .trigger, .type, + ] + + static func ordered(declared: [SidebarServerObjectKind]) -> [SidebarObjectKind] { + builtIn + declared.map { SidebarObjectKind.server($0) } + } + + private static let serverPersistencePrefix = "server." + + // Built-in spellings are shipped in UserDefaults keys, the tree expansion JSON and node ids. + var persistenceName: String { + switch self { + case .table: return "table" + case .view: return "view" + case .materializedView: return "materializedView" + case .foreignTable: return "foreignTable" + case .sequence: return "sequence" + case .procedure: return "procedure" + case .function: return "function" + case .trigger: return "trigger" + case .type: return "type" + case .server(let kind): return Self.serverPersistencePrefix + kind.id + } + } + + // A declared kind decodes before any driver exists, so it carries only its id. + init?(persistenceName: String) { + if let builtIn = Self.builtIn.first(where: { $0.persistenceName == persistenceName }) { + self = builtIn + return + } + guard persistenceName.hasPrefix(Self.serverPersistencePrefix) else { return nil } + let id = String(persistenceName.dropFirst(Self.serverPersistencePrefix.count)) + guard !id.isEmpty else { return nil } + self = .server(SidebarServerObjectKind(id: id)) + } + + var serverKind: SidebarServerObjectKind? { + if case .server(let kind) = self { return kind } + return nil + } var displayName: String { switch self { @@ -37,6 +81,7 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { case .trigger: return String(localized: "Trigger") case .type: return String(localized: "Type") case .sequence: return String(localized: "Sequence") + case .server(let kind): return kind.displayName } } @@ -51,6 +96,7 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { case .trigger: return String(localized: "Triggers") case .type: return String(localized: "Types") case .sequence: return String(localized: "Sequences") + case .server(let kind): return kind.pluralDisplayName } } @@ -65,6 +111,7 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { case .trigger: return String(localized: "No triggers") case .type: return String(localized: "No types") case .sequence: return String(localized: "No sequences") + case .server: return String(localized: "No items") } } @@ -87,6 +134,7 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { case .trigger: return "bolt" case .type: return "cube" case .sequence: return "number" + case .server(let kind): return kind.iconName } } @@ -96,6 +144,7 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { case .procedure, .function: return .routine case .trigger: return .trigger case .type: return .type + case .server: return .server } } @@ -113,22 +162,13 @@ enum SidebarObjectKind: String, CaseIterable, Sendable, Hashable { self == .table } - /// Which kinds a container lists, in declaration order: the kinds holding objects, and the kinds in - /// `keeping`. Both sidebar layouts ask the same question, so toggling the layout never changes which - /// kinds are on screen. - /// - /// A kind is listed for what its driver returned, never for what the plugin declares. Gating on a - /// capability flag hid objects that came back, and listing every declared kind filled the sidebar - /// with Procedures, Triggers and Types sections that held nothing. - /// - /// `keeping` is for a section with something to say and no rows to say it with: a table kind whose - /// objects all sit in folders, which is where a drag takes one back out, and a kind whose fetch - /// failed, which shows the error where its rows would be. + // Listed for its rows or for `keeping` (filed into folders, or a failed fetch), never for being declared. static func visible( itemCounts: [SidebarObjectKind: Int], - keeping: Set = [] + keeping: Set = [], + declared: [SidebarServerObjectKind] = [] ) -> [SidebarObjectKind] { - allCases.filter { kind in + ordered(declared: declared).filter { kind in itemCounts[kind, default: 0] > 0 || keeping.contains(kind) } } diff --git a/apple/TablePro/Models/Sidebar/SidebarServerObjectKind.swift b/apple/TablePro/Models/Sidebar/SidebarServerObjectKind.swift new file mode 100644 index 0000000000..ebf242fbff --- /dev/null +++ b/apple/TablePro/Models/Sidebar/SidebarServerObjectKind.swift @@ -0,0 +1,83 @@ +// +// SidebarServerObjectKind.swift +// TablePro +// + +import Foundation +import TableProPluginKit + +// An object kind a driver declares, such as MySQL events. The app reads it as an opaque descriptor and +// compares it by id alone, so a kind decoded from storage with no driver equals the declared one. +struct SidebarServerObjectKind: Sendable, Hashable { + enum Scope: String, Sendable, Hashable, Codable { + case container + case database + } + + let id: String + let displayName: String + let pluralDisplayName: String + let iconName: String + let scope: Scope + + init(id: String, displayName: String, pluralDisplayName: String, iconName: String, scope: Scope) { + self.id = id + self.displayName = displayName + self.pluralDisplayName = pluralDisplayName + self.iconName = iconName + self.scope = scope + } + + init(id: String) { + self.init(id: id, displayName: id, pluralDisplayName: id, iconName: "shippingbox", scope: .container) + } + + init(_ kind: PluginServerObjectKind) { + let scope: Scope + switch kind.scope { + case .container: scope = .container + case .database: scope = .database + @unknown default: scope = .container + } + self.init( + id: kind.id, + displayName: kind.displayName, + pluralDisplayName: kind.pluralDisplayName, + iconName: kind.iconName, + scope: scope + ) + } + + static func == (lhs: SidebarServerObjectKind, rhs: SidebarServerObjectKind) -> Bool { + lhs.id == rhs.id + } + + func hash(into hasher: inout Hasher) { + hasher.combine(id) + } + + // Menu validation and every redraw ask, and a driver answers the same for its whole life. + @MainActor private static let probed = NSMapTable.weakToStrongObjects() + + @MainActor + static func declared(by driver: DatabaseDriver?) -> [SidebarServerObjectKind] { + guard let driver else { return [] } + if let known = probed.object(forKey: driver) { + return known.kinds + } + var seen = Set() + let kinds = driver.serverObjectKinds + .map(SidebarServerObjectKind.init) + .filter { !$0.id.isEmpty && seen.insert($0.id).inserted } + probed.setObject(DeclaredKinds(kinds), forKey: driver) + return kinds + } + + private final class DeclaredKinds { + let kinds: [SidebarServerObjectKind] + + init(_ kinds: [SidebarServerObjectKind]) { + self.kinds = kinds + } + } +} diff --git a/apple/TablePro/Models/UI/KeyboardShortcutModels.swift b/apple/TablePro/Models/UI/KeyboardShortcutModels.swift index 81f7d56b94..ee2a1a2244 100644 --- a/apple/TablePro/Models/UI/KeyboardShortcutModels.swift +++ b/apple/TablePro/Models/UI/KeyboardShortcutModels.swift @@ -68,6 +68,8 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable { case moveTabToConnection case showServerDashboard case showQueryInsights + case commitTransaction + case rollbackTransaction // Editor & Query case openFile @@ -197,7 +199,7 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable { var category: ShortcutCategory { switch self { case .manageConnections, .newConnection, .openDatabase, .switchConnection, .moveTabToConnection, - .showServerDashboard, .showQueryInsights: + .showServerDashboard, .showQueryInsights, .commitTransaction, .rollbackTransaction: return .connections case .openFile, .saveChanges, .saveAs, .copyPath, .copyRelativePath, .executeQuery, .executeAllStatements, .executeQueryWithoutLimit, .cancelQuery, .explainQuery, .formatQuery, @@ -307,6 +309,8 @@ enum ShortcutAction: String, Codable, CaseIterable, Identifiable { case .moveTabToConnection: return String(localized: "Move Tab to Connection") case .showServerDashboard: return String(localized: "Server Dashboard") case .showQueryInsights: return String(localized: "Query Insights") + case .commitTransaction: return String(localized: "Commit Transaction") + case .rollbackTransaction: return String(localized: "Roll Back Transaction") case .saveChanges: return String(localized: "Save Changes") case .saveAs: return String(localized: "Save As") case .copyPath: return String(localized: "Copy Path") @@ -660,7 +664,8 @@ struct KeyboardSettings: Codable, Equatable { // MARK: - Default Shortcuts - // An action absent here shows as unassigned until the user binds it. + // An action absent here shows as unassigned until the user binds it. Commit and Roll Back Transaction ship + // unbound: they are infrequent, and a stray Roll Back discards work. static let defaultShortcuts: [ShortcutAction: BoundKey] = [ // Connections .newConnection: .character("n", command: true), diff --git a/apple/TablePro/Models/UI/WindowSidebarState.swift b/apple/TablePro/Models/UI/WindowSidebarState.swift index 5dc1aed215..571c1f9bcd 100644 --- a/apple/TablePro/Models/UI/WindowSidebarState.swift +++ b/apple/TablePro/Models/UI/WindowSidebarState.swift @@ -111,8 +111,7 @@ internal final class WindowSidebarState: ObservableObject { isLoaded = true } - /// The kind rides as its raw string. Decoding it as `SidebarObjectKind` throws on a value this - /// build does not know, and one throw inside this blob discards every other expansion set. + // The kind rides as its persistence name, so a value this build does not know drops only that entry. private struct PersistedObjectGroup: Codable { let database: String let schema: String? @@ -176,7 +175,7 @@ internal final class WindowSidebarState: ObservableObject { from persisted: [PersistedObjectGroup] ) -> [DatabaseTreeObjectGroup: Bool] { persisted.reduce(into: [:]) { result, entry in - guard let kind = SidebarObjectKind(rawValue: entry.kind) else { return } + guard let kind = SidebarObjectKind(persistenceName: entry.kind) else { return } let group = DatabaseTreeObjectGroup(database: entry.database, schema: entry.schema, kind: kind) result[group] = entry.expanded } @@ -189,7 +188,7 @@ internal final class WindowSidebarState: ObservableObject { PersistedObjectGroup( database: group.database, schema: group.schema, - kind: group.kind.rawValue, + kind: group.kind.persistenceName, expanded: expanded ) } diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 59ea7fab79..6a3a11adff 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -1072,6 +1072,9 @@ } } } + }, + "“%@” is used more than once." : { + }, "“%@” isn't a port or a range of ports, such as 4000-4999." : { "extractionState" : "manual", @@ -17582,6 +17585,9 @@ } } } + }, + "A transaction is already open on this connection, or it cannot say whether one is. Commit or roll it back first." : { + }, "A transaction is already open. Commit or roll it back first." : { "extractionState" : "manual", @@ -17594,9 +17600,15 @@ } } } + }, + "A Transaction Is Open" : { + }, "A transaction is open on %@. Switching databases reconnects, and the server would roll it back. Commit or roll back the transaction, then switch again." : { + }, + "A transaction is open on this connection, and a failed statement would abort it. Commit or roll back in TablePro first." : { + }, "A transaction is open on this connection, and its uncommitted rows aren't in these counts." : { @@ -19732,6 +19744,9 @@ }, "Add column “%@”" : { + }, + "Add Column Pair" : { + }, "Add Column…" : { "localizations" : { @@ -29711,6 +29726,33 @@ } } } + }, + "Auto Refresh" : { + + }, + "Auto Refresh waits until the connection is back." : { + + }, + "Auto Refresh waits while a cell is being edited." : { + + }, + "Auto Refresh waits while a transaction is open." : { + + }, + "Auto Refresh waits while a value is open in its own window." : { + + }, + "Auto Refresh waits while rows are selected in a table without a primary key." : { + + }, + "Auto Refresh waits while rows load." : { + + }, + "Auto Refresh waits while the table has unsaved changes." : { + + }, + "Auto Refresh: %@" : { + }, "AUTO_INCREMENT columns whose next value has reached 75% of the largest value their type holds." : { @@ -31547,6 +31589,12 @@ }, "Before" : { + }, + "Before Replace" : { + + }, + "Before Restore" : { + }, "BEFORE, AFTER, or INSTEAD OF" : { "localizations" : { @@ -31587,6 +31635,9 @@ } } } + }, + "Begin, commit, or roll back a transaction on the connection's shared session. The transaction stays open across calls until committed or rolled back, and it is the same session the user's own tabs run on, so leave nothing open. Begin is refused while a transaction is already open, and every action is refused on a connection with Manual Commit on, where the user commits in TablePro. Needs tools:write." : { + }, "Begin, commit, or roll back a transaction on the connection's shared session. The transaction stays open across calls until committed or rolled back, and it is the same session the user's own tabs run on, so leave nothing open. Needs tools:write." : { "localizations" : { @@ -32519,6 +32570,9 @@ } } } + }, + "Browse All Versions…" : { + }, "Browse needs a TableName" : { "extractionState" : "manual", @@ -37998,6 +38052,9 @@ }, "Choose a character set first" : { + }, + "Choose a column and a referenced column in every row." : { + }, "Choose a connection" : { "localizations" : { @@ -44102,6 +44159,9 @@ } } } + }, + "Column → Referenced Column" : { + }, "Column comment" : { "extractionState" : "manual", @@ -45572,6 +45632,9 @@ }, "Commit the transaction" : { + }, + "Commit Transaction" : { + }, "Commit…" : { @@ -62077,6 +62140,10 @@ } } }, + "Default Version" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Default view:" : { "localizations" : { "fr" : { @@ -62168,6 +62235,10 @@ } } }, + "Definer" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Definition" : { "localizations" : { "fr" : { @@ -65538,6 +65609,10 @@ } } }, + "Disabled on Replica" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Discard" : { "localizations" : { "fr" : { @@ -72126,6 +72201,8 @@ } }, "Enabled" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -72432,6 +72509,10 @@ } } }, + "Ends" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Ends on %@" : { }, @@ -74693,6 +74774,8 @@ "generatesSymbol" : false }, "Event" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -74732,6 +74815,10 @@ } } }, + "Event Scheduler" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Event Streaming" : { "localizations" : { "fr" : { @@ -74813,6 +74900,8 @@ } }, "Events" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -74834,6 +74923,12 @@ }, "Every 5 Minutes" : { + }, + "Every 5 Seconds" : { + + }, + "Every 10 Seconds" : { + }, "Every 15 minutes" : { "localizations" : { @@ -74857,6 +74952,9 @@ } } } + }, + "Every 30 Seconds" : { + }, "Every call that reaches a connection asks first." : { "localizations" : { @@ -79314,6 +79412,10 @@ } } }, + "Extension" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Extension File" : { "localizations" : { "fr" : { @@ -95876,6 +95978,9 @@ } } } + }, + "Insert rows into a table using bound parameters, so values are never spliced into SQL text. Needs tools:write and goes through the connection's Safe Mode approval. With Manual Commit on, the rows stay pending until the user commits in TablePro." : { + }, "Insert rows the target is missing" : { "localizations" : { @@ -99410,6 +99515,9 @@ } } } + }, + "Keeps up to %1$d versions from the last %2$d days on this Mac." : { + }, "Keeps what rows looked like before each save, on this Mac only, for 7 days. It is encrypted, and never synced." : { "localizations" : { @@ -101137,6 +101245,10 @@ } } }, + "Last Executed" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Last Hour" : { "localizations" : { "fr" : { @@ -107755,6 +107867,15 @@ } } } + }, + "Manual Commit" : { + + }, + "Manual Commit is on for this connection, so this change is pending until the user commits it in TablePro." : { + + }, + "Manual Commit is on for this connection. Commit or roll back in TablePro." : { + }, "Many Databases, One App" : { "localizations" : { @@ -116206,7 +116327,8 @@ } }, "No" : { - + "extractionState" : "manual", + "generatesSymbol" : false }, "No %@" : { "localizations" : { @@ -126534,6 +126656,10 @@ }, "On a partitioned table the drop cannot run concurrently, and it removes the index from every partition." : { + }, + "On Completion" : { + "extractionState" : "manual", + "generatesSymbol" : false }, "On Delete" : { "localizations" : { @@ -127153,6 +127279,10 @@ } } }, + "One Time" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "One username and password, shared by any number of connections. Change it here and every connection using it signs in with the new one." : { "localizations" : { "fr" : { @@ -131488,6 +131618,8 @@ } }, "Owner" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -142576,6 +142708,9 @@ }, "Quit while work is still running?" : { + }, + "Quit with an Open Transaction?" : { + }, "Quit Without Saving" : { @@ -145459,6 +145594,10 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "Recurring" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Red" : { "localizations" : { "fr" : { @@ -145923,6 +146062,9 @@ } } } + }, + "Referenced Column" : { + }, "Referenced row not found" : { "localizations" : { @@ -146660,6 +146802,9 @@ }, "Relaunch while work is still running?" : { + }, + "Relaunch with an Open Transaction?" : { + }, "Release" : { "localizations" : { @@ -147062,6 +147207,10 @@ } } }, + "Relocatable" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Remote (Quack, experimental)" : { "extractionState" : "manual", "generatesSymbol" : false, @@ -147607,6 +147756,9 @@ } } } + }, + "Remove Column Pair" : { + }, "Remove Comment" : { "localizations" : { @@ -152764,6 +152916,9 @@ }, "Revert Changes" : { + }, + "Revert To" : { + }, "review" : { "extractionState" : "stale", @@ -153426,6 +153581,9 @@ }, "Roll Back the Transaction?" : { + }, + "Roll Back Transaction" : { + }, "Roll it back to continue." : { @@ -155841,6 +155999,9 @@ }, "Run one statement, or on SQL Server a whole script. Reads need tools:read; anything that writes needs tools:write and is subject to the connection's Safe Mode, which asks the user to approve it. Destructive statements, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE, go through confirm_destructive_operation instead. Statements that read or write files, or run server-side code, are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at its GO lines and result_sets lists every result set when there is more than one." : { + }, + "Run one statement, or on SQL Server a whole script. Reads need tools:read; anything that writes needs tools:write and is subject to the connection's Safe Mode, which asks the user to approve it. Destructive statements, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE, go through confirm_destructive_operation instead. Statements that read or write files, or run server-side code, are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at its GO lines and result_sets lists every result set when there is more than one. A statement that starts a transaction while one is open is refused. With Manual Commit on, a write stays pending until the user commits it in TablePro, and statements that begin, commit or roll back a transaction are refused. While a transaction is open on PostgreSQL or CockroachDB a statement runs inside a savepoint, so its failure leaves the transaction usable; on Redshift and DuckDB it is refused." : { + }, "Run one statement, or on SQL Server a whole script. Reads need tools:read; anything that writes needs tools:write and is subject to the connection's Safe Mode, which asks the user to approve it. DROP and TRUNCATE go through confirm_destructive_operation instead. Statements that read or write files, or run server-side code, are refused. Send one statement per call, except on SQL Server, where a script runs batch by batch at its GO lines and result_sets lists every result set when there is more than one." : { "localizations" : { @@ -156277,6 +156438,9 @@ }, "Run what execute_query refuses as destructive, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE: one statement, or on SQL Server a whole script. Anything else is refused. The user approves it before it runs: through your elicitation prompt when your client supports elicitation, otherwise through TablePro's own confirmation dialog on the user's Mac. Needs tools:write and a connection the user left writable for external clients." : { + }, + "Run what execute_query refuses as destructive, such as DROP, TRUNCATE, ALTER...DROP or a DELETE with no WHERE: one statement, or on SQL Server a whole script. Anything else is refused. The user approves it before it runs: through your elicitation prompt when your client supports elicitation, otherwise through TablePro's own confirmation dialog on the user's Mac. Needs tools:write and a connection the user left writable for external clients. The transaction rules of execute_query apply, Manual Commit's among them." : { + }, "Run Without Limit" : { "localizations" : { @@ -158643,6 +158807,10 @@ "extractionState" : "manual", "generatesSymbol" : false }, + "Schedule" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "Schema" : { "extractionState" : "manual", "generatesSymbol" : false, @@ -174259,6 +174427,10 @@ } } }, + "Starts" : { + "extractionState" : "manual", + "generatesSymbol" : false + }, "starts with" : { "localizations" : { "fr" : { @@ -175121,6 +175293,8 @@ } }, "Status" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -186406,6 +186580,9 @@ } } } + }, + "The change did not run." : { + }, "The change to this table's structure will be lost." : { @@ -190610,6 +190787,9 @@ }, "The open transaction failed. Roll it back yourself." : { + }, + "The open transaction on this connection ended while this statement ran." : { + }, "The operation failed." : { "localizations" : { @@ -197233,6 +197413,12 @@ } } } + }, + "This change runs on its own connection and can wait until you commit or roll back the open transaction." : { + + }, + "This change sets session options around its statements, so it can't wait for Commit. Turn off Manual Commit in the Database menu to run it." : { + }, "This change was not authorized" : { "localizations" : { @@ -197430,6 +197616,9 @@ } } } + }, + "This connection can't hold this change until you commit, so it did not run. Turn off Manual Commit in the Database menu to run it." : { + }, "This connection can't open the separate session live messages need." : { @@ -198194,6 +198383,9 @@ } } } + }, + "This database can't report an open transaction, so it commits each change." : { + }, "This database cannot be renamed" : { "extractionState" : "manual", @@ -198395,6 +198587,9 @@ }, "This database cannot store a table comment." : { + }, + "This database commits schema and maintenance statements on its own, so changes can't wait for Commit." : { + }, "This database did not return a query plan for the statement." : { "comment" : "Text displayed in a description when the database did not return a query plan for the statement.", @@ -200005,6 +200200,12 @@ } } } + }, + "This import runs each statement on its own, so nothing can wait for Commit. Turn on Wrap in transaction or turn off Manual Commit." : { + + }, + "This import turns off foreign key checks, which can't wait for Commit. Turn off Disable foreign key checks or Manual Commit to run it." : { + }, "This index needs a name." : { "localizations" : { @@ -201853,6 +202054,9 @@ } } } + }, + "This run mixes changes with a statement that can't run in a transaction. Run that statement on its own, or turn off Manual Commit." : { + }, "This save also edits key columns, and the counts don't include those edits." : { @@ -202451,6 +202655,9 @@ }, "This Tab" : { + }, + "This tab is closed. Reopen it to see its history." : { + }, "This tab is on %@. Switch the connection to that database to run it." : { "localizations" : { @@ -203193,6 +203400,9 @@ } } } + }, + "This will permanently delete all query history entries, the saved changes kept for restoring, and the earlier versions of your query tabs. This action cannot be undone." : { + }, "This will reset all settings across every section to their default values." : { "localizations" : { @@ -207703,6 +207913,12 @@ } } } + }, + "Uncommitted changes on “%@” are rolled back when TablePro quits." : { + + }, + "Uncommitted changes on %@ are rolled back when TablePro quits." : { + }, "Under MCP Servers click \"+ Add Custom Server\", select the Local tab, paste the JSON below, then click Add Server" : { "localizations" : { @@ -214043,6 +214259,8 @@ } }, "Version" : { + "extractionState" : "manual", + "generatesSymbol" : false, "localizations" : { "fr" : { "stringUnit" : { @@ -218579,7 +218797,8 @@ } }, "Yes" : { - + "extractionState" : "manual", + "generatesSymbol" : false }, "Yesterday" : { "localizations" : { @@ -219089,6 +219308,9 @@ } } } + }, + "Your first change opens a transaction, and nothing is saved until you commit. Structure changes, Users & Roles, Copy To, Generate Rows and Shortcuts actions still save at once." : { + }, "Your license has expired" : { "localizations" : { diff --git a/apple/TablePro/ViewModels/ERDiagramViewModel+Editing.swift b/apple/TablePro/ViewModels/ERDiagramViewModel+Editing.swift index 3332224271..3989cce6c4 100644 --- a/apple/TablePro/ViewModels/ERDiagramViewModel+Editing.swift +++ b/apple/TablePro/ViewModels/ERDiagramViewModel+Editing.swift @@ -22,13 +22,160 @@ struct ERDiagramColumnHit: Equatable { } struct ERDiagramForeignKeyDraft: Equatable { + struct ColumnPair: Identifiable, Equatable { + let id: UUID + var column: String + var referencedColumn: String + + init(id: UUID = UUID(), column: String, referencedColumn: String) { + self.id = id + self.column = column + self.referencedColumn = referencedColumn + } + } + + enum Issue: Equatable { + case noPairs + case incompletePair + case repeatedColumn(String) + case repeatedReferencedColumn(String) + + var message: String { + switch self { + case .noPairs, .incompletePair: + String(localized: "Choose a column and a referenced column in every row.") + case .repeatedColumn(let name), .repeatedReferencedColumn(let name): + String(format: String(localized: "“%@” is used more than once."), name) + } + } + } + let table: String - var column: String + var pairs: [ColumnPair] var referencedTable: String - var referencedColumn: String var name: String var onDelete: EditableForeignKeyDefinition.ReferentialAction = .noAction var onUpdate: EditableForeignKeyDefinition.ReferentialAction = .noAction + + var issue: Issue? { + guard !pairs.isEmpty else { return .noPairs } + guard !pairs.contains(where: { $0.column.isEmpty || $0.referencedColumn.isEmpty }) else { return .incompletePair } + if let repeated = Self.firstRepeated(pairs.map(\.column)) { return .repeatedColumn(repeated) } + if let repeated = Self.firstRepeated(pairs.map(\.referencedColumn)) { return .repeatedReferencedColumn(repeated) } + return nil + } + + // Pair order is key order on both sides, which is what the database matches the referenced key by. + var foreignKey: EditableForeignKeyDefinition { + var foreignKey = EditableForeignKeyDefinition.placeholder() + foreignKey.name = name.trimmingCharacters(in: .whitespacesAndNewlines) + foreignKey.columns = pairs.map(\.column) + foreignKey.referencedTable = referencedTable + foreignKey.referencedColumns = pairs.map(\.referencedColumn) + foreignKey.onDelete = onDelete + foreignKey.onUpdate = onUpdate + return foreignKey + } + + // A drop on a member of a composite key offers the whole key, with the dragged pair at that member's position. + static func starting( + table: String, + column: String, + referencedTable: String, + keyColumns: [String], + target: String?, + sourceColumns: [String], + name: String + ) -> ERDiagramForeignKeyDraft { + let pairs: [ColumnPair] + if let target, !keyColumns.contains(target) { + pairs = [ColumnPair(column: column, referencedColumn: target)] + } else if keyColumns.isEmpty { + pairs = [ColumnPair(column: column, referencedColumn: target ?? "")] + } else { + let position = target.flatMap { keyColumns.firstIndex(of: $0) } + pairs = guessedPairs(for: keyColumns, keeping: column, at: position, from: sourceColumns) + } + return ERDiagramForeignKeyDraft(table: table, pairs: pairs, referencedTable: referencedTable, name: name) + } + + func settingColumn(_ column: String, of id: UUID) -> ERDiagramForeignKeyDraft { + var draft = self + guard let index = draft.pairs.firstIndex(where: { $0.id == id }) else { return self } + draft.pairs[index].column = column + return draft + } + + func settingReferencedColumn(_ column: String, of id: UUID) -> ERDiagramForeignKeyDraft { + var draft = self + guard let index = draft.pairs.firstIndex(where: { $0.id == id }) else { return self } + draft.pairs[index].referencedColumn = column + return draft + } + + func addingPair() -> ERDiagramForeignKeyDraft { + var draft = self + draft.pairs.append(ColumnPair(column: "", referencedColumn: "")) + return draft + } + + // The last pair stays: a key needs at least one. + func removingPairs(_ ids: Set) -> ERDiagramForeignKeyDraft { + let kept = pairs.filter { !ids.contains($0.id) } + guard !kept.isEmpty else { return self } + var draft = self + draft.pairs = kept + return draft + } + + func movingPairs(fromOffsets source: IndexSet, toOffset destination: Int) -> ERDiagramForeignKeyDraft { + let moving = source.filter { pairs.indices.contains($0) }.map { pairs[$0] } + var remaining = pairs.enumerated().filter { !source.contains($0.offset) }.map(\.element) + let insertion = destination - source.filter { $0 < destination }.count + remaining.insert(contentsOf: moving, at: min(max(insertion, 0), remaining.count)) + var draft = self + draft.pairs = remaining + return draft + } + + // The referenced side follows the referenced table's key, so both change in one write. + func referencing(_ table: String, keyColumns: [String], sourceColumns: [String]) -> ERDiagramForeignKeyDraft { + var draft = self + draft.referencedTable = table + let first = pairs.first?.column ?? "" + draft.pairs = keyColumns.isEmpty + ? [ColumnPair(column: first, referencedColumn: "")] + : Self.guessedPairs(for: keyColumns, keeping: first, at: nil, from: sourceColumns) + return draft + } + + // Unplaced, the kept column takes a key column no other column is named after, else the last: a table's own id shares the referenced id's name. + private static func guessedPairs( + for keyColumns: [String], + keeping column: String, + at position: Int?, + from sourceColumns: [String] + ) -> [ColumnPair] { + let others = sourceColumns.filter { $0 != column } + func namesake(of key: String, excluding used: Set) -> String? { + others.first { !used.contains($0) && $0.caseInsensitiveCompare(key) == .orderedSame } + } + let slot = position + ?? keyColumns.firstIndex { namesake(of: $0, excluding: []) == nil } + ?? max(keyColumns.count - 1, 0) + var used: Set = [] + return keyColumns.enumerated().map { offset, key in + guard offset != slot else { return ColumnPair(column: column, referencedColumn: key) } + let match = namesake(of: key, excluding: used) ?? "" + if !match.isEmpty { used.insert(match) } + return ColumnPair(column: match, referencedColumn: key) + } + } + + private static func firstRepeated(_ names: [String]) -> String? { + var seen = Set() + return names.first { !seen.insert($0).inserted } + } } enum ERDiagramEditSheet: Identifiable, Equatable { @@ -38,7 +185,7 @@ enum ERDiagramEditSheet: Identifiable, Equatable { var id: String { switch self { - case .addForeignKey(let draft): "fk:\(draft.table).\(draft.column)" + case .addForeignKey(let draft): "fk:\(draft.table).\(draft.pairs.first?.column ?? "")" case .addColumn(let table): "add:\(table)" case .renameColumn(let table, let column): "rename:\(table).\(column)" } @@ -306,12 +453,13 @@ extension ERDiagramViewModel { let referencedTable = target?.tableName ?? tables.first { $0 != table } ?? table - let referencedColumn = target?.columnName ?? defaultReferencedColumn(in: referencedTable) - editSheet = .addForeignKey(ERDiagramForeignKeyDraft( + editSheet = .addForeignKey(.starting( table: table, column: column, referencedTable: referencedTable, - referencedColumn: referencedColumn, + keyColumns: referencedKeyColumns(in: referencedTable), + target: target?.columnName, + sourceColumns: columnNames(of: table), name: defaultForeignKeyName(table: table, column: column) )) } @@ -327,12 +475,14 @@ extension ERDiagramViewModel { return node.columns.filter { $0.stagedEdit != .dropped }.map(\.name) } - func defaultReferencedColumn(in table: String) -> String { + // In table order, which is the key's own order unless it was declared in another; the reader can reorder the pairs. + func referencedKeyColumns(in table: String) -> [String] { guard let id = stagedFullGraph.nodeIndex[table], let node = stagedFullGraph.nodes.first(where: { $0.id == id }) - else { return "" } - let keys = node.columns.filter { $0.isPrimaryKey && $0.stagedEdit != .dropped } - return (keys.count == 1 ? keys.first : node.columns.first { $0.stagedEdit != .dropped })?.name ?? "" + else { return [] } + let kept = node.columns.filter { $0.stagedEdit != .dropped } + let keys = kept.filter(\.isPrimaryKey).map(\.name) + return keys.isEmpty ? kept.prefix(1).map(\.name) : keys } // MySQL wants a foreign key's name unique in the whole database, so it is checked against every key drawn. @@ -360,13 +510,7 @@ extension ERDiagramViewModel { } func stageForeignKey(_ draft: ERDiagramForeignKeyDraft) { - var foreignKey = EditableForeignKeyDefinition.placeholder() - foreignKey.name = draft.name.trimmingCharacters(in: .whitespacesAndNewlines) - foreignKey.columns = [draft.column] - foreignKey.referencedTable = draft.referencedTable - foreignKey.referencedColumns = [draft.referencedColumn] - foreignKey.onDelete = draft.onDelete - foreignKey.onUpdate = draft.onUpdate + let foreignKey = draft.foreignKey stage { try await $0.addForeignKey(foreignKey, to: draft.table) } } diff --git a/apple/TablePro/ViewModels/SidebarViewModel.swift b/apple/TablePro/ViewModels/SidebarViewModel.swift index 08ecd41429..5515d5ea79 100644 --- a/apple/TablePro/ViewModels/SidebarViewModel.swift +++ b/apple/TablePro/ViewModels/SidebarViewModel.swift @@ -293,9 +293,14 @@ final class SidebarViewModel: ObservableObject { private static func loadInitialExpansion(connectionId: UUID, defaults: UserDefaults) -> ExpansionState { var values: [SidebarObjectKind: Bool] = [:] - for kind in SidebarObjectKind.allCases { + for kind in SidebarObjectKind.builtIn { values[kind] = loadKindExpansion(connectionId: connectionId, kind: kind, defaults: defaults) } + let serverKey = SidebarPersistenceKey.serverKindsExpanded(connectionId: connectionId) + for (id, expanded) in defaults.dictionary(forKey: serverKey) ?? [:] { + guard let expanded = expanded as? Bool, !id.isEmpty else { continue } + values[.server(SidebarServerObjectKind(id: id))] = expanded + } return ExpansionState(values: values) } @@ -338,12 +343,20 @@ final class SidebarViewModel: ObservableObject { } private func persistExpansion(oldValue: ExpansionState) { - for kind in SidebarObjectKind.allCases where oldValue[kind] != expanded[kind] { + for kind in SidebarObjectKind.builtIn where oldValue[kind] != expanded[kind] { defaults.set( expanded[kind], forKey: SidebarPersistenceKey.expanded(connectionId: connectionId, kind: kind) ) } + let serverKinds = Set(oldValue.values.keys).union(expanded.values.keys).compactMap(\.serverKind) + guard serverKinds.contains(where: { oldValue[.server($0)] != expanded[.server($0)] }) else { return } + var stored: [String: Bool] = [:] + for (kind, isExpanded) in expanded.values { + guard let serverKind = kind.serverKind else { continue } + stored[serverKind.id] = isExpanded + } + defaults.set(stored, forKey: SidebarPersistenceKey.serverKindsExpanded(connectionId: connectionId)) } // MARK: - Batch Operations @@ -472,6 +485,7 @@ final class SidebarViewModel: ObservableObject { private var cachedFilteredTriggersFingerprint: (count: Int, generation: Int, query: String)? private var cachedFilteredUserTypes: [UserDefinedTypeInfo] = [] private var cachedFilteredUserTypesFingerprint: (count: Int, generation: Int, query: String)? + private var cachedFilteredServerObjects: [String: (count: Int, generation: Int, query: String, objects: [ServerObjectInfo])] = [:] private var schemaGeneration: Int { SchemaService.shared.generationToken(for: connectionId) @@ -494,7 +508,7 @@ final class SidebarViewModel: ObservableObject { if cachedFilteredByKindFingerprint?.count != fingerprint.count || cachedFilteredByKindFingerprint?.generation != fingerprint.generation || cachedFilteredByKindFingerprint?.query != fingerprint.query { - for tableKind in SidebarObjectKind.allCases where tableKind.category == .table { + for tableKind in SidebarObjectKind.builtIn where tableKind.category == .table { cachedFilteredByKind[tableKind] = applyQuery(query, to: self.tables(of: tableKind, from: tables)) } cachedFilteredByKindFingerprint = fingerprint @@ -550,6 +564,18 @@ final class SidebarViewModel: ObservableObject { return cachedFilteredUserTypes } + func filteredServerObjects(kindId: String, from objects: [ServerObjectInfo]) -> [ServerObjectInfo] { + let query = filterQuery + let generation = schemaGeneration + if let cached = cachedFilteredServerObjects[kindId], + cached.count == objects.count, cached.generation == generation, cached.query == query { + return cached.objects + } + let filtered = DatabaseTreeFilter.filteredServerObjects(objects, searchText: query, database: browsedDatabase) + cachedFilteredServerObjects[kindId] = (objects.count, generation, query, filtered) + return filtered + } + func effectiveExpanded(kind: SidebarObjectKind, hasMatches: Bool) -> Bool { if !filterQuery.isEmpty && hasMatches { return true } return expanded[kind] @@ -660,7 +686,7 @@ final class SidebarViewModel: ObservableObject { private func rebuildKindBuckets(from tables: [TableInfo]) { var buckets: [SidebarObjectKind: [TableInfo]] = [:] - for kind in SidebarObjectKind.allCases { + for kind in SidebarObjectKind.builtIn { buckets[kind] = [] } for table in tables { @@ -675,6 +701,7 @@ final class SidebarViewModel: ObservableObject { cachedFilteredByKindFingerprint = nil cachedFilteredRoutines = [:] cachedFilteredRoutinesFingerprint = nil + cachedFilteredServerObjects = [:] } /// Clearing the field, or typing the first character into an empty one, changes what the list diff --git a/apple/TablePro/ViewModels/VersionHistoryViewModel.swift b/apple/TablePro/ViewModels/VersionHistoryViewModel.swift index 6d275ca2b5..d213ba80d1 100644 --- a/apple/TablePro/ViewModels/VersionHistoryViewModel.swift +++ b/apple/TablePro/ViewModels/VersionHistoryViewModel.swift @@ -117,7 +117,8 @@ internal final class VersionHistoryViewModel: ObservableObject { } catch { guard !Task.isCancelled else { return } Self.logger.warning("Version history failed to load: \(error.publicLogShape, privacy: .public)") - let subjectIsGone = (error as? VersionHistoryError) == .subjectNotFound + let failure = error as? VersionHistoryError + let subjectIsGone = failure == .subjectNotFound || failure == .tabClosed guard page.entries.isEmpty || subjectIsGone else { return } page = .empty selection = nil diff --git a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator+SafeMode.swift b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator+SafeMode.swift index 551b1e9c6f..d88f931b4e 100644 --- a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator+SafeMode.swift +++ b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator+SafeMode.swift @@ -16,8 +16,7 @@ extension ConnectionFormCoordinator { ) } - /// The level the connection will run at, which is what the form shows. Picking the level - /// already shown keeps the user's saved choice, which comes back once the floor lifts. + // Picking the level already shown keeps the saved choice, which comes back once the floor lifts. var effectiveSafeModeLevel: SafeModeLevel { get { safeModeFloor?.raising(customization.safeModeLevel) ?? customization.safeModeLevel } set { @@ -25,4 +24,15 @@ extension ConnectionFormCoordinator { customization.safeModeLevel = newValue } } + + var isManualCommitAvailable: Bool { ManualCommit.isAvailable(for: network.type) } + + // Off where the engine can't hold, since a stored Manual Commit runs as automatic there. + var effectiveManualCommit: Bool { + get { isManualCommitAvailable && customization.commitMode == .manual } + set { + guard isManualCommitAvailable else { return } + customization.commitMode = newValue ? .manual : .automatic + } + } } diff --git a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift index 963a6e019b..a8e0f3ae9b 100644 --- a/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift +++ b/apple/TablePro/Views/ConnectionForm/ConnectionFormCoordinator.swift @@ -376,6 +376,7 @@ final class ConnectionFormCoordinator: ObservableObject { queryTimeoutSeconds: customization.queryTimeoutSeconds, binaryInlineLimitKilobytes: customization.binaryInlineLimitKilobytes, completionIdentifierCase: customization.completionIdentifierCase, + commitMode: customization.commitMode, redisDatabase: advanced.additionalFieldValues[RedisDatabaseIndex.fieldName].map { RedisDatabaseIndex.parse($0) ?? 0 }, diff --git a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift index c188a6dffb..db0f8cd57e 100644 --- a/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift +++ b/apple/TablePro/Views/ConnectionForm/Panes/OptionsPaneView.swift @@ -355,6 +355,8 @@ struct OptionsPaneView: View { private var safetySection: some View { Section { safeModeRow + Toggle(String(localized: "Manual Commit"), isOn: $coordinator.effectiveManualCommit) + .disabled(!coordinator.isManualCommitAvailable) if aiIsEnabled { Picker(String(localized: "AI Policy"), selection: $coordinator.advanced.aiPolicy) { Text(String(localized: "Use Default")) @@ -398,6 +400,12 @@ struct OptionsPaneView: View { if let floor = coordinator.safeModeFloor { Text(floor.explanation) } + if coordinator.isManualCommitAvailable { + // swiftlint:disable:next line_length + Text(String(localized: "Your first change opens a transaction, and nothing is saved until you commit. Structure changes, Users & Roles, Copy To, Generate Rows and Shortcuts actions still save at once.")) + } else { + Text(ManualCommit.unavailableExplanation) + } if aiIsEnabled { // swiftlint:disable:next line_length Text(String(localized: "AI Policy controls in-app AI agents. External Clients controls Raycast, Cursor, Claude Desktop, other MCP clients, AppleScript, and Shortcuts. Effective scope is the minimum of the requesting token's scope and the External Clients level.")) diff --git a/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift b/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift index 21fcb4ee5f..5e5e16a5cb 100644 --- a/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift +++ b/apple/TablePro/Views/ConnectionForm/Support/ConnectionFormEdits.swift @@ -35,6 +35,7 @@ struct ConnectionFormEdits: Equatable { var queryTimeoutSeconds: Int? var binaryInlineLimitKilobytes: Int? var completionIdentifierCase: SQLIdentifierCase = .default + var commitMode: ConnectionCommitMode = .automatic var redisDatabase: Int? var startupCommands: String? var localOnly: Bool @@ -51,6 +52,7 @@ struct ConnectionFormEdits: Equatable { DatabaseConnection.queryTimeoutSecondsKey, DatabaseConnection.binaryInlineLimitKilobytesKey, DatabaseConnection.completionIdentifierCaseKey, + DatabaseConnection.commitModeKey, DatabaseConnection.maskingRulesKey, DatabaseConnection.sshForwardUnixSocketPathKey, MySQLLocalSocket.fieldKey @@ -89,6 +91,7 @@ struct ConnectionFormEdits: Equatable { result.queryTimeoutSeconds = queryTimeoutSeconds result.binaryInlineLimitKilobytes = binaryInlineLimitKilobytes result.completionIdentifierCase = completionIdentifierCase + result.commitMode = commitMode result.maskingRules = maskingRules return result } diff --git a/apple/TablePro/Views/ConnectionForm/ViewModels/CustomizationPaneViewModel.swift b/apple/TablePro/Views/ConnectionForm/ViewModels/CustomizationPaneViewModel.swift index c255b0a0c3..548285cfac 100644 --- a/apple/TablePro/Views/ConnectionForm/ViewModels/CustomizationPaneViewModel.swift +++ b/apple/TablePro/Views/ConnectionForm/ViewModels/CustomizationPaneViewModel.swift @@ -19,6 +19,7 @@ final class CustomizationPaneViewModel: ObservableObject { @Published var queryTimeoutSeconds: Int? @Published var binaryInlineLimitKilobytes: Int? @Published var completionIdentifierCase: SQLIdentifierCase = .default + @Published var commitMode: ConnectionCommitMode = .automatic @Published var coordinator: WeakCoordinatorRef? @@ -52,5 +53,6 @@ final class CustomizationPaneViewModel: ObservableObject { queryTimeoutSeconds = connection.queryTimeoutSeconds binaryInlineLimitKilobytes = connection.binaryInlineLimitKilobytes completionIdentifierCase = connection.completionIdentifierCase + commitMode = connection.commitMode } } diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift b/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift index ec17de9e21..8884781785 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController+Changes.swift @@ -259,11 +259,11 @@ extension DataGridViewController { } else { table.scroll(NSPoint(x: x, y: -headerInset)) } - guard !placement.selectedRows.isEmpty else { return } + guard placement.selection != .empty else { return } // Selecting publishes the model, which a SwiftUI update or layout pass refuses, so it runs on the next turn. DispatchQueue.main.async { [weak self] in guard let self, self.model.mountedView === self else { return } - self.model.selectRows(placement.selectedRows) + self.model.select(placement.selection) guard placement.revealsSelection, let row = self.model.selection.affectedRows.first else { return } self.reveal(row: row, displayColumn: nil) } diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController+Selection.swift b/apple/TablePro/Views/DataGrid/DataGridViewController+Selection.swift index f3fb692a25..b39acdb327 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController+Selection.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController+Selection.swift @@ -337,6 +337,8 @@ extension DataGridViewController { @discardableResult private func trackDrag(_ drag: GridSelectionDrag, from start: GridCoord) -> Bool { guard let window = view.window else { return false } + isTrackingSelectionDrag = true + defer { isTrackingSelectionDrag = false } var moved = false while let event = window.nextEvent(matching: [.leftMouseDragged, .leftMouseUp]) { if event.type == .leftMouseUp { break } diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController+TableDelegate.swift b/apple/TablePro/Views/DataGrid/DataGridViewController+TableDelegate.swift index 8ad4588ea3..1fd673b3e2 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController+TableDelegate.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController+TableDelegate.swift @@ -154,8 +154,9 @@ extension DataGridViewController: NSTableViewDataSource, NSTableViewDelegate { extension DataGridViewController: NSMenuDelegate { // Built from clickedRow and clickedColumn, so AppKit draws the clicked-row ring and the selection does not move. func menuNeedsUpdate(_ menu: NSMenu) { - menu.removeAllItems() + // The header menu comes built, and has this delegate only so its opening is seen. guard let pane = panes.first(where: { $0.tableView.menu === menu }) else { return } + menu.removeAllItems() let table = pane.tableView let row = table.clickedRow let built: NSMenu? @@ -178,6 +179,16 @@ extension DataGridViewController: NSMenuDelegate { func headerMenu(forTableColumn tableColumn: NSTableColumn, in kind: DataGridPane.Kind) -> NSMenu? { let pane = container.pane(kind) guard let id = pane.columnID(of: tableColumn), let column = model.columns.column(id: id) else { return nil } - return headerMenu(for: column) + let menu = headerMenu(for: column) + menu?.delegate = self + return menu + } + + func menuWillOpen(_ menu: NSMenu) { + isShowingContextMenu = true + } + + func menuDidClose(_ menu: NSMenu) { + isShowingContextMenu = false } } diff --git a/apple/TablePro/Views/DataGrid/DataGridViewController.swift b/apple/TablePro/Views/DataGrid/DataGridViewController.swift index a8d6a114fb..88634ef3be 100644 --- a/apple/TablePro/Views/DataGrid/DataGridViewController.swift +++ b/apple/TablePro/Views/DataGrid/DataGridViewController.swift @@ -39,6 +39,9 @@ final class DataGridViewController: NSViewController, DataGridMountedView { var isFittingHeightScheduled = false var pendingPageReveal: NSRect? var isRevealingInGrid = false + var isLiveScrolling = false + var isTrackingSelectionDrag = false + var isShowingContextMenu = false private var observers: [NSObjectProtocol] = [] private var cancellables: Set = [] private var themeCancellable: AnyCancellable? @@ -170,6 +173,16 @@ final class DataGridViewController: NSViewController, DataGridMountedView { var isEditing: Bool { isEditingCell } + // An open context menu acts on the row ids it was built from, which a reload hands to other rows. + var holdsTransientUI: Bool { + let state = editingState + return isShowingContextMenu || state.textPopover != nil || state.typedPopover != nil + || state.poppedOutEditor != nil || state.foreignKeyPreview?.isShown == true + || menuState.valueFilterPopover != nil + } + + var isTrackingInteraction: Bool { isLiveScrolling || isTrackingSelectionDrag } + @discardableResult func beginEditing(row: Int, displayColumn: Int) -> Bool { beginEditingCell(row: row, displayColumn: displayColumn) @@ -225,10 +238,16 @@ final class DataGridViewController: NSViewController, DataGridMountedView { }) let scrollView = main.scrollView observers.append(center.addObserver(forName: NSScrollView.willStartLiveScrollNotification, object: scrollView, queue: .main) { [weak self] _ in - MainActor.assumeIsolated { self?.pausePrewarm() } + MainActor.assumeIsolated { + self?.isLiveScrolling = true + self?.pausePrewarm() + } }) observers.append(center.addObserver(forName: NSScrollView.didEndLiveScrollNotification, object: scrollView, queue: .main) { [weak self] _ in - MainActor.assumeIsolated { self?.schedulePrewarmResume() } + MainActor.assumeIsolated { + self?.isLiveScrolling = false + self?.schedulePrewarmResume() + } }) } diff --git a/apple/TablePro/Views/ERDiagram/ERDiagramEditSheets.swift b/apple/TablePro/Views/ERDiagram/ERDiagramEditSheets.swift index 45a9fdc6c5..206e1e56b3 100644 --- a/apple/TablePro/Views/ERDiagram/ERDiagramEditSheets.swift +++ b/apple/TablePro/Views/ERDiagram/ERDiagramEditSheets.swift @@ -5,21 +5,12 @@ import SwiftUI -extension ERDiagramForeignKeyDraft { - // The referenced column follows the referenced table, so both change in one write. - func referencing(_ table: String, column: String) -> ERDiagramForeignKeyDraft { - var draft = self - draft.referencedTable = table - draft.referencedColumn = column - return draft - } -} - struct ERDiagramForeignKeySheet: View { @State private var draft: ERDiagramForeignKeyDraft + @State private var selection: Set = [] private let tables: [String] private let columns: (String) -> [String] - private let defaultReferencedColumn: (String) -> String + private let referencedKeyColumns: (String) -> [String] private let isNameTaken: (String) -> Bool private let dialect: ForeignKeyDialect private let onAdd: (ERDiagramForeignKeyDraft) -> Void @@ -29,7 +20,7 @@ struct ERDiagramForeignKeySheet: View { draft: ERDiagramForeignKeyDraft, tables: [String], columns: @escaping (String) -> [String], - defaultReferencedColumn: @escaping (String) -> String, + referencedKeyColumns: @escaping (String) -> [String], isNameTaken: @escaping (String) -> Bool, dialect: ForeignKeyDialect, onAdd: @escaping (ERDiagramForeignKeyDraft) -> Void, @@ -38,7 +29,7 @@ struct ERDiagramForeignKeySheet: View { _draft = State(initialValue: draft) self.tables = tables self.columns = columns - self.defaultReferencedColumn = defaultReferencedColumn + self.referencedKeyColumns = referencedKeyColumns self.isNameTaken = isNameTaken self.dialect = dialect self.onAdd = onAdd @@ -54,14 +45,18 @@ struct ERDiagramForeignKeySheet: View { } private var canAdd: Bool { - !draft.column.isEmpty && !draft.referencedTable.isEmpty && !draft.referencedColumn.isEmpty - && !trimmedName.isEmpty && !nameIsTaken + draft.issue == nil && !draft.referencedTable.isEmpty && !trimmedName.isEmpty && !nameIsTaken } private var referencedTable: Binding { Binding( get: { draft.referencedTable }, - set: { draft = draft.referencing($0, column: defaultReferencedColumn($0)) } + set: { table in + draft = draft.referencing( + table, keyColumns: referencedKeyColumns(table), sourceColumns: columns(draft.table) + ) + selection = [] + } ) } @@ -74,16 +69,20 @@ struct ERDiagramForeignKeySheet: View { Form { Section { LabeledContent(String(localized: "Table"), value: draft.table) - Picker(String(localized: "Column"), selection: $draft.column) { - ForEach(columns(draft.table), id: \.self) { Text($0).tag($0) } - } } Section(String(localized: "References")) { Picker(String(localized: "Table"), selection: referencedTable) { ForEach(tables, id: \.self) { Text($0).tag($0) } } - Picker(String(localized: "Column"), selection: $draft.referencedColumn) { - ForEach(columns(draft.referencedTable), id: \.self) { Text($0).tag($0) } + } + Section { + pairList + } header: { + Text(String(localized: "Column → Referenced Column")) + } footer: { + if let issue = draft.issue { + Text(issue.message) + .foregroundStyle(.secondary) } } Section { @@ -121,10 +120,137 @@ struct ERDiagramForeignKeySheet: View { } .padding() } - .frame(width: 440) + .frame(width: 480) .accessibilityElement(children: .contain) .accessibilityIdentifier("er-diagram-foreign-key-sheet") } + + // MARK: - Column pairs + + private var pairList: some View { + List(selection: $selection) { + ForEach(draft.pairs) { pair in + ERDiagramColumnPairRow( + column: columnBinding(for: pair.id), + referencedColumn: referencedColumnBinding(for: pair.id), + columns: columns(draft.table), + referencedColumns: columns(draft.referencedTable), + onMoveUp: { move(pair.id, by: -1) }, + onMoveDown: { move(pair.id, by: 1) } + ) + .tag(pair.id) + } + .onMove { source, destination in + draft = draft.movingPairs(fromOffsets: source, toOffset: destination) + } + } + .listStyle(.bordered(alternatesRowBackgrounds: false)) + .frame(height: listHeight) + .onDeleteCommand { removeSelection() } + .safeAreaInset(edge: .bottom, spacing: 0) { + VStack(spacing: 0) { + Divider() + HStack(spacing: 0) { + AddRemoveControlGroup( + addLabel: String(localized: "Add Column Pair"), + removeLabel: String(localized: "Remove Column Pair"), + canRemove: canRemoveSelection, + addIdentifier: "er-diagram-foreign-key-add-pair", + removeIdentifier: "er-diagram-foreign-key-remove-pair", + onAdd: addPair, + onRemove: removeSelection + ) + Spacer() + } + .padding(.horizontal, 4) + .padding(.vertical, 2) + } + .background(.bar) + } + } + + private let rowHeight: CGFloat = 26 + private let buttonBarHeight: CGFloat = 32 + + // No cap: a List in a grouped Form never scrolls, so it shows every pair and the Form scrolls. + private var listHeight: CGFloat { + CGFloat(max(draft.pairs.count, 2)) * rowHeight + buttonBarHeight + 8 + } + + private var canRemoveSelection: Bool { + !selection.isEmpty && selection.count < draft.pairs.count + } + + private func columnBinding(for id: UUID) -> Binding { + Binding( + get: { draft.pairs.first { $0.id == id }?.column ?? "" }, + set: { draft = draft.settingColumn($0, of: id) } + ) + } + + private func referencedColumnBinding(for id: UUID) -> Binding { + Binding( + get: { draft.pairs.first { $0.id == id }?.referencedColumn ?? "" }, + set: { draft = draft.settingReferencedColumn($0, of: id) } + ) + } + + private func addPair() { + draft = draft.addingPair() + if let added = draft.pairs.last?.id { + selection = [added] + } + } + + private func removeSelection() { + guard canRemoveSelection else { return } + draft = draft.removingPairs(selection) + selection = [] + } + + private func move(_ id: UUID, by offset: Int) { + guard let index = draft.pairs.firstIndex(where: { $0.id == id }) else { return } + let target = index + offset + guard draft.pairs.indices.contains(target) else { return } + draft = draft.movingPairs(fromOffsets: IndexSet(integer: index), toOffset: offset > 0 ? target + 1 : target) + } +} + +private struct ERDiagramColumnPairRow: View { + @Binding var column: String + @Binding var referencedColumn: String + let columns: [String] + let referencedColumns: [String] + let onMoveUp: () -> Void + let onMoveDown: () -> Void + + var body: some View { + HStack(spacing: 6) { + picker(String(localized: "Column"), selection: $column, options: columns) + Image(systemName: "arrow.right") + .foregroundStyle(.secondary) + .accessibilityHidden(true) + picker(String(localized: "Referenced Column"), selection: $referencedColumn, options: referencedColumns) + } + .accessibilityElement(children: .contain) + .accessibilityActions { + Button(String(localized: "Move Up"), action: onMoveUp) + Button(String(localized: "Move Down"), action: onMoveDown) + } + } + + // A blank side names nothing until it is chosen, so it gets an item of its own rather than an invalid selection. + private func picker(_ title: String, selection: Binding, options: [String]) -> some View { + Picker(title, selection: selection) { + if selection.wrappedValue.isEmpty { + Text(String(localized: "None")).tag("") + } + ForEach(options, id: \.self) { Text($0).tag($0) } + } + .labelsHidden() + .accessibilityLabel(title) + .frame(maxWidth: .infinity) + } } struct ERDiagramColumnSheet: View { diff --git a/apple/TablePro/Views/ERDiagram/ERDiagramView.swift b/apple/TablePro/Views/ERDiagram/ERDiagramView.swift index ef7abaf74e..3d9f47f281 100644 --- a/apple/TablePro/Views/ERDiagram/ERDiagramView.swift +++ b/apple/TablePro/Views/ERDiagram/ERDiagramView.swift @@ -71,7 +71,7 @@ struct ERDiagramView: View { draft: draft, tables: viewModel.referenceableTables, columns: { viewModel.columnNames(of: $0) }, - defaultReferencedColumn: { viewModel.defaultReferencedColumn(in: $0) }, + referencedKeyColumns: { viewModel.referencedKeyColumns(in: $0) }, isNameTaken: { viewModel.isForeignKeyNameTaken($0) }, dialect: ForeignKeyDialect.forType(viewModel.editingConnection?.type ?? .mysql), onAdd: { draft in diff --git a/apple/TablePro/Views/Editor/EditorEventRouter.swift b/apple/TablePro/Views/Editor/EditorEventRouter.swift index a6a2ed5293..187d6ffbff 100644 --- a/apple/TablePro/Views/Editor/EditorEventRouter.swift +++ b/apple/TablePro/Views/Editor/EditorEventRouter.swift @@ -2,9 +2,6 @@ // EditorEventRouter.swift // TablePro // -// Registry of the live SQL editors, used to dispatch window-scoped commands -// to the editor in the key window. -// @preconcurrency import AppKit import TableProEditorKit @@ -81,9 +78,7 @@ internal final class EditorEventRouter { // MARK: - Public API - /// Whether the key window has a SQL editor a find can fall back to. A focused editor answers the - /// Find commands itself through the responder chain, so this only decides whether the window's - /// last-resort handler has anything to search once nothing nearer has claimed them. + // A focused editor answers Find itself, so this decides only whether the window's fallback has anything to search. internal var keyWindowHasEditor: Bool { editor(for: NSApp.keyWindow) != nil } @@ -98,8 +93,6 @@ internal final class EditorEventRouter { coordinator.showFindAndReplacePanel() } - /// Whether the key window's editor has a selection to search for, so the menu can disable - /// Use Selection for Find rather than offering a command that would do nothing. internal var keyWindowEditorHasSelectionForFind: Bool { editor(for: NSApp.keyWindow)?.0.hasSelectionForFind ?? false } @@ -149,10 +142,7 @@ internal final class EditorEventRouter { coordinator.moveCursorToStatement(direction) } - /// Runs the statement the caret is in and advances, entirely inside the editor this resolves to. - /// - /// Handing the SQL back for someone else to execute would let the text and the connection come from two different - /// editors, which a window hosting more than one connection can genuinely do. + // Inside the one editor, so the text and the connection cannot come from two editors in a multi-connection window. internal func runStatementAtCursorAndAdvanceForKeyWindow() { guard let (coordinator, _) = editor(for: NSApp.keyWindow) else { return } coordinator.runStatementAtCursorAndAdvance() @@ -173,16 +163,18 @@ internal final class EditorEventRouter { } } - /// Called when the Edit menu's "Clear Selection" item is chosen with the pointer. - /// - /// Only with the pointer. AppKit never dispatches an unmodified Escape as a key equivalent: - /// measured on macOS 26, `NSMenu.performKeyEquivalent` is not called at all for key code 53, - /// which instead reaches `keyDown:` and `cancelOperation(_:)` through the responder chain, as - /// `StandardKeyBinding.dict` maps 0x1B to. The item carries no Escape key equivalent anyway, - /// because `KeyboardSettings.menuKeyEquivalent(for:)` refuses a bare key. Routes the command to - /// the key window's editor so it can dismiss its completion popup, hand the escape to Vim, and - /// restore first responder. Returns whether the editor consumed it so the caller skips its - /// cancelOperation fallback (the data grid's Clear Selection). + // False when the tab has no mounted editor, or it refused the edit: the caller writes the tab instead. + internal func replaceText(forTab tabId: UUID, connectionId: UUID, in range: NSRange, with string: String) -> Bool { + for ref in editors.values { + guard let coordinator = ref.coordinator, let controller = coordinator.controller, + coordinator.tabID == tabId, coordinator.connectionId == connectionId else { continue } + if controller.replaceTextAsOneEdit(in: range, with: string) { return true } + } + return false + } + + // Clear Selection chosen with the pointer: AppKit never sends a bare Escape as a key equivalent (measured, macOS 26). + // True when the editor consumed it, so the caller skips the grid's fallback. @discardableResult internal func handleEscapeFromMenu() -> Bool { guard let (coordinator, _) = editor(for: NSApp.keyWindow) else { return false } @@ -191,10 +183,8 @@ internal final class EditorEventRouter { // MARK: - Lookup - /// Window-scoped on purpose: every caller is a command the window's front content owns rather than - /// whatever holds first responder, and the escape and find routes exist precisely because focus is - /// somewhere else. A tab switch can leave the outgoing editor registered for a moment, and the - /// dictionary has no order, so the focused editor wins before the remaining candidate is used. + // Window-scoped: these routes exist because focus is elsewhere. A switch can leave the outgoing editor registered, + // so the focused one wins. private func editor(for window: NSWindow?) -> (SQLEditorCoordinator, TextView)? { guard let window else { return nil } var unfocused: (SQLEditorCoordinator, TextView)? diff --git a/apple/TablePro/Views/Main/Child/ConnectionTransactionActions.swift b/apple/TablePro/Views/Main/Child/ConnectionTransactionActions.swift new file mode 100644 index 0000000000..a1d5d3b0d6 --- /dev/null +++ b/apple/TablePro/Views/Main/Child/ConnectionTransactionActions.swift @@ -0,0 +1,78 @@ +// +// ConnectionTransactionActions.swift +// TablePro +// + +import AppKit + +// One flow for the bar's buttons and the Database menu's commands, so both ask and refuse the same way. +@MainActor +internal enum ConnectionTransactionActions { + // Never gated by Safe Mode: rolling back keeps nothing, so it is the way out at every level. + static func rollBack( + _ hold: SessionTransactionTracker.Hold, + databaseManager: DatabaseManager = .shared, + window: NSWindow? + ) async throws { + let confirmed = await AlertHelper.confirmDestructive( + title: String(localized: "Roll Back the Transaction?"), + message: String(localized: "Its uncommitted changes are discarded."), + confirmButton: String(localized: "Roll Back"), + window: window + ) + guard confirmed else { return } + try await databaseManager.endTransaction(hold, committing: false) + } + + // A COMMIT is a write, so it asks what a typed one asks: nothing at Silent, a confirmation at Alert, Touch ID at Safe Mode. + static func commit( + _ hold: SessionTransactionTracker.Hold, + databaseManager: DatabaseManager = .shared, + gate: any ExecutionGate = ExecutionGateProvider.shared + ) async throws { + guard let session = databaseManager.session(for: hold.connectionId) else { return } + let decision = await gate.authorize( + OperationRequest( + connectionId: hold.connectionId, + databaseType: session.connection.type, + sql: "COMMIT", + kind: .writeQuery, + caller: .userInterface, + capabilities: .interactiveUser, + operationDescription: String(localized: "Commit the transaction") + ) + ) + switch decision { + case .authorized: + try await databaseManager.endTransaction(hold, committing: true) + case .denied(_, .cancelledByUser): + return + case .denied(let reason, .policy): + throw SessionTransactionDenial(reason: reason) + } + } + + static func offersCommit(_ hold: SessionTransactionTracker.Hold, safeModeLevel: SafeModeLevel) -> Bool { + hold.state == .open && !safeModeLevel.blocksAllWrites + } + + static func perform(window: NSWindow?, _ action: @MainActor () async throws -> Void) async { + do { + try await action() + } catch is CancellationError { + return + } catch { + AlertHelper.showErrorSheet( + title: String(localized: "Could Not End the Transaction"), + message: error.localizedDescription, + window: window + ) + } + } +} + +private struct SessionTransactionDenial: LocalizedError { + let reason: String + + var errorDescription: String? { reason } +} diff --git a/apple/TablePro/Views/Main/Child/ConnectionTransactionBar.swift b/apple/TablePro/Views/Main/Child/ConnectionTransactionBar.swift index 6106744d43..53a5c82487 100644 --- a/apple/TablePro/Views/Main/Child/ConnectionTransactionBar.swift +++ b/apple/TablePro/Views/Main/Child/ConnectionTransactionBar.swift @@ -51,6 +51,8 @@ internal struct ConnectionTransactionBar: View { let connectionId: UUID @ObservedObject var transactions: SessionTransactionTracker let databaseManager: DatabaseManager + // Passed from the toolbar state the window observes: the session's own level publishes nothing. + let safeModeLevel: SafeModeLevel @State private var actingOn: SessionTransactionTracker.Key? @@ -73,7 +75,7 @@ internal struct ConnectionTransactionBar: View { isAborted: hold.state == .aborted, connectionName: session.connection.name, database: hold.key.lane ?? namedDatabase(of: session), - safeModeLevel: session.safeModeLevel + safeModeLevel: safeModeLevel ) } @@ -109,11 +111,15 @@ internal struct ConnectionTransactionBar: View { Spacer(minLength: 8) Button(String(localized: "Roll Back")) { - act(on: hold) { try await rollBack(hold) } + act(on: hold) { + try await ConnectionTransactionActions.rollBack( + hold, databaseManager: databaseManager, window: NSApp.keyWindow + ) + } } if content.offersCommit { Button(String(localized: "Commit")) { - act(on: hold) { try await commit(hold) } + act(on: hold) { try await ConnectionTransactionActions.commit(hold, databaseManager: databaseManager) } } } } @@ -124,64 +130,11 @@ internal struct ConnectionTransactionBar: View { .paneBarBackground() } - // Never gated by Safe Mode: rolling back keeps nothing, so it is the way out at every level. - private func rollBack(_ hold: SessionTransactionTracker.Hold) async throws { - let confirmed = await AlertHelper.confirmDestructive( - title: String(localized: "Roll Back the Transaction?"), - message: String(localized: "Its uncommitted changes are discarded."), - confirmButton: String(localized: "Roll Back"), - window: NSApp.keyWindow - ) - guard confirmed else { return } - try await databaseManager.endTransaction(hold, committing: false) - } - - // A COMMIT is a write, so it asks what a typed one asks: nothing at Silent, a confirmation at - // Alert, Touch ID at Safe Mode. - private func commit(_ hold: SessionTransactionTracker.Hold) async throws { - guard let session = databaseManager.session(for: connectionId) else { return } - let decision = await ExecutionGateProvider.shared.authorize( - OperationRequest( - connectionId: connectionId, - databaseType: session.connection.type, - sql: "COMMIT", - kind: .writeQuery, - caller: .userInterface, - capabilities: .interactiveUser, - operationDescription: String(localized: "Commit the transaction") - ) - ) - switch decision { - case .authorized: - try await databaseManager.endTransaction(hold, committing: true) - case .denied(_, .cancelledByUser): - return - case .denied(let reason, .policy): - throw SessionTransactionDenial(reason: reason) - } - } - private func act(on hold: SessionTransactionTracker.Hold, _ action: @escaping @MainActor () async throws -> Void) { actingOn = hold.key Task { defer { actingOn = nil } - do { - try await action() - } catch is CancellationError { - return - } catch { - AlertHelper.showErrorSheet( - title: String(localized: "Could Not End the Transaction"), - message: error.localizedDescription, - window: NSApp.keyWindow - ) - } + await ConnectionTransactionActions.perform(window: NSApp.keyWindow, action) } } } - -private struct SessionTransactionDenial: LocalizedError { - let reason: String - - var errorDescription: String? { reason } -} diff --git a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift index a246fc2a70..bed971af0e 100644 --- a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift +++ b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift @@ -269,7 +269,7 @@ struct MainEditorContentView: View { switch subject { case .linkedFile(let url): return url.lastPathComponent - case .savedQuery: + case .savedQuery, .queryTab: return "query.sql" } } @@ -511,7 +511,7 @@ struct MainEditorContentView: View { // By ID: mid-switch, flushTextUpdate() fires for the old tab while selectedTabIndex already points to the new one. guard tabManager.mutate(tabId: tabId, { $0.content.query = newValue }) else { return } - coordinator.scheduleDraftSave() + coordinator.scheduleDraftSave(editedTab: tabId) // Scratch tabs dirty too. Only this tab's window: a background window tab's editor stays mounted and can fire here. guard tabId == tabManager.selectedTabId, @@ -986,7 +986,14 @@ struct MainEditorContentView: View { resultSetMenu: resultSetMenuModel(for: tab, placedIn: .statusBar), resultSetActions: resultSetActions(for: tab), onToggleFilters: { coordinator.toggleFilterPanel() }, - onFetchAll: { coordinator.fetchAllRows() } + onFetchAll: { coordinator.fetchAllRows() }, + autoRefresh: StatusBarAutoRefreshState( + interval: tab.autoRefreshInterval, + pause: tab.autoRefreshPause, + onChange: { [coordinator, tabId = tab.id] interval in + coordinator.setAutoRefreshInterval(interval, forTab: tabId) + } + ) ) } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AIQueryActions.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AIQueryActions.swift index 750e98c33f..8217b45be3 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AIQueryActions.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AIQueryActions.swift @@ -146,13 +146,8 @@ extension MainContentCoordinator { } switch WalkthroughApplyPlan.resolve(beforeSQL: beforeSQL, source: source, tabText: tabText) { case .replace(let tabId, let range): - guard let owner else { return } - owner.tabManager.mutate(tabId: tabId) { tab in - let text = tab.content.query as NSString - guard NSMaxRange(range) <= text.length else { return } - tab.content.query = text.replacingCharacters(in: range, with: afterSQL) - tab.hasUserInteraction = true - } + guard let owner, owner.replaceQueryText(inTab: tabId, range: range, with: afterSQL) else { return } + owner.tabManager.mutate(tabId: tabId) { $0.hasUserInteraction = true } case .insertAsNewQuery: insertQueryFromAI(afterSQL) } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AutoRefresh.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AutoRefresh.swift new file mode 100644 index 0000000000..474224b02a --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+AutoRefresh.swift @@ -0,0 +1,170 @@ +// +// MainContentCoordinator+AutoRefresh.swift +// TablePro +// + +import Combine +import Foundation + +// One per window: only the selected table tab ticks, so a switch, a close or a move ends one wait and starts the next. +@MainActor +final class TableAutoRefreshSchedule { + var clock: any Clock = ContinuousClock() + var visibility: @MainActor (OperationOwner, UUID) -> ResultVisibility = { owner, connectionId in + ResultVisibilityResolver.resolve(owner: owner, connectionId: connectionId) + } + fileprivate(set) var task: Task? + fileprivate var selectionCancellable: AnyCancellable? + + var isRunning: Bool { task != nil } +} + +extension MainContentCoordinator { + func observeSelectionForAutoRefresh() { + autoRefresh.selectionCancellable = tabManager.$selectedTabId + .removeDuplicates() + .dropFirst() + .receive(on: RunLoop.main) + .sink { [weak self] _ in self?.restartAutoRefresh() } + } + + func setAutoRefreshInterval(_ interval: TableAutoRefreshInterval, forTab tabId: UUID) { + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), tab.tabType == .table else { return } + if tab.autoRefreshInterval != interval || tab.autoRefreshPause != nil { + tabManager.mutate(tabId: tabId) { tab in + tab.autoRefreshInterval = interval + tab.autoRefreshPause = nil + } + } + guard tabId == tabManager.selectedTabId else { return } + restartAutoRefresh() + } + + func restartAutoRefresh() { + autoRefresh.task?.cancel() + autoRefresh.task = nil + guard !isTearingDown, + let tab = tabManager.selectedTab, + tab.tabType == .table, + tab.autoRefreshInterval.isOn else { return } + let tabId = tab.id + let clock = autoRefresh.clock + // Weak between ticks, so a wait never keeps a closed window's coordinator alive. + autoRefresh.task = Task { [weak self] in + while !Task.isCancelled { + await self?.awaitAutoRefreshLoad(tabId) + guard let interval = self?.autoRefreshInterval(ofTab: tabId), interval.isOn else { return } + do { + try await clock.sleep(for: interval.duration) + } catch { + return + } + guard let self, !Task.isCancelled else { return } + guard await runAutoRefreshTick(tabId) else { return } + } + } + } + + func stopAutoRefresh() { + autoRefresh.task?.cancel() + autoRefresh.task = nil + autoRefresh.selectionCancellable = nil + } + + func autoRefreshGate(forTab tabId: UUID) -> TableAutoRefreshGate { + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }) else { + return TableAutoRefreshGate(isTableTab: false) + } + let visibility = autoRefresh.visibility(operationOwner(tabId: tabId), connectionId) + let model = existingGridModel(forTab: tabId) + let database = scope(for: tab)?.database ?? browseDatabaseName + var gate = TableAutoRefreshGate() + gate.isTableTab = tab.tabType == .table && tab.tableContext.tableName != nil + gate.hasLoaded = tab.execution.lastExecutedAt != nil + gate.windowIsVisible = visibility.ownerWindowIsVisible + gate.isSelectedInWindow = visibility.ownerIsSelectedInWindow && tabManager.selectedTabId == tabId + gate.isConnected = services.databaseManager.session(for: connectionId)?.reportedStatus == .connected + gate.viewMode = tab.display.resultsViewMode + // Read only: a read inside the user's transaction sees its snapshot, and a failure aborts it on PostgreSQL. + gate.holdsTransaction = services.databaseManager.sessionTransactions + .transactionState(connectionId: connectionId, container: database) != nil + gate.holdsUnsavedWork = holdsUnsavedWork(tab) + gate.hasSidebarDataWork = commandActions?.hasDataWork ?? false + gate.isEditingCell = model?.isEditing ?? false + gate.gridHoldsTransientUI = model?.holdsTransientUI ?? false + gate.gridIsTrackingInteraction = model?.isTrackingInteraction ?? false + gate.inspectorHoldsValueWindow = trailingPaneState.map { InspectorPopOutRegistry.holdsWindow(openedFrom: $0) } ?? false + gate.load = tabExecution.isBusy(tabId) || tableLoadTasks[tabId] != nil ? .busy : .idle + gate.isCountPending = tab.pagination.isCountPending + gate.isLoadingMore = tab.pagination.isLoadingMore + gate.showsSafeModePrompt = isShowingSafeModePrompt + gate.hasSelection = model.map { !$0.selection.affectedRows.isEmpty } ?? false + gate.hasPrimaryKey = !tab.tableContext.primaryKeyColumns.isEmpty + return gate + } + + func autoRefreshHeldByTransientUI(_ tabId: UUID) -> Bool { + let gate = autoRefreshGate(forTab: tabId) + return gate.gridHoldsTransientUI || gate.inspectorHoldsValueWindow || gate.isEditingCell + } + + private func autoRefreshInterval(ofTab tabId: UUID) -> TableAutoRefreshInterval? { + guard tabManager.selectedTabId == tabId else { return nil } + return tabManager.tabs.first { $0.id == tabId }?.autoRefreshInterval + } + + // Once, not in a loop: a finished task its owner has not retired yet would spin the main actor. + private func awaitAutoRefreshLoad(_ tabId: UUID) async { + if let scheduled = tableLoadTasks[tabId]?.task { + await scheduled.value + } + if let running = queryTasks.task(for: tabId) { + await running.value + } + } + + // False ends the schedule: the tab is gone or no longer selected, or the gate stopped it. + private func runAutoRefreshTick(_ tabId: UUID) async -> Bool { + // A retarget keeps the tab id and turns the interval off while this wait slept. + guard autoRefreshInterval(ofTab: tabId)?.isOn == true else { return false } + switch autoRefreshGate(forTab: tabId).decision { + case .stop: + endAutoRefresh(forTab: tabId) + return false + case .wait(let pause): + recordAutoRefreshPause(pause.help == nil ? nil : pause, forTab: tabId) + return true + case .refresh: + recordAutoRefreshPause(nil, forTab: tabId) + } + // Not runQuery, which commits an open edit, nor reloadTableTab, which retires the count. + if let load = executeTableTabQueryDirectly(trigger: .autoRefresh, viewport: .keepPlace) + ?? queryTasks.task(for: tabId) { + await load.value + } + guard !Task.isCancelled else { return false } + var settled = autoRefreshGate(forTab: tabId) + settled.previousTickFailed = tabManager.tabs.first { $0.id == tabId }?.execution.errorMessage != nil + guard settled.decision != .stop else { + endAutoRefresh(forTab: tabId) + return false + } + return true + } + + // The tab keeps its own inline error; only the interval goes. + private func endAutoRefresh(forTab tabId: UUID) { + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), + tab.autoRefreshInterval.isOn || tab.autoRefreshPause != nil else { return } + tabManager.mutate(tabId: tabId) { tab in + tab.autoRefreshInterval = .off + tab.autoRefreshPause = nil + } + } + + // Written only on change: every tab write re-renders the window. + private func recordAutoRefreshPause(_ pause: TableAutoRefreshPause?, forTab tabId: UUID) { + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), tab.autoRefreshPause != pause else { return } + tabManager.mutate(tabId: tabId) { $0.autoRefreshPause = pause } + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DraftSave.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DraftSave.swift new file mode 100644 index 0000000000..74c62ffb1b --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DraftSave.swift @@ -0,0 +1,57 @@ +// +// MainContentCoordinator+DraftSave.swift +// TablePro +// + +import Foundation + +extension MainContentCoordinator { + static let periodicSaveInterval: Duration = .seconds(30) + static let draftSaveDebounce: Duration = .seconds(1) + + // Typing changes no tab identity, so without this a scratch query lives only in memory until the 30s timer or quit flush. + // Any window may save: `saveAggregated` writes every window's tabs for the connection. + func scheduleDraftSave(editedTab tabId: UUID? = nil) { + guard !Self.isAppTerminating, !isTearingDown else { return } + if let tabId { draftSaveTabIds.insert(tabId) } + draftSaveTask?.cancel() + let clock = draftSaveClock + draftSaveTask = Task { [weak self] in + try? await clock.sleep(for: Self.draftSaveDebounce) + guard let self, !Task.isCancelled, !Self.isAppTerminating, !self.isTearingDown else { return } + self.recordIdleVersions() + self.persistence.saveAggregated() + } + } + + func startPeriodicSave() { + guard periodicSaveTask == nil else { return } + periodicSaveTask = Task { [weak self] in + while !Task.isCancelled { + try? await Task.sleep(for: Self.periodicSaveInterval) + guard let self, !Task.isCancelled, !Self.isAppTerminating, !self.isTearingDown else { return } + guard self.isFirstCoordinatorForConnection() else { continue } + self.persistence.saveAggregated() + } + } + } + + static func aggregatedSelectedTabId(for connectionId: UUID) -> UUID? { + activeCoordinators.values + .first { $0.connectionId == connectionId && $0.tabManager.selectedTabId != nil }? + .tabManager.selectedTabId + } + + private func isFirstCoordinatorForConnection() -> Bool { + Self.activeCoordinators.values + .first { $0.connectionId == self.connectionId } === self + } + + private func recordIdleVersions() { + let tabIds = draftSaveTabIds + draftSaveTabIds.removeAll() + for tabId in tabIds { + tabVersions.record(tabManager.tabs.first { $0.id == tabId }, connectionId: connectionId, reason: .idle) + } + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+EditorQueryLoading.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+EditorQueryLoading.swift new file mode 100644 index 0000000000..9a1b94e87e --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+EditorQueryLoading.swift @@ -0,0 +1,99 @@ +// +// MainContentCoordinator+EditorQueryLoading.swift +// TablePro +// + +import Foundation + +extension MainContentCoordinator { + @discardableResult + func loadQueryIntoEditor( + _ query: String, + databaseName: String? = nil, + forceNewTab: Bool = false + ) -> WindowTabOpenDisposition { + let targetDatabaseName = databaseName ?? newTabDatabaseName + if !forceNewTab, + let (tab, tabIndex) = tabManager.selectedTabAndIndex, + tab.tabType == .query, + databaseName == nil + || tab.tableContext.resolvedDatabaseName(browsing: browseDatabaseName) == targetDatabaseName { + replaceQueryText(inTab: tab.id, with: query) + tabManager.mutate(at: tabIndex) { $0.hasUserInteraction = true } + return .currentCoordinator + } + + if !forceNewTab, tabManager.tabs.isEmpty { + tabManager.addTab(initialQuery: query, databaseName: targetDatabaseName) + return .currentCoordinator + } + + let payload = EditorTabPayload( + connectionId: connection.id, + tabType: .query, + databaseName: targetDatabaseName, + initialQuery: query + ) + openTabInNewWindow(payload) + return .focusedElsewhere + } + + var aiInsertReusesSelectedQueryTab: Bool { + guard let (tab, _) = tabManager.selectedTabAndIndex, tab.tabType == .query else { return false } + return tab.content.query.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + } + + func insertQueryFromAI(_ query: String) { + if aiInsertReusesSelectedQueryTab, let (tab, tabIndex) = tabManager.selectedTabAndIndex { + replaceQueryText(inTab: tab.id, with: query) + tabManager.mutate(at: tabIndex) { $0.hasUserInteraction = true } + } else if tabManager.tabs.isEmpty { + tabManager.addTab(initialQuery: query, databaseName: newTabDatabaseName) + } else { + let payload = EditorTabPayload( + connectionId: connection.id, + tabType: .query, + initialQuery: query + ) + WindowManager.shared.openTab(payload: payload) + } + } + + func clearSelectedQuery() { + guard let tab = tabManager.selectedTab, tab.tabType == .query else { return } + replaceQueryText(inTab: tab.id, with: "") + } + + // Through the mounted editor the change is one undoable edit; with no editor mounted the model takes the text, + // and the version recorded first is the way back. Nil `reason` is for a caller that recorded already. + @discardableResult + func replaceQueryText( + inTab tabId: UUID, + range: NSRange? = nil, + with text: String, + recording reason: QueryTabVersionReason? = .beforeReplace + ) -> Bool { + flushEditorText(forTab: tabId) + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), tab.tabType == .query else { return false } + let current = tab.content.query as NSString + let target = range ?? NSRange(location: 0, length: current.length) + guard NSMaxRange(target) <= current.length else { return false } + if let reason { + tabVersions.record(tab, connectionId: connectionId, reason: reason) + } + let replacedInEditor = EditorEventRouter.shared.replaceText( + forTab: tabId, + connectionId: connectionId, + in: target, + with: text + ) + if !replacedInEditor { + let replaced = current.replacingCharacters(in: target, with: text) + tabManager.mutate(tabId: tabId) { $0.content.query = replaced } + } + scheduleDraftSave(editedTab: tabId) + syncQueryToolbarStateForSelectedTab() + refreshUnsavedIndicator() + return true + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift index 067f3496e6..b79ca304b9 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Explain.swift @@ -119,6 +119,7 @@ extension MainContentCoordinator { tabManager.mutate(tabId: tabId) { $0.execution.errorMessage = nil } // EXPLAIN ANALYZE and an actual plan run the statement, and a CREATE TABLE AS among them changes the catalog. + // Under Manual Commit such a run is held, as the write itself would be. let ranWrite = request.operationKind(databaseType: conn.type).declaresWrite let failureOutput = ServerOutputBox() let explainTask = Task { [weak self] in @@ -128,16 +129,18 @@ extension MainContentCoordinator { scope: scope, route: services.databaseManager.executionRoute(for: scope), cancellation: .cancellableRead(lease) - ) { [queryExecutor] driver in - try await ServerOutputCapture.running(on: driver, failureOutput: failureOutput) { () async throws -> QueryFetchResult in - if let step = request.driverStep { - return try await QueryExecutor.fetchExplainPlan( - driver: driver, statement: request.subjectSQL, variantId: step.variantId + ) { [queryExecutor, held = manualCommitRun(skipped: !ranWrite)] driver in + try await held.running([request.gatedSQL(databaseType: conn.type)], on: driver) { + try await ServerOutputCapture.running(on: driver, failureOutput: failureOutput) { () async throws -> QueryFetchResult in + if let step = request.driverStep { + return try await QueryExecutor.fetchExplainPlan( + driver: driver, statement: request.subjectSQL, variantId: step.variantId + ) + } + return try await queryExecutor.executeQuery( + driver: driver, sql: request.sql, parameters: nil, rowCap: nil ) } - return try await queryExecutor.executeQuery( - driver: driver, sql: request.sql, parameters: nil, rowCap: nil - ) } } if ranWrite { Self.postStatementRan(request.subjectSQL, on: conn) } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Favorites.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Favorites.swift index bec094a2e0..f2876bd7a1 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Favorites.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Favorites.swift @@ -18,13 +18,15 @@ extension MainContentCoordinator { return } - if let (tab, tabIndex) = tabManager.selectedTabAndIndex, - tab.tabType == .query { - let existing = tab.content.query.trimmingCharacters(in: .whitespacesAndNewlines) - if existing.isEmpty { - tabManager.mutate(at: tabIndex) { $0.content.query = favorite.query } + if let selected = tabManager.selectedTab, selected.tabType == .query { + // Flushed first: on a large document the tab's text trails the editor, and the end must be the editor's. + flushEditorText(forTab: selected.id) + let existing = tabManager.selectedTab?.content.query ?? "" + if existing.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { + replaceQueryText(inTab: selected.id, with: favorite.query) } else { - tabManager.mutate(at: tabIndex) { $0.content.query += "\n\n" + favorite.query } + let end = NSRange(location: (existing as NSString).length, length: 0) + replaceQueryText(inTab: selected.id, range: end, with: "\n\n" + favorite.query) } } else { runFavoriteInNewTab(favorite) @@ -43,8 +45,7 @@ extension MainContentCoordinator { func openLinkedFavorite(_ favorite: LinkedSQLFavorite) { guard let loaded = FileTextLoader.load(favorite.fileURL) else { return } - /// Any connection's tab, not only this window's selected one: the file can be open in a - /// workspace the window is not showing, and opening it here too made two buffers on one file. + // Any connection's tab: the file can be open in a workspace this window is not showing. if hostedTabRouting.revealTab(editing: favorite.fileURL) { AppActivationPolicyController.shared.activate(ignoringOtherApps: true) return diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridViewport.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridViewport.swift index fffc3eb546..1cbad5583c 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridViewport.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+GridViewport.swift @@ -41,12 +41,16 @@ extension MainContentCoordinator { let sample = intent == .keepPlace ? viewportSample(of: model) : nil guard sample != nil || keepsSelection else { return .top } + // Mode, columns and active cell too, or a cell cursor comes back as whole rows on column 0. + let selection = model.map { + GridSelectionSnapshot($0.selection, order: $0.order, rows: tableRows, columns: $0.columns) + } let snapshot = GridViewportResolver.snapshot( of: tableRows, displayIDs: model?.displayedRowIDs, firstVisibleDisplayRow: sample?.row ?? 0, firstVisibleOffset: sample?.offset ?? 0, - selectedDisplayRows: keepsSelection ? Array(model?.selection.affectedRows ?? IndexSet()) : [], + selection: keepsSelection ? selection ?? .empty : .empty, keyColumns: keyColumns, isCellModified: { [changeManager] rowID, column in changeManager.isCellModified(rowID: rowID, columnIndex: column) diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ManualCommit.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ManualCommit.swift new file mode 100644 index 0000000000..94593f8a72 --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+ManualCommit.swift @@ -0,0 +1,18 @@ +// +// MainContentCoordinator+ManualCommit.swift +// TablePro +// + +import Foundation + +extension MainContentCoordinator { + // A table tab's read and an auto-load are the app's own SELECTs, so they never hold anything. + func manualCommitRun(skipped: Bool) -> ManualCommitRun { + ManualCommitRun( + commitMode: skipped ? .automatic : services.databaseManager.commitMode(for: connectionId), + databaseType: connection.type, + grammar: lexicalGrammar, + connectionId: connectionId + ) + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Phase2Launch.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Phase2Launch.swift new file mode 100644 index 0000000000..d3e7b515a4 --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Phase2Launch.swift @@ -0,0 +1,41 @@ +// +// MainContentCoordinator+Phase2Launch.swift +// TablePro +// + +import Foundation + +extension MainContentCoordinator { + func launchPhase2AfterApply( + tableName: String?, + isEditable: Bool, + tabId: UUID, + trigger: TableLoadTrigger, + needsMetadataFetch: Bool, + schemaTask: Task?, + claim: TabExecutionClaim + ) { + if isEditable, let tableName, trigger.countsRows { + // Marked pending here: phase 1 clears `isLoading` first, so without it the tab looks settled and offers Count Exactly early. + tabManager.mutate(tabId: tabId) { $0.pagination.isCountPending = true } + launchPhase2( + tableName: tableName, + tabId: tabId, + connectionType: connection.type, + needsMetadataFetch: needsMetadataFetch, + schemaTask: schemaTask + ) + } else if isEditable, let tableName, needsMetadataFetch { + // A tick keeps the count it has; only a manual Refresh recounts. + launchPhase2Work( + tableName: tableName, + tabId: tabId, + connectionType: connection.type, + schemaTask: schemaTask, + countsRows: false + ) + } else if !isEditable || tableName == nil { + clearChangesIfCurrent(claim: claim) + } + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QueryHelpers.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QueryHelpers.swift index 2e1a8f6b71..d3946eabbe 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QueryHelpers.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+QueryHelpers.swift @@ -88,7 +88,7 @@ extension MainContentCoordinator { parkedTableLoad = ParkedTableLoad(tabId: tabId, trigger: trigger) return } - handleQueryExecutionError(error, sql: sql, tabId: tabId, connection: conn, serverOutput: serverOutput) + handleQueryExecutionError(error, sql: sql, tabId: tabId, connection: conn, serverOutput: serverOutput, trigger: trigger) reportQueryOperation( claim: claim, trigger: trigger, outcome: .failed(reason: error.localizedDescription) ) @@ -146,7 +146,8 @@ extension MainContentCoordinator { absentCells: [Int: Set] = [:], unloadedCells: [Int: [Int: Int]] = [:], columnOrigins: [PluginColumnOrigin?]? = nil, - prepared: TableRows.PreparedRows? = nil + prepared: TableRows.PreparedRows? = nil, + trigger: TableLoadTrigger = .userInitiated ) { queryExecutionCoordinator.applyPhase1Result( tabId: tabId, @@ -175,7 +176,8 @@ extension MainContentCoordinator { absentCells: absentCells, unloadedCells: unloadedCells, columnOrigins: columnOrigins, - prepared: prepared + prepared: prepared, + trigger: trigger ) } @@ -240,13 +242,15 @@ extension MainContentCoordinator { tableName: String, tabId: UUID, connectionType: DatabaseType, - schemaTask: Task? + schemaTask: Task?, + countsRows: Bool = true ) { queryExecutionCoordinator.launchPhase2Work( tableName: tableName, tabId: tabId, connectionType: connectionType, - schemaTask: schemaTask + schemaTask: schemaTask, + countsRows: countsRows ) } @@ -267,14 +271,16 @@ extension MainContentCoordinator { sql: String, tabId: UUID, connection conn: DatabaseConnection, - serverOutput: PluginServerOutput = .none + serverOutput: PluginServerOutput = .none, + trigger: TableLoadTrigger = .userInitiated ) { queryExecutionCoordinator.handleQueryExecutionError( error, sql: sql, tabId: tabId, connection: conn, - serverOutput: serverOutput + serverOutput: serverOutput, + trigger: trigger ) } } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift index e5dcc43095..33edc43c0c 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift @@ -2,8 +2,6 @@ // MainContentCoordinator+Refresh.swift // TablePro // -// Refresh handling operations for MainContentCoordinator -// import AppKit import Combine @@ -102,5 +100,7 @@ extension MainContentCoordinator { tabManager.mutate(at: tabIndex) { $0.pagination.retireDerivedRowCount() } rebuildTableQuery(at: tabIndex) runQuery(viewport: .keepPlace) + // The next tick counts from this load settling, not from the last one. + restartAutoRefresh() } } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+VersionHistory.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+VersionHistory.swift index 0d8313d732..7d30e1a0f2 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+VersionHistory.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+VersionHistory.swift @@ -17,6 +17,58 @@ extension MainContentCoordinator { openVersionHistory(subject: .linkedFile(url: linked.fileURL), name: linked.fileURL.lastPathComponent) } + func showVersionHistory(ofQueryTab tabId: UUID) { + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), tab.tabType == .query else { return } + openVersionHistory(subject: .queryTab(id: tabId), name: tab.title) + } + + func revertQueryTab(_ tabId: UUID, toVersion versionId: Int64) { + let provider = queryTabVersionProvider(for: tabId) + Task { + do { + try await provider.prepareRestore(.tabVersion(id: versionId)).apply() + } catch { + Self.versionHistoryLogger.error("Reverting a query tab failed: \(error.publicLogShape, privacy: .public)") + presentError(String(localized: "Couldn't Restore Version"), error.localizedDescription, contentWindow) + } + } + } + + func queryTabVersionProvider(for tabId: UUID) -> QueryTabVersionHistoryProvider { + let recorder = tabVersions + return QueryTabVersionHistoryProvider( + tabId: tabId, + storage: recorder.storage, + liveTab: { [weak self] in self?.liveQueryTab(tabId) }, + write: { [weak self] text in self?.replaceQueryText(inTab: tabId, with: text, recording: nil) ?? false }, + isCapturePaused: recorder.isCapturePaused + ) + } + + // The History tab can outlive a move of the tab it shows, so each call finds the tab's current owner. + static func hostedQueryTabVersionProvider(for tabId: UUID) -> QueryTabVersionHistoryProvider { + QueryTabVersionHistoryProvider( + tabId: tabId, + storage: .shared, + liveTab: { Self.owner(ofQueryTab: tabId)?.liveQueryTab(tabId) }, + write: { text in + Self.owner(ofQueryTab: tabId)?.replaceQueryText(inTab: tabId, with: text, recording: nil) ?? false + } + ) + } + + func liveQueryTab(_ tabId: UUID) -> LiveQueryTab? { + flushEditorText(forTab: tabId) + guard let tab = tabManager.tabs.first(where: { $0.id == tabId }), tab.tabType == .query else { return nil } + return LiveQueryTab(connectionId: connectionId, text: tab.content.query) + } + + private static func owner(ofQueryTab tabId: UUID) -> MainContentCoordinator? { + HostedTabRouting.live.allCoordinators().first { coordinator in + coordinator.tabManager.tabs.contains { $0.id == tabId && $0.tabType == .query } + } + } + func openVersionInEditor(_ content: String) { let payload = EditorTabPayload( connectionId: connectionId, diff --git a/apple/TablePro/Views/Main/MainContentCommandActions.swift b/apple/TablePro/Views/Main/MainContentCommandActions.swift index 0decc6483f..be1c6cca5d 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions.swift @@ -1131,14 +1131,7 @@ final class MainContentCommandActions: ObservableObject { } func clearQuery() { - guard let coordinator, - let (tab, tabIndex) = coordinator.tabManager.selectedTabAndIndex, - tab.tabType == .query else { return } - coordinator.tabManager.mutate(at: tabIndex) { $0.content.query = "" } - coordinator.toolbarState.hasQueryText = false - coordinator.scheduleDraftSave() - // Emptying from a command bypasses the editor binding that recomputes the dirty indicator on each keystroke. - coordinator.refreshUnsavedIndicator() + coordinator?.clearSelectedQuery() } var canClearQuery: Bool { @@ -1289,6 +1282,19 @@ final class MainContentCommandActions: ObservableObject { ) } + // Nil off a table tab, which the Database menu's Auto Refresh items dim for. + var tableAutoRefreshInterval: TableAutoRefreshInterval? { + guard let tab = coordinator?.tabManager.selectedTab, + tab.tabType == .table, + tab.tableContext.tableName != nil else { return nil } + return tab.autoRefreshInterval + } + + func setTableAutoRefreshInterval(_ interval: TableAutoRefreshInterval) { + guard let coordinator, let tabId = coordinator.tabManager.selectedTabId else { return } + coordinator.setAutoRefreshInterval(interval, forTab: tabId) + } + private var hasPendingTableOps: Bool { !pendingTruncates.wrappedValue.isEmpty || !pendingDeletes.wrappedValue.isEmpty } diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index d603b2f471..5127b296a3 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -330,8 +330,11 @@ final class MainContentCoordinator: ObservableObject { // Not on `QueryTab`: that is the persisted shape, and staying out of its `Equatable` keeps a push from re-publishing tabs. internal var navigationHistories: [UUID: TabNavigationHistory] = [:] internal var redisDatabaseSwitchTask: Task? - private var periodicSaveTask: Task? - private var draftSaveTask: Task? + var periodicSaveTask: Task? + var draftSaveTask: Task? + var draftSaveTabIds: Set = [] + var draftSaveClock: any Clock = ContinuousClock() + var tabVersions = QueryTabVersionRecorder.live private var terminationObserver: NSObjectProtocol? internal var postConnectCancellable: AnyCancellable? // Owned here, not by the view: a connection switch cancels the view's `task`, and a cancelled first load never reran. @@ -393,6 +396,7 @@ final class MainContentCoordinator: ObservableObject { var refreshCoalesceTask: Task? var refreshPendingTrailing = false + let autoRefresh = TableAutoRefreshSchedule() // SwiftUI creates and discards coordinators during body re-evaluation; those never activate and need no teardown warning. private let _didActivate = OSAllocatedUnfairLock(initialState: false) @@ -566,44 +570,6 @@ final class MainContentCoordinator: ObservableObject { } } - private static let periodicSaveInterval: Duration = .seconds(30) - private static let draftSaveDebounce: Duration = .seconds(1) - - // Typing changes no tab identity, so without this a scratch query lives only in memory until the 30s timer or quit flush. - func scheduleDraftSave() { - guard !Self.isAppTerminating, !isTearingDown else { return } - draftSaveTask?.cancel() - draftSaveTask = Task { [weak self] in - try? await Task.sleep(for: Self.draftSaveDebounce) - guard let self, !Task.isCancelled, !Self.isAppTerminating, !self.isTearingDown else { return } - guard self.isFirstCoordinatorForConnection() else { return } - self.persistence.saveAggregated() - } - } - - private func startPeriodicSave() { - guard periodicSaveTask == nil else { return } - periodicSaveTask = Task { [weak self] in - while !Task.isCancelled { - try? await Task.sleep(for: Self.periodicSaveInterval) - guard let self, !Task.isCancelled, !Self.isAppTerminating, !self.isTearingDown else { return } - guard self.isFirstCoordinatorForConnection() else { continue } - self.persistence.saveAggregated() - } - } - } - - static func aggregatedSelectedTabId(for connectionId: UUID) -> UUID? { - activeCoordinators.values - .first { $0.connectionId == connectionId && $0.tabManager.selectedTabId != nil }? - .tabManager.selectedTabId - } - - private func isFirstCoordinatorForConnection() -> Bool { - Self.activeCoordinators.values - .first { $0.connectionId == self.connectionId } === self - } - private static let registerTerminationObserver: Void = { NotificationCenter.default.addObserver( forName: NSApplication.willTerminateNotification, @@ -709,6 +675,7 @@ final class MainContentCoordinator: ObservableObject { clearedEditsCancellable = resumeWhenEditsClear() gridRepaintCancellable = observeChangeManagerForGridRepaint() maskingRulesCancellable = observeMaskingRuleChanges() + observeSelectionForAutoRefresh() Self.lifecycleLogger.info( "[open] MainContentCoordinator.init done connId=\(connection.id, privacy: .public) elapsedMs=\(Int(Date().timeIntervalSince(initStart) * 1_000))" @@ -854,7 +821,7 @@ final class MainContentCoordinator: ObservableObject { connectionId: connectionId, tabType: .query, databaseName: resolved.database, - schemaName: resolved.schema, + schemaName: resolved.isDatabaseScoped ? nil : resolved.schema, initialQuery: source, skipAutoExecute: true, tabTitle: resolved.displayIdentity @@ -898,6 +865,7 @@ final class MainContentCoordinator: ObservableObject { reportEndedExecutions(tabExecution.invalidateAll(reason: .sessionEnded)) refreshCoalesceTask?.cancel() refreshCoalesceTask = nil + stopAutoRefresh() for entry in tableLoadTasks.values { entry.task.cancel() } tableLoadTasks.removeAll() cancelAllRowCountTasks() @@ -1061,6 +1029,7 @@ final class MainContentCoordinator: ObservableObject { let batches = queryExecutionCoordinator.executionBatches(in: sql, sourceOffset: sourceOffset ?? 0) let statements = batches.flatMap(\.statements) guard !statements.isEmpty else { return false } + tabVersions.record(tabManager.tabs[index], connectionId: connectionId, reason: .run) if services.appSettings.editor.queryParametersEnabled, bindsNamedParameters { let combinedSQL = SQLParameterExtractor.parameterSource(of: statements) @@ -1163,65 +1132,6 @@ final class MainContentCoordinator: ObservableObject { return nil } - // MARK: - Editor Query Loading - - @discardableResult - func loadQueryIntoEditor( - _ query: String, - databaseName: String? = nil, - forceNewTab: Bool = false - ) -> WindowTabOpenDisposition { - let targetDatabaseName = databaseName ?? newTabDatabaseName - if !forceNewTab, - let (tab, tabIndex) = tabManager.selectedTabAndIndex, - tab.tabType == .query, - databaseName == nil - || tab.tableContext.resolvedDatabaseName(browsing: browseDatabaseName) == targetDatabaseName { - tabManager.mutate(at: tabIndex) { - $0.content.query = query - $0.hasUserInteraction = true - } - return .currentCoordinator - } - - if !forceNewTab, tabManager.tabs.isEmpty { - tabManager.addTab(initialQuery: query, databaseName: targetDatabaseName) - return .currentCoordinator - } - - let payload = EditorTabPayload( - connectionId: connection.id, - tabType: .query, - databaseName: targetDatabaseName, - initialQuery: query - ) - openTabInNewWindow(payload) - return .focusedElsewhere - } - - var aiInsertReusesSelectedQueryTab: Bool { - guard let (tab, _) = tabManager.selectedTabAndIndex, tab.tabType == .query else { return false } - return tab.content.query.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty - } - - func insertQueryFromAI(_ query: String) { - if aiInsertReusesSelectedQueryTab, let (_, tabIndex) = tabManager.selectedTabAndIndex { - tabManager.mutate(at: tabIndex) { mutTab in - mutTab.content.query = query - mutTab.hasUserInteraction = true - } - } else if tabManager.tabs.isEmpty { - tabManager.addTab(initialQuery: query, databaseName: newTabDatabaseName) - } else { - let payload = EditorTabPayload( - connectionId: connection.id, - tabType: .query, - initialQuery: query - ) - WindowManager.shared.openTab(payload: payload) - } - } - internal func executeQueryInternal( _ sql: String, isAutoLoad: Bool = false, @@ -1245,7 +1155,7 @@ final class MainContentCoordinator: ObservableObject { let conn = connection let tabId = tabManager.tabs[index].id - let traceToken = adoptOrBeginExecutionTrace(tabId: tabId) + let traceToken = trigger.tracesLoad ? adoptOrBeginExecutionTrace(tabId: tabId) : nil traceExecutionStarted(traceToken, epoch: claim.epoch, isAutoLoad: isAutoLoad) let statement = resolveStatement(sql: sql, tabType: tab.tabType, bypassLimit: bypassRowLimit) @@ -1275,6 +1185,7 @@ final class MainContentCoordinator: ObservableObject { let isTableTab = tab.tabType == .table let readsView = tab.tableContext.knowsView(named: tableName) let (grammar, lengthColumns) = (lexicalGrammar, unloadedValueLengthColumns(for: tab)) + let held = manualCommitRun(skipped: isTableTab || isAutoLoad) let failureOutput = ServerOutputBox() let queryTask = Task { [weak self] in @@ -1293,15 +1204,17 @@ final class MainContentCoordinator: ObservableObject { isTableTab: isTableTab, lease: lease ) { [queryExecutor, manager = services.databaseManager] driver in - // Before it runs, so a statement that fails or a connection lost under it is still noted. - if !isAutoLoad { await manager.noteSessionStatements([statement.sql], ranOn: driver, scope: scope) } - let fetched = try await queryExecutor.executeQuery( - driver: driver, - sql: statement.sql, - parameters: nil, - rowCap: rowCap, - capturingOutputInto: isTableTab ? nil : failureOutput - ) + let fetched = try await held.running([statement.sql], on: driver) { + // Before it runs, so a statement that fails or a connection lost under it is still noted. + if !isAutoLoad { await manager.noteSessionStatements([statement.sql], ranOn: driver, scope: scope) } + return try await queryExecutor.executeQuery( + driver: driver, + sql: statement.sql, + parameters: nil, + rowCap: rowCap, + capturingOutputInto: isTableTab ? nil : failureOutput + ) + } let edits = isAutoLoad ? nil : await SucceededStatements.single( statement.sql, scope: scope, databaseType: conn.type, grammar: grammar, ranOn: driver ) @@ -1338,6 +1251,8 @@ final class MainContentCoordinator: ObservableObject { traceStaleResultDropped(traceToken) return false } + // A menu or value window opened during the read names rows by position, so a tick's rows wait for the next one. + guard trigger != .autoRefresh || !autoRefreshHeldByTransientUI(tabId) else { return false } toolbarState.recordQueryTiming(fetchResult.resolvedTiming, for: tabId) traceApplyingResult(traceToken, tabId: tabId) @@ -1369,7 +1284,8 @@ final class MainContentCoordinator: ObservableObject { absentCells: fetchResult.absentCells, unloadedCells: fetchResult.unloadedCells, columnOrigins: fetchResult.columnOrigins, - prepared: prepared + prepared: prepared, + trigger: trigger ) scheduleTraceCompletion(traceToken, outcome: .completed) @@ -1382,19 +1298,15 @@ final class MainContentCoordinator: ObservableObject { return } - if isEditable, let tableName { - // Marked pending here: phase 1 clears `isLoading` first, so without it the tab looks settled and offers Count Exactly early. - tabManager.mutate(tabId: tabId) { $0.pagination.isCountPending = true } - launchPhase2( - tableName: tableName, - tabId: tabId, - connectionType: conn.type, - needsMetadataFetch: needsMetadataFetch, - schemaTask: schemaTask - ) - } else if !isEditable || tableName == nil { - clearChangesIfCurrent(claim: claim) - } + launchPhase2AfterApply( + tableName: tableName, + isEditable: isEditable, + tabId: tabId, + trigger: trigger, + needsMetadataFetch: needsMetadataFetch, + schemaTask: schemaTask, + claim: claim + ) } catch { schemaTask?.cancel() if !isAutoLoad { Self.postStatementRan(statement.sql, on: conn) } diff --git a/apple/TablePro/Views/Main/MainContentView.swift b/apple/TablePro/Views/Main/MainContentView.swift index 82eade5715..c1e5df3ff7 100644 --- a/apple/TablePro/Views/Main/MainContentView.swift +++ b/apple/TablePro/Views/Main/MainContentView.swift @@ -512,7 +512,8 @@ struct MainContentView: View { ConnectionTransactionBar( connectionId: connection.id, transactions: DatabaseManager.shared.sessionTransactions, - databaseManager: DatabaseManager.shared + databaseManager: DatabaseManager.shared, + safeModeLevel: toolbarState.safeModeLevel ) editorContentView } diff --git a/apple/TablePro/Views/ObjectSource/ObjectSourceTabView.swift b/apple/TablePro/Views/ObjectSource/ObjectSourceTabView.swift index 289b6e133e..67d6514169 100644 --- a/apple/TablePro/Views/ObjectSource/ObjectSourceTabView.swift +++ b/apple/TablePro/Views/ObjectSource/ObjectSourceTabView.swift @@ -2,7 +2,8 @@ // ObjectSourceTabView.swift // TablePro // -// Tab showing the source of one stored procedure, function, trigger, user-defined type or view. +// Tab showing the source of one stored procedure, function, trigger, user-defined type, view or +// driver-declared server object. // import Combine @@ -73,14 +74,12 @@ final class ObjectSourceLoader: ObservableObject { await load() } - /// One round trip. Re-listing the schema to recover the attributes cost a full catalog scan - /// per open and per reload, for values the sidebar's listing already carried into the ref. A - /// type is the exception: its definition and attributes are read fresh. + // One round trip, with the attributes the listing carried; a type and a server object read theirs fresh. private func fetch() async throws -> Fetched { let scope = DatabaseScope( connectionId: connectionId, database: objectRef.database, - schema: objectRef.schema + schema: objectRef.isDatabaseScoped ? nil : objectRef.schema ) return try await metadata.withMetadataDriver(scope: scope) { [objectRef] driver in switch objectRef.kind { @@ -105,6 +104,15 @@ final class ObjectSourceLoader: ObservableObject { throw PluginObjectSourceError.unsupported(objectRef.name) } return Fetched(source: source, attributes: fetched.attributes) + case .serverObject: + guard let object = objectRef.serverObject else { + throw PluginObjectSourceError.unsupported(objectRef.name) + } + let fetched = try await driver.fetchServerObject(object) + guard let source = fetched.definition, !source.isEmpty else { + throw PluginObjectSourceError.unsupported(objectRef.name) + } + return Fetched(source: source, attributes: fetched.attributes.isEmpty ? objectRef.attributes : fetched.attributes) case .view, .materializedView: let source = try await TableDDLComposer.fetchDDL( for: objectRef.name, diff --git a/apple/TablePro/Views/Results/ResultStatusBar.swift b/apple/TablePro/Views/Results/ResultStatusBar.swift index 205be5bc3f..dbea74b74b 100644 --- a/apple/TablePro/Views/Results/ResultStatusBar.swift +++ b/apple/TablePro/Views/Results/ResultStatusBar.swift @@ -3,6 +3,7 @@ // TablePro // +import AppKit import SwiftUI /// The window's bottom bar: what the result is, and the controls that change what of it you see. @@ -50,6 +51,7 @@ struct ResultStatusBar: View { let resultSetActions: ResultSetActions let onToggleFilters: () -> Void let onFetchAll: (() -> Void)? + var autoRefresh: StatusBarAutoRefreshState = .unavailable @State private var showColumnPopover = false @State private var showHighlightPopover = false @@ -290,6 +292,9 @@ struct ResultStatusBar: View { if model.controls.showsHighlightRules { highlightButton } + if model.controls.showsAutoRefresh, presentation.showsAutoRefresh { + autoRefreshMenu(presentation) + } if model.controls.showsFilters { filtersToggle(presentation) } @@ -402,6 +407,51 @@ struct ResultStatusBar: View { return String(format: String(localized: "%d rules"), count) } + // A Menu around an inline Picker: a pop-up button always spells out its choice, so it could not go to a glyph. + private func autoRefreshMenu(_ presentation: ResultStatusPresentation) -> some View { + Menu { + Picker( + String(localized: "Auto Refresh"), + selection: Binding(get: { autoRefresh.interval }, set: { autoRefresh.onChange($0) }) + ) { + ForEach(TableAutoRefreshInterval.allCases) { interval in + Text(interval.title).tag(interval) + } + } + .pickerStyle(.inline) + .labelsHidden() + } label: { + Label { + Text(autoRefresh.interval.shortTitle) + } icon: { + (Self.autoRefreshSymbol(isOn: autoRefresh.interval.isOn).map(Image.init(nsImage:)) + ?? Image(systemName: Self.autoRefreshSymbolName)) + .statusBarControlIcon(besideTitle: presentation.showsControlTitles) + } + } + .menuStyle(.button) + .menuIndicator(presentation.showsControlTitles ? .visible : .hidden) + .statusBarLabelStyle(showsTitle: presentation.showsControlTitles) + .controlSize(.small) + .fixedSize() + .help(autoRefresh.help) + .accessibilityLabel(String(localized: "Auto Refresh")) + .accessibilityValue(autoRefresh.interval.title) + .accessibilityIdentifier("result-status-auto-refresh") + } + + static let autoRefreshSymbolName = "clock.arrow.circlepath" + + // A menu draws a SwiftUI symbol as a template and drops its tint, so the on state is an accent palette image. + static func autoRefreshSymbol(isOn: Bool) -> NSImage? { + let size = NSImage.SymbolConfiguration(pointSize: NSFont.systemFontSize(for: .small), weight: .regular) + let configuration = isOn ? size.applying(NSImage.SymbolConfiguration(paletteColors: [.controlAccentColor])) : size + guard let image = NSImage(systemSymbolName: autoRefreshSymbolName, accessibilityDescription: nil)? + .withSymbolConfiguration(configuration) else { return nil } + image.isTemplate = !isOn + return image + } + private func filtersToggle(_ presentation: ResultStatusPresentation) -> some View { Toggle(isOn: Binding(get: { filterState.isVisible }, set: { _ in onToggleFilters() })) { Label { diff --git a/apple/TablePro/Views/Results/ResultStatusInputs.swift b/apple/TablePro/Views/Results/ResultStatusInputs.swift index af51da33e7..f0382654f5 100644 --- a/apple/TablePro/Views/Results/ResultStatusInputs.swift +++ b/apple/TablePro/Views/Results/ResultStatusInputs.swift @@ -34,6 +34,21 @@ struct StatusBarHighlightState { } } +struct StatusBarAutoRefreshState { + let interval: TableAutoRefreshInterval + let pause: TableAutoRefreshPause? + let onChange: (TableAutoRefreshInterval) -> Void + + static var unavailable: StatusBarAutoRefreshState { + StatusBarAutoRefreshState(interval: .off, pause: nil, onChange: { _ in }) + } + + var help: String { + if interval.isOn, let reason = pause?.help { return reason } + return String(format: String(localized: "Auto Refresh: %@"), interval.title) + } +} + struct StatusBarColumnState { let hidden: Set let columns: [GridColumnEntry] diff --git a/apple/TablePro/Views/Results/TextViewerWindowController.swift b/apple/TablePro/Views/Results/TextViewerWindowController.swift index bca006ed2c..b9a073d875 100644 --- a/apple/TablePro/Views/Results/TextViewerWindowController.swift +++ b/apple/TablePro/Views/Results/TextViewerWindowController.swift @@ -8,12 +8,13 @@ import SwiftUI @MainActor internal final class TextViewerWindowController: ValueViewerWindowController { + @discardableResult static func open( text: String?, columnName: String?, isEditable: Bool, onCommit: ((String) -> Void)? - ) { + ) -> TextViewerWindowController { let title: String if let columnName { title = String(format: String(localized: "Text: %@"), columnName) @@ -34,6 +35,7 @@ internal final class TextViewerWindowController: ValueViewerWindowController { onDismiss: dismiss ) } + return controller } } diff --git a/apple/TablePro/Views/RowInspector/InspectorPopOutRegistry.swift b/apple/TablePro/Views/RowInspector/InspectorPopOutRegistry.swift new file mode 100644 index 0000000000..b62bb99911 --- /dev/null +++ b/apple/TablePro/Views/RowInspector/InspectorPopOutRegistry.swift @@ -0,0 +1,32 @@ +// +// InspectorPopOutRegistry.swift +// TablePro +// + +import Foundation + +// An editable pop-out commits to the row ids it opened on, which a reload hands to other rows, so Auto Refresh waits on it. +@MainActor +internal enum InspectorPopOutRegistry { + private struct Entry { + weak var owner: TrailingPaneState? + weak var window: ValueViewerWindowController? + } + + private static var entries: [Entry] = [] + + // Weak both ways: the window controller lives exactly as long as its window is open. + internal static func record(_ window: ValueViewerWindowController, openedFrom owner: TrailingPaneState) { + prune() + entries.append(Entry(owner: owner, window: window)) + } + + internal static func holdsWindow(openedFrom owner: TrailingPaneState) -> Bool { + prune() + return entries.contains { $0.owner === owner } + } + + private static func prune() { + entries.removeAll { $0.owner == nil || $0.window == nil } + } +} diff --git a/apple/TablePro/Views/RowInspector/RowInspectorView.swift b/apple/TablePro/Views/RowInspector/RowInspectorView.swift index 112c74b661..988d7da378 100644 --- a/apple/TablePro/Views/RowInspector/RowInspectorView.swift +++ b/apple/TablePro/Views/RowInspector/RowInspectorView.swift @@ -149,12 +149,13 @@ internal struct RowInspectorView: View { switch InspectorPopOutWindow.resolve(for: kind) { case .json: - JSONViewerWindowController.open( + let window = JSONViewerWindowController.open( text: text, columnName: field.columnName, isEditable: isEditable, onCommit: commit ) + if commit != nil { InspectorPopOutRegistry.record(window, openedFrom: paneState) } case .php: PhpViewerWindowController.open(text: text, columnName: field.columnName) case .binary(let format): @@ -165,12 +166,13 @@ internal struct RowInspectorView: View { columnName: field.columnName ) case .text: - TextViewerWindowController.open( + let window = TextViewerWindowController.open( text: text, columnName: field.columnName, isEditable: isEditable, onCommit: commit ) + if commit != nil { InspectorPopOutRegistry.record(window, openedFrom: paneState) } } } } diff --git a/apple/TablePro/Views/Settings/Sections/HistorySection.swift b/apple/TablePro/Views/Settings/Sections/HistorySection.swift index addecec3d7..222f8a544f 100644 --- a/apple/TablePro/Views/Settings/Sections/HistorySection.swift +++ b/apple/TablePro/Views/Settings/Sections/HistorySection.swift @@ -35,7 +35,11 @@ struct HistorySection: View { let confirmed = await AlertHelper.confirmDestructive( title: String(localized: "Clear All History?"), message: String( - localized: "This will permanently delete all query history entries, and the saved changes kept for restoring. This action cannot be undone." + localized: """ + This will permanently delete all query history entries, the saved changes kept \ + for restoring, and the earlier versions of your query tabs. This action cannot \ + be undone. + """ ), confirmButton: String(localized: "Clear"), cancelButton: String(localized: "Cancel") @@ -44,6 +48,7 @@ struct HistorySection: View { Task { _ = await QueryHistoryManager.shared.clearEverything() _ = await QueryHistoryManager.shared.clearRewindSnapshots() + _ = await QueryTabVersionStorage.shared.removeAll() } } } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeCellView.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeCellView.swift index e30599e652..32fbe661df 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeCellView.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeCellView.swift @@ -40,6 +40,8 @@ final class DatabaseTreeCellView: RenamableSidebarCellView return metadata.isSystemDatabase ? "gearshape" : "cylinder" case .schema, .tableFolder: return "folder" + case .serverObject(let ref): + return ref.kind.iconName case .routine, .trigger, .userType, .status, .recentSection, .temporarySection, .foldersSection, .objectKindSection, .containerObjectKindSection, .hierarchicalSchemaSection, .redisKeysSection, .redisNode: return "tablecells" diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeDoubleClickIntent.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeDoubleClickIntent.swift index b1808d59ad..dae589edb4 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeDoubleClickIntent.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeDoubleClickIntent.swift @@ -23,9 +23,7 @@ internal enum DatabaseTreeDoubleClickIntent: Equatable { } internal enum DatabaseTreeDoubleClickResolver { - /// Being a table wins over being expandable. A partitioned table is both, and the HIG puts - /// disclosure on the triangle and the Right and Left arrows, so the row's own double-click is - /// free to mean what it means everywhere else: open the thing you clicked. + // Being a table wins over being expandable: the HIG puts disclosure on the triangle and the arrow keys. internal static func resolve(node: DatabaseTreeNode) -> DatabaseTreeDoubleClickIntent { if let ref = DatabaseTreeSelection.tableRef(of: node) { return .openPermanently(ref) @@ -37,6 +35,8 @@ internal enum DatabaseTreeDoubleClickResolver { return .openObjectSource(ref.objectRef) case .userType(let ref): return .openObjectSource(ref.objectRef) + case .serverObject(let ref): + return .openObjectSource(ref.objectRef) default: return node.isExpandable ? .toggleDisclosure : .ignore } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift index adbf4d9282..a280e732b0 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeFilter.swift @@ -37,6 +37,7 @@ struct DatabaseTreeBucketSource: Equatable { let routines: SameRows let triggers: SameRows let userTypes: SameRows + let serverObjects: [String: SameRows] let searchText: String let database: String? @@ -45,6 +46,7 @@ struct DatabaseTreeBucketSource: Equatable { routines: [RoutineInfo], triggers: [TriggerInfo], userTypes: [UserDefinedTypeInfo], + serverObjects: [String: [ServerObjectInfo]] = [:], searchText: String, database: String? ) { @@ -52,6 +54,7 @@ struct DatabaseTreeBucketSource: Equatable { self.routines = SameRows(rows: routines) self.triggers = SameRows(rows: triggers) self.userTypes = SameRows(rows: userTypes) + self.serverObjects = serverObjects.mapValues { SameRows(rows: $0) } self.searchText = searchText self.database = database } @@ -62,6 +65,7 @@ struct DatabaseTreeBucketSource: Equatable { routines: routines.rows, triggers: triggers.rows, userTypes: userTypes.rows, + serverObjects: serverObjects.mapValues(\.rows), searchText: searchText, database: database ) @@ -87,25 +91,27 @@ struct DatabaseTreeBucketMemo { } } -/// One filtered pass over a container's objects, split by kind. The container row needs the counts -/// and each group row under it needs one bucket, so both read this rather than filtering the whole -/// list again per group. +// One filtered pass per container, read by the container row and every group under it. struct DatabaseTreeObjectBuckets { let tables: [SidebarObjectKind: [TableInfo]] let routines: [SidebarObjectKind: [RoutineInfo]] let triggers: [TriggerInfo] let userTypes: [UserDefinedTypeInfo] + // Keyed by the declared kind's id. + let serverObjects: [String: [ServerObjectInfo]] init( tables: [SidebarObjectKind: [TableInfo]], routines: [SidebarObjectKind: [RoutineInfo]], triggers: [TriggerInfo], - userTypes: [UserDefinedTypeInfo] = [] + userTypes: [UserDefinedTypeInfo] = [], + serverObjects: [String: [ServerObjectInfo]] = [:] ) { self.tables = tables self.routines = routines self.triggers = triggers self.userTypes = userTypes + self.serverObjects = serverObjects } var isEmpty: Bool { @@ -113,8 +119,10 @@ struct DatabaseTreeObjectBuckets { && routines.values.allSatisfy(\.isEmpty) && triggers.isEmpty && userTypes.isEmpty + && serverObjects.values.allSatisfy(\.isEmpty) } + // A declared kind compares by id, so the key built here finds the declared descriptor. var itemCounts: [SidebarObjectKind: Int] { var counts = tables.mapValues(\.count) for (kind, list) in routines { @@ -124,6 +132,9 @@ struct DatabaseTreeObjectBuckets { if !userTypes.isEmpty { counts[.type, default: 0] += userTypes.count } + for (kindId, list) in serverObjects where !list.isEmpty { + counts[.server(SidebarServerObjectKind(id: kindId)), default: 0] += list.count + } return counts } } @@ -230,11 +241,21 @@ enum DatabaseTreeFilter { return deduplicated(matched, by: \.id) } + static func filteredServerObjects( + _ objects: [ServerObjectInfo], + searchText: String, + database: String? = nil + ) -> [ServerObjectInfo] { + let matched = SidebarSearch(searchText).ranked(objects, name: { $0.name }, database: database, schema: { $0.schema }) + return deduplicated(matched, by: \.id) + } + static func objectBuckets( tables: [TableInfo], routines: [RoutineInfo], triggers: [TriggerInfo], userTypes: [UserDefinedTypeInfo] = [], + serverObjects: [String: [ServerObjectInfo]] = [:], searchText: String, database: String? = nil ) -> DatabaseTreeObjectBuckets { @@ -250,7 +271,10 @@ enum DatabaseTreeFilter { tables: tableBuckets, routines: routineBuckets, triggers: filteredTriggers(triggers, searchText: searchText, database: database), - userTypes: filteredUserTypes(userTypes, searchText: searchText, database: database) + userTypes: filteredUserTypes(userTypes, searchText: searchText, database: database), + serverObjects: serverObjects.mapValues { + filteredServerObjects($0, searchText: searchText, database: database) + } ) } @@ -310,6 +334,7 @@ enum DatabaseTreeFilter { routines: service.routines(for: connectionId, schema: schema), triggers: service.triggers(for: connectionId, schema: schema), userTypes: service.userDefinedTypes(for: connectionId, schema: schema), + serverObjects: service.serverObjectsByKind(for: connectionId, schema: schema), searchText: searchText, database: database )), @@ -324,14 +349,14 @@ enum DatabaseTreeFilter { return kept } - /// A schema the search matched by name shows everything inside it. Filtering its objects by the - /// same query leaves the matched schema reporting no items. + // A schema matched by name shows everything inside it, or it would report no items. static func hierarchicalBucketSource( schema: String, tables: [TableInfo], routines: [RoutineInfo], triggers: [TriggerInfo], userTypes: [UserDefinedTypeInfo], + serverObjects: [String: [ServerObjectInfo]] = [:], searchText: String, database: String? = nil ) -> DatabaseTreeBucketSource { @@ -341,6 +366,7 @@ enum DatabaseTreeFilter { routines: routines, triggers: triggers, userTypes: userTypes, + serverObjects: serverObjects, searchText: matchesSchema ? "" : searchText, database: database ) @@ -557,11 +583,15 @@ enum DatabaseTreeFilter { !service.isTablesCurrent(connectionId: connectionId, database: database, schema: schema) { tables = listingMatches.rowsBySchema[schema] ?? [] } + let serverKinds = service.serverKinds(connectionId: connectionId, database: database, schema: schema) return objectBuckets( tables: tables, routines: service.routines(connectionId: connectionId, database: database, schema: schema), triggers: service.triggers(connectionId: connectionId, database: database, schema: schema), userTypes: service.userDefinedTypes(connectionId: connectionId, database: database, schema: schema), + serverObjects: Dictionary(uniqueKeysWithValues: serverKinds.map { kind in + (kind.id, service.serverObjects(connectionId: connectionId, database: database, schema: schema, kindId: kind.id)) + }), searchText: searchText, database: database ) diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift index 0a8aaa293c..a8957add7d 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeNode.swift @@ -5,16 +5,16 @@ import Foundation -/// Which groups a container shows. A group stands for objects the container holds, and a container -/// with nothing to list answers with its own status row instead. +// A group stands for objects the container holds; a container with nothing to list shows its own status row. internal enum DatabaseTreeObjectGroupResolver { internal static func groups( database: String, schema: String?, itemCounts: [SidebarObjectKind: Int], - keeping: Set = [] + keeping: Set = [], + declared: [SidebarServerObjectKind] = [] ) -> [DatabaseTreeObjectGroup] { - SidebarObjectKind.visible(itemCounts: itemCounts, keeping: keeping) + SidebarObjectKind.visible(itemCounts: itemCounts, keeping: keeping, declared: declared) .map { DatabaseTreeObjectGroup(database: database, schema: schema, kind: $0) } } } @@ -31,36 +31,48 @@ internal extension DatabaseTreeNode.Status { } } -/// Where each side kind's fetch stands for one container. A kind the engine does not declare has no -/// phase, because a fetch the engine never runs stays idle and would read as loading forever. +// An undeclared kind has no phase: a fetch the engine never runs stays idle and would read as loading forever. internal struct DatabaseTreeSidePhases: Equatable { let routines: MetadataLoadPhase let triggers: MetadataLoadPhase? let types: MetadataLoadPhase? + // Keyed by kind id, holding only the declared kinds this container lists. + let serverObjects: [String: MetadataLoadPhase] + + init( + routines: MetadataLoadPhase, + triggers: MetadataLoadPhase?, + types: MetadataLoadPhase?, + serverObjects: [String: MetadataLoadPhase] = [:] + ) { + self.routines = routines + self.triggers = triggers + self.types = types + self.serverObjects = serverObjects + } var all: [MetadataLoadPhase] { - [routines] + [triggers, types].compactMap { $0 } + [routines] + [triggers, types].compactMap { $0 } + serverObjects.keys.sorted().compactMap { serverObjects[$0] } } - func phase(for category: SidebarObjectCategory) -> MetadataLoadPhase { - switch category { + func phase(for kind: SidebarObjectKind) -> MetadataLoadPhase { + switch kind.category { case .table: return .loaded case .routine: return routines case .trigger: return triggers ?? .loaded case .type: return types ?? .loaded + case .server: return kind.serverKind.flatMap { serverObjects[$0.id] } ?? .loaded } } - /// The declared kinds whose fetch failed. Each keeps its group, empty of rows, so the failure shows - /// where the rows would be: hiding the group would make a list the server refused read as empty. + // A failed kind keeps its group, so a list the server refused never reads as empty. func failedKinds(among declared: Set) -> Set { - declared.filter { phase(for: $0.category).failureMessage != nil } + declared.filter { phase(for: $0).failureMessage != nil } } - /// The row a group with no rows shows: the failure, or nothing, since a group without rows is - /// listed only to show a failure or to take filed objects back out of their folders. + // A group without rows is listed only for a failure or to take filed objects back out of their folders. func placeholder(for kind: SidebarObjectKind) -> DatabaseTreeNode.Status? { - phase(for: kind.category).failureMessage.map(DatabaseTreeNode.Status.error) + phase(for: kind).failureMessage.map(DatabaseTreeNode.Status.error) } } @@ -94,6 +106,7 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { case routine(DatabaseTreeRoutineRef) case trigger(DatabaseTreeTriggerRef) case userType(DatabaseTreeUserTypeRef) + case serverObject(DatabaseTreeServerObjectRef) case status(Status) /// Flat shape: one collapsible section per object kind. @@ -128,7 +141,7 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { case .redisNode(let node): guard case .namespace = node else { return false } return true - case .recentTable, .temporaryTable, .routine, .trigger, .userType, .status: + case .recentTable, .temporaryTable, .routine, .trigger, .userType, .serverObject, .status: return false } } @@ -156,19 +169,14 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { return nil } - /// A source-list group row: chrome the app invented to bucket objects, not an object the - /// database has. AppKit draws these itself once `isGroupItem` says so, and it stops indenting - /// their children, which is what puts a table at the same depth as a database in the tree. - /// - /// A schema is deliberately not one. It is a real object with its own menu and its own - /// children, so it stays an ordinary container row the way a folder does in Xcode's navigator. + // Chrome the app invented to bucket objects; a schema is a real object, so it stays an ordinary container row. var isGroupRow: Bool { switch kind { case .recentSection, .temporarySection, .foldersSection, .objectKindSection, .redisKeysSection: return true case .database, .schema, .containerObjectKindSection, .tableFolder, .hierarchicalSchemaSection, .recentTable, .temporaryTable, .table, .partition, - .routine, .trigger, .userType, .status, .redisNode: + .routine, .trigger, .userType, .serverObject, .status, .redisNode: return false } } @@ -178,7 +186,7 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { case .database, .schema: return true case .recentSection, .recentTable, .temporarySection, .temporaryTable, .foldersSection, .tableFolder, - .table, .partition, .routine, .trigger, .userType, .status, .objectKindSection, + .table, .partition, .routine, .trigger, .userType, .serverObject, .status, .objectKindSection, .containerObjectKindSection, .hierarchicalSchemaSection, .redisKeysSection, .redisNode: return false } @@ -191,7 +199,7 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { case .schema(let database, let schema): return .schema(database: database, schema: schema, isSystem: systemSchemas.contains(schema)) case .recentSection, .recentTable, .temporarySection, .temporaryTable, .foldersSection, .tableFolder, - .table, .partition, .routine, .trigger, .userType, .status, .objectKindSection, + .table, .partition, .routine, .trigger, .userType, .serverObject, .status, .objectKindSection, .containerObjectKindSection, .hierarchicalSchemaSection, .redisKeysSection, .redisNode: return nil } @@ -256,6 +264,7 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { static func routineId(_ ref: DatabaseTreeRoutineRef) -> String { "routine\u{1}\(ref.id)" } static func triggerId(_ ref: DatabaseTreeTriggerRef) -> String { "trigger\u{1}\(ref.id)" } static func userTypeId(_ ref: DatabaseTreeUserTypeRef) -> String { "usertype\u{1}\(ref.id)" } + static func serverObjectId(_ ref: DatabaseTreeServerObjectRef) -> String { "serverobject\u{1}\(ref.id)" } static func statusId(parentId: String, status: Status) -> String { switch status { case .loading: return "\(parentId)\u{1}status.loading" @@ -265,9 +274,9 @@ final class DatabaseTreeNode: NSObject, SidebarOutlineNode { } } - static func objectKindSectionId(_ kind: SidebarObjectKind) -> String { "kindSection\u{1}\(kind.rawValue)" } + static func objectKindSectionId(_ kind: SidebarObjectKind) -> String { "kindSection\u{1}\(kind.persistenceName)" } static func containerObjectKindSectionId(_ group: DatabaseTreeObjectGroup) -> String { - "containerKindSection\u{1}\(group.database)\u{1}\(group.schema ?? "")\u{1}\(group.kind.rawValue)" + "containerKindSection\u{1}\(group.database)\u{1}\(group.schema ?? "")\u{1}\(group.kind.persistenceName)" } static func hierarchicalSchemaSectionId(_ schema: String) -> String { "hschema\u{1}\(schema)" } static let redisKeysSectionId = "redis-keys-section" diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Expansion.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Expansion.swift index c161f342db..e591b4ba23 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Expansion.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Expansion.swift @@ -87,6 +87,7 @@ extension DatabaseTreeOutlineCoordinator { restoreObjectGroupExpansion(under: schemaNode) } } + restoreObjectGroupExpansion(under: databaseNode) } } @@ -182,24 +183,21 @@ extension DatabaseTreeOutlineCoordinator { } } - /// The cascade only re-asserts disclosure the store already holds, so it runs behind the flag - /// `applyDesiredExpansion` uses. Recording each row it opens rewrote the whole expansion - /// snapshot to `UserDefaults`, and re-entered this restore once per row. + // Behind the `applyDesiredExpansion` flag: recording each row rewrote the expansion snapshot and re-entered this. internal func restoreDescendantExpansion(afterExpanding node: DatabaseTreeNode) { let wasApplyingExpansion = isApplyingExpansion isApplyingExpansion = true defer { isApplyingExpansion = wasApplyingExpansion } switch node.kind { - case .database where !supportsSchemaLevel: - restoreObjectGroupExpansion(under: node) - case .schema, .hierarchicalSchemaSection: + // A schema engine's database holds its `.database` kind groups beside the schemas. + case .database, .schema, .hierarchicalSchemaSection: restoreObjectGroupExpansion(under: node) case .foldersSection: restoreFolderExpansion(under: node) case .objectKindSection, .containerObjectKindSection, .tableFolder, .table, .partition: restorePartitionExpansion(under: node) - case .recentSection, .recentTable, .temporarySection, .temporaryTable, .database, .routine, .trigger, - .userType, .status, .redisKeysSection, .redisNode: + case .recentSection, .recentTable, .temporarySection, .temporaryTable, .routine, .trigger, + .userType, .serverObject, .status, .redisKeysSection, .redisNode: break } } @@ -257,7 +255,7 @@ extension DatabaseTreeOutlineCoordinator { case .partition(let ref): guard let tableRef = ref.tableRef else { break } recordTableExpansion(tableRef, expanded: expanded) - case .recentTable, .temporaryTable, .routine, .trigger, .userType, .status, .redisNode: + case .recentTable, .temporaryTable, .routine, .trigger, .userType, .serverObject, .status, .redisNode: break } } @@ -269,6 +267,7 @@ extension DatabaseTreeOutlineCoordinator { if service.schemaListNeedsFetch(connectionId: connectionId, database: metadata.name) { Task { await service.loadSchemas(connectionId: connectionId, database: metadata.name) } } + loadDatabaseServerObjects(database: metadata.name) loadExternalSchemaNames(database: metadata.name) } else { loadObjects(database: metadata.name, schema: nil) @@ -288,12 +287,18 @@ extension DatabaseTreeOutlineCoordinator { else { return } loadHierarchicalSchemaObjects(schema) case .recentSection, .recentTable, .temporarySection, .temporaryTable, .foldersSection, .tableFolder, - .routine, .trigger, .userType, .status, .objectKindSection, .containerObjectKindSection, + .routine, .trigger, .userType, .serverObject, .status, .objectKindSection, .containerObjectKindSection, .redisKeysSection, .redisNode: break } } + private func loadDatabaseServerObjects(database: String) { + guard service.serverObjectsNeedFetch(connectionId: connectionId, database: database, schema: nil) else { return } + let connectionId = connectionId + Task { await service.loadServerObjects(connectionId: connectionId, database: database, schema: nil) } + } + /// A schema the user has open keeps reading the lists a change overtook while a search runs; one /// the search opened shows the listing's tables instead. internal func isOpenedByUser(database: String, schema: String) -> Bool { diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+FolderNodes.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+FolderNodes.swift index 74b3fa8fa7..a7560cd73c 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+FolderNodes.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+FolderNodes.swift @@ -13,7 +13,7 @@ import TableProPluginKit /// its children. Either way an object sits in one row only, so a filed table is drawn inside its /// folder and left out of its kind section. extension DatabaseTreeOutlineCoordinator { - internal static let tableKinds = SidebarObjectKind.allCases.filter { $0.category == .table } + internal static let tableKinds = SidebarObjectKind.builtIn.filter { $0.category == .table } internal func folderScope(for container: TableFolderContainer) -> DatabaseScope? { switch container { diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Menu.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Menu.swift index d48854232f..b0bbb8a0b4 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Menu.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Menu.swift @@ -173,7 +173,7 @@ extension DatabaseTreeOutlineCoordinator: NSMenuDelegate { private func objectKindTitles() -> [SidebarObjectKind: String] { let tableEntityName = PluginManager.shared.tableEntityName(for: databaseType) var titles: [SidebarObjectKind: String] = [:] - for kind in SidebarObjectKind.allCases { + for kind in SidebarObjectKind.ordered(declared: declaredServerKinds) { titles[kind] = kind.title(tableEntityName: tableEntityName) } return titles diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Nodes.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Nodes.swift index 665e162416..191aa1ff86 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Nodes.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator+Nodes.swift @@ -58,7 +58,7 @@ extension DatabaseTreeOutlineCoordinator { return folderMemberNodes(of: ref) case .database(let metadata): return supportsSchemaLevel - ? schemaNodes(database: metadata.name) + ? databaseLevelNodes(database: metadata.name) : objectNodes(database: metadata.name, schema: nil) case .schema(let database, let schema): return objectNodes(database: database, schema: schema) @@ -78,7 +78,7 @@ extension DatabaseTreeOutlineCoordinator { return redisChildren(of: nil) case .redisNode(let redisNode): return redisChildren(of: redisNode) - case .recentTable, .temporaryTable, .routine, .trigger, .userType, .status: + case .recentTable, .temporaryTable, .routine, .trigger, .userType, .serverObject, .status: return [] } } @@ -208,16 +208,17 @@ extension DatabaseTreeOutlineCoordinator { .map { node(id: DatabaseTreeNode.schemaId(database: database, schema: $0), kind: .schema(database: database, schema: $0)) } } - /// A torn-down sidebar has no view model and lists nothing at all. Counting its way to a root of - /// failed sections would fill a pane that is on its way out. + // A torn-down sidebar has no view model and lists nothing, rather than a root of failed sections. private func visibleObjectKinds() -> [SidebarObjectKind] { guard viewModel != nil else { return [] } - let itemCounts = SidebarObjectKind.allCases.reduce(into: [SidebarObjectKind: Int]()) { + let declared = flatServerKinds + let itemCounts = SidebarObjectKind.ordered(declared: declared).reduce(into: [SidebarObjectKind: Int]()) { $0[$1] = flatItemCount(for: $1) } return SidebarObjectKind.visible( itemCounts: itemCounts, - keeping: flatPlan().filedKinds.union(flatSidePhases.failedKinds(among: declaredObjectKinds)) + keeping: flatPlan().filedKinds.union(flatSidePhases.failedKinds(among: declaredObjectKinds)), + declared: declared ) } @@ -228,10 +229,29 @@ extension DatabaseTreeOutlineCoordinator { return .emptyContainer(sideStates: flatSidePhases.all) } - /// What the engine says it fetches. It decides which fetches a container waits on before saying - /// it is empty, and which failures keep a section; it never lists a section on its own. + // Decides which fetches a container waits on and which failures keep a section; it never lists one on its own. internal var declaredObjectKinds: Set { - databaseType.declaredObjectKinds + databaseType.declaredObjectKinds.union(declaredServerKinds.map { SidebarObjectKind.server($0) }) + } + + // Asked of the live driver, which answers per server flavor and version. + internal var declaredServerKinds: [SidebarServerObjectKind] { + SidebarServerObjectKind.declared(by: DatabaseManager.shared.driver(for: connectionId)) + } + + // A hierarchical engine lists its kinds per schema, never in the browsed catalog. + internal var flatServerKinds: [SidebarServerObjectKind] { + PluginManager.shared.databaseGroupingStrategy(for: databaseType) == .hierarchicalSchema ? [] : declaredServerKinds + } + + private var containerServerKinds: [SidebarServerObjectKind] { + declaredServerKinds.filter { $0.scope == .container } + } + + // The tree reads the kinds of the container's own level; a schema-grouped shape lists `.container` kinds alone. + private func serverKinds(database: String, schema: String?) -> [SidebarServerObjectKind] { + if rootShape == .hierarchicalSchema { return containerServerKinds } + return service.serverKinds(connectionId: connectionId, database: database, schema: schema) } internal func flatItemCount(for kind: SidebarObjectKind) -> Int { @@ -245,6 +265,12 @@ extension DatabaseTreeOutlineCoordinator { return viewModel.filteredTriggers(from: schemaService.triggers(for: connectionId)).count case .type: return viewModel.filteredUserTypes(from: schemaService.userDefinedTypes(for: connectionId)).count + case .server: + guard let serverKind = kind.serverKind else { return 0 } + return viewModel.filteredServerObjects( + kindId: serverKind.id, + from: schemaService.serverObjects(for: connectionId, kindId: serverKind.id) + ).count } } @@ -258,14 +284,16 @@ extension DatabaseTreeOutlineCoordinator { return [statusNode(parentId: DatabaseTreeNode.objectKindSectionId(kind), status: placeholder)] } - /// Only the kinds the engine declares take part, the rule `sideLoadStates` follows for a tree - /// container, so a fetch the engine never runs cannot hold the root on a spinner. + // Declared kinds only, as `sideLoadStates`, so a fetch the engine never runs cannot hold the root on a spinner. private var flatSidePhases: DatabaseTreeSidePhases { let declared = declaredObjectKinds return DatabaseTreeSidePhases( routines: schemaService.routinesLoadState(for: connectionId).erased, triggers: declared.contains(.trigger) ? schemaService.triggersLoadState(for: connectionId).erased : nil, - types: declared.contains(.type) ? schemaService.userDefinedTypesLoadState(for: connectionId).erased : nil + types: declared.contains(.type) ? schemaService.userDefinedTypesLoadState(for: connectionId).erased : nil, + serverObjects: Dictionary(uniqueKeysWithValues: flatServerKinds.map { kind in + (kind.id, schemaService.serverObjectsLoadState(for: connectionId, kindId: kind.id).erased) + }) ) } @@ -295,6 +323,13 @@ extension DatabaseTreeOutlineCoordinator { let ref = DatabaseTreeUserTypeRef(database: database, schema: type.schema, type: type) return node(id: DatabaseTreeNode.userTypeId(ref), kind: .userType(ref)) } + case .server: + guard let serverKind = kind.serverKind else { return [] } + let objects = viewModel.filteredServerObjects( + kindId: serverKind.id, + from: schemaService.serverObjects(for: connectionId, kindId: serverKind.id) + ) + return serverObjectNodes(objects, kind: serverKind, database: database, schema: { $0.schema }) } } @@ -414,7 +449,10 @@ extension DatabaseTreeOutlineCoordinator { : nil, types: declared.contains(.type) ? schemaService.userDefinedTypesLoadState(for: connectionId, schema: schema).erased - : nil + : nil, + serverObjects: Dictionary(uniqueKeysWithValues: containerServerKinds.map { kind in + (kind.id, schemaService.serverObjectsLoadState(for: connectionId, schema: schema, kindId: kind.id).erased) + }) ) } @@ -435,6 +473,9 @@ extension DatabaseTreeOutlineCoordinator { routines: shown(schemaService.routines(for: connectionId, schema: schema)), triggers: shown(schemaService.triggers(for: connectionId, schema: schema)), userTypes: shown(schemaService.userDefinedTypes(for: connectionId, schema: schema)), + serverObjects: Dictionary(uniqueKeysWithValues: containerServerKinds.map { kind in + (kind.id, shown(schemaService.serverObjects(for: connectionId, schema: schema, kindId: kind.id))) + }), searchText: searchText, database: browsingDatabase ) @@ -536,6 +577,30 @@ extension DatabaseTreeOutlineCoordinator { DatabaseTreeMetadataService.listsTablesPerSchema(PluginManager.shared.databaseGroupingStrategy(for: databaseType)) } + // A schema engine's database lists its schemas, then the kinds it declares for the whole database. + private func databaseLevelNodes(database: String) -> [DatabaseTreeNode] { + let schemas = schemaNodes(database: database) + let groups = databaseScopedGroupNodes(database: database) + guard !groups.isEmpty else { return schemas } + if schemas.count == 1, case .status(.empty) = schemas[0].kind { return groups } + return schemas + groups + } + + private func databaseScopedGroupNodes(database: String) -> [DatabaseTreeNode] { + let kinds = serverKinds(database: database, schema: nil) + guard !kinds.isEmpty else { return [] } + let phases = sideLoadStates(database: database, schema: nil) + return DatabaseTreeObjectGroupResolver.groups( + database: database, + schema: nil, + itemCounts: objectBuckets(database: database, schema: nil).itemCounts, + keeping: phases.failedKinds(among: Set(kinds.map { SidebarObjectKind.server($0) })), + declared: kinds + ) + .filter { $0.kind.category == .server } + .map { node(id: DatabaseTreeNode.containerObjectKindSectionId($0), kind: .containerObjectKindSection($0)) } + } + private func schemaNodes(database: String) -> [DatabaseTreeNode] { let parentId = DatabaseTreeNode.databaseId(database) switch service.schemaListState(connectionId: connectionId, database: database) { @@ -632,6 +697,14 @@ extension DatabaseTreeOutlineCoordinator { func shown(_ items: [Item], isCurrent: Bool) -> [Item] { showsOvertaken || isCurrent ? items : [] } + let serverObjects = Dictionary(uniqueKeysWithValues: serverKinds(database: database, schema: schema).map { kind in + (kind.id, shown( + service.serverObjects(connectionId: connectionId, database: database, schema: schema, kindId: kind.id), + isCurrent: service.isServerObjectsCurrent( + connectionId: connectionId, database: database, schema: schema, kindId: kind.id + ) + )) + }) return DatabaseTreeBucketSource( tables: tables, routines: shown( @@ -646,6 +719,7 @@ extension DatabaseTreeOutlineCoordinator { service.userDefinedTypes(connectionId: connectionId, database: database, schema: schema), isCurrent: service.isTypesCurrent(connectionId: connectionId, database: database, schema: schema) ), + serverObjects: serverObjects, searchText: searchText, database: database ) @@ -666,9 +740,7 @@ extension DatabaseTreeOutlineCoordinator { return objectBuckets(database: database, schema: schema) } - /// A fetch the engine never runs stays idle for good, and idle is not loaded: counting it - /// would hold every empty container on a spinner for a list that is never coming. So only the - /// kinds this engine declares take part in deciding between "empty" and "loading". + // Declared kinds only: an idle fetch the engine never runs would hold every empty container on a spinner. private func sideLoadStates(database: String, schema: String?) -> DatabaseTreeSidePhases { let declared = declaredObjectKinds return DatabaseTreeSidePhases( @@ -678,7 +750,12 @@ extension DatabaseTreeOutlineCoordinator { : nil, types: declared.contains(.type) ? service.typesLoadState(connectionId: connectionId, database: database, schema: schema).erased - : nil + : nil, + serverObjects: Dictionary(uniqueKeysWithValues: serverKinds(database: database, schema: schema).map { kind in + (kind.id, service.serverObjectsLoadState( + connectionId: connectionId, database: database, schema: schema, kindId: kind.id + ).erased) + }) ) } @@ -712,7 +789,8 @@ extension DatabaseTreeOutlineCoordinator { database: database, schema: schema, itemCounts: looseItemCounts(buckets.itemCounts, plan: plan), - keeping: plan.filedKinds.union(sidePhases.failedKinds(among: declaredObjectKinds)) + keeping: plan.filedKinds.union(sidePhases.failedKinds(among: declaredObjectKinds)), + declared: serverKinds(database: database, schema: schema) ) guard !groups.isEmpty || !folders.isEmpty else { return [statusNode(parentId: parentId, status: .emptyContainer(sideStates: sidePhases.all))] @@ -728,8 +806,7 @@ extension DatabaseTreeOutlineCoordinator { private func containerObjectNodes(for group: DatabaseTreeObjectGroup) -> [DatabaseTreeNode] { let buckets = objectBuckets(database: group.database, schema: group.schema) let emptyId = DatabaseTreeNode.containerObjectKindSectionId(group) - /// A schema-grouped group carries no database, and a reference naming an empty one would - /// switch the session to a database called "" the moment the row opened. + // An empty database name would switch the session to a database called "" the moment the row opened. let database: String? = group.database.isEmpty ? nil : group.database let placeholder = sidePhases(for: group).placeholder(for: group.kind).map { [statusNode(parentId: emptyId, status: $0)] @@ -756,6 +833,25 @@ extension DatabaseTreeOutlineCoordinator { let ref = DatabaseTreeUserTypeRef(database: database, schema: group.schema, type: type) return node(id: DatabaseTreeNode.userTypeId(ref), kind: .userType(ref)) } + case .server: + guard let serverKind = group.kind.serverKind else { return placeholder } + let objects = buckets.serverObjects[serverKind.id] ?? [] + guard !objects.isEmpty else { return placeholder } + // A `.database` kind's object keeps its own schema, which is not the group's. + let fixedSchema = group.schema + return serverObjectNodes(objects, kind: serverKind, database: database, schema: { fixedSchema ?? $0.schema }) + } + } + + private func serverObjectNodes( + _ objects: [ServerObjectInfo], + kind: SidebarServerObjectKind, + database: String?, + schema: (ServerObjectInfo) -> String? + ) -> [DatabaseTreeNode] { + objects.map { object in + let ref = DatabaseTreeServerObjectRef(database: database, schema: schema(object), kind: kind, object: object) + return node(id: DatabaseTreeNode.serverObjectId(ref), kind: .serverObject(ref)) } } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift index c5afbe0e81..7c40f92912 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeOutlineCoordinator.swift @@ -561,7 +561,14 @@ final class DatabaseTreeOutlineCoordinator: NSObject, NSTextFieldDelegate { private func refreshDatabase(_ database: String) { if supportsSchemaLevel { - Task { await service.refreshSchemas(connectionId: connectionId, database: database) } + let connectionId = connectionId + Task { + async let schemas: Void = service.refreshSchemas(connectionId: connectionId, database: database) + async let serverObjects: Void = service.refreshServerObjects( + connectionId: connectionId, database: database, schema: nil + ) + _ = await (schemas, serverObjects) + } } else { Task { await service.refreshObjects(connectionId: connectionId, database: database, schema: nil) } } @@ -648,8 +655,7 @@ final class DatabaseTreeOutlineCoordinator: NSObject, NSTextFieldDelegate { } } - /// A table kind is refreshed as a catalog change, as Cmd+R is, so the partition lists the tree - /// holds under the section are marked stale along with the browsed list. + // A table kind refreshes as a catalog change, as Cmd+R does, so the partition lists under it go stale too. internal func refreshObjectKind(_ kind: SidebarObjectKind) { guard let mainCoordinator else { return } switch kind { @@ -662,6 +668,18 @@ final class DatabaseTreeOutlineCoordinator: NSObject, NSTextFieldDelegate { database: mainCoordinator.browseDatabaseName, kinds: .tables ))) + case .server(let serverKind): + let connectionId = connectionId + let manager = DatabaseManager.shared + let scope = manager.browseScope(for: connectionId) + let kindId = serverKind.id + Task { [schemaService] in + try? await manager.withBrowseMetadataDriver(connectionId: connectionId) { driver in + _ = await schemaService.reloadServerObjects( + connectionId: connectionId, driver: driver, scope: scope, kindId: kindId + ) + } + } } } @@ -697,6 +715,13 @@ final class DatabaseTreeOutlineCoordinator: NSObject, NSTextFieldDelegate { database: group.database, schema: group.schema ) + case .server: + await service.refreshServerObjects( + connectionId: connectionId, + database: group.database, + schema: group.schema, + kindId: group.kind.serverKind?.id + ) } } } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeRenderInputs.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeRenderInputs.swift index 6dbbfc1048..b7c9f74179 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeRenderInputs.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeRenderInputs.swift @@ -10,6 +10,7 @@ import Foundation struct DatabaseTreeRenderInputs: Equatable { typealias DatabaseKey = DatabaseTreeMetadataService.DatabaseKey typealias ObjectsKey = DatabaseTreeMetadataService.ObjectsKey + typealias ServerObjectsKey = DatabaseTreeMetadataService.ServerObjectsKey struct Rows: Equatable { let phase: MetadataLoadPhase @@ -77,7 +78,9 @@ struct DatabaseTreeRenderInputs: Equatable { let routines: [ObjectsKey: Rows] let triggers: [ObjectsKey: Rows] let types: [ObjectsKey: Rows] + let serverObjects: [ServerObjectsKey: Rows] let currency: [ObjectsKey: Currency] + let serverObjectsCurrency: [ServerObjectsKey: Bool] let partitions: [DatabaseTreeMetadataService.PartitionsKey: Rows] let listings: [DatabaseKey: Listing] let listsRevision: Int @@ -87,6 +90,7 @@ struct DatabaseTreeRenderInputs: Equatable { let catalogRoutines: Rows let catalogTriggers: Rows let catalogTypes: Rows + let catalogServerObjects: [String: Rows] let temporaryTables: [TemporaryTableStore.Key: TemporaryTables] let sidebar: Sidebar? @@ -105,6 +109,15 @@ struct DatabaseTreeRenderInputs: Equatable { let routines = Self.slice(treeMetadata.routinesState, where: ownsObjects) { Rows($0) } let triggers = Self.slice(treeMetadata.triggersState, where: ownsObjects) { Rows($0) } let types = Self.slice(treeMetadata.typesState, where: ownsObjects) { Rows($0) } + let serverObjects = Self.slice(treeMetadata.serverObjectsState, where: { $0.connectionId == connectionId }) { + Rows($0) + } + var serverObjectsCurrency: [ServerObjectsKey: Bool] = [:] + for key in serverObjects.keys { + serverObjectsCurrency[key] = treeMetadata.isServerObjectsCurrent( + connectionId: connectionId, database: key.database, schema: key.schema, kindId: key.kindId + ) + } var currency: [ObjectsKey: Currency] = [:] for key in Set(tables.keys).union(routines.keys).union(triggers.keys).union(types.keys) { currency[key] = Currency( @@ -131,7 +144,9 @@ struct DatabaseTreeRenderInputs: Equatable { self.routines = routines self.triggers = triggers self.types = types + self.serverObjects = serverObjects self.currency = currency + self.serverObjectsCurrency = serverObjectsCurrency self.partitions = Self.slice(treeMetadata.partitionsState, where: { $0.connectionId == connectionId }) { Rows($0) } self.listings = listings self.listsRevision = treeMetadata.listsRevision(for: connectionId) @@ -140,6 +155,7 @@ struct DatabaseTreeRenderInputs: Equatable { self.catalogRoutines = Rows(schemaService.routinesLoadState(for: connectionId)) self.catalogTriggers = Rows(schemaService.triggersLoadState(for: connectionId)) self.catalogTypes = Rows(schemaService.userDefinedTypesLoadState(for: connectionId)) + self.catalogServerObjects = schemaService.serverObjectStates(for: connectionId).mapValues { Rows($0) } self.temporaryTables = Self.slice(temporaryTables.listings, where: { $0.connectionId == connectionId }) { TemporaryTables(tables: SameRows(rows: $0.tables), session: $0.session) } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeRowView.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeRowView.swift index ea0b5d864a..85da448667 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeRowView.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeRowView.swift @@ -125,6 +125,8 @@ struct DatabaseTreeRowView: View { TriggerRowView(trigger: ref.trigger) case .userType(let ref): UserTypeRowView(type: ref.type) + case .serverObject(let ref): + ServerObjectRowView(object: ref.object, kind: ref.kind) case .status(let status): statusRow(status) case .objectKindSection(let kind): diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeSelection.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeSelection.swift index 85fa896b1f..a85dc4ca84 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeSelection.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeSelection.swift @@ -18,7 +18,7 @@ internal enum DatabaseTreeSelection { case .status, .recentSection, .temporarySection, .foldersSection, .objectKindSection, .redisKeysSection: return false case .database, .schema, .hierarchicalSchemaSection, .table, .partition, .routine, .trigger, .userType, - .recentTable, .temporaryTable, .containerObjectKindSection, .tableFolder, .redisNode: + .serverObject, .recentTable, .temporaryTable, .containerObjectKindSection, .tableFolder, .redisNode: return true } } diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeTypeSelect.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeTypeSelect.swift index 55f3ed21e8..2e1bd67a02 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeTypeSelect.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeTypeSelect.swift @@ -72,6 +72,8 @@ internal enum DatabaseTreeTypeSelect { return ref.trigger.name case .userType(let ref): return ref.type.name + case .serverObject(let ref): + return ref.object.name case .hierarchicalSchemaSection(let schema): return schema case .redisNode(let node): diff --git a/apple/TablePro/Views/Sidebar/DatabaseTreeView.swift b/apple/TablePro/Views/Sidebar/DatabaseTreeView.swift index e1aaf317a4..01010cd35d 100644 --- a/apple/TablePro/Views/Sidebar/DatabaseTreeView.swift +++ b/apple/TablePro/Views/Sidebar/DatabaseTreeView.swift @@ -48,6 +48,21 @@ struct DatabaseTreeUserTypeRef: Identifiable, Equatable { } } +struct DatabaseTreeServerObjectRef: Identifiable, Equatable { + let database: String? + let schema: String? + let kind: SidebarServerObjectKind + let object: ServerObjectInfo + + var id: String { + IdentityPath.joined([database ?? "", schema ?? "", object.id], separator: "|") + } + + var objectRef: DatabaseObjectRef { + DatabaseObjectRef(serverObject: object, kind: kind, database: database ?? "") + } +} + struct DatabaseTreeView: View { @ObservedObject private var databaseManager = DatabaseManager.shared @ObservedObject private var treeService = DatabaseTreeMetadataService.shared diff --git a/apple/TablePro/Views/Sidebar/Menu/DatabaseTreeMenuSpec.swift b/apple/TablePro/Views/Sidebar/Menu/DatabaseTreeMenuSpec.swift index 39d1569256..3318610d63 100644 --- a/apple/TablePro/Views/Sidebar/Menu/DatabaseTreeMenuSpec.swift +++ b/apple/TablePro/Views/Sidebar/Menu/DatabaseTreeMenuSpec.swift @@ -7,12 +7,7 @@ import Foundation import TableProPluginKit internal enum DatabaseTreeMenuSpec { - /// A menu is a list of groups, and every group here is one intent: reach the object, note it, - /// move its data, change it. The HIG asks for a small number of items in about three groups and - /// for commands relevant to the clicked object only, so nothing connection-wide belongs on a - /// row. View Options used to be appended to every menu including every object row, and View ER - /// Diagram sat in the middle of a table row's clipboard and export commands; both now live on - /// the empty-area menu, where the thing they act on is the sidebar rather than an object. + // One intent per group, and only commands for the clicked object; connection-wide ones live on the empty-area menu. internal static func sections(for context: DatabaseTreeMenuContext) -> [DatabaseTreeMenuSection] { guard let clicked = context.clicked else { return backgroundSections(context) } switch clicked { @@ -46,6 +41,8 @@ internal enum DatabaseTreeMenuSpec { return triggerSections(ref) case .userType(let ref): return userTypeSections(ref, context: context) + case .serverObject(let ref): + return serverObjectSections(ref) case .objectKindSection(let kind): return objectKindSections(kind, context: context) case .containerObjectKindSection(let group): @@ -336,6 +333,14 @@ internal enum DatabaseTreeMenuSpec { ] } + // List plus DDL only: the row offers nothing that writes. + private static func serverObjectSections(_ ref: DatabaseTreeServerObjectRef) -> [DatabaseTreeMenuSection] { + [ + DatabaseTreeMenuSection([.command(String(localized: "Copy Name"), .copyText(ref.object.name))]), + DatabaseTreeMenuSection([.command(String(localized: "Show DDL"), .showObjectSource(ref.objectRef))]) + ] + } + private static func userTypeSections( _ ref: DatabaseTreeUserTypeRef, context: DatabaseTreeMenuContext diff --git a/apple/TablePro/Views/Sidebar/ServerObjectRowView.swift b/apple/TablePro/Views/Sidebar/ServerObjectRowView.swift new file mode 100644 index 0000000000..cd9b583400 --- /dev/null +++ b/apple/TablePro/Views/Sidebar/ServerObjectRowView.swift @@ -0,0 +1,79 @@ +// +// ServerObjectRowView.swift +// TablePro +// + +import SwiftUI + +enum ServerObjectRowLogic { + static func displayName(for object: ServerObjectInfo, kind: SidebarServerObjectKind) -> String { + kind.scope == .database ? object.name : IdentityPath.qualified(name: object.name, schema: object.schema) + } + + static func accessibilityLabel(for object: ServerObjectInfo, kind: SidebarServerObjectKind) -> String { + let base = String(format: String(localized: "%1$@: %2$@"), kind.displayName, object.name) + guard let detail = object.detail, !detail.isEmpty else { return base } + return base + ", " + detail + } + + // The driver states the object's state in its attributes, in its own words, so it is not repeated here. + static func tooltip(for object: ServerObjectInfo, kind: SidebarServerObjectKind) -> String { + var lines: [String] = [displayName(for: object, kind: kind)] + if let detail = object.detail, !detail.isEmpty { lines.append(detail) } + lines.append(contentsOf: object.attributes.map { "\($0.label): \($0.value)" }) + return lines.joined(separator: "\n") + } +} + +// Plain drawn values: `ServerObjectInfo` compares by id, so a refreshed version or state would read as unchanged. +struct ServerObjectRowView: View { + @ObservedObject private var settingsManager = AppSettingsManager.shared + let name: String + let detail: String? + let iconName: String + let tooltip: String + let accessibilityLabel: String + + init(name: String, detail: String?, iconName: String, tooltip: String, accessibilityLabel: String) { + self.name = name + self.detail = detail + self.iconName = iconName + self.tooltip = tooltip + self.accessibilityLabel = accessibilityLabel + } + + init(object: ServerObjectInfo, kind: SidebarServerObjectKind) { + self.init( + name: object.name, + detail: object.detail, + iconName: kind.iconName, + tooltip: ServerObjectRowLogic.tooltip(for: object, kind: kind), + accessibilityLabel: ServerObjectRowLogic.accessibilityLabel(for: object, kind: kind) + ) + } + + var body: some View { + Label { + HStack(spacing: 6) { + Text(name) + .lineLimit(1) + .truncationMode(.tail) + if let detail, !detail.isEmpty { + Text(detail) + .font(.caption) + .foregroundStyle(.secondary) + .lineLimit(1) + .truncationMode(.tail) + } + } + } icon: { + Image(systemName: iconName) + .selectionAwareTint(Color.accentColor) + .frame(width: 16) + } + .sidebarRowIcon(visible: settingsManager.general.showObjectIcons) + .accessibilityElement(children: .combine) + .accessibilityLabel(accessibilityLabel) + .help(tooltip) + } +} diff --git a/apple/TablePro/Views/Sidebar/SidebarPersistenceKey.swift b/apple/TablePro/Views/Sidebar/SidebarPersistenceKey.swift index 98ac303f63..17d3e1d637 100644 --- a/apple/TablePro/Views/Sidebar/SidebarPersistenceKey.swift +++ b/apple/TablePro/Views/Sidebar/SidebarPersistenceKey.swift @@ -42,12 +42,17 @@ enum SidebarPersistenceKey { "sidebar.layout.\(connectionId.uuidString)" } + // Built-in kinds only; a declared kind's state lives under `serverKindsExpanded`. static func expanded(connectionId: UUID, kind: SidebarObjectKind) -> String { - "sidebar.\(connectionId.uuidString).\(kind.rawValue).expanded" + "sidebar.\(connectionId.uuidString).\(kind.persistenceName).expanded" } - /// Every per-connection key this type can produce. A key added above and forgotten here outlives - /// the connection it belongs to for the life of the install. + // One key for every declared kind, since their ids are open-ended and `all` has to name each key. + static func serverKindsExpanded(connectionId: UUID) -> String { + "sidebar.\(connectionId.uuidString).serverKinds.expanded" + } + + // A key missing here outlives its connection for the life of the install. static func all(connectionId: UUID) -> [String] { [ tablesExpanded(connectionId: connectionId), @@ -58,8 +63,9 @@ enum SidebarPersistenceKey { selectedTab(connectionId: connectionId), selectedFavorite(connectionId: connectionId), favoriteDatabaseEnvironmentFilter(connectionId: connectionId), - layout(connectionId: connectionId) - ] + SidebarObjectKind.allCases.map { expanded(connectionId: connectionId, kind: $0) } + layout(connectionId: connectionId), + serverKindsExpanded(connectionId: connectionId) + ] + SidebarObjectKind.builtIn.map { expanded(connectionId: connectionId, kind: $0) } } static func removeAll( diff --git a/apple/TablePro/Views/Sidebar/SidebarView.swift b/apple/TablePro/Views/Sidebar/SidebarView.swift index ec55eab1ab..df0d5d2914 100644 --- a/apple/TablePro/Views/Sidebar/SidebarView.swift +++ b/apple/TablePro/Views/Sidebar/SidebarView.swift @@ -47,7 +47,8 @@ struct SidebarView: View { } private var hasAnyMatch: Bool { - SidebarObjectKind.allCases.contains { kind in + let declared = SidebarServerObjectKind.declared(by: DatabaseManager.shared.driver(for: connectionId)) + return SidebarObjectKind.ordered(declared: declared).contains { kind in countFor(kind: kind) > 0 } || !otherSchemaMatches.isEmpty || !attachedSchemaMatches.isEmpty || hasTemporaryMatch } @@ -493,6 +494,12 @@ struct SidebarView: View { case .routine: return viewModel.filteredRoutines(of: kind, from: routines).count case .trigger: return viewModel.filteredTriggers(from: triggers).count case .type: return viewModel.filteredUserTypes(from: userDefinedTypes).count + case .server: + guard let serverKind = kind.serverKind else { return 0 } + return viewModel.filteredServerObjects( + kindId: serverKind.id, + from: schemaService.serverObjects(for: connectionId, kindId: serverKind.id) + ).count } } } diff --git a/apple/TablePro/Views/Structure/StructureRebuildPlanRunner.swift b/apple/TablePro/Views/Structure/StructureRebuildPlanRunner.swift index 4524bc9ac7..8eaa7cd55f 100644 --- a/apple/TablePro/Views/Structure/StructureRebuildPlanRunner.swift +++ b/apple/TablePro/Views/Structure/StructureRebuildPlanRunner.swift @@ -146,14 +146,12 @@ enum StructureRebuildPlanRunner { throw RunError.schemaChanged } - /// Only the transaction this plan opened is ever rolled back, and none is opened over a - /// transaction the session already holds: on the same shared session an app-owned - /// `COMMIT` commits the user's pending work, and MySQL's `START TRANSACTION` commits it - /// implicitly. A plan that cannot open one falls to its own compensation statements, - /// which is what an engine whose DDL commits as it runs already relies on. + // Never opens a transaction over one the session holds; a plan that cannot open one falls to its compensation statements. + // `.automatic`: the epilogue runs inside the transaction, where SQLite ignores `PRAGMA foreign_keys = on`. let owner = WriteTransactionOwner.resolve( supportsTransactions: driver.supportsTransactions, - sessionState: await driver.heldSessionTransactionState() + sessionState: await driver.heldSessionTransactionState(), + commitMode: .automatic ) // Inside a transaction SQLite ignores `PRAGMA foreign_keys = off`, so the rebuild's DROP TABLE cascades. if !plan.prologue.isEmpty, plan.isTransactional, owner == .session { @@ -164,7 +162,7 @@ enum StructureRebuildPlanRunner { _ = try? await driver.execute(query: sql) } - let usesTransaction = plan.isTransactional && owner.opensTransaction + let usesTransaction = plan.isTransactional && owner.sendsBegin && owner.commitsAtEnd if usesTransaction { try await driver.beginTransaction(mode: .readWrite) } diff --git a/apple/TablePro/Views/VersionHistory/VersionHistoryFactory.swift b/apple/TablePro/Views/VersionHistory/VersionHistoryFactory.swift index cd08b4b162..17070e411d 100644 --- a/apple/TablePro/Views/VersionHistory/VersionHistoryFactory.swift +++ b/apple/TablePro/Views/VersionHistory/VersionHistoryFactory.swift @@ -36,6 +36,15 @@ internal enum VersionHistoryFactory { confirmButton: String(localized: "Restore") ) } + case .queryTab(let id): + viewModel = VersionHistoryViewModel( + subject: subject, + provider: MainContentCoordinator.hostedQueryTabVersionProvider(for: id), + refreshSignal: AppEvents.shared.queryTabVersionsDidUpdate + .filter { $0 == nil || $0 == id } + .map { _ in () } + .eraseToAnyPublisher() + ) } viewModel.reportRestoreFailure = { message in AlertHelper.showErrorSheet( diff --git a/apple/TablePro/Views/VersionHistory/VersionHistoryFormatting.swift b/apple/TablePro/Views/VersionHistory/VersionHistoryFormatting.swift index 4663c05bcc..d1855d56b7 100644 --- a/apple/TablePro/Views/VersionHistory/VersionHistoryFormatting.swift +++ b/apple/TablePro/Views/VersionHistory/VersionHistoryFormatting.swift @@ -71,9 +71,22 @@ internal enum VersionHistoryFormatting { return String(format: String(localized: "Keeps the last %d versions on this Mac."), count) case .showsLatestCommits(let count): return String(format: String(localized: "Showing the %d most recent commits."), count) + case .keepsRecentVersions(let count, let days): + return String( + format: String(localized: "Keeps up to %1$d versions from the last %2$d days on this Mac."), + count, + days + ) } } + // The Revert To menu lists only past versions, so each row leads with its date. + static func menuTitle(for entry: VersionHistoryEntry) -> String { + guard let date = dateText(for: entry) else { return title(for: entry) } + guard let summary = entry.summary, !summary.isBlank else { return date } + return String(format: String(localized: "%1$@ (%2$@)"), date, summary) + } + private static func dateText(for entry: VersionHistoryEntry) -> String? { entry.date?.formatted(date: .abbreviated, time: .shortened) } diff --git a/apple/TableProTests/Core/DataGrid/GridViewportResolverTests.swift b/apple/TableProTests/Core/DataGrid/GridViewportResolverTests.swift index 13773fdddc..f14023d787 100644 --- a/apple/TableProTests/Core/DataGrid/GridViewportResolverTests.swift +++ b/apple/TableProTests/Core/DataGrid/GridViewportResolverTests.swift @@ -28,18 +28,53 @@ struct GridViewportResolverTests { selectedRows: [Int] = [], keyColumns: [String] = keyColumns, isCellModified: (RowID, Int) -> Bool = { _, _ in false } + ) -> GridViewportSnapshot { + let order = DataGridRowOrder(filteredIDs: displayIDs, revision: 0) + let selection = selectedRows.isEmpty + ? GridSelectionSnapshot.empty + : GridSelectionSnapshot( + rowIDs: selectedRows.compactMap { order.rowID(at: $0, in: tableRows) }, + rectangles: [], + activeCell: nil, + anchor: nil + ) + return snapshot( + of: tableRows, + displayIDs: displayIDs, + firstVisibleRow: firstVisibleRow, + offset: offset, + selection: selection, + keyColumns: keyColumns, + isCellModified: isCellModified + ) + } + + private static func snapshot( + of tableRows: TableRows, + displayIDs: [RowID]? = nil, + firstVisibleRow: Int, + offset: CGFloat = 0, + selection: GridSelectionSnapshot, + keyColumns: [String] = keyColumns, + isCellModified: (RowID, Int) -> Bool = { _, _ in false } ) -> GridViewportSnapshot { GridViewportResolver.snapshot( of: tableRows, displayIDs: displayIDs, firstVisibleDisplayRow: firstVisibleRow, firstVisibleOffset: offset, - selectedDisplayRows: selectedRows, + selection: selection, keyColumns: keyColumns, isCellModified: isCellModified ) } + private static let nameColumn = DataGridColumnID(name: "name", occurrence: 0) + + private static func cell(_ row: Int) -> GridSelectionSnapshot.Cell { + GridSelectionSnapshot.Cell(rowID: .existing(row), columnID: nameColumn) + } + private static func placement( _ intent: GridReloadIntent, from snapshot: GridViewportSnapshot, @@ -259,6 +294,49 @@ struct GridViewportResolverTests { #expect(Self.placement(.firstRow, from: snapshot, in: Self.rows(ids: [7, 7, 8])).selectedRows.isEmpty) } + @Test("A re-read keeps a cell cursor a cursor, on its own column, wherever its row lands") + func keepPlaceKeepsTheCursorAndItsColumn() { + var cursor = GridSelectionSnapshot(rowIDs: [.existing(2)], rectangles: [], activeCell: Self.cell(2), anchor: Self.cell(2)) + cursor.mode = .cursor + let snapshot = Self.snapshot(of: Self.rows(ids: Array(1 ... 10)), firstVisibleRow: 0, selection: cursor) + + let placement = Self.placement(.keepPlace, from: snapshot, in: Self.rows(ids: Array((1 ... 10).reversed()))) + + #expect(placement.selection.mode == .cursor) + #expect(placement.selection.rowIDs == [.existing(7)]) + #expect(placement.selection.activeCell == Self.cell(7)) + } + + @Test("A re-read keeps a cell range's columns rather than widening it to whole rows") + func keepPlaceKeepsACellRange() { + let idColumn = DataGridColumnID(name: "id", occurrence: 0) + let range = GridSelectionSnapshot( + rowIDs: [.existing(3), .existing(4)], + rectangles: [GridSelectionSnapshot.Rectangle(rowIDs: [.existing(3), .existing(4)], columnIDs: [Self.nameColumn])], + activeCell: Self.cell(3), + anchor: Self.cell(3) + ) + let snapshot = Self.snapshot(of: Self.rows(ids: Array(1 ... 10)), firstVisibleRow: 0, selection: range) + + let placement = Self.placement(.keepPlace, from: snapshot, in: Self.rows(ids: Array(0 ... 10))) + + #expect(placement.selection.mode == .cells) + #expect(placement.selection.rectangles == [ + GridSelectionSnapshot.Rectangle(rowIDs: [.existing(4), .existing(5)], columnIDs: [Self.nameColumn]) + ]) + #expect(placement.selection.activeCell == Self.cell(4)) + #expect(!placement.selection.rectangles.contains { $0.columnIDs.contains(idColumn) }) + } + + @Test("A cursor whose row is gone leaves nothing selected") + func cursorOnAGoneRowSelectsNothing() { + var cursor = GridSelectionSnapshot(rowIDs: [.existing(5)], rectangles: [], activeCell: Self.cell(5), anchor: Self.cell(5)) + cursor.mode = .cursor + let snapshot = Self.snapshot(of: Self.rows(ids: Array(1 ... 10)), firstVisibleRow: 0, selection: cursor) + + #expect(Self.placement(.keepPlace, from: snapshot, in: Self.rows(ids: [1, 2, 3])).selection == .empty) + } + @Test("Without a key the position is read through the incoming display order") func keepPlaceFallbackUsesTheDisplayOrder() { let snapshot = Self.snapshot(of: Self.rows(ids: Array(1 ... 100)), firstVisibleRow: 2, offset: 4, keyColumns: []) diff --git a/apple/TableProTests/Core/DataWrite/DataWriteExecutorLookupTests.swift b/apple/TableProTests/Core/DataWrite/DataWriteExecutorLookupTests.swift index cc8ad67a43..853f9acf5e 100644 --- a/apple/TableProTests/Core/DataWrite/DataWriteExecutorLookupTests.swift +++ b/apple/TableProTests/Core/DataWrite/DataWriteExecutorLookupTests.swift @@ -83,7 +83,7 @@ struct DataWriteExecutorLookupTests { steps: steps ) let driver = PluginDriverAdapter(connection: TestFixtures.makeConnection(type: .clickhouse), pluginDriver: lookup) - _ = try await DataWriteExecutor.run(plan, on: driver) + _ = try await DataWriteExecutor.run(plan, commitMode: .automatic, on: driver) } @Test("A write runs on the rows its lookup found, their values bound after its own") diff --git a/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift b/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift index 5234fead9b..9d386a97cd 100644 --- a/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift +++ b/apple/TableProTests/Core/DataWrite/DataWriteExecutorTests.swift @@ -114,7 +114,7 @@ struct DataWriteExecutorTests { @Test("The driver's real affected-row count reaches the caller") func reportsRealRowCount() async throws { let counting = CountingDriver(affectedRows: 42) - let results = try await DataWriteExecutor.run(plan(expectedRowCount: 50), on: driver(counting)).results + let results = try await DataWriteExecutor.run(plan(expectedRowCount: 50), commitMode: .automatic, on: driver(counting)).results #expect(results.first?.rowsAffected == 42) #expect(counting.didCommit) @@ -124,7 +124,7 @@ struct DataWriteExecutorTests { func tooManyRowsRollsBack() async throws { let counting = CountingDriver(affectedRows: 2) await #expect(throws: DataWriteError.tooManyRowsAffected(table: "t", expected: 1, actual: 2)) { - try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)) + try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)) } #expect(counting.didRollBack) #expect(counting.didCommit == false) @@ -136,7 +136,7 @@ struct DataWriteExecutorTests { await #expect( throws: DataWriteError.tooManyRowsAffectedUnrecoverable(table: "t", expected: 1, actual: 2) ) { - try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)) + try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)) } #expect(counting.didRollBack == false) } @@ -144,7 +144,7 @@ struct DataWriteExecutorTests { @Test("Fewer rows than expected is an ordinary save, because MySQL reports zero for an unchanged value") func fewerRowsIsNotAFailure() async throws { let counting = CountingDriver(affectedRows: 0) - let results = try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)).results + let results = try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)).results #expect(results.first?.rowsAffected == 0) #expect(counting.didCommit) @@ -157,7 +157,7 @@ struct DataWriteExecutorTests { await #expect(throws: DataWriteError.rowsNoLongerMatch(table: "t", expected: 1, actual: 0)) { try await DataWriteExecutor.run( plan(expectedRowCount: 1, matchesRowsWithoutKey: true), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) } #expect(counting.didRollBack) @@ -171,7 +171,7 @@ struct DataWriteExecutorTests { await #expect(throws: DataWriteError.keyNoLongerMatchesOneRow(table: "t", expected: 1, actual: 0)) { try await DataWriteExecutor.run( plan(expectedRowCount: 1, refusesSharedKey: true), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) } #expect(counting.didRollBack) @@ -184,7 +184,7 @@ struct DataWriteExecutorTests { await #expect(throws: DataWriteError.rowsNoLongerMatch(table: "t", expected: 3, actual: 2)) { try await DataWriteExecutor.run( plan(expectedRowCount: 3, matchesRowsWithoutKey: true), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) } #expect(counting.didRollBack) @@ -196,7 +196,7 @@ struct DataWriteExecutorTests { await #expect(throws: DataWriteError.rowsNoLongerMatch(table: "t", expected: 1, actual: 0)) { try await DataWriteExecutor.run( plan(expectedRowCount: 1, matchesRowsWithoutKey: true, databaseType: .kingbasees), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) } #expect(counting.didRollBack) @@ -207,7 +207,7 @@ struct DataWriteExecutorTests { let counting = CountingDriver(affectedRows: 0) let results = try await DataWriteExecutor.run( plan(expectedRowCount: 1, matchesRowsWithoutKey: true, databaseType: .clickhouse), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ).results #expect(results.first?.rowsAffected == 0) @@ -224,7 +224,7 @@ struct DataWriteExecutorTests { prologue: ["PRAGMA foreign_keys = OFF"], epilogue: ["PRAGMA foreign_keys = ON"] ), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) #expect(counting.trace == [ @@ -248,7 +248,7 @@ struct DataWriteExecutorTests { prologue: ["PRAGMA foreign_keys = OFF"], epilogue: ["PRAGMA foreign_keys = ON"] ), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) } @@ -269,7 +269,7 @@ struct DataWriteExecutorTests { prologue: ["SET session_replication_role = replica"], epilogue: ["SET session_replication_role = origin"] ), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) #expect(!counting.trace.contains("open preserve")) @@ -279,7 +279,7 @@ struct DataWriteExecutorTests { @Test("A plan without a prologue leaves the session's settings alone") func planWithoutPrologueOpensNoScope() async throws { let counting = CountingDriver(affectedRows: 1) - _ = try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)) + _ = try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)) #expect(counting.trace == ["BEGIN", "UPDATE \"t\" SET \"b\" = 0", "COMMIT"]) } @@ -296,7 +296,7 @@ struct DataWriteExecutorTests { prologueIsRequired: true ) await #expect(throws: (any Error).self) { - try await DataWriteExecutor.run(required, on: driver(counting)) + try await DataWriteExecutor.run(required, commitMode: .automatic, on: driver(counting)) } #expect(counting.trace == [ @@ -312,7 +312,7 @@ struct DataWriteExecutorTests { func anOptionalPrologueThatFailsLetsTheStepsRun() async throws { let counting = CountingDriver(affectedRows: 1, failOnStatement: 1) _ = try await DataWriteExecutor.run( - plan(expectedRowCount: 1, prologue: ["PRAGMA foreign_keys = OFF"]), on: driver(counting) + plan(expectedRowCount: 1, prologue: ["PRAGMA foreign_keys = OFF"]), commitMode: .automatic, on: driver(counting) ) #expect(counting.didCommit) @@ -323,7 +323,7 @@ struct DataWriteExecutorTests { let counting = CountingDriver(affectedRows: 1) let run = try await DataWriteExecutor.run( plan(expectedRowCount: 1, prologue: ["A"], epilogue: ["B"]), - on: driver(counting) + commitMode: .automatic, on: driver(counting) ) #expect(run.sideStatements == ["A", "B"]) @@ -335,7 +335,7 @@ struct DataWriteExecutorTests { do { _ = try await DataWriteExecutor.run( - plan(expectedRowCount: 1, statementCount: 3), on: driver(counting) + plan(expectedRowCount: 1, statementCount: 3), commitMode: .automatic, on: driver(counting) ) Issue.record("expected the run to throw") } catch let error as DataWritePartialCommitError { @@ -350,7 +350,7 @@ struct DataWriteExecutorTests { do { _ = try await DataWriteExecutor.run( - plan(expectedRowCount: 1, statementCount: 3), on: driver(counting) + plan(expectedRowCount: 1, statementCount: 3), commitMode: .automatic, on: driver(counting) ) Issue.record("expected the run to throw") } catch is DataWritePartialCommitError { @@ -368,7 +368,7 @@ struct DataWriteExecutorTests { do { _ = try await DataWriteExecutor.run( - plan(expectedRowCount: 1, statementCount: 3), on: driver(counting) + plan(expectedRowCount: 1, statementCount: 3), commitMode: .automatic, on: driver(counting) ) Issue.record("expected the run to throw") } catch let error as DataWritePartialCommitError { @@ -379,7 +379,7 @@ struct DataWriteExecutorTests { @Test("A statement whose row count carries no meaning is not held to one") func unverifiedStepIsNotChecked() async throws { let counting = CountingDriver(affectedRows: 9_999) - let results = try await DataWriteExecutor.run(plan(expectedRowCount: nil), on: driver(counting)).results + let results = try await DataWriteExecutor.run(plan(expectedRowCount: nil), commitMode: .automatic, on: driver(counting)).results #expect(results.first?.wasVerified == false) #expect(counting.didCommit) @@ -391,7 +391,7 @@ struct DataWriteExecutorTests { ) func saveJoinsTheSessionTransaction(state: PluginSessionTransactionState) async throws { let counting = CountingDriver(affectedRows: 1, sessionState: state) - let results = try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)).results + let results = try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)).results #expect(results.first?.rowsAffected == 1) #expect(counting.trace == ["UPDATE \"t\" SET \"b\" = 0"]) @@ -401,7 +401,7 @@ struct DataWriteExecutorTests { func saveWrapsWhenNothingIsOpen() async throws { for state in [PluginSessionTransactionState.idle, .unknown] { let counting = CountingDriver(affectedRows: 1, sessionState: state) - _ = try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)) + _ = try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)) #expect(counting.trace.first == "BEGIN") #expect(counting.didCommit) } @@ -413,7 +413,7 @@ struct DataWriteExecutorTests { do { _ = try await DataWriteExecutor.run( - plan(expectedRowCount: 1, statementCount: 3), on: driver(counting) + plan(expectedRowCount: 1, statementCount: 3), commitMode: .automatic, on: driver(counting) ) Issue.record("expected the run to throw") } catch let error as DataWritePartialCommitError { @@ -430,7 +430,7 @@ struct DataWriteExecutorTests { await #expect( throws: DataWriteError.tooManyRowsAffectedInSessionTransaction(table: "t", expected: 1, actual: 2) ) { - try await DataWriteExecutor.run(plan(expectedRowCount: 1), on: driver(counting)) + try await DataWriteExecutor.run(plan(expectedRowCount: 1), commitMode: .automatic, on: driver(counting)) } #expect(counting.didRollBack == false) } @@ -442,10 +442,12 @@ struct WriteTransactionOwnerTests { for state in [ PluginSessionTransactionState.idle, .inTransaction, .abortedTransaction, .holdsSessionLocks, .unknown, ] { - let owner = WriteTransactionOwner.resolve(supportsTransactions: false, sessionState: state) - #expect(owner == WriteTransactionOwner.none) - #expect(owner.opensTransaction == false) - #expect(owner.canRollBack == false) + for mode in ConnectionCommitMode.allCases { + let owner = WriteTransactionOwner.resolve(supportsTransactions: false, sessionState: state, commitMode: mode) + #expect(owner == WriteTransactionOwner.none) + #expect(owner.sendsBegin == false) + #expect(owner.canRollBack == false) + } } } @@ -454,19 +456,128 @@ struct WriteTransactionOwnerTests { arguments: [PluginSessionTransactionState.inTransaction, .abortedTransaction, .holdsSessionLocks] ) func sessionOwnsWhatItHolds(state: PluginSessionTransactionState) { - let owner = WriteTransactionOwner.resolve(supportsTransactions: true, sessionState: state) - #expect(owner == .session) - #expect(owner.opensTransaction == false) - #expect(owner.canRollBack == false) + for mode in ConnectionCommitMode.allCases { + let owner = WriteTransactionOwner.resolve(supportsTransactions: true, sessionState: state, commitMode: mode) + #expect(owner == .session) + #expect(owner.sendsBegin == false) + #expect(owner.canRollBack == false) + } } @Test("The app owns the transaction when nothing is open, and when the driver cannot say") func appOwnsTheRest() { for state in [PluginSessionTransactionState.idle, .unknown] { - let owner = WriteTransactionOwner.resolve(supportsTransactions: true, sessionState: state) + let owner = WriteTransactionOwner.resolve(supportsTransactions: true, sessionState: state, commitMode: .automatic) #expect(owner == .app) - #expect(owner.opensTransaction) + #expect(owner.sendsBegin) + #expect(owner.commitsAtEnd) #expect(owner.canRollBack) } } + + @Test("Manual Commit holds an idle session's write: BEGIN, no COMMIT, a rollback on failure") + func manualCommitHoldsAnIdleSession() { + let owner = WriteTransactionOwner.resolve(supportsTransactions: true, sessionState: .idle, commitMode: .manual) + #expect(owner == .held) + #expect(owner.sendsBegin) + #expect(owner.commitsAtEnd == false) + #expect(owner.canRollBack) + #expect(WriteTransactionOwner.resolve(supportsTransactions: true, sessionState: .unknown, commitMode: .manual) == .app) + } +} + +struct DataWriteExecutorHeldTests { + private func plan(prologue: [String] = []) -> DataWritePlan { + DataWritePlan( + scope: DatabaseScope(connectionId: UUID(), database: "shop", schema: nil), + databaseType: .sqlite, + steps: [ + DataWriteStep( + kind: .rowWrite, + statement: ParameterizedStatement(sql: "UPDATE \"t\" SET \"b\" = 1", parameters: []), + expectedRowCount: 1, + tableName: "t" + ), + ], + prologue: prologue + ) + } + + private func driver(_ counting: CountingDriver) -> DatabaseDriver { + PluginDriverAdapter(connection: TestFixtures.makeConnection(type: .sqlite), pluginDriver: counting) + } + + @Test("A held save sends BEGIN and the write, and leaves the transaction open") + func heldSaveLeavesTheTransactionOpen() async throws { + let counting = CountingDriver(affectedRows: 1) + _ = try await DataWriteExecutor.run(plan(), commitMode: .manual, on: driver(counting)) + #expect(counting.trace == ["BEGIN", "UPDATE \"t\" SET \"b\" = 1"]) + } + + @Test("A held save that fails rolls back the transaction it opened") + func heldSaveRollsBackAFailure() async { + let counting = CountingDriver(affectedRows: 1, failOnStatement: 1) + await #expect(throws: (any Error).self) { + try await DataWriteExecutor.run(plan(), commitMode: .manual, on: driver(counting)) + } + #expect(counting.trace.first == "BEGIN") + #expect(counting.didRollBack) + #expect(counting.didCommit == false) + } + + @Test("A save with session toggles is refused in Manual Commit before anything runs") + func toggledSaveIsRefused() async { + let counting = CountingDriver(affectedRows: 1) + await #expect(throws: ManualCommitSessionOptionsRefusal()) { + try await DataWriteExecutor.run( + plan(prologue: ["PRAGMA foreign_keys = OFF"]), commitMode: .manual, on: driver(counting) + ) + } + #expect(counting.trace.isEmpty) + } + + @Test( + "A save the session can't hold is refused in Manual Commit before anything runs", + arguments: [PluginSessionTransactionState.unknown, .holdsSessionLocks] + ) + func unholdableSaveIsRefused(state: PluginSessionTransactionState) async { + let counting = CountingDriver(affectedRows: 1, sessionState: state) + await #expect(throws: ManualCommitRefusal()) { + try await DataWriteExecutor.run(plan(), commitMode: .manual, on: driver(counting)) + } + #expect(counting.trace.isEmpty) + } + + @Test("A save on an engine without transactions is refused in Manual Commit") + func saveWithoutTransactionsIsRefused() async { + let counting = CountingDriver(affectedRows: 1, transactional: false) + await #expect(throws: ManualCommitRefusal()) { + try await DataWriteExecutor.run(plan(), commitMode: .manual, on: driver(counting)) + } + #expect(counting.trace.isEmpty) + } + + @Test("A held save marks the turn's hold, and a rolled-back one clears it") + func heldSaveMarksTheTurn() async throws { + let held = ManualCommitTurnHold() + _ = try await ManualCommit.$turnHold.withValue(held) { + try await DataWriteExecutor.run(plan(), commitMode: .manual, on: driver(CountingDriver(affectedRows: 1))) + } + #expect(held.isHeld) + + let failed = ManualCommitTurnHold() + await ManualCommit.$turnHold.withValue(failed) { + _ = try? await DataWriteExecutor.run( + plan(), commitMode: .manual, on: driver(CountingDriver(affectedRows: 1, failOnStatement: 1)) + ) + } + #expect(!failed.isHeld) + } + + @Test("Manual Commit joins a transaction already open and sends no BEGIN") + func heldSaveJoinsAnOpenTransaction() async throws { + let counting = CountingDriver(affectedRows: 1, sessionState: .inTransaction) + _ = try await DataWriteExecutor.run(plan(), commitMode: .manual, on: driver(counting)) + #expect(counting.trace == ["UPDATE \"t\" SET \"b\" = 1"]) + } } diff --git a/apple/TableProTests/Core/Database/DatabaseAccessBridgeScriptTests.swift b/apple/TableProTests/Core/Database/DatabaseAccessBridgeScriptTests.swift index 6b6d12a5d8..2d630470e2 100644 --- a/apple/TableProTests/Core/Database/DatabaseAccessBridgeScriptTests.swift +++ b/apple/TableProTests/Core/Database/DatabaseAccessBridgeScriptTests.swift @@ -53,12 +53,14 @@ struct DatabaseAccessBridgeScriptTests { private func makeDriver( sendsBatchesWhole: Bool = true, transactionState: PluginSessionTransactionState = .idle, + transactionStateAfterBatches: PluginSessionTransactionState? = nil, answer: @escaping @Sendable (String) -> QueryBatchResult = { _ in .empty } ) -> ScriptAnsweringDriver { ScriptAnsweringDriver( connection: TestFixtures.makeConnection(database: "warehouse", type: .mssql), sendsBatchesWhole: sendsBatchesWhole, transactionState: transactionState, + transactionStateAfterBatches: transactionStateAfterBatches, answer: answer ) } @@ -134,7 +136,8 @@ struct DatabaseAccessBridgeScriptTests { @Test("Rows the script changed and a transaction it left open are reported on the first result") func scriptTotalsAndNoticesAreReported() async throws { let notice = try #require(PluginSessionTransactionState.inTransaction.openTransactionNotice) - let driver = makeDriver(transactionState: .inTransaction) { _ in + // Idle before the script, as a session is; a BEGIN TRAN over an open transaction is refused. + let driver = makeDriver(transactionStateAfterBatches: .inTransaction) { _ in ScriptAnsweringDriver.batch( [ScriptAnsweringDriver.resultSet(columns: ["id"], rows: [["1"]], isTruncated: true)], rowsAffected: 3 diff --git a/apple/TableProTests/Core/Database/SchemaChangeHeldTransactionTests.swift b/apple/TableProTests/Core/Database/SchemaChangeHeldTransactionTests.swift new file mode 100644 index 0000000000..e3b682aabd --- /dev/null +++ b/apple/TableProTests/Core/Database/SchemaChangeHeldTransactionTests.swift @@ -0,0 +1,162 @@ +// +// SchemaChangeHeldTransactionTests.swift +// TableProTests +// +// On the session driver an app-owned BEGIN joins what a tab left open and its COMMIT ends it, so a +// schema change there runs inside that transaction. On a pooled connection it can wait on the +// transaction's locks, so it asks first. +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +private final class HeldDDLDriver: PluginDriverStub, @unchecked Sendable { + private let lock = NSLock() + private var current: PluginSessionTransactionState = .idle + private var sent: [String] = [] + + var supportsSchemas: Bool { false } + var currentSchema: String? { nil } + var serverVersion: String? { nil } + var supportsTransactions: Bool { true } + var supportsTransactionalDDL: Bool { true } + + var state: PluginSessionTransactionState { + get { lock.withLock { current } } + set { lock.withLock { current = newValue } } + } + + var statements: [String] { lock.withLock { sent } } + + func execute(query: String) async throws -> PluginQueryResult { + lock.withLock { + sent.append(query) + switch query.uppercased() { + case "BEGIN": current = .inTransaction + case "COMMIT", "ROLLBACK": current = .idle + default: break + } + } + return .empty + } + + func sessionTransactionState() async -> PluginSessionTransactionState { state } + + func switchDatabase(to database: String) async throws {} +} + +@Suite("Schema changes beside an open transaction", .serialized) +@MainActor +struct SchemaChangeHeldTransactionTests { + private static let first = "ALTER TABLE orders ADD COLUMN a INT;" + private static let second = "ALTER TABLE orders ADD COLUMN b INT;" + + private static func injectSession(type: DatabaseType) -> (DatabaseConnection, HeldDDLDriver, DatabaseDriver) { + let connection = TestFixtures.makeConnection(database: "orders", type: type) + let driver = HeldDDLDriver() + let adapter = PluginDriverAdapter(connection: connection, pluginDriver: driver) + var session = ConnectionSession(connection: connection, driver: adapter) + session.browseDatabase = "orders" + session.status = .connected + DatabaseManager.shared.injectSession(session, for: connection.id) + return (connection, driver, adapter) + } + + private static func tearDown(_ connection: DatabaseConnection) { + MetadataConnectionPool.shared.closeAll(connectionId: connection.id) + DatabaseManager.shared.removeSession(for: connection.id) + } + + private static func scope(_ connection: DatabaseConnection) -> DatabaseScope { + DatabaseScope(connectionId: connection.id, database: "orders", schema: nil) + } + + private static func script() -> SchemaChangeScript { + SchemaChangeScript( + tableName: "orders", + statements: [first, second].map { SchemaStatement(sql: $0, description: $0, isDestructive: false) }, + operations: [], + review: PluginSchemaChangeReview() + ) + } + + @Test("On the session driver a Structure save inside an open transaction joins it") + func sessionRouteSaveJoinsTheOpenTransaction() async throws { + let (connection, driver, _) = Self.injectSession(type: .duckdb) + defer { Self.tearDown(connection) } + driver.state = .inTransaction + + try await DatabaseManager.shared.executeSchemaChanges( + Self.script(), databaseType: .duckdb, scope: Self.scope(connection), gate: AlwaysAllowGate() + ) + + #expect(driver.statements == [Self.first, Self.second]) + #expect(driver.state == .inTransaction) + } + + @Test("On the session driver an idle Structure save keeps its own transaction") + func sessionRouteIdleSaveWraps() async throws { + let (connection, driver, _) = Self.injectSession(type: .duckdb) + defer { Self.tearDown(connection) } + + try await DatabaseManager.shared.executeSchemaChanges( + Self.script(), databaseType: .duckdb, scope: Self.scope(connection), gate: AlwaysAllowGate() + ) + + #expect(driver.statements == ["BEGIN", Self.first, Self.second, "COMMIT"]) + } + + @Test("On the session driver Create Table inside an open transaction joins it") + func sessionRouteCreateTableJoins() async throws { + let (connection, driver, _) = Self.injectSession(type: .duckdb) + defer { Self.tearDown(connection) } + driver.state = .inTransaction + + try await DatabaseManager.shared.executeCreateTable( + statements: [Self.first, Self.second], databaseType: .duckdb, scope: Self.scope(connection), + gate: AlwaysAllowGate() + ) + + #expect(driver.statements == [Self.first, Self.second]) + } + + @Test("A trigger edit that joins an open transaction sends no BEGIN or COMMIT") + func joinedTriggerEditSendsNoTransaction() async throws { + let connection = TestFixtures.makeConnection(type: .duckdb) + let driver = HeldDDLDriver() + let adapter = PluginDriverAdapter(connection: connection, pluginDriver: driver) + + try await TriggerEditing.runInTransaction(driver: adapter, dropSQL: "DROP TRIGGER t", sql: "CREATE TRIGGER t", wraps: false) + + #expect(driver.statements == ["DROP TRIGGER t", "CREATE TRIGGER t"]) + } + + @Test("A pooled save beside an open transaction asks first, and a Cancel runs nothing") + func pooledSaveAsksBesideAHold() async throws { + let (connection, _, sessionAdapter) = Self.injectSession(type: .mysql) + defer { Self.tearDown(connection) } + let pooled = HeldDDLDriver() + let pooledAdapter = PluginDriverAdapter(connection: connection, pluginDriver: pooled) + try await pooledAdapter.connect() + MetadataConnectionPool.shared.injectEntry(pooledAdapter, scope: Self.scope(connection)) + DatabaseManager.shared.sessionTransactions.record( + .inTransaction, on: sessionAdapter, for: .init(connectionId: connection.id, lane: nil) + ) + + await #expect(throws: ExecutionGateError.self) { + try await DatabaseManager.shared.executeSchemaChanges( + Self.script(), databaseType: .mysql, scope: Self.scope(connection), gate: AlwaysAllowGate(), + holdConfirmation: { false } + ) + } + #expect(pooled.statements.isEmpty) + + try await DatabaseManager.shared.executeSchemaChanges( + Self.script(), databaseType: .mysql, scope: Self.scope(connection), gate: AlwaysAllowGate(), + holdConfirmation: { true } + ) + #expect(pooled.statements == ["BEGIN", Self.first, Self.second, "COMMIT"]) + } +} diff --git a/apple/TableProTests/Core/Database/SessionLanesTests.swift b/apple/TableProTests/Core/Database/SessionLanesTests.swift index 0baef7ec88..ad7404e4b5 100644 --- a/apple/TableProTests/Core/Database/SessionLanesTests.swift +++ b/apple/TableProTests/Core/Database/SessionLanesTests.swift @@ -886,6 +886,32 @@ struct SessionLanesTests { #expect(holding.isEmpty) } + @Test("A transaction the tracker holds is named even while its connection runs a turn") + func trackedHoldOnABusyConnectionIsNamed() async throws { + let harness = makeHarness() + defer { cleanUp(harness) } + DatabaseManager.shared.sessionTransactions.record( + .inTransaction, on: harness.home, for: .init(connectionId: harness.connection.id, lane: "app") + ) + let release = AsyncGate() + let acquired = AsyncGate() + let holder = Task { @MainActor in + try await DatabaseManager.shared.sessionDriverGate.withExclusiveAccess( + SessionDriverGate.Key(connectionId: harness.connection.id, database: "app") + ) { + await acquired.open() + await release.wait() + } + } + await acquired.wait() + + let holding = await DatabaseManager.shared.databasesHoldingTransaction(for: harness.connection.id) + + await release.open() + try await holder.value + #expect(holding == ["app"]) + } + @Test("A connection that does not answer counts as holding nothing once the bound passes") func silentConnectionIsBounded() async { let harness = makeHarness() diff --git a/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift b/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift index fcf531d914..784e91df77 100644 --- a/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift +++ b/apple/TableProTests/Core/Database/SessionTransactionTrackerTests.swift @@ -16,9 +16,12 @@ import Testing @MainActor struct SessionTransactionTrackerTests { private static func seed( - database: String = "app" + database: String = "app", + type: DatabaseType = .mysql, + commitMode: ConnectionCommitMode = .automatic ) -> (connection: DatabaseConnection, driver: TransactionScriptedDriver) { - let connection = TestFixtures.makeConnection(database: database, type: .mysql) + var connection = TestFixtures.makeConnection(database: database, type: type) + connection.commitMode = commitMode let driver = TransactionScriptedDriver() var session = ConnectionSession( connection: connection, @@ -41,6 +44,30 @@ struct SessionTransactionTrackerTests { } } + // The editor's single-statement path: the hold is opened inside the same turn as the write. + private static func runInMode( + _ mode: ConnectionCommitMode, + _ sql: String, + on connectionId: UUID, + type: DatabaseType = .postgresql + ) async throws { + let scope = DatabaseScope(connectionId: connectionId, database: "app", schema: nil) + _ = try await DatabaseManager.shared.withScopedDriver( + scope: scope, + route: .sessionDriver, + cancellation: .protectedWrite + ) { driver in + _ = try await ManualCommit.openIfNeeded( + on: driver, statements: [sql], databaseType: type, grammar: type.lexicalGrammar, commitMode: mode + ) + return try await driver.execute(query: sql) + } + } + + private static func scope(_ connectionId: UUID) -> DatabaseScope { + DatabaseScope(connectionId: connectionId, database: "app", schema: nil) + } + private static var tracker: SessionTransactionTracker { DatabaseManager.shared.sessionTransactions } private static func states(_ connectionId: UUID, in tracker: SessionTransactionTracker? = nil) -> [SessionTransactionTracker.State] { @@ -239,6 +266,392 @@ struct SessionTransactionTrackerTests { #expect(tracker.holdsTransaction(connectionId: connectionId, container: "hr")) } + @Test("In Manual Commit an UPDATE sends BEGIN first and leaves the transaction open") + func manualCommitOpensTheHold() async throws { + let (connection, driver) = Self.seed(type: .postgresql) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + try await Self.runInMode(.manual, "UPDATE t SET a = 1", on: connection.id) + + #expect(driver.statements == ["BEGIN", "UPDATE t SET a = 1"]) + #expect(Self.states(connection.id) == [.open]) + } + + @Test("Manual Commit sends no BEGIN for a read, nor a second one for a later write") + func manualCommitBeginsOnce() async throws { + let (connection, driver) = Self.seed(type: .postgresql) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + try await Self.runInMode(.manual, "SELECT * FROM t", on: connection.id) + #expect(driver.statements == ["SELECT * FROM t"]) + #expect(Self.tracker.holds(for: connection.id).isEmpty) + + try await Self.runInMode(.manual, "UPDATE t SET a = 1", on: connection.id) + try await Self.runInMode(.manual, "UPDATE t SET a = 2", on: connection.id) + #expect(driver.statements == ["SELECT * FROM t", "BEGIN", "UPDATE t SET a = 1", "UPDATE t SET a = 2"]) + } + + @Test("Automatic commit sends no BEGIN") + func automaticCommitOpensNothing() async throws { + let (connection, driver) = Self.seed(type: .postgresql) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + try await Self.runInMode(.automatic, "UPDATE t SET a = 1", on: connection.id) + + #expect(driver.statements == ["UPDATE t SET a = 1"]) + #expect(Self.tracker.holds(for: connection.id).isEmpty) + } + + @Test( + "A session that cannot report, or holds only a lock, refuses the write and sends nothing", + arguments: [PluginSessionTransactionState.unknown, .holdsSessionLocks] + ) + func unholdableSessionRefuses(state: PluginSessionTransactionState) async throws { + let (connection, driver) = Self.seed(type: .postgresql) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + driver.state = state + + await #expect(throws: ManualCommitRefusal()) { + try await Self.runInMode(.manual, "UPDATE t SET a = 1", on: connection.id) + } + #expect(driver.statements.isEmpty) + } + + @Test("A driver that only answers that it cannot say is reported, and one earlier answer outlasts a later unknown") + func readerThatCannotReportIsRemembered() async throws { + let (connection, driver) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + driver.state = .unknown + try await Self.run("SELECT 1", on: connection.id) + #expect(Self.tracker.cannotReportState(connectionId: connection.id, container: "app")) + + driver.state = .idle + try await Self.run("SELECT 1", on: connection.id) + driver.state = .unknown + try await Self.run("SELECT 1", on: connection.id) + #expect(!Self.tracker.cannotReportState(connectionId: connection.id, container: "app")) + } + + @Test("A table read leaves the hold's revision alone, and a write lease that owns no session state moves it") + func readOnlyTurnKeepsTheRevision() async throws { + let (connection, _) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let hold = try #require(Self.tracker.holds(for: connection.id).first) + + _ = try await DatabaseManager.shared.withTableReadDriver(scope: Self.scope(connection.id), cancellation: .untracked) { driver in + try await driver.execute(query: "SELECT * FROM t") + } + _ = try await DatabaseManager.shared.withScopedDriver( + scope: Self.scope(connection.id), route: .sessionDriver, cancellation: .untracked, + ownsSessionState: false, readsOnly: true + ) { driver in + try await driver.execute(query: "SELECT 1") + } + #expect(Self.tracker.isCurrent(hold)) + + _ = try await DatabaseManager.shared.withScopedDriver( + scope: Self.scope(connection.id), route: .sessionDriver, cancellation: .untracked, ownsSessionState: false + ) { driver in + try await driver.execute(query: "INSERT INTO t VALUES (1)") + } + #expect(!Self.tracker.isCurrent(hold)) + } + + @Test("A read-only turn that finds the transaction failed still moves the hold") + func readOnlyTurnStillReportsAFailure() async throws { + let (connection, driver) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let hold = try #require(Self.tracker.holds(for: connection.id).first) + driver.failsNextStatementAborting = true + + await #expect(throws: (any Error).self) { + try await DatabaseManager.shared.withTableReadDriver(scope: Self.scope(connection.id), cancellation: .untracked) { driver in + try await driver.execute(query: "SELECT * FROM missing") + } + } + + #expect(!Self.tracker.isCurrent(hold)) + #expect(Self.states(connection.id) == [.aborted]) + } + + @Test("MCP begin is refused over a transaction already open and sends nothing") + func mcpBeginRefusedOverAnOpenTransaction() async throws { + let (connection, driver) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let sent = driver.statements.count + let operation = try await ApprovedOperationFixture.approve( + [], scope: Self.scope(connection.id), databaseType: .mysql, kind: .writeQuery + ) + + await #expect(throws: DatabaseAccessError.self) { + _ = try await MCPConnectionBridge().transactionControl(operation, action: "begin") + } + #expect(driver.statements.count == sent) + } + + @Test("Manual Commit refuses every MCP transaction action", arguments: ["begin", "commit", "rollback"]) + func mcpTransactionControlRefusedInManualCommit(action: String) async throws { + let (connection, driver) = Self.seed(type: .postgresql, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let operation = try await ApprovedOperationFixture.approve( + [], scope: Self.scope(connection.id), databaseType: .postgresql, kind: .writeQuery + ) + + await #expect(throws: DatabaseAccessError.self) { + _ = try await MCPConnectionBridge().transactionControl(operation, action: action) + } + #expect(driver.statements.isEmpty) + } + + @Test("An agent's write in Manual Commit is held, and its reply says the change is pending") + func mcpWriteIsHeldAndSaysSo() async throws { + let (connection, driver) = Self.seed(type: .postgresql, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + let outcome = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "UPDATE t SET a = 1", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + + let begin = try #require(driver.statements.firstIndex(of: "BEGIN")) + let update = try #require(driver.statements.firstIndex(of: "UPDATE t SET a = 1")) + #expect(begin < update) + #expect(!driver.statements.contains("COMMIT")) + #expect(outcome.result.statusMessage?.contains(ManualCommit.pendingNotice) == true) + #expect(Self.states(connection.id) == [.open]) + } + + @Test("An agent's insert_rows in Manual Commit is held: BEGIN first, and no COMMIT") + func mcpInsertRowsIsHeld() async throws { + let (connection, driver) = Self.seed(type: .postgresql, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let rows = ["INSERT INTO t VALUES (1)", "INSERT INTO t VALUES (2)"] + let operation = try await ApprovedOperationFixture.approve( + rows.map { OperationStatement(sql: $0) }, scope: Self.scope(connection.id), databaseType: .postgresql, + kind: .writeQuery + ) + + _ = try await DatabaseAccessBridge().runInserts(operation) + + let begin = try #require(driver.statements.firstIndex(of: "BEGIN")) + let first = try #require(driver.statements.firstIndex(of: rows[0])) + #expect(begin < first) + #expect(driver.statements.contains(rows[1])) + #expect(!driver.statements.contains("COMMIT")) + #expect(Self.states(connection.id) == [.open]) + } + + @Test("An insert_rows of several rows joining an open SQLite transaction is taken back whole when a row fails") + func mcpInsertRowsJoiningATransactionIsOneUnit() async throws { + let (connection, driver) = Self.seed(type: .sqlite, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let rows = ["INSERT INTO t VALUES (1)", "INSERT INTO t VALUES (2)"] + driver.failingStatement = rows[1] + let operation = try await ApprovedOperationFixture.approve( + rows.map { OperationStatement(sql: $0) }, scope: Self.scope(connection.id), databaseType: .sqlite, + kind: .writeQuery + ) + + await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runInserts(operation) + } + + #expect(driver.statements.suffix(4) == [ + AgentTransactionGuard.savepointStatement, rows[0], rows[1], AgentTransactionGuard.rollbackStatement, + ]) + #expect(!driver.statements.contains("COMMIT")) + } + + @Test("An insert_rows joining an open SQL Server transaction takes SAVE TRANSACTION, which has no release") + func mcpInsertRowsOnSQLServerTakesItsSavepoint() async throws { + let (connection, driver) = Self.seed(type: .mssql) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let rows = ["INSERT INTO t VALUES (1)", "INSERT INTO t VALUES (2)"] + let operation = try await ApprovedOperationFixture.approve( + rows.map { OperationStatement(sql: $0) }, scope: Self.scope(connection.id), databaseType: .mssql, + kind: .writeQuery + ) + + _ = try await DatabaseAccessBridge().runInserts(operation) + + #expect(driver.statements.suffix(3) == ["SAVE TRANSACTION tablepro_agent", rows[0], rows[1]]) + } + + @Test("An agent's COMMIT, ROLLBACK or BEGIN in Manual Commit is refused and sends nothing", arguments: ["COMMIT", "ROLLBACK", "BEGIN"]) + func mcpTransactionStatementRefusedInManualCommit(sql: String) async throws { + let (connection, driver) = Self.seed(type: .postgresql, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.runInMode(.manual, "UPDATE t SET a = 1", on: connection.id) + let sent = driver.statements.count + + let error = await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: sql, maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + } + #expect(error?.localizedDescription == ManualCommit.transactionControlRefusalMessage) + #expect(driver.statements.count == sent) + #expect(Self.states(connection.id) == [.open]) + } + + @Test( + "With Manual Commit holding a transaction, an agent statement that can end it unseen is refused", + arguments: [ + (DatabaseType.mssql, "UPDATE t SET a = 1 COMMIT"), + (DatabaseType.mssql, "EXEC dbo.p"), + (DatabaseType.postgresql, "CALL p()"), + ] + ) + func mcpUnseenTransactionControlRefused(type: DatabaseType, sql: String) async throws { + let (connection, driver) = Self.seed(type: type, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let sent = driver.statements.count + + let error = await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: sql, maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + } + #expect(error?.localizedDescription == ManualCommit.transactionControlRefusalMessage) + #expect(driver.statements.count == sent) + #expect(Self.states(connection.id) == [.open]) + } + + // Its own COMMIT would end the hold this call opens for its write. + @Test( + "On an idle Manual Commit session, an agent write that can end a transaction unseen is refused", + arguments: [ + (DatabaseType.mssql, "UPDATE t SET a = 1 COMMIT"), + (DatabaseType.postgresql, "CALL p()"), + ] + ) + func mcpUnseenWriteRefusedOnAnIdleSession(type: DatabaseType, sql: String) async throws { + let (connection, driver) = Self.seed(type: type, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + let error = await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: sql, maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + } + #expect(error?.localizedDescription == ManualCommit.transactionControlRefusalMessage) + #expect(driver.statements.isEmpty) + } + + @Test("An agent statement that ended the open transaction says so in its reply") + func mcpReplySaysTheTransactionEnded() async throws { + let (connection, _) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + + let outcome = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "COMMIT", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + + #expect(outcome.result.statusMessage?.contains(ManualCommit.transactionEndedNotice) == true) + #expect(Self.tracker.holds(for: connection.id).isEmpty) + } + + @Test("An agent's BEGIN over a transaction already open is refused, whatever the commit mode") + func mcpBeginStatementRefusedOverAnOpenTransaction() async throws { + let (connection, driver) = Self.seed() + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let sent = driver.statements.count + + let error = await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "START TRANSACTION", maxRows: 10, timeoutSeconds: 30, + cancellation: nil + ) + } + #expect(error?.localizedDescription == ManualCommit.openTransactionRefusalMessage) + #expect(driver.statements.count == sent) + } + + @Test("An agent statement over an open PostgreSQL transaction runs inside a savepoint it releases") + func agentStatementTakesASavepoint() async throws { + let (connection, driver) = Self.seed(type: .postgresql) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "UPDATE t SET a = 1", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + + #expect(driver.statements.suffix(3) == [ + AgentTransactionGuard.savepointStatement, "UPDATE t SET a = 1", AgentTransactionGuard.releaseStatement, + ]) + } + + @Test("An agent statement that fails inside an open PostgreSQL transaction is rolled back to its savepoint") + func failedAgentStatementRollsBackToItsSavepoint() async throws { + let (connection, driver) = Self.seed(type: .postgresql) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + driver.failingStatement = "UPDATE t SET a = 1 / 0" + + await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "UPDATE t SET a = 1 / 0", maxRows: 10, timeoutSeconds: 30, + cancellation: nil + ) + } + + #expect(driver.statements.suffix(3) == [ + AgentTransactionGuard.savepointStatement, "UPDATE t SET a = 1 / 0", AgentTransactionGuard.rollbackStatement, + ]) + } + + @Test("An agent statement over an open DuckDB transaction is refused, since a failure would abort it") + func agentStatementRefusedWithoutSavepoints() async throws { + let (connection, driver) = Self.seed(type: .duckdb) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + try await Self.run("BEGIN", on: connection.id) + let sent = driver.statements.count + + let error = await #expect(throws: (any Error).self) { + _ = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "SELECT 1", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + } + #expect(error?.localizedDescription == AbortingTransactionRefusal().errorDescription) + #expect(driver.statements.count == sent) + } + + @Test("Turning Manual Commit on writes the session and the connection it was opened from") + func menuToggleWritesTheSession() { + let (connection, _) = Self.seed(type: .postgresql) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + + DatabaseManager.shared.setCommitMode(.manual, for: connection.id) + + #expect(DatabaseManager.shared.commitMode(for: connection.id) == .manual) + #expect(DatabaseManager.shared.session(for: connection.id)?.connection.commitMode == .manual) + } + + @Test("A Manual Commit stored on a MySQL connection runs as automatic: an agent's write sends no BEGIN") + func storedManualCommitOnMySQLRunsAutomatic() async throws { + let (connection, driver) = Self.seed(type: .mysql, commitMode: .manual) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + #expect(DatabaseManager.shared.session(for: connection.id)?.connection.commitMode == .manual) + #expect(DatabaseManager.shared.commitMode(for: connection.id) == .automatic) + + let outcome = try await DatabaseAccessBridge().runStatement( + scope: Self.scope(connection.id), query: "UPDATE t SET a = 1", maxRows: 10, timeoutSeconds: 30, cancellation: nil + ) + + #expect(driver.statements.contains("UPDATE t SET a = 1")) + #expect(!driver.statements.contains("BEGIN")) + #expect(outcome.result.statusMessage?.contains(ManualCommit.pendingNotice) != true) + #expect(Self.tracker.holds(for: connection.id).isEmpty) + } + @Test("Read-only hides Commit, an aborted transaction offers only Roll Back") func barContent() { let open = ConnectionTransactionBarContent( @@ -293,10 +706,18 @@ private final class TransactionScriptedDriver: PluginDriverStub, @unchecked Send set { lock.withLock { abortsNext = newValue } } } + private var failing: String? + + // That statement fails and aborts the transaction, the way PostgreSQL answers a division by zero. + var failingStatement: String? { + get { lock.withLock { failing } } + set { lock.withLock { failing = newValue } } + } + func execute(query: String) async throws -> PluginQueryResult { let failure: Bool = lock.withLock { sent.append(query) - if abortsNext { + if abortsNext || query == failing { abortsNext = false current = .abortedTransaction return true diff --git a/apple/TableProTests/Core/Menu/ManualCommitMenuTests.swift b/apple/TableProTests/Core/Menu/ManualCommitMenuTests.swift new file mode 100644 index 0000000000..57a454cc2a --- /dev/null +++ b/apple/TableProTests/Core/Menu/ManualCommitMenuTests.swift @@ -0,0 +1,120 @@ +// +// ManualCommitMenuTests.swift +// TableProTests +// + +import AppKit +@testable import TablePro +import Testing + +@MainActor +struct ManualCommitMenuTests { + private func databaseMenu() throws -> NSMenu { + let bar = MainMenuBuilder.build(keyboard: KeyboardSettings()).bar + return try #require(bar.items.first { $0.title == String(localized: "Database") }?.submenu) + } + + private func enabled(_ selector: Selector, _ context: MenuValidationContext) -> Bool { + MainSplitViewController.isEnabled(selector, context: context) + } + + @Test("Manual Commit, its footnotes, Commit Transaction and Roll Back Transaction follow Safe Mode Level") + func itemsFollowSafeModeLevel() throws { + let items = try databaseMenu().items + let safeMode = try #require(items.firstIndex { $0.title == String(localized: "Safe Mode Level") }) + try #require(safeMode + 5 < items.count) + + #expect(items[safeMode + 1].title == String(localized: "Manual Commit")) + #expect(items[safeMode + 1].action == #selector(MainSplitViewController.toggleManualCommit(_:))) + #expect(items[safeMode + 2].identifier == DatabaseMenuBuilder.manualCommitFootnoteIdentifier) + #expect(items[safeMode + 2].isHidden) + #expect(items[safeMode + 3].identifier == DatabaseMenuBuilder.manualCommitUnavailableFootnoteIdentifier) + #expect(items[safeMode + 3].attributedTitle?.string.replacingOccurrences(of: "\n", with: " ") + == ManualCommit.unavailableExplanation) + #expect(items[safeMode + 3].isHidden) + #expect(items[safeMode + 4].title == String(localized: "Commit Transaction")) + #expect(items[safeMode + 4].action == #selector(MainSplitViewController.commitOpenTransaction(_:))) + #expect(items[safeMode + 5].title == String(localized: "Roll Back Transaction")) + #expect(items[safeMode + 5].action == #selector(MainSplitViewController.rollBackOpenTransaction(_:))) + } + + @Test("Commit and Roll Back Transaction are Keyboard rows with no default chord") + func transactionCommandsShipUnbound() throws { + for action in [ShortcutAction.commitTransaction, .rollbackTransaction] { + #expect(KeyboardSettings.defaultShortcuts[action] == nil) + let item = try #require(try databaseMenu().items.first { $0.identifier == MenuItemFactory.identifier(for: action) }) + #expect(item.keyEquivalent.isEmpty) + } + } + + @Test("Manual Commit needs a connection that can hold a transaction, and stays enabled while on") + func manualCommitEnablement() { + let toggle = #selector(MainSplitViewController.toggleManualCommit(_:)) + var context = MenuValidationContext() + context.transaction.isAvailable = true + context.transaction.canHoldTransactions = true + #expect(!enabled(toggle, context)) + + context.isConnected = true + #expect(enabled(toggle, context)) + + context.transaction.canHoldTransactions = false + #expect(!enabled(toggle, context)) + + context.transaction.isManualCommitOn = true + #expect(enabled(toggle, context)) + } + + @Test("Where DDL commits on its own, Manual Commit is disabled with its footnote, even when stored on") + func manualCommitUnavailable() { + let toggle = #selector(MainSplitViewController.toggleManualCommit(_:)) + var context = MenuValidationContext() + context.isConnected = true + context.transaction.canHoldTransactions = true + context.transaction.isManualCommitOn = true + #expect(!enabled(toggle, context)) + #expect(MainSplitViewController.shownManualCommitFootnote(context.transaction, isConnected: true) + == DatabaseMenuBuilder.manualCommitUnavailableFootnoteIdentifier) + #expect(MainSplitViewController.shownManualCommitFootnote(context.transaction, isConnected: false) == nil) + } + + @Test("The reporting footnote shows only where the engine can hold but its driver can't report, while off") + func reportingFootnote() { + var state = TransactionMenuState() + state.isAvailable = true + #expect(MainSplitViewController.shownManualCommitFootnote(state, isConnected: true) + == DatabaseMenuBuilder.manualCommitFootnoteIdentifier) + state.canHoldTransactions = true + #expect(MainSplitViewController.shownManualCommitFootnote(state, isConnected: true) == nil) + state.canHoldTransactions = false + state.isManualCommitOn = true + #expect(MainSplitViewController.shownManualCommitFootnote(state, isConnected: true) == nil) + } + + @Test("Commit needs an open transaction it may commit; Roll Back needs any held transaction") + func commitAndRollBackEnablement() { + let commit = #selector(MainSplitViewController.commitOpenTransaction(_:)) + let rollBack = #selector(MainSplitViewController.rollBackOpenTransaction(_:)) + var context = MenuValidationContext() + context.isConnected = true + #expect(!enabled(commit, context)) + #expect(!enabled(rollBack, context)) + + context.transaction.holdsTransaction = true + #expect(!enabled(commit, context)) + #expect(enabled(rollBack, context)) + + context.transaction.canCommit = true + #expect(enabled(commit, context)) + } + + @Test("Commit is offered for an open hold below Read-Only, never for an aborted one") + func commitFollowsSafeModeAndState() { + let key = SessionTransactionTracker.Key(connectionId: UUID(), lane: nil) + let open = SessionTransactionTracker.Hold(key: key, state: .open, revision: 1) + let aborted = SessionTransactionTracker.Hold(key: key, state: .aborted, revision: 1) + #expect(ConnectionTransactionActions.offersCommit(open, safeModeLevel: .silent)) + #expect(!ConnectionTransactionActions.offersCommit(open, safeModeLevel: .readOnly)) + #expect(!ConnectionTransactionActions.offersCommit(aborted, safeModeLevel: .silent)) + } +} diff --git a/apple/TableProTests/Core/Menu/QueryTabRevertMenuTests.swift b/apple/TableProTests/Core/Menu/QueryTabRevertMenuTests.swift new file mode 100644 index 0000000000..2152832a19 --- /dev/null +++ b/apple/TableProTests/Core/Menu/QueryTabRevertMenuTests.swift @@ -0,0 +1,89 @@ +// +// QueryTabRevertMenuTests.swift +// TableProTests +// + +import AppKit +@testable import TablePro +import Testing + +@MainActor +struct QueryTabRevertMenuTests { + private static let revert = #selector(MainSplitViewController.revertToQueryTabVersion(_:)) + private static let browse = #selector(MainSplitViewController.browseQueryTabVersions(_:)) + + private func fileMenu() throws -> NSMenu { + let bar = MainMenuBuilder.build(keyboard: KeyboardSettings()).bar + return try #require(bar.items.first { $0.title == String(localized: "File") }?.submenu) + } + + private func summary(_ id: Int64, _ reason: QueryTabVersionReason) -> QueryTabVersionSummary { + QueryTabVersionSummary(id: id, reason: reason, savedAt: Date(timeIntervalSince1970: 1_800_000_000 + Double(id))) + } + + @Test("Revert To follows Save As in File, fills when it opens and carries no shortcut") + func revertToFollowsSaveAs() throws { + let file = try fileMenu() + let saveAs = try #require(file.items.firstIndex { + $0.action == #selector(MainSplitViewController.saveDocumentAs(_:)) + }) + let revertTo = file.items[saveAs + 1] + + #expect(revertTo.title == String(localized: "Revert To")) + #expect(revertTo.keyEquivalent.isEmpty) + #expect(revertTo.submenu?.delegate is QueryTabRevertMenuDelegate) + #expect(revertTo.submenu?.items.isEmpty == true) + } + + @Test("The list is the tab's versions, then Browse All Versions…") + func listsVersionsThenBrowse() throws { + let menu = NSMenu() + let tabId = UUID() + + QueryTabRevertMenuDelegate.fill(menu, tabId: tabId, summaries: [summary(2, .run), summary(1, .idle)]) + + #expect(menu.items.count == 4) + #expect(menu.items[0].action == Self.revert) + #expect(menu.items[0].target == nil) + #expect(menu.items[0].representedObject as? QueryTabRevertChoice == QueryTabRevertChoice(tabId: tabId, versionId: 2)) + #expect(menu.items[1].representedObject as? QueryTabRevertChoice == QueryTabRevertChoice(tabId: tabId, versionId: 1)) + #expect(menu.items[0].title.contains(String(localized: "Run"))) + #expect(menu.items[2].isSeparatorItem) + #expect(menu.items[3].title == String(localized: "Browse All Versions…")) + #expect(menu.items[3].action == Self.browse) + #expect(menu.items[3].target == nil) + } + + @Test("With no versions the list says so and still offers Browse All Versions…") + func emptyListShowsThePlaceholder() { + let menu = NSMenu() + menu.addItem(withTitle: "stale", action: nil, keyEquivalent: "") + + QueryTabRevertMenuDelegate.fill(menu, tabId: UUID(), summaries: []) + + #expect(menu.items.map { $0.title } == [ + String(localized: "None Available"), "", String(localized: "Browse All Versions…"), + ]) + #expect(menu.items[0].isEnabled == false) + } + + @Test("Revert To and Browse All Versions need a query tab, and no connection") + func commandsNeedAQueryTab() { + for selector in [Self.revert, Self.browse] { + var context = MenuValidationContext() + #expect(MainSplitViewController.resolvedEnablement(selector, context: context) == false) + + context.isQueryTab = true + #expect(MainSplitViewController.resolvedEnablement(selector, context: context) == true) + + context.isAgentMode = true + #expect(MainSplitViewController.resolvedEnablement(selector, context: context) == false) + } + } + + @Test("The window answers both commands") + func windowImplementsBoth() { + #expect(MainSplitViewController.instancesRespond(to: Self.revert)) + #expect(MainSplitViewController.instancesRespond(to: Self.browse)) + } +} diff --git a/apple/TableProTests/Core/ObjectCopy/ObjectCopyTransactionTests.swift b/apple/TableProTests/Core/ObjectCopy/ObjectCopyTransactionTests.swift index ab8b8205a5..77179ab158 100644 --- a/apple/TableProTests/Core/ObjectCopy/ObjectCopyTransactionTests.swift +++ b/apple/TableProTests/Core/ObjectCopy/ObjectCopyTransactionTests.swift @@ -286,6 +286,24 @@ struct ObjectCopyTransactionTests { #expect(result.pendingInSessionTransaction == false) } + // A held copy on a pooled lease would be rolled back when the pool closes it, after the sheet reported success. + @Test("Copy To commits its own transaction even on a connection in Manual Commit") + func manualCommitConnectionStillCommitsTheCopy() async throws { + // MySQL runs a stored Manual Commit as automatic, so the session is on an engine that holds. + var connection = TestFixtures.makeConnection(id: targetScope.connectionId, type: .postgresql) + connection.commitMode = .manual + DatabaseManager.shared.injectSession(ConnectionSession(connection: connection), for: connection.id) + defer { DatabaseManager.shared.removeSession(for: connection.id) } + let log = CopyEventLog() + + _ = try await run( + replacementPlan(), source: RecordingCopyDriver("source", log: log), target: RecordingCopyDriver("target", log: log) + ) + + #expect(log.events.first == "target begin") + #expect(log.events.last == "target commit") + } + /// The statements still run: they join the transaction the user opened, and only the user can /// end it. Opening one over it is what aborted it on DuckDB. @Test("A session holding a transaction is joined rather than wrapped, with its keys as they are") diff --git a/apple/TableProTests/Core/Plugins/ConnectionFieldTrustCoverageTests.swift b/apple/TableProTests/Core/Plugins/ConnectionFieldTrustCoverageTests.swift index 8a84c3f491..e3143eda6a 100644 --- a/apple/TableProTests/Core/Plugins/ConnectionFieldTrustCoverageTests.swift +++ b/apple/TableProTests/Core/Plugins/ConnectionFieldTrustCoverageTests.swift @@ -10,6 +10,7 @@ private let hostOwnedKeys: [String] = [ DatabaseConnection.postDisconnectScriptKey, DatabaseConnection.scriptTimeoutSecondsKey, DatabaseConnection.completionIdentifierCaseKey, + DatabaseConnection.commitModeKey, DatabaseConnection.binaryInlineLimitKilobytesKey, DatabaseConnection.maskingRulesKey, DatabaseConnection.sshForwardUnixSocketPathKey, diff --git a/apple/TableProTests/Core/Plugins/ImportManualCommitTests.swift b/apple/TableProTests/Core/Plugins/ImportManualCommitTests.swift new file mode 100644 index 0000000000..0faf907b45 --- /dev/null +++ b/apple/TableProTests/Core/Plugins/ImportManualCommitTests.swift @@ -0,0 +1,154 @@ +// +// ImportManualCommitTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +private final class ImportHoldDriver: PluginDriverStub, @unchecked Sendable { + private let lock = NSLock() + private var current: PluginSessionTransactionState + private var sent: [String] = [] + + init(state: PluginSessionTransactionState = .idle) { + current = state + } + + var supportsSchemas: Bool { false } + var currentSchema: String? { nil } + var serverVersion: String? { nil } + + var statements: [String] { lock.withLock { sent } } + + func execute(query: String) async throws -> PluginQueryResult { + lock.withLock { + sent.append(query) + switch query.uppercased() { + case "BEGIN": current = .inTransaction + case "COMMIT", "ROLLBACK": current = .idle + default: break + } + } + return .empty + } + + func sessionTransactionState() async -> PluginSessionTransactionState { lock.withLock { current } } + + func foreignKeyDisableStatements() -> [String]? { ["PRAGMA foreign_keys = OFF"] } + func foreignKeyEnableStatements() -> [String]? { ["PRAGMA foreign_keys = ON"] } +} + +struct ImportManualCommitTests { + private func sink( + on driver: ImportHoldDriver, + type: DatabaseType = .sqlite, + commitMode: ConnectionCommitMode = .manual + ) -> ImportDataSinkAdapter { + let adapter = PluginDriverAdapter(connection: TestFixtures.makeConnection(type: type), pluginDriver: driver) + return ImportDataSinkAdapter(driver: adapter, databaseType: type, commitMode: commitMode, importRun: ImportRun()) + } + + @Test("A Manual Commit import holds its transaction: BEGIN, and no COMMIT at the end") + func importIsHeld() async throws { + let driver = ImportHoldDriver() + let sink = sink(on: driver) + + try await sink.prepareImportRun() + try await sink.beginTransaction() + try await sink.execute(statement: "INSERT INTO t VALUES (1)", line: 1) + try await sink.commitTransaction() + + #expect(driver.statements == ["BEGIN", "INSERT INTO t VALUES (1)"]) + } + + @Test( + "A Manual Commit import the session can't hold is refused before anything runs", + arguments: [PluginSessionTransactionState.unknown, .holdsSessionLocks] + ) + func unholdableImportIsRefused(state: PluginSessionTransactionState) async { + let driver = ImportHoldDriver(state: state) + let sink = sink(on: driver) + + await #expect(throws: ManualCommitRefusal()) { + try await sink.prepareImportRun() + } + #expect(driver.statements.isEmpty) + } + + // SQLite ignores the re-enable inside a held transaction, so the checks would stay off for the session. + @Test("A Manual Commit import that turned foreign key checks off is refused before its first write") + func foreignKeyToggledImportIsRefused() async throws { + let driver = ImportHoldDriver() + let sink = sink(on: driver) + + try await sink.prepareImportRun() + try await sink.disableForeignKeyChecks() + await #expect(throws: ManualCommitForeignKeyRefusal()) { + try await sink.beginTransaction() + } + try await sink.enableForeignKeyChecks() + + #expect(driver.statements == ["PRAGMA foreign_keys = OFF", "PRAGMA foreign_keys = ON"]) + } + + // Skip and Continue goes on after a refusal, so every later write is refused as well. + @Test("A Manual Commit import that never begins a transaction is refused at each write, before it runs") + func unwrappedImportIsRefused() async throws { + let driver = ImportHoldDriver() + let sink = sink(on: driver) + + try await sink.prepareImportRun() + await #expect(throws: ManualCommitUnwrappedImportRefusal()) { + try await sink.execute(statement: "INSERT INTO t VALUES (1)", line: 1) + } + await #expect(throws: ManualCommitUnwrappedImportRefusal()) { + try await sink.execute(statement: "INSERT INTO t VALUES (2)", line: 2) + } + + #expect(driver.statements.isEmpty) + } + + @Test("A held import runs a dump's DDL inside its transaction, with no COMMIT") + func heldImportRunsDDL() async throws { + let driver = ImportHoldDriver() + let sink = sink(on: driver) + try await sink.prepareImportRun() + try await sink.beginTransaction() + + try await sink.execute(statement: "DROP TABLE IF EXISTS t", line: 1) + try await sink.execute(statement: "INSERT INTO t VALUES (1)", line: 2) + try await sink.commitTransaction() + + #expect(driver.statements == ["BEGIN", "DROP TABLE IF EXISTS t", "INSERT INTO t VALUES (1)"]) + } + + @Test("A held import marks the turn's hold at its BEGIN") + func heldImportMarksTheTurn() async throws { + let driver = ImportHoldDriver() + let sink = sink(on: driver) + let held = ManualCommitTurnHold() + + try await ManualCommit.$turnHold.withValue(held) { + try await sink.prepareImportRun() + try await sink.beginTransaction() + } + + #expect(held.isHeld) + } + + @Test("An automatic import with foreign key checks off still wraps and commits") + func automaticImportWithToggleCommits() async throws { + let driver = ImportHoldDriver() + let sink = sink(on: driver, commitMode: .automatic) + + try await sink.prepareImportRun() + try await sink.disableForeignKeyChecks() + try await sink.beginTransaction() + try await sink.commitTransaction() + + #expect(driver.statements == ["PRAGMA foreign_keys = OFF", "BEGIN", "COMMIT"]) + } +} diff --git a/apple/TableProTests/Core/SchemaTracking/StructureChangeValidationTests.swift b/apple/TableProTests/Core/SchemaTracking/StructureChangeValidationTests.swift index e05e11ea1f..a325c60432 100644 --- a/apple/TableProTests/Core/SchemaTracking/StructureChangeValidationTests.swift +++ b/apple/TableProTests/Core/SchemaTracking/StructureChangeValidationTests.swift @@ -83,6 +83,25 @@ struct StructureChangeValidationTests { #expect(manager.validationSummary.isEmpty) } + @Test("A key with more columns than referenced columns blocks the save, with Create Table's reason") + func unequalColumnCountsBlockTheSave() throws { + let manager = loadedManager() + let foreignKey = validForeignKey(columns: ["id", "customer_id"], referencedColumns: ["id"]) + manager.addForeignKey(foreignKey) + + #expect(!manager.canCommit) + let staged = try #require(manager.workingForeignKeys.last) + #expect(manager.validationErrors[.foreignKey(staged.id)] + == "A foreign key needs as many referenced columns as it has columns.") + } + + @Test("A key with as many referenced columns as columns can be saved") + func equalColumnCountsAllowTheSave() { + let manager = loadedManager() + manager.addForeignKey(validForeignKey(columns: ["id", "customer_id"], referencedColumns: ["tenant_id", "id"])) + #expect(manager.canCommit) + } + @Test("A key on a column the table does not have blocks the save") func foreignKeyOnMissingColumnBlocksCommit() { let manager = loadedManager() diff --git a/apple/TableProTests/Core/Services/Execution/BatchStatementRunTests.swift b/apple/TableProTests/Core/Services/Execution/BatchStatementRunTests.swift index b20e731cd0..d29ac4858a 100644 --- a/apple/TableProTests/Core/Services/Execution/BatchStatementRunTests.swift +++ b/apple/TableProTests/Core/Services/Execution/BatchStatementRunTests.swift @@ -117,6 +117,53 @@ struct BatchStatementRunTests { } } + @Test("A held run sends BEGIN and leaves the transaction open for the user to commit") + func heldRunBeginsAndNeverCommits() async { + let driver = TransactionRecordingDriver() + let outcome = await Self.run(plan: .heldTransaction, driver: driver) + #expect(driver.events == [.begin(mode: .readWrite)]) + guard case .completed(let results) = outcome else { + Issue.record("expected a completed run, got \(outcome)") + return + } + #expect(results.count == 3) + } + + @Test("A held run marks the turn's hold, and one rolled back clears it") + func heldRunMarksTheTurn() async { + let held = ManualCommitTurnHold() + _ = await ManualCommit.$turnHold.withValue(held) { + await Self.run(plan: .heldTransaction, driver: TransactionRecordingDriver()) + } + #expect(held.isHeld) + + let failed = ManualCommitTurnHold() + _ = await ManualCommit.$turnHold.withValue(failed) { + await Self.run(plan: .heldTransaction, driver: TransactionRecordingDriver(), failing: "VACUUM") + } + #expect(!failed.isHeld) + } + + @Test("A held run rolls back what it opened on a failure and on a Stop") + func heldRunRollsBackAFailureAndAStop() async { + let failing = TransactionRecordingDriver() + let failed = await Self.run(plan: .heldTransaction, driver: failing, failing: "VACUUM") + #expect(failing.events == [.begin(mode: .readWrite), .rollback]) + guard case .failed = failed else { + Issue.record("expected a failed run, got \(failed)") + return + } + + let stopped = TransactionRecordingDriver() + let outcome = await Self.run(plan: .heldTransaction, driver: stopped, stopsAfter: 1) + #expect(stopped.events == [.begin(mode: .readWrite), .rollback]) + guard case .cancelled(let results) = outcome else { + Issue.record("expected a cancelled run, got \(outcome)") + return + } + #expect(results.isEmpty) + } + @Test("The app's own transaction is rolled back on a failure, and its results are dropped") func appTransactionRollsBackAFailure() async { let driver = TransactionRecordingDriver() @@ -228,7 +275,7 @@ struct BatchStatementRunTests { @Test( "A driver without transactions is never asked to begin, commit or roll back", - arguments: [BatchTransactionPlan.appTransaction, .scriptTransaction, .autocommit, .sessionTransaction] + arguments: [BatchTransactionPlan.appTransaction, .heldTransaction, .scriptTransaction, .autocommit, .sessionTransaction] ) func driversWithoutTransactionsAreLeftAlone(plan: BatchTransactionPlan) async { let driver = TransactionRecordingDriver(supportsTransactions: false) diff --git a/apple/TableProTests/Core/Services/Execution/BatchTransactionPlanTests.swift b/apple/TableProTests/Core/Services/Execution/BatchTransactionPlanTests.swift index 185bd773ad..49eb84b51e 100644 --- a/apple/TableProTests/Core/Services/Execution/BatchTransactionPlanTests.swift +++ b/apple/TableProTests/Core/Services/Execution/BatchTransactionPlanTests.swift @@ -11,12 +11,16 @@ import Testing struct BatchTransactionPlanTests { private static let textPlans: [BatchTransactionPlan] = [.appTransaction, .scriptTransaction, .autocommit] - @Test("Only the app's own plan opens a transaction") - func onlyTheAppPlanOpensOne() { - #expect(BatchTransactionPlan.appTransaction.opensTransaction) - #expect(BatchTransactionPlan.scriptTransaction.opensTransaction == false) - #expect(BatchTransactionPlan.autocommit.opensTransaction == false) - #expect(BatchTransactionPlan.sessionTransaction.opensTransaction == false) + @Test("The app's own plan and a held one send BEGIN, and only the app's own sends COMMIT") + func onlyTheAppPlanCommits() { + #expect(BatchTransactionPlan.appTransaction.sendsBegin) + #expect(BatchTransactionPlan.appTransaction.commitsAtEnd) + #expect(BatchTransactionPlan.heldTransaction.sendsBegin) + #expect(BatchTransactionPlan.heldTransaction.commitsAtEnd == false) + for plan in [BatchTransactionPlan.scriptTransaction, .autocommit, .sessionTransaction] { + #expect(plan.sendsBegin == false) + #expect(plan.commitsAtEnd == false) + } } @Test("A joined run rolls nothing back and keeps what ran, exactly as autocommit does") @@ -25,31 +29,48 @@ struct BatchTransactionPlanTests { #expect(BatchTransactionPlan.sessionTransaction.keepsExecutedStatements) } + @Test("A held run rolls back on a failure or a Stop and reads what it left open") + func heldRunRollsBackAndReadsItsEnd() { + #expect(BatchTransactionPlan.heldTransaction.rollsBackAfterStop) + #expect(BatchTransactionPlan.heldTransaction.keepsExecutedStatements == false) + #expect(BatchTransactionPlan.heldTransaction.readsSessionEndState) + #expect(BatchTransactionPlan.appTransaction.readsSessionEndState == false) + } + @Test( "A session holding a transaction or a lock takes the plan over, whatever the text said", arguments: [PluginSessionTransactionState.inTransaction, .abortedTransaction, .holdsSessionLocks] ) func heldSessionTakesOver(state: PluginSessionTransactionState) { for plan in Self.textPlans { - #expect(plan.joining(state) == .sessionTransaction) + #expect(plan.joining(state, commitMode: .automatic) == .sessionTransaction) + #expect(plan.joining(state, commitMode: .manual) == .sessionTransaction) } } @Test("A session with nothing open leaves the plan the text decided") func idleSessionChangesNothing() { for plan in Self.textPlans { - #expect(plan.joining(.idle) == plan) + #expect(plan.joining(.idle, commitMode: .automatic) == plan) } } + @Test("Manual Commit holds a wrapped batch on an idle session, and nothing else") + func manualCommitHoldsOnlyAWrappedBatchOnAnIdleSession() { + #expect(BatchTransactionPlan.appTransaction.joining(.idle, commitMode: .manual) == .heldTransaction) + #expect(BatchTransactionPlan.appTransaction.joining(.unknown, commitMode: .manual) == .appTransaction) + #expect(BatchTransactionPlan.scriptTransaction.joining(.idle, commitMode: .manual) == .scriptTransaction) + #expect(BatchTransactionPlan.autocommit.joining(.idle, commitMode: .manual) == .autocommit) + } + /// The wrap stays for a plain batch, because its atomicity is worth more than a transaction that /// may not be there. A self-managed script stops being rolled back, because the transaction its /// text left open may predate the run. @Test("A session that cannot say keeps the wrap but stops the rollback of a self-managed script") func unknownSessionKeepsTheWrapButNotTheRollback() { - #expect(BatchTransactionPlan.appTransaction.joining(.unknown) == .appTransaction) - #expect(BatchTransactionPlan.autocommit.joining(.unknown) == .autocommit) - #expect(BatchTransactionPlan.scriptTransaction.joining(.unknown) == .sessionTransaction) + #expect(BatchTransactionPlan.appTransaction.joining(.unknown, commitMode: .automatic) == .appTransaction) + #expect(BatchTransactionPlan.autocommit.joining(.unknown, commitMode: .automatic) == .autocommit) + #expect(BatchTransactionPlan.scriptTransaction.joining(.unknown, commitMode: .automatic) == .sessionTransaction) } @Test("Joining is settled once: a joined plan stays joined") @@ -57,9 +78,21 @@ struct BatchTransactionPlanTests { for state in [ PluginSessionTransactionState.idle, .inTransaction, .abortedTransaction, .holdsSessionLocks, .unknown, ] { - #expect(BatchTransactionPlan.sessionTransaction.joining(state) == .sessionTransaction) + #expect(BatchTransactionPlan.sessionTransaction.joining(state, commitMode: .manual) == .sessionTransaction) } } + + @Test("A held run is never recorded as committed") + func heldRunIsNotCommitted() { + let completed = StatementCommitEvidence.run( + startedIn: .idle, endedIn: .inTransaction, plan: .heldTransaction, completed: true + ) + let failed = StatementCommitEvidence.run( + startedIn: .idle, endedIn: .idle, plan: .heldTransaction, completed: false + ) + #expect(completed == .runStartedIn(.idle, endedIn: .inTransaction, appTransaction: .none)) + #expect(failed == .runStartedIn(.idle, endedIn: .idle, appTransaction: .rolledBack)) + } } struct SessionTransactionStateTests { diff --git a/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift b/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift new file mode 100644 index 0000000000..d3ff9b353b --- /dev/null +++ b/apple/TableProTests/Core/Services/Execution/ManualCommitTests.swift @@ -0,0 +1,305 @@ +// +// ManualCommitTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import TableProSSHConfig +import Testing + +struct ManualCommitTests { + private func decision( + _ sql: String, + mode: ConnectionCommitMode = .manual, + type: DatabaseType = .postgresql, + supportsTransactions: Bool = true, + state: PluginSessionTransactionState = .idle + ) -> ManualCommit.Decision { + decision([sql], mode: mode, type: type, supportsTransactions: supportsTransactions, state: state) + } + + private func decision( + _ statements: [String], + mode: ConnectionCommitMode = .manual, + type: DatabaseType = .postgresql, + supportsTransactions: Bool = true, + state: PluginSessionTransactionState = .idle + ) -> ManualCommit.Decision { + ManualCommit.decision( + commitMode: mode, + statements: statements, + databaseType: type, + grammar: type.lexicalGrammar, + supportsTransactions: supportsTransactions, + sessionState: state + ) + } + + @Test("A write on an idle session is held in Manual Commit and runs as typed otherwise") + func writeOnAnIdleSessionIsHeld() { + #expect(decision("UPDATE t SET a = 1") == .hold) + #expect(decision("UPDATE t SET a = 1", mode: .automatic) == .runAsIs) + } + + @Test("A read opens nothing") + func readOpensNothing() { + #expect(decision("SELECT * FROM t") == .runAsIs) + } + + @Test("A write joins a transaction already open, aborted or not") + func writeJoinsAnOpenTransaction() { + #expect(decision("UPDATE t SET a = 1", state: .inTransaction) == .runAsIs) + #expect(decision("UPDATE t SET a = 1", state: .abortedTransaction) == .runAsIs) + } + + @Test( + "A write the session cannot hold is refused rather than committed", + arguments: [PluginSessionTransactionState.unknown, .holdsSessionLocks] + ) + func unholdableStateRefuses(state: PluginSessionTransactionState) { + #expect(decision("UPDATE t SET a = 1", state: state) == .refuse) + } + + @Test("An engine without transactions refuses a write in Manual Commit") + func engineWithoutTransactionsRefuses() { + #expect(decision("UPDATE t SET a = 1", supportsTransactions: false) == .refuse) + #expect(decision("SELECT 1", supportsTransactions: false) == .runAsIs) + } + + @Test("Transaction control and statements refused inside a block run as typed") + func transactionControlAndAutocommitOnlyRunAsTyped() { + #expect(decision("BEGIN") == .runAsIs) + #expect(decision("VACUUM") == .runAsIs) + } + + @Test("Manual Commit is offered where DDL waits for Commit", arguments: [ + DatabaseType.postgresql, .sqlite, .mssql, .duckdb, + ]) + func availableWhereDDLWaits(type: DatabaseType) { + #expect(ManualCommit.isAvailable(for: type)) + #expect(ManualCommit.effectiveMode(.manual, databaseType: type) == .manual) + } + + @Test("Manual Commit is not offered where DDL commits on its own, and a stored one runs as automatic", arguments: [ + DatabaseType.mysql, .mariadb, .oracle, .clickhouse, .redshift, .cockroachdb, + ]) + func unavailableWhereDDLCommits(type: DatabaseType) { + #expect(!ManualCommit.isAvailable(for: type)) + #expect(ManualCommit.effectiveMode(.manual, databaseType: type) == .automatic) + } + + @Test("A run that mixes a write with a statement that can't run inside a transaction is refused") + func autocommitOnlyBesideAWriteRefuses() { + #expect(decision(["UPDATE t SET a = 1", "VACUUM t"]) == .refuseMixed) + #expect(decision(["DELETE FROM t", "CREATE INDEX CONCURRENTLY i ON t (a)"]) == .refuseMixed) + #expect(decision(["VACUUM t"]) == .runAsIs) + } + + @Test("DDL beside a write is held like the write where DDL waits for Commit", arguments: [ + DatabaseType.postgresql, .sqlite, .mssql, + ]) + func ddlBesideAWriteIsHeld(type: DatabaseType) { + #expect(decision(["UPDATE t SET a = 1", "ALTER TABLE u ADD c INT"], type: type) == .hold) + #expect(decision(["ALTER TABLE u ADD c INT"], type: type) == .hold) + } + + @Test("Two reads in Manual Commit are not held: the run keeps the plan an automatic connection uses") + func readOnlyRunIsNotHeld() { + let reads = ["SELECT 1", "SELECT 2"] + let decided = decision(reads) + #expect(decided == .runAsIs) + #expect(ManualCommit.runPlan(text: .appTransaction, decision: decided, sessionState: .idle) == .appTransaction) + #expect(ManualCommit.runPlan(text: .appTransaction, decision: .hold, sessionState: .idle) == .heldTransaction) + #expect(ManualCommit.runPlan(text: .autocommit, decision: .runAsIs, sessionState: .inTransaction) == .sessionTransaction) + } + + @Test("An EXPLAIN ANALYZE of a write is held like the write") + func explainAnalyzeOfAWriteIsHeld() { + #expect(decision("EXPLAIN ANALYZE DELETE FROM orders WHERE id = 1") == .hold) + #expect(decision("EXPLAIN SELECT * FROM orders") == .runAsIs) + } + + @Test("A read that timed out after the turn's own BEGIN records the hold it opened") + func timedOutReadAfterTheTurnsBeginIsOpen() { + #expect(ManualCommit.recordedState(read: .unknown, turnOpenedHold: true) == .inTransaction) + #expect(ManualCommit.recordedState(read: .unknown, turnOpenedHold: false) == .unknown) + #expect(ManualCommit.recordedState(read: .idle, turnOpenedHold: true) == .idle) + } + + @Test("COMMIT, ROLLBACK, BEGIN, SAVEPOINT and END control the transaction; a PL/SQL block does not") + func transactionControlIsRecognised() { + let postgres = DatabaseType.postgresql + for sql in ["COMMIT", "ROLLBACK", "ROLLBACK TO SAVEPOINT a", "BEGIN", "START TRANSACTION", "SAVEPOINT a", "END"] { + #expect(ManualCommit.controlsTransaction([sql], databaseType: postgres, grammar: postgres.lexicalGrammar), "\(sql)") + } + #expect(!ManualCommit.controlsTransaction(["UPDATE t SET a = 1"], databaseType: postgres, grammar: postgres.lexicalGrammar)) + let oracle = DatabaseType.oracle + #expect(!ManualCommit.controlsTransaction( + ["BEGIN UPDATE t SET a = 1; END;"], databaseType: oracle, grammar: oracle.lexicalGrammar + )) + } + + @Test("Over an open transaction, an agent statement takes a savepoint where one exists and is refused where none does") + func agentProtectionFollowsTheFamily() { + #expect(AgentTransactionGuard.protection(family: .postgres, sessionState: .inTransaction, controlsTransaction: false) == .savepoint) + #expect(AgentTransactionGuard.protection(family: .cockroach, sessionState: .inTransaction, controlsTransaction: false) == .savepoint) + #expect(AgentTransactionGuard.protection(family: .duckdb, sessionState: .inTransaction, controlsTransaction: false) == .refuse) + #expect(AgentTransactionGuard.protection(family: .redshift, sessionState: .inTransaction, controlsTransaction: false) == .refuse) + #expect(AgentTransactionGuard.protection(family: .mysql, sessionState: .inTransaction, controlsTransaction: false) == .none) + #expect(AgentTransactionGuard.protection(family: .postgres, sessionState: .idle, controlsTransaction: false) == .none) + #expect(AgentTransactionGuard.protection(family: .postgres, sessionState: .inTransaction, controlsTransaction: true) == .none) + } + + @Test("A call of several writes joining an open transaction takes the engine's savepoint, and nothing else does") + func oneUnitSavepointFollowsTheFamily() { + func savepoint(_ family: TransactionEngineFamily, _ state: PluginSessionTransactionState, unit: Bool) + -> AgentTransactionGuard.Savepoint? { + let protection = AgentTransactionGuard.protection(family: family, sessionState: state, controlsTransaction: false) + return AgentTransactionGuard.savepoint(family: family, protection: protection, sessionState: state, asOneUnit: unit) + } + #expect(savepoint(.sqlite, .inTransaction, unit: true) == .standard) + #expect(savepoint(.mysql, .inTransaction, unit: true) == .standard) + #expect(savepoint(.sqlServer, .inTransaction, unit: true)?.open == "SAVE TRANSACTION tablepro_agent") + #expect(savepoint(.sqlServer, .inTransaction, unit: true)?.release == nil) + #expect(savepoint(.oracle, .inTransaction, unit: true)?.release == nil) + #expect(savepoint(.sqlite, .inTransaction, unit: false) == nil) + #expect(savepoint(.sqlite, .idle, unit: true) == nil) + #expect(savepoint(.other, .inTransaction, unit: true) == nil) + #expect(savepoint(.postgres, .inTransaction, unit: false) == .standard) + #expect(savepoint(.duckdb, .inTransaction, unit: true) == nil) + } + + @Test("Unseen transaction control: a T-SQL COMMIT inside a batch, an IF, a procedure call") + func unseenTransactionControlIsRecognised() { + let mssql = DatabaseType.mssql + for sql in ["UPDATE t SET a = 1 COMMIT", "IF @@TRANCOUNT > 0 ROLLBACK", "EXEC dbo.p", "SAVE TRAN s"] { + #expect(ManualCommit.mayEndTransactionUnseen([sql], databaseType: mssql, grammar: mssql.lexicalGrammar), "\(sql)") + } + let mysql = DatabaseType.mysql + #expect(ManualCommit.mayEndTransactionUnseen(["CALL p()"], databaseType: mysql, grammar: mysql.lexicalGrammar)) + for sql in ["UPDATE t SET a = CASE WHEN b = 1 THEN 1 ELSE 2 END", "SELECT 'commit'", "UPDATE t SET a = 1"] { + #expect(!ManualCommit.mayEndTransactionUnseen([sql], databaseType: mysql, grammar: mysql.lexicalGrammar), "\(sql)") + } + } + + @Test("Away from SQL Server, a column named like a transaction word is not transaction control", arguments: [ + DatabaseType.postgresql, .mysql, + ]) + func transactionWordsAsColumnsPass(type: DatabaseType) { + let sql = ["SELECT release, commit FROM t"] + #expect(!ManualCommit.mayEndTransactionUnseen(sql, databaseType: type, grammar: type.lexicalGrammar)) + #expect(!ManualCommit.controlsTransaction(sql, databaseType: type, grammar: type.lexicalGrammar)) + } + + @Test("A reply says the transaction ended when it did, and that a held change is pending otherwise") + func agentNoticeFollowsTheEndState() { + #expect(AgentTransactionGuard.notice(startState: .inTransaction, endState: .idle, pendingMatters: true, openedHold: false) + == ManualCommit.transactionEndedNotice) + #expect(AgentTransactionGuard.notice(startState: .inTransaction, endState: .idle, pendingMatters: false, openedHold: false) + == ManualCommit.transactionEndedNotice) + #expect(AgentTransactionGuard.notice(startState: .idle, endState: .idle, pendingMatters: true, openedHold: true) + == ManualCommit.transactionEndedNotice) + #expect(AgentTransactionGuard.notice(startState: .idle, endState: .inTransaction, pendingMatters: true, openedHold: true) + == ManualCommit.pendingNotice) + #expect(AgentTransactionGuard.notice( + startState: .inTransaction, endState: .inTransaction, pendingMatters: false, openedHold: false + ) == nil) + } + + @Test("The refusal says how to run the change") + func refusalNamesTheWayOut() { + #expect( + ManualCommitRefusal().errorDescription + == "This connection can't hold this change until you commit, so it did not run. Turn off Manual Commit in the Database menu to run it." + ) + #expect(ManualCommit.Decision.refuseMixed.refusal?.localizedDescription == ManualCommitMixedRefusal().errorDescription) + } +} + +struct ConnectionCommitModeStorageTests { + @Test("Manual Commit round-trips through the connection's fields, and Automatic removes the key") + func commitModeRoundTrips() { + var connection = TestFixtures.makeConnection(type: .postgresql) + #expect(connection.commitMode == .automatic) + #expect(connection.additionalFields[DatabaseConnection.commitModeKey] == nil) + + connection.commitMode = .manual + #expect(connection.additionalFields[DatabaseConnection.commitModeKey] == "manual") + #expect(connection.commitMode == .manual) + + connection.commitMode = .automatic + #expect(connection.additionalFields[DatabaseConnection.commitModeKey] == nil) + } + + @Test("A session starts in the connection's commit mode") + func sessionSeedsFromTheConnection() { + var connection = TestFixtures.makeConnection(type: .postgresql) + connection.commitMode = .manual + #expect(ConnectionSession(connection: connection).commitMode == .manual) + } + + @MainActor + @Test("The form's Manual Commit toggle is off and refuses a change where the engine can't hold") + func formToggleIsDisabledWhereUnavailable() { + let coordinator = ConnectionFormCoordinator(connectionId: nil) + coordinator.network.type = .mysql + coordinator.customization.commitMode = .manual + #expect(!coordinator.isManualCommitAvailable) + #expect(!coordinator.effectiveManualCommit) + coordinator.effectiveManualCommit = false + #expect(coordinator.customization.commitMode == .manual) + + coordinator.network.type = .postgresql + #expect(coordinator.isManualCommitAvailable) + #expect(coordinator.effectiveManualCommit) + coordinator.effectiveManualCommit = false + #expect(coordinator.customization.commitMode == .automatic) + } + + @Test("The form writes Manual Commit into the saved fields, and turning it off removes the key") + func formEditsCarryTheMode() { + var base = TestFixtures.makeConnection(type: .postgresql) + base.commitMode = .manual + #expect(ConnectionFormEditsSample.edits(commitMode: .automatic).applied(to: base).commitMode == .automatic) + let saved = ConnectionFormEditsSample.edits(commitMode: .manual).applied(to: TestFixtures.makeConnection()) + #expect(saved.additionalFields[DatabaseConnection.commitModeKey] == "manual") + } +} + +private enum ConnectionFormEditsSample { + static func edits(commitMode: ConnectionCommitMode) -> ConnectionFormEdits { + ConnectionFormEdits( + name: "Edited", + host: "db.example.com", + port: 6_000, + database: "edited_db", + username: "edited_user", + type: .postgresql, + sshConfig: SSHConfiguration(), + sslConfig: SSLConfiguration(), + color: .blue, + iconName: nil, + tagIds: [], + groupId: nil, + sshProfileId: nil, + sshTunnelMode: .disabled, + credentialMode: .inline, + cloudflareTunnelMode: .disabled, + cloudSQLProxyMode: .disabled, + socksProxyMode: .disabled, + tunnelCommandMode: .disabled, + safeModeLevel: .silent, + aiPolicy: nil, + aiRules: nil, + externalAccess: .readOnly, + commitMode: commitMode, + redisDatabase: nil, + startupCommands: nil, + localOnly: false, + additionalFields: [:], + ownedAdditionalFieldIDs: ConnectionFormEdits.appManagedAdditionalFieldIDs + ) + } +} diff --git a/apple/TableProTests/Core/Services/Execution/MultiStatementFailureTests.swift b/apple/TableProTests/Core/Services/Execution/MultiStatementFailureTests.swift index 6b31814b93..8e464169e4 100644 --- a/apple/TableProTests/Core/Services/Execution/MultiStatementFailureTests.swift +++ b/apple/TableProTests/Core/Services/Execution/MultiStatementFailureTests.swift @@ -107,7 +107,7 @@ struct MultiStatementFailureTests { ) #expect(firstStatement.message == "Statement 1/3 failed: database is locked") - for plan in [BatchTransactionPlan.appTransaction, .scriptTransaction] { + for plan in [BatchTransactionPlan.appTransaction, .heldTransaction, .scriptTransaction] { let rolledBack = Self.report( .statement(sql: "VACUUM"), executed: 2, @@ -228,7 +228,7 @@ struct MultiStatementFailureTests { /// `.appTransaction` and a script's own commit under `.scriptTransaction` are both unanswered. @Test( "The unknown-outcome wording does not change with the plan", - arguments: [BatchTransactionPlan.appTransaction, .scriptTransaction, .sessionTransaction, .autocommit] + arguments: [BatchTransactionPlan.appTransaction, .heldTransaction, .scriptTransaction, .sessionTransaction, .autocommit] ) func unknownCommitOutcomeReadsTheSameUnderEveryPlan(plan: BatchTransactionPlan) { let report = Self.report( diff --git a/apple/TableProTests/Core/Services/Infrastructure/QuitTransactionPromptTests.swift b/apple/TableProTests/Core/Services/Infrastructure/QuitTransactionPromptTests.swift new file mode 100644 index 0000000000..d2d1425135 --- /dev/null +++ b/apple/TableProTests/Core/Services/Infrastructure/QuitTransactionPromptTests.swift @@ -0,0 +1,56 @@ +// +// QuitTransactionPromptTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +@MainActor +struct QuitTransactionPromptTests { + @Test("Quit names the connection holding a transaction and offers Quit Anyway") + func quitNamesTheOpenTransaction() throws { + let prompt = try #require(QuitApproval.transactionPrompt(action: .quit, connections: ["Prod"], hasUnsavedTabs: false)) + #expect(prompt.title == "Quit with an Open Transaction?") + #expect(prompt.message == "Uncommitted changes on “Prod” are rolled back when TablePro quits.") + #expect(prompt.confirmButton == "Quit Anyway") + } + + @Test("Unsaved tabs ride along on the transaction question rather than asking again") + func unsavedTabsRideAlong() throws { + let prompt = try #require(QuitApproval.transactionPrompt(action: .quit, connections: ["Prod"], hasUnsavedTabs: true)) + #expect(prompt.message.hasSuffix("Some tabs have unsaved edits. Quitting will discard these changes.")) + } + + @Test("No open transaction asks nothing") + func noTransactionAsksNothing() { + #expect(QuitApproval.transactionPrompt(action: .quit, connections: [], hasUnsavedTabs: true) == nil) + } + + @Test("Each connection holding a transaction is named once, whatever its lanes") + func holdsAreNamedOncePerConnection() { + let prod = UUID() + let staging = UUID() + let driver = PluginDriverAdapter(connection: TestFixtures.makeConnection(), pluginDriver: QuitStubDriver()) + let tracker = SessionTransactionTracker { _, _ in true } + tracker.record(.inTransaction, on: driver, for: .init(connectionId: prod, lane: "sales")) + tracker.record(.abortedTransaction, on: driver, for: .init(connectionId: prod, lane: "hr")) + tracker.record(.inTransaction, on: driver, for: .init(connectionId: staging, lane: nil)) + let names = [prod: "Prod", staging: "Staging"] + + let named = QuitApproval.openTransactionNames( + holds: Array(tracker.holds.values), + connectionName: { names[$0] } + ) + + #expect(named == ["Prod", "Staging"]) + } +} + +private final class QuitStubDriver: PluginDriverStub, @unchecked Sendable { + var supportsSchemas: Bool { false } + var currentSchema: String? { nil } + var serverVersion: String? { nil } +} diff --git a/apple/TableProTests/Core/Services/Query/DatabaseTreeCatalogRefreshPlanTests.swift b/apple/TableProTests/Core/Services/Query/DatabaseTreeCatalogRefreshPlanTests.swift index 4acaa2c342..5c17abcde2 100644 --- a/apple/TableProTests/Core/Services/Query/DatabaseTreeCatalogRefreshPlanTests.swift +++ b/apple/TableProTests/Core/Services/Query/DatabaseTreeCatalogRefreshPlanTests.swift @@ -200,4 +200,74 @@ struct DatabaseTreeCatalogRefreshPlanTests { #expect(result.allSchemaTables.isEmpty) #expect(result.isEmpty) } + + private typealias ServerObjectsKey = DatabaseTreeMetadataService.ServerObjectsKey + + private func serverObjectPlan(_ change: CatalogChange, keys: [ServerObjectsKey]) -> CatalogTreeRefreshPlan { + DatabaseTreeMetadataService.catalogRefreshPlan( + for: change, + hasDatabaseList: false, + schemaListKeys: [], + tableKeys: [], + routineKeys: [], + triggerKeys: [], + typeKeys: [], + serverObjectKeys: keys + ) + } + + @Test("An event change marks the container's events stale and leaves another database current") + func eventChangeReachesItsContainer() { + let shopEvents = ServerObjectsKey(connectionId: connectionId, database: "shop", schema: nil, kindId: "event") + let warehouseEvents = ServerObjectsKey(connectionId: connectionId, database: "warehouse", schema: nil, kindId: "event") + let otherConnection = ServerObjectsKey(connectionId: UUID(), database: "shop", schema: nil, kindId: "event") + + let result = serverObjectPlan( + CatalogChange(connectionId: connectionId, database: "shop", kinds: [.triggers, .serverObjects]), + keys: [shopEvents, warehouseEvents, otherConnection] + ) + + #expect(result.serverObjects == [shopEvents]) + #expect(!result.isEmpty) + } + + @Test("A database-wide list is reached by a change made in any of its schemas") + func databaseScopedListIsReachedFromASchema() { + let extensions = ServerObjectsKey(connectionId: connectionId, database: "app", schema: nil, kindId: "extension") + let auditEvents = ServerObjectsKey(connectionId: connectionId, database: "app", schema: "audit", kindId: "event") + + let result = serverObjectPlan( + CatalogChange(connectionId: connectionId, database: "app", schema: "public", kinds: .objects), + keys: [extensions, auditEvents] + ) + + #expect(result.serverObjects == [extensions]) + } + + @Test("A container lists the declared kinds of its own level") + func serverKindsFollowTheContainerLevel() { + let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + let extensions = SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", iconName: "puzzlepiece.extension", + scope: .database + ) + let declared = [events, extensions] + + #expect(DatabaseTreeMetadataService.serverKinds(declared: declared, schema: "public", listsTablesPerSchema: true) == [events]) + #expect(DatabaseTreeMetadataService.serverKinds(declared: declared, schema: nil, listsTablesPerSchema: true) == [extensions]) + #expect(DatabaseTreeMetadataService.serverKinds(declared: declared, schema: nil, listsTablesPerSchema: false) == declared) + #expect(DatabaseTreeMetadataService.serverKinds(declared: declared, schema: "", listsTablesPerSchema: true) == [extensions]) + } + + @Test("A change that names no server object kind leaves those lists current") + func unrelatedChangeLeavesServerObjects() { + let shopEvents = ServerObjectsKey(connectionId: connectionId, database: "shop", schema: nil, kindId: "event") + + let result = serverObjectPlan(CatalogChange(connectionId: connectionId, database: "shop", kinds: .tables), keys: [shopEvents]) + + #expect(result.serverObjects.isEmpty) + #expect(result.isEmpty) + } } diff --git a/apple/TableProTests/Core/Storage/ConnectionLocalStatePurgeTests.swift b/apple/TableProTests/Core/Storage/ConnectionLocalStatePurgeTests.swift index ee2543d04e..206db0c446 100644 --- a/apple/TableProTests/Core/Storage/ConnectionLocalStatePurgeTests.swift +++ b/apple/TableProTests/Core/Storage/ConnectionLocalStatePurgeTests.swift @@ -256,4 +256,29 @@ struct ConnectionLocalStatePurgeTests { defaults.string(forKey: RowInspectorState.viewModeKey(kept)) == InspectorViewMode.json.rawValue ) } + + @Test("Purging a connection removes its query tabs' versions and leaves another's") + func purgeClearsQueryTabVersions() async { + let (history, _) = makeHistory() + let versions = QueryTabVersionStorage( + databaseURL: FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("purge_tab_versions_\(UUID().uuidString).db"), + announce: { _ in }, + removeDatabaseOnDeinit: true + ) + let deleted = UUID() + let kept = UUID() + let deletedTab = UUID() + let keptTab = UUID() + await versions.record(tabId: deletedTab, connectionId: deleted, text: "SELECT secret", reason: .run) + await versions.record(tabId: keptTab, connectionId: kept, text: "SELECT 1", reason: .run) + + await ConnectionLocalState.purgeAsyncStores( + [deleted], origin: .remote, queryHistory: history, queryTabVersions: versions + ) + + #expect(versions.summaries(tabId: deletedTab).isEmpty) + #expect(versions.summaries(tabId: keptTab).count == 1) + } } diff --git a/apple/TableProTests/Core/Storage/QueryTabVersionStorageTests.swift b/apple/TableProTests/Core/Storage/QueryTabVersionStorageTests.swift new file mode 100644 index 0000000000..b6d8f3c451 --- /dev/null +++ b/apple/TableProTests/Core/Storage/QueryTabVersionStorageTests.swift @@ -0,0 +1,255 @@ +// +// QueryTabVersionStorageTests.swift +// TableProTests +// + +import Foundation +import os +@testable import TablePro +import Testing + +struct QueryTabVersionStorageTests { + private static let start = Date(timeIntervalSince1970: 1_800_000_000) + + private let url: URL + private let now = OSAllocatedUnfairLock(initialState: QueryTabVersionStorageTests.start) + private let historyMaxDays = OSAllocatedUnfairLock(initialState: 90) + private let announced = OSAllocatedUnfairLock(initialState: [UUID?]()) + private let storage: QueryTabVersionStorage + private let tabId = UUID() + private let connectionId = UUID() + + init() { + let url = FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("query_tab_versions_\(UUID().uuidString).db") + self.url = url + storage = Self.makeStorage( + url: url, + now: now, + historyMaxDays: historyMaxDays, + announced: announced, + removeDatabaseOnDeinit: true + ) + } + + private static func makeStorage( + url: URL, + now: OSAllocatedUnfairLock, + historyMaxDays: OSAllocatedUnfairLock, + announced: OSAllocatedUnfairLock<[UUID?]>, + removeDatabaseOnDeinit: Bool + ) -> QueryTabVersionStorage { + QueryTabVersionStorage( + databaseURL: url, + now: { now.withLock { $0 } }, + historyMaxDays: { historyMaxDays.withLock { $0 } }, + announce: { tabId in announced.withLock { $0.append(tabId) } }, + removeDatabaseOnDeinit: removeDatabaseOnDeinit + ) + } + + private func reopenedStorage() -> QueryTabVersionStorage { + Self.makeStorage( + url: url, + now: now, + historyMaxDays: historyMaxDays, + announced: announced, + removeDatabaseOnDeinit: false + ) + } + + private func advance(by seconds: TimeInterval) { + now.withLock { $0 = $0.addingTimeInterval(seconds) } + } + + @discardableResult + private func record(_ text: String, _ reason: QueryTabVersionReason = .run, tab: UUID? = nil) async -> Bool { + await storage.record(tabId: tab ?? tabId, connectionId: connectionId, text: text, reason: reason) + } + + private func texts(tab: UUID? = nil) -> [String] { + let id = tab ?? tabId + return storage.summaries(tabId: id, limit: 200).compactMap { storage.version(id: $0.id, tabId: id)?.query } + } + + @Test("Blank text is not recorded") + func blankTextIsSkipped() async { + #expect(await record(" \n\t ") == false) + #expect(storage.summaries(tabId: tabId).isEmpty) + } + + @Test("Text larger than a tab can persist is not recorded") + func oversizeTextIsSkipped() async { + let oversize = String(repeating: "x", count: TabQueryContent.maxPersistableQuerySize + 1) + #expect(await record(oversize) == false) + #expect(storage.summaries(tabId: tabId).isEmpty) + } + + @Test("Text identical to the tab's newest version is not recorded again") + func duplicateOfNewestIsSkipped() async { + #expect(await record("SELECT 1")) + #expect(await record("SELECT 1", .beforeReplace) == false) + #expect(await record("SELECT 2")) + #expect(await record("SELECT 1")) + #expect(texts() == ["SELECT 1", "SELECT 2", "SELECT 1"]) + } + + @Test("A tab keeps its 50 newest versions") + func capKeepsTheNewest() async { + for index in 1...55 { + await record("SELECT \(index)") + } + let kept = texts() + #expect(kept.count == QueryTabVersionStorage.retainedVersionCount) + #expect(kept.first == "SELECT 55") + #expect(kept.last == "SELECT 6") + } + + @Test("The cap counts each tab on its own") + func capIsPerTab() async { + let other = UUID() + for index in 1...55 { + await record("SELECT \(index)") + } + await record("SELECT other", tab: other) + #expect(texts(tab: other) == ["SELECT other"]) + } + + @Test("Versions older than 30 days are gone, read or written") + func versionsExpireAfterThirtyDays() async { + await record("SELECT 1") + advance(by: 29 * 86_400) + await record("SELECT 2") + #expect(texts() == ["SELECT 2", "SELECT 1"]) + + advance(by: 2 * 86_400) + #expect(texts() == ["SELECT 2"]) + + await record("SELECT 3") + let reopened = reopenedStorage() + #expect(reopened.summaries(tabId: tabId).count == 2) + } + + @Test("A shorter Query History window shortens the retention", arguments: [(7, 7), (90, 30), (0, 30)]) + func historyWindowCapsRetention(historyDays: Int, expected: Int) async { + historyMaxDays.withLock { $0 = historyDays } + #expect(storage.retentionDays == expected) + + await record("SELECT 1") + advance(by: TimeInterval(expected) * 86_400 + 1) + #expect(storage.summaries(tabId: tabId).isEmpty) + } + + @Test("Idle snapshots within five minutes of the first become one version holding the latest text") + func idleSnapshotsCoalesce() async throws { + await record("SELECT 1", .idle) + advance(by: 60) + await record("SELECT 12", .idle) + advance(by: 200) + await record("SELECT 123", .idle) + + let summaries = storage.summaries(tabId: tabId) + let only = try #require(summaries.first) + #expect(summaries.count == 1) + #expect(only.reason == .idle) + #expect(only.savedAt == Self.start.addingTimeInterval(260)) + #expect(texts() == ["SELECT 123"]) + } + + @Test("Steady typing still leaves a version every five minutes") + func idleWindowCountsFromTheFirstSnapshot() async { + await record("SELECT 1", .idle) + for step in 2...4 { + advance(by: 120) + await record("SELECT \(step)", .idle) + } + #expect(texts() == ["SELECT 4", "SELECT 3"]) + } + + @Test("A run between idle snapshots keeps both sides of it") + func runBreaksTheIdleWindow() async { + await record("SELECT 1", .idle) + advance(by: 10) + await record("SELECT 2", .run) + advance(by: 10) + await record("SELECT 3", .idle) + #expect(texts() == ["SELECT 3", "SELECT 2", "SELECT 1"]) + #expect(storage.summaries(tabId: tabId).map { $0.reason } == [.idle, .run, .idle]) + } + + @Test("A restore of the idle text seals it, so the next pause cannot overwrite it") + func restoreSealsTheIdleVersion() async { + await record("SELECT before", .idle) + advance(by: 10) + await record("SELECT before", .beforeRestore) + advance(by: 10) + await record("SELECT restored", .idle) + #expect(texts() == ["SELECT restored", "SELECT before"]) + #expect(storage.summaries(tabId: tabId).map { $0.reason } == [.idle, .beforeRestore]) + } + + @Test("A coalesced pause takes the connection the tab moved to") + func coalescedPauseFollowsTheConnection() async { + let moved = UUID() + await record("SELECT 1", .idle) + advance(by: 10) + _ = await storage.record(tabId: tabId, connectionId: moved, text: "SELECT 2", reason: .idle) + #expect(await storage.removeVersions(forConnection: moved)) + #expect(storage.summaries(tabId: tabId).isEmpty) + } + + @Test("A run, a replace and a restore always add a version") + func otherReasonsAlwaysInsert() async { + await record("SELECT 1", .run) + await record("SELECT 2", .beforeReplace) + await record("SELECT 3", .beforeRestore) + #expect(storage.summaries(tabId: tabId).map { $0.reason } == [.beforeRestore, .beforeReplace, .run]) + } + + @Test("Deleting a connection removes its tabs' versions and leaves another's") + func purgeByConnection() async { + let otherTab = UUID() + await record("SELECT 1") + _ = await storage.record(tabId: otherTab, connectionId: UUID(), text: "SELECT kept", reason: .run) + + #expect(await storage.removeVersions(forConnection: connectionId)) + #expect(storage.summaries(tabId: tabId).isEmpty) + #expect(texts(tab: otherTab) == ["SELECT kept"]) + } + + @Test("Clear History removes every version") + func removeAllClearsEveryTab() async { + let otherTab = UUID() + await record("SELECT 1") + await record("SELECT 2", tab: otherTab) + + #expect(await storage.removeAll()) + #expect(storage.summaries(tabId: tabId).isEmpty) + #expect(storage.summaries(tabId: otherTab).isEmpty) + } + + @Test("Each write is announced with its tab, and a purge with none") + func writesAreAnnounced() async { + await record("SELECT 1") + await record("SELECT 1") + await storage.removeAll() + #expect(announced.withLock { $0 } == [tabId, nil]) + } + + @Test("A second store on the same file reads what the first wrote") + func secondInstanceReadsTheRows() async throws { + await record("SELECT 1") + let second = reopenedStorage() + + let summary = try #require(second.summaries(tabId: tabId).first) + #expect(second.version(id: summary.id, tabId: tabId)?.query == "SELECT 1") + #expect(second.version(id: summary.id, tabId: UUID()) == nil) + } + + @Test("Before the first write there is nothing to read and no file is made") + func readBeforeAnyWriteIsEmpty() { + #expect(storage.summaries(tabId: tabId).isEmpty) + #expect(!FileManager.default.fileExists(atPath: url.path(percentEncoded: false))) + } +} diff --git a/apple/TableProTests/Core/VersionHistory/QueryTabVersionHistoryProviderTests.swift b/apple/TableProTests/Core/VersionHistory/QueryTabVersionHistoryProviderTests.swift new file mode 100644 index 0000000000..cc68ed7f09 --- /dev/null +++ b/apple/TableProTests/Core/VersionHistory/QueryTabVersionHistoryProviderTests.swift @@ -0,0 +1,158 @@ +// +// QueryTabVersionHistoryProviderTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +@MainActor +private final class FakeQueryTab { + let connectionId = UUID() + var text: String? + var isCapturePaused = false + private(set) var writes: [String] = [] + private(set) var reasonsAtWrite: [[QueryTabVersionReason]] = [] + + init(text: String?) { + self.text = text + } + + func write(_ newText: String, storage: QueryTabVersionStorage, tabId: UUID) -> Bool { + guard text != nil else { return false } + reasonsAtWrite.append(storage.summaries(tabId: tabId).map { $0.reason }) + writes.append(newText) + text = newText + return true + } +} + +@MainActor +struct QueryTabVersionHistoryProviderTests { + private let storage = QueryTabVersionStorage( + databaseURL: FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("query_tab_provider_\(UUID().uuidString).db"), + announce: { _ in }, + removeDatabaseOnDeinit: true + ) + private let tabId = UUID() + + private func provider(for tab: FakeQueryTab) -> QueryTabVersionHistoryProvider { + QueryTabVersionHistoryProvider( + tabId: tabId, + storage: storage, + liveTab: { tab.text.map { LiveQueryTab(connectionId: tab.connectionId, text: $0) } }, + write: { tab.write($0, storage: storage, tabId: tabId) }, + isCapturePaused: { tab.isCapturePaused } + ) + } + + private func recordPast(_ texts: [String], connectionId: UUID) async { + for text in texts { + await storage.record(tabId: tabId, connectionId: connectionId, text: text, reason: .run) + } + } + + @Test("The page is the current text, then each version newest first, with the retention notice") + func pageListsCurrentThenNewestFirst() async throws { + let tab = FakeQueryTab(text: "SELECT 3") + await recordPast(["SELECT 1", "SELECT 2"], connectionId: tab.connectionId) + + let page = try await provider(for: tab).loadHistory() + + #expect(page.entries.count == 3) + #expect(page.entries.first?.reference == .current) + #expect(page.entries.dropFirst().allSatisfy { $0.summary == String(localized: "Run") }) + #expect(page.notice == .keepsRecentVersions(count: 50, days: storage.retentionDays)) + let newest = try #require(page.entries.dropFirst().first) + #expect(try await provider(for: tab).content(of: newest.reference) == "SELECT 2") + } + + @Test("The current entry reads the tab's live text") + func currentContentIsTheLiveText() async throws { + let tab = FakeQueryTab(text: "SELECT live") + #expect(try await provider(for: tab).content(of: .current) == "SELECT live") + } + + @Test("A version of another tab, or one that is gone, is not found") + func foreignVersionIsNotFound() async throws { + let tab = FakeQueryTab(text: "SELECT 1") + let otherTab = UUID() + await storage.record(tabId: otherTab, connectionId: tab.connectionId, text: "SELECT other", reason: .run) + let otherId = try #require(storage.summaries(tabId: otherTab).first?.id) + + await #expect(throws: VersionHistoryError.versionNotFound) { + try await provider(for: tab).content(of: .tabVersion(id: otherId)) + } + await #expect(throws: VersionHistoryError.versionNotFound) { + try await provider(for: tab).prepareRestore(.tabVersion(id: 999)) + } + } + + @Test("Restore records the text it replaces first, then writes the version into the tab") + func restoreRecordsThenWrites() async throws { + let tab = FakeQueryTab(text: "SELECT 1") + await recordPast(["SELECT 1"], connectionId: tab.connectionId) + let version = try #require(storage.summaries(tabId: tabId).first) + tab.text = "SELECT typed since" + + let plan = try await provider(for: tab).prepareRestore(.tabVersion(id: version.id)) + #expect(plan.replacesUncommittedChanges == false) + try await plan.apply() + + #expect(tab.writes == ["SELECT 1"]) + #expect(tab.reasonsAtWrite == [[.beforeRestore, .run]]) + let replaced = try #require(storage.summaries(tabId: tabId).first) + #expect(storage.version(id: replaced.id, tabId: tabId)?.query == "SELECT typed since") + } + + @Test("With capture paused, restore writes the version and records nothing") + func pausedRestoreRecordsNothing() async throws { + let tab = FakeQueryTab(text: "SELECT 1") + await recordPast(["SELECT 1"], connectionId: tab.connectionId) + let version = try #require(storage.summaries(tabId: tabId).first) + tab.text = "SELECT typed since" + tab.isCapturePaused = true + + try await provider(for: tab).prepareRestore(.tabVersion(id: version.id)).apply() + + #expect(tab.writes == ["SELECT 1"]) + #expect(storage.summaries(tabId: tabId).map { $0.id } == [version.id]) + } + + @Test("A closed tab says so for its list, its text and a restore") + func closedTabThrowsTabClosed() async throws { + let tab = FakeQueryTab(text: nil) + await recordPast(["SELECT 1"], connectionId: tab.connectionId) + let version = try #require(storage.summaries(tabId: tabId).first) + + await #expect(throws: VersionHistoryError.tabClosed) { try await provider(for: tab).loadHistory() } + await #expect(throws: VersionHistoryError.tabClosed) { try await provider(for: tab).content(of: .current) } + await #expect(throws: VersionHistoryError.tabClosed) { + try await provider(for: tab).prepareRestore(.tabVersion(id: version.id)) + } + #expect(try await provider(for: tab).content(of: .tabVersion(id: version.id)) == "SELECT 1") + } + + @Test("A tab that closes between prepare and apply is not written") + func tabClosedBeforeApplyIsNotWritten() async throws { + let tab = FakeQueryTab(text: "SELECT 2") + await recordPast(["SELECT 1"], connectionId: tab.connectionId) + let version = try #require(storage.summaries(tabId: tabId).first) + let plan = try await provider(for: tab).prepareRestore(.tabVersion(id: version.id)) + tab.text = nil + + await #expect(throws: VersionHistoryError.tabClosed) { try await plan.apply() } + #expect(tab.writes.isEmpty) + } + + @Test("Each reason names its version, and an idle snapshot is titled by its date") + func reasonSummaries() { + #expect(QueryTabVersionReason.run.summary == String(localized: "Run")) + #expect(QueryTabVersionReason.beforeReplace.summary == String(localized: "Before Replace")) + #expect(QueryTabVersionReason.beforeRestore.summary == String(localized: "Before Restore")) + #expect(QueryTabVersionReason.idle.summary == nil) + } +} diff --git a/apple/TableProTests/Core/VersionHistory/QueryTabVersionSubjectTests.swift b/apple/TableProTests/Core/VersionHistory/QueryTabVersionSubjectTests.swift new file mode 100644 index 0000000000..b1ad41bca3 --- /dev/null +++ b/apple/TableProTests/Core/VersionHistory/QueryTabVersionSubjectTests.swift @@ -0,0 +1,66 @@ +// +// QueryTabVersionSubjectTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct QueryTabVersionSubjectTests { + @Test("A history tab for a query tab round-trips the tab it shows") + func queryTabSubjectRoundTrips() throws { + let subject = VersionHistorySubject.queryTab(id: UUID()) + let tab = PersistedTab( + id: UUID(), + title: "History: Query 1", + query: "", + tabType: .versionHistory, + tableName: nil, + versionHistorySubject: subject + ) + + let decoded = try JSONDecoder().decode(PersistedTab.self, from: try JSONEncoder().encode(tab)) + + #expect(decoded.tabType == .versionHistory) + #expect(decoded.versionHistorySubject == subject) + } + + @Test("The saved query and file subjects keep the encoding older builds wrote") + func existingSubjectsDecodeUnchanged() throws { + let favoriteId = UUID() + let json = Data(#"{"savedQuery":{"id":"\#(favoriteId.uuidString)"}}"#.utf8) + #expect(try JSONDecoder().decode(VersionHistorySubject.self, from: json) == .savedQuery(id: favoriteId)) + } + + @Test("The notice names both the count and the days") + func noticeNamesCountAndDays() { + let text = VersionHistoryFormatting.noticeText(.keepsRecentVersions(count: 50, days: 7)) + #expect(text == String( + format: String(localized: "Keeps up to %1$d versions from the last %2$d days on this Mac."), + 50, + 7 + )) + } + + @Test("A Revert To row leads with the date, then names a run") + func menuTitleLeadsWithTheDate() { + let date = Date(timeIntervalSince1970: 1_800_000_000) + let dateText = date.formatted(date: .abbreviated, time: .shortened) + let run = VersionHistoryEntry(reference: .tabVersion(id: 1), summary: String(localized: "Run"), date: date) + let idle = VersionHistoryEntry(reference: .tabVersion(id: 2), summary: nil, date: date) + + #expect(VersionHistoryFormatting.menuTitle(for: run) == String( + format: String(localized: "%1$@ (%2$@)"), + dateText, + String(localized: "Run") + )) + #expect(VersionHistoryFormatting.menuTitle(for: idle) == dateText) + } + + @Test("A closed tab's message tells the reader how to get the history back") + func closedTabMessage() { + #expect(VersionHistoryError.tabClosed.errorDescription + == String(localized: "This tab is closed. Reopen it to see its history.")) + } +} diff --git a/apple/TableProTests/Models/DatabaseObjectRefCodingTests.swift b/apple/TableProTests/Models/DatabaseObjectRefCodingTests.swift index 65ee4448c4..c7c843e7b3 100644 --- a/apple/TableProTests/Models/DatabaseObjectRefCodingTests.swift +++ b/apple/TableProTests/Models/DatabaseObjectRefCodingTests.swift @@ -32,4 +32,102 @@ struct DatabaseObjectRefCodingTests { #expect(ref.resolvingDatabase("shop").typeKind == .range) #expect(ref.resolvingDatabase("shop").database == "shop") } + + private var eventKind: SidebarServerObjectKind { + SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + } + + @Test("A ref written before server objects decodes with no server kind") + func legacyRefHasNoServerKind() throws { + let json = """ + {"kind":"trigger","name":"audit","database":"shop","schema":"public","table":"orders","attributes":[]} + """ + let decoded = try JSONDecoder().decode(DatabaseObjectRef.self, from: Data(json.utf8)) + #expect(decoded.serverKind == nil) + #expect(decoded.serverObject == nil) + } + + @Test("A server object ref round-trips its kind and reads back as the object") + func serverObjectRefRoundTrips() throws { + let object = ServerObjectInfo( + kindId: "event", name: "nightly", schema: "shop", detail: "Recurring", + attributes: [ObjectAttribute(label: "Schedule", value: "EVERY 1 DAY")] + ) + let ref = DatabaseObjectRef(serverObject: object, kind: eventKind, database: "shop") + let decoded = try JSONDecoder().decode(DatabaseObjectRef.self, from: JSONEncoder().encode(ref)) + #expect(decoded == ref) + #expect(decoded.kind == .serverObject) + #expect(decoded.serverKind?.id == "event") + #expect(decoded.kindDisplayName == "Event") + #expect(decoded.kindIconName == "calendar.badge.clock") + #expect(decoded.serverObject?.kindId == "event") + #expect(decoded.serverObject?.name == "nightly") + #expect(decoded.displayIdentity == "shop.nightly") + } + + @Test("Resolving the database keeps the server kind, and the tab is titled with the kind") + @MainActor + func serverObjectRefKeepsKind() { + let ref = DatabaseObjectRef(serverObject: ServerObjectInfo(kindId: "event", name: "nightly"), kind: eventKind, database: "") + #expect(ref.resolvingDatabase("shop").serverKind?.id == "event") + #expect(QueryTabManager.objectSourceTitle(for: ref) == String(format: String(localized: "%1$@: %2$@"), "Event", "nightly")) + } + + private var extensionKind: SidebarServerObjectKind { + SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", iconName: "puzzlepiece.extension", + scope: .database + ) + } + + @Test("A database-wide object is titled by its bare name, its schema left to the attributes") + @MainActor + func databaseScopedObjectHasABareName() throws { + let object = ServerObjectInfo(kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384") + let ref = DatabaseObjectRef(serverObject: object, kind: extensionKind, database: "app") + + #expect(ref.isDatabaseScoped) + #expect(ref.displayIdentity == "pgcrypto") + #expect(QueryTabManager.objectSourceTitle(for: ref) == String(format: String(localized: "%1$@: %2$@"), "Extension", "pgcrypto")) + #expect(ref.schema == "public") + + let decoded = try JSONDecoder().decode(DatabaseObjectRef.self, from: JSONEncoder().encode(ref)) + #expect(decoded.serverKind?.scope == .database) + #expect(decoded.displayIdentity == "pgcrypto") + } + + @Test("A container object keeps its schema in its name") + func containerObjectIsQualified() { + let ref = DatabaseObjectRef(serverObject: ServerObjectInfo(kindId: "event", name: "nightly", schema: "shop"), kind: eventKind, database: "shop") + + #expect(!ref.isDatabaseScoped) + #expect(ref.displayIdentity == "shop.nightly") + } + + @Test("A server kind written without its scope decodes, and reads as a container kind") + func serverKindWithoutScopeDecodes() throws { + let json = """ + {"kind":"serverObject","name":"nightly","database":"shop","schema":"shop","attributes":[],\ + "serverKind":{"id":"event","displayName":"Event","iconName":"calendar.badge.clock"}} + """ + let decoded = try JSONDecoder().decode(DatabaseObjectRef.self, from: Data(json.utf8)) + + #expect(decoded.serverKind?.scope == nil) + #expect(!decoded.isDatabaseScoped) + #expect(decoded.displayIdentity == "shop.nightly") + } + + @Test("Two server objects of different kinds are different objects") + func serverKindSeparatesObjects() { + let extensionKind = SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", iconName: "puzzlepiece.extension", + scope: .database + ) + let event = DatabaseObjectRef(serverObject: ServerObjectInfo(kindId: "event", name: "x"), kind: eventKind, database: "shop") + let ext = DatabaseObjectRef(serverObject: ServerObjectInfo(kindId: "extension", name: "x"), kind: extensionKind, database: "shop") + #expect(!event.namesSameObject(as: ext)) + #expect(event.namesSameObject(as: event)) + } } diff --git a/apple/TableProTests/Models/ERDiagram/ERDiagramStagedOverlayTests.swift b/apple/TableProTests/Models/ERDiagram/ERDiagramStagedOverlayTests.swift index 1fc9fd2f95..ab99fb6d4c 100644 --- a/apple/TableProTests/Models/ERDiagram/ERDiagramStagedOverlayTests.swift +++ b/apple/TableProTests/Models/ERDiagram/ERDiagramStagedOverlayTests.swift @@ -85,6 +85,26 @@ struct ERDiagramStagedOverlayTests { #expect(dropped.id == saved.id) } + @Test("A staged composite key draws one edge per column pair, each marked added") + func stagedCompositeKeyDrawsEachPair() { + let graph = savedGraph() + let orders = manager(for: "orders", in: graph) + var foreignKey = EditableForeignKeyDefinition.placeholder() + foreignKey.name = "fk_orders_customer" + foreignKey.columns = ["customer_id", "shipper_id"] + foreignKey.referencedTable = "customers" + foreignKey.referencedColumns = ["id", "email"] + orders.addForeignKey(foreignKey) + + let overlaid = ERDiagramStagedOverlay.apply( + ["orders": ERDiagramStagedOverlay.TableEdits(orders)], to: graph + ) + + let edges = overlaid.edges.filter { $0.fkName == "fk_orders_customer" } + #expect(edges.map { "\($0.fromColumn)->\($0.toColumn)" } == ["customer_id->id", "shipper_id->email"]) + #expect(edges.allSatisfy { $0.stagedEdit == .added && $0.toTable == "customers" }) + } + @Test("Added, renamed and dropped columns are marked on their table") func stagedColumnsAreMarked() throws { let graph = savedGraph() diff --git a/apple/TableProTests/Models/Query/TableAutoRefreshGateTests.swift b/apple/TableProTests/Models/Query/TableAutoRefreshGateTests.swift new file mode 100644 index 0000000000..438f644dbc --- /dev/null +++ b/apple/TableProTests/Models/Query/TableAutoRefreshGateTests.swift @@ -0,0 +1,83 @@ +// +// TableAutoRefreshGateTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct TableAutoRefreshGateTests { + @Test("A visible, idle, loaded table tab refreshes") + func idleTabRefreshes() { + #expect(TableAutoRefreshGate().decision == .refresh) + } + + @Test("Each state that a reload would break waits, with its own reason") + func eachPauseStateWaits() { + let cases: [(String, (inout TableAutoRefreshGate) -> Void, TableAutoRefreshPause)] = [ + ("window hidden", { $0.windowIsVisible = false }, .notVisible), + ("tab not selected", { $0.isSelectedInWindow = false }, .notVisible), + ("disconnected", { $0.isConnected = false }, .disconnected), + ("JSON mode", { $0.viewMode = .json }, .otherViewMode), + ("structure mode", { $0.viewMode = .structure }, .otherViewMode), + ("open transaction", { $0.holdsTransaction = true }, .openTransaction), + ("unsaved edits", { $0.holdsUnsavedWork = true }, .unsavedChanges), + ("sidebar staged drops", { $0.hasSidebarDataWork = true }, .unsavedChanges), + ("open cell editor", { $0.isEditingCell = true }, .editing), + ("grid popover or context menu", { $0.gridHoldsTransientUI = true }, .interacting), + ("inspector pop-out window", { $0.inspectorHoldsValueWindow = true }, .valueWindowOpen), + ("live scroll or drag", { $0.gridIsTrackingInteraction = true }, .interacting), + ("running load", { $0.load = .busy }, .loading), + ("pending count", { $0.isCountPending = true }, .loading), + ("load more", { $0.isLoadingMore = true }, .loading), + ("never loaded", { $0.hasLoaded = false }, .loading), + ("Safe Mode prompt", { $0.showsSafeModePrompt = true }, .safeModePrompt), + ("selection without a key", { gate in + gate.hasSelection = true + gate.hasPrimaryKey = false + }, .unkeyedSelection), + ] + for (name, configure, pause) in cases { + var gate = TableAutoRefreshGate() + configure(&gate) + #expect(gate.decision == .wait(pause), "\(name)") + } + } + + @Test("An open editor names the editor, not the unsaved work it also counts as") + func editorWinsOverUnsavedWork() { + var gate = TableAutoRefreshGate() + gate.isEditingCell = true + gate.holdsUnsavedWork = true + #expect(gate.decision == .wait(.editing)) + } + + @Test("A selection in a keyed table still refreshes, since the key brings it back") + func keyedSelectionRefreshes() { + var gate = TableAutoRefreshGate() + gate.hasSelection = true + #expect(gate.decision == .refresh) + } + + @Test("A failed tick and a tab that is no longer a table stop the schedule") + func failureAndNonTableStop() { + var failed = TableAutoRefreshGate() + failed.previousTickFailed = true + var query = TableAutoRefreshGate() + query.isTableTab = false + + #expect(failed.decision == .stop) + #expect(query.decision == .stop) + } + + @Test("Only the pauses a reader can act on name themselves in the help") + func pauseHelp() { + #expect(TableAutoRefreshPause.unsavedChanges.help == "Auto Refresh waits while the table has unsaved changes.") + #expect(TableAutoRefreshPause.disconnected.help == "Auto Refresh waits until the connection is back.") + #expect(TableAutoRefreshPause.openTransaction.help == "Auto Refresh waits while a transaction is open.") + #expect(TableAutoRefreshPause.valueWindowOpen.help == "Auto Refresh waits while a value is open in its own window.") + #expect(TableAutoRefreshPause.notVisible.help == nil) + #expect(TableAutoRefreshPause.interacting.help == nil) + } +} diff --git a/apple/TableProTests/Models/Query/TableAutoRefreshIntervalTests.swift b/apple/TableProTests/Models/Query/TableAutoRefreshIntervalTests.swift new file mode 100644 index 0000000000..68b519805f --- /dev/null +++ b/apple/TableProTests/Models/Query/TableAutoRefreshIntervalTests.swift @@ -0,0 +1,47 @@ +// +// TableAutoRefreshIntervalTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct TableAutoRefreshIntervalTests { + @Test("The presets run from five seconds to five minutes, after Off") + func presets() { + let seconds = TableAutoRefreshInterval.allCases.map { $0.rawValue } + #expect(seconds == [0, 5, 10, 30, 60, 300]) + #expect(TableAutoRefreshInterval.allCases.filter { !$0.isOn } == [.off]) + #expect(TableAutoRefreshInterval.oneMinute.duration == .seconds(60)) + } + + @Test("Each preset has its own title") + func titles() { + let titles = TableAutoRefreshInterval.allCases.map { $0.title } + #expect(titles == [ + "Off", "Every 5 Seconds", "Every 10 Seconds", "Every 30 Seconds", "Every Minute", "Every 5 Minutes", + ]) + } + + @Test("An auto refresh writes no history and keeps the count; every other load does both") + func autoRefreshTriggerSkipsHistoryAndCount() { + #expect(!TableLoadTrigger.autoRefresh.recordsHistory) + #expect(!TableLoadTrigger.autoRefresh.countsRows) + #expect(TableLoadTrigger.userInitiated.recordsHistory) + #expect(TableLoadTrigger.restore.countsRows) + } + + @Test("A table tab never saves its interval, so a reopened tab starts with it off") + func intervalIsSessionOnly() { + var tab = QueryTab(title: "jobs", query: "SELECT * FROM jobs", tabType: .table, tableName: "jobs") + tab.autoRefreshInterval = .fiveSeconds + + let reopened = QueryTab(from: tab.toPersistedTab(), defaultPageSize: 1_000) + var stopped = tab + stopped.autoRefreshInterval = .off + + #expect(reopened.autoRefreshInterval == .off) + #expect(tab != stopped, "a view keyed on the tab has to see the interval change") + } +} diff --git a/apple/TableProTests/Models/ResultStatusModelTests.swift b/apple/TableProTests/Models/ResultStatusModelTests.swift index f5a1cc3f23..19304f3637 100644 --- a/apple/TableProTests/Models/ResultStatusModelTests.swift +++ b/apple/TableProTests/Models/ResultStatusModelTests.swift @@ -290,6 +290,16 @@ struct ResultStatusModelTests { #expect(!structure.controls.showsPagination) } + @Test("Auto Refresh is offered on a table tab's data grid only") + func autoRefreshFollowsTheTableGrid() { + let table = makeSnapshot(rowCount: 10) + #expect(model(table, viewMode: .data).controls.showsAutoRefresh) + #expect(!model(table, viewMode: .json).controls.showsAutoRefresh) + #expect(!model(table, viewMode: .structure).controls.showsAutoRefresh) + #expect(!model(makeSnapshot(tabType: .query, rowCount: 10, hasTableName: false)).controls.showsAutoRefresh) + #expect(!model(makeSnapshot(rowCount: 10, hasTableName: false)).controls.showsAutoRefresh) + } + @Test("Highlight rules are offered only where the data grid draws the result") func highlightRulesFollowTheDataGrid() { let table = makeSnapshot(rowCount: 10) diff --git a/apple/TableProTests/Models/ResultStatusPresentationTests.swift b/apple/TableProTests/Models/ResultStatusPresentationTests.swift index f94955df69..2ff0e29653 100644 --- a/apple/TableProTests/Models/ResultStatusPresentationTests.swift +++ b/apple/TableProTests/Models/ResultStatusPresentationTests.swift @@ -35,6 +35,13 @@ struct ResultStatusPresentationTests { #expect(!presentation(.narrow).pageSizeIsInline) } + @Test("Only the narrowest tier leaves Auto Refresh to the Database menu") + func narrowLeavesAutoRefreshToTheMenu() { + #expect(presentation(.regular).showsAutoRefresh) + #expect(presentation(.compact).showsAutoRefresh) + #expect(!presentation(.narrow).showsAutoRefresh) + } + @Test("The narrowest tier swaps the segmented switcher for a pull-down") func narrowUsesAMenuSwitcher() { #expect(!presentation(.narrow).modeSwitcherIsSegmented) diff --git a/apple/TableProTests/Models/ShortcutUniquenessTests.swift b/apple/TableProTests/Models/ShortcutUniquenessTests.swift index 56c209ebbd..0011b93bda 100644 --- a/apple/TableProTests/Models/ShortcutUniquenessTests.swift +++ b/apple/TableProTests/Models/ShortcutUniquenessTests.swift @@ -60,6 +60,7 @@ struct ShortcutUniquenessTests { .newAgentSession, .openAgentSession, .closeAgentSession, .deleteAgentSession, .newAIConversation, .showPreviousWindowTab, .showNextWindowTab, .toggleBreakpoint, + .commitTransaction, .rollbackTransaction, ] let unbound = Set(ShortcutAction.allCases.filter { KeyboardSettings.defaultShortcuts[$0] == nil }) let unlisted = unbound.subtracting(listed).map(\.rawValue).sorted() diff --git a/apple/TableProTests/Models/Sidebar/SidebarObjectKindTests.swift b/apple/TableProTests/Models/Sidebar/SidebarObjectKindTests.swift index 2915e371b1..5295c6c304 100644 --- a/apple/TableProTests/Models/Sidebar/SidebarObjectKindTests.swift +++ b/apple/TableProTests/Models/Sidebar/SidebarObjectKindTests.swift @@ -69,12 +69,82 @@ struct SidebarObjectKindTests { @Test("Every kind belongs to exactly one category") func categoriesPartitionTheKinds() { - #expect(SidebarObjectKind.allCases.filter { $0.category == .routine } == [.procedure, .function]) - #expect(SidebarObjectKind.allCases.filter { $0.category == .trigger } == [.trigger]) - #expect(SidebarObjectKind.allCases.filter { $0.category == .type } == [.type]) + #expect(SidebarObjectKind.builtIn.filter { $0.category == .routine } == [.procedure, .function]) + #expect(SidebarObjectKind.builtIn.filter { $0.category == .trigger } == [.trigger]) + #expect(SidebarObjectKind.builtIn.filter { $0.category == .type } == [.type]) #expect( - SidebarObjectKind.allCases.filter { $0.category == .table } + SidebarObjectKind.builtIn.filter { $0.category == .table } == [.table, .view, .materializedView, .foreignTable, .sequence] ) + #expect(!SidebarObjectKind.builtIn.contains { $0.category == .server }) + #expect(SidebarObjectKind.server(events).category == .server) + } + + private let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + private let extensions = SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", iconName: "puzzlepiece.extension", + scope: .database + ) + + /// UserDefaults keys, the tree expansion JSON and node ids all spell these, so they never move. + @Test("A built-in kind persists under the name it shipped with") + func builtInPersistenceNamesAreShipped() { + #expect(SidebarObjectKind.builtIn.map { $0.persistenceName } == [ + "table", "view", "materializedView", "foreignTable", "sequence", "procedure", "function", "trigger", "type", + ]) + for kind in SidebarObjectKind.builtIn { + #expect(SidebarObjectKind(persistenceName: kind.persistenceName) == kind) + } + } + + @Test("A declared kind persists by its id and decodes without the driver that declared it") + func declaredKindRoundTripsByIdAlone() { + let kind = SidebarObjectKind.server(events) + #expect(kind.persistenceName == "server.event") + let decoded = SidebarObjectKind(persistenceName: "server.event") + #expect(decoded == kind) + #expect(decoded?.hashValue == kind.hashValue) + #expect(decoded?.serverKind?.id == "event") + } + + @Test("An unknown or empty name decodes to nothing") + func unknownPersistenceNameIsSkipped() { + #expect(SidebarObjectKind(persistenceName: "dictionary") == nil) + #expect(SidebarObjectKind(persistenceName: "server.") == nil) + #expect(SidebarObjectKind(persistenceName: "") == nil) + } + + @Test("Declared kinds follow the built-ins, in the order the driver declared them") + func declaredKindsFollowBuiltIns() { + let ordered = SidebarObjectKind.ordered(declared: [events, extensions]) + #expect(Array(ordered.prefix(SidebarObjectKind.builtIn.count)) == SidebarObjectKind.builtIn) + #expect(Array(ordered.suffix(2)) == [.server(events), .server(extensions)]) + } + + @Test("A declared kind is listed only for its rows or a kept failure, never for being declared") + func declaredKindVisibility() { + let counted: [SidebarObjectKind: Int] = [.trigger: 1, .server(SidebarServerObjectKind(id: "event")): 2] + #expect( + SidebarObjectKind.visible(itemCounts: counted, declared: [events, extensions]) + == [.trigger, .server(events)] + ) + #expect( + SidebarObjectKind.visible(itemCounts: [:], keeping: [.server(extensions)], declared: [events, extensions]) + == [.server(extensions)] + ) + #expect(SidebarObjectKind.visible(itemCounts: counted).isEmpty == false) + #expect(SidebarObjectKind.visible(itemCounts: counted) == [.trigger]) + } + + @Test("A declared kind reads its names and symbol from the driver's descriptor") + func declaredKindDisplay() { + let kind = SidebarObjectKind.server(extensions) + #expect(kind.displayName == "Extension") + #expect(kind.pluralDisplayName == "Extensions") + #expect(kind.iconName == "puzzlepiece.extension") + #expect(kind.title(tableEntityName: "Collections") == "Extensions") + #expect(!kind.isExpandedByDefault) } } diff --git a/apple/TableProTests/Services/SchemaServiceSideObjectsTests.swift b/apple/TableProTests/Services/SchemaServiceSideObjectsTests.swift index 8b86086c7d..c75a18f94f 100644 --- a/apple/TableProTests/Services/SchemaServiceSideObjectsTests.swift +++ b/apple/TableProTests/Services/SchemaServiceSideObjectsTests.swift @@ -23,6 +23,10 @@ final class SideObjectsMockDriver: DatabaseDriver, @unchecked Sendable { var schemasToReturn: [String] = [] var routineSchemaRequests: [String?] = [] var triggerSchemaRequests: [String?] = [] + var declaredServerKinds: [PluginServerObjectKind] = [] + var serverObjectsByKind: [String: [ServerObjectInfo]] = [:] + var serverObjectsError: Error? + var serverObjectRequests: [String] = [] init(connection: DatabaseConnection) { self.connection = connection @@ -94,6 +98,14 @@ final class SideObjectsMockDriver: DatabaseDriver, @unchecked Sendable { triggerSchemaRequests.append(schema) return triggersBySchema[schema ?? ""] ?? [] } + + var serverObjectKinds: [PluginServerObjectKind] { declaredServerKinds } + + func fetchServerObjects(ofKind kindId: String, schema: String?) async throws -> [ServerObjectInfo] { + serverObjectRequests.append("\(kindId)@\(schema ?? "")") + if let serverObjectsError { throw serverObjectsError } + return serverObjectsByKind[kindId] ?? [] + } } @MainActor @@ -316,4 +328,96 @@ struct SchemaServiceSideObjectsTests { #expect(service.routinesLoadState(for: connectionId, schema: "hr") == .idle) #expect(!service.isSchemaSettled(for: connectionId, schema: "hr")) } + + // MARK: - Server objects + + private var eventKind: PluginServerObjectKind { + PluginServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock" + ) + } + + private func event(_ name: String) -> ServerObjectInfo { + ServerObjectInfo(kindId: "event", name: name, schema: "shop", detail: "Recurring") + } + + @Test("A connection-wide load reads each declared kind beside the routines") + func loadReadsDeclaredKinds() async { + let (connectionId, connection, driver) = makeDriver() + driver.declaredServerKinds = [eventKind] + driver.serverObjectsByKind["event"] = [event("nightly")] + + let service = SchemaService() + await service.load(connectionId: connectionId, driver: driver, connection: connection) + + #expect(service.serverObjectsLoadState(for: connectionId, kindId: "event") == .loaded([event("nightly")])) + #expect(service.serverObjects(for: connectionId, kindId: "event").map { $0.name } == ["nightly"]) + #expect(driver.serverObjectRequests == ["event@"]) + } + + @Test("A driver that declares no kinds is never asked for server objects") + func undeclaredKindsAreNotFetched() async { + let (connectionId, connection, driver) = makeDriver() + + let service = SchemaService() + await service.load(connectionId: connectionId, driver: driver, connection: connection) + + #expect(driver.serverObjectRequests.isEmpty) + #expect(service.serverObjectsLoadState(for: connectionId, kindId: "event") == .idle) + } + + @Test("A server object fetch that fails is reported, and the tables stay loaded") + func failedServerObjectsAreReported() async { + let (connectionId, connection, driver) = makeDriver() + driver.declaredServerKinds = [eventKind] + driver.serverObjectsError = boom + driver.tablesToReturn = [TestFixtures.makeTableInfo(name: "orders", schema: "public")] + + let service = SchemaService() + await service.load(connectionId: connectionId, driver: driver, connection: connection) + + #expect(service.serverObjectsLoadState(for: connectionId, kindId: "event") == .failed("boom")) + #expect(service.tables(for: connectionId).map { $0.name } == ["orders"]) + } + + @Test("A failed server object reload keeps the objects already loaded") + func failedServerObjectReloadKeepsObjects() async { + let (connectionId, connection, driver) = makeDriver() + driver.declaredServerKinds = [eventKind] + driver.serverObjectsByKind["event"] = [event("nightly")] + let service = SchemaService() + await service.load(connectionId: connectionId, driver: driver, connection: connection) + + driver.serverObjectsError = boom + let reloaded = await service.reloadServerObjects(connectionId: connectionId, driver: driver, scope: nil, kindId: "event") + + #expect(!reloaded) + #expect(service.serverObjects(for: connectionId, kindId: "event").map { $0.name } == ["nightly"]) + + driver.serverObjectsError = nil + driver.serverObjectsByKind["event"] = [event("nightly"), event("hourly")] + #expect(await service.reloadServerObjects(connectionId: connectionId, driver: driver, scope: nil, kindId: "event")) + #expect(service.serverObjects(for: connectionId, kindId: "event").count == 2) + } + + @Test("Expanding a schema reads its container kinds, and a database kind is left to the database") + func schemaLoadReadsContainerKindsOnly() async { + let (connectionId, _, driver) = makeDriver() + driver.declaredServerKinds = [ + eventKind, + PluginServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", + iconName: "puzzlepiece.extension", scope: .database + ) + ] + driver.serverObjectsByKind["event"] = [event("nightly")] + let service = SchemaService() + + await service.loadSchemaObjects(schema: "hr", in: unnamedDatabase(connectionId), driver: driver) + + #expect(driver.serverObjectRequests == ["event@hr"]) + #expect(service.serverObjects(for: connectionId, schema: "hr", kindId: "event").map { $0.name } == ["nightly"]) + #expect(service.serverObjectsByKind(for: connectionId, schema: "hr").keys.sorted() == ["event"]) + #expect(service.isSchemaSettled(for: connectionId, schema: "hr")) + } } diff --git a/apple/TableProTests/ViewModels/ERDiagramForeignKeyDraftTests.swift b/apple/TableProTests/ViewModels/ERDiagramForeignKeyDraftTests.swift new file mode 100644 index 0000000000..b01bed83a8 --- /dev/null +++ b/apple/TableProTests/ViewModels/ERDiagramForeignKeyDraftTests.swift @@ -0,0 +1,133 @@ +// +// ERDiagramForeignKeyDraftTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +struct ERDiagramForeignKeyDraftTests { + private typealias Pair = ERDiagramForeignKeyDraft.ColumnPair + + private func draft(_ pairs: [(String, String)]) -> ERDiagramForeignKeyDraft { + ERDiagramForeignKeyDraft( + table: "line_items", + pairs: pairs.map { Pair(column: $0.0, referencedColumn: $0.1) }, + referencedTable: "orders", + name: "fk_line_items_tenant_id" + ) + } + + @Test("The pairs map to the key's two column lists in pair order") + func pairOrderMapsToTheKey() { + var composite = draft([("tenant_id", "tenant_id"), ("order_id", "id")]) + composite.onDelete = .cascade + + let key = composite.foreignKey + + #expect(key.columns == ["tenant_id", "order_id"]) + #expect(key.referencedColumns == ["tenant_id", "id"]) + #expect(key.referencedTable == "orders") + #expect(key.name == "fk_line_items_tenant_id") + #expect(key.onDelete == .cascade) + } + + @Test("A drop on a member of a two-column key offers the whole key, the dragged pair in its place") + func dropOnACompositeMemberOffersTheWholeKey() { + let started = ERDiagramForeignKeyDraft.starting( + table: "line_items", + column: "order_id", + referencedTable: "orders", + keyColumns: ["tenant_id", "id"], + target: "id", + sourceColumns: ["id", "Tenant_ID", "order_id"], + name: "fk_line_items_order_id" + ) + + #expect(started.pairs.map { $0.referencedColumn } == ["tenant_id", "id"]) + #expect(started.pairs.map { $0.column } == ["Tenant_ID", "order_id"]) + #expect(started.issue == nil) + } + + @Test("A drop on a column outside the key keeps that one pair") + func dropOutsideTheKeyIsOnePair() { + let started = ERDiagramForeignKeyDraft.starting( + table: "line_items", + column: "order_code", + referencedTable: "orders", + keyColumns: ["tenant_id", "id"], + target: "code", + sourceColumns: ["order_code"], + name: "fk" + ) + + #expect(started.pairs.map { $0.column } == ["order_code"]) + #expect(started.pairs.map { $0.referencedColumn } == ["code"]) + } + + @Test("Choosing another referenced table rebuilds its side from that table's key and keeps the chosen column") + func referencingRebuildsFromTheKey() { + let single = draft([("order_id", "id")]) + let sources = ["id", "tenant_id", "order_id"] + + let composite = single.referencing("shipments", keyColumns: ["tenant_id", "id"], sourceColumns: sources) + + #expect(composite.referencedTable == "shipments") + #expect(composite.pairs.map { $0.column } == ["tenant_id", "order_id"]) + #expect(composite.pairs.map { $0.referencedColumn } == ["tenant_id", "id"]) + } + + @Test("A table's own id is never taken for the referenced id") + func ownIdIsNotAGuess() { + let single = draft([("order_id", "id")]) + .referencing("orders", keyColumns: ["id"], sourceColumns: ["id", "order_id"]) + + #expect(single.pairs.map { $0.column } == ["order_id"]) + } + + @Test("A column with no namesake stays blank, and the draft says a row is incomplete") + func unguessedColumnIsBlank() { + let composite = draft([("order_id", "id")]) + .referencing("shipments", keyColumns: ["id", "region"], sourceColumns: ["order_id"]) + + #expect(composite.pairs.map { $0.column } == ["order_id", ""]) + #expect(composite.issue == .incompletePair) + } + + @Test("A repeated column on either side is named") + func repeatsAreNamed() { + #expect(draft([("a", "x"), ("a", "y")]).issue == .repeatedColumn("a")) + #expect(draft([("a", "x"), ("b", "x")]).issue == .repeatedReferencedColumn("x")) + #expect(draft([("a", "x"), ("b", "y")]).issue == nil) + } + + @Test("Each whole-value edit changes only the pair it names") + func editsNameTheirPair() throws { + let composite = draft([("a", "x"), ("b", "y")]) + let second = try #require(composite.pairs.last?.id) + + let edited = composite.settingColumn("c", of: second).settingReferencedColumn("z", of: second) + + #expect(edited.pairs.map { $0.column } == ["a", "c"]) + #expect(edited.pairs.map { $0.referencedColumn } == ["x", "z"]) + } + + @Test("Adding, moving and removing pairs keep the key's order, and the last pair stays") + func addMoveRemove() throws { + let composite = draft([("a", "x"), ("b", "y")]).addingPair() + #expect(composite.pairs.count == 3) + #expect(composite.issue == .incompletePair) + + let moved = composite.movingPairs(fromOffsets: IndexSet(integer: 0), toOffset: 2) + #expect(moved.pairs.map { $0.column } == ["b", "a", ""]) + + let first = try #require(moved.pairs.first?.id) + let blank = try #require(moved.pairs.last?.id) + let trimmed = moved.removingPairs([first, blank]) + #expect(trimmed.pairs.map { $0.column } == ["a"]) + + let everything = Set(trimmed.pairs.map { $0.id }) + #expect(trimmed.removingPairs(everything) == trimmed) + } +} diff --git a/apple/TableProTests/ViewModels/ERDiagramSchemaEditsTests.swift b/apple/TableProTests/ViewModels/ERDiagramSchemaEditsTests.swift index e62e4676ea..b52fb16d24 100644 --- a/apple/TableProTests/ViewModels/ERDiagramSchemaEditsTests.swift +++ b/apple/TableProTests/ViewModels/ERDiagramSchemaEditsTests.swift @@ -193,6 +193,34 @@ struct ERDiagramSchemaEditsTests { #expect(diagram == structureTab) } + @Test("A composite foreign key from the diagram composes what the Structure tab composes") + func compositeForeignKeyMatchesTheStructureTab() async throws { + let fixture = try await Self.makeFixture() + defer { fixture.tearDown() } + let draft = ERDiagramForeignKeyDraft( + table: "orders", + pairs: [ + .init(column: "customer_id", referencedColumn: "id"), + .init(column: "shipper_id", referencedColumn: "email") + ], + referencedTable: "customers", + name: "fk_orders_customer", + onDelete: .cascade + ) + + try await fixture.edits.addForeignKey(draft.foreignKey, to: "orders") + let tab = try await Self.structureTabSession(fixture, table: "orders") + tab.changeManager.addForeignKey(draft.foreignKey) + + let diagram = await fixture.edits.previewStatements() + #expect(diagram == [ + "ALTER TABLE \"public\".\"orders\" ADD CONSTRAINT \"fk_orders_customer\" FOREIGN KEY (\"customer_id\", \"shipper_id\") " + + "REFERENCES \"customers\" (\"id\", \"email\") ON DELETE CASCADE;" + ]) + let structureTab = try await Self.statements(of: tab) + #expect(diagram == structureTab) + } + @Test("Dropping a drawn foreign key drops the constraint the Structure tab would drop") func droppedForeignKeyMatchesTheStructureTab() async throws { let fixture = try await Self.makeFixture() diff --git a/apple/TableProTests/ViewModels/ERDiagramViewModelForeignKeyTests.swift b/apple/TableProTests/ViewModels/ERDiagramViewModelForeignKeyTests.swift new file mode 100644 index 0000000000..be40451d62 --- /dev/null +++ b/apple/TableProTests/ViewModels/ERDiagramViewModelForeignKeyTests.swift @@ -0,0 +1,74 @@ +// +// ERDiagramViewModelForeignKeyTests.swift +// TableProTests +// + +import CoreGraphics +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct ERDiagramViewModelForeignKeyTests { + private func loadedFixture() async -> ERDiagramLoadFixture { + let fixture = ERDiagramLoadFixture(tableCount: 1, columnsPerTable: 3) + fixture.driver.addTable("accounts", columns: [ + TestFixtures.makeColumnInfo(name: "tenant_id", isPrimaryKey: true), + TestFixtures.makeColumnInfo(name: "id", isPrimaryKey: true), + TestFixtures.makeColumnInfo(name: "name", dataType: "TEXT", isPrimaryKey: false) + ]) + await fixture.viewModel.loadDiagram() + return fixture + } + + private func draft(of viewModel: ERDiagramViewModel) -> ERDiagramForeignKeyDraft? { + guard case .addForeignKey(let draft) = viewModel.editSheet else { return nil } + return draft + } + + @Test("A drop on a member of a two-column key offers both key columns, the dragged pair in its place") + func dropOnACompositeKeyOffersBothColumns() async throws { + let fixture = await loadedFixture() + defer { fixture.tearDown() } + let target = ERDiagramColumnHit( + nodeId: UUID(), tableName: "accounts", columnName: "id", rowRect: .zero, isDropped: false + ) + + fixture.viewModel.beginForeignKey(from: "table_0", column: "column_1", to: target) + + let started = try #require(draft(of: fixture.viewModel)) + #expect(started.referencedTable == "accounts") + #expect(started.pairs.map { $0.referencedColumn } == ["tenant_id", "id"]) + #expect(started.pairs.last?.column == "column_1") + #expect(started.foreignKey.referencedColumns == ["tenant_id", "id"]) + } + + @Test("Add Foreign Key from a column prefills the referenced table's whole primary key") + func menuPrefillsTheWholeKey() async throws { + let fixture = await loadedFixture() + defer { fixture.tearDown() } + + fixture.viewModel.beginForeignKey(from: "table_0", column: "column_2", to: nil) + + let started = try #require(draft(of: fixture.viewModel)) + #expect(started.referencedTable == "accounts") + #expect(fixture.viewModel.referencedKeyColumns(in: "accounts") == ["tenant_id", "id"]) + #expect(started.pairs.map { $0.referencedColumn } == ["tenant_id", "id"]) + #expect(started.pairs.contains { $0.column == "column_2" }) + } + + @Test("A table with a single-column key still starts with one pair") + func singleKeyIsOnePair() async throws { + let fixture = await loadedFixture() + defer { fixture.tearDown() } + let target = ERDiagramColumnHit( + nodeId: UUID(), tableName: "table_0", columnName: "column_0", rowRect: .zero, isDropped: false + ) + + fixture.viewModel.beginForeignKey(from: "accounts", column: "name", to: target) + + let started = try #require(draft(of: fixture.viewModel)) + #expect(started.pairs.map { $0.column } == ["name"]) + #expect(started.pairs.map { $0.referencedColumn } == ["column_0"]) + } +} diff --git a/apple/TableProTests/ViewModels/SidebarViewModelTests.swift b/apple/TableProTests/ViewModels/SidebarViewModelTests.swift index d6689d4181..ea043ab89d 100644 --- a/apple/TableProTests/ViewModels/SidebarViewModelTests.swift +++ b/apple/TableProTests/ViewModels/SidebarViewModelTests.swift @@ -434,6 +434,56 @@ struct SidebarViewModelMultiSectionTests { #expect(defaults.bool(forKey: key) == true) } + @Test("A declared kind's flat expansion persists under the one per-connection key and restores by id") + func declaredKindExpansionPersistsUnderOneKey() { + let defaults = suite.defaults + let connectionId = UUID() + let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + let vm = makeViewModel(connectionId: connectionId, databaseType: .mysql, defaults: defaults) + #expect(vm.expanded[.server(events)] == false) + + vm.expanded[.server(events)] = true + + let key = SidebarPersistenceKey.serverKindsExpanded(connectionId: connectionId) + #expect(defaults.dictionary(forKey: key)?["event"] as? Bool == true) + #expect(defaults.object(forKey: "sidebar.\(connectionId.uuidString).server.event.expanded") == nil) + #expect(SidebarPersistenceKey.all(connectionId: connectionId).contains(key)) + + let restored = makeViewModel(connectionId: connectionId, databaseType: .mysql, defaults: defaults) + #expect(restored.expanded[.server(SidebarServerObjectKind(id: "event"))] == true) + + SidebarPersistenceKey.removeAll(connectionId: connectionId, defaults: defaults) + #expect(defaults.object(forKey: key) == nil) + } + + @Test("Every built-in kind's expansion key is listed for cleanup") + func builtInExpansionKeysAreListed() { + let connectionId = UUID() + let all = SidebarPersistenceKey.all(connectionId: connectionId) + for kind in SidebarObjectKind.builtIn { + #expect(all.contains(SidebarPersistenceKey.expanded(connectionId: connectionId, kind: kind))) + } + #expect(SidebarPersistenceKey.expanded(connectionId: connectionId, kind: .materializedView) + == "sidebar.\(connectionId.uuidString).materializedView.expanded") + } + + @Test("filteredServerObjects narrows by the search and follows each kind on its own") + func filteredServerObjectsBySearch() { + let vm = makeViewModel(databaseType: .mysql, defaults: suite.defaults) + let events = [ + ServerObjectInfo(kindId: "event", name: "nightly_cleanup", schema: "shop"), + ServerObjectInfo(kindId: "event", name: "hourly_rollup", schema: "shop") + ] + #expect(vm.filteredServerObjects(kindId: "event", from: events).count == 2) + + vm.searchText = "nightly" + + #expect(vm.filteredServerObjects(kindId: "event", from: events).map { $0.name } == ["nightly_cleanup"]) + #expect(vm.filteredServerObjects(kindId: "extension", from: []).isEmpty) + } + @Test("recents expansion defaults to expanded and persists across init") func recentsExpansionPersists() { let defaults = suite.defaults diff --git a/apple/TableProTests/ViewModels/VersionHistoryViewModelTests.swift b/apple/TableProTests/ViewModels/VersionHistoryViewModelTests.swift index 89a29599c9..c0f502b491 100644 --- a/apple/TableProTests/ViewModels/VersionHistoryViewModelTests.swift +++ b/apple/TableProTests/ViewModels/VersionHistoryViewModelTests.swift @@ -236,6 +236,19 @@ struct VersionHistoryViewModelTests { #expect(viewModel.page.entries.isEmpty) #expect(viewModel.listState == .failed(VersionHistoryError.subjectNotFound.localizedDescription)) } + + @Test("A query tab that closed clears the list and says how to get it back") + func closedTabClearsTheList() async throws { + let provider = makeProvider() + let viewModel = makeViewModel(provider) + await viewModel.loadList() + await viewModel.detailTask?.value + + await provider.setLoadError(.tabClosed) + await viewModel.loadList() + #expect(viewModel.page.entries.isEmpty) + #expect(viewModel.listState == .failed(VersionHistoryError.tabClosed.localizedDescription)) + } } struct VersionComparisonTests { diff --git a/apple/TableProTests/ViewModels/WindowSidebarStateTests.swift b/apple/TableProTests/ViewModels/WindowSidebarStateTests.swift index 131a852862..3b72dee2d0 100644 --- a/apple/TableProTests/ViewModels/WindowSidebarStateTests.swift +++ b/apple/TableProTests/ViewModels/WindowSidebarStateTests.swift @@ -214,6 +214,41 @@ struct WindowSidebarStateTests { ) } + @Test("A declared kind's group expansion persists by id and restores before any driver exists") + func declaredKindGroupExpansionRoundTrips() throws { + let defaults = try makeDefaults() + let connectionId = UUID() + let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + let group = DatabaseTreeObjectGroup(database: "shop", schema: nil, kind: .server(events)) + + let state = WindowSidebarState(connectionId: connectionId, defaults: defaults) + #expect(!state.isTreeObjectGroupExpanded(group)) + state.setTreeObjectGroup(group, expanded: true) + + let stored = try #require(defaults.data(forKey: "com.TablePro.sidebar.treeExpansion.\(connectionId.uuidString)")) + #expect(String(decoding: stored, as: UTF8.self).contains("\"server.event\"")) + + let restored = WindowSidebarState(connectionId: connectionId, defaults: defaults) + #expect(restored.isTreeObjectGroupExpanded(group)) + let decodedOnly = DatabaseTreeObjectGroup( + database: "shop", schema: nil, kind: .server(SidebarServerObjectKind(id: "event")) + ) + #expect(restored.isTreeObjectGroupExpanded(decodedOnly)) + } + + @Test("A built-in group still persists under the kind name it shipped with") + func builtInGroupKeepsShippedName() throws { + let defaults = try makeDefaults() + let connectionId = UUID() + let state = WindowSidebarState(connectionId: connectionId, defaults: defaults) + state.setTreeObjectGroup(DatabaseTreeObjectGroup(database: "shop", schema: "public", kind: .materializedView), expanded: true) + + let stored = try #require(defaults.data(forKey: "com.TablePro.sidebar.treeExpansion.\(connectionId.uuidString)")) + #expect(String(decoding: stored, as: UTF8.self).contains("\"materializedView\"")) + } + @Test("A window without a connection does not persist") func nilConnectionDoesNotPersist() throws { let defaults = try makeDefaults() diff --git a/apple/TableProTests/Views/DataGrid/DataGridContextMenuTransientTests.swift b/apple/TableProTests/Views/DataGrid/DataGridContextMenuTransientTests.swift new file mode 100644 index 0000000000..4196cd69e4 --- /dev/null +++ b/apple/TableProTests/Views/DataGrid/DataGridContextMenuTransientTests.swift @@ -0,0 +1,43 @@ +// +// DataGridContextMenuTransientTests.swift +// TableProTests +// + +import AppKit +@testable import TablePro +import Testing + +@MainActor +struct DataGridContextMenuTransientTests { + @Test("An open row menu holds the grid until it closes") + func rowMenuHoldsTheGrid() throws { + let grid = HostedDataGrid(rows: GridFixtureRows.people) + defer { grid.close() } + let menu = try #require(grid.main.menu) + + #expect(!grid.model.holdsTransientUI) + grid.controller.menuWillOpen(menu) + #expect(grid.model.holdsTransientUI) + grid.controller.menuDidClose(menu) + #expect(!grid.model.holdsTransientUI) + } + + @Test("The header menu reports its opening to the grid and keeps the items it was built with") + func headerMenuHoldsTheGrid() throws { + let grid = HostedDataGrid(rows: GridFixtureRows.people) + defer { grid.close() } + let column = try #require(grid.tableColumn(named: "name")) + let menu = try #require(grid.controller.headerMenu(forTableColumn: column, in: .main)) + let items = menu.items.count + #expect(items > 0) + #expect(menu.delegate === grid.controller) + + grid.controller.menuNeedsUpdate(menu) + #expect(menu.items.count == items) + + grid.controller.menuWillOpen(menu) + #expect(grid.model.holdsTransientUI) + grid.controller.menuDidClose(menu) + #expect(!grid.model.holdsTransientUI) + } +} diff --git a/apple/TableProTests/Views/DataGrid/DataGridViewportTests.swift b/apple/TableProTests/Views/DataGrid/DataGridViewportTests.swift index b5ada9e63f..3494536405 100644 --- a/apple/TableProTests/Views/DataGrid/DataGridViewportTests.swift +++ b/apple/TableProTests/Views/DataGrid/DataGridViewportTests.swift @@ -126,6 +126,26 @@ struct DataGridViewportTests { #expect(NSLocationInRange(250, grid.main.rows(in: grid.main.visibleRect))) } + @Test("A placement keeps a cell cursor on its column rather than selecting the whole row on column 0") + func placedCursorKeepsItsColumn() async throws { + let grid = makeGrid() + defer { grid.close() } + let column = DataGridColumnID(name: "c3", occurrence: 0) + let cell = GridSelectionSnapshot.Cell(rowID: .existing(40), columnID: column) + var cursor = GridSelectionSnapshot(rowIDs: [.existing(40)], rectangles: [], activeCell: cell, anchor: cell) + cursor.mode = .cursor + + grid.model.store.viewportAnchor = GridViewportPlacement( + firstVisibleRow: .existing(30), firstVisibleOffset: 0, selection: cursor, revealsSelection: false + ) + grid.controller.applyStagedViewport() + + try await waitUntil { grid.model.selection.mode == .cursor } + let position = try #require(grid.model.columns.displayPosition(of: column)) + #expect(grid.model.selection.activeCell == GridCoord(row: 40, displayColumn: position)) + #expect(grid.model.selectedRowIDs == [.existing(40)]) + } + @Test("Revealing a row scrolls the frozen pane with it") func revealScrollsBothPanes() async throws { let grid = makeGrid() diff --git a/apple/TableProTests/Views/Main/GridReloadViewportTests.swift b/apple/TableProTests/Views/Main/GridReloadViewportTests.swift index 7de3db2103..c85b15082f 100644 --- a/apple/TableProTests/Views/Main/GridReloadViewportTests.swift +++ b/apple/TableProTests/Views/Main/GridReloadViewportTests.swift @@ -147,6 +147,27 @@ struct GridReloadViewportTests { withExtendedLifetime(fixture) {} } + @Test("Reading the same table again keeps a cell cursor a cursor, on its own column") + func sameSourceKeepsTheCursorAndItsColumn() throws { + let fixture = makeFixture() + defer { fixture.coordinator.teardown() } + fixture.model.selectCell(GridCoord(row: 2, displayColumn: 1)) + let column = try #require(fixture.model.activeCell?.column.id) + + fixture.coordinator.setActiveTableRows( + Self.rows(ids: Array((1 ... 10).reversed())), + for: fixture.tabId, + viewport: .keepPlace, + source: .sameSource + ) + + let selection = try #require(fixture.takePlacement()?.selection) + #expect(selection.mode == .cursor) + #expect(selection.rowIDs == [.existing(7)]) + #expect(selection.activeCell == GridSelectionSnapshot.Cell(rowID: .existing(7), columnID: column)) + withExtendedLifetime(fixture) {} + } + @Test("A new result does not carry the selection over") func newSourceRestoresNoSelection() { let fixture = makeFixture() diff --git a/apple/TableProTests/Views/Main/MainContentCoordinatorAutoRefreshTests.swift b/apple/TableProTests/Views/Main/MainContentCoordinatorAutoRefreshTests.swift new file mode 100644 index 0000000000..e314bd5c04 --- /dev/null +++ b/apple/TableProTests/Views/Main/MainContentCoordinatorAutoRefreshTests.swift @@ -0,0 +1,449 @@ +// +// MainContentCoordinatorAutoRefreshTests.swift +// TableProTests +// + +import Foundation +import os +@testable import TablePro +import TableProPluginKit +import TableProTestSupport +import Testing + +@MainActor +struct MainContentCoordinatorAutoRefreshTests { + @Test("A tick re-reads the selected table tab and writes no history row") + func tickRereadsWithoutHistory() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + + await harness.tickOnce() + + #expect(harness.tableReads == 1) + let tab = try #require(harness.tab) + #expect(tab.execution.errorMessage == nil) + #expect(tab.autoRefreshInterval == .fiveSeconds) + await harness.expectOnlyTheMarkerIsRecorded() + } + + @Test("A manual load records its row, so the marker check would see a tick's") + func manualLoadRecordsHistory() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + + harness.coordinator.executeTableTabQueryDirectly(viewport: .keepPlace) + await harness.coordinator.queryTasks.task(for: harness.tabId)?.value + await harness.recordMarker() + + #expect(harness.recordAttempts == 2) + try await waitUntil { await harness.historyCount == 2 } + } + + @Test("A tick keeps the derived count rather than counting again") + func tickKeepsTheCount() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + harness.coordinator.tabManager.mutate(tabId: harness.tabId) { tab in + tab.pagination.applyDerivedRowCount(42, isApproximate: false) + } + + await harness.tickOnce() + + let tab = try #require(harness.tab) + #expect(harness.tableReads == 1) + #expect(tab.pagination.totalRowCount == 42) + #expect(!tab.pagination.isCountPending) + } + + @Test("An open cell editor holds the tick: no query, no commit, and the bar says why") + func openEditorPausesWithoutCommitting() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + let model = harness.coordinator.gridModel(forTab: harness.tabId) + let mount = DataTabGridMountStub() + mount.isEditing = true + model.attach(mount) + + await harness.tickOnce() + + #expect(harness.tableReads == 0) + #expect(mount.commitCount == 0) + #expect(harness.tab?.autoRefreshPause == .editing) + #expect(harness.tab?.autoRefreshInterval == .fiveSeconds) + withExtendedLifetime(mount) {} + } + + @Test("An editable inspector pop-out holds the tick until its window goes") + func inspectorPopOutPauses() async { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + let paneState = TrailingPaneState() + harness.coordinator.trailingPaneState = paneState + var popOut: JSONViewerWindowController? = JSONViewerWindowController() + if let popOut { InspectorPopOutRegistry.record(popOut, openedFrom: paneState) } + + await harness.tickOnce() + + #expect(harness.tableReads == 0) + #expect(harness.tab?.autoRefreshPause == .valueWindowOpen) + + popOut = nil + #expect(!harness.coordinator.autoRefreshGate(forTab: harness.tabId).inspectorHoldsValueWindow) + withExtendedLifetime(paneState) {} + } + + @Test("A tick's rows are dropped when a value window opens during its read, and the next tick applies its own") + func tickRowsWaitForAValueWindowOpenedMidRead() async { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + let paneState = TrailingPaneState() + harness.coordinator.trailingPaneState = paneState + let read = AsyncGate() + harness.driver.holdReads(on: read) + + harness.start() + await harness.clock.waitUntilSleeping() + harness.clock.advance(by: .seconds(5)) + await read.arrived(1) + var popOut: JSONViewerWindowController? = JSONViewerWindowController() + if let popOut { InspectorPopOutRegistry.record(popOut, openedFrom: paneState) } + await read.open() + await harness.clock.waitUntilSleeping() + + #expect(harness.tableReads == 1) + #expect(harness.loadedRowCount == 1, "the held tick's two rows must not replace the one on screen") + #expect(harness.tab?.autoRefreshInterval == .fiveSeconds) + + popOut = nil + harness.clock.advance(by: .seconds(5)) + await harness.clock.waitUntilSleeping() + + #expect(harness.tableReads == 2) + #expect(harness.loadedRowCount == 2, "with the window gone the next tick's rows land") + withExtendedLifetime(paneState) {} + } + + @Test("A tick waits while the tab's database holds an open transaction") + func openTransactionPauses() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + let tracker = DatabaseManager.shared.sessionTransactions + let key = SessionTransactionTracker.Key(connectionId: harness.connection.id, lane: nil) + tracker.record(.inTransaction, on: harness.driver, for: key) + defer { tracker.record(.idle, on: harness.driver, for: key) } + + await harness.tickOnce() + + #expect(harness.tableReads == 0) + #expect(harness.tab?.autoRefreshPause == .openTransaction) + } + + @Test("A disconnected session runs nothing and waits for the connection") + func disconnectedSessionWaits() async throws { + let harness = AutoRefreshHarness(status: .disconnected) + defer { harness.tearDown() } + + await harness.tickOnce() + + #expect(harness.tableReads == 0) + #expect(harness.tab?.autoRefreshPause == .disconnected) + } + + @Test("A failed tick turns Auto Refresh off and leaves the tab's error on screen") + func failedTickStops() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + harness.driver.failReads() + + harness.start() + await harness.clock.waitUntilSleeping() + harness.clock.advance(by: .seconds(5)) + try await waitUntil { harness.tab?.autoRefreshInterval == .off } + + #expect(harness.tab?.execution.errorMessage != nil) + #expect(harness.clock.pendingSleeperCount == 0) + await harness.expectOnlyTheMarkerIsRecorded() + } + + @Test("A tab that is not selected never ticks") + func backgroundTabNeverRuns() async throws { + let harness = AutoRefreshHarness() + defer { harness.tearDown() } + harness.coordinator.observeSelectionForAutoRefresh() + harness.start() + await harness.clock.waitUntilSleeping() + + let other = QueryTab(title: "Query", query: "SELECT 1", tabType: .query) + harness.coordinator.tabManager.tabs.append(other) + harness.coordinator.tabManager.selectedTabId = other.id + try await waitUntil { !harness.coordinator.autoRefresh.isRunning } + harness.clock.advance(by: .seconds(5)) + + #expect(harness.tableReads == 0) + #expect(harness.clock.pendingSleeperCount == 0) + } +} + +// MARK: - Harness + +@MainActor +private final class AutoRefreshHarness { + let connection: DatabaseConnection + let driver: AutoRefreshDriver + let coordinator: MainContentCoordinator + let clock = ManualClock() + let tabId: UUID + private let history: QueryHistoryManager + private let attempts: RecordAttemptCounter + + init(status: ConnectionStatus = .connected) { + connection = DatabaseConnection(name: "Auto Refresh", database: "app", type: .postgresql) + driver = AutoRefreshDriver(connection: connection) + var session = ConnectionSession(connection: connection, driver: driver) + session.status = status + DatabaseManager.shared.injectSession(session, for: connection.id) + + let url = FileManager.default.temporaryDirectory + .appendingPathComponent("auto-refresh-\(UUID().uuidString).db") + // Asked once per write, first thing, on the task that records it. + let attempts = RecordAttemptCounter() + self.attempts = attempts + history = QueryHistoryManager( + storage: QueryHistoryStorage(databaseURL: url, removeDatabaseOnDeinit: true), + isCapturePaused: { + attempts.increment() + return false + } + ) + coordinator = MainContentCoordinator( + connection: connection, + tabManager: QueryTabManager(), + changeManager: DataChangeManager(), + toolbarState: ConnectionToolbarState(), + services: AppServices.live.replacingQueryHistory(history) + ) + coordinator.executionGate = AlwaysAllowGate() + coordinator.autoRefresh.clock = clock + coordinator.autoRefresh.visibility = { _, _ in .onScreen } + + var tab = QueryTab(title: "jobs", query: "SELECT * FROM jobs LIMIT 1000", tabType: .table, tableName: "jobs") + tab.tableContext.primaryKeyColumns = ["id"] + tab.execution.lastExecutedAt = Date() + coordinator.tabManager.tabs.append(tab) + coordinator.tabManager.selectedTabId = tab.id + tabId = tab.id + coordinator.setActiveTableRows( + TableRows.from( + queryRows: [[.text("1"), .text("first")]], + columns: ["id", "name"], + columnTypes: [.text(rawType: "INTEGER"), .text(rawType: "TEXT")] + ), + for: tab.id + ) + } + + var tab: QueryTab? { + coordinator.tabManager.tabs.first { $0.id == tabId } + } + + var tableReads: Int { + driver.sentStatements.filter { $0.contains("jobs") }.count + } + + var loadedRowCount: Int { + coordinator.tabSessionRegistry.tableRows(for: tabId).rows.count + } + + var historyCount: Int { + get async { await history.count(scope: .connection(connection.id)) } + } + + var recordAttempts: Int { attempts.value } + + // Through the same manager and store after the load, so a row the load recorded is written before it. + func recordMarker() async { + await history.record(QueryHistoryRecordRequest( + query: "SELECT 'marker'", + connectionId: connection.id, + databaseName: connection.database, + databaseType: connection.type, + source: .editor, + executionTime: 0, + rowCount: 0, + wasSuccessful: true + )) + } + + func expectOnlyTheMarkerIsRecorded(sourceLocation: SourceLocation = #_sourceLocation) async { + await recordMarker() + #expect(recordAttempts == 1, "the tick asked to record \(recordAttempts - 1) rows", sourceLocation: sourceLocation) + #expect(await historyCount == 1, sourceLocation: sourceLocation) + } + + func start() { + coordinator.setAutoRefreshInterval(.fiveSeconds, forTab: tabId) + } + + // One interval passes, and the schedule is back to waiting once the tick it ran has settled. + func tickOnce() async { + start() + await clock.waitUntilSleeping() + clock.advance(by: .seconds(5)) + await clock.waitUntilSleeping() + } + + func tearDown() { + coordinator.stopAutoRefresh() + coordinator.cancelAllQueryTasks() + DatabaseManager.shared.removeSession(for: connection.id) + coordinator.teardown() + } +} + +private final class RecordAttemptCounter: Sendable { + private let count = OSAllocatedUnfairLock(initialState: 0) + + var value: Int { count.withLock { $0 } } + + func increment() { + count.withLock { $0 += 1 } + } +} + +private struct AutoRefreshReadFailure: LocalizedError { + var errorDescription: String? { "relation \"jobs\" does not exist" } +} + +private final class AutoRefreshDriver: DatabaseDriver, @unchecked Sendable { + let connection: DatabaseConnection + var status: ConnectionStatus = .connected + var serverVersion: String? { nil } + + private let lock = NSLock() + private var statements: [String] = [] + private var fails = false + private var readGate: AsyncGate? + + init(connection: DatabaseConnection) { + self.connection = connection + } + + var sentStatements: [String] { + lock.withLock { statements } + } + + func failReads() { + lock.withLock { fails = true } + } + + // Every later read of the table waits on the gate until the test opens it. + func holdReads(on gate: AsyncGate) { + lock.withLock { readGate = gate } + } + + private func answer(_ query: String) async throws -> QueryResult { + let (failing, gate) = lock.withLock { () -> (Bool, AsyncGate?) in + statements.append(query) + let readsTable = query.contains("jobs") + return (fails && readsTable, readsTable ? readGate : nil) + } + if let gate { await gate.wait() } + if failing { throw AutoRefreshReadFailure() } + return ScriptAnsweringDriver.resultSet(columns: ["id", "name"], rows: [["1", "first"], ["2", "second"]]) + } + + func execute(query: String) async throws -> QueryResult { try await answer(query) } + func executeParameterized(query: String, parameters: [Any?]) async throws -> QueryResult { try await answer(query) } + func executeUserQuery(query: String, rowCap: Int?, parameters: [Any?]?) async throws -> QueryResult { + try await answer(query) + } + + func connect() async throws {} + func disconnect() {} + func testConnection() async throws -> Bool { true } + func ping() async throws {} + func cancelQuery() throws {} + func applyQueryTimeout(_ seconds: Int) async throws {} + + func fetchTables() async throws -> [TableInfo] { [] } + func fetchTables(schema: String?) async throws -> [TableInfo] { [] } + func fetchColumns(table: String) async throws -> [ColumnInfo] { [] } + func fetchAllColumns() async throws -> [String: [ColumnInfo]] { [:] } + func fetchIndexes(table: String) async throws -> [IndexInfo] { [] } + func fetchForeignKeys(table: String) async throws -> [ForeignKeyInfo] { [] } + func fetchApproximateRowCount(table: String) async throws -> Int? { nil } + func fetchDatabases() async throws -> [String] { [] } + func fetchDatabaseMetadata(_ database: String) async throws -> DatabaseMetadata { + DatabaseMetadata( + id: database, + name: database, + tableCount: nil, + sizeBytes: nil, + lastAccessed: nil, + isSystemDatabase: false, + icon: "cylinder" + ) + } + + func fetchTableDDL(table: String) async throws -> String { "" } + func fetchTableMetadata(tableName: String) async throws -> TableMetadata { + TableMetadata( + tableName: tableName, + dataSize: nil, + indexSize: nil, + totalSize: nil, + avgRowLength: nil, + rowCount: nil, + comment: nil, + engine: nil, + collation: nil, + createTime: nil, + updateTime: nil + ) + } + + func fetchViewDefinition(view: String) async throws -> String { "" } + func beginTransaction() async throws {} + func commitTransaction() async throws {} + func rollbackTransaction() async throws {} +} + +@MainActor +private extension AppServices { + func replacingQueryHistory(_ manager: QueryHistoryManager) -> AppServices { + AppServices( + appEvents: appEvents, + appSettings: appSettings, + appSettingsStorage: appSettingsStorage, + connectionStorage: connectionStorage, + databaseManager: databaseManager, + pluginManager: pluginManager, + schemaService: schemaService, + schemaRefreshService: schemaRefreshService, + schemaProviderRegistry: schemaProviderRegistry, + catalogChangeService: catalogChangeService, + sqlFavoriteManager: sqlFavoriteManager, + favoriteTablesStorage: favoriteTablesStorage, + favoriteDatabasesStorage: favoriteDatabasesStorage, + tableFolderStorage: tableFolderStorage, + aiChatStorage: aiChatStorage, + aiKeyStorage: aiKeyStorage, + aiAccessApprovals: aiAccessApprovals, + groupStorage: groupStorage, + tagStorage: tagStorage, + sshProfileStorage: sshProfileStorage, + credentialProfileStorage: credentialProfileStorage, + featureAccess: featureAccess, + syncMetadataStorage: syncMetadataStorage, + favoritesExpansionState: favoritesExpansionState, + linkedFolderWatcher: linkedFolderWatcher, + queryHistoryManager: manager, + dateFormattingService: dateFormattingService, + copilotService: copilotService, + mcpServerManager: mcpServerManager, + syncTracker: syncTracker, + welcomeRouter: welcomeRouter, + telemetry: telemetry + ) + } +} diff --git a/apple/TableProTests/Views/Main/QueryTabVersionTriggerTests.swift b/apple/TableProTests/Views/Main/QueryTabVersionTriggerTests.swift new file mode 100644 index 0000000000..1a0301d4ba --- /dev/null +++ b/apple/TableProTests/Views/Main/QueryTabVersionTriggerTests.swift @@ -0,0 +1,165 @@ +// +// QueryTabVersionTriggerTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import TableProTestSupport +import Testing + +@MainActor +private final class CaptureSwitch { + var isPaused = false +} + +@MainActor +struct QueryTabVersionTriggerTests { + private let storage = QueryTabVersionStorage( + databaseURL: FileManager.default.temporaryDirectory + .appendingPathComponent("tablepro-tests") + .appendingPathComponent("query_tab_triggers_\(UUID().uuidString).db"), + announce: { _ in }, + removeDatabaseOnDeinit: true + ) + private let capture = CaptureSwitch() + + // The draft-save clock never moves unless a case advances it, so no idle version lands mid-case. + private func makeFixture( + query: String = "SELECT 1", + connection: DatabaseConnection = TestFixtures.makeConnection() + ) -> (CoordinatorFixture, QueryTab, ManualClock) { + let fixture = CoordinatorFixture(connection: connection) + let clock = ManualClock() + fixture.coordinator.draftSaveClock = clock + fixture.coordinator.executionGate = AlwaysDenyGate() + fixture.coordinator.openTabInNewWindow = { _ in } + let capture = capture + fixture.coordinator.tabVersions = QueryTabVersionRecorder( + storage: storage, + isCapturePaused: { capture.isPaused } + ) + let tab = fixture.addTab(QueryTab(title: "Query 1", query: query, tabType: .query)) + return (fixture, tab, clock) + } + + private func versions(of tabId: UUID) -> [(reason: QueryTabVersionReason, text: String?)] { + storage.summaries(tabId: tabId).map { ($0.reason, storage.version(id: $0.id, tabId: tabId)?.query) } + } + + @Test("Running a query tab records its text") + func runRecordsTheText() async throws { + let (fixture, tab, _) = makeFixture(query: "SELECT 1") + + fixture.coordinator.runQuery(viewport: .firstRow) + + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + let recorded = versions(of: tab.id) + #expect(recorded.map { $0.reason } == [.run]) + #expect(recorded.first?.text == "SELECT 1") + } + + @Test("Run All records the tab's text") + func runAllRecordsTheText() async throws { + let (fixture, tab, _) = makeFixture(query: "SELECT 1; SELECT 2") + + fixture.coordinator.runAllStatements() + + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + #expect(versions(of: tab.id).first?.text == "SELECT 1; SELECT 2") + } + + @Test("Clear Query keeps the text it removes as a version") + func clearQueryRecordsTheReplacedText() async throws { + let (fixture, tab, _) = makeFixture(query: "SELECT long_draft FROM t") + + fixture.coordinator.clearSelectedQuery() + + #expect(fixture.tabManager.selectedTab?.content.query == "") + #expect(!fixture.coordinator.toolbarState.hasQueryText) + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + let recorded = versions(of: tab.id) + #expect(recorded.first?.reason == .beforeReplace) + #expect(recorded.first?.text == "SELECT long_draft FROM t") + } + + @Test("Loading a query into the current tab keeps the text it replaces as a version") + func loadIntoEditorRecordsTheReplacedText() async throws { + let (fixture, tab, _) = makeFixture(query: "SELECT typed") + + fixture.coordinator.loadQueryIntoEditor("SELECT * FROM users") + + #expect(fixture.tabManager.selectedTab?.content.query == "SELECT * FROM users") + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + #expect(versions(of: tab.id).first?.reason == .beforeReplace) + #expect(versions(of: tab.id).first?.text == "SELECT typed") + } + + @Test("Inserting a saved query appends it and keeps the text before it") + func insertFavoriteAppendsAndRecords() async throws { + let (fixture, tab, _) = makeFixture(query: "SELECT 1") + + fixture.coordinator.insertFavorite(SQLFavorite(name: "Users", query: "SELECT * FROM users")) + + #expect(fixture.tabManager.selectedTab?.content.query == "SELECT 1\n\nSELECT * FROM users") + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + #expect(versions(of: tab.id).first?.text == "SELECT 1") + } + + // Any window may record and save: the old first-coordinator check left a second window's typing unsaved for 30s. + @Test("An idle pause records a version from a window that is not the connection's first") + func idlePauseRecordsFromAnyWindow() async throws { + let (fixture, tab, clock) = makeFixture(query: "SELECT draft") + let firstWindow = CoordinatorFixture(connection: fixture.connection) + firstWindow.coordinator.registerEagerly() + #expect(MainContentCoordinator.activeCoordinators[fixture.coordinator.instanceId] == nil) + + fixture.coordinator.scheduleDraftSave(editedTab: tab.id) + await clock.waitUntilSleeping() + clock.advance(by: MainContentCoordinator.draftSaveDebounce) + + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + #expect(versions(of: tab.id).first?.reason == .idle) + #expect(versions(of: tab.id).first?.text == "SELECT draft") + } + + @Test("With Query History capture paused, nothing a tab runs or replaces is kept") + func pausedCaptureRecordsNothing() async throws { + let (fixture, tab, clock) = makeFixture(query: "SELECT secret") + capture.isPaused = true + + fixture.coordinator.runQuery(viewport: .firstRow) + fixture.coordinator.loadQueryIntoEditor("SELECT other") + #expect(fixture.coordinator.tabVersions.record(tab, connectionId: fixture.connection.id, reason: .run) == nil) + + // A version recorded after the pause lifts is the only one, so nothing earlier was queued. + capture.isPaused = false + fixture.coordinator.scheduleDraftSave(editedTab: tab.id) + await clock.waitUntilSleeping() + clock.advance(by: MainContentCoordinator.draftSaveDebounce) + try await waitUntil { !storage.summaries(tabId: tab.id).isEmpty } + #expect(versions(of: tab.id).map { $0.reason } == [.idle]) + #expect(versions(of: tab.id).first?.text == "SELECT other") + } + + @Test("Revert To writes the chosen version into the tab and keeps the text it replaced") + func revertRestoresAndRecords() async throws { + let (fixture, tab, _) = makeFixture(query: "SELECT first") + await storage.record(tabId: tab.id, connectionId: fixture.connection.id, text: "SELECT first", reason: .run) + let version = try #require(storage.summaries(tabId: tab.id).first) + fixture.tabManager.mutate(tabId: tab.id) { $0.content.query = "SELECT second" } + + fixture.coordinator.revertQueryTab(tab.id, toVersion: version.id) + + try await waitUntil { fixture.tabManager.selectedTab?.content.query == "SELECT first" } + #expect(versions(of: tab.id).first?.reason == .beforeRestore) + #expect(versions(of: tab.id).first?.text == "SELECT second") + } + + @Test("A table tab records nothing") + func tableTabRecordsNothing() { + let (fixture, _, _) = makeFixture() + let table = QueryTab(title: "users", query: "SELECT * FROM users", tabType: .table, tableName: "users") + #expect(fixture.coordinator.tabVersions.record(table, connectionId: fixture.connection.id, reason: .run) == nil) + } +} diff --git a/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift b/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift index 6afd854e6a..02d5af2e59 100644 --- a/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift +++ b/apple/TableProTests/Views/Main/ResultStatusBarLayoutTests.swift @@ -29,7 +29,8 @@ struct ResultStatusBarLayoutTests { pagination: PaginationState = PaginationState(), statusMessage: String? = nil, selectionSummary: SelectionSummaryState? = nil, - lastTiming: PluginQueryTiming? = nil + lastTiming: PluginQueryTiming? = nil, + autoRefresh: StatusBarAutoRefreshState = .unavailable ) -> ResultStatusBar { let snapshot = StatusBarSnapshot( tabId: UUID(), @@ -92,7 +93,8 @@ struct ResultStatusBarLayoutTests { resultSetMenu: ResultSetMenuModel(entries: [], activeOrdinal: 0, total: 0), resultSetActions: .none, onToggleFilters: {}, - onFetchAll: {} + onFetchAll: {}, + autoRefresh: autoRefresh ) } @@ -629,6 +631,47 @@ struct ResultStatusBarLayoutTests { expectFills(bar, at: pane) } + private static var autoRefreshOn: StatusBarAutoRefreshState { + StatusBarAutoRefreshState(interval: .fiveSeconds, pause: nil, onChange: { _ in }) + } + + @Test("A table tab with Auto Refresh on is never wider than its host", arguments: statusBarHostWidths) + func autoRefreshBarNeverExceedsItsHost(width: CGFloat) { + let bar = makeBar( + rowCount: 1_000, + hasColumns: true, + tabType: .table, + viewMode: .data, + pagination: PaginationState(totalRowCount: 5_000, pageSize: 1_000), + autoRefresh: Self.autoRefreshOn + ) + #expect(bar.model.controls.showsAutoRefresh) + expectFills(bar, at: width) + } + + @Test("The Auto Refresh glyph keeps its own color only while an interval runs") + func autoRefreshGlyphShowsTheOnState() throws { + let on = try #require(ResultStatusBar.autoRefreshSymbol(isOn: true)) + let off = try #require(ResultStatusBar.autoRefreshSymbol(isOn: false)) + + #expect(!on.isTemplate, "a template image draws in the control's color and hides the on state") + #expect(off.isTemplate) + #expect(on.size == off.size, "the glyph must not change width when the interval turns on") + } + + @Test("The narrowest tier still holds 280pt with Auto Refresh on the bar") + func narrowestTierHoldsWithAutoRefresh() { + let bar = makeBar( + rowCount: 1_000, + hasColumns: true, + tabType: .table, + viewMode: .data, + pagination: PaginationState(totalRowCount: 5_000, pageSize: 1_000), + autoRefresh: Self.autoRefreshOn + ) + expectFills(bar, at: 280) + } + /// The grid shares a `VStack` with the bar, closed by a `.frame(maxWidth: .infinity)` that can /// never report less than its widest child, which is how the bar's width reached the grid at /// all. Measuring the column rather than the bar alone is what pins that path. diff --git a/apple/TableProTests/Views/ObjectSource/ObjectSourceLoaderTests.swift b/apple/TableProTests/Views/ObjectSource/ObjectSourceLoaderTests.swift index a9c3151c1f..5e2029f58a 100644 --- a/apple/TableProTests/Views/ObjectSource/ObjectSourceLoaderTests.swift +++ b/apple/TableProTests/Views/ObjectSource/ObjectSourceLoaderTests.swift @@ -117,6 +117,68 @@ struct ObjectSourceLoaderTests { #expect(loader.source == definition) #expect(driver.recordedCalls == [fetchCall]) } + + @Test("A server object reads its definition and fresh attributes through the driver") + func serverObjectLoadsThroughDriver() async { + let driver = ServerObjectSourceDriver() + driver.reread = PluginServerObjectInfo( + kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384", + definition: "CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;", + attributes: [PluginObjectAttribute(label: "Default Version", value: "1.3")] + ) + let kind = SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", + iconName: "puzzlepiece.extension", scope: .database + ) + let object = ServerObjectInfo(kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384") + let ref = DatabaseObjectRef(serverObject: object, kind: kind, database: "shop") + let adapter = PluginDriverAdapter(connection: TestFixtures.makeConnection(type: .postgresql), pluginDriver: driver) + let metadata = ScriptedSourceMetadata(driver: adapter) + let loader = ObjectSourceLoader(connectionId: UUID(), objectRef: ref, metadata: metadata) + + await loader.load() + + // A database-wide object is read with no schema: `public` is only where its members live. + #expect(metadata.scopes.map { $0.schema } == [nil]) + #expect(metadata.scopes.map { $0.database } == ["shop"]) + #expect(loader.source == "CREATE EXTENSION IF NOT EXISTS pgcrypto WITH SCHEMA public;") + #expect(loader.attributes == [ObjectAttribute(label: "Default Version", value: "1.3")]) + let requested = driver.requested + #expect(requested.map { $0.kindId } == ["extension"]) + #expect(requested.map { $0.identity } == ["16384"]) + } + + @Test("A server object the driver cannot read reports the failure") + func serverObjectWithoutDefinitionFails() async { + let driver = ServerObjectSourceDriver() + let kind = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + let ref = DatabaseObjectRef(serverObject: ServerObjectInfo(kindId: "event", name: "nightly"), kind: kind, database: "shop") + let adapter = PluginDriverAdapter(connection: TestFixtures.makeConnection(type: .mysql), pluginDriver: driver) + let loader = ObjectSourceLoader(connectionId: UUID(), objectRef: ref, metadata: ScriptedSourceMetadata(driver: adapter)) + + await loader.load() + + guard case .failed = loader.state else { + Issue.record("expected the load to fail") + return + } + } +} + +private final class ServerObjectSourceDriver: PluginDriverStub, @unchecked Sendable { + private let lock = NSLock() + private var log: [PluginServerObjectInfo] = [] + var reread: PluginServerObjectInfo? + + var requested: [PluginServerObjectInfo] { lock.withLock { log } } + + func fetchServerObject(_ object: PluginServerObjectInfo) async throws -> PluginServerObjectInfo { + lock.withLock { log.append(object) } + guard let reread else { throw PluginObjectSourceError.unsupported(object.name) } + return reread + } } /// Throws `CancellationError` once on request, the way a metadata read does when the task that @@ -125,6 +187,7 @@ struct ObjectSourceLoaderTests { private final class ScriptedSourceMetadata: ScopedMetadataProviding { private let driver: DatabaseDriver var cancelsNextRead = false + private(set) var scopes: [DatabaseScope] = [] init(driver: DatabaseDriver) { self.driver = driver @@ -135,6 +198,7 @@ private final class ScriptedSourceMetadata: ScopedMetadataProviding { workload: MetadataConnectionPool.Workload, _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> T { + scopes.append(scope) if cancelsNextRead { cancelsNextRead = false throw CancellationError() diff --git a/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterTests.swift b/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterTests.swift index 21e52a2e21..433a1c4796 100644 --- a/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterTests.swift +++ b/apple/TableProTests/Views/Sidebar/DatabaseTreeFilterTests.swift @@ -132,9 +132,67 @@ struct DatabaseTreeFilterTests { #expect(SidebarObjectKind.visible(itemCounts: buckets.itemCounts) == [.table]) } - @Test("Types is the last kind listed") + @Test("Types is the last built-in kind listed, and declared kinds follow it") func typesIsTheLastKind() { - #expect(SidebarObjectKind.allCases.last == .type) + #expect(SidebarObjectKind.builtIn.last == .type) + let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + #expect(SidebarObjectKind.ordered(declared: [events]).suffix(2) == [.type, .server(events)]) + } + + private var eventKind: SidebarServerObjectKind { + SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + } + + private func event(_ name: String, schema: String = "shop") -> ServerObjectInfo { + ServerObjectInfo(kindId: "event", name: name, schema: schema, detail: "Recurring") + } + + @Test("Server objects are filtered by the search and counted under their declared kind") + func serverObjectsFilteredAndCounted() { + let buckets = DatabaseTreeFilter.objectBuckets( + tables: [table("users")], + routines: [], + triggers: [], + serverObjects: ["event": [event("nightly_cleanup"), event("hourly_rollup")]], + searchText: "nightly" + ) + #expect(buckets.serverObjects["event"]?.map { $0.name } == ["nightly_cleanup"]) + #expect(buckets.itemCounts[.server(eventKind)] == 1) + #expect(SidebarObjectKind.visible(itemCounts: buckets.itemCounts, declared: [eventKind]) == [.server(eventKind)]) + #expect(!buckets.isEmpty) + } + + @Test("A search matching no server object leaves its kind with no section") + func serverObjectsWithoutMatchHaveNoSection() { + let buckets = DatabaseTreeFilter.objectBuckets( + tables: [table("users")], + routines: [], + triggers: [], + serverObjects: ["event": [event("nightly_cleanup")]], + searchText: "users" + ) + #expect(buckets.serverObjects["event"]?.isEmpty == true) + #expect(SidebarObjectKind.visible(itemCounts: buckets.itemCounts, declared: [eventKind]) == [.table]) + } + + @Test("Only server objects keep a container from reading as empty") + func serverObjectsAloneAreContent() { + let buckets = DatabaseTreeFilter.objectBuckets( + tables: [], routines: [], triggers: [], serverObjects: ["event": [event("nightly")]], searchText: "" + ) + #expect(!buckets.isEmpty) + let none = DatabaseTreeFilter.objectBuckets(tables: [], routines: [], triggers: [], serverObjects: ["event": []], searchText: "") + #expect(none.isEmpty) + } + + @Test("One server object listed twice is drawn once") + func serverObjectsDeduplicated() { + let objects = [event("nightly"), event("nightly"), event("nightly", schema: "audit")] + #expect(DatabaseTreeFilter.filteredServerObjects(objects, searchText: "").count == 2) } @Test("visibleSchemas drops system schemas and deduplicates") diff --git a/apple/TableProTests/Views/Sidebar/DatabaseTreeFlatRootTests.swift b/apple/TableProTests/Views/Sidebar/DatabaseTreeFlatRootTests.swift index bc8187afbd..f2f058fcc9 100644 --- a/apple/TableProTests/Views/Sidebar/DatabaseTreeFlatRootTests.swift +++ b/apple/TableProTests/Views/Sidebar/DatabaseTreeFlatRootTests.swift @@ -26,10 +26,17 @@ struct DatabaseTreeFlatRootTests { private let boom = NSError(domain: "test", code: 1, userInfo: [NSLocalizedDescriptionKey: "permission denied"]) - private func makeFixture(_ configure: (SideObjectsMockDriver) -> Void) async throws -> Fixture { + private func makeFixture( + injectsSession: Bool = false, + _ configure: (SideObjectsMockDriver) -> Void + ) async throws -> Fixture { let connection = TestFixtures.makeConnection(database: "shop", type: .postgresql) let driver = SideObjectsMockDriver(connection: connection) configure(driver) + // The outline asks the session's driver which kinds it declares. + if injectsSession { + DatabaseManager.shared.injectSession(ConnectionSession(connection: connection, driver: driver), for: connection.id) + } await SchemaService.shared.load(connectionId: connection.id, driver: driver, connection: connection) let scrollView = SidebarOutlineScaffold.makeScrollView( @@ -69,6 +76,9 @@ struct DatabaseTreeFlatRootTests { } private func tearDown(_ fixture: Fixture) async { + if DatabaseManager.shared.session(for: fixture.connectionId) != nil { + DatabaseManager.shared.removeSession(for: fixture.connectionId) + } await SchemaService.shared.invalidate(connectionId: fixture.connectionId) SharedSidebarState.removeConnection(fixture.connectionId) _ = fixture.scrollView @@ -142,4 +152,86 @@ struct DatabaseTreeFlatRootTests { #expect(rootStatuses(fixture).isEmpty) await tearDown(fixture) } + + private var extensionKind: PluginServerObjectKind { + PluginServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", + iconName: "puzzlepiece.extension", scope: .database + ) + } + + private var extensions: SidebarObjectKind { + .server(SidebarServerObjectKind(extensionKind)) + } + + @Test("A declared kind with objects gets a section after the built-in kinds") + func declaredKindIsListedAfterBuiltIns() async throws { + let fixture = try await makeFixture(injectsSession: true) { driver in + driver.tablesToReturn = [TestFixtures.makeTableInfo(name: "orders", schema: "public")] + driver.declaredServerKinds = [extensionKind] + driver.serverObjectsByKind["extension"] = [ + ServerObjectInfo(kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384", detail: "1.3") + ] + } + + #expect(rootSections(fixture) == [.table, extensions]) + let section = try #require(section(fixture, extensions)) + let rows = fixture.coordinator.resolvedChildren(of: section) + guard case .serverObject(let ref) = rows.first?.kind else { + Issue.record("The Extensions section lists no extension row") + await tearDown(fixture) + return + } + #expect(rows.count == 1) + #expect(ref.object.name == "pgcrypto") + #expect(ref.kind.pluralDisplayName == "Extensions") + #expect(ref.objectRef.kind == .serverObject) + await tearDown(fixture) + } + + @Test("A declared kind that returned nothing gets no section") + func emptyDeclaredKindIsOmitted() async throws { + let fixture = try await makeFixture(injectsSession: true) { driver in + driver.tablesToReturn = [TestFixtures.makeTableInfo(name: "orders", schema: "public")] + driver.declaredServerKinds = [extensionKind] + } + + #expect(rootSections(fixture) == [.table]) + await tearDown(fixture) + } + + @Test("A declared kind whose fetch failed keeps its section, showing the error") + func failedDeclaredKindKeepsItsSection() async throws { + let fixture = try await makeFixture(injectsSession: true) { driver in + driver.tablesToReturn = [TestFixtures.makeTableInfo(name: "orders", schema: "public")] + driver.declaredServerKinds = [extensionKind] + driver.serverObjectsError = boom + } + + #expect(rootSections(fixture) == [.table, extensions]) + let section = try #require(section(fixture, extensions)) + let rows = fixture.coordinator.resolvedChildren(of: section) + guard case .status(let status) = rows.first?.kind else { + Issue.record("The failed section shows no status row") + await tearDown(fixture) + return + } + #expect(status == .error("permission denied")) + await tearDown(fixture) + } + + @Test("A search that matches only a server object keeps its section") + func searchMatchingOnlyAServerObject() async throws { + let fixture = try await makeFixture(injectsSession: true) { driver in + driver.tablesToReturn = [TestFixtures.makeTableInfo(name: "orders", schema: "public")] + driver.declaredServerKinds = [extensionKind] + driver.serverObjectsByKind["extension"] = [ServerObjectInfo(kindId: "extension", name: "pgcrypto", schema: "public")] + } + fixture.viewModel.searchText = "crypto" + fixture.coordinator.searchText = "crypto" + fixture.coordinator.refresh() + + #expect(rootSections(fixture) == [extensions]) + await tearDown(fixture) + } } diff --git a/apple/TableProTests/Views/Sidebar/DatabaseTreeMenuSpecTests.swift b/apple/TableProTests/Views/Sidebar/DatabaseTreeMenuSpecTests.swift index 37660fb554..7e0d7ea310 100644 --- a/apple/TableProTests/Views/Sidebar/DatabaseTreeMenuSpecTests.swift +++ b/apple/TableProTests/Views/Sidebar/DatabaseTreeMenuSpecTests.swift @@ -1041,6 +1041,55 @@ struct DatabaseTreeMenuSpecTests { } } + // MARK: - Server objects + + private var eventKind: SidebarServerObjectKind { + SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + } + + private func serverObjectRef(_ name: String) -> DatabaseTreeServerObjectRef { + DatabaseTreeServerObjectRef( + database: "shop", schema: "shop", kind: eventKind, + object: ServerObjectInfo(kindId: "event", name: name, schema: "shop", detail: "Recurring") + ) + } + + @Test("A server object row copies its name and shows its DDL, and offers nothing that writes") + func serverObjectRowItems() { + let ref = serverObjectRef("nightly_cleanup") + let sections = DatabaseTreeMenuSpec.sections(for: context(clicked: .serverObject(ref))) + + #expect(commands(sections) == [.copyText("nightly_cleanup"), .showObjectSource(ref.objectRef)]) + #expect(titles(sections) == [String(localized: "Copy Name"), String(localized: "Show DDL")]) + #expect(ref.objectRef.kind == .serverObject) + #expect(ref.objectRef.serverKind?.id == "event") + } + + @Test("A server object kind's group offers Refresh and no New Folder or New Type") + func serverObjectGroupItems() { + let group = DatabaseTreeObjectGroup(database: "shop", schema: nil, kind: .server(eventKind)) + let sections = DatabaseTreeMenuSpec.sections(for: context(clicked: .containerObjectKindSection(group))) + + #expect(commands(sections) == [.refreshContainerObjectKind(group)]) + } + + @Test("A flat server object section offers Refresh alone") + func flatServerObjectSectionItems() { + let sections = DatabaseTreeMenuSpec.sections(for: context(clicked: .objectKindSection(.server(eventKind)))) + + #expect(commands(sections) == [.refreshObjectKind(.server(eventKind))]) + } + + @Test("A read-only connection keeps the server object row's menu") + func serverObjectRowReadOnly() { + let ref = serverObjectRef("nightly_cleanup") + let sections = DatabaseTreeMenuSpec.sections(for: context(clicked: .serverObject(ref), isReadOnly: true)) + + #expect(commands(sections) == [.copyText("nightly_cleanup"), .showObjectSource(ref.objectRef)]) + } + // MARK: - Types private func userTypeRef(_ name: String, schema: String? = "public") -> DatabaseTreeUserTypeRef { diff --git a/apple/TableProTests/Views/Sidebar/DatabaseTreeNodeTests.swift b/apple/TableProTests/Views/Sidebar/DatabaseTreeNodeTests.swift index bd0cb34067..427f6f3421 100644 --- a/apple/TableProTests/Views/Sidebar/DatabaseTreeNodeTests.swift +++ b/apple/TableProTests/Views/Sidebar/DatabaseTreeNodeTests.swift @@ -333,4 +333,87 @@ struct DatabaseTreeNodeTests { #expect(groups.map(\.kind) == [.procedure]) #expect(groups.allSatisfy { $0.schema == "HR" }) } + + // MARK: - Server objects + + private let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + private let extensions = SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", iconName: "puzzlepiece.extension", + scope: .database + ) + + @Test("A container kind's group follows Types in a container and carries the declared descriptor") + func containerKindGroupFollowsTypes() { + let groups = DatabaseTreeObjectGroupResolver.groups( + database: "shop", + schema: nil, + itemCounts: [.table: 2, .trigger: 1, .type: 1, .server(SidebarServerObjectKind(id: "event")): 3], + declared: [events] + ) + + #expect(groups.map { $0.kind } == [.table, .trigger, .type, .server(events)]) + #expect(groups.last?.kind.pluralDisplayName == "Events") + } + + @Test("A database kind's group lists beside the schemas only when it holds rows or failed") + func databaseKindGroupNeedsRowsOrFailure() { + let empty = DatabaseTreeObjectGroupResolver.groups(database: "app", schema: nil, itemCounts: [:], declared: [extensions]) + #expect(empty.isEmpty) + + let phases = DatabaseTreeSidePhases( + routines: .idle, triggers: nil, types: nil, serverObjects: ["extension": .failed("permission denied")] + ) + let failed = DatabaseTreeObjectGroupResolver.groups( + database: "app", + schema: nil, + itemCounts: [:], + keeping: phases.failedKinds(among: [.server(extensions)]), + declared: [extensions] + ) + #expect(failed.map { $0.kind } == [.server(extensions)]) + #expect(phases.placeholder(for: .server(extensions)) == .error("permission denied")) + } + + @Test("A server kind's phase is its own fetch's, and an undeclared kind reads as loaded") + func serverKindPhases() { + let phases = DatabaseTreeSidePhases( + routines: .loaded, triggers: .loaded, types: nil, serverObjects: ["event": .loading] + ) + + #expect(phases.phase(for: .server(events)) == .loading) + #expect(phases.phase(for: .server(extensions)) == .loaded) + #expect(phases.all == [.loaded, .loaded, .loading]) + #expect(DatabaseTreeNode.Status.emptyContainer(sideStates: phases.all) == .loading) + #expect(phases.placeholder(for: .server(events)) == nil) + } + + @Test("A server object row is a selectable leaf with an id of its own") + func serverObjectRowIdentity() { + let object = ServerObjectInfo(kindId: "event", name: "nightly", schema: "shop") + let ref = DatabaseTreeServerObjectRef(database: "shop", schema: "shop", kind: events, object: object) + let node = DatabaseTreeNode(id: DatabaseTreeNode.serverObjectId(ref), kind: .serverObject(ref)) + let trigger = DatabaseTreeTriggerRef( + database: "shop", schema: "shop", + trigger: TriggerInfo(name: "nightly", timing: "AFTER", event: "INSERT", statement: "") + ) + + #expect(!node.isExpandable) + #expect(!node.isGroupRow) + #expect(!node.isContainer) + #expect(DatabaseTreeSelection.isSelectable(node.kind)) + #expect(DatabaseTreeNode.serverObjectId(ref) != DatabaseTreeNode.triggerId(trigger)) + #expect(DatabaseTreeDoubleClickResolver.resolve(node: node) == .openObjectSource(ref.objectRef)) + } + + @Test("A declared kind's group id spells its persistence name, and built-in ids are unchanged") + func serverGroupIdsUsePersistenceName() { + #expect(DatabaseTreeNode.objectKindSectionId(.table) == "kindSection\u{1}table") + #expect( + DatabaseTreeNode.containerObjectKindSectionId(objectGroup(kind: .materializedView)) + == "containerKindSection\u{1}shop\u{1}public\u{1}materializedView" + ) + #expect(DatabaseTreeNode.objectKindSectionId(.server(events)) == "kindSection\u{1}server.event") + } } diff --git a/apple/TableProTests/Views/Sidebar/ServerObjectRowLogicTests.swift b/apple/TableProTests/Views/Sidebar/ServerObjectRowLogicTests.swift new file mode 100644 index 0000000000..f25dc8f89e --- /dev/null +++ b/apple/TableProTests/Views/Sidebar/ServerObjectRowLogicTests.swift @@ -0,0 +1,67 @@ +// +// ServerObjectRowLogicTests.swift +// TableProTests +// + +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct ServerObjectRowLogicTests { + private let events = SidebarServerObjectKind( + id: "event", displayName: "Event", pluralDisplayName: "Events", iconName: "calendar.badge.clock", scope: .container + ) + private let extensions = SidebarServerObjectKind( + id: "extension", displayName: "Extension", pluralDisplayName: "Extensions", iconName: "puzzlepiece.extension", + scope: .database + ) + + private func event(enabled: Bool?, status: String) -> ServerObjectInfo { + ServerObjectInfo( + kindId: "event", name: "nightly", schema: "shop", detail: "Recurring", isEnabled: enabled, + attributes: [ObjectAttribute(label: "Status", value: status), ObjectAttribute(label: "Schedule", value: "EVERY 1 DAY")] + ) + } + + @Test("The tooltip names the state once, in the driver's own words") + func tooltipNamesStateOnce() { + let tooltip = ServerObjectRowLogic.tooltip(for: event(enabled: false, status: "Disabled on Replica"), kind: events) + + #expect(tooltip == "shop.nightly\nRecurring\nStatus: Disabled on Replica\nSchedule: EVERY 1 DAY") + } + + @Test("A database-wide object's tooltip opens with its bare name") + func databaseScopedTooltipIsBare() { + let object = ServerObjectInfo( + kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384", detail: "1.3", + attributes: [ObjectAttribute(label: "Schema", value: "public")] + ) + + #expect(ServerObjectRowLogic.tooltip(for: object, kind: extensions) == "pgcrypto\n1.3\nSchema: public") + #expect(ServerObjectRowLogic.displayName(for: object, kind: extensions) == "pgcrypto") + #expect(ServerObjectRowLogic.displayName(for: event(enabled: true, status: "Enabled"), kind: events) == "shop.nightly") + } + + @Test("The accessibility label reads kind, name and detail") + func accessibilityLabel() { + let label = ServerObjectRowLogic.accessibilityLabel(for: event(enabled: true, status: "Enabled"), kind: events) + #expect(label == String(format: String(localized: "%1$@: %2$@"), "Event", "nightly") + ", Recurring") + } + + // `ServerObjectInfo` is equal by id, so a row holding it would not redraw a new version or state. + @Test("A refreshed object with the same id hands the row new drawn values") + func refreshedObjectChangesTheDrawnValues() { + let before = ServerObjectInfo(kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384", detail: "1.3") + let after = ServerObjectInfo(kindId: "extension", name: "pgcrypto", schema: "public", identity: "16384", detail: "1.4") + #expect(before == after) + + let oldRow = ServerObjectRowView(object: before, kind: extensions) + let newRow = ServerObjectRowView(object: after, kind: extensions) + + #expect(oldRow.detail == "1.3") + #expect(newRow.detail == "1.4") + #expect(oldRow.tooltip != newRow.tooltip) + #expect(newRow.iconName == "puzzlepiece.extension") + } +} diff --git a/apple/TableProUITests/ManualCommitUITests.swift b/apple/TableProUITests/ManualCommitUITests.swift new file mode 100644 index 0000000000..f355a330eb --- /dev/null +++ b/apple/TableProUITests/ManualCommitUITests.swift @@ -0,0 +1,28 @@ +import XCTest + +final class ManualCommitUITests: UITestCase { + func testManualCommitHoldsAnUpdateUntilItIsRolledBack() throws { + let launch = try launchWithFixture() + let window = launch.window + let app = launch.app + XCTAssertTrue(app.menuBars.firstMatch.waitToExist(timeout: 20), "The app must publish its menu bar") + + menuItem("Manual Commit", under: "Database", in: app).click() + typeQuery("UPDATE note SET body = 'Held' WHERE id = 1;", in: window) + app.typeShortcut(.runQuery) + + let bar = window.descendants(matching: .any)["connection-transaction-bar"].firstMatch + XCTAssertTrue(bar.waitToExist(timeout: 20), "A write in Manual Commit must leave its transaction open") + + let rollBack = bar.buttons["Roll Back"].firstMatch + XCTAssertTrue(waitUntilHittable(rollBack, timeout: 10), "The bar must offer Roll Back") + rollBack.click() + let confirm = window.sheets.buttons["Roll Back"].firstMatch + XCTAssertTrue(waitUntilHittable(confirm, timeout: 10), "Roll Back must ask first") + confirm.click() + + XCTAssertTrue(bar.waitForNonExistence(timeout: 20), "Rolling back must end the transaction the bar shows") + let rows = FixtureFile.rows("SELECT body FROM note WHERE id = 1", in: launch.database) + XCTAssertEqual(rows, [["Buy milk"]], "The rolled-back UPDATE must leave the row as it was") + } +} diff --git a/apple/TableProUITests/TableAutoRefreshUITests.swift b/apple/TableProUITests/TableAutoRefreshUITests.swift new file mode 100644 index 0000000000..fa6980e740 --- /dev/null +++ b/apple/TableProUITests/TableAutoRefreshUITests.swift @@ -0,0 +1,26 @@ +import XCTest + +final class TableAutoRefreshUITests: UITestCase { + func testAutoRefreshShowsAWriteMadeElsewhereWithoutARefresh() throws { + let (launch, grid) = try launchWithFixtureTable("document") + let app = launch.app + XCTAssertTrue(grid.waitForValue("First", row: 1, column: 2), "document must open on its first row") + + // The 1024x768 runner gives the status bar its narrowest tier, which leaves Auto Refresh to this menu. + let autoRefresh = menuItem("Auto Refresh", under: "Database", in: app) + autoRefresh.click() + let everyFiveSeconds = autoRefresh.menuItems["Every 5 Seconds"].firstMatch + XCTAssertTrue(everyFiveSeconds.waitToExist(timeout: 10), "Database > Auto Refresh must list Every 5 Seconds") + everyFiveSeconds.click() + + XCTAssertTrue( + FixtureFile.execute("UPDATE document SET label = 'Written Elsewhere' WHERE id = 1", in: launch.database), + "The fixture file must take the write" + ) + XCTAssertTrue( + grid.waitForValue("Written Elsewhere", row: 1, column: 2, timeout: 30), + "Auto Refresh must show the write with no Refresh, the grid shows " + + "'\(grid.value(row: 1, column: 2) ?? "nil")'" + ) + } +} diff --git a/apple/changelog.d/4214.added.1.md b/apple/changelog.d/4214.added.1.md new file mode 100644 index 0000000000..626ed8ec13 --- /dev/null +++ b/apple/changelog.d/4214.added.1.md @@ -0,0 +1 @@ +A table tab can refresh itself on an interval, set from its status bar or Database > Auto Refresh. diff --git a/apple/changelog.d/4214.added.2.md b/apple/changelog.d/4214.added.2.md new file mode 100644 index 0000000000..83152ab3c9 --- /dev/null +++ b/apple/changelog.d/4214.added.2.md @@ -0,0 +1 @@ +Query tabs keep earlier versions of their text, listed under File > Revert To. diff --git a/apple/changelog.d/4214.added.3.md b/apple/changelog.d/4214.added.3.md new file mode 100644 index 0000000000..e9ea1e5804 --- /dev/null +++ b/apple/changelog.d/4214.added.3.md @@ -0,0 +1 @@ +The sidebar lists MySQL and MariaDB events and PostgreSQL extensions, and opens their DDL. diff --git a/apple/changelog.d/4214.added.4.md b/apple/changelog.d/4214.added.4.md new file mode 100644 index 0000000000..9d91cb3400 --- /dev/null +++ b/apple/changelog.d/4214.added.4.md @@ -0,0 +1 @@ +The ER diagram's Add Foreign Key sheet builds keys over several columns. diff --git a/apple/changelog.d/4214.added.md b/apple/changelog.d/4214.added.md new file mode 100644 index 0000000000..3b8c703b40 --- /dev/null +++ b/apple/changelog.d/4214.added.md @@ -0,0 +1 @@ +Manual Commit keeps a connection's changes in a transaction until you commit or roll back. diff --git a/apple/changelog.d/4214.fixed.1.md b/apple/changelog.d/4214.fixed.1.md new file mode 100644 index 0000000000..3e96339e89 --- /dev/null +++ b/apple/changelog.d/4214.fixed.1.md @@ -0,0 +1 @@ +Closing, disconnecting or quitting did not mention an open transaction it was about to roll back. diff --git a/apple/changelog.d/4214.fixed.10.md b/apple/changelog.d/4214.fixed.10.md new file mode 100644 index 0000000000..67cc9c96bb --- /dev/null +++ b/apple/changelog.d/4214.fixed.10.md @@ -0,0 +1 @@ +An MCP insert_rows call that failed partway inside an open transaction left its earlier rows pending. diff --git a/apple/changelog.d/4214.fixed.2.md b/apple/changelog.d/4214.fixed.2.md new file mode 100644 index 0000000000..2015bc0a7e --- /dev/null +++ b/apple/changelog.d/4214.fixed.2.md @@ -0,0 +1 @@ +Commit and Roll Back in the transaction bar refused after any background read on the connection. diff --git a/apple/changelog.d/4214.fixed.3.md b/apple/changelog.d/4214.fixed.3.md new file mode 100644 index 0000000000..443c047748 --- /dev/null +++ b/apple/changelog.d/4214.fixed.3.md @@ -0,0 +1 @@ +The transaction bar's Commit button did not follow a Safe Mode change until the next statement. diff --git a/apple/changelog.d/4214.fixed.4.md b/apple/changelog.d/4214.fixed.4.md new file mode 100644 index 0000000000..349037c127 --- /dev/null +++ b/apple/changelog.d/4214.fixed.4.md @@ -0,0 +1 @@ +An MCP client could start a transaction over an open one, which on MySQL commits the open one. diff --git a/apple/changelog.d/4214.fixed.5.md b/apple/changelog.d/4214.fixed.5.md new file mode 100644 index 0000000000..873610b77c --- /dev/null +++ b/apple/changelog.d/4214.fixed.5.md @@ -0,0 +1 @@ +A refresh turned a selected cell or cell range into whole selected rows. diff --git a/apple/changelog.d/4214.fixed.6.md b/apple/changelog.d/4214.fixed.6.md new file mode 100644 index 0000000000..520d92aff3 --- /dev/null +++ b/apple/changelog.d/4214.fixed.6.md @@ -0,0 +1 @@ +Clear Query, loading a query, AI apply and inserting a saved query could not be undone. diff --git a/apple/changelog.d/4214.fixed.7.md b/apple/changelog.d/4214.fixed.7.md new file mode 100644 index 0000000000..b49cce4f28 --- /dev/null +++ b/apple/changelog.d/4214.fixed.7.md @@ -0,0 +1 @@ +Typing in a second window of a connection waited up to 30 seconds to be saved. diff --git a/apple/changelog.d/4214.fixed.8.md b/apple/changelog.d/4214.fixed.8.md new file mode 100644 index 0000000000..6b69890e33 --- /dev/null +++ b/apple/changelog.d/4214.fixed.8.md @@ -0,0 +1 @@ +The Structure tab accepted a foreign key whose column and referenced column counts differ. diff --git a/apple/changelog.d/4214.fixed.9.md b/apple/changelog.d/4214.fixed.9.md new file mode 100644 index 0000000000..32f12e10e1 --- /dev/null +++ b/apple/changelog.d/4214.fixed.9.md @@ -0,0 +1 @@ +The Server Dashboard's refresh interval was not shown in your language. diff --git a/apple/changelog.d/4214.fixed.md b/apple/changelog.d/4214.fixed.md new file mode 100644 index 0000000000..0019928d55 --- /dev/null +++ b/apple/changelog.d/4214.fixed.md @@ -0,0 +1 @@ +On DuckDB and PGlite, saving a structure, a new table or a trigger committed a transaction left open.