WMS_MAIN/database/migrations/20260921150100-add-app-id-to-wms-user-oauth.js

45 lines
1.6 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';
/**
* wms_user_oauth 增加应用映射字段 app_id。
*
* 背景:微信 openid 是「小程序 appid」维度唯一,同一用户在不同小程序下
* openid 不同。为使用户授权记录能定位到对应小程序应用(wms_wechat_app),
* 以便取该应用自己的 access_token 处理,新增 app_id 关联字段。
*
* 索引调整:
* - 删除 uk_provider_openid(provider + openid),
* 改为 uk_app_provider_openid(app_id + provider + openid)。
* - 注意:MySQL 唯一索引对 NULL 不生效,app_id 为 NULL 的记录(非小程序
* 厂商场景)不参与唯一约束,写入时需业务侧先查后插保证幂等。
*/
const TABLE_NAME = 'wms_user_oauth';
module.exports = {
async up(queryInterface, Sequelize) {
const { BIGINT } = Sequelize;
await queryInterface.addColumn(TABLE_NAME, 'app_id', {
type: BIGINT(20).UNSIGNED,
allowNull: true,
comment: '微信应用ID(关联 wms_wechat_app.id),用于取对应应用 token 处理',
});
await queryInterface.removeIndex(TABLE_NAME, 'uk_provider_openid');
await queryInterface.addIndex(TABLE_NAME, [ 'app_id', 'provider', 'openid' ], {
unique: true,
name: 'uk_app_provider_openid',
});
},
// eslint-disable-next-line no-unused-vars
async down(queryInterface, Sequelize) {
await queryInterface.removeIndex(TABLE_NAME, 'uk_app_provider_openid');
await queryInterface.addIndex(TABLE_NAME, [ 'provider', 'openid' ], {
unique: true,
name: 'uk_provider_openid',
});
await queryInterface.removeColumn(TABLE_NAME, 'app_id');
},
};