Skip to content

A native macOS / windows / Linux database IDE built with Swift and AppKit. Connect to PostgreSQL, MySQL, SQLite, and Redis from a single app with a fast, keyboard-driven interface.

C++

fix(redis): reject stale query execution ownership

Tobid208630bd5ef
diff --git a/macos/App/State/AppState.swift b/macos/App/State/AppState.swift
index 5e887e9..76b126a 100644
--- a/macos/App/State/AppState.swift
+++ b/macos/App/State/AppState.swift
@@ -704,13 +704,26 @@ final class AppState: ObservableObject {
func performRedisCLIStatement(
_ statement: String,
execute: (RedisAdapter, String) async throws -> QueryResult
+ ) async -> Result<QueryResult, Error>? {
+ guard let expectedContext = currentRedisContext else { return nil }
+ return await performRedisCLIStatement(
+ statement,
+ expectedContext: expectedContext,
+ execute: execute
+ )
+ }
+
+ private func performRedisCLIStatement(
+ _ statement: String,
+ expectedContext: RedisTabContext,
+ execute: (RedisAdapter, String) async throws -> QueryResult
) async -> Result<QueryResult, Error>? {
let command = statement.trimmingCharacters(in: .whitespacesAndNewlines)
guard !command.isEmpty else { return nil }
+ guard currentRedisContext == expectedContext else { return nil }
guard let databaseName = Self.redisDatabaseNameSelected(by: command) else {
- guard let context = currentRedisContext else { return nil }
- return await performRedisOperation(for: context) { adapter in
+ return await performRedisOperation(for: expectedContext) { adapter in
try await execute(adapter, command)
}
}
@@ -791,12 +804,22 @@ final class AppState: ObservableObject {
return Optional<[RedisCLIStatementExecution]>.none
}
+ var expectedContext = context
var executions: [RedisCLIStatementExecution] = []
for command in commands {
+ guard currentRedisContext == expectedContext else { return nil }
let start = Date()
- guard let result = await performRedisCLIStatement(command, execute: execute) else {
+ guard let result = await performRedisCLIStatement(
+ command,
+ expectedContext: expectedContext,
+ execute: execute
+ ) else {
return nil
}
+ if Self.redisDatabaseNameSelected(by: command) != nil {
+ guard let nextContext = currentRedisContext else { return nil }
+ expectedContext = nextContext
+ }
executions.append(
RedisCLIStatementExecution(
statement: command,
diff --git a/macos/Presentation/Views/QueryEditor/QueryEditorView.swift b/macos/Presentation/Views/QueryEditor/QueryEditorView.swift
index 5e5e77f..1f5f6cf 100644
--- a/macos/Presentation/Views/QueryEditor/QueryEditorView.swift
+++ b/macos/Presentation/Views/QueryEditor/QueryEditorView.swift
@@ -182,9 +182,11 @@ struct QueryEditorView: View {
}
}
.onReceive(NotificationCenter.default.publisher(for: .executeQuery)) { _ in
+ guard appState.activeTabId == tabId else { return }
executeQuery()
}
.onReceive(NotificationCenter.default.publisher(for: .explainQuery)) { _ in
+ guard appState.activeTabId == tabId else { return }
explainQuery()
}
.onAppear {
@@ -232,7 +234,20 @@ struct QueryEditorView: View {
}
private func executeQuery() {
- guard let adapter = appState.activeAdapter else { return }
+ guard appState.activeTabId == tabId,
+ !isExecuting,
+ let adapter = appState.activeAdapter,
+ let tab = appState.tabs.first(where: { $0.id == tabId }) else { return }
+ let redisContext = tab.redisContext
+ if redisContext == nil, adapter.databaseType == .redis {
+ errorMessage = "Open a Redis query from the current database before running commands."
+ return
+ }
+ if let redisContext, appState.activeRedisAdapter(for: redisContext) == nil {
+ errorMessage = "This Redis query belongs to another connection or database."
+ return
+ }
+ let databaseType: DatabaseType = redisContext == nil ? adapter.databaseType : .redis
// A regular Run wipes any previous EXPLAIN output so the data grid
// can take over the results pane again.
@@ -244,7 +259,7 @@ struct QueryEditorView: View {
let fullText = sqlText.trimmingCharacters(in: .whitespacesAndNewlines)
guard !fullText.isEmpty else { return }
- let batches = splitIntoBatches(fullText, databaseType: adapter.databaseType)
+ let batches = splitIntoBatches(fullText, databaseType: databaseType)
// If only one statement, use the cursor-aware behavior (backwards compat)
let toRun: [String]
@@ -260,13 +275,7 @@ struct QueryEditorView: View {
errorMessage = nil
statusMessage = nil
- if adapter.databaseType == .redis {
- guard let redisContext = appState.tabs.first(where: { $0.id == tabId })?.redisContext,
- appState.activeRedisAdapter(for: redisContext) != nil else {
- errorMessage = "This Redis query belongs to another connection or database."
- isExecuting = false
- return
- }
+ if let redisContext {
Task {
await executeRedisStatements(toRun, from: redisContext)
}
@@ -489,7 +498,11 @@ struct QueryEditorView: View {
}
private func explainQuery() {
- guard let adapter = appState.activeAdapter else { return }
+ guard appState.activeTabId == tabId,
+ !isExecuting,
+ let adapter = appState.activeAdapter,
+ appState.tabs.first(where: { $0.id == tabId })?.redisContext == nil,
+ adapter.databaseType != .redis else { return }
let sql = statementToRun()
guard !sql.isEmpty else { return }