fix(tests): use in-memory SQLite per worker in test mode

vitest pool:forks spawns parallel worker processes that all called
initDb() on the same data/travel.db, causing SQLite "database is locked"
and "duplicate column name" races.

When NODE_ENV=test each fork now gets an isolated :memory: DB so migrations
run independently with no file contention.
This commit is contained in:
jubnl
2026-05-25 20:52:57 +02:00
parent 515d1084da
commit 3f760338c8
+13 -5
View File
@@ -6,12 +6,20 @@ import { runMigrations } from './migrations';
import { runSeeds } from './seeds'; import { runSeeds } from './seeds';
import { Place, Tag } from '../types'; import { Place, Tag } from '../types';
const dataDir = path.join(__dirname, '../../data'); // In test mode each vitest worker gets an isolated in-memory DB so that
if (!fs.existsSync(dataDir)) { // parallel forks can't race on the same file or share migration state.
fs.mkdirSync(dataDir, { recursive: true }); const isTest = process.env.NODE_ENV === 'test';
}
const dbPath = path.join(dataDir, 'travel.db'); let dbPath: string;
if (isTest) {
dbPath = ':memory:';
} else {
const dataDir = path.join(__dirname, '../../data');
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir, { recursive: true });
}
dbPath = path.join(dataDir, 'travel.db');
}
let _db: Database.Database | null = null; let _db: Database.Database | null = null;