-
Notifications
You must be signed in to change notification settings - Fork 57
fix(db): throw instead of silently ignoring initDatabase with differe… #445
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import { initFTS } from './fts.js'; | |
|
|
||
| let db: ReturnType<typeof drizzle> | null = null; | ||
| let sqlite: Database.Database | null = null; | ||
| let initializedWorkspacePath: string | null = null; | ||
|
|
||
| const CREATE_TABLES_SQL = ` | ||
| CREATE TABLE IF NOT EXISTS tasks ( | ||
|
|
@@ -107,12 +108,19 @@ function openDatabaseAt(workspacePath: string): void { | |
| } | ||
|
|
||
| export function initDatabase(workspacePath: string): void { | ||
| if (db) return; | ||
| if (db) { | ||
| if (initializedWorkspacePath !== workspacePath) { | ||
| throw new Error('initDatabase called with different workspace path; use reinitDatabase to switch'); | ||
| } | ||
| return; | ||
| } | ||
| initializedWorkspacePath = workspacePath; | ||
| openDatabaseAt(workspacePath); | ||
| } | ||
|
|
||
| export function reinitDatabase(workspacePath: string): void { | ||
| closeDatabase(); | ||
| initializedWorkspacePath = workspacePath; | ||
| openDatabaseAt(workspacePath); | ||
| } | ||
|
|
||
|
|
@@ -133,4 +141,5 @@ export function closeDatabase(): void { | |
| sqlite?.close(); | ||
| sqlite = null; | ||
| db = null; | ||
| initializedWorkspacePath = null; | ||
|
Comment on lines
141
to
+144
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If try {
sqlite?.close();
} finally {
sqlite = null;
db = null;
initializedWorkspacePath = null;
} |
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; | ||
| import { initDatabase, reinitDatabase, closeDatabase } from '../src/main/db/index.js'; | ||
| import Database from 'better-sqlite3'; | ||
|
|
||
| const mockDbInstance = { | ||
| pragma: vi.fn(), | ||
| exec: vi.fn(), | ||
| close: vi.fn(), | ||
| }; | ||
|
|
||
| vi.mock('better-sqlite3', () => ({ | ||
| default: vi.fn(() => mockDbInstance), | ||
| })); | ||
|
|
||
| describe('initDatabase', () => { | ||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| closeDatabase(); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| closeDatabase(); | ||
| }); | ||
|
|
||
| it('opens database on first call', () => { | ||
| expect(() => initDatabase('/tmp/workspace-a')).not.toThrow(); | ||
| expect(Database).toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('returns early when called with same path', () => { | ||
| initDatabase('/tmp/workspace-a'); | ||
| vi.clearAllMocks(); | ||
| expect(() => initDatabase('/tmp/workspace-a')).not.toThrow(); | ||
| expect(Database).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('throws when called with different workspace path', () => { | ||
| initDatabase('/tmp/workspace-a'); | ||
| expect(() => initDatabase('/tmp/workspace-b')).toThrow( | ||
| 'initDatabase called with different workspace path; use reinitDatabase to switch', | ||
| ); | ||
| }); | ||
|
|
||
| it('allows init after close', () => { | ||
| initDatabase('/tmp/workspace-a'); | ||
| closeDatabase(); | ||
| vi.clearAllMocks(); | ||
| expect(() => initDatabase('/tmp/workspace-b')).not.toThrow(); | ||
| }); | ||
|
|
||
| it('throws when init is called with old path after reinit with new path', () => { | ||
| initDatabase('/tmp/workspace-a'); | ||
| reinitDatabase('/tmp/workspace-b'); | ||
| expect(() => initDatabase('/tmp/workspace-a')).toThrow( | ||
| 'initDatabase called with different workspace path; use reinitDatabase to switch', | ||
| ); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comparing raw strings for file paths can be fragile. Differences in trailing slashes (e.g.,
/path/to/wsvs/path/to/ws/) or relative vs. absolute paths will cause this check to fail even if they point to the same directory. It is recommended to normalize theworkspacePath(e.g., usingpath.resolve()) before storing and comparing it to ensure the check is robust.