From 398ccec35792bed5f5504dbb239389f9c8bf5179 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 9 Nov 2026 21:10:03 +0700 Subject: [PATCH 01/13] refactor(table-health): typed findings, history ledger and one tab per database --- .../EditorTabLabelResolver.swift | 67 ++-- .../Infrastructure/EditorTabOpener.swift | 17 +- ...inSplitViewController+MenuValidation.swift | 4 +- .../MainSplitViewController+RecentTabs.swift | 43 +-- .../MySQLTableHealthProvider.swift | 137 +++++-- .../PostgreSQLTableHealthProvider.swift | 210 +++++++++-- .../TableHealth/TableHealthFixScript.swift | 35 ++ .../TableHealthHistoryStoring.swift | 37 ++ .../Core/TableHealth/TableHealthLedger.swift | 348 ++++++++++++++++++ .../TableHealth/TableHealthProvider.swift | 79 +++- .../TableHealth/TableHealthReportLoader.swift | 32 ++ .../Models/Query/QueryTabManager.swift | 25 +- .../TableHealth/TableHealthModels.swift | 248 ++++++++++++- .../ViewModels/TableHealthViewModel.swift | 16 +- .../AIChat/AIChatWalkthroughBlockView.swift | 7 +- .../ConnectionUnavailableView.swift | 3 +- .../Main/Child/MainEditorContentView.swift | 6 +- .../MainContentCoordinator+TableHealth.swift | 29 ++ ...nContentCommandActions+DatabaseTools.swift | 8 +- .../QueryInsightsSummaryBar.swift | 7 +- .../QueryInsights/QueryInsightsView.swift | 2 +- .../Results/GeometryMapWindowController.swift | 2 +- .../Views/Results/ResultMapToolbar.swift | 2 +- .../ServerDashboard/MetricsBarView.swift | 2 +- .../ServerDashboard/ServerDashboardView.swift | 2 +- .../Views/TableHealth/TableHealthView.swift | 19 +- .../Core/Menu/MainMenuBuilderTests.swift | 13 + .../Infrastructure/EditorTabOpenerTests.swift | 37 ++ .../MySQLTableHealthProviderTests.swift | 106 ++++-- .../PostgreSQLTableHealthProviderTests.swift | 209 +++++++++-- .../TableHealthFixScriptTests.swift | 87 +++++ .../TableHealth/TableHealthLedgerTests.swift | 255 +++++++++++++ .../TableHealth/TableHealthTestSupport.swift | 39 +- ...QueryTabManagerVisibleContainerTests.swift | 52 +++ .../Main/EditorTabLabelResolverTests.swift | 29 ++ .../MainContentCoordinatorRefreshTests.swift | 5 +- 36 files changed, 1948 insertions(+), 271 deletions(-) create mode 100644 apple/TablePro/Core/TableHealth/TableHealthFixScript.swift create mode 100644 apple/TablePro/Core/TableHealth/TableHealthHistoryStoring.swift create mode 100644 apple/TablePro/Core/TableHealth/TableHealthLedger.swift create mode 100644 apple/TablePro/Core/TableHealth/TableHealthReportLoader.swift create mode 100644 apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealth.swift create mode 100644 apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift create mode 100644 apple/TableProTests/Core/TableHealth/TableHealthLedgerTests.swift diff --git a/apple/TablePro/Core/Services/Infrastructure/EditorTabLabelResolver.swift b/apple/TablePro/Core/Services/Infrastructure/EditorTabLabelResolver.swift index 57012883e1..7c5fc295d6 100644 --- a/apple/TablePro/Core/Services/Infrastructure/EditorTabLabelResolver.swift +++ b/apple/TablePro/Core/Services/Infrastructure/EditorTabLabelResolver.swift @@ -5,37 +5,23 @@ import Foundation -/// What the editor tab strip draws on each tab, and what it says about that tab on hover and to -/// VoiceOver. -/// -/// A tab's title names the object it shows, and two databases of one connection routinely hold the -/// same object name, so browsing both leaves two tabs reading `role_ability` with nothing to tell -/// them apart. The container qualifies the label when, and only when, another open tab would draw -/// the same one. That is what DataGrip does across schemas and VS Code across folders: a name that -/// is already unique stays short, because a bar of qualified names is harder to read, not easier. -/// -/// Only a tab that names an object is ever qualified. `container.name` reads as a path to that -/// object, which a Server Dashboard or a Users & Roles tab is not: those are server work that -/// happens to have been opened from a database, and `mydb.Server Dashboard` names nothing. -/// -/// The tooltip carries the container either way, since hovering has no width budget. The -/// accessibility label stays on the drawn text, so what VoiceOver reads is what is on screen; when -/// two tabs really do collide the drawn text is already qualified, and both channels say so. -/// -/// Resolved for rendering rather than written into `QueryTab.title`, because qualification is a -/// function of what else is open: baked into the tab it would outlive the sibling that caused it, -/// follow the tab into persistence, and come back out of Recently Closed long after it meant -/// anything. +// Two databases often hold the same object name, so a tab is qualified with its container only when another +// open tab would draw the same label: a bar of qualified names is harder to read. Only a tab naming an object +// is qualified, since `mydb.Server Dashboard` names nothing. Resolved per render, never written into the title, +// because it depends on what else is open and would otherwise outlive that sibling in persistence. internal enum EditorTabLabelResolver { internal struct Label: Equatable { - /// What the strip draws, and what VoiceOver reads. + // Drawn on the strip and read by VoiceOver. internal let text: String - /// What hovering says. + // The tooltip, which has room for the container whether or not the text needed it. internal let description: String + // The container the text leaves out, for a line beside it such as the Control-Tab detail. + internal let container: String? - internal init(text: String, description: String) { + internal init(text: String, description: String, container: String? = nil) { self.text = text self.description = description + self.container = container } } @@ -52,21 +38,36 @@ internal enum EditorTabLabelResolver { } return tabs.reduce(into: [:]) { result, tab in - /// More than one container behind one title is the whole test. Two tabs sharing a title - /// inside one container are not told apart by naming that container, so they stay bare. + if let database = checkedDatabase(of: tab) { + result[tab.id] = Label( + text: tab.title, + description: String(format: String(localized: "%@: %@"), tab.title, database), + container: database + ) + return + } + let container = containers[tab.id] ?? "" + // Two tabs sharing a title inside one container are not told apart by naming it. let isAmbiguous = (containersByTitle[tab.title]?.count ?? 0) > 1 - let qualified = qualify(tab.title, with: containers[tab.id]) + let qualified = qualify(tab.title, with: container) + let text = isAmbiguous ? qualified : tab.title result[tab.id] = Label( - text: isAmbiguous ? qualified : tab.title, - description: qualified + text: text, + description: qualified, + container: container.isEmpty || text.hasPrefix("\(container).") ? nil : container ) } } - /// On an engine whose containers are schemas, a table tab outside the default schema is already - /// titled `reporting.orders`, so prefixing the container again would read `reporting.reporting.orders`. - private static func qualify(_ title: String, with container: String?) -> String { - guard let container, !container.isEmpty, !title.hasPrefix("\(container).") else { return title } + // Every Table Health tab carries the same title, one per database, so the database is said beside it. + private static func checkedDatabase(of tab: QueryTab) -> String? { + guard tab.tabType == .tableHealth, !tab.tableContext.databaseName.isEmpty else { return nil } + return tab.tableContext.databaseName + } + + // A schema engine already titles a table outside the default schema `reporting.orders`. + private static func qualify(_ title: String, with container: String) -> String { + guard !container.isEmpty, !title.hasPrefix("\(container).") else { return title } return "\(container).\(title)" } } diff --git a/apple/TablePro/Core/Services/Infrastructure/EditorTabOpener.swift b/apple/TablePro/Core/Services/Infrastructure/EditorTabOpener.swift index fc2d84e0af..a62bc8a3ad 100644 --- a/apple/TablePro/Core/Services/Infrastructure/EditorTabOpener.swift +++ b/apple/TablePro/Core/Services/Infrastructure/EditorTabOpener.swift @@ -6,9 +6,7 @@ import Foundation import os -/// Turns an `EditorTabPayload` into a tab. This is the single translation from "what the user -/// asked to open" to "a tab in a connection's list", so a payload opens the same tab whether the -/// connection is being created for it or has been open for hours. +// The one payload-to-tab translation, so a payload opens the same tab on a new connection and on one open for hours. @MainActor internal enum EditorTabOpener { nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "EditorTabOpener") @@ -20,9 +18,8 @@ internal enum EditorTabOpener { toolbarState: ConnectionToolbarState? ) { let browseDatabaseName = DatabaseManager.shared.browseDatabaseName(for: connection) - /// A table named without a database is the one in the database being browsed, the same answer - /// the lookup for a tab already showing it gives. Anything else opened without one belongs to - /// the entry on screen, which the cursor lags while a switch to it is on its way. + // A bare table is the browsed database's, as the lookup for an open copy answers. Anything else + // goes to the entry on screen, which the browse cursor lags during a switch. let openingDatabaseName = tabManager.openingDatabase(browsing: browseDatabaseName) switch payload.intent { @@ -90,8 +87,10 @@ internal enum EditorTabOpener { databaseName: payload.databaseName ?? openingDatabaseName, schemaName: payload.schemaName ) - case .serverDashboard, .usersRoles, .insights, .tableHealth: + case .serverDashboard, .usersRoles, .insights: tabManager.addSingletonTab(payload.tabType) + case .tableHealth: + tabManager.addTableHealthTab(databaseName: payload.databaseName ?? openingDatabaseName) case .objectSource: guard let objectRef = payload.objectRef else { return } tabManager.addObjectSourceTab(objectRef: objectRef) @@ -144,9 +143,7 @@ internal enum EditorTabOpener { return } - /// Only a tab this payload created may take the payload's state. An existing tab was - /// reselected, not asked for, and writing the payload's filter over its own would destroy - /// work the user did there while leaving the grid on rows the new filter never ran. + // A reselected tab keeps its own filter: the payload's would replace the user's work over rows it never ran on. guard didCreateTab, let index = tabManager.selectedTabIndex else { return } tabManager.tabs[index].tableContext.isView = payload.isView tabManager.tabs[index].tableContext.objectType = payload.objectType diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift index 3083a75b24..c5a8b64834 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift @@ -124,6 +124,7 @@ struct MenuValidationContext: Equatable { var supportsServerDashboard = false var supportsUserManagement = false var supportsTableHealth = false + var hasDatabase = false var supportsSchemaSwitching = false // Same rule as the editor bar, so the menu cannot run a statement the bar's button refuses. var supportsExplain = false @@ -301,7 +302,7 @@ extension MainSplitViewController: NSMenuItemValidation { case #selector(showQueryInsights(_:)): return context.isConnected case #selector(showTableHealth(_:)): - return context.isConnected && context.supportsTableHealth + return context.isConnected && context.supportsTableHealth && context.hasDatabase case #selector(openLiveMessages(_:)): return context.isConnected && !context.isAgentMode && context.canOpenLiveMessages @@ -744,6 +745,7 @@ extension MainSplitViewController: NSMenuItemValidation { supportsServerDashboard: actions.supportsServerDashboard, supportsUserManagement: actions.supportsUserManagement, supportsTableHealth: actions.supportsTableHealth, + hasDatabase: actions.hasDatabase, supportsSchemaSwitching: actions.supportsSchemaSwitching, supportsExplain: actions.supportsExplain, supportsFormatting: actions.supportsFormatting, diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+RecentTabs.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+RecentTabs.swift index e9068e4b76..eac03679e0 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+RecentTabs.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+RecentTabs.swift @@ -5,9 +5,8 @@ import AppKit -/// Control-Tab across every connection this window hosts. The order is derived from each -/// connection's own record of when its tabs were selected, so nothing here has to follow a tab -/// that closes, moves to another window or arrives from a restore. +// Control-Tab across the window's connections, ordered from each one's selection record, so nothing here follows a +// tab that closes, moves or is restored. internal extension MainSplitViewController { @objc func switchToRecentTab(_ sender: Any?) { switchRecentTab(.forward, sender: sender) @@ -17,8 +16,7 @@ internal extension MainSplitViewController { switchRecentTab(.backward, sender: sender) } - /// Control-Tab is the chord AppKit gives window tabs, so where there is no editor tab to switch - /// to it still switches the window's tabs rather than doing nothing. + // Control-Tab is AppKit's window-tab chord, so with no editor tab to switch to it still switches window tabs. private func switchRecentTab(_ direction: RecentTabSwitchDirection, sender: Any?) { guard canSwitchToRecentTab else { switch direction { @@ -40,8 +38,7 @@ internal extension MainSplitViewController { (view.window?.tabbedWindows?.count ?? 0) > 1 } - /// A tab other than the one on screen, which a connection with no tab open of its own can still - /// switch to in another connection. + // A connection with no tab of its own can still switch to another connection's. var hasRecentTabToSwitchTo: Bool { let tabCount = recentTabSources().reduce(0) { $0 + $1.tabIds.count } let showsOneOfThem = currentRecentTab.map(hostsOpenTab) ?? false @@ -62,8 +59,7 @@ internal extension MainSplitViewController { ) } - /// Only a connection that is on and browsing contributes. A tab behind a connecting or failed - /// pane, or behind Agent mode, is a tab the user could not see after switching to it. + // A tab behind a connecting or failed pane, or behind Agent mode, could not be seen after switching to it. private var recentTabWorkspaces: [ConnectionWorkspace] { workspaces.workspaces.filter(showsTabs) } @@ -75,10 +71,8 @@ internal extension MainSplitViewController { && workspace.resolvedContentMode == .browse } - /// Settles which connection's tabs count as seen: the one on screen, while the window is key and - /// its tabs are showing, and no other. Decided here from the window's own state rather than - /// cached per coordinator, because a window hosting several connections has exactly one answer - /// and every connection has to hear it, including one that just left the screen. + // Decided from the window, not cached per coordinator: a window has one answer and every connection must hear it, + // including one that just left the screen. func syncFrontmostTabManager() { let frontmost = Self.frontmostConnectionId( selectedConnectionId: workspaces.selectedConnectionId, @@ -93,7 +87,6 @@ internal extension MainSplitViewController { } } - /// At most one connection per window, and none while the window is not key. static func frontmostConnectionId( selectedConnectionId: UUID?, windowIsKey: Bool, @@ -120,8 +113,7 @@ internal extension MainSplitViewController { return RecentTabReference(connectionId: workspace.connectionId, tabId: tabId) } - /// A row names its connection only when the window hosts more than one, where two connections - /// can hold tabs with the same title. + // Two connections can hold tabs with the same title, so a row names its connection once the window hosts several. func recentTabCandidates() -> [RecentTabCandidate] { let hosted = recentTabWorkspaces let namesConnection = hosted.count > 1 @@ -134,14 +126,11 @@ internal extension MainSplitViewController { let connectionName = namesConnection ? workspace.connection?.name : nil for tab in tabs { let reference = RecentTabReference(connectionId: workspace.connectionId, tabId: tab.id) - let title = labels[tab.id]?.text ?? tab.title + let label = labels[tab.id] rows[reference] = RecentTabCandidate( reference: reference, - title: title, - detail: Self.recentTabDetail( - container: Self.recentTabContainer(of: tab, title: title, target: target), - connectionName: connectionName - ), + title: label?.text ?? tab.title, + detail: Self.recentTabDetail(container: label?.container, connectionName: connectionName), symbolName: Self.recentTabSymbol(for: tab) ) } @@ -179,14 +168,6 @@ internal extension MainSplitViewController { return manager.tabs.contains { $0.id == reference.tabId } } - /// The database or schema a table tab reads, unless the title already carries it. - private static func recentTabContainer(of tab: QueryTab, title: String, target: ContainerSwitchTarget?) -> String? { - guard tab.tableContext.tableName != nil, - let container = WorkspaceAnchoring.containerName(of: tab, target: target), - !container.isEmpty, !title.hasPrefix("\(container).") else { return nil } - return container - } - private static func recentTabDetail(container: String?, connectionName: String?) -> String { [container, connectionName].compactMap { $0 }.joined(separator: ", ") } @@ -202,7 +183,7 @@ internal extension MainSplitViewController { case .erDiagram: return "point.3.connected.trianglepath.dotted" case .serverDashboard: - return "gauge.with.dots.needle.33percent" + return "speedometer" case .usersRoles: return "person.2" case .insights: diff --git a/apple/TablePro/Core/TableHealth/MySQLTableHealthProvider.swift b/apple/TablePro/Core/TableHealth/MySQLTableHealthProvider.swift index fdeb5f3f91..9669688813 100644 --- a/apple/TablePro/Core/TableHealth/MySQLTableHealthProvider.swift +++ b/apple/TablePro/Core/TableHealth/MySQLTableHealthProvider.swift @@ -68,7 +68,9 @@ struct MySQLTableHealthProvider: TableHealthProvider { // MariaDB lists a system-versioned table under its own type. static let missingPrimaryKeyQuery = """ - SELECT t.TABLE_NAME AS table_name + SELECT t.TABLE_NAME AS table_name, + t.TABLE_ROWS AS estimated_rows, + t.DATA_LENGTH + t.INDEX_LENGTH AS table_bytes FROM information_schema.TABLES t WHERE t.TABLE_SCHEMA = DATABASE() AND t.TABLE_TYPE IN ('BASE TABLE', 'SYSTEM VERSIONED') @@ -81,16 +83,47 @@ struct MySQLTableHealthProvider: TableHealthProvider { ORDER BY t.TABLE_NAME """ + // Joined to the unused index candidates in Swift by exact name, like the index rows, so no SQL + // compares performance_schema names with information_schema ones, whose collations differ. + static let tableRowsQuery = """ + SELECT t.TABLE_NAME AS table_name, + t.TABLE_ROWS AS estimated_rows + FROM information_schema.TABLES t + WHERE t.TABLE_SCHEMA = DATABASE() + AND t.TABLE_TYPE IN ('BASE TABLE', 'SYSTEM VERSIONED') + ORDER BY t.TABLE_NAME + """ + + static let versionQuery = "SELECT VERSION() AS version" + var statisticsQueries: [String] { - [Self.performanceSchemaQuery, Self.uptimeQuery, Self.usageCountingQuery] + [Self.performanceSchemaQuery, Self.uptimeQuery, Self.usageCountingQuery, Self.versionQuery] } - func queries(for check: TableHealthCheckKind) -> [String] { + func check(_ kind: TableHealthCheckKind) -> TableHealthCheck { + switch kind { + case .unusedIndex: + return TableHealthCheck(kind: kind, summary: String( + localized: "Indexes with no recorded reads since the server started, leaving out unique, full-text and foreign key indexes." + )) + case .redundantIndex: + return TableHealthCheck(kind: kind, summary: String( + localized: "Indexes whose columns lead another visible index of the same type on the same table." + )) + case .missingPrimaryKey: + return TableHealthCheck(kind: kind, summary: String(localized: "Tables with no primary key.")) + case .bloat, .unindexedForeignKey, .invalidIndex, .nearLimit, .transactionIdAge, .staleStatistics, .reclaimableSpace: + return TableHealthCheck(kind: kind, summary: "") + } + } + + func queries(for check: TableHealthCheckKind, statistics: TableHealthStatistics) -> [String] { switch check { - case .unusedIndex: return [Self.unusedIndexQuery, Self.indexQuery, Self.foreignKeyQuery] + case .unusedIndex: return [Self.unusedIndexQuery, Self.indexQuery, Self.foreignKeyQuery, Self.tableRowsQuery] case .redundantIndex: return [Self.indexQuery] case .missingPrimaryKey: return [Self.missingPrimaryKeyQuery] - case .bloat, .unindexedForeignKey: return [] + case .bloat, .unindexedForeignKey, .invalidIndex, .nearLimit, .transactionIdAge, .staleStatistics, .reclaimableSpace: + return [] } } @@ -99,55 +132,91 @@ struct MySQLTableHealthProvider: TableHealthProvider { guard position < results.count, let result = results[position] else { return nil } return TableHealthRow.rows(of: result).first?[column] } - func skippingUnused(_ reason: String) -> TableHealthStatistics { - TableHealthStatistics(skippedChecks: [.unusedIndex: reason]) + + var statistics = TableHealthStatistics() + if let version = value(3, "version") { + statistics.serverVersion = Self.serverVersion(from: version) + statistics.isMariaDB = version.range(of: "MariaDB", options: .caseInsensitive) != nil + } + if let reason = Self.unusedSkipReason(value) { + statistics.skippedChecks[.unusedIndex] = reason + return statistics + } + if let uptime = value(1, "value").flatMap(Double.init) { + let start = TableHealthFormatting.date(now.addingTimeInterval(-uptime)) + statistics.notes.append(String(format: String(localized: "Index usage counts since the server started at %@."), start)) } + return statistics + } + private static func unusedSkipReason(_ value: (Int, String) -> String?) -> String? { if let enabled = value(0, "performance_schema"), enabled != "1", enabled.uppercased() != "ON" { - return skippingUnused(String(localized: "Performance Schema is off, so the server records no index reads.")) + return String(localized: "Performance Schema is off, so the server records no index reads.") } if value(2, "table_io_instrument") == "NO" { - return skippingUnused(String(localized: "The wait/io/table/sql/handler instrument is disabled, so the server records no index reads.")) + return String(localized: "The wait/io/table/sql/handler instrument is disabled, so the server records no index reads.") } if value(2, "global_instrumentation") == "NO" { - return skippingUnused(String(localized: "The global_instrumentation consumer is disabled, so the server records no index reads.")) + return String(localized: "The global_instrumentation consumer is disabled, so the server records no index reads.") } if value(2, "counted_tables") == "0" { - return skippingUnused(String( + return String( localized: "No table in this database has a recorded read or write since the server started, so an unread index cannot be told from an uncounted one." - )) + ) } + return nil + } - guard let uptime = value(1, "value").flatMap(Double.init) else { return TableHealthStatistics() } - let start = now.addingTimeInterval(-uptime).formatted(date: .abbreviated, time: .shortened) - return TableHealthStatistics( - note: String(format: String(localized: "Index usage counts since the server started at %@."), start) - ) + // "8.0.46" is 80046 and "10.6.28-MariaDB-1:10.6.28+maria~ubu2004" is 100628. A MariaDB server that + // fronts its version with "5.5.5-" for old clients is read past that prefix. + static func serverVersion(from text: String) -> Int? { + var version = Substring(text.trimmingCharacters(in: .whitespaces)) + if version.hasPrefix("5.5.5-") { version = version.dropFirst(6) } + let parts = version.prefix { $0.isASCII && ($0.isNumber || $0 == ".") } + .split(separator: ".", omittingEmptySubsequences: false) + .prefix(3) + .map { Int($0) } + guard let major = parts.first ?? nil else { return nil } + let minor = parts.count > 1 ? parts[1] ?? 0 : 0 + let patch = parts.count > 2 ? parts[2] ?? 0 : 0 + return major * 10_000 + minor * 100 + patch } func findings( for check: TableHealthCheckKind, from results: [QueryResult], - context: TableHealthQueryContext - ) -> [TableHealthFinding] { + context: TableHealthQueryContext, + statistics: TableHealthStatistics + ) -> (findings: [TableHealthFinding], note: String?) { switch check { case .unusedIndex: - guard results.count == 3 else { return [] } - return unusedIndexFindings( + guard results.count == 4 else { return ([], nil) } + return (unusedIndexFindings( candidates: TableHealthRow.rows(of: results[0]), tables: MySQLIndexShape.tables(from: TableHealthRow.rows(of: results[1])), foreignKeys: MySQLIndexShape.foreignKeys(from: TableHealthRow.rows(of: results[2])), + tableRows: Self.estimatedRows(from: TableHealthRow.rows(of: results[3])), context: context - ) + ), nil) case .redundantIndex: - guard let result = results.first else { return [] } - return redundantIndexFindings(MySQLIndexShape.tables(from: TableHealthRow.rows(of: result)), context) + guard let result = results.first else { return ([], nil) } + return (redundantIndexFindings(MySQLIndexShape.tables(from: TableHealthRow.rows(of: result)), context), nil) case .missingPrimaryKey: - guard let result = results.first else { return [] } - return TableHealthRow.rows(of: result).map { missingPrimaryKeyFinding($0["table_name"], context) } - case .bloat, .unindexedForeignKey: - return [] + guard let result = results.first else { return ([], nil) } + return (TableHealthRow.rows(of: result).map { missingPrimaryKeyFinding($0, context) }, nil) + case .bloat, .unindexedForeignKey, .invalidIndex, .nearLimit, .transactionIdAge, .staleStatistics, .reclaimableSpace: + return ([], nil) + } + } + + private static func estimatedRows(from rows: [TableHealthRow]) -> [String: Int64] { + var byTable: [String: Int64] = [:] + for row in rows { + if let count = row.int64("estimated_rows") { + byTable[row["table_name"]] = count + } } + return byTable } // A unique index enforces something no read counts, and a MATCH search reads a full-text index @@ -157,6 +226,7 @@ struct MySQLTableHealthProvider: TableHealthProvider { candidates: [TableHealthRow], tables: [MySQLIndexShape], foreignKeys: [String: [[String]]], + tableRows: [String: Int64], context: TableHealthQueryContext ) -> [TableHealthFinding] { let shapes = Dictionary(tables.map { ($0.name, $0) }, uniquingKeysWith: { first, _ in first }) @@ -170,6 +240,7 @@ struct MySQLTableHealthProvider: TableHealthProvider { return TableHealthFinding( kind: .unusedIndex, schema: nil, table: table, objectName: indexName, detail: String(format: String(localized: "%@ has no recorded reads."), indexName), + metrics: tableRows[table].map { [TableHealthMetric.estimatedRows($0)] } ?? [], fixSQL: dropIndexSQL(table: table, index: indexName, context) ) } @@ -186,6 +257,9 @@ struct MySQLTableHealthProvider: TableHealthProvider { findings.append(TableHealthFinding( kind: .redundantIndex, schema: nil, table: table.name, objectName: index.name, detail: String(format: String(localized: "%1$@ is covered by %2$@."), index.name, dominant.name), + metrics: [TableHealthMetric( + id: "covering_index", label: String(localized: "Covering Index"), value: .text(dominant.name) + )], fixSQL: dropIndexSQL(table: table.name, index: index.name, context) )) } @@ -193,10 +267,13 @@ struct MySQLTableHealthProvider: TableHealthProvider { return findings } - private func missingPrimaryKeyFinding(_ table: String, _ context: TableHealthQueryContext) -> TableHealthFinding { - TableHealthFinding( + private func missingPrimaryKeyFinding(_ row: TableHealthRow, _ context: TableHealthQueryContext) -> TableHealthFinding { + let table = row["table_name"] + return TableHealthFinding( kind: .missingPrimaryKey, schema: nil, table: table, objectName: nil, detail: String(localized: "No primary key. Replace column_name with the columns that identify a row."), + sizeBytes: row.int64("table_bytes"), + metrics: row.int64("estimated_rows").map { [TableHealthMetric.estimatedRows($0)] } ?? [], fixSQL: "ALTER TABLE \(context.qualified(context.database, table)) ADD PRIMARY KEY (column_name);" ) } diff --git a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift index 844ce65702..88c51caead 100644 --- a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift +++ b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift @@ -1,30 +1,49 @@ import Foundation // scripts/probes/check-table-health-catalogs.sh runs these literals against real servers: keep them free of interpolation. +// Every query leaves out what an extension owns, by relation and by schema: TimescaleDB's chunk tables are not extension +// members themselves, but their schema is, and a fix there would change the extension's own objects. struct PostgreSQLTableHealthProvider: TableHealthProvider { let checks: [TableHealthCheckKind] = [.bloat, .unusedIndex, .missingPrimaryKey, .unindexedForeignKey] + // The checks that read activity counters, which a standby keeps for its own queries only. + private static let counterChecks: [TableHealthCheckKind] = [.bloat, .unusedIndex, .staleStatistics] + static let statisticsQuery = """ SELECT pg_catalog.current_setting('track_counts') AS track_counts, - pg_catalog.date_trunc('second', d.stats_reset)::pg_catalog.text AS stats_reset + pg_catalog.date_part('epoch', d.stats_reset) AS stats_reset, + pg_catalog.current_setting('server_version_num') AS server_version_num, + pg_catalog.pg_is_in_recovery() AS in_recovery FROM pg_catalog.pg_stat_database d WHERE d.datname = pg_catalog.current_database() """ - // Autovacuum's own trigger is 20% dead rows plus 50; the floor keeps tiny tables out. A pg_ - // schema is another session's temporary one. + // The floor keeps tiny tables out. A pg_ schema is another session's temporary one. static let bloatQuery = """ SELECT s.schemaname AS schema_name, s.relname AS table_name, s.n_live_tup AS live_rows, s.n_dead_tup AS dead_rows, - pg_catalog.date_trunc('second', GREATEST(s.last_vacuum, s.last_autovacuum))::pg_catalog.text AS last_vacuum + pg_catalog.date_part('epoch', GREATEST(s.last_vacuum, s.last_autovacuum)) AS last_vacuum, + pg_catalog.pg_table_size(c.oid) AS table_bytes FROM pg_catalog.pg_stat_user_tables s JOIN pg_catalog.pg_class c ON c.oid = s.relid WHERE c.relkind = 'r' AND pg_catalog.left(s.schemaname, 3) <> 'pg_' AND s.n_dead_tup >= 1000 AND s.n_dead_tup >= s.n_live_tup / 5 + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND x.objid = c.oid + AND x.deptype = 'e' + ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_namespace'::pg_catalog.regclass + AND x.objid = c.relnamespace + AND x.deptype = 'e' + ) ORDER BY s.n_dead_tup DESC, s.schemaname, s.relname """ @@ -58,13 +77,27 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { AND (i.indkey::pg_catalog.int2[])[0:pg_catalog.array_length(fk.conkey, 1) - 1] OPERATOR(pg_catalog.@>) fk.conkey ) ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND x.objid = t.oid + AND x.deptype = 'e' + ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_namespace'::pg_catalog.regclass + AND x.objid = t.relnamespace + AND x.deptype = 'e' + ) ORDER BY index_bytes DESC, schema_name, index_name """ // A partition takes its primary key from its parent, so only the parent is listed. static let missingPrimaryKeyQuery = """ SELECT n.nspname AS schema_name, - c.relname AS table_name + c.relname AS table_name, + GREATEST(c.reltuples, 0)::pg_catalog.int8 AS estimated_rows, + pg_catalog.pg_table_size(c.oid) AS table_bytes FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace WHERE c.relkind IN ('r', 'p') @@ -79,6 +112,18 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { JOIN pg_catalog.pg_class p ON p.oid = h.inhparent WHERE h.inhrelid = c.oid AND p.relkind = 'p' ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND x.objid = c.oid + AND x.deptype = 'e' + ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_namespace'::pg_catalog.regclass + AND x.objid = c.relnamespace + AND x.deptype = 'e' + ) ORDER BY n.nspname, c.relname """ @@ -91,10 +136,16 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { c.relname AS table_name, c.relkind AS table_kind, con.conname AS constraint_name, - a.attname AS column_name + a.attname AS column_name, + rn.nspname AS referenced_schema, + rc.relname AS referenced_table, + GREATEST(c.reltuples, 0)::pg_catalog.int8 AS estimated_rows, + pg_catalog.pg_table_size(c.oid) AS table_bytes FROM pg_catalog.pg_constraint con JOIN pg_catalog.pg_class c ON c.oid = con.conrelid JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + JOIN pg_catalog.pg_class rc ON rc.oid = con.confrelid + JOIN pg_catalog.pg_namespace rn ON rn.oid = rc.relnamespace CROSS JOIN LATERAL pg_catalog.unnest(con.conkey) WITH ORDINALITY AS k(attnum, position) JOIN pg_catalog.pg_attribute a ON a.attrelid = con.conrelid AND a.attnum = k.attnum WHERE con.contype = 'f' @@ -109,18 +160,54 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { <= COALESCE((pg_catalog.row_to_json(i) OPERATOR(pg_catalog.->>) 'indnkeyatts'::pg_catalog.text)::pg_catalog.int4, i.indnatts) AND (i.indkey::pg_catalog.int2[])[0:pg_catalog.array_length(con.conkey, 1) - 1] OPERATOR(pg_catalog.@>) con.conkey ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND x.objid = c.oid + AND x.deptype = 'e' + ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_namespace'::pg_catalog.regclass + AND x.objid = c.relnamespace + AND x.deptype = 'e' + ) ORDER BY n.nspname, c.relname, con.conname, k.position """ var statisticsQueries: [String] { [Self.statisticsQuery] } - func queries(for check: TableHealthCheckKind) -> [String] { + func check(_ kind: TableHealthCheckKind) -> TableHealthCheck { + switch kind { + case .bloat: + return TableHealthCheck(kind: kind, summary: String( + localized: "Tables with at least 1,000 dead rows, and at least 20% as many dead rows as live ones." + )) + case .unusedIndex: + return TableHealthCheck(kind: kind, summary: String( + localized: "Indexes never scanned since the statistics were last reset, leaving out unique, constraint and foreign key indexes." + )) + case .missingPrimaryKey: + return TableHealthCheck(kind: kind, summary: String( + localized: "Tables with no primary key; a partition takes its key from its parent, so only the parent is listed." + )) + case .unindexedForeignKey: + return TableHealthCheck(kind: kind, summary: String( + localized: "Foreign keys whose columns lead no valid, non-partial index, so a delete on the referenced table scans this one." + )) + case .redundantIndex, .invalidIndex, .nearLimit, .transactionIdAge, .staleStatistics, .reclaimableSpace: + return TableHealthCheck(kind: kind, summary: "") + } + } + + func queries(for check: TableHealthCheckKind, statistics: TableHealthStatistics) -> [String] { switch check { case .bloat: return [Self.bloatQuery] case .unusedIndex: return [Self.unusedIndexQuery] case .missingPrimaryKey: return [Self.missingPrimaryKeyQuery] case .unindexedForeignKey: return [Self.unindexedForeignKeyQuery] - case .redundantIndex: return [] + case .redundantIndex, .invalidIndex, .nearLimit, .transactionIdAge, .staleStatistics, .reclaimableSpace: + return [] } } @@ -129,52 +216,84 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { let row = TableHealthRow.rows(of: result).first else { return TableHealthStatistics() } - if row["track_counts"] == "off" { - let reason = String(localized: "track_counts is off, so the server records no table or index activity.") - return TableHealthStatistics(skippedChecks: [.bloat: reason, .unusedIndex: reason]) + var statistics = TableHealthStatistics( + serverVersion: row.int64("server_version_num").map { Int($0) }, + isStandby: row.flag("in_recovery") == true + ) + let counterChecks = Self.counterChecks.filter(checks.contains) + func skipCounterChecks(_ reason: String) { + for kind in counterChecks { + statistics.skippedChecks[kind] = reason + } } - let reset = row["stats_reset"] - guard !reset.isEmpty else { - return TableHealthStatistics( - note: String(localized: "Index usage counts since the statistics were first collected; they have never been reset.") + + if statistics.isStandby { + statistics.notes.append(String(localized: "This server is a read-only standby. Run the fixes on the primary.")) + skipCounterChecks(String(localized: "This server is a standby. Its activity counters do not reflect the primary.")) + } else if row["track_counts"] == "off" { + skipCounterChecks(String(localized: "track_counts is off, so the server records no table or index activity.")) + } else if let reset = row.date("stats_reset") { + statistics.notes.append(String( + format: String(localized: "Index usage counts since the statistics were last reset at %@."), + TableHealthFormatting.date(reset) + )) + } else { + statistics.notes.append( + String(localized: "Index usage counts since the statistics were first collected; they have never been reset.") ) } - return TableHealthStatistics( - note: String(format: String(localized: "Index usage counts since the statistics were last reset at %@."), reset) - ) + return statistics } func findings( for check: TableHealthCheckKind, from results: [QueryResult], - context: TableHealthQueryContext - ) -> [TableHealthFinding] { - guard let result = results.first else { return [] } + context: TableHealthQueryContext, + statistics: TableHealthStatistics + ) -> (findings: [TableHealthFinding], note: String?) { + guard let result = results.first else { return ([], nil) } let rows = TableHealthRow.rows(of: result) switch check { case .bloat: - return rows.map { bloatFinding($0, context) } + return (rows.map { bloatFinding($0, context) }, nil) case .unusedIndex: - return rows.map { unusedIndexFinding($0, context) } + return (rows.map { unusedIndexFinding($0, context) }, nil) case .missingPrimaryKey: - return rows.map { missingPrimaryKeyFinding($0, context) } + return (rows.map { missingPrimaryKeyFinding($0, context) }, nil) case .unindexedForeignKey: - return unindexedForeignKeyFindings(rows, context) - case .redundantIndex: - return [] + return (unindexedForeignKeyFindings(rows, context), nil) + case .redundantIndex, .invalidIndex, .nearLimit, .transactionIdAge, .staleStatistics, .reclaimableSpace: + return ([], nil) } } private func bloatFinding(_ row: TableHealthRow, _ context: TableHealthQueryContext) -> TableHealthFinding { let schema = row["schema_name"], table = row["table_name"] - let dead = TableHealthFormatting.count(row["dead_rows"]) - let live = TableHealthFormatting.count(row["live_rows"]) - let lastVacuum = row["last_vacuum"] - let detail = lastVacuum.isEmpty - ? String(format: String(localized: "%1$@ dead rows, %2$@ live. Never vacuumed."), dead, live) - : String(format: String(localized: "%1$@ dead rows, %2$@ live. Last vacuumed %3$@."), dead, live, lastVacuum) + let deadRows = row.int64("dead_rows"), liveRows = row.int64("live_rows") + let lastVacuum = row.date("last_vacuum") + let dead = deadRows?.formatted() ?? row["dead_rows"] + let live = liveRows?.formatted() ?? row["live_rows"] + let detail = lastVacuum.map { + String(format: String(localized: "%1$@ dead rows, %2$@ live. Last vacuumed %3$@."), dead, live, TableHealthFormatting.date($0)) + } ?? String(format: String(localized: "%1$@ dead rows, %2$@ live. Never vacuumed."), dead, live) + + var metrics: [TableHealthMetric] = [] + if let deadRows { + metrics.append(TableHealthMetric(id: "dead_rows", label: String(localized: "Dead Rows"), value: .count(deadRows))) + } + if let liveRows { + metrics.append(TableHealthMetric(id: "live_rows", label: String(localized: "Live Rows"), value: .count(liveRows))) + } + if let deadRows, let liveRows, Double(deadRows) + Double(liveRows) > 0 { + let share = Double(deadRows) / (Double(deadRows) + Double(liveRows)) + metrics.append(TableHealthMetric(id: "dead_percent", label: String(localized: "Dead Row Share"), value: .percent(share))) + } + if let lastVacuum { + metrics.append(TableHealthMetric(id: "last_vacuum", label: String(localized: "Last Vacuum"), value: .date(lastVacuum))) + } return TableHealthFinding( kind: .bloat, schema: schema, table: table, objectName: nil, detail: detail, + sizeBytes: row.int64("table_bytes"), metrics: metrics, fixSQL: "VACUUM (ANALYZE) \(context.qualified(schema, table));" ) } @@ -185,6 +304,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { return TableHealthFinding( kind: .unusedIndex, schema: schema, table: row["table_name"], objectName: index, detail: String(format: String(localized: "%1$@ has never been scanned. It takes %2$@."), index, size), + sizeBytes: row.int64("index_bytes"), fixSQL: "DROP INDEX CONCURRENTLY \(context.qualified(schema, index));" ) } @@ -194,6 +314,8 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { return TableHealthFinding( kind: .missingPrimaryKey, schema: schema, table: table, objectName: nil, detail: String(localized: "No primary key. Replace column_name with the columns that identify a row."), + sizeBytes: row.int64("table_bytes"), + metrics: row.int64("estimated_rows").map { [TableHealthMetric.estimatedRows($0)] } ?? [], fixSQL: "ALTER TABLE \(context.qualified(schema, table)) ADD PRIMARY KEY (column_name);" ) } @@ -204,6 +326,9 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { let isPartitioned: Bool let constraint: String var columns: [String] + let referencedTable: String? + let estimatedRows: Int64? + let tableBytes: Int64? } private func unindexedForeignKeyFindings( @@ -216,9 +341,13 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { if let last = keys.last, last.schema == schema, last.table == table, last.constraint == constraint { keys[keys.count - 1].columns.append(row["column_name"]) } else { + let referenced = [row["referenced_schema"], row["referenced_table"]].filter { !$0.isEmpty } keys.append(ForeignKey( schema: schema, table: table, isPartitioned: row["table_kind"] == "p", - constraint: constraint, columns: [row["column_name"]] + constraint: constraint, columns: [row["column_name"]], + referencedTable: referenced.isEmpty ? nil : referenced.joined(separator: "."), + estimatedRows: row.int64("estimated_rows"), + tableBytes: row.int64("table_bytes") )) } } @@ -226,6 +355,17 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { let columnList = key.columns.map(context.quote).joined(separator: ", ") // PostgreSQL cannot build an index on a partitioned table concurrently. let create = key.isPartitioned ? "CREATE INDEX" : "CREATE INDEX CONCURRENTLY" + var metrics = [ + TableHealthMetric(id: "columns", label: String(localized: "Columns"), value: .text(key.columns.joined(separator: ", "))) + ] + if let referenced = key.referencedTable { + metrics.append(TableHealthMetric( + id: "referenced_table", label: String(localized: "Referenced Table"), value: .text(referenced) + )) + } + if let rows = key.estimatedRows { + metrics.append(.estimatedRows(rows)) + } return TableHealthFinding( kind: .unindexedForeignKey, schema: key.schema, table: key.table, objectName: key.constraint, detail: String( @@ -233,6 +373,8 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { key.constraint, key.columns.joined(separator: ", ") ), + sizeBytes: key.tableBytes, + metrics: metrics, fixSQL: "\(create) ON \(context.qualified(key.schema, key.table)) (\(columnList));" ) } diff --git a/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift b/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift new file mode 100644 index 0000000000..85c290b52c --- /dev/null +++ b/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift @@ -0,0 +1,35 @@ +import Foundation + +enum TableHealthFixScript { + static func script( + database: String, + generatedAt date: Date, + databaseType: DatabaseType, + findings: [TableHealthFinding] + ) -> String { + var script = [ + commentLine(String(format: String(localized: "Database: %1$@ (%2$@)"), database, databaseType.displayName)), + commentLine(String(format: String(localized: "Generated: %@"), date.formatted(.iso8601))), + commentLine(String(localized: "Run each statement on its own; some cannot run inside a transaction.")) + ].joined(separator: "\n") + for finding in findings { + guard let fix = finding.fixSQL, !fix.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { continue } + let name = String(format: String(localized: "%1$@: %2$@"), finding.kind.title, finding.qualifiedName) + script += "\n\n" + commentLine(name) + "\n" + fix + } + return script + "\n" + } + + // A name can hold any character. Flattened and with its comment markers broken, it cannot end the comment early or + // open a MySQL /*! ... */ comment the server would run. + static func commentLine(_ text: String) -> String { + let flat = singleLine(text) + .replacingOccurrences(of: "/*", with: "/ *") + .replacingOccurrences(of: "*/", with: "* /") + return "-- " + flat + } + + static func singleLine(_ text: String) -> String { + String(text.map { $0.isNewline || $0 == "\t" ? " " : $0 }) + } +} diff --git a/apple/TablePro/Core/TableHealth/TableHealthHistoryStoring.swift b/apple/TablePro/Core/TableHealth/TableHealthHistoryStoring.swift new file mode 100644 index 0000000000..3607644162 --- /dev/null +++ b/apple/TablePro/Core/TableHealth/TableHealthHistoryStoring.swift @@ -0,0 +1,37 @@ +import Foundation + +extension Notification.Name { + static let tableHealthHistoryDidChange = Notification.Name("com.TablePro.tableHealthHistoryDidChange") +} + +enum TableHealthHistoryChange { + static let connectionIdKey = "connectionId" + static let databaseKey = "database" + + static func userInfo(connectionId: UUID, database: String) -> [AnyHashable: Any] { + [connectionIdKey: connectionId, databaseKey: database] + } + + static func scope(of notification: Notification) -> (connectionId: UUID, database: String)? { + guard let connectionId = notification.userInfo?[connectionIdKey] as? UUID, + let database = notification.userInfo?[databaseKey] as? String + else { return nil } + return (connectionId, database) + } +} + +// Each mutation loads the ledger fresh, changes it, saves it, posts tableHealthHistoryDidChange and returns what it +// saved. A save that fails throws to the caller, which shows it. +@MainActor +protocol TableHealthHistoryStoring: AnyObject { + func ledger(connectionId: UUID, database: String) throws -> TableHealthLedger + func record(_ report: TableHealthReport, connectionId: UUID, database: String, at date: Date) throws -> TableHealthLedger + func setIgnored( + _ ids: Set, + ignored: Bool, + connectionId: UUID, + database: String, + at date: Date + ) throws -> TableHealthLedger + func clearResolved(connectionId: UUID, database: String) throws -> TableHealthLedger +} diff --git a/apple/TablePro/Core/TableHealth/TableHealthLedger.swift b/apple/TablePro/Core/TableHealth/TableHealthLedger.swift new file mode 100644 index 0000000000..bb886bf213 --- /dev/null +++ b/apple/TablePro/Core/TableHealth/TableHealthLedger.swift @@ -0,0 +1,348 @@ +import Foundation + +enum TableHealthFindingStatus: String, Sendable { + case new + case ignored + case none +} + +// What one database's findings did across recorded runs. Every rule runs on run numbers and dates are only shown, so +// a clock change cannot reorder history. +struct TableHealthLedger: Sendable, Equatable { + struct Entry: Codable, Sendable, Equatable { + var firstSeenRun: Int + var firstSeen: Date + var lastSeen: Date + var resolvedRun: Int? + var resolvedAt: Date? + } + + struct ResolvedFinding: Identifiable, Sendable, Equatable { + let id: TableHealthFindingID + let firstSeen: Date + let lastSeen: Date + let resolvedAt: Date + let resolvedRun: Int + } + + static let resolvedLimit = 1_000 + + private(set) var runIndex = 0 + private(set) var entries: [TableHealthFindingID: Entry] = [:] + private(set) var ignored: [TableHealthFindingID: Date] = [:] + // Keyed by the kind's raw value. + private var firstCompletedRun: [String: Int] = [:] + private var lastCompletedRun: [String: Int] = [:] + // What a newer build wrote under a kind this one does not know, kept as read so going back and forth loses nothing. + private var unknownEntries: [String: TableHealthLedgerJSON] = [:] + private var unknownIgnored: [String: TableHealthLedgerJSON] = [:] + + init() {} + + // Only a check that completed moves its entries: one that failed or was skipped says nothing about what is there. + func recording(_ report: TableHealthReport, at date: Date) -> TableHealthLedger { + var next = self + next.runIndex += 1 + let run = next.runIndex + for result in report.checks where result.outcome == .completed { + let present = Set(result.findings.map(\.id)) + for id in present { + if var entry = next.entries[id], entry.resolvedRun == nil { + entry.lastSeen = date + next.entries[id] = entry + } else { + next.entries[id] = Entry(firstSeenRun: run, firstSeen: date, lastSeen: date) + } + } + for (id, entry) in next.entries + where id.kind == result.kind && entry.resolvedRun == nil && !present.contains(id) { + var resolved = entry + resolved.resolvedRun = run + resolved.resolvedAt = date + next.entries[id] = resolved + } + let kind = result.kind.rawValue + if next.firstCompletedRun[kind] == nil { + next.firstCompletedRun[kind] = run + } + next.lastCompletedRun[kind] = run + } + next.trimResolved() + return next + } + + // New: first seen in the latest run, by a check that had completed in an earlier one. A check's first completion + // has nothing to compare with, so it marks nothing new. + func status(of id: TableHealthFindingID) -> TableHealthFindingStatus { + if ignored[id] != nil { return .ignored } + guard let entry = entries[id], entry.resolvedRun == nil, entry.firstSeenRun == runIndex, + let firstCompletion = firstCompletedRun[id.kind.rawValue], firstCompletion < entry.firstSeenRun + else { return .none } + return .new + } + + // For a report read without recording it: new when recording it would mark the finding new. + func mcpStatus(of id: TableHealthFindingID) -> TableHealthFindingStatus { + if ignored[id] != nil { return .ignored } + if let entry = entries[id], entry.resolvedRun == nil { return .none } + return lastCompletedRun[id.kind.rawValue] == nil ? .none : .new + } + + func firstSeen(_ id: TableHealthFindingID) -> Date? { + entries[id]?.firstSeen + } + + func lastSeen(_ id: TableHealthFindingID) -> Date? { + entries[id]?.lastSeen + } + + func isIgnored(_ id: TableHealthFindingID) -> Bool { + ignored[id] != nil + } + + var resolved: [ResolvedFinding] { + entries + .filter { $0.value.resolvedRun != nil } + .sorted(by: Self.resolvedFirst) + .map { id, entry in + ResolvedFinding( + id: id, + firstSeen: entry.firstSeen, + lastSeen: entry.lastSeen, + resolvedAt: entry.resolvedAt ?? entry.lastSeen, + resolvedRun: entry.resolvedRun ?? 0 + ) + } + } + + // An ignore outlives the finding, so an accepted index that is dropped and built again stays ignored. + func ignoring(_ ids: some Sequence, at date: Date) -> TableHealthLedger { + var next = self + for id in ids where next.ignored[id] == nil { + next.ignored[id] = date + } + return next + } + + func unignoring(_ ids: some Sequence) -> TableHealthLedger { + var next = self + for id in ids { + next.ignored[id] = nil + } + return next + } + + func clearingResolved() -> TableHealthLedger { + var next = self + next.entries = entries.filter { $0.value.resolvedRun == nil } + return next + } + + func renamingTable(schema: String?, from oldName: String, to newName: String) -> TableHealthLedger { + rewritingKeys { key in + guard key.schema == schema, key.table == oldName else { return key } + var moved = key + moved.table = newName + return moved + } + } + + func renamingSchema(from oldName: String, to newName: String) -> TableHealthLedger { + rewritingKeys { key in + guard key.schema == oldName else { return key } + var moved = key + moved.schema = newName + return moved + } + } + + func droppingTable(schema: String?, name: String) -> TableHealthLedger { + removingKeys { $0.schema == schema && $0.table == name } + } + + func droppingSchema(_ schema: String) -> TableHealthLedger { + removingKeys { $0.schema == schema } + } + + private func rewritingKeys(_ transform: (TableHealthStorageKey) -> TableHealthStorageKey) -> TableHealthLedger { + var next = self + next.entries = Self.rekeyed(entries) { TableHealthFindingID(parts: transform($0.storageParts)) ?? $0 } + next.ignored = Self.rekeyed(ignored) { TableHealthFindingID(parts: transform($0.storageParts)) ?? $0 } + next.unknownEntries = Self.rekeyed(unknownEntries) { key in + TableHealthStorageKey(encoded: key).map { transform($0).encoded } ?? key + } + next.unknownIgnored = Self.rekeyed(unknownIgnored) { key in + TableHealthStorageKey(encoded: key).map { transform($0).encoded } ?? key + } + return next + } + + private func removingKeys(_ isGone: (TableHealthStorageKey) -> Bool) -> TableHealthLedger { + var next = self + next.entries = entries.filter { !isGone($0.key.storageParts) } + next.ignored = ignored.filter { !isGone($0.key.storageParts) } + next.unknownEntries = unknownEntries.filter { TableHealthStorageKey(encoded: $0.key).map { !isGone($0) } ?? true } + next.unknownIgnored = unknownIgnored.filter { TableHealthStorageKey(encoded: $0.key).map { !isGone($0) } ?? true } + return next + } + + // A moved key wins over one already there: the object was renamed onto that name, so what is there is stale. + private static func rekeyed(_ source: [Key: Value], _ move: (Key) -> Key) -> [Key: Value] { + var result: [Key: Value] = [:] + var moved: [(Key, Value)] = [] + for (key, value) in source { + let target = move(key) + if target == key { + result[key] = value + } else { + moved.append((target, value)) + } + } + for (key, value) in moved { + result[key] = value + } + return result + } + + private mutating func trimResolved() { + let resolved = entries.filter { $0.value.resolvedRun != nil } + guard resolved.count > Self.resolvedLimit else { return } + for (id, _) in resolved.sorted(by: Self.resolvedFirst).dropFirst(Self.resolvedLimit) { + entries[id] = nil + } + } + + private static func resolvedFirst( + _ lhs: (key: TableHealthFindingID, value: Entry), + _ rhs: (key: TableHealthFindingID, value: Entry) + ) -> Bool { + let lhsRun = lhs.value.resolvedRun ?? 0, rhsRun = rhs.value.resolvedRun ?? 0 + if lhsRun != rhsRun { return lhsRun > rhsRun } + let lhsDate = lhs.value.resolvedAt ?? .distantPast, rhsDate = rhs.value.resolvedAt ?? .distantPast + if lhsDate != rhsDate { return lhsDate > rhsDate } + return lhs.key.storageKey < rhs.key.storageKey + } +} + +extension TableHealthLedger: Codable { + private enum CodingKeys: String, CodingKey { + case runIndex + case entries + case ignored + case firstCompletedRun + case lastCompletedRun + } + + private struct StorageKey: CodingKey { + let stringValue: String + var intValue: Int? { nil } + + init(_ stringValue: String) { + self.stringValue = stringValue + } + + init?(stringValue: String) { + self.stringValue = stringValue + } + + init?(intValue: Int) { + nil + } + } + + // A key this build cannot read as a finding id is kept verbatim. An entry of a known kind that does not decode is + // dropped: keeping it beside the entry a later run writes would give the file the key twice. + init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + runIndex = try container.decodeIfPresent(Int.self, forKey: .runIndex) ?? 0 + firstCompletedRun = try container.decodeIfPresent([String: Int].self, forKey: .firstCompletedRun) ?? [:] + lastCompletedRun = try container.decodeIfPresent([String: Int].self, forKey: .lastCompletedRun) ?? [:] + if container.contains(.entries) { + let stored = try container.nestedContainer(keyedBy: StorageKey.self, forKey: .entries) + for key in stored.allKeys { + if let id = TableHealthFindingID(storageKey: key.stringValue) { + if let entry = try? stored.decode(Entry.self, forKey: key) { + entries[id] = entry + } + } else { + unknownEntries[key.stringValue] = try stored.decode(TableHealthLedgerJSON.self, forKey: key) + } + } + } + if container.contains(.ignored) { + let stored = try container.nestedContainer(keyedBy: StorageKey.self, forKey: .ignored) + for key in stored.allKeys { + if let id = TableHealthFindingID(storageKey: key.stringValue) { + if let date = try? stored.decode(Date.self, forKey: key) { + ignored[id] = date + } + } else { + unknownIgnored[key.stringValue] = try stored.decode(TableHealthLedgerJSON.self, forKey: key) + } + } + } + } + + func encode(to encoder: any Encoder) throws { + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(runIndex, forKey: .runIndex) + try container.encode(firstCompletedRun, forKey: .firstCompletedRun) + try container.encode(lastCompletedRun, forKey: .lastCompletedRun) + var storedEntries = container.nestedContainer(keyedBy: StorageKey.self, forKey: .entries) + for (key, value) in unknownEntries { + try storedEntries.encode(value, forKey: StorageKey(key)) + } + for (id, entry) in entries { + try storedEntries.encode(entry, forKey: StorageKey(id.storageKey)) + } + var storedIgnored = container.nestedContainer(keyedBy: StorageKey.self, forKey: .ignored) + for (key, value) in unknownIgnored { + try storedIgnored.encode(value, forKey: StorageKey(key)) + } + for (id, date) in ignored { + try storedIgnored.encode(date, forKey: StorageKey(id.storageKey)) + } + } +} + +private indirect enum TableHealthLedgerJSON: Codable, Sendable, Equatable { + case null + case bool(Bool) + case integer(Int64) + case number(Double) + case string(String) + case array([TableHealthLedgerJSON]) + case object([String: TableHealthLedgerJSON]) + + init(from decoder: any Decoder) throws { + let container = try decoder.singleValueContainer() + if container.decodeNil() { + self = .null + } else if let value = try? container.decode(Bool.self) { + self = .bool(value) + } else if let value = try? container.decode(Int64.self) { + self = .integer(value) + } else if let value = try? container.decode(Double.self) { + self = .number(value) + } else if let value = try? container.decode(String.self) { + self = .string(value) + } else if let value = try? container.decode([TableHealthLedgerJSON].self) { + self = .array(value) + } else { + self = .object(try container.decode([String: TableHealthLedgerJSON].self)) + } + } + + func encode(to encoder: any Encoder) throws { + var container = encoder.singleValueContainer() + switch self { + case .null: try container.encodeNil() + case .bool(let value): try container.encode(value) + case .integer(let value): try container.encode(value) + case .number(let value): try container.encode(value) + case .string(let value): try container.encode(value) + case .array(let value): try container.encode(value) + case .object(let value): try container.encode(value) + } + } +} diff --git a/apple/TablePro/Core/TableHealth/TableHealthProvider.swift b/apple/TablePro/Core/TableHealth/TableHealthProvider.swift index 49342b60e2..393c63add1 100644 --- a/apple/TablePro/Core/TableHealth/TableHealthProvider.swift +++ b/apple/TablePro/Core/TableHealth/TableHealthProvider.swift @@ -4,6 +4,8 @@ import TableProPluginKit struct TableHealthQueryContext { let database: String let quote: (String) -> String + // A whole string literal, quotes included. + let literal: (String) -> String func qualified(_ parts: String...) -> String { parts.map(quote).joined(separator: ".") @@ -14,12 +16,14 @@ protocol TableHealthProvider: Sendable { var checks: [TableHealthCheckKind] { get } var statisticsQueries: [String] { get } func statistics(from results: [QueryResult?], now: Date) -> TableHealthStatistics - func queries(for check: TableHealthCheckKind) -> [String] + func check(_ kind: TableHealthCheckKind) -> TableHealthCheck + func queries(for check: TableHealthCheckKind, statistics: TableHealthStatistics) -> [String] func findings( for check: TableHealthCheckKind, from results: [QueryResult], - context: TableHealthQueryContext - ) -> [TableHealthFinding] + context: TableHealthQueryContext, + statistics: TableHealthStatistics + ) -> (findings: [TableHealthFinding], note: String?) } extension TableHealthProvider { @@ -58,27 +62,32 @@ extension TableHealthProvider { let statistics = statistics(from: statisticsResults, now: now) var results: [TableHealthCheckResult] = [] - for check in checks { - if let reason = statistics.skippedChecks[check] { - results.append(TableHealthCheckResult(kind: check, findings: [], outcome: .skipped(reason))) + for kind in checks { + let descriptor = check(kind) + if let reason = statistics.skippedChecks[kind] { + results.append(TableHealthCheckResult(check: descriptor, findings: [], outcome: .skipped(reason))) continue } do { var inputs: [QueryResult] = [] - for query in queries(for: check) { + for query in queries(for: kind, statistics: statistics) { inputs.append(try await run(query)) } - let found = findings(for: check, from: inputs, context: context) - results.append(TableHealthCheckResult(kind: check, findings: found, outcome: .completed)) + let output = findings(for: kind, from: inputs, context: context, statistics: statistics) + results.append(TableHealthCheckResult( + check: descriptor, findings: output.findings, outcome: .completed, note: output.note + )) } catch let error as CancellationError { throw error } catch { results.append(TableHealthCheckResult( - kind: check, findings: [], outcome: .failed(error.localizedDescription) + check: descriptor, findings: [], outcome: .failed(error.localizedDescription) )) } } - return TableHealthReport(database: context.database, checks: results, statisticsNote: statistics.note) + return TableHealthReport( + database: context.database, checks: results, notes: statistics.notes, generatedAt: now + ) } } @@ -109,14 +118,48 @@ struct TableHealthRow { } subscript(_ name: String) -> String { - guard let index = columns[name], index < values.count else { return "" } - return values[index].asText ?? "" + text(name) ?? "" + } + + // Nil for NULL and for a column the result does not have. + func text(_ name: String) -> String? { + guard let index = columns[name], index < values.count else { return nil } + return values[index].asText + } + + // A server can print an integer column as a decimal ("1.5e+06", "42.0"), so a whole Double is taken too. + func int64(_ name: String) -> Int64? { + guard let text = self.text(name)?.trimmingCharacters(in: .whitespaces), !text.isEmpty else { return nil } + if let value = Int64(text) { return value } + guard let value = Double(text), value.isFinite, value >= Double(Int64.min), value < Double(Int64.max) + else { return nil } + return Int64(value.rounded()) } -} -enum TableHealthFormatting { - static func count(_ text: String) -> String { - guard let value = Int64(text) else { return text } - return value.formatted() + func uint64(_ name: String) -> UInt64? { + guard let text = self.text(name)?.trimmingCharacters(in: .whitespaces), !text.isEmpty else { return nil } + if let value = UInt64(text) { return value } + guard let value = Double(text), value.isFinite, value >= 0, value < Double(UInt64.max) else { return nil } + return UInt64(value.rounded()) + } + + func double(_ name: String) -> Double? { + guard let text = self.text(name)?.trimmingCharacters(in: .whitespaces), let value = Double(text), value.isFinite + else { return nil } + return value + } + + // Queries return dates as Unix epoch seconds, so no server time zone or text format reaches the app. + func date(_ name: String) -> Date? { + double(name).map { Date(timeIntervalSince1970: $0) } + } + + func flag(_ name: String) -> Bool? { + guard let text = self.text(name) else { return nil } + switch text.trimmingCharacters(in: .whitespaces).lowercased() { + case "t", "true", "1", "yes", "on": return true + case "f", "false", "0", "no", "off": return false + default: return nil + } } } diff --git a/apple/TablePro/Core/TableHealth/TableHealthReportLoader.swift b/apple/TablePro/Core/TableHealth/TableHealthReportLoader.swift new file mode 100644 index 0000000000..dec57f2ae5 --- /dev/null +++ b/apple/TablePro/Core/TableHealth/TableHealthReportLoader.swift @@ -0,0 +1,32 @@ +import Foundation + +enum TableHealthError: LocalizedError, Equatable { + case noDatabase + + var errorDescription: String? { + String(localized: "Select a database in the sidebar to check its tables.") + } +} + +// The one read the Table Health tab and the MCP tool share. The caller fixes the scope, so a report never +// follows the sidebar to another database. +enum TableHealthReportLoader { + static func load( + provider: any TableHealthProvider, + scope: DatabaseScope, + metadata: any ScopedMetadataProviding, + now: Date = Date() + ) async throws -> TableHealthReport { + guard !scope.isServerScoped else { throw TableHealthError.noDatabase } + return try await metadata.withMetadataDriver(scope: scope, workload: .bulk) { driver in + let context = TableHealthQueryContext( + database: scope.database, + quote: { driver.quoteIdentifier($0) }, + literal: { "'" + driver.escapeStringLiteral($0) + "'" } + ) + return try await provider.fetchReport(context: context, now: now) { query in + try await driver.executeCatalogRead(query: query) + } + } + } +} diff --git a/apple/TablePro/Models/Query/QueryTabManager.swift b/apple/TablePro/Models/Query/QueryTabManager.swift index 7f1d233ecf..42d045985b 100644 --- a/apple/TablePro/Models/Query/QueryTabManager.swift +++ b/apple/TablePro/Models/Query/QueryTabManager.swift @@ -503,7 +503,7 @@ final class QueryTabManager: ObservableObject { selectedTabId = newTab.id } - // Server Dashboard, Users & Roles, Query Insights and Table Health: one per connection. + // Server Dashboard, Users & Roles and Query Insights: one per connection. func addSingletonTab(_ tabType: TabType) { if let existing = tabs.first(where: { $0.tabType == tabType }) { selectedTabId = existing.id @@ -569,6 +569,25 @@ final class QueryTabManager: ObservableObject { return newTab.id } + // One per database. No database opens nothing: the report never falls back to the browse cursor, which moves. + @discardableResult + func addTableHealthTab(databaseName: String) -> UUID? { + guard !databaseName.isEmpty else { return nil } + if let existing = tabs.first(where: { + $0.tabType == .tableHealth && $0.tableContext.databaseName == databaseName + }) { + selectedTabId = existing.id + return existing.id + } + var newTab = QueryTab(title: WindowTitleResolver.title(for: .tableHealth), tabType: .tableHealth) + newTab.tableContext.isEditable = false + newTab.tableContext.databaseName = databaseName + newTab.hasUserInteraction = true + tabs.append(newTab) + selectedTabId = newTab.id + return newTab.id + } + // One tab per run: debugging the same routine again starts a second session rather than taking over a live one. @discardableResult func addRoutineDebugTab(id: UUID = UUID(), title: String, databaseName: String, schemaName: String?) -> UUID { @@ -814,10 +833,10 @@ extension QueryTabManager { // Server-wide tabs answer for the whole connection, so they belong to every entry. private static func bindsToContainer(_ type: TabType) -> Bool { switch type { - case .query, .table, .createTable, .erDiagram, .objectSource, .tableListing: + case .query, .table, .createTable, .erDiagram, .objectSource, .tableListing, .tableHealth: return true // A paused debug target holds locks, so its tab stays in sight whichever entry the sidebar shows. - case .serverDashboard, .usersRoles, .insights, .tableHealth, .versionHistory, .routineDebug, .liveMessages: + case .serverDashboard, .usersRoles, .insights, .versionHistory, .routineDebug, .liveMessages: return false } } diff --git a/apple/TablePro/Models/TableHealth/TableHealthModels.swift b/apple/TablePro/Models/TableHealth/TableHealthModels.swift index cbf616086e..a839fa361f 100644 --- a/apple/TablePro/Models/TableHealth/TableHealthModels.swift +++ b/apple/TablePro/Models/TableHealth/TableHealthModels.swift @@ -1,11 +1,17 @@ import Foundation -enum TableHealthCheckKind: String, Sendable { +// The raw values are stored in the history ledger: renaming one orphans every finding saved under it. +enum TableHealthCheckKind: String, CaseIterable, Codable, Sendable { case bloat case unusedIndex case redundantIndex case missingPrimaryKey case unindexedForeignKey + case invalidIndex + case nearLimit + case transactionIdAge + case staleStatistics + case reclaimableSpace var title: String { switch self { @@ -14,9 +20,15 @@ enum TableHealthCheckKind: String, Sendable { case .redundantIndex: return String(localized: "Redundant Indexes") case .missingPrimaryKey: return String(localized: "Tables Without a Primary Key") case .unindexedForeignKey: return String(localized: "Foreign Keys Without an Index") + case .invalidIndex: return String(localized: "Invalid Indexes") + case .nearLimit: return String(localized: "Counters Near Their Limit") + case .transactionIdAge: return String(localized: "Transaction ID Wraparound") + case .staleStatistics: return String(localized: "Stale Statistics") + case .reclaimableSpace: return String(localized: "Reclaimable Space") } } + // Symbols from SF Symbols 2 or earlier, so each draws on macOS 13. var systemImage: String { switch self { case .bloat: return "trash" @@ -24,27 +36,212 @@ enum TableHealthCheckKind: String, Sendable { case .redundantIndex: return "square.on.square" case .missingPrimaryKey: return "key" case .unindexedForeignKey: return "link" + case .invalidIndex: return "xmark.square" + case .nearLimit: return "arrow.up.to.line" + case .transactionIdAge: return "hourglass" + case .staleStatistics: return "chart.bar.xaxis" + case .reclaimableSpace: return "internaldrive" } } } -struct TableHealthFinding: Identifiable, Hashable, Sendable { +struct TableHealthCheck: Hashable, Sendable { let kind: TableHealthCheckKind + let title: String + let systemImage: String + let summary: String + + init(kind: TableHealthCheckKind, title: String? = nil, systemImage: String? = nil, summary: String) { + self.kind = kind + self.title = title ?? kind.title + self.systemImage = systemImage ?? kind.systemImage + self.summary = summary + } +} + +struct TableHealthFindingID: Hashable, Sendable { + let kind: TableHealthCheckKind + let schema: String? + let table: String? + let object: String? + + init(kind: TableHealthCheckKind, schema: String?, table: String?, object: String?) { + self.kind = kind + self.schema = schema + self.table = table + self.object = object + } + + init?(storageKey: String) { + guard let parts = TableHealthStorageKey(encoded: storageKey) else { return nil } + self.init(parts: parts) + } + + init?(parts: TableHealthStorageKey) { + guard let kind = TableHealthCheckKind(rawValue: parts.kindName) else { return nil } + self.init(kind: kind, schema: parts.schema, table: parts.table, object: parts.object) + } + + var storageParts: TableHealthStorageKey { + TableHealthStorageKey(kindName: kind.rawValue, schema: schema, table: table, object: object) + } + + var storageKey: String { storageParts.encoded } +} + +// A finding id as stored, its kind kept as text so a kind this build does not know still parses. Only ASCII letters, +// digits and `_` stay literal: what CharacterSet.alphanumerics holds grows with each Unicode version, and a key that +// encoded differently after an OS update would read as a new finding. +struct TableHealthStorageKey: Hashable, Sendable { + var kindName: String + var schema: String? + var table: String? + var object: String? + + init(kindName: String, schema: String?, table: String?, object: String?) { + self.kindName = kindName + self.schema = schema + self.table = table + self.object = object + } + + init?(encoded: String) { + let parts = encoded.split(separator: ".", omittingEmptySubsequences: false).map(String.init) + guard parts.count == 4, + parts[0] != "-", + let kindName = parts[0].removingPercentEncoding, + let schema = Self.decode(parts[1]), + let table = Self.decode(parts[2]), + let object = Self.decode(parts[3]) + else { return nil } + self.init(kindName: kindName, schema: schema, table: table, object: object) + } + + var encoded: String { + [kindName, schema, table, object].map(Self.encode).joined(separator: ".") + } + + private static func encode(_ part: String?) -> String { + guard let part else { return "-" } + var encoded = "" + for byte in part.utf8 { + switch byte { + case UInt8(ascii: "a")...UInt8(ascii: "z"), UInt8(ascii: "A")...UInt8(ascii: "Z"), + UInt8(ascii: "0")...UInt8(ascii: "9"), UInt8(ascii: "_"): + encoded.unicodeScalars.append(Unicode.Scalar(byte)) + default: + encoded += String(format: "%%%02X", byte) + } + } + return encoded + } + + // Nil for text that is not a part, .some(nil) for an absent one. + private static func decode(_ part: String) -> String?? { + if part == "-" { return .some(nil) } + guard let decoded = part.removingPercentEncoding else { return nil } + return .some(decoded) + } +} + +struct TableHealthMetric: Hashable, Sendable { + enum Value: Hashable, Sendable { + case count(Int64) + // AUTO_INCREMENT on a bigint unsigned column passes Int64.max. + case bigCount(UInt64) + case bytes(Int64) + // A fraction: 0.25 reads as 25%. + case percent(Double) + case date(Date) + case text(String) + case flag(Bool) + } + + // Stable snake_case: the export column id. + let id: String + let label: String + let value: Value + + var formattedValue: String { value.formatted } + + static func estimatedRows(_ rows: Int64) -> TableHealthMetric { + TableHealthMetric(id: "estimated_rows", label: String(localized: "Estimated Rows"), value: .count(rows)) + } +} + +extension TableHealthMetric.Value { + var formatted: String { + switch self { + case .count(let value): return value.formatted() + case .bigCount(let value): return value.formatted() + case .bytes(let value): return ByteSizeFormatting.string(bytes: value) + case .percent(let value): + guard value.isFinite else { return "" } + return value.formatted(.percent.precision(.fractionLength(0...1))) + case .date(let value): return TableHealthFormatting.date(value) + case .text(let value): return value + case .flag(let value): return value ? String(localized: "Yes") : String(localized: "No") + } + } +} + +// Equal and hashed by id alone, so a finding whose numbers moved between runs is still the same finding. +struct TableHealthFinding: Identifiable, Hashable, Sendable { + let id: TableHealthFindingID // Nil where the engine has no schema level below the database. let schema: String? - let table: String + // Nil only for a sequence no column uses. + let table: String? let objectName: String? let detail: String - let fixSQL: String + // On-disk size of what the fix acts on: the index for an index check, the table otherwise. + let sizeBytes: Int64? + let metrics: [TableHealthMetric] + let fixSQL: String? - var id: String { - [kind.rawValue, schema ?? "", table, objectName ?? ""].joined(separator: "\u{1F}") + init( + kind: TableHealthCheckKind, + schema: String?, + table: String?, + objectName: String?, + detail: String, + sizeBytes: Int64? = nil, + metrics: [TableHealthMetric] = [], + fixSQL: String? + ) { + self.id = TableHealthFindingID(kind: kind, schema: schema, table: table, object: objectName) + self.schema = schema + self.table = table + self.objectName = objectName + self.detail = detail + self.sizeBytes = sizeBytes + self.metrics = metrics + self.fixSQL = fixSQL } - var qualifiedTable: String { + var kind: TableHealthCheckKind { id.kind } + + var qualifiedTable: String? { + guard let table else { return nil } guard let schema else { return table } return "\(schema).\(table)" } + + var qualifiedName: String { + [schema, table, objectName].compactMap { $0 }.joined(separator: ".") + } + + func metric(_ id: String) -> TableHealthMetric? { + metrics.first { $0.id == id } + } + + static func == (lhs: TableHealthFinding, rhs: TableHealthFinding) -> Bool { + lhs.id == rhs.id + } + + func hash(into hasher: inout Hasher) { + hasher.combine(id) + } } struct TableHealthCheckResult: Identifiable, Sendable { @@ -55,33 +252,50 @@ struct TableHealthCheckResult: Identifiable, Sendable { case skipped(String) } - let kind: TableHealthCheckKind + let check: TableHealthCheck var findings: [TableHealthFinding] var outcome: Outcome + var note: String? + + var kind: TableHealthCheckKind { check.kind } - var id: String { kind.rawValue } + var id: TableHealthCheckKind { check.kind } } struct TableHealthReport: Sendable { let database: String var checks: [TableHealthCheckResult] - var statisticsNote: String? + var notes: [String] = [] + let generatedAt: Date var findingCount: Int { checks.reduce(0) { $0 + $1.findings.count } } + var findings: [TableHealthFinding] { + checks.flatMap(\.findings) + } + + func result(for kind: TableHealthCheckKind) -> TableHealthCheckResult? { + checks.first { $0.kind == kind } + } + func finding(id: TableHealthFinding.ID) -> TableHealthFinding? { - for check in checks { - if let finding = check.findings.first(where: { $0.id == id }) { - return finding - } - } - return nil + result(for: id.kind)?.findings.first { $0.id == id } } } struct TableHealthStatistics: Equatable, Sendable { - var note: String? + // PostgreSQL's server_version_num; for MySQL and MariaDB, major * 10000 + minor * 100 + patch. + var serverVersion: Int? + var isMariaDB = false + var isStandby = false + var notes: [String] = [] var skippedChecks: [TableHealthCheckKind: String] = [:] } + +enum TableHealthFormatting { + static func date(_ date: Date) -> String { + date.formatted(date: .abbreviated, time: .shortened) + } +} diff --git a/apple/TablePro/ViewModels/TableHealthViewModel.swift b/apple/TablePro/ViewModels/TableHealthViewModel.swift index 8d536ec013..c89b85bedf 100644 --- a/apple/TablePro/ViewModels/TableHealthViewModel.swift +++ b/apple/TablePro/ViewModels/TableHealthViewModel.swift @@ -2,14 +2,6 @@ import Combine import Foundation import os -enum TableHealthError: LocalizedError { - case noDatabase - - var errorDescription: String? { - String(localized: "Select a database in the sidebar to check its tables.") - } -} - @MainActor final class TableHealthViewModel: ObservableObject { nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "TableHealth") @@ -70,13 +62,7 @@ final class TableHealthViewModel: ObservableObject { do { guard let scope = metadata.browseScope(for: connectionId) else { throw DatabaseError.notConnected } - guard !scope.isServerScoped else { throw TableHealthError.noDatabase } - let fresh = try await metadata.withMetadataDriver(scope: scope, workload: .bulk) { driver in - let context = TableHealthQueryContext(database: scope.database) { driver.quoteIdentifier($0) } - return try await provider.fetchReport(context: context) { query in - try await driver.executeCatalogRead(query: query) - } - } + let fresh = try await TableHealthReportLoader.load(provider: provider, scope: scope, metadata: metadata) report = Self.merging(fresh, over: report) loadError = nil lastRefreshDate = Date() diff --git a/apple/TablePro/Views/AIChat/AIChatWalkthroughBlockView.swift b/apple/TablePro/Views/AIChat/AIChatWalkthroughBlockView.swift index 8b7c1e00ab..67ca590852 100644 --- a/apple/TablePro/Views/AIChat/AIChatWalkthroughBlockView.swift +++ b/apple/TablePro/Views/AIChat/AIChatWalkthroughBlockView.swift @@ -341,13 +341,18 @@ struct AIChatWalkthroughBlockView: View { .clipShape(RoundedRectangle(cornerRadius: 4)) } + private static var askSymbol: String { + if #available(macOS 15.0, *) { return "bubble.and.pencil" } + return "text.bubble" + } + @ViewBuilder private func followUpControls(for step: SqlWalkthroughStep, index: Int) -> some View { Button { followUpText = "" activeFollowUpStepID = step.id } label: { - Label(String(localized: "Ask about this change"), systemImage: "bubble.and.pencil") + Label(String(localized: "Ask about this change"), systemImage: Self.askSymbol) .font(.caption) } .buttonStyle(.link) diff --git a/apple/TablePro/Views/Connection/ConnectionUnavailableView.swift b/apple/TablePro/Views/Connection/ConnectionUnavailableView.swift index 4f338c20dc..df81223b30 100644 --- a/apple/TablePro/Views/Connection/ConnectionUnavailableView.swift +++ b/apple/TablePro/Views/Connection/ConnectionUnavailableView.swift @@ -81,7 +81,8 @@ internal struct ConnectionUnavailableView: View { case .disconnected: return "bolt.slash" case .disconnectedByUser: - return "cable.connector.slash" + if #available(macOS 14.0, *) { return "cable.connector.slash" } + return "bolt.horizontal.circle" case .failed: return "exclamationmark.triangle" case .actionRequired(_, let action): diff --git a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift index 10f531ed79..46c9da5eb0 100644 --- a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift +++ b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift @@ -355,11 +355,13 @@ struct MainEditorContentView: View { private var tableHealthActions: TableHealthActions { TableHealthActions( openTable: { finding, database in - coordinator.openTableTab(finding.table, schema: finding.schema, database: database) + guard let table = finding.table else { return } + coordinator.openTableTab(table, schema: finding.schema, database: database) }, openFix: { finding, database in + guard let fix = finding.fixSQL else { return } coordinator.openQuery( - finding.fixSQL, + fix, on: connection.id, databaseName: database, intent: .loadIntoFrontTab diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealth.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealth.swift new file mode 100644 index 0000000000..b0fb67314c --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealth.swift @@ -0,0 +1,29 @@ +import Foundation + +extension MainContentCoordinator { + // The entry on screen, not the browse cursor, which lags a switch to it. + func showTableHealth() { + let databaseName = newTabDatabaseName + guard !databaseName.isEmpty else { return } + let checksDatabase: (QueryTab) -> Bool = { + $0.tabType == .tableHealth && $0.tableContext.databaseName == databaseName + } + + if let existing = Self.coordinator(forConnection: connectionId, tabMatching: checksDatabase), + let match = existing.tabManager.tabs.first(where: checksDatabase) { + existing.selectTabAndFocusWindow(match.id) + return + } + + if tabManager.tabs.isEmpty { + tabManager.addTableHealthTab(databaseName: databaseName) + return + } + + WindowManager.shared.openTab(payload: EditorTabPayload( + connectionId: connection.id, + tabType: .tableHealth, + databaseName: databaseName + )) + } +} diff --git a/apple/TablePro/Views/Main/MainContentCommandActions+DatabaseTools.swift b/apple/TablePro/Views/Main/MainContentCommandActions+DatabaseTools.swift index 5500dd2acf..c7f4fd3344 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions+DatabaseTools.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions+DatabaseTools.swift @@ -32,7 +32,13 @@ extension MainContentCommandActions { } func showTableHealth() { - coordinator?.showSingletonTab(.tableHealth) + coordinator?.showTableHealth() + } + + // The entry a new tab opens on names a database; Table Health checks one and never falls back to the browse cursor. + var hasDatabase: Bool { + guard let coordinator else { return false } + return !coordinator.newTabDatabaseName.isEmpty } var supportsServerDashboard: Bool { diff --git a/apple/TablePro/Views/QueryInsights/QueryInsightsSummaryBar.swift b/apple/TablePro/Views/QueryInsights/QueryInsightsSummaryBar.swift index 5054644156..845a7a28c0 100644 --- a/apple/TablePro/Views/QueryInsights/QueryInsightsSummaryBar.swift +++ b/apple/TablePro/Views/QueryInsights/QueryInsightsSummaryBar.swift @@ -40,7 +40,7 @@ struct QueryInsightsSummaryBar: View { format: String(localized: "%@ slowest", comment: "Slowest single run, %@ is a duration"), QueryDurationFormatter.string(from: totals.maxDuration) ), - systemImage: "gauge.with.needle", + systemImage: Self.averageSymbol, tint: .secondary ) metric( @@ -55,6 +55,11 @@ struct QueryInsightsSummaryBar: View { .padding(.vertical, 12) } + private static var averageSymbol: String { + if #available(macOS 14.0, *) { return "gauge.with.needle" } + return "gauge" + } + private func metric( label: String, value: String, diff --git a/apple/TablePro/Views/QueryInsights/QueryInsightsView.swift b/apple/TablePro/Views/QueryInsights/QueryInsightsView.swift index 5ccaa0ea9a..0966d178c6 100644 --- a/apple/TablePro/Views/QueryInsights/QueryInsightsView.swift +++ b/apple/TablePro/Views/QueryInsights/QueryInsightsView.swift @@ -88,7 +88,7 @@ struct QueryInsightsView: View { QueryInsightsSection( title: String(localized: "Most Run"), - systemImage: "arrow.trianglehead.2.clockwise", + systemImage: "arrow.triangle.2.circlepath", isEmpty: viewModel.snapshot.mostRun.isEmpty, emptyMessage: String(localized: "No queries in this range.") ) { diff --git a/apple/TablePro/Views/Results/GeometryMapWindowController.swift b/apple/TablePro/Views/Results/GeometryMapWindowController.swift index d61ba7c672..cc3cfe6be4 100644 --- a/apple/TablePro/Views/Results/GeometryMapWindowController.swift +++ b/apple/TablePro/Views/Results/GeometryMapWindowController.swift @@ -64,7 +64,7 @@ private struct GeometryMapWindowContent: View { Button { fitToken &+= 1 } label: { - Label(String(localized: "Fit to Geometry"), systemImage: "arrow.down.left.and.arrow.up.right") + Label(String(localized: "Fit to Geometry"), systemImage: "arrow.up.left.and.arrow.down.right") } .buttonStyle(.borderless) .controlSize(.small) diff --git a/apple/TablePro/Views/Results/ResultMapToolbar.swift b/apple/TablePro/Views/Results/ResultMapToolbar.swift index 190ebae994..aa0800aabf 100644 --- a/apple/TablePro/Views/Results/ResultMapToolbar.swift +++ b/apple/TablePro/Views/Results/ResultMapToolbar.swift @@ -38,7 +38,7 @@ struct ResultMapToolbar: View { Button { onFit() } label: { - Label(String(localized: "Fit to Result"), systemImage: "arrow.down.left.and.arrow.up.right") + Label(String(localized: "Fit to Result"), systemImage: "arrow.up.left.and.arrow.down.right") } .buttonStyle(.borderless) .controlSize(.small) diff --git a/apple/TablePro/Views/ServerDashboard/MetricsBarView.swift b/apple/TablePro/Views/ServerDashboard/MetricsBarView.swift index ff877ded32..24554d80fe 100644 --- a/apple/TablePro/Views/ServerDashboard/MetricsBarView.swift +++ b/apple/TablePro/Views/ServerDashboard/MetricsBarView.swift @@ -7,7 +7,7 @@ struct MetricsBarView: View { var body: some View { VStack(alignment: .leading, spacing: 0) { HStack { - Label(String(localized: "Server Metrics"), systemImage: "gauge.with.dots.needle.33percent") + Label(String(localized: "Server Metrics"), systemImage: "speedometer") .font(.headline) Spacer() if let error { diff --git a/apple/TablePro/Views/ServerDashboard/ServerDashboardView.swift b/apple/TablePro/Views/ServerDashboard/ServerDashboardView.swift index d49b219dbc..81c418131c 100644 --- a/apple/TablePro/Views/ServerDashboard/ServerDashboardView.swift +++ b/apple/TablePro/Views/ServerDashboard/ServerDashboardView.swift @@ -11,7 +11,7 @@ struct ServerDashboardView: View { if viewModel.supportedPanels.isEmpty { UnavailableStateView( String(localized: "Dashboard Not Available"), - systemImage: "gauge.with.dots.needle.0percent", + systemImage: "gauge.low", description: Text("Server monitoring is not available for this database type.") ) } else { diff --git a/apple/TablePro/Views/TableHealth/TableHealthView.swift b/apple/TablePro/Views/TableHealth/TableHealthView.swift index da5705215e..2a860e04a5 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthView.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthView.swift @@ -6,7 +6,8 @@ struct TableHealthActions { let openFix: (_ finding: TableHealthFinding, _ database: String) -> Void func copyFix(_ finding: TableHealthFinding) { - ClipboardService.shared.writeText(finding.fixSQL) + guard let fix = finding.fixSQL else { return } + ClipboardService.shared.writeText(fix) } } @@ -106,11 +107,13 @@ private struct TableHealthFindingsList: View { var body: some View { if let report = viewModel.report { List(selection: $viewModel.selectedFindingId) { - if let note = report.statisticsNote { + if !report.notes.isEmpty { Section { - Label(note, systemImage: "info.circle") - .font(.callout) - .foregroundStyle(.secondary) + ForEach(report.notes, id: \.self) { note in + Label(note, systemImage: "info.circle") + .font(.callout) + .foregroundStyle(.secondary) + } } } ForEach(report.checks) { check in @@ -158,7 +161,7 @@ private struct TableHealthFindingsList: View { } ForEach(check.findings) { finding in VStack(alignment: .leading, spacing: 2) { - Text(verbatim: finding.qualifiedTable) + Text(verbatim: finding.qualifiedTable ?? finding.qualifiedName) Text(verbatim: finding.detail) .font(.callout) .foregroundStyle(.secondary) @@ -194,7 +197,7 @@ private struct TableHealthDetailPane: View { if let finding = viewModel.selectedFinding, let database = viewModel.report?.database { VStack(alignment: .leading, spacing: 8) { HStack(spacing: 8) { - Text(verbatim: finding.qualifiedTable) + Text(verbatim: finding.qualifiedTable ?? finding.qualifiedName) .font(.headline) .lineLimit(1) .truncationMode(.middle) @@ -204,7 +207,7 @@ private struct TableHealthDetailPane: View { Button(String(localized: "Open in Editor")) { actions.openFix(finding, database) } } SQLStatementPreview( - prepared: SQLReviewSheet.build(statements: [finding.fixSQL], databaseType: databaseType), + prepared: SQLReviewSheet.build(statements: finding.fixSQL.map { [$0] } ?? [], databaseType: databaseType), databaseType: databaseType ) .id(finding.id) diff --git a/apple/TableProTests/Core/Menu/MainMenuBuilderTests.swift b/apple/TableProTests/Core/Menu/MainMenuBuilderTests.swift index ea2b6f671f..293a87cf7b 100644 --- a/apple/TableProTests/Core/Menu/MainMenuBuilderTests.swift +++ b/apple/TableProTests/Core/Menu/MainMenuBuilderTests.swift @@ -757,12 +757,24 @@ struct MainMenuValidationTests { context.supportsUserManagement = true context.supportsContainerSwitching = true context.supportsTableHealth = true + context.hasDatabase = true #expect(enabled(#selector(MainSplitViewController.showServerDashboard(_:)), context)) #expect(enabled(#selector(MainSplitViewController.showUsersAndRoles(_:)), context)) #expect(enabled(#selector(MainSplitViewController.openContainerSwitcher(_:)), context)) #expect(enabled(#selector(MainSplitViewController.showTableHealth(_:)), context)) } + // A tab opened without one would check whatever database the sidebar browsed next. + @Test("Table Health stays dim while the entry on screen names no database") + func tableHealthNeedsADatabase() { + var context = capableContext() + context.isConnected = true + context.hasDatabase = false + #expect(!enabled(#selector(MainSplitViewController.showTableHealth(_:)), context)) + context.hasDatabase = true + #expect(enabled(#selector(MainSplitViewController.showTableHealth(_:)), context)) + } + // Each opens a tab in browse content, which Agent mode does not mount. @Test("Agent mode dims the commands that open a connection's tool tabs") func toolTabsDimInAgentMode() { @@ -810,6 +822,7 @@ struct MainMenuValidationTests { context.canInsertRows = true context.canDeleteRows = true context.supportsTableHealth = true + context.hasDatabase = true context.isCurrentTabSchemaResolved = true context.hasTableSelection = true context.hasRowSelection = true diff --git a/apple/TableProTests/Core/Services/Infrastructure/EditorTabOpenerTests.swift b/apple/TableProTests/Core/Services/Infrastructure/EditorTabOpenerTests.swift index d0efb9dee2..548345542d 100644 --- a/apple/TableProTests/Core/Services/Infrastructure/EditorTabOpenerTests.swift +++ b/apple/TableProTests/Core/Services/Infrastructure/EditorTabOpenerTests.swift @@ -262,6 +262,43 @@ struct EditorTabOpenerTests { #expect(manager.tabs.isEmpty) } + @Test("A Table Health payload opens on the database it names, else the entry on screen, once each") + func tableHealthPayloadBindsItsDatabase() { + let connection = TestFixtures.makeConnection(database: "tenant_a", type: .postgresql) + let manager = QueryTabManager(containerTarget: .database, visibleContainer: "tenant_b") + let openTableHealth = { (database: String?) in + EditorTabOpener.apply( + EditorTabPayload(connectionId: connection.id, tabType: .tableHealth, databaseName: database), + to: manager, + connection: connection, + toolbarState: nil + ) + } + + openTableHealth(nil) + openTableHealth("analytics") + openTableHealth("tenant_b") + + let databases = manager.tabs.map { $0.tableContext.databaseName } + #expect(databases == ["tenant_b", "analytics"]) + #expect(manager.selectedTab?.tableContext.databaseName == "tenant_b") + } + + @Test("A Table Health payload with no database to check opens nothing") + func tableHealthPayloadWithoutDatabaseOpensNothing() { + let connection = TestFixtures.makeConnection(database: "", type: .mysql) + let manager = QueryTabManager(containerTarget: .database) + + EditorTabOpener.apply( + EditorTabPayload(connectionId: connection.id, tabType: .tableHealth), + to: manager, + connection: connection, + toolbarState: nil + ) + + #expect(manager.tabs.isEmpty) + } + @Test("A query payload carrying a query opens a tab holding it") func queryPayloadOpensTab() { let connection = makeConnection() diff --git a/apple/TableProTests/Core/TableHealth/MySQLTableHealthProviderTests.swift b/apple/TableProTests/Core/TableHealth/MySQLTableHealthProviderTests.swift index 4841190650..404e89705a 100644 --- a/apple/TableProTests/Core/TableHealth/MySQLTableHealthProviderTests.swift +++ b/apple/TableProTests/Core/TableHealth/MySQLTableHealthProviderTests.swift @@ -42,9 +42,13 @@ struct MySQLTableHealthProviderTests { ) } + private func findings(_ check: TableHealthCheckKind, from results: [QueryResult]) -> [TableHealthFinding] { + provider.findings(for: check, from: results, context: context, statistics: TableHealthStatistics()).findings + } + private func redundant(_ rows: [IndexRow]) -> [String: String] { var map: [String: String] = [:] - for finding in provider.findings(for: .redundantIndex, from: [Self.indexResult(rows)], context: context) { + for finding in findings(.redundantIndex, from: [Self.indexResult(rows)]) { map[finding.objectName ?? ""] = finding.detail } return map @@ -67,6 +71,8 @@ struct MySQLTableHealthProviderTests { "referenced_table_name", "referenced_column_name", "ordinal_position", "is_local", "references_here" ] + private static let tableRowsColumns = ["table_name", "estimated_rows"] + private func unused( candidates: [(table: String, index: String)], indexes: [IndexRow], @@ -89,16 +95,18 @@ struct MySQLTableHealthProviderTests { String(seen[identity, default: 1]), key.isLocal ? "1" : "0", key.referencesHere ? "1" : "0" ] } - ) + ), + TableHealthFixture.result(columns: Self.tableRowsColumns, rows: []) ] - return provider.findings(for: .unusedIndex, from: results, context: context).compactMap(\.objectName) + return findings(.unusedIndex, from: results).compactMap { $0.objectName } } private func statistics( performanceSchema: String = "1", instrument: String = "YES", consumer: String = "YES", - countedTables: String = "3" + countedTables: String = "3", + version: String = "8.0.46" ) -> TableHealthStatistics { provider.statistics( from: [ @@ -107,7 +115,8 @@ struct MySQLTableHealthProviderTests { TableHealthFixture.result( columns: ["table_io_instrument", "global_instrumentation", "counted_tables"], rows: [[instrument, consumer, countedTables]] - ) + ), + TableHealthFixture.result(columns: ["version"], rows: [[version]]) ], now: Date(timeIntervalSince1970: 1_800_000_000) ) @@ -209,8 +218,8 @@ struct MySQLTableHealthProviderTests { IndexRow(index: "idx_ab", nonUnique: "1", column: "a", visible: "NO"), IndexRow(index: "idx_ab", nonUnique: "1", column: "b", visible: "NO") ]) - let ignored = provider.findings( - for: .redundantIndex, + let ignored = findings( + .redundantIndex, from: [TableHealthFixture.result( columns: [ "TABLE_NAME", "INDEX_NAME", "SEQ_IN_INDEX", "NON_UNIQUE", "INDEX_TYPE", "COLUMN_NAME", "SUB_PART", "COLLATION", @@ -221,8 +230,7 @@ struct MySQLTableHealthProviderTests { ["orders", "idx_ab", "1", "1", "BTREE", "a", nil, "A", "YES"], ["orders", "idx_ab", "2", "1", "BTREE", "b", nil, "A", "YES"] ] - )], - context: context + )] ) #expect(invisible.isEmpty) #expect(ignored.isEmpty) @@ -259,19 +267,19 @@ struct MySQLTableHealthProviderTests { #expect(found.isEmpty) } - @Test("A redundant index previews a drop qualified by the report's database") + @Test("A redundant index previews a drop qualified by the report's database and names its covering index") func redundantFixQuotes() throws { - let finding = try #require(provider.findings( - for: .redundantIndex, + let finding = try #require(findings( + .redundantIndex, from: [Self.indexResult([ IndexRow(table: "odd`name", index: "idx_a", nonUnique: "1", column: "a"), IndexRow(table: "odd`name", index: "idx_ab", nonUnique: "1", column: "a"), IndexRow(table: "odd`name", index: "idx_ab", nonUnique: "1", column: "b") - ])], - context: context + ])] ).first) #expect(finding.schema == nil) #expect(finding.fixSQL == "ALTER TABLE `shop`.`odd``name` DROP INDEX `idx_a`;") + #expect(finding.metric("covering_index")?.value == .text("idx_ab")) } @Test("An unread plain index is unused; a unique, a full-text and a dropped index are not") @@ -351,25 +359,30 @@ struct MySQLTableHealthProviderTests { #expect(found == ["refs_id_idx"]) } - @Test("An unused index and a table without a primary key preview their fixes") + @Test("An unused index and a table without a primary key preview their fixes and carry the table's rows") func unusedAndMissingKeyFixes() throws { - let unusedFinding = try #require(provider.findings( - for: .unusedIndex, + let unusedFinding = try #require(findings( + .unusedIndex, from: [ TableHealthFixture.result(columns: ["table_name", "index_name"], rows: [["orders", "orders_note_idx"]]), Self.indexResult([IndexRow(index: "orders_note_idx", nonUnique: "1", column: "note")]), - TableHealthFixture.result(columns: Self.keyColumns, rows: []) - ], - context: context + TableHealthFixture.result(columns: Self.keyColumns, rows: []), + TableHealthFixture.result(columns: Self.tableRowsColumns, rows: [["Orders", "7"], ["orders", "4200"]]) + ] ).first) - let keyFinding = try #require(provider.findings( - for: .missingPrimaryKey, - from: [TableHealthFixture.result(columns: ["table_name"], rows: [["logs"]])], - context: context + let keyFinding = try #require(findings( + .missingPrimaryKey, + from: [TableHealthFixture.result( + columns: ["table_name", "estimated_rows", "table_bytes"], rows: [["logs", "900", "49152"]] + )] ).first) #expect(unusedFinding.fixSQL == "ALTER TABLE `shop`.`orders` DROP INDEX `orders_note_idx`;") + #expect(unusedFinding.metric("estimated_rows")?.value == .count(4200)) + #expect(unusedFinding.sizeBytes == nil) #expect(keyFinding.fixSQL == "ALTER TABLE `shop`.`logs` ADD PRIMARY KEY (column_name);") + #expect(keyFinding.metric("estimated_rows")?.value == .count(900)) + #expect(keyFinding.sizeBytes == 49152) } @Test("Each setting that stops index reads being counted skips the unused index check") @@ -384,7 +397,22 @@ struct MySQLTableHealthProviderTests { @Test("While reads are counted, the note names the server start") func serverStartNote() { let start = Date(timeIntervalSince1970: 1_800_000_000 - 3600).formatted(date: .abbreviated, time: .shortened) - #expect(statistics().note?.contains(start) == true) + #expect(statistics().notes.contains { $0.contains(start) }) + } + + @Test("The statistics read the server version and tell MariaDB apart, also when the unused check is skipped") + func serverVersion() { + let mysql = statistics(version: "8.0.46") + let mariadb = statistics(performanceSchema: "0", version: "10.6.28-MariaDB-1:10.6.28+maria~ubu2004") + #expect(mysql.serverVersion == 80046) + #expect(!mysql.isMariaDB) + #expect(mariadb.serverVersion == 100_628) + #expect(mariadb.isMariaDB) + #expect(mariadb.skippedChecks[.unusedIndex] != nil) + #expect(MySQLTableHealthProvider.serverVersion(from: "8.4.13-log") == 80413) + #expect(MySQLTableHealthProvider.serverVersion(from: "5.5.5-10.11.6-MariaDB") == 101_106) + #expect(MySQLTableHealthProvider.serverVersion(from: "11.4") == 110_400) + #expect(MySQLTableHealthProvider.serverVersion(from: "unknown") == nil) } @Test("Without access to the counting settings the unused check still runs and reports for itself") @@ -398,7 +426,8 @@ struct MySQLTableHealthProviderTests { now: Date() ) #expect(stats.skippedChecks.isEmpty) - #expect(stats.note == nil) + #expect(stats.notes.isEmpty) + #expect(stats.serverVersion == nil) } @Test("The index catalog is read once for the two checks that use it") @@ -411,9 +440,32 @@ struct MySQLTableHealthProviderTests { #expect(asked.filter { $0 == MySQLTableHealthProvider.indexQuery }.count == 1) } + @Test("Every check the provider lists has a query on every server it can meet, so none reports nothing found unread") + func everyCheckHasAQuery() { + var servers = [provider.statistics(from: [nil, nil, nil, nil], now: Date())] + for version in ["5.7.44", "8.0.46", "8.4.13", "10.6.28-MariaDB", "11.4.13-MariaDB"] { + servers.append(statistics(version: version)) + servers.append(statistics(performanceSchema: "0", version: version)) + } + for server in servers { + for check in provider.checks where server.skippedChecks[check] == nil { + #expect(!provider.queries(for: check, statistics: server).isEmpty, "\(check.rawValue) has no query") + } + } + } + + @Test("Every check names itself and states what it looks for") + func everyCheckDescribed() { + for check in provider.checks { + #expect(provider.check(check).kind == check) + #expect(!provider.check(check).summary.isEmpty, "\(check.rawValue) has no summary") + } + } + @Test("Every query the report runs only reads") func queriesOnlyRead() { - let queries = provider.statisticsQueries + provider.checks.flatMap { provider.queries(for: $0) } + let queries = provider.statisticsQueries + + provider.checks.flatMap { provider.queries(for: $0, statistics: TableHealthStatistics()) } for query in queries where query != MySQLTableHealthProvider.uptimeQuery { let text = query.trimmingCharacters(in: .whitespacesAndNewlines) #expect(text.uppercased().hasPrefix("SELECT"), "\(text.prefix(40)) does not start with SELECT") diff --git a/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift b/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift index 63ae179304..3a0c8cf8e6 100644 --- a/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift +++ b/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift @@ -8,6 +8,8 @@ struct PostgreSQLTableHealthProviderTests { var errorDescription: String? { "permission denied for pg_stat_user_tables" } } + private static let statisticsColumns = ["track_counts", "stats_reset", "server_version_num", "in_recovery"] + private let provider = PostgreSQLTableHealthProvider() private let context = TableHealthFixture.postgresContext() @@ -15,13 +17,28 @@ struct PostgreSQLTableHealthProviderTests { provider.findings( for: check, from: [TableHealthFixture.result(columns: columns, rows: rows)], - context: context - ) + context: context, + statistics: TableHealthStatistics() + ).findings + } + + private static func statisticsResult( + trackCounts: String = "on", + statsReset: String? = nil, + version: String? = "170011", + inRecovery: String? = "f" + ) -> QueryResult { + TableHealthFixture.result(columns: statisticsColumns, rows: [[trackCounts, statsReset, version, inRecovery]]) } - private func statistics(trackCounts: String, statsReset: String?) -> TableHealthStatistics { + private func statistics( + trackCounts: String = "on", + statsReset: String? = nil, + version: String? = "170011", + inRecovery: String? = "f" + ) -> TableHealthStatistics { provider.statistics( - from: [TableHealthFixture.result(columns: ["track_counts", "stats_reset"], rows: [[trackCounts, statsReset]])], + from: [Self.statisticsResult(trackCounts: trackCounts, statsReset: statsReset, version: version, inRecovery: inRecovery)], now: Date() ) } @@ -35,32 +52,47 @@ struct PostgreSQLTableHealthProviderTests { #expect(TableHealthProviderFactory.provider(for: .mssql) == nil) } - @Test("A bloated table names its dead and live rows and previews a vacuum") + @Test("A bloated table carries its rows, dead share, last vacuum and size as values, and previews a vacuum") func bloatFinding() throws { + let vacuumed = Date(timeIntervalSince1970: 1_800_000_000) let finding = try #require(findings( .bloat, - columns: ["schema_name", "table_name", "live_rows", "dead_rows", "last_vacuum"], - rows: [["public", "orders", "2000", "3000", "2026-10-31 19:48:20+00"]] + columns: ["schema_name", "table_name", "live_rows", "dead_rows", "last_vacuum", "table_bytes"], + rows: [["public", "orders", "2000", "3000", "1800000000", "81920"]] ).first) #expect(finding.schema == "public") #expect(finding.table == "orders") #expect(finding.objectName == nil) - #expect(finding.detail.contains("2026-10-31 19:48:20+00")) + #expect(finding.sizeBytes == 81920) + #expect(finding.metric("dead_rows")?.value == .count(3000)) + #expect(finding.metric("live_rows")?.value == .count(2000)) + #expect(finding.metric("dead_percent")?.value == .percent(0.6)) + #expect(finding.metric("last_vacuum")?.value == .date(vacuumed)) + #expect(finding.metric("table_bytes") == nil) + #expect(finding.detail.contains(TableHealthFormatting.date(vacuumed))) #expect(finding.fixSQL == #"VACUUM (ANALYZE) "public"."orders";"#) } - @Test("A table never vacuumed says so") + @Test("A table never vacuumed says so and has no last vacuum value") func neverVacuumed() throws { let finding = try #require(findings( .bloat, - columns: ["schema_name", "table_name", "live_rows", "dead_rows", "last_vacuum"], - rows: [["public", "orders", "2000", "3000", nil]] + columns: ["schema_name", "table_name", "live_rows", "dead_rows", "last_vacuum", "table_bytes"], + rows: [["public", "orders", "2000", "3000", nil, "81920"]] ).first) #expect(finding.detail.contains("Never vacuumed")) + #expect(finding.metric("last_vacuum") == nil) + } + + @Test("The bloat check states the rule its query applies") + func bloatSummaryStatesRule() { + let summary = provider.check(.bloat).summary + #expect(summary.contains("1,000")) + #expect(summary.contains("20%")) } - @Test("An unused index previews a concurrent drop") + @Test("An unused index previews a concurrent drop and is sized by the index") func unusedIndexFinding() throws { let finding = try #require(findings( .unusedIndex, @@ -70,17 +102,20 @@ struct PostgreSQLTableHealthProviderTests { #expect(finding.table == "orders") #expect(finding.objectName == "orders_note_idx") + #expect(finding.sizeBytes == 8192) #expect(finding.fixSQL == #"DROP INDEX CONCURRENTLY "sales"."orders_note_idx";"#) } - @Test("A table without a primary key quotes its name in the preview") + @Test("A table without a primary key quotes its name in the preview and carries its rows and size") func missingPrimaryKeyQuotesName() throws { let finding = try #require(findings( .missingPrimaryKey, - columns: ["schema_name", "table_name"], - rows: [["public", #"Odd "Name""#]] + columns: ["schema_name", "table_name", "estimated_rows", "table_bytes"], + rows: [["public", #"Odd "Name""#, "1200", "65536"]] ).first) #expect(finding.fixSQL == #"ALTER TABLE "public"."Odd ""Name""" ADD PRIMARY KEY (column_name);"#) + #expect(finding.metric("estimated_rows")?.value == .count(1200)) + #expect(finding.sizeBytes == 65536) } @Test("A foreign key's column rows fold into one finding, in key order") @@ -96,10 +131,26 @@ struct PostgreSQLTableHealthProviderTests { ] ) - #expect(found.map(\.objectName) == ["line_order_fk", "line_pair_fk", "line_order_fk"]) - #expect(found.map(\.schema) == ["public", "public", "audit"]) + #expect(found.map { $0.objectName } == ["line_order_fk", "line_pair_fk", "line_order_fk"]) + #expect(found.map { $0.schema } == ["public", "public", "audit"]) #expect(found[0].fixSQL == #"CREATE INDEX CONCURRENTLY ON "public"."line" ("order_id");"#) #expect(found[1].fixSQL == #"CREATE INDEX CONCURRENTLY ON "public"."line" ("y", "x");"#) + #expect(found[1].metric("columns")?.value == .text("y, x")) + } + + @Test("A foreign key names the table it references and carries the referencing table's rows and size") + func foreignKeyValues() throws { + let finding = try #require(findings( + .unindexedForeignKey, + columns: [ + "schema_name", "table_name", "table_kind", "constraint_name", "column_name", + "referenced_schema", "referenced_table", "estimated_rows", "table_bytes" + ], + rows: [["public", "line", "r", "line_order_fk", "order_id", "sales", "orders", "5000", "40960"]] + ).first) + #expect(finding.metric("referenced_table")?.value == .text("sales.orders")) + #expect(finding.metric("estimated_rows")?.value == .count(5000)) + #expect(finding.sizeBytes == 40960) } @Test("A partitioned table's index is not built concurrently, which PostgreSQL refuses there") @@ -118,22 +169,79 @@ struct PostgreSQLTableHealthProviderTests { .unusedIndex, columns: ["schema_name", "table_name", "index_name", "index_bytes"], rows: [["a", "t", "i1", "1"], ["a", "t", "i2", "1"], ["b", "t", "i1", "1"]] - ).map(\.id) + ).map { $0.id } #expect(Set(ids).count == 3) } + @Test("A finding is the same finding when its numbers move between runs") + func identityIgnoresNumbers() throws { + let columns = ["schema_name", "table_name", "index_name", "index_bytes"] + let before = try #require(findings(.unusedIndex, columns: columns, rows: [["a", "t", "i1", "8192"]]).first) + let after = try #require(findings(.unusedIndex, columns: columns, rows: [["a", "t", "i1", "16384"]]).first) + #expect(before == after) + #expect(Set([before, after]).count == 1) + } + @Test("The note names when statistics were reset, or that they never were") func statisticsNote() { - #expect(statistics(trackCounts: "on", statsReset: "2026-10-31 19:48:20+00").note?.contains("2026-10-31 19:48:20+00") == true) - #expect(statistics(trackCounts: "on", statsReset: nil).note?.contains("never been reset") == true) + let reset = Date(timeIntervalSince1970: 1_800_000_000) + #expect(statistics(statsReset: "1800000000").notes == [ + String(format: String(localized: "Index usage counts since the statistics were last reset at %@."), TableHealthFormatting.date(reset)) + ]) + #expect(statistics(statsReset: nil).notes.first?.contains("never been reset") == true) #expect(provider.statistics(from: [nil], now: Date()) == TableHealthStatistics()) } + @Test("The statistics read the server version") + func serverVersion() { + #expect(statistics(version: "90624").serverVersion == 90624) + #expect(statistics(version: "170011").serverVersion == 170_011) + #expect(statistics(version: nil).serverVersion == nil) + #expect(statistics().isStandby == false) + } + @Test("With track_counts off, the checks that read activity counters are skipped") func trackCountsOffSkipsCounterChecks() { - let stats = statistics(trackCounts: "off", statsReset: nil) - #expect(Set(stats.skippedChecks.keys) == [.bloat, .unusedIndex]) - #expect(stats.note == nil) + let stats = statistics(trackCounts: "off") + #expect(stats.skippedChecks[.bloat] != nil) + #expect(stats.skippedChecks[.unusedIndex] != nil) + #expect(stats.skippedChecks[.missingPrimaryKey] == nil) + #expect(stats.skippedChecks[.unindexedForeignKey] == nil) + #expect(stats.notes.isEmpty) + } + + @Test("On a standby the counter checks are skipped with the reason, and the report says to fix on the primary") + func standbySkipsCounterChecks() { + let stats = statistics(inRecovery: "t") + let reason = String(localized: "This server is a standby. Its activity counters do not reflect the primary.") + #expect(stats.isStandby) + #expect(stats.skippedChecks[.bloat] == reason) + #expect(stats.skippedChecks[.unusedIndex] == reason) + #expect(stats.skippedChecks[.missingPrimaryKey] == nil) + #expect(stats.skippedChecks[.unindexedForeignKey] == nil) + #expect(stats.notes == [String(localized: "This server is a read-only standby. Run the fixes on the primary.")]) + } + + @Test("A standby report runs the structural checks only and carries the standby note") + func standbyReport() async throws { + var asked: [String] = [] + let report = try await provider.fetchReport(context: context) { query in + asked.append(query) + if query == PostgreSQLTableHealthProvider.statisticsQuery { + return Self.statisticsResult(inRecovery: "t") + } + if query == PostgreSQLTableHealthProvider.missingPrimaryKeyQuery { + return TableHealthFixture.result(columns: ["schema_name", "table_name"], rows: [["public", "logs"]]) + } + return TableHealthFixture.empty() + } + + #expect(!asked.contains(PostgreSQLTableHealthProvider.bloatQuery)) + #expect(!asked.contains(PostgreSQLTableHealthProvider.unusedIndexQuery)) + #expect(asked.contains(PostgreSQLTableHealthProvider.unindexedForeignKeyQuery)) + #expect(report.result(for: .missingPrimaryKey)?.outcome == .completed) + #expect(report.result(for: .missingPrimaryKey)?.findings.count == 1) + #expect(report.notes.contains(String(localized: "This server is a read-only standby. Run the fixes on the primary."))) } @Test("A skipped check runs nothing and says why") @@ -142,20 +250,21 @@ struct PostgreSQLTableHealthProviderTests { let report = try await provider.fetchReport(context: context) { query in asked.append(query) if query == PostgreSQLTableHealthProvider.statisticsQuery { - return TableHealthFixture.result(columns: ["track_counts", "stats_reset"], rows: [["off", nil]]) + return Self.statisticsResult(trackCounts: "off") } return TableHealthFixture.empty() } #expect(!asked.contains(PostgreSQLTableHealthProvider.bloatQuery)) #expect(!asked.contains(PostgreSQLTableHealthProvider.unusedIndexQuery)) - #expect(report.checks[0].outcome == .skipped(statistics(trackCounts: "off", statsReset: nil).skippedChecks[.bloat] ?? "")) + #expect(report.checks[0].outcome == .skipped(statistics(trackCounts: "off").skippedChecks[.bloat] ?? "")) #expect(report.checks[2].outcome == .completed) } @Test("A failing check reports its error and leaves the other checks standing") func failingCheckIsolated() async throws { - let report = try await provider.fetchReport(context: context) { query in + let generated = Date(timeIntervalSince1970: 1_800_000_000) + let report = try await provider.fetchReport(context: context, now: generated) { query in if query == PostgreSQLTableHealthProvider.bloatQuery { throw QueryFailure() } if query == PostgreSQLTableHealthProvider.missingPrimaryKeyQuery { return TableHealthFixture.result(columns: ["schema_name", "table_name"], rows: [["public", "logs"]]) @@ -164,9 +273,11 @@ struct PostgreSQLTableHealthProviderTests { } #expect(report.database == "shop") - #expect(report.checks.map(\.kind) == [.bloat, .unusedIndex, .missingPrimaryKey, .unindexedForeignKey]) + #expect(report.generatedAt == generated) + #expect(report.checks.map { $0.kind } == [.bloat, .unusedIndex, .missingPrimaryKey, .unindexedForeignKey]) + #expect(report.checks.map { $0.check } == provider.checks.map { provider.check($0) }) #expect(report.checks[0].outcome == .failed("permission denied for pg_stat_user_tables")) - #expect(report.checks[2].findings.map(\.table) == ["logs"]) + #expect(report.checks[2].findings.map { $0.table } == ["logs"]) #expect(report.findingCount == 1) } @@ -177,7 +288,7 @@ struct PostgreSQLTableHealthProviderTests { return TableHealthFixture.empty() } #expect(report.checks.allSatisfy { $0.outcome == .completed }) - #expect(report.statisticsNote == nil) + #expect(report.notes.isEmpty) } @Test("A cancelled refresh stops instead of reporting every check as failed") @@ -187,9 +298,47 @@ struct PostgreSQLTableHealthProviderTests { } } + @Test("Every check the provider lists has a query on every server it can meet, so none reports nothing found unread") + func everyCheckHasAQuery() { + var servers = [provider.statistics(from: [nil], now: Date())] + for version in ["90624", "100000", "120000", "170011", nil] { + for trackCounts in ["on", "off"] { + for inRecovery in ["t", "f"] { + servers.append(statistics(trackCounts: trackCounts, version: version, inRecovery: inRecovery)) + } + } + } + for server in servers { + for check in provider.checks where server.skippedChecks[check] == nil { + #expect(!provider.queries(for: check, statistics: server).isEmpty, "\(check.rawValue) has no query") + } + } + } + + @Test("Every check names itself and states what it looks for") + func everyCheckDescribed() { + for check in provider.checks { + #expect(provider.check(check).kind == check) + #expect(!provider.check(check).summary.isEmpty, "\(check.rawValue) has no summary") + } + } + + // TimescaleDB's chunk tables are not extension members, but their schema is. + @Test("Every check query leaves out extension members, by relation and by schema") + func everyQueryLeavesOutExtensions() { + for check in provider.checks { + for query in provider.queries(for: check, statistics: TableHealthStatistics()) { + #expect(query.contains("'pg_catalog.pg_class'::pg_catalog.regclass"), "\(check.rawValue) keeps extension relations") + #expect(query.contains("'pg_catalog.pg_namespace'::pg_catalog.regclass"), "\(check.rawValue) keeps extension schemas") + #expect(query.contains("deptype = 'e'"), "\(check.rawValue) keeps extension members") + } + } + } + @Test("Every query the report runs only reads") func queriesOnlyRead() { - let queries = provider.statisticsQueries + provider.checks.flatMap { provider.queries(for: $0) } + let queries = provider.statisticsQueries + + provider.checks.flatMap { provider.queries(for: $0, statistics: TableHealthStatistics()) } for query in queries { let text = query.trimmingCharacters(in: .whitespacesAndNewlines) #expect(text.uppercased().hasPrefix("SELECT"), "\(text.prefix(40)) does not start with SELECT") diff --git a/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift b/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift new file mode 100644 index 0000000000..d2e40fd734 --- /dev/null +++ b/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift @@ -0,0 +1,87 @@ +import Foundation +@testable import TablePro +import Testing + +struct TableHealthFixScriptTests { + private static let generated = Date(timeIntervalSince1970: 1_800_000_000) + + private func script(database: String = "shop", _ findings: [TableHealthFinding]) -> String { + TableHealthFixScript.script( + database: database, generatedAt: Self.generated, databaseType: .postgresql, findings: findings + ) + } + + private func lines(_ text: String) -> [Substring] { + text.split(omittingEmptySubsequences: false, whereSeparator: { $0.isNewline }) + } + + @Test("The script names the database and date, says to run each statement alone, then gives each fix under its finding") + func scriptShape() { + let text = script([ + TableHealthFixture.finding(.unusedIndex, schema: "public", table: "orders", object: "orders_note_idx", + fixSQL: #"DROP INDEX CONCURRENTLY "public"."orders_note_idx";"#), + TableHealthFixture.finding(schema: "public", table: "logs", + fixSQL: #"ALTER TABLE "public"."logs" ADD PRIMARY KEY (column_name);"#) + ]) + let expected = [ + "-- " + String(format: String(localized: "Database: %1$@ (%2$@)"), "shop", DatabaseType.postgresql.displayName), + "-- " + String(format: String(localized: "Generated: %@"), "2027-01-15T08:00:00Z"), + "-- " + String(localized: "Run each statement on its own; some cannot run inside a transaction."), + "", + "-- " + String(format: String(localized: "%1$@: %2$@"), TableHealthCheckKind.unusedIndex.title, "public.orders.orders_note_idx"), + #"DROP INDEX CONCURRENTLY "public"."orders_note_idx";"#, + "", + "-- " + String(format: String(localized: "%1$@: %2$@"), TableHealthCheckKind.missingPrimaryKey.title, "public.logs"), + #"ALTER TABLE "public"."logs" ADD PRIMARY KEY (column_name);"#, + "" + ].joined(separator: "\n") + #expect(text == expected) + } + + @Test("A finding without a fix is left out") + func findingWithoutFixSkipped() { + let text = script([ + TableHealthFixture.finding(table: "kept", fixSQL: "SELECT 1;"), + TableHealthFixture.finding(table: "nofix", fixSQL: nil), + TableHealthFixture.finding(table: "blank", fixSQL: " \n") + ]) + #expect(text.contains("public.kept")) + #expect(!text.contains("nofix")) + #expect(!text.contains("blank")) + } + + @Test( + "A hostile name stays on its one comment line and cannot open or close a block comment", + arguments: [ + "evil\nDROP TABLE users;", + "evil\rDROP TABLE users;", + "evil\r\nDROP TABLE users;", + "evil\u{2028}DROP TABLE users;", + "evil\u{2029}DROP TABLE users;", + "evil\u{0085}DROP TABLE users;", + "evil\u{000B}DROP\u{000C}TABLE\tusers;", + "evil */ DROP TABLE users; /*", + "evil /*!50000 DROP TABLE users */", + "evil*/*/" + ] + ) + func hostileNamesStayInTheComment(name: String) throws { + let text = script(database: name, [ + TableHealthFixture.finding(.unusedIndex, schema: name, table: name, object: name, fixSQL: "SELECT 1;") + ]) + let all = lines(text) + try #require(all.count == 7) + #expect(all.filter { !$0.isEmpty }.allSatisfy { $0.hasPrefix("-- ") || $0 == "SELECT 1;" }) + #expect(all[4].hasPrefix("-- ")) + #expect(all[5] == "SELECT 1;") + #expect(!text.contains("/*")) + #expect(!text.contains("*/")) + #expect(!text.contains("\t")) + } + + @Test("Flattening turns every line break and tab into a space and keeps the rest") + func singleLine() { + #expect(TableHealthFixScript.singleLine("a\nb\r\nc\u{2028}d\te") == "a b c d e") + #expect(TableHealthFixScript.singleLine("表 é") == "表 é") + } +} diff --git a/apple/TableProTests/Core/TableHealth/TableHealthLedgerTests.swift b/apple/TableProTests/Core/TableHealth/TableHealthLedgerTests.swift new file mode 100644 index 0000000000..9b0ec7f6c2 --- /dev/null +++ b/apple/TableProTests/Core/TableHealth/TableHealthLedgerTests.swift @@ -0,0 +1,255 @@ +import Foundation +@testable import TablePro +import Testing + +struct TableHealthLedgerTests { + private typealias Fixture = TableHealthFixture + + private let logs = Fixture.finding(table: "logs") + private let events = Fixture.finding(table: "events") + private let noteIndex = Fixture.finding(.unusedIndex, table: "orders", object: "orders_note_idx") + + private static func day(_ number: Int) -> Date { + Date(timeIntervalSince1970: TimeInterval(number) * 86_400) + } + + private func recorded( + _ runs: [[(kind: TableHealthCheckKind, outcome: TableHealthCheckResult.Outcome, findings: [TableHealthFinding])]], + into ledger: TableHealthLedger = TableHealthLedger() + ) -> TableHealthLedger { + // Run n is recorded on day n, so a date in an expectation names its run. + var ledger = ledger + for run in runs { + ledger = ledger.recording(Fixture.report(run), at: Self.day(ledger.runIndex + 1)) + } + return ledger + } + + private func missingKeys(_ findings: [TableHealthFinding]) -> [( + kind: TableHealthCheckKind, outcome: TableHealthCheckResult.Outcome, findings: [TableHealthFinding] + )] { + [(.missingPrimaryKey, .completed, findings)] + } + + @Test("The first run marks nothing new: there is nothing to compare it with") + func firstRunMarksNothingNew() { + let ledger = recorded([missingKeys([logs])]) + #expect(ledger.runIndex == 1) + #expect(ledger.status(of: logs.id) == .none) + #expect(ledger.firstSeen(logs.id) == Self.day(1)) + } + + @Test("A finding first seen in the latest run is new, and only for that run") + func laterFindingIsNew() { + let second = recorded([missingKeys([logs]), missingKeys([logs, events])]) + #expect(second.status(of: events.id) == .new) + #expect(second.status(of: logs.id) == .none) + + let third = recorded([missingKeys([logs, events])], into: second) + #expect(third.status(of: events.id) == .none) + #expect(third.firstSeen(events.id) == Self.day(2)) + #expect(third.lastSeen(events.id) == Self.day(3)) + } + + @Test("A check's first completion marks nothing new, even after earlier runs of other checks") + func firstCompletionOfACheckMarksNothingNew() { + let ledger = recorded([ + [(.missingPrimaryKey, .completed, [logs]), (.unusedIndex, .skipped("track_counts is off"), [])], + [(.missingPrimaryKey, .completed, [logs]), (.unusedIndex, .completed, [noteIndex])] + ]) + #expect(ledger.status(of: noteIndex.id) == .none) + } + + @Test("A failed or skipped check resolves nothing and moves nothing") + func failedAndSkippedChecksResolveNothing() { + let ledger = recorded([ + missingKeys([logs]), + [(.missingPrimaryKey, .failed("permission denied"), [])], + [(.missingPrimaryKey, .skipped("standby"), [])] + ]) + #expect(ledger.resolved.isEmpty) + #expect(ledger.lastSeen(logs.id) == Self.day(1)) + #expect(ledger.status(of: logs.id) == .none) + } + + @Test("A finding absent from a completed run is resolved once, at that run") + func absentFindingResolvesOnce() throws { + let ledger = recorded([missingKeys([logs, events]), missingKeys([logs]), missingKeys([logs])]) + let resolved = try #require(ledger.resolved.first) + #expect(ledger.resolved.count == 1) + #expect(resolved.id == events.id) + #expect(resolved.resolvedRun == 2) + #expect(resolved.resolvedAt == Self.day(2)) + #expect(resolved.lastSeen == Self.day(1)) + } + + @Test("A resolved finding that comes back is open again and new from that run") + func reappearingFindingIsNewAgain() { + let ledger = recorded([missingKeys([logs, events]), missingKeys([logs]), missingKeys([logs, events])]) + #expect(ledger.resolved.isEmpty) + #expect(ledger.status(of: events.id) == .new) + #expect(ledger.firstSeen(events.id) == Self.day(3)) + } + + @Test("An ignore wins over new and survives the finding going away and coming back") + func ignoreSurvives() { + var ledger = recorded([missingKeys([logs]), missingKeys([logs, events])]) + ledger = ledger.ignoring([events.id], at: Self.day(10)) + #expect(ledger.status(of: events.id) == .ignored) + #expect(ledger.ignored[events.id] == Self.day(10)) + + ledger = recorded([missingKeys([logs]), missingKeys([logs, events])], into: ledger) + #expect(ledger.status(of: events.id) == .ignored) + + ledger = ledger.ignoring([events.id], at: Self.day(20)) + #expect(ledger.ignored[events.id] == Self.day(10)) + + ledger = ledger.unignoring([events.id]) + #expect(ledger.status(of: events.id) == .new) + #expect(!ledger.isIgnored(events.id)) + } + + @Test("Resolved findings list the latest run first, by run before date, so a clock change cannot reorder them") + func resolvedOrderFollowsRuns() { + var ledger = TableHealthLedger() + ledger = ledger.recording(Fixture.report(missingKeys([logs, events])), at: Self.day(5)) + ledger = ledger.recording(Fixture.report(missingKeys([events])), at: Self.day(6)) + ledger = ledger.recording(Fixture.report(missingKeys([])), at: Self.day(1)) + #expect(ledger.resolved.map { $0.id } == [events.id, logs.id]) + } + + @Test("Only the 1,000 most recently resolved findings are kept") + func resolvedIsCapped() { + let old = Fixture.finding(table: "old") + let many = (0.. TableHealthQueryContext { - TableHealthQueryContext(database: database) { "\"" + $0.replacingOccurrences(of: "\"", with: "\"\"") + "\"" } + TableHealthQueryContext( + database: database, + quote: { "\"" + $0.replacingOccurrences(of: "\"", with: "\"\"") + "\"" }, + literal: { "'" + $0.replacingOccurrences(of: "'", with: "''") + "'" } + ) } static func mysqlContext(database: String = "shop") -> TableHealthQueryContext { - TableHealthQueryContext(database: database) { "`" + $0.replacingOccurrences(of: "`", with: "``") + "`" } + TableHealthQueryContext( + database: database, + quote: { "`" + $0.replacingOccurrences(of: "`", with: "``") + "`" }, + literal: { + "'" + $0.replacingOccurrences(of: "\\", with: "\\\\").replacingOccurrences(of: "'", with: "''") + "'" + } + ) + } + + static func finding( + _ kind: TableHealthCheckKind = .missingPrimaryKey, + schema: String? = "public", + table: String?, + object: String? = nil, + fixSQL: String? = "SELECT 1;" + ) -> TableHealthFinding { + TableHealthFinding( + kind: kind, schema: schema, table: table, objectName: object, detail: "", fixSQL: fixSQL + ) + } + + static func report( + _ checks: [(kind: TableHealthCheckKind, outcome: TableHealthCheckResult.Outcome, findings: [TableHealthFinding])], + generatedAt: Date = Date(timeIntervalSince1970: 0) + ) -> TableHealthReport { + TableHealthReport( + database: "shop", + checks: checks.map { + TableHealthCheckResult(check: TableHealthCheck(kind: $0.kind, summary: ""), findings: $0.findings, outcome: $0.outcome) + }, + generatedAt: generatedAt + ) } } diff --git a/apple/TableProTests/Models/Query/QueryTabManagerVisibleContainerTests.swift b/apple/TableProTests/Models/Query/QueryTabManagerVisibleContainerTests.swift index 10f7c813be..48ff56fd27 100644 --- a/apple/TableProTests/Models/Query/QueryTabManagerVisibleContainerTests.swift +++ b/apple/TableProTests/Models/Query/QueryTabManagerVisibleContainerTests.swift @@ -102,6 +102,58 @@ struct QueryTabManagerVisibleContainerTests { #expect(manager.tabs.map(\.tableContext.databaseName) == ["tenant_b", ""]) } + // MARK: - Table Health + + // It used to belong to every entry, so a refresh after a browse switch reported on the other database. + @Test("A Table Health tab keeps its database while another entry is shown") + func tableHealthStaysOnItsDatabase() throws { + let manager = makeManager() + + let id = try #require(manager.addTableHealthTab(databaseName: "tenant_a")) + manager.show(container: "tenant_b") + + #expect(manager.tabs.first?.tableContext.databaseName == "tenant_a") + #expect(manager.visibleTabs.isEmpty) + + manager.show(container: "tenant_a") + + #expect(manager.selectedTabId == id) + } + + @Test("Table Health opens once per database") + func tableHealthOpensOncePerDatabase() throws { + let manager = makeManager() + + let first = try #require(manager.addTableHealthTab(databaseName: "tenant_a")) + let again = try #require(manager.addTableHealthTab(databaseName: "tenant_a")) + let other = try #require(manager.addTableHealthTab(databaseName: "tenant_b")) + + let databases = manager.tabs.map { $0.tableContext.databaseName } + #expect(again == first) + #expect(other != first) + #expect(databases == ["tenant_a", "tenant_b"]) + #expect(manager.selectedTabId == other) + } + + @Test("Table Health opens nothing without a database") + func tableHealthNeedsADatabase() { + let manager = makeManager() + + #expect(manager.addTableHealthTab(databaseName: "") == nil) + #expect(manager.tabs.isEmpty) + } + + @Test("A restore binds a Table Health tab saved before it had a database") + func restoreBindsLegacyTableHealthTab() { + let manager = makeManager(visible: "tenant_b") + manager.tabs = [QueryTab(title: "Table Health", tabType: .tableHealth), dashboardTab()] + + manager.bindUnboundTabs() + + let databases = manager.tabs.map { $0.tableContext.databaseName } + #expect(databases == ["tenant_b", ""]) + } + @Test("A tab opened without a database goes to the entry on screen, not the browse cursor") func openingDatabaseIsTheShownEntry() { #expect(makeManager().openingDatabase(browsing: "tenant_b") == "tenant_a") diff --git a/apple/TableProTests/Views/Main/EditorTabLabelResolverTests.swift b/apple/TableProTests/Views/Main/EditorTabLabelResolverTests.swift index 646553e89d..8e52191d19 100644 --- a/apple/TableProTests/Views/Main/EditorTabLabelResolverTests.swift +++ b/apple/TableProTests/Views/Main/EditorTabLabelResolverTests.swift @@ -87,6 +87,35 @@ struct EditorTabLabelResolverTests { #expect(labels[other.id]?.text == "Server Dashboard") } + // One per database, all titled alike: the strip shows one entry's, but the tooltip and Control-Tab list them all. + @Test("A Table Health tab keeps its title and names its database beside it") + func tableHealthNamesItsDatabaseBesideTheTitle() { + var shop = QueryTab(title: "Table Health", tabType: .tableHealth) + shop.tableContext.databaseName = "shop" + var archive = QueryTab(title: "Table Health", tabType: .tableHealth) + archive.tableContext.databaseName = "archive" + let labels = EditorTabLabelResolver.resolve(tabs: [shop, archive], target: .database) + + #expect(labels[shop.id]?.text == "Table Health") + #expect(labels[shop.id]?.description == "Table Health: shop") + #expect(labels[shop.id]?.container == "shop") + #expect(labels[archive.id]?.text == "Table Health") + #expect(labels[archive.id]?.container == "archive") + } + + @Test("The container beside a label is left out once the label already carries it") + func containerIsLeftOutOfAQualifiedLabel() { + let tabs = [ + tableTab("orders", database: "app"), + tableTab("orders", database: "logs"), + tableTab("customers", database: "app"), + ] + let labels = EditorTabLabelResolver.resolve(tabs: tabs, target: .database) + + #expect(labels[tabs[0].id]?.container == nil) + #expect(labels[tabs[2].id]?.container == "app") + } + @Test("A collision between a bound and an unbound tab qualifies only the one that can be") func qualifiesOnlyTheTabWithAContainer() { let tabs = [tableTab("orders", database: "app"), tableTab("orders")] diff --git a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift index cac01ec4dd..d7fcf7c933 100644 --- a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift +++ b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift @@ -386,11 +386,14 @@ struct MainContentCoordinatorRefreshTests { defer { subscription.cancel() } var expected: [UUID] = [] - for tabType in [TabType.serverDashboard, .insights, .tableHealth] { + for tabType in [TabType.serverDashboard, .insights] { tabManager.addSingletonTab(tabType) expected += [tabManager.selectedTabId].compactMap { $0 } coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) } + tabManager.addTableHealthTab(databaseName: "shop") + expected += [tabManager.selectedTabId].compactMap { $0 } + coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) tabManager.addObjectSourceTab(objectRef: DatabaseObjectRef(kind: .view, name: "active_orders", database: "shop")) expected += [tabManager.selectedTabId].compactMap { $0 } coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) From 0a251103655ca39cb7396139c8de3d364a2cb947 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 9 Nov 2026 21:54:13 +0700 Subject: [PATCH 02/13] feat(table-health): checks list, findings table, export, history and new checks --- .claude/rules/data-sync-storage.md | 1 + .../MCP/MCPConnectionBridge+TableHealth.swift | 146 ++++ .../Protocol/Tools/GetTableHealthTool.swift | 122 ++++ .../MCP/Protocol/Tools/MCPToolRegistry.swift | 1 + ...inSplitViewController+MenuValidation.swift | 5 +- .../MainSplitViewController.swift | 4 +- .../ConnectionActionsMenuResolver.swift | 3 +- .../Storage/TableHealthHistoryStorage.swift | 270 +++++++ .../Storage/TableScopedSettingsStore.swift | 16 +- .../MySQLTableHealthProvider.swift | 259 ++++++- .../TableHealth/PostgreSQLIndexShape.swift | 139 ++++ .../PostgreSQLTableHealthProvider.swift | 689 +++++++++++++++++- .../TableHealthExportDataSource.swift | 310 ++++++++ .../TableHealth/TableHealthFixScript.swift | 5 +- .../TablePro/Resources/Localizable.xcstrings | 489 ++++++++++++- .../ViewModels/TableHealthViewModel.swift | 398 +++++++++- .../Main/Child/MainEditorContentView.swift | 41 +- .../MainContentCoordinator+Refresh.swift | 7 +- .../MainContentCoordinator+TabClosing.swift | 3 + ...ontentCoordinator+TableHealthActions.swift | 87 +++ .../MainContentView+TransferSheets.swift | 6 + .../Main/MainContentCommandActions.swift | 14 +- .../Views/Main/MainContentCoordinator.swift | 6 + .../TablePro/Views/Main/MainContentView.swift | 2 +- .../TableHealth/TableHealthChecksList.swift | 71 ++ .../TableHealth/TableHealthFindingsPane.swift | 97 +++ .../TableHealthFindingsTable.swift | 191 +++++ .../TableHealthLayoutMetrics.swift | 13 + .../TableHealth/TableHealthPaneBar.swift | 61 ++ .../TableHealth/TableHealthPreviewPane.swift | 127 ++++ .../Views/TableHealth/TableHealthView.swift | 207 +----- .../Tools/GetTableHealthToolTests.swift | 239 ++++++ .../Core/Menu/MainMenuBuilderTests.swift | 9 + .../ConnectionActionsMenuResolverTests.swift | 8 + .../TableHealthHistoryStorageTests.swift | 331 +++++++++ .../MySQLTableHealthProviderTests.swift | 299 +++++++- .../PostgreSQLIndexShapeTests.swift | 268 +++++++ .../PostgreSQLTableHealthProviderTests.swift | 385 +++++++++- .../TableHealthExportDataSourceTests.swift | 188 +++++ ...nSplitViewControllerDetailWidthTests.swift | 7 + .../TableHealthViewModelTests.swift | 371 +++++++++- .../MainContentCoordinatorRefreshTests.swift | 81 +- .../check-postgresql-catalog-shadowing.sh | 3 +- .../probes/check-table-health-catalogs.sh | 620 +++++++++++++++- 44 files changed, 6244 insertions(+), 355 deletions(-) create mode 100644 apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift create mode 100644 apple/TablePro/Core/MCP/Protocol/Tools/GetTableHealthTool.swift create mode 100644 apple/TablePro/Core/Storage/TableHealthHistoryStorage.swift create mode 100644 apple/TablePro/Core/TableHealth/PostgreSQLIndexShape.swift create mode 100644 apple/TablePro/Core/TableHealth/TableHealthExportDataSource.swift create mode 100644 apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift create mode 100644 apple/TablePro/Views/TableHealth/TableHealthChecksList.swift create mode 100644 apple/TablePro/Views/TableHealth/TableHealthFindingsPane.swift create mode 100644 apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift create mode 100644 apple/TablePro/Views/TableHealth/TableHealthLayoutMetrics.swift create mode 100644 apple/TablePro/Views/TableHealth/TableHealthPaneBar.swift create mode 100644 apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift create mode 100644 apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift create mode 100644 apple/TableProTests/Core/Storage/TableHealthHistoryStorageTests.swift create mode 100644 apple/TableProTests/Core/TableHealth/PostgreSQLIndexShapeTests.swift create mode 100644 apple/TableProTests/Core/TableHealth/TableHealthExportDataSourceTests.swift diff --git a/.claude/rules/data-sync-storage.md b/.claude/rules/data-sync-storage.md index abd611cd4e..7b1fde0b28 100644 --- a/.claude/rules/data-sync-storage.md +++ b/.claude/rules/data-sync-storage.md @@ -169,6 +169,7 @@ This path holds user data. A silent wrong answer here looks exactly like an empt | Sidebar database and schema filters, recent tables | UserDefaults, device-local | `DatabaseTreeFilterStorage`, `RecentTablesStore` | | History drawer state | UserDefaults, device-local | `HistoryPanelPreferencesStorage` | | Server Dashboard tiles, per connection | UserDefaults, device-local | `ServerDashboardTileStorage` | +| Table Health history, per connection and database: when each finding was first and last seen, resolved findings, ignored findings | JSON, one file per connection under `TableHealth/`, device-local, never synced | `TableHealthHistoryStorage` | | Module on/off (Settings > Extensions) | UserDefaults, device-local, never synced | `AppModuleSettings` | | Routine debugger start options, per connection and field | UserDefaults, device-local | `RoutineDebugOptionStore` | | Whether the toolbar arrangement already carries the debug items | UserDefaults, device-local | `MainWindowToolbar` | diff --git a/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift b/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift new file mode 100644 index 0000000000..3a6356a246 --- /dev/null +++ b/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift @@ -0,0 +1,146 @@ +import Foundation +import os +import TableProMCP + +extension MCPConnectionBridge { + // Reads the stored history and never records this run, so the tab's New and Resolved stay its own. + func tableHealth(scope: DatabaseScope) async throws -> JsonValue { + let databaseType = try await ensureConnected(scope.connectionId) + guard let provider = TableHealthProviderFactory.provider(for: databaseType) else { + throw MCPToolExecutionError.unsupported( + String(localized: "TablePro has no Table Health checks for this engine.") + ) + } + guard !scope.isServerScoped else { + throw DatabaseAccessError.invalidArgument( + String(localized: "The connection has no current database. Pass a database name.") + ) + } + let ledger = try await Self.tableHealthLedger(connectionId: scope.connectionId, database: scope.database) + // The checks cover every schema, as the Table Health tab does. + let report = try await TableHealthReportLoader.load( + provider: provider, + scope: DatabaseScope(connectionId: scope.connectionId, database: scope.database, schema: nil), + metadata: DatabaseManager.shared + ) + return Self.tableHealthPayload(report, ledger: ledger) + } + + private static func tableHealthLedger(connectionId: UUID, database: String) async throws -> TableHealthLedger { + do { + return try await MainActor.run { + try TableHealthHistoryStorage.shared.ledger(connectionId: connectionId, database: database) + } + } catch { + logger.error("Table Health history unreadable for MCP: \(error.publicLogShape, privacy: .public)") + throw MCPToolExecutionError( + code: .internalFailure, + message: String(localized: "TablePro could not read its Table Health history for this connection.") + ) + } + } + + static func tableHealthPayload(_ report: TableHealthReport, ledger: TableHealthLedger) -> JsonValue { + .object([ + "database": .string(report.database), + "generated_at": .string(iso8601.withLockUnchecked { $0.string(from: report.generatedAt) }), + "notes": .array(report.notes.map { .string($0) }), + "checks": .array(report.checks.map(tableHealthCheckPayload)), + "findings": .array(report.findings.map { finding in + tableHealthFindingPayload(finding, status: ledger.mcpStatus(of: finding.id)) + }) + ]) + } + + static func tableHealthCheckPayload(_ result: TableHealthCheckResult) -> JsonValue { + var fields: [String: JsonValue] = [ + "kind": .string(result.kind.rawValue), + "title": .string(result.check.title), + "summary": .string(result.check.summary), + "finding_count": .int(result.findings.count) + ] + switch result.outcome { + case .completed: + fields["outcome"] = .string("completed") + case .skipped(let reason): + fields["outcome"] = .string("skipped") + fields["reason"] = .string(reason) + case .failed(let message): + fields["outcome"] = .string("failed") + fields["reason"] = .string(tableHealthCheckFailure(kind: result.kind, message: message)) + } + if let note = result.note { + fields["note"] = .string(note) + } + return .object(fields) + } + + // The client is told which check failed, never what the server said: its text can name a role, a table or a + // statement, and this reply leaves the Mac. The Table Health tab shows the full message. + static func tableHealthCheckFailure(kind: TableHealthCheckKind, message: String) -> String { + logger.warning( + "Table Health check \(kind.rawValue, privacy: .public) failed: \(message, privacy: .private)" + ) + return String(localized: "The server did not answer this check.") + } + + static func tableHealthFindingPayload(_ finding: TableHealthFinding, status: TableHealthFindingStatus) -> JsonValue { + var fields: [String: JsonValue] = [ + "kind": .string(finding.kind.rawValue), + "detail": .string(finding.detail), + "metrics": .array(finding.metrics.map(tableHealthMetricPayload)), + "status": .string(status.rawValue) + ] + if let schema = finding.schema { + fields["schema"] = .string(schema) + } + if let table = finding.table { + fields["table"] = .string(table) + } + if let object = finding.objectName { + fields["object"] = .string(object) + } + if let sizeBytes = finding.sizeBytes { + fields["size_bytes"] = .int(Int(sizeBytes)) + } + if let fixSQL = finding.fixSQL { + fields["fix_sql"] = .string(fixSQL) + } + return .object(fields) + } + + static func tableHealthMetricPayload(_ metric: TableHealthMetric) -> JsonValue { + let type: String + let value: JsonValue + switch metric.value { + case .count(let count): + type = "count" + value = .int(Int(count)) + case .bigCount(let count): + type = "count" + // JSON numbers past Int64 lose digits in most clients, so the largest unsigned counters go as text. + value = count <= UInt64(Int.max) ? .int(Int(count)) : .string(String(count)) + case .bytes(let bytes): + type = "bytes" + value = .int(Int(bytes)) + case .percent(let fraction): + type = "percent" + value = fraction.isFinite ? .double(fraction) : .null + case .date(let date): + type = "date" + value = .string(iso8601.withLockUnchecked { $0.string(from: date) }) + case .text(let text): + type = "text" + value = .string(text) + case .flag(let flag): + type = "flag" + value = .bool(flag) + } + return .object([ + "id": .string(metric.id), + "label": .string(metric.label), + "type": .string(type), + "value": value + ]) + } +} diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/GetTableHealthTool.swift b/apple/TablePro/Core/MCP/Protocol/Tools/GetTableHealthTool.swift new file mode 100644 index 0000000000..ef3beb61cf --- /dev/null +++ b/apple/TablePro/Core/MCP/Protocol/Tools/GetTableHealthTool.swift @@ -0,0 +1,122 @@ +import Foundation +import TableProMCP + +public struct GetTableHealthTool: MCPToolImplementation { + public static let name = "get_table_health" + public static let title: String? = String(localized: "Get Table Health") + public static let description = String( + localized: """ + Run TablePro's Table Health checks on one database and return each check's outcome and every finding, \ + with its metrics and the SQL that fixes it. Status comes from the history the Table Health tab records: \ + new since its last run, or ignored by the user. This call records nothing and runs no fix. Available on \ + every engine that has Table Health in TablePro. + """ + ) + public static let requiredScopes: Set = [.toolsRead] + public static let annotations = MCPToolAnnotations( + title: String(localized: "Get Table Health"), + readOnlyHint: true, + destructiveHint: false, + idempotentHint: true, + openWorldHint: false + ) + + static let outcomes = ["completed", "skipped", "failed"] + static let statuses = ["new", "ignored", "none"] + static let metricTypes = ["count", "bytes", "percent", "date", "text", "flag"] + + public static let inputSchema = MCPToolSchema.object( + properties: [ + "connection_id": MCPToolSchema.connectionId, + "database": MCPToolSchema.database + ], + required: ["connection_id"] + ) + + public static let outputSchema: JsonValue? = MCPToolSchema.object( + properties: [ + "database": MCPToolSchema.string(String(localized: "Database the checks ran on")), + "generated_at": MCPToolSchema.string(String(localized: "ISO 8601 time the checks ran")), + "notes": MCPToolSchema.array( + String(localized: "Notes on the whole report, such as when the usage counters started"), + of: MCPToolSchema.stringItem + ), + "checks": MCPToolSchema.array( + String(localized: "Every check this engine runs, in order"), + of: MCPToolSchema.object( + properties: [ + "kind": MCPToolSchema.string(String(localized: "Check id, also set on each of its findings")), + "title": MCPToolSchema.string(String(localized: "Check name")), + "summary": MCPToolSchema.string(String(localized: "What the check looks for, with its threshold")), + "outcome": MCPToolSchema.string( + String(localized: "Whether the check ran"), + enumValues: outcomes + ), + "reason": MCPToolSchema.string(String(localized: "Why the check was skipped or failed")), + "note": MCPToolSchema.string(String(localized: "What to know when reading this check's findings")), + "finding_count": MCPToolSchema.integer(String(localized: "Findings the check reported")) + ], + required: ["kind", "title", "summary", "outcome", "finding_count"] + ) + ), + "findings": MCPToolSchema.array( + String(localized: "Findings of every check that completed, ignored ones included"), + of: MCPToolSchema.object( + properties: [ + "kind": MCPToolSchema.string(String(localized: "Id of the check that reported it")), + "schema": MCPToolSchema.string(String(localized: "Schema, on engines that have one")), + "table": MCPToolSchema.string(String(localized: "Table the finding is about")), + "object": MCPToolSchema.string( + String(localized: "Index, constraint, sequence or column the finding is about") + ), + "detail": MCPToolSchema.string(String(localized: "One-line description")), + "size_bytes": MCPToolSchema.integer( + String(localized: "On-disk size of what the fix acts on: the index for an index check, the table otherwise") + ), + "metrics": MCPToolSchema.array( + String(localized: "Measured values"), + of: MCPToolSchema.object( + properties: [ + "id": MCPToolSchema.string(String(localized: "Stable metric id")), + "label": MCPToolSchema.string(String(localized: "Metric name")), + "type": MCPToolSchema.string( + String(localized: "How to read value: percent is a fraction (0.25 means 25 percent), date is ISO 8601"), + enumValues: metricTypes + ), + "value": .object([ + "description": .string( + String(localized: "Number, string, boolean or null, as type says") + ) + ]) + ], + required: ["id", "label", "type", "value"] + ) + ), + "fix_sql": MCPToolSchema.string( + String(localized: "SQL that fixes the finding, absent when there is none. Review it before running it.") + ), + "status": MCPToolSchema.string( + String(localized: "new: not seen in the last run the Table Health tab recorded; ignored: the user accepted it"), + enumValues: statuses + ) + ], + required: ["kind", "detail", "metrics", "status"] + ) + ) + ], + required: ["database", "generated_at", "notes", "checks", "findings"] + ) + + public init() {} + + public func perform( + arguments: JsonValue, + context: MCPRequestContext, + services: MCPToolServices + ) async throws -> MCPToolCallResult { + try MCPArgumentDecoder.rejectUnknownKeys(arguments, allowed: ["connection_id", "database"]) + let scope = try await MCPScopeArguments.resolve(arguments, services: services) + let payload = try await services.connectionBridge.tableHealth(scope: scope) + return .structured(payload) + } +} diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/MCPToolRegistry.swift b/apple/TablePro/Core/MCP/Protocol/Tools/MCPToolRegistry.swift index db34b4e42c..6c589ba12f 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/MCPToolRegistry.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/MCPToolRegistry.swift @@ -22,6 +22,7 @@ public enum MCPToolRegistry { GetConnectionStatusTool(), GetDatabaseStatisticsTool(), GetTableDdlTool(), + GetTableHealthTool(), GetTableStatisticsTool(), GetViewDefinitionTool(), InsertRowsTool(), diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift index c5a8b64834..af0799bf23 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController+MenuValidation.swift @@ -32,6 +32,8 @@ struct MenuValidationContext: Equatable { var canPresentHighlightRules = false var isQueryTab = false var hasResultRows = false + // Export Results reaches a Table Health tab's findings, which no result set holds. + var hasTableHealthFindings = false var canExportDataDictionary = false var canInsertRows = false var canDeleteRows = false @@ -196,7 +198,7 @@ extension MainSplitViewController: NSMenuItemValidation { case #selector(saveDocumentAs(_:)): return context.isConnected && context.isQueryTab case #selector(exportQueryResults(_:)): - return context.isConnected && context.hasResultRows + return context.isConnected && (context.hasResultRows || context.hasTableHealthFindings) case #selector(exportDataDictionary(_:)): return context.isConnected && context.canExportDataDictionary @@ -671,6 +673,7 @@ extension MainSplitViewController: NSMenuItemValidation { canPresentHighlightRules: actions.canPresentHighlightRules, isQueryTab: actions.isQueryTab, hasResultRows: actions.hasResultRows, + hasTableHealthFindings: actions.hasTableHealthFindings, canExportDataDictionary: actions.canExportDataDictionary, canInsertRows: actions.currentTabAllows(.insert), canDeleteRows: actions.currentTabAllows(.delete), diff --git a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift index 341d3bd2fe..af481d43d9 100644 --- a/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift +++ b/apple/TablePro/Core/Services/Infrastructure/MainSplitViewController.swift @@ -1407,7 +1407,9 @@ internal final class MainSplitViewController: NSSplitViewController { switch tabType { case .usersRoles: return UsersRolesLayoutMetrics.tabMinimumWidth - case .query, .table, .createTable, .erDiagram, .serverDashboard, .insights, .tableHealth, .objectSource, + case .tableHealth: + return TableHealthLayoutMetrics.tabMinimumWidth + case .query, .table, .createTable, .erDiagram, .serverDashboard, .insights, .objectSource, .versionHistory, .tableListing, .routineDebug, .liveMessages: return defaultDetailMinThickness } diff --git a/apple/TablePro/Core/Services/Infrastructure/Toolbar/ConnectionActionsMenuResolver.swift b/apple/TablePro/Core/Services/Infrastructure/Toolbar/ConnectionActionsMenuResolver.swift index 0b8820ed47..ac8a5de61c 100644 --- a/apple/TablePro/Core/Services/Infrastructure/Toolbar/ConnectionActionsMenuResolver.swift +++ b/apple/TablePro/Core/Services/Infrastructure/Toolbar/ConnectionActionsMenuResolver.swift @@ -146,7 +146,8 @@ internal enum ConnectionActionsMenuResolver { ) ) } - if context.tabKind == .table || context.tabKind == .query { + // A Table Health tab exports its findings through the same command. + if context.tabKind == .table || context.tabKind == .query || context.tabKind == .tableHealth { entries.append( ActionsMenuEntry( title: String(localized: "Export Results…"), diff --git a/apple/TablePro/Core/Storage/TableHealthHistoryStorage.swift b/apple/TablePro/Core/Storage/TableHealthHistoryStorage.swift new file mode 100644 index 0000000000..ffd20b45dc --- /dev/null +++ b/apple/TablePro/Core/Storage/TableHealthHistoryStorage.swift @@ -0,0 +1,270 @@ +import Foundation +import os + +enum TableHealthHistoryStorageError: LocalizedError { + case readFailed(String) + case saveFailed(String) + + var errorDescription: String? { + switch self { + case .readFailed(let reason): + return String(format: String(localized: "TablePro could not read the Table Health history: %@"), reason) + case .saveFailed(let reason): + return String(format: String(localized: "TablePro could not save the Table Health history: %@"), reason) + } + } +} + +// Nothing is cached, so every window and the MCP tool read what the last write left on disk. +@MainActor +final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSettingsStore { + static let shared = TableHealthHistoryStorage() + + private static let logger = Logger(subsystem: "com.TablePro", category: "TableHealthHistoryStorage") + + // An object rather than a bare map of databases, so a later build can add a field beside them. + private struct Document: Codable { + var databases: [String: TableHealthLedger] = [:] + } + + private let directory: URL + private let notificationCenter: NotificationCenter + + init( + directory: URL = AppStorageEnvironment.shared.supportDirectory + .appendingPathComponent("TableHealth", isDirectory: true), + notificationCenter: NotificationCenter = .default + ) { + self.directory = directory + self.notificationCenter = notificationCenter + } + + func ledger(connectionId: UUID, database: String) throws -> TableHealthLedger { + try load(connectionId).databases[database] ?? TableHealthLedger() + } + + func record( + _ report: TableHealthReport, + connectionId: UUID, + database: String, + at date: Date + ) throws -> TableHealthLedger { + try mutate(connectionId: connectionId, database: database) { $0.recording(report, at: date) } + } + + func setIgnored( + _ ids: Set, + ignored: Bool, + connectionId: UUID, + database: String, + at date: Date + ) throws -> TableHealthLedger { + try mutate(connectionId: connectionId, database: database) { ledger in + ignored ? ledger.ignoring(ids, at: date) : ledger.unignoring(ids) + } + } + + func clearResolved(connectionId: UUID, database: String) throws -> TableHealthLedger { + try mutate(connectionId: connectionId, database: database) { $0.clearingResolved() } + } + + // Engines without a schema level (MySQL) store findings with a nil schema, which is what their scopes carry. + func renameTable(from oldScope: TableScope, to newScope: TableScope) { + guard let database = oldScope.database, + oldScope.connectionId == newScope.connectionId, + newScope.database == database, + newScope.schema == oldScope.schema, + newScope.table != oldScope.table + else { return } + mutateLogging(connectionId: oldScope.connectionId, database: database) { + $0.renamingTable(schema: oldScope.schema, from: oldScope.table, to: newScope.table) + } + } + + func renameContainer( + connectionId: UUID, + fromDatabase: String, + fromSchema: String?, + toDatabase: String, + toSchema: String? + ) { + if let fromSchema { + guard let toSchema, fromDatabase == toDatabase, fromSchema != toSchema else { return } + mutateLogging(connectionId: connectionId, database: fromDatabase) { + $0.renamingSchema(from: fromSchema, to: toSchema) + } + return + } + guard fromDatabase != toDatabase else { return } + do { + var document = try load(connectionId) + guard let moving = document.databases.removeValue(forKey: fromDatabase) else { return } + // The renamed database takes the name, so a ledger left under it describes objects that are gone. + document.databases[toDatabase] = moving + try save(document, for: connectionId) + postChange(connectionId: connectionId, databases: [fromDatabase, toDatabase]) + } catch { + Self.logFailure(error, connectionId: connectionId) + } + } + + func dropTable(_ scope: TableScope) { + guard let database = scope.database else { return } + mutateLogging(connectionId: scope.connectionId, database: database) { + $0.droppingTable(schema: scope.schema, name: scope.table) + } + } + + func dropContainer(connectionId: UUID, database: String, schema: String?) { + if let schema { + mutateLogging(connectionId: connectionId, database: database) { $0.droppingSchema(schema) } + return + } + do { + var document = try load(connectionId) + guard document.databases.removeValue(forKey: database) != nil else { return } + try save(document, for: connectionId) + postChange(connectionId: connectionId, databases: [database]) + } catch { + Self.logFailure(error, connectionId: connectionId) + } + } + + // Device-local, so there is never a tombstone to leave. + func purgeConnections(_ connectionIds: Set, leavesTombstones: Bool) { + for connectionId in connectionIds { + let databases = (try? load(connectionId))?.databases.keys.sorted() ?? [] + for url in storedFiles(for: connectionId) { + do { + try FileManager.default.removeItem(at: url) + } catch { + Self.logger.error( + "Failed to remove Table Health history for \(connectionId, privacy: .public): \(error.publicLogShape, privacy: .public)" + ) + } + } + postChange(connectionId: connectionId, databases: databases) + } + } + + private func mutate( + connectionId: UUID, + database: String, + _ change: (TableHealthLedger) -> TableHealthLedger + ) throws -> TableHealthLedger { + var document = try load(connectionId) + let current = document.databases[database] ?? TableHealthLedger() + let next = change(current) + guard next != current else { return next } + document.databases[database] = next + try save(document, for: connectionId) + postChange(connectionId: connectionId, databases: [database]) + return next + } + + private func mutateLogging( + connectionId: UUID, + database: String, + _ change: (TableHealthLedger) -> TableHealthLedger + ) { + do { + _ = try mutate(connectionId: connectionId, database: database, change) + } catch { + Self.logFailure(error, connectionId: connectionId) + } + } + + private func load(_ connectionId: UUID) throws -> Document { + let url = fileURL(for: connectionId) + guard FileManager.default.fileExists(atPath: url.path) else { return Document() } + let data: Data + do { + data = try Data(contentsOf: url) + } catch { + Self.logger.error( + "Failed to read Table Health history for \(connectionId, privacy: .public): \(error.publicLogShape, privacy: .public)" + ) + throw TableHealthHistoryStorageError.readFailed(error.localizedDescription) + } + do { + return try JSONDecoder().decode(Document.self, from: data) + } catch { + Self.logger.error( + "Unreadable Table Health history for \(connectionId, privacy: .public): \(error.publicLogShape, privacy: .public)" + ) + try setAside(url, connectionId: connectionId) + return Document() + } + } + + // A file this build cannot decode may be a newer build's, so it is kept under a free name and never written over. + private func setAside(_ url: URL, connectionId: UUID) throws { + var number = 1 + while FileManager.default.fileExists(atPath: corruptFileURL(for: connectionId, number: number).path) { + number += 1 + } + do { + try FileManager.default.moveItem(at: url, to: corruptFileURL(for: connectionId, number: number)) + } catch { + Self.logger.error( + "Failed to set aside unreadable Table Health history for \(connectionId, privacy: .public): \(error.publicLogShape, privacy: .public)" + ) + throw TableHealthHistoryStorageError.readFailed(error.localizedDescription) + } + } + + private func save(_ document: Document, for connectionId: UUID) throws { + let url = fileURL(for: connectionId) + do { + guard !document.databases.isEmpty else { + if FileManager.default.fileExists(atPath: url.path) { + try FileManager.default.removeItem(at: url) + } + return + } + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + try JSONEncoder().encode(document).write(to: url, options: .atomic) + } catch { + Self.logger.error( + "Failed to save Table Health history for \(connectionId, privacy: .public): \(error.publicLogShape, privacy: .public)" + ) + throw TableHealthHistoryStorageError.saveFailed(error.localizedDescription) + } + } + + private func storedFiles(for connectionId: UUID) -> [URL] { + let name = connectionId.uuidString + let contents = (try? FileManager.default.contentsOfDirectory( + at: directory, + includingPropertiesForKeys: nil + )) ?? [] + return contents.filter { url in + let file = url.lastPathComponent + return file == "\(name).json" || (file.hasPrefix("\(name).corrupt-") && file.hasSuffix(".json")) + } + } + + private func fileURL(for connectionId: UUID) -> URL { + directory.appendingPathComponent("\(connectionId.uuidString).json") + } + + private func corruptFileURL(for connectionId: UUID, number: Int) -> URL { + directory.appendingPathComponent("\(connectionId.uuidString).corrupt-\(number).json") + } + + private func postChange(connectionId: UUID, databases: [String]) { + for database in Set(databases) { + notificationCenter.post( + name: .tableHealthHistoryDidChange, + object: self, + userInfo: TableHealthHistoryChange.userInfo(connectionId: connectionId, database: database) + ) + } + } + + private static func logFailure(_ error: any Error, connectionId: UUID) { + logger.error( + "Table Health history for \(connectionId, privacy: .public) was left unchanged: \(error.publicLogShape, privacy: .public)" + ) + } +} diff --git a/apple/TablePro/Core/Storage/TableScopedSettingsStore.swift b/apple/TablePro/Core/Storage/TableScopedSettingsStore.swift index dc73eea2fe..470982998a 100644 --- a/apple/TablePro/Core/Storage/TableScopedSettingsStore.swift +++ b/apple/TablePro/Core/Storage/TableScopedSettingsStore.swift @@ -16,19 +16,14 @@ internal protocol TableScopedSettingsStore: AnyObject { toSchema: String? ) - /// Forgets one table's saved settings, because the table is gone. func dropTable(_ scope: TableScope) - /// Forgets the saved settings of every table inside a dropped database or schema. - /// - /// A prefix sweep rather than a list of tables, for the reason `renameContainer` takes one: the - /// table list is loaded lazily, so a table nobody opened this session still has settings on - /// disk and could never be named here. A nil schema means the whole database. + // A prefix sweep rather than a list of tables: the table list is loaded lazily, so a table nobody opened this + // session still has settings on disk. A nil schema means the whole database. func dropContainer(connectionId: UUID, database: String, schema: String?) - /// Forgets everything these connections saved. `leavesTombstones` is false when another device - /// did the deleting: a synced store must not mark its records deleted there, or it pushes the - /// sender's own deletion back at it. + // leavesTombstones is false when another device did the deleting: a synced store that marks its records deleted + // there pushes the sender's own deletion back at it. func purgeConnections(_ connectionIds: Set, leavesTombstones: Bool) } @@ -43,7 +38,8 @@ internal enum TableScopedSettingsRegistry { ForeignKeyLabelColumnStore.shared, ImportColumnMappingStore.shared, RowGenerationSettingsStore.shared, - TableFolderStorage.shared + TableFolderStorage.shared, + TableHealthHistoryStorage.shared ] } } diff --git a/apple/TablePro/Core/TableHealth/MySQLTableHealthProvider.swift b/apple/TablePro/Core/TableHealth/MySQLTableHealthProvider.swift index 9669688813..dc997cea60 100644 --- a/apple/TablePro/Core/TableHealth/MySQLTableHealthProvider.swift +++ b/apple/TablePro/Core/TableHealth/MySQLTableHealthProvider.swift @@ -3,7 +3,7 @@ import Foundation // InnoDB indexes a foreign key's columns itself, so there is no unindexed foreign key check. // scripts/probes/check-table-health-catalogs.sh runs these literals against real servers: keep them free of interpolation. struct MySQLTableHealthProvider: TableHealthProvider { - let checks: [TableHealthCheckKind] = [.unusedIndex, .redundantIndex, .missingPrimaryKey] + let checks: [TableHealthCheckKind] = [.unusedIndex, .redundantIndex, .missingPrimaryKey, .nearLimit, .reclaimableSpace] static let performanceSchemaQuery = "SELECT @@GLOBAL.performance_schema AS performance_schema" @@ -96,8 +96,96 @@ struct MySQLTableHealthProvider: TableHealthProvider { static let versionQuery = "SELECT VERSION() AS version" + // PROCESS gates every InnoDB table in information_schema. This one exists on MySQL and MariaDB and holds a + // row per buffer pool, so it answers for the tablespace tables the reclaimable space check reads. + static let processPrivilegeQuery = "SELECT COUNT(*) AS buffer_pools FROM information_schema.INNODB_BUFFER_POOL_STATS" + + // The limits sys.schema_auto_increment_columns uses; AUTO_INCREMENT is the next value the table hands out. + // The 75% threshold compares integers, since 100 * AUTO_INCREMENT overflows BIGINT UNSIGNED near a bigint limit. + // A FLOAT or DOUBLE column has no limit here and drops out. + static let autoIncrementQuery = """ + SELECT r.table_name, r.column_name, r.data_type, r.column_type, r.is_nullable, r.column_comment, r.extra, + r.auto_increment, r.max_value, r.table_bytes + FROM ( + SELECT t.TABLE_NAME AS table_name, + c.COLUMN_NAME AS column_name, + c.DATA_TYPE AS data_type, + c.COLUMN_TYPE AS column_type, + c.IS_NULLABLE AS is_nullable, + c.COLUMN_COMMENT AS column_comment, + c.EXTRA AS extra, + t.AUTO_INCREMENT AS auto_increment, + (CASE c.DATA_TYPE + WHEN 'tinyint' THEN 255 + WHEN 'smallint' THEN 65535 + WHEN 'mediumint' THEN 16777215 + WHEN 'int' THEN 4294967295 + WHEN 'bigint' THEN 18446744073709551615 + END >> IF(LOCATE('unsigned', c.COLUMN_TYPE) > 0, 0, 1)) AS max_value, + t.DATA_LENGTH + t.INDEX_LENGTH AS table_bytes + FROM information_schema.TABLES t + JOIN information_schema.COLUMNS c + ON c.TABLE_SCHEMA = t.TABLE_SCHEMA AND c.TABLE_NAME = t.TABLE_NAME + WHERE t.TABLE_SCHEMA = DATABASE() + AND t.TABLE_TYPE IN ('BASE TABLE', 'SYSTEM VERSIONED') + AND c.EXTRA LIKE '%auto_increment%' + AND t.AUTO_INCREMENT IS NOT NULL + ) r + WHERE r.auto_increment >= r.max_value - r.max_value DIV 4 + ORDER BY r.auto_increment / r.max_value DESC, r.table_name + """ + + // A table in a shared tablespace reports the free space of the whole tablespace, so InnoDB counts only with a + // file of its own. INNODB_TABLES encodes special characters in a name (`@0020`), so such a table drops out + // rather than being judged by another table's tablespace. + static let reclaimableSpaceQuery = """ + SELECT t.TABLE_NAME AS table_name, + t.ENGINE AS engine, + t.TABLE_ROWS AS estimated_rows, + t.DATA_LENGTH + t.INDEX_LENGTH AS table_bytes, + t.DATA_FREE AS free_bytes + FROM information_schema.TABLES t + WHERE t.TABLE_SCHEMA = DATABASE() + AND t.TABLE_TYPE IN ('BASE TABLE', 'SYSTEM VERSIONED') + AND t.DATA_FREE >= 104857600 + AND t.DATA_FREE * 10 >= t.DATA_LENGTH + t.INDEX_LENGTH + t.DATA_FREE + AND (t.ENGINE IN ('MyISAM', 'Aria') + OR (t.ENGINE = 'InnoDB' AND EXISTS ( + SELECT 1 + FROM information_schema.INNODB_TABLES it + JOIN information_schema.INNODB_TABLESPACES ts ON ts.SPACE = it.SPACE + WHERE ts.SPACE_TYPE = 'Single' + AND (it.NAME = CONCAT(t.TABLE_SCHEMA, '/', t.TABLE_NAME) + OR LEFT(it.NAME, CHAR_LENGTH(t.TABLE_SCHEMA) + CHAR_LENGTH(t.TABLE_NAME) + 4) + = CONCAT(t.TABLE_SCHEMA, '/', t.TABLE_NAME, '#p#'))))) + ORDER BY t.DATA_FREE DESC, t.TABLE_NAME + """ + + // MariaDB has no INNODB_TABLES and no general tablespaces: space 0 is the system tablespace. + static let mariaDBReclaimableSpaceQuery = """ + SELECT t.TABLE_NAME AS table_name, + t.ENGINE AS engine, + t.TABLE_ROWS AS estimated_rows, + t.DATA_LENGTH + t.INDEX_LENGTH AS table_bytes, + t.DATA_FREE AS free_bytes + FROM information_schema.TABLES t + WHERE t.TABLE_SCHEMA = DATABASE() + AND t.TABLE_TYPE IN ('BASE TABLE', 'SYSTEM VERSIONED') + AND t.DATA_FREE >= 104857600 + AND t.DATA_FREE * 10 >= t.DATA_LENGTH + t.INDEX_LENGTH + t.DATA_FREE + AND (t.ENGINE IN ('MyISAM', 'Aria') + OR (t.ENGINE = 'InnoDB' AND EXISTS ( + SELECT 1 + FROM information_schema.INNODB_SYS_TABLES it + WHERE it.SPACE <> 0 + AND (it.NAME = CONCAT(t.TABLE_SCHEMA, '/', t.TABLE_NAME) + OR LEFT(it.NAME, CHAR_LENGTH(t.TABLE_SCHEMA) + CHAR_LENGTH(t.TABLE_NAME) + 4) + = CONCAT(t.TABLE_SCHEMA, '/', t.TABLE_NAME, '#P#'))))) + ORDER BY t.DATA_FREE DESC, t.TABLE_NAME + """ + var statisticsQueries: [String] { - [Self.performanceSchemaQuery, Self.uptimeQuery, Self.usageCountingQuery, Self.versionQuery] + [Self.performanceSchemaQuery, Self.uptimeQuery, Self.usageCountingQuery, Self.versionQuery, Self.processPrivilegeQuery] } func check(_ kind: TableHealthCheckKind) -> TableHealthCheck { @@ -112,7 +200,17 @@ struct MySQLTableHealthProvider: TableHealthProvider { )) case .missingPrimaryKey: return TableHealthCheck(kind: kind, summary: String(localized: "Tables with no primary key.")) - case .bloat, .unindexedForeignKey, .invalidIndex, .nearLimit, .transactionIdAge, .staleStatistics, .reclaimableSpace: + case .nearLimit: + return TableHealthCheck( + kind: kind, + title: String(localized: "AUTO_INCREMENT Near Its Limit"), + summary: String(localized: "AUTO_INCREMENT columns whose next value has reached 75% of the largest value their type holds.") + ) + case .reclaimableSpace: + return TableHealthCheck(kind: kind, summary: String( + localized: "Tables stored in their own files whose free space is at least 100 MiB and at least 10% of their size on disk." + )) + case .bloat, .unindexedForeignKey, .invalidIndex, .transactionIdAge, .staleStatistics: return TableHealthCheck(kind: kind, summary: "") } } @@ -122,7 +220,10 @@ struct MySQLTableHealthProvider: TableHealthProvider { case .unusedIndex: return [Self.unusedIndexQuery, Self.indexQuery, Self.foreignKeyQuery, Self.tableRowsQuery] case .redundantIndex: return [Self.indexQuery] case .missingPrimaryKey: return [Self.missingPrimaryKeyQuery] - case .bloat, .unindexedForeignKey, .invalidIndex, .nearLimit, .transactionIdAge, .staleStatistics, .reclaimableSpace: + case .nearLimit: return [Self.autoIncrementQuery, Self.foreignKeyQuery] + case .reclaimableSpace: + return [statistics.isMariaDB ? Self.mariaDBReclaimableSpaceQuery : Self.reclaimableSpaceQuery] + case .bloat, .unindexedForeignKey, .invalidIndex, .transactionIdAge, .staleStatistics: return [] } } @@ -138,17 +239,31 @@ struct MySQLTableHealthProvider: TableHealthProvider { statistics.serverVersion = Self.serverVersion(from: version) statistics.isMariaDB = version.range(of: "MariaDB", options: .caseInsensitive) != nil } + if !statistics.isMariaDB, let version = statistics.serverVersion, version < 80_000 { + statistics.skippedChecks[.reclaimableSpace] = String(localized: "Needs MySQL 8.0 or later.") + } else if (value(4, "buffer_pools").flatMap { Int($0) } ?? 0) == 0 { + statistics.skippedChecks[.reclaimableSpace] = String( + localized: "Needs the PROCESS privilege to tell a table in its own file from one in a shared tablespace." + ) + } if let reason = Self.unusedSkipReason(value) { statistics.skippedChecks[.unusedIndex] = reason - return statistics - } - if let uptime = value(1, "value").flatMap(Double.init) { + } else if let uptime = value(1, "value").flatMap(Double.init) { let start = TableHealthFormatting.date(now.addingTimeInterval(-uptime)) statistics.notes.append(String(format: String(localized: "Index usage counts since the server started at %@."), start)) } return statistics } + // MySQL 8 serves AUTO_INCREMENT and the sizes in information_schema.TABLES from a cache. Changing the expiry + // would change a session the app pools, so the check says so instead. + private static func statisticsCacheNote(_ statistics: TableHealthStatistics) -> String? { + guard !statistics.isMariaDB, let version = statistics.serverVersion, version >= 80_000 else { return nil } + return String( + localized: "MySQL caches the values this check reads for information_schema_stats_expiry seconds, one day by default. ANALYZE TABLE refreshes a table's values." + ) + } + private static func unusedSkipReason(_ value: (Int, String) -> String?) -> String? { if let enabled = value(0, "performance_schema"), enabled != "1", enabled.uppercased() != "ON" { return String(localized: "Performance Schema is off, so the server records no index reads.") @@ -204,7 +319,15 @@ struct MySQLTableHealthProvider: TableHealthProvider { case .missingPrimaryKey: guard let result = results.first else { return ([], nil) } return (TableHealthRow.rows(of: result).map { missingPrimaryKeyFinding($0, context) }, nil) - case .bloat, .unindexedForeignKey, .invalidIndex, .nearLimit, .transactionIdAge, .staleStatistics, .reclaimableSpace: + case .nearLimit: + guard results.count == 2 else { return ([], nil) } + let keys = Self.foreignKeysByColumn(TableHealthRow.rows(of: results[1])) + let findings = TableHealthRow.rows(of: results[0]).compactMap { nearLimitFinding($0, foreignKeys: keys, context) } + return (findings, Self.statisticsCacheNote(statistics)) + case .reclaimableSpace: + guard let result = results.first else { return ([], nil) } + return (TableHealthRow.rows(of: result).map { reclaimableSpaceFinding($0, context) }, Self.statisticsCacheNote(statistics)) + case .bloat, .unindexedForeignKey, .invalidIndex, .transactionIdAge, .staleStatistics: return ([], nil) } } @@ -281,6 +404,126 @@ struct MySQLTableHealthProvider: TableHealthProvider { private func dropIndexSQL(table: String, index: String, _ context: TableHealthQueryContext) -> String { "ALTER TABLE \(context.qualified(context.database, table)) DROP INDEX \(context.quote(index));" } + + private func nearLimitFinding( + _ row: TableHealthRow, + foreignKeys: [String: [String]], + _ context: TableHealthQueryContext + ) -> TableHealthFinding? { + guard let next = row.uint64("auto_increment"), let limit = row.uint64("max_value"), limit > 0 else { return nil } + let table = row["table_name"], column = row["column_name"], columnType = row["column_type"] + let used = TableHealthMetric( + id: "used_percent", label: String(localized: "Share Used"), value: .percent(Double(next) / Double(limit)) + ) + var detail = String( + format: String(localized: "%1$@ is at %2$@ of the largest value %3$@ holds."), column, used.formattedValue, columnType + ) + var fixSQL: String? + if let type = Self.widenedType(dataType: row["data_type"], columnType: columnType) { + fixSQL = "ALTER TABLE \(context.qualified(context.database, table)) MODIFY COLUMN \(context.quote(column)) " + + Self.columnDefinition(type: type, row, context) + ";" + if row["data_type"].lowercased() == "bigint" { + detail += " " + String(localized: "Run the fix only if the column holds no negative values.") + } + if let keys = foreignKeys[Self.columnKey(table: table, column: column)] { + detail += " " + String( + format: String( + localized: "Foreign keys use this column (%@): drop them, give the column at the other end of each the same type, then add them again." + ), + keys.joined(separator: ", ") + ) + } + } else { + detail += " " + String(localized: "No integer type holds larger values.") + } + return TableHealthFinding( + kind: .nearLimit, schema: nil, table: table, objectName: column, detail: detail, + sizeBytes: row.int64("table_bytes"), + metrics: [ + TableHealthMetric(id: "auto_increment", label: String(localized: "Next Value"), value: .bigCount(next)), + TableHealthMetric(id: "max_value", label: String(localized: "Largest Value"), value: .bigCount(limit)), + used, + TableHealthMetric(id: "column_type", label: String(localized: "Column Type"), value: .text(columnType)) + ], + fixSQL: fixSQL + ) + } + + // A smaller integer widens to BIGINT with its sign kept, and ZEROFILL keeps the width it pads to. A signed + // bigint can only give up its sign; an unsigned one is the widest integer there is. + static func widenedType(dataType: String, columnType: String) -> String? { + let words = columnType.lowercased().split(whereSeparator: \.isWhitespace) + let isUnsigned = words.contains("unsigned"), isZerofill = words.contains("zerofill") + if dataType.lowercased() == "bigint" { return isUnsigned ? nil : "BIGINT UNSIGNED" } + var type = "BIGINT" + if isZerofill, let declared = words.first, let width = declared.firstIndex(of: "(") { + type += declared[width...] + } + if isUnsigned { type += " UNSIGNED" } + if isZerofill { type += " ZEROFILL" } + return type + } + + // MODIFY replaces the whole definition, so what the column has besides its type is stated again. + private static func columnDefinition(type: String, _ row: TableHealthRow, _ context: TableHealthQueryContext) -> String { + var parts = [type, row["is_nullable"].uppercased() == "YES" ? "NULL" : "NOT NULL", "AUTO_INCREMENT"] + let extra = row["extra"].split { !$0.isLetter && $0 != "_" } + if extra.contains(where: { $0.uppercased() == "INVISIBLE" }) { + parts.append("INVISIBLE") + } + let comment = row["column_comment"] + if !comment.isEmpty { + parts.append("COMMENT " + context.literal(comment)) + } + return parts.joined(separator: " ") + } + + // MySQL refuses to change the type of a column a foreign key uses at either end, even with the checks off. + private static func foreignKeysByColumn(_ rows: [TableHealthRow]) -> [String: [String]] { + var keys: [String: Set] = [:] + for row in rows { + let isLocal = row["is_local"] == "1" + let owner = isLocal ? row["table_name"] : row["table_schema"] + "." + row["table_name"] + let key = String(format: String(localized: "%1$@ on %2$@"), row["constraint_name"], owner) + if isLocal { + keys[columnKey(table: row["table_name"], column: row["column_name"]), default: []].insert(key) + } + if row["references_here"] == "1" { + keys[columnKey(table: row["referenced_table_name"], column: row["referenced_column_name"]), default: []] + .insert(key) + } + } + return keys.mapValues { $0.sorted() } + } + + // Column names never differ only in case; table names can. + private static func columnKey(table: String, column: String) -> String { + table + "\u{1F}" + column.lowercased() + } + + private func reclaimableSpaceFinding(_ row: TableHealthRow, _ context: TableHealthQueryContext) -> TableHealthFinding { + let table = row["table_name"] + let tableBytes = row.int64("table_bytes"), freeBytes = row.int64("free_bytes") ?? 0 + let onDisk = Double(tableBytes ?? 0) + Double(freeBytes) + let free = TableHealthMetric(id: "free_bytes", label: String(localized: "Reclaimable"), value: .bytes(freeBytes)) + let share = TableHealthMetric( + id: "free_percent", label: String(localized: "Reclaimable Share"), value: .percent(onDisk > 0 ? Double(freeBytes) / onDisk : 0) + ) + var metrics = [free, share] + if let rows = row.int64("estimated_rows") { + metrics.append(.estimatedRows(rows)) + } + metrics.append(TableHealthMetric(id: "engine", label: String(localized: "Engine"), value: .text(row["engine"]))) + return TableHealthFinding( + kind: .reclaimableSpace, schema: nil, table: table, objectName: nil, + detail: String( + format: String(localized: "%1$@ reclaimable, %2$@ of the table's size on disk."), free.formattedValue, share.formattedValue + ), + sizeBytes: tableBytes, + metrics: metrics, + fixSQL: "OPTIMIZE TABLE \(context.qualified(context.database, table));" + ) + } } // The indexes of one table, from information_schema.STATISTICS rows ordered by table, index and position. diff --git a/apple/TablePro/Core/TableHealth/PostgreSQLIndexShape.swift b/apple/TablePro/Core/TableHealth/PostgreSQLIndexShape.swift new file mode 100644 index 0000000000..d2ec75e771 --- /dev/null +++ b/apple/TablePro/Core/TableHealth/PostgreSQLIndexShape.swift @@ -0,0 +1,139 @@ +import Foundation + +// The valid indexes of one table, from PostgreSQLTableHealthProvider.redundantIndexQuery: a row per index column, +// key columns first, then the INCLUDE columns. +struct PostgreSQLIndexShape { + // A key column or expression as the server deparses it, with what decides which lookups it serves. + struct KeyPart: Equatable { + let column: String + let operatorClass: String + let collation: String + let sortOption: String + } + + struct Index { + let name: String + let accessMethod: String + let isUnique: Bool + let isPrimary: Bool + let isImmediate: Bool + let nullsNotDistinct: Bool + // Backs a constraint, belongs to a partitioned index or is the replica identity: dropping it alone fails or + // changes what the table does. + let isProtected: Bool + let predicate: String? + let sizeBytes: Int64? + var definition: String? + var keys: [KeyPart] + var includes: [String] + + var columns: [String] { keys.map(\.column) + includes } + + // Only a B-tree serves a lookup on its leading keys, so another method is redundant only to an equal index. + // A unique index is redundant only to one enforcing the same rule. Of two indexes that cover each other, a + // protected one stays, else the earlier name. + func isRedundant(to other: Index) -> Bool { + guard other.name != name, !isProtected, !isPrimary, + other.accessMethod == accessMethod, other.predicate == predicate, + keys.count <= other.keys.count, + accessMethod == "btree" || keys.count == other.keys.count, + Array(other.keys.prefix(keys.count)) == keys, + Set(includes).isSubset(of: other.columns) + else { return false } + if isUnique { + guard other.isUnique, keys.count == other.keys.count, + isImmediate == other.isImmediate, nullsNotDistinct == other.nullsNotDistinct + else { return false } + } + let mutual = keys.count == other.keys.count && isUnique == other.isUnique + && Set(other.includes).isSubset(of: columns) + return !mutual || other.isProtected || other.isPrimary || other.name < name + } + + // Which of several covering indexes a finding names: the one least likely to be dropped itself. + func outranks(_ other: Index) -> Bool { + if isPrimary != other.isPrimary { return isPrimary } + if isProtected != other.isProtected { return isProtected } + if isUnique != other.isUnique { return isUnique } + if keys.count != other.keys.count { return keys.count > other.keys.count } + if columns.count != other.columns.count { return columns.count > other.columns.count } + return name < other.name + } + } + + let schema: String + let table: String + let isPartitioned: Bool + var indexes: [Index] + + var redundantIndexes: [(index: Index, coveredBy: Index)] { + indexes.compactMap { index in + indexes.filter { index.isRedundant(to: $0) } + .min { $0.outranks($1) } + .map { (index: index, coveredBy: $0) } + } + } + + // Grouped by exact names and ordered by position, so the grouping never depends on the row order. + static func tables(from rows: [TableHealthRow]) -> [PostgreSQLIndexShape] { + var tables: [PostgreSQLIndexShape] = [] + var tablePositions: [[String]: Int] = [:] + var parts: [[String]: [(position: Int, keyCount: Int, row: TableHealthRow)]] = [:] + for row in rows { + let tableKey = [row["schema_name"], row["table_name"]] + let tablePosition: Int + if let known = tablePositions[tableKey] { + tablePosition = known + } else { + tablePosition = tables.count + tablePositions[tableKey] = tablePosition + tables.append(PostgreSQLIndexShape( + schema: row["schema_name"], table: row["table_name"], isPartitioned: row["table_kind"] == "p", indexes: [] + )) + } + let indexName = row["index_name"] + if !tables[tablePosition].indexes.contains(where: { $0.name == indexName }) { + tables[tablePosition].indexes.append(Index( + name: indexName, + accessMethod: row["access_method"], + isUnique: row.flag("is_unique") == true, + isPrimary: row.flag("is_primary") == true, + isImmediate: row.flag("is_immediate") != false, + nullsNotDistinct: row.flag("nulls_not_distinct") == true, + isProtected: row.flag("is_protected") == true, + predicate: row.text("predicate"), + sizeBytes: row.int64("index_bytes"), + definition: nil, + keys: [], + includes: [] + )) + } + parts[tableKey + [indexName], default: []].append(( + Int(row.int64("position") ?? 0), Int(row.int64("key_count") ?? 0), row + )) + } + for tablePosition in tables.indices { + let tableKey = [tables[tablePosition].schema, tables[tablePosition].table] + for indexPosition in tables[tablePosition].indexes.indices { + let indexParts = (parts[tableKey + [tables[tablePosition].indexes[indexPosition].name]] ?? []) + .sorted { $0.position < $1.position } + var index = tables[tablePosition].indexes[indexPosition] + for part in indexParts { + if index.definition == nil { index.definition = part.row.text("index_definition") } + if part.position <= part.keyCount { + index.keys.append(KeyPart( + column: part.row["column_text"], + operatorClass: part.row["opclass"], + collation: part.row["collation"], + sortOption: part.row["sort_option"] + )) + } else { + index.includes.append(part.row["column_text"]) + } + } + tables[tablePosition].indexes[indexPosition] = index + } + } + return tables + } +} diff --git a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift index 88c51caead..cf71771091 100644 --- a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift +++ b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift @@ -4,7 +4,10 @@ import Foundation // Every query leaves out what an extension owns, by relation and by schema: TimescaleDB's chunk tables are not extension // members themselves, but their schema is, and a fix there would change the extension's own objects. struct PostgreSQLTableHealthProvider: TableHealthProvider { - let checks: [TableHealthCheckKind] = [.bloat, .unusedIndex, .missingPrimaryKey, .unindexedForeignKey] + let checks: [TableHealthCheckKind] = [ + .bloat, .unusedIndex, .redundantIndex, .invalidIndex, .missingPrimaryKey, .unindexedForeignKey, + .nearLimit, .transactionIdAge, .staleStatistics + ] // The checks that read activity counters, which a standby keeps for its own queries only. private static let counterChecks: [TableHealthCheckKind] = [.bloat, .unusedIndex, .staleStatistics] @@ -175,6 +178,323 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { ORDER BY n.nspname, c.relname, con.conname, k.position """ + // One row per index column; PostgreSQLIndexShape compares them. A key part is the deparsed column or expression + // with its operator class, collation and sort flags. indnkeyatts (11+) and indnullsnotdistinct (15+) go through + // row_to_json for older servers. + static let redundantIndexQuery = """ + SELECT n.nspname AS schema_name, + t.relname AS table_name, + t.relkind AS table_kind, + ic.relname AS index_name, + am.amname AS access_method, + i.indisunique AS is_unique, + i.indisprimary AS is_primary, + i.indimmediate AS is_immediate, + COALESCE(pg_catalog.row_to_json(i) OPERATOR(pg_catalog.->>) 'indnullsnotdistinct'::pg_catalog.text, 'false') AS nulls_not_distinct, + (i.indisreplident + OR EXISTS (SELECT 1 FROM pg_catalog.pg_constraint con WHERE con.conindid = i.indexrelid) + OR EXISTS (SELECT 1 FROM pg_catalog.pg_inherits h WHERE h.inhrelid = i.indexrelid)) AS is_protected, + pg_catalog.pg_get_expr(i.indpred, i.indrelid) AS predicate, + pg_catalog.pg_relation_size(i.indexrelid) AS index_bytes, + CASE WHEN p.position = 1 THEN pg_catalog.pg_get_indexdef(i.indexrelid) END AS index_definition, + k.key_count, + p.position, + pg_catalog.pg_get_indexdef(i.indexrelid, p.position, true) AS column_text, + CASE WHEN p.position <= k.key_count THEN (i.indclass::pg_catalog.oid[])[p.position - 1] END AS opclass, + CASE WHEN p.position <= k.key_count THEN (i.indcollation::pg_catalog.oid[])[p.position - 1] END AS collation, + CASE WHEN p.position <= k.key_count THEN (i.indoption::pg_catalog.int2[])[p.position - 1] END AS sort_option + FROM pg_catalog.pg_index i + JOIN pg_catalog.pg_class ic ON ic.oid = i.indexrelid + JOIN pg_catalog.pg_class t ON t.oid = i.indrelid + JOIN pg_catalog.pg_namespace n ON n.oid = t.relnamespace + JOIN pg_catalog.pg_am am ON am.oid = ic.relam + CROSS JOIN LATERAL ( + SELECT COALESCE((pg_catalog.row_to_json(i) OPERATOR(pg_catalog.->>) 'indnkeyatts'::pg_catalog.text)::pg_catalog.int4, i.indnatts) AS key_count + ) k + CROSS JOIN LATERAL pg_catalog.generate_series(1, i.indnatts) AS p(position) + WHERE i.indisvalid + AND t.relkind IN ('r', 'p', 'm') + AND n.nspname <> 'information_schema' + AND pg_catalog.left(n.nspname, 3) <> 'pg_' + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND x.objid = t.oid + AND x.deptype = 'e' + ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_namespace'::pg_catalog.regclass + AND x.objid = t.relnamespace + AND x.deptype = 'e' + ) + ORDER BY n.nspname, t.relname, ic.relname, p.position + """ + + // A build still running holds SHARE UPDATE EXCLUSIVE on its table for the whole build, which any role can see in + // pg_locks. An index built ON ONLY a partitioned table (relkind I) stays invalid by design until every partition + // attaches one, so it is not a leftover. + static let invalidIndexQuery = """ + SELECT n.nspname AS schema_name, + t.relname AS table_name, + c.relname AS index_name, + i.indisunique AS is_unique, + i.indisready AS is_ready, + pg_catalog.pg_relation_size(i.indexrelid) AS index_bytes, + pg_catalog.pg_get_indexdef(i.indexrelid) AS index_definition + FROM pg_catalog.pg_index i + JOIN pg_catalog.pg_class c ON c.oid = i.indexrelid + JOIN pg_catalog.pg_class t ON t.oid = i.indrelid + JOIN pg_catalog.pg_namespace n ON n.oid = t.relnamespace + WHERE NOT i.indisvalid + AND c.relkind = 'i' + AND pg_catalog.left(n.nspname, 3) <> 'pg_' + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_locks l + WHERE l.locktype = 'relation' + AND l.database = (SELECT d.oid FROM pg_catalog.pg_database d WHERE d.datname = pg_catalog.current_database()) + AND l.relation = i.indrelid + AND l.mode = 'ShareUpdateExclusiveLock' + AND l.granted + ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND x.objid = t.oid + AND x.deptype = 'e' + ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_namespace'::pg_catalog.regclass + AND x.objid = t.relnamespace + AND x.deptype = 'e' + ) + ORDER BY n.nspname, t.relname, c.relname + """ + + // pg_sequence is 10+. The range is the narrower of the sequence's own limits and the integer column it feeds + // through OWNED BY, an identity or a nextval default; the narrowest such column wins. last_value reads as + // pg_sequences reads it: NULL without SELECT or USAGE, and for an unlogged sequence on a standby, where reading + // one fails before 17. A cycling sequence wraps by design. Unreadable sequences come back for the check's note. + static let nearLimitQuery = """ + SELECT r.sequence_schema, r.sequence_name, r.sequence_type, r.increment_by, r.sequence_min, r.sequence_max, + r.last_value, r.lower_bound, r.upper_bound, r.used_fraction, r.readable, + r.table_schema, r.table_name, r.column_name, r.column_type, r.column_min, r.column_max, r.ownership, + r.table_bytes, r.referencing_columns + FROM ( + SELECT sn.nspname AS sequence_schema, + s.relname AS sequence_name, + pg_catalog.format_type(q.seqtypid, NULL) AS sequence_type, + q.seqincrement AS increment_by, + q.seqmin AS sequence_min, + q.seqmax AS sequence_max, + v.last_value, + b.lower_bound, + b.upper_bound, + CASE WHEN q.seqincrement > 0 + THEN v.last_value::pg_catalog.numeric - b.lower_bound::pg_catalog.numeric + ELSE b.upper_bound::pg_catalog.numeric - v.last_value::pg_catalog.numeric + END / NULLIF(b.upper_bound::pg_catalog.numeric - b.lower_bound::pg_catalog.numeric, 0::pg_catalog.numeric) + AS used_fraction, + v.readable, + col.table_schema, + col.table_name, + col.column_name, + col.column_type, + col.column_min, + col.column_max, + col.ownership, + col.table_bytes, + col.referencing_columns + FROM pg_catalog.pg_sequence q + JOIN pg_catalog.pg_class s ON s.oid = q.seqrelid + JOIN pg_catalog.pg_namespace sn ON sn.oid = s.relnamespace + CROSS JOIN LATERAL ( + SELECT pg_catalog.has_sequence_privilege(s.oid, 'SELECT,USAGE') AS readable, + CASE WHEN pg_catalog.has_sequence_privilege(s.oid, 'SELECT,USAGE') + AND (s.relpersistence = 'p' OR (s.relpersistence = 'u' AND NOT pg_catalog.pg_is_in_recovery())) + THEN pg_catalog.pg_sequence_last_value(s.oid) + END AS last_value + ) v + LEFT JOIN LATERAL ( + SELECT tn.nspname AS table_schema, + t.relname AS table_name, + a.attname AS column_name, + pg_catalog.format_type(a.atttypid, a.atttypmod) AS column_type, + f.ownership, + pg_catalog.pg_table_size(t.oid) AS table_bytes, + CASE WHEN ty.base OPERATOR(pg_catalog.=) 'pg_catalog.int2'::pg_catalog.regtype THEN -32768::pg_catalog.int8 + WHEN ty.base OPERATOR(pg_catalog.=) 'pg_catalog.int4'::pg_catalog.regtype THEN -2147483648::pg_catalog.int8 + END AS column_min, + CASE WHEN ty.base OPERATOR(pg_catalog.=) 'pg_catalog.int2'::pg_catalog.regtype THEN 32767::pg_catalog.int8 + WHEN ty.base OPERATOR(pg_catalog.=) 'pg_catalog.int4'::pg_catalog.regtype THEN 2147483647::pg_catalog.int8 + END AS column_max, + (SELECT pg_catalog.string_agg(pg_catalog.concat_ws('.', rn.nspname, rc.relname, ra.attname), ', ' + ORDER BY rn.nspname, rc.relname, ra.attname) + FROM pg_catalog.pg_constraint fk + JOIN pg_catalog.pg_class rc ON rc.oid = fk.conrelid + JOIN pg_catalog.pg_namespace rn ON rn.oid = rc.relnamespace + CROSS JOIN LATERAL ROWS FROM (pg_catalog.unnest(fk.conkey), pg_catalog.unnest(fk.confkey)) AS fkc(attnum, refnum) + JOIN pg_catalog.pg_attribute ra ON ra.attrelid = fk.conrelid AND ra.attnum = fkc.attnum + WHERE fk.contype = 'f' + AND fk.confrelid = f.table_oid + AND fkc.refnum = f.column_number + AND COALESCE(pg_catalog.row_to_json(fk) OPERATOR(pg_catalog.->>) 'conparentid'::pg_catalog.text, '0') = '0' + AND (ra.atttypid OPERATOR(pg_catalog.=) 'pg_catalog.int2'::pg_catalog.regtype + OR ra.atttypid OPERATOR(pg_catalog.=) 'pg_catalog.int4'::pg_catalog.regtype)) AS referencing_columns + FROM ( + SELECT d.refobjid AS table_oid, + d.refobjsubid AS column_number, + CASE d.deptype WHEN 'i' THEN 'identity' ELSE 'owned' END AS ownership + FROM pg_catalog.pg_depend d + WHERE d.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND d.objid = s.oid + AND d.refclassid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND d.refobjsubid > 0 + AND d.deptype IN ('a', 'i') + UNION + SELECT ad.adrelid, ad.adnum, 'default' + FROM pg_catalog.pg_depend d + JOIN pg_catalog.pg_attrdef ad ON ad.oid = d.objid + WHERE d.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_attrdef'::pg_catalog.regclass + AND d.refclassid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND d.refobjid = s.oid + ) f + JOIN pg_catalog.pg_class t ON t.oid = f.table_oid + JOIN pg_catalog.pg_namespace tn ON tn.oid = t.relnamespace + JOIN pg_catalog.pg_attribute a ON a.attrelid = f.table_oid AND a.attnum = f.column_number + JOIN pg_catalog.pg_type ct ON ct.oid = a.atttypid + CROSS JOIN LATERAL (SELECT CASE WHEN ct.typtype = 'd' THEN ct.typbasetype ELSE ct.oid END AS base) ty + WHERE NOT a.attisdropped + ORDER BY column_max NULLS LAST, f.ownership DESC + LIMIT 1 + ) col ON true + CROSS JOIN LATERAL ( + SELECT GREATEST(q.seqmin, COALESCE(col.column_min, q.seqmin)) AS lower_bound, + LEAST(q.seqmax, COALESCE(col.column_max, q.seqmax)) AS upper_bound + ) b + WHERE NOT q.seqcycle + AND pg_catalog.left(sn.nspname, 3) <> 'pg_' + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND x.objid = s.oid + AND x.deptype = 'e' + ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_namespace'::pg_catalog.regclass + AND x.objid = s.relnamespace + AND x.deptype = 'e' + ) + ) r + WHERE NOT r.readable OR r.used_fraction >= 0.75 + ORDER BY r.used_fraction DESC NULLS LAST, r.sequence_schema, r.sequence_name + """ + + // Past its effective autovacuum_freeze_max_age a table has already had a forced freeze, so one still above it + // is not getting frozen. A table's own setting counts only when lower: autovacuum ignores a larger one. The TOAST + // table can lag its heap. A partitioned table has no storage and reads as 2^31 - 1, so it is left out. + static let transactionIdAgeQuery = """ + SELECT r.schema_name, r.table_name, r.table_xid_age, r.toast_xid_age, r.xid_age, r.freeze_max_age, + r.transactions_left, r.last_vacuum, r.table_bytes + FROM ( + SELECT n.nspname AS schema_name, + c.relname AS table_name, + pg_catalog.age(c.relfrozenxid) AS table_xid_age, + pg_catalog.age(t.relfrozenxid) AS toast_xid_age, + GREATEST(pg_catalog.age(c.relfrozenxid), COALESCE(pg_catalog.age(t.relfrozenxid), 0)) AS xid_age, + f.freeze_max_age, + 2147483648 - GREATEST(pg_catalog.age(c.relfrozenxid), COALESCE(pg_catalog.age(t.relfrozenxid), 0)) AS transactions_left, + pg_catalog.date_part('epoch', GREATEST(s.last_vacuum, s.last_autovacuum)) AS last_vacuum, + pg_catalog.pg_table_size(c.oid) AS table_bytes + FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + LEFT JOIN pg_catalog.pg_class t ON t.oid = c.reltoastrelid + LEFT JOIN pg_catalog.pg_stat_all_tables s ON s.relid = c.oid + CROSS JOIN LATERAL ( + SELECT LEAST( + pg_catalog.current_setting('autovacuum_freeze_max_age')::pg_catalog.int8, + COALESCE((SELECT o.option_value::pg_catalog.int8 + FROM pg_catalog.pg_options_to_table(c.reloptions) o + WHERE o.option_name = 'autovacuum_freeze_max_age'), + pg_catalog.current_setting('autovacuum_freeze_max_age')::pg_catalog.int8) + ) AS freeze_max_age + ) f + WHERE c.relkind IN ('r', 'm') + AND n.nspname <> 'information_schema' + AND pg_catalog.left(n.nspname, 3) <> 'pg_' + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND x.objid = c.oid + AND x.deptype = 'e' + ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_namespace'::pg_catalog.regclass + AND x.objid = c.relnamespace + AND x.deptype = 'e' + ) + ) r + WHERE r.xid_age > r.freeze_max_age + ORDER BY r.xid_age DESC, r.schema_name, r.table_name + """ + + // Autoanalyze's default rule: more rows modified than 10% of reltuples plus 50. reltuples is -1 before the + // first analyze on 14+. Autovacuum never analyzes a partitioned table, and its activity counters stay empty, so + // a parent counts as never analyzed while pg_stats holds no inherited rows for it. + static let staleStatisticsQuery = """ + SELECT n.nspname AS schema_name, + c.relname AS table_name, + c.relkind AS table_kind, + COALESCE(s.n_live_tup, 0) AS live_rows, + COALESCE(s.n_mod_since_analyze, 0) AS modified_rows, + th.analyze_threshold::pg_catalog.int8 AS analyze_threshold, + CASE WHEN c.relkind = 'p' + THEN NOT EXISTS (SELECT 1 FROM pg_catalog.pg_stats st + WHERE st.schemaname = n.nspname AND st.tablename = c.relname AND st.inherited) + ELSE s.last_analyze IS NULL AND s.last_autoanalyze IS NULL + END AS never_analyzed, + pg_catalog.date_part('epoch', GREATEST(s.last_analyze, s.last_autoanalyze)) AS last_analyze, + pg_catalog.pg_table_size(c.oid) AS table_bytes + FROM pg_catalog.pg_class c + JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + LEFT JOIN pg_catalog.pg_stat_user_tables s ON s.relid = c.oid + CROSS JOIN LATERAL ( + SELECT 50::pg_catalog.float8 + + 0.1::pg_catalog.float8 * GREATEST(c.reltuples::pg_catalog.float8, 0::pg_catalog.float8) AS analyze_threshold + ) th + WHERE c.relkind IN ('r', 'm', 'p') + AND n.nspname <> 'information_schema' + AND pg_catalog.left(n.nspname, 3) <> 'pg_' + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND x.objid = c.oid + AND x.deptype = 'e' + ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_namespace'::pg_catalog.regclass + AND x.objid = c.relnamespace + AND x.deptype = 'e' + ) + AND ( + (c.relkind = 'p' + AND pg_catalog.has_table_privilege(c.oid, 'SELECT') + AND EXISTS (SELECT 1 FROM pg_catalog.pg_inherits h + JOIN pg_catalog.pg_stat_user_tables ps ON ps.relid = h.inhrelid + WHERE h.inhparent = c.oid AND ps.n_live_tup > 0) + AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_stats st + WHERE st.schemaname = n.nspname AND st.tablename = c.relname AND st.inherited)) + OR (c.relkind <> 'p' + AND ((s.last_analyze IS NULL AND s.last_autoanalyze IS NULL AND s.n_live_tup > 0) + OR s.n_mod_since_analyze::pg_catalog.float8 > th.analyze_threshold)) + ) + ORDER BY modified_rows DESC, n.nspname, c.relname + """ + var statisticsQueries: [String] { [Self.statisticsQuery] } func check(_ kind: TableHealthCheckKind) -> TableHealthCheck { @@ -195,7 +515,27 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { return TableHealthCheck(kind: kind, summary: String( localized: "Foreign keys whose columns lead no valid, non-partial index, so a delete on the referenced table scans this one." )) - case .redundantIndex, .invalidIndex, .nearLimit, .transactionIdAge, .staleStatistics, .reclaimableSpace: + case .redundantIndex: + return TableHealthCheck(kind: kind, summary: String( + localized: "Indexes whose keys lead another index with the same method, predicate and uniqueness; primary key and constraint indexes stay." + )) + case .invalidIndex: + return TableHealthCheck(kind: kind, summary: String( + localized: "Indexes a failed or cancelled CREATE INDEX CONCURRENTLY or REINDEX CONCURRENTLY left invalid, leaving out builds still running." + )) + case .nearLimit: + return TableHealthCheck(kind: kind, title: String(localized: "Sequences Near Their Limit"), summary: String( + localized: "Sequences that have used at least 75% of what their type, limits and integer column allow, leaving out cycling ones." + )) + case .transactionIdAge: + return TableHealthCheck(kind: kind, summary: String( + localized: "Tables whose oldest unfrozen row is more than autovacuum_freeze_max_age transactions old (200,000,000 unless set lower)." + )) + case .staleStatistics: + return TableHealthCheck(kind: kind, summary: String( + localized: "Tables never analyzed, or with more rows modified since the last ANALYZE than 10% of their rows plus 50." + )) + case .reclaimableSpace: return TableHealthCheck(kind: kind, summary: "") } } @@ -204,10 +544,14 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { switch check { case .bloat: return [Self.bloatQuery] case .unusedIndex: return [Self.unusedIndexQuery] + case .redundantIndex: return [Self.redundantIndexQuery] + case .invalidIndex: return [Self.invalidIndexQuery] case .missingPrimaryKey: return [Self.missingPrimaryKeyQuery] case .unindexedForeignKey: return [Self.unindexedForeignKeyQuery] - case .redundantIndex, .invalidIndex, .nearLimit, .transactionIdAge, .staleStatistics, .reclaimableSpace: - return [] + case .nearLimit: return [Self.nearLimitQuery] + case .transactionIdAge: return [Self.transactionIdAgeQuery] + case .staleStatistics: return [Self.staleStatisticsQuery] + case .reclaimableSpace: return [] } } @@ -220,6 +564,9 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { serverVersion: row.int64("server_version_num").map { Int($0) }, isStandby: row.flag("in_recovery") == true ) + if let version = statistics.serverVersion, version < 100_000, checks.contains(.nearLimit) { + statistics.skippedChecks[.nearLimit] = String(localized: "Needs PostgreSQL 10 or later.") + } let counterChecks = Self.counterChecks.filter(checks.contains) func skipCounterChecks(_ reason: String) { for kind in counterChecks { @@ -262,7 +609,17 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { return (rows.map { missingPrimaryKeyFinding($0, context) }, nil) case .unindexedForeignKey: return (unindexedForeignKeyFindings(rows, context), nil) - case .redundantIndex, .invalidIndex, .nearLimit, .transactionIdAge, .staleStatistics, .reclaimableSpace: + case .redundantIndex: + return (redundantIndexFindings(PostgreSQLIndexShape.tables(from: rows), context), nil) + case .invalidIndex: + return (rows.map { invalidIndexFinding($0, context, serverVersion: statistics.serverVersion) }, nil) + case .nearLimit: + return nearLimitFindings(rows, context) + case .transactionIdAge: + return (rows.map { transactionIdAgeFinding($0, context) }, nil) + case .staleStatistics: + return (rows.map { staleStatisticsFinding($0, context) }, nil) + case .reclaimableSpace: return ([], nil) } } @@ -380,3 +737,325 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { } } } + +extension PostgreSQLTableHealthProvider { + private static func countText(_ row: TableHealthRow, _ name: String) -> String { + row.int64(name)?.formatted() ?? row[name] + } + + private func redundantIndexFindings( + _ tables: [PostgreSQLIndexShape], + _ context: TableHealthQueryContext + ) -> [TableHealthFinding] { + var findings: [TableHealthFinding] = [] + for table in tables { + for redundancy in table.redundantIndexes { + let index = redundancy.index, covering = redundancy.coveredBy + var detail = String(format: String(localized: "%1$@ is covered by %2$@."), index.name, covering.name) + if table.isPartitioned { + detail += " " + String( + localized: "On a partitioned table the drop cannot run concurrently, and it removes the index from every partition." + ) + } + var metrics = [ + TableHealthMetric(id: "covering_index", label: String(localized: "Covering Index"), value: .text(covering.name)) + ] + if let definition = index.definition { + metrics.append(TableHealthMetric( + id: "index_definition", label: String(localized: "Definition"), value: .text(definition) + )) + } + if let definition = covering.definition { + metrics.append(TableHealthMetric( + id: "covering_definition", label: String(localized: "Covering Definition"), value: .text(definition) + )) + } + // PostgreSQL refuses to drop an index on a partitioned table concurrently. + let drop = table.isPartitioned ? "DROP INDEX" : "DROP INDEX CONCURRENTLY" + findings.append(TableHealthFinding( + kind: .redundantIndex, schema: table.schema, table: table.table, objectName: index.name, + detail: detail, sizeBytes: index.sizeBytes, metrics: metrics, + fixSQL: "\(drop) \(context.qualified(table.schema, index.name));" + )) + } + } + return findings + } + + private func invalidIndexFinding( + _ row: TableHealthRow, + _ context: TableHealthQueryContext, + serverVersion: Int? + ) -> TableHealthFinding { + let schema = row["schema_name"], index = row["index_name"] + let definition = row.text("index_definition") + let isUnique = row.flag("is_unique") == true, isReady = row.flag("is_ready") == true + let target = context.qualified(schema, index) + let canReindexConcurrently = (serverVersion ?? 0) >= 120_000 + // REINDEX CONCURRENTLY (12+) builds _ccnew and keeps the old copy as _ccold, adding a digit to + // a name already taken. Either one left invalid is dropped, not rebuilt. + let leftover = index.range(of: "_cc(new|old)[0-9]*$", options: .regularExpression).map { String(index[$0]) } + + let detail: String + let fixSQL: String + if let leftover, serverVersion.map({ $0 >= 120_000 }) ?? true { + detail = leftover.hasPrefix("_ccold") + ? String(format: String( + localized: "%@ is the old copy a REINDEX CONCURRENTLY left behind after the rebuild finished." + ), index) + : String(format: String( + localized: "%@ is the new copy an interrupted REINDEX CONCURRENTLY left behind. Drop it, then run the rebuild again." + ), index) + fixSQL = "DROP INDEX CONCURRENTLY \(target);" + } else { + var sentences = [ + isReady + ? String(format: String( + localized: "%@ is invalid after a failed or cancelled concurrent build. Queries never use it, but every write still updates it." + ), index) + : String(format: String( + localized: "%@ is invalid after a failed or cancelled concurrent build. Queries never use it." + ), index) + ] + if isUnique { + sentences.append(String(localized: "Rebuilding a unique index fails while the table holds duplicate values.")) + } + detail = sentences.joined(separator: " ") + if canReindexConcurrently { + fixSQL = "REINDEX INDEX CONCURRENTLY \(target);" + } else if let create = definition.flatMap(Self.concurrentCreate) { + fixSQL = "DROP INDEX CONCURRENTLY \(target);\n\(create);" + } else { + fixSQL = "REINDEX INDEX \(target);" + } + } + + var metrics: [TableHealthMetric] = [] + if let definition { + metrics.append(TableHealthMetric(id: "index_definition", label: String(localized: "Definition"), value: .text(definition))) + } + metrics.append(TableHealthMetric(id: "is_unique", label: String(localized: "Unique"), value: .flag(isUnique))) + metrics.append(TableHealthMetric(id: "is_ready", label: String(localized: "Updated by Writes"), value: .flag(isReady))) + return TableHealthFinding( + kind: .invalidIndex, schema: schema, table: row["table_name"], objectName: index, detail: detail, + sizeBytes: row.int64("index_bytes"), metrics: metrics, fixSQL: fixSQL + ) + } + + // pg_get_indexdef writes CREATE INDEX or CREATE UNIQUE INDEX, then the name. + private static func concurrentCreate(_ definition: String) -> String? { + for prefix in ["CREATE UNIQUE INDEX ", "CREATE INDEX "] where definition.hasPrefix(prefix) { + return prefix + "CONCURRENTLY " + String(definition.dropFirst(prefix.count)) + } + return nil + } + + private func nearLimitFindings( + _ rows: [TableHealthRow], + _ context: TableHealthQueryContext + ) -> (findings: [TableHealthFinding], note: String?) { + var findings: [TableHealthFinding] = [] + var unreadable = 0 + for row in rows { + if row.flag("readable") == false { + unreadable += 1 + } else if let finding = nearLimitFinding(row, context) { + findings.append(finding) + } + } + let note: String? + switch unreadable { + case 0: + note = nil + case 1: + note = String(localized: "One sequence is not readable by this user, so its counter was not checked.") + default: + note = String( + format: String(localized: "%lld sequences are not readable by this user, so their counters were not checked."), + unreadable + ) + } + return (findings, note) + } + + private static func typeLimit(of type: String, ascending: Bool) -> Int64? { + switch type { + case "smallint": return ascending ? Int64(Int16.max) : Int64(Int16.min) + case "integer": return ascending ? Int64(Int32.max) : Int64(Int32.min) + case "bigint": return ascending ? Int64.max : Int64.min + default: return nil + } + } + + private static func ownershipLabel(_ ownership: String) -> String? { + switch ownership { + case "identity": return String(localized: "Identity column") + case "owned": return String(localized: "Owned by the column") + case "default": return String(localized: "Column default") + default: return nil + } + } + + private func nearLimitFinding(_ row: TableHealthRow, _ context: TableHealthQueryContext) -> TableHealthFinding? { + guard let increment = row.int64("increment_by"), let lastValue = row.int64("last_value"), + let lowerBound = row.int64("lower_bound"), let upperBound = row.int64("upper_bound"), + let sequenceMin = row.int64("sequence_min"), let sequenceMax = row.int64("sequence_max"), + let used = row.double("used_fraction") + else { return nil } + let sequenceSchema = row["sequence_schema"], sequence = row["sequence_name"], sequenceType = row["sequence_type"] + let table = row.text("table_name"), column = row.text("column_name") + let tableSchema = row.text("table_schema") ?? sequenceSchema + let ascending = increment > 0 + let sequenceLimit = ascending ? sequenceMax : sequenceMin + let columnLimit = ascending ? row.int64("column_max") : row.int64("column_min") + let columnRunsOutFirst = columnLimit.map { ascending ? $0 <= sequenceLimit : $0 >= sequenceLimit } ?? false + + var statements: [String] = [] + let widensColumn = columnRunsOutFirst && table != nil && column != nil + if widensColumn, let table, let column { + statements.append("ALTER TABLE \(context.qualified(tableSchema, table)) ALTER COLUMN \(context.quote(column)) TYPE bigint;") + } + var sequenceChanges: [String] = [] + // An identity sequence takes the column's new type by itself; a serial one keeps its own. + if sequenceType != "bigint", !(widensColumn && row["ownership"] == "identity") { + sequenceChanges.append("AS bigint") + } + // AS bigint moves a limit only when it was the old type's own, so a limit set lower stays until removed. + if !widensColumn, let typeLimit = Self.typeLimit(of: sequenceType, ascending: ascending), typeLimit != sequenceLimit { + sequenceChanges.append(ascending ? "NO MAXVALUE" : "NO MINVALUE") + } + if !sequenceChanges.isEmpty { + statements.append("ALTER SEQUENCE \(context.qualified(sequenceSchema, sequence)) \(sequenceChanges.joined(separator: " "));") + } + + let limit = ascending ? upperBound : lowerBound + var sentences = [String( + format: String(localized: "%1$@ has used %2$@ of its range: its last value is %3$@ and its limit %4$@."), + sequence, TableHealthMetric.Value.percent(used).formatted, lastValue.formatted(), limit.formatted() + )] + if widensColumn { + sentences.append(String(localized: "Changing the column type rewrites the table and its indexes under an exclusive lock.")) + } + let referencing = row.text("referencing_columns").flatMap { $0.isEmpty ? nil : $0 } + if let referencing { + sentences.append(String( + format: String(localized: "These foreign key columns reference it and keep their narrower type: %@."), referencing + )) + } + if statements.isEmpty { + sentences.append(String(localized: "The sequence and its column are already bigint, so no wider type exists.")) + } + + var metrics = [ + TableHealthMetric(id: "used_percent", label: String(localized: "Used"), value: .percent(used)), + TableHealthMetric(id: "last_value", label: String(localized: "Last Value"), value: .count(lastValue)), + TableHealthMetric(id: "lower_bound", label: String(localized: "Lower Bound"), value: .count(lowerBound)), + TableHealthMetric(id: "upper_bound", label: String(localized: "Upper Bound"), value: .count(upperBound)), + TableHealthMetric(id: "sequence_type", label: String(localized: "Sequence Type"), value: .text(sequenceType)) + ] + if let column { + metrics.append(TableHealthMetric(id: "column", label: String(localized: "Column"), value: .text(column))) + } + if let columnType = row.text("column_type") { + metrics.append(TableHealthMetric(id: "column_type", label: String(localized: "Column Type"), value: .text(columnType))) + } + if let ownership = Self.ownershipLabel(row["ownership"]) { + metrics.append(TableHealthMetric(id: "ownership", label: String(localized: "Ownership"), value: .text(ownership))) + } + if let referencing { + metrics.append(TableHealthMetric( + id: "referencing_columns", label: String(localized: "Referencing Columns"), value: .text(referencing) + )) + } + return TableHealthFinding( + kind: .nearLimit, schema: table == nil ? sequenceSchema : tableSchema, table: table, objectName: sequence, + detail: sentences.joined(separator: " "), + sizeBytes: table == nil ? nil : row.int64("table_bytes"), + metrics: metrics, + fixSQL: statements.isEmpty ? nil : statements.joined(separator: "\n") + ) + } + + private func transactionIdAgeFinding(_ row: TableHealthRow, _ context: TableHealthQueryContext) -> TableHealthFinding { + let schema = row["schema_name"], table = row["table_name"] + let detail = [ + String( + format: String(localized: "%1$@ transactions old, past the %2$@ at which autovacuum forces a freeze. %3$@ remain before wraparound."), + Self.countText(row, "xid_age"), Self.countText(row, "freeze_max_age"), Self.countText(row, "transactions_left") + ), + String(localized: "A freeze cannot pass the oldest open transaction, prepared transaction or replication slot.") + ].joined(separator: " ") + + var metrics: [TableHealthMetric] = [] + let counts = [ + ("xid_age", String(localized: "Transaction Age")), + ("table_xid_age", String(localized: "Table Age")), + ("toast_xid_age", String(localized: "TOAST Age")), + ("freeze_max_age", String(localized: "Freeze Max Age")), + ("transactions_left", String(localized: "Transactions Left")) + ] + for (id, label) in counts { + if let value = row.int64(id) { + metrics.append(TableHealthMetric(id: id, label: label, value: .count(value))) + } + } + if let lastVacuum = row.date("last_vacuum") { + metrics.append(TableHealthMetric(id: "last_vacuum", label: String(localized: "Last Vacuum"), value: .date(lastVacuum))) + } + return TableHealthFinding( + kind: .transactionIdAge, schema: schema, table: table, objectName: nil, detail: detail, + sizeBytes: row.int64("table_bytes"), metrics: metrics, + fixSQL: "VACUUM (FREEZE) \(context.qualified(schema, table));" + ) + } + + private func staleStatisticsFinding(_ row: TableHealthRow, _ context: TableHealthQueryContext) -> TableHealthFinding { + let schema = row["schema_name"], table = row["table_name"] + let isPartitioned = row["table_kind"] == "p" + let neverAnalyzed = row.flag("never_analyzed") == true + let lastAnalyze = row.date("last_analyze") + let modified = Self.countText(row, "modified_rows"), threshold = Self.countText(row, "analyze_threshold") + let detail: String + if isPartitioned { + detail = String( + localized: "Never analyzed. Autovacuum does not analyze a partitioned table, so only a manual ANALYZE gives it statistics." + ) + } else if neverAnalyzed { + detail = String(localized: "Never analyzed, so the planner has no statistics for it.") + } else if let lastAnalyze { + detail = String( + format: String(localized: "%1$@ rows modified since it was last analyzed %2$@, more than the %3$@ that start an autoanalyze."), + modified, TableHealthFormatting.date(lastAnalyze), threshold + ) + } else { + detail = String( + format: String(localized: "%1$@ rows modified since it was last analyzed, more than the %2$@ that start an autoanalyze."), + modified, threshold + ) + } + + var metrics: [TableHealthMetric] = [] + // A partitioned table's activity counters stay empty, so only its children's would mean anything. + if !isPartitioned { + let counts = [ + ("modified_rows", String(localized: "Modified Rows")), + ("live_rows", String(localized: "Live Rows")), + ("analyze_threshold", String(localized: "Analyze Threshold")) + ] + for (id, label) in counts { + if let value = row.int64(id) { + metrics.append(TableHealthMetric(id: id, label: label, value: .count(value))) + } + } + } + metrics.append(TableHealthMetric(id: "never_analyzed", label: String(localized: "Never Analyzed"), value: .flag(neverAnalyzed))) + if let lastAnalyze { + metrics.append(TableHealthMetric(id: "last_analyze", label: String(localized: "Last Analyze"), value: .date(lastAnalyze))) + } + return TableHealthFinding( + kind: .staleStatistics, schema: schema, table: table, objectName: nil, detail: detail, + sizeBytes: row.int64("table_bytes"), metrics: metrics, + fixSQL: "ANALYZE \(context.qualified(schema, table));" + ) + } +} diff --git a/apple/TablePro/Core/TableHealth/TableHealthExportDataSource.swift b/apple/TablePro/Core/TableHealth/TableHealthExportDataSource.swift new file mode 100644 index 0000000000..de1c76c6ff --- /dev/null +++ b/apple/TablePro/Core/TableHealth/TableHealthExportDataSource.swift @@ -0,0 +1,310 @@ +import Foundation +import TableProPluginKit + +enum TableHealthRowStatus: Int, Sendable { + case new + case seen + case ignored + case resolved + + var title: String { + switch self { + case .new: return String(localized: "New") + case .seen: return "" + case .ignored: return String(localized: "Ignored") + case .resolved: return String(localized: "Resolved") + } + } + + var exportValue: String? { + switch self { + case .new: return "new" + case .seen: return nil + case .ignored: return "ignored" + case .resolved: return "resolved" + } + } +} + +// One row of the findings or Resolved table, and of what Copy and Export write. The sort keys are stored and never +// optional: a Table column sorts only by a Comparable value. +struct TableHealthFindingRow: Identifiable, Sendable { + let id: TableHealthFindingID + // Nil for a resolved finding, which has no current detail or fix. + let finding: TableHealthFinding? + let checkTitle: String + let checkOrder: Int + let status: TableHealthRowStatus + let firstSeen: Date? + let lastSeen: Date? + let statusRank: Int + let tableSortKey: String + let objectSortKey: String + let sizeSortKey: Int64 + let detail: String + let lastSeenSortKey: Date + + init( + finding: TableHealthFinding, + checkTitle: String, + checkOrder: Int, + status: TableHealthRowStatus, + firstSeen: Date?, + lastSeen: Date? + ) { + self.init( + id: finding.id, + finding: finding, + checkTitle: checkTitle, + checkOrder: checkOrder, + status: status, + firstSeen: firstSeen, + lastSeen: lastSeen, + detail: finding.detail + ) + } + + init(resolved: TableHealthLedger.ResolvedFinding, checkTitle: String, checkOrder: Int) { + self.init( + id: resolved.id, + finding: nil, + checkTitle: checkTitle, + checkOrder: checkOrder, + status: .resolved, + firstSeen: resolved.firstSeen, + lastSeen: resolved.lastSeen, + detail: "" + ) + } + + private init( + id: TableHealthFindingID, + finding: TableHealthFinding?, + checkTitle: String, + checkOrder: Int, + status: TableHealthRowStatus, + firstSeen: Date?, + lastSeen: Date?, + detail: String + ) { + self.id = id + self.finding = finding + self.checkTitle = checkTitle + self.checkOrder = checkOrder + self.status = status + self.firstSeen = firstSeen + self.lastSeen = lastSeen + self.detail = detail + statusRank = status.rawValue + tableSortKey = Self.qualifiedTable(schema: id.schema, table: id.table) ?? "" + objectSortKey = id.object ?? "" + sizeSortKey = finding?.sizeBytes ?? -1 + lastSeenSortKey = lastSeen ?? .distantPast + } + + var qualifiedTable: String? { + Self.qualifiedTable(schema: id.schema, table: id.table) + } + + func matches(_ filter: String) -> Bool { + [checkTitle, tableSortKey, objectSortKey, detail].contains { $0.localizedCaseInsensitiveContains(filter) } + } + + private static func qualifiedTable(schema: String?, table: String?) -> String? { + guard let table else { return nil } + guard let schema else { return table } + return "\(schema).\(table)" + } +} + +// The columns the screen shows, which Copy writes with the same titles and text. +enum TableHealthColumn: Sendable { + case status + case check + case table + case object + case size + case detail + case lastSeen + + static let findings: [TableHealthColumn] = [.status, .check, .table, .object, .size, .detail] + static let resolved: [TableHealthColumn] = [.check, .table, .object, .lastSeen] + + var title: String { + switch self { + case .status: return String(localized: "Status") + case .check: return String(localized: "Check") + case .table: return String(localized: "Table") + case .object: return String(localized: "Object") + case .size: return String(localized: "Size") + case .detail: return String(localized: "Detail") + case .lastSeen: return String(localized: "Last Seen") + } + } + + func text(for row: TableHealthFindingRow) -> String { + switch self { + case .status: return row.status.title + case .check: return row.checkTitle + case .table: return row.tableSortKey + case .object: return row.objectSortKey + case .size: return row.finding?.sizeBytes.map { ByteSizeFormatting.string(bytes: $0) } ?? "" + case .detail: return row.detail + case .lastSeen: return row.lastSeen.map(TableHealthFormatting.date) ?? "" + } + } +} + +enum TableHealthTSV { + static func text(_ rows: [TableHealthFindingRow], columns: [TableHealthColumn]) -> String { + let header = columns.map(\.title).joined(separator: "\t") + let lines = rows.map { row in + columns.map { TableHealthFixScript.singleLine($0.text(for: row)) }.joined(separator: "\t") + } + return ([header] + lines).joined(separator: "\n") + } +} + +// Rows are taken when the export request is built, so a refresh while the dialog is open cannot change what it writes. +// Cells are machine formatted: POSIX numbers, UTC timestamps, null for a missing or non-finite value. +final class TableHealthExportDataSource: PluginExportDataSource { + struct Column: Equatable, Sendable { + let name: String + let typeName: String + } + + static let fixedColumns: [Column] = [ + Column(name: "database", typeName: "varchar"), + Column(name: "check", typeName: "varchar"), + Column(name: "schema", typeName: "varchar"), + Column(name: "table", typeName: "varchar"), + Column(name: "object", typeName: "varchar"), + Column(name: "status", typeName: "varchar"), + Column(name: "size_bytes", typeName: "bigint"), + Column(name: "detail", typeName: "varchar"), + Column(name: "first_seen", typeName: "timestamp"), + Column(name: "last_seen", typeName: "timestamp"), + Column(name: "fix_sql", typeName: "varchar"), + ] + + let databaseTypeId = DataSourceExportRequest.dataFileTypeId + let columns: [Column] + let rows: [PluginRow] + + init(database: String, rows: [TableHealthFindingRow]) { + let metricColumns = Self.metricColumns(in: rows) + columns = Self.fixedColumns + metricColumns + let metricIds = metricColumns.map(\.name) + self.rows = rows.map { Self.cells(of: $0, database: database, metricIds: metricIds) } + } + + func streamRows(table: String, databaseName: String) -> AsyncThrowingStream { + let header = PluginStreamHeader( + columns: columns.map(\.name), + columnTypeNames: columns.map(\.typeName), + estimatedRowCount: rows.count + ) + let rows = rows + return AsyncThrowingStream { continuation in + continuation.yield(.header(header)) + if !rows.isEmpty { + continuation.yield(.rows(rows)) + } + continuation.finish() + } + } + + // Parquet types its columns from here, not from the stream header. + func fetchColumns(table: String, databaseName: String) async throws -> [PluginColumnInfo] { + columns.map { PluginColumnInfo(name: $0.name, dataType: $0.typeName) } + } + + func fetchApproximateRowCount(table: String, databaseName: String) async throws -> Int? { + rows.count + } + + func fetchTableDDL(table: String, databaseName: String) async throws -> String { + "" + } + + func execute(query: String) async throws -> PluginQueryResult { + throw ExportError.exportFailed(String(localized: "Table Health findings cannot run queries.")) + } + + func quoteIdentifier(_ identifier: String) -> String { + SQLEscaping.quoteIdentifier(identifier) + } + + func escapeStringLiteral(_ value: String) -> String { + SQLEscaping.escapeStringLiteral(value) + } + + static func metricColumns(in rows: [TableHealthFindingRow]) -> [Column] { + let ordered = rows.enumerated() + .sorted { ($0.element.checkOrder, $0.offset) < ($1.element.checkOrder, $1.offset) } + .map(\.element) + var seen: Set = [] + var columns: [Column] = [] + for row in ordered { + for metric in row.finding?.metrics ?? [] where seen.insert(metric.id).inserted { + columns.append(Column(name: metric.id, typeName: typeName(of: metric.value))) + } + } + return columns + } + + static func typeName(of value: TableHealthMetric.Value) -> String { + switch value { + case .count, .bytes: return "bigint" + case .bigCount: return "decimal(20,0)" + case .percent: return "double" + case .date: return "timestamp" + case .flag: return "boolean" + case .text: return "varchar" + } + } + + static func cell(_ value: TableHealthMetric.Value) -> PluginCellValue { + switch value { + case .count(let count): return .text(String(count)) + case .bigCount(let count): return .text(String(count)) + case .bytes(let bytes): return .text(String(bytes)) + case .percent(let fraction): return fraction.isFinite ? .text(String(fraction)) : .null + case .date(let date): return .text(timestamp(date)) + case .text(let text): return .text(text) + case .flag(let flag): return .text(flag ? "true" : "false") + } + } + + static func timestamp(_ date: Date) -> String { + var calendar = Calendar(identifier: .gregorian) + calendar.timeZone = TimeZone(identifier: "UTC") ?? calendar.timeZone + let parts = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: date) + return String( + format: "%04ld-%02ld-%02ld %02ld:%02ld:%02ld", + parts.year ?? 0, parts.month ?? 0, parts.day ?? 0, parts.hour ?? 0, parts.minute ?? 0, parts.second ?? 0 + ) + } + + private static func cells(of row: TableHealthFindingRow, database: String, metricIds: [String]) -> PluginRow { + let finding = row.finding + let text: (String?) -> PluginCellValue = { $0.map(PluginCellValue.text) ?? .null } + var cells: PluginRow = [ + .text(database), + .text(row.checkTitle), + text(row.id.schema), + text(row.id.table), + text(row.id.object), + text(row.status.exportValue), + text(finding?.sizeBytes.map { String($0) }), + text(finding?.detail), + text(row.firstSeen.map(timestamp)), + text(row.lastSeen.map(timestamp)), + text(finding?.fixSQL), + ] + for id in metricIds { + cells.append(finding?.metric(id).map { cell($0.value) } ?? .null) + } + return cells + } +} diff --git a/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift b/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift index 85c290b52c..6384408c76 100644 --- a/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift +++ b/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift @@ -5,7 +5,8 @@ enum TableHealthFixScript { database: String, generatedAt date: Date, databaseType: DatabaseType, - findings: [TableHealthFinding] + findings: [TableHealthFinding], + checkTitle: (TableHealthCheckKind) -> String = { $0.title } ) -> String { var script = [ commentLine(String(format: String(localized: "Database: %1$@ (%2$@)"), database, databaseType.displayName)), @@ -14,7 +15,7 @@ enum TableHealthFixScript { ].joined(separator: "\n") for finding in findings { guard let fix = finding.fixSQL, !fix.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { continue } - let name = String(format: String(localized: "%1$@: %2$@"), finding.kind.title, finding.qualifiedName) + let name = String(format: String(localized: "%1$@: %2$@"), checkTitle(finding.kind), finding.qualifiedName) script += "\n\n" + commentLine(name) + "\n" + fix } return script + "\n" diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 8ecb808b3c..71ac2bffef 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -3972,6 +3972,12 @@ } } } + }, + "%@ is invalid after a failed or cancelled concurrent build. Queries never use it, but every write still updates it." : { + + }, + "%@ is invalid after a failed or cancelled concurrent build. Queries never use it." : { + }, "%@ is invalid, so queries skip it and exports leave it out. Drop it, or rebuild it with REINDEX." : { "localizations" : { @@ -4231,6 +4237,12 @@ } } } + }, + "%@ is the new copy an interrupted REINDEX CONCURRENTLY left behind. Drop it, then run the rebuild again." : { + + }, + "%@ is the old copy a REINDEX CONCURRENTLY left behind after the rebuild finished." : { + }, "%@ is too wide for this engine to index whole, so only a prefix is indexed. Rows differing only past it are refused as duplicates." : { "localizations" : { @@ -7173,6 +7185,9 @@ } } } + }, + "%1$@ has used %2$@ of its range: its last value is %3$@ and its limit %4$@." : { + }, "%1$@ holds numbers from %2$@ to %3$@." : { @@ -7252,6 +7267,9 @@ }, "%1$@ is a light theme, so %2$@ is used." : { + }, + "%1$@ is at %2$@ of the largest value %3$@ holds." : { + }, "%1$@ is covered by %2$@." : { @@ -7493,6 +7511,9 @@ } } } + }, + "%1$@ reclaimable, %2$@ of the table's size on disk." : { + }, "%1$@ references %2$@, which has no rows." : { @@ -7516,6 +7537,12 @@ } } } + }, + "%1$@ rows modified since it was last analyzed %2$@, more than the %3$@ that start an autoanalyze." : { + + }, + "%1$@ rows modified since it was last analyzed, more than the %2$@ that start an autoanalyze." : { + }, "%1$@ tables hold “%2$@”." : { @@ -7528,6 +7555,9 @@ }, "%1$@ to %2$@: %3$@" : { + }, + "%1$@ transactions old, past the %2$@ at which autovacuum forces a freeze. %3$@ remain before wraparound." : { + }, "%1$@ wants to run a query on \"%2$@\"" : { "localizations" : { @@ -11768,6 +11798,9 @@ } } } + }, + "%lld sequences are not readable by this user, so their counters were not checked." : { + }, "%lld skipped (no options)" : { "localizations" : { @@ -12570,6 +12603,12 @@ }, "^[%lld finding](inflect: true)" : { + }, + "^[%lld fix](inflect: true)" : { + + }, + "^[%lld ignored finding](inflect: true) hidden." : { + }, "^[%lld line](inflect: true)" : { "localizations" : { @@ -15917,6 +15956,9 @@ } } } + }, + "A finding moves here when a refresh no longer reports it." : { + }, "A Firestore document is a JSON object of fields." : { "extractionState" : "manual", @@ -15960,6 +16002,9 @@ } } } + }, + "A freeze cannot pass the oldest open transaction, prepared transaction or replication slot." : { + }, "A global secondary index takes a partition key and an optional sort key" : { "extractionState" : "manual", @@ -16501,6 +16546,9 @@ } } } + }, + "A plan works on up to 3 devices at a time. Removing one signs TablePro out there." : { + }, "A point's id cannot change. Insert a new row with the new id instead." : { "extractionState" : "manual", @@ -16508,9 +16556,6 @@ }, "A publish to the team library is still running. Try again when it finishes." : { - }, - "A plan works on up to 3 devices at a time. Removing one signs TablePro out there." : { - }, "a query" : { "localizations" : { @@ -22774,6 +22819,12 @@ } } } + }, + "All Findings" : { + + }, + "All Findings (%@)" : { + }, "All Objects" : { "localizations" : { @@ -25020,6 +25071,9 @@ } } } + }, + "Analyze Threshold" : { + }, "ANCHOR needs a partition:offset list." : { "extractionState" : "manual", @@ -29595,6 +29649,12 @@ } } } + }, + "AUTO_INCREMENT columns whose next value has reached 75% of the largest value their type holds." : { + + }, + "AUTO_INCREMENT Near Its Limit" : { + }, "Auto-detects from ~/.ssh/config and default key locations." : { "localizations" : { @@ -36559,6 +36619,9 @@ } } } + }, + "Changing the column type rewrites the table and its indexes under an exclusive lock." : { + }, "Changing the primary key rebuilds the table and can fail on duplicate values." : { "localizations" : { @@ -37183,6 +37246,9 @@ } } } + }, + "Check Failed" : { + }, "Check for a firewall, VPN, or load balancer between you and the server that closes connections mid-handshake." : { "extractionState" : "manual", @@ -37249,6 +37315,9 @@ } } } + }, + "Check id, also set on each of its findings" : { + }, "Check mode:" : { "extractionState" : "manual", @@ -37291,9 +37360,15 @@ } } } + }, + "Check name" : { + }, "Check Now" : { + }, + "Check Skipped" : { + }, "Check that the Client Key path points to a valid PEM private key." : { "extractionState" : "manual", @@ -40178,6 +40253,9 @@ } } } + }, + "Clear Resolved" : { + }, "Clear Results" : { "localizations" : { @@ -44074,6 +44152,9 @@ } } } + }, + "Column default" : { + }, "Column default expression" : { "extractionState" : "manual", @@ -44601,6 +44682,9 @@ } } } + }, + "Column Type" : { + }, "Column type, for a column match" : { "localizations" : { @@ -54566,6 +54650,9 @@ } } } + }, + "Could Not Save the Fix Script" : { + }, "Could not save the password to the Keychain. Nothing was changed." : { "localizations" : { @@ -54661,6 +54748,9 @@ } } } + }, + "Could not update the finding history. %@" : { + }, "Could not use this license key." : { @@ -55501,6 +55591,9 @@ } } } + }, + "Counters Near Their Limit" : { + }, "Counting rows" : { "localizations" : { @@ -55597,6 +55690,12 @@ }, "Country" : { + }, + "Covering Definition" : { + + }, + "Covering Index" : { + }, "Covers the loaded rows only." : { @@ -59813,6 +59912,9 @@ } } } + }, + "Database the checks ran on" : { + }, "Database the listing covers" : { "localizations" : { @@ -60391,6 +60493,9 @@ } } } + }, + "Database: %1$@ (%2$@)" : { + }, "Database: %1$@, schema: %2$@" : { @@ -60837,6 +60942,12 @@ } } } + }, + "Dead Row Share" : { + + }, + "Dead Rows" : { + }, "Debounce: %d ms" : { "localizations" : { @@ -74590,6 +74701,12 @@ } } } + }, + "Every check that ran found nothing." : { + + }, + "Every check this engine runs, in order" : { + }, "Every collection with this ID, at any depth" : { "extractionState" : "manual", @@ -83866,6 +83983,15 @@ } } } + }, + "Findings" : { + + }, + "Findings of every check that completed, ignored ones included" : { + + }, + "Findings the check reported" : { + }, "Finish Signing In" : { "localizations" : { @@ -85115,6 +85241,12 @@ } } } + }, + "Foreign keys use this column (%@): drop them, give the column at the other end of each the same type, then add them again." : { + + }, + "Foreign keys whose columns lead no valid, non-partial index, so a delete on the referenced table scans this one." : { + }, "Foreign Keys Without an Index" : { @@ -85761,6 +85893,9 @@ } } } + }, + "Freeze Max Age" : { + }, "Freeze the list; messages keep arriving underneath" : { @@ -86581,6 +86716,9 @@ } } } + }, + "Generated: %@" : { + }, "Generation failed." : { "localizations" : { @@ -86892,6 +87030,9 @@ } } } + }, + "Get Table Health" : { + }, "Get Table Statistics" : { "localizations" : { @@ -90695,6 +90836,9 @@ } } } + }, + "How to read value: percent is a fraction (0.25 means 25 percent), date is ISO 8601" : { + }, "HTTP %d with a body of %d bytes" : { "extractionState" : "manual", @@ -91094,6 +91238,9 @@ } } } + }, + "Id of the check that reported it" : { + }, "Identical" : { "localizations" : { @@ -91224,6 +91371,9 @@ } } } + }, + "Identity column" : { + }, "Identity File" : { "localizations" : { @@ -91516,6 +91666,9 @@ } } } + }, + "Ignore" : { + }, "Ignore case" : { "localizations" : { @@ -91616,6 +91769,9 @@ } } } + }, + "Ignored" : { + }, "Image" : { "localizations" : { @@ -94526,6 +94682,9 @@ }, "Index usage counts since the statistics were last reset at %@." : { + }, + "Index, constraint, sequence or column the finding is about" : { + }, "Indexed columns in order" : { "extractionState" : "manual", @@ -94610,6 +94769,12 @@ } } } + }, + "Indexes a failed or cancelled CREATE INDEX CONCURRENTLY or REINDEX CONCURRENTLY left invalid, leaving out builds still running." : { + + }, + "Indexes never scanned since the statistics were last reset, leaving out unique, constraint and foreign key indexes." : { + }, "Indexes on the table" : { "localizations" : { @@ -94650,6 +94815,15 @@ } } } + }, + "Indexes whose columns lead another visible index of the same type on the same table." : { + + }, + "Indexes whose keys lead another index with the same method, predicate and uniqueness; primary key and constraint indexes stay." : { + + }, + "Indexes with no recorded reads since the server started, leaving out unique, full-text and foreign key indexes." : { + }, "Indexing an expression needs MySQL 8.0.13 or later." : { "extractionState" : "manual", @@ -96864,6 +97038,9 @@ } } } + }, + "Invalid Indexes" : { + }, "Invalid indexes are not recreated: %@." : { "extractionState" : "manual", @@ -97819,6 +97996,9 @@ } } } + }, + "ISO 8601 time the checks ran" : { + }, "ISO Date (2024-12-31)" : { "localizations" : { @@ -100493,6 +100673,9 @@ } } } + }, + "Largest Value" : { + }, "Last 4 Characters" : { @@ -100736,6 +100919,9 @@ } } } + }, + "Last Analyze" : { + }, "Last checked: %@" : { "localizations" : { @@ -100972,6 +101158,9 @@ } } } + }, + "Last Seen" : { + }, "Last seen %@" : { @@ -101136,6 +101325,12 @@ } } } + }, + "Last Vacuum" : { + + }, + "Last Value" : { + }, "Later" : { @@ -105229,6 +105424,9 @@ }, "Live Messages…" : { + }, + "Live Rows" : { + }, "Live: %@" : { @@ -106642,6 +106840,9 @@ } } } + }, + "Lower Bound" : { + }, "lowercase" : { "extractionState" : "manual", @@ -109340,6 +109541,9 @@ } } } + }, + "Measured values" : { + }, "Median" : { "localizations" : { @@ -110183,6 +110387,9 @@ } } } + }, + "Metric name" : { + }, "Metric unit" : { "localizations" : { @@ -111099,6 +111306,9 @@ } } } + }, + "Modified Rows" : { + }, "Modified:" : { "extractionState" : "stale", @@ -113056,6 +113266,9 @@ } } } + }, + "MySQL caches the values this check reads for information_schema_stats_expiry seconds, one day by default. ANALYZE TABLE refreshes a table's values." : { + }, "MySQL, PostgreSQL, SQLite, ClickHouse, and Redis are built in. Others install when you choose them." : { "localizations" : { @@ -113719,6 +113932,15 @@ }, "Needs attention" : { + }, + "Needs MySQL 8.0 or later." : { + + }, + "Needs PostgreSQL 10 or later." : { + + }, + "Needs the PROCESS privilege to tell a table in its own file from one in a shared tablespace." : { + }, "Negotiating encryption" : { "localizations" : { @@ -113947,6 +114169,15 @@ } } } + }, + "Never Analyzed" : { + + }, + "Never analyzed, so the planner has no statistics for it." : { + + }, + "Never analyzed. Autovacuum does not analyze a partitioned table, so only a manual ANALYZE gives it statistics." : { + }, "Never Ask" : { "localizations" : { @@ -115474,6 +115705,9 @@ } } } + }, + "new: not seen in the last run the Table Health tab recorded; ignored: the user accepted it" : { + }, "New…" : { "localizations" : { @@ -115763,6 +115997,9 @@ }, "Next value" : { + }, + "Next Value" : { + }, "Nightly tables" : { "localizations" : { @@ -115773,6 +116010,9 @@ } } } + }, + "No" : { + }, "No %@" : { "localizations" : { @@ -117778,6 +118018,9 @@ } } } + }, + "No Findings" : { + }, "No findings." : { @@ -118031,6 +118274,9 @@ } } } + }, + "No integer type holds larger values." : { + }, "No item read so far holds \"%@\" as a key type. Create this index from the editor with its AttributeDefinitions." : { "extractionState" : "manual", @@ -119988,6 +120234,9 @@ } } } + }, + "No Resolved Findings" : { + }, "No Result to Compare With" : { @@ -123906,6 +124155,9 @@ } } } + }, + "Notes on the whole report, such as when the usage counters started" : { + }, "Nothing got at least %1$lld%% slower than the period before. A query needs %2$lld runs in both periods, and has to have grown by at least %3$@." : { "comment" : "Empty state for regressions: %1$lld is a percentage, %2$lld a run count, %3$@ a duration", @@ -125306,6 +125558,9 @@ } } } + }, + "Number, string, boolean or null, as type says" : { + }, "Numbered List" : { "extractionState" : "manual", @@ -126072,6 +126327,9 @@ } } } + }, + "On a partitioned table the drop cannot run concurrently, and it removes the index from every partition." : { + }, "On Delete" : { "localizations" : { @@ -126449,6 +126707,9 @@ } } } + }, + "On-disk size of what the fix acts on: the index for an index check, the table otherwise" : { + }, "Once this save updates the validator, it would reject the document with _id %@. Fix the document or the validator first." : { "extractionState" : "manual", @@ -126630,6 +126891,9 @@ } } } + }, + "One sequence is not readable by this user, so its counter was not checked." : { + }, "One SQL or NoSQL statement" : { "extractionState" : "stale", @@ -126691,6 +126955,9 @@ } } } + }, + "One-line description" : { + }, "One-time" : { "localizations" : { @@ -128065,6 +128332,9 @@ }, "Open Fix in Editor" : { + }, + "Open Fixes in Editor" : { + }, "Open in Agent Mode" : { "localizations" : { @@ -130963,6 +131233,9 @@ }, "Owned by %@" : { + }, + "Owned by the column" : { + }, "Owned objects" : { "localizations" : { @@ -131043,6 +131316,9 @@ } } } + }, + "Ownership" : { + }, "Owning table, for a column match" : { "localizations" : { @@ -144311,6 +144587,9 @@ } } } + }, + "Rebuilding a unique index fails while the table holds duplicate values." : { + }, "Received" : { "localizations" : { @@ -144692,6 +144971,15 @@ } } } + }, + "Reclaimable" : { + + }, + "Reclaimable Share" : { + + }, + "Reclaimable Space" : { + }, "Reconnect" : { "localizations" : { @@ -145524,6 +145812,9 @@ } } } + }, + "Referenced Table" : { + }, "Referenced Value" : { @@ -145595,6 +145886,9 @@ } } } + }, + "Referencing Columns" : { + }, "Referencing table" : { "localizations" : { @@ -150186,6 +150480,12 @@ }, "Resolve the conflict in a Git client before committing this file." : { + }, + "Resolved" : { + + }, + "Resolved Findings" : { + }, "Resolved on every connect. It stays on this Mac and never syncs, because it can name a command to run." : { "extractionState" : "stale", @@ -155034,6 +155334,9 @@ } } } + }, + "Run each statement on its own; some cannot run inside a transaction." : { + }, "Run EXEC to apply the block, or DISCARD to drop it." : { "extractionState" : "manual", @@ -155496,6 +155799,9 @@ } } } + }, + "Run TablePro's Table Health checks on one database and return each check's outcome and every finding, with its metrics and the SQL that fixes it. Status comes from the history the Table Health tab records: new since its last run, or ignored by the user. This call records nothing and runs no fix. Available on every engine that has Table Health in TablePro." : { + }, "Run the command below in your terminal" : { "localizations" : { @@ -155546,6 +155852,9 @@ } } } + }, + "Run the fix only if the column holds no negative values." : { + }, "Run the query again. TablePro reconnects to the server automatically." : { "extractionState" : "manual", @@ -157090,6 +157399,9 @@ } } } + }, + "Save Fix Script…" : { + }, "Save frequently used queries\nfor quick access." : { "extractionState" : "stale", @@ -159436,6 +159748,9 @@ } } } + }, + "Schema, on engines that have one" : { + }, "Schemas" : { "localizations" : { @@ -162143,6 +162458,9 @@ } } } + }, + "Selected Findings (%@)" : { + }, "Selected Item" : { "extractionState" : "stale", @@ -162826,6 +163144,9 @@ } } } + }, + "Sequence Type" : { + }, "Sequence Unavailable" : { @@ -162839,6 +163160,12 @@ } } } + }, + "Sequences Near Their Limit" : { + + }, + "Sequences that have used at least 75% of what their type, limits and integer column allow, leaving out cycling ones." : { + }, "Serial Consistency" : { "extractionState" : "manual", @@ -165042,6 +165369,9 @@ } } } + }, + "Share Used" : { + }, "Sheet" : { "localizations" : { @@ -165933,6 +166263,9 @@ } } } + }, + "Show Ignored" : { + }, "Show in Finder" : { "localizations" : { @@ -167592,6 +167925,9 @@ } } } + }, + "Shown Findings (%@)" : { + }, "Shows a type icon before each object name in the sidebar. Turn it off for a plain list of names." : { "localizations" : { @@ -168745,15 +169081,15 @@ } } } - }, - "Signing in with credentials from this Mac was not approved." : { - }, "Signing in" : { }, "Signing in by email? Type the 6-digit code from the email in the sign-in sheet. The link in the email opens your browser instead." : { + }, + "Signing in with credentials from this Mac was not approved." : { + }, "Signing in…" : { "localizations" : { @@ -169346,6 +169682,9 @@ } } } + }, + "Skipped" : { + }, "Skipped %@: no endpoint yet." : { "localizations" : { @@ -171951,6 +172290,9 @@ } } } + }, + "SQL that fixes the finding, absent when there is none. Review it before running it." : { + }, "SQL the session proposes appears here before it runs." : { "localizations" : { @@ -173371,6 +173713,9 @@ }, "Stable Hash" : { + }, + "Stable metric id" : { + }, "Stack" : { "extractionState" : "manual", @@ -173386,6 +173731,9 @@ } } } + }, + "Stale Statistics" : { + }, "Standalone" : { "extractionState" : "manual", @@ -175464,6 +175812,9 @@ } } } + }, + "Stop Ignoring" : { + }, "Stop Import" : { @@ -179080,6 +179431,9 @@ } } } + }, + "Table Age" : { + }, "Table and view names in a database. Pass database and schema to look outside the browsed one, and row_counts=true for approximate row counts." : { "extractionState" : "manual", @@ -179472,6 +179826,12 @@ }, "Table Health" : { + }, + "Table Health findings cannot run queries." : { + + }, + "Table Health for %@" : { + }, "Table health is available for PostgreSQL, MySQL and MariaDB." : { @@ -180215,6 +180575,9 @@ } } } + }, + "Table the finding is about" : { + }, "Table the statistics describe" : { "localizations" : { @@ -180931,6 +181294,9 @@ }, "TablePro could not reach the Schema Registry: %@" : { + }, + "TablePro could not read its Table Health history for this connection." : { + }, "TablePro could not read the CA Certificate at %@: %@" : { "extractionState" : "manual", @@ -180945,6 +181311,9 @@ } } } + }, + "TablePro could not read the Table Health history: %@" : { + }, "TablePro could not read what this connection is holding, so it kept the file." : { "extractionState" : "manual", @@ -180963,6 +181332,9 @@ }, "TablePro could not read your saved connections, so it did not save over them." : { + }, + "TablePro could not save the Table Health history: %@" : { + }, "TablePro could not tell how this Dameng server treats backslashes, so it will not send a value that contains one. Reconnect, then try again." : { "extractionState" : "manual", @@ -181258,6 +181630,9 @@ } } } + }, + "TablePro has no Table Health checks for this engine." : { + }, "TablePro has not confirmed this license with the server in 30 days." : { "localizations" : { @@ -182284,6 +182659,9 @@ }, "Tables more than %lld foreign keys away weren't checked." : { + }, + "Tables never analyzed, or with more rows modified since the last ANALYZE than 10% of their rows plus 50." : { + }, "Tables of %@" : { "localizations" : { @@ -182364,6 +182742,9 @@ } } } + }, + "Tables stored in their own files whose free space is at least 100 MiB and at least 10% of their size on disk." : { + }, "Tables that carry at least one foreign key" : { "localizations" : { @@ -182484,6 +182865,12 @@ } } } + }, + "Tables whose oldest unfrozen row is more than autovacuum_freeze_max_age transactions old (200,000,000 unless set lower)." : { + + }, + "Tables with at least 1,000 dead rows, and at least 20% as many dead rows as live ones." : { + }, "Tables with more estimated rows use approximate counts to avoid slow COUNT(*) queries" : { "localizations" : { @@ -182524,6 +182911,12 @@ } } } + }, + "Tables with no primary key; a partition takes its key from its parent, so only the parent is listed." : { + + }, + "Tables with no primary key." : { + }, "Tables Without a Primary Key" : { @@ -186486,6 +186879,9 @@ "The connection from the database didn't complete the debugger handshake." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "The connection has no current database. Pass a database name." : { + }, "The connection has no current DuckDB catalog to read metadata from" : { "extractionState" : "manual", @@ -192243,6 +192639,12 @@ } } } + }, + "The selected findings have no SQL fix." : { + + }, + "The sequence and its column are already bigint, so no wider type exists." : { + }, "The sequence for %@ runs past the largest number it can hold." : { @@ -192395,6 +192797,9 @@ "The server did not answer the TLS handshake. If it serves plain HTTP, set SSL Mode to Disabled." : { "extractionState" : "manual", "generatesSymbol" : false + }, + "The server did not answer this check." : { + }, "The server did not say which InfluxDB version it runs." : { "extractionState" : "manual", @@ -194656,9 +195061,6 @@ } } } - }, - "The text at byte %lld is not valid UTF-8." : { - }, "The team library asked TablePro to download it again." : { @@ -194674,6 +195076,9 @@ }, "The team library refused the change. It may be too large or hold a field the library never stores." : { + }, + "The text at byte %lld is not valid UTF-8." : { + }, "The text could not be parsed as JSON." : { "localizations" : { @@ -195968,6 +196373,9 @@ } } } + }, + "These foreign key columns reference it and keep their narrower type: %@." : { + }, "These messages use the %@ compression codec, which TablePro cannot read." : { "extractionState" : "manual", @@ -199001,6 +199409,9 @@ } } } + }, + "This finding has no SQL fix." : { + }, "This folder is already linked" : { "comment" : "Alert title when trying to add a folder that is already linked.", @@ -201323,6 +201734,12 @@ } } } + }, + "This server is a read-only standby. Run the fixes on the primary." : { + + }, + "This server is a standby. Its activity counters do not reflect the primary." : { + }, "This server is Databend. Edit the connection and choose Databend as its type." : { "extractionState" : "manual", @@ -203455,6 +203872,9 @@ } } } + }, + "TOAST Age" : { + }, "Today" : { "localizations" : { @@ -204924,6 +205344,9 @@ } } } + }, + "Transaction Age" : { + }, "Transaction Control" : { "localizations" : { @@ -204979,6 +205402,9 @@ }, "Transaction failed on %1$@, %2$@. Roll it back to continue." : { + }, + "Transaction ID Wraparound" : { + }, "transaction open" : { @@ -205018,6 +205444,9 @@ } } } + }, + "Transactions Left" : { + }, "Transfer" : { "extractionState" : "manual", @@ -209728,6 +210157,9 @@ } } } + }, + "Updated by Writes" : { + }, "Updated to v%@" : { "extractionState" : "stale", @@ -209934,6 +210366,9 @@ "Upload Again" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Upper Bound" : { + }, "Upper bound, for BETWEEN" : { "localizations" : { @@ -210880,6 +211315,9 @@ } } } + }, + "Used" : { + }, "Used by %lld" : { "localizations" : { @@ -214754,6 +215192,9 @@ } } } + }, + "What the check looks for, with its threshold" : { + }, "What the option does" : { "localizations" : { @@ -214934,6 +215375,9 @@ } } } + }, + "What to know when reading this check's findings" : { + }, "What will run against %@" : { "localizations" : { @@ -215390,6 +215834,9 @@ } } } + }, + "Whether the check ran" : { + }, "Whether the column accepts NULL" : { "extractionState" : "manual", @@ -216305,6 +216752,9 @@ } } } + }, + "Why the check was skipped or failed" : { + }, "Why the metrics panel could not be read" : { "localizations" : { @@ -217658,6 +218108,9 @@ } } } + }, + "Yes" : { + }, "Yesterday" : { "localizations" : { @@ -217859,6 +218312,15 @@ } } } + }, + "You were signed out. Sign in again." : { + + }, + "You're on Free" : { + + }, + "You're signed in" : { + }, "You've used TablePro on %lld days" : { "localizations" : { @@ -217899,15 +218361,6 @@ } } } - }, - "You were signed out. Sign in again." : { - - }, - "You're on Free" : { - - }, - "You're signed in" : { - }, "Your account is not allowed to read the source of %@" : { "extractionState" : "manual", diff --git a/apple/TablePro/ViewModels/TableHealthViewModel.swift b/apple/TablePro/ViewModels/TableHealthViewModel.swift index c89b85bedf..5a025d10fe 100644 --- a/apple/TablePro/ViewModels/TableHealthViewModel.swift +++ b/apple/TablePro/ViewModels/TableHealthViewModel.swift @@ -2,72 +2,162 @@ import Combine import Foundation import os +enum TableHealthScope: Hashable, Sendable { + case all + case check(TableHealthCheckKind) + case resolved +} + +enum TableHealthListState: Equatable { + case rows + case noFindings(String) + case skipped(String) + case failed(String) + case noResults + case ignoredHidden(Int) + case noResolved +} + @MainActor final class TableHealthViewModel: ObservableObject { nonisolated private static let logger = Logger(subsystem: "com.TablePro", category: "TableHealth") + private static let exportScopeAll = "all" + private static let exportScopeShown = "shown" + private static let exportScopeSelected = "selected" let provider: (any TableHealthProvider)? + let connectionId: UUID @Published private(set) var report: TableHealthReport? + @Published private(set) var ledger = TableHealthLedger() @Published private(set) var isRefreshing = false @Published private(set) var lastRefreshDate: Date? @Published private(set) var loadError: String? - @Published var selectedFindingId: TableHealthFinding.ID? + @Published private(set) var storeError: String? - private let connectionId: UUID + @Published var scope: TableHealthScope = .all { + didSet { if scope != oldValue { rebuildRows() } } + } + @Published var selection: Set = [] + @Published var sortOrder: [KeyPathComparator] = [KeyPathComparator(\TableHealthFindingRow.checkOrder)] { + didSet { rebuildRows() } + } + @Published var resolvedSortOrder: [KeyPathComparator] = [ + KeyPathComparator(\TableHealthFindingRow.lastSeenSortKey, order: .reverse) + ] { + didSet { rebuildRows() } + } + @Published var filterText = "" { + didSet { if filterText != oldValue { rebuildRows() } } + } + @Published var showIgnored = false { + didSet { if showIgnored != oldValue { rebuildRows() } } + } + + // What the table shows for the scope, sorted and filtered once per change rather than in `body`. + @Published private(set) var rows: [TableHealthFindingRow] = [] + // Every current finding, ignored ones included, in the current sort. + @Published private(set) var findingRows: [TableHealthFindingRow] = [] + @Published private(set) var checkCounts: [TableHealthCheckKind: Int] = [:] + @Published private(set) var allFindingsCount = 0 + @Published private(set) var resolvedCount = 0 + @Published private(set) var hiddenIgnoredCount = 0 + @Published private(set) var scopeRowCount = 0 + + private let history: any TableHealthHistoryStoring private let metadata: any ScopedMetadataProviding - private var refreshTask: Task? + // Read at each refresh, so a database rename retargets the next run and nothing follows the sidebar. + private let database: @MainActor () -> String + private var runTask: Task? + private var refreshQueued = false + private var ledgerLoaded = false + private var historyObserver: AnyCancellable? var isSupported: Bool { provider != nil } - var selectedFinding: TableHealthFinding? { - selectedFindingId.flatMap { report?.finding(id: $0) } - } + var databaseName: String { database() } init( connectionId: UUID, provider: (any TableHealthProvider)?, - metadata: any ScopedMetadataProviding = DatabaseManager.shared + history: any TableHealthHistoryStoring, + metadata: any ScopedMetadataProviding = DatabaseManager.shared, + database: @escaping @MainActor () -> String ) { self.connectionId = connectionId self.provider = provider + self.history = history self.metadata = metadata + self.database = database + historyObserver = NotificationCenter.default.publisher(for: .tableHealthHistoryDidChange) + .receive(on: RunLoop.main) + .sink { [weak self] notification in + self?.historyDidChange(notification) + } } - // A pane switch restarts `.task`, so only the first appearance loads. + // MARK: - Loading + + // A pane switch restarts `.task`, so only the first appearance loads, and it joins a run already going. func loadIfNeeded() async { + if !ledgerLoaded { reloadLedger() } guard report == nil, loadError == nil else { return } + if let runTask { + await runTask.value + return + } await refresh() } - // The model owns the read, so a pane switch that cancels `.task` neither drops it nor leaves the - // next appearance waiting on nothing: a second call joins the one in flight. + // The model owns the run, so a pane switch that cancels `.task` does not drop it. A refresh asked for during a run + // runs once more after it: the first may have read the catalog before the change the user refreshed for. func refresh() async { - if let refreshTask { - await refreshTask.value + if let runTask { + refreshQueued = true + await runTask.value return } guard let provider else { return } - let task = Task { await self.performRefresh(provider) } - refreshTask = task + let task = Task { [weak self] in + guard let self else { return } + await runRefreshes(provider) + } + runTask = task await task.value } - private func performRefresh(_ provider: any TableHealthProvider) async { + // The tab closed: stop reading so the bulk metadata lane is free for the next caller. + func cancel() { + refreshQueued = false + runTask?.cancel() + } + + private func runRefreshes(_ provider: any TableHealthProvider) async { isRefreshing = true defer { isRefreshing = false - refreshTask = nil + runTask = nil } + repeat { + refreshQueued = false + await performRefresh(provider) + } while refreshQueued && !Task.isCancelled + } + private func performRefresh(_ provider: any TableHealthProvider) async { + let target = self.database() do { - guard let scope = metadata.browseScope(for: connectionId) else { throw DatabaseError.notConnected } + let scope = DatabaseScope(connectionId: connectionId, database: target, schema: nil) let fresh = try await TableHealthReportLoader.load(provider: provider, scope: scope, metadata: metadata) + guard !Task.isCancelled else { return } report = Self.merging(fresh, over: report) loadError = nil lastRefreshDate = Date() - if selectedFinding == nil { selectedFindingId = nil } + record(fresh, database: target) + settleAfterRefresh() + rebuildRows() } catch { + if Task.isCancelled || error is CancellationError { return } Self.logger.warning("Table health refresh failed: \(error.publicLogShape, privacy: .public)") loadError = error.localizedDescription } @@ -85,4 +175,276 @@ final class TableHealthViewModel: ObservableObject { } return merged } + + private func settleAfterRefresh() { + if case .check(let kind) = scope, report?.result(for: kind) == nil { + scope = .all + } + var known = Set(report?.findings.map(\.id) ?? []) + known.formUnion(ledger.resolved.map(\.id)) + let kept = selection.intersection(known) + if kept != selection { selection = kept } + } + + // MARK: - History + + private func record(_ report: TableHealthReport, database: String) { + do { + ledger = try history.record(report, connectionId: connectionId, database: database, at: report.generatedAt) + ledgerLoaded = true + storeError = nil + } catch { + Self.logger.error("Table health history not recorded: \(error.publicLogShape, privacy: .public)") + storeError = error.localizedDescription + } + } + + private func reloadLedger() { + let target = self.database() + guard !target.isEmpty else { return } + do { + let fresh = try history.ledger(connectionId: connectionId, database: target) + ledgerLoaded = true + guard fresh != ledger else { return } + ledger = fresh + rebuildRows() + } catch { + Self.logger.error("Table health history not read: \(error.publicLogShape, privacy: .public)") + storeError = error.localizedDescription + } + } + + private func historyDidChange(_ notification: Notification) { + guard let changed = TableHealthHistoryChange.scope(of: notification), + changed.connectionId == connectionId, + changed.database == database() + else { return } + reloadLedger() + } + + func setIgnored(_ ids: Set, ignored: Bool) { + guard !ids.isEmpty else { return } + updateLedger { + try history.setIgnored(ids, ignored: ignored, connectionId: connectionId, database: database(), at: Date()) + } + } + + func clearResolved() { + updateLedger { + try history.clearResolved(connectionId: connectionId, database: database()) + } + } + + private func updateLedger(_ change: () throws -> TableHealthLedger) { + guard !database().isEmpty else { return } + do { + ledger = try change() + ledgerLoaded = true + storeError = nil + } catch { + Self.logger.error("Table health history not saved: \(error.publicLogShape, privacy: .public)") + storeError = error.localizedDescription + } + rebuildRows() + } + + // MARK: - Rows + + private func rebuildRows() { + var current: [TableHealthFindingRow] = [] + var checkPositions: [TableHealthCheckKind: (title: String, order: Int)] = [:] + for (order, result) in (report?.checks ?? []).enumerated() { + checkPositions[result.kind] = (result.check.title, order) + for finding in result.findings { + current.append(TableHealthFindingRow( + finding: finding, + checkTitle: result.check.title, + checkOrder: order, + status: rowStatus(of: finding.id), + firstSeen: ledger.firstSeen(finding.id), + lastSeen: ledger.lastSeen(finding.id) + )) + } + } + findingRows = current.sorted(using: sortOrder) + + let visible = findingRows.filter { showIgnored || $0.status != .ignored } + var counts: [TableHealthCheckKind: Int] = [:] + for row in visible { + counts[row.id.kind, default: 0] += 1 + } + checkCounts = counts + allFindingsCount = visible.count + + let checkCount = checkPositions.count + let resolved = ledger.resolved.map { entry -> TableHealthFindingRow in + let kind = entry.id.kind + let position = checkPositions[kind] + ?? (title: kind.title, order: checkCount + (TableHealthCheckKind.allCases.firstIndex(of: kind) ?? 0)) + return TableHealthFindingRow(resolved: entry, checkTitle: position.title, checkOrder: position.order) + } + resolvedCount = resolved.count + + let inScope: [TableHealthFindingRow] + switch scope { + case .all: + inScope = visible + hiddenIgnoredCount = findingRows.count - visible.count + case .check(let kind): + inScope = visible.filter { $0.id.kind == kind } + hiddenIgnoredCount = showIgnored ? 0 : findingRows.filter { $0.id.kind == kind && $0.status == .ignored }.count + case .resolved: + inScope = resolved.sorted(using: resolvedSortOrder) + hiddenIgnoredCount = 0 + } + scopeRowCount = inScope.count + + let filter = filterText.trimmingCharacters(in: .whitespacesAndNewlines) + rows = filter.isEmpty ? inScope : inScope.filter { $0.matches(filter) } + } + + private func rowStatus(of id: TableHealthFindingID) -> TableHealthRowStatus { + switch ledger.status(of: id) { + case .new: return .new + case .ignored: return .ignored + case .none: return .seen + } + } + + func badgeCount(for kind: TableHealthCheckKind) -> Int { + checkCounts[kind] ?? 0 + } + + // The selection as the table shows it: a row the filter or the scope hides is not acted on. + var selectedRows: [TableHealthFindingRow] { + rows.filter { selection.contains($0.id) } + } + + var selectedFindings: [TableHealthFinding] { + selectedRows.compactMap(\.finding) + } + + var shownFindings: [TableHealthFinding] { + rows.compactMap(\.finding) + } + + func rows(for ids: Set) -> [TableHealthFindingRow] { + rows.filter { ids.contains($0.id) } + } + + var hasExportableRows: Bool { + !findingRows.isEmpty || !rows.isEmpty + } + + var currentCheck: TableHealthCheckResult? { + guard case .check(let kind) = scope else { return nil } + return report?.result(for: kind) + } + + // A check that failed but kept its earlier findings shows them under this line. + var checkFailure: String? { + guard let currentCheck, case .failed(let message) = currentCheck.outcome, !currentCheck.findings.isEmpty + else { return nil } + return message + } + + var notes: [String] { + switch scope { + case .all: + return report?.notes ?? [] + case .check: + return (report?.notes ?? []) + [currentCheck?.note].compactMap { $0 } + case .resolved: + return [] + } + } + + var listState: TableHealthListState { + if !rows.isEmpty { return .rows } + if let currentCheck { + switch currentCheck.outcome { + case .skipped(let reason): + return .skipped(reason) + case .failed(let message) where currentCheck.findings.isEmpty: + return .failed(message) + case .failed, .completed: + break + } + } + if !filterText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, scopeRowCount > 0 { + return .noResults + } + if hiddenIgnoredCount > 0 { return .ignoredHidden(hiddenIgnoredCount) } + switch scope { + case .all: + return .noFindings(String(localized: "Every check that ran found nothing.")) + case .check: + return .noFindings(currentCheck?.check.summary ?? "") + case .resolved: + return .noResolved + } + } + + // MARK: - Fixes + + nonisolated static func hasFix(_ finding: TableHealthFinding) -> Bool { + guard let fix = finding.fixSQL else { return false } + return !fix.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + } + + // Every fix in one script, each under a comment naming its finding: what Save Fix Script writes. + func fixScript(for findings: [TableHealthFinding], databaseType: DatabaseType) -> String? { + let fixes = findings.filter(Self.hasFix) + guard !fixes.isEmpty, let report else { return nil } + return TableHealthFixScript.script( + database: report.database, + generatedAt: report.generatedAt, + databaseType: databaseType, + findings: fixes, + checkTitle: { report.result(for: $0)?.check.title ?? $0.title } + ) + } + + // One fix as it is; several as the script, so the preview, Copy and Open in Editor show the same text. + func fixText(for findings: [TableHealthFinding], databaseType: DatabaseType) -> String? { + let fixes = findings.filter(Self.hasFix) + if fixes.count == 1 { return fixes[0].fixSQL } + return fixScript(for: fixes, databaseType: databaseType) + } + + // MARK: - Copy and export + + func tsv(for rows: [TableHealthFindingRow]) -> String? { + guard !rows.isEmpty else { return nil } + return TableHealthTSV.text(rows, columns: scope == .resolved ? TableHealthColumn.resolved : TableHealthColumn.findings) + } + + // A snapshot: a refresh while the dialog is open does not change what it writes or the counts it shows. + func exportRequest() -> DataSourceExportRequest? { + guard hasExportableRows else { return nil } + let exportDatabase = report?.database ?? databaseName + let makeScope: (String, String, [TableHealthFindingRow]) -> DataSourceExportScope = { id, title, rows in + DataSourceExportScope(id: id, title: String(format: title, rows.count.formatted()), rowCount: rows.count) { + TableHealthExportDataSource(database: exportDatabase, rows: rows) + } + } + var scopes = [makeScope(Self.exportScopeAll, String(localized: "All Findings (%@)"), findingRows)] + let showsSubset = scope != .all + || !filterText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + || hiddenIgnoredCount > 0 + if showsSubset { + scopes.append(makeScope(Self.exportScopeShown, String(localized: "Shown Findings (%@)"), rows)) + } + let selected = selectedRows + if !selected.isEmpty { + scopes.append(makeScope(Self.exportScopeSelected, String(localized: "Selected Findings (%@)"), selected)) + } + let title = String(format: String(localized: "Table Health for %@"), exportDatabase) + return DataSourceExportRequest( + title: title, + suggestedFileName: title, + scopes: scopes, + initialScopeId: showsSubset ? Self.exportScopeShown : Self.exportScopeAll + ) + } } diff --git a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift index 46c9da5eb0..b5c02080e7 100644 --- a/apple/TablePro/Views/Main/Child/MainEditorContentView.swift +++ b/apple/TablePro/Views/Main/Child/MainEditorContentView.swift @@ -55,7 +55,6 @@ struct MainEditorContentView: View { @State private var serverDashboardViewModels: [UUID: ServerDashboardViewModel] = [:] @State private var usersRolesViewModels: [UUID: UsersRolesViewModel] = [:] @State private var queryInsightsViewModels: [UUID: QueryInsightsViewModel] = [:] - @State private var tableHealthViewModels: [UUID: TableHealthViewModel] = [:] @State private var tableListingModels = TableListingModelStore() @ObservedObject private var treeService = DatabaseTreeMetadataService.shared @@ -155,7 +154,6 @@ struct MainEditorContentView: View { serverDashboardViewModels = serverDashboardViewModels.filter { openTabIds.contains($0.key) } usersRolesViewModels = usersRolesViewModels.filter { openTabIds.contains($0.key) } queryInsightsViewModels = queryInsightsViewModels.filter { openTabIds.contains($0.key) } - tableHealthViewModels = tableHealthViewModels.filter { openTabIds.contains($0.key) } tableListingModels.retainTabs(openTabIds) SchemaProviderRegistry.shared.reclaimUnheldProviders(for: connectionId) } @@ -331,45 +329,22 @@ struct MainEditorContentView: View { private func tableHealthContent(tab: QueryTab) -> some View { Group { - if let vm = tableHealthViewModels[tab.id] { - TableHealthView(viewModel: vm, databaseType: connection.type, actions: tableHealthActions) - .onReceive(AppEvents.shared.tabRefreshRequested) { requestedTabId in - guard requestedTabId == tab.id else { return } - Task { await vm.refresh() } - } + if let model = coordinator.tableHealthModels[tab.id] { + TableHealthView( + viewModel: model, + databaseName: tab.tableContext.databaseName, + databaseType: connection.type, + actions: coordinator.tableHealthActions(for: model) + ) } else { ProgressView(String(localized: "Loading table health…")) .frame(maxWidth: .infinity, maxHeight: .infinity) - .onAppear { - guard tableHealthViewModels[tab.id] == nil else { return } - tableHealthViewModels[tab.id] = TableHealthViewModel( - connectionId: connection.id, - provider: TableHealthProviderFactory.provider(for: connection.type) - ) - } + .onAppear { coordinator.tableHealthModel(for: tab.id) } } } .id(tab.id) } - private var tableHealthActions: TableHealthActions { - TableHealthActions( - openTable: { finding, database in - guard let table = finding.table else { return } - coordinator.openTableTab(table, schema: finding.schema, database: database) - }, - openFix: { finding, database in - guard let fix = finding.fixSQL else { return } - coordinator.openQuery( - fix, - on: connection.id, - databaseName: database, - intent: .loadIntoFrontTab - ) - } - ) - } - // MARK: - Users & Roles Tab Content @ViewBuilder diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift index b08726e990..e5dcc43095 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+Refresh.swift @@ -44,9 +44,14 @@ extension MainContentCoordinator { ) { guard let (tab, _) = tabManager.selectedTabAndIndex else { return } switch tab.tabType { - case .versionHistory, .tableListing, .serverDashboard, .insights, .tableHealth, .objectSource: + case .versionHistory, .tableListing, .serverDashboard, .insights, .objectSource: AppEvents.shared.tabRefreshRequested.send(tab.id) return + case .tableHealth: + if let model = tableHealthModels[tab.id] { + Task { await model.refresh() } + } + return case .erDiagram: if let diagram = erDiagramModels[tab.id] { Task { await diagram.refresh() } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift index 3a7f159b30..857cecd874 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TabClosing.swift @@ -74,6 +74,7 @@ extension MainContentCoordinator { prune(&createTableDrafts, keeping: openTabIds) prune(&userTypeEditors, keeping: openTabIds) prune(&erDiagramModels, keeping: openTabIds) + discardTableHealthModels(tableHealthModels.keys.filter { !openTabIds.contains($0) }) prune(&navigationHistories, keeping: openTabIds) toolbarState.forgetQueryTimings(keeping: openTabIds) for (tabId, session) in structureSessions where !openTabIds.contains(tabId) { @@ -95,6 +96,7 @@ extension MainContentCoordinator { structureSessions.removeValue(forKey: tabId)?.releaseViewWiring() createTableDrafts.removeValue(forKey: tabId) erDiagramModels.removeValue(forKey: tabId) + discardTableHealthModels([tabId]) tabsWithStagedPrincipals.remove(tabId) toolbarState.hasCreateTablePending = false toolbarState.hasPrincipalChanges = false @@ -108,6 +110,7 @@ extension MainContentCoordinator { createTableDrafts.removeValue(forKey: tab.id) userTypeEditors.removeValue(forKey: tab.id) erDiagramModels.removeValue(forKey: tab.id) + discardTableHealthModels([tab.id]) navigationHistories.removeValue(forKey: tab.id) releaseGridModel(forTab: tab.id) tableMetadataCache.removeValue(forKey: tab.id) diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift new file mode 100644 index 0000000000..32b14a5377 --- /dev/null +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift @@ -0,0 +1,87 @@ +import AppKit +import Foundation +import TableProUtilities + +extension MainContentCoordinator { + @discardableResult + func tableHealthModel(for tabId: UUID) -> TableHealthViewModel { + if let model = tableHealthModels[tabId] { return model } + let model = TableHealthViewModel( + connectionId: connectionId, + provider: TableHealthProviderFactory.provider(for: connection.type), + history: TableHealthHistoryStorage.shared, + database: { [weak self] in + self?.tabManager.tabs.first { $0.id == tabId }?.tableContext.databaseName ?? "" + } + ) + tableHealthModels[tabId] = model + return model + } + + // Every place a Table Health tab goes away comes here, so no report keeps the bulk metadata lane after its tab. + func discardTableHealthModels(_ tabIds: [UUID]) { + for tabId in tabIds { + tableHealthModels.removeValue(forKey: tabId)?.cancel() + } + } + + func tableHealthActions(for model: TableHealthViewModel) -> TableHealthActions { + TableHealthActions( + openTable: { [weak self, weak model] finding in + guard let self, let model, let table = finding.table else { return } + self.openTableTab(table, schema: finding.schema, database: model.databaseName) + }, + openInEditor: { [weak self, weak model] text in + guard let self, let model else { return } + self.openTableHealthFixes(text, database: model.databaseName) + }, + saveFixScript: { [weak self, weak model] script in + guard let self, let model else { return } + self.saveTableHealthFixScript(script, database: model.databaseName) + }, + export: { [weak self, weak model] in + guard let self, let model else { return } + self.openTableHealthExport(model) + } + ) + } + + // File > Export > Export Results… and the Actions menu land here for a Table Health tab. + func openTableHealthExport(forTab tabId: UUID) { + guard let model = tableHealthModels[tabId] else { return } + openTableHealthExport(model) + } + + func openTableHealthExport(_ model: TableHealthViewModel) { + guard admitsSheetCommand(), let request = model.exportRequest() else { return } + activeSheet = .exportDataSource(request) + } + + func openTableHealthFixes(_ text: String, database: String) { + WindowManager.shared.openTab( + payload: EditorTabPayload( + connectionId: connectionId, + tabType: .query, + databaseName: database, + initialQuery: text, + skipAutoExecute: true + ) + ) + } + + func saveTableHealthFixScript(_ script: String, database: String) { + let suggestedName = String(format: String(localized: "Table Health for %@"), database) + ".sql" + Task { @MainActor [weak self] in + guard let url = await SQLFileService.showSavePanel(suggestedName: suggestedName) else { return } + do { + try await SQLFileService.writeFile(content: script, to: url, encoding: .utf8) + } catch { + AlertHelper.showErrorSheet( + title: String(localized: "Could Not Save the Fix Script"), + message: error.localizedDescription, + window: self?.contentWindow + ) + } + } + } +} diff --git a/apple/TablePro/Views/Main/Extensions/MainContentView+TransferSheets.swift b/apple/TablePro/Views/Main/Extensions/MainContentView+TransferSheets.swift index 0d46f85082..519116c285 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentView+TransferSheets.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentView+TransferSheets.swift @@ -52,6 +52,12 @@ extension MainContentView { ) } } + case .exportDataSource(let request): + ExportDialog( + isPresented: dismissBinding, + mode: .dataSource(request), + sheetWork: coordinator.sheetWork + ) case .importDialog(let formatId): ImportDialog( isPresented: dismissBinding, diff --git a/apple/TablePro/Views/Main/MainContentCommandActions.swift b/apple/TablePro/Views/Main/MainContentCommandActions.swift index ee0a6abd59..97c20f1071 100644 --- a/apple/TablePro/Views/Main/MainContentCommandActions.swift +++ b/apple/TablePro/Views/Main/MainContentCommandActions.swift @@ -468,6 +468,12 @@ final class MainContentCommandActions: ObservableObject { return !coordinator.tabSessionRegistry.tableRows(for: tab.id).rows.isEmpty } + // Export Results on a Table Health tab exports its findings, which are not a result set. + var hasTableHealthFindings: Bool { + guard let coordinator, let tab = coordinator.tabManager.selectedTab, tab.tabType == .tableHealth else { return false } + return coordinator.tableHealthModels[tab.id]?.hasExportableRows == true + } + var hasRowSelection: Bool { selectionOwner != .none && coordinator?.activeGridModel?.selection.isEmpty == false } @@ -976,8 +982,12 @@ final class MainContentCommandActions: ObservableObject { } func exportQueryResults() { - guard coordinator?.admitsSheetCommand() == true else { return } - coordinator?.openExportQueryResultsDialog() + guard let coordinator, coordinator.admitsSheetCommand() else { return } + if let tab = coordinator.tabManager.selectedTab, tab.tabType == .tableHealth { + coordinator.openTableHealthExport(forTab: tab.id) + return + } + coordinator.openExportQueryResultsDialog() } func importData() { diff --git a/apple/TablePro/Views/Main/MainContentCoordinator.swift b/apple/TablePro/Views/Main/MainContentCoordinator.swift index 8d3cde5fab..3a65f56ef4 100644 --- a/apple/TablePro/Views/Main/MainContentCoordinator.swift +++ b/apple/TablePro/Views/Main/MainContentCoordinator.swift @@ -56,6 +56,8 @@ enum ActiveSheet: Identifiable { // The files are in `importBatch`, each imported into its own table. case rowImportBatch case exportQueryResults + // Carries its rows: Table Health exports the findings it lists, not a result set. + case exportDataSource(DataSourceExportRequest) // Carries the right-clicked tables: the object browser may point elsewhere by the time the sheet appears. case transferTables(tables: Set, schema: String?) // Carries the right-clicked databases, not the object browser's current one. Empty means the browse database. @@ -95,6 +97,7 @@ enum ActiveSheet: Identifiable { case .rowImport(let formatId): "rowImport-\(formatId)" case .rowImportBatch: "rowImportBatch" case .exportQueryResults: "exportQueryResults" + case .exportDataSource: "exportDataSource" case .transferTables(let tables, let schema): "transferTables-\(schema ?? "")-\(tables.sorted().joined(separator: ","))" case .backupDatabase: "backupDatabase" @@ -202,6 +205,8 @@ final class MainContentCoordinator: ObservableObject { @Published var userTypeEditors: [UUID: UserTypeEditorModel] = [:] // Held here for the same reason: the diagram stages schema edits that Save and the close gate reach. @Published var erDiagramModels: [UUID: ERDiagramViewModel] = [:] + // Held here so Refresh, File > Export and a tab close reach the model; a close cancels its report. + @Published var tableHealthModels: [UUID: TableHealthViewModel] = [:] // The cached view model keeps staged work after `usersRolesActions` is nilled; without this a background tab closes silently. internal var tabsWithStagedPrincipals: Set = [] @@ -878,6 +883,7 @@ final class MainContentCoordinator: ObservableObject { createTableDrafts.removeAll() userTypeEditors.removeAll() erDiagramModels.removeAll() + discardTableHealthModels(Array(tableHealthModels.keys)) displayFormatsCache.removeAll() tableMetadataCache.removeAll() schemaColumns.removeAll() diff --git a/apple/TablePro/Views/Main/MainContentView.swift b/apple/TablePro/Views/Main/MainContentView.swift index 366d35fafd..82eade5715 100644 --- a/apple/TablePro/Views/Main/MainContentView.swift +++ b/apple/TablePro/Views/Main/MainContentView.swift @@ -236,7 +236,7 @@ struct MainContentView: View { } case .generateRows(let session): GenerateRowsSheet(session: session) - case .exportDialog, .exportQueryResults, .importDialog, .rowImport, .rowImportBatch, + case .exportDialog, .exportQueryResults, .exportDataSource, .importDialog, .rowImport, .rowImportBatch, .transferTables, .backupDatabase, .restoreDatabase: transferSheetContent(for: sheet, dismiss: dismissBinding) case .maintenance(let operation, let tableName, let database, let schema): diff --git a/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift b/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift new file mode 100644 index 0000000000..c0820b3eb5 --- /dev/null +++ b/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift @@ -0,0 +1,71 @@ +import SwiftUI + +struct TableHealthChecksList: View { + // HIG, Offering help: a tooltip past about 75 characters stops being read. Longer text shows in the pane. + private static let tooltipLimit = 75 + + @ObservedObject var viewModel: TableHealthViewModel + + var body: some View { + List(selection: scopeSelection) { + Label(String(localized: "All Findings"), systemImage: "tray.full") + .badge(viewModel.allFindingsCount) + .tag(TableHealthScope.all) + if let report = viewModel.report { + Section(String(localized: "Checks")) { + ForEach(report.checks) { result in + Label(result.check.title, systemImage: result.check.systemImage) + .badge(badge(for: result)) + .help(tooltip(for: result)) + .tag(TableHealthScope.check(result.kind)) + } + } + } + Section(String(localized: "History")) { + Label(String(localized: "Resolved"), systemImage: "clock.arrow.circlepath") + .badge(viewModel.resolvedCount) + .tag(TableHealthScope.resolved) + } + } + .listStyle(.sidebar) + .accessibilityLabel(String(localized: "Checks")) + .contextMenu(forSelectionType: TableHealthScope.self) { scopes in + if scopes.contains(.resolved), viewModel.resolvedCount > 0 { + Button(String(localized: "Clear Resolved")) { viewModel.clearResolved() } + } + } + } + + // A List cannot deselect into nothing here: some scope is always shown. + private var scopeSelection: Binding { + Binding( + get: { viewModel.scope }, + set: { scope in + guard let scope else { return } + viewModel.scope = scope + } + ) + } + + private func badge(for result: TableHealthCheckResult) -> Text? { + switch result.outcome { + case .completed: + let findings = viewModel.badgeCount(for: result.kind) + return findings == 0 ? nil : Text(verbatim: findings.formatted()) + case .skipped: + return Text("Skipped") + case .failed: + return Text("Failed") + } + } + + private func tooltip(for result: TableHealthCheckResult) -> String { + let text: String + switch result.outcome { + case .completed: text = result.check.summary + case .skipped(let reason): text = reason + case .failed(let message): text = message + } + return text.count <= Self.tooltipLimit ? text : "" + } +} diff --git a/apple/TablePro/Views/TableHealth/TableHealthFindingsPane.swift b/apple/TablePro/Views/TableHealth/TableHealthFindingsPane.swift new file mode 100644 index 0000000000..c7abf6b6a4 --- /dev/null +++ b/apple/TablePro/Views/TableHealth/TableHealthFindingsPane.swift @@ -0,0 +1,97 @@ +import SwiftUI + +struct TableHealthFindingsPane: View { + @ObservedObject var viewModel: TableHealthViewModel + let databaseType: DatabaseType + let actions: TableHealthActions + + var body: some View { + VStack(alignment: .leading, spacing: 0) { + if hasHeader { + header + Divider() + } + content + } + .frame(maxWidth: .infinity, maxHeight: .infinity) + } + + private var summary: String? { + guard let summary = viewModel.currentCheck?.check.summary, !summary.isEmpty else { return nil } + return summary + } + + private var hasHeader: Bool { + summary != nil || viewModel.checkFailure != nil || !viewModel.notes.isEmpty + } + + private var header: some View { + VStack(alignment: .leading, spacing: 4) { + if let summary { + Text(verbatim: summary) + } + if let failure = viewModel.checkFailure { + Label(failure, systemImage: "exclamationmark.triangle") + } + ForEach(viewModel.notes, id: \.self) { note in + Label(note, systemImage: "info.circle") + } + } + .font(.callout) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + .frame(maxWidth: .infinity, alignment: .leading) + .padding(.horizontal, 12) + .padding(.vertical, 8) + } + + @ViewBuilder + private var content: some View { + switch viewModel.listState { + case .rows: + if viewModel.scope == .resolved { + TableHealthResolvedTable(viewModel: viewModel) + } else { + TableHealthFindingsTable(viewModel: viewModel, databaseType: databaseType, actions: actions) + } + case .noFindings(let description): + UnavailableStateView( + String(localized: "No Findings"), + systemImage: "checkmark.circle", + description: description.isEmpty ? nil : Text(verbatim: description) + ) + case .skipped(let reason): + UnavailableStateView( + String(localized: "Check Skipped"), + systemImage: "info.circle", + description: Text(verbatim: reason) + ) + case .failed(let message): + UnavailableStateView { + Label(String(localized: "Check Failed"), systemImage: "exclamationmark.triangle") + } description: { + Text(verbatim: message) + } actions: { + Button(String(localized: "Try Again")) { + Task { await viewModel.refresh() } + } + } + case .noResults: + UnavailableStateView.search(text: viewModel.filterText) + case .ignoredHidden(let count): + UnavailableStateView { + Label(String(localized: "No Findings"), systemImage: "eye.slash") + } description: { + Text("^[\(count) ignored finding](inflect: true) hidden.") + } actions: { + Button(String(localized: "Show Ignored")) { viewModel.showIgnored = true } + } + case .noResolved: + UnavailableStateView( + String(localized: "No Resolved Findings"), + systemImage: "clock.arrow.circlepath", + description: Text("A finding moves here when a refresh no longer reports it.") + ) + } + } +} diff --git a/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift b/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift new file mode 100644 index 0000000000..8e993c786c --- /dev/null +++ b/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift @@ -0,0 +1,191 @@ +import AppKit +import SwiftUI + +// One fixed column set: an `if` among table columns is macOS 14.4, so the Check column stays in every scope. +struct TableHealthFindingsTable: View { + @ObservedObject var viewModel: TableHealthViewModel + let databaseType: DatabaseType + let actions: TableHealthActions + + var body: some View { + Table(viewModel.rows, selection: $viewModel.selection, sortOrder: $viewModel.sortOrder) { + TableColumn(TableHealthColumn.status.title, value: \TableHealthFindingRow.statusRank) { row in + Text(verbatim: TableHealthColumn.status.text(for: row)) + } + .width(min: 44, ideal: 56, max: 90) + TableColumn(TableHealthColumn.check.title, value: \TableHealthFindingRow.checkOrder) { row in + Text(verbatim: row.checkTitle) + .lineLimit(1) + } + .width(min: 80, ideal: 150) + TableColumn( + TableHealthColumn.table.title, + value: \TableHealthFindingRow.tableSortKey, + comparator: String.StandardComparator.localizedStandard + ) { row in + Text(verbatim: row.tableSortKey) + .lineLimit(1) + .truncationMode(.middle) + } + .width(min: 80, ideal: 160) + TableColumn( + TableHealthColumn.object.title, + value: \TableHealthFindingRow.objectSortKey, + comparator: String.StandardComparator.localizedStandard + ) { row in + Text(verbatim: row.objectSortKey) + .lineLimit(1) + .truncationMode(.middle) + } + .width(min: 60, ideal: 140) + TableColumn(TableHealthColumn.size.title, value: \TableHealthFindingRow.sizeSortKey) { row in + Text(verbatim: TableHealthColumn.size.text(for: row)) + .monospacedDigit() + .frame(maxWidth: .infinity, alignment: .trailing) + } + .width(min: 50, ideal: 72, max: 110) + TableColumn( + TableHealthColumn.detail.title, + value: \TableHealthFindingRow.detail, + comparator: String.StandardComparator.localizedStandard + ) { row in + Text(verbatim: row.detail) + .lineLimit(1) + .help(row.detail) + } + .width(min: 120, ideal: 280) + } + .tableStyle(.inset) + .alternatingRowBackgroundsCompat() + .accessibilityLabel(String(localized: "Findings")) + .contextMenu(forSelectionType: TableHealthFindingID.self) { ids in + menu(for: viewModel.rows(for: ids)) + } primaryAction: { ids in + if let finding = Self.singleTableFinding(in: viewModel.rows(for: ids)) { + actions.openTable(finding) + } + } + .onCopyCommand { + TableHealthCopy.providers(viewModel.tsv(for: viewModel.selectedRows)) + } + } + + // HIG, Context menus: unavailable items are left out, Copy aside. + @ViewBuilder + private func menu(for rows: [TableHealthFindingRow]) -> some View { + if !rows.isEmpty { + let findings = rows.compactMap(\.finding) + let fixCount = findings.filter(TableHealthViewModel.hasFix).count + if let finding = Self.singleTableFinding(in: rows) { + Button(String(localized: "Open Table")) { actions.openTable(finding) } + } + if fixCount > 0 { + Button(fixCount == 1 ? String(localized: "Open Fix in Editor") : String(localized: "Open Fixes in Editor")) { + if let text = viewModel.fixText(for: findings, databaseType: databaseType) { + actions.openInEditor(text) + } + } + } + Divider() + Button(String(localized: "Copy")) { + if let text = viewModel.tsv(for: rows) { + ClipboardService.shared.writeText(text) + } + } + if fixCount > 0 { + Button(String(localized: "Copy Fix SQL")) { + if let text = viewModel.fixText(for: findings, databaseType: databaseType) { + ClipboardService.shared.writeText(text) + } + } + } + Divider() + let ids = Set(rows.map(\.id)) + if rows.allSatisfy({ $0.status == .ignored }) { + Button(String(localized: "Stop Ignoring")) { viewModel.setIgnored(ids, ignored: false) } + } else { + Button(String(localized: "Ignore")) { viewModel.setIgnored(ids, ignored: true) } + } + if fixCount > 0 { + Button(String(localized: "Save Fix Script…")) { + if let script = viewModel.fixScript(for: findings, databaseType: databaseType) { + actions.saveFixScript(script) + } + } + } + } + } + + // Open Table applies when the rows name one table, however many findings it has. + static func singleTableFinding(in rows: [TableHealthFindingRow]) -> TableHealthFinding? { + let findings = rows.compactMap(\.finding).filter { $0.table != nil } + guard let first = findings.first, + findings.allSatisfy({ $0.schema == first.schema && $0.table == first.table }) + else { return nil } + return first + } +} + +struct TableHealthResolvedTable: View { + @ObservedObject var viewModel: TableHealthViewModel + + var body: some View { + Table(viewModel.rows, selection: $viewModel.selection, sortOrder: $viewModel.resolvedSortOrder) { + TableColumn(TableHealthColumn.check.title, value: \TableHealthFindingRow.checkOrder) { row in + Text(verbatim: row.checkTitle) + .lineLimit(1) + } + .width(min: 80, ideal: 150) + TableColumn( + TableHealthColumn.table.title, + value: \TableHealthFindingRow.tableSortKey, + comparator: String.StandardComparator.localizedStandard + ) { row in + Text(verbatim: row.tableSortKey) + .lineLimit(1) + .truncationMode(.middle) + } + .width(min: 80, ideal: 180) + TableColumn( + TableHealthColumn.object.title, + value: \TableHealthFindingRow.objectSortKey, + comparator: String.StandardComparator.localizedStandard + ) { row in + Text(verbatim: row.objectSortKey) + .lineLimit(1) + .truncationMode(.middle) + } + .width(min: 60, ideal: 160) + TableColumn(TableHealthColumn.lastSeen.title, value: \TableHealthFindingRow.lastSeenSortKey) { row in + Text(verbatim: TableHealthColumn.lastSeen.text(for: row)) + .monospacedDigit() + } + .width(min: 100, ideal: 160) + } + .tableStyle(.inset) + .alternatingRowBackgroundsCompat() + .accessibilityLabel(String(localized: "Resolved Findings")) + .contextMenu(forSelectionType: TableHealthFindingID.self) { ids in + let rows = viewModel.rows(for: ids) + if !rows.isEmpty { + Button(String(localized: "Copy")) { + if let text = viewModel.tsv(for: rows) { + ClipboardService.shared.writeText(text) + } + } + Divider() + } + Button(String(localized: "Clear Resolved")) { viewModel.clearResolved() } + } + .onCopyCommand { + TableHealthCopy.providers(viewModel.tsv(for: viewModel.selectedRows)) + } + } +} + +enum TableHealthCopy { + static func providers(_ text: String?) -> [NSItemProvider] { + guard let text else { return [] } + return [NSItemProvider(object: text as NSString)] + } +} diff --git a/apple/TablePro/Views/TableHealth/TableHealthLayoutMetrics.swift b/apple/TablePro/Views/TableHealth/TableHealthLayoutMetrics.swift new file mode 100644 index 0000000000..ee56b668ff --- /dev/null +++ b/apple/TablePro/Views/TableHealth/TableHealthLayoutMetrics.swift @@ -0,0 +1,13 @@ +import CoreGraphics + +internal enum TableHealthLayoutMetrics { + static let checksListMinimumWidth: CGFloat = 180 + static let checksListMaximumWidth: CGFloat = 280 + static let findingsMinimumWidth: CGFloat = 320 + static let findingsMinimumHeight: CGFloat = 160 + static let previewMinimumHeight: CGFloat = 150 + + // The pane bar at its narrowest, with the database name truncated and the filter at 120: wider than the checks + // list and the findings table need side by side, so the bar sets the floor. + static let tabMinimumWidth: CGFloat = 560 +} diff --git a/apple/TablePro/Views/TableHealth/TableHealthPaneBar.swift b/apple/TablePro/Views/TableHealth/TableHealthPaneBar.swift new file mode 100644 index 0000000000..51b36276a8 --- /dev/null +++ b/apple/TablePro/Views/TableHealth/TableHealthPaneBar.swift @@ -0,0 +1,61 @@ +import SwiftUI + +struct TableHealthPaneBar: View { + @ObservedObject var viewModel: TableHealthViewModel + let databaseName: String + let databaseType: DatabaseType + let actions: TableHealthActions + + var body: some View { + HStack(spacing: 8) { + Text(verbatim: databaseName) + .font(.callout.weight(.semibold)) + .lineLimit(1) + .truncationMode(.middle) + .frame(minWidth: 0, alignment: .leading) + if viewModel.report != nil, let error = viewModel.loadError { + warning(error) + } + if let error = viewModel.storeError { + warning(String(format: String(localized: "Could not update the finding history. %@"), error)) + } + + Spacer(minLength: 8) + + NativeSearchField( + text: $viewModel.filterText, + placeholder: String(localized: "Filter"), + controlSize: .small, + accessibilityIdentifier: "table-health-filter" + ) + .frame(minWidth: 120, idealWidth: 180, maxWidth: 180) + Toggle(String(localized: "Show Ignored"), isOn: $viewModel.showIgnored) + .toggleStyle(.checkbox) + Button(String(localized: "Export…")) { actions.export() } + .disabled(!viewModel.hasExportableRows) + Button(String(localized: "Save Fix Script…")) { + if let script = viewModel.fixScript(for: viewModel.shownFindings, databaseType: databaseType) { + actions.saveFixScript(script) + } + } + .disabled(!viewModel.shownFindings.contains(where: TableHealthViewModel.hasFix)) + PaneRefreshControls( + isRefreshing: viewModel.isRefreshing, + lastRefreshDate: viewModel.lastRefreshDate, + accessibilityIdentifier: "table-health-refresh-button" + ) { + Task { await viewModel.refresh() } + } + } + .controlSize(.small) + .padding(.horizontal, 10) + .padding(.vertical, 6) + } + + private func warning(_ message: String) -> some View { + Image(systemName: "exclamationmark.triangle") + .foregroundStyle(.secondary) + .help(message) + .accessibilityLabel(message) + } +} diff --git a/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift b/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift new file mode 100644 index 0000000000..6c98e11012 --- /dev/null +++ b/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift @@ -0,0 +1,127 @@ +import SwiftUI + +struct TableHealthPreviewPane: View { + @ObservedObject var viewModel: TableHealthViewModel + let databaseType: DatabaseType + let actions: TableHealthActions + + var body: some View { + let findings = viewModel.selectedFindings + if findings.count == 1, let finding = findings.first { + single(finding) + } else if findings.count > 1 { + several(findings) + } else { + Text("Select a finding to preview the SQL that fixes it.") + .font(.callout) + .foregroundStyle(.secondary) + .frame(maxWidth: .infinity, maxHeight: .infinity) + } + } + + private func single(_ finding: TableHealthFinding) -> some View { + let fix = TableHealthViewModel.hasFix(finding) ? finding.fixSQL : nil + return VStack(alignment: .leading, spacing: 8) { + HStack(spacing: 8) { + Text(verbatim: finding.qualifiedName) + .font(.headline) + .lineLimit(1) + .truncationMode(.middle) + Spacer(minLength: 8) + Button(String(localized: "Open Table")) { actions.openTable(finding) } + .disabled(finding.table == nil) + Button(String(localized: "Copy")) { + if let fix { ClipboardService.shared.writeText(fix) } + } + .disabled(fix == nil) + Button(String(localized: "Open in Editor")) { + if let fix { actions.openInEditor(fix) } + } + .disabled(fix == nil) + } + Text(verbatim: finding.detail) + .font(.callout) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + facts(finding) + if let fix { + SQLStatementPreview( + prepared: SQLReviewSheet.build(statements: [fix], databaseType: databaseType), + databaseType: databaseType + ) + .id(finding.id) + previewNote + } else { + Text("This finding has no SQL fix.") + .font(.callout) + .foregroundStyle(.secondary) + .frame(maxWidth: .infinity, maxHeight: .infinity) + } + } + .padding(12) + } + + @ViewBuilder + private func facts(_ finding: TableHealthFinding) -> some View { + if finding.sizeBytes != nil || !finding.metrics.isEmpty { + Grid(alignment: .leadingFirstTextBaseline, horizontalSpacing: 12, verticalSpacing: 4) { + if let size = finding.sizeBytes { + GridRow { + Text(verbatim: TableHealthColumn.size.title) + .foregroundStyle(.secondary) + Text(verbatim: ByteSizeFormatting.string(bytes: size)) + } + } + ForEach(finding.metrics, id: \.id) { metric in + GridRow { + Text(verbatim: metric.label) + .foregroundStyle(.secondary) + Text(verbatim: metric.formattedValue) + } + } + } + .font(.callout) + .textSelection(.enabled) + } + } + + private func several(_ findings: [TableHealthFinding]) -> some View { + let fixCount = findings.filter(TableHealthViewModel.hasFix).count + let script = viewModel.fixText(for: findings, databaseType: databaseType) + return VStack(alignment: .leading, spacing: 8) { + HStack(spacing: 8) { + Text("^[\(fixCount) fix](inflect: true)") + .font(.headline) + Spacer(minLength: 8) + Button(String(localized: "Copy")) { + if let script { ClipboardService.shared.writeText(script) } + } + .disabled(script == nil) + Button(String(localized: "Open in Editor")) { + if let script { actions.openInEditor(script) } + } + .disabled(script == nil) + } + if let script { + SQLStatementPreview( + prepared: SQLReviewSheet.build(statements: [script], databaseType: databaseType, verbatim: true), + databaseType: databaseType + ) + .id(script) + previewNote + } else { + Text("The selected findings have no SQL fix.") + .font(.callout) + .foregroundStyle(.secondary) + .frame(maxWidth: .infinity, maxHeight: .infinity) + } + } + .padding(12) + } + + private var previewNote: some View { + Text("This SQL is a preview. TablePro never runs it; review it in the editor and run it yourself.") + .font(.caption) + .foregroundStyle(.secondary) + } +} diff --git a/apple/TablePro/Views/TableHealth/TableHealthView.swift b/apple/TablePro/Views/TableHealth/TableHealthView.swift index 2a860e04a5..3f563a4973 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthView.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthView.swift @@ -2,17 +2,15 @@ import SwiftUI @MainActor struct TableHealthActions { - let openTable: (_ finding: TableHealthFinding, _ database: String) -> Void - let openFix: (_ finding: TableHealthFinding, _ database: String) -> Void - - func copyFix(_ finding: TableHealthFinding) { - guard let fix = finding.fixSQL else { return } - ClipboardService.shared.writeText(fix) - } + let openTable: (TableHealthFinding) -> Void + let openInEditor: (String) -> Void + let saveFixScript: (String) -> Void + let export: () -> Void } struct TableHealthView: View { @ObservedObject var viewModel: TableHealthViewModel + let databaseName: String let databaseType: DatabaseType let actions: TableHealthActions @@ -20,7 +18,16 @@ struct TableHealthView: View { var body: some View { VStack(spacing: 0) { if viewModel.isSupported { - header + VStack(spacing: 0) { + TableHealthPaneBar( + viewModel: viewModel, + databaseName: databaseName, + databaseType: databaseType, + actions: actions + ) + Divider() + } + .paneBarBackground() } content } @@ -38,15 +45,25 @@ struct TableHealthView: View { ) } else if viewModel.report != nil { AutosavingSplitView( - autosaveName: "com.TablePro.tableHealth.findingsDetail", - isVertical: false, - primaryMinimum: 160, - secondaryMinimum: 150, - primaryHoldingPriority: .splitPaneFlexible + autosaveName: "com.TablePro.tableHealth.checksFindings", + primaryMinimum: TableHealthLayoutMetrics.checksListMinimumWidth, + primaryMaximum: TableHealthLayoutMetrics.checksListMaximumWidth, + secondaryMinimum: TableHealthLayoutMetrics.findingsMinimumWidth, + collapsesPrimaryWhenTight: true ) { - TableHealthFindingsList(viewModel: viewModel, actions: actions) + TableHealthChecksList(viewModel: viewModel) } secondary: { - TableHealthDetailPane(viewModel: viewModel, databaseType: databaseType, actions: actions) + AutosavingSplitView( + autosaveName: "com.TablePro.tableHealth.findingsDetail", + isVertical: false, + primaryMinimum: TableHealthLayoutMetrics.findingsMinimumHeight, + secondaryMinimum: TableHealthLayoutMetrics.previewMinimumHeight, + primaryHoldingPriority: .splitPaneFlexible + ) { + TableHealthFindingsPane(viewModel: viewModel, databaseType: databaseType, actions: actions) + } secondary: { + TableHealthPreviewPane(viewModel: viewModel, databaseType: databaseType, actions: actions) + } } } else if let error = viewModel.loadError { UnavailableStateView { @@ -63,164 +80,4 @@ struct TableHealthView: View { .frame(maxWidth: .infinity, maxHeight: .infinity) } } - - private var header: some View { - VStack(spacing: 0) { - HStack(spacing: 8) { - if let report = viewModel.report { - Text(verbatim: report.database) - .font(.callout.weight(.semibold)) - Text("^[\(report.findingCount) finding](inflect: true)") - .font(.callout) - .monospacedDigit() - if let error = viewModel.loadError { - Image(systemName: "exclamationmark.triangle") - .foregroundStyle(.secondary) - .help(error) - .accessibilityLabel(error) - } - } - - Spacer(minLength: 8) - - PaneRefreshControls( - isRefreshing: viewModel.isRefreshing, - lastRefreshDate: viewModel.lastRefreshDate, - accessibilityIdentifier: "table-health-refresh-button" - ) { - Task { await viewModel.refresh() } - } - } - .padding(.horizontal, 10) - .padding(.vertical, 6) - - Divider() - } - .paneBarBackground() - } -} - -private struct TableHealthFindingsList: View { - @ObservedObject var viewModel: TableHealthViewModel - let actions: TableHealthActions - - var body: some View { - if let report = viewModel.report { - List(selection: $viewModel.selectedFindingId) { - if !report.notes.isEmpty { - Section { - ForEach(report.notes, id: \.self) { note in - Label(note, systemImage: "info.circle") - .font(.callout) - .foregroundStyle(.secondary) - } - } - } - ForEach(report.checks) { check in - Section { - rows(check) - } header: { - HStack { - Label(check.kind.title, systemImage: check.kind.systemImage) - Spacer() - if check.outcome == .completed || !check.findings.isEmpty { - Text(verbatim: "\(check.findings.count)") - .monospacedDigit() - } - } - } - } - } - .contextMenu(forSelectionType: TableHealthFinding.ID.self) { ids in - if let finding = finding(in: ids, of: report) { - Button(String(localized: "Open Table")) { actions.openTable(finding, report.database) } - Button(String(localized: "Open Fix in Editor")) { actions.openFix(finding, report.database) } - Button(String(localized: "Copy Fix SQL")) { actions.copyFix(finding) } - } - } primaryAction: { ids in - if let finding = finding(in: ids, of: report) { - actions.openTable(finding, report.database) - } - } - } - } - - @ViewBuilder - private func rows(_ check: TableHealthCheckResult) -> some View { - switch check.outcome { - case .failed(let message): - status(message, systemImage: "exclamationmark.triangle") - case .skipped(let reason): - status(reason, systemImage: "info.circle") - case .completed: - if check.findings.isEmpty { - Text("No findings.") - .font(.callout) - .foregroundStyle(.secondary) - } - } - ForEach(check.findings) { finding in - VStack(alignment: .leading, spacing: 2) { - Text(verbatim: finding.qualifiedTable ?? finding.qualifiedName) - Text(verbatim: finding.detail) - .font(.callout) - .foregroundStyle(.secondary) - .lineLimit(2) - } - .padding(.vertical, 2) - .tag(finding.id) - } - } - - private func status(_ message: String, systemImage: String) -> some View { - Label { - Text(verbatim: message) - } icon: { - Image(systemName: systemImage) - } - .font(.callout) - .foregroundStyle(.secondary) - } - - private func finding(in ids: Set, of report: TableHealthReport) -> TableHealthFinding? { - guard ids.count == 1, let id = ids.first else { return nil } - return report.finding(id: id) - } -} - -private struct TableHealthDetailPane: View { - @ObservedObject var viewModel: TableHealthViewModel - let databaseType: DatabaseType - let actions: TableHealthActions - - var body: some View { - if let finding = viewModel.selectedFinding, let database = viewModel.report?.database { - VStack(alignment: .leading, spacing: 8) { - HStack(spacing: 8) { - Text(verbatim: finding.qualifiedTable ?? finding.qualifiedName) - .font(.headline) - .lineLimit(1) - .truncationMode(.middle) - Spacer(minLength: 8) - Button(String(localized: "Open Table")) { actions.openTable(finding, database) } - Button(String(localized: "Copy")) { actions.copyFix(finding) } - Button(String(localized: "Open in Editor")) { actions.openFix(finding, database) } - } - SQLStatementPreview( - prepared: SQLReviewSheet.build(statements: finding.fixSQL.map { [$0] } ?? [], databaseType: databaseType), - databaseType: databaseType - ) - .id(finding.id) - Text("This SQL is a preview. TablePro never runs it; review it in the editor and run it yourself.") - .font(.caption) - .foregroundStyle(.secondary) - } - .padding(12) - } else { - Text("Select a finding to preview the SQL that fixes it.") - .font(.callout) - .foregroundStyle(.secondary) - .frame(maxWidth: .infinity, maxHeight: .infinity) - } - } } diff --git a/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift b/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift new file mode 100644 index 0000000000..7338c0ceef --- /dev/null +++ b/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift @@ -0,0 +1,239 @@ +import Foundation +@testable import TablePro +import TableProMCP +import Testing + +struct GetTableHealthToolTests { + private typealias Fixture = TableHealthFixture + + private let orders = Fixture.finding(table: "orders") + private let invoices = Fixture.finding(table: "invoices") + private let logs = Fixture.finding(table: "logs") + + private static let day1 = Date(timeIntervalSince1970: 86_400) + + private func report(_ results: [TableHealthCheckResult], notes: [String] = []) -> TableHealthReport { + TableHealthReport(database: "shop", checks: results, notes: notes, generatedAt: Self.day1) + } + + private func result( + _ kind: TableHealthCheckKind, + _ outcome: TableHealthCheckResult.Outcome, + _ findings: [TableHealthFinding] = [], + note: String? = nil + ) -> TableHealthCheckResult { + TableHealthCheckResult( + check: TableHealthCheck(kind: kind, summary: "Summary"), + findings: findings, + outcome: outcome, + note: note + ) + } + + private func status(of finding: TableHealthFinding, in payload: JsonValue) -> String? { + payload["findings"]?.arrayValue? + .first { $0["table"]?.stringValue == finding.table }?["status"]?.stringValue + } + + private func keys(_ value: JsonValue?) -> Set { + Set(value?.objectValue.map { Array($0.keys) } ?? []) + } + + private func required(_ schema: JsonValue?) -> Set { + Set(schema?["required"]?.arrayValue?.compactMap { $0.stringValue } ?? []) + } + + @Test("A failed check reaches the client as fixed text, never the server's own words") + func failedCheckIsFixedText() { + let payload = MCPConnectionBridge.tableHealthPayload( + report([result(.bloat, .failed("permission denied for table secrets"))]), + ledger: TableHealthLedger() + ) + + let check = payload["checks"]?.arrayValue?.first + #expect(check?["outcome"]?.stringValue == "failed") + #expect(check?["reason"]?.stringValue == "The server did not answer this check.") + #expect(!MCPToolCallResult.encodeJsonString(payload).contains("secrets")) + } + + @Test("A skipped check passes its reason and note through, and report notes come along") + func skippedCheckKeepsItsReason() { + let payload = MCPConnectionBridge.tableHealthPayload( + report( + [result(.unusedIndex, .skipped("track_counts is off"), note: "Counts since the reset")], + notes: ["This server is a read-only standby."] + ), + ledger: TableHealthLedger() + ) + + let check = payload["checks"]?.arrayValue?.first + #expect(check?["outcome"]?.stringValue == "skipped") + #expect(check?["reason"]?.stringValue == "track_counts is off") + #expect(check?["note"]?.stringValue == "Counts since the reset") + #expect(payload["notes"] == .array([.string("This server is a read-only standby.")])) + #expect(payload["database"]?.stringValue == "shop") + #expect(payload["generated_at"]?.stringValue == "1970-01-02T00:00:00Z") + } + + @Test("Status comes from the stored history: new since its last run, or ignored") + func statusFollowsTheLedger() { + let ledger = TableHealthLedger() + .recording(report([result(.missingPrimaryKey, .completed, [orders, logs])]), at: Self.day1) + .ignoring([logs.id], at: Self.day1) + + let payload = MCPConnectionBridge.tableHealthPayload( + report([result(.missingPrimaryKey, .completed, [orders, logs, invoices])]), + ledger: ledger + ) + + #expect(status(of: orders, in: payload) == "none") + #expect(status(of: logs, in: payload) == "ignored") + #expect(status(of: invoices, in: payload) == "new") + } + + @Test("With no recorded run there is nothing to compare with, so nothing reads as new") + func nothingIsNewWithoutHistory() { + let payload = MCPConnectionBridge.tableHealthPayload( + report([result(.missingPrimaryKey, .completed, [orders])]), + ledger: TableHealthLedger() + ) + + #expect(status(of: orders, in: payload) == "none") + } + + @Test("A finding carries typed metrics and its size, and leaves out what it lacks") + func findingCarriesTypedValues() throws { + let finding = TableHealthFinding( + kind: .nearLimit, + schema: nil, + table: "events", + objectName: nil, + detail: "Counter near its limit", + sizeBytes: 4_096, + metrics: [ + TableHealthMetric(id: "rows", label: "Rows", value: .count(5)), + TableHealthMetric(id: "next_value", label: "Next", value: .bigCount(UInt64.max)), + TableHealthMetric(id: "small_next", label: "Next", value: .bigCount(10)), + TableHealthMetric(id: "size", label: "Size", value: .bytes(2_048)), + TableHealthMetric(id: "used", label: "Used", value: .percent(0.25)), + TableHealthMetric(id: "broken", label: "Broken", value: .percent(.nan)), + TableHealthMetric(id: "seen", label: "Seen", value: .date(Self.day1)), + TableHealthMetric(id: "type", label: "Type", value: .text("bigint unsigned")), + TableHealthMetric(id: "unsigned", label: "Unsigned", value: .flag(true)) + ], + fixSQL: nil + ) + let payload = MCPConnectionBridge.tableHealthPayload( + report([result(.nearLimit, .completed, [finding])]), + ledger: TableHealthLedger() + ) + + let encoded = try #require(payload["findings"]?.arrayValue?.first) + let fields = keys(encoded) + #expect(fields == ["kind", "table", "detail", "size_bytes", "metrics", "status"]) + #expect(encoded["kind"]?.stringValue == "nearLimit") + #expect(encoded["size_bytes"] == .int(4_096)) + + let metrics = try #require(encoded["metrics"]?.arrayValue) + let values = metrics.compactMap { $0["value"] } + let types = metrics.compactMap { $0["type"]?.stringValue } + #expect(values == [ + .int(5), + .string("18446744073709551615"), + .int(10), + .int(2_048), + .double(0.25), + .null, + .string("1970-01-02T00:00:00Z"), + .string("bigint unsigned"), + .bool(true) + ]) + #expect(types == ["count", "count", "count", "bytes", "percent", "percent", "date", "text", "flag"]) + #expect(MCPToolCallResult.encodeJsonString(payload) != "{}") + } + + @Test("Every key the payload sends is declared in the output schema, and every required one is sent") + func payloadMatchesTheOutputSchema() throws { + let finding = TableHealthFinding( + kind: .unusedIndex, + schema: "public", + table: "orders", + objectName: "orders_note_idx", + detail: "Never scanned", + sizeBytes: 8_192, + metrics: [TableHealthMetric.estimatedRows(10)], + fixSQL: "DROP INDEX public.orders_note_idx;" + ) + let payload = MCPConnectionBridge.tableHealthPayload( + report( + [ + result(.unusedIndex, .completed, [finding], note: "Counts since the reset"), + result(.bloat, .failed("boom")), + result(.staleStatistics, .skipped("standby")) + ], + notes: ["Note"] + ), + ledger: TableHealthLedger() + ) + let schema = try #require(GetTableHealthTool.outputSchema) + let checkSchema = schema["properties"]?["checks"]?["items"] + let findingSchema = schema["properties"]?["findings"]?["items"] + let metricSchema = findingSchema?["properties"]?["metrics"]?["items"] + + let rootKeys = keys(payload) + #expect(rootKeys.isSubset(of: keys(schema["properties"]))) + #expect(required(schema).isSubset(of: rootKeys)) + + for check in payload["checks"]?.arrayValue ?? [] { + let checkKeys = keys(check) + #expect(checkKeys.isSubset(of: keys(checkSchema?["properties"])), "undeclared: \(checkKeys)") + #expect(required(checkSchema).isSubset(of: checkKeys)) + } + + let encoded = try #require(payload["findings"]?.arrayValue?.first) + let findingKeys = keys(encoded) + #expect(findingKeys == keys(findingSchema?["properties"])) + #expect(required(findingSchema).isSubset(of: findingKeys)) + + let metric = encoded["metrics"]?.arrayValue?.first + #expect(keys(metric) == keys(metricSchema?["properties"])) + #expect(required(metricSchema) == keys(metric)) + } + + @Test("get_table_health is a registered read-only tool taking a connection and a database") + func toolIsReadOnlyAndRegistered() { + #expect(GetTableHealthTool.name == "get_table_health") + #expect(GetTableHealthTool.requiredScopes == [.toolsRead]) + #expect(GetTableHealthTool.annotations.readOnlyHint == true) + #expect(GetTableHealthTool.annotations.destructiveHint == false) + #expect(MCPToolRegistry.tool(named: "get_table_health") != nil) + let inputs = keys(GetTableHealthTool.inputSchema["properties"]) + #expect(inputs == ["connection_id", "database"]) + #expect(required(GetTableHealthTool.inputSchema) == ["connection_id"]) + } + + @Test("An argument other than connection_id and database is refused before anything runs") + func unknownArgumentIsRefused() async { + await #expect(throws: MCPProtocolError.self) { + _ = try await GetTableHealthTool().call( + arguments: .object([ + "connection_id": .string(UUID().uuidString), + "schema": .string("public") + ]), + context: MCPToolTestHarness.context(), + services: MCPToolTestHarness.services() + ) + } + } + + @Test("A call without a connection_id is refused") + func missingConnectionIsRefused() async { + await #expect(throws: MCPProtocolError.self) { + _ = try await GetTableHealthTool().call( + arguments: .object(["database": .string("shop")]), + context: MCPToolTestHarness.context(), + services: MCPToolTestHarness.services() + ) + } + } +} diff --git a/apple/TableProTests/Core/Menu/MainMenuBuilderTests.swift b/apple/TableProTests/Core/Menu/MainMenuBuilderTests.swift index 293a87cf7b..f1f26bdb2c 100644 --- a/apple/TableProTests/Core/Menu/MainMenuBuilderTests.swift +++ b/apple/TableProTests/Core/Menu/MainMenuBuilderTests.swift @@ -614,6 +614,15 @@ struct MainMenuValidationTests { #expect(enabled(#selector(MainSplitViewController.exportQueryResults(_:)), context)) } + @Test("Export Results exports a Table Health tab's findings, and only while connected") + func exportResultsOnTableHealth() { + var context = MenuValidationContext() + context.hasTableHealthFindings = true + #expect(!enabled(#selector(MainSplitViewController.exportQueryResults(_:)), context)) + context.isConnected = true + #expect(enabled(#selector(MainSplitViewController.exportQueryResults(_:)), context)) + } + @Test("Neither survives losing the connection") func bothStillNeedAConnection() { var context = MenuValidationContext() diff --git a/apple/TableProTests/Core/Services/Infrastructure/ConnectionActionsMenuResolverTests.swift b/apple/TableProTests/Core/Services/Infrastructure/ConnectionActionsMenuResolverTests.swift index 0c90fd6701..64a9c4081a 100644 --- a/apple/TableProTests/Core/Services/Infrastructure/ConnectionActionsMenuResolverTests.swift +++ b/apple/TableProTests/Core/Services/Infrastructure/ConnectionActionsMenuResolverTests.swift @@ -307,6 +307,14 @@ struct ConnectionActionsMenuResolverTests { #expect(titles.contains(String(localized: "Forward")) == (tabKind == .table)) } + @Test("A Table Health tab offers Export Results for its findings and nothing that reads rows") + func tableHealthOffersExportResults() { + let titles = Self.titles(Self.context(tabKind: .tableHealth)) + #expect(titles.contains(String(localized: "Export Results…"))) + #expect(titles.contains(String(localized: "Add Row")) == false) + #expect(titles.contains(String(localized: "Preview SQL")) == false) + } + @Test("An unsaved definition offers its preview but nothing that reads rows") func createTableOffersPreviewOnly() { let titles = Self.titles(Self.context(tabKind: .createTable)) diff --git a/apple/TableProTests/Core/Storage/TableHealthHistoryStorageTests.swift b/apple/TableProTests/Core/Storage/TableHealthHistoryStorageTests.swift new file mode 100644 index 0000000000..0ecdfe0fb0 --- /dev/null +++ b/apple/TableProTests/Core/Storage/TableHealthHistoryStorageTests.swift @@ -0,0 +1,331 @@ +import Foundation +import os +@testable import TablePro +import Testing + +@MainActor +struct TableHealthHistoryStorageTests { + private typealias Fixture = TableHealthFixture + + private struct Change: Hashable, Sendable { + let connectionId: UUID + let database: String + } + + private let directory = FileManager.default.temporaryDirectory + .appendingPathComponent("TableHealthHistoryStorageTests-\(UUID().uuidString)", isDirectory: true) + private let connectionId = UUID() + private let notifications = NotificationCenter() + + private let orders = Fixture.finding(table: "orders") + private let ordersNoteIndex = Fixture.finding(.unusedIndex, table: "orders", object: "orders_note_idx") + private let invoices = Fixture.finding(table: "invoices") + private let billingOrders = Fixture.finding(schema: "billing", table: "orders") + + private static func day(_ number: Int) -> Date { + Date(timeIntervalSince1970: TimeInterval(number) * 86_400) + } + + private func makeStore() -> TableHealthHistoryStorage { + TableHealthHistoryStorage(directory: directory, notificationCenter: notifications) + } + + private var fileURL: URL { + directory.appendingPathComponent("\(connectionId.uuidString).json") + } + + private func corruptFileURL(_ number: Int) -> URL { + directory.appendingPathComponent("\(connectionId.uuidString).corrupt-\(number).json") + } + + private func report(_ findings: [TableHealthFinding]) -> TableHealthReport { + Fixture.report([ + (.missingPrimaryKey, .completed, findings.filter { $0.kind == .missingPrimaryKey }), + (.unusedIndex, .completed, findings.filter { $0.kind == .unusedIndex }) + ]) + } + + private func id( + _ kind: TableHealthCheckKind = .missingPrimaryKey, + schema: String? = "public", + table: String, + object: String? = nil + ) -> TableHealthFindingID { + TableHealthFindingID(kind: kind, schema: schema, table: table, object: object) + } + + private func scope(_ table: String, database: String = "shop", schema: String? = "public") -> TableScope { + TableScope(connectionId: connectionId, database: database, schema: schema, table: table) + } + + // Records orders, its note index and billing.orders, then ignores orders. + private func seededStore() throws -> TableHealthHistoryStorage { + let store = makeStore() + _ = try store.record( + report([orders, ordersNoteIndex, billingOrders]), + connectionId: connectionId, + database: "shop", + at: Self.day(1) + ) + _ = try store.setIgnored([orders.id], ignored: true, connectionId: connectionId, database: "shop", at: Self.day(1)) + return store + } + + private func changes(during body: () throws -> Void) rethrows -> [Change] { + let received = OSAllocatedUnfairLock(initialState: [Change]()) + let observer = notifications.addObserver(forName: .tableHealthHistoryDidChange, object: nil, queue: nil) { note in + guard let scope = TableHealthHistoryChange.scope(of: note) else { return } + received.withLock { $0.append(Change(connectionId: scope.connectionId, database: scope.database)) } + } + defer { notifications.removeObserver(observer) } + try body() + return received.withLock { $0 } + } + + @Test("A recorded run reads back from disk in a new store") + func recordedRunReadsBack() throws { + let saved = try makeStore().record(report([orders]), connectionId: connectionId, database: "shop", at: Self.day(1)) + + let reread = try makeStore().ledger(connectionId: connectionId, database: "shop") + + #expect(reread == saved) + #expect(reread.runIndex == 1) + #expect(reread.firstSeen(orders.id) == Self.day(1)) + } + + @Test("Each database keeps its own ledger inside the connection's one file") + func databasesShareOneFile() throws { + let store = makeStore() + _ = try store.record(report([orders]), connectionId: connectionId, database: "shop", at: Self.day(1)) + _ = try store.record(report([invoices]), connectionId: connectionId, database: "analytics", at: Self.day(2)) + + let shop = try store.ledger(connectionId: connectionId, database: "shop") + let analytics = try store.ledger(connectionId: connectionId, database: "analytics") + let files = try FileManager.default.contentsOfDirectory(atPath: directory.path) + + #expect(shop.firstSeen(orders.id) == Self.day(1)) + #expect(shop.firstSeen(invoices.id) == nil) + #expect(analytics.firstSeen(invoices.id) == Self.day(2)) + #expect(analytics.firstSeen(orders.id) == nil) + #expect(files == ["\(connectionId.uuidString).json"]) + } + + @Test("A database with no history reads as an empty ledger and leaves no file") + func missingHistoryIsEmpty() throws { + let ledger = try makeStore().ledger(connectionId: connectionId, database: "shop") + + #expect(ledger == TableHealthLedger()) + #expect(!FileManager.default.fileExists(atPath: fileURL.path)) + } + + @Test("Every write posts the connection and database it changed") + func writesPostTheirScope() throws { + let store = makeStore() + + let posted = try changes { + _ = try store.record(report([orders]), connectionId: connectionId, database: "shop", at: Self.day(1)) + _ = try store.setIgnored([orders.id], ignored: true, connectionId: connectionId, database: "shop", at: Self.day(1)) + } + + #expect(posted == [ + Change(connectionId: connectionId, database: "shop"), + Change(connectionId: connectionId, database: "shop") + ]) + } + + @Test("A change that leaves the ledger as it was writes nothing and posts nothing") + func noOpChangeWritesNothing() throws { + let store = makeStore() + + let posted = try changes { + _ = try store.clearResolved(connectionId: connectionId, database: "shop") + _ = try store.setIgnored([orders.id], ignored: false, connectionId: connectionId, database: "shop", at: Self.day(1)) + } + + #expect(posted.isEmpty) + #expect(!FileManager.default.fileExists(atPath: fileURL.path)) + } + + @Test("Ignoring and stopping ignoring both persist") + func ignorePersists() throws { + let store = try seededStore() + #expect(try makeStore().ledger(connectionId: connectionId, database: "shop").isIgnored(orders.id)) + + _ = try store.setIgnored([orders.id], ignored: false, connectionId: connectionId, database: "shop", at: Self.day(2)) + + let reread = try makeStore().ledger(connectionId: connectionId, database: "shop") + #expect(!reread.isIgnored(orders.id)) + } + + @Test("Clear Resolved forgets resolved findings and keeps the open ones") + func clearResolvedPersists() throws { + let store = makeStore() + _ = try store.record(report([orders, invoices]), connectionId: connectionId, database: "shop", at: Self.day(1)) + let resolvedRun = try store.record(report([orders]), connectionId: connectionId, database: "shop", at: Self.day(2)) + let resolvedIds = resolvedRun.resolved.map { $0.id } + #expect(resolvedIds == [invoices.id]) + + _ = try store.clearResolved(connectionId: connectionId, database: "shop") + + let reread = try makeStore().ledger(connectionId: connectionId, database: "shop") + #expect(reread.resolved.isEmpty) + #expect(reread.firstSeen(orders.id) == Self.day(1)) + } + + @Test("An unreadable file is set aside under a free name and never written over") + func unreadableFileIsSetAside() throws { + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + try Data("older copy".utf8).write(to: corruptFileURL(1)) + try Data("{ not json".utf8).write(to: fileURL) + let store = makeStore() + + #expect(try store.ledger(connectionId: connectionId, database: "shop") == TableHealthLedger()) + #expect(try String(contentsOf: corruptFileURL(1), encoding: .utf8) == "older copy") + #expect(try String(contentsOf: corruptFileURL(2), encoding: .utf8) == "{ not json") + + _ = try store.record(report([orders]), connectionId: connectionId, database: "shop", at: Self.day(1)) + + #expect(try makeStore().ledger(connectionId: connectionId, database: "shop").firstSeen(orders.id) == Self.day(1)) + #expect(try String(contentsOf: corruptFileURL(2), encoding: .utf8) == "{ not json") + } + + @Test("A write that cannot land throws and posts nothing") + func failedWriteThrows() throws { + // A file where the folder should be, so the folder cannot be made. + try Data().write(to: URL(fileURLWithPath: directory.path, isDirectory: false)) + defer { try? FileManager.default.removeItem(at: directory) } + let store = makeStore() + var thrown: (any Error)? + + let posted = changes { + do { + _ = try store.record(report([orders]), connectionId: connectionId, database: "shop", at: Self.day(1)) + } catch { + thrown = error + } + } + + #expect(thrown is TableHealthHistoryStorageError) + #expect(posted.isEmpty) + } + + @Test("A renamed table keeps its findings, their dates and its ignore mark") + func renameTableMovesEntries() throws { + let store = try seededStore() + + store.renameTable(from: scope("orders"), to: scope("purchases")) + + let ledger = try store.ledger(connectionId: connectionId, database: "shop") + #expect(ledger.isIgnored(id(table: "purchases"))) + #expect(ledger.firstSeen(id(table: "purchases")) == Self.day(1)) + #expect(ledger.firstSeen(id(.unusedIndex, table: "purchases", object: "orders_note_idx")) == Self.day(1)) + #expect(ledger.firstSeen(orders.id) == nil) + #expect(ledger.firstSeen(billingOrders.id) == Self.day(1)) + } + + @Test("A table on an engine without schemas, MySQL, follows its rename") + func renameSchemalessTable() throws { + let store = makeStore() + let mysqlOrders = Fixture.finding(schema: nil, table: "orders") + _ = try store.record(report([mysqlOrders]), connectionId: connectionId, database: "shop", at: Self.day(1)) + + store.renameTable(from: scope("orders", schema: nil), to: scope("purchases", schema: nil)) + + let ledger = try store.ledger(connectionId: connectionId, database: "shop") + #expect(ledger.firstSeen(id(schema: nil, table: "purchases")) == Self.day(1)) + #expect(ledger.firstSeen(mysqlOrders.id) == nil) + } + + @Test("A renamed schema takes its findings along and leaves other schemas alone") + func renameSchemaMovesEntries() throws { + let store = try seededStore() + + store.renameContainer( + connectionId: connectionId, fromDatabase: "shop", fromSchema: "billing", toDatabase: "shop", toSchema: "finance" + ) + + let ledger = try store.ledger(connectionId: connectionId, database: "shop") + #expect(ledger.firstSeen(id(schema: "finance", table: "orders")) == Self.day(1)) + #expect(ledger.firstSeen(billingOrders.id) == nil) + #expect(ledger.isIgnored(orders.id)) + } + + @Test("A renamed database takes its whole ledger along") + func renameDatabaseMovesLedger() throws { + let store = try seededStore() + let before = try store.ledger(connectionId: connectionId, database: "shop") + + let posted = changes { + store.renameContainer( + connectionId: connectionId, fromDatabase: "shop", fromSchema: nil, toDatabase: "store", toSchema: nil + ) + } + + #expect(try store.ledger(connectionId: connectionId, database: "store") == before) + #expect(try store.ledger(connectionId: connectionId, database: "shop") == TableHealthLedger()) + #expect(Set(posted) == [ + Change(connectionId: connectionId, database: "shop"), + Change(connectionId: connectionId, database: "store") + ]) + } + + @Test("A dropped table forgets its findings and ignore marks, and only its own") + func dropTableForgetsEntries() throws { + let store = try seededStore() + + store.dropTable(scope("orders")) + + let ledger = try store.ledger(connectionId: connectionId, database: "shop") + #expect(ledger.firstSeen(orders.id) == nil) + #expect(ledger.firstSeen(ordersNoteIndex.id) == nil) + #expect(!ledger.isIgnored(orders.id)) + #expect(ledger.firstSeen(billingOrders.id) == Self.day(1)) + } + + @Test("A dropped schema forgets its findings and leaves other schemas alone") + func dropSchemaForgetsEntries() throws { + let store = try seededStore() + + store.dropContainer(connectionId: connectionId, database: "shop", schema: "billing") + + let ledger = try store.ledger(connectionId: connectionId, database: "shop") + #expect(ledger.firstSeen(billingOrders.id) == nil) + #expect(ledger.firstSeen(orders.id) == Self.day(1)) + } + + @Test("A dropped database forgets its ledger, and the file goes with the last one") + func dropDatabaseForgetsLedger() throws { + let store = try seededStore() + _ = try store.record(report([invoices]), connectionId: connectionId, database: "analytics", at: Self.day(1)) + + store.dropContainer(connectionId: connectionId, database: "shop", schema: nil) + + #expect(try store.ledger(connectionId: connectionId, database: "shop") == TableHealthLedger()) + #expect(try store.ledger(connectionId: connectionId, database: "analytics").firstSeen(invoices.id) == Self.day(1)) + + store.dropContainer(connectionId: connectionId, database: "analytics", schema: nil) + + #expect(!FileManager.default.fileExists(atPath: fileURL.path)) + } + + @Test("Deleting a connection removes its history and set-aside copies, and no other connection's") + func purgeRemovesConnectionFiles() throws { + let store = try seededStore() + try Data("{ not json".utf8).write(to: corruptFileURL(1)) + let otherConnection = UUID() + _ = try store.record(report([orders]), connectionId: otherConnection, database: "shop", at: Self.day(1)) + + store.purgeConnections([connectionId], leavesTombstones: true) + + #expect(!FileManager.default.fileExists(atPath: fileURL.path)) + #expect(!FileManager.default.fileExists(atPath: corruptFileURL(1).path)) + #expect(try store.ledger(connectionId: otherConnection, database: "shop").firstSeen(orders.id) == Self.day(1)) + } + + @Test("Deleting a connection reaches the history store through the registry") + func registryHoldsTheStore() { + let shared = TableHealthHistoryStorage.shared + let registered = TableScopedSettingsRegistry.stores.contains { $0 === shared } + #expect(registered) + } +} diff --git a/apple/TableProTests/Core/TableHealth/MySQLTableHealthProviderTests.swift b/apple/TableProTests/Core/TableHealth/MySQLTableHealthProviderTests.swift index 404e89705a..ca98035fcf 100644 --- a/apple/TableProTests/Core/TableHealth/MySQLTableHealthProviderTests.swift +++ b/apple/TableProTests/Core/TableHealth/MySQLTableHealthProviderTests.swift @@ -73,40 +73,114 @@ struct MySQLTableHealthProviderTests { private static let tableRowsColumns = ["table_name", "estimated_rows"] + private static func keyResult(_ keys: [KeyRow]) -> QueryResult { + var seen: [String: Int] = [:] + return TableHealthFixture.result( + columns: keyColumns, + rows: keys.map { key -> [String?] in + let identity = key.schema + "." + key.table + "." + key.constraint + seen[identity, default: 0] += 1 + return [ + key.schema, key.table, key.constraint, key.column, key.referencedTable, key.referencedColumn, + String(seen[identity, default: 1]), key.isLocal ? "1" : "0", key.referencesHere ? "1" : "0" + ] + } + ) + } + private func unused( candidates: [(table: String, index: String)], indexes: [IndexRow], foreignKeys: [KeyRow] = [] ) -> [String] { - var seen: [String: Int] = [:] let results = [ TableHealthFixture.result( columns: ["table_name", "index_name"], rows: candidates.map { candidate -> [String?] in [candidate.table, candidate.index] } ), Self.indexResult(indexes), - TableHealthFixture.result( - columns: Self.keyColumns, - rows: foreignKeys.map { key -> [String?] in - let identity = key.schema + "." + key.table + "." + key.constraint - seen[identity, default: 0] += 1 - return [ - key.schema, key.table, key.constraint, key.column, key.referencedTable, key.referencedColumn, - String(seen[identity, default: 1]), key.isLocal ? "1" : "0", key.referencesHere ? "1" : "0" - ] - } - ), + Self.keyResult(foreignKeys), TableHealthFixture.result(columns: Self.tableRowsColumns, rows: []) ] return findings(.unusedIndex, from: results).compactMap { $0.objectName } } + // One row of autoIncrementQuery. MariaDB writes display widths into column_type ("int(11)"). + private struct CounterRow { + var table = "orders" + var column = "id" + var dataType = "int" + var columnType = "int" + var nullable = "NO" + var comment = "" + var extra = "auto_increment" + let next: String + let max: String + } + + private static let counterColumns = [ + "table_name", "column_name", "data_type", "column_type", "is_nullable", "column_comment", "extra", + "auto_increment", "max_value", "table_bytes" + ] + + private func nearLimit( + _ rows: [CounterRow], + foreignKeys: [KeyRow] = [], + statistics: TableHealthStatistics = TableHealthStatistics() + ) -> (findings: [TableHealthFinding], note: String?) { + provider.findings( + for: .nearLimit, + from: [ + TableHealthFixture.result( + columns: Self.counterColumns, + rows: rows.map { row -> [String?] in + [ + row.table, row.column, row.dataType, row.columnType, row.nullable, row.comment, row.extra, + row.next, row.max, "16384" + ] + } + ), + Self.keyResult(foreignKeys) + ], + context: context, + statistics: statistics + ) + } + + private func nearLimitFix(_ row: CounterRow) -> String? { + nearLimit([row]).findings.first?.fixSQL + } + + private static let reclaimableColumns = ["table_name", "engine", "estimated_rows", "table_bytes", "free_bytes"] + + private struct ProbeDenied: LocalizedError { + var errorDescription: String? { "Access denied; you need (at least one of) the PROCESS privilege(s) for this operation" } + } + + // The queries a whole report asks a server that answers only its version and the PROCESS probe. + private func report(version: String, canReadInnoDB: Bool = true) async throws -> (report: TableHealthReport, asked: [String]) { + var asked: [String] = [] + let report = try await provider.fetchReport(context: context) { query in + asked.append(query) + if query == MySQLTableHealthProvider.versionQuery { + return TableHealthFixture.result(columns: ["version"], rows: [[version]]) + } + if query == MySQLTableHealthProvider.processPrivilegeQuery { + guard canReadInnoDB else { throw ProbeDenied() } + return TableHealthFixture.result(columns: ["buffer_pools"], rows: [["1"]]) + } + return TableHealthFixture.empty() + } + return (report, asked) + } + private func statistics( performanceSchema: String = "1", instrument: String = "YES", consumer: String = "YES", countedTables: String = "3", - version: String = "8.0.46" + version: String = "8.0.46", + canReadInnoDB: Bool = true ) -> TableHealthStatistics { provider.statistics( from: [ @@ -116,7 +190,8 @@ struct MySQLTableHealthProviderTests { columns: ["table_io_instrument", "global_instrumentation", "counted_tables"], rows: [[instrument, consumer, countedTables]] ), - TableHealthFixture.result(columns: ["version"], rows: [[version]]) + TableHealthFixture.result(columns: ["version"], rows: [[version]]), + canReadInnoDB ? TableHealthFixture.result(columns: ["buffer_pools"], rows: [["1"]]) : nil ], now: Date(timeIntervalSince1970: 1_800_000_000) ) @@ -425,7 +500,7 @@ struct MySQLTableHealthProviderTests { ], now: Date() ) - #expect(stats.skippedChecks.isEmpty) + #expect(stats.skippedChecks[.unusedIndex] == nil) #expect(stats.notes.isEmpty) #expect(stats.serverVersion == nil) } @@ -462,14 +537,202 @@ struct MySQLTableHealthProviderTests { } } - @Test("Every query the report runs only reads") + @Test("Every query the report runs only reads, on MySQL and on MariaDB") func queriesOnlyRead() { - let queries = provider.statisticsQueries - + provider.checks.flatMap { provider.queries(for: $0, statistics: TableHealthStatistics()) } + var queries = provider.statisticsQueries + for server in [statistics(version: "8.4.13"), statistics(version: "11.4.13-MariaDB")] { + queries += provider.checks.flatMap { provider.queries(for: $0, statistics: server) } + } for query in queries where query != MySQLTableHealthProvider.uptimeQuery { let text = query.trimmingCharacters(in: .whitespacesAndNewlines) #expect(text.uppercased().hasPrefix("SELECT"), "\(text.prefix(40)) does not start with SELECT") } #expect(MySQLTableHealthProvider.uptimeQuery.hasPrefix("SHOW ")) } + + @Test("A counter near its limit carries its typed values and widens the column to BIGINT with its sign kept") + func nearLimitWidensToBigint() throws { + let found = nearLimit([ + CounterRow(table: "ai_tiny_unsigned", dataType: "tinyint", columnType: "tinyint unsigned", next: "200", max: "255"), + CounterRow(table: "ai_int", next: "1700000001", max: "2147483647") + ]).findings + let tiny = try #require(found.first { $0.table == "ai_tiny_unsigned" }) + let int = try #require(found.first { $0.table == "ai_int" }) + + #expect(tiny.schema == nil) + #expect(tiny.objectName == "id") + #expect(tiny.sizeBytes == 16384) + #expect(tiny.metric("auto_increment")?.value == .bigCount(200)) + #expect(tiny.metric("max_value")?.value == .bigCount(255)) + #expect(tiny.metric("used_percent")?.value == .percent(200.0 / 255.0)) + #expect(tiny.metric("column_type")?.value == .text("tinyint unsigned")) + #expect(tiny.fixSQL == "ALTER TABLE `shop`.`ai_tiny_unsigned` MODIFY COLUMN `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT;") + #expect(int.fixSQL == "ALTER TABLE `shop`.`ai_int` MODIFY COLUMN `id` BIGINT NOT NULL AUTO_INCREMENT;") + } + + @Test("MariaDB's display widths do not reach the new type, except the width a ZEROFILL column pads to") + func nearLimitReadsMariaDBTypes() { + let signed = nearLimitFix(CounterRow(columnType: "int(11)", next: "1700000001", max: "2147483647")) + let unsigned = nearLimitFix(CounterRow(dataType: "tinyint", columnType: "tinyint(3) unsigned", next: "200", max: "255")) + let zerofill = nearLimitFix(CounterRow(columnType: "int(5) unsigned zerofill", next: "4000000000", max: "4294967295")) + let nullable = nearLimitFix(CounterRow(columnType: "int(11)", nullable: "YES", next: "1700000001", max: "2147483647")) + + #expect(signed == "ALTER TABLE `shop`.`orders` MODIFY COLUMN `id` BIGINT NOT NULL AUTO_INCREMENT;") + #expect(unsigned == "ALTER TABLE `shop`.`orders` MODIFY COLUMN `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT;") + #expect(zerofill == "ALTER TABLE `shop`.`orders` MODIFY COLUMN `id` BIGINT(5) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT;") + #expect(nullable == "ALTER TABLE `shop`.`orders` MODIFY COLUMN `id` BIGINT NULL AUTO_INCREMENT;") + } + + @Test("An unsigned bigint counter past Int64 keeps its exact value and has no fix; a signed one can drop its sign") + func nearLimitAtTheWidestType() throws { + let found = nearLimit([ + CounterRow( + table: "events", dataType: "bigint", columnType: "bigint unsigned", + next: "15000000000000000000", max: "18446744073709551615" + ), + CounterRow(table: "logs", dataType: "bigint", columnType: "bigint(20)", next: "7000000000000000000", max: "9223372036854775807") + ]).findings + let unsigned = try #require(found.first { $0.table == "events" }) + let signed = try #require(found.first { $0.table == "logs" }) + + #expect(unsigned.metric("auto_increment")?.value == .bigCount(15_000_000_000_000_000_000)) + #expect(unsigned.metric("max_value")?.value == .bigCount(UInt64.max)) + #expect(unsigned.fixSQL == nil) + #expect(!unsigned.detail.isEmpty) + #expect(signed.metric("max_value")?.value == .bigCount(UInt64(Int64.max))) + #expect(signed.fixSQL == "ALTER TABLE `shop`.`logs` MODIFY COLUMN `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT;") + } + + @Test("The fix states the whole column again: an invisible column stays invisible and a hostile comment stays one literal") + func nearLimitRestatesTheColumn() { + let comment = "it's a \\ path\n*/ DROP TABLE x; -- " + let mysql = nearLimitFix(CounterRow( + table: "odd`name", column: "se`q", dataType: "smallint", columnType: "smallint", comment: comment, + extra: "auto_increment INVISIBLE", next: "30001", max: "32767" + )) + let mariadb = nearLimitFix(CounterRow( + dataType: "smallint", columnType: "smallint(6)", comment: "row counter", extra: "auto_increment, INVISIBLE", + next: "30001", max: "32767" + )) + let visible = nearLimitFix(CounterRow(dataType: "smallint", columnType: "smallint", next: "30001", max: "32767")) + + #expect(mysql == "ALTER TABLE `shop`.`odd``name` MODIFY COLUMN `se``q` BIGINT NOT NULL AUTO_INCREMENT INVISIBLE COMMENT 'it''s a \\\\ path\n*/ DROP TABLE x; -- ';") + #expect(mariadb == "ALTER TABLE `shop`.`orders` MODIFY COLUMN `id` BIGINT NOT NULL AUTO_INCREMENT INVISIBLE COMMENT 'row counter';") + #expect(visible == "ALTER TABLE `shop`.`orders` MODIFY COLUMN `id` BIGINT NOT NULL AUTO_INCREMENT;") + } + + @Test("A counter a foreign key uses at either end names each key, and the fix still widens the column") + func nearLimitNamesForeignKeys() throws { + let found = nearLimit( + [ + CounterRow(table: "ai_parent", next: "1800000001", max: "2147483647"), + CounterRow(table: "ai_child", column: "seq", next: "1800000001", max: "2147483647"), + CounterRow(table: "ai_alone", next: "1800000001", max: "2147483647") + ], + foreignKeys: [ + KeyRow( + table: "ai_child", constraint: "ai_child_fk", column: "parent_id", + referencedTable: "ai_parent", referencedColumn: "ID", referencesHere: true + ), + KeyRow( + schema: "archive", table: "audit", constraint: "audit_fk", column: "parent_id", + referencedTable: "ai_parent", referencedColumn: "id", referencesHere: true + ), + KeyRow(table: "ai_child", constraint: "ai_child_seq_fk", column: "seq", referencedTable: "elsewhere", referencedColumn: "id") + ] + ).findings + let parent = try #require(found.first { $0.table == "ai_parent" }) + let child = try #require(found.first { $0.table == "ai_child" }) + let alone = try #require(found.first { $0.table == "ai_alone" }) + + #expect(parent.detail.contains("ai_child_fk")) + #expect(parent.detail.contains("archive.audit")) + #expect(!parent.detail.contains("ai_child_seq_fk")) + #expect(parent.fixSQL == "ALTER TABLE `shop`.`ai_parent` MODIFY COLUMN `id` BIGINT NOT NULL AUTO_INCREMENT;") + #expect(child.detail.contains("ai_child_seq_fk")) + #expect(!alone.detail.contains("_fk")) + } + + @Test("On MySQL 8 the cached checks say information_schema may lag; MariaDB and MySQL 5.7 read live values") + func statisticsCacheNote() { + let reclaimable = { (server: TableHealthStatistics) in + self.provider.findings( + for: .reclaimableSpace, + from: [TableHealthFixture.result(columns: Self.reclaimableColumns, rows: [])], + context: self.context, + statistics: server + ).note + } + let mysql8 = statistics(version: "8.4.13") + + #expect(nearLimit([], statistics: mysql8).note?.contains("information_schema_stats_expiry") == true) + #expect(reclaimable(mysql8)?.contains("information_schema_stats_expiry") == true) + #expect(nearLimit([], statistics: statistics(version: "11.4.13-MariaDB-ubu2404")).note == nil) + #expect(reclaimable(statistics(version: "11.4.13-MariaDB-ubu2404")) == nil) + #expect(nearLimit([], statistics: statistics(version: "5.7.44")).note == nil) + } + + @Test("A table with reclaimable space carries its typed values and previews OPTIMIZE TABLE") + func reclaimableSpaceFinding() throws { + let found = provider.findings( + for: .reclaimableSpace, + from: [TableHealthFixture.result( + columns: Self.reclaimableColumns, + rows: [ + ["fx_parts", "InnoDB", "122895", "134905856", "148897792"], + ["odd`name", "MyISAM", "131072", "241770496", "119537664"] + ] + )], + context: context, + statistics: statistics(version: "8.4.13") + ).findings + let parts = try #require(found.first) + let myisam = try #require(found.last) + + #expect(found.count == 2) + #expect(parts.schema == nil) + #expect(parts.table == "fx_parts") + #expect(parts.objectName == nil) + #expect(parts.sizeBytes == 134_905_856) + #expect(parts.metric("free_bytes")?.value == .bytes(148_897_792)) + #expect(parts.metric("free_percent")?.value == .percent(148_897_792.0 / (134_905_856.0 + 148_897_792.0))) + #expect(parts.metric("estimated_rows")?.value == .count(122_895)) + #expect(parts.metric("engine")?.value == .text("InnoDB")) + #expect(parts.fixSQL == "OPTIMIZE TABLE `shop`.`fx_parts`;") + #expect(myisam.fixSQL == "OPTIMIZE TABLE `shop`.`odd``name`;") + } + + @Test("The reclaimable space check reads MySQL 8's tablespace tables on MySQL and MariaDB's own on MariaDB") + func reclaimableQueryFollowsTheServer() async throws { + let mysql = try await report(version: "8.4.13-log") + let mariadb = try await report(version: "11.4.13-MariaDB-ubu2404") + + #expect(mysql.asked.contains(MySQLTableHealthProvider.reclaimableSpaceQuery)) + #expect(!mysql.asked.contains(MySQLTableHealthProvider.mariaDBReclaimableSpaceQuery)) + #expect(mariadb.asked.contains(MySQLTableHealthProvider.mariaDBReclaimableSpaceQuery)) + #expect(!mariadb.asked.contains(MySQLTableHealthProvider.reclaimableSpaceQuery)) + #expect(mysql.report.result(for: .reclaimableSpace)?.outcome == .completed) + #expect(mariadb.report.result(for: .reclaimableSpace)?.outcome == .completed) + } + + @Test("Without PROCESS, or before MySQL 8.0, the reclaimable space check is skipped with its reason and the rest run") + func reclaimableSkips() async throws { + let denied = try await report(version: "8.4.13", canReadInnoDB: false) + let old = try await report(version: "5.7.44") + + guard case .skipped(let reason) = denied.report.result(for: .reclaimableSpace)?.outcome else { + Issue.record("the check ran without PROCESS") + return + } + #expect(reason.contains("PROCESS")) + #expect(!denied.asked.contains(MySQLTableHealthProvider.reclaimableSpaceQuery)) + #expect(denied.report.result(for: .nearLimit)?.outcome == .completed) + #expect(denied.report.result(for: .missingPrimaryKey)?.outcome == .completed) + guard case .skipped(let oldReason) = old.report.result(for: .reclaimableSpace)?.outcome else { + Issue.record("the check ran on MySQL 5.7") + return + } + #expect(oldReason.contains("8.0")) + #expect(statistics(version: "11.4.13-MariaDB", canReadInnoDB: false).skippedChecks[.reclaimableSpace] != nil) + } } diff --git a/apple/TableProTests/Core/TableHealth/PostgreSQLIndexShapeTests.swift b/apple/TableProTests/Core/TableHealth/PostgreSQLIndexShapeTests.swift new file mode 100644 index 0000000000..aa5ae43d02 --- /dev/null +++ b/apple/TableProTests/Core/TableHealth/PostgreSQLIndexShapeTests.swift @@ -0,0 +1,268 @@ +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +// The cases a PostgreSQL 13 and 17 server gave for the redundant index rule; the probe seeds the same names. +struct PostgreSQLIndexShapeTests { + private struct Key: ExpressibleByStringLiteral { + let column: String + var opclass = "1978" + var collation = "0" + var sort = "0" + + init(_ column: String, opclass: String = "1978", collation: String = "0", sort: String = "0") { + self.column = column + self.opclass = opclass + self.collation = collation + self.sort = sort + } + + init(stringLiteral value: String) { + self.init(value) + } + } + + private struct IndexRow { + var schema = "public" + var table = "ri" + var tableKind = "r" + let name: String + var method = "btree" + var unique = false + var primary = false + var immediate = true + var nullsNotDistinct = false + var protected = false + var predicate: String? + var bytes = "8192" + let keys: [Key] + var includes: [String] = [] + } + + private static let columns = [ + "schema_name", "table_name", "table_kind", "index_name", "access_method", "is_unique", "is_primary", "is_immediate", + "nulls_not_distinct", "is_protected", "predicate", "index_bytes", "index_definition", "key_count", "position", + "column_text", "opclass", "collation", "sort_option" + ] + + private static func flag(_ value: Bool) -> String { value ? "t" : "f" } + + private static func rows(_ indexes: [IndexRow]) -> [[String?]] { + indexes.flatMap { index -> [[String?]] in + var parts: [(column: String, key: Key?)] = [] + for key in index.keys { + parts.append((key.column, key)) + } + for column in index.includes { + parts.append((column, nil)) + } + return parts.enumerated().map { offset, part -> [String?] in + [ + index.schema, index.table, index.tableKind, index.name, index.method, flag(index.unique), flag(index.primary), + flag(index.immediate), index.nullsNotDistinct ? "true" : "false", flag(index.protected), index.predicate, + index.bytes, offset == 0 ? "CREATE INDEX \(index.name) ON \(index.schema).\(index.table)" : nil, + String(index.keys.count), String(offset + 1), part.column, part.key?.opclass, part.key?.collation, + part.key?.sort + ] + } + } + } + + private let provider = PostgreSQLTableHealthProvider() + + private func findings(_ indexes: [IndexRow]) -> [TableHealthFinding] { + findings(rows: Self.rows(indexes)) + } + + private func findings(rows: [[String?]]) -> [TableHealthFinding] { + provider.findings( + for: .redundantIndex, + from: [TableHealthFixture.result(columns: Self.columns, rows: rows)], + context: TableHealthFixture.postgresContext(), + statistics: TableHealthStatistics() + ).findings + } + + // Redundant index name to the covering index the finding names. + private func redundant(_ indexes: [IndexRow]) -> [String: String] { + var map: [String: String] = [:] + for finding in findings(indexes) { + if case .text(let covering)? = finding.metric("covering_index")?.value { + map[finding.objectName ?? ""] = covering + } + } + return map + } + + @Test("An index whose keys lead another B-tree index is redundant to it, and of an equal pair the later name goes") + func prefixAndDuplicate() { + let found = redundant([ + IndexRow(name: "ri_pkey", primary: true, protected: true, keys: ["id"]), + IndexRow(name: "r_a", keys: ["a"]), + IndexRow(name: "k_ab", keys: ["a", "b"]), + IndexRow(name: "r_ab_dup", keys: ["a", "b"]) + ]) + #expect(found == ["r_a": "k_ab", "r_ab_dup": "k_ab"]) + } + + @Test("Sort order, key order, operator class, collation, access method and expression keep indexes apart") + func keyPartsKeepIndexesApart() { + let found = redundant([ + IndexRow(name: "k_ab", keys: ["a", "b"]), + IndexRow(name: "k_a_desc", keys: [Key("a", sort: "3")]), + IndexRow(name: "k_ba", keys: ["b", "a"]), + IndexRow(name: "k_code", keys: [Key("code", opclass: "3126", collation: "100")]), + IndexRow(name: "k_code_pattern", keys: [Key("code", opclass: "4217", collation: "100")]), + IndexRow(name: "k_code_c", keys: [Key("code", opclass: "3126", collation: "950")]), + IndexRow(name: "k_hash_a", method: "hash", keys: [Key("a", opclass: "10020")]), + IndexRow(name: "k_lower_email", keys: [Key("lower(email)", opclass: "3126", collation: "100")]), + IndexRow(name: "k_upper_email", keys: [Key("upper(email)", opclass: "3126", collation: "100")]) + ]) + #expect(found.isEmpty) + } + + @Test("The same expression twice is a duplicate") + func duplicateExpression() { + let found = redundant([ + IndexRow(name: "k_lower_email", keys: [Key("lower(email)", opclass: "3126", collation: "100")]), + IndexRow(name: "r_lower_email_dup", keys: [Key("lower(email)", opclass: "3126", collation: "100")]) + ]) + #expect(found == ["r_lower_email_dup": "k_lower_email"]) + } + + @Test("A partial index is covered only by one with the same predicate") + func partialNeedsSamePredicate() { + let found = redundant([ + IndexRow(name: "k_ab", keys: ["a", "b"]), + IndexRow(name: "k_a_partial", predicate: "(a > 0)", keys: ["a"]), + IndexRow(name: "k_a_partial_twin_b", predicate: "(a > 0)", keys: ["a", "b"]), + IndexRow(name: "r_a_partial_dup", predicate: "(a > 0)", keys: ["a"]) + ]) + #expect(found == ["k_a_partial": "k_a_partial_twin_b", "r_a_partial_dup": "k_a_partial_twin_b"]) + } + + @Test("A plain index is covered by a unique one, but a unique index never by a longer one") + func uniqueNeedsSameKeys() { + let found = redundant([ + IndexRow(name: "k_uniq_c", unique: true, keys: ["c"]), + IndexRow(name: "k_cb", keys: ["c", "b"]), + IndexRow(name: "r_c_plain", keys: ["c"]) + ]) + #expect(found == ["r_c_plain": "k_uniq_c"]) + } + + @Test("A unique index equal to the primary key is redundant, and the primary key never is") + func uniqueEqualToPrimaryKey() { + let found = redundant([ + IndexRow(name: "a_id_uniq", unique: true, keys: ["id"]), + IndexRow(name: "ri_pkey", unique: true, primary: true, protected: true, keys: ["id"]) + ]) + #expect(found == ["a_id_uniq": "ri_pkey"]) + } + + @Test("A deferrable unique constraint and an immediate unique index enforce different rules") + func deferrableIsNotImmediate() { + let found = redundant([ + IndexRow(table: "rd", name: "k_rd_v_deferrable", unique: true, immediate: false, protected: true, keys: ["v"]), + IndexRow(table: "rd", name: "k_rd_v_immediate", unique: true, keys: ["v"]) + ]) + #expect(found.isEmpty) + } + + @Test("NULLS NOT DISTINCT is a different rule, and a plain index names the first unique one") + func nullsNotDistinct() { + let found = redundant([ + IndexRow(name: "k_nd", unique: true, keys: ["b"]), + IndexRow(name: "k_nnd", unique: true, nullsNotDistinct: true, keys: ["b"]), + IndexRow(name: "r_b_plain", keys: ["b"]) + ]) + #expect(found == ["r_b_plain": "k_nd"]) + } + + @Test("Constraint, partition and replica identity indexes stay, and a duplicate of one goes whatever its name") + func protectedIndexesStay() { + let found = redundant([ + IndexRow(table: "rc", name: "k_rc_x_key", unique: true, protected: true, keys: ["x"]), + IndexRow(table: "rc", name: "a_rc_x_uidx", unique: true, keys: ["x"]), + IndexRow(table: "rc", name: "r_rc_x_plain", keys: ["x"]), + IndexRow(table: "rk", name: "z_rk_z_fkref", unique: true, protected: true, keys: ["z"]), + IndexRow(table: "rk", name: "a_rk_z_twin", unique: true, keys: ["z"]), + IndexRow(table: "rr", name: "a_replica", unique: true, protected: true, keys: ["k"]), + IndexRow(table: "rr", name: "b_replica_twin", unique: true, protected: true, keys: ["k"]) + ]) + #expect(found == ["a_rc_x_uidx": "k_rc_x_key", "r_rc_x_plain": "k_rc_x_key", "a_rk_z_twin": "z_rk_z_fkref"]) + } + + @Test("An INCLUDE column must be in the covering index, and the widest cover is named") + func includeColumns() { + let indexes = [ + IndexRow(table: "ri_incl", name: "k_a_incl_c", keys: ["a"], includes: ["c"]), + IndexRow(table: "ri_incl", name: "k_ab2", keys: ["a", "b"]), + IndexRow(table: "ri_incl", name: "r_a_only", keys: ["a"]) + ] + #expect(redundant(indexes) == ["r_a_only": "k_ab2"]) + + let withCover = indexes + [IndexRow(table: "ri_incl", name: "k_abc_incl", keys: ["a", "b"], includes: ["c"])] + #expect(redundant(withCover) == ["k_a_incl_c": "k_abc_incl", "k_ab2": "k_abc_incl", "r_a_only": "k_abc_incl"]) + } + + @Test("A GiST or GIN index is redundant only to an equal one, never to a longer one") + func nonBTreeNeedsEqualKeys() { + let found = redundant([ + IndexRow(table: "rx", name: "rx_room_during_excl", method: "gist", protected: true, keys: ["room", "during"]), + IndexRow(table: "rx", name: "k_rx_room_gist", method: "gist", keys: ["room"]), + IndexRow(name: "k_note_gin", method: "gin", keys: [Key("to_tsvector('simple'::regconfig, note)", opclass: "10073")]), + IndexRow(name: "k_note_gin_2", method: "gin", keys: [Key("to_tsvector('english'::regconfig, note)", opclass: "10073")]), + IndexRow(name: "r_note_gin_dup", method: "gin", keys: [Key("to_tsvector('simple'::regconfig, note)", opclass: "10073")]) + ]) + #expect(found == ["r_note_gin_dup": "k_note_gin"]) + } + + @Test("A redundant index previews a concurrent drop and carries its size and both definitions") + func redundantFinding() throws { + let finding = try #require(findings([ + IndexRow(schema: "sales", table: "orders", name: "orders_a_idx", bytes: "16384", keys: ["a"]), + IndexRow(schema: "sales", table: "orders", name: "orders_ab_idx", keys: ["a", "b"]) + ]).first) + #expect(finding.schema == "sales") + #expect(finding.table == "orders") + #expect(finding.objectName == "orders_a_idx") + #expect(finding.sizeBytes == 16_384) + #expect(finding.fixSQL == #"DROP INDEX CONCURRENTLY "sales"."orders_a_idx";"#) + #expect(finding.metric("covering_index")?.value == .text("orders_ab_idx")) + #expect(finding.metric("index_definition")?.value == .text("CREATE INDEX orders_a_idx ON sales.orders")) + #expect(finding.metric("covering_definition")?.value == .text("CREATE INDEX orders_ab_idx ON sales.orders")) + #expect(finding.metric("index_bytes") == nil) + #expect(finding.detail == String(format: String(localized: "%1$@ is covered by %2$@."), "orders_a_idx", "orders_ab_idx")) + } + + @Test("On a partitioned table the drop is not concurrent, which PostgreSQL refuses there") + func partitionedDropIsPlain() throws { + let finding = try #require(findings([ + IndexRow(table: "rp", tableKind: "p", name: "r_rp_k", bytes: "0", keys: ["k"]), + IndexRow(table: "rp", tableKind: "p", name: "k_rp_k_at", bytes: "0", keys: ["k", "at"]) + ]).first) + #expect(finding.objectName == "r_rp_k") + #expect(finding.fixSQL == #"DROP INDEX "public"."r_rp_k";"#) + #expect(finding.detail.contains("every partition")) + } + + @Test("Indexes group by schema and table and order by position, whatever order the rows arrive in") + func rowsGroupByExactNames() { + let rows = Self.rows([ + IndexRow(schema: "a", table: "t", name: "t_x", keys: ["x"]), + IndexRow(schema: "b", table: "t", name: "t_xy", keys: ["x", "y"]), + IndexRow(schema: "a", table: "t", name: "t_yx", keys: ["y", "x"]) + ]) + let found = findings(rows: rows.reversed()) + #expect(found.isEmpty) + + let sameTable = Self.rows([ + IndexRow(name: "r_a", keys: ["a"]), + IndexRow(name: "k_ab", keys: ["a", "b"]) + ]) + let shuffled = [sameTable[2], sameTable[0], sameTable[1]] + #expect(findings(rows: shuffled).map { $0.objectName } == ["r_a"]) + } +} diff --git a/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift b/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift index 3a0c8cf8e6..98b0579f6f 100644 --- a/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift +++ b/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift @@ -13,13 +13,27 @@ struct PostgreSQLTableHealthProviderTests { private let provider = PostgreSQLTableHealthProvider() private let context = TableHealthFixture.postgresContext() - private func findings(_ check: TableHealthCheckKind, columns: [String], rows: [[String?]]) -> [TableHealthFinding] { + private func findings( + _ check: TableHealthCheckKind, + columns: [String], + rows: [[String?]], + statistics: TableHealthStatistics = TableHealthStatistics() + ) -> [TableHealthFinding] { + output(check, columns: columns, rows: rows, statistics: statistics).findings + } + + private func output( + _ check: TableHealthCheckKind, + columns: [String], + rows: [[String?]], + statistics: TableHealthStatistics = TableHealthStatistics() + ) -> (findings: [TableHealthFinding], note: String?) { provider.findings( for: check, from: [TableHealthFixture.result(columns: columns, rows: rows)], context: context, - statistics: TableHealthStatistics() - ).findings + statistics: statistics + ) } private static func statisticsResult( @@ -64,9 +78,9 @@ struct PostgreSQLTableHealthProviderTests { #expect(finding.schema == "public") #expect(finding.table == "orders") #expect(finding.objectName == nil) - #expect(finding.sizeBytes == 81920) - #expect(finding.metric("dead_rows")?.value == .count(3000)) - #expect(finding.metric("live_rows")?.value == .count(2000)) + #expect(finding.sizeBytes == 81_920) + #expect(finding.metric("dead_rows")?.value == .count(3_000)) + #expect(finding.metric("live_rows")?.value == .count(2_000)) #expect(finding.metric("dead_percent")?.value == .percent(0.6)) #expect(finding.metric("last_vacuum")?.value == .date(vacuumed)) #expect(finding.metric("table_bytes") == nil) @@ -102,7 +116,7 @@ struct PostgreSQLTableHealthProviderTests { #expect(finding.table == "orders") #expect(finding.objectName == "orders_note_idx") - #expect(finding.sizeBytes == 8192) + #expect(finding.sizeBytes == 8_192) #expect(finding.fixSQL == #"DROP INDEX CONCURRENTLY "sales"."orders_note_idx";"#) } @@ -114,8 +128,8 @@ struct PostgreSQLTableHealthProviderTests { rows: [["public", #"Odd "Name""#, "1200", "65536"]] ).first) #expect(finding.fixSQL == #"ALTER TABLE "public"."Odd ""Name""" ADD PRIMARY KEY (column_name);"#) - #expect(finding.metric("estimated_rows")?.value == .count(1200)) - #expect(finding.sizeBytes == 65536) + #expect(finding.metric("estimated_rows")?.value == .count(1_200)) + #expect(finding.sizeBytes == 65_536) } @Test("A foreign key's column rows fold into one finding, in key order") @@ -149,8 +163,8 @@ struct PostgreSQLTableHealthProviderTests { rows: [["public", "line", "r", "line_order_fk", "order_id", "sales", "orders", "5000", "40960"]] ).first) #expect(finding.metric("referenced_table")?.value == .text("sales.orders")) - #expect(finding.metric("estimated_rows")?.value == .count(5000)) - #expect(finding.sizeBytes == 40960) + #expect(finding.metric("estimated_rows")?.value == .count(5_000)) + #expect(finding.sizeBytes == 40_960) } @Test("A partitioned table's index is not built concurrently, which PostgreSQL refuses there") @@ -194,7 +208,7 @@ struct PostgreSQLTableHealthProviderTests { @Test("The statistics read the server version") func serverVersion() { - #expect(statistics(version: "90624").serverVersion == 90624) + #expect(statistics(version: "90624").serverVersion == 90_624) #expect(statistics(version: "170011").serverVersion == 170_011) #expect(statistics(version: nil).serverVersion == nil) #expect(statistics().isStandby == false) @@ -205,8 +219,10 @@ struct PostgreSQLTableHealthProviderTests { let stats = statistics(trackCounts: "off") #expect(stats.skippedChecks[.bloat] != nil) #expect(stats.skippedChecks[.unusedIndex] != nil) + #expect(stats.skippedChecks[.staleStatistics] != nil) #expect(stats.skippedChecks[.missingPrimaryKey] == nil) #expect(stats.skippedChecks[.unindexedForeignKey] == nil) + #expect(stats.skippedChecks[.transactionIdAge] == nil) #expect(stats.notes.isEmpty) } @@ -217,8 +233,10 @@ struct PostgreSQLTableHealthProviderTests { #expect(stats.isStandby) #expect(stats.skippedChecks[.bloat] == reason) #expect(stats.skippedChecks[.unusedIndex] == reason) - #expect(stats.skippedChecks[.missingPrimaryKey] == nil) - #expect(stats.skippedChecks[.unindexedForeignKey] == nil) + #expect(stats.skippedChecks[.staleStatistics] == reason) + for structural in [TableHealthCheckKind.missingPrimaryKey, .unindexedForeignKey, .redundantIndex, .invalidIndex, .nearLimit, .transactionIdAge] { + #expect(stats.skippedChecks[structural] == nil, "\(structural.rawValue) is skipped on a standby") + } #expect(stats.notes == [String(localized: "This server is a read-only standby. Run the fixes on the primary.")]) } @@ -238,7 +256,15 @@ struct PostgreSQLTableHealthProviderTests { #expect(!asked.contains(PostgreSQLTableHealthProvider.bloatQuery)) #expect(!asked.contains(PostgreSQLTableHealthProvider.unusedIndexQuery)) + #expect(!asked.contains(PostgreSQLTableHealthProvider.staleStatisticsQuery)) #expect(asked.contains(PostgreSQLTableHealthProvider.unindexedForeignKeyQuery)) + #expect(asked.contains(PostgreSQLTableHealthProvider.redundantIndexQuery)) + #expect(asked.contains(PostgreSQLTableHealthProvider.invalidIndexQuery)) + #expect(asked.contains(PostgreSQLTableHealthProvider.nearLimitQuery)) + #expect(asked.contains(PostgreSQLTableHealthProvider.transactionIdAgeQuery)) + #expect(report.result(for: .staleStatistics)?.outcome == .skipped( + String(localized: "This server is a standby. Its activity counters do not reflect the primary.") + )) #expect(report.result(for: .missingPrimaryKey)?.outcome == .completed) #expect(report.result(for: .missingPrimaryKey)?.findings.count == 1) #expect(report.notes.contains(String(localized: "This server is a read-only standby. Run the fixes on the primary."))) @@ -257,8 +283,9 @@ struct PostgreSQLTableHealthProviderTests { #expect(!asked.contains(PostgreSQLTableHealthProvider.bloatQuery)) #expect(!asked.contains(PostgreSQLTableHealthProvider.unusedIndexQuery)) - #expect(report.checks[0].outcome == .skipped(statistics(trackCounts: "off").skippedChecks[.bloat] ?? "")) - #expect(report.checks[2].outcome == .completed) + #expect(!asked.contains(PostgreSQLTableHealthProvider.staleStatisticsQuery)) + #expect(report.result(for: .bloat)?.outcome == .skipped(statistics(trackCounts: "off").skippedChecks[.bloat] ?? "")) + #expect(report.result(for: .missingPrimaryKey)?.outcome == .completed) } @Test("A failing check reports its error and leaves the other checks standing") @@ -274,10 +301,13 @@ struct PostgreSQLTableHealthProviderTests { #expect(report.database == "shop") #expect(report.generatedAt == generated) - #expect(report.checks.map { $0.kind } == [.bloat, .unusedIndex, .missingPrimaryKey, .unindexedForeignKey]) + #expect(report.checks.map { $0.kind } == [ + .bloat, .unusedIndex, .redundantIndex, .invalidIndex, .missingPrimaryKey, .unindexedForeignKey, + .nearLimit, .transactionIdAge, .staleStatistics + ]) #expect(report.checks.map { $0.check } == provider.checks.map { provider.check($0) }) - #expect(report.checks[0].outcome == .failed("permission denied for pg_stat_user_tables")) - #expect(report.checks[2].findings.map { $0.table } == ["logs"]) + #expect(report.result(for: .bloat)?.outcome == .failed("permission denied for pg_stat_user_tables")) + #expect(report.result(for: .missingPrimaryKey)?.findings.map { $0.table } == ["logs"]) #expect(report.findingCount == 1) } @@ -344,4 +374,321 @@ struct PostgreSQLTableHealthProviderTests { #expect(text.uppercased().hasPrefix("SELECT"), "\(text.prefix(40)) does not start with SELECT") } } + + private static let invalidColumns = [ + "schema_name", "table_name", "index_name", "is_unique", "is_ready", "index_bytes", "index_definition" + ] + + private func invalid(_ index: String, unique: Bool = false, ready: Bool = true, version: String?) throws -> TableHealthFinding { + let definition = unique + ? "CREATE UNIQUE INDEX \(index) ON public.inv USING btree (v)" + : "CREATE INDEX \(index) ON public.inv USING btree (v, w)" + return try #require(findings( + .invalidIndex, + columns: Self.invalidColumns, + rows: [["public", "inv", index, unique ? "t" : "f", ready ? "t" : "f", "16384", definition]], + statistics: statistics(version: version) + ).first) + } + + @Test("An invalid index rebuilds concurrently from PostgreSQL 12, and carries its size, definition and state") + func invalidIndexReindexes() throws { + let finding = try invalid("inv_vw_cancelled", version: "170011") + #expect(finding.table == "inv") + #expect(finding.objectName == "inv_vw_cancelled") + #expect(finding.sizeBytes == 16_384) + #expect(finding.fixSQL == #"REINDEX INDEX CONCURRENTLY "public"."inv_vw_cancelled";"#) + #expect(finding.metric("index_definition")?.value == .text("CREATE INDEX inv_vw_cancelled ON public.inv USING btree (v, w)")) + #expect(finding.metric("is_unique")?.value == .flag(false)) + #expect(finding.metric("is_ready")?.value == .flag(true)) + #expect(try invalid("inv_vw_cancelled", version: "120000").fixSQL == #"REINDEX INDEX CONCURRENTLY "public"."inv_vw_cancelled";"#) + } + + @Test("Before PostgreSQL 12, or with no version read, an invalid index is dropped and built again concurrently") + func invalidIndexBeforeTwelve() throws { + let plain = try invalid("inv_vw_cancelled", version: "110000") + #expect(plain.fixSQL == """ + DROP INDEX CONCURRENTLY "public"."inv_vw_cancelled"; + CREATE INDEX CONCURRENTLY inv_vw_cancelled ON public.inv USING btree (v, w); + """) + let unique = try invalid("inv_v_uniq", unique: true, version: nil) + #expect(unique.fixSQL == """ + DROP INDEX CONCURRENTLY "public"."inv_v_uniq"; + CREATE UNIQUE INDEX CONCURRENTLY inv_v_uniq ON public.inv USING btree (v); + """) + } + + @Test("A copy an interrupted REINDEX CONCURRENTLY left is dropped, not rebuilt") + func reindexLeftoverIsDropped() throws { + for name in ["inv_v_uniq_ccnew", "inv_w_idx_ccold", "inv_w_idx_ccnew1"] { + #expect(try invalid(name, version: "170011").fixSQL == "DROP INDEX CONCURRENTLY \"public\".\"\(name)\";") + } + #expect(try invalid("inv_w_idx_ccnew", version: nil).fixSQL == #"DROP INDEX CONCURRENTLY "public"."inv_w_idx_ccnew";"#) + #expect(try invalid("inv_ccnew_backup", version: "170011").fixSQL == #"REINDEX INDEX CONCURRENTLY "public"."inv_ccnew_backup";"#) + #expect(try invalid("inv_w_idx_ccnew", version: "110000").fixSQL?.hasPrefix("DROP INDEX CONCURRENTLY") == true) + #expect(try invalid("inv_w_idx_ccnew", version: "110000").fixSQL?.contains("CREATE INDEX CONCURRENTLY") == true) + } + + @Test("An invalid unique index warns that duplicate values make the rebuild fail") + func invalidUniqueWarnsOfDuplicates() throws { + let duplicates = String(localized: "Rebuilding a unique index fails while the table holds duplicate values.") + #expect(try invalid("inv_v_uniq", unique: true, ready: false, version: "170011").detail.contains(duplicates)) + #expect(try !invalid("inv_vw_cancelled", version: "170011").detail.contains(duplicates)) + #expect(try invalid("inv_v_uniq", unique: true, ready: false, version: "170011").metric("is_ready")?.value == .flag(false)) + } + + private static let sequenceColumns = [ + "sequence_schema", "sequence_name", "sequence_type", "increment_by", "sequence_min", "sequence_max", + "last_value", "lower_bound", "upper_bound", "used_fraction", "readable", + "table_schema", "table_name", "column_name", "column_type", "column_min", "column_max", "ownership", + "table_bytes", "referencing_columns" + ] + + private struct SequenceRow { + let name: String + var type = "integer" + var increment = "1" + var minimum = "1" + var maximum = "2147483647" + var last: String? = "1700000000" + var lower = "1" + var upper = "2147483647" + var used: String? = "0.79162418869475292852" + var readable = "t" + var table: String? + var column: String? = "id" + var columnType: String? = "integer" + var columnMin: String? = "-2147483648" + var columnMax: String? = "2147483647" + var ownership: String? = "owned" + var referencing: String? + + var cells: [String?] { + [ + "public", name, type, increment, minimum, maximum, last, lower, upper, used, readable, + table == nil ? nil : "public", table, table == nil ? nil : column, table == nil ? nil : columnType, + table == nil ? nil : columnMin, table == nil ? nil : columnMax, table == nil ? nil : ownership, + table == nil ? nil : "8192", referencing + ] + } + } + + private func sequences(_ rows: [SequenceRow]) -> (findings: [TableHealthFinding], note: String?) { + output(.nearLimit, columns: Self.sequenceColumns, rows: rows.map(\.cells)) + } + + private func sequenceFix(_ row: SequenceRow) throws -> String? { + try #require(sequences([row]).findings.first).fixSQL + } + + @Test("A serial column near its limit widens the column, then the sequence, which keeps its own type") + func serialWidensColumnAndSequence() throws { + let finding = try #require(sequences([ + SequenceRow(name: "seq_int_id_seq", table: "seq_int", referencing: "public.seq_int_child.seq_int_id") + ]).findings.first) + #expect(finding.schema == "public") + #expect(finding.table == "seq_int") + #expect(finding.objectName == "seq_int_id_seq") + #expect(finding.sizeBytes == 8_192) + #expect(finding.fixSQL == """ + ALTER TABLE "public"."seq_int" ALTER COLUMN "id" TYPE bigint; + ALTER SEQUENCE "public"."seq_int_id_seq" AS bigint; + """) + #expect(finding.metric("used_percent")?.value == .percent(0.79162418869475292852)) + #expect(finding.metric("last_value")?.value == .count(1_700_000_000)) + #expect(finding.metric("upper_bound")?.value == .count(2_147_483_647)) + #expect(finding.metric("column")?.value == .text("id")) + #expect(finding.metric("column_type")?.value == .text("integer")) + #expect(finding.metric("referencing_columns")?.value == .text("public.seq_int_child.seq_int_id")) + #expect(finding.detail.contains("public.seq_int_child.seq_int_id")) + #expect(finding.detail.contains(String( + localized: "Changing the column type rewrites the table and its indexes under an exclusive lock." + ))) + } + + @Test("An identity column needs only the column change: its sequence follows") + func identityWidensColumnOnly() throws { + #expect(try sequenceFix(SequenceRow( + name: "seq_identity_id_seq", last: "1800000000", used: "0.838", table: "seq_identity", ownership: "identity" + )) == #"ALTER TABLE "public"."seq_identity" ALTER COLUMN "id" TYPE bigint;"#) + } + + @Test("A bigint sequence feeding an integer column widens the column only") + func bigintSequenceWidensColumnOnly() throws { + #expect(try sequenceFix(SequenceRow( + name: "seq_big_for_int", type: "bigint", maximum: "9223372036854775807", table: "seq_intcol_bigseq", ownership: "default" + )) == #"ALTER TABLE "public"."seq_intcol_bigseq" ALTER COLUMN "id" TYPE bigint;"#) + } + + @Test("An integer sequence feeding a bigint column changes only the sequence") + func bigintColumnWidensSequenceOnly() throws { + let finding = try #require(sequences([SequenceRow( + name: "seq_int_typed", last: "2000000000", used: "0.931", table: "seq_bigcol_intseq", + columnType: "bigint", columnMin: nil, columnMax: nil, ownership: "default" + )]).findings.first) + #expect(finding.fixSQL == #"ALTER SEQUENCE "public"."seq_int_typed" AS bigint;"#) + #expect(!finding.detail.contains(String( + localized: "Changing the column type rewrites the table and its indexes under an exclusive lock." + ))) + } + + @Test("A sequence no column uses has no table and changes only itself") + func standaloneSequence() throws { + let finding = try #require(sequences([SequenceRow( + name: "seq_standalone_small", type: "smallint", maximum: "32767", last: "30000", upper: "32767", used: "0.9155" + )]).findings.first) + #expect(finding.schema == "public") + #expect(finding.table == nil) + #expect(finding.sizeBytes == nil) + #expect(finding.metric("column") == nil) + #expect(finding.fixSQL == #"ALTER SEQUENCE "public"."seq_standalone_small" AS bigint;"#) + } + + @Test("A descending sequence is measured toward its minimum") + func descendingSequence() throws { + let finding = try #require(sequences([SequenceRow( + name: "seq_desc_s", increment: "-1", minimum: "-2147483648", maximum: "-1", last: "-1700000000", + lower: "-2147483648", upper: "-1", table: "seq_desc", ownership: "default" + )]).findings.first) + #expect(finding.fixSQL == """ + ALTER TABLE "public"."seq_desc" ALTER COLUMN "id" TYPE bigint; + ALTER SEQUENCE "public"."seq_desc_s" AS bigint; + """) + #expect(finding.detail.contains((-2_147_483_648 as Int64).formatted())) + } + + @Test("A limit set below the sequence type's own is removed, since AS bigint keeps it") + func explicitLimitIsRemoved() throws { + #expect(try sequenceFix(SequenceRow( + name: "seq_explicit", maximum: "1000", last: "900", upper: "1000", used: "0.8999" + )) == #"ALTER SEQUENCE "public"."seq_explicit" AS bigint NO MAXVALUE;"#) + #expect(try sequenceFix(SequenceRow( + name: "seq_explicit_desc", increment: "-1", minimum: "-1000", maximum: "-1", last: "-900", lower: "-1000", upper: "-1" + )) == #"ALTER SEQUENCE "public"."seq_explicit_desc" AS bigint NO MINVALUE;"#) + } + + @Test("A bigint sequence and column have no wider type, so the finding has no fix") + func bigintHasNoFix() throws { + let finding = try #require(sequences([SequenceRow( + name: "seq_huge", type: "bigint", maximum: "9223372036854775807", last: "9000000000000000000", + upper: "9223372036854775807", used: "0.9758", table: "events", columnType: "bigint", columnMin: nil, columnMax: nil + )]).findings.first) + #expect(finding.fixSQL == nil) + #expect(finding.detail.contains(String(localized: "The sequence and its column are already bigint, so no wider type exists."))) + } + + @Test("Unreadable sequences become the check's note, never findings") + func unreadableSequencesBecomeNote() { + let secret = SequenceRow(name: "seq_secret", type: "smallint", last: nil, used: nil, readable: "f") + let one = sequences([secret, SequenceRow(name: "seq_int_id_seq", table: "seq_int")]) + #expect(one.findings.map { $0.objectName } == ["seq_int_id_seq"]) + #expect(one.note == String(localized: "One sequence is not readable by this user, so its counter was not checked.")) + + let two = sequences([secret, SequenceRow(name: "seq_other", last: nil, used: nil, readable: "f")]) + #expect(two.findings.isEmpty) + #expect(two.note == String( + format: String(localized: "%lld sequences are not readable by this user, so their counters were not checked."), 2 + )) + #expect(sequences([SequenceRow(name: "seq_int_id_seq", table: "seq_int")]).note == nil) + } + + @Test("The sequence check needs PostgreSQL 10, and is skipped before it without running") + func sequencesNeedTen() async throws { + #expect(statistics(version: "90624").skippedChecks[.nearLimit] == String(localized: "Needs PostgreSQL 10 or later.")) + #expect(statistics(version: "100000").skippedChecks[.nearLimit] == nil) + #expect(statistics(version: nil).skippedChecks[.nearLimit] == nil) + + var asked: [String] = [] + let report = try await provider.fetchReport(context: context) { query in + asked.append(query) + if query == PostgreSQLTableHealthProvider.statisticsQuery { + return Self.statisticsResult(version: "90624") + } + return TableHealthFixture.empty() + } + #expect(!asked.contains(PostgreSQLTableHealthProvider.nearLimitQuery)) + #expect(report.result(for: .nearLimit)?.outcome == .skipped(String(localized: "Needs PostgreSQL 10 or later."))) + #expect(report.result(for: .invalidIndex)?.outcome == .completed) + } + + @Test("The sequence check names itself after sequences") + func sequenceCheckTitle() { + #expect(provider.check(.nearLimit).title == String(localized: "Sequences Near Their Limit")) + #expect(provider.check(.nearLimit).summary.contains("75%")) + } + + @Test("A table past its freeze age previews a freeze and carries its ages as values") + func transactionIdAgeFinding() throws { + let finding = try #require(findings( + .transactionIdAge, + columns: [ + "schema_name", "table_name", "table_xid_age", "toast_xid_age", "xid_age", "freeze_max_age", + "transactions_left", "last_vacuum", "table_bytes" + ], + rows: [["public", "xid_old", "200069", "200071", "200071", "100000", "2147283577", nil, "16384"]] + ).first) + #expect(finding.table == "xid_old") + #expect(finding.objectName == nil) + #expect(finding.sizeBytes == 16_384) + #expect(finding.fixSQL == #"VACUUM (FREEZE) "public"."xid_old";"#) + #expect(finding.metric("xid_age")?.value == .count(200_071)) + #expect(finding.metric("table_xid_age")?.value == .count(200_069)) + #expect(finding.metric("toast_xid_age")?.value == .count(200_071)) + #expect(finding.metric("freeze_max_age")?.value == .count(100_000)) + #expect(finding.metric("transactions_left")?.value == .count(2_147_283_577)) + #expect(finding.metric("last_vacuum") == nil) + #expect(finding.detail.contains((100_000 as Int64).formatted())) + } + + @Test("The wraparound summary states the freeze age it compares against") + func transactionIdAgeSummary() { + #expect(provider.check(.transactionIdAge).summary.contains("autovacuum_freeze_max_age")) + #expect(provider.check(.transactionIdAge).summary.contains("200,000,000")) + #expect(provider.check(.staleStatistics).summary.contains("10%")) + #expect(provider.check(.staleStatistics).summary.contains("50")) + } + + private static let staleColumns = [ + "schema_name", "table_name", "table_kind", "live_rows", "modified_rows", "analyze_threshold", "never_analyzed", + "last_analyze", "table_bytes" + ] + + @Test("A table modified past the autoanalyze threshold previews an analyze and carries its counts") + func staleStatisticsFinding() throws { + let analyzed = Date(timeIntervalSince1970: 1_800_000_000) + let finding = try #require(findings( + .staleStatistics, + columns: Self.staleColumns, + rows: [["public", "st_stale", "r", "5000", "1000", "550", "f", "1800000000", "245760"]] + ).first) + #expect(finding.table == "st_stale") + #expect(finding.sizeBytes == 245_760) + #expect(finding.fixSQL == #"ANALYZE "public"."st_stale";"#) + #expect(finding.metric("modified_rows")?.value == .count(1_000)) + #expect(finding.metric("live_rows")?.value == .count(5_000)) + #expect(finding.metric("analyze_threshold")?.value == .count(550)) + #expect(finding.metric("never_analyzed")?.value == .flag(false)) + #expect(finding.metric("last_analyze")?.value == .date(analyzed)) + #expect(finding.detail.contains(TableHealthFormatting.date(analyzed))) + } + + @Test("A table never analyzed says so, and a partitioned one says only a manual analyze reaches it") + func neverAnalyzed() throws { + let found = findings( + .staleStatistics, + columns: Self.staleColumns, + rows: [ + ["public", "st_never", "r", "5000", "5000", "50", "t", nil, "212992"], + ["public", "st_part", "p", "0", "0", "50", "t", nil, "0"] + ] + ) + #expect(found.count == 2) + #expect(found[0].detail == String(localized: "Never analyzed, so the planner has no statistics for it.")) + #expect(found[0].metric("last_analyze") == nil) + #expect(found[0].metric("never_analyzed")?.value == .flag(true)) + #expect(found[1].detail.contains("partitioned")) + #expect(found[1].metric("live_rows") == nil) + #expect(found[1].metric("modified_rows") == nil) + #expect(found[1].fixSQL == #"ANALYZE "public"."st_part";"#) + } } diff --git a/apple/TableProTests/Core/TableHealth/TableHealthExportDataSourceTests.swift b/apple/TableProTests/Core/TableHealth/TableHealthExportDataSourceTests.swift new file mode 100644 index 0000000000..0541d142ab --- /dev/null +++ b/apple/TableProTests/Core/TableHealth/TableHealthExportDataSourceTests.swift @@ -0,0 +1,188 @@ +import Foundation +@testable import TablePro +import TableProPluginKit +import Testing + +struct TableHealthExportDataSourceTests { + private static let epoch = Date(timeIntervalSince1970: 0) + + private static func missingKeyRow(status: TableHealthRowStatus = .new) -> TableHealthFindingRow { + let finding = TableHealthFinding( + kind: .missingPrimaryKey, + schema: "public", + table: "logs", + objectName: nil, + detail: "No primary key.", + sizeBytes: 1_048_576, + metrics: [TableHealthMetric.estimatedRows(42)], + fixSQL: "ALTER TABLE \"public\".\"logs\" ADD PRIMARY KEY (column_name);" + ) + return TableHealthFindingRow( + finding: finding, + checkTitle: "Tables Without a Primary Key", + checkOrder: 1, + status: status, + firstSeen: epoch, + lastSeen: Date(timeIntervalSince1970: 86_400 + 3_661) + ) + } + + private static func bloatRow() -> TableHealthFindingRow { + let finding = TableHealthFinding( + kind: .bloat, + schema: "public", + table: "events", + objectName: nil, + detail: "Dead rows.", + metrics: [ + TableHealthMetric(id: "dead_percent", label: "Dead", value: .percent(0.25)), + TableHealthMetric(id: "last_vacuum", label: "Last Vacuum", value: .date(epoch)), + TableHealthMetric(id: "next_value", label: "Next Value", value: .bigCount(UInt64.max)), + TableHealthMetric(id: "cycles", label: "Cycles", value: .flag(true)), + TableHealthMetric(id: "column_type", label: "Type", value: .text("int")), + TableHealthMetric(id: "ratio", label: "Ratio", value: .percent(.nan)), + ], + fixSQL: nil + ) + return TableHealthFindingRow( + finding: finding, + checkTitle: "Bloated Tables", + checkOrder: 0, + status: .seen, + firstSeen: nil, + lastSeen: nil + ) + } + + private static func stream(_ source: TableHealthExportDataSource) async throws -> (PluginStreamHeader?, [PluginRow]) { + var header: PluginStreamHeader? + var rows: [PluginRow] = [] + for try await element in source.streamRows(table: "findings", databaseName: "") { + switch element { + case .header(let first): header = first + case .rows(let batch): rows.append(contentsOf: batch) + } + } + return (header, rows) + } + + @Test("Columns are the fixed set, then one per metric id in the order the checks run, each typed") + func typedColumnsInCheckOrder() async throws { + let source = TableHealthExportDataSource(database: "shop", rows: [Self.missingKeyRow(), Self.bloatRow()]) + + let (header, rows) = try await Self.stream(source) + + let fixed = ["database", "check", "schema", "table", "object", "status", "size_bytes", "detail", + "first_seen", "last_seen", "fix_sql"] + let metrics = ["dead_percent", "last_vacuum", "next_value", "cycles", "column_type", "ratio", "estimated_rows"] + #expect(header?.columns == fixed + metrics) + #expect(header?.columnTypeNames == [ + "varchar", "varchar", "varchar", "varchar", "varchar", "varchar", "bigint", "varchar", + "timestamp", "timestamp", "varchar", + "double", "timestamp", "decimal(20,0)", "boolean", "varchar", "double", "bigint", + ]) + #expect(header?.estimatedRowCount == 2) + #expect(rows.count == 2) + #expect(rows.allSatisfy { $0.count == fixed.count + metrics.count }) + } + + @Test("Cells are machine formatted: POSIX numbers, UTC timestamps, null for what is missing") + func machineFormattedCells() async throws { + let source = TableHealthExportDataSource(database: "shop", rows: [Self.missingKeyRow(), Self.bloatRow()]) + + let (header, rows) = try await Self.stream(source) + let columns = try #require(header?.columns) + func cell(_ row: Int, _ name: String) throws -> PluginCellValue { + let index = try #require(columns.firstIndex(of: name)) + return rows[row][index] + } + + #expect(try cell(0, "database") == .text("shop")) + #expect(try cell(0, "check") == .text("Tables Without a Primary Key")) + #expect(try cell(0, "object") == .null) + #expect(try cell(0, "status") == .text("new")) + #expect(try cell(0, "size_bytes") == .text("1048576")) + #expect(try cell(0, "first_seen") == .text("1970-01-01 00:00:00")) + #expect(try cell(0, "last_seen") == .text("1970-01-02 01:01:01")) + #expect(try cell(0, "estimated_rows") == .text("42")) + #expect(try cell(0, "dead_percent") == .null) + + #expect(try cell(1, "status") == .null) + #expect(try cell(1, "size_bytes") == .null) + #expect(try cell(1, "fix_sql") == .null) + #expect(try cell(1, "dead_percent") == .text("0.25")) + #expect(try cell(1, "last_vacuum") == .text("1970-01-01 00:00:00")) + #expect(try cell(1, "next_value") == .text("18446744073709551615")) + #expect(try cell(1, "cycles") == .text("true")) + #expect(try cell(1, "ratio") == .null) + } + + // The SQL export writes a value bare when its column type reads as numeric and the value parses as a number. + @Test("Every numeric cell is one the SQL export can write unquoted, and text columns stay quoted") + func numericCellsSurviveTheSQLExport() async throws { + let source = TableHealthExportDataSource(database: "shop", rows: [Self.missingKeyRow(), Self.bloatRow()]) + + let (header, rows) = try await Self.stream(source) + let types = try #require(header?.columnTypeNames) + + for (index, type) in types.enumerated() where PluginExportUtilities.isNumericColumnType(type) { + for row in rows { + if case .text(let value) = row[index] { + #expect(PluginNumericLiteral.isValid(value), "\(value) in a \(type) column") + } + } + } + #expect(!PluginExportUtilities.isNumericColumnType("varchar")) + #expect(!PluginExportUtilities.isNumericColumnType("timestamp")) + #expect(PluginExportUtilities.isNumericColumnType("decimal(20,0)")) + } + + @Test("Parquet reads the same column types from fetchColumns as the header carries") + func fetchColumnsMatchesTheHeader() async throws { + let source = TableHealthExportDataSource(database: "shop", rows: [Self.missingKeyRow(), Self.bloatRow()]) + + let (header, _) = try await Self.stream(source) + let columns = try await source.fetchColumns(table: "anything", databaseName: "anywhere") + + #expect(columns.map { $0.name } == header?.columns) + #expect(columns.map { $0.typeNameForClassification } == header?.columnTypeNames) + } + + @Test("The source counts its rows, writes no DDL and runs no query") + func noQueriesNoDDL() async throws { + let source = TableHealthExportDataSource(database: "shop", rows: [Self.missingKeyRow()]) + + #expect(source.databaseTypeId == DataSourceExportRequest.dataFileTypeId) + #expect(try await source.fetchApproximateRowCount(table: "", databaseName: "") == 1) + #expect(try await source.fetchTableDDL(table: "", databaseName: "").isEmpty) + await #expect(throws: (any Error).self) { + _ = try await source.execute(query: "SELECT 1") + } + } + + @Test("Copy keeps one line per row when a field holds tabs or line breaks") + func copyFlattensFields() { + let finding = TableHealthFinding( + kind: .missingPrimaryKey, + schema: "public", + table: "x\tDROP\nTABLE", + objectName: nil, + detail: "line one\r\nline two", + fixSQL: nil + ) + let row = TableHealthFindingRow( + finding: finding, + checkTitle: "Tables Without a Primary Key", + checkOrder: 0, + status: .seen, + firstSeen: nil, + lastSeen: nil + ) + + let text = TableHealthTSV.text([row], columns: TableHealthColumn.findings) + let lines = text.components(separatedBy: "\n") + + #expect(lines.count == 2) + #expect(lines[1].components(separatedBy: "\t").count == TableHealthColumn.findings.count) + } +} diff --git a/apple/TableProTests/Services/MainSplitViewControllerDetailWidthTests.swift b/apple/TableProTests/Services/MainSplitViewControllerDetailWidthTests.swift index 8c846e1522..18b187b76d 100644 --- a/apple/TableProTests/Services/MainSplitViewControllerDetailWidthTests.swift +++ b/apple/TableProTests/Services/MainSplitViewControllerDetailWidthTests.swift @@ -43,6 +43,13 @@ struct MainSplitViewControllerDetailWidthTests { #expect(resolved == 560) } + @Test("Table Health declares the width its pane bar needs, which holds the checks list beside the findings") + func tableHealthDeclaresItsOwnMinimum() { + let resolved = MainSplitViewController.resolveDetailMinimumThickness(for: .tableHealth, contentMode: .browse) + #expect(resolved == TableHealthLayoutMetrics.tabMinimumWidth) + #expect(resolved >= TableHealthLayoutMetrics.checksListMinimumWidth + TableHealthLayoutMetrics.findingsMinimumWidth) + } + @Test( "Every other tab type keeps the default detail minimum", arguments: [TabType.query, .table, .createTable, .erDiagram, .serverDashboard] diff --git a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift index 1d431323aa..1dc993f109 100644 --- a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift +++ b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift @@ -8,11 +8,16 @@ private struct Offline: LocalizedError { var errorDescription: String? { "No active connection" } } +private struct DiskFull: LocalizedError { + var errorDescription: String? { "The disk is full" } +} + // Answers the missing primary key check with the given tables and every other read with nothing. // The driver runs off the main actor, so the state is locked rather than isolated. private final class HealthServer: @unchecked Sendable { private let lock = NSLock() private var _tablesWithoutKey = ["logs"] + private var _tableBytes: [String: Int64] = [:] private var _failsMissingKeyCheck = false private var _queries: [String] = [] private var _catalogReadFlags: [Bool] = [] @@ -22,6 +27,11 @@ private final class HealthServer: @unchecked Sendable { set { lock.withLock { _tablesWithoutKey = newValue } } } + var tableBytes: [String: Int64] { + get { lock.withLock { _tableBytes } } + set { lock.withLock { _tableBytes = newValue } } + } + var failsMissingKeyCheck: Bool { get { lock.withLock { _failsMissingKeyCheck } } set { lock.withLock { _failsMissingKeyCheck = newValue } } @@ -36,18 +46,20 @@ private final class HealthServer: @unchecked Sendable { } func answer(_ query: String) throws -> PluginQueryResult { - let (tables, fails) = lock.withLock { () -> ([String], Bool) in + let (tables, bytes, fails) = lock.withLock { () -> ([String], [String: Int64], Bool) in _queries.append(query) - return (_tablesWithoutKey, _failsMissingKeyCheck) + return (_tablesWithoutKey, _tableBytes, _failsMissingKeyCheck) } guard query == PostgreSQLTableHealthProvider.missingPrimaryKeyQuery else { return PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) } if fails { throw Offline() } return PluginQueryResult( - columns: ["schema_name", "table_name"], - columnTypeNames: ["TEXT", "TEXT"], - rows: tables.map { [.text("public"), .text($0)] }, + columns: ["schema_name", "table_name", "table_bytes"], + columnTypeNames: ["TEXT", "TEXT", "INT8"], + rows: tables.map { table in + [.text("public"), .text(table), bytes[table].map { PluginCellValue.text(String($0)) } ?? PluginCellValue.null] + }, rowsAffected: 0, executionTime: 0 ) @@ -75,8 +87,8 @@ private final class HealthPluginDriver: PluginDriverStub, @unchecked Sendable { private final class HealthMetadata: ScopedMetadataProviding { let server = HealthServer() let connection = TestFixtures.makeConnection(database: "shop", type: .postgresql) - var database = "shop" - var isConnected = true + // The sidebar's database, which a Table Health tab never reads. + var browsedDatabase = "archive" var leaseFails = false // Holds the next lease until the test releases it, the way a slow catalog read holds one. var hold: (reached: AsyncGate, release: AsyncGate)? @@ -98,8 +110,67 @@ private final class HealthMetadata: ScopedMetadataProviding { } func browseScope(for connectionId: UUID) -> DatabaseScope? { - guard isConnected else { return nil } - return DatabaseScope(connectionId: connectionId, database: database, schema: "public") + DatabaseScope(connectionId: connectionId, database: browsedDatabase, schema: "public") + } +} + +// Keeps ledgers in memory and posts the change the way the real store does. +@MainActor +private final class InMemoryHistory: TableHealthHistoryStoring { + var ledgers: [String: TableHealthLedger] = [:] + var failsWrites = false + private(set) var recordCount = 0 + + func ledger(connectionId: UUID, database: String) throws -> TableHealthLedger { + ledgers[database] ?? TableHealthLedger() + } + + func record(_ report: TableHealthReport, connectionId: UUID, database: String, at date: Date) throws -> TableHealthLedger { + try save(connectionId: connectionId, database: database) { ledger in + recordCount += 1 + return ledger.recording(report, at: date) + } + } + + func setIgnored( + _ ids: Set, + ignored: Bool, + connectionId: UUID, + database: String, + at date: Date + ) throws -> TableHealthLedger { + try save(connectionId: connectionId, database: database) { ledger in + ignored ? ledger.ignoring(ids, at: date) : ledger.unignoring(ids) + } + } + + func clearResolved(connectionId: UUID, database: String) throws -> TableHealthLedger { + try save(connectionId: connectionId, database: database) { $0.clearingResolved() } + } + + private func save( + connectionId: UUID, + database: String, + _ change: (TableHealthLedger) -> TableHealthLedger + ) throws -> TableHealthLedger { + if failsWrites { throw DiskFull() } + let next = change(ledgers[database] ?? TableHealthLedger()) + ledgers[database] = next + NotificationCenter.default.post( + name: .tableHealthHistoryDidChange, + object: nil, + userInfo: TableHealthHistoryChange.userInfo(connectionId: connectionId, database: database) + ) + return next + } +} + +@MainActor +private final class BoundDatabase { + var name: String + + init(_ name: String) { + self.name = name } } @@ -107,15 +178,23 @@ private final class HealthMetadata: ScopedMetadataProviding { struct TableHealthViewModelTests { private func makeViewModel( _ metadata: HealthMetadata, + history: InMemoryHistory = InMemoryHistory(), + database: BoundDatabase = BoundDatabase("shop"), databaseType: DatabaseType = .postgresql ) -> TableHealthViewModel { TableHealthViewModel( connectionId: metadata.connection.id, provider: TableHealthProviderFactory.provider(for: databaseType), - metadata: metadata + history: history, + metadata: metadata, + database: { database.name } ) } + private func row(_ viewModel: TableHealthViewModel, table: String) -> TableHealthFindingRow? { + viewModel.findingRows.first { $0.id.table == table } + } + @Test("An engine without a provider is not supported and reads nothing") func unsupportedEngine() async { let metadata = HealthMetadata() @@ -128,8 +207,8 @@ struct TableHealthViewModelTests { #expect(metadata.leases.isEmpty) } - @Test("A refresh runs every read in one metadata lease on the browsed database, and names it") - func oneLeaseOnBrowseScope() async throws { + @Test("A refresh reads the tab's own database in one bulk lease, whatever the sidebar browses") + func oneLeaseOnTheTabDatabase() async throws { let metadata = HealthMetadata() let viewModel = makeViewModel(metadata) @@ -187,12 +266,51 @@ struct TableHealthViewModelTests { #expect(!viewModel.isRefreshing) } - @Test("A connection with no database selected asks for one and reads nothing") - func serverScopedAsksForDatabase() async { + @Test("A refresh asked for during a refresh runs once more after it") + func refreshDuringRefreshRunsAgain() async { let metadata = HealthMetadata() - metadata.database = "" + let hold = (reached: AsyncGate(), release: AsyncGate()) + metadata.hold = hold let viewModel = makeViewModel(metadata) + let first = Task { await viewModel.refresh() } + await hold.reached.wait() + metadata.server.tablesWithoutKey = ["logs", "events"] + let second = Task { await viewModel.refresh() } + await hold.release.open() + await second.value + await first.value + + #expect(metadata.leases.count == 2) + #expect(viewModel.report?.findingCount == 2) + #expect(!viewModel.isRefreshing) + } + + @Test("A cancelled refresh records nothing and reports no error") + func cancelledRefreshRecordsNothing() async { + let metadata = HealthMetadata() + let hold = (reached: AsyncGate(), release: AsyncGate()) + metadata.hold = hold + let history = InMemoryHistory() + let viewModel = makeViewModel(metadata, history: history) + + let run = Task { await viewModel.refresh() } + await hold.reached.wait() + viewModel.cancel() + await hold.release.open() + await run.value + + #expect(viewModel.report == nil) + #expect(viewModel.loadError == nil) + #expect(history.recordCount == 0) + #expect(!viewModel.isRefreshing) + } + + @Test("A tab with no database asks for one and reads nothing") + func noDatabaseAsksForOne() async { + let metadata = HealthMetadata() + let viewModel = makeViewModel(metadata, database: BoundDatabase("")) + await viewModel.refresh() #expect(viewModel.loadError == TableHealthError.noDatabase.errorDescription) @@ -222,7 +340,7 @@ struct TableHealthViewModelTests { let viewModel = makeViewModel(metadata) await viewModel.refresh() - metadata.isConnected = false + metadata.leaseFails = true await viewModel.refresh() #expect(viewModel.report?.findingCount == 1) @@ -234,31 +352,35 @@ struct TableHealthViewModelTests { let metadata = HealthMetadata() let viewModel = makeViewModel(metadata) await viewModel.refresh() - let logs = try #require(viewModel.report?.checks.flatMap { $0.findings }.first) - viewModel.selectedFindingId = logs.id + let logs = try #require(row(viewModel, table: "logs")) + viewModel.scope = .check(.missingPrimaryKey) + viewModel.selection = [logs.id] metadata.server.failsMissingKeyCheck = true await viewModel.refresh() - let check = try #require(viewModel.report?.checks.first { $0.kind == .missingPrimaryKey }) + let check = try #require(viewModel.report?.result(for: .missingPrimaryKey)) #expect(check.outcome == .failed("No active connection")) #expect(check.findings.map { $0.table } == ["logs"]) - #expect(viewModel.selectedFinding?.table == "logs") + #expect(viewModel.selectedFindings.map { $0.table } == ["logs"]) + #expect(viewModel.checkFailure == "No active connection") + #expect(viewModel.listState == .rows) } - @Test("Findings from another database are not carried over when a check fails") - func otherDatabaseFindingsDropped() async { + @Test("A renamed database retargets the next refresh and carries no findings across") + func renameRetargetsTheNextRefresh() async { let metadata = HealthMetadata() - let viewModel = makeViewModel(metadata) + let database = BoundDatabase("shop") + let viewModel = makeViewModel(metadata, database: database) await viewModel.refresh() - metadata.database = "archive" + database.name = "shop_v2" metadata.server.failsMissingKeyCheck = true await viewModel.refresh() - #expect(viewModel.report?.database == "archive") + #expect(metadata.leases.map { $0.scope.database } == ["shop", "shop_v2"]) + #expect(viewModel.report?.database == "shop_v2") #expect(viewModel.report?.findingCount == 0) - #expect(viewModel.selectedFindingId == nil) } @Test("A refresh drops the selection when its finding is gone and keeps it otherwise") @@ -268,15 +390,202 @@ struct TableHealthViewModelTests { let viewModel = makeViewModel(metadata) await viewModel.refresh() - let events = try #require(viewModel.report?.checks.flatMap { $0.findings }.first { $0.table == "events" }) - viewModel.selectedFindingId = events.id + let events = try #require(row(viewModel, table: "events")) + viewModel.selection = [events.id] await viewModel.refresh() - #expect(viewModel.selectedFinding?.table == "events") + #expect(viewModel.selectedFindings.map { $0.table } == ["events"]) + + metadata.server.tablesWithoutKey = ["logs"] + await viewModel.refresh() + #expect(viewModel.selectedFindings.isEmpty) + } + + @Test("The first run marks nothing new; a finding that appears later is New") + func newFindingsAfterTheFirstRun() async throws { + let metadata = HealthMetadata() + let history = InMemoryHistory() + let viewModel = makeViewModel(metadata, history: history) + await viewModel.refresh() + #expect(viewModel.findingRows.map { $0.status } == [.seen]) + + metadata.server.tablesWithoutKey = ["logs", "events"] + await viewModel.refresh() + + #expect(history.recordCount == 2) + #expect(row(viewModel, table: "events")?.status == .new) + #expect(row(viewModel, table: "logs")?.status == .seen) + } + + @Test("A finding gone since the last run is listed under Resolved with when it was last seen") + func goneFindingsAreResolved() async throws { + let metadata = HealthMetadata() + metadata.server.tablesWithoutKey = ["logs", "events"] + let viewModel = makeViewModel(metadata) + await viewModel.refresh() metadata.server.tablesWithoutKey = ["logs"] await viewModel.refresh() - #expect(viewModel.selectedFindingId == nil) - #expect(viewModel.selectedFinding == nil) + viewModel.scope = .resolved + + #expect(viewModel.resolvedCount == 1) + let resolved = try #require(viewModel.rows.first) + #expect(resolved.id.table == "events") + #expect(resolved.status == .resolved) + #expect(resolved.lastSeen != nil) + #expect(resolved.finding == nil) + + viewModel.clearResolved() + #expect(viewModel.resolvedCount == 0) + #expect(viewModel.listState == .noResolved) + } + + @Test("Ignore hides a finding until Show Ignored, survives a refresh, and Stop Ignoring brings it back") + func ignoreHidesUntilShown() async throws { + let metadata = HealthMetadata() + metadata.server.tablesWithoutKey = ["logs", "events"] + let viewModel = makeViewModel(metadata) + await viewModel.refresh() + let logs = try #require(row(viewModel, table: "logs")) + + viewModel.setIgnored([logs.id], ignored: true) + await viewModel.refresh() + + #expect(viewModel.rows.map { $0.id.table } == ["events"]) + #expect(viewModel.allFindingsCount == 1) + #expect(viewModel.badgeCount(for: .missingPrimaryKey) == 1) + viewModel.showIgnored = true + #expect(Set(viewModel.rows.compactMap { $0.id.table }) == ["logs", "events"]) + #expect(row(viewModel, table: "logs")?.status == .ignored) + + viewModel.setIgnored([logs.id], ignored: false) + viewModel.showIgnored = false + #expect(viewModel.rows.count == 2) + } + + @Test("When every finding is ignored the pane says how many are hidden") + func everyFindingIgnored() async throws { + let metadata = HealthMetadata() + let viewModel = makeViewModel(metadata) + await viewModel.refresh() + let logs = try #require(row(viewModel, table: "logs")) + + viewModel.setIgnored([logs.id], ignored: true) + + #expect(viewModel.rows.isEmpty) + #expect(viewModel.listState == .ignoredHidden(1)) + } + + @Test("A history that cannot be saved keeps the report and says so") + func storeFailureKeepsTheReport() async throws { + let metadata = HealthMetadata() + let history = InMemoryHistory() + history.failsWrites = true + let viewModel = makeViewModel(metadata, history: history) + + await viewModel.refresh() + + #expect(viewModel.report?.findingCount == 1) + #expect(viewModel.storeError == "The disk is full") + #expect(viewModel.loadError == nil) + + history.failsWrites = false + await viewModel.refresh() + #expect(viewModel.storeError == nil) + } + + @Test("An ignore made in another tab on the same database reaches this one") + func otherTabsIgnoreArrives() async throws { + let metadata = HealthMetadata() + let history = InMemoryHistory() + let first = makeViewModel(metadata, history: history) + let second = makeViewModel(metadata, history: history) + await first.refresh() + await second.refresh() + let logs = try #require(row(first, table: "logs")) + + first.setIgnored([logs.id], ignored: true) + + try await waitUntil { second.rows.isEmpty } + #expect(row(second, table: "logs")?.status == .ignored) + } + + @Test("The filter narrows the rows and the export offers all, shown and selected findings") + func filterAndExportScopes() async throws { + let metadata = HealthMetadata() + metadata.server.tablesWithoutKey = ["logs", "events", "event_archive"] + let viewModel = makeViewModel(metadata) + await viewModel.refresh() + + viewModel.filterText = "event" + #expect(Set(viewModel.rows.compactMap { $0.id.table }) == ["events", "event_archive"]) + let logs = try #require(row(viewModel, table: "logs")) + let events = try #require(row(viewModel, table: "events")) + viewModel.selection = [logs.id, events.id] + + let request = try #require(viewModel.exportRequest()) + #expect(request.scopes.map { $0.id } == ["all", "shown", "selected"]) + #expect(request.scopes.map { $0.rowCount } == [3, 2, 1]) + #expect(request.initialScope?.id == "shown") + #expect(request.databaseTypeId == DataSourceExportRequest.dataFileTypeId) + + viewModel.filterText = "nothing matches" + #expect(viewModel.listState == .noResults) + } + + @Test("With every finding shown, the export offers only all findings") + func exportWithNothingNarrowed() async throws { + let metadata = HealthMetadata() + let viewModel = makeViewModel(metadata) + await viewModel.refresh() + + let request = try #require(viewModel.exportRequest()) + + #expect(request.scopes.map { $0.id } == ["all"]) + #expect(request.initialScope?.id == "all") + } + + @Test("A column sort reorders the rows once, here largest table first") + func sortBySize() async { + let metadata = HealthMetadata() + metadata.server.tablesWithoutKey = ["small", "large", "medium"] + metadata.server.tableBytes = ["small": 10, "large": 3_000, "medium": 500] + let viewModel = makeViewModel(metadata) + await viewModel.refresh() + + viewModel.sortOrder = [KeyPathComparator(\TableHealthFindingRow.sizeSortKey, order: .reverse)] + + #expect(viewModel.rows.map { $0.id.table } == ["large", "medium", "small"]) + } + + @Test("Copy writes the shown columns as tab-separated rows under a header") + func copyIsTabSeparatedWithAHeader() async throws { + let metadata = HealthMetadata() + let viewModel = makeViewModel(metadata) + await viewModel.refresh() + let logs = try #require(row(viewModel, table: "logs")) + + let text = try #require(viewModel.tsv(for: [logs])) + let lines = text.components(separatedBy: "\n") + + #expect(lines.count == 2) + #expect(lines[0] == TableHealthColumn.findings.map { $0.title }.joined(separator: "\t")) + #expect(lines[1].components(separatedBy: "\t").count == TableHealthColumn.findings.count) + #expect(lines[1].contains("public.logs")) + } + + @Test("One fix opens as itself; several open as one script naming each finding") + func fixTextForOneAndSeveral() async throws { + let metadata = HealthMetadata() + metadata.server.tablesWithoutKey = ["logs", "events"] + let viewModel = makeViewModel(metadata) + await viewModel.refresh() + let findings = viewModel.findingRows.compactMap(\.finding) + let logs = try #require(findings.first { $0.table == "logs" }) + + #expect(viewModel.fixText(for: [logs], databaseType: .postgresql) == logs.fixSQL) + let script = try #require(viewModel.fixText(for: findings, databaseType: .postgresql)) + #expect(script.hasPrefix("-- ")) + #expect(findings.allSatisfy { finding in finding.fixSQL.map { script.contains($0) } ?? false }) } } diff --git a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift index d7fcf7c933..72a56ca92b 100644 --- a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift +++ b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift @@ -391,9 +391,6 @@ struct MainContentCoordinatorRefreshTests { expected += [tabManager.selectedTabId].compactMap { $0 } coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) } - tabManager.addTableHealthTab(databaseName: "shop") - expected += [tabManager.selectedTabId].compactMap { $0 } - coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) tabManager.addObjectSourceTab(objectRef: DatabaseObjectRef(kind: .view, name: "active_orders", database: "shop")) expected += [tabManager.selectedTabId].compactMap { $0 } coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) @@ -401,6 +398,39 @@ struct MainContentCoordinatorRefreshTests { #expect(requested == expected) } + @Test("Refresh on a Table Health tab refreshes its model on the tab's own database, with no broadcast") + func refreshOnTableHealthRefreshesItsModel() async throws { + let (coordinator, tabManager) = makeCoordinator() + var requested: [UUID] = [] + let subscription = AppEvents.shared.tabRefreshRequested.sink { requested.append($0) } + defer { subscription.cancel() } + let tabId = try #require(tabManager.addTableHealthTab(databaseName: "shop")) + let metadata = RefreshLeaseRecorder() + coordinator.tableHealthModels[tabId] = TableHealthViewModel( + connectionId: coordinator.connectionId, + provider: TableHealthProviderFactory.provider(for: .postgresql), + history: RefreshUnusedHistory(), + metadata: metadata, + database: { "shop" } + ) + + coordinator.handleRefresh(hasPendingTableOps: false, onDiscard: {}) + + try await waitUntil { metadata.databases == ["shop"] } + #expect(requested.isEmpty) + } + + @Test("Closing a Table Health tab drops its model") + func closingTableHealthDropsItsModel() throws { + let (coordinator, tabManager) = makeCoordinator() + let tabId = try #require(tabManager.addTableHealthTab(databaseName: "shop")) + coordinator.tableHealthModel(for: tabId) + + coordinator.cleanupTabCaches(openTabIds: []) + + #expect(coordinator.tableHealthModels[tabId] == nil) + } + @Test("Refresh on Users & Roles reloads the principals through the tab's own handler") func refreshOnUsersRolesReloads() { let (coordinator, tabManager) = makeCoordinator() @@ -415,3 +445,48 @@ struct MainContentCoordinatorRefreshTests { #expect(reloads == 1) } } + +private struct RefreshLeaseRefused: Error {} + +@MainActor +private final class RefreshLeaseRecorder: ScopedMetadataProviding { + private(set) var databases: [String] = [] + + func withMetadataDriver( + scope: DatabaseScope, + workload: MetadataConnectionPool.Workload, + _ body: @Sendable @escaping (DatabaseDriver) async throws -> T + ) async throws -> T { + databases.append(scope.database) + throw RefreshLeaseRefused() + } + + func browseScope(for connectionId: UUID) -> DatabaseScope? { + nil + } +} + +@MainActor +private final class RefreshUnusedHistory: TableHealthHistoryStoring { + func ledger(connectionId: UUID, database: String) throws -> TableHealthLedger { + TableHealthLedger() + } + + func record(_ report: TableHealthReport, connectionId: UUID, database: String, at date: Date) throws -> TableHealthLedger { + TableHealthLedger() + } + + func setIgnored( + _ ids: Set, + ignored: Bool, + connectionId: UUID, + database: String, + at date: Date + ) throws -> TableHealthLedger { + TableHealthLedger() + } + + func clearResolved(connectionId: UUID, database: String) throws -> TableHealthLedger { + TableHealthLedger() + } +} diff --git a/apple/scripts/probes/check-postgresql-catalog-shadowing.sh b/apple/scripts/probes/check-postgresql-catalog-shadowing.sh index dbf59b4cd2..4553423db9 100755 --- a/apple/scripts/probes/check-postgresql-catalog-shadowing.sh +++ b/apple/scripts/probes/check-postgresql-catalog-shadowing.sh @@ -167,7 +167,8 @@ static_query() { ' "$1" } HEALTH="$ROOT/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift" -for name in statisticsQuery bloatQuery unusedIndexQuery missingPrimaryKeyQuery unindexedForeignKeyQuery; do +for name in statisticsQuery bloatQuery unusedIndexQuery missingPrimaryKeyQuery unindexedForeignKeyQuery \ + redundantIndexQuery invalidIndexQuery nearLimitQuery transactionIdAgeQuery staleStatisticsQuery; do static_query "$HEALTH" "$name" > "$HOST_SQL/health-$name.sql" [[ -s "$HOST_SQL/health-$name.sql" ]] || { echo "FAIL: $name not found in $HEALTH" >&2; exit 2; } done diff --git a/apple/scripts/probes/check-table-health-catalogs.sh b/apple/scripts/probes/check-table-health-catalogs.sh index 7f98fd43c2..120d5f2aa3 100755 --- a/apple/scripts/probes/check-table-health-catalogs.sh +++ b/apple/scripts/probes/check-table-health-catalogs.sh @@ -5,15 +5,20 @@ # Each query is read out of the provider source, so the probe and the app cannot drift. The probe # builds a database with one object per finding and one per near miss, runs every query, checks # which objects come back, then runs the fix the report would preview and checks the finding goes. -# The MySQL checks that combine rows in Swift (unused minus unique and foreign key indexes, -# redundant indexes) are pinned by MySQLTableHealthProviderTests; here, the rows they read. +# The checks that combine rows in Swift (MySQL unused minus unique and foreign key indexes, redundant +# indexes on both engines) are pinned by MySQLTableHealthProviderTests and PostgreSQLIndexShapeTests; +# here, the rows they read. # # Usage: # scripts/probes/check-table-health-catalogs.sh postgres # scripts/probes/check-table-health-catalogs.sh mysql # +# TABLEPRO_PROBE_RECLAIMABLE=1 adds the MySQL reclaimable space case, about 1 GB of writes. +# # Passwords come from PGPASSWORD and MYSQL_PWD. Needs psql or mysql on PATH, and a user that may -# create and drop a database. Exits non-zero on a disagreement, 3 when it cannot run. +# create and drop a database; on PostgreSQL also a role, for the unreadable sequence case. Exits non-zero +# on a disagreement, 3 when it cannot run. MySQL also takes a user, for the PROCESS refusal. PostgreSQL takes about a minute: the invalid index and +# transaction ID cases wait on held snapshots. set -uo pipefail @@ -24,6 +29,7 @@ USER_NAME="${4:-}" SOURCES="$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." && pwd)/TablePro/Core/TableHealth" DATABASE="tablepro_health_check" OTHER_DATABASE="tablepro_health_other" +READER_ROLE="tablepro_health_reader" FAILURES=0 # Prints the body of `static let = """ ... """`, or the text of `static let = "..."`. @@ -42,6 +48,16 @@ query_of() { printf '%s' "$text" } +expect_pattern() { + local label="$1" output="$2" pattern="$3" + if grep -qE -- "$pattern" <<< "$output"; then + echo "ok $label" + else + echo "FAIL $label: expected a match for '$pattern'" >&2 + FAILURES=$((FAILURES + 1)) + fi +} + expect() { local label="$1" output="$2" needle="$3" wanted="$4" found=no grep -qF -- "$needle" <<< "$output" && found=yes @@ -65,7 +81,7 @@ check_postgres() { echo "PostgreSQL $(pg -d postgres -c 'SHOW server_version') at $HOST:$PORT" pg -d postgres -c "DROP DATABASE IF EXISTS $DATABASE" > /dev/null pg -d postgres -c "CREATE DATABASE $DATABASE" > /dev/null - trap 'psql -X -q -h "$HOST" -p "$PORT" -U "$USER_NAME" -d postgres -c "DROP DATABASE IF EXISTS '"$DATABASE"' WITH (FORCE)" > /dev/null 2>&1 || psql -X -q -h "$HOST" -p "$PORT" -U "$USER_NAME" -d postgres -c "DROP DATABASE IF EXISTS '"$DATABASE"'" > /dev/null 2>&1' EXIT + trap cleanup_postgres EXIT pg -d "$DATABASE" > /dev/null <<'SQL' CREATE TABLE parent (id int PRIMARY KEY, code int UNIQUE); @@ -104,8 +120,9 @@ SQL fi # Another session's temporary table lives in its own pg_temp schema while that session is open. - psql -X -q -h "$HOST" -p "$PORT" -U "$USER_NAME" -d "$DATABASE" \ - -c "CREATE TEMP TABLE probe_temp (v int)" -c "CREATE INDEX probe_temp_idx ON probe_temp (v)" -c "SELECT pg_sleep(30)" \ + PGAPPNAME=tablepro_health_temp psql -X -q -h "$HOST" -p "$PORT" -U "$USER_NAME" -d "$DATABASE" \ + -c "CREATE TEMP TABLE probe_temp (v int)" -c "CREATE INDEX probe_temp_idx ON probe_temp (v)" \ + -c "CREATE TEMP SEQUENCE probe_temp_seq MAXVALUE 100" -c "SELECT setval('probe_temp_seq', 90)" -c "SELECT pg_sleep(30)" \ > /dev/null 2>&1 & local holder=$! temp_ready=0 for _ in $(seq 1 20); do @@ -144,6 +161,16 @@ SQL expect "unused index skips a partition index its parent owns" "$unused" "events_2026_kind_idx" no fi + local shape + shape="$(pg -d "$DATABASE" -c "$(query_of "$source" redundantIndexQuery)")" + expect "index shape skips another session's temporary table" "$shape" "probe_temp_idx" no + expect "index shape reads a primary key as protected" "$shape" "public|parent|r|parent_pkey|btree|t|t|t|false|t|" yes + if [ "$version" -ge 100000 ]; then + local sequences + sequences="$(pg -d "$DATABASE" -c "$(query_of "$source" nearLimitQuery)")" + expect "sequences skip another session's temporary sequence without reading it" "$sequences" "probe_temp_seq" no + fi + local nopk nopk="$(pg -d "$DATABASE" -c "$(query_of "$source" missingPrimaryKeyQuery)")" expect "missing primary key lists a heap" "$nopk" "public|no_pk" yes @@ -200,8 +227,417 @@ SQL nopk="$(pg -d "$DATABASE" -c "$(query_of "$source" missingPrimaryKeyQuery)")" expect "the previewed primary key clears its finding" "$nopk" "public|no_pk" no + terminate tablepro_health_temp kill "$holder" 2> /dev/null wait "$holder" 2> /dev/null + + check_postgres_extension_members + check_postgres_redundant_indexes + check_postgres_invalid_indexes + if [ "$version" -ge 100000 ]; then + check_postgres_sequences + else + echo "note PostgreSQL $version has no pg_sequence, so the sequence check is skipped there" + fi + check_postgres_stale_statistics + check_postgres_transaction_id_age +} + +cleanup_postgres() { + local admin=(psql -X -q -h "$HOST" -p "$PORT" -U "$USER_NAME" -d postgres) + "${admin[@]}" -c "SELECT pg_catalog.pg_terminate_backend(pid) FROM pg_catalog.pg_stat_activity WHERE datname = '$DATABASE' AND pid <> pg_catalog.pg_backend_pid()" > /dev/null 2>&1 + "${admin[@]}" -c "DROP DATABASE IF EXISTS $DATABASE" > /dev/null 2>&1 + "${admin[@]}" -c "DROP ROLE IF EXISTS $READER_ROLE" > /dev/null 2>&1 +} + +# Ends the sessions a background psql opened under this application name. Killing psql alone leaves the +# server running the statement, and the snapshot it holds, until the statement ends. +terminate() { + pg -d postgres -c "SELECT pg_catalog.pg_terminate_backend(pid) FROM pg_catalog.pg_stat_activity WHERE application_name = '$1'" > /dev/null +} + +# Holds a REPEATABLE READ snapshot in the background under application name $1 for $2 seconds, and waits +# until the server shows it. +hold_snapshot() { + PGAPPNAME="$1" psql -X -q -h "$HOST" -p "$PORT" -U "$USER_NAME" -d "$DATABASE" \ + -c "BEGIN ISOLATION LEVEL REPEATABLE READ; SELECT 1; SELECT pg_sleep($2); COMMIT;" > /dev/null 2>&1 & + for _ in $(seq 1 20); do + [ "$(pg -d postgres -c "SELECT count(*) FROM pg_stat_activity WHERE application_name = '$1' AND backend_xmin IS NOT NULL")" = "1" ] && return + sleep 0.25 + done + echo "note the snapshot held as $1 did not appear" >&2 +} + +# An extension's own table, and a table in an extension's schema the way TimescaleDB keeps its chunks, each +# with what every check would list: no primary key, a duplicate index, rows never analyzed, a low freeze age. +check_postgres_extension_members() { + if ! pg -d "$DATABASE" -c "CREATE EXTENSION IF NOT EXISTS btree_gist" > /dev/null 2>&1; then + echo "note btree_gist is not available, so the extension member cases are skipped" + return + fi + pg -d "$DATABASE" > /dev/null <<'SQL' +CREATE TABLE ext_owned (a int) WITH (autovacuum_enabled = false, autovacuum_freeze_max_age = 100000); +CREATE INDEX ext_owned_a1 ON ext_owned (a); +CREATE INDEX ext_owned_a2 ON ext_owned (a); +INSERT INTO ext_owned SELECT g FROM generate_series(1, 100) g; +ALTER EXTENSION btree_gist ADD TABLE ext_owned; +CREATE SCHEMA ext_schema; +ALTER EXTENSION btree_gist ADD SCHEMA ext_schema; +CREATE TABLE ext_schema.chunk (a int) WITH (autovacuum_enabled = false, autovacuum_freeze_max_age = 100000); +CREATE INDEX chunk_a1 ON ext_schema.chunk (a); +CREATE INDEX chunk_a2 ON ext_schema.chunk (a); +INSERT INTO ext_schema.chunk SELECT g FROM generate_series(1, 100) g; +SQL + local query output + for query in bloatQuery unusedIndexQuery redundantIndexQuery missingPrimaryKeyQuery unindexedForeignKeyQuery \ + invalidIndexQuery staleStatisticsQuery transactionIdAgeQuery; do + output="$(pg -d "$DATABASE" -c "$(query_of "$source" "$query")")" + expect "$query skips an extension's table" "$output" "ext_owned" no + expect "$query skips a table in an extension's schema" "$output" "ext_schema" no + done + if [ "$version" -ge 100000 ]; then + pg -d "$DATABASE" > /dev/null <<'SQL' +CREATE SEQUENCE ext_owned_seq AS smallint; +SELECT setval('ext_owned_seq', 32000); +ALTER EXTENSION btree_gist ADD SEQUENCE ext_owned_seq; +CREATE SEQUENCE ext_schema.chunk_seq AS smallint; +SELECT setval('ext_schema.chunk_seq', 32000); +SQL + output="$(pg -d "$DATABASE" -c "$(query_of "$source" nearLimitQuery)")" + expect "nearLimitQuery skips an extension's sequence" "$output" "ext_owned_seq" no + expect "nearLimitQuery skips a sequence in an extension's schema" "$output" "ext_schema" no + fi +} + +check_postgres_redundant_indexes() { + pg -d "$DATABASE" > /dev/null <<'SQL' +CREATE TABLE ri (id int PRIMARY KEY, a int, b int, c int, email text, code text, note text); +CREATE INDEX r_a ON ri (a); +CREATE INDEX k_ab ON ri (a, b); +CREATE INDEX r_ab_dup ON ri (a, b); +CREATE INDEX k_a_desc ON ri (a DESC); +CREATE INDEX k_ba ON ri (b, a); +CREATE INDEX k_a_partial ON ri (a) WHERE a > 0; +CREATE INDEX k_a_partial_twin_b ON ri (a, b) WHERE a > 0; +CREATE INDEX r_a_partial_dup ON ri (a) WHERE a > 0; +CREATE INDEX k_lower_email ON ri (lower(email)); +CREATE INDEX r_lower_email_dup ON ri (lower(email)); +CREATE INDEX k_upper_email ON ri (upper(email)); +CREATE INDEX k_code_pattern ON ri (code text_pattern_ops); +CREATE INDEX k_code ON ri (code); +CREATE INDEX k_code_c ON ri (code COLLATE "C"); +CREATE INDEX k_hash_a ON ri USING hash (a); +CREATE UNIQUE INDEX k_uniq_c ON ri (c); +CREATE INDEX k_cb ON ri (c, b); +CREATE INDEX r_c_plain ON ri (c); +CREATE UNIQUE INDEX r_id_uniq ON ri (id); +CREATE INDEX k_note_gin ON ri USING gin (to_tsvector('simple', note)); +CREATE INDEX k_note_gin_2 ON ri USING gin (to_tsvector('english', note)); +CREATE TABLE rc (id int PRIMARY KEY, x int, CONSTRAINT k_rc_x_key UNIQUE (x)); +CREATE UNIQUE INDEX r_rc_x_uidx ON rc (x); +CREATE INDEX r_rc_x_plain ON rc (x); +CREATE TABLE rk (id int PRIMARY KEY, z int); +CREATE UNIQUE INDEX k_rk_z_fkref ON rk (z); +CREATE UNIQUE INDEX r_rk_z_twin ON rk (z); +CREATE TABLE rk_child (id int PRIMARY KEY, z int REFERENCES rk (z)); +CREATE TABLE rd (id int PRIMARY KEY, v int, CONSTRAINT k_rd_v_deferrable UNIQUE (v) DEFERRABLE INITIALLY DEFERRED); +CREATE UNIQUE INDEX k_rd_v_immediate ON rd (v); +CREATE TABLE rr (id int PRIMARY KEY, k int NOT NULL); +CREATE UNIQUE INDEX rr_zz_replident ON rr (k); +ALTER TABLE rr REPLICA IDENTITY USING INDEX rr_zz_replident; +CREATE UNIQUE INDEX rr_aa_twin ON rr (k); +CREATE MATERIALIZED VIEW rmv AS SELECT g AS a, g AS b FROM generate_series(1, 10) g; +CREATE INDEX k_rmv_ab ON rmv (a, b); +CREATE INDEX r_rmv_a ON rmv (a); +SQL + if [ "$version" -ge 110000 ]; then + pg -d "$DATABASE" > /dev/null <<'SQL' +CREATE TABLE ri_incl (id int PRIMARY KEY, a int, b int, c int); +CREATE INDEX k_a_incl_c ON ri_incl (a) INCLUDE (c); +CREATE INDEX k_ab2 ON ri_incl (a, b); +CREATE INDEX k_abc_incl ON ri_incl (a, b) INCLUDE (c); +CREATE INDEX r_a_only ON ri_incl (a); +CREATE TABLE rp (id int, at date, k int) PARTITION BY RANGE (at); +CREATE TABLE rp_2026 PARTITION OF rp FOR VALUES FROM ('2026-01-01') TO ('2027-01-01'); +CREATE INDEX k_rp_k_at ON rp (k, at); +CREATE INDEX r_rp_k ON rp (k); +CREATE INDEX r_rp_2026_k_standalone ON rp_2026 (k); +SQL + fi + if [ "$version" -ge 150000 ]; then + pg -d "$DATABASE" -c "CREATE UNIQUE INDEX k_nd ON ri (b)" -c "CREATE UNIQUE INDEX k_nnd ON ri (b) NULLS NOT DISTINCT" \ + -c "CREATE INDEX r_b_plain ON ri (b)" > /dev/null + fi + if pg -d "$DATABASE" -c "CREATE EXTENSION IF NOT EXISTS btree_gist" > /dev/null 2>&1; then + pg -d "$DATABASE" > /dev/null <<'SQL' +CREATE TABLE rx (id int PRIMARY KEY, room int, during tsrange, EXCLUDE USING gist (room WITH =, during WITH &&)); +CREATE INDEX k_rx_room_gist ON rx USING gist (room); +SQL + fi + + local shape + shape="$(pg -d "$DATABASE" -c "$(query_of "$source" redundantIndexQuery)")" + expect "index shape reads a unique constraint's index as protected" "$shape" "public|rc|r|k_rc_x_key|btree|t|f|t|false|t|" yes + expect "index shape reads a plain unique index as unprotected" "$shape" "public|rc|r|r_rc_x_uidx|btree|t|f|t|false|f|" yes + expect "index shape reads an index a foreign key uses as protected" "$shape" "public|rk|r|k_rk_z_fkref|btree|t|f|t|false|t|" yes + expect "index shape reads a deferrable constraint as not immediate" "$shape" "public|rd|r|k_rd_v_deferrable|btree|t|f|f|false|t|" yes + expect "index shape reads the replica identity index as protected" "$shape" "public|rr|r|rr_zz_replident|btree|t|f|t|false|t|" yes + expect "index shape reads a predicate" "$shape" "public|ri|r|r_a_partial_dup|btree|f|f|t|false|f|(a > 0)|" yes + expect "index shape deparses an expression key" "$shape" "|1|1|lower(email)|" yes + expect "index shape reads a descending key's sort flags" "$shape" "|1|1|a|1978|0|3" yes + expect "index shape reads a materialized view's index" "$shape" "public|rmv|m|r_rmv_a|btree|" yes + expect "index shape reads a GIN index's method" "$shape" "public|ri|r|k_note_gin|gin|" yes + if [ "$version" -ge 110000 ]; then + expect "index shape reads an INCLUDE column after the key count" "$shape" "k_a_incl_c|btree|f|f|t|false|f||8192||1|2|c|||" yes + expect "index shape reads an attached partition index as protected" "$shape" "public|rp_2026|r|rp_2026_k_at_idx|btree|f|f|t|false|t|" yes + expect "index shape reads a partitioned table's index" "$shape" "public|rp|p|r_rp_k|btree|f|f|t|false|f||0|" yes + fi + if [ "$version" -ge 150000 ]; then + expect "index shape reads NULLS NOT DISTINCT" "$shape" "public|ri|r|k_nnd|btree|t|f|t|true|f|" yes + fi + + # The fixes exactly as PostgreSQLIndexShapeTests pins them, and the refusals that make the protected set. + local refused + for index in r_a r_ab_dup r_lower_email_dup r_c_plain r_id_uniq r_rc_x_uidx r_rk_z_twin rr_aa_twin r_rmv_a; do + pg -d "$DATABASE" -c "DROP INDEX CONCURRENTLY \"public\".\"$index\";" > /dev/null + done + refused="$(pg -d "$DATABASE" -c 'DROP INDEX CONCURRENTLY "public"."k_rc_x_key";' 2>&1)" + expect "a unique constraint's index cannot be dropped alone" "$refused" "requires it" yes + refused="$(pg -d "$DATABASE" -c 'DROP INDEX CONCURRENTLY "public"."k_rk_z_fkref";' 2>&1)" + expect "an index a foreign key uses cannot be dropped alone" "$refused" "depend on it" yes + if [ "$version" -ge 110000 ]; then + refused="$(pg -d "$DATABASE" -c 'DROP INDEX CONCURRENTLY "public"."r_rp_k";' 2>&1)" + expect "a partitioned table's index cannot be dropped concurrently" "$refused" "concurrently" yes + refused="$(pg -d "$DATABASE" -c 'DROP INDEX CONCURRENTLY "public"."rp_2026_k_at_idx";' 2>&1)" + expect "an attached partition index cannot be dropped alone" "$refused" "requires it" yes + pg -d "$DATABASE" -c 'DROP INDEX "public"."r_rp_k";' > /dev/null + pg -d "$DATABASE" -c 'DROP INDEX CONCURRENTLY "public"."r_rp_2026_k_standalone";' > /dev/null + fi + shape="$(pg -d "$DATABASE" -c "$(query_of "$source" redundantIndexQuery)")" + expect "the previewed drops clear their findings" "$shape" "|r_ab_dup|" no + expect "the previewed drop on a materialized view clears its finding" "$shape" "|r_rmv_a|" no + if [ "$version" -ge 110000 ]; then + expect "the previewed plain drop clears a partitioned table's finding" "$shape" "|r_rp_k|" no + fi +} + +check_postgres_invalid_indexes() { + pg -d "$DATABASE" > /dev/null <<'SQL' +CREATE TABLE inv (id int PRIMARY KEY, v int, w int); +INSERT INTO inv SELECT g, g % 10, g FROM generate_series(1, 1000) g; +CREATE INDEX inv_w_idx ON inv (w); +SQL + local failed + failed="$(pg -d "$DATABASE" -c "CREATE UNIQUE INDEX CONCURRENTLY inv_v_uniq ON inv (v)" 2>&1)" + expect "a unique concurrent build over duplicates fails" "$failed" "could not create unique index" yes + + # A concurrent build waits for older snapshots; cancelled while it waits, it leaves its index invalid. + hold_snapshot tablepro_health_invalid 5 + pg -d "$DATABASE" -c "SET statement_timeout = '1500ms'" -c "CREATE INDEX CONCURRENTLY inv_vw_cancelled ON inv (v, w)" > /dev/null 2>&1 + if [ "$version" -ge 120000 ]; then + pg -d "$DATABASE" -c "SET statement_timeout = '1500ms'" -c "REINDEX INDEX CONCURRENTLY inv_w_idx" > /dev/null 2>&1 + fi + terminate tablepro_health_invalid + if [ "$version" -ge 110000 ]; then + pg -d "$DATABASE" -c "CREATE INDEX rp_id_only ON ONLY rp (id)" > /dev/null + fi + + # While a build runs, its table holds SHARE UPDATE EXCLUSIVE and every invalid index on it is left out. + hold_snapshot tablepro_health_building 4 + PGAPPNAME=tablepro_health_builder psql -X -q -h "$HOST" -p "$PORT" -U "$USER_NAME" -d "$DATABASE" \ + -c "CREATE INDEX CONCURRENTLY inv_building ON inv (w, v)" > /dev/null 2>&1 & + local builder=$! + for _ in $(seq 1 20); do + [ "$(pg -d "$DATABASE" -c "SELECT count(*) FROM pg_locks WHERE relation = 'inv'::regclass AND mode = 'ShareUpdateExclusiveLock' AND granted")" = "1" ] && break + sleep 0.25 + done + local invalid + invalid="$(pg -d "$DATABASE" -c "$(query_of "$source" invalidIndexQuery)")" + expect "invalid indexes leave out a table with a build running" "$invalid" "|inv|" no + wait "$builder" + + invalid="$(pg -d "$DATABASE" -c "$(query_of "$source" invalidIndexQuery)")" + expect "invalid indexes list a failed unique build, never ready" "$invalid" "public|inv|inv_v_uniq|t|f|" yes + expect "invalid indexes list a cancelled build, still updated by writes" "$invalid" "public|inv|inv_vw_cancelled|f|t|" yes + expect "invalid indexes skip a build that finished" "$invalid" "inv_building" no + if [ "$version" -ge 120000 ]; then + expect "invalid indexes list a cancelled REINDEX CONCURRENTLY's new copy" "$invalid" "public|inv|inv_w_idx_ccnew|f|" yes + fi + if [ "$version" -ge 110000 ]; then + expect "invalid indexes skip an index built ON ONLY a partitioned table" "$invalid" "rp_id_only" no + fi + local shape + shape="$(pg -d "$DATABASE" -c "$(query_of "$source" redundantIndexQuery)")" + expect "index shape skips an invalid index" "$shape" "inv_vw_cancelled" no + + # The fixes exactly as PostgreSQLTableHealthProviderTests pins them. + if [ "$version" -ge 120000 ]; then + pg -d "$DATABASE" -c 'REINDEX INDEX CONCURRENTLY "public"."inv_vw_cancelled";' > /dev/null + pg -d "$DATABASE" -c 'DROP INDEX CONCURRENTLY "public"."inv_w_idx_ccnew";' > /dev/null + failed="$(pg -d "$DATABASE" -c 'REINDEX INDEX CONCURRENTLY "public"."inv_v_uniq";' 2>&1)" + expect "rebuilding a unique index fails while duplicates remain, as its detail says" "$failed" "could not create unique index" yes + else + pg -d "$DATABASE" -c 'DROP INDEX CONCURRENTLY "public"."inv_vw_cancelled";' > /dev/null + pg -d "$DATABASE" -c 'CREATE INDEX CONCURRENTLY inv_vw_cancelled ON public.inv USING btree (v, w);' > /dev/null + fi + invalid="$(pg -d "$DATABASE" -c "$(query_of "$source" invalidIndexQuery)")" + expect "the previewed rebuild clears its finding" "$invalid" "|inv_vw_cancelled|" no + expect "the previewed drop of a leftover copy clears its finding" "$invalid" "|inv_w_idx_ccnew|" no +} + +check_postgres_sequences() { + pg -d "$DATABASE" > /dev/null <<'SQL' +CREATE TABLE seq_int (id serial PRIMARY KEY, v int); +CREATE TABLE seq_int_child (id int PRIMARY KEY, seq_int_id int REFERENCES seq_int (id)); +CREATE TABLE seq_identity (id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, v int); +CREATE TABLE seq_small (id smallserial PRIMARY KEY); +CREATE TABLE seq_bigcol_intseq (id bigint PRIMARY KEY); +CREATE SEQUENCE seq_int_typed AS integer; +ALTER TABLE seq_bigcol_intseq ALTER COLUMN id SET DEFAULT nextval('seq_int_typed'); +CREATE TABLE seq_intcol_bigseq (id int PRIMARY KEY); +CREATE SEQUENCE seq_big_for_int; +ALTER TABLE seq_intcol_bigseq ALTER COLUMN id SET DEFAULT nextval('seq_big_for_int'); +CREATE TABLE seq_healthy (id serial PRIMARY KEY); +CREATE SEQUENCE seq_standalone_small AS smallint; +CREATE SEQUENCE seq_never_used AS integer; +CREATE SEQUENCE seq_cycle AS smallint CYCLE; +CREATE TABLE seq_desc (id int PRIMARY KEY); +CREATE SEQUENCE seq_desc_s AS integer INCREMENT BY -1; +ALTER TABLE seq_desc ALTER COLUMN id SET DEFAULT nextval('seq_desc_s'); +CREATE SEQUENCE seq_explicit AS integer MAXVALUE 1000; +CREATE SEQUENCE seq_secret AS smallint; +SELECT setval('seq_int_id_seq', 1700000000); +SELECT setval('seq_identity_id_seq', 1800000000); +SELECT setval('seq_small_id_seq', 30000); +SELECT setval('seq_int_typed', 2000000000); +SELECT setval('seq_big_for_int', 1700000000); +SELECT nextval('seq_healthy_id_seq'); +SELECT setval('seq_standalone_small', 30000); +SELECT setval('seq_cycle', 32000); +SELECT setval('seq_desc_s', -1700000000); +SELECT setval('seq_explicit', 900); +SELECT setval('seq_secret', 32000); +SQL + local sequences + sequences="$(pg -d "$DATABASE" -c "$(query_of "$source" nearLimitQuery)")" + expect "a serial column near its limit, bounded by the column" "$sequences" "public|seq_int_id_seq|integer|1|1|2147483647|1700000000|1|2147483647|" yes + expect "a serial column names its table, column, type limits and ownership" "$sequences" "|public|seq_int|id|integer|-2147483648|2147483647|owned|" yes + expect "a serial column names the foreign key column that stays narrow" "$sequences" "|public.seq_int_child.seq_int_id" yes + expect "an identity column" "$sequences" "|public|seq_identity|id|integer|-2147483648|2147483647|identity|" yes + expect "a smallserial column" "$sequences" "public|seq_small_id_seq|smallint|1|1|32767|30000|1|32767|" yes + expect "a bigint sequence is bounded by the integer column it feeds" "$sequences" "public|seq_big_for_int|bigint|1|1|9223372036854775807|1700000000|1|2147483647|" yes + expect "an integer sequence feeding a bigint column" "$sequences" "|public|seq_bigcol_intseq|id|bigint|||default|" yes + expect "a sequence no column uses" "$sequences" "public|seq_standalone_small|smallint|1|1|32767|30000|1|32767|" yes + expect "a descending sequence, bounded below" "$sequences" "public|seq_desc_s|integer|-1|-2147483648|-1|-1700000000|-2147483648|-1|" yes + expect "a sequence with its own lower maximum" "$sequences" "public|seq_explicit|integer|1|1|1000|900|1|1000|" yes + expect "sequences skip a cycling sequence" "$sequences" "seq_cycle" no + expect "sequences skip a sequence never used" "$sequences" "seq_never_used" no + expect "sequences skip a sequence far from its limit" "$sequences" "seq_healthy" no + + pg -d postgres -c "DROP ROLE IF EXISTS $READER_ROLE" > /dev/null 2>&1 + if pg -d "$DATABASE" -c "CREATE ROLE $READER_ROLE" > /dev/null 2>&1 \ + && pg -d "$DATABASE" -c "SET ROLE $READER_ROLE" > /dev/null 2>&1; then + pg -d "$DATABASE" -c "GRANT USAGE ON ALL SEQUENCES IN SCHEMA public TO $READER_ROLE" \ + -c "REVOKE ALL ON SEQUENCE seq_secret FROM $READER_ROLE" > /dev/null + local unreadable + unreadable="$(pg -d "$DATABASE" -c "SET ROLE $READER_ROLE" -c "$(query_of "$source" nearLimitQuery)")" + expect "a sequence the user cannot read comes back unread, for the check's note" "$unreadable" "public|seq_secret|smallint|1|1|32767||1|32767||f|" yes + expect "a sequence the user can read still reads" "$unreadable" "public|seq_small_id_seq|smallint|1|1|32767|30000|" yes + else + echo "note this user cannot create a role and take it on, so the unreadable sequence case is skipped" + fi + + # The fixes exactly as PostgreSQLTableHealthProviderTests pins them. + pg -d "$DATABASE" > /dev/null <<'SQL' +ALTER TABLE "public"."seq_int" ALTER COLUMN "id" TYPE bigint; +ALTER SEQUENCE "public"."seq_int_id_seq" AS bigint; +ALTER TABLE "public"."seq_identity" ALTER COLUMN "id" TYPE bigint; +ALTER TABLE "public"."seq_small" ALTER COLUMN "id" TYPE bigint; +ALTER SEQUENCE "public"."seq_small_id_seq" AS bigint; +ALTER TABLE "public"."seq_intcol_bigseq" ALTER COLUMN "id" TYPE bigint; +ALTER SEQUENCE "public"."seq_int_typed" AS bigint; +ALTER SEQUENCE "public"."seq_standalone_small" AS bigint; +ALTER TABLE "public"."seq_desc" ALTER COLUMN "id" TYPE bigint; +ALTER SEQUENCE "public"."seq_desc_s" AS bigint; +ALTER SEQUENCE "public"."seq_explicit" AS bigint NO MAXVALUE; +ALTER SEQUENCE "public"."seq_secret" AS bigint; +SQL + sequences="$(pg -d "$DATABASE" -c "$(query_of "$source" nearLimitQuery)")" + expect "the previewed fixes clear every sequence finding" "$sequences" "seq_" no +} + +check_postgres_stale_statistics() { + # Insert, analyze and update in separate sessions: inside one session the server counts the rows twice. + pg -d "$DATABASE" > /dev/null <<'SQL' +CREATE TABLE st_never (id int PRIMARY KEY, v int) WITH (autovacuum_enabled = false); +CREATE TABLE st_stale (id int PRIMARY KEY, v int) WITH (autovacuum_enabled = false); +CREATE TABLE st_fresh (id int PRIMARY KEY, v int) WITH (autovacuum_enabled = false); +CREATE TABLE st_empty (id int PRIMARY KEY) WITH (autovacuum_enabled = false); +SQL + if [ "$version" -ge 100000 ]; then + pg -d "$DATABASE" -c "CREATE TABLE st_part (id int, at date) PARTITION BY RANGE (at)" \ + -c "CREATE TABLE st_part_2026 PARTITION OF st_part FOR VALUES FROM ('2026-01-01') TO ('2027-01-01') WITH (autovacuum_enabled = false)" \ + > /dev/null + pg -d "$DATABASE" -c "INSERT INTO st_part SELECT g, '2026-03-01' FROM generate_series(1, 5000) g" > /dev/null + fi + pg -d "$DATABASE" -c "INSERT INTO st_never SELECT g, g FROM generate_series(1, 5000) g" \ + -c "INSERT INTO st_stale SELECT g, g FROM generate_series(1, 5000) g" \ + -c "INSERT INTO st_fresh SELECT g, g FROM generate_series(1, 5000) g" > /dev/null + pg -d "$DATABASE" -c "ANALYZE st_stale" -c "ANALYZE st_fresh" > /dev/null + pg -d "$DATABASE" -c "UPDATE st_stale SET v = v + 1 WHERE id <= 1000" -c "UPDATE st_fresh SET v = v + 1 WHERE id <= 100" > /dev/null + + local stale="" + for _ in $(seq 1 10); do + stale="$(pg -d "$DATABASE" -c "$(query_of "$source" staleStatisticsQuery)")" + grep -qF "|st_stale|" <<< "$stale" && grep -qF "|st_never|" <<< "$stale" && break + sleep 1 + done + expect "stale statistics list a table never analyzed" "$stale" "public|st_never|r|5000|5000|50|t|" yes + expect "stale statistics list a table modified past 10% plus 50 rows" "$stale" "public|st_stale|r|5000|1000|550|f|" yes + expect "stale statistics skip a table modified less" "$stale" "st_fresh" no + expect "stale statistics skip an empty table" "$stale" "st_empty" no + if [ "$version" -ge 100000 ]; then + expect "stale statistics list a partitioned table no one analyzed" "$stale" "public|st_part|p|0|0|50|t|" yes + fi + + # The fixes exactly as PostgreSQLTableHealthProviderTests pins them. + pg -d "$DATABASE" -c 'ANALYZE "public"."st_never";' -c 'ANALYZE "public"."st_stale";' > /dev/null + if [ "$version" -ge 100000 ]; then + pg -d "$DATABASE" -c 'ANALYZE "public"."st_part";' > /dev/null + fi + for _ in $(seq 1 10); do + stale="$(pg -d "$DATABASE" -c "$(query_of "$source" staleStatisticsQuery)")" + grep -qF "|st_" <<< "$stale" || break + sleep 1 + done + expect "the previewed analyze clears its findings" "$stale" "|st_" no +} + +check_postgres_transaction_id_age() { + pg -d "$DATABASE" > /dev/null <<'SQL' +CREATE TABLE xid_old (id int PRIMARY KEY, body text) WITH (autovacuum_enabled = false, autovacuum_freeze_max_age = 100000); +INSERT INTO xid_old SELECT g, repeat('x', 3000) FROM generate_series(1, 20) g; +CREATE TABLE xid_young (id int PRIMARY KEY) WITH (autovacuum_enabled = false); +CREATE TABLE xid_burn (v int); +SQL + # Autovacuum still freezes a table past its freeze age, autovacuum_enabled or not. A held snapshot keeps + # any freeze from moving past it, the way a long transaction does on a real server. + hold_snapshot tablepro_health_xid 120 + pg -d "$DATABASE" -c "DO \$\$ BEGIN FOR i IN 1..120000 LOOP BEGIN INSERT INTO xid_burn VALUES (i); EXCEPTION WHEN OTHERS THEN NULL; END; END LOOP; END \$\$" > /dev/null + + local ages + ages="$(pg -d "$DATABASE" -c "$(query_of "$source" transactionIdAgeQuery)")" + expect "transaction ID age lists a table past its own lower freeze age" "$ages" "public|xid_old|" yes + expect "transaction ID age reads the table's own freeze age" "$ages" "|100000|" yes + expect "transaction ID age skips a table under the server's freeze age" "$ages" "xid_young" no + expect "transaction ID age skips an extension's table past its freeze age" "$ages" "ext_owned" no + expect "transaction ID age skips a table in an extension's schema past its freeze age" "$ages" "ext_schema" no + + terminate tablepro_health_xid + # The fix exactly as PostgreSQLTableHealthProviderTests pins it. + pg -d "$DATABASE" -c 'VACUUM (FREEZE) "public"."xid_old";' > /dev/null + ages="$(pg -d "$DATABASE" -c "$(query_of "$source" transactionIdAgeQuery)")" + expect "the previewed freeze clears its finding" "$ages" "xid_old" no } check_mysql() { @@ -217,7 +653,7 @@ check_mysql() { echo "$(my -e 'SELECT VERSION()') at $HOST:$PORT" # The other database holds a key into this one, so it is dropped first. my -e "DROP DATABASE IF EXISTS $OTHER_DATABASE; DROP DATABASE IF EXISTS $DATABASE; CREATE DATABASE $DATABASE; CREATE DATABASE $OTHER_DATABASE" - trap 'mysql --protocol=TCP -h "$HOST" -P "$PORT" -u "$USER_NAME" -e "DROP DATABASE IF EXISTS '"$OTHER_DATABASE"'; DROP DATABASE IF EXISTS '"$DATABASE"'; UPDATE performance_schema.setup_instruments SET ENABLED = '"'YES'"' WHERE NAME = '"'wait/io/table/sql/handler'"'; DELETE FROM performance_schema.setup_objects WHERE OBJECT_SCHEMA = '"'$DATABASE'"'" > /dev/null 2>&1' EXIT + trap cleanup_mysql EXIT # A table setup_objects leaves out keeps zero-count index rows even after a read through the index. my -e "INSERT INTO performance_schema.setup_objects (OBJECT_TYPE, OBJECT_SCHEMA, OBJECT_NAME, ENABLED, TIMED) VALUES ('TABLE', '$DATABASE', 'uncounted', 'NO', 'NO')" 2> /dev/null @@ -304,6 +740,176 @@ SQL expect "the previewed primary key clears its finding" "$nopk" "odd\`name" no indexes="$(my "$DATABASE" -e "$(query_of "$source" indexQuery)")" expect "the previewed drop clears its finding" "$indexes" "no_pk_a_idx" no + + # MySQL 8 caches information_schema.TABLES for information_schema_stats_expiry seconds. The probe's own + # session reads fresh after its writes; the app never changes it. + local expiry="" + my -e "SELECT @@SESSION.information_schema_stats_expiry" > /dev/null 2>&1 && expiry="SET SESSION information_schema_stats_expiry = 0; " + check_mysql_auto_increment + check_mysql_process_privilege + if [ "${TABLEPRO_PROBE_RECLAIMABLE:-}" = "1" ]; then + check_mysql_reclaimable_space + else + echo "note the reclaimable space case writes about 1 GB; TABLEPRO_PROBE_RECLAIMABLE=1 runs it" + fi +} + +cleanup_mysql() { + local admin=(mysql --protocol=TCP -h "$HOST" -P "$PORT" -u "$USER_NAME" ${tls[@]+"${tls[@]}"}) + "${admin[@]}" -e "DROP DATABASE IF EXISTS $OTHER_DATABASE; DROP DATABASE IF EXISTS $DATABASE; UPDATE performance_schema.setup_instruments SET ENABLED = 'YES' WHERE NAME = 'wait/io/table/sql/handler'; DELETE FROM performance_schema.setup_objects WHERE OBJECT_SCHEMA = '$DATABASE'" > /dev/null 2>&1 + "${admin[@]}" -e "DROP USER IF EXISTS $READER_ROLE@'%', $READER_ROLE@'localhost'" > /dev/null 2>&1 + if [ -n "${FILE_PER_TABLE:-}" ]; then + "${admin[@]}" -e "SET GLOBAL innodb_file_per_table = $FILE_PER_TABLE" > /dev/null 2>&1 + fi +} + +# One counter per integer type and per flavor quirk: unsigned, a non-key counter with a comment, an invisible +# one with a hostile comment, ZEROFILL, a key a foreign key references, and an unsigned bigint past Int64. +check_mysql_auto_increment() { + my "$DATABASE" > /dev/null <<'SQL' || exit 1 +CREATE TABLE ai_tiny_unsigned (id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY); +INSERT INTO ai_tiny_unsigned VALUES (199); +CREATE TABLE ai_int (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY); +INSERT INTO ai_int VALUES (1700000000); +CREATE TABLE ai_int_unsigned (id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY); +INSERT INTO ai_int_unsigned VALUES (1700000000); +CREATE TABLE ai_healthy (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY); +INSERT INTO ai_healthy VALUES (NULL), (NULL); +CREATE TABLE ai_secondary (id INT NOT NULL, seq SMALLINT NOT NULL AUTO_INCREMENT COMMENT 'row counter', PRIMARY KEY (id), UNIQUE KEY (seq)); +INSERT INTO ai_secondary VALUES (1, 30000); +CREATE TABLE ai_zerofill (id INT(5) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT PRIMARY KEY); +INSERT INTO ai_zerofill VALUES (4000000000); +CREATE TABLE ai_parent (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY); +INSERT INTO ai_parent VALUES (1800000000); +CREATE TABLE ai_child (id INT NOT NULL PRIMARY KEY, parent_id INT, CONSTRAINT ai_child_fk FOREIGN KEY (parent_id) REFERENCES ai_parent (id)); +CREATE TABLE ai_bigint_unsigned (id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY); +INSERT INTO ai_bigint_unsigned VALUES (15000000000000000000); +CREATE TABLE ai_bigint_far (id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY); +INSERT INTO ai_bigint_far VALUES (1000000000000000000); +SQL + # INVISIBLE needs MySQL 8.0.23 or MariaDB 10.3.3. + local invisible=no + my "$DATABASE" > /dev/null 2>&1 <<'SQL' && invisible=yes +CREATE TABLE ai_invisible (id INT NOT NULL PRIMARY KEY, seq SMALLINT NOT NULL AUTO_INCREMENT INVISIBLE COMMENT 'it''s a \\ path', UNIQUE KEY (seq)); +INSERT INTO ai_invisible (id, seq) VALUES (1, 30000); +SQL + [ "$invisible" = yes ] || echo "note this server has no invisible columns, so that case is skipped" + + local counters + counters="$(my "$DATABASE" -e "$expiry$(query_of "$source" autoIncrementQuery)" 2>&1)" + expect "AUTO_INCREMENT reads without overflowing next to a large bigint counter" "$counters" "ERROR" no + expect "AUTO_INCREMENT lists a tinyint unsigned at 78%" "$counters" "ai_tiny_unsigned id tinyint " yes + expect "AUTO_INCREMENT lists a non-key counter with its comment" "$counters" "ai_secondary seq smallint " yes + expect "AUTO_INCREMENT reads an unsigned bigint past Int64 exactly" "$counters" "15000000000000000001 18446744073709551615" yes + expect "AUTO_INCREMENT keeps ZEROFILL in the column type" "$counters" "unsigned zerofill" yes + expect "AUTO_INCREMENT skips an unsigned int at 40%" "$counters" "ai_int_unsigned" no + expect "AUTO_INCREMENT skips a bigint at 11%" "$counters" "ai_bigint_far" no + expect "AUTO_INCREMENT skips a fresh counter" "$counters" "ai_healthy" no + if [ "$invisible" = yes ]; then + expect "AUTO_INCREMENT lists an invisible counter" "$counters" "INVISIBLE 30001 32767" yes + fi + + # The fixes exactly as MySQLTableHealthProviderTests pins them. The bash string 'it''s a \\\\ path' + # reaches the server as 'it''s a \\ path', the literal the driver's escaping writes. + my -e "ALTER TABLE \`$DATABASE\`.\`ai_tiny_unsigned\` MODIFY COLUMN \`id\` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT;" + my -e "ALTER TABLE \`$DATABASE\`.\`ai_secondary\` MODIFY COLUMN \`seq\` BIGINT NOT NULL AUTO_INCREMENT COMMENT 'row counter';" + my -e "ALTER TABLE \`$DATABASE\`.\`ai_zerofill\` MODIFY COLUMN \`id\` BIGINT(5) UNSIGNED ZEROFILL NOT NULL AUTO_INCREMENT;" + if [ "$invisible" = yes ]; then + my -e "ALTER TABLE \`$DATABASE\`.\`ai_invisible\` MODIFY COLUMN \`seq\` BIGINT NOT NULL AUTO_INCREMENT INVISIBLE COMMENT 'it''s a \\\\ path';" + fi + local refused + refused="$(my -e "ALTER TABLE \`$DATABASE\`.\`ai_parent\` MODIFY COLUMN \`id\` BIGINT NOT NULL AUTO_INCREMENT;" 2>&1)" + expect_pattern "widening a key a foreign key references is refused, as its detail says" "$refused" "3780|1833" + + counters="$(my "$DATABASE" -e "$expiry$(query_of "$source" autoIncrementQuery)" 2>&1)" + expect "the previewed widening clears an unsigned tinyint counter" "$counters" "ai_tiny_unsigned" no + expect "the previewed widening clears a non-key counter" "$counters" "ai_secondary" no + expect "the previewed widening clears a ZEROFILL counter" "$counters" "ai_zerofill" no + expect "a counter a foreign key holds stays listed" "$counters" "ai_parent" yes + expect "an unsigned bigint counter has no wider type and stays listed" "$counters" "ai_bigint_unsigned" yes + if [ "$invisible" = yes ]; then + expect "the previewed widening clears an invisible counter" "$counters" "ai_invisible" no + fi + + local restated + restated="$(my "$DATABASE" -e "SELECT TABLE_NAME, COLUMN_TYPE, EXTRA, COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '$DATABASE' AND TABLE_NAME IN ('ai_invisible', 'ai_secondary', 'ai_zerofill') AND EXTRA LIKE '%auto_increment%' ORDER BY 1")" + expect "the widened counter keeps its comment" "$restated" "auto_increment row counter" yes + expect "the widened ZEROFILL counter keeps its width" "$restated" "bigint(5) unsigned zerofill" yes + if [ "$invisible" = yes ]; then + expect "the widened counter stays invisible and keeps its hostile comment" "$restated" "INVISIBLE it's a \\\\ path" yes + fi +} + +check_mysql_process_privilege() { + local pools + pools="$(my -e "$(query_of "$source" processPrivilegeQuery)" 2>&1)" + expect_pattern "the PROCESS probe reads the buffer pools" "$pools" "^[1-9][0-9]*$" + + # Also at localhost: an anonymous ''@'localhost', which some installs create, outranks a named user at '%'. + my -e "DROP USER IF EXISTS $READER_ROLE@'%', $READER_ROLE@'localhost'" > /dev/null 2>&1 + if my -e "CREATE USER $READER_ROLE@'%' IDENTIFIED BY 'tablepro_health', $READER_ROLE@'localhost' IDENTIFIED BY 'tablepro_health'; GRANT SELECT ON \`$DATABASE\`.* TO $READER_ROLE@'%', $READER_ROLE@'localhost'" > /dev/null 2>&1; then + local denied + denied="$(MYSQL_PWD=tablepro_health mysql --protocol=TCP -h "$HOST" -P "$PORT" -u "$READER_ROLE" ${tls[@]+"${tls[@]}"} -N -B \ + -e "$(query_of "$source" processPrivilegeQuery)" 2>&1)" + expect "without PROCESS the probe is refused" "$denied" "1227" yes + else + echo "note this user cannot create a user, so the PROCESS refusal is skipped" + fi +} + +# A table in its own file, a partitioned one and a MyISAM one with three quarters deleted, and one in the +# system tablespace, whose free space belongs to every table there. +check_mysql_reclaimable_space() { + local query=reclaimableSpaceQuery + my -e "SELECT VERSION()" | grep -qi mariadb && query=mariaDBReclaimableSpaceQuery + my "$DATABASE" > /dev/null <<'SQL' || exit 1 +CREATE TABLE fx_own (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, pad VARCHAR(1000) NOT NULL) ENGINE = InnoDB; +INSERT INTO fx_own (pad) VALUES (REPEAT('x', 900)); +SQL + for _ in $(seq 1 18); do + my "$DATABASE" -e "INSERT INTO fx_own (pad) SELECT pad FROM fx_own" || exit 1 + done + my "$DATABASE" > /dev/null <<'SQL' || exit 1 +CREATE TABLE fx_parts (id INT NOT NULL PRIMARY KEY, pad VARCHAR(1000) NOT NULL) ENGINE = InnoDB PARTITION BY RANGE (id) (PARTITION p0 VALUES LESS THAN (200000), PARTITION p1 VALUES LESS THAN MAXVALUE); +INSERT INTO fx_parts SELECT * FROM fx_own; +CREATE TABLE fx_myisam (id INT NOT NULL PRIMARY KEY, pad VARCHAR(1000) NOT NULL) ENGINE = MyISAM; +INSERT INTO fx_myisam SELECT * FROM fx_own; +SQL + local system_table="CREATE TABLE fx_sys (id INT NOT NULL PRIMARY KEY, pad VARCHAR(1000) NOT NULL) ENGINE = InnoDB" + if [ "$query" = mariaDBReclaimableSpaceQuery ]; then + FILE_PER_TABLE="$(my -e "SELECT @@GLOBAL.innodb_file_per_table")" + my -e "SET GLOBAL innodb_file_per_table = OFF" + my "$DATABASE" -e "$system_table" + my -e "SET GLOBAL innodb_file_per_table = $FILE_PER_TABLE" + FILE_PER_TABLE="" + else + my "$DATABASE" -e "$system_table TABLESPACE innodb_system" + fi + my "$DATABASE" > /dev/null <<'SQL' || exit 1 +INSERT INTO fx_sys SELECT * FROM fx_own; +DELETE FROM fx_own WHERE id <= 196608; +DELETE FROM fx_parts WHERE id < 200000; +DELETE FROM fx_myisam WHERE id <= 196608; +DELETE FROM fx_sys WHERE id <= 196608; +SQL + for _ in $(seq 1 120); do + my -e "SHOW ENGINE INNODB STATUS" | grep -q "History list length 0" && break + sleep 1 + done + + local frag + frag="$(my "$DATABASE" -e "$expiry$(query_of "$source" "$query")" 2>&1)" + expect "reclaimable space lists a table in its own file" "$frag" "fx_own InnoDB" yes + expect "reclaimable space lists a partitioned table" "$frag" "fx_parts InnoDB" yes + expect "reclaimable space lists a MyISAM table" "$frag" "fx_myisam MyISAM" yes + expect "reclaimable space skips a table in the system tablespace" "$frag" "fx_sys" no + + # The fixes exactly as MySQLTableHealthProviderTests pins them. + for table in fx_own fx_parts fx_myisam; do + my -e "OPTIMIZE TABLE \`$DATABASE\`.\`$table\`;" > /dev/null + done + frag="$(my "$DATABASE" -e "$expiry$(query_of "$source" "$query")" 2>&1)" + expect "the previewed OPTIMIZE clears its findings" "$frag" "fx_" no } case "$ENGINE" in From 4d88006017dfa1e1ec423adfd3f545913b4b55bf Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 9 Nov 2026 22:31:18 +0700 Subject: [PATCH 03/13] fix(table-health): review round 1 findings --- .../MCP/MCPConnectionBridge+TableHealth.swift | 38 +++++- .../Protocol/Tools/GetTableHealthTool.swift | 6 +- .../Storage/TableHealthHistoryStorage.swift | 55 ++++++-- .../PostgreSQLTableHealthProvider.swift | 95 ++++++++++--- .../TableHealthExportDataSource.swift | 7 +- .../TableHealth/TableHealthFixScript.swift | 18 ++- .../TableHealth/TableHealthReportLoader.swift | 2 +- .../TablePro/Resources/Localizable.xcstrings | 24 ++++ .../ViewModels/TableHealthViewModel.swift | 22 ++- ...ainContentCoordinator+RenameAdoption.swift | 30 +++-- .../TableHealth/TableHealthChecksList.swift | 4 + .../TableHealth/TableHealthFindingsPane.swift | 25 +++- .../TableHealthFindingsTable.swift | 6 +- .../TableHealthLayoutMetrics.swift | 20 ++- .../TableHealth/TableHealthPreviewPane.swift | 126 +++++++++++++----- .../Views/TableHealth/TableHealthView.swift | 8 +- .../Tools/GetTableHealthToolTests.swift | 36 +++++ .../TableHealthHistoryStorageTests.swift | 36 ++++- .../PostgreSQLTableHealthProviderTests.swift | 64 ++++++++- .../TableHealthExportDataSourceTests.swift | 8 ++ .../TableHealthFixScriptTests.swift | 23 ++++ ...nSplitViewControllerDetailWidthTests.swift | 5 +- .../TableHealthViewModelTests.swift | 81 ++++++++++- .../MainContentCoordinatorRefreshTests.swift | 55 ++++++++ .../check-postgresql-catalog-shadowing.sh | 3 +- .../probes/check-table-health-catalogs.sh | 46 ++++++- 26 files changed, 729 insertions(+), 114 deletions(-) diff --git a/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift b/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift index 3a6356a246..3eee2000f5 100644 --- a/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift +++ b/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift @@ -4,7 +4,7 @@ import TableProMCP extension MCPConnectionBridge { // Reads the stored history and never records this run, so the tab's New and Resolved stay its own. - func tableHealth(scope: DatabaseScope) async throws -> JsonValue { + func tableHealth(scope: DatabaseScope, timeoutSeconds: Int) async throws -> JsonValue { let databaseType = try await ensureConnected(scope.connectionId) guard let provider = TableHealthProviderFactory.provider(for: databaseType) else { throw MCPToolExecutionError.unsupported( @@ -18,14 +18,40 @@ extension MCPConnectionBridge { } let ledger = try await Self.tableHealthLedger(connectionId: scope.connectionId, database: scope.database) // The checks cover every schema, as the Table Health tab does. - let report = try await TableHealthReportLoader.load( - provider: provider, - scope: DatabaseScope(connectionId: scope.connectionId, database: scope.database, schema: nil), - metadata: DatabaseManager.shared - ) + let reportScope = DatabaseScope(connectionId: scope.connectionId, database: scope.database, schema: nil) + let report = try await Self.withinTableHealthTimeout(seconds: timeoutSeconds) { + try await TableHealthReportLoader.load(provider: provider, scope: reportScope, metadata: DatabaseManager.shared) + } return Self.tableHealthPayload(report, ledger: ledger) } + // The race runRacingTimeout runs for a statement. A metadata read cannot be aborted on the server, so on a timeout + // the checks stop before their next statement and the reply waits only for the one already running. + static func withinTableHealthTimeout( + seconds: Int, + _ body: @Sendable @escaping () async throws -> T + ) async throws -> T { + try await withThrowingTaskGroup(of: T.self) { group in + group.addTask { + try await body() + } + group.addTask { + try await Task.sleep(for: .seconds(seconds)) + throw DatabaseAccessError.timeout( + String( + format: String(localized: "Table Health did not finish within the MCP query timeout of %lld seconds."), + seconds + ) + ) + } + guard let first = try await group.next() else { + throw CancellationError() + } + group.cancelAll() + return first + } + } + private static func tableHealthLedger(connectionId: UUID, database: String) async throws -> TableHealthLedger { do { return try await MainActor.run { diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/GetTableHealthTool.swift b/apple/TablePro/Core/MCP/Protocol/Tools/GetTableHealthTool.swift index ef3beb61cf..03de73f3df 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/GetTableHealthTool.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/GetTableHealthTool.swift @@ -116,7 +116,11 @@ public struct GetTableHealthTool: MCPToolImplementation { ) async throws -> MCPToolCallResult { try MCPArgumentDecoder.rejectUnknownKeys(arguments, allowed: ["connection_id", "database"]) let scope = try await MCPScopeArguments.resolve(arguments, services: services) - let payload = try await services.connectionBridge.tableHealth(scope: scope) + let settings = await services.settingsProvider() + let payload = try await services.connectionBridge.tableHealth( + scope: scope, + timeoutSeconds: settings.validatedQueryTimeoutSeconds + ) return .structured(payload) } } diff --git a/apple/TablePro/Core/Storage/TableHealthHistoryStorage.swift b/apple/TablePro/Core/Storage/TableHealthHistoryStorage.swift index ffd20b45dc..2b370b8215 100644 --- a/apple/TablePro/Core/Storage/TableHealthHistoryStorage.swift +++ b/apple/TablePro/Core/Storage/TableHealthHistoryStorage.swift @@ -1,14 +1,19 @@ import Foundation import os -enum TableHealthHistoryStorageError: LocalizedError { +enum TableHealthHistoryStorageError: LocalizedError, Equatable { case readFailed(String) + case unreadable case saveFailed(String) var errorDescription: String? { switch self { case .readFailed(let reason): return String(format: String(localized: "TablePro could not read the Table Health history: %@"), reason) + case .unreadable: + return String( + localized: "The Table Health history file could not be read. The next refresh starts a new history and keeps the old file." + ) case .saveFailed(let reason): return String(format: String(localized: "TablePro could not save the Table Health history: %@"), reason) } @@ -27,6 +32,11 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet var databases: [String: TableHealthLedger] = [:] } + private struct Loaded { + var document = Document() + var isUnreadable = false + } + private let directory: URL private let notificationCenter: NotificationCenter @@ -97,11 +107,12 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet } guard fromDatabase != toDatabase else { return } do { - var document = try load(connectionId) + let loaded = try read(connectionId) + var document = loaded.document guard let moving = document.databases.removeValue(forKey: fromDatabase) else { return } // The renamed database takes the name, so a ledger left under it describes objects that are gone. document.databases[toDatabase] = moving - try save(document, for: connectionId) + try write(document, over: loaded, for: connectionId) postChange(connectionId: connectionId, databases: [fromDatabase, toDatabase]) } catch { Self.logFailure(error, connectionId: connectionId) @@ -121,9 +132,10 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet return } do { - var document = try load(connectionId) + let loaded = try read(connectionId) + var document = loaded.document guard document.databases.removeValue(forKey: database) != nil else { return } - try save(document, for: connectionId) + try write(document, over: loaded, for: connectionId) postChange(connectionId: connectionId, databases: [database]) } catch { Self.logFailure(error, connectionId: connectionId) @@ -152,12 +164,16 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet database: String, _ change: (TableHealthLedger) -> TableHealthLedger ) throws -> TableHealthLedger { - var document = try load(connectionId) + let loaded = try read(connectionId) + var document = loaded.document let current = document.databases[database] ?? TableHealthLedger() let next = change(current) - guard next != current else { return next } + guard next != current else { + if loaded.isUnreadable { throw TableHealthHistoryStorageError.unreadable } + return next + } document.databases[database] = next - try save(document, for: connectionId) + try write(document, over: loaded, for: connectionId) postChange(connectionId: connectionId, databases: [database]) return next } @@ -174,9 +190,16 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet } } + // A read leaves a file it cannot decode where it is: only a write sets it aside, right before it replaces it. private func load(_ connectionId: UUID) throws -> Document { + let loaded = try read(connectionId) + guard !loaded.isUnreadable else { throw TableHealthHistoryStorageError.unreadable } + return loaded.document + } + + private func read(_ connectionId: UUID) throws -> Loaded { let url = fileURL(for: connectionId) - guard FileManager.default.fileExists(atPath: url.path) else { return Document() } + guard FileManager.default.fileExists(atPath: url.path) else { return Loaded() } let data: Data do { data = try Data(contentsOf: url) @@ -187,16 +210,22 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet throw TableHealthHistoryStorageError.readFailed(error.localizedDescription) } do { - return try JSONDecoder().decode(Document.self, from: data) + return Loaded(document: try JSONDecoder().decode(Document.self, from: data)) } catch { Self.logger.error( "Unreadable Table Health history for \(connectionId, privacy: .public): \(error.publicLogShape, privacy: .public)" ) - try setAside(url, connectionId: connectionId) - return Document() + return Loaded(isUnreadable: true) } } + private func write(_ document: Document, over loaded: Loaded, for connectionId: UUID) throws { + if loaded.isUnreadable { + try setAside(fileURL(for: connectionId), connectionId: connectionId) + } + try save(document, for: connectionId) + } + // A file this build cannot decode may be a newer build's, so it is kept under a free name and never written over. private func setAside(_ url: URL, connectionId: UUID) throws { var number = 1 @@ -209,7 +238,7 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet Self.logger.error( "Failed to set aside unreadable Table Health history for \(connectionId, privacy: .public): \(error.publicLogShape, privacy: .public)" ) - throw TableHealthHistoryStorageError.readFailed(error.localizedDescription) + throw TableHealthHistoryStorageError.saveFailed(error.localizedDescription) } } diff --git a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift index cf71771091..1520b455b7 100644 --- a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift +++ b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift @@ -21,14 +21,18 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { WHERE d.datname = pg_catalog.current_database() """ - // The floor keeps tiny tables out. A pg_ schema is another session's temporary one. + // The floor keeps tiny tables out. A pg_ schema is another session's temporary one. A reset, a crash or a failover + // empties last_vacuum along with the counters, so the reset time says how far back "no vacuum" reaches. static let bloatQuery = """ SELECT s.schemaname AS schema_name, s.relname AS table_name, s.n_live_tup AS live_rows, s.n_dead_tup AS dead_rows, pg_catalog.date_part('epoch', GREATEST(s.last_vacuum, s.last_autovacuum)) AS last_vacuum, - pg_catalog.pg_table_size(c.oid) AS table_bytes + pg_catalog.pg_table_size(c.oid) AS table_bytes, + (SELECT pg_catalog.date_part('epoch', d.stats_reset) + FROM pg_catalog.pg_stat_database d + WHERE d.datname = pg_catalog.current_database()) AS stats_reset FROM pg_catalog.pg_stat_user_tables s JOIN pg_catalog.pg_class c ON c.oid = s.relid WHERE c.relkind = 'r' @@ -53,7 +57,8 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { // A unique or constraint index enforces something no scan counts, an index a foreign key's // checks use is needed the first time the referenced table deletes, and a partition's index // that a parent index owns cannot be dropped alone. The foreign key test is the one - // unindexedForeignKeyQuery uses. + // unindexedForeignKeyQuery uses. An invalid index, a build still running included, is the + // invalid index check's. static let unusedIndexQuery = """ SELECT s.schemaname AS schema_name, s.relname AS table_name, @@ -66,11 +71,11 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { AND t.relkind = 'r' AND pg_catalog.left(s.schemaname, 3) <> 'pg_' AND NOT i.indisunique + AND i.indisvalid AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_constraint c WHERE c.conindid = s.indexrelid) AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_inherits h WHERE h.inhrelid = s.indexrelid) AND NOT ( i.indpred IS NULL - AND i.indisvalid AND EXISTS ( SELECT 1 FROM pg_catalog.pg_constraint fk WHERE fk.contype = 'f' @@ -231,10 +236,51 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { ORDER BY n.nspname, t.relname, ic.relname, p.position """ - // A build still running holds SHARE UPDATE EXCLUSIVE on its table for the whole build, which any role can see in - // pg_locks. An index built ON ONLY a partitioned table (relkind I) stays invalid by design until every partition - // attaches one, so it is not a leftover. + // A build still running holds SHARE UPDATE EXCLUSIVE on its table for the whole build, and from 12 on its backend + // shows in pg_stat_progress_create_index, pid included for any role. Autovacuum and ANALYZE take the same lock, so + // the lock alone would hide a leftover while they run. An index built ON ONLY a partitioned table (relkind I) stays + // invalid by design until every partition attaches one, so it is not a leftover. static let invalidIndexQuery = """ + SELECT n.nspname AS schema_name, + t.relname AS table_name, + c.relname AS index_name, + i.indisunique AS is_unique, + i.indisready AS is_ready, + pg_catalog.pg_relation_size(i.indexrelid) AS index_bytes, + pg_catalog.pg_get_indexdef(i.indexrelid) AS index_definition + FROM pg_catalog.pg_index i + JOIN pg_catalog.pg_class c ON c.oid = i.indexrelid + JOIN pg_catalog.pg_class t ON t.oid = i.indrelid + JOIN pg_catalog.pg_namespace n ON n.oid = t.relnamespace + WHERE NOT i.indisvalid + AND c.relkind = 'i' + AND pg_catalog.left(n.nspname, 3) <> 'pg_' + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_locks l + WHERE l.locktype = 'relation' + AND l.database = (SELECT d.oid FROM pg_catalog.pg_database d WHERE d.datname = pg_catalog.current_database()) + AND l.relation = i.indrelid + AND l.mode = 'ShareUpdateExclusiveLock' + AND l.granted + AND l.pid IN (SELECT p.pid FROM pg_catalog.pg_stat_progress_create_index p) + ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_class'::pg_catalog.regclass + AND x.objid = t.oid + AND x.deptype = 'e' + ) + AND NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_depend x + WHERE x.classid OPERATOR(pg_catalog.=) 'pg_catalog.pg_namespace'::pg_catalog.regclass + AND x.objid = t.relnamespace + AND x.deptype = 'e' + ) + ORDER BY n.nspname, t.relname, c.relname + """ + + // Before 12 there is no progress view, so any holder of the lock hides the table's invalid indexes for a while. + static let invalidIndexBefore12Query = """ SELECT n.nspname AS schema_name, t.relname AS table_name, c.relname AS index_name, @@ -442,8 +488,10 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { """ // Autoanalyze's default rule: more rows modified than 10% of reltuples plus 50. reltuples is -1 before the - // first analyze on 14+. Autovacuum never analyzes a partitioned table, and its activity counters stay empty, so - // a parent counts as never analyzed while pg_stats holds no inherited rows for it. + // first analyze on 14+. A reset, a crash or a failover empties last_analyze with the counters, so a table counts + // as never analyzed only while pg_stats also holds nothing for it. Autovacuum never analyzes a partitioned + // table, and its activity counters stay empty, so a parent counts as never analyzed while pg_stats holds no + // inherited rows for it. static let staleStatisticsQuery = """ SELECT n.nspname AS schema_name, c.relname AS table_name, @@ -455,6 +503,8 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { THEN NOT EXISTS (SELECT 1 FROM pg_catalog.pg_stats st WHERE st.schemaname = n.nspname AND st.tablename = c.relname AND st.inherited) ELSE s.last_analyze IS NULL AND s.last_autoanalyze IS NULL + AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_stats st + WHERE st.schemaname = n.nspname AND st.tablename = c.relname AND NOT st.inherited) END AS never_analyzed, pg_catalog.date_part('epoch', GREATEST(s.last_analyze, s.last_autoanalyze)) AS last_analyze, pg_catalog.pg_table_size(c.oid) AS table_bytes @@ -489,7 +539,9 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_stats st WHERE st.schemaname = n.nspname AND st.tablename = c.relname AND st.inherited)) OR (c.relkind <> 'p' - AND ((s.last_analyze IS NULL AND s.last_autoanalyze IS NULL AND s.n_live_tup > 0) + AND ((s.last_analyze IS NULL AND s.last_autoanalyze IS NULL AND s.n_live_tup > 0 + AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_stats st + WHERE st.schemaname = n.nspname AND st.tablename = c.relname AND NOT st.inherited)) OR s.n_mod_since_analyze::pg_catalog.float8 > th.analyze_threshold)) ) ORDER BY modified_rows DESC, n.nspname, c.relname @@ -517,7 +569,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { )) case .redundantIndex: return TableHealthCheck(kind: kind, summary: String( - localized: "Indexes whose keys lead another index with the same method, predicate and uniqueness; primary key and constraint indexes stay." + localized: "Indexes whose keys lead another with the same method and predicate, a plain one under a unique one included; primary key and constraint indexes stay." )) case .invalidIndex: return TableHealthCheck(kind: kind, summary: String( @@ -529,7 +581,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { )) case .transactionIdAge: return TableHealthCheck(kind: kind, summary: String( - localized: "Tables whose oldest unfrozen row is more than autovacuum_freeze_max_age transactions old (200,000,000 unless set lower)." + localized: "Tables whose oldest unfrozen row is more than autovacuum_freeze_max_age transactions old (200,000,000 by default)." )) case .staleStatistics: return TableHealthCheck(kind: kind, summary: String( @@ -545,7 +597,8 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { case .bloat: return [Self.bloatQuery] case .unusedIndex: return [Self.unusedIndexQuery] case .redundantIndex: return [Self.redundantIndexQuery] - case .invalidIndex: return [Self.invalidIndexQuery] + case .invalidIndex: + return [(statistics.serverVersion ?? 0) >= 120_000 ? Self.invalidIndexQuery : Self.invalidIndexBefore12Query] case .missingPrimaryKey: return [Self.missingPrimaryKeyQuery] case .unindexedForeignKey: return [Self.unindexedForeignKeyQuery] case .nearLimit: return [Self.nearLimitQuery] @@ -630,9 +683,19 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { let lastVacuum = row.date("last_vacuum") let dead = deadRows?.formatted() ?? row["dead_rows"] let live = liveRows?.formatted() ?? row["live_rows"] - let detail = lastVacuum.map { - String(format: String(localized: "%1$@ dead rows, %2$@ live. Last vacuumed %3$@."), dead, live, TableHealthFormatting.date($0)) - } ?? String(format: String(localized: "%1$@ dead rows, %2$@ live. Never vacuumed."), dead, live) + let detail: String + if let lastVacuum { + detail = String( + format: String(localized: "%1$@ dead rows, %2$@ live. Last vacuumed %3$@."), dead, live, TableHealthFormatting.date(lastVacuum) + ) + } else if let reset = row.date("stats_reset") { + detail = String( + format: String(localized: "%1$@ dead rows, %2$@ live. No vacuum recorded since the statistics were reset at %3$@."), + dead, live, TableHealthFormatting.date(reset) + ) + } else { + detail = String(format: String(localized: "%1$@ dead rows, %2$@ live. Never vacuumed."), dead, live) + } var metrics: [TableHealthMetric] = [] if let deadRows { diff --git a/apple/TablePro/Core/TableHealth/TableHealthExportDataSource.swift b/apple/TablePro/Core/TableHealth/TableHealthExportDataSource.swift index de1c76c6ff..ffe30d1665 100644 --- a/apple/TablePro/Core/TableHealth/TableHealthExportDataSource.swift +++ b/apple/TablePro/Core/TableHealth/TableHealthExportDataSource.swift @@ -142,13 +142,18 @@ enum TableHealthColumn: Sendable { } } + // "0 bytes", where the file style spells out "Zero kB" for an empty index. + static func byteSize(_ bytes: Int64) -> String { + bytes.formatted(.byteCount(style: .file, spellsOutZero: false)) + } + func text(for row: TableHealthFindingRow) -> String { switch self { case .status: return row.status.title case .check: return row.checkTitle case .table: return row.tableSortKey case .object: return row.objectSortKey - case .size: return row.finding?.sizeBytes.map { ByteSizeFormatting.string(bytes: $0) } ?? "" + case .size: return row.finding?.sizeBytes.map(Self.byteSize) ?? "" case .detail: return row.detail case .lastSeen: return row.lastSeen.map(TableHealthFormatting.date) ?? "" } diff --git a/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift b/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift index 6384408c76..78dc6aadf5 100644 --- a/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift +++ b/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift @@ -13,10 +13,24 @@ enum TableHealthFixScript { commentLine(String(format: String(localized: "Generated: %@"), date.formatted(.iso8601))), commentLine(String(localized: "Run each statement on its own; some cannot run inside a transaction.")) ].joined(separator: "\n") + // An index both unused and redundant has one drop for two findings; run twice, the second fails. The fix + // runs once, under the comment line of every finding it fixes. + var blocks: [(comments: [String], fix: String)] = [] + var blockForFix: [String: Int] = [:] for finding in findings { - guard let fix = finding.fixSQL, !fix.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty else { continue } + guard let fix = finding.fixSQL else { continue } + let statement = fix.trimmingCharacters(in: .whitespacesAndNewlines) + guard !statement.isEmpty else { continue } let name = String(format: String(localized: "%1$@: %2$@"), checkTitle(finding.kind), finding.qualifiedName) - script += "\n\n" + commentLine(name) + "\n" + fix + if let block = blockForFix[statement] { + blocks[block].comments.append(commentLine(name)) + } else { + blockForFix[statement] = blocks.count + blocks.append((comments: [commentLine(name)], fix: fix)) + } + } + for block in blocks { + script += "\n\n" + block.comments.joined(separator: "\n") + "\n" + block.fix } return script + "\n" } diff --git a/apple/TablePro/Core/TableHealth/TableHealthReportLoader.swift b/apple/TablePro/Core/TableHealth/TableHealthReportLoader.swift index dec57f2ae5..863ec73a83 100644 --- a/apple/TablePro/Core/TableHealth/TableHealthReportLoader.swift +++ b/apple/TablePro/Core/TableHealth/TableHealthReportLoader.swift @@ -4,7 +4,7 @@ enum TableHealthError: LocalizedError, Equatable { case noDatabase var errorDescription: String? { - String(localized: "Select a database in the sidebar to check its tables.") + String(localized: "Close this tab and choose Database > Table Health.") } } diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 71ac2bffef..72d129a513 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -7112,6 +7112,9 @@ }, "%1$@ dead rows, %2$@ live. Never vacuumed." : { + }, + "%1$@ dead rows, %2$@ live. No vacuum recorded since the statistics were reset at %3$@." : { + }, "%1$@ does not apply to a %2$@." : { "localizations" : { @@ -12513,6 +12516,9 @@ }, "^[%lld change](inflect: true)" : { + }, + "^[%lld check](inflect: true) could not run. Select one to see why." : { + }, "^[%lld column](inflect: true)" : { "localizations" : { @@ -42111,6 +42117,9 @@ } } } + }, + "Close this tab and choose Database > Table Health." : { + }, "Close while work is still running?" : { @@ -94821,6 +94830,9 @@ }, "Indexes whose keys lead another index with the same method, predicate and uniqueness; primary key and constraint indexes stay." : { + }, + "Indexes whose keys lead another with the same method and predicate, a plain one under a unique one included; primary key and constraint indexes stay." : { + }, "Indexes with no recorded reads since the server started, leaving out unique, full-text and foreign key indexes." : { @@ -170436,6 +170448,9 @@ "Some Changes Not Uploaded" : { "extractionState" : "manual", "generatesSymbol" : false + }, + "Some Checks Failed" : { + }, "Some columns in this preset don't exist in the current table" : { "localizations" : { @@ -179826,6 +179841,9 @@ }, "Table Health" : { + }, + "Table Health did not finish within the MCP query timeout of %lld seconds." : { + }, "Table Health findings cannot run queries." : { @@ -182865,6 +182883,9 @@ } } } + }, + "Tables whose oldest unfrozen row is more than autovacuum_freeze_max_age transactions old (200,000,000 by default)." : { + }, "Tables whose oldest unfrozen row is more than autovacuum_freeze_max_age transactions old (200,000,000 unless set lower)." : { @@ -194528,6 +194549,9 @@ } } } + }, + "The Table Health history file could not be read. The next refresh starts a new history and keeps the old file." : { + }, "The table is being read again after the save." : { diff --git a/apple/TablePro/ViewModels/TableHealthViewModel.swift b/apple/TablePro/ViewModels/TableHealthViewModel.swift index 5a025d10fe..5ba9f1a1aa 100644 --- a/apple/TablePro/ViewModels/TableHealthViewModel.swift +++ b/apple/TablePro/ViewModels/TableHealthViewModel.swift @@ -15,6 +15,7 @@ enum TableHealthListState: Equatable { case failed(String) case noResults case ignoredHidden(Int) + case someChecksFailed(Int) case noResolved } @@ -77,6 +78,9 @@ final class TableHealthViewModel: ObservableObject { var databaseName: String { database() } + // A tab restored from before tabs were bound to a database; it can only be closed and opened again. + var needsDatabase: Bool { databaseName.isEmpty } + init( connectionId: UUID, provider: (any TableHealthProvider)?, @@ -214,6 +218,11 @@ final class TableHealthViewModel: ObservableObject { } } + // The tab now names another database, after a rename: its history moved with it. + func reloadHistory() { + reloadLedger() + } + private func historyDidChange(_ notification: Notification) { guard let changed = TableHealthHistoryChange.scope(of: notification), changed.connectionId == connectionId, @@ -341,6 +350,10 @@ final class TableHealthViewModel: ObservableObject { return report?.result(for: kind) } + var failedCheckCount: Int { + report?.checks.filter { if case .failed = $0.outcome { return true } else { return false } }.count ?? 0 + } + // A check that failed but kept its earlier findings shows them under this line. var checkFailure: String? { guard let currentCheck, case .failed(let message) = currentCheck.outcome, !currentCheck.findings.isEmpty @@ -374,12 +387,14 @@ final class TableHealthViewModel: ObservableObject { if !filterText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, scopeRowCount > 0 { return .noResults } + if scope == .all, failedCheckCount > 0 { return .someChecksFailed(failedCheckCount) } if hiddenIgnoredCount > 0 { return .ignoredHidden(hiddenIgnoredCount) } switch scope { case .all: return .noFindings(String(localized: "Every check that ran found nothing.")) case .check: - return .noFindings(currentCheck?.check.summary ?? "") + // The summary already heads the pane. + return .noFindings("") case .resolved: return .noResolved } @@ -414,6 +429,11 @@ final class TableHealthViewModel: ObservableObject { // MARK: - Copy and export + // Edit > Copy from the table or the checks list: the selected findings the table shows. + func selectionTSV() -> String? { + tsv(for: selectedRows) + } + func tsv(for rows: [TableHealthFindingRow]) -> String? { guard !rows.isEmpty else { return nil } return TableHealthTSV.text(rows, columns: scope == .resolved ? TableHealthColumn.resolved : TableHealthColumn.findings) diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+RenameAdoption.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+RenameAdoption.swift index fa18aeeba4..41e3314c15 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+RenameAdoption.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+RenameAdoption.swift @@ -5,12 +5,12 @@ import Foundation -/// A window's tabs moved onto an object's or a container's new name. -/// -/// A rename is the one destructive-looking operation whose object survives it, so the tab stays open -/// on the same rows with its filters and column widths applied. What the connection keeps about the -/// object, its favorites, recents, per-table settings and queued operations, moves once in -/// `CatalogEditAdoption`; this is the part every window hosting the connection does for itself. +// A window's tabs moved onto an object's or a container's new name. +// +// A rename is the one destructive-looking operation whose object survives it, so the tab stays open +// on the same rows with its filters and column widths applied. What the connection keeps about the +// object, its favorites, recents, per-table settings and queued operations, moves once in +// `CatalogEditAdoption`; this is the part every window hosting the connection does for itself. extension MainContentCoordinator { func retitleTabs(ids: Set, to newName: String) { guard !ids.isEmpty else { return } @@ -23,19 +23,19 @@ extension MainContentCoordinator { } tabManager.markTabRenamed(tabManager.tabs[index].id) followRename(ofTableMetadataFor: tabManager.tabs[index]) - /// The browse query still names the old table, so the next page, sort or filter would - /// run against a name the server no longer has. + // The browse query still names the old table, so the next page, sort or filter would + // run against a name the server no longer has. rebuildTableQuery(at: index) } - /// One change manager serves the whole window and holds the name its statements target, so - /// a save started after the rename would still write to the old one. It moves only when the - /// tab it is serving is one of the tabs that was renamed. + // One change manager serves the whole window and holds the name its statements target, so + // a save started after the rename would still write to the old one. It moves only when the + // tab it is serving is one of the tabs that was renamed. guard renamedSelectedTab else { return } changeManager.tableName = newName } - /// A container's new name has to reach every tab keyed by its old one, or the next page or save - /// targets something that is gone. + // A container's new name has to reach every tab keyed by its old one, or the next page or save + // targets something that is gone. func retargetTabs(database: String, schema: String?, toDatabase: String, toSchema: String?) { let browseDatabase = browseDatabaseName for index in tabManager.tabs.indices { @@ -48,6 +48,10 @@ extension MainContentCoordinator { } followRename(ofTableMetadataFor: tabManager.tabs[index]) rebuildTableQuery(at: index) + // The history's change notice can land while the tab still names the old database. + if tabManager.tabs[index].tabType == .tableHealth { + tableHealthModels[tabManager.tabs[index].id]?.reloadHistory() + } } } } diff --git a/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift b/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift index c0820b3eb5..96c441dd05 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift @@ -34,6 +34,10 @@ struct TableHealthChecksList: View { Button(String(localized: "Clear Resolved")) { viewModel.clearResolved() } } } + // Without it Edit > Copy falls through to the window, which copies the sidebar's table names. + .onCopyCommand { + TableHealthCopy.providers(viewModel.selectionTSV()) + } } // A List cannot deselect into nothing here: some scope is always shown. diff --git a/apple/TablePro/Views/TableHealth/TableHealthFindingsPane.swift b/apple/TablePro/Views/TableHealth/TableHealthFindingsPane.swift index c7abf6b6a4..413996ffc0 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthFindingsPane.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthFindingsPane.swift @@ -21,8 +21,14 @@ struct TableHealthFindingsPane: View { return summary } + // In All Findings with rows on screen; with none, the empty state says it instead. + private var failedChecks: Int { + guard viewModel.scope == .all, viewModel.listState == .rows else { return 0 } + return viewModel.failedCheckCount + } + private var hasHeader: Bool { - summary != nil || viewModel.checkFailure != nil || !viewModel.notes.isEmpty + summary != nil || viewModel.checkFailure != nil || failedChecks > 0 || !viewModel.notes.isEmpty } private var header: some View { @@ -33,6 +39,13 @@ struct TableHealthFindingsPane: View { if let failure = viewModel.checkFailure { Label(failure, systemImage: "exclamationmark.triangle") } + if failedChecks > 0 { + Label { + Text(Self.failedChecksText(failedChecks)) + } icon: { + Image(systemName: "exclamationmark.triangle") + } + } ForEach(viewModel.notes, id: \.self) { note in Label(note, systemImage: "info.circle") } @@ -86,6 +99,12 @@ struct TableHealthFindingsPane: View { } actions: { Button(String(localized: "Show Ignored")) { viewModel.showIgnored = true } } + case .someChecksFailed(let count): + UnavailableStateView { + Label(String(localized: "Some Checks Failed"), systemImage: "exclamationmark.triangle") + } description: { + Text(Self.failedChecksText(count)) + } case .noResolved: UnavailableStateView( String(localized: "No Resolved Findings"), @@ -94,4 +113,8 @@ struct TableHealthFindingsPane: View { ) } } + + private static func failedChecksText(_ count: Int) -> LocalizedStringKey { + "^[\(count) check](inflect: true) could not run. Select one to see why." + } } diff --git a/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift b/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift index 8e993c786c..568beade74 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift @@ -12,7 +12,7 @@ struct TableHealthFindingsTable: View { TableColumn(TableHealthColumn.status.title, value: \TableHealthFindingRow.statusRank) { row in Text(verbatim: TableHealthColumn.status.text(for: row)) } - .width(min: 44, ideal: 56, max: 90) + .width(min: 40, ideal: 52, max: 60) TableColumn(TableHealthColumn.check.title, value: \TableHealthFindingRow.checkOrder) { row in Text(verbatim: row.checkTitle) .lineLimit(1) @@ -66,7 +66,7 @@ struct TableHealthFindingsTable: View { } } .onCopyCommand { - TableHealthCopy.providers(viewModel.tsv(for: viewModel.selectedRows)) + TableHealthCopy.providers(viewModel.selectionTSV()) } } @@ -178,7 +178,7 @@ struct TableHealthResolvedTable: View { Button(String(localized: "Clear Resolved")) { viewModel.clearResolved() } } .onCopyCommand { - TableHealthCopy.providers(viewModel.tsv(for: viewModel.selectedRows)) + TableHealthCopy.providers(viewModel.selectionTSV()) } } } diff --git a/apple/TablePro/Views/TableHealth/TableHealthLayoutMetrics.swift b/apple/TablePro/Views/TableHealth/TableHealthLayoutMetrics.swift index ee56b668ff..dc3c70c4cf 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthLayoutMetrics.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthLayoutMetrics.swift @@ -1,13 +1,21 @@ import CoreGraphics +// The split items open at their minimums: AutosavingSplitView's preferred fractions set no first layout here +// (measured on macOS 27). A minimum also clamps a saved divider record, so raising one reaches existing windows. internal enum TableHealthLayoutMetrics { - static let checksListMinimumWidth: CGFloat = 180 - static let checksListMaximumWidth: CGFloat = 280 + // The longest check title, "Foreign Keys Without an Index", beside a two-digit count, measured. + static let checksListMinimumWidth: CGFloat = 274 + static let checksListMaximumWidth: CGFloat = 360 static let findingsMinimumWidth: CGFloat = 320 - static let findingsMinimumHeight: CGFloat = 160 - static let previewMinimumHeight: CGFloat = 150 + static let findingsMinimumHeight: CGFloat = 140 + // Leaves the fix SQL about 140pt under the title, detail line and note. + static let previewMinimumHeight: CGFloat = 240 - // The pane bar at its narrowest, with the database name truncated and the filter at 120: wider than the checks - // list and the findings table need side by side, so the bar sets the floor. + // Below this the SQL and the facts beside it stop fitting, and the facts move under the SQL. + static let previewSQLIdealWidth: CGFloat = 280 + static let previewFactsWidth: CGFloat = 220 + + // The pane bar at its narrowest, with the database name truncated and the filter at 120. Narrower than the + // checks list and the findings table side by side: between the two the list collapses. static let tabMinimumWidth: CGFloat = 560 } diff --git a/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift b/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift index 6c98e11012..926e6d6d93 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift @@ -1,10 +1,18 @@ import SwiftUI +// The fix SQL is the pane's main content: the facts sit beside it, or under it in one line each when the pane is +// too narrow, so they never push it out of view. struct TableHealthPreviewPane: View { @ObservedObject var viewModel: TableHealthViewModel let databaseType: DatabaseType let actions: TableHealthActions + private struct Fact: Identifiable { + let id: String + let label: String + let value: String + } + var body: some View { let findings = viewModel.selectedFindings if findings.count == 1, let finding = findings.first { @@ -21,6 +29,7 @@ struct TableHealthPreviewPane: View { private func single(_ finding: TableHealthFinding) -> some View { let fix = TableHealthViewModel.hasFix(finding) ? finding.fixSQL : nil + let facts = Self.facts(of: finding) return VStack(alignment: .leading, spacing: 8) { HStack(spacing: 8) { Text(verbatim: finding.qualifiedName) @@ -42,47 +51,104 @@ struct TableHealthPreviewPane: View { Text(verbatim: finding.detail) .font(.callout) .foregroundStyle(.secondary) - .fixedSize(horizontal: false, vertical: true) - facts(finding) - if let fix { - SQLStatementPreview( - prepared: SQLReviewSheet.build(statements: [fix], databaseType: databaseType), - databaseType: databaseType - ) - .id(finding.id) - previewNote + .lineLimit(2) + .help(finding.detail) + if facts.isEmpty { + fixArea(fix, id: finding.id) } else { - Text("This finding has no SQL fix.") - .font(.callout) - .foregroundStyle(.secondary) - .frame(maxWidth: .infinity, maxHeight: .infinity) + ViewThatFits(in: .horizontal) { + HStack(alignment: .top, spacing: 12) { + fixArea(fix, id: finding.id) + .frame( + minWidth: 0, + idealWidth: TableHealthLayoutMetrics.previewSQLIdealWidth, + maxWidth: .infinity, + maxHeight: .infinity + ) + ScrollView { + factsColumn(facts) + } + .frame(width: TableHealthLayoutMetrics.previewFactsWidth) + } + VStack(alignment: .leading, spacing: 8) { + fixArea(fix, id: finding.id) + .frame(maxWidth: .infinity, maxHeight: .infinity) + factsGrid(facts) + } + } + } + if fix != nil { + previewNote } } .padding(12) } @ViewBuilder - private func facts(_ finding: TableHealthFinding) -> some View { - if finding.sizeBytes != nil || !finding.metrics.isEmpty { - Grid(alignment: .leadingFirstTextBaseline, horizontalSpacing: 12, verticalSpacing: 4) { - if let size = finding.sizeBytes { - GridRow { - Text(verbatim: TableHealthColumn.size.title) - .foregroundStyle(.secondary) - Text(verbatim: ByteSizeFormatting.string(bytes: size)) - } + private func fixArea(_ fix: String?, id: TableHealthFindingID) -> some View { + if let fix { + SQLStatementPreview( + prepared: SQLReviewSheet.build(statements: [fix], databaseType: databaseType), + databaseType: databaseType + ) + .id(id) + } else { + Text("This finding has no SQL fix.") + .font(.callout) + .foregroundStyle(.secondary) + .frame(maxWidth: .infinity, maxHeight: .infinity) + } + } + + // Each label over its value, which wraps: a definition is longer than the column. + private func factsColumn(_ facts: [Fact]) -> some View { + VStack(alignment: .leading, spacing: 6) { + ForEach(facts) { fact in + VStack(alignment: .leading, spacing: 1) { + Text(verbatim: fact.label) + .font(.caption) + .foregroundStyle(.secondary) + Text(verbatim: fact.value) + .font(.callout) + .fixedSize(horizontal: false, vertical: true) } - ForEach(finding.metrics, id: \.id) { metric in - GridRow { - Text(verbatim: metric.label) - .foregroundStyle(.secondary) - Text(verbatim: metric.formattedValue) - } + } + } + .frame(maxWidth: .infinity, alignment: .leading) + .textSelection(.enabled) + } + + private func factsGrid(_ facts: [Fact]) -> some View { + Grid(alignment: .leadingFirstTextBaseline, horizontalSpacing: 12, verticalSpacing: 2) { + ForEach(facts) { fact in + GridRow { + Text(verbatim: fact.label) + .foregroundStyle(.secondary) + Text(verbatim: fact.value) + .lineLimit(1) + .truncationMode(.middle) + .help(fact.value) } } - .font(.callout) - .textSelection(.enabled) } + .font(.callout) + } + + private static func facts(of finding: TableHealthFinding) -> [Fact] { + var facts: [Fact] = [] + if let size = finding.sizeBytes { + facts.append(Fact(id: "size_bytes", label: TableHealthColumn.size.title, value: TableHealthColumn.byteSize(size))) + } + for metric in finding.metrics { + let value: String + if case .bytes(let bytes) = metric.value { + value = TableHealthColumn.byteSize(bytes) + } else { + value = metric.formattedValue + } + facts.append(Fact(id: metric.id, label: metric.label, value: value)) + } + return facts } private func several(_ findings: [TableHealthFinding]) -> some View { diff --git a/apple/TablePro/Views/TableHealth/TableHealthView.swift b/apple/TablePro/Views/TableHealth/TableHealthView.swift index 3f563a4973..29812a27b9 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthView.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthView.swift @@ -17,7 +17,7 @@ struct TableHealthView: View { // A VStack, not safeAreaInset: the inset does not reach a List hosted inside the split view. var body: some View { VStack(spacing: 0) { - if viewModel.isSupported { + if viewModel.isSupported, !viewModel.needsDatabase { VStack(spacing: 0) { TableHealthPaneBar( viewModel: viewModel, @@ -43,6 +43,12 @@ struct TableHealthView: View { systemImage: "stethoscope", description: Text("Table health is available for PostgreSQL, MySQL and MariaDB.") ) + } else if viewModel.needsDatabase { + UnavailableStateView( + String(localized: "No Database Selected"), + systemImage: "cylinder", + description: Text(verbatim: TableHealthError.noDatabase.errorDescription ?? "") + ) } else if viewModel.report != nil { AutosavingSplitView( autosaveName: "com.TablePro.tableHealth.checksFindings", diff --git a/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift b/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift index 7338c0ceef..4b2661b1b3 100644 --- a/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift +++ b/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift @@ -1,6 +1,7 @@ import Foundation @testable import TablePro import TableProMCP +import TableProTestSupport import Testing struct GetTableHealthToolTests { @@ -200,6 +201,41 @@ struct GetTableHealthToolTests { #expect(required(metricSchema) == keys(metric)) } + @Test("Checks that outlast the MCP query timeout fail as a timeout, once the statement in flight returns") + func timeoutStopsTheChecks() async { + let inFlight = AsyncGate() + let cancelled = AsyncGate() + let call = Task { + try await MCPConnectionBridge.withinTableHealthTimeout(seconds: 0) { + try await withTaskCancellationHandler { + await inFlight.wait() + try Task.checkCancellation() + return 1 + } onCancel: { + Task { await cancelled.open() } + } + } + } + + await cancelled.wait() + await inFlight.open() + let outcome = await call.result + + var thrown: (any Error)? + if case .failure(let error) = outcome { + thrown = error + } + let mapped = (thrown as? DatabaseAccessError).map { MCPToolExecutionError.from($0) } + #expect(mapped?.code == .timeout) + #expect(mapped?.message == "Table Health did not finish within the MCP query timeout of 0 seconds.") + } + + @Test("Checks that finish within the MCP query timeout return their report") + func checksWithinTheTimeoutReturn() async throws { + let value = try await MCPConnectionBridge.withinTableHealthTimeout(seconds: 60) { 7 } + #expect(value == 7) + } + @Test("get_table_health is a registered read-only tool taking a connection and a database") func toolIsReadOnlyAndRegistered() { #expect(GetTableHealthTool.name == "get_table_health") diff --git a/apple/TableProTests/Core/Storage/TableHealthHistoryStorageTests.swift b/apple/TableProTests/Core/Storage/TableHealthHistoryStorageTests.swift index 0ecdfe0fb0..8f5509b584 100644 --- a/apple/TableProTests/Core/Storage/TableHealthHistoryStorageTests.swift +++ b/apple/TableProTests/Core/Storage/TableHealthHistoryStorageTests.swift @@ -172,21 +172,43 @@ struct TableHealthHistoryStorageTests { #expect(reread.firstSeen(orders.id) == Self.day(1)) } - @Test("An unreadable file is set aside under a free name and never written over") - func unreadableFileIsSetAside() throws { + @Test("A read, or a change that writes nothing, reports an unreadable file and leaves it where it is") + func readLeavesUnreadableFile() throws { try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) - try Data("older copy".utf8).write(to: corruptFileURL(1)) try Data("{ not json".utf8).write(to: fileURL) let store = makeStore() + var readError: (any Error)? + var clearError: (any Error)? - #expect(try store.ledger(connectionId: connectionId, database: "shop") == TableHealthLedger()) - #expect(try String(contentsOf: corruptFileURL(1), encoding: .utf8) == "older copy") - #expect(try String(contentsOf: corruptFileURL(2), encoding: .utf8) == "{ not json") + do { + _ = try store.ledger(connectionId: connectionId, database: "shop") + } catch { + readError = error + } + do { + _ = try store.clearResolved(connectionId: connectionId, database: "shop") + } catch { + clearError = error + } + + #expect(readError as? TableHealthHistoryStorageError == .unreadable) + #expect(clearError as? TableHealthHistoryStorageError == .unreadable) + #expect(try String(contentsOf: fileURL, encoding: .utf8) == "{ not json") + #expect(!FileManager.default.fileExists(atPath: corruptFileURL(1).path)) + } + + @Test("A write sets an unreadable file aside under a free name before it replaces it") + func writeSetsUnreadableFileAside() throws { + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + try Data("older copy".utf8).write(to: corruptFileURL(1)) + try Data("{ not json".utf8).write(to: fileURL) + let store = makeStore() _ = try store.record(report([orders]), connectionId: connectionId, database: "shop", at: Self.day(1)) - #expect(try makeStore().ledger(connectionId: connectionId, database: "shop").firstSeen(orders.id) == Self.day(1)) + #expect(try String(contentsOf: corruptFileURL(1), encoding: .utf8) == "older copy") #expect(try String(contentsOf: corruptFileURL(2), encoding: .utf8) == "{ not json") + #expect(try makeStore().ledger(connectionId: connectionId, database: "shop").firstSeen(orders.id) == Self.day(1)) } @Test("A write that cannot land throws and posts nothing") diff --git a/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift b/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift index 98b0579f6f..c9685b1b29 100644 --- a/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift +++ b/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift @@ -354,10 +354,22 @@ struct PostgreSQLTableHealthProviderTests { } // TimescaleDB's chunk tables are not extension members, but their schema is. + // The queries a check runs on a server too old for some of them, on a current one, and on one whose version did not read. + private func everyServerQuery(for check: TableHealthCheckKind) -> [String] { + let servers = [TableHealthStatistics(), statistics(version: "110000"), statistics(version: "170011")] + var queries: [String] = [] + for server in servers { + for query in provider.queries(for: check, statistics: server) where !queries.contains(query) { + queries.append(query) + } + } + return queries + } + @Test("Every check query leaves out extension members, by relation and by schema") func everyQueryLeavesOutExtensions() { for check in provider.checks { - for query in provider.queries(for: check, statistics: TableHealthStatistics()) { + for query in everyServerQuery(for: check) { #expect(query.contains("'pg_catalog.pg_class'::pg_catalog.regclass"), "\(check.rawValue) keeps extension relations") #expect(query.contains("'pg_catalog.pg_namespace'::pg_catalog.regclass"), "\(check.rawValue) keeps extension schemas") #expect(query.contains("deptype = 'e'"), "\(check.rawValue) keeps extension members") @@ -367,8 +379,7 @@ struct PostgreSQLTableHealthProviderTests { @Test("Every query the report runs only reads") func queriesOnlyRead() { - let queries = provider.statisticsQueries - + provider.checks.flatMap { provider.queries(for: $0, statistics: TableHealthStatistics()) } + let queries = provider.statisticsQueries + provider.checks.flatMap { everyServerQuery(for: $0) } for query in queries { let text = query.trimmingCharacters(in: .whitespacesAndNewlines) #expect(text.uppercased().hasPrefix("SELECT"), "\(text.prefix(40)) does not start with SELECT") @@ -643,7 +654,7 @@ struct PostgreSQLTableHealthProviderTests { @Test("The wraparound summary states the freeze age it compares against") func transactionIdAgeSummary() { #expect(provider.check(.transactionIdAge).summary.contains("autovacuum_freeze_max_age")) - #expect(provider.check(.transactionIdAge).summary.contains("200,000,000")) + #expect(provider.check(.transactionIdAge).summary.contains("200,000,000 by default")) #expect(provider.check(.staleStatistics).summary.contains("10%")) #expect(provider.check(.staleStatistics).summary.contains("50")) } @@ -691,4 +702,49 @@ struct PostgreSQLTableHealthProviderTests { #expect(found[1].metric("modified_rows") == nil) #expect(found[1].fixSQL == #"ANALYZE "public"."st_part";"#) } + + @Test("The redundant index summary says a plain index under a unique one counts, as the rule flags it") + func redundantSummaryNamesUniqueCover() { + #expect(provider.check(.redundantIndex).summary.contains("a plain one under a unique one")) + } + + @Test("A bloated table with no vacuum since the statistics were reset says so, not that it was never vacuumed") + func noVacuumSinceReset() throws { + let reset = Date(timeIntervalSince1970: 1_800_000_000) + let finding = try #require(findings( + .bloat, + columns: ["schema_name", "table_name", "live_rows", "dead_rows", "last_vacuum", "table_bytes", "stats_reset"], + rows: [["public", "orders", "2000", "3000", nil, "81920", "1800000000"]] + ).first) + #expect(finding.detail.contains(TableHealthFormatting.date(reset))) + #expect(!finding.detail.contains("Never vacuumed")) + #expect(finding.metric("last_vacuum") == nil) + } + + @Test("A table whose counters were emptied but still has statistics is stale by its modified rows, not never analyzed") + func emptiedCountersAreNotNeverAnalyzed() throws { + let finding = try #require(findings( + .staleStatistics, + columns: Self.staleColumns, + rows: [["public", "st_reset", "r", "5000", "1000", "550", "f", nil, "245760"]] + ).first) + #expect(finding.detail == String( + format: String(localized: "%1$@ rows modified since it was last analyzed, more than the %2$@ that start an autoanalyze."), + (1_000 as Int64).formatted(), (550 as Int64).formatted() + )) + #expect(finding.metric("never_analyzed")?.value == .flag(false)) + #expect(finding.metric("last_analyze") == nil) + } + + @Test("From PostgreSQL 12 only an index build hides its table's invalid indexes; before, or unread, any holder of its lock does") + func invalidIndexQueryByVersion() { + for version in ["120000", "170011"] { + #expect(provider.queries(for: .invalidIndex, statistics: statistics(version: version)) + == [PostgreSQLTableHealthProvider.invalidIndexQuery]) + } + #expect(provider.queries(for: .invalidIndex, statistics: statistics(version: "110000")) + == [PostgreSQLTableHealthProvider.invalidIndexBefore12Query]) + #expect(provider.queries(for: .invalidIndex, statistics: TableHealthStatistics()) + == [PostgreSQLTableHealthProvider.invalidIndexBefore12Query]) + } } diff --git a/apple/TableProTests/Core/TableHealth/TableHealthExportDataSourceTests.swift b/apple/TableProTests/Core/TableHealth/TableHealthExportDataSourceTests.swift index 0541d142ab..41bc359e1b 100644 --- a/apple/TableProTests/Core/TableHealth/TableHealthExportDataSourceTests.swift +++ b/apple/TableProTests/Core/TableHealth/TableHealthExportDataSourceTests.swift @@ -160,6 +160,14 @@ struct TableHealthExportDataSourceTests { } } + @Test("An empty object's size reads as a number, not spelled out") + func zeroSizeIsANumber() { + let zero = TableHealthColumn.byteSize(0) + + #expect(zero.contains("0")) + #expect(zero != Int64(0).formatted(.byteCount(style: .file))) + } + @Test("Copy keeps one line per row when a field holds tabs or line breaks") func copyFlattensFields() { let finding = TableHealthFinding( diff --git a/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift b/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift index d2e40fd734..e9903cc301 100644 --- a/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift +++ b/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift @@ -38,6 +38,29 @@ struct TableHealthFixScriptTests { #expect(text == expected) } + @Test("A fix two findings share runs once, under both findings' comment lines, where the first stood") + func sharedFixRunsOnce() { + let drop = #"DROP INDEX CONCURRENTLY "public"."orders_a_idx";"# + let text = script([ + TableHealthFixture.finding(.unusedIndex, schema: "public", table: "orders", object: "orders_a_idx", fixSQL: drop), + TableHealthFixture.finding(schema: "public", table: "logs", + fixSQL: #"ALTER TABLE "public"."logs" ADD PRIMARY KEY (column_name);"#), + TableHealthFixture.finding(.redundantIndex, schema: "public", table: "orders", object: "orders_a_idx", + fixSQL: drop + "\n") + ]) + let body = lines(text).dropFirst(4) + #expect(Array(body) == [ + Substring("-- " + String(format: String(localized: "%1$@: %2$@"), TableHealthCheckKind.unusedIndex.title, "public.orders.orders_a_idx")), + Substring("-- " + String(format: String(localized: "%1$@: %2$@"), TableHealthCheckKind.redundantIndex.title, "public.orders.orders_a_idx")), + Substring(drop), + "", + Substring("-- " + String(format: String(localized: "%1$@: %2$@"), TableHealthCheckKind.missingPrimaryKey.title, "public.logs")), + #"ALTER TABLE "public"."logs" ADD PRIMARY KEY (column_name);"#, + "" + ]) + #expect(text.components(separatedBy: drop).count == 2) + } + @Test("A finding without a fix is left out") func findingWithoutFixSkipped() { let text = script([ diff --git a/apple/TableProTests/Services/MainSplitViewControllerDetailWidthTests.swift b/apple/TableProTests/Services/MainSplitViewControllerDetailWidthTests.swift index 18b187b76d..3e29154f16 100644 --- a/apple/TableProTests/Services/MainSplitViewControllerDetailWidthTests.swift +++ b/apple/TableProTests/Services/MainSplitViewControllerDetailWidthTests.swift @@ -43,11 +43,12 @@ struct MainSplitViewControllerDetailWidthTests { #expect(resolved == 560) } - @Test("Table Health declares the width its pane bar needs, which holds the checks list beside the findings") + @Test("Table Health declares the width its pane bar needs, which holds the findings once the checks list collapses") func tableHealthDeclaresItsOwnMinimum() { let resolved = MainSplitViewController.resolveDetailMinimumThickness(for: .tableHealth, contentMode: .browse) #expect(resolved == TableHealthLayoutMetrics.tabMinimumWidth) - #expect(resolved >= TableHealthLayoutMetrics.checksListMinimumWidth + TableHealthLayoutMetrics.findingsMinimumWidth) + #expect(resolved >= TableHealthLayoutMetrics.findingsMinimumWidth) + #expect(TableHealthLayoutMetrics.checksListMinimumWidth < TableHealthLayoutMetrics.checksListMaximumWidth) } @Test( diff --git a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift index 1dc993f109..fa32a23f92 100644 --- a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift +++ b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift @@ -306,15 +306,18 @@ struct TableHealthViewModelTests { #expect(!viewModel.isRefreshing) } - @Test("A tab with no database asks for one and reads nothing") + @Test("A tab with no database says to open Table Health again, and reads nothing") func noDatabaseAsksForOne() async { let metadata = HealthMetadata() let viewModel = makeViewModel(metadata, database: BoundDatabase("")) await viewModel.refresh() + #expect(viewModel.needsDatabase) #expect(viewModel.loadError == TableHealthError.noDatabase.errorDescription) + #expect(TableHealthError.noDatabase.errorDescription == "Close this tab and choose Database > Table Health.") #expect(metadata.leases.isEmpty) + #expect(!makeViewModel(metadata).needsDatabase) } @Test("A lost connection says so once, and a later refresh clears it") @@ -588,4 +591,80 @@ struct TableHealthViewModelTests { #expect(script.hasPrefix("-- ")) #expect(findings.allSatisfy { finding in finding.fixSQL.map { script.contains($0) } ?? false }) } + + @Test("All Findings says that checks failed when they did and nothing else was found") + func allFindingsSaysChecksFailed() async { + let metadata = HealthMetadata() + metadata.server.failsMissingKeyCheck = true + let viewModel = makeViewModel(metadata) + + await viewModel.refresh() + + #expect(viewModel.rows.isEmpty) + #expect(viewModel.failedCheckCount == 1) + #expect(viewModel.listState == .someChecksFailed(1)) + } + + @Test("All Findings keeps the rows it has when a check fails, and counts the failure") + func allFindingsKeepsRowsUnderAFailure() async { + let metadata = HealthMetadata() + let viewModel = makeViewModel(metadata) + await viewModel.refresh() + + metadata.server.failsMissingKeyCheck = true + await viewModel.refresh() + + #expect(viewModel.listState == .rows) + #expect(viewModel.failedCheckCount == 1) + } + + @Test("A check that found nothing leaves its description to the pane's header") + func cleanCheckStatesItsSummaryOnce() async { + let metadata = HealthMetadata() + metadata.server.tablesWithoutKey = [] + let viewModel = makeViewModel(metadata) + await viewModel.refresh() + + viewModel.scope = .check(.missingPrimaryKey) + + #expect(viewModel.listState == .noFindings("")) + #expect(viewModel.currentCheck?.check.summary.isEmpty == false) + } + + @Test("Copy writes the selected findings the table shows, whichever list has focus") + func selectionCopyUsesShownRows() async throws { + let metadata = HealthMetadata() + metadata.server.tablesWithoutKey = ["logs", "events"] + let viewModel = makeViewModel(metadata) + await viewModel.refresh() + let logs = try #require(row(viewModel, table: "logs")) + let events = try #require(row(viewModel, table: "events")) + viewModel.selection = [logs.id, events.id] + + viewModel.filterText = "events" + let text = try #require(viewModel.selectionTSV()) + + let lines = text.components(separatedBy: "\n") + #expect(lines.count == 2) + #expect(lines[1].contains("public.events")) + + viewModel.selection = [] + #expect(viewModel.selectionTSV() == nil) + } + + @Test("A history reload reads the database the tab names now") + func reloadHistoryFollowsTheTab() async throws { + let metadata = HealthMetadata() + let history = InMemoryHistory() + let database = BoundDatabase("shop") + let viewModel = makeViewModel(metadata, history: history, database: database) + await viewModel.refresh() + let logs = try #require(row(viewModel, table: "logs")) + history.ledgers["shop_v2"] = (history.ledgers["shop"] ?? TableHealthLedger()).ignoring([logs.id], at: Date()) + + database.name = "shop_v2" + viewModel.reloadHistory() + + #expect(row(viewModel, table: "logs")?.status == .ignored) + } } diff --git a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift index 72a56ca92b..f73a6ceb1c 100644 --- a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift +++ b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift @@ -431,6 +431,27 @@ struct MainContentCoordinatorRefreshTests { #expect(coordinator.tableHealthModels[tabId] == nil) } + @Test("A database rename reloads a Table Health tab's history from the new name") + func renameReloadsTableHealthHistory() throws { + let (coordinator, tabManager) = makeCoordinator() + let tabId = try #require(tabManager.addTableHealthTab(databaseName: "shop")) + let logs = TableHealthFindingID(kind: .missingPrimaryKey, schema: "public", table: "logs", object: nil) + let history = RenamedHistory(database: "shop_v2", ledger: TableHealthLedger().ignoring([logs], at: Date())) + let model = TableHealthViewModel( + connectionId: coordinator.connectionId, + provider: TableHealthProviderFactory.provider(for: .postgresql), + history: history, + metadata: RefreshLeaseRecorder(), + database: { tabManager.tabs.first { $0.id == tabId }?.tableContext.databaseName ?? "" } + ) + coordinator.tableHealthModels[tabId] = model + + coordinator.retargetTabs(database: "shop", schema: nil, toDatabase: "shop_v2", toSchema: nil) + + #expect(model.databaseName == "shop_v2") + #expect(model.ledger.isIgnored(logs)) + } + @Test("Refresh on Users & Roles reloads the principals through the tab's own handler") func refreshOnUsersRolesReloads() { let (coordinator, tabManager) = makeCoordinator() @@ -490,3 +511,37 @@ private final class RefreshUnusedHistory: TableHealthHistoryStoring { TableHealthLedger() } } + +// Holds one database's ledger, the way the store holds a ledger moved by a database rename. +@MainActor +private final class RenamedHistory: TableHealthHistoryStoring { + private let database: String + private let stored: TableHealthLedger + + init(database: String, ledger: TableHealthLedger) { + self.database = database + stored = ledger + } + + func ledger(connectionId: UUID, database: String) throws -> TableHealthLedger { + database == self.database ? stored : TableHealthLedger() + } + + func record(_ report: TableHealthReport, connectionId: UUID, database: String, at date: Date) throws -> TableHealthLedger { + try ledger(connectionId: connectionId, database: database) + } + + func setIgnored( + _ ids: Set, + ignored: Bool, + connectionId: UUID, + database: String, + at date: Date + ) throws -> TableHealthLedger { + try ledger(connectionId: connectionId, database: database) + } + + func clearResolved(connectionId: UUID, database: String) throws -> TableHealthLedger { + try ledger(connectionId: connectionId, database: database) + } +} diff --git a/apple/scripts/probes/check-postgresql-catalog-shadowing.sh b/apple/scripts/probes/check-postgresql-catalog-shadowing.sh index 4553423db9..0c451f348e 100755 --- a/apple/scripts/probes/check-postgresql-catalog-shadowing.sh +++ b/apple/scripts/probes/check-postgresql-catalog-shadowing.sh @@ -168,7 +168,8 @@ static_query() { } HEALTH="$ROOT/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift" for name in statisticsQuery bloatQuery unusedIndexQuery missingPrimaryKeyQuery unindexedForeignKeyQuery \ - redundantIndexQuery invalidIndexQuery nearLimitQuery transactionIdAgeQuery staleStatisticsQuery; do + redundantIndexQuery invalidIndexQuery invalidIndexBefore12Query nearLimitQuery transactionIdAgeQuery \ + staleStatisticsQuery; do static_query "$HEALTH" "$name" > "$HOST_SQL/health-$name.sql" [[ -s "$HOST_SQL/health-$name.sql" ]] || { echo "FAIL: $name not found in $HEALTH" >&2; exit 2; } done diff --git a/apple/scripts/probes/check-table-health-catalogs.sh b/apple/scripts/probes/check-table-health-catalogs.sh index 120d5f2aa3..167e0a1d51 100755 --- a/apple/scripts/probes/check-table-health-catalogs.sh +++ b/apple/scripts/probes/check-table-health-catalogs.sh @@ -290,7 +290,7 @@ INSERT INTO ext_schema.chunk SELECT g FROM generate_series(1, 100) g; SQL local query output for query in bloatQuery unusedIndexQuery redundantIndexQuery missingPrimaryKeyQuery unindexedForeignKeyQuery \ - invalidIndexQuery staleStatisticsQuery transactionIdAgeQuery; do + invalidIndexQuery invalidIndexBefore12Query staleStatisticsQuery transactionIdAgeQuery; do output="$(pg -d "$DATABASE" -c "$(query_of "$source" "$query")")" expect "$query skips an extension's table" "$output" "ext_owned" no expect "$query skips a table in an extension's schema" "$output" "ext_schema" no @@ -422,6 +422,9 @@ SQL } check_postgres_invalid_indexes() { + # The provider picks its literal by version: only 12+ has pg_stat_progress_create_index. + local query=invalidIndexBefore12Query + [ "$version" -ge 120000 ] && query=invalidIndexQuery pg -d "$DATABASE" > /dev/null <<'SQL' CREATE TABLE inv (id int PRIMARY KEY, v int, w int); INSERT INTO inv SELECT g, g % 10, g FROM generate_series(1, 1000) g; @@ -452,11 +455,14 @@ SQL sleep 0.25 done local invalid - invalid="$(pg -d "$DATABASE" -c "$(query_of "$source" invalidIndexQuery)")" + invalid="$(pg -d "$DATABASE" -c "$(query_of "$source" "$query")")" expect "invalid indexes leave out a table with a build running" "$invalid" "|inv|" no + local unused + unused="$(pg -d "$DATABASE" -c "$(query_of "$source" unusedIndexQuery)")" + expect "unused indexes skip an index still being built" "$unused" "inv_building" no wait "$builder" - invalid="$(pg -d "$DATABASE" -c "$(query_of "$source" invalidIndexQuery)")" + invalid="$(pg -d "$DATABASE" -c "$(query_of "$source" "$query")")" expect "invalid indexes list a failed unique build, never ready" "$invalid" "public|inv|inv_v_uniq|t|f|" yes expect "invalid indexes list a cancelled build, still updated by writes" "$invalid" "public|inv|inv_vw_cancelled|f|t|" yes expect "invalid indexes skip a build that finished" "$invalid" "inv_building" no @@ -469,6 +475,25 @@ SQL local shape shape="$(pg -d "$DATABASE" -c "$(query_of "$source" redundantIndexQuery)")" expect "index shape skips an invalid index" "$shape" "inv_vw_cancelled" no + unused="$(pg -d "$DATABASE" -c "$(query_of "$source" unusedIndexQuery)")" + expect "unused indexes skip an invalid index, which the invalid index check owns" "$unused" "inv_vw_cancelled" no + + # Autovacuum and ANALYZE take the lock a build holds. From 12 on, only a build hides its table's leftovers. + if [ "$version" -ge 120000 ]; then + PGAPPNAME=tablepro_health_lock psql -X -q -h "$HOST" -p "$PORT" -U "$USER_NAME" -d "$DATABASE" \ + -c "BEGIN; LOCK TABLE inv IN SHARE UPDATE EXCLUSIVE MODE; SELECT pg_sleep(30); COMMIT;" > /dev/null 2>&1 & + local locker=$! + for _ in $(seq 1 20); do + [ "$(pg -d "$DATABASE" -c "SELECT count(*) FROM pg_locks WHERE relation = 'inv'::regclass AND mode = 'ShareUpdateExclusiveLock' AND granted")" = "1" ] && break + sleep 0.25 + done + invalid="$(pg -d "$DATABASE" -c "$(query_of "$source" invalidIndexQuery)")" + expect "invalid indexes stay listed while something other than a build holds the table's lock" "$invalid" "public|inv|inv_vw_cancelled|" yes + invalid="$(pg -d "$DATABASE" -c "$(query_of "$source" invalidIndexBefore12Query)")" + expect "before 12 the same lock hides them, which is why that query is kept for old servers only" "$invalid" "|inv|" no + terminate tablepro_health_lock + wait "$locker" 2> /dev/null + fi # The fixes exactly as PostgreSQLTableHealthProviderTests pins them. if [ "$version" -ge 120000 ]; then @@ -480,7 +505,7 @@ SQL pg -d "$DATABASE" -c 'DROP INDEX CONCURRENTLY "public"."inv_vw_cancelled";' > /dev/null pg -d "$DATABASE" -c 'CREATE INDEX CONCURRENTLY inv_vw_cancelled ON public.inv USING btree (v, w);' > /dev/null fi - invalid="$(pg -d "$DATABASE" -c "$(query_of "$source" invalidIndexQuery)")" + invalid="$(pg -d "$DATABASE" -c "$(query_of "$source" "$query")")" expect "the previewed rebuild clears its finding" "$invalid" "|inv_vw_cancelled|" no expect "the previewed drop of a leftover copy clears its finding" "$invalid" "|inv_w_idx_ccnew|" no } @@ -611,6 +636,19 @@ SQL sleep 1 done expect "the previewed analyze clears its findings" "$stale" "|st_" no + + # A reset, a crash or a failover empties last_analyze with the counters; pg_stats still says the table was analyzed. + if pg -d "$DATABASE" -c "SELECT pg_catalog.pg_stat_reset()" > /dev/null 2>&1; then + pg -d "$DATABASE" -c "INSERT INTO st_stale SELECT g, g FROM generate_series(5001, 5100) g" > /dev/null + for _ in $(seq 1 10); do + [ "$(pg -d "$DATABASE" -c "SELECT n_live_tup > 0 FROM pg_stat_user_tables WHERE relname = 'st_stale'")" = "t" ] && break + sleep 1 + done + stale="$(pg -d "$DATABASE" -c "$(query_of "$source" staleStatisticsQuery)")" + expect "stale statistics trust pg_stats over counters a reset emptied" "$stale" "|st_stale|" no + else + echo "note this user cannot call pg_stat_reset(), so the emptied counters case is skipped" + fi } check_postgres_transaction_id_age() { From ffebe721ff4c9fa68c8189b94ec9e3d72ed5fc95 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 9 Nov 2026 23:58:24 +0700 Subject: [PATCH 04/13] fix(table-health): review round 2 findings --- .../MCP/MCPConnectionBridge+TableHealth.swift | 92 ++++++-- .../TableHealth/PostgreSQLIndexShape.swift | 59 ++++- .../PostgreSQLTableHealthProvider.swift | 201 +++++++++++++----- .../TableHealth/TableHealthFixScript.swift | 65 +++++- .../TablePro/Resources/Localizable.xcstrings | 30 +++ .../ViewModels/TableHealthViewModel.swift | 7 +- ...MainContentCoordinator+CatalogChange.swift | 17 +- ...ntentCoordinator+DatabaseObjectTools.swift | 11 +- ...ontentCoordinator+TableHealthActions.swift | 9 + .../TableHealth/TableHealthFindingsPane.swift | 19 +- .../TableHealthLayoutMetrics.swift | 15 +- .../TableHealth/TableHealthPreviewPane.swift | 102 +++++---- .../Tools/GetTableHealthToolTests.swift | 65 +++++- .../PostgreSQLIndexShapeTests.swift | 91 ++++++-- .../PostgreSQLTableHealthProviderTests.swift | 92 ++++++-- .../TableHealthFixScriptTests.swift | 88 ++++++++ ...nSplitViewControllerDetailWidthTests.swift | 8 + .../TableHealthViewModelTests.swift | 19 +- .../MainContentCoordinatorRefreshTests.swift | 55 +++++ .../probes/check-table-health-catalogs.sh | 119 ++++++++++- 20 files changed, 955 insertions(+), 209 deletions(-) diff --git a/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift b/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift index 3eee2000f5..f63c1adcf6 100644 --- a/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift +++ b/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift @@ -25,30 +25,40 @@ extension MCPConnectionBridge { return Self.tableHealthPayload(report, ledger: ledger) } - // The race runRacingTimeout runs for a statement. A metadata read cannot be aborted on the server, so on a timeout - // the checks stop before their next statement and the reply waits only for the one already running. + // Answers at the deadline and lets the checks wind down after it: a read queued on the pool's serial lane sees its + // cancel only when the job ahead of it ends, and a catalog read already running cannot be stopped on the server. static func withinTableHealthTimeout( seconds: Int, _ body: @Sendable @escaping () async throws -> T ) async throws -> T { - try await withThrowingTaskGroup(of: T.self) { group in - group.addTask { - try await body() + let deadline = TableHealthDeadline() + let work = Task { + do { + deadline.settle(.success(try await body())) + } catch { + deadline.settle(.failure(error)) } - group.addTask { + } + let timer = Task { + do { try await Task.sleep(for: .seconds(seconds)) - throw DatabaseAccessError.timeout( - String( - format: String(localized: "Table Health did not finish within the MCP query timeout of %lld seconds."), - seconds - ) - ) - } - guard let first = try await group.next() else { - throw CancellationError() + } catch { + return } - group.cancelAll() - return first + deadline.settle(.failure(DatabaseAccessError.timeout( + String( + format: String(localized: "Table Health did not finish within the MCP query timeout of %lld seconds."), + seconds + ) + ))) + work.cancel() + } + defer { timer.cancel() } + return try await withTaskCancellationHandler { + try await deadline.wait() + } onCancel: { + deadline.settle(.failure(CancellationError())) + work.cancel() } } @@ -170,3 +180,51 @@ extension MCPConnectionBridge { ]) } } + +// The first outcome wins; later ones are dropped, so a read that ends after the timeout answered changes nothing. +private final class TableHealthDeadline: Sendable { + private enum State: Sendable { + case running + case waiting(CheckedContinuation) + case settled(Result) + case delivered + } + + private let state = OSAllocatedUnfairLock(initialState: .running) + + func settle(_ result: Result) { + let waiter = state.withLock { state -> CheckedContinuation? in + switch state { + case .running: + state = .settled(result) + return nil + case .waiting(let continuation): + state = .delivered + return continuation + case .settled, .delivered: + return nil + } + } + waiter?.resume(with: result) + } + + func wait() async throws -> T { + try await withCheckedThrowingContinuation { continuation in + let ready = state.withLock { state -> Result? in + switch state { + case .running: + state = .waiting(continuation) + return nil + case .settled(let result): + state = .delivered + return result + case .waiting, .delivered: + return .failure(CancellationError()) + } + } + if let ready { + continuation.resume(with: ready) + } + } + } +} diff --git a/apple/TablePro/Core/TableHealth/PostgreSQLIndexShape.swift b/apple/TablePro/Core/TableHealth/PostgreSQLIndexShape.swift index d2ec75e771..03bf9ce507 100644 --- a/apple/TablePro/Core/TableHealth/PostgreSQLIndexShape.swift +++ b/apple/TablePro/Core/TableHealth/PostgreSQLIndexShape.swift @@ -3,12 +3,17 @@ import Foundation // The valid indexes of one table, from PostgreSQLTableHealthProvider.redundantIndexQuery: a row per index column, // key columns first, then the INCLUDE columns. struct PostgreSQLIndexShape { - // A key column or expression as the server deparses it, with what decides which lookups it serves. + // A key column by name, or an expression by its node text, with what decides which lookups it serves. struct KeyPart: Equatable { let column: String + let isExpression: Bool let operatorClass: String let collation: String let sortOption: String + + var displayName: String { + isExpression ? String(localized: "(expression)") : column + } } struct Index { @@ -23,12 +28,18 @@ struct PostgreSQLIndexShape { let isProtected: Bool let predicate: String? let sizeBytes: Int64? - var definition: String? var keys: [KeyPart] var includes: [String] var columns: [String] { keys.map(\.column) + includes } + // The key columns, expressions as a placeholder, then any INCLUDE columns. + var columnsDescription: String { + let keyList = keys.map(\.displayName).joined(separator: ", ") + guard !includes.isEmpty else { return keyList } + return String(format: String(localized: "%1$@ INCLUDE (%2$@)"), keyList, includes.joined(separator: ", ")) + } + // Only a B-tree serves a lookup on its leading keys, so another method is redundant only to an equal index. // A unique index is redundant only to one enforcing the same rule. Of two indexes that cover each other, a // protected one stays, else the earlier name. @@ -103,7 +114,6 @@ struct PostgreSQLIndexShape { isProtected: row.flag("is_protected") == true, predicate: row.text("predicate"), sizeBytes: row.int64("index_bytes"), - definition: nil, keys: [], includes: [] )) @@ -118,17 +128,23 @@ struct PostgreSQLIndexShape { let indexParts = (parts[tableKey + [tables[tablePosition].indexes[indexPosition].name]] ?? []) .sorted { $0.position < $1.position } var index = tables[tablePosition].indexes[indexPosition] + // indexprs holds one expression per zero in indkey, in key order. + var expressions = indexParts.lazy.compactMap { $0.row.text("expressions") }.first.map(Self.expressions) ?? [] for part in indexParts { - if index.definition == nil { index.definition = part.row.text("index_definition") } + let isExpression = part.row.int64("attnum") == 0 + let column = isExpression + ? (expressions.isEmpty ? "" : expressions.removeFirst()) + : part.row["column_name"] if part.position <= part.keyCount { index.keys.append(KeyPart( - column: part.row["column_text"], + column: column, + isExpression: isExpression, operatorClass: part.row["opclass"], collation: part.row["collation"], sortOption: part.row["sort_option"] )) } else { - index.includes.append(part.row["column_text"]) + index.includes.append(column) } } tables[tablePosition].indexes[indexPosition] = index @@ -136,4 +152,35 @@ struct PostgreSQLIndexShape { } return tables } + + // The top-level nodes of a node-tree list, "({A ...} {B ...})". The server escapes a brace inside a token with a + // backslash, so an escaped character never opens or closes a node. + static func expressions(_ list: String) -> [String] { + var nodes: [String] = [] + var current = "" + var depth = 0 + var escaped = false + for character in list { + if escaped { + if depth > 0 { current.append(character) } + escaped = false + continue + } + if character == "\\" { + escaped = true + if depth > 0 { current.append(character) } + continue + } + if character == "{" { depth += 1 } + if depth > 0 { current.append(character) } + if character == "}", depth > 0 { + depth -= 1 + if depth == 0 { + nodes.append(current) + current = "" + } + } + } + return nodes + } } diff --git a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift index 1520b455b7..ec4e9f35c8 100644 --- a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift +++ b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift @@ -3,6 +3,10 @@ import Foundation // scripts/probes/check-table-health-catalogs.sh runs these literals against real servers: keep them free of interpolation. // Every query leaves out what an extension owns, by relation and by schema: TimescaleDB's chunk tables are not extension // members themselves, but their schema is, and a fix there would change the extension's own objects. +// No query opens a relation another session may hold locked: the size functions and the index and expression +// deparsers wait behind an ACCESS EXCLUSIVE lock (an open ALTER TABLE, TRUNCATE or VACUUM FULL) until it ends. Sizes +// are relpages times block_size, as of the last VACUUM, ANALYZE or index build; on 14 and later a table never measured +// has reltuples -1 and no size. struct PostgreSQLTableHealthProvider: TableHealthProvider { let checks: [TableHealthCheckKind] = [ .bloat, .unusedIndex, .redundantIndex, .invalidIndex, .missingPrimaryKey, .unindexedForeignKey, @@ -16,25 +20,30 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { SELECT pg_catalog.current_setting('track_counts') AS track_counts, pg_catalog.date_part('epoch', d.stats_reset) AS stats_reset, pg_catalog.current_setting('server_version_num') AS server_version_num, - pg_catalog.pg_is_in_recovery() AS in_recovery + pg_catalog.pg_is_in_recovery() AS in_recovery, + (SELECT pg_catalog.count(*) FROM pg_catalog.pg_stat_replication) AS standbys FROM pg_catalog.pg_stat_database d WHERE d.datname = pg_catalog.current_database() """ - // The floor keeps tiny tables out. A pg_ schema is another session's temporary one. A reset, a crash or a failover - // empties last_vacuum along with the counters, so the reset time says how far back "no vacuum" reaches. + // The floor keeps tiny tables out. A pg_ schema is another session's temporary one. pg_stat_reset() empties + // last_vacuum and records when; a crash empties it too, and on 15 and later leaves stats_reset NULL, so an empty + // last_vacuum proves only that the server's statistics hold no vacuum. static let bloatQuery = """ SELECT s.schemaname AS schema_name, s.relname AS table_name, s.n_live_tup AS live_rows, s.n_dead_tup AS dead_rows, pg_catalog.date_part('epoch', GREATEST(s.last_vacuum, s.last_autovacuum)) AS last_vacuum, - pg_catalog.pg_table_size(c.oid) AS table_bytes, + CASE WHEN c.reltuples >= 0::pg_catalog.float4 + THEN (c.relpages::pg_catalog.int8 + COALESCE(tc.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 + END AS table_bytes, (SELECT pg_catalog.date_part('epoch', d.stats_reset) FROM pg_catalog.pg_stat_database d WHERE d.datname = pg_catalog.current_database()) AS stats_reset FROM pg_catalog.pg_stat_user_tables s JOIN pg_catalog.pg_class c ON c.oid = s.relid + LEFT JOIN pg_catalog.pg_class tc ON tc.oid = c.reltoastrelid WHERE c.relkind = 'r' AND pg_catalog.left(s.schemaname, 3) <> 'pg_' AND s.n_dead_tup >= 1000 @@ -63,9 +72,10 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { SELECT s.schemaname AS schema_name, s.relname AS table_name, s.indexrelname AS index_name, - pg_catalog.pg_relation_size(s.indexrelid) AS index_bytes + ic.relpages::pg_catalog.int8 * pg_catalog.current_setting('block_size')::pg_catalog.int8 AS index_bytes FROM pg_catalog.pg_stat_user_indexes s JOIN pg_catalog.pg_index i ON i.indexrelid = s.indexrelid + JOIN pg_catalog.pg_class ic ON ic.oid = s.indexrelid JOIN pg_catalog.pg_class t ON t.oid = s.relid WHERE s.idx_scan = 0 AND t.relkind = 'r' @@ -105,9 +115,12 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { SELECT n.nspname AS schema_name, c.relname AS table_name, GREATEST(c.reltuples, 0)::pg_catalog.int8 AS estimated_rows, - pg_catalog.pg_table_size(c.oid) AS table_bytes + CASE WHEN c.reltuples >= 0::pg_catalog.float4 + THEN (c.relpages::pg_catalog.int8 + COALESCE(tc.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 + END AS table_bytes FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + LEFT JOIN pg_catalog.pg_class tc ON tc.oid = c.reltoastrelid WHERE c.relkind IN ('r', 'p') AND n.nspname <> 'information_schema' AND pg_catalog.left(n.nspname, 3) <> 'pg_' @@ -148,9 +161,12 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { rn.nspname AS referenced_schema, rc.relname AS referenced_table, GREATEST(c.reltuples, 0)::pg_catalog.int8 AS estimated_rows, - pg_catalog.pg_table_size(c.oid) AS table_bytes + CASE WHEN c.reltuples >= 0::pg_catalog.float4 + THEN (c.relpages::pg_catalog.int8 + COALESCE(tc.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 + END AS table_bytes FROM pg_catalog.pg_constraint con JOIN pg_catalog.pg_class c ON c.oid = con.conrelid + LEFT JOIN pg_catalog.pg_class tc ON tc.oid = c.reltoastrelid JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace JOIN pg_catalog.pg_class rc ON rc.oid = con.confrelid JOIN pg_catalog.pg_namespace rn ON rn.oid = rc.relnamespace @@ -183,9 +199,10 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { ORDER BY n.nspname, c.relname, con.conname, k.position """ - // One row per index column; PostgreSQLIndexShape compares them. A key part is the deparsed column or expression - // with its operator class, collation and sort flags. indnkeyatts (11+) and indnullsnotdistinct (15+) go through - // row_to_json for older servers. + // One row per index column; PostgreSQLIndexShape compares them. A key part is the column, or for attnum 0 the next + // expression of indexprs, with its operator class, collation and sort flags. Expressions and predicates compare as + // node text without the :location offsets, which differ with where the statement wrote them; deparsing them would + // lock the table. indnkeyatts (11+) and indnullsnotdistinct (15+) go through row_to_json for older servers. static let redundantIndexQuery = """ SELECT n.nspname AS schema_name, t.relname AS table_name, @@ -199,12 +216,15 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { (i.indisreplident OR EXISTS (SELECT 1 FROM pg_catalog.pg_constraint con WHERE con.conindid = i.indexrelid) OR EXISTS (SELECT 1 FROM pg_catalog.pg_inherits h WHERE h.inhrelid = i.indexrelid)) AS is_protected, - pg_catalog.pg_get_expr(i.indpred, i.indrelid) AS predicate, - pg_catalog.pg_relation_size(i.indexrelid) AS index_bytes, - CASE WHEN p.position = 1 THEN pg_catalog.pg_get_indexdef(i.indexrelid) END AS index_definition, + pg_catalog.regexp_replace(i.indpred::pg_catalog.text, ' :location -?[0-9]+', '', 'g') AS predicate, + CASE WHEN p.position = 1 + THEN pg_catalog.regexp_replace(i.indexprs::pg_catalog.text, ' :location -?[0-9]+', '', 'g') + END AS expressions, + ic.relpages::pg_catalog.int8 * pg_catalog.current_setting('block_size')::pg_catalog.int8 AS index_bytes, k.key_count, p.position, - pg_catalog.pg_get_indexdef(i.indexrelid, p.position, true) AS column_text, + (i.indkey::pg_catalog.int2[])[p.position - 1] AS attnum, + a.attname AS column_name, CASE WHEN p.position <= k.key_count THEN (i.indclass::pg_catalog.oid[])[p.position - 1] END AS opclass, CASE WHEN p.position <= k.key_count THEN (i.indcollation::pg_catalog.oid[])[p.position - 1] END AS collation, CASE WHEN p.position <= k.key_count THEN (i.indoption::pg_catalog.int2[])[p.position - 1] END AS sort_option @@ -217,6 +237,8 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { SELECT COALESCE((pg_catalog.row_to_json(i) OPERATOR(pg_catalog.->>) 'indnkeyatts'::pg_catalog.text)::pg_catalog.int4, i.indnatts) AS key_count ) k CROSS JOIN LATERAL pg_catalog.generate_series(1, i.indnatts) AS p(position) + LEFT JOIN pg_catalog.pg_attribute a + ON a.attrelid = i.indrelid AND a.attnum = (i.indkey::pg_catalog.int2[])[p.position - 1] WHERE i.indisvalid AND t.relkind IN ('r', 'p', 'm') AND n.nspname <> 'information_schema' @@ -239,15 +261,26 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { // A build still running holds SHARE UPDATE EXCLUSIVE on its table for the whole build, and from 12 on its backend // shows in pg_stat_progress_create_index, pid included for any role. Autovacuum and ANALYZE take the same lock, so // the lock alone would hide a leftover while they run. An index built ON ONLY a partitioned table (relkind I) stays - // invalid by design until every partition attaches one, so it is not a leftover. + // invalid by design until every partition attaches one, so it is not a leftover. pg_get_indexdef locks the table, + // so the definition is read only while no other session holds or awaits ACCESS EXCLUSIVE on it; a lock taken in + // the moment between the two still makes this read wait. static let invalidIndexQuery = """ SELECT n.nspname AS schema_name, t.relname AS table_name, c.relname AS index_name, i.indisunique AS is_unique, i.indisready AS is_ready, - pg_catalog.pg_relation_size(i.indexrelid) AS index_bytes, - pg_catalog.pg_get_indexdef(i.indexrelid) AS index_definition + c.relpages::pg_catalog.int8 * pg_catalog.current_setting('block_size')::pg_catalog.int8 AS index_bytes, + CASE WHEN NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_locks l + WHERE l.locktype = 'relation' + AND l.database = (SELECT d.oid FROM pg_catalog.pg_database d WHERE d.datname = pg_catalog.current_database()) + AND l.relation = i.indrelid + AND l.mode = 'AccessExclusiveLock' + AND l.pid <> pg_catalog.pg_backend_pid() + ) + THEN pg_catalog.pg_get_indexdef(i.indexrelid) + END AS index_definition FROM pg_catalog.pg_index i JOIN pg_catalog.pg_class c ON c.oid = i.indexrelid JOIN pg_catalog.pg_class t ON t.oid = i.indrelid @@ -286,8 +319,17 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { c.relname AS index_name, i.indisunique AS is_unique, i.indisready AS is_ready, - pg_catalog.pg_relation_size(i.indexrelid) AS index_bytes, - pg_catalog.pg_get_indexdef(i.indexrelid) AS index_definition + c.relpages::pg_catalog.int8 * pg_catalog.current_setting('block_size')::pg_catalog.int8 AS index_bytes, + CASE WHEN NOT EXISTS ( + SELECT 1 FROM pg_catalog.pg_locks l + WHERE l.locktype = 'relation' + AND l.database = (SELECT d.oid FROM pg_catalog.pg_database d WHERE d.datname = pg_catalog.current_database()) + AND l.relation = i.indrelid + AND l.mode = 'AccessExclusiveLock' + AND l.pid <> pg_catalog.pg_backend_pid() + ) + THEN pg_catalog.pg_get_indexdef(i.indexrelid) + END AS index_definition FROM pg_catalog.pg_index i JOIN pg_catalog.pg_class c ON c.oid = i.indexrelid JOIN pg_catalog.pg_class t ON t.oid = i.indrelid @@ -321,10 +363,12 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { // pg_sequence is 10+. The range is the narrower of the sequence's own limits and the integer column it feeds // through OWNED BY, an identity or a nextval default; the narrowest such column wins. last_value reads as // pg_sequences reads it: NULL without SELECT or USAGE, and for an unlogged sequence on a standby, where reading - // one fails before 17. A cycling sequence wraps by design. Unreadable sequences come back for the check's note. + // one fails before 17. Reading it takes ROW EXCLUSIVE on the sequence, so a sequence another session holds or + // awaits in a conflicting mode (an open ALTER SEQUENCE) is left unread. A cycling sequence wraps by design. + // Unreadable and locked sequences come back for the check's note. static let nearLimitQuery = """ SELECT r.sequence_schema, r.sequence_name, r.sequence_type, r.increment_by, r.sequence_min, r.sequence_max, - r.last_value, r.lower_bound, r.upper_bound, r.used_fraction, r.readable, + r.last_value, r.lower_bound, r.upper_bound, r.used_fraction, r.readable, r.locked, r.table_schema, r.table_name, r.column_name, r.column_type, r.column_min, r.column_max, r.ownership, r.table_bytes, r.referencing_columns FROM ( @@ -343,6 +387,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { END / NULLIF(b.upper_bound::pg_catalog.numeric - b.lower_bound::pg_catalog.numeric, 0::pg_catalog.numeric) AS used_fraction, v.readable, + v.locked, col.table_schema, col.table_name, col.column_name, @@ -355,9 +400,19 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { FROM pg_catalog.pg_sequence q JOIN pg_catalog.pg_class s ON s.oid = q.seqrelid JOIN pg_catalog.pg_namespace sn ON sn.oid = s.relnamespace + LEFT JOIN ( + SELECT DISTINCT l.relation + FROM pg_catalog.pg_locks l + WHERE l.locktype = 'relation' + AND l.database = (SELECT d.oid FROM pg_catalog.pg_database d WHERE d.datname = pg_catalog.current_database()) + AND l.mode IN ('ShareLock', 'ShareRowExclusiveLock', 'ExclusiveLock', 'AccessExclusiveLock') + AND l.pid <> pg_catalog.pg_backend_pid() + ) lk ON lk.relation = s.oid CROSS JOIN LATERAL ( SELECT pg_catalog.has_sequence_privilege(s.oid, 'SELECT,USAGE') AS readable, + lk.relation IS NOT NULL AS locked, CASE WHEN pg_catalog.has_sequence_privilege(s.oid, 'SELECT,USAGE') + AND lk.relation IS NULL AND (s.relpersistence = 'p' OR (s.relpersistence = 'u' AND NOT pg_catalog.pg_is_in_recovery())) THEN pg_catalog.pg_sequence_last_value(s.oid) END AS last_value @@ -368,7 +423,9 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { a.attname AS column_name, pg_catalog.format_type(a.atttypid, a.atttypmod) AS column_type, f.ownership, - pg_catalog.pg_table_size(t.oid) AS table_bytes, + CASE WHEN t.reltuples >= 0::pg_catalog.float4 + THEN (t.relpages::pg_catalog.int8 + COALESCE(tt.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 + END AS table_bytes, CASE WHEN ty.base OPERATOR(pg_catalog.=) 'pg_catalog.int2'::pg_catalog.regtype THEN -32768::pg_catalog.int8 WHEN ty.base OPERATOR(pg_catalog.=) 'pg_catalog.int4'::pg_catalog.regtype THEN -2147483648::pg_catalog.int8 END AS column_min, @@ -407,6 +464,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { AND d.refobjid = s.oid ) f JOIN pg_catalog.pg_class t ON t.oid = f.table_oid + LEFT JOIN pg_catalog.pg_class tt ON tt.oid = t.reltoastrelid JOIN pg_catalog.pg_namespace tn ON tn.oid = t.relnamespace JOIN pg_catalog.pg_attribute a ON a.attrelid = f.table_oid AND a.attnum = f.column_number JOIN pg_catalog.pg_type ct ON ct.oid = a.atttypid @@ -434,7 +492,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { AND x.deptype = 'e' ) ) r - WHERE NOT r.readable OR r.used_fraction >= 0.75 + WHERE NOT r.readable OR r.locked OR r.used_fraction >= 0.75 ORDER BY r.used_fraction DESC NULLS LAST, r.sequence_schema, r.sequence_name """ @@ -453,7 +511,9 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { f.freeze_max_age, 2147483648 - GREATEST(pg_catalog.age(c.relfrozenxid), COALESCE(pg_catalog.age(t.relfrozenxid), 0)) AS transactions_left, pg_catalog.date_part('epoch', GREATEST(s.last_vacuum, s.last_autovacuum)) AS last_vacuum, - pg_catalog.pg_table_size(c.oid) AS table_bytes + CASE WHEN c.reltuples >= 0::pg_catalog.float4 + THEN (c.relpages::pg_catalog.int8 + COALESCE(t.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 + END AS table_bytes FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace LEFT JOIN pg_catalog.pg_class t ON t.oid = c.reltoastrelid @@ -489,9 +549,10 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { // Autoanalyze's default rule: more rows modified than 10% of reltuples plus 50. reltuples is -1 before the // first analyze on 14+. A reset, a crash or a failover empties last_analyze with the counters, so a table counts - // as never analyzed only while pg_stats also holds nothing for it. Autovacuum never analyzes a partitioned - // table, and its activity counters stay empty, so a parent counts as never analyzed while pg_stats holds no - // inherited rows for it. + // as never analyzed only while pg_stats also holds nothing for it. pg_stats shows a table's rows only to a role + // that may read the table and is not under row-level security; elsewhere the counters are all there is, and + // stats_readable says so. Autovacuum never analyzes a partitioned table, and its activity counters stay empty, so + // a parent counts as never analyzed while a role that can read its pg_stats finds no inherited rows there. static let staleStatisticsQuery = """ SELECT n.nspname AS schema_name, c.relname AS table_name, @@ -503,14 +564,22 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { THEN NOT EXISTS (SELECT 1 FROM pg_catalog.pg_stats st WHERE st.schemaname = n.nspname AND st.tablename = c.relname AND st.inherited) ELSE s.last_analyze IS NULL AND s.last_autoanalyze IS NULL - AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_stats st - WHERE st.schemaname = n.nspname AND st.tablename = c.relname AND NOT st.inherited) + AND NOT (sr.stats_readable + AND EXISTS (SELECT 1 FROM pg_catalog.pg_stats st + WHERE st.schemaname = n.nspname AND st.tablename = c.relname AND NOT st.inherited)) END AS never_analyzed, + sr.stats_readable, pg_catalog.date_part('epoch', GREATEST(s.last_analyze, s.last_autoanalyze)) AS last_analyze, - pg_catalog.pg_table_size(c.oid) AS table_bytes + CASE WHEN c.reltuples >= 0::pg_catalog.float4 + THEN (c.relpages::pg_catalog.int8 + COALESCE(tc.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 + END AS table_bytes FROM pg_catalog.pg_class c JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace + LEFT JOIN pg_catalog.pg_class tc ON tc.oid = c.reltoastrelid LEFT JOIN pg_catalog.pg_stat_user_tables s ON s.relid = c.oid + CROSS JOIN LATERAL ( + SELECT pg_catalog.has_table_privilege(c.oid, 'SELECT') AND NOT pg_catalog.row_security_active(c.oid) AS stats_readable + ) sr CROSS JOIN LATERAL ( SELECT 50::pg_catalog.float8 + 0.1::pg_catalog.float8 * GREATEST(c.reltuples::pg_catalog.float8, 0::pg_catalog.float8) AS analyze_threshold @@ -532,7 +601,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { ) AND ( (c.relkind = 'p' - AND pg_catalog.has_table_privilege(c.oid, 'SELECT') + AND sr.stats_readable AND EXISTS (SELECT 1 FROM pg_catalog.pg_inherits h JOIN pg_catalog.pg_stat_user_tables ps ON ps.relid = h.inhrelid WHERE h.inhparent = c.oid AND ps.n_live_tup > 0) @@ -540,8 +609,9 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { WHERE st.schemaname = n.nspname AND st.tablename = c.relname AND st.inherited)) OR (c.relkind <> 'p' AND ((s.last_analyze IS NULL AND s.last_autoanalyze IS NULL AND s.n_live_tup > 0 - AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_stats st - WHERE st.schemaname = n.nspname AND st.tablename = c.relname AND NOT st.inherited)) + AND NOT (sr.stats_readable + AND EXISTS (SELECT 1 FROM pg_catalog.pg_stats st + WHERE st.schemaname = n.nspname AND st.tablename = c.relname AND NOT st.inherited))) OR s.n_mod_since_analyze::pg_catalog.float8 > th.analyze_threshold)) ) ORDER BY modified_rows DESC, n.nspname, c.relname @@ -638,9 +708,13 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { TableHealthFormatting.date(reset) )) } else { - statistics.notes.append( - String(localized: "Index usage counts since the statistics were first collected; they have never been reset.") - ) + statistics.notes.append(String( + localized: "Index usage counts cover only what the server's statistics hold; a reset, a crash or a failover discards them." + )) + } + // A primary counts the index scans its own sessions make, never those on its standbys. + if !statistics.isStandby, let standbys = row.int64("standbys"), standbys > 0 { + statistics.notes.append(String(localized: "Index scans on this server's standbys are not counted here.")) } return statistics } @@ -694,7 +768,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { dead, live, TableHealthFormatting.date(reset) ) } else { - detail = String(format: String(localized: "%1$@ dead rows, %2$@ live. Never vacuumed."), dead, live) + detail = String(format: String(localized: "%1$@ dead rows, %2$@ live. No vacuum recorded in the server's statistics."), dead, live) } var metrics: [TableHealthMetric] = [] @@ -823,16 +897,12 @@ extension PostgreSQLTableHealthProvider { var metrics = [ TableHealthMetric(id: "covering_index", label: String(localized: "Covering Index"), value: .text(covering.name)) ] - if let definition = index.definition { - metrics.append(TableHealthMetric( - id: "index_definition", label: String(localized: "Definition"), value: .text(definition) - )) - } - if let definition = covering.definition { - metrics.append(TableHealthMetric( - id: "covering_definition", label: String(localized: "Covering Definition"), value: .text(definition) - )) - } + metrics.append(TableHealthMetric( + id: "columns", label: String(localized: "Columns"), value: .text(index.columnsDescription) + )) + metrics.append(TableHealthMetric( + id: "covering_columns", label: String(localized: "Covering Columns"), value: .text(covering.columnsDescription) + )) // PostgreSQL refuses to drop an index on a partitioned table concurrently. let drop = table.isPartitioned ? "DROP INDEX" : "DROP INDEX CONCURRENTLY" findings.append(TableHealthFinding( @@ -918,27 +988,40 @@ extension PostgreSQLTableHealthProvider { _ context: TableHealthQueryContext ) -> (findings: [TableHealthFinding], note: String?) { var findings: [TableHealthFinding] = [] - var unreadable = 0 + var unreadable = 0, locked = 0 for row in rows { if row.flag("readable") == false { unreadable += 1 + } else if row.flag("locked") == true { + locked += 1 } else if let finding = nearLimitFinding(row, context) { findings.append(finding) } } - let note: String? + var notes: [String] = [] switch unreadable { case 0: - note = nil + break case 1: - note = String(localized: "One sequence is not readable by this user, so its counter was not checked.") + notes.append(String(localized: "One sequence is not readable by this user, so its counter was not checked.")) default: - note = String( + notes.append(String( format: String(localized: "%lld sequences are not readable by this user, so their counters were not checked."), unreadable - ) + )) + } + switch locked { + case 0: + break + case 1: + notes.append(String(localized: "One sequence is locked by another session, so its counter was not checked.")) + default: + notes.append(String( + format: String(localized: "%lld sequences are locked by another session, so their counters were not checked."), + locked + )) } - return (findings, note) + return (findings, notes.isEmpty ? nil : notes.joined(separator: " ")) } private static func typeLimit(of type: String, ascending: Bool) -> Int64? { @@ -1076,6 +1159,8 @@ extension PostgreSQLTableHealthProvider { let schema = row["schema_name"], table = row["table_name"] let isPartitioned = row["table_kind"] == "p" let neverAnalyzed = row.flag("never_analyzed") == true + // Without SELECT on the table, or under row-level security, pg_stats hides its rows and only the counters speak. + let statisticsReadable = row.flag("stats_readable") != false let lastAnalyze = row.date("last_analyze") let modified = Self.countText(row, "modified_rows"), threshold = Self.countText(row, "analyze_threshold") let detail: String @@ -1083,6 +1168,10 @@ extension PostgreSQLTableHealthProvider { detail = String( localized: "Never analyzed. Autovacuum does not analyze a partitioned table, so only a manual ANALYZE gives it statistics." ) + } else if neverAnalyzed, !statisticsReadable { + detail = String( + localized: "No analyze recorded in the server's statistics, and this user cannot read the table's statistics to confirm it." + ) } else if neverAnalyzed { detail = String(localized: "Never analyzed, so the planner has no statistics for it.") } else if let lastAnalyze { @@ -1111,7 +1200,9 @@ extension PostgreSQLTableHealthProvider { } } } - metrics.append(TableHealthMetric(id: "never_analyzed", label: String(localized: "Never Analyzed"), value: .flag(neverAnalyzed))) + if statisticsReadable { + metrics.append(TableHealthMetric(id: "never_analyzed", label: String(localized: "Never Analyzed"), value: .flag(neverAnalyzed))) + } if let lastAnalyze { metrics.append(TableHealthMetric(id: "last_analyze", label: String(localized: "Last Analyze"), value: .date(lastAnalyze))) } diff --git a/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift b/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift index 78dc6aadf5..9485ddf0a7 100644 --- a/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift +++ b/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift @@ -13,28 +13,79 @@ enum TableHealthFixScript { commentLine(String(format: String(localized: "Generated: %@"), date.formatted(.iso8601))), commentLine(String(localized: "Run each statement on its own; some cannot run inside a transaction.")) ].joined(separator: "\n") + let withFixes = findings.filter { finding in + guard let fix = finding.fixSQL else { return false } + return !fix.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + } + let kept = keptRedundantIndexes(withFixes) // An index both unused and redundant has one drop for two findings; run twice, the second fails. The fix // runs once, under the comment line of every finding it fixes. - var blocks: [(comments: [String], fix: String)] = [] + var blocks: [(comments: [String], fix: String?)] = [] var blockForFix: [String: Int] = [:] - for finding in findings { + for finding in withFixes { guard let fix = finding.fixSQL else { continue } + let name = commentLine(String(format: String(localized: "%1$@: %2$@"), checkTitle(finding.kind), finding.qualifiedName)) + if let cover = kept[finding.id] { + let note = String(format: String(localized: "Not dropped: its covering index %@ is dropped in this script."), cover) + blocks.append((comments: [name, commentLine(note)], fix: nil)) + continue + } let statement = fix.trimmingCharacters(in: .whitespacesAndNewlines) - guard !statement.isEmpty else { continue } - let name = String(format: String(localized: "%1$@: %2$@"), checkTitle(finding.kind), finding.qualifiedName) if let block = blockForFix[statement] { - blocks[block].comments.append(commentLine(name)) + blocks[block].comments.append(name) } else { blockForFix[statement] = blocks.count - blocks.append((comments: [commentLine(name)], fix: fix)) + blocks.append((comments: [name], fix: fix)) } } for block in blocks { - script += "\n\n" + block.comments.joined(separator: "\n") + "\n" + block.fix + script += "\n\n" + block.comments.joined(separator: "\n") + if let fix = block.fix { + script += "\n" + fix + } } return script + "\n" } + private struct IndexKey: Hashable { + let schema: String? + let table: String? + let name: String + } + + // A redundant index whose covering index this script drops too stays, or the script would drop both and leave + // the lookups they serve with no index: an unused composite covering a single-column index that queries use is + // the common case. Returns each kept finding with its covering index's name. + private static func keptRedundantIndexes(_ findings: [TableHealthFinding]) -> [TableHealthFindingID: String] { + var unusedDrops: Set = [] + var redundant: [IndexKey: (id: TableHealthFindingID, cover: IndexKey, coverName: String)] = [:] + for finding in findings { + guard let name = finding.objectName else { continue } + let key = IndexKey(schema: finding.schema, table: finding.table, name: name) + switch finding.kind { + case .unusedIndex: + unusedDrops.insert(key) + case .redundantIndex: + guard case .text(let cover)? = finding.metric("covering_index")?.value else { continue } + redundant[key] = (finding.id, IndexKey(schema: finding.schema, table: finding.table, name: cover), cover) + default: + continue + } + } + // Covers point from an index to one that serves at least its lookups, never back, so the walk ends; the + // visited set only guards against a malformed report. + func isDropped(_ key: IndexKey, visited: Set) -> Bool { + if unusedDrops.contains(key) { return true } + guard let entry = redundant[key], !visited.contains(key) else { return false } + return !isDropped(entry.cover, visited: visited.union([key])) + } + var kept: [TableHealthFindingID: String] = [:] + for (key, entry) in redundant where !unusedDrops.contains(key) && isDropped(entry.cover, visited: [key]) { + kept[entry.id] = entry.coverName + } + return kept + } + // A name can hold any character. Flattened and with its comment markers broken, it cannot end the comment early or // open a MySQL /*! ... */ comment the server would run. static func commentLine(_ text: String) -> String { diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 72d129a513..104232adb4 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -1818,6 +1818,9 @@ } } } + }, + "(expression)" : { + }, "(NULL)" : { "localizations" : { @@ -7112,6 +7115,9 @@ }, "%1$@ dead rows, %2$@ live. Never vacuumed." : { + }, + "%1$@ dead rows, %2$@ live. No vacuum recorded in the server's statistics." : { + }, "%1$@ dead rows, %2$@ live. No vacuum recorded since the statistics were reset at %3$@." : { @@ -7240,6 +7246,9 @@ }, "%1$@ in SRID %2$d." : { + }, + "%1$@ INCLUDE (%2$@)" : { + }, "%1$@ is a capped collection, and a longer field name makes MongoDB delete its oldest documents. Choose a name no longer than %2$@." : { "extractionState" : "manual", @@ -11801,6 +11810,9 @@ } } } + }, + "%lld sequences are locked by another session, so their counters were not checked." : { + }, "%lld sequences are not readable by this user, so their counters were not checked." : { @@ -55699,6 +55711,9 @@ }, "Country" : { + }, + "Covering Columns" : { + }, "Covering Definition" : { @@ -94600,6 +94615,9 @@ } } } + }, + "Index scans on this server's standbys are not counted here." : { + }, "Index size" : { "localizations" : { @@ -94682,6 +94700,9 @@ } } } + }, + "Index usage counts cover only what the server's statistics hold; a reset, a crash or a failover discards them." : { + }, "Index usage counts since the server started at %@." : { @@ -116278,6 +116299,9 @@ } } } + }, + "No analyze recorded in the server's statistics, and this user cannot read the table's statistics to confirm it." : { + }, "No Attached Databases" : { @@ -123153,6 +123177,9 @@ } } } + }, + "Not dropped: its covering index %@ is dropped in this script." : { + }, "not equals" : { "localizations" : { @@ -126903,6 +126930,9 @@ } } } + }, + "One sequence is locked by another session, so its counter was not checked." : { + }, "One sequence is not readable by this user, so its counter was not checked." : { diff --git a/apple/TablePro/ViewModels/TableHealthViewModel.swift b/apple/TablePro/ViewModels/TableHealthViewModel.swift index 5ba9f1a1aa..97e4e19190 100644 --- a/apple/TablePro/ViewModels/TableHealthViewModel.swift +++ b/apple/TablePro/ViewModels/TableHealthViewModel.swift @@ -15,7 +15,8 @@ enum TableHealthListState: Equatable { case failed(String) case noResults case ignoredHidden(Int) - case someChecksFailed(Int) + // A failure outranks hidden ignored findings, which it still counts. + case someChecksFailed(failed: Int, hiddenIgnored: Int) case noResolved } @@ -387,7 +388,9 @@ final class TableHealthViewModel: ObservableObject { if !filterText.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty, scopeRowCount > 0 { return .noResults } - if scope == .all, failedCheckCount > 0 { return .someChecksFailed(failedCheckCount) } + if scope == .all, failedCheckCount > 0 { + return .someChecksFailed(failed: failedCheckCount, hiddenIgnored: hiddenIgnoredCount) + } if hiddenIgnoredCount > 0 { return .ignoredHidden(hiddenIgnoredCount) } switch scope { case .all: diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+CatalogChange.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+CatalogChange.swift index a47a3b402e..3c77962771 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+CatalogChange.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+CatalogChange.swift @@ -7,13 +7,13 @@ import Foundation import TableProPluginKit import TableProSQLGrammar -/// What a window does with a change to its connection's catalog. The connection-level half has -/// already run once in `CatalogChangeService`; this is the part each window owns: its tabs, its -/// selection and its cached columns. +// What a window does with a change to its connection's catalog. The connection-level half has +// already run once in `CatalogChangeService`; this is the part each window owns: its tabs, its +// selection and its cached columns. extension MainContentCoordinator { - /// Reported whether the statement succeeded or failed: DDL that commits as it runs, a procedure - /// or a dropped connection can leave the catalog changed behind an error. A table it dropped or - /// renamed is reported apart, and only by a statement that succeeded. + // Reported whether the statement succeeded or failed: DDL that commits as it runs, a procedure + // or a dropped connection can leave the catalog changed behind an error. A table it dropped or + // renamed is reported apart, and only by a statement that succeeded. nonisolated static func postStatementRan( _ sql: String, on connection: DatabaseConnection, @@ -46,11 +46,12 @@ extension MainContentCoordinator { case (.renamed(let newName), .schema): guard let schema = container.schema else { return } retargetTabs(database: database, schema: schema, toDatabase: database, toSchema: newName) + refreshTableHealth(inDatabase: database) } } - /// Selection and queued operations naming objects the freshly loaded catalog no longer has, - /// judged by the object each names rather than by a bare table name. + // Selection and queued operations naming objects the freshly loaded catalog no longer has, + // judged by the object each names rather than by a bare table name. func pruneStaleSidebarState() { let adoption = CatalogEditAdoption( databaseManager: services.databaseManager, diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DatabaseObjectTools.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DatabaseObjectTools.swift index 79a3777531..4743a0417b 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DatabaseObjectTools.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DatabaseObjectTools.swift @@ -13,8 +13,8 @@ import TableProPluginKit extension MainContentCoordinator { private static let objectToolsLogger = Logger(subsystem: "com.TablePro", category: "DatabaseObjectTools") - /// The object a sidebar row or the menu bar's selection names, scoped to its own database and - /// schema. A row whose database is nil belongs to the database being browsed. + // The object a sidebar row or the menu bar's selection names, scoped to its own database and + // schema. A row whose database is nil belongs to the database being browsed. func objectTarget(for ref: DatabaseTreeTableRef) -> DatabaseObjectTarget? { guard let scope = services.databaseManager.resolvedScope( database: ref.database, @@ -31,7 +31,7 @@ extension MainContentCoordinator { // MARK: - Copy DDL - /// The same text the DDL viewer and the Structure tab's DDL show, read in the object's own scope. + // The same text the DDL viewer and the Structure tab's DDL show, read in the object's own scope. func copyDDL(of ref: DatabaseTreeTableRef) { guard let target = objectTarget(for: ref) else { return } let name = target.name @@ -66,8 +66,8 @@ extension MainContentCoordinator { } } - /// A failed check still asks the question: the refresh itself does not depend on it, and a - /// server that could not answer the catalog read will say why when the refresh runs. + // A failed check still asks the question: the refresh itself does not depend on it, and a + // server that could not answer the catalog read will say why when the refresh runs. private func refreshPrompt(for target: DatabaseObjectTarget) async -> MaterializedViewRefreshPrompt { do { let availability = try await MaterializedViewRefreshing.concurrentRefreshAvailability(of: target) @@ -167,6 +167,7 @@ extension MainContentCoordinator { closeTabsForRemovedObjects(ids: showing.map(\.id)) case .renamed(let newName): retitleTabs(ids: Set(showing.map(\.id)), to: newName) + refreshTableHealth(inDatabase: change.scope.database) } } } diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift index 32b14a5377..8ae4a5dd14 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift @@ -25,6 +25,15 @@ extension MainContentCoordinator { } } + // A table or schema rename moves the history's keys, while the report on screen still names the old object: + // an ignored finding would show again and Ignore would write an id no run reports. + func refreshTableHealth(inDatabase database: String) { + for tab in tabManager.tabs where tab.tabType == .tableHealth && tab.tableContext.databaseName == database { + guard let model = tableHealthModels[tab.id] else { continue } + Task { await model.refresh() } + } + } + func tableHealthActions(for model: TableHealthViewModel) -> TableHealthActions { TableHealthActions( openTable: { [weak self, weak model] finding in diff --git a/apple/TablePro/Views/TableHealth/TableHealthFindingsPane.swift b/apple/TablePro/Views/TableHealth/TableHealthFindingsPane.swift index 413996ffc0..47d039b624 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthFindingsPane.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthFindingsPane.swift @@ -95,15 +95,24 @@ struct TableHealthFindingsPane: View { UnavailableStateView { Label(String(localized: "No Findings"), systemImage: "eye.slash") } description: { - Text("^[\(count) ignored finding](inflect: true) hidden.") + Text(Self.hiddenIgnoredText(count)) } actions: { Button(String(localized: "Show Ignored")) { viewModel.showIgnored = true } } - case .someChecksFailed(let count): + case .someChecksFailed(let failed, let hiddenIgnored): UnavailableStateView { Label(String(localized: "Some Checks Failed"), systemImage: "exclamationmark.triangle") } description: { - Text(Self.failedChecksText(count)) + VStack(spacing: 4) { + Text(Self.failedChecksText(failed)) + if hiddenIgnored > 0 { + Text(Self.hiddenIgnoredText(hiddenIgnored)) + } + } + } actions: { + if hiddenIgnored > 0 { + Button(String(localized: "Show Ignored")) { viewModel.showIgnored = true } + } } case .noResolved: UnavailableStateView( @@ -117,4 +126,8 @@ struct TableHealthFindingsPane: View { private static func failedChecksText(_ count: Int) -> LocalizedStringKey { "^[\(count) check](inflect: true) could not run. Select one to see why." } + + private static func hiddenIgnoredText(_ count: Int) -> LocalizedStringKey { + "^[\(count) ignored finding](inflect: true) hidden." + } } diff --git a/apple/TablePro/Views/TableHealth/TableHealthLayoutMetrics.swift b/apple/TablePro/Views/TableHealth/TableHealthLayoutMetrics.swift index dc3c70c4cf..d3f99e602e 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthLayoutMetrics.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthLayoutMetrics.swift @@ -7,13 +7,18 @@ internal enum TableHealthLayoutMetrics { static let checksListMinimumWidth: CGFloat = 274 static let checksListMaximumWidth: CGFloat = 360 static let findingsMinimumWidth: CGFloat = 320 - static let findingsMinimumHeight: CGFloat = 140 - // Leaves the fix SQL about 140pt under the title, detail line and note. - static let previewMinimumHeight: CGFloat = 240 - // Below this the SQL and the facts beside it stop fitting, and the facts move under the SQL. + // A 480pt window leaves the tab about 357pt under its toolbar, tab strip and pane bar (measured). The two panes + // and the 1pt divider between them fit in that, or the window cannot shrink to its minimum. + static let findingsMinimumHeight: CGFloat = 120 + static let previewMinimumHeight: CGFloat = 236 + + // At the preview's minimum height: the SQL keeps 71pt with the detail and facts capped under it, and 159pt + // with them beside it (measured). + static let previewSQLMinimumHeight: CGFloat = 60 static let previewSQLIdealWidth: CGFloat = 280 - static let previewFactsWidth: CGFloat = 220 + static let previewAboutWidth: CGFloat = 280 + static let previewAboutMaximumHeight: CGFloat = 80 // The pane bar at its narrowest, with the database name truncated and the filter at 120. Narrower than the // checks list and the findings table side by side: between the two the list collapses. diff --git a/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift b/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift index 926e6d6d93..2d3530c8d6 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift @@ -1,12 +1,14 @@ import SwiftUI -// The fix SQL is the pane's main content: the facts sit beside it, or under it in one line each when the pane is -// too narrow, so they never push it out of view. +// The fix SQL is the pane's main content. The finding's detail and facts sit beside it, or under it in a capped +// scrolling area when the pane is too narrow, so they never push it out of view. struct TableHealthPreviewPane: View { @ObservedObject var viewModel: TableHealthViewModel let databaseType: DatabaseType let actions: TableHealthActions + @State private var aboutHeight: CGFloat = 0 + private struct Fact: Identifiable { let id: String let label: String @@ -29,7 +31,6 @@ struct TableHealthPreviewPane: View { private func single(_ finding: TableHealthFinding) -> some View { let fix = TableHealthViewModel.hasFix(finding) ? finding.fixSQL : nil - let facts = Self.facts(of: finding) return VStack(alignment: .leading, spacing: 8) { HStack(spacing: 8) { Text(verbatim: finding.qualifiedName) @@ -48,33 +49,32 @@ struct TableHealthPreviewPane: View { } .disabled(fix == nil) } - Text(verbatim: finding.detail) - .font(.callout) - .foregroundStyle(.secondary) - .lineLimit(2) - .help(finding.detail) - if facts.isEmpty { - fixArea(fix, id: finding.id) - } else { - ViewThatFits(in: .horizontal) { - HStack(alignment: .top, spacing: 12) { - fixArea(fix, id: finding.id) - .frame( - minWidth: 0, - idealWidth: TableHealthLayoutMetrics.previewSQLIdealWidth, - maxWidth: .infinity, - maxHeight: .infinity - ) - ScrollView { - factsColumn(facts) - } - .frame(width: TableHealthLayoutMetrics.previewFactsWidth) + ViewThatFits(in: .horizontal) { + HStack(alignment: .top, spacing: 12) { + fixArea(fix, id: finding.id) + .frame( + minWidth: 0, + idealWidth: TableHealthLayoutMetrics.previewSQLIdealWidth, + maxWidth: .infinity, + maxHeight: .infinity + ) + ScrollView { + about(finding) } - VStack(alignment: .leading, spacing: 8) { - fixArea(fix, id: finding.id) - .frame(maxWidth: .infinity, maxHeight: .infinity) - factsGrid(facts) + .frame(width: TableHealthLayoutMetrics.previewAboutWidth) + } + VStack(alignment: .leading, spacing: 8) { + fixArea(fix, id: finding.id) + .frame( + maxWidth: .infinity, + minHeight: TableHealthLayoutMetrics.previewSQLMinimumHeight, + maxHeight: .infinity + ) + // A ScrollView takes all the height it is offered, so it gets the content's height up to the cap. + ScrollView { + about(finding) } + .frame(height: min(aboutHeight, TableHealthLayoutMetrics.previewAboutMaximumHeight)) } } if fix != nil { @@ -100,38 +100,32 @@ struct TableHealthPreviewPane: View { } } - // Each label over its value, which wraps: a definition is longer than the column. - private func factsColumn(_ facts: [Fact]) -> some View { - VStack(alignment: .leading, spacing: 6) { - ForEach(facts) { fact in - VStack(alignment: .leading, spacing: 1) { - Text(verbatim: fact.label) - .font(.caption) - .foregroundStyle(.secondary) - Text(verbatim: fact.value) - .font(.callout) - .fixedSize(horizontal: false, vertical: true) + // The whole detail: a warning such as the lock a type change takes must not be cut off. + private func about(_ finding: TableHealthFinding) -> some View { + let facts = Self.facts(of: finding) + return VStack(alignment: .leading, spacing: 6) { + Text(verbatim: finding.detail) + .foregroundStyle(.secondary) + .fixedSize(horizontal: false, vertical: true) + if !facts.isEmpty { + Form { + ForEach(facts) { fact in + LabeledContent(fact.label) { + Text(verbatim: fact.value) + } + } } + .formStyle(.columns) } } + .font(.callout) .frame(maxWidth: .infinity, alignment: .leading) .textSelection(.enabled) - } - - private func factsGrid(_ facts: [Fact]) -> some View { - Grid(alignment: .leadingFirstTextBaseline, horizontalSpacing: 12, verticalSpacing: 2) { - ForEach(facts) { fact in - GridRow { - Text(verbatim: fact.label) - .foregroundStyle(.secondary) - Text(verbatim: fact.value) - .lineLimit(1) - .truncationMode(.middle) - .help(fact.value) - } - } + .onGeometryChange(for: CGFloat.self) { proxy in + proxy.size.height + } action: { height in + aboutHeight = height } - .font(.callout) } private static func facts(of finding: TableHealthFinding) -> [Fact] { diff --git a/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift b/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift index 4b2661b1b3..085b9b3eaf 100644 --- a/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift +++ b/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift @@ -201,33 +201,76 @@ struct GetTableHealthToolTests { #expect(required(metricSchema) == keys(metric)) } - @Test("Checks that outlast the MCP query timeout fail as a timeout, once the statement in flight returns") - func timeoutStopsTheChecks() async { - let inFlight = AsyncGate() - let cancelled = AsyncGate() + // The stand-in read stays held until after the call has answered, like one queued behind the tab's report on + // the pool's serial lane, so a race that waited for it would never finish. + @Test("The MCP query timeout answers at once and tells the checks to stop", .timeLimit(.minutes(1))) + func timeoutAnswersWithoutWaitingForTheChecks() async { + let held = AsyncGate() + let stopped = AsyncGate() + var thrown: (any Error)? + + do { + _ = try await MCPConnectionBridge.withinTableHealthTimeout(seconds: 0) { + try await withTaskCancellationHandler { + await held.wait() + try Task.checkCancellation() + return 1 + } onCancel: { + Task { await stopped.open() } + } + } + } catch { + thrown = error + } + await stopped.wait() + await held.open() + + let mapped = (thrown as? DatabaseAccessError).map { MCPToolExecutionError.from($0) } + #expect(mapped?.code == .timeout) + #expect(mapped?.message == "Table Health did not finish within the MCP query timeout of 0 seconds.") + } + + @Test("A cancelled call answers at once and tells the checks to stop", .timeLimit(.minutes(1))) + func cancelAnswersWithoutWaitingForTheChecks() async { + let held = AsyncGate() + let stopped = AsyncGate() let call = Task { - try await MCPConnectionBridge.withinTableHealthTimeout(seconds: 0) { + try await MCPConnectionBridge.withinTableHealthTimeout(seconds: 3_600) { try await withTaskCancellationHandler { - await inFlight.wait() + await held.wait() try Task.checkCancellation() return 1 } onCancel: { - Task { await cancelled.open() } + Task { await stopped.open() } } } } - await cancelled.wait() - await inFlight.open() + await held.arrived(1) + call.cancel() let outcome = await call.result + await stopped.wait() + await held.open() var thrown: (any Error)? if case .failure(let error) = outcome { thrown = error } + #expect(thrown is CancellationError) + } + + @Test("An error from the checks reaches the caller as it is") + func checkErrorPassesThrough() async { + var thrown: (any Error)? + do { + _ = try await MCPConnectionBridge.withinTableHealthTimeout(seconds: 3_600) { () async throws -> Int in + throw DatabaseAccessError.notFound("no such database") + } + } catch { + thrown = error + } let mapped = (thrown as? DatabaseAccessError).map { MCPToolExecutionError.from($0) } - #expect(mapped?.code == .timeout) - #expect(mapped?.message == "Table Health did not finish within the MCP query timeout of 0 seconds.") + #expect(mapped?.code == .notFound) } @Test("Checks that finish within the MCP query timeout return their report") diff --git a/apple/TableProTests/Core/TableHealth/PostgreSQLIndexShapeTests.swift b/apple/TableProTests/Core/TableHealth/PostgreSQLIndexShapeTests.swift index aa5ae43d02..8c9366cc1a 100644 --- a/apple/TableProTests/Core/TableHealth/PostgreSQLIndexShapeTests.swift +++ b/apple/TableProTests/Core/TableHealth/PostgreSQLIndexShapeTests.swift @@ -5,8 +5,10 @@ import Testing // The cases a PostgreSQL 13 and 17 server gave for the redundant index rule; the probe seeds the same names. struct PostgreSQLIndexShapeTests { + // A column by name, or an expression by its node text as the query returns it, without :location. private struct Key: ExpressibleByStringLiteral { let column: String + var isExpression = false var opclass = "1978" var collation = "0" var sort = "0" @@ -21,6 +23,18 @@ struct PostgreSQLIndexShapeTests { init(stringLiteral value: String) { self.init(value) } + + static func expression(_ node: String, opclass: String = "3126", collation: String = "100") -> Key { + var key = Key(node, opclass: opclass, collation: collation) + key.isExpression = true + return key + } + } + + private static let lowerEmail = "{FUNCEXPR :funcid 870 :funcresulttype 25 :args ({VAR :varno 1 :varattno 5 :vartype 25})}" + private static let upperEmail = "{FUNCEXPR :funcid 871 :funcresulttype 25 :args ({VAR :varno 1 :varattno 5 :vartype 25})}" + private static func tsvector(_ config: String) -> String { + "{FUNCEXPR :funcid 3745 :args ({CONST :consttype 3734 :constvalue 4 [ \(config) 0 0 0 ]} {VAR :varno 1 :varattno 7})}" } private struct IndexRow { @@ -42,14 +56,16 @@ struct PostgreSQLIndexShapeTests { private static let columns = [ "schema_name", "table_name", "table_kind", "index_name", "access_method", "is_unique", "is_primary", "is_immediate", - "nulls_not_distinct", "is_protected", "predicate", "index_bytes", "index_definition", "key_count", "position", - "column_text", "opclass", "collation", "sort_option" + "nulls_not_distinct", "is_protected", "predicate", "expressions", "index_bytes", "key_count", "position", + "attnum", "column_name", "opclass", "collation", "sort_option" ] private static func flag(_ value: Bool) -> String { value ? "t" : "f" } + // An expression key has attnum 0 and its node in the index's expressions list, which the first row carries. private static func rows(_ indexes: [IndexRow]) -> [[String?]] { - indexes.flatMap { index -> [[String?]] in + var attnums: [String: Int] = [:] + return indexes.flatMap { index -> [[String?]] in var parts: [(column: String, key: Key?)] = [] for key in index.keys { parts.append((key.column, key)) @@ -57,13 +73,25 @@ struct PostgreSQLIndexShapeTests { for column in index.includes { parts.append((column, nil)) } + let expressionNodes = index.keys.filter(\.isExpression).map(\.column) + let expressions = expressionNodes.isEmpty ? nil : "(" + expressionNodes.joined(separator: " ") + ")" return parts.enumerated().map { offset, part -> [String?] in - [ + let isExpression = part.key?.isExpression == true + let attnumKey = index.schema + "." + index.table + "." + part.column + let attnum: Int + if isExpression { + attnum = 0 + } else if let known = attnums[attnumKey] { + attnum = known + } else { + attnum = attnums.count + 1 + attnums[attnumKey] = attnum + } + return [ index.schema, index.table, index.tableKind, index.name, index.method, flag(index.unique), flag(index.primary), flag(index.immediate), index.nullsNotDistinct ? "true" : "false", flag(index.protected), index.predicate, - index.bytes, offset == 0 ? "CREATE INDEX \(index.name) ON \(index.schema).\(index.table)" : nil, - String(index.keys.count), String(offset + 1), part.column, part.key?.opclass, part.key?.collation, - part.key?.sort + offset == 0 ? expressions : nil, index.bytes, String(index.keys.count), String(offset + 1), + String(attnum), isExpression ? nil : part.column, part.key?.opclass, part.key?.collation, part.key?.sort ] } } @@ -116,8 +144,8 @@ struct PostgreSQLIndexShapeTests { IndexRow(name: "k_code_pattern", keys: [Key("code", opclass: "4217", collation: "100")]), IndexRow(name: "k_code_c", keys: [Key("code", opclass: "3126", collation: "950")]), IndexRow(name: "k_hash_a", method: "hash", keys: [Key("a", opclass: "10020")]), - IndexRow(name: "k_lower_email", keys: [Key("lower(email)", opclass: "3126", collation: "100")]), - IndexRow(name: "k_upper_email", keys: [Key("upper(email)", opclass: "3126", collation: "100")]) + IndexRow(name: "k_lower_email", keys: [.expression(Self.lowerEmail)]), + IndexRow(name: "k_upper_email", keys: [.expression(Self.upperEmail)]) ]) #expect(found.isEmpty) } @@ -125,12 +153,39 @@ struct PostgreSQLIndexShapeTests { @Test("The same expression twice is a duplicate") func duplicateExpression() { let found = redundant([ - IndexRow(name: "k_lower_email", keys: [Key("lower(email)", opclass: "3126", collation: "100")]), - IndexRow(name: "r_lower_email_dup", keys: [Key("lower(email)", opclass: "3126", collation: "100")]) + IndexRow(name: "k_lower_email", keys: [.expression(Self.lowerEmail)]), + IndexRow(name: "r_lower_email_dup", keys: [.expression(Self.lowerEmail)]) ]) #expect(found == ["r_lower_email_dup": "k_lower_email"]) } + @Test("Expressions compare by node text, and each zero in the key takes the next expression in order") + func expressionsByNodeText() throws { + let found = redundant([ + IndexRow(name: "r_a_lower", keys: ["a", .expression(Self.lowerEmail)]), + IndexRow(name: "k_a_lower_b", keys: ["a", .expression(Self.lowerEmail), "b"]), + IndexRow(name: "k_lower_upper", keys: [.expression(Self.lowerEmail), .expression(Self.upperEmail)]), + IndexRow(name: "k_upper_lower", keys: [.expression(Self.upperEmail), .expression(Self.lowerEmail)]) + ]) + #expect(found == ["r_a_lower": "k_a_lower_b"]) + + let finding = try #require(findings([ + IndexRow(name: "r_a_lower", keys: ["a", .expression(Self.lowerEmail)]), + IndexRow(name: "k_a_lower_b", keys: ["a", .expression(Self.lowerEmail), "b"]) + ]).first) + let expression = String(localized: "(expression)") + #expect(finding.metric("columns")?.value == .text("a, " + expression)) + #expect(finding.metric("covering_columns")?.value == .text("a, " + expression + ", b")) + } + + @Test("A node list splits into its top-level nodes, and an escaped brace opens or closes none") + func expressionListSplits() { + #expect(PostgreSQLIndexShape.expressions(#"({A :x 1} {B :args ({C :y 2})} {D :name a\}b\{c})"#) == [ + "{A :x 1}", "{B :args ({C :y 2})}", #"{D :name a\}b\{c}"# + ]) + #expect(PostgreSQLIndexShape.expressions("").isEmpty) + } + @Test("A partial index is covered only by one with the same predicate") func partialNeedsSamePredicate() { let found = redundant([ @@ -205,6 +260,8 @@ struct PostgreSQLIndexShapeTests { let withCover = indexes + [IndexRow(table: "ri_incl", name: "k_abc_incl", keys: ["a", "b"], includes: ["c"])] #expect(redundant(withCover) == ["k_a_incl_c": "k_abc_incl", "k_ab2": "k_abc_incl", "r_a_only": "k_abc_incl"]) + let included = findings(withCover).first { $0.objectName == "k_a_incl_c" } + #expect(included?.metric("columns")?.value == .text(String(format: String(localized: "%1$@ INCLUDE (%2$@)"), "a", "c"))) } @Test("A GiST or GIN index is redundant only to an equal one, never to a longer one") @@ -212,14 +269,14 @@ struct PostgreSQLIndexShapeTests { let found = redundant([ IndexRow(table: "rx", name: "rx_room_during_excl", method: "gist", protected: true, keys: ["room", "during"]), IndexRow(table: "rx", name: "k_rx_room_gist", method: "gist", keys: ["room"]), - IndexRow(name: "k_note_gin", method: "gin", keys: [Key("to_tsvector('simple'::regconfig, note)", opclass: "10073")]), - IndexRow(name: "k_note_gin_2", method: "gin", keys: [Key("to_tsvector('english'::regconfig, note)", opclass: "10073")]), - IndexRow(name: "r_note_gin_dup", method: "gin", keys: [Key("to_tsvector('simple'::regconfig, note)", opclass: "10073")]) + IndexRow(name: "k_note_gin", method: "gin", keys: [.expression(Self.tsvector("1"), opclass: "10073", collation: "0")]), + IndexRow(name: "k_note_gin_2", method: "gin", keys: [.expression(Self.tsvector("2"), opclass: "10073", collation: "0")]), + IndexRow(name: "r_note_gin_dup", method: "gin", keys: [.expression(Self.tsvector("1"), opclass: "10073", collation: "0")]) ]) #expect(found == ["r_note_gin_dup": "k_note_gin"]) } - @Test("A redundant index previews a concurrent drop and carries its size and both definitions") + @Test("A redundant index previews a concurrent drop and carries its size and both indexes' columns") func redundantFinding() throws { let finding = try #require(findings([ IndexRow(schema: "sales", table: "orders", name: "orders_a_idx", bytes: "16384", keys: ["a"]), @@ -231,8 +288,8 @@ struct PostgreSQLIndexShapeTests { #expect(finding.sizeBytes == 16_384) #expect(finding.fixSQL == #"DROP INDEX CONCURRENTLY "sales"."orders_a_idx";"#) #expect(finding.metric("covering_index")?.value == .text("orders_ab_idx")) - #expect(finding.metric("index_definition")?.value == .text("CREATE INDEX orders_a_idx ON sales.orders")) - #expect(finding.metric("covering_definition")?.value == .text("CREATE INDEX orders_ab_idx ON sales.orders")) + #expect(finding.metric("columns")?.value == .text("a")) + #expect(finding.metric("covering_columns")?.value == .text("a, b")) #expect(finding.metric("index_bytes") == nil) #expect(finding.detail == String(format: String(localized: "%1$@ is covered by %2$@."), "orders_a_idx", "orders_ab_idx")) } diff --git a/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift b/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift index c9685b1b29..b9d45473bd 100644 --- a/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift +++ b/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift @@ -8,7 +8,7 @@ struct PostgreSQLTableHealthProviderTests { var errorDescription: String? { "permission denied for pg_stat_user_tables" } } - private static let statisticsColumns = ["track_counts", "stats_reset", "server_version_num", "in_recovery"] + private static let statisticsColumns = ["track_counts", "stats_reset", "server_version_num", "in_recovery", "standbys"] private let provider = PostgreSQLTableHealthProvider() private let context = TableHealthFixture.postgresContext() @@ -40,19 +40,23 @@ struct PostgreSQLTableHealthProviderTests { trackCounts: String = "on", statsReset: String? = nil, version: String? = "170011", - inRecovery: String? = "f" + inRecovery: String? = "f", + standbys: String? = "0" ) -> QueryResult { - TableHealthFixture.result(columns: statisticsColumns, rows: [[trackCounts, statsReset, version, inRecovery]]) + TableHealthFixture.result(columns: statisticsColumns, rows: [[trackCounts, statsReset, version, inRecovery, standbys]]) } private func statistics( trackCounts: String = "on", statsReset: String? = nil, version: String? = "170011", - inRecovery: String? = "f" + inRecovery: String? = "f", + standbys: String? = "0" ) -> TableHealthStatistics { provider.statistics( - from: [Self.statisticsResult(trackCounts: trackCounts, statsReset: statsReset, version: version, inRecovery: inRecovery)], + from: [Self.statisticsResult( + trackCounts: trackCounts, statsReset: statsReset, version: version, inRecovery: inRecovery, standbys: standbys + )], now: Date() ) } @@ -88,14 +92,18 @@ struct PostgreSQLTableHealthProviderTests { #expect(finding.fixSQL == #"VACUUM (ANALYZE) "public"."orders";"#) } - @Test("A table never vacuumed says so and has no last vacuum value") + @Test("A table with no vacuum recorded says only that, since a crash also empties the record, and has no last vacuum value") func neverVacuumed() throws { let finding = try #require(findings( .bloat, - columns: ["schema_name", "table_name", "live_rows", "dead_rows", "last_vacuum", "table_bytes"], - rows: [["public", "orders", "2000", "3000", nil, "81920"]] + columns: ["schema_name", "table_name", "live_rows", "dead_rows", "last_vacuum", "table_bytes", "stats_reset"], + rows: [["public", "orders", "2000", "3000", nil, "81920", nil]] ).first) - #expect(finding.detail.contains("Never vacuumed")) + #expect(finding.detail == String( + format: String(localized: "%1$@ dead rows, %2$@ live. No vacuum recorded in the server's statistics."), + (3_000 as Int64).formatted(), (2_000 as Int64).formatted() + )) + #expect(!finding.detail.contains("Never")) #expect(finding.metric("last_vacuum") == nil) } @@ -202,7 +210,9 @@ struct PostgreSQLTableHealthProviderTests { #expect(statistics(statsReset: "1800000000").notes == [ String(format: String(localized: "Index usage counts since the statistics were last reset at %@."), TableHealthFormatting.date(reset)) ]) - #expect(statistics(statsReset: nil).notes.first?.contains("never been reset") == true) + #expect(statistics(statsReset: nil).notes == [String( + localized: "Index usage counts cover only what the server's statistics hold; a reset, a crash or a failover discards them." + )]) #expect(provider.statistics(from: [nil], now: Date()) == TableHealthStatistics()) } @@ -450,7 +460,7 @@ struct PostgreSQLTableHealthProviderTests { private static let sequenceColumns = [ "sequence_schema", "sequence_name", "sequence_type", "increment_by", "sequence_min", "sequence_max", - "last_value", "lower_bound", "upper_bound", "used_fraction", "readable", + "last_value", "lower_bound", "upper_bound", "used_fraction", "readable", "locked", "table_schema", "table_name", "column_name", "column_type", "column_min", "column_max", "ownership", "table_bytes", "referencing_columns" ] @@ -466,6 +476,7 @@ struct PostgreSQLTableHealthProviderTests { var upper = "2147483647" var used: String? = "0.79162418869475292852" var readable = "t" + var locked = "f" var table: String? var column: String? = "id" var columnType: String? = "integer" @@ -476,7 +487,7 @@ struct PostgreSQLTableHealthProviderTests { var cells: [String?] { [ - "public", name, type, increment, minimum, maximum, last, lower, upper, used, readable, + "public", name, type, increment, minimum, maximum, last, lower, upper, used, readable, locked, table == nil ? nil : "public", table, table == nil ? nil : column, table == nil ? nil : columnType, table == nil ? nil : columnMin, table == nil ? nil : columnMax, table == nil ? nil : ownership, table == nil ? nil : "8192", referencing @@ -747,4 +758,61 @@ struct PostgreSQLTableHealthProviderTests { #expect(provider.queries(for: .invalidIndex, statistics: TableHealthStatistics()) == [PostgreSQLTableHealthProvider.invalidIndexBefore12Query]) } + + @Test("A primary with standbys says their index scans are not counted, and a standby or a lone primary says nothing") + func standbyScansNote() { + let note = String(localized: "Index scans on this server's standbys are not counted here.") + #expect(statistics(standbys: "2").notes.contains(note)) + #expect(!statistics(standbys: "0").notes.contains(note)) + #expect(!statistics(standbys: nil).notes.contains(note)) + #expect(!statistics(inRecovery: "t", standbys: "1").notes.contains(note)) + } + + @Test("A table whose statistics this user cannot read is not called never analyzed, and the detail says why") + func unreadableStatisticsAreNotNeverAnalyzed() throws { + let columns = Self.staleColumns + ["stats_readable"] + let found = findings( + .staleStatistics, + columns: columns, + rows: [ + ["public", "st_hidden", "r", "5000", "5000", "50", "t", nil, "212992", "f"], + ["public", "st_never", "r", "5000", "5000", "50", "t", nil, "212992", "t"] + ] + ) + #expect(found.count == 2) + #expect(found[0].detail == String( + localized: "No analyze recorded in the server's statistics, and this user cannot read the table's statistics to confirm it." + )) + #expect(found[0].metric("never_analyzed") == nil) + #expect(found[1].detail == String(localized: "Never analyzed, so the planner has no statistics for it.")) + #expect(found[1].metric("never_analyzed")?.value == .flag(true)) + } + + @Test("A sequence another session holds locked is left unread and counted in the check's note") + func lockedSequencesBecomeNote() { + let locked = SequenceRow(name: "seq_busy", last: nil, used: nil, locked: "t") + let one = sequences([locked, SequenceRow(name: "seq_int_id_seq", table: "seq_int")]) + #expect(one.findings.map { $0.objectName } == ["seq_int_id_seq"]) + #expect(one.note == String(localized: "One sequence is locked by another session, so its counter was not checked.")) + + let secret = SequenceRow(name: "seq_secret", type: "smallint", last: nil, used: nil, readable: "f") + let both = sequences([locked, SequenceRow(name: "seq_busy_2", last: nil, used: nil, locked: "t"), secret]) + #expect(both.findings.isEmpty) + #expect(both.note == [ + String(localized: "One sequence is not readable by this user, so its counter was not checked."), + String(format: String(localized: "%lld sequences are locked by another session, so their counters were not checked."), 2) + ].joined(separator: " ")) + } + + @Test("Before 12, an invalid index whose table was locked, so its definition was not read, falls back to a plain rebuild") + func lockedDefinitionFallsBackToReindex() throws { + let finding = try #require(findings( + .invalidIndex, + columns: Self.invalidColumns, + rows: [["public", "inv", "inv_vw_cancelled", "f", "t", "16384", nil]], + statistics: statistics(version: "110000") + ).first) + #expect(finding.fixSQL == #"REINDEX INDEX "public"."inv_vw_cancelled";"#) + #expect(finding.metric("index_definition") == nil) + } } diff --git a/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift b/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift index e9903cc301..bf0fc3eab9 100644 --- a/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift +++ b/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift @@ -61,6 +61,94 @@ struct TableHealthFixScriptTests { #expect(text.components(separatedBy: drop).count == 2) } + private func redundant( + _ index: String, + coveredBy cover: String, + schema: String? = "public", + table: String = "orders", + fix: String? = nil + ) -> TableHealthFinding { + TableHealthFinding( + kind: .redundantIndex, schema: schema, table: table, objectName: index, detail: "", + metrics: [TableHealthMetric(id: "covering_index", label: "Covering Index", value: .text(cover))], + fixSQL: fix ?? "DROP INDEX CONCURRENTLY \"public\".\"\(index)\";" + ) + } + + private func unused(_ index: String, schema: String? = "public", table: String = "orders", fix: String? = nil) -> TableHealthFinding { + TableHealthFixture.finding( + .unusedIndex, schema: schema, table: table, object: index, + fixSQL: fix ?? "DROP INDEX CONCURRENTLY \"public\".\"\(index)\";" + ) + } + + private func comment(_ kind: TableHealthCheckKind, _ name: String) -> Substring { + Substring("-- " + String(format: String(localized: "%1$@: %2$@"), kind.title, name)) + } + + @Test("A redundant index whose covering index the script drops as unused stays, with a note, so its lookups keep an index") + func redundantIndexStaysWhenItsCoverIsDropped() { + let text = script([ + unused("orders_customer_created_idx"), + redundant("orders_customer_idx", coveredBy: "orders_customer_created_idx") + ]) + #expect(Array(lines(text).dropFirst(4)) == [ + comment(.unusedIndex, "public.orders.orders_customer_created_idx"), + #"DROP INDEX CONCURRENTLY "public"."orders_customer_created_idx";"#, + "", + comment(.redundantIndex, "public.orders.orders_customer_idx"), + Substring("-- " + String( + format: String(localized: "Not dropped: its covering index %@ is dropped in this script."), "orders_customer_created_idx" + )), + "" + ]) + #expect(!text.contains(#"DROP INDEX CONCURRENTLY "public"."orders_customer_idx";"#)) + } + + @Test("A redundant index still drops when its cover stays, when the cover is another table's, or when it is unused itself") + func redundantIndexDropsOtherwise() { + let coverStays = script([redundant("orders_customer_idx", coveredBy: "orders_customer_created_idx")]) + #expect(coverStays.contains(#"DROP INDEX CONCURRENTLY "public"."orders_customer_idx";"#)) + + let otherTable = script([ + unused("orders_customer_created_idx", table: "archive"), + redundant("orders_customer_idx", coveredBy: "orders_customer_created_idx") + ]) + #expect(otherTable.contains(#"DROP INDEX CONCURRENTLY "public"."orders_customer_idx";"#)) + + let bothUnused = script([ + unused("orders_customer_created_idx"), + unused("orders_customer_idx"), + redundant("orders_customer_idx", coveredBy: "orders_customer_created_idx") + ]) + #expect(bothUnused.components(separatedBy: #"DROP INDEX CONCURRENTLY "public"."orders_customer_idx";"#).count == 2) + #expect(!bothUnused.contains("Not dropped")) + } + + @Test("A cover the script keeps does not keep what it covers, so a chain drops down to the index that stays") + func coverChainResolves() { + let text = script([ + unused("idx_abc"), + redundant("idx_ab", coveredBy: "idx_abc"), + redundant("idx_a", coveredBy: "idx_ab") + ]) + #expect(text.contains(#"DROP INDEX CONCURRENTLY "public"."idx_abc";"#)) + #expect(!text.contains(#"DROP INDEX CONCURRENTLY "public"."idx_ab";"#)) + #expect(text.contains(#"DROP INDEX CONCURRENTLY "public"."idx_a";"#)) + } + + @Test("MySQL findings, which have no schema, keep a redundant index the same way") + func mysqlRedundantIndexStays() { + let drop = { (index: String) in "ALTER TABLE `shop`.`orders` DROP INDEX `\(index)`;" } + let text = script([ + unused("orders_customer_created_idx", schema: nil, fix: drop("orders_customer_created_idx")), + redundant("orders_customer_idx", coveredBy: "orders_customer_created_idx", schema: nil, fix: drop("orders_customer_idx")) + ]) + #expect(text.contains(drop("orders_customer_created_idx"))) + #expect(!text.contains(drop("orders_customer_idx"))) + #expect(text.contains("Not dropped")) + } + @Test("A finding without a fix is left out") func findingWithoutFixSkipped() { let text = script([ diff --git a/apple/TableProTests/Services/MainSplitViewControllerDetailWidthTests.swift b/apple/TableProTests/Services/MainSplitViewControllerDetailWidthTests.swift index 3e29154f16..aafac64e73 100644 --- a/apple/TableProTests/Services/MainSplitViewControllerDetailWidthTests.swift +++ b/apple/TableProTests/Services/MainSplitViewControllerDetailWidthTests.swift @@ -51,6 +51,14 @@ struct MainSplitViewControllerDetailWidthTests { #expect(TableHealthLayoutMetrics.checksListMinimumWidth < TableHealthLayoutMetrics.checksListMaximumWidth) } + // Measured: a 480pt window leaves a tab about 357pt under its toolbar, tab strip and pane bar. + @Test("Table Health's findings and preview minimums fit the smallest window, divider included") + func tableHealthFitsTheSmallestWindow() { + let required = TableHealthLayoutMetrics.findingsMinimumHeight + 1 + TableHealthLayoutMetrics.previewMinimumHeight + #expect(required <= 357) + #expect(MainSplitViewController.baseWindowMinHeight == 480) + } + @Test( "Every other tab type keeps the default detail minimum", arguments: [TabType.query, .table, .createTable, .erDiagram, .serverDashboard] diff --git a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift index fa32a23f92..9ca07fafbc 100644 --- a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift +++ b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift @@ -602,7 +602,24 @@ struct TableHealthViewModelTests { #expect(viewModel.rows.isEmpty) #expect(viewModel.failedCheckCount == 1) - #expect(viewModel.listState == .someChecksFailed(1)) + #expect(viewModel.listState == .someChecksFailed(failed: 1, hiddenIgnored: 0)) + } + + @Test("A failed check in All Findings still says how many ignored findings are hidden") + func failedChecksKeepTheIgnoredCount() async throws { + let metadata = HealthMetadata() + let viewModel = makeViewModel(metadata) + await viewModel.refresh() + let logs = try #require(row(viewModel, table: "logs")) + viewModel.setIgnored([logs.id], ignored: true) + + metadata.server.failsMissingKeyCheck = true + await viewModel.refresh() + + #expect(viewModel.rows.isEmpty) + #expect(viewModel.listState == .someChecksFailed(failed: 1, hiddenIgnored: 1)) + viewModel.showIgnored = true + #expect(viewModel.listState == .rows) } @Test("All Findings keeps the rows it has when a check fails, and counts the failure") diff --git a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift index f73a6ceb1c..3495695e34 100644 --- a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift +++ b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift @@ -452,6 +452,61 @@ struct MainContentCoordinatorRefreshTests { #expect(model.ledger.isIgnored(logs)) } + @Test("A table rename refreshes the Table Health tabs on its database and no other") + func tableRenameRefreshesTableHealth() async throws { + let (coordinator, tabManager) = makeCoordinator() + let shop = RefreshLeaseRecorder() + let archive = RefreshLeaseRecorder() + let shopModel = try makeTableHealthTab("shop", metadata: shop, coordinator: coordinator, tabManager: tabManager) + let archiveModel = try makeTableHealthTab("archive", metadata: archive, coordinator: coordinator, tabManager: tabManager) + await shopModel.refresh() + await archiveModel.refresh() + + coordinator.applyObjectChange(DatabaseObjectChange( + connectionId: coordinator.connectionId, + scope: DatabaseScope(connectionId: coordinator.connectionId, database: "shop", schema: "public"), + name: "orders", + kind: .renamed(to: "orders_v2") + )) + + try await waitUntil { shop.databases.count == 2 } + #expect(archive.databases == ["archive"]) + } + + @Test("A schema rename refreshes the Table Health tabs on its database") + func schemaRenameRefreshesTableHealth() async throws { + let (coordinator, tabManager) = makeCoordinator() + let shop = RefreshLeaseRecorder() + let model = try makeTableHealthTab("shop", metadata: shop, coordinator: coordinator, tabManager: tabManager) + await model.refresh() + + coordinator.applyContainerChange(DatabaseContainerChange( + connectionId: coordinator.connectionId, + container: .schema(database: "shop", schema: "billing"), + kind: .renamed(to: "finance") + )) + + try await waitUntil { shop.databases.count == 2 } + } + + private func makeTableHealthTab( + _ database: String, + metadata: RefreshLeaseRecorder, + coordinator: MainContentCoordinator, + tabManager: QueryTabManager + ) throws -> TableHealthViewModel { + let tabId = try #require(tabManager.addTableHealthTab(databaseName: database)) + let model = TableHealthViewModel( + connectionId: coordinator.connectionId, + provider: TableHealthProviderFactory.provider(for: .postgresql), + history: RefreshUnusedHistory(), + metadata: metadata, + database: { database } + ) + coordinator.tableHealthModels[tabId] = model + return model + } + @Test("Refresh on Users & Roles reloads the principals through the tab's own handler") func refreshOnUsersRolesReloads() { let (coordinator, tabManager) = makeCoordinator() diff --git a/apple/scripts/probes/check-table-health-catalogs.sh b/apple/scripts/probes/check-table-health-catalogs.sh index 167e0a1d51..764a9c6fe7 100755 --- a/apple/scripts/probes/check-table-health-catalogs.sh +++ b/apple/scripts/probes/check-table-health-catalogs.sh @@ -17,7 +17,9 @@ # # Passwords come from PGPASSWORD and MYSQL_PWD. Needs psql or mysql on PATH, and a user that may # create and drop a database; on PostgreSQL also a role, for the unreadable sequence case. Exits non-zero -# on a disagreement, 3 when it cannot run. MySQL also takes a user, for the PROCESS refusal. PostgreSQL takes about a minute: the invalid index and +# on a disagreement, 3 when it cannot run. MySQL also takes a user, for the PROCESS refusal. Every +# PostgreSQL query also runs while another session holds ACCESS EXCLUSIVE on a table it lists and on one +# it does not, and must answer without waiting. PostgreSQL takes about a minute: the invalid index and # transaction ID cases wait on held snapshots. set -uo pipefail @@ -241,6 +243,81 @@ SQL fi check_postgres_stale_statistics check_postgres_transaction_id_age + check_postgres_locks +} + +# Every query must answer while another session holds ACCESS EXCLUSIVE on a table: a size or a definition read +# from the relation itself would wait until that transaction ends. An open TRUNCATE locks the table, its TOAST +# table and every index; an open ALTER SEQUENCE blocks reading the sequence. lock_timeout in the probe's own +# session turns a wait into an error. +check_postgres_locks() { + pg -d "$DATABASE" > /dev/null <<'SQL' +CREATE TABLE lk_other (id int PRIMARY KEY); +INSERT INTO lk_other SELECT g FROM generate_series(1, 10) g; +CREATE TABLE lk_listed (id serial PRIMARY KEY, v int, w int, note text); +INSERT INTO lk_listed (v, w, note) SELECT g % 3, g, repeat('x', 3000) FROM generate_series(1, 10) g; +CREATE INDEX lk_listed_v_idx ON lk_listed (v); +CREATE INDEX lk_listed_vw_idx ON lk_listed (v, w); +SELECT setval('lk_listed_id_seq', 1700000000); +CREATE TABLE lk_nopk (v int); +CREATE TABLE lk_parent (id int PRIMARY KEY); +CREATE TABLE lk_child (id int PRIMARY KEY, parent_id int REFERENCES lk_parent (id)); +CREATE TABLE lk_bloat (id int PRIMARY KEY, v int) WITH (autovacuum_enabled = false); +INSERT INTO lk_bloat SELECT g, g FROM generate_series(1, 5000) g; +DELETE FROM lk_bloat WHERE id <= 3000; +SQL + pg -d "$DATABASE" -c "CREATE UNIQUE INDEX CONCURRENTLY lk_listed_v_uniq ON lk_listed (v)" > /dev/null 2>&1 + for _ in $(seq 1 10); do + [ "$(pg -d "$DATABASE" -c "SELECT count(*) FROM pg_stat_user_tables WHERE relname IN ('lk_bloat', 'lk_listed') AND n_live_tup > 0")" = "2" ] && break + sleep 1 + done + + local invalid=invalidIndexBefore12Query + [ "$version" -ge 120000 ] && invalid=invalidIndexQuery + # Query, then what it must still list while lk_listed and its neighbours are locked. + local cases=( + "statisticsQuery|" + "bloatQuery||lk_bloat|" + "unusedIndexQuery|lk_listed_v_idx" + "redundantIndexQuery||lk_listed|r|lk_listed_v_idx|" + "$invalid|public|lk_listed|lk_listed_v_uniq|t|f|" + "missingPrimaryKeyQuery|public|lk_nopk" + "unindexedForeignKeyQuery|lk_child_parent_id_fkey" + "staleStatisticsQuery|public|lk_listed|r|" + "transactionIdAgeQuery|" + ) + [ "$version" -ge 100000 ] && cases+=("nearLimitQuery|") + + local entry query listed output + hold_lock tablepro_health_other_lock "LOCK TABLE lk_other IN ACCESS EXCLUSIVE MODE" + for entry in "${cases[@]}"; do + query="${entry%%|*}" + output="$(pg -d "$DATABASE" -c "SET lock_timeout = '5s'" -c "$(query_of "$source" "$query")" 2>&1)" + expect "$query answers while a table it does not list is locked" "$output" "lock timeout" no + done + terminate tablepro_health_other_lock + + local sequence_lock="" + [ "$version" -ge 100000 ] && sequence_lock="; ALTER SEQUENCE lk_listed_id_seq MAXVALUE 2147483647" + hold_lock tablepro_health_listed_lock "TRUNCATE lk_listed, lk_nopk, lk_child, lk_bloat$sequence_lock" + for entry in "${cases[@]}"; do + query="${entry%%|*}" + listed="${entry#*|}" + output="$(pg -d "$DATABASE" -c "SET lock_timeout = '5s'" -c "$(query_of "$source" "$query")" 2>&1)" + expect "$query answers while the tables it lists, their indexes and TOAST tables are locked" "$output" "lock timeout" no + if [ -n "$listed" ]; then + expect "$query still lists the locked table" "$output" "$listed" yes + fi + done + output="$(pg -d "$DATABASE" -c "SET lock_timeout = '5s'" -c "$(query_of "$source" "$invalid")" 2>&1)" + expect_pattern "an invalid index on a locked table comes back without the definition that would wait" "$output" \ + "lk_listed_v_uniq\|t\|f\|[0-9]*\|$" + if [ "$version" -ge 100000 ]; then + output="$(pg -d "$DATABASE" -c "SET lock_timeout = '5s'" -c "$(query_of "$source" nearLimitQuery)" 2>&1)" + expect "a sequence an open ALTER SEQUENCE holds comes back unread and marked locked" "$output" \ + "public|lk_listed_id_seq|integer|1|1|2147483647||1|2147483647||t|t|" yes + fi + terminate tablepro_health_listed_lock } cleanup_postgres() { @@ -256,6 +333,18 @@ terminate() { pg -d postgres -c "SELECT pg_catalog.pg_terminate_backend(pid) FROM pg_catalog.pg_stat_activity WHERE application_name = '$1'" > /dev/null } +# Runs $2 in a background transaction under application name $1 and holds its locks until terminated, once the +# server shows one of them granted. +hold_lock() { + PGAPPNAME="$1" psql -X -q -h "$HOST" -p "$PORT" -U "$USER_NAME" -d "$DATABASE" \ + -c "BEGIN; $2; SELECT pg_sleep(120); COMMIT;" > /dev/null 2>&1 & + for _ in $(seq 1 20); do + [ "$(pg -d postgres -c "SELECT count(*) > 0 FROM pg_locks l JOIN pg_stat_activity a ON a.pid = l.pid WHERE a.application_name = '$1' AND l.mode = 'AccessExclusiveLock' AND l.granted")" = "t" ] && return + sleep 0.25 + done + echo "note the locks held as $1 did not appear" >&2 +} + # Holds a REPEATABLE READ snapshot in the background under application name $1 for $2 seconds, and waits # until the server shows it. hold_snapshot() { @@ -382,15 +471,17 @@ SQL expect "index shape reads an index a foreign key uses as protected" "$shape" "public|rk|r|k_rk_z_fkref|btree|t|f|t|false|t|" yes expect "index shape reads a deferrable constraint as not immediate" "$shape" "public|rd|r|k_rd_v_deferrable|btree|t|f|f|false|t|" yes expect "index shape reads the replica identity index as protected" "$shape" "public|rr|r|rr_zz_replident|btree|t|f|t|false|t|" yes - expect "index shape reads a predicate" "$shape" "public|ri|r|r_a_partial_dup|btree|f|f|t|false|f|(a > 0)|" yes - expect "index shape deparses an expression key" "$shape" "|1|1|lower(email)|" yes - expect "index shape reads a descending key's sort flags" "$shape" "|1|1|a|1978|0|3" yes + expect "index shape reads a predicate as node text" "$shape" "public|ri|r|r_a_partial_dup|btree|f|f|t|false|f|{OPEXPR :opno 521 " yes + expect "index shape reads an expression key as node text in its index's list" "$shape" "|k_lower_email|btree|f|f|t|false|f||({FUNCEXPR :funcid 870 " yes + expect "index shape gives an expression key attnum 0 and no column" "$shape" "|1|1|0||3126|100|0" yes + expect "index shape drops the :location offsets that differ with where a statement wrote an expression" "$shape" ":location" no + expect "index shape reads a descending key's column and sort flags" "$shape" "|1|1|2|a|1978|0|3" yes expect "index shape reads a materialized view's index" "$shape" "public|rmv|m|r_rmv_a|btree|" yes expect "index shape reads a GIN index's method" "$shape" "public|ri|r|k_note_gin|gin|" yes if [ "$version" -ge 110000 ]; then - expect "index shape reads an INCLUDE column after the key count" "$shape" "k_a_incl_c|btree|f|f|t|false|f||8192||1|2|c|||" yes + expect "index shape reads an INCLUDE column after the key count" "$shape" "k_a_incl_c|btree|f|f|t|false|f|||8192|1|2|4|c|||" yes expect "index shape reads an attached partition index as protected" "$shape" "public|rp_2026|r|rp_2026_k_at_idx|btree|f|f|t|false|t|" yes - expect "index shape reads a partitioned table's index" "$shape" "public|rp|p|r_rp_k|btree|f|f|t|false|f||0|" yes + expect "index shape reads a partitioned table's index" "$shape" "public|rp|p|r_rp_k|btree|f|f|t|false|f|||0|" yes fi if [ "$version" -ge 150000 ]; then expect "index shape reads NULLS NOT DISTINCT" "$shape" "public|ri|r|k_nnd|btree|t|f|t|true|f|" yes @@ -646,6 +737,17 @@ SQL done stale="$(pg -d "$DATABASE" -c "$(query_of "$source" staleStatisticsQuery)")" expect "stale statistics trust pg_stats over counters a reset emptied" "$stale" "|st_stale|" no + # pg_stats hides a table's rows from a role without SELECT on it, and under row-level security. + if [ "$(pg -d "$DATABASE" -c "SELECT count(*) FROM pg_roles WHERE rolname = '$READER_ROLE'")" = "1" ]; then + stale="$(pg -d "$DATABASE" -c "SET ROLE $READER_ROLE" -c "$(query_of "$source" staleStatisticsQuery)")" + expect_pattern "without SELECT the counters decide, and the row says pg_stats could not be read" "$stale" \ + "\|st_stale\|r\|[0-9]+\|[0-9]+\|[0-9]+\|t\|f\|" + pg -d "$DATABASE" -c "GRANT SELECT ON st_stale TO $READER_ROLE" -c "ALTER TABLE st_stale ENABLE ROW LEVEL SECURITY" > /dev/null + stale="$(pg -d "$DATABASE" -c "SET ROLE $READER_ROLE" -c "$(query_of "$source" staleStatisticsQuery)")" + expect_pattern "under row-level security the counters decide, and the row says pg_stats could not be read" "$stale" \ + "\|st_stale\|r\|[0-9]+\|[0-9]+\|[0-9]+\|t\|f\|" + pg -d "$DATABASE" -c "ALTER TABLE st_stale DISABLE ROW LEVEL SECURITY" -c "REVOKE SELECT ON st_stale FROM $READER_ROLE" > /dev/null + fi else echo "note this user cannot call pg_stat_reset(), so the emptied counters case is skipped" fi @@ -670,6 +772,11 @@ SQL expect "transaction ID age skips a table under the server's freeze age" "$ages" "xid_young" no expect "transaction ID age skips an extension's table past its freeze age" "$ages" "ext_owned" no expect "transaction ID age skips a table in an extension's schema past its freeze age" "$ages" "ext_schema" no + hold_lock tablepro_health_xid_lock "LOCK TABLE xid_old IN ACCESS EXCLUSIVE MODE" + ages="$(pg -d "$DATABASE" -c "SET lock_timeout = '5s'" -c "$(query_of "$source" transactionIdAgeQuery)" 2>&1)" + expect "transaction ID age answers while the table it lists is locked" "$ages" "lock timeout" no + expect "transaction ID age still lists the locked table" "$ages" "public|xid_old|" yes + terminate tablepro_health_xid_lock terminate tablepro_health_xid # The fix exactly as PostgreSQLTableHealthProviderTests pins it. From 50bc1177989a1a5397b09ae98190a63232571bc6 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Mon, 9 Nov 2026 23:59:53 +0700 Subject: [PATCH 05/13] docs(changelog): Table Health rebuild entries --- apple/changelog.d/4167.added.1.md | 1 + apple/changelog.d/4167.added.2.md | 1 + apple/changelog.d/4167.added.3.md | 1 + apple/changelog.d/4167.added.4.md | 1 + apple/changelog.d/4167.added.md | 1 + apple/changelog.d/4167.changed.md | 1 + apple/changelog.d/4167.fixed.md | 1 + 7 files changed, 7 insertions(+) create mode 100644 apple/changelog.d/4167.added.1.md create mode 100644 apple/changelog.d/4167.added.2.md create mode 100644 apple/changelog.d/4167.added.3.md create mode 100644 apple/changelog.d/4167.added.4.md create mode 100644 apple/changelog.d/4167.added.md create mode 100644 apple/changelog.d/4167.changed.md create mode 100644 apple/changelog.d/4167.fixed.md diff --git a/apple/changelog.d/4167.added.1.md b/apple/changelog.d/4167.added.1.md new file mode 100644 index 0000000000..50fc23847f --- /dev/null +++ b/apple/changelog.d/4167.added.1.md @@ -0,0 +1 @@ +Table Health marks new findings, keeps resolved ones and lets you ignore a finding, per database. diff --git a/apple/changelog.d/4167.added.2.md b/apple/changelog.d/4167.added.2.md new file mode 100644 index 0000000000..e15ff4216c --- /dev/null +++ b/apple/changelog.d/4167.added.2.md @@ -0,0 +1 @@ +Table Health checks redundant and invalid indexes, values near their limit, transaction ID age and stale statistics. diff --git a/apple/changelog.d/4167.added.3.md b/apple/changelog.d/4167.added.3.md new file mode 100644 index 0000000000..0888ab58c7 --- /dev/null +++ b/apple/changelog.d/4167.added.3.md @@ -0,0 +1 @@ +Table Health finds reclaimable space on MySQL and MariaDB. diff --git a/apple/changelog.d/4167.added.4.md b/apple/changelog.d/4167.added.4.md new file mode 100644 index 0000000000..d4e168c2d3 --- /dev/null +++ b/apple/changelog.d/4167.added.4.md @@ -0,0 +1 @@ +The get_table_health MCP tool returns Table Health findings to a connected client. diff --git a/apple/changelog.d/4167.added.md b/apple/changelog.d/4167.added.md new file mode 100644 index 0000000000..e5c0256bd1 --- /dev/null +++ b/apple/changelog.d/4167.added.md @@ -0,0 +1 @@ +Table Health exports findings to CSV, Excel, JSON and other formats, copies them, and saves every fix as one script. diff --git a/apple/changelog.d/4167.changed.md b/apple/changelog.d/4167.changed.md new file mode 100644 index 0000000000..6b5d68ddd3 --- /dev/null +++ b/apple/changelog.d/4167.changed.md @@ -0,0 +1 @@ +Table Health opens one tab per database and stays on it when the sidebar switches database. diff --git a/apple/changelog.d/4167.fixed.md b/apple/changelog.d/4167.fixed.md new file mode 100644 index 0000000000..a3da25c133 --- /dev/null +++ b/apple/changelog.d/4167.fixed.md @@ -0,0 +1 @@ +Table Health reported a PostgreSQL standby's activity counters and objects an extension owns. From b0c3c13bde49651952e88c366b6a6146bd6c8646 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Tue, 10 Nov 2026 00:33:14 +0700 Subject: [PATCH 06/13] fix(table-health): review round 3 findings --- .../DatabaseManager+ScopedDriver.swift | 9 +- .../MCP/MCPConnectionBridge+TableHealth.swift | 41 +++++-- .../Protocol/Tools/GetTableHealthTool.swift | 14 ++- .../PostgreSQLTableHealthProvider.swift | 64 ++++++---- .../TableHealth/TableHealthFixScript.swift | 45 +++++--- .../TableHealth/TableHealthReportLoader.swift | 28 ++++- .../TablePro/Resources/Localizable.xcstrings | 21 ++++ .../ViewModels/TableHealthViewModel.swift | 19 ++- ...ontentCoordinator+TableHealthActions.swift | 2 +- .../TableHealth/TableHealthPreviewPane.swift | 10 +- .../Tools/GetTableHealthToolTests.swift | 88 +++++++++++--- .../PostgreSQLTableHealthProviderTests.swift | 24 +++- .../TableHealthFixScriptTests.swift | 14 +++ .../TableHealthReportLoaderTests.swift | 101 ++++++++++++++++ .../TableHealthViewModelTests.swift | 24 +++- .../MainContentCoordinatorRefreshTests.swift | 6 +- .../probes/check-table-health-catalogs.sh | 109 ++++++++++++++---- 17 files changed, 511 insertions(+), 108 deletions(-) create mode 100644 apple/TableProTests/Core/TableHealth/TableHealthReportLoaderTests.swift diff --git a/apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift b/apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift index 5f3389174b..188d59c552 100644 --- a/apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift +++ b/apple/TablePro/Core/Database/DatabaseManager+ScopedDriver.swift @@ -112,17 +112,20 @@ extension DatabaseManager { } } - /// A read that stops on the server once the task awaiting it is cancelled. It runs under a lease - /// owner of its own, so the cancel reaches this read and nothing else on the connection. + // A read that stops on the server once the task awaiting it is cancelled. It runs under a lease owner of its own, + // so the cancel reaches this read and nothing else on the connection. func withCancellableRead( scope: DatabaseScope, route: ScopedDriverRoute, + workload: MetadataConnectionPool.Workload = .interactive, _ body: @Sendable @escaping (DatabaseDriver) async throws -> T ) async throws -> T { let owner = DriverLeaseOwner() let connectionId = scope.connectionId return try await withTaskCancellationHandler { - try await withScopedDriver(scope: scope, route: route, cancellation: .cancellableRead(owner), body) + try await withScopedDriver( + scope: scope, route: route, workload: workload, cancellation: .cancellableRead(owner), body + ) } onCancel: { Task { @MainActor in try? DatabaseManager.shared.cancelRunningQuery(owner: owner, on: connectionId, delivery: .background) diff --git a/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift b/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift index f63c1adcf6..591ef341eb 100644 --- a/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift +++ b/apple/TablePro/Core/MCP/MCPConnectionBridge+TableHealth.swift @@ -20,13 +20,13 @@ extension MCPConnectionBridge { // The checks cover every schema, as the Table Health tab does. let reportScope = DatabaseScope(connectionId: scope.connectionId, database: scope.database, schema: nil) let report = try await Self.withinTableHealthTimeout(seconds: timeoutSeconds) { - try await TableHealthReportLoader.load(provider: provider, scope: reportScope, metadata: DatabaseManager.shared) + try await TableHealthReportLoader.load(provider: provider, scope: reportScope, lease: .cancellable) } return Self.tableHealthPayload(report, ledger: ledger) } - // Answers at the deadline and lets the checks wind down after it: a read queued on the pool's serial lane sees its - // cancel only when the job ahead of it ends, and a catalog read already running cannot be stopped on the server. + // Answers at the deadline and cancels the checks without waiting for them: a read queued on the pool's serial lane + // sees its cancel only when the job ahead of it ends, and a running statement ends once the server takes the cancel. static func withinTableHealthTimeout( seconds: Int, _ body: @Sendable @escaping () async throws -> T @@ -76,14 +76,21 @@ extension MCPConnectionBridge { } } + // A client that runs every fix_sql it gets would drop a used index along with the unused index covering it, so the + // fixes the Save Fix Script leaves out are left out here too. static func tableHealthPayload(_ report: TableHealthReport, ledger: TableHealthLedger) -> JsonValue { - .object([ + let withheld = TableHealthFixScript.withheldFixes(in: report.findings) + return .object([ "database": .string(report.database), "generated_at": .string(iso8601.withLockUnchecked { $0.string(from: report.generatedAt) }), "notes": .array(report.notes.map { .string($0) }), "checks": .array(report.checks.map(tableHealthCheckPayload)), "findings": .array(report.findings.map { finding in - tableHealthFindingPayload(finding, status: ledger.mcpStatus(of: finding.id)) + tableHealthFindingPayload( + finding, + status: ledger.mcpStatus(of: finding.id), + withholdsFix: withheld.contains(finding.id) + ) }) ]) } @@ -120,7 +127,11 @@ extension MCPConnectionBridge { return String(localized: "The server did not answer this check.") } - static func tableHealthFindingPayload(_ finding: TableHealthFinding, status: TableHealthFindingStatus) -> JsonValue { + static func tableHealthFindingPayload( + _ finding: TableHealthFinding, + status: TableHealthFindingStatus, + withholdsFix: Bool + ) -> JsonValue { var fields: [String: JsonValue] = [ "kind": .string(finding.kind.rawValue), "detail": .string(finding.detail), @@ -139,12 +150,28 @@ extension MCPConnectionBridge { if let sizeBytes = finding.sizeBytes { fields["size_bytes"] = .int(Int(sizeBytes)) } - if let fixSQL = finding.fixSQL { + if withholdsFix { + fields["fix_withheld_reason"] = .string(withheldFixReason(finding)) + } else if let fixSQL = finding.fixSQL { fields["fix_sql"] = .string(fixSQL) } return .object(fields) } + static func withheldFixReason(_ finding: TableHealthFinding) -> String { + guard case .text(let cover)? = finding.metric("covering_index")?.value else { + return String( + localized: "Withheld: another finding's fix drops the index that covers this one, and dropping both leaves these lookups with no index." + ) + } + return String( + format: String( + localized: "Withheld: another finding's fix drops %@, the index that covers this one, and dropping both leaves these lookups with no index." + ), + cover + ) + } + static func tableHealthMetricPayload(_ metric: TableHealthMetric) -> JsonValue { let type: String let value: JsonValue diff --git a/apple/TablePro/Core/MCP/Protocol/Tools/GetTableHealthTool.swift b/apple/TablePro/Core/MCP/Protocol/Tools/GetTableHealthTool.swift index 03de73f3df..e07ccf8631 100644 --- a/apple/TablePro/Core/MCP/Protocol/Tools/GetTableHealthTool.swift +++ b/apple/TablePro/Core/MCP/Protocol/Tools/GetTableHealthTool.swift @@ -71,7 +71,12 @@ public struct GetTableHealthTool: MCPToolImplementation { ), "detail": MCPToolSchema.string(String(localized: "One-line description")), "size_bytes": MCPToolSchema.integer( - String(localized: "On-disk size of what the fix acts on: the index for an index check, the table otherwise") + String( + localized: """ + Estimated size of what the fix acts on: the index for an index check, the table \ + otherwise. On PostgreSQL it is as of the last VACUUM, ANALYZE or index build. + """ + ) ), "metrics": MCPToolSchema.array( String(localized: "Measured values"), @@ -93,7 +98,12 @@ public struct GetTableHealthTool: MCPToolImplementation { ) ), "fix_sql": MCPToolSchema.string( - String(localized: "SQL that fixes the finding, absent when there is none. Review it before running it.") + String( + localized: "SQL that fixes the finding, absent when there is none or it is withheld. Review it before running it." + ) + ), + "fix_withheld_reason": MCPToolSchema.string( + String(localized: "Why fix_sql is left out: running it with the other fixes would do harm") ), "status": MCPToolSchema.string( String(localized: "new: not seen in the last run the Table Health tab recorded; ignored: the user accepted it"), diff --git a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift index ec4e9f35c8..367347c1fc 100644 --- a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift +++ b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift @@ -5,8 +5,10 @@ import Foundation // members themselves, but their schema is, and a fix there would change the extension's own objects. // No query opens a relation another session may hold locked: the size functions and the index and expression // deparsers wait behind an ACCESS EXCLUSIVE lock (an open ALTER TABLE, TRUNCATE or VACUUM FULL) until it ends. Sizes -// are relpages times block_size, as of the last VACUUM, ANALYZE or index build; on 14 and later a table never measured -// has reltuples -1 and no size. +// are relpages times block_size, as of the last VACUUM, ANALYZE or index build. A relation with no pages recorded has +// no size rather than 0: on 10 to 13 a table never vacuumed or analyzed reads relpages 0 and reltuples 0, the same as +// an empty one. Before 13 ANALYZE leaves the TOAST table's relpages alone, so there a table's TOAST share is as of its +// last VACUUM. struct PostgreSQLTableHealthProvider: TableHealthProvider { let checks: [TableHealthCheckKind] = [ .bloat, .unusedIndex, .redundantIndex, .invalidIndex, .missingPrimaryKey, .unindexedForeignKey, @@ -16,12 +18,18 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { // The checks that read activity counters, which a standby keeps for its own queries only. private static let counterChecks: [TableHealthCheckKind] = [.bloat, .unusedIndex, .staleStatistics] + // A physical standby's walsender has no database; a logical one (a subscription, a CDC reader) has one, and a + // base backup's reports state backup. Without pg_read_all_stats the state reads NULL, which still counts. static let statisticsQuery = """ SELECT pg_catalog.current_setting('track_counts') AS track_counts, pg_catalog.date_part('epoch', d.stats_reset) AS stats_reset, pg_catalog.current_setting('server_version_num') AS server_version_num, pg_catalog.pg_is_in_recovery() AS in_recovery, - (SELECT pg_catalog.count(*) FROM pg_catalog.pg_stat_replication) AS standbys + (SELECT pg_catalog.count(*) + FROM pg_catalog.pg_stat_replication r + JOIN pg_catalog.pg_stat_activity a ON a.pid = r.pid + WHERE a.datid IS NULL + AND r.state IS DISTINCT FROM 'backup') AS standbys FROM pg_catalog.pg_stat_database d WHERE d.datname = pg_catalog.current_database() """ @@ -35,7 +43,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { s.n_live_tup AS live_rows, s.n_dead_tup AS dead_rows, pg_catalog.date_part('epoch', GREATEST(s.last_vacuum, s.last_autovacuum)) AS last_vacuum, - CASE WHEN c.reltuples >= 0::pg_catalog.float4 + CASE WHEN c.relpages > 0 THEN (c.relpages::pg_catalog.int8 + COALESCE(tc.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 END AS table_bytes, (SELECT pg_catalog.date_part('epoch', d.stats_reset) @@ -72,7 +80,9 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { SELECT s.schemaname AS schema_name, s.relname AS table_name, s.indexrelname AS index_name, - ic.relpages::pg_catalog.int8 * pg_catalog.current_setting('block_size')::pg_catalog.int8 AS index_bytes + CASE WHEN ic.relpages > 0 + THEN ic.relpages::pg_catalog.int8 * pg_catalog.current_setting('block_size')::pg_catalog.int8 + END AS index_bytes FROM pg_catalog.pg_stat_user_indexes s JOIN pg_catalog.pg_index i ON i.indexrelid = s.indexrelid JOIN pg_catalog.pg_class ic ON ic.oid = s.indexrelid @@ -115,7 +125,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { SELECT n.nspname AS schema_name, c.relname AS table_name, GREATEST(c.reltuples, 0)::pg_catalog.int8 AS estimated_rows, - CASE WHEN c.reltuples >= 0::pg_catalog.float4 + CASE WHEN c.relpages > 0 THEN (c.relpages::pg_catalog.int8 + COALESCE(tc.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 END AS table_bytes FROM pg_catalog.pg_class c @@ -161,7 +171,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { rn.nspname AS referenced_schema, rc.relname AS referenced_table, GREATEST(c.reltuples, 0)::pg_catalog.int8 AS estimated_rows, - CASE WHEN c.reltuples >= 0::pg_catalog.float4 + CASE WHEN c.relpages > 0 THEN (c.relpages::pg_catalog.int8 + COALESCE(tc.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 END AS table_bytes FROM pg_catalog.pg_constraint con @@ -220,7 +230,9 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { CASE WHEN p.position = 1 THEN pg_catalog.regexp_replace(i.indexprs::pg_catalog.text, ' :location -?[0-9]+', '', 'g') END AS expressions, - ic.relpages::pg_catalog.int8 * pg_catalog.current_setting('block_size')::pg_catalog.int8 AS index_bytes, + CASE WHEN ic.relpages > 0 + THEN ic.relpages::pg_catalog.int8 * pg_catalog.current_setting('block_size')::pg_catalog.int8 + END AS index_bytes, k.key_count, p.position, (i.indkey::pg_catalog.int2[])[p.position - 1] AS attnum, @@ -270,14 +282,16 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { c.relname AS index_name, i.indisunique AS is_unique, i.indisready AS is_ready, - c.relpages::pg_catalog.int8 * pg_catalog.current_setting('block_size')::pg_catalog.int8 AS index_bytes, + CASE WHEN c.relpages > 0 + THEN c.relpages::pg_catalog.int8 * pg_catalog.current_setting('block_size')::pg_catalog.int8 + END AS index_bytes, CASE WHEN NOT EXISTS ( SELECT 1 FROM pg_catalog.pg_locks l WHERE l.locktype = 'relation' AND l.database = (SELECT d.oid FROM pg_catalog.pg_database d WHERE d.datname = pg_catalog.current_database()) AND l.relation = i.indrelid AND l.mode = 'AccessExclusiveLock' - AND l.pid <> pg_catalog.pg_backend_pid() + AND l.pid IS DISTINCT FROM pg_catalog.pg_backend_pid() ) THEN pg_catalog.pg_get_indexdef(i.indexrelid) END AS index_definition @@ -319,14 +333,16 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { c.relname AS index_name, i.indisunique AS is_unique, i.indisready AS is_ready, - c.relpages::pg_catalog.int8 * pg_catalog.current_setting('block_size')::pg_catalog.int8 AS index_bytes, + CASE WHEN c.relpages > 0 + THEN c.relpages::pg_catalog.int8 * pg_catalog.current_setting('block_size')::pg_catalog.int8 + END AS index_bytes, CASE WHEN NOT EXISTS ( SELECT 1 FROM pg_catalog.pg_locks l WHERE l.locktype = 'relation' AND l.database = (SELECT d.oid FROM pg_catalog.pg_database d WHERE d.datname = pg_catalog.current_database()) AND l.relation = i.indrelid AND l.mode = 'AccessExclusiveLock' - AND l.pid <> pg_catalog.pg_backend_pid() + AND l.pid IS DISTINCT FROM pg_catalog.pg_backend_pid() ) THEN pg_catalog.pg_get_indexdef(i.indexrelid) END AS index_definition @@ -406,7 +422,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { WHERE l.locktype = 'relation' AND l.database = (SELECT d.oid FROM pg_catalog.pg_database d WHERE d.datname = pg_catalog.current_database()) AND l.mode IN ('ShareLock', 'ShareRowExclusiveLock', 'ExclusiveLock', 'AccessExclusiveLock') - AND l.pid <> pg_catalog.pg_backend_pid() + AND l.pid IS DISTINCT FROM pg_catalog.pg_backend_pid() ) lk ON lk.relation = s.oid CROSS JOIN LATERAL ( SELECT pg_catalog.has_sequence_privilege(s.oid, 'SELECT,USAGE') AS readable, @@ -423,7 +439,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { a.attname AS column_name, pg_catalog.format_type(a.atttypid, a.atttypmod) AS column_type, f.ownership, - CASE WHEN t.reltuples >= 0::pg_catalog.float4 + CASE WHEN t.relpages > 0 THEN (t.relpages::pg_catalog.int8 + COALESCE(tt.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 END AS table_bytes, CASE WHEN ty.base OPERATOR(pg_catalog.=) 'pg_catalog.int2'::pg_catalog.regtype THEN -32768::pg_catalog.int8 @@ -511,7 +527,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { f.freeze_max_age, 2147483648 - GREATEST(pg_catalog.age(c.relfrozenxid), COALESCE(pg_catalog.age(t.relfrozenxid), 0)) AS transactions_left, pg_catalog.date_part('epoch', GREATEST(s.last_vacuum, s.last_autovacuum)) AS last_vacuum, - CASE WHEN c.reltuples >= 0::pg_catalog.float4 + CASE WHEN c.relpages > 0 THEN (c.relpages::pg_catalog.int8 + COALESCE(t.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 END AS table_bytes FROM pg_catalog.pg_class c @@ -570,7 +586,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { END AS never_analyzed, sr.stats_readable, pg_catalog.date_part('epoch', GREATEST(s.last_analyze, s.last_autoanalyze)) AS last_analyze, - CASE WHEN c.reltuples >= 0::pg_catalog.float4 + CASE WHEN c.relpages > 0 THEN (c.relpages::pg_catalog.int8 + COALESCE(tc.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 END AS table_bytes FROM pg_catalog.pg_class c @@ -797,7 +813,7 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { let size = ByteSizeFormatting.string(byteString: row["index_bytes"]) return TableHealthFinding( kind: .unusedIndex, schema: schema, table: row["table_name"], objectName: index, - detail: String(format: String(localized: "%1$@ has never been scanned. It takes %2$@."), index, size), + detail: String(format: String(localized: "%1$@ has no scans in the server's statistics. It takes %2$@."), index, size), sizeBytes: row.int64("index_bytes"), fixSQL: "DROP INDEX CONCURRENTLY \(context.qualified(schema, index));" ) @@ -930,7 +946,7 @@ extension PostgreSQLTableHealthProvider { let leftover = index.range(of: "_cc(new|old)[0-9]*$", options: .regularExpression).map { String(index[$0]) } let detail: String - let fixSQL: String + let fixSQL: String? if let leftover, serverVersion.map({ $0 >= 120_000 }) ?? true { detail = leftover.hasPrefix("_ccold") ? String(format: String( @@ -953,14 +969,22 @@ extension PostgreSQLTableHealthProvider { if isUnique { sentences.append(String(localized: "Rebuilding a unique index fails while the table holds duplicate values.")) } - detail = sentences.joined(separator: " ") + // Before 12 the only rebuild that keeps writes going is a drop and a concurrent build from the definition, + // which the query leaves out while another session holds the table. A plain REINDEX would block writes + // for the whole rebuild, so there is no fix until a refresh can read the definition. if canReindexConcurrently { fixSQL = "REINDEX INDEX CONCURRENTLY \(target);" } else if let create = definition.flatMap(Self.concurrentCreate) { fixSQL = "DROP INDEX CONCURRENTLY \(target);\n\(create);" } else { - fixSQL = "REINDEX INDEX \(target);" + fixSQL = nil + if definition == nil { + sentences.append(String( + localized: "Another session held its table locked, so its definition could not be read. Refresh once the lock is gone for a fix." + )) + } } + detail = sentences.joined(separator: " ") } var metrics: [TableHealthMetric] = [] diff --git a/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift b/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift index 9485ddf0a7..4da1a28dcb 100644 --- a/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift +++ b/apple/TablePro/Core/TableHealth/TableHealthFixScript.swift @@ -13,11 +13,8 @@ enum TableHealthFixScript { commentLine(String(format: String(localized: "Generated: %@"), date.formatted(.iso8601))), commentLine(String(localized: "Run each statement on its own; some cannot run inside a transaction.")) ].joined(separator: "\n") - let withFixes = findings.filter { finding in - guard let fix = finding.fixSQL else { return false } - return !fix.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty - } - let kept = keptRedundantIndexes(withFixes) + let withFixes = findings.filter(hasFix) + let withheld = withheldFixes(in: withFixes) // An index both unused and redundant has one drop for two findings; run twice, the second fails. The fix // runs once, under the comment line of every finding it fixes. var blocks: [(comments: [String], fix: String?)] = [] @@ -25,8 +22,10 @@ enum TableHealthFixScript { for finding in withFixes { guard let fix = finding.fixSQL else { continue } let name = commentLine(String(format: String(localized: "%1$@: %2$@"), checkTitle(finding.kind), finding.qualifiedName)) - if let cover = kept[finding.id] { - let note = String(format: String(localized: "Not dropped: its covering index %@ is dropped in this script."), cover) + if withheld.contains(finding.id) { + let note = String( + format: String(localized: "Not dropped: its covering index %@ is dropped in this script."), coveringIndex(of: finding) ?? "" + ) blocks.append((comments: [name, commentLine(note)], fix: nil)) continue } @@ -53,21 +52,31 @@ enum TableHealthFixScript { let name: String } - // A redundant index whose covering index this script drops too stays, or the script would drop both and leave - // the lookups they serve with no index: an unused composite covering a single-column index that queries use is - // the common case. Returns each kept finding with its covering index's name. - private static func keptRedundantIndexes(_ findings: [TableHealthFinding]) -> [TableHealthFindingID: String] { + private static func hasFix(_ finding: TableHealthFinding) -> Bool { + guard let fix = finding.fixSQL else { return false } + return !fix.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty + } + + private static func coveringIndex(of finding: TableHealthFinding) -> String? { + guard case .text(let cover)? = finding.metric("covering_index")?.value else { return nil } + return cover + } + + // A redundant index whose covering index another finding in the list drops stays, or applying both would drop + // the two and leave the lookups they serve with no index: an unused composite covering a single-column index + // that queries use is the common case. Only a finding with a fix drops anything. + static func withheldFixes(in findings: [TableHealthFinding]) -> Set { var unusedDrops: Set = [] - var redundant: [IndexKey: (id: TableHealthFindingID, cover: IndexKey, coverName: String)] = [:] - for finding in findings { + var redundant: [IndexKey: (id: TableHealthFindingID, cover: IndexKey)] = [:] + for finding in findings where hasFix(finding) { guard let name = finding.objectName else { continue } let key = IndexKey(schema: finding.schema, table: finding.table, name: name) switch finding.kind { case .unusedIndex: unusedDrops.insert(key) case .redundantIndex: - guard case .text(let cover)? = finding.metric("covering_index")?.value else { continue } - redundant[key] = (finding.id, IndexKey(schema: finding.schema, table: finding.table, name: cover), cover) + guard let cover = coveringIndex(of: finding) else { continue } + redundant[key] = (finding.id, IndexKey(schema: finding.schema, table: finding.table, name: cover)) default: continue } @@ -79,11 +88,11 @@ enum TableHealthFixScript { guard let entry = redundant[key], !visited.contains(key) else { return false } return !isDropped(entry.cover, visited: visited.union([key])) } - var kept: [TableHealthFindingID: String] = [:] + var withheld: Set = [] for (key, entry) in redundant where !unusedDrops.contains(key) && isDropped(entry.cover, visited: [key]) { - kept[entry.id] = entry.coverName + withheld.insert(entry.id) } - return kept + return withheld } // A name can hold any character. Flattened and with its comment markers broken, it cannot end the comment early or diff --git a/apple/TablePro/Core/TableHealth/TableHealthReportLoader.swift b/apple/TablePro/Core/TableHealth/TableHealthReportLoader.swift index 863ec73a83..324e0a4465 100644 --- a/apple/TablePro/Core/TableHealth/TableHealthReportLoader.swift +++ b/apple/TablePro/Core/TableHealth/TableHealthReportLoader.swift @@ -11,14 +11,38 @@ enum TableHealthError: LocalizedError, Equatable { // The one read the Table Health tab and the MCP tool share. The caller fixes the scope, so a report never // follows the sidebar to another database. enum TableHealthReportLoader { + // How the report reaches a driver. + struct Lease: Sendable { + let read: @Sendable ( + DatabaseScope, + @escaping @Sendable (DatabaseDriver) async throws -> TableHealthReport + ) async throws -> TableHealthReport + + // The bulk lane under a lease owner of its own: cancelling the task that awaits the report stops the + // statement on the server, so a closed tab or an MCP timeout frees the lane for the next caller. + static var cancellable: Lease { + Lease { scope, body in + let manager = await MainActor.run { DatabaseManager.shared } + let route = await manager.metadataRoute(for: scope) + return try await manager.withCancellableRead(scope: scope, route: route, workload: .bulk, body) + } + } + + static func metadata(_ metadata: any ScopedMetadataProviding) -> Lease { + Lease { scope, body in + try await metadata.withMetadataDriver(scope: scope, workload: .bulk, body) + } + } + } + static func load( provider: any TableHealthProvider, scope: DatabaseScope, - metadata: any ScopedMetadataProviding, + lease: Lease, now: Date = Date() ) async throws -> TableHealthReport { guard !scope.isServerScoped else { throw TableHealthError.noDatabase } - return try await metadata.withMetadataDriver(scope: scope, workload: .bulk) { driver in + return try await lease.read(scope) { driver in let context = TableHealthQueryContext( database: scope.database, quote: { driver.quoteIdentifier($0) }, diff --git a/apple/TablePro/Resources/Localizable.xcstrings b/apple/TablePro/Resources/Localizable.xcstrings index 104232adb4..d9bfe40505 100644 --- a/apple/TablePro/Resources/Localizable.xcstrings +++ b/apple/TablePro/Resources/Localizable.xcstrings @@ -7182,6 +7182,9 @@ } } } + }, + "%1$@ has no scans in the server's statistics. It takes %2$@." : { + }, "%1$@ has type %2$@, which MongoDB does not have. Choose a type from the list." : { "extractionState" : "manual", @@ -25455,6 +25458,9 @@ }, "Another selected row adds the same query." : { + }, + "Another session held its table locked, so its definition could not be read. Refresh once the lock is gone for a fix." : { + }, "Another source column is mapped to the same destination column." : { "localizations" : { @@ -74297,6 +74303,9 @@ } } } + }, + "Estimated size of what the fix acts on: the index for an index check, the table otherwise. On PostgreSQL it is as of the last VACUUM, ANALYZE or index build." : { + }, "Estimated Startup Cost" : { "localizations" : { @@ -172335,6 +172344,9 @@ } } } + }, + "SQL that fixes the finding, absent when there is none or it is withheld. Review it before running it." : { + }, "SQL that fixes the finding, absent when there is none. Review it before running it." : { @@ -216806,6 +216818,9 @@ } } } + }, + "Why fix_sql is left out: running it with the other fixes would do harm" : { + }, "Why the check was skipped or failed" : { @@ -217535,6 +217550,12 @@ }, "With the Dashboard" : { + }, + "Withheld: another finding's fix drops %@, the index that covers this one, and dropping both leaves these lookups with no index." : { + + }, + "Withheld: another finding's fix drops the index that covers this one, and dropping both leaves these lookups with no index." : { + }, "Word wrap" : { "localizations" : { diff --git a/apple/TablePro/ViewModels/TableHealthViewModel.swift b/apple/TablePro/ViewModels/TableHealthViewModel.swift index 97e4e19190..341a252e87 100644 --- a/apple/TablePro/ViewModels/TableHealthViewModel.swift +++ b/apple/TablePro/ViewModels/TableHealthViewModel.swift @@ -67,11 +67,13 @@ final class TableHealthViewModel: ObservableObject { @Published private(set) var scopeRowCount = 0 private let history: any TableHealthHistoryStoring - private let metadata: any ScopedMetadataProviding + private let lease: TableHealthReportLoader.Lease // Read at each refresh, so a database rename retargets the next run and nothing follows the sidebar. private let database: @MainActor () -> String private var runTask: Task? private var refreshQueued = false + // Bumped by a rename: a run that read the catalog before it would record the old names over the moved history. + private var catalogRevision = 0 private var ledgerLoaded = false private var historyObserver: AnyCancellable? @@ -86,13 +88,13 @@ final class TableHealthViewModel: ObservableObject { connectionId: UUID, provider: (any TableHealthProvider)?, history: any TableHealthHistoryStoring, - metadata: any ScopedMetadataProviding = DatabaseManager.shared, + lease: TableHealthReportLoader.Lease = .cancellable, database: @escaping @MainActor () -> String ) { self.connectionId = connectionId self.provider = provider self.history = history - self.metadata = metadata + self.lease = lease self.database = database historyObserver = NotificationCenter.default.publisher(for: .tableHealthHistoryDidChange) .receive(on: RunLoop.main) @@ -131,6 +133,12 @@ final class TableHealthViewModel: ObservableObject { await task.value } + // A table or schema was renamed. A run already reading is dropped and this one reads the catalog again. + func refreshAfterCatalogChange() async { + catalogRevision += 1 + await refresh() + } + // The tab closed: stop reading so the bulk metadata lane is free for the next caller. func cancel() { refreshQueued = false @@ -151,10 +159,11 @@ final class TableHealthViewModel: ObservableObject { private func performRefresh(_ provider: any TableHealthProvider) async { let target = self.database() + let revision = catalogRevision do { let scope = DatabaseScope(connectionId: connectionId, database: target, schema: nil) - let fresh = try await TableHealthReportLoader.load(provider: provider, scope: scope, metadata: metadata) - guard !Task.isCancelled else { return } + let fresh = try await TableHealthReportLoader.load(provider: provider, scope: scope, lease: lease) + guard !Task.isCancelled, revision == catalogRevision else { return } report = Self.merging(fresh, over: report) loadError = nil lastRefreshDate = Date() diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift index 8ae4a5dd14..70d3a8117d 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift @@ -30,7 +30,7 @@ extension MainContentCoordinator { func refreshTableHealth(inDatabase database: String) { for tab in tabManager.tabs where tab.tabType == .tableHealth && tab.tableContext.databaseName == database { guard let model = tableHealthModels[tab.id] else { continue } - Task { await model.refresh() } + Task { await model.refreshAfterCatalogChange() } } } diff --git a/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift b/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift index 2d3530c8d6..945ca063b6 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift @@ -70,11 +70,15 @@ struct TableHealthPreviewPane: View { minHeight: TableHealthLayoutMetrics.previewSQLMinimumHeight, maxHeight: .infinity ) - // A ScrollView takes all the height it is offered, so it gets the content's height up to the cap. + // A ScrollView takes all the height it is offered: at least the content up to the cap, and no + // more than the content when the pane is dragged taller. ScrollView { about(finding) } - .frame(height: min(aboutHeight, TableHealthLayoutMetrics.previewAboutMaximumHeight)) + .frame( + minHeight: min(aboutHeight, TableHealthLayoutMetrics.previewAboutMaximumHeight), + maxHeight: aboutHeight + ) } } if fix != nil { @@ -110,8 +114,10 @@ struct TableHealthPreviewPane: View { if !facts.isEmpty { Form { ForEach(facts) { fact in + // Without the flexible frame the columns form lays a long value out wider than the pane. LabeledContent(fact.label) { Text(verbatim: fact.value) + .frame(maxWidth: .infinity, alignment: .leading) } } } diff --git a/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift b/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift index 085b9b3eaf..9774f8b3d3 100644 --- a/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift +++ b/apple/TableProTests/Core/MCP/Protocol/Tools/GetTableHealthToolTests.swift @@ -44,6 +44,32 @@ struct GetTableHealthToolTests { Set(schema?["required"]?.arrayValue?.compactMap { $0.stringValue } ?? []) } + // An unused index, and a redundant one it covers: dropping both leaves the redundant one's lookups with no index. + private let unusedCover = TableHealthFinding( + kind: .unusedIndex, + schema: "public", + table: "orders", + objectName: "orders_customer_status_idx", + detail: "No scans", + sizeBytes: 8_192, + metrics: [TableHealthMetric.estimatedRows(10)], + fixSQL: "DROP INDEX public.orders_customer_status_idx;" + ) + private let coveredRedundant = TableHealthFinding( + kind: .redundantIndex, + schema: "public", + table: "orders", + objectName: "orders_customer_idx", + detail: "Covered", + sizeBytes: 4_096, + metrics: [TableHealthMetric(id: "covering_index", label: "Covering Index", value: .text("orders_customer_status_idx"))], + fixSQL: "DROP INDEX public.orders_customer_idx;" + ) + + private func finding(named object: String, in payload: JsonValue) -> JsonValue? { + payload["findings"]?.arrayValue?.first { $0["object"]?.stringValue == object } + } + @Test("A failed check reaches the client as fixed text, never the server's own words") func failedCheckIsFixedText() { let payload = MCPConnectionBridge.tableHealthPayload( @@ -155,20 +181,11 @@ struct GetTableHealthToolTests { @Test("Every key the payload sends is declared in the output schema, and every required one is sent") func payloadMatchesTheOutputSchema() throws { - let finding = TableHealthFinding( - kind: .unusedIndex, - schema: "public", - table: "orders", - objectName: "orders_note_idx", - detail: "Never scanned", - sizeBytes: 8_192, - metrics: [TableHealthMetric.estimatedRows(10)], - fixSQL: "DROP INDEX public.orders_note_idx;" - ) let payload = MCPConnectionBridge.tableHealthPayload( report( [ - result(.unusedIndex, .completed, [finding], note: "Counts since the reset"), + result(.unusedIndex, .completed, [unusedCover], note: "Counts since the reset"), + result(.redundantIndex, .completed, [coveredRedundant]), result(.bloat, .failed("boom")), result(.staleStatistics, .skipped("standby")) ], @@ -191,14 +208,49 @@ struct GetTableHealthToolTests { #expect(required(checkSchema).isSubset(of: checkKeys)) } - let encoded = try #require(payload["findings"]?.arrayValue?.first) - let findingKeys = keys(encoded) - #expect(findingKeys == keys(findingSchema?["properties"])) - #expect(required(findingSchema).isSubset(of: findingKeys)) + let findings = payload["findings"]?.arrayValue ?? [] + var sentFindingKeys: Set = [] + for encoded in findings { + let findingKeys = keys(encoded) + sentFindingKeys.formUnion(findingKeys) + #expect(findingKeys.isSubset(of: keys(findingSchema?["properties"])), "undeclared: \(findingKeys)") + #expect(required(findingSchema).isSubset(of: findingKeys)) + for metric in encoded["metrics"]?.arrayValue ?? [] { + #expect(keys(metric) == keys(metricSchema?["properties"])) + #expect(required(metricSchema) == keys(metric)) + } + } + #expect(sentFindingKeys == keys(findingSchema?["properties"])) + } + + @Test("A fix that would drop a redundant index along with its unused cover is withheld, with the reason") + func coveredFixIsWithheld() throws { + let payload = MCPConnectionBridge.tableHealthPayload( + report([ + result(.unusedIndex, .completed, [unusedCover]), + result(.redundantIndex, .completed, [coveredRedundant]) + ]), + ledger: TableHealthLedger() + ) + + let withheld = try #require(finding(named: "orders_customer_idx", in: payload)) + let kept = try #require(finding(named: "orders_customer_status_idx", in: payload)) + #expect(withheld["fix_sql"] == nil) + #expect(withheld["fix_withheld_reason"]?.stringValue?.contains("orders_customer_status_idx") == true) + #expect(kept["fix_sql"]?.stringValue == "DROP INDEX public.orders_customer_status_idx;") + #expect(kept["fix_withheld_reason"] == nil) + } + + @Test("A redundant index whose cover stays keeps its fix") + func uncoveredFixIsSent() throws { + let payload = MCPConnectionBridge.tableHealthPayload( + report([result(.redundantIndex, .completed, [coveredRedundant])]), + ledger: TableHealthLedger() + ) - let metric = encoded["metrics"]?.arrayValue?.first - #expect(keys(metric) == keys(metricSchema?["properties"])) - #expect(required(metricSchema) == keys(metric)) + let redundant = try #require(finding(named: "orders_customer_idx", in: payload)) + #expect(redundant["fix_sql"]?.stringValue == "DROP INDEX public.orders_customer_idx;") + #expect(redundant["fix_withheld_reason"] == nil) } // The stand-in read stays held until after the call has answered, like one queued behind the tab's report on diff --git a/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift b/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift index b9d45473bd..3d208c29d3 100644 --- a/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift +++ b/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift @@ -128,6 +128,20 @@ struct PostgreSQLTableHealthProviderTests { #expect(finding.fixSQL == #"DROP INDEX CONCURRENTLY "sales"."orders_note_idx";"#) } + @Test("An unused index says only that the server's statistics hold no scans, which a reset or a crash also empties") + func unusedIndexDetailClaimsOnlyTheStatistics() throws { + let finding = try #require(findings( + .unusedIndex, + columns: ["schema_name", "table_name", "index_name", "index_bytes"], + rows: [["sales", "orders", "orders_note_idx", "8192"]] + ).first) + #expect(finding.detail == String( + format: String(localized: "%1$@ has no scans in the server's statistics. It takes %2$@."), + "orders_note_idx", ByteSizeFormatting.string(byteString: "8192") + )) + #expect(!finding.detail.contains("never")) + } + @Test("A table without a primary key quotes its name in the preview and carries its rows and size") func missingPrimaryKeyQuotesName() throws { let finding = try #require(findings( @@ -804,15 +818,19 @@ struct PostgreSQLTableHealthProviderTests { ].joined(separator: " ")) } - @Test("Before 12, an invalid index whose table was locked, so its definition was not read, falls back to a plain rebuild") - func lockedDefinitionFallsBackToReindex() throws { + @Test("Before 12, an invalid index whose table was locked has no fix rather than a rebuild that blocks writes, and says to refresh") + func lockedDefinitionHasNoFix() throws { let finding = try #require(findings( .invalidIndex, columns: Self.invalidColumns, rows: [["public", "inv", "inv_vw_cancelled", "f", "t", "16384", nil]], statistics: statistics(version: "110000") ).first) - #expect(finding.fixSQL == #"REINDEX INDEX "public"."inv_vw_cancelled";"#) + #expect(finding.fixSQL == nil) + #expect(finding.detail.contains(String( + localized: "Another session held its table locked, so its definition could not be read. Refresh once the lock is gone for a fix." + ))) #expect(finding.metric("index_definition") == nil) + #expect(try invalid("inv_vw_cancelled", version: "170011").fixSQL == #"REINDEX INDEX CONCURRENTLY "public"."inv_vw_cancelled";"#) } } diff --git a/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift b/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift index bf0fc3eab9..efb144245f 100644 --- a/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift +++ b/apple/TableProTests/Core/TableHealth/TableHealthFixScriptTests.swift @@ -105,6 +105,20 @@ struct TableHealthFixScriptTests { #expect(!text.contains(#"DROP INDEX CONCURRENTLY "public"."orders_customer_idx";"#)) } + @Test("The withheld fixes are the redundant indexes whose cover another finding with a fix drops") + func withheldFixesNameTheKeptFindings() { + let kept = redundant("orders_customer_idx", coveredBy: "orders_customer_created_idx") + let coverStays = redundant("orders_status_idx", coveredBy: "orders_status_created_idx") + let findings = [unused("orders_customer_created_idx"), kept, coverStays] + #expect(TableHealthFixScript.withheldFixes(in: findings) == [kept.id]) + #expect(TableHealthFixScript.withheldFixes(in: [kept, coverStays]).isEmpty) + + let coverWithoutFix = TableHealthFixture.finding( + .unusedIndex, schema: "public", table: "orders", object: "orders_customer_created_idx", fixSQL: nil + ) + #expect(TableHealthFixScript.withheldFixes(in: [coverWithoutFix, kept]).isEmpty) + } + @Test("A redundant index still drops when its cover stays, when the cover is another table's, or when it is unused itself") func redundantIndexDropsOtherwise() { let coverStays = script([redundant("orders_customer_idx", coveredBy: "orders_customer_created_idx")]) diff --git a/apple/TableProTests/Core/TableHealth/TableHealthReportLoaderTests.swift b/apple/TableProTests/Core/TableHealth/TableHealthReportLoaderTests.swift new file mode 100644 index 0000000000..05b761719f --- /dev/null +++ b/apple/TableProTests/Core/TableHealth/TableHealthReportLoaderTests.swift @@ -0,0 +1,101 @@ +import Foundation +@testable import TablePro +import TableProPluginKit +import TableProTestSupport +import Testing + +@MainActor +struct TableHealthReportLoaderTests { + @Test("Cancelling a report read on the cancellable lease stops its statement on the server", .timeLimit(.minutes(1))) + func cancelStopsTheStatementOnTheServer() async throws { + let connection = TestFixtures.makeConnection(database: "shop", type: .postgresql) + let plugin = HeldCatalogDriver() + DatabaseManager.shared.injectSession( + ConnectionSession(connection: connection, driver: PluginDriverAdapter(connection: connection, pluginDriver: plugin)), + for: connection.id + ) + let scope = DatabaseScope(connectionId: connection.id, database: "shop", schema: nil) + // Only the bulk lane has a connection, so the read has to lease there. + let pooled = PluginDriverAdapter(connection: connection, pluginDriver: plugin) + try await pooled.connect() + MetadataConnectionPool.shared.injectEntry(pooled, scope: scope, workload: .bulk) + defer { + MetadataConnectionPool.shared.closeAll(connectionId: connection.id) + DatabaseManager.shared.removeSession(for: connection.id) + } + + let load = Task { + try await TableHealthReportLoader.load(provider: OneCheckProvider(), scope: scope, lease: .cancellable) + } + await plugin.reached.wait() + load.cancel() + await plugin.cancelled.wait() + _ = await load.result + + #expect(plugin.cancelCount == 1) + #expect(DatabaseManager.shared.runningDrivers[connection.id] == nil) + } +} + +private struct OneCheckProvider: TableHealthProvider { + var checks: [TableHealthCheckKind] { [.missingPrimaryKey] } + var statisticsQueries: [String] { [] } + + func statistics(from results: [QueryResult?], now: Date) -> TableHealthStatistics { + TableHealthStatistics() + } + + func check(_ kind: TableHealthCheckKind) -> TableHealthCheck { + TableHealthCheck(kind: kind, summary: "") + } + + func queries(for check: TableHealthCheckKind, statistics: TableHealthStatistics) -> [String] { + ["SELECT 1"] + } + + func findings( + for check: TableHealthCheckKind, + from results: [QueryResult], + context: TableHealthQueryContext, + statistics: TableHealthStatistics + ) -> (findings: [TableHealthFinding], note: String?) { + ([], nil) + } +} + +private struct CancelledOnServer: Error {} + +// Holds every statement until the server is told to cancel it, the way a slow catalog read on a large database does. +private final class HeldCatalogDriver: PluginDriverStub, @unchecked Sendable { + let reached = AsyncGate() + let cancelled = AsyncGate() + private let lock = NSLock() + private var pending: CheckedContinuation? + private var cancels = 0 + + var cancelCount: Int { + lock.lock() + defer { lock.unlock() } + return cancels + } + + func execute(query: String, context: PluginStatementContext) async throws -> PluginQueryResult { + try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in + lock.lock() + pending = continuation + lock.unlock() + Task { await reached.open() } + } + return .empty + } + + func cancelQuery() throws { + lock.lock() + cancels += 1 + let continuation = pending + pending = nil + lock.unlock() + continuation?.resume(throwing: CancelledOnServer()) + Task { await cancelled.open() } + } +} diff --git a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift index 9ca07fafbc..8efec815f7 100644 --- a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift +++ b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift @@ -186,7 +186,7 @@ struct TableHealthViewModelTests { connectionId: metadata.connection.id, provider: TableHealthProviderFactory.provider(for: databaseType), history: history, - metadata: metadata, + lease: .metadata(metadata), database: { database.name } ) } @@ -684,4 +684,26 @@ struct TableHealthViewModelTests { #expect(row(viewModel, table: "logs")?.status == .ignored) } + + @Test("A run that read the catalog before a rename records nothing, and the run after the rename records") + func renameDuringARunDropsThatRun() async { + let metadata = HealthMetadata() + let hold = (reached: AsyncGate(), release: AsyncGate()) + metadata.hold = hold + let history = InMemoryHistory() + let viewModel = makeViewModel(metadata, history: history) + + let first = Task { await viewModel.refresh() } + await hold.reached.wait() + metadata.server.tablesWithoutKey = ["logs_v2"] + let afterRename = Task { await viewModel.refreshAfterCatalogChange() } + await hold.release.open() + await afterRename.value + await first.value + + #expect(metadata.leases.count == 2) + #expect(history.recordCount == 1) + #expect(viewModel.findingRows.map { $0.id.table } == ["logs_v2"]) + #expect(viewModel.resolvedCount == 0) + } } diff --git a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift index 3495695e34..3855cb8cb7 100644 --- a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift +++ b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift @@ -410,7 +410,7 @@ struct MainContentCoordinatorRefreshTests { connectionId: coordinator.connectionId, provider: TableHealthProviderFactory.provider(for: .postgresql), history: RefreshUnusedHistory(), - metadata: metadata, + lease: .metadata(metadata), database: { "shop" } ) @@ -441,7 +441,7 @@ struct MainContentCoordinatorRefreshTests { connectionId: coordinator.connectionId, provider: TableHealthProviderFactory.provider(for: .postgresql), history: history, - metadata: RefreshLeaseRecorder(), + lease: .metadata(RefreshLeaseRecorder()), database: { tabManager.tabs.first { $0.id == tabId }?.tableContext.databaseName ?? "" } ) coordinator.tableHealthModels[tabId] = model @@ -500,7 +500,7 @@ struct MainContentCoordinatorRefreshTests { connectionId: coordinator.connectionId, provider: TableHealthProviderFactory.provider(for: .postgresql), history: RefreshUnusedHistory(), - metadata: metadata, + lease: .metadata(metadata), database: { database } ) coordinator.tableHealthModels[tabId] = model diff --git a/apple/scripts/probes/check-table-health-catalogs.sh b/apple/scripts/probes/check-table-health-catalogs.sh index 764a9c6fe7..b22320222f 100755 --- a/apple/scripts/probes/check-table-health-catalogs.sh +++ b/apple/scripts/probes/check-table-health-catalogs.sh @@ -244,6 +244,7 @@ SQL check_postgres_stale_statistics check_postgres_transaction_id_age check_postgres_locks + check_postgres_standbys } # Every query must answer while another session holds ACCESS EXCLUSIVE on a table: a size or a definition read @@ -288,40 +289,102 @@ SQL ) [ "$version" -ge 100000 ] && cases+=("nearLimitQuery|") - local entry query listed output + # A table never vacuumed or analyzed has no pages recorded, so no size rather than 0. + local output + output="$(pg -d "$DATABASE" -c "$(query_of "$source" missingPrimaryKeyQuery)")" + expect_pattern "a table with no pages recorded has no size" "$output" "^public\|lk_nopk\|0\|$" + + local entry query listed + # $1 says which lock is held; with $2 = listed, each query must still list what the lock covers. + run_lock_cases() { + for entry in "${cases[@]}"; do + query="${entry%%|*}" + listed="${entry#*|}" + output="$(pg -d "$DATABASE" -c "SET lock_timeout = '5s'" -c "$(query_of "$source" "$query")" 2>&1)" + expect "$query answers while $1" "$output" "lock timeout" no + if [ "$2" = listed ] && [ -n "$listed" ]; then + expect "$query still lists the locked table while $1" "$output" "$listed" yes + fi + done + [ "$2" = listed ] || return + output="$(pg -d "$DATABASE" -c "SET lock_timeout = '5s'" -c "$(query_of "$source" "$invalid")" 2>&1)" + expect_pattern "an invalid index comes back without the definition that would wait while $1" "$output" \ + "lk_listed_v_uniq\|t\|f\|[0-9]*\|$" + if [ "$version" -ge 100000 ]; then + output="$(pg -d "$DATABASE" -c "SET lock_timeout = '5s'" -c "$(query_of "$source" nearLimitQuery)" 2>&1)" + expect "a sequence comes back unread and marked locked while $1" "$output" \ + "public|lk_listed_id_seq|integer|1|1|2147483647||1|2147483647||t|t|" yes + fi + } + hold_lock tablepro_health_other_lock "LOCK TABLE lk_other IN ACCESS EXCLUSIVE MODE" - for entry in "${cases[@]}"; do - query="${entry%%|*}" - output="$(pg -d "$DATABASE" -c "SET lock_timeout = '5s'" -c "$(query_of "$source" "$query")" 2>&1)" - expect "$query answers while a table it does not list is locked" "$output" "lock timeout" no - done + run_lock_cases "a table it does not list is locked" unlisted terminate tablepro_health_other_lock local sequence_lock="" [ "$version" -ge 100000 ] && sequence_lock="; ALTER SEQUENCE lk_listed_id_seq MAXVALUE 2147483647" hold_lock tablepro_health_listed_lock "TRUNCATE lk_listed, lk_nopk, lk_child, lk_bloat$sequence_lock" - for entry in "${cases[@]}"; do - query="${entry%%|*}" - listed="${entry#*|}" - output="$(pg -d "$DATABASE" -c "SET lock_timeout = '5s'" -c "$(query_of "$source" "$query")" 2>&1)" - expect "$query answers while the tables it lists, their indexes and TOAST tables are locked" "$output" "lock timeout" no - if [ -n "$listed" ]; then - expect "$query still lists the locked table" "$output" "$listed" yes - fi + run_lock_cases "an open transaction locks the tables it lists, their indexes and TOAST tables" listed + terminate tablepro_health_listed_lock + + # A prepared transaction keeps its locks with no backend, so pg_locks shows them with a NULL pid. + if [ "$(pg -d "$DATABASE" -c "SHOW max_prepared_transactions")" -gt 0 ]; then + local prepared=(-c "BEGIN" -c "TRUNCATE lk_listed, lk_nopk, lk_child, lk_bloat") + [ "$version" -ge 100000 ] && prepared+=(-c "ALTER SEQUENCE lk_listed_id_seq MAXVALUE 2147483647") + pg -d "$DATABASE" "${prepared[@]}" -c "PREPARE TRANSACTION 'tablepro_health_lock'" > /dev/null + run_lock_cases "a prepared transaction locks the tables it lists" listed + pg -d "$DATABASE" -c "ROLLBACK PREPARED 'tablepro_health_lock'" > /dev/null + else + echo "note max_prepared_transactions is 0, so the prepared transaction lock case is skipped" + fi +} + +# Only a physical standby runs queries whose index scans this server does not count: a logical walsender feeds a +# subscriber or a CDC reader, and a base backup runs no queries. +check_postgres_standbys() { + local stats appeared=0 + PGAPPNAME=tablepro_health_logical psql -X -q "host=$HOST port=$PORT user=$USER_NAME dbname=$DATABASE replication=database" \ + -c "SELECT pg_sleep(30)" > /dev/null 2>&1 & + for _ in $(seq 1 20); do + [ "$(pg -d postgres -c "SELECT count(*) FROM pg_stat_replication WHERE application_name = 'tablepro_health_logical'")" = "1" ] \ + && { appeared=1; break; } + sleep 0.25 done - output="$(pg -d "$DATABASE" -c "SET lock_timeout = '5s'" -c "$(query_of "$source" "$invalid")" 2>&1)" - expect_pattern "an invalid index on a locked table comes back without the definition that would wait" "$output" \ - "lk_listed_v_uniq\|t\|f\|[0-9]*\|$" - if [ "$version" -ge 100000 ]; then - output="$(pg -d "$DATABASE" -c "SET lock_timeout = '5s'" -c "$(query_of "$source" nearLimitQuery)" 2>&1)" - expect "a sequence an open ALTER SEQUENCE holds comes back unread and marked locked" "$output" \ - "public|lk_listed_id_seq|integer|1|1|2147483647||1|2147483647||t|t|" yes + if [ "$appeared" = 1 ]; then + stats="$(pg -d "$DATABASE" -c "$(query_of "$source" statisticsQuery)")" + expect_pattern "a logical walsender is not counted as a standby" "$stats" "\|0$" + else + echo "note no logical walsender appeared, so that standby case is skipped" fi - terminate tablepro_health_listed_lock + terminate tablepro_health_logical + + command -v pg_receivewal > /dev/null || { echo "note pg_receivewal not found, so the physical standby case is skipped"; return; } + local wal receiver + wal="$(mktemp -d)" + PGAPPNAME=tablepro_health_physical pg_receivewal -h "$HOST" -p "$PORT" -U "$USER_NAME" -D "$wal" --no-sync > /dev/null 2>&1 & + receiver=$! + appeared=0 + for _ in $(seq 1 20); do + [ "$(pg -d postgres -c "SELECT count(*) FROM pg_stat_replication WHERE application_name = 'tablepro_health_physical'")" = "1" ] \ + && { appeared=1; break; } + sleep 0.25 + done + if [ "$appeared" = 1 ]; then + stats="$(pg -d "$DATABASE" -c "$(query_of "$source" statisticsQuery)")" + expect_pattern "a physical walsender is counted as a standby" "$stats" "\|1$" + else + echo "note pg_receivewal could not stream (a replication line in pg_hba.conf is needed), so the physical standby case is skipped" + fi + kill "$receiver" 2> /dev/null + wait "$receiver" 2> /dev/null + rm -rf "$wal" } cleanup_postgres() { local admin=(psql -X -q -h "$HOST" -p "$PORT" -U "$USER_NAME" -d postgres) + # A prepared transaction left by an interrupted run would block the drop; it ends only from its own database. + psql -X -q -h "$HOST" -p "$PORT" -U "$USER_NAME" -d "$DATABASE" \ + -c "ROLLBACK PREPARED 'tablepro_health_lock'" > /dev/null 2>&1 "${admin[@]}" -c "SELECT pg_catalog.pg_terminate_backend(pid) FROM pg_catalog.pg_stat_activity WHERE datname = '$DATABASE' AND pid <> pg_catalog.pg_backend_pid()" > /dev/null 2>&1 "${admin[@]}" -c "DROP DATABASE IF EXISTS $DATABASE" > /dev/null 2>&1 "${admin[@]}" -c "DROP ROLE IF EXISTS $READER_ROLE" > /dev/null 2>&1 @@ -481,7 +544,7 @@ SQL if [ "$version" -ge 110000 ]; then expect "index shape reads an INCLUDE column after the key count" "$shape" "k_a_incl_c|btree|f|f|t|false|f|||8192|1|2|4|c|||" yes expect "index shape reads an attached partition index as protected" "$shape" "public|rp_2026|r|rp_2026_k_at_idx|btree|f|f|t|false|t|" yes - expect "index shape reads a partitioned table's index" "$shape" "public|rp|p|r_rp_k|btree|f|f|t|false|f|||0|" yes + expect "index shape reads a partitioned table's index, which has no pages and so no size" "$shape" "public|rp|p|r_rp_k|btree|f|f|t|false|f||||1|1|" yes fi if [ "$version" -ge 150000 ]; then expect "index shape reads NULLS NOT DISTINCT" "$shape" "public|ri|r|k_nnd|btree|t|f|t|true|f|" yes From 3bb1cc94fdedca545d0dcaba806cdae20549587e Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Tue, 10 Nov 2026 00:56:36 +0700 Subject: [PATCH 07/13] fix(table-health): review round 4 findings --- .../Storage/TableHealthHistoryStorage.swift | 31 ++++- .../PostgreSQLTableHealthProvider.swift | 21 ++-- .../TableHealthHistoryStoring.swift | 13 +- .../ViewModels/TableHealthViewModel.swift | 28 +++-- ...ontentCoordinator+TableHealthActions.swift | 2 +- .../TableHealth/TableHealthPreviewPane.swift | 5 + .../TableHealthHistoryStorageTests.swift | 113 ++++++++++++++++++ .../PostgreSQLTableHealthProviderTests.swift | 22 ++++ .../TableHealthViewModelTests.swift | 71 ++++++++++- .../MainContentCoordinatorRefreshTests.swift | 24 +++- .../probes/check-table-health-catalogs.sh | 37 +++++- 11 files changed, 333 insertions(+), 34 deletions(-) diff --git a/apple/TablePro/Core/Storage/TableHealthHistoryStorage.swift b/apple/TablePro/Core/Storage/TableHealthHistoryStorage.swift index 2b370b8215..7a70a5d8e6 100644 --- a/apple/TablePro/Core/Storage/TableHealthHistoryStorage.swift +++ b/apple/TablePro/Core/Storage/TableHealthHistoryStorage.swift @@ -37,8 +37,17 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet var isUnreadable = false } + private struct GenerationKey: Hashable { + let connectionId: UUID + let database: String + } + private let directory: URL private let notificationCenter: NotificationCenter + // In memory: a run that has to compare against one never outlives the app. A purge moves every database of the + // connection, including ones this store never saw. + private var databaseGenerations: [GenerationKey: Int] = [:] + private var connectionGenerations: [UUID: Int] = [:] init( directory: URL = AppStorageEnvironment.shared.supportDirectory @@ -53,13 +62,22 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet try load(connectionId).databases[database] ?? TableHealthLedger() } + func generation(connectionId: UUID, database: String) -> Int { + connectionGenerations[connectionId, default: 0] + + databaseGenerations[GenerationKey(connectionId: connectionId, database: database), default: 0] + } + func record( _ report: TableHealthReport, connectionId: UUID, database: String, + generation: Int, at date: Date ) throws -> TableHealthLedger { - try mutate(connectionId: connectionId, database: database) { $0.recording(report, at: date) } + guard generation == self.generation(connectionId: connectionId, database: database) else { + return try ledger(connectionId: connectionId, database: database) + } + return try mutate(connectionId: connectionId, database: database) { $0.recording(report, at: date) } } func setIgnored( @@ -80,6 +98,7 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet // Engines without a schema level (MySQL) store findings with a nil schema, which is what their scopes carry. func renameTable(from oldScope: TableScope, to newScope: TableScope) { + advanceGenerations(connectionId: oldScope.connectionId, databases: [oldScope.database, newScope.database]) guard let database = oldScope.database, oldScope.connectionId == newScope.connectionId, newScope.database == database, @@ -98,6 +117,7 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet toDatabase: String, toSchema: String? ) { + advanceGenerations(connectionId: connectionId, databases: [fromDatabase, toDatabase]) if let fromSchema { guard let toSchema, fromDatabase == toDatabase, fromSchema != toSchema else { return } mutateLogging(connectionId: connectionId, database: fromDatabase) { @@ -120,6 +140,7 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet } func dropTable(_ scope: TableScope) { + advanceGenerations(connectionId: scope.connectionId, databases: [scope.database]) guard let database = scope.database else { return } mutateLogging(connectionId: scope.connectionId, database: database) { $0.droppingTable(schema: scope.schema, name: scope.table) @@ -127,6 +148,7 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet } func dropContainer(connectionId: UUID, database: String, schema: String?) { + advanceGenerations(connectionId: connectionId, databases: [database]) if let schema { mutateLogging(connectionId: connectionId, database: database) { $0.droppingSchema(schema) } return @@ -145,6 +167,7 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet // Device-local, so there is never a tombstone to leave. func purgeConnections(_ connectionIds: Set, leavesTombstones: Bool) { for connectionId in connectionIds { + connectionGenerations[connectionId, default: 0] += 1 let databases = (try? load(connectionId))?.databases.keys.sorted() ?? [] for url in storedFiles(for: connectionId) { do { @@ -159,6 +182,12 @@ final class TableHealthHistoryStorage: TableHealthHistoryStoring, TableScopedSet } } + private func advanceGenerations(connectionId: UUID, databases: [String?]) { + for database in Set(databases.compactMap { $0 }) { + databaseGenerations[GenerationKey(connectionId: connectionId, database: database), default: 0] += 1 + } + } + private func mutate( connectionId: UUID, database: String, diff --git a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift index 367347c1fc..c56a6ef3e0 100644 --- a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift +++ b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift @@ -7,8 +7,8 @@ import Foundation // deparsers wait behind an ACCESS EXCLUSIVE lock (an open ALTER TABLE, TRUNCATE or VACUUM FULL) until it ends. Sizes // are relpages times block_size, as of the last VACUUM, ANALYZE or index build. A relation with no pages recorded has // no size rather than 0: on 10 to 13 a table never vacuumed or analyzed reads relpages 0 and reltuples 0, the same as -// an empty one. Before 13 ANALYZE leaves the TOAST table's relpages alone, so there a table's TOAST share is as of its -// last VACUUM. +// an empty one. Row estimates follow the same rule, so a partitioned table, which has no pages, has none. Before 13 +// ANALYZE leaves the TOAST table's relpages alone, so there a table's TOAST share is as of its last VACUUM. struct PostgreSQLTableHealthProvider: TableHealthProvider { let checks: [TableHealthCheckKind] = [ .bloat, .unusedIndex, .redundantIndex, .invalidIndex, .missingPrimaryKey, .unindexedForeignKey, @@ -19,7 +19,8 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { private static let counterChecks: [TableHealthCheckKind] = [.bloat, .unusedIndex, .staleStatistics] // A physical standby's walsender has no database; a logical one (a subscription, a CDC reader) has one, and a - // base backup's reports state backup. Without pg_read_all_stats the state reads NULL, which still counts. + // base backup's reports state backup. Without pg_read_all_stats the state reads NULL, which still counts. Before + // 10 pg_stat_activity lists only backends with a database, so a physical walsender is the one with no row there. static let statisticsQuery = """ SELECT pg_catalog.current_setting('track_counts') AS track_counts, pg_catalog.date_part('epoch', d.stats_reset) AS stats_reset, @@ -27,9 +28,9 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { pg_catalog.pg_is_in_recovery() AS in_recovery, (SELECT pg_catalog.count(*) FROM pg_catalog.pg_stat_replication r - JOIN pg_catalog.pg_stat_activity a ON a.pid = r.pid - WHERE a.datid IS NULL - AND r.state IS DISTINCT FROM 'backup') AS standbys + WHERE r.state IS DISTINCT FROM 'backup' + AND NOT EXISTS (SELECT 1 FROM pg_catalog.pg_stat_activity a + WHERE a.pid = r.pid AND a.datid IS NOT NULL)) AS standbys FROM pg_catalog.pg_stat_database d WHERE d.datname = pg_catalog.current_database() """ @@ -124,7 +125,9 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { static let missingPrimaryKeyQuery = """ SELECT n.nspname AS schema_name, c.relname AS table_name, - GREATEST(c.reltuples, 0)::pg_catalog.int8 AS estimated_rows, + CASE WHEN c.relpages > 0 AND c.reltuples >= 0::pg_catalog.float4 + THEN c.reltuples::pg_catalog.int8 + END AS estimated_rows, CASE WHEN c.relpages > 0 THEN (c.relpages::pg_catalog.int8 + COALESCE(tc.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 END AS table_bytes @@ -170,7 +173,9 @@ struct PostgreSQLTableHealthProvider: TableHealthProvider { a.attname AS column_name, rn.nspname AS referenced_schema, rc.relname AS referenced_table, - GREATEST(c.reltuples, 0)::pg_catalog.int8 AS estimated_rows, + CASE WHEN c.relpages > 0 AND c.reltuples >= 0::pg_catalog.float4 + THEN c.reltuples::pg_catalog.int8 + END AS estimated_rows, CASE WHEN c.relpages > 0 THEN (c.relpages::pg_catalog.int8 + COALESCE(tc.relpages, 0)::pg_catalog.int8) * pg_catalog.current_setting('block_size')::pg_catalog.int8 END AS table_bytes diff --git a/apple/TablePro/Core/TableHealth/TableHealthHistoryStoring.swift b/apple/TablePro/Core/TableHealth/TableHealthHistoryStoring.swift index 3607644162..d9c7b42a2f 100644 --- a/apple/TablePro/Core/TableHealth/TableHealthHistoryStoring.swift +++ b/apple/TablePro/Core/TableHealth/TableHealthHistoryStoring.swift @@ -25,7 +25,18 @@ enum TableHealthHistoryChange { @MainActor protocol TableHealthHistoryStoring: AnyObject { func ledger(connectionId: UUID, database: String) throws -> TableHealthLedger - func record(_ report: TableHealthReport, connectionId: UUID, database: String, at date: Date) throws -> TableHealthLedger + // Moves on in the same main-actor call that renames or drops anything in the database's history, so a run that + // read the catalog before it can tell. + func generation(connectionId: UUID, database: String) -> Int + // A run that started under another generation read names the history no longer has: it records nothing and gets + // the current ledger back. + func record( + _ report: TableHealthReport, + connectionId: UUID, + database: String, + generation: Int, + at date: Date + ) throws -> TableHealthLedger func setIgnored( _ ids: Set, ignored: Bool, diff --git a/apple/TablePro/ViewModels/TableHealthViewModel.swift b/apple/TablePro/ViewModels/TableHealthViewModel.swift index 341a252e87..1a4d2a6a19 100644 --- a/apple/TablePro/ViewModels/TableHealthViewModel.swift +++ b/apple/TablePro/ViewModels/TableHealthViewModel.swift @@ -72,8 +72,6 @@ final class TableHealthViewModel: ObservableObject { private let database: @MainActor () -> String private var runTask: Task? private var refreshQueued = false - // Bumped by a rename: a run that read the catalog before it would record the old names over the moved history. - private var catalogRevision = 0 private var ledgerLoaded = false private var historyObserver: AnyCancellable? @@ -133,12 +131,6 @@ final class TableHealthViewModel: ObservableObject { await task.value } - // A table or schema was renamed. A run already reading is dropped and this one reads the catalog again. - func refreshAfterCatalogChange() async { - catalogRevision += 1 - await refresh() - } - // The tab closed: stop reading so the bulk metadata lane is free for the next caller. func cancel() { refreshQueued = false @@ -157,17 +149,21 @@ final class TableHealthViewModel: ObservableObject { } while refreshQueued && !Task.isCancelled } + // A rename or drop moves the history and its generation in one main-actor call, so a run that read the catalog + // before it is dropped however late the refresh the rename asks for arrives; that refresh shows the new names. private func performRefresh(_ provider: any TableHealthProvider) async { let target = self.database() - let revision = catalogRevision + let generation = history.generation(connectionId: connectionId, database: target) do { let scope = DatabaseScope(connectionId: connectionId, database: target, schema: nil) let fresh = try await TableHealthReportLoader.load(provider: provider, scope: scope, lease: lease) - guard !Task.isCancelled, revision == catalogRevision else { return } + guard !Task.isCancelled, + history.generation(connectionId: connectionId, database: target) == generation + else { return } report = Self.merging(fresh, over: report) loadError = nil lastRefreshDate = Date() - record(fresh, database: target) + record(fresh, database: target, generation: generation) settleAfterRefresh() rebuildRows() } catch { @@ -202,9 +198,15 @@ final class TableHealthViewModel: ObservableObject { // MARK: - History - private func record(_ report: TableHealthReport, database: String) { + private func record(_ report: TableHealthReport, database: String, generation: Int) { do { - ledger = try history.record(report, connectionId: connectionId, database: database, at: report.generatedAt) + ledger = try history.record( + report, + connectionId: connectionId, + database: database, + generation: generation, + at: report.generatedAt + ) ledgerLoaded = true storeError = nil } catch { diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift index 70d3a8117d..8ae4a5dd14 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift @@ -30,7 +30,7 @@ extension MainContentCoordinator { func refreshTableHealth(inDatabase database: String) { for tab in tabManager.tabs where tab.tabType == .tableHealth && tab.tableContext.databaseName == database { guard let model = tableHealthModels[tab.id] else { continue } - Task { await model.refreshAfterCatalogChange() } + Task { await model.refresh() } } } diff --git a/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift b/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift index 945ca063b6..fe233ed338 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthPreviewPane.swift @@ -114,10 +114,15 @@ struct TableHealthPreviewPane: View { if !facts.isEmpty { Form { ForEach(facts) { fact in + // A value is a name, a column list or a definition: wrapped, it hyphenates into a name + // that does not exist, so it stays on one line with the whole value in its help tag. // Without the flexible frame the columns form lays a long value out wider than the pane. LabeledContent(fact.label) { Text(verbatim: fact.value) + .lineLimit(1) + .truncationMode(.middle) .frame(maxWidth: .infinity, alignment: .leading) + .help(fact.value) } } } diff --git a/apple/TableProTests/Core/Storage/TableHealthHistoryStorageTests.swift b/apple/TableProTests/Core/Storage/TableHealthHistoryStorageTests.swift index 8f5509b584..842908336d 100644 --- a/apple/TableProTests/Core/Storage/TableHealthHistoryStorageTests.swift +++ b/apple/TableProTests/Core/Storage/TableHealthHistoryStorageTests.swift @@ -71,6 +71,14 @@ struct TableHealthHistoryStorageTests { return store } + private func databasesMoved(in store: TableHealthHistoryStorage, by change: () throws -> Void) rethrows -> Set { + let databases = ["shop", "store", "analytics"] + let before = databases.map { store.generation(connectionId: connectionId, database: $0) } + try change() + let after = databases.map { store.generation(connectionId: connectionId, database: $0) } + return Set(databases.indices.filter { before[$0] != after[$0] }.map { databases[$0] }) + } + private func changes(during body: () throws -> Void) rethrows -> [Change] { let received = OSAllocatedUnfairLock(initialState: [Change]()) let observer = notifications.addObserver(forName: .tableHealthHistoryDidChange, object: nil, queue: nil) { note in @@ -344,6 +352,98 @@ struct TableHealthHistoryStorageTests { #expect(try store.ledger(connectionId: otherConnection, database: "shop").firstSeen(orders.id) == Self.day(1)) } + @Test("A run that started before a rename records nothing and gets the moved history back") + func runFromBeforeARenameRecordsNothing() throws { + let store = try seededStore() + let started = store.generation(connectionId: connectionId, database: "shop") + store.renameTable(from: scope("orders"), to: scope("purchases")) + let moved = try store.ledger(connectionId: connectionId, database: "shop") + + var returned: TableHealthLedger? + let posted = try changes { + returned = try store.record( + report([orders, ordersNoteIndex, billingOrders]), + connectionId: connectionId, + database: "shop", + generation: started, + at: Self.day(2) + ) + } + + #expect(returned == moved) + #expect(posted.isEmpty) + let ledger = try makeStore().ledger(connectionId: connectionId, database: "shop") + #expect(ledger.runIndex == 1) + #expect(ledger.firstSeen(orders.id) == nil) + #expect(ledger.isIgnored(id(table: "purchases"))) + } + + @Test("A run that started after the rename records with the new names") + func runFromAfterARenameRecords() throws { + let store = try seededStore() + store.renameTable(from: scope("orders"), to: scope("purchases")) + let purchases = Fixture.finding(table: "purchases") + + let ledger = try store.record( + report([purchases, billingOrders]), + connectionId: connectionId, + database: "shop", + generation: store.generation(connectionId: connectionId, database: "shop"), + at: Self.day(2) + ) + + #expect(ledger.runIndex == 2) + #expect(ledger.firstSeen(purchases.id) == Self.day(1)) + #expect(ledger.isIgnored(purchases.id)) + let resolved = ledger.resolved.map { $0.id } + #expect(resolved == [id(.unusedIndex, table: "purchases", object: "orders_note_idx")]) + } + + @Test("Renames, drops and purges move the generation of the databases they touch, and nothing else does") + func generationMovesWithCatalogChanges() throws { + let store = makeStore() + + let byRuns = try databasesMoved(in: store) { + _ = try store.record(report([orders]), connectionId: connectionId, database: "shop", at: Self.day(1)) + _ = try store.setIgnored([orders.id], ignored: true, connectionId: connectionId, database: "shop", at: Self.day(1)) + _ = try store.clearResolved(connectionId: connectionId, database: "shop") + } + let byTableRename = databasesMoved(in: store) { + store.renameTable(from: scope("orders"), to: scope("purchases")) + } + let bySchemaRename = databasesMoved(in: store) { + store.renameContainer( + connectionId: connectionId, fromDatabase: "shop", fromSchema: "public", toDatabase: "shop", toSchema: "app" + ) + } + let byTableDrop = databasesMoved(in: store) { + store.dropTable(scope("purchases", schema: "app")) + } + let bySchemaDrop = databasesMoved(in: store) { + store.dropContainer(connectionId: connectionId, database: "shop", schema: "app") + } + let byDatabaseRename = databasesMoved(in: store) { + store.renameContainer( + connectionId: connectionId, fromDatabase: "shop", fromSchema: nil, toDatabase: "store", toSchema: nil + ) + } + let byDatabaseDrop = databasesMoved(in: store) { + store.dropContainer(connectionId: connectionId, database: "store", schema: nil) + } + let byPurge = databasesMoved(in: store) { + store.purgeConnections([connectionId], leavesTombstones: true) + } + + #expect(byRuns.isEmpty) + #expect(byTableRename == ["shop"]) + #expect(bySchemaRename == ["shop"]) + #expect(byTableDrop == ["shop"]) + #expect(bySchemaDrop == ["shop"]) + #expect(byDatabaseRename == ["shop", "store"]) + #expect(byDatabaseDrop == ["store"]) + #expect(byPurge == ["shop", "store", "analytics"]) + } + @Test("Deleting a connection reaches the history store through the registry") func registryHoldsTheStore() { let shared = TableHealthHistoryStorage.shared @@ -351,3 +451,16 @@ struct TableHealthHistoryStorageTests { #expect(registered) } } + +private extension TableHealthHistoryStorage { + // A run nothing was renamed under: it records in the generation it started with. + func record(_ report: TableHealthReport, connectionId: UUID, database: String, at date: Date) throws -> TableHealthLedger { + try record( + report, + connectionId: connectionId, + database: database, + generation: generation(connectionId: connectionId, database: database), + at: date + ) + } +} diff --git a/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift b/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift index 3d208c29d3..daf32da03f 100644 --- a/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift +++ b/apple/TableProTests/Core/TableHealth/PostgreSQLTableHealthProviderTests.swift @@ -154,6 +154,28 @@ struct PostgreSQLTableHealthProviderTests { #expect(finding.sizeBytes == 65_536) } + @Test("A table the server has not measured, a partitioned parent or one freshly loaded, carries no row estimate rather than 0") + func unmeasuredTableHasNoRowEstimate() throws { + let noKey = try #require(findings( + .missingPrimaryKey, + columns: ["schema_name", "table_name", "estimated_rows", "table_bytes"], + rows: [["public", "events", nil, nil]] + ).first) + #expect(noKey.metric("estimated_rows") == nil) + #expect(noKey.sizeBytes == nil) + + let foreignKey = try #require(findings( + .unindexedForeignKey, + columns: [ + "schema_name", "table_name", "table_kind", "constraint_name", "column_name", + "referenced_schema", "referenced_table", "estimated_rows", "table_bytes" + ], + rows: [["public", "line", "r", "line_order_fk", "order_id", "sales", "orders", nil, nil]] + ).first) + #expect(foreignKey.metric("estimated_rows") == nil) + #expect(foreignKey.metric("referenced_table")?.value == .text("sales.orders")) + } + @Test("A foreign key's column rows fold into one finding, in key order") func foreignKeyColumnsGroup() { let found = findings( diff --git a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift index 8efec815f7..82bb9e9601 100644 --- a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift +++ b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift @@ -118,6 +118,8 @@ private final class HealthMetadata: ScopedMetadataProviding { @MainActor private final class InMemoryHistory: TableHealthHistoryStoring { var ledgers: [String: TableHealthLedger] = [:] + // What the store's rename and drop move in the same call that moves the history. + var generations: [String: Int] = [:] var failsWrites = false private(set) var recordCount = 0 @@ -125,8 +127,21 @@ private final class InMemoryHistory: TableHealthHistoryStoring { ledgers[database] ?? TableHealthLedger() } - func record(_ report: TableHealthReport, connectionId: UUID, database: String, at date: Date) throws -> TableHealthLedger { - try save(connectionId: connectionId, database: database) { ledger in + func generation(connectionId: UUID, database: String) -> Int { + generations[database, default: 0] + } + + func record( + _ report: TableHealthReport, + connectionId: UUID, + database: String, + generation: Int, + at date: Date + ) throws -> TableHealthLedger { + guard generation == self.generation(connectionId: connectionId, database: database) else { + return ledgers[database] ?? TableHealthLedger() + } + return try save(connectionId: connectionId, database: database) { ledger in recordCount += 1 return ledger.recording(report, at: date) } @@ -696,7 +711,8 @@ struct TableHealthViewModelTests { let first = Task { await viewModel.refresh() } await hold.reached.wait() metadata.server.tablesWithoutKey = ["logs_v2"] - let afterRename = Task { await viewModel.refreshAfterCatalogChange() } + history.generations["shop", default: 0] += 1 + let afterRename = Task { await viewModel.refresh() } await hold.release.open() await afterRename.value await first.value @@ -706,4 +722,53 @@ struct TableHealthViewModelTests { #expect(viewModel.findingRows.map { $0.id.table } == ["logs_v2"]) #expect(viewModel.resolvedCount == 0) } + + // The window asks for the refresh a hop or two after the rename moved the history, so the run that read the old + // names can resume first. It must still record nothing: the store moved its generation with the history. + @Test("A run that resumes after a rename but before the rename's refresh arrives records nothing") + func runResumingBeforeTheRenameRefreshRecordsNothing() async throws { + let metadata = HealthMetadata() + let connectionId = metadata.connection.id + let store = TableHealthHistoryStorage( + directory: FileManager.default.temporaryDirectory + .appendingPathComponent("TableHealthViewModelTests-\(UUID().uuidString)", isDirectory: true), + notificationCenter: NotificationCenter() + ) + let viewModel = TableHealthViewModel( + connectionId: connectionId, + provider: TableHealthProviderFactory.provider(for: .postgresql), + history: store, + lease: .metadata(metadata), + database: { "shop" } + ) + await viewModel.refresh() + let logs = try #require(row(viewModel, table: "logs")) + viewModel.setIgnored([logs.id], ignored: true) + let renamed = TableHealthFindingID(kind: .missingPrimaryKey, schema: "public", table: "logs_v2", object: nil) + + let hold = (reached: AsyncGate(), release: AsyncGate()) + metadata.hold = hold + let stale = Task { await viewModel.refresh() } + await hold.reached.wait() + metadata.server.tablesWithoutKey = ["logs_v2"] + store.renameTable( + from: TableScope(connectionId: connectionId, database: "shop", schema: "public", table: "logs"), + to: TableScope(connectionId: connectionId, database: "shop", schema: "public", table: "logs_v2") + ) + await hold.release.open() + await stale.value + + let afterStale = try store.ledger(connectionId: connectionId, database: "shop") + #expect(afterStale.runIndex == 1) + #expect(afterStale.firstSeen(logs.id) == nil) + #expect(afterStale.isIgnored(renamed)) + + await viewModel.refresh() + + let afterRefresh = try store.ledger(connectionId: connectionId, database: "shop") + #expect(afterRefresh.runIndex == 2) + #expect(afterRefresh.resolved.isEmpty) + #expect(row(viewModel, table: "logs_v2")?.status == .ignored) + #expect(row(viewModel, table: "logs") == nil) + } } diff --git a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift index 3855cb8cb7..6c22752ec6 100644 --- a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift +++ b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift @@ -548,7 +548,17 @@ private final class RefreshUnusedHistory: TableHealthHistoryStoring { TableHealthLedger() } - func record(_ report: TableHealthReport, connectionId: UUID, database: String, at date: Date) throws -> TableHealthLedger { + func generation(connectionId: UUID, database: String) -> Int { + 0 + } + + func record( + _ report: TableHealthReport, + connectionId: UUID, + database: String, + generation: Int, + at date: Date + ) throws -> TableHealthLedger { TableHealthLedger() } @@ -582,7 +592,17 @@ private final class RenamedHistory: TableHealthHistoryStoring { database == self.database ? stored : TableHealthLedger() } - func record(_ report: TableHealthReport, connectionId: UUID, database: String, at date: Date) throws -> TableHealthLedger { + func generation(connectionId: UUID, database: String) -> Int { + 0 + } + + func record( + _ report: TableHealthReport, + connectionId: UUID, + database: String, + generation: Int, + at date: Date + ) throws -> TableHealthLedger { try ledger(connectionId: connectionId, database: database) } diff --git a/apple/scripts/probes/check-table-health-catalogs.sh b/apple/scripts/probes/check-table-health-catalogs.sh index b22320222f..ffb7f73820 100755 --- a/apple/scripts/probes/check-table-health-catalogs.sh +++ b/apple/scripts/probes/check-table-health-catalogs.sh @@ -289,10 +289,33 @@ SQL ) [ "$version" -ge 100000 ] && cases+=("nearLimitQuery|") - # A table never vacuumed or analyzed has no pages recorded, so no size rather than 0. + # A table never vacuumed or analyzed has no pages recorded, so no size and no row estimate rather than 0. A + # partitioned table has no pages at all, however many rows its partitions hold. + pg -d "$DATABASE" > /dev/null <<'SQL' +CREATE TABLE lk_loaded (v int, parent_id int REFERENCES lk_parent (id)); +INSERT INTO lk_loaded (v) SELECT g FROM generate_series(1, 20000) g; +SQL + if [ "$version" -ge 100000 ]; then + pg -d "$DATABASE" > /dev/null <<'SQL' +CREATE TABLE lk_parted (v int, at date) PARTITION BY RANGE (at); +CREATE TABLE lk_parted_2026 PARTITION OF lk_parted FOR VALUES FROM ('2026-01-01') TO ('2027-01-01'); +INSERT INTO lk_parted SELECT g, '2026-03-01' FROM generate_series(1, 20000) g; +ANALYZE lk_parted; +SQL + fi local output output="$(pg -d "$DATABASE" -c "$(query_of "$source" missingPrimaryKeyQuery)")" - expect_pattern "a table with no pages recorded has no size" "$output" "^public\|lk_nopk\|0\|$" + expect_pattern "an empty table never measured has no row estimate and no size" "$output" "^public\|lk_nopk\|\|$" + expect_pattern "a freshly loaded table has no row estimate and no size until a VACUUM or ANALYZE" "$output" "^public\|lk_loaded\|\|$" + if [ "$version" -ge 100000 ]; then + expect_pattern "an analyzed partitioned table has no row estimate, since it has no pages" "$output" "^public\|lk_parted\|\|$" + fi + output="$(pg -d "$DATABASE" -c "$(query_of "$source" unindexedForeignKeyQuery)")" + expect_pattern "a freshly loaded referencing table has no row estimate" "$output" \ + "lk_loaded_parent_id_fkey\|parent_id\|public\|lk_parent\|\|$" + pg -d "$DATABASE" -c "ANALYZE lk_loaded" > /dev/null + output="$(pg -d "$DATABASE" -c "$(query_of "$source" missingPrimaryKeyQuery)")" + expect_pattern "once analyzed, the table has its row estimate and size" "$output" "^public\|lk_loaded\|20000\|[0-9]+$" local entry query listed # $1 says which lock is held; with $2 = listed, each query must still list what the lock covers. @@ -356,9 +379,12 @@ check_postgres_standbys() { else echo "note no logical walsender appeared, so that standby case is skipped" fi - terminate tablepro_health_logical - command -v pg_receivewal > /dev/null || { echo "note pg_receivewal not found, so the physical standby case is skipped"; return; } + command -v pg_receivewal > /dev/null || { + echo "note pg_receivewal not found, so the physical standby case is skipped" + terminate tablepro_health_logical + return + } local wal receiver wal="$(mktemp -d)" PGAPPNAME=tablepro_health_physical pg_receivewal -h "$HOST" -p "$PORT" -U "$USER_NAME" -D "$wal" --no-sync > /dev/null 2>&1 & @@ -371,12 +397,13 @@ check_postgres_standbys() { done if [ "$appeared" = 1 ]; then stats="$(pg -d "$DATABASE" -c "$(query_of "$source" statisticsQuery)")" - expect_pattern "a physical walsender is counted as a standby" "$stats" "\|1$" + expect_pattern "a physical walsender is counted as a standby, and a logical one beside it is not" "$stats" "\|1$" else echo "note pg_receivewal could not stream (a replication line in pg_hba.conf is needed), so the physical standby case is skipped" fi kill "$receiver" 2> /dev/null wait "$receiver" 2> /dev/null + terminate tablepro_health_logical rm -rf "$wal" } From 05e6e35d1052214baa3001b0ea51bc3f52c41b6d Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Tue, 10 Nov 2026 01:10:54 +0700 Subject: [PATCH 08/13] fix(table-health): rerun a refresh a catalog change discarded --- .../ViewModels/TableHealthViewModel.swift | 10 +++-- ...MainContentCoordinator+CatalogChange.swift | 1 + ...ntentCoordinator+DatabaseObjectTools.swift | 1 + ...ontentCoordinator+TableHealthActions.swift | 4 +- .../TableHealthViewModelTests.swift | 44 +++++++++++++++---- .../MainContentCoordinatorRefreshTests.swift | 37 ++++++++++++++++ 6 files changed, 83 insertions(+), 14 deletions(-) diff --git a/apple/TablePro/ViewModels/TableHealthViewModel.swift b/apple/TablePro/ViewModels/TableHealthViewModel.swift index 1a4d2a6a19..5b3f6040a3 100644 --- a/apple/TablePro/ViewModels/TableHealthViewModel.swift +++ b/apple/TablePro/ViewModels/TableHealthViewModel.swift @@ -150,16 +150,18 @@ final class TableHealthViewModel: ObservableObject { } // A rename or drop moves the history and its generation in one main-actor call, so a run that read the catalog - // before it is dropped however late the refresh the rename asks for arrives; that refresh shows the new names. + // before it records nothing, however late a refresh for the change arrives, and reads the catalog once more. private func performRefresh(_ provider: any TableHealthProvider) async { let target = self.database() let generation = history.generation(connectionId: connectionId, database: target) do { let scope = DatabaseScope(connectionId: connectionId, database: target, schema: nil) let fresh = try await TableHealthReportLoader.load(provider: provider, scope: scope, lease: lease) - guard !Task.isCancelled, - history.generation(connectionId: connectionId, database: target) == generation - else { return } + guard !Task.isCancelled else { return } + guard history.generation(connectionId: connectionId, database: target) == generation else { + refreshQueued = true + return + } report = Self.merging(fresh, over: report) loadError = nil lastRefreshDate = Date() diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+CatalogChange.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+CatalogChange.swift index 3c77962771..537d0592ac 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+CatalogChange.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+CatalogChange.swift @@ -41,6 +41,7 @@ extension MainContentCoordinator { closeTabsForRemovedObjects(ids: tableTabIds(inDatabase: database, schema: nil)) case (.dropped, .schema): closeTabsForRemovedObjects(ids: tableTabIds(inDatabase: database, schema: container.schema)) + refreshTableHealth(inDatabase: database) case (.renamed(let newName), .database): retargetTabs(database: database, schema: nil, toDatabase: newName, toSchema: nil) case (.renamed(let newName), .schema): diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DatabaseObjectTools.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DatabaseObjectTools.swift index 4743a0417b..0c9b0b2b3c 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DatabaseObjectTools.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+DatabaseObjectTools.swift @@ -165,6 +165,7 @@ extension MainContentCoordinator { refreshStructure(ofTabs: showing) case .dropped: closeTabsForRemovedObjects(ids: showing.map(\.id)) + refreshTableHealth(inDatabase: change.scope.database) case .renamed(let newName): retitleTabs(ids: Set(showing.map(\.id)), to: newName) refreshTableHealth(inDatabase: change.scope.database) diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift index 8ae4a5dd14..bb42178a5e 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift @@ -25,8 +25,8 @@ extension MainContentCoordinator { } } - // A table or schema rename moves the history's keys, while the report on screen still names the old object: - // an ignored finding would show again and Ignore would write an id no run reports. + // A table or schema rename or drop moves or removes history keys while the report on screen still names the + // old objects: an ignored finding would show again and Ignore would write an id no run reports. func refreshTableHealth(inDatabase database: String) { for tab in tabManager.tabs where tab.tabType == .tableHealth && tab.tableContext.databaseName == database { guard let model = tableHealthModels[tab.id] else { continue } diff --git a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift index 82bb9e9601..8484fb3485 100644 --- a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift +++ b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift @@ -724,8 +724,9 @@ struct TableHealthViewModelTests { } // The window asks for the refresh a hop or two after the rename moved the history, so the run that read the old - // names can resume first. It must still record nothing: the store moved its generation with the history. - @Test("A run that resumes after a rename but before the rename's refresh arrives records nothing") + // names can resume first. It records nothing, since the store moved its generation with the history, and the + // model reads the catalog again on its own; the refresh the rename asks for then finds the same names. + @Test("A run that resumes after a rename but before the rename's refresh arrives records nothing and reruns") func runResumingBeforeTheRenameRefreshRecordsNothing() async throws { let metadata = HealthMetadata() let connectionId = metadata.connection.id @@ -758,17 +759,44 @@ struct TableHealthViewModelTests { await hold.release.open() await stale.value - let afterStale = try store.ledger(connectionId: connectionId, database: "shop") - #expect(afterStale.runIndex == 1) - #expect(afterStale.firstSeen(logs.id) == nil) - #expect(afterStale.isIgnored(renamed)) + // Run 2 is the rerun: had the stale run recorded, `logs` would be back and the rerun would resolve it. + let afterRerun = try store.ledger(connectionId: connectionId, database: "shop") + #expect(metadata.leases.count == 3) + #expect(afterRerun.runIndex == 2) + #expect(afterRerun.firstSeen(logs.id) == nil) + #expect(afterRerun.isIgnored(renamed)) + #expect(afterRerun.resolved.isEmpty) + #expect(row(viewModel, table: "logs_v2")?.status == .ignored) + #expect(row(viewModel, table: "logs") == nil) await viewModel.refresh() let afterRefresh = try store.ledger(connectionId: connectionId, database: "shop") - #expect(afterRefresh.runIndex == 2) + #expect(afterRefresh.runIndex == 3) #expect(afterRefresh.resolved.isEmpty) #expect(row(viewModel, table: "logs_v2")?.status == .ignored) - #expect(row(viewModel, table: "logs") == nil) + } + + @Test("A drop during the first load ends with a report from the run after it, with no refresh asked for") + func dropDuringFirstLoadReruns() async { + let metadata = HealthMetadata() + metadata.server.tablesWithoutKey = ["logs", "events"] + let hold = (reached: AsyncGate(), release: AsyncGate()) + metadata.hold = hold + let history = InMemoryHistory() + let viewModel = makeViewModel(metadata, history: history) + + let firstLoad = Task { await viewModel.loadIfNeeded() } + await hold.reached.wait() + metadata.server.tablesWithoutKey = ["logs"] + history.generations["shop", default: 0] += 1 + await hold.release.open() + await firstLoad.value + + #expect(metadata.leases.count == 2) + #expect(history.recordCount == 1) + #expect(viewModel.report != nil) + #expect(viewModel.isRefreshing == false) + #expect(viewModel.findingRows.map { $0.id.table } == ["logs"]) } } diff --git a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift index 6c22752ec6..2633965cfb 100644 --- a/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift +++ b/apple/TableProTests/Views/Main/MainContentCoordinatorRefreshTests.swift @@ -489,6 +489,43 @@ struct MainContentCoordinatorRefreshTests { try await waitUntil { shop.databases.count == 2 } } + @Test("A table drop refreshes the Table Health tabs on its database and no other") + func tableDropRefreshesTableHealth() async throws { + let (coordinator, tabManager) = makeCoordinator() + let shop = RefreshLeaseRecorder() + let archive = RefreshLeaseRecorder() + let shopModel = try makeTableHealthTab("shop", metadata: shop, coordinator: coordinator, tabManager: tabManager) + let archiveModel = try makeTableHealthTab("archive", metadata: archive, coordinator: coordinator, tabManager: tabManager) + await shopModel.refresh() + await archiveModel.refresh() + + coordinator.applyObjectChange(DatabaseObjectChange( + connectionId: coordinator.connectionId, + scope: DatabaseScope(connectionId: coordinator.connectionId, database: "shop", schema: "public"), + name: "orders", + kind: .dropped + )) + + try await waitUntil { shop.databases.count == 2 } + #expect(archive.databases == ["archive"]) + } + + @Test("A schema drop refreshes the Table Health tabs on its database") + func schemaDropRefreshesTableHealth() async throws { + let (coordinator, tabManager) = makeCoordinator() + let shop = RefreshLeaseRecorder() + let model = try makeTableHealthTab("shop", metadata: shop, coordinator: coordinator, tabManager: tabManager) + await model.refresh() + + coordinator.applyContainerChange(DatabaseContainerChange( + connectionId: coordinator.connectionId, + container: .schema(database: "shop", schema: "billing"), + kind: .dropped + )) + + try await waitUntil { shop.databases.count == 2 } + } + private func makeTableHealthTab( _ database: String, metadata: RefreshLeaseRecorder, From 1ca81da97dbedbb1eeb8e9f1f0643763fd27c8d5 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Tue, 10 Nov 2026 01:20:46 +0700 Subject: [PATCH 09/13] fix(table-health): skip a second rerun for the change a run already reruns for --- .../ViewModels/TableHealthViewModel.swift | 7 ++++ ...ontentCoordinator+TableHealthActions.swift | 2 +- .../TableHealthViewModelTests.swift | 40 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/apple/TablePro/ViewModels/TableHealthViewModel.swift b/apple/TablePro/ViewModels/TableHealthViewModel.swift index 5b3f6040a3..d567a49b01 100644 --- a/apple/TablePro/ViewModels/TableHealthViewModel.swift +++ b/apple/TablePro/ViewModels/TableHealthViewModel.swift @@ -131,6 +131,13 @@ final class TableHealthViewModel: ObservableObject { await task.value } + // A run in flight that read the catalog before the change reruns itself through the generation; one that started + // after it already reads the changed catalog, so queuing another would read it twice. + func refreshAfterCatalogChange() async { + guard runTask == nil else { return } + await refresh() + } + // The tab closed: stop reading so the bulk metadata lane is free for the next caller. func cancel() { refreshQueued = false diff --git a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift index bb42178a5e..c3283f103a 100644 --- a/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift +++ b/apple/TablePro/Views/Main/Extensions/MainContentCoordinator+TableHealthActions.swift @@ -30,7 +30,7 @@ extension MainContentCoordinator { func refreshTableHealth(inDatabase database: String) { for tab in tabManager.tabs where tab.tabType == .tableHealth && tab.tableContext.databaseName == database { guard let model = tableHealthModels[tab.id] else { continue } - Task { await model.refresh() } + Task { await model.refreshAfterCatalogChange() } } } diff --git a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift index 8484fb3485..e87a9bd9e8 100644 --- a/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift +++ b/apple/TableProTests/ViewModels/TableHealthViewModelTests.swift @@ -321,6 +321,46 @@ struct TableHealthViewModelTests { #expect(!viewModel.isRefreshing) } + @Test("A cancelled run whose history moved records nothing and does not rerun") + func cancelledRunWithMovedHistoryStops() async { + let metadata = HealthMetadata() + let hold = (reached: AsyncGate(), release: AsyncGate()) + metadata.hold = hold + let history = InMemoryHistory() + let viewModel = makeViewModel(metadata, history: history) + + let run = Task { await viewModel.refresh() } + await hold.reached.wait() + viewModel.cancel() + history.generations["shop", default: 0] += 1 + await hold.release.open() + await run.value + + #expect(metadata.leases.count == 1) + #expect(history.recordCount == 0) + #expect(!viewModel.isRefreshing) + } + + @Test("A catalog change during a run reruns once, not once more for the change's own refresh") + func catalogChangeDuringARunRerunsOnce() async { + let metadata = HealthMetadata() + let hold = (reached: AsyncGate(), release: AsyncGate()) + metadata.hold = hold + let history = InMemoryHistory() + let viewModel = makeViewModel(metadata, history: history) + + let run = Task { await viewModel.refresh() } + await hold.reached.wait() + history.generations["shop", default: 0] += 1 + await viewModel.refreshAfterCatalogChange() + await hold.release.open() + await run.value + + #expect(metadata.leases.count == 2) + #expect(history.recordCount == 1) + #expect(!viewModel.isRefreshing) + } + @Test("A tab with no database says to open Table Health again, and reads nothing") func noDatabaseAsksForOne() async { let metadata = HealthMetadata() From ad5218e2b3a37c64d30d66d70f963d966dc2d555 Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Tue, 10 Nov 2026 03:01:53 +0700 Subject: [PATCH 10/13] fix(table-health): match REINDEX leftovers without a regex literal the SQL check reads --- .../Core/TableHealth/PostgreSQLTableHealthProvider.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift index c56a6ef3e0..7fcfd3fdba 100644 --- a/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift +++ b/apple/TablePro/Core/TableHealth/PostgreSQLTableHealthProvider.swift @@ -948,7 +948,10 @@ extension PostgreSQLTableHealthProvider { let canReindexConcurrently = (serverVersion ?? 0) >= 120_000 // REINDEX CONCURRENTLY (12+) builds _ccnew and keeps the old copy as _ccold, adding a digit to // a name already taken. Either one left invalid is dropped, not rebuilt. - let leftover = index.range(of: "_cc(new|old)[0-9]*$", options: .regularExpression).map { String(index[$0]) } + let leftover = ["_ccnew", "_ccold"].first { marker in + guard let range = index.range(of: marker, options: .backwards) else { return false } + return index[range.upperBound...].allSatisfy { ("0"..."9").contains($0) } + } let detail: String let fixSQL: String? From 024fa8d5a555648ac271e46f0d5a640b8fe67dfe Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Tue, 10 Nov 2026 11:56:25 +0700 Subject: [PATCH 11/13] fix(table-health): give the checks list and findings table focus on click --- .../TableHealth/TableHealthChecksList.swift | 100 +++-- .../TableHealthChecksOutline.swift | 364 +++++++++++++++++ .../TableHealthFindingsTable.swift | 292 ++++++------- .../TableHealth/TableHealthRowsTable.swift | 384 ++++++++++++++++++ .../TableHealth/TableHealthTablesTests.swift | 244 +++++++++++ 5 files changed, 1182 insertions(+), 202 deletions(-) create mode 100644 apple/TablePro/Views/TableHealth/TableHealthChecksOutline.swift create mode 100644 apple/TablePro/Views/TableHealth/TableHealthRowsTable.swift create mode 100644 apple/TableProTests/Views/TableHealth/TableHealthTablesTests.swift diff --git a/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift b/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift index 96c441dd05..359d76064c 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift @@ -7,69 +7,79 @@ struct TableHealthChecksList: View { @ObservedObject var viewModel: TableHealthViewModel var body: some View { - List(selection: scopeSelection) { - Label(String(localized: "All Findings"), systemImage: "tray.full") - .badge(viewModel.allFindingsCount) - .tag(TableHealthScope.all) - if let report = viewModel.report { - Section(String(localized: "Checks")) { - ForEach(report.checks) { result in - Label(result.check.title, systemImage: result.check.systemImage) - .badge(badge(for: result)) - .help(tooltip(for: result)) - .tag(TableHealthScope.check(result.kind)) - } + TableHealthChecksOutline( + sections: sections, + scope: $viewModel.scope, + menu: { scope in TableHealthRowMenu.checks(scope: scope, resolvedCount: viewModel.resolvedCount) }, + perform: { command in + if command == .clearResolved { + viewModel.clearResolved() } - } - Section(String(localized: "History")) { - Label(String(localized: "Resolved"), systemImage: "clock.arrow.circlepath") - .badge(viewModel.resolvedCount) - .tag(TableHealthScope.resolved) - } - } - .listStyle(.sidebar) - .accessibilityLabel(String(localized: "Checks")) - .contextMenu(forSelectionType: TableHealthScope.self) { scopes in - if scopes.contains(.resolved), viewModel.resolvedCount > 0 { - Button(String(localized: "Clear Resolved")) { viewModel.clearResolved() } - } - } - // Without it Edit > Copy falls through to the window, which copies the sidebar's table names. - .onCopyCommand { - TableHealthCopy.providers(viewModel.selectionTSV()) + }, + copyText: { viewModel.selectionTSV() } + ) + } + + private var sections: [TableHealthChecksSection] { + var sections = [ + TableHealthChecksSection(title: nil, rows: [ + TableHealthChecksRow( + scope: .all, + title: String(localized: "All Findings"), + systemImage: "tray.full", + badge: Self.count(viewModel.allFindingsCount), + toolTip: nil + ), + ]), + ] + if let report = viewModel.report { + sections.append(TableHealthChecksSection( + title: String(localized: "Checks"), + rows: report.checks.map { result in + TableHealthChecksRow( + scope: .check(result.kind), + title: result.check.title, + systemImage: result.check.systemImage, + badge: badge(for: result), + toolTip: tooltip(for: result) + ) + } + )) } + sections.append(TableHealthChecksSection(title: String(localized: "History"), rows: [ + TableHealthChecksRow( + scope: .resolved, + title: String(localized: "Resolved"), + systemImage: "clock.arrow.circlepath", + badge: Self.count(viewModel.resolvedCount), + toolTip: nil + ), + ])) + return sections } - // A List cannot deselect into nothing here: some scope is always shown. - private var scopeSelection: Binding { - Binding( - get: { viewModel.scope }, - set: { scope in - guard let scope else { return } - viewModel.scope = scope - } - ) + private static func count(_ value: Int) -> String? { + value == 0 ? nil : value.formatted() } - private func badge(for result: TableHealthCheckResult) -> Text? { + private func badge(for result: TableHealthCheckResult) -> String? { switch result.outcome { case .completed: - let findings = viewModel.badgeCount(for: result.kind) - return findings == 0 ? nil : Text(verbatim: findings.formatted()) + return Self.count(viewModel.badgeCount(for: result.kind)) case .skipped: - return Text("Skipped") + return String(localized: "Skipped") case .failed: - return Text("Failed") + return String(localized: "Failed") } } - private func tooltip(for result: TableHealthCheckResult) -> String { + private func tooltip(for result: TableHealthCheckResult) -> String? { let text: String switch result.outcome { case .completed: text = result.check.summary case .skipped(let reason): text = reason case .failed(let message): text = message } - return text.count <= Self.tooltipLimit ? text : "" + return text.isEmpty || text.count > Self.tooltipLimit ? nil : text } } diff --git a/apple/TablePro/Views/TableHealth/TableHealthChecksOutline.swift b/apple/TablePro/Views/TableHealth/TableHealthChecksOutline.swift new file mode 100644 index 0000000000..2ba983d359 --- /dev/null +++ b/apple/TablePro/Views/TableHealth/TableHealthChecksOutline.swift @@ -0,0 +1,364 @@ +import AppKit +import SwiftUI + +struct TableHealthChecksRow: Equatable { + let scope: TableHealthScope + let title: String + let systemImage: String + let badge: String? + let toolTip: String? +} + +struct TableHealthChecksSection: Equatable { + let title: String? + let rows: [TableHealthChecksRow] +} + +// An NSOutlineView source list, not a SwiftUI List: under the app's bare NSHostingController a click on a +// SwiftUI List row lands on its cell hosting view, which never makes the list first responder, so the +// selection stays grey and the arrow keys reach whatever held the keyboard before (FavoritesOutlineView). +struct TableHealthChecksOutline: NSViewRepresentable { + let sections: [TableHealthChecksSection] + @Binding var scope: TableHealthScope + let menu: (TableHealthScope) -> [SidebarMenuSection] + let perform: (TableHealthRowCommand) -> Void + let copyText: () -> String? + + func makeCoordinator() -> TableHealthChecksCoordinator { + TableHealthChecksCoordinator(owner: self) + } + + func makeNSView(context: Context) -> NSScrollView { + let outlineView = TableHealthChecksNSOutlineView() + outlineView.style = .sourceList + outlineView.rowSizeStyle = .default + outlineView.headerView = nil + outlineView.allowsMultipleSelection = false + // Some scope is always shown, so the list never deselects into nothing. + outlineView.allowsEmptySelection = false + outlineView.floatsGroupRows = false + outlineView.autosaveExpandedItems = false + outlineView.setAccessibilityLabel(String(localized: "Checks")) + + let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("TableHealthChecksColumn")) + column.resizingMask = .autoresizingMask + outlineView.addTableColumn(column) + outlineView.outlineTableColumn = column + + outlineView.dataSource = context.coordinator + outlineView.delegate = context.coordinator + outlineView.copySource = context.coordinator + let menu = NSMenu() + menu.delegate = context.coordinator + outlineView.menu = menu + + let scrollView = NSScrollView() + scrollView.documentView = outlineView + scrollView.hasVerticalScroller = true + scrollView.hasHorizontalScroller = false + scrollView.autohidesScrollers = true + scrollView.drawsBackground = false + context.coordinator.attach(outlineView) + return scrollView + } + + func updateNSView(_ scrollView: NSScrollView, context: Context) { + context.coordinator.update(owner: self) + } +} + +final class TableHealthChecksNSOutlineView: NSOutlineView { + weak var copySource: (any TableHealthCopySource)? + + // Without it Edit > Copy falls through to the window, which copies the sidebar's table names. + @objc func copy(_ sender: Any?) { + guard let text = copySource?.copyText() else { return } + ClipboardService.shared.writeText(text) + } + + override func validateUserInterfaceItem(_ item: any NSValidatedUserInterfaceItem) -> Bool { + guard item.action == #selector(copy(_:)) else { return super.validateUserInterfaceItem(item) } + return copySource?.copyText() != nil + } +} + +final class TableHealthChecksNode: NSObject { + let id: String + var title: String + var row: TableHealthChecksRow? + var children: [TableHealthChecksNode] = [] + + init(id: String, title: String, row: TableHealthChecksRow?) { + self.id = id + self.title = title + self.row = row + } + + var isGroup: Bool { row == nil } +} + +@MainActor +final class TableHealthChecksCoordinator: NSObject, NSOutlineViewDataSource, NSOutlineViewDelegate, NSMenuDelegate, + TableHealthCopySource { + private static let rowCellIdentifier = NSUserInterfaceItemIdentifier("TableHealthCheckCell") + private static let headerCellIdentifier = NSUserInterfaceItemIdentifier("TableHealthChecksHeaderCell") + + private(set) var owner: TableHealthChecksOutline + private weak var outlineView: NSOutlineView? + // Kept by id and updated in place, so a refresh that changes only a badge keeps the rows and the selection. + private var nodes: [String: TableHealthChecksNode] = [:] + private var roots: [TableHealthChecksNode] = [] + private var structure: [String] = [] + private var isSyncing = false + + init(owner: TableHealthChecksOutline) { + self.owner = owner + super.init() + } + + static func nodeId(for scope: TableHealthScope) -> String { + switch scope { + case .all: return "all" + case .check(let kind): return "check." + kind.rawValue + case .resolved: return "resolved" + } + } + + func attach(_ outlineView: NSOutlineView) { + self.outlineView = outlineView + update(owner: owner) + } + + func update(owner: TableHealthChecksOutline) { + self.owner = owner + guard let outlineView else { return } + isSyncing = true + defer { isSyncing = false } + + roots = buildRoots(owner.sections) + let shape = roots.flatMap { [$0.id] + $0.children.map { " " + $0.id } } + if shape != structure { + structure = shape + outlineView.reloadData() + for node in roots where node.isGroup { + outlineView.expandItem(node) + } + } else { + refreshVisibleRows(in: outlineView) + } + + if let node = nodes[Self.nodeId(for: owner.scope)] { + let row = outlineView.row(forItem: node) + if row >= 0, outlineView.selectedRow != row { + outlineView.selectRowIndexes(IndexSet(integer: row), byExtendingSelection: false) + } + } + } + + private func buildRoots(_ sections: [TableHealthChecksSection]) -> [TableHealthChecksNode] { + var built: [TableHealthChecksNode] = [] + for section in sections { + let rows = section.rows.map { rowNode($0) } + guard let title = section.title else { + built += rows + continue + } + let group = node(id: "group." + title, title: title, row: nil) + group.children = rows + built.append(group) + } + return built + } + + private func rowNode(_ row: TableHealthChecksRow) -> TableHealthChecksNode { + node(id: Self.nodeId(for: row.scope), title: row.title, row: row) + } + + private func node(id: String, title: String, row: TableHealthChecksRow?) -> TableHealthChecksNode { + if let existing = nodes[id] { + existing.title = title + existing.row = row + return existing + } + let created = TableHealthChecksNode(id: id, title: title, row: row) + nodes[id] = created + return created + } + + private func refreshVisibleRows(in outlineView: NSOutlineView) { + for index in 0.. String? { + owner.copyText() + } + + func menuNeedsUpdate(_ menu: NSMenu) { + let clicked = outlineView.flatMap { $0.clickedRow >= 0 ? $0.item(atRow: $0.clickedRow) as? TableHealthChecksNode : nil } + let sections = clicked?.row.map { owner.menu($0.scope) } ?? [] + SidebarMenuBuilder.fill(menu, with: sections, target: self, action: #selector(performMenuCommand(_:))) + } + + @objc func performMenuCommand(_ sender: NSMenuItem) { + guard let box = sender.representedObject as? SidebarMenuCommandBox else { return } + owner.perform(box.command) + } + + // MARK: - Data source and delegate + + func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int { + (item as? TableHealthChecksNode)?.children.count ?? roots.count + } + + func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any { + ((item as? TableHealthChecksNode)?.children ?? roots)[index] + } + + func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool { + (item as? TableHealthChecksNode)?.isGroup ?? false + } + + func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool { + (item as? TableHealthChecksNode)?.isGroup ?? false + } + + func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool { + (item as? TableHealthChecksNode)?.isGroup == false + } + + func outlineView(_ outlineView: NSOutlineView, shouldShowOutlineCellForItem item: Any) -> Bool { + false + } + + func outlineView(_ outlineView: NSOutlineView, shouldCollapseItem item: Any) -> Bool { + false + } + + func outlineView(_ outlineView: NSOutlineView, typeSelectStringFor tableColumn: NSTableColumn?, item: Any) -> String? { + (item as? TableHealthChecksNode)?.row?.title + } + + func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? { + guard let node = item as? TableHealthChecksNode else { return nil } + guard let row = node.row else { + let cell = outlineView.makeView(withIdentifier: Self.headerCellIdentifier, owner: self) + as? TableHealthChecksHeaderCellView ?? TableHealthChecksHeaderCellView(identifier: Self.headerCellIdentifier) + cell.textField?.stringValue = node.title + return cell + } + let cell = outlineView.makeView(withIdentifier: Self.rowCellIdentifier, owner: self) + as? TableHealthCheckCellView ?? TableHealthCheckCellView(identifier: Self.rowCellIdentifier) + cell.configure(row) + return cell + } + + func outlineViewSelectionDidChange(_ notification: Notification) { + guard !isSyncing, let outlineView, + let scope = (outlineView.item(atRow: outlineView.selectedRow) as? TableHealthChecksNode)?.row?.scope, + scope != owner.scope + else { return } + owner.scope = scope + } +} + +// AppKit sets the title's font and the symbol's size from the row size and turns both white on the +// emphasized selection; the symbol slot and the badge follow the title's size. +final class TableHealthCheckCellView: NSTableCellView { + private let badge = NSTextField(labelWithString: "") + private var symbolWidth: NSLayoutConstraint? + + init(identifier: NSUserInterfaceItemIdentifier) { + super.init(frame: .zero) + self.identifier = identifier + let image = NSImageView() + let title = NSTextField(labelWithString: "") + title.lineBreakMode = .byTruncatingTail + title.maximumNumberOfLines = 1 + title.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) + badge.textColor = .secondaryLabelColor + badge.alignment = .right + badge.setContentHuggingPriority(.required, for: .horizontal) + badge.setContentCompressionResistancePriority(.required, for: .horizontal) + for view in [image, title, badge] { + view.translatesAutoresizingMaskIntoConstraints = false + addSubview(view) + } + imageView = image + textField = title + // One slot width for every symbol, so the titles line up. + let symbolWidth = image.widthAnchor.constraint(equalToConstant: Self.slotWidth(forTitleSize: NSFont.systemFontSize)) + self.symbolWidth = symbolWidth + NSLayoutConstraint.activate([ + image.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 2), + image.centerYAnchor.constraint(equalTo: centerYAnchor), + symbolWidth, + title.leadingAnchor.constraint(equalTo: image.trailingAnchor, constant: 6), + title.centerYAnchor.constraint(equalTo: centerYAnchor), + badge.leadingAnchor.constraint(greaterThanOrEqualTo: title.trailingAnchor, constant: 6), + badge.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -6), + badge.firstBaselineAnchor.constraint(equalTo: title.firstBaselineAnchor), + ]) + } + + @available(*, unavailable) + required init?(coder: NSCoder) { + fatalError("TableHealthCheckCellView does not support NSCoder init") + } + + private static func slotWidth(forTitleSize titleSize: CGFloat) -> CGFloat { + (titleSize * 1.75).rounded() + } + + func configure(_ row: TableHealthChecksRow) { + imageView?.image = NSImage(systemSymbolName: row.systemImage, accessibilityDescription: nil) + textField?.stringValue = row.title + badge.stringValue = row.badge ?? "" + badge.isHidden = row.badge == nil + toolTip = row.toolTip + } + + override func layout() { + if let size = textField?.font?.pointSize { + let width = Self.slotWidth(forTitleSize: size) + if let symbolWidth, symbolWidth.constant != width { + symbolWidth.constant = width + } + let font = NSFont.monospacedDigitSystemFont(ofSize: max(size - 2, NSFont.smallSystemFontSize - 1), weight: .regular) + if badge.font != font { + badge.font = font + } + } + super.layout() + } +} + +final class TableHealthChecksHeaderCellView: NSTableCellView { + init(identifier: NSUserInterfaceItemIdentifier) { + super.init(frame: .zero) + self.identifier = identifier + let title = NSTextField(labelWithString: "") + title.textColor = .secondaryLabelColor + title.lineBreakMode = .byTruncatingTail + title.translatesAutoresizingMaskIntoConstraints = false + addSubview(title) + textField = title + NSLayoutConstraint.activate([ + title.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 2), + title.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -2), + title.centerYAnchor.constraint(equalTo: centerYAnchor), + ]) + } + + @available(*, unavailable) + required init?(coder: NSCoder) { + fatalError("TableHealthChecksHeaderCellView does not support NSCoder init") + } +} diff --git a/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift b/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift index 568beade74..fabe591430 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift @@ -1,191 +1,169 @@ import AppKit import SwiftUI -// One fixed column set: an `if` among table columns is macOS 14.4, so the Check column stays in every scope. struct TableHealthFindingsTable: View { + static let columns: [TableHealthTableColumn] = [ + TableHealthTableColumn(.status, min: 40, ideal: 52, max: 60), + TableHealthTableColumn(.check, min: 80, ideal: 150), + TableHealthTableColumn(.table, min: 80, ideal: 160), + TableHealthTableColumn(.object, min: 60, ideal: 140), + TableHealthTableColumn(.size, min: 50, ideal: 72, max: 110), + TableHealthTableColumn(.detail, min: 120, ideal: 280), + ] + @ObservedObject var viewModel: TableHealthViewModel let databaseType: DatabaseType let actions: TableHealthActions var body: some View { - Table(viewModel.rows, selection: $viewModel.selection, sortOrder: $viewModel.sortOrder) { - TableColumn(TableHealthColumn.status.title, value: \TableHealthFindingRow.statusRank) { row in - Text(verbatim: TableHealthColumn.status.text(for: row)) - } - .width(min: 40, ideal: 52, max: 60) - TableColumn(TableHealthColumn.check.title, value: \TableHealthFindingRow.checkOrder) { row in - Text(verbatim: row.checkTitle) - .lineLimit(1) - } - .width(min: 80, ideal: 150) - TableColumn( - TableHealthColumn.table.title, - value: \TableHealthFindingRow.tableSortKey, - comparator: String.StandardComparator.localizedStandard - ) { row in - Text(verbatim: row.tableSortKey) - .lineLimit(1) - .truncationMode(.middle) - } - .width(min: 80, ideal: 160) - TableColumn( - TableHealthColumn.object.title, - value: \TableHealthFindingRow.objectSortKey, - comparator: String.StandardComparator.localizedStandard - ) { row in - Text(verbatim: row.objectSortKey) - .lineLimit(1) - .truncationMode(.middle) - } - .width(min: 60, ideal: 140) - TableColumn(TableHealthColumn.size.title, value: \TableHealthFindingRow.sizeSortKey) { row in - Text(verbatim: TableHealthColumn.size.text(for: row)) - .monospacedDigit() - .frame(maxWidth: .infinity, alignment: .trailing) - } - .width(min: 50, ideal: 72, max: 110) - TableColumn( - TableHealthColumn.detail.title, - value: \TableHealthFindingRow.detail, - comparator: String.StandardComparator.localizedStandard - ) { row in - Text(verbatim: row.detail) - .lineLimit(1) - .help(row.detail) - } - .width(min: 120, ideal: 280) - } - .tableStyle(.inset) - .alternatingRowBackgroundsCompat() - .accessibilityLabel(String(localized: "Findings")) - .contextMenu(forSelectionType: TableHealthFindingID.self) { ids in - menu(for: viewModel.rows(for: ids)) - } primaryAction: { ids in - if let finding = Self.singleTableFinding(in: viewModel.rows(for: ids)) { - actions.openTable(finding) - } - } - .onCopyCommand { - TableHealthCopy.providers(viewModel.selectionTSV()) - } + TableHealthRowsTable( + rows: viewModel.rows, + columns: Self.columns, + selection: $viewModel.selection, + sortOrder: $viewModel.sortOrder, + accessibilityLabel: String(localized: "Findings"), + menu: TableHealthRowMenu.findings, + perform: { command, rows in perform(command, on: rows) }, + primaryAction: { rows in + if let finding = TableHealthRowMenu.singleTableFinding(in: rows) { + actions.openTable(finding) + } + }, + copyText: { viewModel.selectionTSV() } + ) } - // HIG, Context menus: unavailable items are left out, Copy aside. - @ViewBuilder - private func menu(for rows: [TableHealthFindingRow]) -> some View { - if !rows.isEmpty { - let findings = rows.compactMap(\.finding) - let fixCount = findings.filter(TableHealthViewModel.hasFix).count - if let finding = Self.singleTableFinding(in: rows) { - Button(String(localized: "Open Table")) { actions.openTable(finding) } - } - if fixCount > 0 { - Button(fixCount == 1 ? String(localized: "Open Fix in Editor") : String(localized: "Open Fixes in Editor")) { - if let text = viewModel.fixText(for: findings, databaseType: databaseType) { - actions.openInEditor(text) - } - } + private func perform(_ command: TableHealthRowCommand, on rows: [TableHealthFindingRow]) { + let findings = rows.compactMap(\.finding) + switch command { + case .openTable: + if let finding = TableHealthRowMenu.singleTableFinding(in: rows) { + actions.openTable(finding) } - Divider() - Button(String(localized: "Copy")) { - if let text = viewModel.tsv(for: rows) { - ClipboardService.shared.writeText(text) - } + case .openFixes: + if let text = viewModel.fixText(for: findings, databaseType: databaseType) { + actions.openInEditor(text) } - if fixCount > 0 { - Button(String(localized: "Copy Fix SQL")) { - if let text = viewModel.fixText(for: findings, databaseType: databaseType) { - ClipboardService.shared.writeText(text) - } - } + case .copy: + if let text = viewModel.tsv(for: rows) { + ClipboardService.shared.writeText(text) } - Divider() - let ids = Set(rows.map(\.id)) - if rows.allSatisfy({ $0.status == .ignored }) { - Button(String(localized: "Stop Ignoring")) { viewModel.setIgnored(ids, ignored: false) } - } else { - Button(String(localized: "Ignore")) { viewModel.setIgnored(ids, ignored: true) } + case .copyFixSQL: + if let text = viewModel.fixText(for: findings, databaseType: databaseType) { + ClipboardService.shared.writeText(text) } - if fixCount > 0 { - Button(String(localized: "Save Fix Script…")) { - if let script = viewModel.fixScript(for: findings, databaseType: databaseType) { - actions.saveFixScript(script) - } - } + case .ignore: + viewModel.setIgnored(Set(rows.map(\.id)), ignored: true) + case .stopIgnoring: + viewModel.setIgnored(Set(rows.map(\.id)), ignored: false) + case .saveFixScript: + if let script = viewModel.fixScript(for: findings, databaseType: databaseType) { + actions.saveFixScript(script) } + case .clearResolved: + viewModel.clearResolved() } } - - // Open Table applies when the rows name one table, however many findings it has. - static func singleTableFinding(in rows: [TableHealthFindingRow]) -> TableHealthFinding? { - let findings = rows.compactMap(\.finding).filter { $0.table != nil } - guard let first = findings.first, - findings.allSatisfy({ $0.schema == first.schema && $0.table == first.table }) - else { return nil } - return first - } } struct TableHealthResolvedTable: View { + static let columns: [TableHealthTableColumn] = [ + TableHealthTableColumn(.check, min: 80, ideal: 150), + TableHealthTableColumn(.table, min: 80, ideal: 180), + TableHealthTableColumn(.object, min: 60, ideal: 160), + TableHealthTableColumn(.lastSeen, min: 100, ideal: 160), + ] + @ObservedObject var viewModel: TableHealthViewModel var body: some View { - Table(viewModel.rows, selection: $viewModel.selection, sortOrder: $viewModel.resolvedSortOrder) { - TableColumn(TableHealthColumn.check.title, value: \TableHealthFindingRow.checkOrder) { row in - Text(verbatim: row.checkTitle) - .lineLimit(1) - } - .width(min: 80, ideal: 150) - TableColumn( - TableHealthColumn.table.title, - value: \TableHealthFindingRow.tableSortKey, - comparator: String.StandardComparator.localizedStandard - ) { row in - Text(verbatim: row.tableSortKey) - .lineLimit(1) - .truncationMode(.middle) - } - .width(min: 80, ideal: 180) - TableColumn( - TableHealthColumn.object.title, - value: \TableHealthFindingRow.objectSortKey, - comparator: String.StandardComparator.localizedStandard - ) { row in - Text(verbatim: row.objectSortKey) - .lineLimit(1) - .truncationMode(.middle) - } - .width(min: 60, ideal: 160) - TableColumn(TableHealthColumn.lastSeen.title, value: \TableHealthFindingRow.lastSeenSortKey) { row in - Text(verbatim: TableHealthColumn.lastSeen.text(for: row)) - .monospacedDigit() - } - .width(min: 100, ideal: 160) - } - .tableStyle(.inset) - .alternatingRowBackgroundsCompat() - .accessibilityLabel(String(localized: "Resolved Findings")) - .contextMenu(forSelectionType: TableHealthFindingID.self) { ids in - let rows = viewModel.rows(for: ids) - if !rows.isEmpty { - Button(String(localized: "Copy")) { + TableHealthRowsTable( + rows: viewModel.rows, + columns: Self.columns, + selection: $viewModel.selection, + sortOrder: $viewModel.resolvedSortOrder, + accessibilityLabel: String(localized: "Resolved Findings"), + menu: TableHealthRowMenu.resolved, + perform: { command, rows in + switch command { + case .copy: if let text = viewModel.tsv(for: rows) { ClipboardService.shared.writeText(text) } + case .clearResolved: + viewModel.clearResolved() + case .openTable, .openFixes, .copyFixSQL, .ignore, .stopIgnoring, .saveFixScript: + break } - Divider() - } - Button(String(localized: "Clear Resolved")) { viewModel.clearResolved() } + }, + copyText: { viewModel.selectionTSV() } + ) + } +} + +enum TableHealthRowCommand: Equatable, SidebarMenuShortcutProviding { + case openTable + case openFixes + case copy + case copyFixSQL + case ignore + case stopIgnoring + case saveFixScript + case clearResolved + + var shortcutAction: ShortcutAction? { nil } +} + +// HIG, Context menus: unavailable items are left out, Copy aside. +enum TableHealthRowMenu { + static func findings(_ rows: [TableHealthFindingRow]) -> [SidebarMenuSection] { + guard !rows.isEmpty else { return [] } + let fixCount = rows.compactMap(\.finding).filter(TableHealthViewModel.hasFix).count + + var open: [SidebarMenuItem] = [] + if singleTableFinding(in: rows) != nil { + open.append(.command(String(localized: "Open Table"), .openTable)) } - .onCopyCommand { - TableHealthCopy.providers(viewModel.selectionTSV()) + if fixCount > 0 { + let title = fixCount == 1 ? String(localized: "Open Fix in Editor") : String(localized: "Open Fixes in Editor") + open.append(.command(title, .openFixes)) + } + + var copy: [SidebarMenuItem] = [.command(String(localized: "Copy"), .copy)] + if fixCount > 0 { + copy.append(.command(String(localized: "Copy Fix SQL"), .copyFixSQL)) } + + var manage: [SidebarMenuItem] = [] + if rows.allSatisfy({ $0.status == .ignored }) { + manage.append(.command(String(localized: "Stop Ignoring"), .stopIgnoring)) + } else { + manage.append(.command(String(localized: "Ignore"), .ignore)) + } + if fixCount > 0 { + manage.append(.command(String(localized: "Save Fix Script…"), .saveFixScript)) + } + return [SidebarMenuSection(open), SidebarMenuSection(copy), SidebarMenuSection(manage)] + } + + static func resolved(_ rows: [TableHealthFindingRow]) -> [SidebarMenuSection] { + let copy: [SidebarMenuItem] = rows.isEmpty + ? [] + : [.command(String(localized: "Copy"), .copy)] + return [SidebarMenuSection(copy), SidebarMenuSection([.command(String(localized: "Clear Resolved"), .clearResolved)])] + } + + static func checks(scope: TableHealthScope?, resolvedCount: Int) -> [SidebarMenuSection] { + guard scope == .resolved, resolvedCount > 0 else { return [] } + return [SidebarMenuSection([.command(String(localized: "Clear Resolved"), .clearResolved)])] } -} -enum TableHealthCopy { - static func providers(_ text: String?) -> [NSItemProvider] { - guard let text else { return [] } - return [NSItemProvider(object: text as NSString)] + // Open Table applies when the rows name one table, however many findings it has. + static func singleTableFinding(in rows: [TableHealthFindingRow]) -> TableHealthFinding? { + let findings = rows.compactMap(\.finding).filter { $0.table != nil } + guard let first = findings.first, + findings.allSatisfy({ $0.schema == first.schema && $0.table == first.table }) + else { return nil } + return first } } diff --git a/apple/TablePro/Views/TableHealth/TableHealthRowsTable.swift b/apple/TablePro/Views/TableHealth/TableHealthRowsTable.swift new file mode 100644 index 0000000000..a86a97c1d3 --- /dev/null +++ b/apple/TablePro/Views/TableHealth/TableHealthRowsTable.swift @@ -0,0 +1,384 @@ +import AppKit +import SwiftUI + +struct TableHealthTableColumn: Sendable { + let column: TableHealthColumn + let minWidth: CGFloat + let idealWidth: CGFloat + let maxWidth: CGFloat? + + init(_ column: TableHealthColumn, min: CGFloat, ideal: CGFloat, max: CGFloat? = nil) { + self.column = column + minWidth = min + idealWidth = ideal + maxWidth = max + } +} + +// An NSTableView, not a SwiftUI Table: under the app's bare NSHostingController a click on a SwiftUI +// Table row lands on its cell hosting view, which handles it without making the table first responder, +// so the selection stays grey and the arrow keys reach whatever held the keyboard before. +struct TableHealthRowsTable: NSViewRepresentable { + let rows: [TableHealthFindingRow] + let columns: [TableHealthTableColumn] + @Binding var selection: Set + @Binding var sortOrder: [KeyPathComparator] + let accessibilityLabel: String + let menu: ([TableHealthFindingRow]) -> [SidebarMenuSection] + let perform: (TableHealthRowCommand, [TableHealthFindingRow]) -> Void + var primaryAction: (([TableHealthFindingRow]) -> Void)? + let copyText: () -> String? + + func makeCoordinator() -> TableHealthRowsTableCoordinator { + TableHealthRowsTableCoordinator(owner: self) + } + + func makeNSView(context: Context) -> NSScrollView { + let tableView = TableHealthNSTableView() + tableView.style = .inset + tableView.usesAlternatingRowBackgroundColors = true + tableView.allowsMultipleSelection = true + tableView.allowsEmptySelection = true + tableView.allowsColumnReordering = false + tableView.allowsColumnResizing = true + tableView.columnAutoresizingStyle = .uniformColumnAutoresizingStyle + tableView.setAccessibilityLabel(accessibilityLabel) + for spec in columns { + let column = NSTableColumn(identifier: spec.column.identifier) + column.title = spec.column.title + column.minWidth = spec.minWidth + column.width = spec.idealWidth + if let maxWidth = spec.maxWidth { + column.maxWidth = maxWidth + } + column.sortDescriptorPrototype = NSSortDescriptor(key: spec.column.identifier.rawValue, ascending: true) + tableView.addTableColumn(column) + } + + tableView.dataSource = context.coordinator + tableView.delegate = context.coordinator + tableView.target = context.coordinator + tableView.doubleAction = #selector(TableHealthRowsTableCoordinator.handleDoubleClick) + tableView.copySource = context.coordinator + tableView.primaryActionSource = context.coordinator + // The table owns the menu, so AppKit sets `clickedRow` and draws the clicked-row outline. + let menu = NSMenu() + menu.delegate = context.coordinator + tableView.menu = menu + + let scrollView = NSScrollView() + scrollView.documentView = tableView + scrollView.hasVerticalScroller = true + scrollView.hasHorizontalScroller = true + scrollView.autohidesScrollers = true + scrollView.drawsBackground = false + context.coordinator.attach(tableView) + return scrollView + } + + func updateNSView(_ scrollView: NSScrollView, context: Context) { + context.coordinator.update(owner: self) + } +} + +@MainActor +protocol TableHealthCopySource: AnyObject { + func copyText() -> String? +} + +@MainActor +protocol TableHealthPrimaryActionSource: AnyObject { + func performPrimaryAction() -> Bool +} + +// Return runs the primary action and Edit > Copy copies the selection; NSTableView routes neither on its own. +final class TableHealthNSTableView: NSTableView { + weak var copySource: (any TableHealthCopySource)? + weak var primaryActionSource: (any TableHealthPrimaryActionSource)? + + // Measured: NSTableView takes Return in keyDown and never sends insertNewline(_:). + override func keyDown(with event: NSEvent) { + let modifiers = event.modifierFlags.intersection(.deviceIndependentFlagsMask) + let key = KeyCode(rawValue: event.keyCode) + if key == .return || key == .enter, + modifiers.isDisjoint(with: [.command, .option, .control, .shift]), + primaryActionSource?.performPrimaryAction() == true { + return + } + super.keyDown(with: event) + } + + @objc func copy(_ sender: Any?) { + guard let text = copySource?.copyText() else { return } + ClipboardService.shared.writeText(text) + } + + override func validateUserInterfaceItem(_ item: any NSValidatedUserInterfaceItem) -> Bool { + guard item.action == #selector(copy(_:)) else { return super.validateUserInterfaceItem(item) } + return copySource?.copyText() != nil + } +} + +@MainActor +final class TableHealthRowsTableCoordinator: NSObject, NSTableViewDataSource, NSTableViewDelegate, NSMenuDelegate, + TableHealthCopySource, TableHealthPrimaryActionSource { + private static let cellIdentifier = NSUserInterfaceItemIdentifier("TableHealthTextCell") + + // What a reload has to redraw. Rows equal in all of it keep their views and the selection. + private struct RowKey: Equatable { + let id: TableHealthFindingID + let status: TableHealthRowStatus + let checkTitle: String + let size: Int64 + let detail: String + let lastSeen: Date + + init(_ row: TableHealthFindingRow) { + id = row.id + status = row.status + checkTitle = row.checkTitle + size = row.sizeSortKey + detail = row.detail + lastSeen = row.lastSeenSortKey + } + } + + private(set) var owner: TableHealthRowsTable + private weak var tableView: NSTableView? + private var rows: [TableHealthFindingRow] = [] + private var rowKeys: [RowKey] = [] + private var menuRows: [TableHealthFindingRow] = [] + private var isSyncing = false + + init(owner: TableHealthRowsTable) { + self.owner = owner + super.init() + } + + func attach(_ tableView: NSTableView) { + self.tableView = tableView + update(owner: owner) + } + + func update(owner: TableHealthRowsTable) { + self.owner = owner + guard let tableView else { return } + isSyncing = true + defer { isSyncing = false } + + rows = owner.rows + let keys = owner.rows.map(RowKey.init) + if keys != rowKeys { + rowKeys = keys + tableView.reloadData() + } + + let descriptors = TableHealthTableSort.descriptors(for: owner.sortOrder, columns: columnKinds) + if !TableHealthTableSort.same(tableView.sortDescriptors, descriptors) { + tableView.sortDescriptors = descriptors + } + + let wanted = IndexSet(rows.indices.filter { owner.selection.contains(rows[$0].id) }) + if wanted != tableView.selectedRowIndexes { + tableView.selectRowIndexes(wanted, byExtendingSelection: false) + } + } + + private var columnKinds: [TableHealthColumn] { + owner.columns.map(\.column) + } + + private func rows(at indexes: IndexSet) -> [TableHealthFindingRow] { + indexes.compactMap { rows.indices.contains($0) ? rows[$0] : nil } + } + + // A right-click on a selected row acts on the whole selection, on any other row on that row alone. + private func clickedRows() -> [TableHealthFindingRow] { + guard let tableView, rows.indices.contains(tableView.clickedRow) else { return [] } + if tableView.selectedRowIndexes.contains(tableView.clickedRow) { + return rows(at: tableView.selectedRowIndexes) + } + return [rows[tableView.clickedRow]] + } + + // MARK: - Actions + + @objc func handleDoubleClick() { + guard let tableView, tableView.clickedRow >= 0 else { return } + owner.primaryAction?(clickedRows()) + } + + func performPrimaryAction() -> Bool { + guard let primaryAction = owner.primaryAction, let tableView else { return false } + primaryAction(rows(at: tableView.selectedRowIndexes)) + return true + } + + func copyText() -> String? { + owner.copyText() + } + + func menuNeedsUpdate(_ menu: NSMenu) { + menuRows = clickedRows() + SidebarMenuBuilder.fill( + menu, + with: owner.menu(menuRows), + target: self, + action: #selector(performMenuCommand(_:)) + ) + } + + @objc func performMenuCommand(_ sender: NSMenuItem) { + guard let box = sender.representedObject as? SidebarMenuCommandBox else { return } + owner.perform(box.command, menuRows) + } + + // MARK: - Data source and delegate + + func numberOfRows(in tableView: NSTableView) -> Int { + rows.count + } + + func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { + guard let tableColumn, rows.indices.contains(row), + let column = columnKinds.first(where: { $0.identifier == tableColumn.identifier }) + else { return nil } + let cell = tableView.makeView(withIdentifier: Self.cellIdentifier, owner: self) as? TableHealthTextCellView + ?? TableHealthTextCellView(identifier: Self.cellIdentifier) + cell.configure(column: column, row: rows[row]) + return cell + } + + func tableViewSelectionDidChange(_ notification: Notification) { + guard !isSyncing, let tableView else { return } + owner.selection = Set(rows(at: tableView.selectedRowIndexes).map(\.id)) + } + + func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor]) { + guard !isSyncing else { return } + owner.sortOrder = TableHealthTableSort.comparators(for: tableView.sortDescriptors, columns: columnKinds) + } +} + +final class TableHealthTextCellView: NSTableCellView { + init(identifier: NSUserInterfaceItemIdentifier) { + super.init(frame: .zero) + self.identifier = identifier + let field = NSTextField(labelWithString: "") + field.translatesAutoresizingMaskIntoConstraints = false + field.maximumNumberOfLines = 1 + field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) + addSubview(field) + textField = field + NSLayoutConstraint.activate([ + field.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 2), + field.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -2), + field.centerYAnchor.constraint(equalTo: centerYAnchor), + ]) + } + + @available(*, unavailable) + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + func configure(column: TableHealthColumn, row: TableHealthFindingRow) { + guard let field = textField else { return } + let text = column.text(for: row) + field.stringValue = text + field.lineBreakMode = column.lineBreakMode + field.alignment = column.alignment + field.font = column.usesMonospacedDigits + ? .monospacedDigitSystemFont(ofSize: NSFont.systemFontSize, weight: .regular) + : .systemFont(ofSize: NSFont.systemFontSize) + field.toolTip = column == .detail && !text.isEmpty ? text : nil + } +} + +extension TableHealthColumn { + var identifier: NSUserInterfaceItemIdentifier { + switch self { + case .status: return NSUserInterfaceItemIdentifier("status") + case .check: return NSUserInterfaceItemIdentifier("check") + case .table: return NSUserInterfaceItemIdentifier("table") + case .object: return NSUserInterfaceItemIdentifier("object") + case .size: return NSUserInterfaceItemIdentifier("size") + case .detail: return NSUserInterfaceItemIdentifier("detail") + case .lastSeen: return NSUserInterfaceItemIdentifier("lastSeen") + } + } + + var lineBreakMode: NSLineBreakMode { + switch self { + case .table, .object: return .byTruncatingMiddle + case .status, .check, .size, .detail, .lastSeen: return .byTruncatingTail + } + } + + var alignment: NSTextAlignment { + self == .size ? .right : .natural + } + + var usesMonospacedDigits: Bool { + self == .size || self == .lastSeen + } +} + +// The header's sort descriptors and the view model's comparators, each derived from the other. +enum TableHealthTableSort { + static func comparator(for column: TableHealthColumn, order: SortOrder) -> KeyPathComparator { + switch column { + case .status: + return KeyPathComparator(\TableHealthFindingRow.statusRank, order: order) + case .check: + return KeyPathComparator(\TableHealthFindingRow.checkOrder, order: order) + case .table: + return KeyPathComparator( + \TableHealthFindingRow.tableSortKey, + comparator: String.StandardComparator.localizedStandard, + order: order + ) + case .object: + return KeyPathComparator( + \TableHealthFindingRow.objectSortKey, + comparator: String.StandardComparator.localizedStandard, + order: order + ) + case .size: + return KeyPathComparator(\TableHealthFindingRow.sizeSortKey, order: order) + case .detail: + return KeyPathComparator( + \TableHealthFindingRow.detail, + comparator: String.StandardComparator.localizedStandard, + order: order + ) + case .lastSeen: + return KeyPathComparator(\TableHealthFindingRow.lastSeenSortKey, order: order) + } + } + + static func comparators( + for descriptors: [NSSortDescriptor], + columns: [TableHealthColumn] + ) -> [KeyPathComparator] { + descriptors.compactMap { descriptor in + guard let column = columns.first(where: { $0.identifier.rawValue == descriptor.key }) else { return nil } + return comparator(for: column, order: descriptor.ascending ? .forward : .reverse) + } + } + + static func descriptors( + for comparators: [KeyPathComparator], + columns: [TableHealthColumn] + ) -> [NSSortDescriptor] { + comparators.compactMap { sort in + guard let column = columns.first(where: { comparator(for: $0, order: .forward).keyPath == sort.keyPath }) + else { return nil } + return NSSortDescriptor(key: column.identifier.rawValue, ascending: sort.order == .forward) + } + } + + static func same(_ lhs: [NSSortDescriptor], _ rhs: [NSSortDescriptor]) -> Bool { + lhs.map(\.key) == rhs.map(\.key) && lhs.map(\.ascending) == rhs.map(\.ascending) + } +} diff --git a/apple/TableProTests/Views/TableHealth/TableHealthTablesTests.swift b/apple/TableProTests/Views/TableHealth/TableHealthTablesTests.swift new file mode 100644 index 0000000000..ccda1a6e40 --- /dev/null +++ b/apple/TableProTests/Views/TableHealth/TableHealthTablesTests.swift @@ -0,0 +1,244 @@ +import AppKit +import Foundation +import SwiftUI +@testable import TablePro +import Testing + +// The checks list and the findings table were SwiftUI, whose cell hosting view takes a row click without making +// the list first responder: the selection drew grey and the arrow keys went to the database sidebar. +@MainActor +struct TableHealthTablesTests { + // The test host never gives a window the keyboard, so the window says it has it. AppKit still draws no + // emphasis here, so the accent selection is measured in the running app; this pins the first responder + // AppKit derives it from. + private final class KeyWindow: NSWindow { + override var isKeyWindow: Bool { true } + } + + @MainActor + private final class State { + var selection: Set = [] + var sortOrder: [KeyPathComparator] = [KeyPathComparator(\TableHealthFindingRow.checkOrder)] + var scope: TableHealthScope = .all + var primaryActionRows: [[TableHealthFindingID]] = [] + } + + private struct Hosted { + let window: KeyWindow + let other: NSTextField + let root: NSView + + func first(_ type: Found.Type) -> Found? { + Self.descendants(of: root).lazy.compactMap { $0 as? Found }.first + } + + private static func descendants(of view: NSView) -> [NSView] { + [view] + view.subviews.flatMap(descendants(of:)) + } + + func close() { + window.orderOut(nil) + } + } + + private nonisolated static func row(_ table: String, kind: TableHealthCheckKind = .missingPrimaryKey) -> TableHealthFindingRow { + TableHealthFindingRow( + finding: TableHealthFixture.finding(kind, table: table), + checkTitle: kind.title, + checkOrder: 0, + status: .seen, + firstSeen: nil, + lastSeen: nil + ) + } + + private nonisolated static let rows = ["orders", "events", "audit_log"].map { row($0) } + + // The keyboard starts in another view, the way the database sidebar holds it when the tab opens. + private func host(_ content: some View) -> Hosted { + let frame = NSRect(x: 0, y: 0, width: 700, height: 420) + let window = KeyWindow(contentRect: frame, styleMask: [.titled], backing: .buffered, defer: false) + window.isReleasedWhenClosed = false + let container = NSView(frame: frame) + let other = NSTextField(frame: NSRect(x: 0, y: 390, width: 200, height: 22)) + let hosting = NSHostingView(rootView: content) + hosting.frame = NSRect(x: 0, y: 0, width: 700, height: 380) + container.addSubview(hosting) + container.addSubview(other) + window.contentView = container + hosting.layoutSubtreeIfNeeded() + window.makeFirstResponder(other) + return Hosted(window: window, other: other, root: hosting) + } + + // A click goes to the view under the pointer, which is what a SwiftUI cell took for itself. NSTableView + // tracks the press until the mouse-up, so the mouse-up waits in the queue first. + private func click(row: Int, in tableView: NSTableView, window: NSWindow) throws { + tableView.layoutSubtreeIfNeeded() + let rect = tableView.rect(ofRow: row) + let location = tableView.convert(NSPoint(x: rect.minX + 40, y: rect.midY), to: nil) + func event(_ type: NSEvent.EventType) throws -> NSEvent { + try #require(NSEvent.mouseEvent( + with: type, + location: location, + modifierFlags: [], + timestamp: ProcessInfo.processInfo.systemUptime, + windowNumber: window.windowNumber, + context: nil, + eventNumber: 0, + clickCount: 1, + pressure: type == .leftMouseDown ? 1 : 0 + )) + } + let up = try event(.leftMouseUp) + let down = try event(.leftMouseDown) + let target = try #require(window.contentView?.hitTest(location)) + NSApp.postEvent(up, atStart: true) + target.mouseDown(with: down) + _ = NSApp.nextEvent(matching: .leftMouseUp, until: .distantPast, inMode: .default, dequeue: true) + } + + private func press(_ key: KeyCode, characters: String, in view: NSView, window: NSWindow) throws { + let modifiers: NSEvent.ModifierFlags = key.isArrowKey ? [.numericPad, .function] : [] + let event = try #require(NSEvent.keyEvent( + with: .keyDown, + location: .zero, + modifierFlags: modifiers, + timestamp: ProcessInfo.processInfo.systemUptime, + windowNumber: window.windowNumber, + context: nil, + characters: characters, + charactersIgnoringModifiers: characters, + isARepeat: false, + keyCode: key.rawValue + )) + view.keyDown(with: event) + } + + private func pressDownArrow(in view: NSView, window: NSWindow) throws { + try press(.downArrow, characters: String(Character(UnicodeScalar(NSDownArrowFunctionKey)!)), in: view, window: window) + } + + private func findingsTable(_ state: State) -> TableHealthRowsTable { + TableHealthRowsTable( + rows: Self.rows, + columns: TableHealthFindingsTable.columns, + selection: Binding(get: { state.selection }, set: { state.selection = $0 }), + sortOrder: Binding(get: { state.sortOrder }, set: { state.sortOrder = $0 }), + accessibilityLabel: "Findings", + menu: TableHealthRowMenu.findings, + perform: { _, _ in }, + primaryAction: { rows in state.primaryActionRows.append(rows.map(\.id)) }, + copyText: { nil } + ) + } + + @Test("A click on a finding takes the keyboard, the arrow keys move it, and Return opens it") + func clickFocusesTheFindingsTable() throws { + let state = State() + let hosted = host(findingsTable(state)) + defer { hosted.close() } + let tableView = try #require(hosted.first(TableHealthNSTableView.self)) + #expect(hosted.window.firstResponder !== tableView) + + try click(row: 1, in: tableView, window: hosted.window) + + #expect(hosted.window.firstResponder === tableView) + #expect(state.selection == [Self.rows[1].id]) + + try pressDownArrow(in: tableView, window: hosted.window) + #expect(state.selection == [Self.rows[2].id]) + + try press(.return, characters: "\r", in: tableView, window: hosted.window) + #expect(state.primaryActionRows == [[Self.rows[2].id]]) + } + + @Test("A click on a check takes the keyboard, and the arrow keys skip the section headers") + func clickFocusesTheChecksList() throws { + let state = State() + let sections = [ + TableHealthChecksSection(title: nil, rows: [ + TableHealthChecksRow(scope: .all, title: "All Findings", systemImage: "tray.full", badge: "3", toolTip: nil), + ]), + TableHealthChecksSection(title: "Checks", rows: [ + TableHealthChecksRow( + scope: .check(.missingPrimaryKey), title: "Keys", systemImage: "key", badge: "3", toolTip: nil + ), + TableHealthChecksRow( + scope: .check(.unusedIndex), title: "Unused", systemImage: "list.bullet.indent", badge: nil, toolTip: nil + ), + ]), + TableHealthChecksSection(title: "History", rows: [ + TableHealthChecksRow( + scope: .resolved, title: "Resolved", systemImage: "clock.arrow.circlepath", badge: nil, toolTip: nil + ), + ]), + ] + let hosted = host(TableHealthChecksOutline( + sections: sections, + scope: Binding(get: { state.scope }, set: { state.scope = $0 }), + menu: { _ in [] }, + perform: { _ in }, + copyText: { nil } + )) + defer { hosted.close() } + let outlineView = try #require(hosted.first(TableHealthChecksNSOutlineView.self)) + #expect(outlineView.selectedRow == 0) + + // Rows: All Findings, the Checks header, Keys, Unused, the History header, Resolved. + try click(row: 3, in: outlineView, window: hosted.window) + + #expect(hosted.window.firstResponder === outlineView) + #expect(state.scope == .check(.unusedIndex)) + + try pressDownArrow(in: outlineView, window: hosted.window) + #expect(state.scope == .resolved) + } + + @Test("A header click becomes the view model's comparators, and its sort shows on the header") + func sortDescriptorsMapToComparators() { + let columns = TableHealthFindingsTable.columns.map(\.column) + let comparators = TableHealthTableSort.comparators( + for: [NSSortDescriptor(key: "table", ascending: false), NSSortDescriptor(key: "check", ascending: true)], + columns: columns + ) + + let sorted = Self.rows.sorted(using: comparators).map { $0.tableSortKey } + #expect(sorted == ["public.orders", "public.events", "public.audit_log"]) + let descriptors = TableHealthTableSort.descriptors(for: comparators, columns: columns) + #expect(descriptors.map { $0.key } == ["table", "check"]) + #expect(descriptors.map { $0.ascending } == [false, true]) + } + + @Test("The Resolved table shows its newest-first sort on the Last Seen header") + func resolvedSortShowsOnItsHeader() { + let descriptors = TableHealthTableSort.descriptors( + for: [KeyPathComparator(\TableHealthFindingRow.lastSeenSortKey, order: .reverse)], + columns: TableHealthResolvedTable.columns.map(\.column) + ) + + #expect(descriptors.map { $0.key } == ["lastSeen"]) + #expect(descriptors.map { $0.ascending } == [false]) + } + + @Test("A finding's menu leaves out what does not apply") + func findingsMenuLeavesOutWhatDoesNotApply() { + let twoTables = TableHealthRowMenu.findings([Self.rows[0], Self.rows[1]]) + let titles = twoTables.map { section in + section.items.compactMap { item -> String? in + guard case .command(let entry) = item else { return nil } + return entry.title + } + } + + #expect(titles == [ + ["Open Fixes in Editor"], + ["Copy", "Copy Fix SQL"], + ["Ignore", "Save Fix Script…"], + ]) + #expect(TableHealthRowMenu.findings([]).isEmpty) + #expect(TableHealthRowMenu.checks(scope: .resolved, resolvedCount: 0).isEmpty) + #expect(TableHealthRowMenu.checks(scope: .resolved, resolvedCount: 2).count == 1) + #expect(TableHealthRowMenu.resolved([]).nonEmptySections().count == 1) + } +} From 034358ad7239b0e2c3d1d6a1ffa81eb240432dfb Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Tue, 10 Nov 2026 12:13:40 +0700 Subject: [PATCH 12/13] fix(table-health): reload findings rows only when their shown text changes --- .../TableHealth/TableHealthRowsTable.swift | 20 +++++++------------ 1 file changed, 7 insertions(+), 13 deletions(-) diff --git a/apple/TablePro/Views/TableHealth/TableHealthRowsTable.swift b/apple/TablePro/Views/TableHealth/TableHealthRowsTable.swift index a86a97c1d3..3f179fa246 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthRowsTable.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthRowsTable.swift @@ -124,22 +124,15 @@ final class TableHealthRowsTableCoordinator: NSObject, NSTableViewDataSource, NS TableHealthCopySource, TableHealthPrimaryActionSource { private static let cellIdentifier = NSUserInterfaceItemIdentifier("TableHealthTextCell") - // What a reload has to redraw. Rows equal in all of it keep their views and the selection. + // What a reload has to redraw: each row's id and the text its columns show. Rows equal in all of it keep their + // views and the selection. private struct RowKey: Equatable { let id: TableHealthFindingID - let status: TableHealthRowStatus - let checkTitle: String - let size: Int64 - let detail: String - let lastSeen: Date + let texts: [String] - init(_ row: TableHealthFindingRow) { + init(_ row: TableHealthFindingRow, columns: [TableHealthColumn]) { id = row.id - status = row.status - checkTitle = row.checkTitle - size = row.sizeSortKey - detail = row.detail - lastSeen = row.lastSeenSortKey + texts = columns.map { $0.text(for: row) } } } @@ -167,7 +160,8 @@ final class TableHealthRowsTableCoordinator: NSObject, NSTableViewDataSource, NS defer { isSyncing = false } rows = owner.rows - let keys = owner.rows.map(RowKey.init) + let columns = columnKinds + let keys = owner.rows.map { RowKey($0, columns: columns) } if keys != rowKeys { rowKeys = keys tableView.reloadData() From 9bcc8bf509ef40750bc55fe92884987db2f87a0b Mon Sep 17 00:00:00 2001 From: Ngo Quoc Dat Date: Tue, 10 Nov 2026 19:06:57 +0700 Subject: [PATCH 13/13] refactor(table-health): go back to SwiftUI List and Table now that a clicked list takes focus app-wide --- .../TableHealth/TableHealthChecksList.swift | 100 +++-- .../TableHealthChecksOutline.swift | 364 ----------------- .../TableHealthFindingsTable.swift | 292 +++++++------- .../TableHealth/TableHealthRowsTable.swift | 378 ------------------ .../TableHealthFindingsTableTests.swift | 31 ++ .../TableHealth/TableHealthTablesTests.swift | 244 ----------- 6 files changed, 233 insertions(+), 1176 deletions(-) delete mode 100644 apple/TablePro/Views/TableHealth/TableHealthChecksOutline.swift delete mode 100644 apple/TablePro/Views/TableHealth/TableHealthRowsTable.swift create mode 100644 apple/TableProTests/Views/TableHealth/TableHealthFindingsTableTests.swift delete mode 100644 apple/TableProTests/Views/TableHealth/TableHealthTablesTests.swift diff --git a/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift b/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift index 359d76064c..96c441dd05 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthChecksList.swift @@ -7,79 +7,69 @@ struct TableHealthChecksList: View { @ObservedObject var viewModel: TableHealthViewModel var body: some View { - TableHealthChecksOutline( - sections: sections, - scope: $viewModel.scope, - menu: { scope in TableHealthRowMenu.checks(scope: scope, resolvedCount: viewModel.resolvedCount) }, - perform: { command in - if command == .clearResolved { - viewModel.clearResolved() + List(selection: scopeSelection) { + Label(String(localized: "All Findings"), systemImage: "tray.full") + .badge(viewModel.allFindingsCount) + .tag(TableHealthScope.all) + if let report = viewModel.report { + Section(String(localized: "Checks")) { + ForEach(report.checks) { result in + Label(result.check.title, systemImage: result.check.systemImage) + .badge(badge(for: result)) + .help(tooltip(for: result)) + .tag(TableHealthScope.check(result.kind)) + } } - }, - copyText: { viewModel.selectionTSV() } - ) - } - - private var sections: [TableHealthChecksSection] { - var sections = [ - TableHealthChecksSection(title: nil, rows: [ - TableHealthChecksRow( - scope: .all, - title: String(localized: "All Findings"), - systemImage: "tray.full", - badge: Self.count(viewModel.allFindingsCount), - toolTip: nil - ), - ]), - ] - if let report = viewModel.report { - sections.append(TableHealthChecksSection( - title: String(localized: "Checks"), - rows: report.checks.map { result in - TableHealthChecksRow( - scope: .check(result.kind), - title: result.check.title, - systemImage: result.check.systemImage, - badge: badge(for: result), - toolTip: tooltip(for: result) - ) - } - )) + } + Section(String(localized: "History")) { + Label(String(localized: "Resolved"), systemImage: "clock.arrow.circlepath") + .badge(viewModel.resolvedCount) + .tag(TableHealthScope.resolved) + } + } + .listStyle(.sidebar) + .accessibilityLabel(String(localized: "Checks")) + .contextMenu(forSelectionType: TableHealthScope.self) { scopes in + if scopes.contains(.resolved), viewModel.resolvedCount > 0 { + Button(String(localized: "Clear Resolved")) { viewModel.clearResolved() } + } + } + // Without it Edit > Copy falls through to the window, which copies the sidebar's table names. + .onCopyCommand { + TableHealthCopy.providers(viewModel.selectionTSV()) } - sections.append(TableHealthChecksSection(title: String(localized: "History"), rows: [ - TableHealthChecksRow( - scope: .resolved, - title: String(localized: "Resolved"), - systemImage: "clock.arrow.circlepath", - badge: Self.count(viewModel.resolvedCount), - toolTip: nil - ), - ])) - return sections } - private static func count(_ value: Int) -> String? { - value == 0 ? nil : value.formatted() + // A List cannot deselect into nothing here: some scope is always shown. + private var scopeSelection: Binding { + Binding( + get: { viewModel.scope }, + set: { scope in + guard let scope else { return } + viewModel.scope = scope + } + ) } - private func badge(for result: TableHealthCheckResult) -> String? { + private func badge(for result: TableHealthCheckResult) -> Text? { switch result.outcome { case .completed: - return Self.count(viewModel.badgeCount(for: result.kind)) + let findings = viewModel.badgeCount(for: result.kind) + return findings == 0 ? nil : Text(verbatim: findings.formatted()) case .skipped: - return String(localized: "Skipped") + return Text("Skipped") case .failed: - return String(localized: "Failed") + return Text("Failed") } } - private func tooltip(for result: TableHealthCheckResult) -> String? { + private func tooltip(for result: TableHealthCheckResult) -> String { let text: String switch result.outcome { case .completed: text = result.check.summary case .skipped(let reason): text = reason case .failed(let message): text = message } - return text.isEmpty || text.count > Self.tooltipLimit ? nil : text + return text.count <= Self.tooltipLimit ? text : "" } } diff --git a/apple/TablePro/Views/TableHealth/TableHealthChecksOutline.swift b/apple/TablePro/Views/TableHealth/TableHealthChecksOutline.swift deleted file mode 100644 index 2ba983d359..0000000000 --- a/apple/TablePro/Views/TableHealth/TableHealthChecksOutline.swift +++ /dev/null @@ -1,364 +0,0 @@ -import AppKit -import SwiftUI - -struct TableHealthChecksRow: Equatable { - let scope: TableHealthScope - let title: String - let systemImage: String - let badge: String? - let toolTip: String? -} - -struct TableHealthChecksSection: Equatable { - let title: String? - let rows: [TableHealthChecksRow] -} - -// An NSOutlineView source list, not a SwiftUI List: under the app's bare NSHostingController a click on a -// SwiftUI List row lands on its cell hosting view, which never makes the list first responder, so the -// selection stays grey and the arrow keys reach whatever held the keyboard before (FavoritesOutlineView). -struct TableHealthChecksOutline: NSViewRepresentable { - let sections: [TableHealthChecksSection] - @Binding var scope: TableHealthScope - let menu: (TableHealthScope) -> [SidebarMenuSection] - let perform: (TableHealthRowCommand) -> Void - let copyText: () -> String? - - func makeCoordinator() -> TableHealthChecksCoordinator { - TableHealthChecksCoordinator(owner: self) - } - - func makeNSView(context: Context) -> NSScrollView { - let outlineView = TableHealthChecksNSOutlineView() - outlineView.style = .sourceList - outlineView.rowSizeStyle = .default - outlineView.headerView = nil - outlineView.allowsMultipleSelection = false - // Some scope is always shown, so the list never deselects into nothing. - outlineView.allowsEmptySelection = false - outlineView.floatsGroupRows = false - outlineView.autosaveExpandedItems = false - outlineView.setAccessibilityLabel(String(localized: "Checks")) - - let column = NSTableColumn(identifier: NSUserInterfaceItemIdentifier("TableHealthChecksColumn")) - column.resizingMask = .autoresizingMask - outlineView.addTableColumn(column) - outlineView.outlineTableColumn = column - - outlineView.dataSource = context.coordinator - outlineView.delegate = context.coordinator - outlineView.copySource = context.coordinator - let menu = NSMenu() - menu.delegate = context.coordinator - outlineView.menu = menu - - let scrollView = NSScrollView() - scrollView.documentView = outlineView - scrollView.hasVerticalScroller = true - scrollView.hasHorizontalScroller = false - scrollView.autohidesScrollers = true - scrollView.drawsBackground = false - context.coordinator.attach(outlineView) - return scrollView - } - - func updateNSView(_ scrollView: NSScrollView, context: Context) { - context.coordinator.update(owner: self) - } -} - -final class TableHealthChecksNSOutlineView: NSOutlineView { - weak var copySource: (any TableHealthCopySource)? - - // Without it Edit > Copy falls through to the window, which copies the sidebar's table names. - @objc func copy(_ sender: Any?) { - guard let text = copySource?.copyText() else { return } - ClipboardService.shared.writeText(text) - } - - override func validateUserInterfaceItem(_ item: any NSValidatedUserInterfaceItem) -> Bool { - guard item.action == #selector(copy(_:)) else { return super.validateUserInterfaceItem(item) } - return copySource?.copyText() != nil - } -} - -final class TableHealthChecksNode: NSObject { - let id: String - var title: String - var row: TableHealthChecksRow? - var children: [TableHealthChecksNode] = [] - - init(id: String, title: String, row: TableHealthChecksRow?) { - self.id = id - self.title = title - self.row = row - } - - var isGroup: Bool { row == nil } -} - -@MainActor -final class TableHealthChecksCoordinator: NSObject, NSOutlineViewDataSource, NSOutlineViewDelegate, NSMenuDelegate, - TableHealthCopySource { - private static let rowCellIdentifier = NSUserInterfaceItemIdentifier("TableHealthCheckCell") - private static let headerCellIdentifier = NSUserInterfaceItemIdentifier("TableHealthChecksHeaderCell") - - private(set) var owner: TableHealthChecksOutline - private weak var outlineView: NSOutlineView? - // Kept by id and updated in place, so a refresh that changes only a badge keeps the rows and the selection. - private var nodes: [String: TableHealthChecksNode] = [:] - private var roots: [TableHealthChecksNode] = [] - private var structure: [String] = [] - private var isSyncing = false - - init(owner: TableHealthChecksOutline) { - self.owner = owner - super.init() - } - - static func nodeId(for scope: TableHealthScope) -> String { - switch scope { - case .all: return "all" - case .check(let kind): return "check." + kind.rawValue - case .resolved: return "resolved" - } - } - - func attach(_ outlineView: NSOutlineView) { - self.outlineView = outlineView - update(owner: owner) - } - - func update(owner: TableHealthChecksOutline) { - self.owner = owner - guard let outlineView else { return } - isSyncing = true - defer { isSyncing = false } - - roots = buildRoots(owner.sections) - let shape = roots.flatMap { [$0.id] + $0.children.map { " " + $0.id } } - if shape != structure { - structure = shape - outlineView.reloadData() - for node in roots where node.isGroup { - outlineView.expandItem(node) - } - } else { - refreshVisibleRows(in: outlineView) - } - - if let node = nodes[Self.nodeId(for: owner.scope)] { - let row = outlineView.row(forItem: node) - if row >= 0, outlineView.selectedRow != row { - outlineView.selectRowIndexes(IndexSet(integer: row), byExtendingSelection: false) - } - } - } - - private func buildRoots(_ sections: [TableHealthChecksSection]) -> [TableHealthChecksNode] { - var built: [TableHealthChecksNode] = [] - for section in sections { - let rows = section.rows.map { rowNode($0) } - guard let title = section.title else { - built += rows - continue - } - let group = node(id: "group." + title, title: title, row: nil) - group.children = rows - built.append(group) - } - return built - } - - private func rowNode(_ row: TableHealthChecksRow) -> TableHealthChecksNode { - node(id: Self.nodeId(for: row.scope), title: row.title, row: row) - } - - private func node(id: String, title: String, row: TableHealthChecksRow?) -> TableHealthChecksNode { - if let existing = nodes[id] { - existing.title = title - existing.row = row - return existing - } - let created = TableHealthChecksNode(id: id, title: title, row: row) - nodes[id] = created - return created - } - - private func refreshVisibleRows(in outlineView: NSOutlineView) { - for index in 0.. String? { - owner.copyText() - } - - func menuNeedsUpdate(_ menu: NSMenu) { - let clicked = outlineView.flatMap { $0.clickedRow >= 0 ? $0.item(atRow: $0.clickedRow) as? TableHealthChecksNode : nil } - let sections = clicked?.row.map { owner.menu($0.scope) } ?? [] - SidebarMenuBuilder.fill(menu, with: sections, target: self, action: #selector(performMenuCommand(_:))) - } - - @objc func performMenuCommand(_ sender: NSMenuItem) { - guard let box = sender.representedObject as? SidebarMenuCommandBox else { return } - owner.perform(box.command) - } - - // MARK: - Data source and delegate - - func outlineView(_ outlineView: NSOutlineView, numberOfChildrenOfItem item: Any?) -> Int { - (item as? TableHealthChecksNode)?.children.count ?? roots.count - } - - func outlineView(_ outlineView: NSOutlineView, child index: Int, ofItem item: Any?) -> Any { - ((item as? TableHealthChecksNode)?.children ?? roots)[index] - } - - func outlineView(_ outlineView: NSOutlineView, isItemExpandable item: Any) -> Bool { - (item as? TableHealthChecksNode)?.isGroup ?? false - } - - func outlineView(_ outlineView: NSOutlineView, isGroupItem item: Any) -> Bool { - (item as? TableHealthChecksNode)?.isGroup ?? false - } - - func outlineView(_ outlineView: NSOutlineView, shouldSelectItem item: Any) -> Bool { - (item as? TableHealthChecksNode)?.isGroup == false - } - - func outlineView(_ outlineView: NSOutlineView, shouldShowOutlineCellForItem item: Any) -> Bool { - false - } - - func outlineView(_ outlineView: NSOutlineView, shouldCollapseItem item: Any) -> Bool { - false - } - - func outlineView(_ outlineView: NSOutlineView, typeSelectStringFor tableColumn: NSTableColumn?, item: Any) -> String? { - (item as? TableHealthChecksNode)?.row?.title - } - - func outlineView(_ outlineView: NSOutlineView, viewFor tableColumn: NSTableColumn?, item: Any) -> NSView? { - guard let node = item as? TableHealthChecksNode else { return nil } - guard let row = node.row else { - let cell = outlineView.makeView(withIdentifier: Self.headerCellIdentifier, owner: self) - as? TableHealthChecksHeaderCellView ?? TableHealthChecksHeaderCellView(identifier: Self.headerCellIdentifier) - cell.textField?.stringValue = node.title - return cell - } - let cell = outlineView.makeView(withIdentifier: Self.rowCellIdentifier, owner: self) - as? TableHealthCheckCellView ?? TableHealthCheckCellView(identifier: Self.rowCellIdentifier) - cell.configure(row) - return cell - } - - func outlineViewSelectionDidChange(_ notification: Notification) { - guard !isSyncing, let outlineView, - let scope = (outlineView.item(atRow: outlineView.selectedRow) as? TableHealthChecksNode)?.row?.scope, - scope != owner.scope - else { return } - owner.scope = scope - } -} - -// AppKit sets the title's font and the symbol's size from the row size and turns both white on the -// emphasized selection; the symbol slot and the badge follow the title's size. -final class TableHealthCheckCellView: NSTableCellView { - private let badge = NSTextField(labelWithString: "") - private var symbolWidth: NSLayoutConstraint? - - init(identifier: NSUserInterfaceItemIdentifier) { - super.init(frame: .zero) - self.identifier = identifier - let image = NSImageView() - let title = NSTextField(labelWithString: "") - title.lineBreakMode = .byTruncatingTail - title.maximumNumberOfLines = 1 - title.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) - badge.textColor = .secondaryLabelColor - badge.alignment = .right - badge.setContentHuggingPriority(.required, for: .horizontal) - badge.setContentCompressionResistancePriority(.required, for: .horizontal) - for view in [image, title, badge] { - view.translatesAutoresizingMaskIntoConstraints = false - addSubview(view) - } - imageView = image - textField = title - // One slot width for every symbol, so the titles line up. - let symbolWidth = image.widthAnchor.constraint(equalToConstant: Self.slotWidth(forTitleSize: NSFont.systemFontSize)) - self.symbolWidth = symbolWidth - NSLayoutConstraint.activate([ - image.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 2), - image.centerYAnchor.constraint(equalTo: centerYAnchor), - symbolWidth, - title.leadingAnchor.constraint(equalTo: image.trailingAnchor, constant: 6), - title.centerYAnchor.constraint(equalTo: centerYAnchor), - badge.leadingAnchor.constraint(greaterThanOrEqualTo: title.trailingAnchor, constant: 6), - badge.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -6), - badge.firstBaselineAnchor.constraint(equalTo: title.firstBaselineAnchor), - ]) - } - - @available(*, unavailable) - required init?(coder: NSCoder) { - fatalError("TableHealthCheckCellView does not support NSCoder init") - } - - private static func slotWidth(forTitleSize titleSize: CGFloat) -> CGFloat { - (titleSize * 1.75).rounded() - } - - func configure(_ row: TableHealthChecksRow) { - imageView?.image = NSImage(systemSymbolName: row.systemImage, accessibilityDescription: nil) - textField?.stringValue = row.title - badge.stringValue = row.badge ?? "" - badge.isHidden = row.badge == nil - toolTip = row.toolTip - } - - override func layout() { - if let size = textField?.font?.pointSize { - let width = Self.slotWidth(forTitleSize: size) - if let symbolWidth, symbolWidth.constant != width { - symbolWidth.constant = width - } - let font = NSFont.monospacedDigitSystemFont(ofSize: max(size - 2, NSFont.smallSystemFontSize - 1), weight: .regular) - if badge.font != font { - badge.font = font - } - } - super.layout() - } -} - -final class TableHealthChecksHeaderCellView: NSTableCellView { - init(identifier: NSUserInterfaceItemIdentifier) { - super.init(frame: .zero) - self.identifier = identifier - let title = NSTextField(labelWithString: "") - title.textColor = .secondaryLabelColor - title.lineBreakMode = .byTruncatingTail - title.translatesAutoresizingMaskIntoConstraints = false - addSubview(title) - textField = title - NSLayoutConstraint.activate([ - title.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 2), - title.trailingAnchor.constraint(lessThanOrEqualTo: trailingAnchor, constant: -2), - title.centerYAnchor.constraint(equalTo: centerYAnchor), - ]) - } - - @available(*, unavailable) - required init?(coder: NSCoder) { - fatalError("TableHealthChecksHeaderCellView does not support NSCoder init") - } -} diff --git a/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift b/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift index fabe591430..568beade74 100644 --- a/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift +++ b/apple/TablePro/Views/TableHealth/TableHealthFindingsTable.swift @@ -1,169 +1,191 @@ import AppKit import SwiftUI +// One fixed column set: an `if` among table columns is macOS 14.4, so the Check column stays in every scope. struct TableHealthFindingsTable: View { - static let columns: [TableHealthTableColumn] = [ - TableHealthTableColumn(.status, min: 40, ideal: 52, max: 60), - TableHealthTableColumn(.check, min: 80, ideal: 150), - TableHealthTableColumn(.table, min: 80, ideal: 160), - TableHealthTableColumn(.object, min: 60, ideal: 140), - TableHealthTableColumn(.size, min: 50, ideal: 72, max: 110), - TableHealthTableColumn(.detail, min: 120, ideal: 280), - ] - @ObservedObject var viewModel: TableHealthViewModel let databaseType: DatabaseType let actions: TableHealthActions var body: some View { - TableHealthRowsTable( - rows: viewModel.rows, - columns: Self.columns, - selection: $viewModel.selection, - sortOrder: $viewModel.sortOrder, - accessibilityLabel: String(localized: "Findings"), - menu: TableHealthRowMenu.findings, - perform: { command, rows in perform(command, on: rows) }, - primaryAction: { rows in - if let finding = TableHealthRowMenu.singleTableFinding(in: rows) { - actions.openTable(finding) - } - }, - copyText: { viewModel.selectionTSV() } - ) + Table(viewModel.rows, selection: $viewModel.selection, sortOrder: $viewModel.sortOrder) { + TableColumn(TableHealthColumn.status.title, value: \TableHealthFindingRow.statusRank) { row in + Text(verbatim: TableHealthColumn.status.text(for: row)) + } + .width(min: 40, ideal: 52, max: 60) + TableColumn(TableHealthColumn.check.title, value: \TableHealthFindingRow.checkOrder) { row in + Text(verbatim: row.checkTitle) + .lineLimit(1) + } + .width(min: 80, ideal: 150) + TableColumn( + TableHealthColumn.table.title, + value: \TableHealthFindingRow.tableSortKey, + comparator: String.StandardComparator.localizedStandard + ) { row in + Text(verbatim: row.tableSortKey) + .lineLimit(1) + .truncationMode(.middle) + } + .width(min: 80, ideal: 160) + TableColumn( + TableHealthColumn.object.title, + value: \TableHealthFindingRow.objectSortKey, + comparator: String.StandardComparator.localizedStandard + ) { row in + Text(verbatim: row.objectSortKey) + .lineLimit(1) + .truncationMode(.middle) + } + .width(min: 60, ideal: 140) + TableColumn(TableHealthColumn.size.title, value: \TableHealthFindingRow.sizeSortKey) { row in + Text(verbatim: TableHealthColumn.size.text(for: row)) + .monospacedDigit() + .frame(maxWidth: .infinity, alignment: .trailing) + } + .width(min: 50, ideal: 72, max: 110) + TableColumn( + TableHealthColumn.detail.title, + value: \TableHealthFindingRow.detail, + comparator: String.StandardComparator.localizedStandard + ) { row in + Text(verbatim: row.detail) + .lineLimit(1) + .help(row.detail) + } + .width(min: 120, ideal: 280) + } + .tableStyle(.inset) + .alternatingRowBackgroundsCompat() + .accessibilityLabel(String(localized: "Findings")) + .contextMenu(forSelectionType: TableHealthFindingID.self) { ids in + menu(for: viewModel.rows(for: ids)) + } primaryAction: { ids in + if let finding = Self.singleTableFinding(in: viewModel.rows(for: ids)) { + actions.openTable(finding) + } + } + .onCopyCommand { + TableHealthCopy.providers(viewModel.selectionTSV()) + } } - private func perform(_ command: TableHealthRowCommand, on rows: [TableHealthFindingRow]) { - let findings = rows.compactMap(\.finding) - switch command { - case .openTable: - if let finding = TableHealthRowMenu.singleTableFinding(in: rows) { - actions.openTable(finding) + // HIG, Context menus: unavailable items are left out, Copy aside. + @ViewBuilder + private func menu(for rows: [TableHealthFindingRow]) -> some View { + if !rows.isEmpty { + let findings = rows.compactMap(\.finding) + let fixCount = findings.filter(TableHealthViewModel.hasFix).count + if let finding = Self.singleTableFinding(in: rows) { + Button(String(localized: "Open Table")) { actions.openTable(finding) } + } + if fixCount > 0 { + Button(fixCount == 1 ? String(localized: "Open Fix in Editor") : String(localized: "Open Fixes in Editor")) { + if let text = viewModel.fixText(for: findings, databaseType: databaseType) { + actions.openInEditor(text) + } + } } - case .openFixes: - if let text = viewModel.fixText(for: findings, databaseType: databaseType) { - actions.openInEditor(text) + Divider() + Button(String(localized: "Copy")) { + if let text = viewModel.tsv(for: rows) { + ClipboardService.shared.writeText(text) + } } - case .copy: - if let text = viewModel.tsv(for: rows) { - ClipboardService.shared.writeText(text) + if fixCount > 0 { + Button(String(localized: "Copy Fix SQL")) { + if let text = viewModel.fixText(for: findings, databaseType: databaseType) { + ClipboardService.shared.writeText(text) + } + } } - case .copyFixSQL: - if let text = viewModel.fixText(for: findings, databaseType: databaseType) { - ClipboardService.shared.writeText(text) + Divider() + let ids = Set(rows.map(\.id)) + if rows.allSatisfy({ $0.status == .ignored }) { + Button(String(localized: "Stop Ignoring")) { viewModel.setIgnored(ids, ignored: false) } + } else { + Button(String(localized: "Ignore")) { viewModel.setIgnored(ids, ignored: true) } } - case .ignore: - viewModel.setIgnored(Set(rows.map(\.id)), ignored: true) - case .stopIgnoring: - viewModel.setIgnored(Set(rows.map(\.id)), ignored: false) - case .saveFixScript: - if let script = viewModel.fixScript(for: findings, databaseType: databaseType) { - actions.saveFixScript(script) + if fixCount > 0 { + Button(String(localized: "Save Fix Script…")) { + if let script = viewModel.fixScript(for: findings, databaseType: databaseType) { + actions.saveFixScript(script) + } + } } - case .clearResolved: - viewModel.clearResolved() } } + + // Open Table applies when the rows name one table, however many findings it has. + static func singleTableFinding(in rows: [TableHealthFindingRow]) -> TableHealthFinding? { + let findings = rows.compactMap(\.finding).filter { $0.table != nil } + guard let first = findings.first, + findings.allSatisfy({ $0.schema == first.schema && $0.table == first.table }) + else { return nil } + return first + } } struct TableHealthResolvedTable: View { - static let columns: [TableHealthTableColumn] = [ - TableHealthTableColumn(.check, min: 80, ideal: 150), - TableHealthTableColumn(.table, min: 80, ideal: 180), - TableHealthTableColumn(.object, min: 60, ideal: 160), - TableHealthTableColumn(.lastSeen, min: 100, ideal: 160), - ] - @ObservedObject var viewModel: TableHealthViewModel var body: some View { - TableHealthRowsTable( - rows: viewModel.rows, - columns: Self.columns, - selection: $viewModel.selection, - sortOrder: $viewModel.resolvedSortOrder, - accessibilityLabel: String(localized: "Resolved Findings"), - menu: TableHealthRowMenu.resolved, - perform: { command, rows in - switch command { - case .copy: + Table(viewModel.rows, selection: $viewModel.selection, sortOrder: $viewModel.resolvedSortOrder) { + TableColumn(TableHealthColumn.check.title, value: \TableHealthFindingRow.checkOrder) { row in + Text(verbatim: row.checkTitle) + .lineLimit(1) + } + .width(min: 80, ideal: 150) + TableColumn( + TableHealthColumn.table.title, + value: \TableHealthFindingRow.tableSortKey, + comparator: String.StandardComparator.localizedStandard + ) { row in + Text(verbatim: row.tableSortKey) + .lineLimit(1) + .truncationMode(.middle) + } + .width(min: 80, ideal: 180) + TableColumn( + TableHealthColumn.object.title, + value: \TableHealthFindingRow.objectSortKey, + comparator: String.StandardComparator.localizedStandard + ) { row in + Text(verbatim: row.objectSortKey) + .lineLimit(1) + .truncationMode(.middle) + } + .width(min: 60, ideal: 160) + TableColumn(TableHealthColumn.lastSeen.title, value: \TableHealthFindingRow.lastSeenSortKey) { row in + Text(verbatim: TableHealthColumn.lastSeen.text(for: row)) + .monospacedDigit() + } + .width(min: 100, ideal: 160) + } + .tableStyle(.inset) + .alternatingRowBackgroundsCompat() + .accessibilityLabel(String(localized: "Resolved Findings")) + .contextMenu(forSelectionType: TableHealthFindingID.self) { ids in + let rows = viewModel.rows(for: ids) + if !rows.isEmpty { + Button(String(localized: "Copy")) { if let text = viewModel.tsv(for: rows) { ClipboardService.shared.writeText(text) } - case .clearResolved: - viewModel.clearResolved() - case .openTable, .openFixes, .copyFixSQL, .ignore, .stopIgnoring, .saveFixScript: - break } - }, - copyText: { viewModel.selectionTSV() } - ) - } -} - -enum TableHealthRowCommand: Equatable, SidebarMenuShortcutProviding { - case openTable - case openFixes - case copy - case copyFixSQL - case ignore - case stopIgnoring - case saveFixScript - case clearResolved - - var shortcutAction: ShortcutAction? { nil } -} - -// HIG, Context menus: unavailable items are left out, Copy aside. -enum TableHealthRowMenu { - static func findings(_ rows: [TableHealthFindingRow]) -> [SidebarMenuSection] { - guard !rows.isEmpty else { return [] } - let fixCount = rows.compactMap(\.finding).filter(TableHealthViewModel.hasFix).count - - var open: [SidebarMenuItem] = [] - if singleTableFinding(in: rows) != nil { - open.append(.command(String(localized: "Open Table"), .openTable)) - } - if fixCount > 0 { - let title = fixCount == 1 ? String(localized: "Open Fix in Editor") : String(localized: "Open Fixes in Editor") - open.append(.command(title, .openFixes)) - } - - var copy: [SidebarMenuItem] = [.command(String(localized: "Copy"), .copy)] - if fixCount > 0 { - copy.append(.command(String(localized: "Copy Fix SQL"), .copyFixSQL)) - } - - var manage: [SidebarMenuItem] = [] - if rows.allSatisfy({ $0.status == .ignored }) { - manage.append(.command(String(localized: "Stop Ignoring"), .stopIgnoring)) - } else { - manage.append(.command(String(localized: "Ignore"), .ignore)) + Divider() + } + Button(String(localized: "Clear Resolved")) { viewModel.clearResolved() } } - if fixCount > 0 { - manage.append(.command(String(localized: "Save Fix Script…"), .saveFixScript)) + .onCopyCommand { + TableHealthCopy.providers(viewModel.selectionTSV()) } - return [SidebarMenuSection(open), SidebarMenuSection(copy), SidebarMenuSection(manage)] - } - - static func resolved(_ rows: [TableHealthFindingRow]) -> [SidebarMenuSection] { - let copy: [SidebarMenuItem] = rows.isEmpty - ? [] - : [.command(String(localized: "Copy"), .copy)] - return [SidebarMenuSection(copy), SidebarMenuSection([.command(String(localized: "Clear Resolved"), .clearResolved)])] - } - - static func checks(scope: TableHealthScope?, resolvedCount: Int) -> [SidebarMenuSection] { - guard scope == .resolved, resolvedCount > 0 else { return [] } - return [SidebarMenuSection([.command(String(localized: "Clear Resolved"), .clearResolved)])] } +} - // Open Table applies when the rows name one table, however many findings it has. - static func singleTableFinding(in rows: [TableHealthFindingRow]) -> TableHealthFinding? { - let findings = rows.compactMap(\.finding).filter { $0.table != nil } - guard let first = findings.first, - findings.allSatisfy({ $0.schema == first.schema && $0.table == first.table }) - else { return nil } - return first +enum TableHealthCopy { + static func providers(_ text: String?) -> [NSItemProvider] { + guard let text else { return [] } + return [NSItemProvider(object: text as NSString)] } } diff --git a/apple/TablePro/Views/TableHealth/TableHealthRowsTable.swift b/apple/TablePro/Views/TableHealth/TableHealthRowsTable.swift deleted file mode 100644 index 3f179fa246..0000000000 --- a/apple/TablePro/Views/TableHealth/TableHealthRowsTable.swift +++ /dev/null @@ -1,378 +0,0 @@ -import AppKit -import SwiftUI - -struct TableHealthTableColumn: Sendable { - let column: TableHealthColumn - let minWidth: CGFloat - let idealWidth: CGFloat - let maxWidth: CGFloat? - - init(_ column: TableHealthColumn, min: CGFloat, ideal: CGFloat, max: CGFloat? = nil) { - self.column = column - minWidth = min - idealWidth = ideal - maxWidth = max - } -} - -// An NSTableView, not a SwiftUI Table: under the app's bare NSHostingController a click on a SwiftUI -// Table row lands on its cell hosting view, which handles it without making the table first responder, -// so the selection stays grey and the arrow keys reach whatever held the keyboard before. -struct TableHealthRowsTable: NSViewRepresentable { - let rows: [TableHealthFindingRow] - let columns: [TableHealthTableColumn] - @Binding var selection: Set - @Binding var sortOrder: [KeyPathComparator] - let accessibilityLabel: String - let menu: ([TableHealthFindingRow]) -> [SidebarMenuSection] - let perform: (TableHealthRowCommand, [TableHealthFindingRow]) -> Void - var primaryAction: (([TableHealthFindingRow]) -> Void)? - let copyText: () -> String? - - func makeCoordinator() -> TableHealthRowsTableCoordinator { - TableHealthRowsTableCoordinator(owner: self) - } - - func makeNSView(context: Context) -> NSScrollView { - let tableView = TableHealthNSTableView() - tableView.style = .inset - tableView.usesAlternatingRowBackgroundColors = true - tableView.allowsMultipleSelection = true - tableView.allowsEmptySelection = true - tableView.allowsColumnReordering = false - tableView.allowsColumnResizing = true - tableView.columnAutoresizingStyle = .uniformColumnAutoresizingStyle - tableView.setAccessibilityLabel(accessibilityLabel) - for spec in columns { - let column = NSTableColumn(identifier: spec.column.identifier) - column.title = spec.column.title - column.minWidth = spec.minWidth - column.width = spec.idealWidth - if let maxWidth = spec.maxWidth { - column.maxWidth = maxWidth - } - column.sortDescriptorPrototype = NSSortDescriptor(key: spec.column.identifier.rawValue, ascending: true) - tableView.addTableColumn(column) - } - - tableView.dataSource = context.coordinator - tableView.delegate = context.coordinator - tableView.target = context.coordinator - tableView.doubleAction = #selector(TableHealthRowsTableCoordinator.handleDoubleClick) - tableView.copySource = context.coordinator - tableView.primaryActionSource = context.coordinator - // The table owns the menu, so AppKit sets `clickedRow` and draws the clicked-row outline. - let menu = NSMenu() - menu.delegate = context.coordinator - tableView.menu = menu - - let scrollView = NSScrollView() - scrollView.documentView = tableView - scrollView.hasVerticalScroller = true - scrollView.hasHorizontalScroller = true - scrollView.autohidesScrollers = true - scrollView.drawsBackground = false - context.coordinator.attach(tableView) - return scrollView - } - - func updateNSView(_ scrollView: NSScrollView, context: Context) { - context.coordinator.update(owner: self) - } -} - -@MainActor -protocol TableHealthCopySource: AnyObject { - func copyText() -> String? -} - -@MainActor -protocol TableHealthPrimaryActionSource: AnyObject { - func performPrimaryAction() -> Bool -} - -// Return runs the primary action and Edit > Copy copies the selection; NSTableView routes neither on its own. -final class TableHealthNSTableView: NSTableView { - weak var copySource: (any TableHealthCopySource)? - weak var primaryActionSource: (any TableHealthPrimaryActionSource)? - - // Measured: NSTableView takes Return in keyDown and never sends insertNewline(_:). - override func keyDown(with event: NSEvent) { - let modifiers = event.modifierFlags.intersection(.deviceIndependentFlagsMask) - let key = KeyCode(rawValue: event.keyCode) - if key == .return || key == .enter, - modifiers.isDisjoint(with: [.command, .option, .control, .shift]), - primaryActionSource?.performPrimaryAction() == true { - return - } - super.keyDown(with: event) - } - - @objc func copy(_ sender: Any?) { - guard let text = copySource?.copyText() else { return } - ClipboardService.shared.writeText(text) - } - - override func validateUserInterfaceItem(_ item: any NSValidatedUserInterfaceItem) -> Bool { - guard item.action == #selector(copy(_:)) else { return super.validateUserInterfaceItem(item) } - return copySource?.copyText() != nil - } -} - -@MainActor -final class TableHealthRowsTableCoordinator: NSObject, NSTableViewDataSource, NSTableViewDelegate, NSMenuDelegate, - TableHealthCopySource, TableHealthPrimaryActionSource { - private static let cellIdentifier = NSUserInterfaceItemIdentifier("TableHealthTextCell") - - // What a reload has to redraw: each row's id and the text its columns show. Rows equal in all of it keep their - // views and the selection. - private struct RowKey: Equatable { - let id: TableHealthFindingID - let texts: [String] - - init(_ row: TableHealthFindingRow, columns: [TableHealthColumn]) { - id = row.id - texts = columns.map { $0.text(for: row) } - } - } - - private(set) var owner: TableHealthRowsTable - private weak var tableView: NSTableView? - private var rows: [TableHealthFindingRow] = [] - private var rowKeys: [RowKey] = [] - private var menuRows: [TableHealthFindingRow] = [] - private var isSyncing = false - - init(owner: TableHealthRowsTable) { - self.owner = owner - super.init() - } - - func attach(_ tableView: NSTableView) { - self.tableView = tableView - update(owner: owner) - } - - func update(owner: TableHealthRowsTable) { - self.owner = owner - guard let tableView else { return } - isSyncing = true - defer { isSyncing = false } - - rows = owner.rows - let columns = columnKinds - let keys = owner.rows.map { RowKey($0, columns: columns) } - if keys != rowKeys { - rowKeys = keys - tableView.reloadData() - } - - let descriptors = TableHealthTableSort.descriptors(for: owner.sortOrder, columns: columnKinds) - if !TableHealthTableSort.same(tableView.sortDescriptors, descriptors) { - tableView.sortDescriptors = descriptors - } - - let wanted = IndexSet(rows.indices.filter { owner.selection.contains(rows[$0].id) }) - if wanted != tableView.selectedRowIndexes { - tableView.selectRowIndexes(wanted, byExtendingSelection: false) - } - } - - private var columnKinds: [TableHealthColumn] { - owner.columns.map(\.column) - } - - private func rows(at indexes: IndexSet) -> [TableHealthFindingRow] { - indexes.compactMap { rows.indices.contains($0) ? rows[$0] : nil } - } - - // A right-click on a selected row acts on the whole selection, on any other row on that row alone. - private func clickedRows() -> [TableHealthFindingRow] { - guard let tableView, rows.indices.contains(tableView.clickedRow) else { return [] } - if tableView.selectedRowIndexes.contains(tableView.clickedRow) { - return rows(at: tableView.selectedRowIndexes) - } - return [rows[tableView.clickedRow]] - } - - // MARK: - Actions - - @objc func handleDoubleClick() { - guard let tableView, tableView.clickedRow >= 0 else { return } - owner.primaryAction?(clickedRows()) - } - - func performPrimaryAction() -> Bool { - guard let primaryAction = owner.primaryAction, let tableView else { return false } - primaryAction(rows(at: tableView.selectedRowIndexes)) - return true - } - - func copyText() -> String? { - owner.copyText() - } - - func menuNeedsUpdate(_ menu: NSMenu) { - menuRows = clickedRows() - SidebarMenuBuilder.fill( - menu, - with: owner.menu(menuRows), - target: self, - action: #selector(performMenuCommand(_:)) - ) - } - - @objc func performMenuCommand(_ sender: NSMenuItem) { - guard let box = sender.representedObject as? SidebarMenuCommandBox else { return } - owner.perform(box.command, menuRows) - } - - // MARK: - Data source and delegate - - func numberOfRows(in tableView: NSTableView) -> Int { - rows.count - } - - func tableView(_ tableView: NSTableView, viewFor tableColumn: NSTableColumn?, row: Int) -> NSView? { - guard let tableColumn, rows.indices.contains(row), - let column = columnKinds.first(where: { $0.identifier == tableColumn.identifier }) - else { return nil } - let cell = tableView.makeView(withIdentifier: Self.cellIdentifier, owner: self) as? TableHealthTextCellView - ?? TableHealthTextCellView(identifier: Self.cellIdentifier) - cell.configure(column: column, row: rows[row]) - return cell - } - - func tableViewSelectionDidChange(_ notification: Notification) { - guard !isSyncing, let tableView else { return } - owner.selection = Set(rows(at: tableView.selectedRowIndexes).map(\.id)) - } - - func tableView(_ tableView: NSTableView, sortDescriptorsDidChange oldDescriptors: [NSSortDescriptor]) { - guard !isSyncing else { return } - owner.sortOrder = TableHealthTableSort.comparators(for: tableView.sortDescriptors, columns: columnKinds) - } -} - -final class TableHealthTextCellView: NSTableCellView { - init(identifier: NSUserInterfaceItemIdentifier) { - super.init(frame: .zero) - self.identifier = identifier - let field = NSTextField(labelWithString: "") - field.translatesAutoresizingMaskIntoConstraints = false - field.maximumNumberOfLines = 1 - field.setContentCompressionResistancePriority(.defaultLow, for: .horizontal) - addSubview(field) - textField = field - NSLayoutConstraint.activate([ - field.leadingAnchor.constraint(equalTo: leadingAnchor, constant: 2), - field.trailingAnchor.constraint(equalTo: trailingAnchor, constant: -2), - field.centerYAnchor.constraint(equalTo: centerYAnchor), - ]) - } - - @available(*, unavailable) - required init?(coder: NSCoder) { - fatalError("init(coder:) has not been implemented") - } - - func configure(column: TableHealthColumn, row: TableHealthFindingRow) { - guard let field = textField else { return } - let text = column.text(for: row) - field.stringValue = text - field.lineBreakMode = column.lineBreakMode - field.alignment = column.alignment - field.font = column.usesMonospacedDigits - ? .monospacedDigitSystemFont(ofSize: NSFont.systemFontSize, weight: .regular) - : .systemFont(ofSize: NSFont.systemFontSize) - field.toolTip = column == .detail && !text.isEmpty ? text : nil - } -} - -extension TableHealthColumn { - var identifier: NSUserInterfaceItemIdentifier { - switch self { - case .status: return NSUserInterfaceItemIdentifier("status") - case .check: return NSUserInterfaceItemIdentifier("check") - case .table: return NSUserInterfaceItemIdentifier("table") - case .object: return NSUserInterfaceItemIdentifier("object") - case .size: return NSUserInterfaceItemIdentifier("size") - case .detail: return NSUserInterfaceItemIdentifier("detail") - case .lastSeen: return NSUserInterfaceItemIdentifier("lastSeen") - } - } - - var lineBreakMode: NSLineBreakMode { - switch self { - case .table, .object: return .byTruncatingMiddle - case .status, .check, .size, .detail, .lastSeen: return .byTruncatingTail - } - } - - var alignment: NSTextAlignment { - self == .size ? .right : .natural - } - - var usesMonospacedDigits: Bool { - self == .size || self == .lastSeen - } -} - -// The header's sort descriptors and the view model's comparators, each derived from the other. -enum TableHealthTableSort { - static func comparator(for column: TableHealthColumn, order: SortOrder) -> KeyPathComparator { - switch column { - case .status: - return KeyPathComparator(\TableHealthFindingRow.statusRank, order: order) - case .check: - return KeyPathComparator(\TableHealthFindingRow.checkOrder, order: order) - case .table: - return KeyPathComparator( - \TableHealthFindingRow.tableSortKey, - comparator: String.StandardComparator.localizedStandard, - order: order - ) - case .object: - return KeyPathComparator( - \TableHealthFindingRow.objectSortKey, - comparator: String.StandardComparator.localizedStandard, - order: order - ) - case .size: - return KeyPathComparator(\TableHealthFindingRow.sizeSortKey, order: order) - case .detail: - return KeyPathComparator( - \TableHealthFindingRow.detail, - comparator: String.StandardComparator.localizedStandard, - order: order - ) - case .lastSeen: - return KeyPathComparator(\TableHealthFindingRow.lastSeenSortKey, order: order) - } - } - - static func comparators( - for descriptors: [NSSortDescriptor], - columns: [TableHealthColumn] - ) -> [KeyPathComparator] { - descriptors.compactMap { descriptor in - guard let column = columns.first(where: { $0.identifier.rawValue == descriptor.key }) else { return nil } - return comparator(for: column, order: descriptor.ascending ? .forward : .reverse) - } - } - - static func descriptors( - for comparators: [KeyPathComparator], - columns: [TableHealthColumn] - ) -> [NSSortDescriptor] { - comparators.compactMap { sort in - guard let column = columns.first(where: { comparator(for: $0, order: .forward).keyPath == sort.keyPath }) - else { return nil } - return NSSortDescriptor(key: column.identifier.rawValue, ascending: sort.order == .forward) - } - } - - static func same(_ lhs: [NSSortDescriptor], _ rhs: [NSSortDescriptor]) -> Bool { - lhs.map(\.key) == rhs.map(\.key) && lhs.map(\.ascending) == rhs.map(\.ascending) - } -} diff --git a/apple/TableProTests/Views/TableHealth/TableHealthFindingsTableTests.swift b/apple/TableProTests/Views/TableHealth/TableHealthFindingsTableTests.swift new file mode 100644 index 0000000000..c6cbd274eb --- /dev/null +++ b/apple/TableProTests/Views/TableHealth/TableHealthFindingsTableTests.swift @@ -0,0 +1,31 @@ +import Foundation +@testable import TablePro +import Testing + +@MainActor +struct TableHealthFindingsTableTests { + private static func row(_ table: String?, schema: String? = "public", kind: TableHealthCheckKind = .missingPrimaryKey) -> TableHealthFindingRow { + TableHealthFindingRow( + finding: TableHealthFixture.finding(kind, schema: schema, table: table), + checkTitle: kind.title, + checkOrder: 0, + status: .seen, + firstSeen: nil, + lastSeen: nil + ) + } + + @Test("Open Table applies to findings that all name one table") + func oneTableAcrossFindings() { + let rows = [Self.row("orders"), Self.row("orders", kind: .unusedIndex), Self.row(nil)] + + #expect(TableHealthFindingsTable.singleTableFinding(in: rows)?.table == "orders") + } + + @Test("Open Table does not apply across two tables, or one name in two schemas") + func twoTablesHaveNoSingleTable() { + #expect(TableHealthFindingsTable.singleTableFinding(in: [Self.row("orders"), Self.row("events")]) == nil) + #expect(TableHealthFindingsTable.singleTableFinding(in: [Self.row("orders"), Self.row("orders", schema: "audit")]) == nil) + #expect(TableHealthFindingsTable.singleTableFinding(in: [Self.row(nil)]) == nil) + } +} diff --git a/apple/TableProTests/Views/TableHealth/TableHealthTablesTests.swift b/apple/TableProTests/Views/TableHealth/TableHealthTablesTests.swift deleted file mode 100644 index ccda1a6e40..0000000000 --- a/apple/TableProTests/Views/TableHealth/TableHealthTablesTests.swift +++ /dev/null @@ -1,244 +0,0 @@ -import AppKit -import Foundation -import SwiftUI -@testable import TablePro -import Testing - -// The checks list and the findings table were SwiftUI, whose cell hosting view takes a row click without making -// the list first responder: the selection drew grey and the arrow keys went to the database sidebar. -@MainActor -struct TableHealthTablesTests { - // The test host never gives a window the keyboard, so the window says it has it. AppKit still draws no - // emphasis here, so the accent selection is measured in the running app; this pins the first responder - // AppKit derives it from. - private final class KeyWindow: NSWindow { - override var isKeyWindow: Bool { true } - } - - @MainActor - private final class State { - var selection: Set = [] - var sortOrder: [KeyPathComparator] = [KeyPathComparator(\TableHealthFindingRow.checkOrder)] - var scope: TableHealthScope = .all - var primaryActionRows: [[TableHealthFindingID]] = [] - } - - private struct Hosted { - let window: KeyWindow - let other: NSTextField - let root: NSView - - func first(_ type: Found.Type) -> Found? { - Self.descendants(of: root).lazy.compactMap { $0 as? Found }.first - } - - private static func descendants(of view: NSView) -> [NSView] { - [view] + view.subviews.flatMap(descendants(of:)) - } - - func close() { - window.orderOut(nil) - } - } - - private nonisolated static func row(_ table: String, kind: TableHealthCheckKind = .missingPrimaryKey) -> TableHealthFindingRow { - TableHealthFindingRow( - finding: TableHealthFixture.finding(kind, table: table), - checkTitle: kind.title, - checkOrder: 0, - status: .seen, - firstSeen: nil, - lastSeen: nil - ) - } - - private nonisolated static let rows = ["orders", "events", "audit_log"].map { row($0) } - - // The keyboard starts in another view, the way the database sidebar holds it when the tab opens. - private func host(_ content: some View) -> Hosted { - let frame = NSRect(x: 0, y: 0, width: 700, height: 420) - let window = KeyWindow(contentRect: frame, styleMask: [.titled], backing: .buffered, defer: false) - window.isReleasedWhenClosed = false - let container = NSView(frame: frame) - let other = NSTextField(frame: NSRect(x: 0, y: 390, width: 200, height: 22)) - let hosting = NSHostingView(rootView: content) - hosting.frame = NSRect(x: 0, y: 0, width: 700, height: 380) - container.addSubview(hosting) - container.addSubview(other) - window.contentView = container - hosting.layoutSubtreeIfNeeded() - window.makeFirstResponder(other) - return Hosted(window: window, other: other, root: hosting) - } - - // A click goes to the view under the pointer, which is what a SwiftUI cell took for itself. NSTableView - // tracks the press until the mouse-up, so the mouse-up waits in the queue first. - private func click(row: Int, in tableView: NSTableView, window: NSWindow) throws { - tableView.layoutSubtreeIfNeeded() - let rect = tableView.rect(ofRow: row) - let location = tableView.convert(NSPoint(x: rect.minX + 40, y: rect.midY), to: nil) - func event(_ type: NSEvent.EventType) throws -> NSEvent { - try #require(NSEvent.mouseEvent( - with: type, - location: location, - modifierFlags: [], - timestamp: ProcessInfo.processInfo.systemUptime, - windowNumber: window.windowNumber, - context: nil, - eventNumber: 0, - clickCount: 1, - pressure: type == .leftMouseDown ? 1 : 0 - )) - } - let up = try event(.leftMouseUp) - let down = try event(.leftMouseDown) - let target = try #require(window.contentView?.hitTest(location)) - NSApp.postEvent(up, atStart: true) - target.mouseDown(with: down) - _ = NSApp.nextEvent(matching: .leftMouseUp, until: .distantPast, inMode: .default, dequeue: true) - } - - private func press(_ key: KeyCode, characters: String, in view: NSView, window: NSWindow) throws { - let modifiers: NSEvent.ModifierFlags = key.isArrowKey ? [.numericPad, .function] : [] - let event = try #require(NSEvent.keyEvent( - with: .keyDown, - location: .zero, - modifierFlags: modifiers, - timestamp: ProcessInfo.processInfo.systemUptime, - windowNumber: window.windowNumber, - context: nil, - characters: characters, - charactersIgnoringModifiers: characters, - isARepeat: false, - keyCode: key.rawValue - )) - view.keyDown(with: event) - } - - private func pressDownArrow(in view: NSView, window: NSWindow) throws { - try press(.downArrow, characters: String(Character(UnicodeScalar(NSDownArrowFunctionKey)!)), in: view, window: window) - } - - private func findingsTable(_ state: State) -> TableHealthRowsTable { - TableHealthRowsTable( - rows: Self.rows, - columns: TableHealthFindingsTable.columns, - selection: Binding(get: { state.selection }, set: { state.selection = $0 }), - sortOrder: Binding(get: { state.sortOrder }, set: { state.sortOrder = $0 }), - accessibilityLabel: "Findings", - menu: TableHealthRowMenu.findings, - perform: { _, _ in }, - primaryAction: { rows in state.primaryActionRows.append(rows.map(\.id)) }, - copyText: { nil } - ) - } - - @Test("A click on a finding takes the keyboard, the arrow keys move it, and Return opens it") - func clickFocusesTheFindingsTable() throws { - let state = State() - let hosted = host(findingsTable(state)) - defer { hosted.close() } - let tableView = try #require(hosted.first(TableHealthNSTableView.self)) - #expect(hosted.window.firstResponder !== tableView) - - try click(row: 1, in: tableView, window: hosted.window) - - #expect(hosted.window.firstResponder === tableView) - #expect(state.selection == [Self.rows[1].id]) - - try pressDownArrow(in: tableView, window: hosted.window) - #expect(state.selection == [Self.rows[2].id]) - - try press(.return, characters: "\r", in: tableView, window: hosted.window) - #expect(state.primaryActionRows == [[Self.rows[2].id]]) - } - - @Test("A click on a check takes the keyboard, and the arrow keys skip the section headers") - func clickFocusesTheChecksList() throws { - let state = State() - let sections = [ - TableHealthChecksSection(title: nil, rows: [ - TableHealthChecksRow(scope: .all, title: "All Findings", systemImage: "tray.full", badge: "3", toolTip: nil), - ]), - TableHealthChecksSection(title: "Checks", rows: [ - TableHealthChecksRow( - scope: .check(.missingPrimaryKey), title: "Keys", systemImage: "key", badge: "3", toolTip: nil - ), - TableHealthChecksRow( - scope: .check(.unusedIndex), title: "Unused", systemImage: "list.bullet.indent", badge: nil, toolTip: nil - ), - ]), - TableHealthChecksSection(title: "History", rows: [ - TableHealthChecksRow( - scope: .resolved, title: "Resolved", systemImage: "clock.arrow.circlepath", badge: nil, toolTip: nil - ), - ]), - ] - let hosted = host(TableHealthChecksOutline( - sections: sections, - scope: Binding(get: { state.scope }, set: { state.scope = $0 }), - menu: { _ in [] }, - perform: { _ in }, - copyText: { nil } - )) - defer { hosted.close() } - let outlineView = try #require(hosted.first(TableHealthChecksNSOutlineView.self)) - #expect(outlineView.selectedRow == 0) - - // Rows: All Findings, the Checks header, Keys, Unused, the History header, Resolved. - try click(row: 3, in: outlineView, window: hosted.window) - - #expect(hosted.window.firstResponder === outlineView) - #expect(state.scope == .check(.unusedIndex)) - - try pressDownArrow(in: outlineView, window: hosted.window) - #expect(state.scope == .resolved) - } - - @Test("A header click becomes the view model's comparators, and its sort shows on the header") - func sortDescriptorsMapToComparators() { - let columns = TableHealthFindingsTable.columns.map(\.column) - let comparators = TableHealthTableSort.comparators( - for: [NSSortDescriptor(key: "table", ascending: false), NSSortDescriptor(key: "check", ascending: true)], - columns: columns - ) - - let sorted = Self.rows.sorted(using: comparators).map { $0.tableSortKey } - #expect(sorted == ["public.orders", "public.events", "public.audit_log"]) - let descriptors = TableHealthTableSort.descriptors(for: comparators, columns: columns) - #expect(descriptors.map { $0.key } == ["table", "check"]) - #expect(descriptors.map { $0.ascending } == [false, true]) - } - - @Test("The Resolved table shows its newest-first sort on the Last Seen header") - func resolvedSortShowsOnItsHeader() { - let descriptors = TableHealthTableSort.descriptors( - for: [KeyPathComparator(\TableHealthFindingRow.lastSeenSortKey, order: .reverse)], - columns: TableHealthResolvedTable.columns.map(\.column) - ) - - #expect(descriptors.map { $0.key } == ["lastSeen"]) - #expect(descriptors.map { $0.ascending } == [false]) - } - - @Test("A finding's menu leaves out what does not apply") - func findingsMenuLeavesOutWhatDoesNotApply() { - let twoTables = TableHealthRowMenu.findings([Self.rows[0], Self.rows[1]]) - let titles = twoTables.map { section in - section.items.compactMap { item -> String? in - guard case .command(let entry) = item else { return nil } - return entry.title - } - } - - #expect(titles == [ - ["Open Fixes in Editor"], - ["Copy", "Copy Fix SQL"], - ["Ignore", "Save Fix Script…"], - ]) - #expect(TableHealthRowMenu.findings([]).isEmpty) - #expect(TableHealthRowMenu.checks(scope: .resolved, resolvedCount: 0).isEmpty) - #expect(TableHealthRowMenu.checks(scope: .resolved, resolvedCount: 2).count == 1) - #expect(TableHealthRowMenu.resolved([]).nonEmptySections().count == 1) - } -}