ddcfe3334e
部署到群晖 / deploy (push) Successful in 44s
- Phase 1: 添加纪念日合并人物创建流程(方案 B) - Phase 2: 农历提醒按 lunarMonth/Day 计算每年公历 - Phase 3: 人员数据同步到后端(新增 /api/person) - Phase 4: 新设备启动从云端恢复数据 - Phase 5: 工具函数收敛 utils/format.js - Phase 6: 同步失败入队 + 启动重试 - Phase 7: 闰月生日完整支持(含 isLeapMonth + UI 警示) - 修复 lunarInfo 数据表错位(替换为权威源 jjonline/calendar.js) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
82 lines
2.6 KiB
JavaScript
82 lines
2.6 KiB
JavaScript
const Database = require('better-sqlite3')
|
|
const path = require('path')
|
|
const fs = require('fs')
|
|
|
|
const dbPath = process.env.DB_PATH || path.join(__dirname, '..', 'data', 'birthday.db')
|
|
|
|
// 确保数据目录存在
|
|
const dir = path.dirname(dbPath)
|
|
if (!fs.existsSync(dir)) fs.mkdirSync(dir, { recursive: true })
|
|
|
|
const db = new Database(dbPath)
|
|
db.pragma('journal_mode = WAL')
|
|
|
|
// 纪念日表:兼容原云数据库 anniversaries 集合字段
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS anniversaries (
|
|
id TEXT PRIMARY KEY,
|
|
openid TEXT NOT NULL,
|
|
personId TEXT,
|
|
personName TEXT,
|
|
type TEXT,
|
|
customTypeName TEXT,
|
|
isLunar INTEGER DEFAULT 0,
|
|
solarYear INTEGER,
|
|
solarMonth INTEGER,
|
|
solarDay INTEGER,
|
|
lunarYear INTEGER,
|
|
lunarMonth INTEGER,
|
|
lunarDay INTEGER,
|
|
isLeapMonth INTEGER DEFAULT 0,
|
|
importance TEXT,
|
|
remindEnabled INTEGER DEFAULT 0,
|
|
remindDays INTEGER DEFAULT 0,
|
|
remark TEXT,
|
|
createTime INTEGER,
|
|
updateTime INTEGER
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_anniv_openid ON anniversaries(openid);
|
|
CREATE INDEX IF NOT EXISTS idx_anniv_remind ON anniversaries(remindEnabled);
|
|
|
|
CREATE TABLE IF NOT EXISTS persons (
|
|
id TEXT PRIMARY KEY,
|
|
openid TEXT NOT NULL,
|
|
name TEXT NOT NULL,
|
|
nickname TEXT,
|
|
avatar TEXT,
|
|
remark TEXT,
|
|
createTime INTEGER,
|
|
updateTime INTEGER
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_person_openid ON persons(openid);
|
|
|
|
CREATE TABLE IF NOT EXISTS remind_logs (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
anniversaryId TEXT,
|
|
personName TEXT,
|
|
typeName TEXT,
|
|
daysUntil INTEGER,
|
|
sendDate INTEGER,
|
|
status TEXT,
|
|
error TEXT
|
|
);
|
|
CREATE INDEX IF NOT EXISTS idx_log_anniv ON remind_logs(anniversaryId);
|
|
CREATE INDEX IF NOT EXISTS idx_log_date ON remind_logs(sendDate);
|
|
`)
|
|
|
|
// 旧库迁移:CREATE TABLE IF NOT EXISTS 不会给已存在的表加列,需要手动 ALTER
|
|
// 失败说明列已存在,安全忽略
|
|
function tryAddColumn(table, column, type) {
|
|
try {
|
|
db.prepare(`ALTER TABLE ${table} ADD COLUMN ${column} ${type}`).run()
|
|
} catch (e) {
|
|
if (!/duplicate column/i.test(e.message)) throw e
|
|
}
|
|
}
|
|
tryAddColumn('anniversaries', 'lunarYear', 'INTEGER')
|
|
tryAddColumn('anniversaries', 'lunarMonth', 'INTEGER')
|
|
tryAddColumn('anniversaries', 'lunarDay', 'INTEGER')
|
|
tryAddColumn('anniversaries', 'isLeapMonth', 'INTEGER DEFAULT 0')
|
|
|
|
module.exports = db
|