Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions electron/helpers/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,12 +251,57 @@ async function safeEndConnection(connection?: PoolConnection): Promise<void> {
}
}

interface DatabaseRow extends RowDataPacket {
Database?: string;
database?: string;
}

async function listDatabases(
config: AppConnection
): Promise<{ success: boolean; databases: string[]; message?: string }> {
let connection: PoolConnection;

try {
connection = await createConnection(config);

const [rows] = await connection.query<DatabaseRow[]>('SHOW DATABASES');
const databases = rows
.map((r: DatabaseRow) => r.Database || r.database)
.filter(
(db: string) =>
![
'information_schema',
'performance_schema',
'mysql',
'sys'
].includes(db)
);

return { success: true, databases };
} catch (err: any) {
let msg = err.message;
if (err.code === 'ER_ACCESS_DENIED_ERROR') {
msg = 'Access denied with the provided credentials';
} else if (err.code === 'ECONNREFUSED') {
msg = 'Connection refused - check host and port';
}
return {
success: false,
message: msg,
databases: []
};
} finally {
await safeEndConnection(connection);
}
}

export {
createConnection,
testConnection,
getConnectionOptions,
closeAllPools,
releaseConnection,
safeEndConnection,
listDatabases,
ERROR_MESSAGES
};
2 changes: 2 additions & 0 deletions electron/main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { registerSqlExecutorHandlers } from '../modules/sql-executor';
import { registerUpdaterHandlers, cleanup } from '../modules/updater';
import { registerSshHandlers } from '../modules/ssh';
import { registerGitHandlers } from '../modules/git';
import { registerDockMenuHandlers } from '../modules/dock-menu';
import { closeAllPools } from '../helpers/mysql';
import { closeAllConnections, closeAllTunnels } from '../helpers/ssh';

Expand Down Expand Up @@ -431,6 +432,7 @@ function registerHandlers(win: BrowserWindow) {
registerUpdaterHandlers(win);
registerSshHandlers();
registerGitHandlers();
registerDockMenuHandlers();

handlersRegistered = true;
}
Expand Down
234 changes: 234 additions & 0 deletions electron/modules/dock-menu.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,234 @@
import { ipcMain, Menu, app, BrowserWindow } from 'electron';
import { AppConnection } from '../../src/types/ssh-connection';
import { listDatabases } from '../helpers/mysql';
import fs from 'fs';
import path from 'path';

let currentConnectionId: string | null = null;
let currentAppConnection: AppConnection | null = null;
let availableDatabases: string[] = [];
let currentDatabase: string | null = null;
let projectDatabase: string | null = null;

function updateDockMenu() {
if (process.platform !== 'darwin') {
return;
}

if (
!currentConnectionId ||
!currentAppConnection ||
availableDatabases.length === 0
) {
try {
app.dock.setMenu(Menu.buildFromTemplate([]));
} catch (error) {
console.error('Error clearing dock menu:', error);
}
return;
}

const menuItems = availableDatabases.map((database) => ({
label: database === currentDatabase ? `${database} ✓` : database,
type: 'normal' as const,
click: () => handleDatabaseSwitch(database)
}));

try {
const menu = Menu.buildFromTemplate([
{
label: 'Switch Database',
type: 'submenu',
submenu: menuItems
}
]);

app.dock.setMenu(menu);
} catch (error) {
console.error('Error setting dock menu:', error);
}
}

async function updateEnvDatabase(projectPath: string, database: string) {
try {
if (!projectPath || !database) {
return {
success: false,
message: 'Missing project path or database name'
};
}

const envPath = path.join(projectPath, '.env');

if (!fs.existsSync(envPath)) {
return {
success: false,
message: '.env file not found in project'
};
}

let envContent = fs.readFileSync(envPath, 'utf8');
const lines = envContent.split('\n');
let dbLineFound = false;

for (let i = 0; i < lines.length; i++) {
const line = lines[i];

if (line.trim().startsWith('#')) {
continue;
}

if (line.trim().startsWith('DB_DATABASE=')) {
lines[i] = `DB_DATABASE=${database}`;
dbLineFound = true;
break;
}
}

if (!dbLineFound) {
lines.push(`DB_DATABASE=${database}`);
}

const updatedContent = lines.join('\n');
fs.writeFileSync(envPath, updatedContent);

return {
success: true,
message: `Updated database to ${database} in .env file`
};
} catch (error: any) {
return {
success: false,
message: error.message || 'Failed to update database in .env file'
};
}
}

