trading-platform-database-v2/ddl/schemas/investment/tables/08-distribution_history.sql
Adrian Flores Cortes 439489bde4 feat: Add DDL tables for notifications system
- Add auth.notifications table for storing user notifications
- Add auth.user_push_tokens table for FCM/APNs tokens
- Add investment.distribution_history for daily distribution records
- Add investment.distribution_runs for job execution logs

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-01-25 03:48:28 -06:00

56 lines
2.7 KiB
SQL

-- ============================================================================
-- 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';