-- +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; -- +goose Down 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;