WMS_MAIN/database/migrations/20260830150000-add-store-id-index-to-wms-inbox-group.js

60 lines
2.2 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

'use strict';
/**
* 为站内信群表补充货主维度查询索引。
*
* 背景:inbox.group.quickList 接口支持双维度过滤:
* - store_id(货主维度)→ wms_inbox_group.store_id 列
* - warehouse_id(仓库维度)→ wms_inbox_group.warehouse_id 列
* 两者可单独或同时(AND)过滤。store_id 列此前无索引,
* 单独按货主过滤或与仓库 AND 过滤时会走全表扫描。
*
* wms_inbox_group:
* - idx_store_id: (store_id)
* 用于按货主维度过滤群列表。warehouse_id 维度已由唯一索引
* uk_warehouse_group_id(warehouse_id, group_id) 的前导列覆盖,无需新增仓库单列索引;
* 双条件 AND 场景由 idx_store_id 与 uk_warehouse_group_id 组合命中,
* 群表数据量小,单列索引已足够,不引入复合索引以控制索引维护成本。
*/
const TABLE_NAME = 'wms_inbox_group';
const INDEX_NAME = 'idx_store_id';
async function indexExists(queryInterface, tableName, indexName) {
const indexes = await queryInterface.showIndex(tableName);
return (indexes || []).some(i => (typeof i === 'string' ? i : i.name) === indexName);
}
async function addIndexIfNotExists(queryInterface, tableName, indexName, options = {}) {
if (await indexExists(queryInterface, tableName, indexName)) {
console.log(`[idx] ${tableName}.${indexName} 已存在,跳过`);
return;
}
await queryInterface.addIndex(tableName, options.fields, {
name: indexName,
unique: !!options.unique,
});
console.log(`[idx] ${tableName}.${indexName} 已创建`);
}
async function dropIndexIfExists(queryInterface, tableName, indexName) {
if (await indexExists(queryInterface, tableName, indexName)) {
await queryInterface.removeIndex(tableName, indexName);
console.log(`[idx] ${tableName}.${indexName} 已删除`);
}
}
module.exports = {
// eslint-disable-next-line no-unused-vars
async up(queryInterface, Sequelize) {
await addIndexIfNotExists(queryInterface, TABLE_NAME, INDEX_NAME, {
fields: [ 'store_id' ],
});
},
// eslint-disable-next-line no-unused-vars
async down(queryInterface, Sequelize) {
await dropIndexIfExists(queryInterface, TABLE_NAME, INDEX_NAME);
},
};