From df9e1ca81c33c3410b7ac9fb7889b34d731fe638 Mon Sep 17 00:00:00 2001 From: patdelphi Date: Sat, 23 Aug 2025 00:03:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DKoyeb=20Volume?= =?UTF-8?q?=E6=8C=82=E8=BD=BD=E8=B7=AF=E5=BE=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 根据Koyeb实际挂载信息(/workspace/data)调整数据库路径配置 - 添加Koyeb环境检测逻辑,优先使用/workspace/data路径 - 更新.koyeb/koyeb.yaml中的mount_path和DB_PATH配置 - 增强日志输出,显示Koyeb环境状态和工作目录 - 解决数据库持久化路径不匹配导致的数据丢失问题 - 确保Volume正确挂载到/workspace/data目录 --- .koyeb/koyeb.yaml | 4 ++-- server/database/index.cjs | 21 +++++++++++++++++---- 2 files changed, 19 insertions(+), 6 deletions(-) diff --git a/.koyeb/koyeb.yaml b/.koyeb/koyeb.yaml index 976603b..07f684a 100644 --- a/.koyeb/koyeb.yaml +++ b/.koyeb/koyeb.yaml @@ -18,7 +18,7 @@ services: - key: PORT value: "8000" - key: DB_PATH - value: "/app/data/numerology.db" + value: "/workspace/data/numerology.db" - key: JWT_SECRET value: "xVtKLcdGpYdtoEjEBE9hFTJgBCJrEIu9AjXtAJMtwTU=" - key: JWT_EXPIRES_IN @@ -27,7 +27,7 @@ services: value: "info" volumes: - name: sqlite-data - mount_path: /app/data + mount_path: /workspace/data size: 1GB health_check: http: diff --git a/server/database/index.cjs b/server/database/index.cjs index d22d181..2f015a5 100644 --- a/server/database/index.cjs +++ b/server/database/index.cjs @@ -5,16 +5,29 @@ const fs = require('fs'); class DatabaseManager { constructor() { this.db = null; - // 生产环境使用持久化存储路径,开发环境使用本地路径 - this.dbPath = process.env.NODE_ENV === 'production' - ? '/app/data/numerology.db' - : path.join(__dirname, '../../numerology.db'); + + // 检测Koyeb环境并使用正确的挂载路径 + const isKoyeb = process.env.KOYEB_APP_NAME || process.env.KOYEB_SERVICE_NAME || fs.existsSync('/workspace/data'); + + if (isKoyeb) { + // Koyeb环境:Volume挂载到/workspace/data + this.dbPath = '/workspace/data/numerology.db'; + } else if (process.env.NODE_ENV === 'production') { + // 其他生产环境:使用/app/data + this.dbPath = '/app/data/numerology.db'; + } else { + // 开发环境:使用本地路径 + this.dbPath = path.join(__dirname, '../../numerology.db'); + } + this.schemaPath = path.join(__dirname, 'schema.sql'); // 输出数据库配置信息 console.log(`🗄️ 数据库路径: ${this.dbPath}`); console.log(`🌍 运行环境: ${process.env.NODE_ENV || 'development'}`); console.log(`📊 数据库文件: ${path.basename(this.dbPath)}`); + console.log(`🏢 Koyeb环境: ${isKoyeb ? 'Yes' : 'No'}`); + console.log(`📁 工作目录: ${process.cwd()}`); } // 初始化数据库连接