-- Migration: Add profile_id to orders and trade_history
-- Date: 2026-02-07
-- Purpose: Map every order and trade to its originating trade_profile for per-profile analytics

-- Orders table
ALTER TABLE orders
ADD COLUMN IF NOT EXISTS profile_id uuid REFERENCES trade_profiles(id) ON DELETE SET NULL;

CREATE INDEX IF NOT EXISTS idx_orders_profile_id ON orders(profile_id);

-- Trade history table
ALTER TABLE trade_history
ADD COLUMN IF NOT EXISTS profile_id uuid REFERENCES trade_profiles(id) ON DELETE SET NULL;

CREATE INDEX IF NOT EXISTS idx_trade_history_profile_id ON trade_history(profile_id);

COMMENT ON COLUMN orders.profile_id IS 'The trade profile that triggered this order';
COMMENT ON COLUMN trade_history.profile_id IS 'The trade profile that triggered this trade';
