|
| 1 | +// |
| 2 | +// CompareMetadataService+SourceDefinitions.swift |
| 3 | +// TablePro |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import os |
| 8 | +import TableProPluginKit |
| 9 | +import TableProSQLGrammar |
| 10 | + |
| 11 | +internal struct RoutineSourceRead: Sendable { |
| 12 | + internal let name: String |
| 13 | + internal let kind: CompareObjectKind |
| 14 | + internal let schema: String? |
| 15 | + internal let signature: String? |
| 16 | + internal let source: String |
| 17 | + internal let failure: String? |
| 18 | + |
| 19 | + internal init( |
| 20 | + name: String, |
| 21 | + kind: CompareObjectKind, |
| 22 | + schema: String?, |
| 23 | + signature: String?, |
| 24 | + source: String, |
| 25 | + failure: String? = nil |
| 26 | + ) { |
| 27 | + self.name = name |
| 28 | + self.kind = kind |
| 29 | + self.schema = schema |
| 30 | + self.signature = signature |
| 31 | + self.source = source |
| 32 | + self.failure = failure |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +internal extension CompareMetadataService { |
| 37 | + nonisolated private static let definitionLogger = Logger( |
| 38 | + subsystem: "com.TablePro", category: "CompareMetadataService" |
| 39 | + ) |
| 40 | + |
| 41 | + nonisolated static func readViewDefinitions( |
| 42 | + _ views: [PluginTableInfo], |
| 43 | + schema: String?, |
| 44 | + using plugin: any PluginDatabaseDriver |
| 45 | + ) async throws -> [RoutineSourceRead] { |
| 46 | + var reads: [RoutineSourceRead] = [] |
| 47 | + for view in views { |
| 48 | + try Task.checkCancellation() |
| 49 | + let viewSchema = view.schema ?? schema |
| 50 | + reads.append(try await definitionRead( |
| 51 | + name: view.name, |
| 52 | + kind: CompareTableKindClassifier.kind(of: view), |
| 53 | + schema: viewSchema, |
| 54 | + signature: nil |
| 55 | + ) { |
| 56 | + try await plugin.fetchViewDefinition(view: view.name, schema: viewSchema) |
| 57 | + }) |
| 58 | + } |
| 59 | + return reads |
| 60 | + } |
| 61 | + |
| 62 | + nonisolated static func readRoutineDefinitions( |
| 63 | + schema: String?, |
| 64 | + endpointName: String, |
| 65 | + using plugin: any PluginDatabaseDriver |
| 66 | + ) async throws -> [RoutineSourceRead] { |
| 67 | + let routines: [PluginRoutineInfo] |
| 68 | + do { |
| 69 | + routines = try await plugin.fetchRoutines(schema: schema) |
| 70 | + } catch { |
| 71 | + throw listingFailure(error, message: String( |
| 72 | + format: String(localized: "The procedures and functions in %1$@ could not be listed: %2$@"), |
| 73 | + endpointName, error.localizedDescription |
| 74 | + )) |
| 75 | + } |
| 76 | + var reads: [RoutineSourceRead] = [] |
| 77 | + for routine in routines { |
| 78 | + try Task.checkCancellation() |
| 79 | + reads.append(try await definitionRead( |
| 80 | + name: routine.name, |
| 81 | + kind: routine.kind == .procedure ? .procedure : .function, |
| 82 | + schema: routine.schema ?? schema, |
| 83 | + signature: routine.argumentSignature |
| 84 | + ) { |
| 85 | + try await plugin.fetchRoutineDDL(routine) |
| 86 | + }) |
| 87 | + } |
| 88 | + return reads |
| 89 | + } |
| 90 | + |
| 91 | + nonisolated static func readTriggerDefinitions( |
| 92 | + tables: [String], |
| 93 | + schema: String?, |
| 94 | + endpointName: String, |
| 95 | + using plugin: any PluginDatabaseDriver |
| 96 | + ) async throws -> [RoutineSourceRead] { |
| 97 | + let listed = try await listTriggers(tables: tables, schema: schema, endpointName: endpointName, using: plugin) |
| 98 | + var reads: [RoutineSourceRead] = [] |
| 99 | + for (trigger, owningTable) in listed { |
| 100 | + try Task.checkCancellation() |
| 101 | + reads.append(try await definitionRead( |
| 102 | + name: trigger.name, |
| 103 | + kind: .trigger, |
| 104 | + schema: trigger.schema ?? schema, |
| 105 | + signature: trigger.table ?? owningTable |
| 106 | + ) { |
| 107 | + if let definition = trigger.definition, StatementBlank.hasContent(definition) { |
| 108 | + return definition |
| 109 | + } |
| 110 | + return try await plugin.fetchTriggerDDL(trigger) |
| 111 | + }) |
| 112 | + } |
| 113 | + return reads |
| 114 | + } |
| 115 | + |
| 116 | + nonisolated private static func listTriggers( |
| 117 | + tables: [String], |
| 118 | + schema: String?, |
| 119 | + endpointName: String, |
| 120 | + using plugin: any PluginDatabaseDriver |
| 121 | + ) async throws -> [(trigger: PluginTriggerInfo, owningTable: String?)] { |
| 122 | + guard plugin.providesBulkTriggerFetch else { |
| 123 | + return try await listTriggersPerTable(tables, schema: schema, endpointName: endpointName, using: plugin) |
| 124 | + } |
| 125 | + let triggers: [PluginTriggerInfo] |
| 126 | + do { |
| 127 | + triggers = try await plugin.fetchAllTriggers(schema: schema) |
| 128 | + } catch is CancellationError { |
| 129 | + throw CancellationError() |
| 130 | + } catch { |
| 131 | + definitionLogger.warning( |
| 132 | + "Whole-schema trigger read failed, falling back per table: \(error.publicLogShape, privacy: .public)" |
| 133 | + ) |
| 134 | + return try await listTriggersPerTable(tables, schema: schema, endpointName: endpointName, using: plugin) |
| 135 | + } |
| 136 | + let inScope = Set(tables.map { $0.lowercased() }) |
| 137 | + return triggers |
| 138 | + .filter { trigger in |
| 139 | + guard let table = trigger.table?.lowercased() else { return true } |
| 140 | + return inScope.contains(table) |
| 141 | + } |
| 142 | + .map { (trigger: $0, owningTable: nil) } |
| 143 | + } |
| 144 | + |
| 145 | + nonisolated private static func listTriggersPerTable( |
| 146 | + _ tables: [String], |
| 147 | + schema: String?, |
| 148 | + endpointName: String, |
| 149 | + using plugin: any PluginDatabaseDriver |
| 150 | + ) async throws -> [(trigger: PluginTriggerInfo, owningTable: String?)] { |
| 151 | + var listed: [(trigger: PluginTriggerInfo, owningTable: String?)] = [] |
| 152 | + for table in tables { |
| 153 | + try Task.checkCancellation() |
| 154 | + do { |
| 155 | + listed += try await plugin.fetchTriggers(table: table, schema: schema) |
| 156 | + .map { (trigger: $0, owningTable: table) } |
| 157 | + } catch { |
| 158 | + throw listingFailure(error, message: String( |
| 159 | + format: String(localized: "The triggers on %1$@ in %2$@ could not be listed: %3$@"), |
| 160 | + table, endpointName, error.localizedDescription |
| 161 | + )) |
| 162 | + } |
| 163 | + } |
| 164 | + return listed |
| 165 | + } |
| 166 | + |
| 167 | + nonisolated private static func listingFailure(_ error: Error, message: String) -> Error { |
| 168 | + guard !(error is CancellationError), !Task.isCancelled else { return CancellationError() } |
| 169 | + definitionLogger.warning("Definition listing failed: \(error.publicLogShape, privacy: .public)") |
| 170 | + return CompareSyncError.readFailed(message) |
| 171 | + } |
| 172 | + |
| 173 | + nonisolated private static func definitionRead( |
| 174 | + name: String, |
| 175 | + kind: CompareObjectKind, |
| 176 | + schema: String?, |
| 177 | + signature: String?, |
| 178 | + reading: () async throws -> String |
| 179 | + ) async throws -> RoutineSourceRead { |
| 180 | + do { |
| 181 | + let source = try await reading() |
| 182 | + return RoutineSourceRead(name: name, kind: kind, schema: schema, signature: signature, source: source) |
| 183 | + } catch { |
| 184 | + guard !(error is CancellationError), !Task.isCancelled else { throw CancellationError() } |
| 185 | + definitionLogger.warning( |
| 186 | + "Definition read failed for \(kind.rawValue, privacy: .public) \(name, privacy: .private(mask: .hash)): \(error.publicLogShape, privacy: .public)" |
| 187 | + ) |
| 188 | + return RoutineSourceRead( |
| 189 | + name: name, kind: kind, schema: schema, signature: signature, |
| 190 | + source: "", failure: error.localizedDescription |
| 191 | + ) |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments