fix(db): open the move's SQLite source through openDatabase, keep the drop failure as the cause

ESLint refused both: better-sqlite3 is imported only in src/db/connection.ts and its named neighbours, and an error thrown in a catch carries what that catch caught.
This commit is contained in:
Maurice
2026-11-17 14:43:02 +01:00
parent 900f2bfc30
commit 5316362e9e
2 changed files with 10 additions and 6 deletions
@@ -256,15 +256,15 @@ async function copyAll(
* empty before it, so everything in its schema is the move's). When even that
* fails, the error says so, since the database then needs dropping by hand.
*/
async function emptyAgain(db: Kysely<object>, cause: unknown): Promise<void> {
async function emptyAgain(db: Kysely<object>, failure: unknown): Promise<void> {
try {
await run(db, CLEAR_SCHEMA_SQL);
} catch (err) {
throw new Error(
`The move failed (${cause instanceof Error ? cause.message : String(cause)}), and the schema it had created ` +
`The move failed (${failure instanceof Error ? failure.message : String(failure)}), and the schema it had created ` +
`could not be dropped (${err instanceof Error ? err.message : String(err)}). Drop the Postgres database and ` +
'create it again before the next attempt.',
{ cause },
{ cause: err },
);
}
}
+7 -3
View File
@@ -1,4 +1,5 @@
import Database from 'better-sqlite3';
import { openDatabase } from '../../connection';
import fs from 'node:fs';
/**
@@ -46,9 +47,12 @@ function quoteSqliteIdentifier(name: string): string {
return `"${name.replaceAll('"', '""')}"`;
}
/** A handle as `openDatabase` gives it: the driver itself stays behind src/db/connection.ts. */
type SqliteHandle = ReturnType<typeof openDatabase>;
export class SqliteSource {
private constructor(
private readonly db: Database.Database,
private readonly db: SqliteHandle,
readonly file: string,
) {}
@@ -58,7 +62,7 @@ export class SqliteSource {
*/
static open(file: string): SqliteSource {
if (!fs.existsSync(file)) throw new Error(`There is no SQLite file at ${file}.`);
const db = new Database(file, { readonly: true, fileMustExist: true });
const db = openDatabase(file, { readonly: true, fileMustExist: true });
try {
db.defaultSafeIntegers(true);
db.exec('BEGIN');