From b4967f4c3594564696797d7276f15cd0ab380e4f Mon Sep 17 00:00:00 2001 From: XuJiacheng Date: Wed, 4 Mar 2026 09:58:53 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=90=AF=E7=94=A8=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E5=88=9D=E5=A7=8B=E5=8C=96=E4=B8=8E=E5=88=86=E5=8C=BA?= =?UTF-8?q?=E7=BB=B4=E6=8A=A4=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E9=85=8D=E7=BD=AE=E4=BB=A5=E6=94=AF=E6=8C=81=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E6=8E=A7=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 5 ++++- src/config/config.example.js | 3 +++ src/db/databaseManager.js | 21 +++++++++++++-------- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index 442bb71..89d1f3d 100644 --- a/.env.example +++ b/.env.example @@ -69,4 +69,7 @@ PROCESSOR_BATCH_TIMEOUT=5000 # 日志配置 LOG_LEVEL=info -LOG_FORMAT=json \ No newline at end of file +LOG_FORMAT=json + + +DB_INIT_AND_PARTITION_ENABLED=false \ No newline at end of file diff --git a/src/config/config.example.js b/src/config/config.example.js index 67ee750..2864268 100644 --- a/src/config/config.example.js +++ b/src/config/config.example.js @@ -53,6 +53,9 @@ export default { retryAttempts: 3, // 重试次数 retryDelay: 1000, // 重试延迟 + // 是否启用数据库初始化与分区维护(若为 false,跳过建表、分区预创建、定时分区检查) + initAndPartitionEnabled: (env.DB_INIT_AND_PARTITION_ENABLED ?? 'true') === 'true', + // 分区维护(方案1):启动时预创建 + 周期维护 partitionMaintenance: { enabled: true, diff --git a/src/db/databaseManager.js b/src/db/databaseManager.js index d02c201..d58980e 100644 --- a/src/db/databaseManager.js +++ b/src/db/databaseManager.js @@ -37,15 +37,20 @@ class DatabaseManager { client.release(); console.log('数据库连接池创建成功'); - // 初始化表结构 - await this.initTables(); + // 根据配置决定是否执行初始化与分区维护 + if (this.config.initAndPartitionEnabled !== false) { + // 初始化表结构 + await this.initTables(); - // 分区维护(方案1):启动时预创建 + 定时维护 - await this.ensurePartitionsForRange({ - startDayOffset: -1, - endDayOffset: this.getPartitionFutureDays(), - }); - this.startPartitionMaintenance(); + // 分区维护(方案1):启动时预创建 + 定时维护 + await this.ensurePartitionsForRange({ + startDayOffset: -1, + endDayOffset: this.getPartitionFutureDays(), + }); + this.startPartitionMaintenance(); + } else { + console.log('[db] 已禁用数据库初始化与分区维护(DB_INIT_AND_PARTITION_ENABLED=false),跳过建表、分区预创建、定时维护'); + } } catch (error) { console.error('数据库连接失败:', error); throw error;