diff --git a/.changeset/validate-distinct-database-paths.md b/.changeset/validate-distinct-database-paths.md new file mode 100644 index 0000000..1983ba7 --- /dev/null +++ b/.changeset/validate-distinct-database-paths.md @@ -0,0 +1,5 @@ +--- +'@goto/cli': patch +--- + +Reject configurations that point the visit and worker databases at the same path. The CLI, services, and visit seeder now enforce the same rule when loading YAML. diff --git a/apps/cli/src/cli.test.ts b/apps/cli/src/cli.test.ts index 1004315..8f743a0 100644 --- a/apps/cli/src/cli.test.ts +++ b/apps/cli/src/cli.test.ts @@ -145,6 +145,12 @@ describe('goto-cli config validate', () => { source: validYaml, environment: { ...env, DUMMY_SECRET: 'too-short' } }, + { + name: 'database settings that resolve to the same path', + source: `${validYaml}database:\n path: ./data/goto.sqlite\n worker_path: ./work/../data/goto.sqlite\n`, + environment: env, + issue: '\n /database/worker_path: Must not resolve to the same path as database.path\n' + }, { name: 'invalid appearance alongside dropped tags', source: `${validYaml} tags: [., ""]\nappearance: false\n`, diff --git a/apps/worker/src/services/startup.test.ts b/apps/worker/src/services/startup.test.ts index 2b930c9..39609cd 100644 --- a/apps/worker/src/services/startup.test.ts +++ b/apps/worker/src/services/startup.test.ts @@ -170,10 +170,10 @@ test('rejects a shared worker/visit file before opening either connection', asyn } expect(failure).toBeInstanceOf(ConfigError); expect(failure).toHaveProperty('issues', [ - { path: '/database/worker_path', message: 'Expected a different file from database.path' } + { path: '/database/worker_path', message: 'Must not resolve to the same path as database.path' } ]); expect(formatWorkerStartupFailure(failure)).toBe( - 'Unable to start worker service\n /database/worker_path: Expected a different file from database.path' + 'Unable to start worker service\n /database/worker_path: Must not resolve to the same path as database.path' ); expect(existsSync(join(directory, 'data'))).toBe(false); }); diff --git a/apps/worker/src/services/startup.ts b/apps/worker/src/services/startup.ts index 82210bf..acfc143 100644 --- a/apps/worker/src/services/startup.ts +++ b/apps/worker/src/services/startup.ts @@ -49,11 +49,6 @@ export async function startWorkerService( if (prepared.warnings.length > 0) logger.warn(formatConfigWarnings(prepared.warnings)); const workerDatabasePath = prepared.configuration.database.worker_path; const applicationDatabasePath = prepared.configuration.database.path; - if (workerDatabasePath === applicationDatabasePath) { - throw new ConfigError([ - { path: '/database/worker_path', message: 'Expected a different file from database.path' } - ]); - } const resources: ShutdownResources = {}; const stop = createShutdown(resources); try { diff --git a/packages/config/src/load.ts b/packages/config/src/load.ts index af44744..411bf7e 100644 --- a/packages/config/src/load.ts +++ b/packages/config/src/load.ts @@ -29,6 +29,14 @@ export async function loadConfigFile( const database = prepared.configuration.database; database.path = resolve(deploymentRoot, database.path); database.worker_path = resolve(deploymentRoot, database.worker_path); + if (database.path === database.worker_path) { + throw new ConfigError([ + { + path: '/database/worker_path', + message: 'Must not resolve to the same path as database.path' + } + ]); + } return prepared; } diff --git a/scripts/seed-visits.js b/scripts/seed-visits.js index bc9f292..c8ade90 100644 --- a/scripts/seed-visits.js +++ b/scripts/seed-visits.js @@ -3,7 +3,7 @@ import { resolve } from 'node:path'; import { setImmediate } from 'node:timers/promises'; import { parseArgs } from 'node:util'; import { createVisitPeriod } from '../packages/analytics/src/period.ts'; -import { loadConfigFile } from '../packages/config/src/index.ts'; +import { ConfigError, formatConfigError, loadConfigFile } from '../packages/config/src/index.ts'; import { openDb, insertVisit } from '../packages/db/src/index.ts'; import { generateVisits } from './seed-visits-data.js'; @@ -77,15 +77,11 @@ function seedPlan(prepared, options) { const total = Object.keys(domains).length * options.visitsPerDomain; if (!Number.isSafeInteger(total)) throw new Error('Total visit count exceeds the safe integer range'); - const databasePath = configuration.database.path; - if (databasePath === configuration.database.worker_path) { - throw new Error('Visits and worker database paths must differ'); - } const asOf = Date.now(); const period = createVisitPeriod(months, asOf); const since = Date.parse(period.from); return { - databasePath, + databasePath: configuration.database.path, domains, since, asOf, @@ -165,7 +161,7 @@ async function appendBatch(db, visits) { if (import.meta.main) { main().catch((error) => { - console.error(error.message); + console.error(error instanceof ConfigError ? formatConfigError(error) : error.message); process.exitCode = 1; }); } diff --git a/scripts/seed-visits.test.js b/scripts/seed-visits.test.js index d86e9e7..4e52a0d 100644 --- a/scripts/seed-visits.test.js +++ b/scripts/seed-visits.test.js @@ -150,6 +150,23 @@ test('preview opens no SQLite file and applying refuses to create a missing data } }); +test('reports the field when the worker database path conflicts with the default visits path', async () => { + const directory = await mkdtemp(join(tmpdir(), 'goto-seed-shared-database-')); + try { + const { configPath } = await deployment(directory); + await writeFile( + configPath, + JSON.stringify({ ...fixture, database: { worker_path: './data/goto.sqlite' } }) + ); + const result = run('--config', configPath); + assert.equal(result.status, 1); + assert.match(result.stderr, /\/database\/worker_path: Must not resolve to the same path/); + assert.equal(result.stdout, ''); + } finally { + await rm(directory, { recursive: true, force: true }); + } +}); + test('preview leaves existing visits untouched; apply appends visits for every domain', async () => { const directory = await mkdtemp(join(tmpdir(), 'goto-seed-apply-')); try {