Skip to content

Conversation

@V4Vikaskumar
Copy link

@V4Vikaskumar V4Vikaskumar commented Dec 27, 2025

Summary

This PR improves the robustness and reliability of the SQLite database connection by configuring important connection settings that were previously missing. These changes help prevent avoidable runtime issues under concurrent access and improve overall developer experience.


What changed

The following improvements were made in get_db_connection():

  • Added a connection timeout to reduce database is locked errors under concurrent access
  • Enabled WAL (Write-Ahead Logging) mode to improve read/write concurrency and performance
  • Configured row_factory to return sqlite3.Row objects for safer and more readable query results

Why this change is important

SQLite is sensitive to concurrent access patterns. Without these settings:

  • Write operations can fail prematurely due to database locks
  • Default journal mode limits concurrent reads and writes
  • Tuple-based query results increase the risk of index-based access bugs

Making these configurations explicit hardens the database layer and improves stability as the application scales.


Testing

  • Verified WAL mode using PRAGMA journal_mode
  • Confirmed named-column access via sqlite3.Row
  • Manually tested database access under concurrent operations

Checklist

  • Added SQLite connection timeout
  • Enabled WAL mode
  • Configured row_factory
  • Verified changes locally
  • No breaking changes introduced

Related Issue

Fixes #863

Summary by CodeRabbit

  • Chores
    • Enhanced database connection stability with improved timeout handling
    • Optimized database performance and concurrency support through advanced journaling techniques

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Dec 27, 2025

📝 Walkthrough

Walkthrough

Database connection configuration is enhanced to improve concurrency and error resilience. A 30-second timeout is added to the connection, row factory is configured to return sqlite3.Row objects for easier column access, and WAL (Write-Ahead Logging) journaling mode is enabled via PRAGMA statement.

Changes

Cohort / File(s) Summary
Database Connection Configuration
backend/app/database/connection.py
Added 30-second timeout to sqlite3.connect() call; configured row factory to return sqlite3.Row objects; enabled WAL journaling mode via PRAGMA journal_mode = WAL; for improved concurrency and performance.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 Hops through the database with glee,
WAL mode sets connections free,
Timeouts prevent the endless wait,
Row factories seal our data's fate!
Performance bounces, concurrency springs,
See what this little fix brings!

Pre-merge checks and finishing touches

❌ Failed checks (2 warnings)
Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning The PR objectives show a mismatch: the code changes address SQLite database configuration (#856 is about fixing the Home navbar button), not the linked issue's requirements. Verify the correct linked issue. The current changes target database stability, not navbar navigation. Either update the linked issue or ensure the PR addresses the correct issue.
Out of Scope Changes check ⚠️ Warning The database connection changes are out of scope for issue #856, which concerns fixing navbar Home button navigation on the landing page. Link the PR to the correct issue addressing SQLite improvements, or move the navbar fix to a separate PR focused on that functionality.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main changes: adding timeout, WAL mode, and row_factory to improve SQLite connection stability.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
backend/app/database/connection.py (1)

28-29: Consider WAL mode persistence and deployment implications.

WAL mode is persistent—once enabled, it remains active even after the connection closes and affects all future connections to this database. Consider:

  • Filesystem compatibility: WAL requires a filesystem that supports shared memory; it won't work on network filesystems (e.g., NFS).
  • Additional files: WAL creates -wal and -shm files alongside the database.
  • Backup implications: Backups should use PRAGMA wal_checkpoint or backup tools that handle WAL.

Ensure your deployment environment and backup strategy accommodate WAL mode.

📜 Review details

Configuration used: defaults

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 81286fa and ed07ee6.

📒 Files selected for processing (1)
  • backend/app/database/connection.py
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: Hemil36
Repo: AOSSIE-Org/PictoPy PR: 570
File: backend/app/database/connection.py:16-24
Timestamp: 2025-10-31T17:00:50.132Z
Learning: In PictoPy backend, the user prefers not to use database connection retry logic or extended busy timeouts in the centralized get_db_connection() context manager, even though the app has concurrent access patterns via ProcessPoolExecutor and FastAPI.
🔇 Additional comments (3)
backend/app/database/connection.py (3)

1-38: Critical mismatch between PR changes and linked issue.

The PR description states this targets issue #856, which is about fixing a "Home" button navigation bug in the navbar. However, all changes in this PR are database connection improvements with no UI or navigation code. This suggests either:

  • The wrong issue number is linked, or
  • This PR should be split and the database changes should target a different issue

Please verify the correct issue number and update the PR description accordingly.


16-16: Timeout contradicts previous preference.

The 30-second timeout contradicts a previous preference documented in the learnings, where you indicated not wanting extended busy timeouts in this connection manager, even with concurrent access patterns.

Has your approach changed, or should this be reconsidered?

Based on learnings from PR #570 on the same file.


18-19: The row_factory change is safe for this codebase and will not cause breaking changes.

While tuple unpacking patterns exist in backend/app/database/face_clusters.py and backend/app/database/faces.py (e.g., cluster_id, cluster_name, face_count, face_image_base64 = row), these files use direct sqlite3.connect() rather than get_db_connection(), so they are unaffected by the row_factory change.

Code that actually uses get_db_connection() (like backend/app/database/albums.py and backend/app/utils/face_clusters.py) consistently uses compatible patterns:

  • Integer indexing: row[0]
  • List comprehensions: [row[0] for row in ...]

No tuple unpacking occurs in files using get_db_connection(), so this change introduces no breaking changes.

Likely an incorrect or invalid review comment.

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.

BUG: Improve SQLite connection reliability with timeout, WAL mode, and row_factory

2 participants