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): own operational view and mutation context

Tobi53e16370da8e
diff --git a/macos/App/State/AppState.swift b/macos/App/State/AppState.swift
index edbd5d0..0b04b27 100644
--- a/macos/App/State/AppState.swift
+++ b/macos/App/State/AppState.swift
@@ -104,6 +104,8 @@ final class AppState: ObservableObject {
@Published var redisDBSize: Int?
@Published var showFlushDBConfirm = false
@Published var showRedisAddKey = false
+ @Published private(set) var redisFlushContext: RedisTabContext?
+ @Published private(set) var redisAddKeyContext: RedisTabContext?
@Published private(set) var redisKeyBrowserRefreshNonce = 0
struct RedisSessionRevisionToken: Hashable, Sendable {
@@ -157,6 +159,28 @@ final class AppState: ObservableObject {
redisKeyBrowserRefreshNonce &+= 1
}
+ func presentRedisAddKey() {
+ guard let context = currentRedisContext else { return }
+ redisAddKeyContext = context
+ showRedisAddKey = true
+ }
+
+ func dismissRedisAddKey() {
+ showRedisAddKey = false
+ redisAddKeyContext = nil
+ }
+
+ func presentRedisFlushConfirmation() {
+ guard let context = currentRedisContext else { return }
+ redisFlushContext = context
+ showFlushDBConfirm = true
+ }
+
+ func dismissRedisFlushConfirmation() {
+ showFlushDBConfirm = false
+ redisFlushContext = nil
+ }
+
// MARK: - Query Log (global, shared across all tables)
@Published var queryLog: [QueryLogEntry] = []
@Published var showQueryLog: Bool = false
@@ -1068,25 +1092,47 @@ final class AppState: ObservableObject {
}
func openRedisServerInfo() {
- if let existing = tabs.first(where: { $0.type == .redisServerInfo }) {
+ guard let context = currentRedisContext else { return }
+ if let existing = tabs.first(where: {
+ $0.type == .redisServerInfo && $0.redisContext == context
+ }) {
activeTabId = existing.id
return
}
- let tab = ContentTab(id: UUID(), type: .redisServerInfo, title: "Server Info", tableName: nil, schema: nil, databaseName: currentDatabaseName)
+ let tab = ContentTab(
+ id: UUID(),
+ type: .redisServerInfo,
+ title: "Server Info",
+ tableName: nil,
+ schema: nil,
+ databaseName: context.databaseName,
+ redisContext: context
+ )
tabs.append(tab)
activeTabId = tab.id
- if let db = currentDatabaseName { ensureTabGroup(for: db) }
+ ensureTabGroup(for: context.databaseName)
}
func openRedisSlowLog() {
- if let existing = tabs.first(where: { $0.type == .redisSlowLog }) {
+ guard let context = currentRedisContext else { return }
+ if let existing = tabs.first(where: {
+ $0.type == .redisSlowLog && $0.redisContext == context
+ }) {
activeTabId = existing.id
return
}
- let tab = ContentTab(id: UUID(), type: .redisSlowLog, title: "Slow Log", tableName: nil, schema: nil, databaseName: currentDatabaseName)
+ let tab = ContentTab(
+ id: UUID(),
+ type: .redisSlowLog,
+ title: "Slow Log",
+ tableName: nil,
+ schema: nil,
+ databaseName: context.databaseName,
+ redisContext: context
+ )
tabs.append(tab)
activeTabId = tab.id
- if let db = currentDatabaseName { ensureTabGroup(for: db) }
+ ensureTabGroup(for: context.databaseName)
}
func openNewQueryTab() {
diff --git a/macos/Presentation/Views/Main/MainView.swift b/macos/Presentation/Views/Main/MainView.swift
index a7f51b5..290adc5 100644
--- a/macos/Presentation/Views/Main/MainView.swift
+++ b/macos/Presentation/Views/Main/MainView.swift
@@ -1191,19 +1191,32 @@ struct WorkspaceView: View {
}
}
.alert("Flush Database", isPresented: $appState.showFlushDBConfirm) {
- Button("Cancel", role: .cancel) {}
+ Button("Cancel", role: .cancel) {
+ appState.dismissRedisFlushConfirmation()
+ }
Button("Flush", role: .destructive) {
+ let context = appState.redisFlushContext
+ appState.dismissRedisFlushConfirmation()
Task {
- if let redis = appState.activeAdapter as? RedisAdapter {
- try? await redis.flushDB()
+ guard let context else { return }
+ let result = await appState.performRedisOperation(for: context) { redis in
+ try await redis.flushDB()
+ }
+ if case .success? = result {
appState.redisDBSize = 0
+ appState.requestRedisKeyBrowserRefresh()
NotificationCenter.default.post(name: .reloadData, object: nil)
}
}
}
} message: { Text("This will permanently delete ALL keys in the current database. This cannot be undone.") }
- .sheet(isPresented: $appState.showRedisAddKey) {
- RedisAddKeySheet()
+ .sheet(
+ isPresented: $appState.showRedisAddKey,
+ onDismiss: { appState.dismissRedisAddKey() }
+ ) {
+ if let context = appState.redisAddKeyContext {
+ RedisAddKeySheet(redisContext: context)
+ }
}
}
@@ -1258,9 +1271,19 @@ struct WorkspaceView: View {
)
}
case .redisServerInfo:
- RedisServerInfoView()
+ if let context = tab.redisContext,
+ appState.activeRedisAdapter(for: context) != nil {
+ RedisServerInfoView(redisContext: context)
+ } else {
+ redisContextMismatchView
+ }
case .redisSlowLog:
- RedisSlowLogView()
+ if let context = tab.redisContext,
+ appState.activeRedisAdapter(for: context) != nil {
+ RedisSlowLogView(redisContext: context)
+ } else {
+ redisContextMismatchView
+ }
}
}
diff --git a/macos/Presentation/Views/Redis/RedisAddKeySheet.swift b/macos/Presentation/Views/Redis/RedisAddKeySheet.swift
index 26dc5f7..9210cab 100644
--- a/macos/Presentation/Views/Redis/RedisAddKeySheet.swift
+++ b/macos/Presentation/Views/Redis/RedisAddKeySheet.swift
@@ -9,6 +9,8 @@ struct RedisAddKeySheet: View {
@EnvironmentObject private var appState: AppState
@Environment(\.dismiss) private var dismiss
+ let redisContext: AppState.RedisTabContext
+
@State private var keyName = ""
@State private var keyType: RedisKeyType = .string
@State private var ttlString = ""
@@ -145,7 +147,6 @@ struct RedisAddKeySheet: View {
// MARK: - Create
private func createKey() {
- guard let redis = appState.activeAdapter as? RedisAdapter else { return }
isCreating = true
errorMessage = nil
@@ -163,17 +164,25 @@ struct RedisAddKeySheet: View {
let ttl = Int(ttlString)
Task {
- do {
- try await redis.redisInsertTyped(key: keyName, type: keyType, data: data, ttl: ttl)
- await MainActor.run {
- dismiss()
- NotificationCenter.default.post(name: .reloadData, object: nil)
- }
- } catch {
- await MainActor.run {
- errorMessage = error.localizedDescription
- isCreating = false
- }
+ let result = await appState.performRedisOperation(for: redisContext) { redis in
+ try await redis.redisInsertTyped(
+ key: keyName,
+ type: keyType,
+ data: data,
+ ttl: ttl
+ )
+ }
+ switch result {
+ case .success?:
+ appState.requestRedisKeyBrowserRefresh()
+ dismiss()
+ NotificationCenter.default.post(name: .reloadData, object: nil)
+ case .failure(let error)?:
+ errorMessage = error.localizedDescription
+ isCreating = false
+ case nil:
+ errorMessage = "The Redis connection or database changed. Reopen Add Key and try again."
+ isCreating = false
}
}
}
diff --git a/macos/Presentation/Views/Redis/RedisKeyBrowserView.swift b/macos/Presentation/Views/Redis/RedisKeyBrowserView.swift
index 56fb21a..f742407 100644
--- a/macos/Presentation/Views/Redis/RedisKeyBrowserView.swift
+++ b/macos/Presentation/Views/Redis/RedisKeyBrowserView.swift
@@ -242,6 +242,30 @@ struct RedisKeyBrowserView: View {
Spacer()
+ Menu {
+ Button("Add Key…") {
+ appState.presentRedisAddKey()
+ }
+ Divider()
+ Button("Server Info") {
+ appState.openRedisServerInfo()
+ }
+ Button("Slow Log") {
+ appState.openRedisSlowLog()
+ }
+ Divider()
+ Button("Flush Database…", role: .destructive) {
+ appState.presentRedisFlushConfirmation()
+ }
+ } label: {
+ Image(systemName: "ellipsis.circle")
+ .font(.system(size: 11))
+ }
+ .menuStyle(.borderlessButton)
+ .fixedSize()
+ .accessibilityLabel("Redis actions")
+ .help("Redis actions")
+
Button {
appState.requestRedisKeyBrowserRefresh()
} label: {
diff --git a/macos/Presentation/Views/Redis/RedisServerInfoView.swift b/macos/Presentation/Views/Redis/RedisServerInfoView.swift
index 194a5fe..80255be 100644
--- a/macos/Presentation/Views/Redis/RedisServerInfoView.swift
+++ b/macos/Presentation/Views/Redis/RedisServerInfoView.swift
@@ -7,7 +7,11 @@ import SwiftUI
struct RedisServerInfoView: View {
@EnvironmentObject private var appState: AppState
+
+ let redisContext: AppState.RedisTabContext
+
@State private var sections: [RedisInfoSection] = []
+ @State private var databaseSize: Int?
@State private var isLoading = true
@State private var errorMessage: String?
@State private var autoRefresh = false
@@ -62,7 +66,7 @@ struct RedisServerInfoView: View {
}
}
}
- .task { await load() }
+ .task(id: redisContext) { await load() }
.onChange(of: autoRefresh) { _, on in
refreshTask?.cancel()
if on {
@@ -85,7 +89,7 @@ struct RedisServerInfoView: View {
metricCard("Memory", value: findValue("used_memory_human") ?? "—")
metricCard("Clients", value: findValue("connected_clients") ?? "—")
metricCard("Uptime", value: uptimeString)
- metricCard("Keys", value: appState.redisDBSize.map { "\($0)" } ?? "—")
+ metricCard("Keys", value: databaseSize.map { "\($0)" } ?? "—")
metricCard("Ops/sec", value: findValue("instantaneous_ops_per_sec") ?? "—")
metricCard("Hit Rate", value: hitRateString)
Spacer()
@@ -148,18 +152,25 @@ struct RedisServerInfoView: View {
}
private func load() async {
- guard let redis = appState.activeAdapter as? RedisAdapter else {
- await MainActor.run { errorMessage = "Not connected to Redis"; isLoading = false }
- return
+ isLoading = true
+ let result = await appState.performRedisOperation(for: redisContext) { redis in
+ let loadedSections = try await redis.serverInfoSections()
+ let loadedDatabaseSize = try await redis.dbSize()
+ return (sections: loadedSections, databaseSize: loadedDatabaseSize)
}
- do {
- let s = try await redis.serverInfoSections()
- await MainActor.run { sections = s; errorMessage = nil; isLoading = false }
- if let size = try? await redis.dbSize() {
- await MainActor.run { appState.redisDBSize = size }
- }
- } catch {
- await MainActor.run { errorMessage = error.localizedDescription; isLoading = false }
+
+ switch result {
+ case .success(let snapshot)?:
+ sections = snapshot.sections
+ databaseSize = snapshot.databaseSize
+ appState.redisDBSize = snapshot.databaseSize
+ errorMessage = nil
+ isLoading = false
+ case .failure(let error)?:
+ errorMessage = error.localizedDescription
+ isLoading = false
+ case nil:
+ isLoading = false
}
}
}
diff --git a/macos/Presentation/Views/Redis/RedisSlowLogView.swift b/macos/Presentation/Views/Redis/RedisSlowLogView.swift
index 0c771ae..dde829f 100644
--- a/macos/Presentation/Views/Redis/RedisSlowLogView.swift
+++ b/macos/Presentation/Views/Redis/RedisSlowLogView.swift
@@ -7,9 +7,13 @@ import SwiftUI
struct RedisSlowLogView: View {
@EnvironmentObject private var appState: AppState
+
+ let redisContext: AppState.RedisTabContext
+
@State private var entries: [RedisSlowLogEntry] = []
@State private var isLoading = true
@State private var showResetConfirm = false
+ @State private var errorMessage: String?
var body: some View {
VStack(spacing: 0) {
@@ -30,7 +34,18 @@ struct RedisSlowLogView: View {
.padding(.horizontal, 12).padding(.vertical, 8)
Divider()
- if isLoading {
+ if let errorMessage {
+ VStack(spacing: 8) {
+ Image(systemName: "exclamationmark.triangle")
+ .font(.system(size: 28))
+ .foregroundStyle(.orange)
+ Text(errorMessage)
+ .font(.system(size: 12))
+ .foregroundStyle(.secondary)
+ .textSelection(.enabled)
+ }
+ .frame(maxWidth: .infinity, maxHeight: .infinity)
+ } else if isLoading {
ProgressView().frame(maxWidth: .infinity, maxHeight: .infinity)
} else if entries.isEmpty {
VStack(spacing: 8) {
@@ -68,15 +83,11 @@ struct RedisSlowLogView: View {
}
}
}
- .task { await load() }
+ .task(id: redisContext) { await load() }
.alert("Reset Slow Log", isPresented: $showResetConfirm) {
Button("Cancel", role: .cancel) {}
Button("Reset", role: .destructive) {
- Task {
- guard let redis = appState.activeAdapter as? RedisAdapter else { return }
- try? await redis.executeRaw(sql: "SLOWLOG RESET")
- await load()
- }
+ Task { await reset() }
}
} message: { Text("This will clear all slow log entries.") }
}
@@ -130,10 +141,33 @@ struct RedisSlowLogView: View {
}
private func load() async {
- guard let redis = appState.activeAdapter as? RedisAdapter else { return }
- do {
- entries = try await redis.slowLog()
+ isLoading = true
+ let result = await appState.performRedisOperation(for: redisContext) { redis in
+ try await redis.slowLog()
+ }
+ publish(result)
+ }
+
+ private func reset() async {
+ isLoading = true
+ let result = await appState.performRedisOperation(for: redisContext) { redis in
+ _ = try await redis.executeRaw(sql: "SLOWLOG RESET")
+ return try await redis.slowLog()
+ }
+ publish(result)
+ }
+
+ private func publish(_ result: Result<[RedisSlowLogEntry], Error>?) {
+ switch result {
+ case .success(let loadedEntries)?:
+ entries = loadedEntries
+ errorMessage = nil
isLoading = false
- } catch { isLoading = false }
+ case .failure(let error)?:
+ errorMessage = error.localizedDescription
+ isLoading = false
+ case nil:
+ isLoading = false
+ }
}
}
diff --git a/macos/Presentation/Views/Sidebar/SidebarView.swift b/macos/Presentation/Views/Sidebar/SidebarView.swift
index 1ff2383..7abf2b3 100644
--- a/macos/Presentation/Views/Sidebar/SidebarView.swift
+++ b/macos/Presentation/Views/Sidebar/SidebarView.swift
@@ -530,13 +530,13 @@ struct SidebarItemRow: View {
appState.openTable(name: "Keys", schema: nil)
}
Button("Add Key…") {
- appState.showRedisAddKey = true
+ appState.presentRedisAddKey()
}
Divider()
Button("Server Info") { appState.openRedisServerInfo() }
Button("Slow Log") { appState.openRedisSlowLog() }
Divider()
- Button("Flush Database…") { appState.showFlushDBConfirm = true }
+ Button("Flush Database…") { appState.presentRedisFlushConfirmation() }
Divider()
Button("Refresh") { appState.refreshSidebar() }
} else {