Files
hfb_sys/backend/migrations/000098_tenant_identity_scope.sql
T

170 lines
6.4 KiB
SQL

-- +goose Up
CREATE TABLE IF NOT EXISTS tenants (
id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
code VARCHAR(32) NOT NULL,
name VARCHAR(64) NOT NULL,
status VARCHAR(16) NOT NULL DEFAULT 'active',
profile_json JSON NULL,
features_json JSON NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY uk_tenants_code (code),
KEY idx_tenants_status (status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='经营租户';
INSERT INTO tenants (code, name, status, profile_json, features_json)
VALUES
('dachui', '大锤商行', 'active', JSON_OBJECT('site_name', '大锤商行'), JSON_OBJECT()),
('jieyou', '解忧商行', 'active', JSON_OBJECT('site_name', '解忧商行'), JSON_OBJECT())
ON DUPLICATE KEY UPDATE
name = VALUES(name),
status = VALUES(status),
updated_at = CURRENT_TIMESTAMP;
CREATE TABLE IF NOT EXISTS tenant_domains (
id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
host VARCHAR(255) NOT NULL,
domain_type VARCHAR(32) NOT NULL DEFAULT 'storefront',
status VARCHAR(16) NOT NULL DEFAULT 'active',
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY uk_tenant_domains_host (host),
KEY idx_tenant_domains_tenant (tenant_id, domain_type, status)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='经营租户域名';
CREATE TABLE IF NOT EXISTS tenant_listing_offers (
id BIGINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
tenant_id BIGINT UNSIGNED NOT NULL,
listing_id BIGINT UNSIGNED NOT NULL,
display_status VARCHAR(16) NOT NULL DEFAULT 'enabled',
price_cent BIGINT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY uk_tenant_listing_offers_listing (tenant_id, listing_id),
KEY idx_tenant_listing_offers_display (tenant_id, display_status, listing_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='经营租户商品货架';
-- 现有用户、分销账号和后台人员归入大锤租户;平台超管角色保持 tenant_id=0。
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
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;
DROP TABLE IF EXISTS tenant_listing_offers;
DROP TABLE IF EXISTS tenant_domains;
DROP TABLE IF EXISTS tenants;