ai-humanoid-robotics / DATABASE_DEPLOYMENT_FIX.md
GitHub Actions
Deploy backend from GitHub Actions
84b0fa3

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.py instead of direct uvicorn
  • Ensures database is ready before the server starts

6. Enhanced FastAPI Main Application

  • Updated main.py to ensure database directory exists
  • Added database directory creation in the lifespan function

How It Works

  1. 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
  2. Database Location:

    • SQLite database: database/auth.db
    • Automatically created on first run
    • Stored in the container's filesystem (persistent for the container's lifetime)
  3. 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

  1. Check that database/auth.db is not tracked by git:

    git status  # Should not show database/auth.db
    
  2. Verify .gitignore includes database patterns

  3. 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

  1. Persistent Storage: Consider using Hugging Face Spaces persistent storage if available
  2. Database Migration: Add Alembic migrations for schema changes
  3. Backup Strategy: Implement regular database backups
  4. Cloud Database: Migrate to PostgreSQL or MySQL for better scalability

Files Modified

  • .gitignore - Added database file patterns
  • backend/database/config.py - Fixed import path for models
  • backend/main.py - Added database directory creation
  • backend/Dockerfile - Updated startup command
  • backend/init_database.py - New file (database initialization)
  • backend/start_server.py - New file (server startup script)