Skip to content

Migrate from SQLite to PostgreSQL - #27

Merged
surrealwolf merged 18 commits into
mainfrom
feature/postgresql-migration
Jan 12, 2026
Merged

surrealwolf merged 18 commits into
mainfrom
feature/postgresql-migration

Conversation

@surrealwolf

Copy link
Copy Markdown
Collaborator

PostgreSQL Migration

This PR migrates High Command API from SQLite to PostgreSQL using CloudNativePG operator.

Changes

Code Migration

  • database.py: Complete rewrite for PostgreSQL (psycopg2)
  • SQL syntax updates: SERIAL, TIMESTAMP, JSONB, ON CONFLICT
  • Connection string-based instead of file-based
  • Updated config.py, main.py, app_readonly.py
  • Added psycopg2-binary==2.9.9 to requirements.txt

Infrastructure (separate PR/manifests)

  • Deploy 3-node PostgreSQL cluster (CloudNativePG)
  • Single poller deployment (removed blue/green separation)
  • Updated API deployments to use PostgreSQL
  • Removed init containers and PVC mounts

Benefits

  • ✅ Native concurrent read/write
  • ✅ No NFS permissions issues
  • ✅ Better performance
  • ✅ High availability
  • ✅ ACID compliance

Testing

  • PostgreSQL cluster deployed and healthy
  • Connection tested successfully
  • Code migration complete

Next Steps

  • Rebuild Docker image with new code
  • Deploy and test end-to-end

- Replace sqlite3 with psycopg2 for PostgreSQL support
- Update Database class to use connection strings instead of file paths
- Convert SQL syntax: INTEGER AUTOINCREMENT -> SERIAL, DATETIME -> TIMESTAMP
- Use JSONB for data columns (better PostgreSQL support)
- Replace ? placeholders with %s (PostgreSQL format)
- Replace INSERT OR REPLACE with INSERT ... ON CONFLICT ... DO UPDATE
- Update config.py to use DATABASE_URL environment variable
- Update main.py and app_readonly.py to pass connection string
- Add psycopg2-binary==2.9.9 to requirements.txt
- Remove SQLite-specific file path handling

Benefits:
- Native concurrent read/write support
- Better performance for multiple connections
- No NFS permissions issues
- Proper ACID compliance
- High availability with PostgreSQL cluster
@surrealwolf
surrealwolf force-pushed the feature/postgresql-migration branch from 2dfea9c to db16513 Compare January 12, 2026 02:15
- Move Config import to top of file in app_readonly.py (E402)
- Remove unused RealDictCursor import from database.py (F401)
- Remove unused urlparse import from database.py (F401)
- Remove f-string prefix where no placeholders used (F541)
- Change database_url: str = None to database_url: Optional[str] = None
- Fixes mypy type checking error (PEP 484 no_implicit_optional)
- Add psycopg2-binary>=2.9.9 to dependencies list
- Required for PostgreSQL database support
- Fixes ModuleNotFoundError in CI tests
- Add Config import at top of file
- Initialize Database with connection string from Config
- Fixes test failures where app.py imports Database() without connection string
- Add DATABASE_URL env var to pytest step
- Required for app.py and app_readonly.py imports
- Uses test database connection string for CI
- Don't fail if database connection fails during __init__
- _init_db() now handles OperationalError gracefully
- Schema will be created on first actual database operation
- Allows tests to import modules without requiring database connection
- Update Database() calls to use database_url instead of db_path
- Fix test_init_creates_file to work with PostgreSQL connection strings
- Fix test_init_creates_tables to handle PostgreSQL gracefully
- Update config tests for PostgreSQL defaults
- Tests can now import without errors (connection errors handled)
- Remove unused sqlite3 import
- Remove unused tempfile import
- Fix unused database_url variable in temp_db fixture
…base.py from coverage

- Remove SQLite-specific code (tempfile, sqlite3)
- Update temp_db fixture to use PostgreSQL connection string
- Fix test that was checking SQLite db_path
- Temporarily exclude database.py from coverage (requires PostgreSQL in CI)
- Created conftest.py with mock_psycopg2 and temp_db fixtures
- Updated all test methods to use database mocks
- Tests now work without PostgreSQL connection
- Removed database.py from coverage exclusion (now testable)
- All database operations are mocked for CI compatibility
- Rewrote test_database.py with proper mock setup
- Fixed test_database_coverage.py to use mocks
- All tests now properly mock PostgreSQL connections
- Tests work without actual database connection
- Fixed syntax errors and indentation issues
@surrealwolf
surrealwolf merged commit e0fcf91 into main Jan 12, 2026
8 checks passed
@surrealwolf
surrealwolf deleted the feature/postgresql-migration branch January 12, 2026 04:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant