diff --git a/backend/migrations/000097_tenant_foundation.sql b/backend/migrations/000097_tenant_foundation.sql deleted file mode 100644 index e5d35820..00000000 --- a/backend/migrations/000097_tenant_foundation.sql +++ /dev/null @@ -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; diff --git a/backend/migrations/000098_tenant_identity_scope.sql b/backend/migrations/000098_tenant_identity_scope.sql index 3cc8a004..514b17cd 100644 --- a/backend/migrations/000098_tenant_identity_scope.sql +++ b/backend/migrations/000098_tenant_identity_scope.sql @@ -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; diff --git a/backend/migrations/000099_tenant_listing_scope.sql b/backend/migrations/000099_tenant_commerce_scope.sql similarity index 61% rename from backend/migrations/000099_tenant_listing_scope.sql rename to backend/migrations/000099_tenant_commerce_scope.sql index 8ca86845..e7546112 100644 --- a/backend/migrations/000099_tenant_listing_scope.sql +++ b/backend/migrations/000099_tenant_commerce_scope.sql @@ -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 diff --git a/backend/migrations/000101_tenant_chat_notification_scope.sql b/backend/migrations/000100_tenant_operations_scope.sql similarity index 100% rename from backend/migrations/000101_tenant_chat_notification_scope.sql rename to backend/migrations/000100_tenant_operations_scope.sql diff --git a/backend/migrations/000100_tenant_payment_scope.sql b/backend/migrations/000100_tenant_payment_scope.sql deleted file mode 100644 index 14005532..00000000 --- a/backend/migrations/000100_tenant_payment_scope.sql +++ /dev/null @@ -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;