feat: migrate backend persistence from TinyDB to MongoDB Atlas

- Implement lazy MongoDB client initialization in backend/db/mongo_client.py.
- Create Gunicorn configuration to ensure MongoDB client is initialized per worker.
- Refactor database access layer to support MongoDB with a new MongoLockedTable adapter.
- Add migration script to transfer existing TinyDB data to MongoDB, preserving idempotency.
- Update tracking event handling to ensure deterministic ordering with a monotonic sequence.
- Modify tests to use mongomock for MongoDB integration and ensure existing tests pass.
- Add integration test script to run tests against a local MongoDB Docker container.
- Document environment variables and migration process in specs/feat-database-migration.md.
This commit is contained in:
2026-07-28 01:10:52 -04:00
parent c9c1fa18a2
commit da7ed41938
17 changed files with 1364 additions and 62 deletions
+17
View File
@@ -0,0 +1,17 @@
"""Gunicorn configuration for the chore/reward Flask backend.
This file is automatically loaded by Gunicorn when it is started from the
backend directory. It ensures each worker process creates its own MongoDB
client after forking, avoiding shared socket/file-descriptor issues.
"""
def post_fork(server, worker):
"""Reinitialize the MongoDB client in each worker process after forking."""
try:
from db.mongo_client import init_mongo_client
init_mongo_client()
except Exception:
# If MongoDB is not configured (USE_MONGODB=false), there is no client
# to initialize; ignore the error silently.
pass