-- ============================================================================ -- OrbiQuant IA - Trading Platform -- Schema: investment -- File: tables/08-distribution_history.sql -- Description: Daily distribution history per account -- ============================================================================ CREATE TABLE investment.distribution_history ( -- Primary Key id UUID PRIMARY KEY DEFAULT gen_random_uuid(), -- Foreign Keys account_id UUID NOT NULL REFERENCES investment.accounts(id) ON DELETE CASCADE, product_id UUID NOT NULL REFERENCES investment.products(id) ON DELETE RESTRICT, -- Distribution Period distribution_date DATE NOT NULL, -- Amounts gross_amount DECIMAL(18,2) NOT NULL, fee_amount DECIMAL(18,2) NOT NULL DEFAULT 0, net_amount DECIMAL(18,2) NOT NULL, -- Balance Tracking balance_before DECIMAL(18,2) NOT NULL, balance_after DECIMAL(18,2) NOT NULL, -- Audit created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), -- Constraints CONSTRAINT positive_amounts CHECK (gross_amount >= 0 AND net_amount >= 0 AND fee_amount >= 0), CONSTRAINT unique_daily_distribution UNIQUE (account_id, distribution_date), CONSTRAINT valid_balance_change CHECK (balance_after = balance_before + net_amount) ); -- Indexes for Performance CREATE INDEX idx_distribution_history_account ON investment.distribution_history(account_id); CREATE INDEX idx_distribution_history_date ON investment.distribution_history(distribution_date DESC); CREATE INDEX idx_distribution_history_product ON investment.distribution_history(product_id); -- Table Comments COMMENT ON TABLE investment.distribution_history IS 'Daily distribution records per investment account'; -- Column Comments COMMENT ON COLUMN investment.distribution_history.id IS 'Unique identifier for the distribution record'; COMMENT ON COLUMN investment.distribution_history.account_id IS 'Reference to the investment account'; COMMENT ON COLUMN investment.distribution_history.product_id IS 'Reference to the investment product'; COMMENT ON COLUMN investment.distribution_history.distribution_date IS 'Date of the distribution'; COMMENT ON COLUMN investment.distribution_history.gross_amount IS 'Gross return before fees'; COMMENT ON COLUMN investment.distribution_history.fee_amount IS 'Performance fee deducted'; COMMENT ON COLUMN investment.distribution_history.net_amount IS 'Net amount credited to account'; COMMENT ON COLUMN investment.distribution_history.balance_before IS 'Account balance before distribution'; COMMENT ON COLUMN investment.distribution_history.balance_after IS 'Account balance after distribution'; COMMENT ON COLUMN investment.distribution_history.created_at IS 'Timestamp when record was created';