trading-platform-database-v2/ddl/schemas/investment/tables/09-distribution_runs.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

66 lines
2.8 KiB
SQL

-- ============================================================================
-- OrbiQuant IA - Trading Platform
-- Schema: investment
-- File: tables/09-distribution_runs.sql
-- Description: Distribution job execution logs and summaries
-- ============================================================================
CREATE TABLE investment.distribution_runs (
-- Primary Key
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
-- Run Information
run_date DATE NOT NULL UNIQUE,
-- Statistics
total_accounts INTEGER NOT NULL DEFAULT 0,
successful_count INTEGER NOT NULL DEFAULT 0,
failed_count INTEGER NOT NULL DEFAULT 0,
-- Financial Totals
total_gross_amount DECIMAL(18,2) NOT NULL DEFAULT 0,
total_fee_amount DECIMAL(18,2) NOT NULL DEFAULT 0,
total_net_amount DECIMAL(18,2) NOT NULL DEFAULT 0,
-- Timing
started_at TIMESTAMPTZ NOT NULL,
completed_at TIMESTAMPTZ,
duration_ms INTEGER,
-- Error Tracking
error_details JSONB,
-- Audit
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
-- Constraints
CONSTRAINT valid_counts CHECK (
successful_count >= 0 AND
failed_count >= 0 AND
total_accounts = successful_count + failed_count
),
CONSTRAINT valid_duration CHECK (duration_ms IS NULL OR duration_ms >= 0)
);
-- Indexes for Performance
CREATE INDEX idx_distribution_runs_date ON investment.distribution_runs(run_date DESC);
CREATE INDEX idx_distribution_runs_started ON investment.distribution_runs(started_at DESC);
-- Table Comments
COMMENT ON TABLE investment.distribution_runs IS 'Distribution job execution logs and performance metrics';
-- Column Comments
COMMENT ON COLUMN investment.distribution_runs.id IS 'Unique identifier for the run record';
COMMENT ON COLUMN investment.distribution_runs.run_date IS 'Date of the distribution run';
COMMENT ON COLUMN investment.distribution_runs.total_accounts IS 'Total accounts processed';
COMMENT ON COLUMN investment.distribution_runs.successful_count IS 'Number of successful distributions';
COMMENT ON COLUMN investment.distribution_runs.failed_count IS 'Number of failed distributions';
COMMENT ON COLUMN investment.distribution_runs.total_gross_amount IS 'Sum of all gross returns';
COMMENT ON COLUMN investment.distribution_runs.total_fee_amount IS 'Sum of all performance fees';
COMMENT ON COLUMN investment.distribution_runs.total_net_amount IS 'Sum of all net distributions';
COMMENT ON COLUMN investment.distribution_runs.started_at IS 'Timestamp when run started';
COMMENT ON COLUMN investment.distribution_runs.completed_at IS 'Timestamp when run completed';
COMMENT ON COLUMN investment.distribution_runs.duration_ms IS 'Run duration in milliseconds';
COMMENT ON COLUMN investment.distribution_runs.error_details IS 'Error information as JSON if failures occurred';
COMMENT ON COLUMN investment.distribution_runs.created_at IS 'Timestamp when record was created';