'use strict'; const fs = require('fs'); const path = require('path'); /** * Baseline migration - 2026-05-04 * 以 SequelizeMeta.sql 为基准,定义当前数据库完整结构。 * 生产环境:请将本文件名插入 SequelizeMeta 表,标记为已执行。 * 新环境:直接运行 db:migrate,本文件会创建所有表结构。 */ module.exports = { up: async queryInterface => { const sqlRaw = fs.readFileSync( path.join(__dirname, '../../SequelizeMeta.sql'), 'utf8' ); // 预处理:去掉 DROP TABLE,改为 CREATE TABLE IF NOT EXISTS,跳过 SequelizeMeta 表 const sql = sqlRaw .replace(/SET NAMES utf8mb4;\r?\n/g, '') .replace(/DROP TABLE IF EXISTS `SequelizeMeta`;\r?\nCREATE TABLE[\s\S]*?ROW_FORMAT = Dynamic;\r?\n/m, '') .replace(/DROP TABLE IF EXISTS `[^`]+`;\r?\n/g, '') .replace(/CREATE TABLE `/g, 'CREATE TABLE IF NOT EXISTS `') .replace(/SET FOREIGN_KEY_CHECKS = 1;?/g, ''); // 按分号+换行切割,逐条执行 const statements = sql .split(/;\s*\r?\n/) .map(s => s.trim()) .filter(s => s && !s.startsWith('--') && !s.startsWith('/*') && s !== 'SET FOREIGN_KEY_CHECKS = 0'); // 先禁用外键检查 await queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 0'); for (const stmt of statements) { if (stmt) { await queryInterface.sequelize.query(stmt + ';'); } } await queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 1'); }, down: async queryInterface => { await queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 0'); // 按外键依赖逆序删除 const tables = [ 'wms_account_platform_active', 'wms_employee_role', 'wms_wave_batch', 'wms_picking', 'wms_picking_strategy', 'wms_operation_log', 'wms_notification_send_log', 'wms_notification_webhook', 'wms_stock_transaction', 'wms_storage', 'wms_receiving_detail', 'wms_receiving', 'wms_order_express_print_data', 'wms_order_express', 'wms_entry_items', 'wms_entry_order', 'wms_file_reference', 'wms_file', 'wms_i18n_translations', 'wms_i18n_keys', 'wms_express_print_record', 'wms_print_log', 'wms_express_interface', 'wms_express_setting', 'wms_embargo_policies', 'wms_prohibited_type', 'wms_location_space', 'wms_location_zone', 'wms_location_type', 'wms_print_station_express', 'wms_print_stations', 'wms_print_template', 'wms_platform_print_config', 'wms_print_record', 'wms_print_computer', 'wms_print_items', 'wms_print_log', 'wms_system_log', 'wms_stock_unit', 'wms_stock_warehouse', 'wms_stock_store', 'wms_api_app', 'wms_interface_type', 'wms_account_store_employee', 'wms_role', 'wms_permission', 'wms_platfrom_setting', 'wms_fast_where', 'wms_qimen_log', 'wms_qimen_apps', 'wms_express_product_type', 'wms_express', 'wms_account_login_log', 'wms_store_warehouse', 'wms_store', 'wms_account', ]; for (const table of tables) { await queryInterface.sequelize.query(`DROP TABLE IF EXISTS \`${table}\``); } await queryInterface.sequelize.query('SET FOREIGN_KEY_CHECKS = 1'); }, };