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
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ import { toast } from '../../store/toastStore';
import { useSyncStore } from '../../store/useSyncStore';
import { useUiStore } from '../../store/uiStore';
import { useLokeeHistoryStore } from '../../store/lokeeHistoryStore';
import { resolveHistoryCompare, sortVersionsNewestFirst } from '../../lib/historyCompare';
import {
databaseLocation,
resolveHistoryCompare,
sortVersionsNewestFirst,
} from '../../lib/historyCompare';
import { SQL_ICON_STROKE } from '../sql-editor/sqlIconStyle';

export interface LokeeWeaveViewProps {
Expand All @@ -52,7 +56,7 @@ const EMPTY_DTO: VersionGraphDTO = {

function describe(database: LokeeDatabase | undefined): string | undefined {
if (!database) return undefined;
const where = [database.host, database.database].filter(Boolean).join('/');
const where = databaseLocation(database);
// ` · schema`, not `.schema`: a SQLite path already ends in `.db`, so the
// dotted form rendered as `/tmp/app.db.main` and read like a file extension.
const schema = database.schema ? ` · ${database.schema}` : '';
Expand Down
45 changes: 45 additions & 0 deletions apps/web/src/frontend/lib/historyCompare.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
import { describe, expect, it } from 'vitest';
import {
databaseLocation,
historyVersionLabel,
lokeeDatabaseLabel,
resolveHistoryCompare,
Expand Down Expand Up @@ -141,3 +142,47 @@ describe('compare-only labelling', () => {
).toBe('v7 · before launch — compare only');
});
});

describe('databaseLocation', () => {
// `localhost//tmp/app.db` — a stray double slash in front of a path, from a
// host value that means nothing for a file dialect in the first place.
it.each(['sqlite', 'duckdb'])('drops the host for %s', (dialect) => {
expect(databaseLocation({ dialect, host: 'localhost', database: '/tmp/app.db' })).toBe(
'/tmp/app.db'
);
});

it('keeps host and database for a server dialect', () => {
expect(databaseLocation({ dialect: 'postgres', host: 'localhost', database: 'foxdb' })).toBe(
'localhost/foxdb'
);
});

it('does not leave a dangling separator when one half is missing', () => {
expect(databaseLocation({ dialect: 'postgres', database: 'foxdb' })).toBe('foxdb');
expect(databaseLocation({ dialect: 'postgres', host: 'db.internal' })).toBe('db.internal');
});

it('is empty when there is nothing to show, so callers can fall back', () => {
expect(databaseLocation({ dialect: 'sqlite' })).toBe('');
});
});

describe('lokeeDatabaseLabel reads the same way', () => {
it('names a SQLite file without inventing a host', () => {
expect(
lokeeDatabaseLabel({
id: 'abc12345',
dialect: 'sqlite',
host: 'localhost',
database: '/tmp/app.db',
schema: 'main',
versionCount: 3,
})
).toBe('SQLITE · /tmp/app.db · main (3 v)');
});

it('falls back to a short id when the database has no location at all', () => {
expect(lokeeDatabaseLabel({ id: 'abcdef1234', dialect: 'sqlite' })).toContain('abcdef12');
});
});
21 changes: 19 additions & 2 deletions apps/web/src/frontend/lib/historyCompare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,24 @@ export function historyVersionLabel(
return base;
}

/**
* Where a captured database lives, in the words that dialect uses.
*
* A file dialect has no host. Joining one on produced `localhost//tmp/app.db` —
* a stray double slash in front of a path, from a value that means nothing for
* SQLite or DuckDB in the first place.
*/
export function databaseLocation(database: {
dialect: string;
host?: string;
database?: string;
}): string {
const path = (database.database ?? '').trim();
const isFile = database.dialect === 'sqlite' || database.dialect === 'duckdb';
if (isFile) return path;
return [database.host, path].filter(Boolean).join('/');
}

export function lokeeDatabaseLabel(database: {
id: string;
dialect: string;
Expand All @@ -101,8 +119,7 @@ export function lokeeDatabaseLabel(database: {
schema?: string;
versionCount?: number;
}): string {
const where =
[database.host, database.database].filter(Boolean).join('/') || database.id.slice(0, 8);
const where = databaseLocation(database) || database.id.slice(0, 8);
const schema = database.schema ? ` · ${database.schema}` : '';
const versions =
typeof database.versionCount === 'number' ? ` (${database.versionCount} v)` : '';
Expand Down
Loading