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;