Skip to content

feat(state): MongoDB state backend, CLI wiring, and backend tests#11

Merged
himewel merged 5 commits into
mainfrom
feat/mongodb-state-backend
May 24, 2026
Merged

feat(state): MongoDB state backend, CLI wiring, and backend tests#11
himewel merged 5 commits into
mainfrom
feat/mongodb-state-backend

Conversation

@himewel
Copy link
Copy Markdown
Owner

@himewel himewel commented May 18, 2026

Summary

  • Add MongoBackend in q2google/state/mongo.py with three collections (sessions, items, batches) mirroring the JsonFileBackend layout for per-document visibility and resume checkpoints.
  • Introduce build_backend(cfg) in q2google/state/__init__.pyQ2GOOGLE_STATE_URI scheme selects the backend; filesystem fallback uses Q2GOOGLE_STATE_DIR when the URI is unset.
  • Wire the sync CLI through build_backend and sync_settings() so env/.env settings (including Q2GOOGLE_STATE_URI and Q2GOOGLE_LOG_LEVEL) apply consistently; pass one backend instance through the runner for pre/post-run inspection.
  • pymongo stays optional (pip install q2google[mongo]); lazy import raises a clear error only when a mongodb:// URI is requested without the extra installed.

Rebased on latest main (unified GoPro settings via Q2GoogleSettings, gopro-api 0.0.9).

Changes

  • q2google/state/mongo.pyMongoBackend with load / save, lazy indexes, session_id on item/batch documents
  • q2google/state/__init__.pybuild_backend(cfg) factory
  • q2google/config.pystate_uri: str | None (Q2GOOGLE_STATE_URI)
  • q2google/cli/_settings.py — merge CLI --state-dir into settings before build_backend
  • q2google/cli/_app.py, q2google/cli/_runner.py — use build_backend(sync_cfg); runner accepts state_backend instead of constructing JsonFileBackend locally
  • q2google/cli/_logging.py — honor Q2GOOGLE_LOG_LEVEL when --log-level / -v are omitted
  • pyproject.toml[mongo] optional extra; mongomock / pymongo in dev group
  • tests/conftest.py, tests/test_state_backends.py — parametrized json + mongo (mongomock) protocol tests (12 cases)
  • tests/test_sync.py — removed; filesystem-specific coverage lives in backend parametrized tests
  • .github/workflows/ci.yml — dedicated test job running uv run task test
  • docker-compose.yaml — local MongoDB 7 for manual testing
  • docs/backends.md and related docs — backend guide, configuration, and architecture updates

Test plan

  • pip install q2google — no pymongo; filesystem backend unchanged when Q2GOOGLE_STATE_URI is unset
  • pip install q2google[mongo] + Q2GOOGLE_STATE_URI=mongodb://localhost:27017/q2google — MongoBackend used by CLI and library
  • docker compose up -d then sync with Mongo URI — state persists across restarts
  • uv run pytest tests/ -vv — 12 parametrized backend tests pass
  • Unknown URI scheme → ValueError with supported schemes listed
  • mongodb:// without pymongo → ImportError pointing to pip install q2google[mongo]
  • CI green on PR (lint matrix, docs, test job)

Add a MongoDB storage backend for session state using three collections
(sessions, items, batches) that mirror the JsonFileBackend directory layout.
Introduce a URI-based build_backend() factory so future backends can be
added with zero config changes — the URI scheme selects the implementation.

- Add q2google/state/mongo.py with MongoBackend (pymongo optional extra)
- Add build_backend(cfg) factory to q2google/state/__init__.py with lazy
  MongoBackend import and clear error when pymongo is not installed
- Add Q2GOOGLE_STATE_URI setting to Q2GoogleSettings; state_dir unchanged
- Refactor _runner.py to accept SyncStateBackend instead of state_dir Path
- Update _app.py to use build_backend(); --state-dir flag still overrides
- Add parametrized backend fixture in tests/conftest.py (json + mongo via
  mongomock); extract protocol-compliance tests to test_state_backends.py
- Add docs/backends.md guide; update configuration.md, getting-started.md,
  ARCHITECTURE.md, api/state.md, and mkdocs.yml nav

Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@github-actions github-actions Bot added feature and removed feature labels May 18, 2026
himewel and others added 2 commits May 24, 2026 18:43
Co-authored-by: Cursor <cursoragent@cursor.com>
Use sync_settings for --state-dir overrides, honor Q2GOOGLE_LOG_LEVEL,
and pass a single state backend through the runner. Add CI test job and
docker-compose for local MongoDB.

Co-authored-by: Cursor <cursoragent@cursor.com>
@github-actions github-actions Bot added feature and removed feature labels May 24, 2026
@himewel himewel changed the title feat(state): add MongoBackend and URI-based backend factory feat(state): MongoDB state backend, CLI wiring, and backend tests May 24, 2026
Co-authored-by: Cursor <cursoragent@cursor.com>
@github-actions github-actions Bot added feature and removed feature labels May 24, 2026
@himewel himewel merged commit 448eb7e into main May 24, 2026
6 checks passed
@himewel himewel deleted the feat/mongodb-state-backend branch May 24, 2026 21:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant