AeThex-OS/migrations/0006_revenue_events.sql
MrPiglr 4b84eedbd3 feat: Add revenue_events table and fix migration FK constraints
- Add revenue_events table to track org/project revenue with source tracking
- Add Drizzle schema for revenue_events with proper org/project references
- Create migration 0006_revenue_events.sql with indexes
- Fix migration 0004: Remove FK constraints to profiles.id (auth schema incompatibility)
- Document auth.users/profiles.id type mismatch (UUID vs VARCHAR)
- Harden profile update authorization (self-update or org admin/owner only)
- Complete org-scoping security audit implementation (42 gaps closed)
2026-01-05 04:54:12 +00:00

19 lines
938 B
SQL

-- Revenue Events: Track platform revenue by organization and project
CREATE TABLE IF NOT EXISTS revenue_events (
id VARCHAR PRIMARY KEY DEFAULT gen_random_uuid()::text,
organization_id VARCHAR NOT NULL REFERENCES organizations(id),
project_id VARCHAR REFERENCES projects(id) ON DELETE SET NULL,
source_type TEXT NOT NULL,
source_id TEXT NOT NULL,
gross_amount NUMERIC(10,2) NOT NULL,
platform_fee NUMERIC(10,2) NOT NULL DEFAULT 0,
net_amount NUMERIC(10,2) NOT NULL,
currency TEXT NOT NULL DEFAULT 'USD',
metadata JSONB,
created_at TIMESTAMP NOT NULL DEFAULT now()
);
-- Indexes for revenue_events
CREATE INDEX IF NOT EXISTS idx_revenue_events_org_created ON revenue_events(organization_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_revenue_events_project_created ON revenue_events(project_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_revenue_events_source ON revenue_events(source_type, source_id);