Skip to content

Commit 653e368

Browse files
committed
clean up
1 parent 6e52778 commit 653e368

File tree

12 files changed

+142
-90
lines changed

12 files changed

+142
-90
lines changed

graphql-server/schema.gql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,10 @@ type CurrentDatabaseState {
123123
currentDatabase: String
124124
}
125125

126+
type DoltServerStatus {
127+
active: Boolean
128+
}
129+
126130
type DiffStat {
127131
rowsUnmodified: Float!
128132
rowsAdded: Float!
@@ -328,7 +332,7 @@ type Query {
328332
currentConnection: DatabaseConnection
329333
storedConnections: [DatabaseConnection!]!
330334
databases: [String!]!
331-
checkConnection(connectionUrl: String!, name: String!, port: String, hideDoltFeatures: Boolean, useSSL: Boolean, type: DatabaseType, isLocalDolt: Boolean): Boolean!
335+
doltServerStatus(connectionUrl: String!, name: String!, port: String, hideDoltFeatures: Boolean, useSSL: Boolean, type: DatabaseType, isLocalDolt: Boolean): DoltServerStatus!
332336
databasesByConnection(connectionUrl: String!, name: String!, port: String, hideDoltFeatures: Boolean, useSSL: Boolean, type: DatabaseType, isLocalDolt: Boolean): [String!]!
333337
schemas(databaseName: String!, refName: String!): [String!]!
334338
doltDatabaseDetails: DoltDatabaseDetails!

graphql-server/src/databases/database.resolver.ts

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ class CurrentDatabaseState {
6161
currentDatabase?: string;
6262
}
6363

64+
@ObjectType()
65+
class DoltServerStatus {
66+
@Field({ nullable: true })
67+
active?: boolean;
68+
}
69+
6470
@ArgsType()
6571
class RemoveDatabaseConnectionArgs {
6672
@Field()
@@ -125,22 +131,24 @@ export class DatabaseResolver {
125131
return dbs;
126132
}
127133

128-
@Query(_returns => Boolean)
129-
async checkConnection(
134+
// Checking if the internal dolt server is running
135+
@Query(_returns => DoltServerStatus)
136+
async doltServerStatus(
130137
@Args() args: AddDatabaseConnectionArgs,
131-
): Promise<boolean> {
138+
): Promise<DoltServerStatus> {
139+
// if current connection is the same as the internal dolt server, return true
132140
if (this.conn.getWorkbenchConfig()?.connectionUrl === args.connectionUrl) {
133-
return true;
141+
return { active: true };
134142
}
135143
const workbenchConfig = getWorkbenchConfigFromArgs(args);
136144
try {
137145
const ds = getDataSource(workbenchConfig);
138146
await ds.initialize();
139147
await ds.query("SELECT 1");
140-
return true;
148+
return { active: true };
141149
} catch (error) {
142150
console.error("Error checking connection:", error.message);
143-
return false;
151+
return { active: false };
144152
}
145153
}
146154

web/main/background.ts

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { createWindow } from "./helpers";
1515
import { initMenu } from "./helpers/menu";
1616
import { removeDoltServerFolder, startServer } from "./doltServer";
1717
import { ChildProcess } from "child_process";
18-
import { L } from "ace-builds-internal/lib/bidiutil";
1918

2019
const isProd = process.env.NODE_ENV === "production";
2120
const userDataPath = app.getPath("userData");
@@ -222,16 +221,6 @@ ipcMain.handle("toggle-left-sidebar", () => {
222221
mainWindow.webContents.send("toggle-left-sidebar");
223222
});
224223

225-
function getErrorMessage(error: unknown): string {
226-
if (error instanceof Error) {
227-
return error.message;
228-
} else if (typeof error === "string") {
229-
return error;
230-
} else {
231-
return "An unknown error occurred";
232-
}
233-
}
234-
235224
ipcMain.handle(
236225
"start-dolt-server",
237226
async (event, connectionName: string, port: string, init?: boolean) => {
@@ -253,6 +242,7 @@ ipcMain.handle(
253242
ipcMain.handle(
254243
"remove-dolt-connection",
255244
async (event, connectionName: string) => {
245+
// if doltServerProcess is running, kill it
256246
if (doltServerProcess) {
257247
doltServerProcess.kill();
258248
doltServerProcess = null;
@@ -262,11 +252,27 @@ ipcMain.handle(
262252
: path.join(__dirname, "..", "build", "databases", connectionName);
263253

264254
try {
265-
await removeDoltServerFolder(dbFolderPath);
255+
const { errorMsg } = await removeDoltServerFolder(
256+
dbFolderPath,
257+
mainWindow,
258+
);
259+
if (errorMsg) {
260+
throw new Error(errorMsg);
261+
}
266262
} catch (error) {
267263
throw new Error(getErrorMessage(error));
268264
} finally {
269265
return "Connection removed successfully";
270266
}
271267
},
272268
);
269+
270+
function getErrorMessage(error: unknown): string {
271+
if (error instanceof Error) {
272+
return error.message;
273+
} else if (typeof error === "string") {
274+
return error;
275+
} else {
276+
return "An unknown error occurred";
277+
}
278+
}

web/main/doltServer.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ export async function startServer(
1515
port: string,
1616
init?: boolean,
1717
): Promise<ChildProcess | null> {
18+
// Set the path for the database folder
19+
// In production, it's in the userData directory
20+
// In development, it's in the build directory since the development userData directory clears its contents every time the app is rerun in dev mode
1821
const dbFolderPath = isProd
1922
? path.join(app.getPath("userData"), "databases", connectionName)
2023
: path.join(__dirname, "..", "build", "databases", connectionName);
@@ -32,10 +35,8 @@ export async function startServer(
3235

3336
// Initialize and start the server without checking if it's already running
3437
await initializeDoltRepository(doltPath, dbFolderPath, mainWindow);
35-
return await startServerProcess(doltPath, dbFolderPath, port, mainWindow);
36-
} else {
37-
return await startServerProcess(doltPath, dbFolderPath, port, mainWindow);
3838
}
39+
return await startServerProcess(doltPath, dbFolderPath, port, mainWindow);
3940
} catch (error) {
4041
console.error("Failed to set up Dolt server:", error);
4142
throw error;
@@ -73,7 +74,7 @@ function getDoltPaths(): string {
7374
}
7475
}
7576

76-
//initialize the Dolt repository, running dolt init in dbFolderPath
77+
//initialize the Dolt repository by running `dolt init` in dbFolderPath
7778
function initializeDoltRepository(
7879
doltPath: string,
7980
dbFolderPath: string,
@@ -86,8 +87,10 @@ function initializeDoltRepository(
8687
mainWindow.webContents.send("server-error", initErr);
8788

8889
// Clean up: Delete the folder
89-
const { errorMsg: removeFolderErr } =
90-
removeDoltServerFolder(dbFolderPath);
90+
const { errorMsg: removeFolderErr } = removeDoltServerFolder(
91+
dbFolderPath,
92+
mainWindow,
93+
);
9194
if (removeFolderErr) {
9295
mainWindow.webContents.send("server-error", removeFolderErr);
9396
}
@@ -106,8 +109,10 @@ function initializeDoltRepository(
106109
mainWindow.webContents.send("server-error", stderr);
107110

108111
// Clean up: Delete the folder
109-
const { errorMsg: removeFolderErr } =
110-
removeDoltServerFolder(dbFolderPath);
112+
const { errorMsg: removeFolderErr } = removeDoltServerFolder(
113+
dbFolderPath,
114+
mainWindow,
115+
);
111116
if (removeFolderErr) {
112117
mainWindow.webContents.send("server-error", removeFolderErr);
113118
}
@@ -124,7 +129,7 @@ function initializeDoltRepository(
124129
});
125130
}
126131

127-
// start the Dolt SQL server
132+
// start the Dolt SQL server and return the server process
128133
function startServerProcess(
129134
doltPath: string,
130135
dbFolderPath: string,
@@ -170,8 +175,6 @@ function startServerProcess(
170175
mainWindow.webContents.send("server-error", errorMessage);
171176

172177
reject(new Error(errorMessage));
173-
} else {
174-
mainWindow.webContents.send("server-log", errorMessage);
175178
}
176179

177180
// Resolve the promise when the server is ready
@@ -185,10 +188,17 @@ function startServerProcess(
185188
});
186189
}
187190

188-
export function removeDoltServerFolder(dbFolderPath: string): ErrorReturnType {
191+
export function removeDoltServerFolder(
192+
dbFolderPath: string,
193+
mainWindow: BrowserWindow,
194+
): ErrorReturnType {
189195
// Delete the folder
190196
fs.rm(dbFolderPath, { recursive: true, force: true }, err => {
191197
if (err) {
198+
mainWindow.webContents.send(
199+
`server-error:Failed to delete folder, folder:${dbFolderPath}, `,
200+
err,
201+
);
192202
return {
193203
errorMsg: `Failed to delete folder: ${err}`,
194204
};

web/renderer/components/DatabaseHeaderAndNav/queries.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,20 +45,22 @@ export const DATABASES_BY_CONNECTION = gql`
4545
}
4646
`;
4747

48-
export const CHECK_CONNECTION = gql`
49-
query CheckConnection(
48+
export const DOLT_SERVER_STATUS = gql`
49+
query DoltServerStatus(
5050
$connectionUrl: String!
5151
$name: String!
5252
$hideDoltFeatures: Boolean
5353
$useSSL: Boolean
5454
$type: DatabaseType
5555
) {
56-
checkConnection(
56+
doltServerStatus(
5757
connectionUrl: $connectionUrl
5858
name: $name
5959
hideDoltFeatures: $hideDoltFeatures
6060
useSSL: $useSSL
6161
type: $type
62-
)
62+
) {
63+
active
64+
}
6365
}
6466
`;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
.bold {
22
@apply font-semibold;
33
}
4+
5+
.alert {
6+
@apply text-red-400 font-semibold;
7+
}

web/renderer/components/DeleteModal/index.tsx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,11 @@ type Props<TData, TVariables> = {
1515
assetId?: string;
1616
btnText?: string;
1717
mutationProps: MutationProps<TData, TVariables>;
18-
callback?: (d: TData) => void;
18+
callback?: (d: TData) => Error | void;
1919
children?: ReactNode;
2020
className?: string;
2121
buttonDataCy?: string;
22+
alertMessage?: string;
2223
};
2324

2425
export default function DeleteModal<TData, TVariables>({
@@ -48,7 +49,13 @@ export default function DeleteModal<TData, TVariables>({
4849
variables: mutationProps.variables,
4950
});
5051
if (!success || !data) return;
51-
if (props.callback) props.callback(data);
52+
if (props.callback) {
53+
const error = props.callback(data);
54+
if (error) {
55+
setErr(error);
56+
return;
57+
}
58+
}
5259
onClose();
5360
};
5461

@@ -78,6 +85,7 @@ export default function DeleteModal<TData, TVariables>({
7885
{props.cannotBeUndone && " This cannot be undone."}
7986
</p>
8087
)}
88+
{props.alertMessage && <p className={css.alert}>{props.alertMessage}</p>}
8189
{children}
8290
</Modal>
8391
);

web/renderer/components/pageComponents/ConnectionsPage/ExistingConnections/Item.tsx

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
import { getDatabaseType } from "@components/DatabaseTypeLabel";
22
import { Button, ErrorMsg, Loader } from "@dolthub/react-components";
3-
import {
4-
DatabaseConnectionFragment,
5-
useCheckConnectionQuery,
6-
} from "@gen/graphql-types";
3+
import { DatabaseConnectionFragment } from "@gen/graphql-types";
74
import { IoMdClose } from "@react-icons/all-files/io/IoMdClose";
85
import Image from "next/legacy/image";
96
import cx from "classnames";
@@ -26,15 +23,12 @@ export default function Item({
2623
borderClassName,
2724
shorterLine,
2825
}: Props) {
29-
const { onAdd, err, loading } = useAddConnection(conn);
30-
const res = useCheckConnectionQuery({
31-
variables: conn,
32-
});
26+
const { onAdd, err, loading, doltServerIsActive } = useAddConnection(conn);
3327

3428
const type = getDatabaseType(conn.type ?? undefined, !!conn.isDolt);
3529
const handleSubmit = async (e: React.FormEvent) => {
3630
e.preventDefault();
37-
if (!res.data?.checkConnection) {
31+
if (!doltServerIsActive) {
3832
try {
3933
await window.ipc.invoke(
4034
"start-dolt-server",

web/renderer/components/pageComponents/ConnectionsPage/ExistingConnections/index.tsx

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,20 @@ export default function ExistingConnections(props: Props) {
2121
const [deleteModalOpen, setDeleteModalOpen] = useState(false);
2222
const [connectionNameToDelete, setConnectionNameToDelete] = useState("");
2323
const [isLocalDolt, setIsLocalDolt] = useState(false);
24-
2524
const onDeleteClicked = (name: string, local: boolean) => {
2625
setConnectionNameToDelete(name);
2726
setDeleteModalOpen(true);
2827
setIsLocalDolt(local);
2928
};
3029

31-
// TODO: need to reveal the remove folder error
32-
const removeLocalDoltFolder = async (name: string) => {
30+
const removeLocalDoltFolder = () => {
3331
try {
34-
const result = await window.ipc.invoke("remove-dolt-connection", name);
35-
console.log(result);
32+
window.ipc.invoke("remove-dolt-connection", connectionNameToDelete);
3633
} catch (error) {
3734
console.error("Failed to remove local Dolt server:", error);
35+
return new Error(`Failed to remove local Dolt server: ${error}`);
3836
}
37+
return undefined;
3938
};
4039

4140
const router = useRouter();
@@ -99,9 +98,10 @@ export default function ExistingConnections(props: Props) {
9998
variables: { name: connectionNameToDelete },
10099
refetchQueries: [{ query: StoredConnectionsDocument }],
101100
}}
102-
callback={
101+
callback={isLocalDolt ? () => removeLocalDoltFolder() : undefined}
102+
alertMessage={
103103
isLocalDolt
104-
? async () => removeLocalDoltFolder(connectionNameToDelete)
104+
? "This action will permanently delete the local Dolt server and all associated data stored within it. This cannot be undone."
105105
: undefined
106106
}
107107
/>

web/renderer/components/pageComponents/ConnectionsPage/ExistingConnections/useAddConnection.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {
22
DatabaseConnectionFragment,
33
useAddDatabaseConnectionMutation,
4+
useDoltServerStatusQuery,
45
} from "@gen/graphql-types";
56
import { ApolloErrorType } from "@lib/errors/types";
67
import { maybeDatabase } from "@lib/urls";
@@ -10,14 +11,17 @@ type ReturnType = {
1011
onAdd: () => Promise<void>;
1112
err: ApolloErrorType;
1213
loading: boolean;
14+
doltServerIsActive?: boolean;
1315
};
1416

1517
export default function useAddConnection(
1618
conn: DatabaseConnectionFragment,
1719
): ReturnType {
1820
const router = useRouter();
1921
const [addDb, res] = useAddDatabaseConnectionMutation();
20-
22+
const doltServerStatus = useDoltServerStatusQuery({
23+
variables: conn,
24+
});
2125
const onAdd = async () => {
2226
try {
2327
const db = await addDb({ variables: conn });
@@ -34,5 +38,10 @@ export default function useAddConnection(
3438
}
3539
};
3640

37-
return { onAdd, err: res.error, loading: res.loading };
41+
return {
42+
onAdd,
43+
err: res.error,
44+
loading: res.loading,
45+
doltServerIsActive: !!doltServerStatus.data?.doltServerStatus.active,
46+
};
3847
}

0 commit comments

Comments
 (0)