Files
baby-tracker/scripts/migrate_v14.js
T
2026-09-25 22:08:37 +08:00

71 lines
3.1 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
const { pool } = require('../server/db');
async function migrate() {
const conn = await pool.getConnection();
try {
console.log('Running V1.3 & V1.4 DB migration...');
await conn.query(`
CREATE TABLE IF NOT EXISTS \`family_memo\` (
\`id\` BIGINT AUTO_INCREMENT PRIMARY KEY COMMENT '自增主键',
\`memo_id\` VARCHAR(64) NOT NULL UNIQUE COMMENT '备忘唯一业务ID',
\`family_id\` VARCHAR(64) NOT NULL DEFAULT 'family-001' COMMENT '所属家庭ID',
\`content\` VARCHAR(255) NOT NULL COMMENT '待办备忘内容',
\`due_period\` VARCHAR(30) DEFAULT 'today' COMMENT '推荐时段',
\`is_completed\` TINYINT(1) DEFAULT 0 COMMENT '是否已完成',
\`completed_at\` DATETIME NULL COMMENT '完成时刻',
\`created_by\` VARCHAR(50) DEFAULT '宝妈' COMMENT '创建人称谓',
\`created_at\` DATETIME DEFAULT CURRENT_TIMESTAMP,
\`updated_at\` DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX \`idx_memo_family\` (\`family_id\`, \`is_completed\`),
INDEX \`idx_memo_created\` (\`created_at\`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='家庭日常备忘待办清单表';
`);
console.log('✅ Table family_memo ensured.');
try {
await conn.query("ALTER TABLE `family_member` ADD COLUMN `preferences` JSON NULL COMMENT '角色专属行为偏好'");
console.log('✅ Column preferences added to family_member.');
} catch (e) {
if (e.code === 'ER_DUP_FIELDNAME') {
console.log('ℹ️ Column preferences already exists in family_member.');
} else {
console.warn('family_member column warning:', e.message);
}
}
try {
await conn.query("ALTER TABLE `baby_profile` ADD COLUMN `default_config` JSON NULL COMMENT '宝宝默认行为配置'");
console.log('✅ Column default_config added to baby_profile.');
} catch (e) {
if (e.code === 'ER_DUP_FIELDNAME') {
console.log('ℹ️ Column default_config already exists in baby_profile.');
} else {
console.warn('baby_profile column warning:', e.message);
}
}
// Insert sample initial memo if none exists
const [memos] = await conn.query("SELECT COUNT(*) as count FROM `family_memo` WHERE `family_id` = 'family-001'");
if (memos[0].count === 0) {
await conn.query(`
INSERT INTO \`family_memo\` (\`memo_id\`, \`family_id\`, \`content\`, \`due_period\`, \`is_completed\`, \`created_by\`)
VALUES
('memo-001', 'family-001', '下午3点:准备45℃温水洗澡', 'afternoon', 0, '宝妈'),
('memo-002', 'family-001', '睡前:喂服维生素 D3 滴剂', 'evening', 0, '宝爸'),
('memo-003', 'family-001', '消毒所有奶瓶及吸奶器配件', 'morning', 1, '奶奶')
`);
console.log('✅ Seed memos initialized for family-001.');
}
console.log('Migration finished successfully!');
} finally {
conn.release();
process.exit(0);
}
}
migrate().catch(err => {
console.error('Migration failed:', err);
process.exit(1);
});