Skip to content

Conversation

@IngmarStein
Copy link
Member

Previously, updating a single field on a device or app required loading the entire user JSON object, modifying it in memory, and writing the whole object back to the database. This approach was inefficient and created potential race conditions where concurrent updates could overwrite each other.

This commit refactors the update logic to use SQLite's json_set function for atomic and granular field updates. New helper functions, update_device_field and update_app_field, have been introduced to encapsulate this logic.

All relevant routes in the manager and the websocket receiver have been updated to use these new functions, ensuring that changes are committed atomically and safely.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @IngmarStein, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly refactors how device and app data fields are updated in the database. By transitioning from a full object load-modify-save pattern to atomic field updates using SQLite's json_set function, the system gains improved efficiency and eliminates potential race conditions. This change ensures that individual field modifications are handled safely and consistently across all relevant application components, from API routes to websocket interactions.

Highlights

  • Atomic JSON Updates: The core database update mechanism has been refactored to use SQLite's json_set function for atomic updates to individual device and app fields, replacing the previous less efficient load-modify-save approach for entire JSON objects.
  • New Helper Functions: Three new helper functions, _update_json_field, update_device_field, and update_app_field, have been introduced in tronbyt_server/db.py to encapsulate and standardize the new atomic update logic.
  • Refactored save_app Function: The save_app function has been updated to utilize json_set for atomically saving app data, which improves reliability and consistency.
  • Widespread Integration: Various manager routes (e.g., next_app_logic, update_brightness, toggle_pin) and the websocket receiver have been updated to integrate these new atomic update functions, ensuring consistent and safe data modifications across the application.
  • Improved Error Handling: Enhanced error handling with sqlite3.Error and db.rollback() has been added to database operations, increasing the robustness and data integrity of the application.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great refactoring that moves from inefficient read-modify-write updates of the entire user JSON object to atomic and granular updates using SQLite's json_set function. The introduction of update_device_field and update_app_field helpers centralizes the new update logic effectively.

My review focuses on a couple of areas where the new implementation introduces repetitive code patterns. I've suggested refactoring these patterns into helpers or context managers to improve code maintainability and reduce boilerplate. Specifically, the transaction handling in tronbyt_server/routers/manager.py and the field updates in tronbyt_server/routers/websockets.py could be made more concise.

Overall, this is a solid improvement for database efficiency and safety. The changes are well-aligned with the pull request's goal.

@IngmarStein IngmarStein force-pushed the sqlite_json branch 2 times, most recently from 72543fc to 71ae55b Compare November 10, 2025 13:00
@IngmarStein IngmarStein requested a review from tavdog November 10, 2025 13:40
@IngmarStein IngmarStein force-pushed the sqlite_json branch 2 times, most recently from 6f87a7f to 00a957d Compare November 10, 2025 19:29
@IngmarStein
Copy link
Member Author

/gemini review

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This is an excellent pull request that significantly improves the database update logic. By replacing the read-modify-write pattern with atomic json_set operations, you've effectively eliminated potential race conditions and improved performance. The introduction of the db_transaction context manager and the helper functions for field updates are great additions that improve code clarity and maintainability. The refactoring of get_device_by_id and get_user_by_device_id to use json_each is also a fantastic performance enhancement. I have one minor suggestion to ensure consistency by using the new context manager in one more place. Overall, this is a high-quality contribution.

Previously, updating a single field on a device or app required loading the entire user JSON object, modifying it in memory, and writing the whole object back to the database. This approach was inefficient and created potential race conditions where concurrent updates could overwrite each other.

This commit refactors the update logic to use SQLite's `json_set` function for atomic and granular field updates. New helper functions, `update_device_field` and `update_app_field`, have been introduced to encapsulate this logic.

All relevant routes in the manager and the websocket receiver have been updated to use these new functions, ensuring that changes are committed atomically and safely.
@IngmarStein IngmarStein merged commit b586ec0 into tronbyt:main Nov 10, 2025
1 check failed
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