Skip to content

refactor(api): use caller session for dataset-deleted cleanup - #45841

Open
ojasarora77 wants to merge 2 commits into
langgenius:mainfrom
ojasarora77:refactor/clean-dataset-deleted-caller-session
Open

ojasarora77 wants to merge 2 commits into
langgenius:mainfrom
ojasarora77:refactor/clean-dataset-deleted-caller-session

Conversation

@ojasarora77

Copy link
Copy Markdown
Contributor

Summary

  • Part of [Chore/Refactor] use sqlalchemy engine #45584: migrate clean_when_dataset_deleted off the global db.session.
  • Require the caller-provided session and pass it from DatasetService.delete_dataset (already session-scoped).
  • Update send-site assertions and add unit coverage for the required session / enqueue / skip paths.

Test plan

  • uv run pytest tests/unit_tests/events/event_handlers/test_clean_when_dataset_deleted.py -q
  • uv run pyrefly check on the changed handler + unit test
  • Delete a dataset with indexing technique + chunk structure and confirm cleanup still enqueues
  • Delete a dataset without indexing technique and confirm cleanup is skipped

Pass the delete transaction into clean_when_dataset_deleted instead of opening the global db.session for doc-form lookup.
@github-actions github-actions Bot added the size:M This PR changes 30-99 lines, ignoring generated files. label Nov 8, 2026
@github-actions

github-actions Bot commented Nov 8, 2026 •

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 87.84% 87.84% +0.00%
Strict coverage 87.50% 87.50% +0.00%
Typed symbols 96,950 96,956 +6
Untyped symbols 13,477 13,477 0
Modules 4666 4667 +1

@codecov

codecov Bot commented Nov 8, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.45%. Comparing base (55b4bd7) to head (258e62d).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #45841      +/-   ##
==========================================
+ Coverage   89.72%   90.45%   +0.73%     
==========================================
  Files        6562     6569       +7     
  Lines      409450   409745     +295     
  Branches    82067    82077      +10     
==========================================
+ Hits       367393   370650    +3257     
+ Misses      35602    32709    -2893     
+ Partials     6455     6386      -69     
Flag Coverage Δ
api 90.16% <100.00%> (+1.20%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@asukaminato0721 asukaminato0721 self-assigned this Nov 16, 2026

@asukaminato0721 asukaminato0721 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.

2 inline suggestions at 1cbea62f62, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong.

dify-codereview

with patch("events.event_handlers.clean_when_dataset_deleted.clean_dataset_task.delay") as delay:
handle(dataset, session=sqlite_session)

delay.assert_called_once_with(

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.

Please don't pin the exact calls here (assert_called_once_with and friends): that breaks on harmless refactors and only proves the mock was hit. Assert the general behaviour instead: what is returned, persisted, raised or emitted.

dify-codereview · tests-behavior-not-calls Reply here if this is wrong.

assert result is True
assert db_session_with_containers.get(Dataset, dataset.id) is None
dataset_deleted_signal.send.assert_called_once_with(dataset)
dataset_deleted_signal.send.assert_called_once_with(dataset, session=db_session_with_containers)

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.

Please don't pin the exact calls here (assert_called_once_with and friends): that breaks on harmless refactors and only proves the mock was hit. Assert the general behaviour instead: what is returned, persisted, raised or emitted.

dify-codereview · tests-behavior-not-calls Reply here if this is wrong.

asukaminato0721

This comment was marked as outdated.

asukaminato0721

This comment was marked as outdated.

@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 16, 2026

@asukaminato0721 asukaminato0721 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.

3 inline suggestions at 258e62d009, each from a rule learned from earlier reviews in this repository. Reply on a comment if it is wrong. 2 review threads are still unresolved.

dify-codereview

assert result is True
assert db_session_with_containers.get(Dataset, dataset.id) is None
mock_dataset_was_deleted.send.assert_called_once_with(dataset)
mock_dataset_was_deleted.send.assert_called_once_with(dataset, session=db_session_with_containers)

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.

Please don't pin the exact calls here (assert_called_once_with and friends): that breaks on harmless refactors and only proves the mock was hit. Assert the general behaviour instead: what is returned, persisted, raised or emitted.

dify-codereview · tests-behavior-not-calls Reply here if this is wrong.

assert result is True
assert db_session_with_containers.get(Dataset, dataset.id) is None
send_deleted_signal.assert_called_once_with(dataset)
send_deleted_signal.assert_called_once_with(dataset, session=db_session_with_containers)

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.

Please don't pin the exact calls here (assert_called_once_with and friends): that breaks on harmless refactors and only proves the mock was hit. Assert the general behaviour instead: what is returned, persisted, raised or emitted.

dify-codereview · tests-behavior-not-calls Reply here if this is wrong.

def test_enqueues_cleanup_with_provided_session(sqlite_session: Session) -> None:
dataset = _dataset(indexing_technique=IndexTechniqueType.ECONOMY, chunk_structure="text_model")

with patch("events.event_handlers.clean_when_dataset_deleted.clean_dataset_task.delay") as delay:

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.

Rather than patching this in the test, change the production code to take the dependency as a parameter (pass the session / account / service in), so the test hands in a real one and needs no patch.

dify-codereview · tests-real-dependencies (inject-dependency) Learned from an earlier review. Reply here if this is wrong.

@asukaminato0721 asukaminato0721 added waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address and removed waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address labels Nov 25, 2026

@asukaminato0721 asukaminato0721 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.

CI is failing: API Tests, Run API Tests / API Integration Tests (3.13). Please fix it. Checked 258e62d009.

dify-codereview

@asukaminato0721 asukaminato0721 added the waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:M This PR changes 30-99 lines, ignoring generated files. waiting-for-author dify-codereview: the author has suggestions, a conflict, failing CI or unresolved threads to address

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants