Spaces:
Running
π Complete Supabase Deployment Guide
Overview
This guide will help you set up your Unified Assistant application with Supabase for both local development and Hugging Face deployment.
π― What You'll Learn
- Local Development Setup - Test with SQLite and Supabase
- Hugging Face Deployment - Production-ready setup
- Database Migration - From SQLite to Supabase
- Testing & Validation - Comprehensive testing scripts
π Prerequisites
- Python 3.11+
- Supabase account (free at https://supabase.com)
- OpenAI API key
- Hugging Face account (for deployment)
π οΈ Quick Setup (Recommended)
Step 1: Run the Complete Setup Script
python setup_supabase_complete.py
This interactive script will:
- β Guide you through Supabase setup
- β Configure environment variables
- β Test database connection
- β Create configuration files
- β Generate Hugging Face secrets template
Step 2: Test Your Setup
python test_supabase_complete.py
This comprehensive test will verify:
- β Environment variables
- β Database connection
- β AI services
- β API endpoints
- β Configuration validation
π§ Manual Setup (Alternative)
1. Create Supabase Project
- Go to https://supabase.com
- Sign up/Login with GitHub
- Click "New Project"
- Choose organization and project name
- Set a strong database password
- Choose a region close to your users
- Wait for setup to complete (2-3 minutes)
2. Get Database Connection String
- In your Supabase dashboard, go to Settings β Database
- Find the Connection string section
- Copy the URI and modify it:
- Original:
postgresql://postgres:[password]@[host]:5432/postgres
- Modified:
postgresql+asyncpg://postgres:[password]@[host]:5432/postgres
- Original:
3. Create Environment Files
For Local Development (.env)
# Database Configuration
SUPABASE_DB_URL=postgresql+asyncpg://postgres:[password]@[host]:5432/postgres
DATABASE_URL=sqlite:///./unified_assistant.db
ENVIRONMENT=development
DEBUG=true
# Security
SECRET_KEY=your-secret-key-here
# AI Services
OPENAI_API_KEY=sk-your-openai-key-here
# Application
HOST=0.0.0.0
PORT=7860
For Hugging Face Deployment (secrets)
SUPABASE_DB_URL=postgresql+asyncpg://postgres:[password]@[host]:5432/postgres
ENVIRONMENT=production
SECRET_KEY=your-secret-key-here
OPENAI_API_KEY=sk-your-openai-key-here
π§ͺ Testing Your Setup
Local Testing
# Test database configuration
python test_database_config.py
# Test complete setup
python test_supabase_complete.py
# Test database connection
python setup_database.py
# Start local server
python main.py
Production Testing
# Test with production environment
ENVIRONMENT=production python test_supabase_complete.py
# Test Hugging Face deployment
python test_supabase.py
π Hugging Face Deployment
1. Prepare Your Repository
Ensure your repository has:
- β
main.py
(FastAPI application) - β
requirements.txt
(dependencies) - β
Dockerfile
(container configuration) - β
.dockerignore
(exclude unnecessary files)
2. Configure Hugging Face Spaces
- Go to your Hugging Face Space
- Navigate to Settings β Repository secrets
- Add the secrets from
huggingface_secrets_template.txt
:
SUPABASE_DB_URL=postgresql+asyncpg://postgres:[password]@[host]:5432/postgres
ENVIRONMENT=production
SECRET_KEY=your-secret-key-here
OPENAI_API_KEY=sk-your-openai-key-here
3. Deploy
- Push your code to the repository
- Hugging Face will automatically build and deploy
- Monitor the build logs for any issues
- Test the deployed application
π Troubleshooting
Common Issues
1. Database Connection Errors
Error: sqlite3.OperationalError: unable to open database file
Solution:
- Set
SUPABASE_DB_URL
environment variable - Ensure
ENVIRONMENT=production
for Hugging Face deployment
2. Missing Environment Variables
Error: SUPABASE_DB_URL environment variable is required
Solution:
- Add
SUPABASE_DB_URL
to your environment - Use the setup script:
python setup_supabase_complete.py
3. Authentication Errors
Error: authentication failed
Solution:
- Verify your Supabase password
- Check the connection string format
- Ensure you're using
postgresql+asyncpg://
notpostgresql://
4. Network Connectivity
Error: connection refused
Solution:
- Check if Supabase project is active
- Verify the host and port in connection string
- Test connection locally first
Debug Commands
# Check environment variables
python debug_env.py
# Test database configuration
python test_database_config.py
# Test complete setup
python test_supabase_complete.py
# Check application logs
python main.py
π Monitoring & Maintenance
Health Checks
Your application includes health check endpoints:
/health
- Basic health status/docs
- API documentation/
- Application information
Database Monitoring
- Supabase provides built-in monitoring
- Check connection pool usage
- Monitor query performance
- Review error logs
Application Logs
The application logs important events:
- Database connection status
- Environment detection
- Error messages
- Startup sequence
π Security Best Practices
Environment Variables
- β
Never commit
.env
files to version control - β Use Hugging Face secrets for production
- β Rotate secrets regularly
- β Use strong, unique passwords
Database Security
- β Supabase handles SSL automatically
- β Database credentials are encrypted in transit
- β Regular backups are automatic
- β Access is restricted by IP (if configured)
API Security
- β JWT tokens for authentication
- β Secure password hashing
- β Input validation and sanitization
- β Rate limiting (if implemented)
π Performance Optimization
Database Optimization
- Connection pooling is configured for Hugging Face Spaces
- Pool size reduced to 5 connections to avoid limits
- Automatic connection recycling every 5 minutes
- Pre-ping enabled for connection health
Application Optimization
- Async database operations
- Efficient query patterns
- Proper indexing (automatic with Supabase)
- Caching where appropriate
π Migration from SQLite
Automatic Migration
The application automatically detects the environment and switches databases:
- Development: Uses SQLite for local testing
- Production: Uses Supabase for deployment
- Hugging Face: Always uses Supabase
Data Migration
If you have existing SQLite data:
- Export data from SQLite
- Import to Supabase using their tools
- Update environment variables
- Test thoroughly
π Support
Getting Help
- Check the logs - Application provides detailed logging
- Run tests - Use the provided test scripts
- Review configuration - Verify environment variables
- Check Supabase status - Visit Supabase status page
Useful Resources
π Success Checklist
Before deploying to production, ensure:
- β Supabase project is created and active
- β Database connection string is correct
- β Environment variables are set
- β All tests pass locally
- β Application starts successfully
- β API endpoints respond correctly
- β Hugging Face secrets are configured
- β Deployment logs show no errors
π Next Steps
After successful setup:
- Test thoroughly - Use all provided test scripts
- Deploy to Hugging Face - Follow the deployment guide
- Monitor performance - Check logs and metrics
- Scale as needed - Supabase scales automatically
- Add features - Extend your application
Need help? Run python setup_supabase_complete.py
for interactive setup assistance!