Skip to content
Shell
10.3 KB
Raw Blame

DeepStack Database Architecture Fix Plan

Problem Summary

  1. Wrong Supabase Project: Codebase connected to ARC Generator project (rlzacttzdhmzypgjccri) instead of DeepStack project (scfdoayhmcruieppwawg)
  2. Missing Database Tables: Trading tables (watchlists, trade_journal, price_alerts, thesis) don't exist
  3. Stale Pipeline Status: PIPELINE_STATUS.md missing new features (Journal, Thesis, Insights)
  4. Duplicate Migrations: Multiple conflicting SQL migration files
  5. Stores Not Wired: All Zustand stores use localStorage only, not Supabase

Phase 1: Fix Supabase Connection (10 min)

1.1 Update MCP Configuration

  • Update Supabase MCP to use correct project ID: scfdoayhmcruieppwawg
  • Verify connection with list_tables after switch

1.2 Update Environment Variables

File: web/.env.local

NEXT_PUBLIC_SUPABASE_URL=https://scfdoayhmcruieppwawg.supabase.co
NEXT_PUBLIC_SUPABASE_ANON_KEY=<get from Supabase dashboard>

Phase 2: Apply Database Migrations (20 min)

2.1 Consolidated Migration: Core Trading Tables

Tool: mcp__supabase__apply_migration

