Skip to content

refactor(api): update tag name with a single UPDATE statement - #42449

Merged
asukaminato0721 merged 1 commit into
langgenius:mainfrom
CodeTrainerMan:refactor/use-update-stmt-in-tag-service-38419
Sep 18, 2026
Merged

asukaminato0721 merged 1 commit into
langgenius:mainfrom
CodeTrainerMan:refactor/use-update-stmt-in-tag-service-38419

Conversation

@CodeTrainerMan

@CodeTrainerMan CodeTrainerMan commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #38419

TagService.update_tags previously loaded the Tag entity, mutated tag.name, and relied on the flush/commit to write it back. This PR applies the rename with a single targeted statement instead:

session.execute(
    update(Tag)
    .where(Tag.id == tag_id, Tag.tenant_id == current_tenant_id)
    .values(name=payload.name)
    .execution_options(synchronize_session="fetch")
)
  • Follows the convention already used in DatasetService.update_dataset.
  • synchronize_session="fetch" keeps the returned Tag instance in sync, so callers keep observing the new name right after the call. Verified with a minimal SQLAlchemy repro: the emitted SQL is a single UPDATE ... RETURNING and the in-memory entity already reports the new value before the commit.
  • The Tag model has no ORM event listener and no updated_at column, so skipping the attribute mutation does not drop any side effect.
  • Scope: only update_tags is changed. save_tags, delete_tag and the binding helpers are untouched, because they insert or delete rows where there is nothing to gain.

Verified locally: ruff check, ruff format --check and pyrefly check on api/services/tag_service.py (0 diagnostics). The full make lint / make type-check were not run locally because the optional vdb dependencies cannot be built in this Windows environment; CI covers them.

Screenshots

Before After
N/A - backend only, no UI change N/A - backend only, no UI change

Checklist

  • This change requires a documentation update, included: Dify Document
  • I understand that this PR may be closed in case there was no previous discussion or issues. (This doesn't apply to typos!)
  • I've verified the change and added or updated tests where meaningful regression risk justifies coverage.
  • I've updated the documentation accordingly.
  • I ran make lint && make type-check (backend) and vp staged (frontend) to appease the lint gods

Replace the load-mutate-commit flow in TagService.update_tags with a
targeted UPDATE ... RETURNING executed via update(Tag).where(...).values(name=...),
matching the existing convention in DatasetService.update_dataset.

synchronize_session="fetch" keeps the returned Tag instance in sync, so
callers still observe the new name right after the call.

Fixes langgenius#38419
@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 64.34% 64.34% -0.00%
Strict coverage 63.95% 63.95% -0.00%
Typed symbols 46,479 46,478 -1
Untyped symbols 25,916 25,917 +1
Modules 3417 3417 0

@codecov

codecov Bot commented Sep 17, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 87.40%. Comparing base (ad9a5be) to head (d975022).

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #42449   +/-   ##
=======================================
  Coverage   87.40%   87.40%           
=======================================
  Files        5462     5462           
  Lines      315635   315635           
  Branches    63612    63612           
=======================================
+ Hits       275867   275868    +1     
  Misses      34385    34385           
+ Partials     5383     5382    -1     
Flag Coverage Δ
api 87.08% <100.00%> (+<0.01%) ⬆️

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 added this pull request to the merge queue Sep 18, 2026
Merged via the queue into langgenius:main with commit 2683144 Sep 18, 2026
52 checks passed
@CodeTrainerMan
CodeTrainerMan deleted the refactor/use-update-stmt-in-tag-service-38419 branch September 18, 2026 09:56
@CodeTrainerMan
CodeTrainerMan restored the refactor/use-update-stmt-in-tag-service-38419 branch October 10, 2026 10:44
@CodeTrainerMan
CodeTrainerMan deleted the refactor/use-update-stmt-in-tag-service-38419 branch October 10, 2026 10:44
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.

use update(model).select().values to update, instead of manually commit

3 participants