Skip to content

Commit 0d3fda5

Browse files
Define databaseUrl under userDirectoryPath (#1537)
Adds a default databaseUrl pointing to userDirectoryPath which is under basePath, which resolves the linked issue as this path is writable as we do a writability check for basePath's parent. Resolves #1532 ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-1537-Define-databaseUrl-under-userDirectoryPath-2ea6d73d36508178b476f31d4ee93a2c) by [Unito](https://www.unito.io)
1 parent 3432ce9 commit 0d3fda5

File tree

2 files changed

+69
-8
lines changed

2 files changed

+69
-8
lines changed

src/main-process/comfyServer.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,20 @@ export class ComfyServer implements HasTelemetry {
8181
return `http://${this.serverArgs.listen}:${this.serverArgs.port}`;
8282
}
8383

84+
private resolveDatabasePath(userDirectoryPath: string): string {
85+
if (process.platform === 'win32') {
86+
return path.win32.resolve(userDirectoryPath, 'comfyui.db');
87+
}
88+
89+
return path.resolve(userDirectoryPath, 'comfyui.db');
90+
}
91+
92+
private get databaseUrl(): string {
93+
const dbPath = this.resolveDatabasePath(this.userDirectoryPath);
94+
const normalizedDbPath = process.platform === 'win32' ? dbPath.replaceAll('\\', '/') : dbPath;
95+
return `sqlite:///${normalizedDbPath}`;
96+
}
97+
8498
/**
8599
* Core arguments to pass to the ComfyUI server to ensure electron app
86100
* works as expected.
@@ -92,6 +106,7 @@ export class ComfyServer implements HasTelemetry {
92106
'output-directory': this.outputDirectoryPath,
93107
'front-end-root': this.webRootPath,
94108
'base-directory': this.basePath,
109+
'database-url': this.databaseUrl,
95110
'extra-model-paths-config': ComfyServerConfig.configPath,
96111
'log-stdout': '',
97112
};

tests/unit/main-process/comfyServer.test.ts

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import log from 'electron-log/main';
33
import { ChildProcess } from 'node:child_process';
44
import { EventEmitter } from 'node:events';
55
import path from 'node:path';
6-
import { test as baseTest, describe, expect, vi } from 'vitest';
6+
import { afterEach, test as baseTest, describe, expect, vi } from 'vitest';
77
import waitOn from 'wait-on';
88

99
import { ServerArgs } from '@/constants';
@@ -64,13 +64,7 @@ const test = baseTest.extend<TestContext>({
6464
transports: { file: { transforms: [] } },
6565
} as unknown as MainLogger & { default: MainLogger });
6666

67-
const server = new ComfyServer(
68-
basePath,
69-
mockServerArgs,
70-
mockVirtualEnvironment as any,
71-
mockAppWindow as any,
72-
mockTelemetry as any
73-
);
67+
const server = new ComfyServer(basePath, mockServerArgs, mockVirtualEnvironment, mockAppWindow, mockTelemetry);
7468
await use(server);
7569
},
7670
runningServer: async ({ server, mockProcess }, use) => {
@@ -80,6 +74,10 @@ const test = baseTest.extend<TestContext>({
8074
},
8175
});
8276

77+
afterEach(() => {
78+
vi.unstubAllGlobals();
79+
});
80+
8381
describe('buildLaunchArgs', () => {
8482
test('should convert basic arguments correctly', () => {
8583
const args = {
@@ -145,15 +143,63 @@ describe('baseUrl', () => {
145143
describe('coreLaunchArgs', () => {
146144
test('should return the correct core launch arguments', ({ server }) => {
147145
const args = server.coreLaunchArgs;
146+
const databaseUrl = args['database-url'];
147+
const normalizedUserDir = server.userDirectoryPath.replaceAll('\\', '/');
148148
expect(args).toEqual({
149149
'user-directory': server.userDirectoryPath,
150150
'input-directory': server.inputDirectoryPath,
151151
'output-directory': server.outputDirectoryPath,
152152
'front-end-root': server.webRootPath,
153153
'base-directory': basePath,
154+
'database-url': databaseUrl,
154155
'extra-model-paths-config': expect.any(String),
155156
'log-stdout': '',
156157
});
158+
expect(databaseUrl).toMatch(/^sqlite:\/\//);
159+
expect(databaseUrl).toContain(normalizedUserDir);
160+
expect(databaseUrl).toMatch(/\/comfyui\.db$/);
161+
});
162+
163+
test('normalizes database URL path separators on win32', ({
164+
mockServerArgs,
165+
mockVirtualEnvironment,
166+
mockAppWindow,
167+
mockTelemetry,
168+
}) => {
169+
vi.stubGlobal('process', { ...process, platform: 'win32' });
170+
const windowsBasePath = String.raw`C:\ComfyUI`;
171+
const windowsServer = new ComfyServer(
172+
windowsBasePath,
173+
mockServerArgs,
174+
mockVirtualEnvironment,
175+
mockAppWindow,
176+
mockTelemetry
177+
);
178+
const databaseUrl = windowsServer.coreLaunchArgs['database-url'];
179+
const expectedPath = path.win32.resolve(windowsBasePath, 'user', 'comfyui.db').replaceAll('\\', '/');
180+
expect(databaseUrl).toBe(`sqlite:///${expectedPath}`);
181+
expect(databaseUrl).not.toContain('\\');
182+
});
183+
});
184+
185+
describe('launchArgs', () => {
186+
test('should allow user override of database-url', ({ mockVirtualEnvironment, mockAppWindow, mockTelemetry }) => {
187+
const customDatabaseUrl = 'sqlite:///override.db';
188+
const serverArgs: ServerArgs = {
189+
listen: 'localhost',
190+
port: '8188',
191+
'database-url': customDatabaseUrl,
192+
};
193+
const server = new ComfyServer(basePath, serverArgs, mockVirtualEnvironment, mockAppWindow, mockTelemetry);
194+
const args = server.launchArgs;
195+
const coreDatabaseUrl = server.coreLaunchArgs['database-url'];
196+
expect(coreDatabaseUrl).not.toBe(customDatabaseUrl);
197+
const databaseUrlIndices = args
198+
.map((value, index) => (value === '--database-url' ? index : -1))
199+
.filter((index) => index !== -1);
200+
expect(databaseUrlIndices).toHaveLength(1);
201+
expect(args[databaseUrlIndices[0] + 1]).toBe(customDatabaseUrl);
202+
expect(args).not.toContain(coreDatabaseUrl);
157203
});
158204
});
159205

0 commit comments

Comments
 (0)