Skip to content

Bugfix/orphaned rows cascade delete#1407

Open
AbiramiR-27 wants to merge 2 commits into
AOSSIE-Org:mainfrom
AbiramiR-27:bugfix/orphaned-rows-cascade-delete
Open

Bugfix/orphaned rows cascade delete#1407
AbiramiR-27 wants to merge 2 commits into
AOSSIE-Org:mainfrom
AbiramiR-27:bugfix/orphaned-rows-cascade-delete

Conversation

@AbiramiR-27

@AbiramiR-27 AbiramiR-27 commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Addressed Issues:

Fixes #1405

Screenshots/Recordings:

N/A (This is a database integrity update).

Additional Notes:

Both the image_embeddings and image_semantic_labels tables define foreign key constraints with ON DELETE CASCADE referencing images(id). This configuration is intended to automatically delete embeddings and semantic labels when an image is deleted.

However, both modules established database connections using the private _connect() helper from app.database.images, which does not run PRAGMA foreign_keys = ON;. Because SQLite disables foreign key constraints by default, image deletions left orphaned records in these tables.

Changes made in this PR:

  • Refactored backend/app/database/image_embeddings.py to use the get_db_connection() context manager from app.database.connection.
  • Refactored backend/app/database/semantic_labels.py to use the get_db_connection() context manager.
  • Updated backend/tests/test_image_embeddings.py's _isolated_db test fixture to patch app.database.connection.DATABASE_PATH, redirecting the context manager to the test database and ensuring all tests pass cleanly.
  • Updated backend/tests/test_semantic_labels.py's _isolated_db test fixture to patch app.database.connection.DATABASE_PATH, ensuring the new semantic search tests pass cleanly.

AI Usage Disclosure:

  • This PR does not contain AI-generated code at all.
  • This PR contains AI-generated code. I have read the AI Usage Policy and this PR complies with this policy. I have tested the code locally and I am responsible for it.

I have used the following AI models and tools: Gemini 3.5 Flash .

Checklist

  • My PR addresses a single issue, fixes a single bug or makes a single improvement.
  • My code follows the project's code style and conventions
  • If applicable, I have made corresponding changes or additions to the documentation
  • If applicable, I have made corresponding changes or additions to tests
  • My changes generate no new warnings or errors
  • I have joined the Discord server and I will share a link to this PR with the project maintainers there
  • I have read the Contribution Guidelines
  • Once I submit my PR, CodeRabbit AI will automatically review it and I will address CodeRabbit's comments.
  • I have filled this PR template completely and carefully, and I understand that my PR may be closed without review otherwise.

Summary by CodeRabbit

  • Bug Fixes

    • Improved database connection handling for image embeddings and semantic labels.
    • Ensured database transactions and connections are closed reliably.
    • Improved consistency when retrieving and storing embedding data.
  • Tests

    • Strengthened test database isolation to prevent tests from using configured production database paths.

@coderabbitai

coderabbitai Bot commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Walkthrough

Database helpers now use get_db_connection() context managers, while embedding retrieval is simplified and both test fixtures patch the shared database path for isolated SQLite tests.

Changes

Database connection migration

Layer / File(s) Summary
Image embedding database access
backend/app/database/image_embeddings.py, backend/tests/test_image_embeddings.py
Image embedding table setup, writes, reads, scoring queries, and counting now use context-managed connections; retrieval builds NumPy matrices directly, and tests patch the temporary database path.
Semantic-label database access
backend/app/database/semantic_labels.py, backend/tests/test_semantic_labels.py
Semantic-label database helpers use context-managed connections instead of manual commit and close handling, while tests patch the temporary database path.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

Suggested labels: Python

Suggested reviewers: rohan-pandeyy

Poem

I’m a rabbit with connections neat,
Contexts close them, quick and sweet.
Embeddings stack in arrays bright,
Labels sync through SQLite light.
Test paths hop to temp-file lands—
Clean databases, tidy hands! 🐇

🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly matches the bugfix and the cascade-delete/orphaned-row problem being addressed.
Linked Issues check ✅ Passed The PR refactors both affected modules to use get_db_connection and updates tests for isolated databases, matching #1405.
Out of Scope Changes check ✅ Passed The test fixture changes are directly related to the database connection refactor and do not appear out of scope.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@backend/tests/test_image_embeddings.py`:
- Line 40: The cascade test should execute DELETE through get_db_connection()
rather than _connect(), and the application image-deletion path should use
get_db_connection() as well. Update the relevant test delete block and
production image-deletion method, preserving the existing cascade behavior and
connection lifecycle.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: b4d90432-c4fe-4078-8c32-3755a3d450ef

📥 Commits

Reviewing files that changed from the base of the PR and between 859ac7a and c14ec24.

📒 Files selected for processing (4)
  • backend/app/database/image_embeddings.py
  • backend/app/database/semantic_labels.py
  • backend/tests/test_image_embeddings.py
  • backend/tests/test_semantic_labels.py

monkeypatch.setattr(images_module, "DATABASE_PATH", db_path)
monkeypatch.setattr(folders_module, "DATABASE_PATH", db_path)
monkeypatch.setattr(yolo_mapping_module, "DATABASE_PATH", db_path)
monkeypatch.setattr(connection_module, "DATABASE_PATH", db_path)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🗄️ Data Integrity & Integration | 🟠 Major | 🏗️ Heavy lift

Exercise the cascade through an FK-enabled delete connection.

The cascade test still deletes via _connect() (Lines 167-170), which the PR identifies as foreign-key-disabled. SQLite applies cascades on the connection executing DELETE, so this test cannot validate the intended behavior—and the production delete path remains unaffected if it also uses _connect(). Use get_db_connection() for the delete and migrate the application image-deletion connection as well.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@backend/tests/test_image_embeddings.py` at line 40, The cascade test should
execute DELETE through get_db_connection() rather than _connect(), and the
application image-deletion path should use get_db_connection() as well. Update
the relevant test delete block and production image-deletion method, preserving
the existing cascade behavior and connection lifecycle.

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: Orphaned Rows in image_embeddings and semantic_labels on Image Deletion

1 participant