Hugging Face Spaces Database Deployment Fix
Problem
The Hugging Face deployment was failing due to a binary database file (database/auth.db) being present in the repository. Hugging Face Spaces doesn't allow binary files in the repository without using xet storage.
Solution Implemented
1. Removed Database File from Git Tracking
- Executed:
git rm --cached backend/database/auth.db - This removes the file from git tracking while keeping it locally
2. Updated .gitignore
Added the following patterns to exclude database files:
# Database files
*.db
*.sqlite
*.sqlite3
backend/database/*.db
backend/database/*.sqlite
backend/database/*.sqlite3
3. Created Database Initialization Script
- File:
backend/init_database.py - Automatically creates the database directory and tables on startup
- Safe to run multiple times (uses SQLAlchemy's
create_all)
4. Created Server Startup Script
- File:
backend/start_server.py - Initializes the database before starting the FastAPI server
- Provides clear logging of the initialization process
5. Updated Dockerfile
- Changed CMD to use
start_server.pyinstead of direct uvicorn - Ensures database is ready before the server starts
6. Enhanced FastAPI Main Application
- Updated
main.pyto ensure database directory exists - Added database directory creation in the lifespan function
How It Works
On Deployment Start:
- The container starts and runs
start_server.py - This script first runs
init_database.py - The database directory and tables are created if they don't exist
- Then the FastAPI server starts normally
- The container starts and runs
Database Location:
- SQLite database:
database/auth.db - Automatically created on first run
- Stored in the container's filesystem (persistent for the container's lifetime)
- SQLite database:
Safety Features:
- Database initialization is idempotent (safe to run multiple times)
- Uses SQLAlchemy's
create_all()which only creates missing tables - The FastAPI app also ensures the database directory exists on startup
Verification Steps
Check that
database/auth.dbis not tracked by git:git status # Should not show database/auth.dbVerify .gitignore includes database patterns
Test locally:
cd backend rm -f database/auth.db # Remove existing database python start_server.py # Should create database and start server
Deployment Notes
- The database will be created automatically when the Space starts
- The database persists as long as the Space container is not restarted
- For production use, consider migrating to a persistent database solution
- The SQLite file is stored in the container's filesystem at
/app/database/auth.db
Future Improvements
- Persistent Storage: Consider using Hugging Face Spaces persistent storage if available
- Database Migration: Add Alembic migrations for schema changes
- Backup Strategy: Implement regular database backups
- Cloud Database: Migrate to PostgreSQL or MySQL for better scalability
Files Modified
.gitignore- Added database file patternsbackend/database/config.py- Fixed import path for modelsbackend/main.py- Added database directory creationbackend/Dockerfile- Updated startup commandbackend/init_database.py- New file (database initialization)backend/start_server.py- New file (server startup script)