-
-
Notifications
You must be signed in to change notification settings - Fork 347
feat(compare): compare and sync structure or data between two connections (#721) #1968
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
datlechin
wants to merge
3
commits into
main
Choose a base branch
from
feat/721-compare-sync
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
20bfb6e
feat(compare): compare and sync structure or data between two connect…
datlechin 5971751
feat(compare): key and column selection, definition diff, profiles, a…
datlechin 856a9f6
fix(compare): make the merge join's key order contract explicit and v…
datlechin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import Foundation | ||
|
|
||
| public enum ForeignKeyTopologicalSort { | ||
| public static func orderedNames( | ||
| _ names: [String], | ||
| foreignKeysByTable: [String: [PluginForeignKeyInfo]] | ||
| ) -> [String] { | ||
| let nameSet = Set(names) | ||
| var indegree: [String: Int] = [:] | ||
| var children: [String: Set<String>] = [:] | ||
| for name in names { indegree[name] = 0 } | ||
|
|
||
| for name in names { | ||
| var seenParents: Set<String> = [] | ||
| for fk in foreignKeysByTable[name] ?? [] where fk.referencedTable != name { | ||
| guard nameSet.contains(fk.referencedTable), | ||
| !seenParents.contains(fk.referencedTable) else { continue } | ||
| seenParents.insert(fk.referencedTable) | ||
| children[fk.referencedTable, default: []].insert(name) | ||
| indegree[name, default: 0] += 1 | ||
| } | ||
| } | ||
|
|
||
| var queue = names.filter { (indegree[$0] ?? 0) == 0 }.sorted() | ||
| var ordered: [String] = [] | ||
| while !queue.isEmpty { | ||
| let head = queue.removeFirst() | ||
| ordered.append(head) | ||
| for child in (children[head] ?? []).sorted() { | ||
| indegree[child] = (indegree[child] ?? 0) - 1 | ||
| if indegree[child] == 0 { | ||
| queue.append(child) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| guard ordered.count < names.count else { return ordered } | ||
| let placed = Set(ordered) | ||
| return ordered + names.filter { !placed.contains($0) }.sorted() | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| // | ||
| // CompareSyncEndpoint.swift | ||
| // TablePro | ||
| // | ||
| // One side of a comparison. The source never changes; the target is written to. | ||
| // | ||
|
|
||
| import Foundation | ||
| import TableProPluginKit | ||
|
|
||
| internal struct CompareSyncEndpoint: Hashable, Identifiable { | ||
| internal let connectionId: UUID | ||
| internal let displayName: String | ||
| internal let databaseType: DatabaseType | ||
| internal let database: String? | ||
| internal let schema: String? | ||
| internal let safeModeLevel: SafeModeLevel | ||
| internal let color: ConnectionColor | ||
|
|
||
| internal var id: String { | ||
| "\(connectionId.uuidString)-\(database ?? "")-\(schema ?? "")" | ||
| } | ||
|
|
||
| internal var canBeWrittenTo: Bool { | ||
| safeModeLevel != .readOnly | ||
| } | ||
|
|
||
| internal var ineligibleAsTargetReason: String? { | ||
| guard !canBeWrittenTo else { return nil } | ||
| return String(localized: "Read-Only. Choose a different connection to write changes to.") | ||
| } | ||
|
|
||
| internal var qualifiedDescription: String { | ||
| var parts = [displayName] | ||
| if let database, !database.isEmpty { parts.append(database) } | ||
| if let schema, !schema.isEmpty { parts.append(schema) } | ||
| return parts.joined(separator: " / ") | ||
| } | ||
| } | ||
|
|
||
| internal extension CompareSyncEndpoint { | ||
| static func candidates(from connections: [DatabaseConnection]) -> [CompareSyncEndpoint] { | ||
| connections.map { connection in | ||
| CompareSyncEndpoint( | ||
| connectionId: connection.id, | ||
| displayName: connection.name, | ||
| databaseType: connection.type, | ||
| database: connection.database, | ||
| schema: nil, | ||
| safeModeLevel: connection.safeModeLevel, | ||
| color: connection.color | ||
| ) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal enum CompareSyncEligibility { | ||
| static func refusalReason( | ||
| for driver: any PluginDatabaseDriver, | ||
| mode: CompareSyncMode, | ||
| endpointName: String | ||
| ) -> String? { | ||
| let required: PluginCapabilities = mode == .structure ? .schemaCompare : .dataCompare | ||
| guard !driver.capabilities.contains(required) else { return nil } | ||
| switch mode { | ||
| case .structure: | ||
| return String( | ||
| format: String(localized: "%@ does not report structure metadata that can be compared."), | ||
| endpointName | ||
| ) | ||
| case .data: | ||
| return String( | ||
| format: String(localized: "%@ does not support reading rows in key order, which data compare needs."), | ||
| endpointName | ||
| ) | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| // | ||
| // CompareSyncEngineFamily.swift | ||
| // TablePro | ||
| // | ||
| // Which pairs of database types may generate a structure sync script. | ||
| // Column data types are driver-native strings, so generating DDL for one engine | ||
| // from another engine's metadata is unsound. Comparison stays available across | ||
| // engines as an informational read; only script generation is gated. | ||
| // | ||
|
|
||
| import Foundation | ||
|
|
||
| internal enum CompareSyncEngineFamily { | ||
| internal static func canGenerateStructureScript(from source: DatabaseType, to target: DatabaseType) -> Bool { | ||
| guard source != target else { return true } | ||
| return sameFamily(source, target) | ||
| } | ||
|
|
||
| internal static func sameFamily(_ lhs: DatabaseType, _ rhs: DatabaseType) -> Bool { | ||
| guard lhs != rhs else { return true } | ||
| let key = [lhs.rawValue, rhs.rawValue].sorted().joined(separator: "\u{1F}") | ||
| return compatiblePairKeys.contains(key) | ||
| } | ||
|
|
||
| private static let compatiblePairKeys: Set<String> = { | ||
| let pairs: [[DatabaseType]] = [[.mysql, .mariadb]] | ||
| return Set(pairs.map { $0.map { $0.rawValue }.sorted().joined(separator: "\u{1F}") }) | ||
| }() | ||
|
|
||
| internal static func structureScriptRefusal(from source: DatabaseType, to target: DatabaseType) -> String { | ||
| String( | ||
| format: String(localized: "Structure sync needs matching database types. %@ and %@ can be compared, but no script is generated."), | ||
| source.rawValue, target.rawValue | ||
| ) | ||
| } | ||
|
|
||
| internal static func crossEngineDataWarning(from source: DatabaseType, to target: DatabaseType) -> String? { | ||
| guard source != target else { return nil } | ||
| return String( | ||
| format: String(localized: "Syncing data from %@ to %@. Value formatting can differ between engines; review the script before applying."), | ||
| source.rawValue, target.rawValue | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This eligibility check rejects every real database driver because the commit defines the two capability bits but never adds either bit to any
PluginDatabaseDriver.capabilitiesimplementation. I checked all driver capability declarations underPlugins; consequently both structure and data comparisons always stop with the unsupported message before doing any work.Useful? React with 👍 / 👎.