Files
baby-tracker/migrate_metric.js
T

28 lines
1.3 KiB
JavaScript

const { pool } = require('./server/db');
async function migrate() {
try {
const [cols] = await pool.query('SHOW COLUMNS FROM custom_metric LIKE "default_value"');
if (cols.length === 0) {
await pool.query("ALTER TABLE custom_metric ADD COLUMN default_value VARCHAR(30) DEFAULT '1' COMMENT '默认打卡数值' AFTER unit");
console.log('Added default_value column to custom_metric');
} else {
console.log('default_value column already exists');
}
// Update existing metrics with proper defaults if needed
await pool.query("UPDATE custom_metric SET default_value = '1' WHERE metric_id = 'metric-d3' AND (default_value IS NULL OR default_value = '')");
await pool.query("UPDATE custom_metric SET default_value = '15' WHERE metric_id = 'metric-tummy' AND (default_value IS NULL OR default_value = '')");
await pool.query("UPDATE custom_metric SET default_value = '1' WHERE metric_id = 'metric-massage' AND (default_value IS NULL OR default_value = '')");
await pool.query("UPDATE custom_metric SET default_value = '36.5' WHERE metric_id = 'metric-temp' AND (default_value IS NULL OR default_value = '')");
console.log('Migration finished successfully!');
process.exit(0);
} catch (e) {
console.error('Migration failed:', e);
process.exit(1);
}
}
migrate();