chore(migrations): 重新编号并合并租户迁移

This commit is contained in:
yml
2026-11-15 17:37:56 +08:00
parent 8ede8f89c2
commit fb81315a35
5 changed files with 95 additions and 102 deletions
@@ -1,53 +0,0 @@
-- +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='经营租户商品货架';
-- +goose Down
DROP TABLE IF EXISTS tenant_listing_offers;
DROP TABLE IF EXISTS tenant_domains;
DROP TABLE IF EXISTS tenants;
@@ -1,6 +1,52 @@
-- +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);
@@ -78,7 +124,6 @@ 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,
@@ -118,3 +163,7 @@ ALTER TABLE users
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;
@@ -42,8 +42,52 @@ CROSS JOIN rental_listings l
WHERE t.status = 'active'
ON DUPLICATE KEY UPDATE display_status = display_status;
ALTER TABLE payment_merchant_configs
ADD COLUMN owner_tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
ADD KEY idx_payment_merchant_configs_owner (owner_tenant_id, pay_way, status);
UPDATE payment_merchant_configs
SET owner_tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
WHERE owner_tenant_id = 0;
ALTER TABLE payment_orders
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
ADD KEY idx_payment_orders_tenant_created (tenant_id, created_at);
UPDATE payment_orders p
JOIN rental_orders o ON o.id = p.order_id
SET p.tenant_id = o.payment_tenant_id
WHERE p.tenant_id = 0
AND o.payment_tenant_id > 0;
UPDATE payment_orders p
JOIN users u ON u.id = p.user_id
SET p.tenant_id = u.tenant_id
WHERE p.tenant_id = 0;
ALTER TABLE payment_config_usage_logs
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
ADD KEY idx_payment_config_usage_logs_tenant (tenant_id, created_at);
UPDATE payment_config_usage_logs l
JOIN payment_orders p ON p.id = l.payment_order_id
SET l.tenant_id = p.tenant_id
WHERE l.tenant_id = 0;
-- +goose Down
ALTER TABLE payment_config_usage_logs
DROP KEY idx_payment_config_usage_logs_tenant,
DROP COLUMN tenant_id;
ALTER TABLE payment_orders
DROP KEY idx_payment_orders_tenant_created,
DROP COLUMN tenant_id;
ALTER TABLE payment_merchant_configs
DROP KEY idx_payment_merchant_configs_owner,
DROP COLUMN owner_tenant_id;
DELETE FROM tenant_listing_offers;
ALTER TABLE rental_orders
@@ -1,47 +0,0 @@
-- +goose Up
ALTER TABLE payment_merchant_configs
ADD COLUMN owner_tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
ADD KEY idx_payment_merchant_configs_owner (owner_tenant_id, pay_way, status);
UPDATE payment_merchant_configs
SET owner_tenant_id = (SELECT id FROM tenants WHERE code = 'dachui' LIMIT 1)
WHERE owner_tenant_id = 0;
ALTER TABLE payment_orders
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
ADD KEY idx_payment_orders_tenant_created (tenant_id, created_at);
UPDATE payment_orders p
JOIN rental_orders o ON o.id = p.order_id
SET p.tenant_id = o.payment_tenant_id
WHERE p.tenant_id = 0
AND o.payment_tenant_id > 0;
UPDATE payment_orders p
JOIN users u ON u.id = p.user_id
SET p.tenant_id = u.tenant_id
WHERE p.tenant_id = 0;
ALTER TABLE payment_config_usage_logs
ADD COLUMN tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
ADD KEY idx_payment_config_usage_logs_tenant (tenant_id, created_at);
UPDATE payment_config_usage_logs l
JOIN payment_orders p ON p.id = l.payment_order_id
SET l.tenant_id = p.tenant_id
WHERE l.tenant_id = 0;
-- +goose Down
ALTER TABLE payment_config_usage_logs
DROP KEY idx_payment_config_usage_logs_tenant,
DROP COLUMN tenant_id;
ALTER TABLE payment_orders
DROP KEY idx_payment_orders_tenant_created,
DROP COLUMN tenant_id;
ALTER TABLE payment_merchant_configs
DROP KEY idx_payment_merchant_configs_owner,
DROP COLUMN owner_tenant_id;