'use strict'; /** * 回填系统默认用户须知(2026-10-14): * - 对 wms_wechat_app 中尚不存在任何用户须知版本的小程序,复制系统默认须知 * (wms_wechat_agreement_version 中 appid='system' 最新一条)作为第一版; * - 幂等:已有任意版本的小程序跳过;无系统默认行则跳过(依赖种子迁移 20261014100000)。 */ const TABLE_NAME = 'wms_wechat_agreement_version'; const SYSTEM_APPID = 'system'; module.exports = { async up(queryInterface) { const { QueryTypes } = queryInterface.sequelize; const systemRows = await queryInterface.sequelize.query( `SELECT content FROM ${TABLE_NAME} WHERE appid = ? ORDER BY id DESC LIMIT 1`, { replacements: [ SYSTEM_APPID ], type: QueryTypes.SELECT } ); const systemContent = systemRows && systemRows.length > 0 ? systemRows[0].content : null; if (!systemContent) { console.log('[backfill] 缺少系统默认须知(appid=system),跳过回填'); return; } const appids = await queryInterface.sequelize.query( `SELECT a.appid FROM wms_wechat_app a WHERE NOT EXISTS ( SELECT 1 FROM ${TABLE_NAME} v WHERE v.appid = a.appid ) ORDER BY a.id ASC`, { type: QueryTypes.SELECT } ); for (const row of appids) { await queryInterface.sequelize.query( `INSERT INTO ${TABLE_NAME} (appid, content, created_at, updated_at) VALUES (?, ?, NOW(), NOW())`, { replacements: [ row.appid, systemContent ] } ); } console.log(`[backfill] 回填系统默认须知 appid 数量: ${appids.length}`); }, async down() { // 回填行与人工版本无法可靠区分,不做逆向删除 }, };