The Database component is the central hub for all database operations in OpenClone. It handles more than just the database itself - it manages database logic, bootstrap scripts, migrations, and provides the PostgreSQL RDBMS via VS Code extensions.
- Database: PostgreSQL
- Chosen over MS SQL Server for cost efficiency and vector data type support
- Vector data type enables phrase similarity matching for user questions
- Cross-platform compatibility (Windows/Linux/Mac)
- Framework: Entity Framework (code-first approach)
- Language: Python 3.11.5 (better PostgreSQL integration than .NET)
- Code-first database design with Entity Framework
- Code furst preferred over stored procedures
- ASP.NET Identity tables integrated with main database
- Designed for Kubernetes deployment but supports local development
Database/
├── Backups/ # Bootstrap data for system initialization
│ ├── OpenCloneFS/ # File system bootstrap (single version)
│ ├── open_clone_db/ # Main database SQL scripts
│ ├── open_clone_logging_db/ # Logging database SQL scripts
│ └── openclone_db_prod_db/ # Production DB (same as main for now)
├── BatchScripts/ # Development lifecycle automation
│ ├── backup.bat # Captures current state as new bootstrap
│ ├── migrate.bat # Applies EF schema changes
│ └── restore.bat # Resets to clean bootstrap state
├── Container/ # Docker configuration
├── SQL/ # Manual setup scripts
│ ├── DBConfiguration/ # Database configuration scripts
│ └── StoredProcedures/ # Custom stored procedures
├── Main.py # Python entry point
├── DatabaseInterface.py # Data Access Layer for Python scripts
└── requirements.txt # Python dependencies
- Use
../StartStopScripts/Database/start.batto run PostgreSQL container - Container name:
openclone-database - Port mapping: host 5433 → container 5432
- Dockerfile handles basic PostgreSQL setup
- Run scripts in
./SQL/DBConfiguration/ - Run scripts in
./SQL/StoredProcedures/ - Additional manual configuration steps required
- migrate.bat: Builds Website project → applies EF changes to database schema
- restore.bat: Complete reset to clean state
- Deletes existing databases and root
/OpenCloneFS - Restores from bootstrap versions in
Database/Backup - Perfect for "I broke something" recovery
- Deletes existing databases and root
- backup.bat: Captures current state as new baseline
- Replaces
/BackupsSQL scripts with current database state - Replaces
/Backups/OpenCloneFSwith current/OpenCloneFScontents
- Replaces
The following environment variables must be set on the host system for the Database project:
Database Configuration:
OpenClone_DefaultConnection_Super- Super user connection string for main database (used during EF migrations)OpenClone_LogDbConnection_Super- Super user connection string for logging database (used during EF migrations)OpenClone_DefaultConnection- Regular connection string for main database (used by web application)OpenClone_LogDbConnection- Regular connection string for logging database (used by web application)OpenClone_postgres_superuser_password- PostgreSQL superuser passwordOpenClone_OpenCloneDB_User- Main database usernameOpenClone_OpenCloneDB_Password- Main database passwordOpenClone_OpenCloneDB_Name- Main database nameOpenClone_LogDB_User- Logging database usernameOpenClone_LogDB_Password- Logging database passwordOpenClone_LogDB_Name- Logging database nameOpenClone_DB_Host- Database host addressOpenClone_DB_Port- Database port
File System:
OpenClone_Root_Dir- Root directory path for OpenClone projectOpenClone_OpenCloneFS- OpenCloneFS directory path for backup/restore operations
Entity Framework migrations require special handling for security:
-
During Migration (
OpenClone_EF_MIGRATION=True):- Uses super user connection strings (
OpenClone_DefaultConnection_Super,OpenClone_LogDbConnection_Super) - Set by
Migrations.pywhen running EF migration commands - Required because schema changes need elevated privileges
- Uses super user connection strings (
-
During Normal Operation (
OpenClone_EF_MIGRATIONnot set):- Uses regular user connection strings (
OpenClone_DefaultConnection,OpenClone_LogDbConnection) - Web application runs with limited database privileges
- Configured in
Website/OpenClone.UI/Configuration/DbContextConfigurator.cs
- Uses regular user connection strings (
This dual-connection approach ensures migrations can modify schema while keeping the running application secure with minimal database permissions.
- Main.py: Entry point, parses environment variables, orchestrates operations
- DatabaseInterface.py: Data Access Layer for all Database project Python scripts
- Purpose: External tooling for database operations (migrate/restore/backup/initialization)
- Note: Python runs outside the container, not inside PostgreSQL container
The /Backups directory contains complete bootstrap data for rapid system setup:
- Environment-specific SQL scripts for minimal database population
- Single OpenCloneFS version (not yet environment-specific)
- Supports the architectural goal: download repo → setup dependencies → click build → entire solution runs
Core Architectural Concept: /OpenCloneFS serves as the shared file system for the entire OpenClone application. All containers in the cluster use this common directory for logical simplicity, avoiding the complexity of distributed file systems communicating over REST, WebRTC, or sockets.
Why in Database Project: Although not technically database-related, OpenCloneFS management resides in the Database project because it's fundamentally about data management. This centralized approach keeps file system operations consolidated with other data operations. "It was the best place to put it".
Bootstrap Integration: The Database component manages OpenCloneFS through the backup/restore cycle:
- Restore: Replaces root
/OpenCloneFSwith bootstrap version from/Backups/OpenCloneFS - Backup: Captures current
/OpenCloneFSstate as new bootstrap baseline
Current State: Database runs in a single container/deployment for development simplicity.
Future Architecture: Database should eventually be moved onto /OpenCloneFS for better scalability. The IAC project already uses Longhorn storage which provides ReadWriteMany access - a step in the right direction.
Scope: Extensive scalability work remains for both database and OpenCloneFS components, but this is considered IAC-level architecture rather than core database/schema work. The Database project focuses on data structure and schema design, while scalability decisions belong in the IAC project.
Development Status: Core database functionality is complete - scalability features are architectural enhancements rather than core requirements.
- VS Code Extension: ckolkman.vscode-postgres
- Focus: Database work should primarily be schema-focused
- Deep Changes: Place in
/Database/SQL/startup scripts
User questions → vector similarity matching → find similar Q&A pairs → feed to LLM for clone responses