From c20c8d1176af9e438fa404c339e10dcb4c348b38 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 22 Jul 2024 21:17:27 +0300 Subject: [PATCH] server: Fix init database not working due to share db --- src/share/sql.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/share/sql.ts b/src/share/sql.ts index 0f39dd77d..87e9cf133 100644 --- a/src/share/sql.ts +++ b/src/share/sql.ts @@ -2,16 +2,21 @@ import Database from "better-sqlite3"; import dataDir from "../services/data_dir.js"; +import sql_init from "../services/sql_init.js"; -const dbConnection = new Database(dataDir.DOCUMENT_PATH, { readonly: true }); +let dbConnection!: Database.Database; -[`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`].forEach(eventType => { - process.on(eventType, () => { - if (dbConnection) { - // closing connection is especially important to fold -wal file into the main DB file - // (see https://sqlite.org/tempfiles.html for details) - dbConnection.close(); - } +sql_init.dbReady.then(() => { + dbConnection = new Database(dataDir.DOCUMENT_PATH, { readonly: true }); + + [`exit`, `SIGINT`, `SIGUSR1`, `SIGUSR2`, `SIGTERM`].forEach(eventType => { + process.on(eventType, () => { + if (dbConnection) { + // closing connection is especially important to fold -wal file into the main DB file + // (see https://sqlite.org/tempfiles.html for details) + dbConnection.close(); + } + }); }); });