feat(tenant): 按租户隔离用户身份与后台权限
This commit is contained in:
@@ -0,0 +1,120 @@
|
||||
-- +goose Up
|
||||
|
||||
-- 现有用户、分销账号和后台人员全部归入大锤租户;新增解忧数据由应用写入真实租户。
|
||||
ALTER TABLE users
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_users_tenant_status (tenant_id, status);
|
||||
|
||||
UPDATE users
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE users
|
||||
DROP KEY uk_users_phone,
|
||||
ADD UNIQUE KEY uk_users_tenant_phone (tenant_id, phone),
|
||||
DROP KEY uk_users_invite_code,
|
||||
ADD UNIQUE KEY uk_users_tenant_invite_code (tenant_id, invite_code);
|
||||
|
||||
ALTER TABLE user_realname
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_user_realname_tenant_user (tenant_id, user_id);
|
||||
|
||||
UPDATE user_realname r
|
||||
JOIN users u ON u.id = r.user_id
|
||||
SET r.tenant_id = u.tenant_id
|
||||
WHERE r.tenant_id = 0;
|
||||
|
||||
ALTER TABLE user_realname
|
||||
DROP KEY uk_user_realname_user_id,
|
||||
ADD UNIQUE KEY uk_user_realname_tenant_user (tenant_id, user_id),
|
||||
DROP KEY uk_user_realname_id_no_hash,
|
||||
ADD UNIQUE KEY uk_user_realname_tenant_id_no (tenant_id, id_no_hash);
|
||||
|
||||
ALTER TABLE user_payment_accounts
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_user_payment_accounts_tenant_user (tenant_id, user_id, status);
|
||||
|
||||
UPDATE user_payment_accounts a
|
||||
JOIN users u ON u.id = a.user_id
|
||||
SET a.tenant_id = u.tenant_id
|
||||
WHERE a.tenant_id = 0;
|
||||
|
||||
ALTER TABLE wallet_accounts
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
DROP KEY uk_wallet_accounts_user_site,
|
||||
ADD UNIQUE KEY uk_wallet_accounts_tenant_user_site (tenant_id, user_id, distribution_site_id),
|
||||
ADD KEY idx_wallet_accounts_tenant_user (tenant_id, user_id);
|
||||
|
||||
UPDATE wallet_accounts
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE wallet_ledger
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_wallet_ledger_tenant_user_created (tenant_id, user_id, created_at DESC);
|
||||
|
||||
UPDATE wallet_ledger
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE withdrawal_requests
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
|
||||
ADD KEY idx_withdrawal_tenant_user_created (tenant_id, user_id, created_at DESC);
|
||||
|
||||
UPDATE withdrawal_requests
|
||||
SET tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
|
||||
WHERE tenant_id = 0;
|
||||
|
||||
ALTER TABLE admin_user_roles
|
||||
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER role_id,
|
||||
DROP KEY uk_admin_user_roles_user_role,
|
||||
ADD UNIQUE KEY uk_admin_user_roles_tenant_user_role (tenant_id, admin_user_id, role_id),
|
||||
ADD KEY idx_admin_user_roles_tenant_admin (tenant_id, admin_user_id);
|
||||
|
||||
UPDATE admin_user_roles aur
|
||||
JOIN roles r ON r.id = aur.role_id
|
||||
SET aur.tenant_id = IF(r.code = 'super_admin', 0, (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1))
|
||||
WHERE aur.tenant_id = 0;
|
||||
|
||||
-- +goose Down
|
||||
|
||||
-- 若升级后已存在同手机号跨租户账号,Down 恢复全局唯一键会失败,必须先人工处理重复数据。
|
||||
ALTER TABLE admin_user_roles
|
||||
DROP KEY idx_admin_user_roles_tenant_admin,
|
||||
DROP KEY uk_admin_user_roles_tenant_user_role,
|
||||
ADD UNIQUE KEY uk_admin_user_roles_user_role (admin_user_id, role_id),
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE withdrawal_requests
|
||||
DROP KEY idx_withdrawal_tenant_user_created,
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE wallet_ledger
|
||||
DROP KEY idx_wallet_ledger_tenant_user_created,
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE wallet_accounts
|
||||
DROP KEY idx_wallet_accounts_tenant_user,
|
||||
DROP KEY uk_wallet_accounts_tenant_user_site,
|
||||
ADD UNIQUE KEY uk_wallet_accounts_user_site (user_id, distribution_site_id),
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE user_payment_accounts
|
||||
DROP KEY idx_user_payment_accounts_tenant_user,
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE user_realname
|
||||
DROP KEY uk_user_realname_tenant_id_no,
|
||||
DROP KEY uk_user_realname_tenant_user,
|
||||
DROP KEY idx_user_realname_tenant_user,
|
||||
ADD UNIQUE KEY uk_user_realname_user_id (user_id),
|
||||
ADD UNIQUE KEY uk_user_realname_id_no_hash (id_no_hash),
|
||||
DROP COLUMN tenant_id;
|
||||
|
||||
ALTER TABLE users
|
||||
DROP KEY uk_users_tenant_invite_code,
|
||||
DROP KEY uk_users_tenant_phone,
|
||||
DROP KEY idx_users_tenant_status,
|
||||
ADD UNIQUE KEY uk_users_phone (phone),
|
||||
ADD UNIQUE KEY uk_users_invite_code (invite_code),
|
||||
DROP COLUMN tenant_id;
|
||||
Reference in New Issue
Block a user