优化数据库自检

This commit is contained in:
CN-JS-HuiBai
2026-04-11 00:10:36 +08:00
parent 1826d4e34a
commit 14796231f1
2 changed files with 109 additions and 122 deletions

View File

@@ -305,15 +305,26 @@ function getCookie(req, name) {
return matches ? decodeURIComponent(matches[1]) : undefined;
}
/**
* Database Initialization Check
*/
let isDbInitialized = false;
async function checkDb() {
try {
const fs = require('fs');
if (!fs.existsSync(path.join(__dirname, '..', '.env'))) {
// In Docker or high-level environments, .env might not exist but process.env is set
const hasConfig = process.env.MYSQL_HOST || fs.existsSync(path.join(__dirname, '..', '.env'));
if (!hasConfig) {
isDbInitialized = false;
return;
}
// Check for essential tables
const [rows] = await db.query("SHOW TABLES LIKE 'prometheus_sources'");
isDbInitialized = rows.length > 0;
const [rows2] = await db.query("SHOW TABLES LIKE 'site_settings'");
isDbInitialized = rows.length > 0 && rows2.length > 0;
} catch (err) {
isDbInitialized = false;
}