-
-
Notifications
You must be signed in to change notification settings - Fork 109
fix: add SQL fallbacks for DROP/TRUNCATE and fix MySQL FK metadata after db switch #353
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
53a9ae0
fix: add SQL fallbacks for DROP/TRUNCATE and fix MySQL FK metadata af…
datlechin c71a9ab
fix: address review — add MySQL FK changelog entry, remove unused quo…
datlechin 1ae79ef
fix: use _activeDatabase in connect() so reconnect preserves switched…
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
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
137 changes: 137 additions & 0 deletions
137
TableProTests/Core/Plugins/PluginDriverAdapterTableOpsTests.swift
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,137 @@ | ||
| // | ||
| // PluginDriverAdapterTableOpsTests.swift | ||
| // TableProTests | ||
| // | ||
|
|
||
| import Foundation | ||
| @testable import TablePro | ||
| import TableProPluginKit | ||
| import Testing | ||
|
|
||
| private final class StubTableOpsDriver: PluginDatabaseDriver { | ||
| var supportsSchemas: Bool { false } | ||
| var supportsTransactions: Bool { false } | ||
| var currentSchema: String? { nil } | ||
| var serverVersion: String? { nil } | ||
|
|
||
| var truncateOverride: ((String, String?, Bool) -> [String]?)? | ||
| var dropOverride: ((String, String, String?, Bool) -> String?)? | ||
|
|
||
| func truncateTableStatements(table: String, schema: String?, cascade: Bool) -> [String]? { | ||
| truncateOverride?(table, schema, cascade) | ||
| } | ||
|
|
||
| func dropObjectStatement(name: String, objectType: String, schema: String?, cascade: Bool) -> String? { | ||
| dropOverride?(name, objectType, schema, cascade) | ||
| } | ||
|
|
||
| func connect() async throws {} | ||
| func disconnect() {} | ||
| func ping() async throws {} | ||
| func execute(query: String) async throws -> PluginQueryResult { | ||
| PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) | ||
| } | ||
|
|
||
| func fetchRowCount(query: String) async throws -> Int { 0 } | ||
| func fetchRows(query: String, offset: Int, limit: Int) async throws -> PluginQueryResult { | ||
| PluginQueryResult(columns: [], columnTypeNames: [], rows: [], rowsAffected: 0, executionTime: 0) | ||
| } | ||
|
|
||
| func fetchTables(schema: String?) async throws -> [PluginTableInfo] { [] } | ||
| func fetchColumns(table: String, schema: String?) async throws -> [PluginColumnInfo] { [] } | ||
| func fetchIndexes(table: String, schema: String?) async throws -> [PluginIndexInfo] { [] } | ||
| func fetchForeignKeys(table: String, schema: String?) async throws -> [PluginForeignKeyInfo] { [] } | ||
| func fetchTableDDL(table: String, schema: String?) async throws -> String { "" } | ||
| func fetchViewDefinition(view: String, schema: String?) async throws -> String { "" } | ||
| func fetchTableMetadata(table: String, schema: String?) async throws -> PluginTableMetadata { | ||
| PluginTableMetadata(tableName: table) | ||
| } | ||
|
|
||
| func fetchDatabases() async throws -> [String] { [] } | ||
| func fetchDatabaseMetadata(_ database: String) async throws -> PluginDatabaseMetadata { | ||
| PluginDatabaseMetadata(name: database) | ||
| } | ||
| } | ||
|
|
||
| @Suite("PluginDriverAdapter table operations") | ||
| struct PluginDriverAdapterTableOpsTests { | ||
| private func makeAdapter(driver: StubTableOpsDriver) -> PluginDriverAdapter { | ||
| let connection = DatabaseConnection(name: "Test", type: .postgresql) | ||
| return PluginDriverAdapter(connection: connection, pluginDriver: driver) | ||
| } | ||
|
|
||
| // MARK: - dropObjectStatement | ||
|
|
||
| @Test("Fallback produces DROP TABLE with quoted name") | ||
| func dropTableFallback() { | ||
| let adapter = makeAdapter(driver: StubTableOpsDriver()) | ||
| let result = adapter.dropObjectStatement(name: "users", objectType: "TABLE", schema: nil, cascade: false) | ||
| #expect(result == "DROP TABLE \"users\"") | ||
| } | ||
|
|
||
| @Test("Fallback produces DROP VIEW for views") | ||
| func dropViewFallback() { | ||
| let adapter = makeAdapter(driver: StubTableOpsDriver()) | ||
| let result = adapter.dropObjectStatement(name: "active_users", objectType: "VIEW", schema: nil, cascade: false) | ||
| #expect(result == "DROP VIEW \"active_users\"") | ||
| } | ||
|
|
||
| @Test("Fallback appends CASCADE when requested") | ||
| func dropWithCascade() { | ||
| let adapter = makeAdapter(driver: StubTableOpsDriver()) | ||
| let result = adapter.dropObjectStatement(name: "orders", objectType: "TABLE", schema: nil, cascade: true) | ||
| #expect(result == "DROP TABLE \"orders\" CASCADE") | ||
| } | ||
|
|
||
| @Test("Fallback includes schema qualification") | ||
| func dropWithSchema() { | ||
| let adapter = makeAdapter(driver: StubTableOpsDriver()) | ||
| let result = adapter.dropObjectStatement(name: "users", objectType: "TABLE", schema: "public", cascade: false) | ||
| #expect(result == "DROP TABLE \"public\".\"users\"") | ||
| } | ||
|
|
||
| @Test("Plugin override is returned when non-nil") | ||
| func dropPluginOverride() { | ||
| let driver = StubTableOpsDriver() | ||
| driver.dropOverride = { name, objectType, _, _ in | ||
| "DROP \(objectType) IF EXISTS `\(name)`" | ||
| } | ||
| let adapter = makeAdapter(driver: driver) | ||
| let result = adapter.dropObjectStatement(name: "users", objectType: "TABLE", schema: nil, cascade: false) | ||
| #expect(result == "DROP TABLE IF EXISTS `users`") | ||
| } | ||
|
|
||
| // MARK: - truncateTableStatements | ||
|
|
||
| @Test("Fallback produces TRUNCATE TABLE with quoted name") | ||
| func truncateFallback() { | ||
| let adapter = makeAdapter(driver: StubTableOpsDriver()) | ||
| let result = adapter.truncateTableStatements(table: "users", schema: nil, cascade: false) | ||
| #expect(result == ["TRUNCATE TABLE \"users\""]) | ||
| } | ||
|
|
||
| @Test("Fallback appends CASCADE when requested") | ||
| func truncateWithCascade() { | ||
| let adapter = makeAdapter(driver: StubTableOpsDriver()) | ||
| let result = adapter.truncateTableStatements(table: "orders", schema: nil, cascade: true) | ||
| #expect(result == ["TRUNCATE TABLE \"orders\" CASCADE"]) | ||
| } | ||
|
|
||
| @Test("Fallback includes schema qualification") | ||
| func truncateWithSchema() { | ||
| let adapter = makeAdapter(driver: StubTableOpsDriver()) | ||
| let result = adapter.truncateTableStatements(table: "users", schema: "public", cascade: false) | ||
| #expect(result == ["TRUNCATE TABLE \"public\".\"users\""]) | ||
| } | ||
|
|
||
| @Test("Plugin override is returned when non-nil") | ||
| func truncatePluginOverride() { | ||
| let driver = StubTableOpsDriver() | ||
| driver.truncateOverride = { table, _, _ in | ||
| ["DELETE FROM `\(table)`", "ALTER TABLE `\(table)` AUTO_INCREMENT = 1"] | ||
| } | ||
| let adapter = makeAdapter(driver: driver) | ||
| let result = adapter.truncateTableStatements(table: "users", schema: nil, cascade: false) | ||
| #expect(result == ["DELETE FROM `users`", "ALTER TABLE `users` AUTO_INCREMENT = 1"]) | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.