From 3519003a77a39b1639bc9ef9144bba01ecc77f05 Mon Sep 17 00:00:00 2001 From: CN-JS-HuiBai Date: Fri, 10 Apr 2026 23:44:46 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E9=A1=B9=E7=9B=AE=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E8=87=AA=E6=A3=80=E6=B5=81=E7=A8=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- server/db-schema-check.js | 10 ++++++++-- server/index.js | 11 ++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/server/db-schema-check.js b/server/db-schema-check.js index 0b76471..1c00d3b 100644 --- a/server/db-schema-check.js +++ b/server/db-schema-check.js @@ -235,15 +235,21 @@ async function ensureTable(tableName, tableSchema) { } async function checkAndFixDatabase() { - const envPath = path.join(__dirname, '..', '.env'); - if (!fs.existsSync(envPath)) return; + // Check for DB host to see if we have configuration (could be in .env or environment) + if (!process.env.MYSQL_HOST && !fs.existsSync(path.join(__dirname, '..', '.env'))) { + console.log('[Database Integrity] No configuration found, skipping check.'); + return false; + } try { for (const [tableName, tableSchema] of Object.entries(SCHEMA)) { await ensureTable(tableName, tableSchema); } + return true; } catch (err) { console.error('[Database Integrity] Startup schema check failed:', err.message); + // Rethrow to allow caller to decide if this is fatal + throw err; } } diff --git a/server/index.js b/server/index.js index 6279052..dcbdc6a 100644 --- a/server/index.js +++ b/server/index.js @@ -1418,7 +1418,16 @@ async function start() { try { console.log('🔧 Initializing services...'); // Ensure DB is ready before starting anything else - await checkAndFixDatabase(); + try { + const dbFixed = await checkAndFixDatabase(); + if (dbFixed) { + // Re-run the local checkDb() to update isDbInitialized and other status variables + await checkDb(); + } + } catch (dbErr) { + console.warn('⚠️ Database initialization check encountered an error:', dbErr.message); + // We don't necessarily crash here, but users will see the setup screen if isDbInitialized is false + } // Start services latencyService.start();