async function handleDatabaseSwitch(databaseName: string) {
if (
!currentAppConnection ||
!currentConnectionId ||
databaseName === currentDatabase
) {
return;
}

try {
currentAppConnection.localDbConfig.database = databaseName;
currentDatabase = databaseName;

let envUpdated = false;
if (projectDatabase) {
const envResult = await updateEnvDatabase(
projectDatabase,
databaseName
);
if (envResult.success) {
envUpdated = true;
} else {
console.error('Failed to update .env file:', envResult.message);
}
}

updateDockMenu();

const windows = BrowserWindow.getAllWindows();

windows.forEach((window) => {
if (window.webContents) {
window.webContents.send('database-switched', {
connectionId: currentConnectionId,
database: databaseName,
success: true,
envUpdated
});
}
});
} catch (error: any) {
console.error('Failed to switch database from dock menu:', error);
}
}

export function registerDockMenuHandlers() {
ipcMain.handle(
'dock-menu:set-connection',
async (
_,
connectionId: string,
appConnection: AppConnection,
projectPath?: string
) => {
try {
currentConnectionId = connectionId;
currentAppConnection = appConnection;
currentDatabase = appConnection.localDbConfig?.database || null;
projectDatabase = projectPath || null;

const result = await listDatabases(appConnection);
if (result.success) {
availableDatabases = result.databases;
updateDockMenu();
}

return { success: true };
} catch (error: any) {
console.error('Error setting dock menu connection:', error);
return { success: false, message: error.message };
}
}
);

ipcMain.handle('dock-menu:clear-connection', async () => {
try {
currentConnectionId = null;
currentAppConnection = null;
availableDatabases = [];
currentDatabase = null;
projectDatabase = null;
updateDockMenu();
return { success: true };
} catch (error: any) {
console.error('Error clearing dock menu connection:', error);
return { success: false, message: error.message };
}
});

ipcMain.handle(
'dock-menu:update-databases',
async (
_,
databases: string[],
currentDb: string,
projectDb?: string
) => {
try {
availableDatabases = databases;
currentDatabase = currentDb;
projectDatabase = projectDb || null;
updateDockMenu();
return { success: true };
} catch (error: any) {
console.error('Error updating dock menu databases:', error);
return { success: false, message: error.message };
}
}
);

ipcMain.handle('dock-menu:refresh', async () => {
if (!currentAppConnection) {
return { success: false, message: 'No active connection' };
}

try {
const result = await listDatabases(currentAppConnection);
if (result.success) {
availableDatabases = result.databases;
updateDockMenu();
}
return result;
} catch (error: any) {
console.error('Error refreshing dock menu databases:', error);
return { success: false, message: error.message };
}
});
}
46 changes: 4 additions & 42 deletions electron/modules/mysql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,11 @@ import { ipcMain } from 'electron';
import {
testConnection,
createConnection,
safeEndConnection
safeEndConnection,
listDatabases
} from '../helpers/mysql';
import { AppConnection } from '../../src/types/ssh-connection';
import { PoolConnection, RowDataPacket } from 'mysql2/promise';

interface DatabaseRow extends RowDataPacket {
Database?: string;
database?: string;
}
import { PoolConnection } from 'mysql2/promise';

function registerMysqlHandlers() {
ipcMain.handle(
Expand Down Expand Up @@ -59,41 +55,7 @@ function registerMysqlHandlers() {
);

ipcMain.handle('list-databases', async (_, config: AppConnection) => {
let connection: PoolConnection;

try {
connection = await createConnection(config);

const [rows] =
await connection.query<DatabaseRow[]>('SHOW DATABASES');
const databases = rows
.map((r: DatabaseRow) => r.Database || r.database)
.filter(
(db: string) =>
![
'information_schema',
'performance_schema',
'mysql',
'sys'
].includes(db)
);

return { success: true, databases };
} catch (err) {
let msg = err.message;
if (err.code === 'ER_ACCESS_DENIED_ERROR') {
msg = 'Access denied with the provided credentials';
} else if (err.code === 'ECONNREFUSED') {
msg = 'Connection refused - check host and port';
}
return {
success: false,
message: msg,
databases: []
};
} finally {
await safeEndConnection(connection);
}
return await listDatabases(config);
});

ipcMain.handle(
Expand Down
Loading