'use strict'; const fs = require('fs'); const path = require('path'); const db = require('../src/infra/db'); const TABLES = [ 'online_exam_category_stats_monthly', 'online_exam_category_stats_yearly', ]; async function main() { const schema = fs.readFileSync( path.resolve(__dirname, '../../../deploy/mysql/init/01-schema.sql'), 'utf8', ); for (const table of TABLES) { const start = schema.indexOf(`CREATE TABLE ${table} (`); const marker = ') ENGINE=InnoDB;'; const end = schema.indexOf(marker, start); if (start < 0 || end < 0) throw new Error(`未找到 ${table} DDL`); const statement = schema.slice(start, end + marker.length); await db.execute(statement.replace('CREATE TABLE', 'CREATE TABLE IF NOT EXISTS')); await db.execute( `INSERT INTO sync_table_versions (table_name, version) VALUES (?, 0) ON DUPLICATE KEY UPDATE table_name = VALUES(table_name)`, [table], ); } console.log('monthly exams category stats schema migrated'); } main() .then(() => db.close()) .catch(async (error) => { console.error(error); await db.close(); process.exit(1); });