From 93363a2d2c090c35b390372e4b1c78f9dba0d9ae Mon Sep 17 00:00:00 2001 From: Daniel Holmgren Date: Mon, 4 Dec 2023 17:08:02 -0600 Subject: [PATCH] Pds v2 ensure sqlite ready (#1918) ensure sqlite is ready before making queries --- packages/pds/src/actor-store/index.ts | 9 ++++++++- packages/pds/src/db/db.ts | 9 ++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/packages/pds/src/actor-store/index.ts b/packages/pds/src/actor-store/index.ts index 59ee12e3274..1f64a00bcb2 100644 --- a/packages/pds/src/actor-store/index.ts +++ b/packages/pds/src/actor-store/index.ts @@ -65,7 +65,14 @@ export class ActorStore { throw new InvalidRequestError('Repo not found', 'NotFound') } - return getDb(dbLocation, this.cfg.disableWalAutoCheckpoint) + const db = getDb(dbLocation, this.cfg.disableWalAutoCheckpoint) + try { + await db.ensureReady() + } catch (err) { + db.close() + throw err + } + return db } async read(did: string, fn: ActorStoreReadFn) { diff --git a/packages/pds/src/db/db.ts b/packages/pds/src/db/db.ts index a0e5dac8e77..ffd984dc656 100644 --- a/packages/pds/src/db/db.ts +++ b/packages/pds/src/db/db.ts @@ -15,7 +15,7 @@ import { dbLogger } from '../logger' import { retrySqlite } from './util' const DEFAULT_PRAGMAS = { - strict: 'ON', // @TODO strictness should live on table defs instead + // strict: 'ON', // @TODO strictness should live on table defs instead } export class Database { @@ -50,6 +50,13 @@ export class Database { await sql`PRAGMA journal_mode = WAL`.execute(this.db) } + // run a simple select with retry logic to ensure the db is ready (not in wal recovery mode) + async ensureReady() { + await retrySqlite(async () => { + await sql`select 1`.execute(this.db) + }) + } + async transactionNoRetry( fn: (db: Database) => Promise, ): Promise {