feat(state): add MongoBackend and URI-based backend factory#11
Open
himewel wants to merge 2 commits into
Open
Conversation
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 <[email protected]>
Co-authored-by: Cursor <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
MongoBackendinq2google/state/mongo.pystoring sessions across three MongoDB collections (sessions,items,batches) that mirror theJsonFileBackenddirectory layout, enabling per-collection visibility into what is runningbuild_backend(cfg)factory inq2google/state/__init__.pythat selects the backend from a singleQ2GOOGLE_STATE_URIenv var — URI scheme routes to the right implementation; future backends need zero config changespymongois an optional extra (pip install q2google[mongo]);import q2google.statenever fails without it — lazy import inside the factory raises a clear error only when amongodb://URI is actually requestedChanges
q2google/state/mongo.py— newMongoBackendwithload/save, lazy index creation, andsession_idinjected into item/batch documents for per-collection queriesq2google/state/__init__.py—build_backend(cfg)factory +MongoBackendre-exportq2google/config.py—state_uri: str | Nonefield (Q2GOOGLE_STATE_URI);state_dirunchangedq2google/cli/_runner.py— acceptsstate_backend: SyncStateBackendinstead ofstate_dir: Pathq2google/cli/_app.py— usesbuild_backend(cfg); explicit--state-dirflag still overrides to filesystempyproject.toml—[mongo]optional extra (pymongo>=4),mongomock>=4in[dev]tests/conftest.py— parametrizedbackendfixture (json + mongo via mongomock); mongo variant auto-skips when dependencies are absenttests/test_state_backends.py— 5 protocol-compliance tests running for all backends (10 cases total)tests/test_sync.py— trimmed to filesystem-specific tests (legacy format, directory layout)docs/backends.md— new guide covering Filesystem, MongoDB, and Custom backends with collection schemas and install instructionsdocs/configuration.md,docs/getting-started.md,docs/ARCHITECTURE.md,docs/api/state.md,mkdocs.yml— updated to reference new backendTest plan
pip install q2googleinstalls without pymongo — filesystem backend works as beforepip install q2google[mongo]installs pymongo —Q2GOOGLE_STATE_URI=mongodb://...activates MongoBackenduv run pytest tests/ -vv— all 14 tests pass (12 backend-parametrized + 2 filesystem-specific)ValueErrorwith a helpful messagemongodb://URI without pymongo raisesImportErrorpointing topip install q2google[mongo]Made with Cursor