From a9a7e98be55560699042ab53c607ea35cb028f60 Mon Sep 17 00:00:00 2001 From: shats Date: Sun, 26 Apr 2026 02:07:35 +0700 Subject: [PATCH] Enhance SQLite database initialization in sqliteDb.js - Added checks to ensure the SQLite database file exists before opening, improving resilience during container startup. - Implemented error handling for file accessibility, providing clearer error messages if the database file is not readable or writable. Made-with: Cursor --- backend/db/sqliteDb.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/backend/db/sqliteDb.js b/backend/db/sqliteDb.js index 81d4ef8..f5be2f6 100644 --- a/backend/db/sqliteDb.js +++ b/backend/db/sqliteDb.js @@ -20,6 +20,21 @@ function getDbPath() { if (!fs.existsSync(dir)) { fs.mkdirSync(dir, { recursive: true }); } + + // Ensure the file exists before better-sqlite3 opens it. + // This makes container startup resilient when SQLITE_PATH points to + // a persistent volume and database file is not created yet. + if (!fs.existsSync(p)) { + fs.closeSync(fs.openSync(p, 'a')); + } + + try { + fs.accessSync(p, fs.constants.R_OK | fs.constants.W_OK); + } catch (err) { + const details = err && err.message ? err.message : String(err); + throw new Error(`SQLite file is not readable/writable at "${p}": ${details}`); + } + return p; }