Upgrades & Migrations
Relevant source files
The following files were used as context for generating this wiki page:
The Upgrades & Migrations system in OpenAlgo provides a standardized framework for evolving the application's database schemas, environmental configurations, and core dependencies. It ensures that both fresh installations and existing deployments can transition between versions without data loss. The system is designed around idempotency, allowing migration scripts to be executed multiple times safely by checking for existing states before applying changes.
This module handles the lifecycle of multiple database engines—including SQLite for transactional data and DuckDB for historical market data—while also managing security hardening for .env configurations and systemd service updates in production environments.
Sources: upgrade/README.md:5-15, CLAUDE.md:143-155, install/update.sh:22-30
Migration Architecture
The migration system follows a centralized execution model. While individual scripts exist for specific features (e.g., Telegram bot, Sandbox mode, Performance indexes), the primary entry point for users is migrate_all.py. This script orchestrates the execution of all registered migrations in a defined sequence.
Core Execution Flow
The update.sh script manages the high-level orchestration for server-side deployments, including service suspension, data backup, and the eventual call to the Python-based migration suite.
Migration Principles
All migration scripts within the upgrade/ directory must adhere to the following invariants:
- Idempotency: Scripts must check for the presence of columns or tables (e.g., using
PRAGMA table_info) and exit quietly if the change is already applied. - Safety: Migrations should not clobber user-customized values.
- Database Isolation: OpenAlgo uses six isolated databases (
openalgo.db,logs.db,latency.db,health.db,sandbox.db, andhistorify.duckdb). Migrations must target the correct engine factory. - Status Reporting: Scripts should support a
--statusflag to report the current state without making changes.
Sources: CLAUDE.md:107-111, CLAUDE.md:143-157, upgrade/migrate_historify.py:165-175
Database Initialization and Recovery
The system provides specialized tools for recovery when standard migrations fail or when environment mismatches occur.
Diagnostic Tools
| Script | Purpose |
|---|---|
init_db.py | Foreground creation of missing tables; verifies if stored credentials decrypt with current API_KEY_PEPPER and FERNET_SALT. |
reset_admin_password.py | Command-line tool to reset credentials when locked out; uses current .env pepper for hashing. |
migrate_historify.py | Initializes the DuckDB database for historical OHLCV data. |
Sources: upgrade/README.md:20-40, upgrade/migrate_historify.py:48-60
DuckDB Setup (Historify)
The migrate_historify.py script specifically initializes the high-performance DuckDB environment used for /trading and /tools.
Sources: upgrade/migrate_historify.py:73-115
Component Migrations
Master Contract Stats
Migration migrate_master_contract_stats.py introduces smart download tracking for broker contract masters. It adds columns to the master_contract_status table to track download duration and exchange-specific statistics, preventing redundant daily downloads if completed after 8 AM IST.
Sources: upgrade/migrate_master_contract_stats.py:53-70
Security Hardening
The update.sh script performs automated security migrations for existing installations:
- Permission Tightening: Forces
.envfile permissions to600(owner-only). - Proxy Configuration: Automatically adds
TRUST_PROXY_HEADERSto.envbased on Nginx detection to prevent IP spoofing or incorrect IP logging. - Key Rotation: Generates
APP_KEYandAPI_KEY_PEPPERif missing from sample templates.
Sources: install/update.sh:267-300
Operational Procedures
Server Update Command
For production servers, the recommended path is utilizing the uv package manager within the deployment directory:
cd upgrade
uv run migrate_all.py
Sources: upgrade/README.md:9-11
Backup Locations
During the upgrade process, update.sh creates a timestamped backup in db/backup_YYYYMMDD_HHMMSS/ containing:
openalgo.db(Main transactional data)logs.db(Traffic and error logs)sandbox.db(Virtual trading data)historify.duckdb(Historical market data)
Sources: install/update.sh:205-230
Troubleshooting Migrations
If a migration fails due to database locks (common in SQLite/DuckDB one-writer environments), the system recommends stopping the main OpenAlgo service first to release file descriptors.
Sources: upgrade/README.md:36-39, CLAUDE.md:133-138
Summary
The OpenAlgo upgrade framework provides a robust mechanism for maintaining system integrity across versions. By combining shell-based infrastructure management in update.sh with Python-based idempotent logic in the upgrade/ suite, it enables automated schema evolution while maintaining strict security standards for configuration and credential management. Significant features like Sandbox mode and Historify rely on these scripts to ensure required data structures are available before the application logic executes.