From 903c81d34898e54cdc04877ac4cd660565e3728a Mon Sep 17 00:00:00 2001 From: Paul Fitzpatrick Date: Tue, 4 Apr 2023 15:59:26 -0400 Subject: [PATCH] close db after checkAllegedGristDoc (#482) This closes a file left open during importing, not by the import itself, but by a SQLite integrity check. This was causing imports to fail on Windows (see https://github.com/gristlabs/grist-electron/issues/3) --- app/server/lib/serverUtils.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/server/lib/serverUtils.ts b/app/server/lib/serverUtils.ts index b234fd2a..7c50d747 100644 --- a/app/server/lib/serverUtils.ts +++ b/app/server/lib/serverUtils.ts @@ -136,12 +136,16 @@ export function getDatabaseUrl(options: ConnectionOptions, includeCredentials: b */ export async function checkAllegedGristDoc(docSession: OptDocSession, fname: string) { const db = await SQLiteDB.openDBRaw(fname, OpenMode.OPEN_READONLY); - const integrityCheckResults = await db.all('PRAGMA integrity_check'); - if (integrityCheckResults.length !== 1 || integrityCheckResults[0].integrity_check !== 'ok') { - const uuid = uuidv4(); - log.info('Integrity check failure on import', {uuid, integrityCheckResults, - ...getLogMetaFromDocSession(docSession)}); - throw new Error(`Document failed integrity checks - is it corrupted? Event ID: ${uuid}`); + try { + const integrityCheckResults = await db.all('PRAGMA integrity_check'); + if (integrityCheckResults.length !== 1 || integrityCheckResults[0].integrity_check !== 'ok') { + const uuid = uuidv4(); + log.info('Integrity check failure on import', {uuid, integrityCheckResults, + ...getLogMetaFromDocSession(docSession)}); + throw new Error(`Document failed integrity checks - is it corrupted? Event ID: ${uuid}`); + } + } finally { + await db.close(); } }