Files
hfb_sys/backend/migrations/000099_tenant_commerce_scope.sql
T

108 lines
3.6 KiB
SQL

-- +goose Up
ALTER TABLE game_accounts
ADD COLUMN source_tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
ADD KEY idx_game_accounts_source_tenant (source_tenant_id, status);
UPDATE game_accounts a
JOIN users u ON u.id = a.owner_id
SET a.source_tenant_id = u.tenant_id
WHERE a.source_tenant_id = 0;
ALTER TABLE rental_listings
ADD COLUMN source_tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
ADD KEY idx_rental_listings_source_tenant (source_tenant_id, status, review_status);
UPDATE rental_listings l
JOIN game_accounts a ON a.id = l.account_id
SET l.source_tenant_id = a.source_tenant_id
WHERE l.source_tenant_id = 0;
ALTER TABLE rental_orders
ADD COLUMN buyer_tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER id,
ADD COLUMN source_tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER buyer_tenant_id,
ADD COLUMN payment_tenant_id BIGINT UNSIGNED NOT NULL DEFAULT 0 AFTER source_tenant_id,
ADD COLUMN source_amount_cent BIGINT NOT NULL DEFAULT 0 AFTER owner_rent_amount_cent,
ADD KEY idx_rental_orders_buyer_tenant_created (buyer_tenant_id, created_at),
ADD KEY idx_rental_orders_source_tenant_created (source_tenant_id, created_at);
UPDATE rental_orders o
JOIN users r ON r.id = o.renter_id
JOIN rental_listings l ON l.id = o.listing_id
SET o.buyer_tenant_id = r.tenant_id,
o.payment_tenant_id = r.tenant_id,
o.source_tenant_id = l.source_tenant_id,
o.source_amount_cent = o.owner_rent_amount_cent
WHERE o.buyer_tenant_id = 0;
INSERT INTO tenant_listing_offers (tenant_id, listing_id, display_status, price_cent)
SELECT t.id, l.id, 'enabled', NULL
FROM tenants t
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
DROP KEY idx_rental_orders_source_tenant_created,
DROP KEY idx_rental_orders_buyer_tenant_created,
DROP COLUMN source_amount_cent,
DROP COLUMN payment_tenant_id,
DROP COLUMN source_tenant_id,
DROP COLUMN buyer_tenant_id;
ALTER TABLE rental_listings
DROP KEY idx_rental_listings_source_tenant,
DROP COLUMN source_tenant_id;
ALTER TABLE game_accounts
DROP KEY idx_game_accounts_source_tenant,
DROP COLUMN source_tenant_id;