Skip to content

Replace SQlite with TinyDB#366

Closed
dphuang2 wants to merge 10 commits intomainfrom
dhuang/pro-431-replace-peeweesqlite-with-tinydb
Closed

Replace SQlite with TinyDB#366
dphuang2 wants to merge 10 commits intomainfrom
dhuang/pro-431-replace-peeweesqlite-with-tinydb

Conversation

@dphuang2
Copy link
Copy Markdown
Collaborator

@dphuang2 dphuang2 commented Dec 13, 2025

  • Replaced peewee with tinydb as the default storage backend in pyproject.toml.
  • Added support for both tinydb and sqlite in the evaluation row store and event bus database.
  • Refactored logger and database initialization to accommodate the new storage options.
  • Updated the SQLite-related classes to raise informative errors if peewee is not installed.
  • Ensured backward compatibility for existing SQLite logger and event bus implementations.

name: Pull Request
about: Propose changes to the codebase
title: "Brief description of changes"
labels: ''
assignees: ''


Description

Please include a summary of the change and which issue is fixed or feature is implemented. Please also include relevant motivation and context. List any dependencies that are required for this change.

Fixes # (issue)
Implements # (issue)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Refactoring/Code cleanup
  • Build/CI/CD related changes
  • Other (please describe):

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration.

  • Test A
  • Test B

Test Configuration:

  • Firmware version:
  • Hardware:
  • Toolchain:
  • SDK:

Checklist:

  • My code follows the style guidelines of this project (ran black ., isort ., flake8 .)
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules
  • I have checked my code and corrected any misspellings

Screenshots (if applicable)

If applicable, add screenshots to help showcase your changes.

Additional context

Add any other context about the PR here.


Note

Switches default storage to TinyDB and introduces pluggable backends for dataset logging and the cross-process event bus, with SQLite remaining via optional extra and full backward compatibility.

  • Core storage (Dataset Logger)
    • Introduce EvaluationRowStore ABC with implementations: TinyDBEvaluationRowStore (default) and SqliteEvaluationRowStore (optional peewee).
    • Add factory get_evaluation_row_store(db_path) and _get_default_db_filename(); select backend via EP_STORAGE (tinydb default).
    • Refactor logger to DatasetLoggerAdapter using the store; keep SqliteDatasetLoggerAdapter as an alias; support no-op logger via DISABLE_EP_SQLITE_LOG.
  • Event Bus
    • Add cross-process CrossProcessEventBus using storage-backed EventBusDatabase ABC.
    • Implement TinyDBEventBusDatabase and SqliteEventBusDatabase; add get_event_bus_database(db_path) and _get_default_db_filename(); keep SqliteEventBus as alias.
  • Tests
    • New tests for backend ABCs, TinyDB/SQLite stores, factory selection, cross-process cache invalidation, and logging behavior.
    • Test isolation: per-session .eval_protocol dir to avoid TinyDB file contention.
  • Config/Deps
    • Add deps tinydb, tinyrecord; move SQLite to optional extra sqlite_storage.
    • Update pyproject.toml and type-checker config (include tests); lockfile adjusted.

Written by Cursor Bugbot for commit dbab765. This will update automatically on new commits. Configure here.

- Replaced `peewee` with `tinydb` as the default storage backend in `pyproject.toml`.
- Added support for both `tinydb` and `sqlite` in the evaluation row store and event bus database.
- Refactored logger and database initialization to accommodate the new storage options.
- Updated the SQLite-related classes to raise informative errors if `peewee` is not installed.
- Ensured backward compatibility for existing SQLite logger and event bus implementations.
- Changed default database filenames from "events.db" to "logs.db" and "events.json" to "logs.json" based on storage backend.
- Added cache clearing and table reloading in TinyDBEventBusDatabase to ensure fresh reads of unprocessed events and improve cleanup accuracy.
@dphuang2 dphuang2 changed the title Update dependencies and refactor storage backend handling Replace SQlite with TinyDB Dec 13, 2025
Dylan Huang added 2 commits December 13, 2025 00:21
Dylan Huang added 2 commits December 13, 2025 00:33
…nd event bus database

- Introduced `TestCrossProcessCacheInvalidation` to verify that query caches are properly invalidated when another process modifies the database.
- Added tests to ensure that both evaluation row stores and event bus databases can read fresh data and updates from other processes.
- Implemented scenarios for both reading and writing operations across multiple simulated processes to validate cache behavior.
Dylan Huang added 4 commits December 13, 2025 00:49
…ate removal count and clear cache for fresh reads
- Introduced `test_delete_row_nonexistent` to verify that attempting to delete a non-existent row returns a count of 0 and confirms the store remains empty.
@dphuang2 dphuang2 closed this Dec 13, 2025
@dphuang2 dphuang2 deleted the dhuang/pro-431-replace-peeweesqlite-with-tinydb branch December 13, 2025 09:11
# Temporarily restore the original function
dir_utils.find_eval_protocol_dir = _original_find_eval_protocol_dir
yield _original_find_eval_protocol_dir
# The session fixture will clean up when tests complete
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Test fixture doesn't restore isolated directory function

The restore_original_find_eval_protocol_dir fixture sets dir_utils.find_eval_protocol_dir to the original function at line 69, but after yielding it doesn't restore it back to the isolated version. The comment claims "The session fixture will clean up when tests complete", but this only happens at session end. If test_directory_utils.py tests run mid-session before other tests, those subsequent tests will use the real ~/.eval_protocol directory instead of the isolated temp directory, potentially causing test pollution or corruption of the user's actual data.

Fix in Cursor Fix in Web

def delete_all_rows(self) -> int:
count = len(self._table)
self._table.truncate()
return count
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: Missing cache clear before counting rows in delete_all

The delete_all_rows method calls len(self._table) without first calling self._table.clear_cache(). In multi-process scenarios, the cached table data may be stale, causing the returned count to be inaccurate. Other methods like upsert_row, read_rows, and delete_row correctly call clear_cache() before reading from the table, but this method is inconsistent.

Fix in Cursor Fix in Web

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