[PR #31727] refactor: migrate db.session to session_factory (part of #24115) #33373

Closed
opened 2026-02-21 20:53:10 -05:00 by yindo · 0 comments
Owner

Original Pull Request: https://github.com/langgenius/dify/pull/31727

State: closed
Merged: No


Summary

Part of #24115 - Migrating Flask-SQLAlchemy db.session to pure SQLAlchemy session_factory pattern.

This PR migrates 8 files from the old db.session pattern to the new with session_factory.create_session() pattern.

Changes

Event Handlers (5 files)

  • api/events/event_handlers/create_document_index.py
  • api/events/event_handlers/update_app_dataset_join_when_app_published_workflow_updated.py
  • api/events/event_handlers/update_app_dataset_join_when_app_model_config_updated.py
  • api/events/event_handlers/create_site_record_when_app_created.py
  • api/events/event_handlers/create_installed_app_when_app_created.py

Tasks (1 file)

  • api/tasks/delete_conversation_task.py

Services (2 files)

  • api/services/credit_pool_service.py
  • api/services/datasource_provider_service.py (partial - one method)

Migration Pattern

Before:

from extensions.ext_database import db

dataset = db.session.query(Dataset).where(...).first()
db.session.add(obj)
db.session.commit()
db.session.close()  # Manual cleanup

After:

from core.db.session_factory import session_factory

with session_factory.create_session() as session:
    dataset = session.query(Dataset).where(...).first()
    session.add(obj)
    session.commit()
    # Auto cleanup via context manager

Checklist

  • All changes follow the migration pattern from PR #31198
  • Removed manual session.close() calls (context manager handles it)
  • Updated imports from extensions.ext_database.db to core.db.session_factory
  • make lint passes
  • make type-check passes
  • No business logic changes - only session management refactoring

Related

Notes

This is an incremental migration. There are still ~150+ files using db.session that will be migrated in subsequent PRs.

**Original Pull Request:** https://github.com/langgenius/dify/pull/31727 **State:** closed **Merged:** No --- ## Summary Part of #24115 - Migrating Flask-SQLAlchemy `db.session` to pure SQLAlchemy `session_factory` pattern. This PR migrates **8 files** from the old `db.session` pattern to the new `with session_factory.create_session()` pattern. ## Changes ### Event Handlers (5 files) - `api/events/event_handlers/create_document_index.py` - `api/events/event_handlers/update_app_dataset_join_when_app_published_workflow_updated.py` - `api/events/event_handlers/update_app_dataset_join_when_app_model_config_updated.py` - `api/events/event_handlers/create_site_record_when_app_created.py` - `api/events/event_handlers/create_installed_app_when_app_created.py` ### Tasks (1 file) - `api/tasks/delete_conversation_task.py` ### Services (2 files) - `api/services/credit_pool_service.py` - `api/services/datasource_provider_service.py` (partial - one method) ## Migration Pattern **Before:** ```python from extensions.ext_database import db dataset = db.session.query(Dataset).where(...).first() db.session.add(obj) db.session.commit() db.session.close() # Manual cleanup ``` **After:** ```python from core.db.session_factory import session_factory with session_factory.create_session() as session: dataset = session.query(Dataset).where(...).first() session.add(obj) session.commit() # Auto cleanup via context manager ``` ## Checklist - [x] All changes follow the migration pattern from PR #31198 - [x] Removed manual `session.close()` calls (context manager handles it) - [x] Updated imports from `extensions.ext_database.db` to `core.db.session_factory` - [x] `make lint` passes - [x] `make type-check` passes - [x] No business logic changes - only session management refactoring ## Related - Issue: #24115 - Previous work: PR #31198 (merged) ## Notes This is an incremental migration. There are still ~150+ files using `db.session` that will be migrated in subsequent PRs.
yindo added the pull-request label 2026-02-21 20:53:10 -05:00
yindo closed this issue 2026-02-21 20:53:10 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify#33373