'use strict'; const TABLE_NAME = 'wms_workbench_member'; /** * Migration: create wms_workbench_member(工作台成员/用户登录副表) * - user_id 允许为空:非系统账号用户(如未注册 wms_account 的硬件操作员)以 NULL 记录,名称存 user_name 快照。 * - 软删除(deleted_at),移除成员通过 workbench.removeMember 完成。 * * Idempotent: 表已存在时跳过创建。 */ module.exports = { /** * 执行正向迁移:创建工作台成员副表与索引。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @param {import('sequelize')} Sequelize Sequelize 实例。 * @return {Promise} 无返回值。 */ up: async (queryInterface, Sequelize) => { const tables = await queryInterface.showAllTables(); const exists = tables.some(t => (typeof t === 'string' ? t : (t.tableName || t.name)) === TABLE_NAME); if (exists) return; await queryInterface.createTable(TABLE_NAME, { id: { type: Sequelize.BIGINT(20).UNSIGNED, primaryKey: true, autoIncrement: true, allowNull: false, comment: '主键ID', }, workbench_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: false, comment: '工作台ID,关联 wms_workbench.id', }, user_id: { type: Sequelize.BIGINT(20).UNSIGNED, allowNull: true, comment: '账号ID,关联 wms_account.id;非系统账号用户为 NULL', }, user_name: { type: Sequelize.STRING(128), allowNull: true, comment: '成员名称快照(非系统账号用户显示名)', }, created_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'), comment: '创建时间', }, updated_at: { type: Sequelize.DATE, allowNull: false, defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'), comment: '更新时间', }, deleted_at: { type: Sequelize.DATE, allowNull: true, comment: '软删除时间', }, }); // 按工作台查询成员 await queryInterface.addIndex(TABLE_NAME, [ 'workbench_id' ], { name: 'idx_workbench_id', }); // 按账号反查成员 await queryInterface.addIndex(TABLE_NAME, [ 'user_id' ], { name: 'idx_user_id', }); }, /** * 执行回滚迁移:删除工作台成员副表。 * @param {import('sequelize').QueryInterface} queryInterface 查询接口。 * @return {Promise} 无返回值。 */ down: async queryInterface => { await queryInterface.dropTable(TABLE_NAME); }, };