'use strict'; /** * NVR 唯一键从「全局」改成「按主体」 * * 之前是 `(host, port, username)` 全局唯一 —— 结果是**别的仓库**加过同一个内网地址 * (192.168.8.242:8000 这种各仓库都一样的默认地址)后,本主体就再也加不进去, * 报「已被其他主体添加」。不同主体本来就是不同的仓库,地址撞车是正常现象。 * * 改成 `(subject_id, host, port, username)`:同一主体内仍然不允许重复添加同一台 NVR, * 不同主体之间互不影响。 */ const INDEX_NAME = 'uk_host_port_username'; const SCOPED_INDEX_NAME = 'uk_subject_host_port_username'; /** 判断索引是否存在(重跑/已经手工改过的库不报错) */ async function hasIndex(queryInterface, table, name) { const indexes = await queryInterface.showIndex(table); return indexes.some(item => item.name === name); } module.exports = { async up(queryInterface) { if (await hasIndex(queryInterface, 'nvrs', INDEX_NAME)) { await queryInterface.removeIndex('nvrs', INDEX_NAME); } if (!(await hasIndex(queryInterface, 'nvrs', SCOPED_INDEX_NAME))) { await queryInterface.addIndex('nvrs', [ 'subject_id', 'host', 'port', 'username' ], { name: SCOPED_INDEX_NAME, unique: true, }); } }, async down(queryInterface) { if (await hasIndex(queryInterface, 'nvrs', SCOPED_INDEX_NAME)) { await queryInterface.removeIndex('nvrs', SCOPED_INDEX_NAME); } if (!(await hasIndex(queryInterface, 'nvrs', INDEX_NAME))) { await queryInterface.addIndex('nvrs', [ 'host', 'port', 'username' ], { name: INDEX_NAME, unique: true, }); } }, };