-- conversations table
CREATE TABLE IF NOT EXISTS public.conversations (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
  title TEXT,
  provider TEXT DEFAULT 'claude',
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- messages table
CREATE TABLE IF NOT EXISTS public.messages (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  conversation_id UUID REFERENCES public.conversations(id) ON DELETE CASCADE NOT NULL,
  role TEXT NOT NULL CHECK (role IN ('user', 'assistant', 'system', 'tool')),
  content TEXT NOT NULL,
  tool_calls JSONB,
  tool_results JSONB,
  provider TEXT,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

-- watchlists table
CREATE TABLE IF NOT EXISTS public.watchlists (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
  name TEXT NOT NULL,
  symbols TEXT[] DEFAULT '{}',
  sort_order INTEGER DEFAULT 0,
  is_default BOOLEAN DEFAULT FALSE,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- price_alerts table
CREATE TABLE IF NOT EXISTS public.price_alerts (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
  symbol TEXT NOT NULL,
  alert_type TEXT NOT NULL CHECK (alert_type IN ('price_above', 'price_below', 'pct_change_up', 'pct_change_down')),
  target_value NUMERIC NOT NULL,
  triggered BOOLEAN DEFAULT FALSE,
  triggered_at TIMESTAMPTZ,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  expires_at TIMESTAMPTZ
);

-- chart_layouts table
CREATE TABLE IF NOT EXISTS public.chart_layouts (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
  name TEXT NOT NULL,
  is_default BOOLEAN DEFAULT FALSE,
  config JSONB NOT NULL DEFAULT '{}',
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- trade_journal table (basic trades)
CREATE TABLE IF NOT EXISTS public.trade_journal (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
  symbol TEXT NOT NULL,
  action TEXT NOT NULL CHECK (action IN ('BUY', 'SELL')),
  quantity NUMERIC NOT NULL,
  price NUMERIC NOT NULL,
  order_type TEXT NOT NULL CHECK (order_type IN ('MKT', 'LMT', 'STP')),
  notes TEXT,
  tags TEXT[] DEFAULT '{}',
  pnl NUMERIC,
  created_at TIMESTAMPTZ DEFAULT NOW()
);

2.2 New Migration: Thesis Table

CREATE TABLE IF NOT EXISTS public.thesis (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
  title TEXT NOT NULL,
  symbol TEXT NOT NULL,
  status TEXT NOT NULL DEFAULT 'drafting'
    CHECK (status IN ('drafting', 'active', 'validated', 'invalidated', 'archived')),
  hypothesis TEXT NOT NULL,
  timeframe TEXT NOT NULL,
  entry_target NUMERIC,
  exit_target NUMERIC,
  stop_loss NUMERIC,
  risk_reward_ratio NUMERIC,
  key_conditions JSONB DEFAULT '[]'::jsonb,
  validation_score INTEGER CHECK (validation_score >= 0 AND validation_score <= 100),
  validation_notes TEXT,
  conversation_id UUID REFERENCES public.conversations(id) ON DELETE SET NULL,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

2.3 New Migration: Journal Entries (with emotions)

CREATE TABLE IF NOT EXISTS public.journal_entries (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE NOT NULL,
  symbol TEXT NOT NULL,
  trade_date DATE NOT NULL,
  direction TEXT NOT NULL CHECK (direction IN ('long', 'short')),
  entry_price NUMERIC NOT NULL,
  exit_price NUMERIC,
  quantity NUMERIC NOT NULL,
  pnl NUMERIC,
  pnl_percent NUMERIC,
  emotion_at_entry TEXT NOT NULL CHECK (emotion_at_entry IN (
    'confident', 'anxious', 'greedy', 'fearful', 'fomo',
    'regret', 'relief', 'neutral', 'excited', 'frustrated'
  )),
  emotion_at_exit TEXT CHECK (emotion_at_exit IN (
    'confident', 'anxious', 'greedy', 'fearful', 'fomo',
    'regret', 'relief', 'neutral', 'excited', 'frustrated'
  )),
  notes TEXT,
  lessons_learned TEXT,
  thesis_id UUID REFERENCES public.thesis(id) ON DELETE SET NULL,
  screenshot_urls JSONB DEFAULT '[]'::jsonb,
  created_at TIMESTAMPTZ DEFAULT NOW(),
  updated_at TIMESTAMPTZ DEFAULT NOW()
);

2.4 Migration: Indexes & RLS

-- Indexes
CREATE INDEX idx_watchlists_user_id ON public.watchlists(user_id);
CREATE INDEX idx_price_alerts_user_id ON public.price_alerts(user_id);
CREATE INDEX idx_price_alerts_symbol ON public.price_alerts(symbol);
CREATE INDEX idx_trade_journal_user_id ON public.trade_journal(user_id);
CREATE INDEX idx_thesis_user_id ON public.thesis(user_id);
CREATE INDEX idx_thesis_symbol ON public.thesis(symbol);
CREATE INDEX idx_journal_entries_user_id ON public.journal_entries(user_id);

-- Enable RLS
ALTER TABLE public.conversations ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.messages ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.watchlists ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.price_alerts ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.chart_layouts ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.trade_journal ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.thesis ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.journal_entries ENABLE ROW LEVEL SECURITY;

-- RLS policies (for each table)
-- Pattern: auth.uid() = user_id for SELECT/INSERT/UPDATE/DELETE

Phase 3: Update Pipeline Status (10 min)

File: /Users/eddiebelaval/Development/deepstack/PIPELINE_STATUS.md

Updates Required:

  1. Change "Last Updated" to 2025-12-07
  2. Update Stage 5 Feature table with accurate percentages
  3. Add new features: Journal, Thesis Engine, AI Insights
  4. Add critical blocker note about database tables

New Stage 5 Feature Table:

Feature Status Complete E2E Notes
AI Chat Interface 85% Partial Streaming + 20 tools working
Market Data Display 85% Partial Alpaca news integration added
Portfolio Tracker 25% No UI exists, DB not connected
Options Screener 70% Partial Routes built, backend incomplete
Emotional Firewall 100% Yes Complete
Trade Journal 40% No TipTap UI done, needs DB
Thesis Engine 35% No Dashboard done, needs DB table
AI Insights 25% No Page exists, mock data

Phase 4: Clean Up Duplicate Migrations (5 min)

Files to DELETE (keep only 002_create_trading_tables.sql):

  • web/supabase/migrations/001_create_trade_journal.sql
  • web/supabase/migrations/002_create_watchlists.sql
  • web/supabase/migrations/003_create_price_alerts.sql

Files to KEEP:

  • web/supabase/migrations/001_create_chat_tables.sql
  • web/supabase/migrations/002_create_trading_tables.sql
  • web/supabase/usage_tracking.sql

Files to CREATE:

  • web/supabase/migrations/003_create_thesis_table.sql
  • web/supabase/migrations/004_create_journal_entries.sql
  • web/supabase/migrations/005_indexes_and_rls.sql

Phase 5: Verify & Run Security Advisor (5 min)

After migrations applied:

  1. Run mcp__supabase__list_tables - verify all 8 tables exist
  2. Run mcp__supabase__get_advisors (security) - check RLS policies
  3. Run mcp__supabase__generate_typescript_types - update types

Execution Checklist

  • P1.1 Update Supabase MCP to project scfdoayhmcruieppwawg
  • P1.2 Update web/.env.local with correct Supabase URL/key
  • P2.1 Apply core trading tables migration
  • P2.2 Apply thesis table migration
  • P2.3 Apply journal_entries table migration
  • P2.4 Apply indexes and RLS migration
  • P3 Update PIPELINE_STATUS.md
  • P4 Delete duplicate migration files
  • P5.1 Verify tables with list_tables
  • P5.2 Run security advisor
  • P5.3 Generate TypeScript types

Files to Modify

File Action Phase
MCP Supabase config Update project ID P1.1
web/.env.local Update SUPABASE_URL, ANON_KEY P1.2
PIPELINE_STATUS.md Update features, stage 5 status P3
web/supabase/migrations/001_create_trade_journal.sql DELETE P4
web/supabase/migrations/002_create_watchlists.sql DELETE P4
web/supabase/migrations/003_create_price_alerts.sql DELETE P4
web/supabase/migrations/003_create_thesis_table.sql CREATE P4
web/supabase/migrations/004_create_journal_entries.sql CREATE P4
web/supabase/migrations/005_indexes_and_rls.sql CREATE P4

Agent/Tool Assignment

Task Agent/Tool
Apply migrations mcp__supabase__apply_migration
Verify tables mcp__supabase__list_tables
Security check mcp__supabase__get_advisors
Generate types mcp__supabase__generate_typescript_types
File edits Direct Edit/Write tools
Delete files Bash rm command

Success Criteria

  1. mcp__supabase__list_tables shows: conversations, messages, watchlists, price_alerts, chart_layouts, trade_journal, thesis, journal_entries
  2. mcp__supabase__get_advisors (security) returns no critical issues
  3. PIPELINE_STATUS.md reflects accurate Stage 5 status
  4. No duplicate migration files remain
  5. Environment pointing to correct Supabase project