feat: 周期账单 — 自动生成定期交易(房租/订阅/工资)
This commit is contained in:
@@ -243,6 +243,40 @@ async function runMigrations(conn: mysql.Connection) {
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
||||
`)
|
||||
}
|
||||
|
||||
// 周期账单模板表
|
||||
const hasRecurring = await tableExists(conn, 'recurring_templates')
|
||||
if (!hasRecurring) {
|
||||
console.log('[DB] Migrating: creating recurring_templates table')
|
||||
await conn.query(`
|
||||
CREATE TABLE IF NOT EXISTS recurring_templates (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
user_id INT NOT NULL,
|
||||
amount INT NOT NULL COMMENT '单位:分',
|
||||
type ENUM('expense', 'income') NOT NULL,
|
||||
category_id INT NOT NULL,
|
||||
note VARCHAR(200) DEFAULT '',
|
||||
frequency ENUM('weekly', 'monthly', 'yearly') NOT NULL DEFAULT 'monthly',
|
||||
next_date DATE NOT NULL COMMENT '下次生成日期',
|
||||
end_date DATE DEFAULT NULL COMMENT '截止日期,NULL=永久',
|
||||
is_active TINYINT(1) DEFAULT 1,
|
||||
group_id INT DEFAULT NULL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
INDEX idx_user (user_id),
|
||||
INDEX idx_next_date (next_date),
|
||||
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
||||
`)
|
||||
}
|
||||
|
||||
// transactions 表添加 recurring_id
|
||||
const hasRecurringId = await columnExists(conn, 'transactions', 'recurring_id')
|
||||
if (!hasRecurringId) {
|
||||
console.log('[DB] Migrating: adding recurring_id to transactions')
|
||||
await conn.query("ALTER TABLE transactions ADD COLUMN recurring_id INT DEFAULT NULL COMMENT '关联的周期模板ID' AFTER group_id")
|
||||
}
|
||||
}
|
||||
|
||||
export async function initDatabase() {
|
||||
|
||||
Reference in New Issue
Block a user