Supabase Migration Summary
Overview
The application has been updated to use Supabase's REST API instead of direct PostgreSQL connections. This provides better reliability and easier access to your database.
What Changed
1. Dependencies Added
- supabase - Official Python client for Supabase
2. Database Module Updated (trade_calc/database.py)
All database functions now use the Supabase client instead of psycopg2:
get_supabase_client()- New function to create Supabase client with automatic authenticationget_db_connection()- Now returns Supabase client (backwards compatible)fetch_price_history()- Uses Supabase REST APIget_current_price_from_db()- Uses Supabase REST APIget_available_tickers()- Uses Supabase REST API
Important: The client automatically signs in with your username/password if provided in .env, which is required for tables with Row Level Security (RLS) enabled.
3. Scripts Updated
scripts/next_friday_calc.py- Updated to use Supabase client
4. Environment Variables
Required credentials in .env:
SUPABASE_URL=http://192.168.1.147:8000
SUPABASE_ANON_KEY=your-anon-key
SUPABASE_USERNAME=your-email@example.com
SUPABASE_PASSWORD=your-password
Benefits
- More Reliable - No more "server closed connection" errors
- RESTful API - Uses HTTP/REST instead of PostgreSQL wire protocol
- Better Error Handling - Clearer error messages
- Automatic Connection Pooling - Supabase handles connection management
Available Tables
Currently available in your Supabase instance:
- ✅ earnings_calendar - Earnings report dates for symbols
Not yet available (may need to be created):
- ❌ tcs_ohlc_daily - Historical OHLC price data
Testing
The script was successfully tested:
Result: ✅ Connected successfully to Supabase and queried earnings_calendar
Note: Initial connection returned 0 results because Row Level Security (RLS) was enabled on the table. After adding authentication with username/password, the script successfully found 1,442 symbols with earnings in the specified date range.
Backwards Compatibility
The old get_db_connection() function still exists but now returns a Supabase client instead of a psycopg2 connection. All code has been updated to use the new client API.
Next Steps
If you need historical price data (OHLC), you'll need to:
1. Create the tcs_ohlc_daily table in Supabase
2. Populate it with historical data
3. Ensure the table is exposed through Supabase's REST API
For now, the application will fall back to using yfinance for fetching current prices and options data.