Skip to content

Add offline SQLite backend#175

Open
cfuselli wants to merge 37 commits intomasterfrom
sqlite-offline
Open

Add offline SQLite backend#175
cfuselli wants to merge 37 commits intomasterfrom
sqlite-offline

Conversation

@cfuselli
Copy link
Member

This pull request introduces offline (SQLite-based) backend support to utilix, allowing core database and GridFS operations to work seamlessly when disconnected from the main MongoDB service. The main changes involve integrating the new sqlite_backend and updating the relevant classes and methods to use it when configured.

Offline backend integration:

  • Added imports for sqlite_backend and its key classes/functions (OfflineGridFS, OfflineSQLiteCollection, SQLiteConfig, _load_sqlite_config) in utilix/__init__.py, utilix/mongo_storage.py, and utilix/rundb.py to enable offline support throughout the package. [1] [2] [3]

GridFS (file storage) offline support:

  • Updated the GridFsBase class in utilix/mongo_storage.py to initialize and use OfflineGridFS when offline mode is detected, including offline-safe overrides for file listing, config existence checks, and file downloads. This ensures file storage and retrieval continue to work in offline scenarios. [1] [2]

RunDB (database) offline support:

  • Modified utilix/rundb.py to provide an _sqlite_collection function and update xent_collection to use the offline SQLite backend if enabled, allowing database queries to fallback to the local SQLite database when offline.

General improvements and error handling:

  • Added robust error handling for configuration loading and type checking for cache directories, improving reliability and user feedback when initializing offline backends.

Consistency and code reuse:

  • Ensured consistent use of the offline backend throughout the codebase, with shared logic for configuration loading and backend selection in both file and database operations. [1] [2] [3]

cfuselli and others added 30 commits January 19, 2026 03:27
…ne() logic

- Fix AttributeError: replace sqlite_config.sqlite_path with rundb_sqlite_path
  in rundb.py and mongo_storage.py (3 locations)
- Fix NameError in OfflineSQLiteCollection.find_one(): add proper _id handling
  and default query case
- These bugs would cause immediate crashes when offline mode is activated

Fixes ensure offline SQLite backend actually works when both rundb and xedocs
SQLite files are present.
Tests cover:
- SQLiteConfig loading and activation logic
- OfflineGridFS file operations (list, download)
- OfflineSQLiteCollection queries (find_one, find, count_documents)
- xent_collection() fallback behavior (SQLite vs MongoDB)
- Edge cases and error handling

All 13 tests pass successfully.
- Added detailed section in README.md covering setup, usage, and troubleshooting
- Documented environment variables and their purposes
- Added examples for generating SQLite dumps and using offline mode
- Included limitations and performance considerations
- Enhanced module docstring in sqlite_backend.py with usage examples
)

* Fix critical bugs: correct sqlite_path references and complete find_one() logic

- Fix AttributeError: replace sqlite_config.sqlite_path with rundb_sqlite_path
  in rundb.py and mongo_storage.py (3 locations)
- Fix NameError in OfflineSQLiteCollection.find_one(): add proper _id handling
  and default query case
- These bugs would cause immediate crashes when offline mode is activated

Fixes ensure offline SQLite backend actually works when both rundb and xedocs
SQLite files are present.

* Apply black formatting to sqlite_backend.py

* Add comprehensive tests for offline SQLite backend

Tests cover:
- SQLiteConfig loading and activation logic
- OfflineGridFS file operations (list, download)
- OfflineSQLiteCollection queries (find_one, find, count_documents)
- xent_collection() fallback behavior (SQLite vs MongoDB)
- Edge cases and error handling

All 13 tests pass successfully.

* Add comprehensive documentation for offline SQLite backend

- Added detailed section in README.md covering setup, usage, and troubleshooting
- Documented environment variables and their purposes
- Added examples for generating SQLite dumps and using offline mode
- Included limitations and performance considerations
- Enhanced module docstring in sqlite_backend.py with usage examples

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix pre-commit issues: syntax error and flake8 violations

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix flake8 issues in test file: line length and unused variables

* Fix mypy type errors and flake8 whitespace issues

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Fix pre-existing flake8 and mypy issues in mongo_to_sqlite.py

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Add noqa comments for unavoidable black/flake8 conflicts

* Move noqa comment to correct line in test file

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Rewrite SQL as single-line strings to avoid black/flake8 conflict

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Allow long first line in SQL query with noqa

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* Remove accidentally committed .DS_Store file

* Add .DS_Store to gitignore

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
@cfuselli cfuselli marked this pull request as ready for review February 12, 2026 13:18
@cfuselli
Copy link
Member Author

🎉 PR Ready for Review!

This PR has been thoroughly reviewed and enhanced with:

Critical Bug Fixes Applied

  • Fixed 4 critical bugs that would cause crashes:
    • Corrected sqlite_config.sqlite_pathsqlite_config.rundb_sqlite_path in 3 locations
    • Fixed undefined row variable in find_one() method
    • Completed missing _id query logic

🧪 Comprehensive Test Suite Added

  • Added tests/test_offline_sqlite.py with 13 comprehensive unit tests
  • All tests passing (100% ✅)
  • Tests cover:
    • SQLiteConfig loading and validation
    • OfflineGridFS file operations
    • OfflineSQLiteCollection queries (find, find_one, count)
    • Integration with xent_collection()
    • Fallback to MongoDB when offline unavailable

📚 Complete Documentation Added

  • 150+ lines added to README.md covering:
    • Setup instructions
    • Usage examples with code snippets
    • Environment variables reference
    • Troubleshooting guide
    • Performance considerations
  • Enhanced module docstrings in sqlite_backend.py

🎨 Code Quality

  • All mypy type errors resolved
  • All flake8 linting issues fixed
  • Black formatting applied
  • Pre-commit hooks passing

The offline SQLite backend is now production-ready with proper testing and documentation! 🚀

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