Skip to content

[Chore/Refactor] use sqlalchemy engine #24138

Closed

Description

@asukaminato0721

Self Checks

  • I have read the Contributing Guide and Language Policy.
  • This is only for refactoring, if you would like to ask a question, please head to Discussions.
  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report, otherwise it will be closed.
  • 【中文用户 & Non English User】请使用英语提交,否则会被关闭 :)
  • Please do not modify this template :) and fill in all the required fields.

Description

take #35934 as a ref.

continue of #24115

Motivation

One more step to remove flask_sqlalchemy

Additional Context

part of #23647

Activity

  1. dosubot commented on Sep 20, 2025

    @dosubot

    Hi, @asukaminato0721. I'm Dosu, and I'm helping the Dify team manage their backlog and am marking this issue as stale.

    Issue Summary

    Next Steps

    • Please let me know if this issue is still relevant with the latest version of Dify by commenting here.
    • If I do not hear back within 15 days, I will automatically close this issue.

    Thank you for your understanding and contribution!

  2. asukaminato0721 commented on Feb 5, 2026

    @asukaminato0721
    ContributorAuthor

    @dosu what is current progress

  3. 2 remaining items

  4. dataCenter430 commented on Apr 8, 2026

    @dataCenter430
    Contributor

    Hi, @dosu
    are there still remaning tasks for this issue?
    what is the next step to complete this?

  5. dosubot commented on Apr 8, 2026

    @dosubot

    Yes, there's still significant remaining work. Here's a summary of where things stand and what's next:

    Current Status: The migration is a two-stage process — Stage 1 replaces db.session with Session(db.engine) [1], and Stage 2 further optimizes to sessionmaker().begin() which handles commit/rollback automatically [2]. Good progress has been made recently (~7 PRs merged in the last few weeks), but ~150–200 files still need conversion.

    What's already done:

    • Session factory infrastructure is in place [3]
    • Multiple controller layers have been migrated (console, web, service_api, inner_api) [4] [5] [6]
    • Test fixtures updated across 43 test files [7]
    • Only 2 files still explicitly import flask_sqlalchemy [8] [9]

    What's remaining:

    1. Three major open PRs need review/merge: refactor: migrate core, models, tasks, events and schedule to sessionmaker pattern #34381 (core/models/tasks/events — 37 files) [10], refactor: migrate services layer to sessionmaker pattern #34382 (services — 19 files) [11], and refactor: migrate manual Session usage to sessionmaker().begin() for better transaction management #32433 (75+ files across multiple layers) [12]
    2. Services layer — workflow_service.py, dataset_service.py, message_service.py, annotation_service.py, etc.
    3. Core modules — app runners (api/core/app/apps/), agent runner (api/core/agent/base_agent_runner.py), RAG pipeline, tool engine
    4. Model property accessors in api/models/model.py — these use db.session.scalar() / db.session.get() in properties, making conversion tricky [13]
    5. Tasks and scheduled jobs — workflow execution, document indexing, conversation deletion
    6. Final cleanup — remove flask_sqlalchemy from dependencies and replace the Pagination import in app_service.py

    Recommended next steps:

    1. Get the open PRs (refactor: migrate core, models, tasks, events and schedule to sessionmaker pattern #34381, refactor: migrate services layer to sessionmaker pattern #34382, refactor: migrate manual Session usage to sessionmaker().begin() for better transaction management #32433) reviewed and merged to clear the backlog
    2. Tackle the model property accessors in api/models/model.py — this is on the critical path since many other files depend on it
    3. Continue the systematic module-by-module conversion for the remaining files (most are mechanical ~5–15 min per file conversions) [2]

    At the current pace, the full migration is estimated at 2–4 months to completion [1]. This is all tracked under the parent epic #23647 [14].

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

    Leave Feedback Ask Dosu about dify

  6. dataCenter430 commented on Apr 8, 2026

    @dataCenter430
    Contributor

    @dosu what is the estimate remaining time for this issue.

  7. dosubot commented on Jun 9, 2026

    @dosubot

    Hi, @asukaminato0721. I'm Dosu, and I'm helping the dify team manage their backlog and am marking this issue as stale.

    Recap

    • You proposed refactoring the codebase to use SQLAlchemy’s engine directly, such as Session(db.engine), as part of the broader effort to remove flask_sqlalchemy.
    • This builds on the earlier migration work in [Chore/Refactor] db.session.xxx -> with Session as session #24115 and the larger epic in [Chore/Refactor] adapt async / await #23647.
    • The migration is still in progress and being handled in stages, with many files already converted.
    • There is still substantial remaining work, including roughly 150–200 files and several larger PRs that still need review and merge.
    • The main blockers appear to be remaining service/core/task modules and some model property accessors that still depend on db.session.

    Next Steps

    • If this issue is still relevant to the latest version of the dify repository, please comment here to keep the discussion open.
    • Otherwise, this issue will be automatically closed in 15 days.

    Thanks for your understanding and contribution.

  8. asukaminato0721 commented on Nov 4, 2026

    @asukaminato0721
    ContributorAuthor

    @dosu any remaining files?

  9. dosubot commented on Nov 4, 2026

    @dosubot

    Yes. As of the November 4 checkout, these files still have direct db.session usage beyond cleanup. Paths are relative to api/; this is a verified shortlist, not an exhaustive backlog:

    File Remaining usage
    core/agent/base_agent_runner.py Adds, commits and queries agent thoughts. [1]
    core/app/apps/agent_app/app_runner.py Reads, deletes and commits agent thoughts. [2]
    core/provider_manager.py Looks up the tenant’s default model. [3]
    core/rag/embedding/cached_embedding.py Queries the embedding cache. [4]
    services/tools/api_tools_manage_service.py Passes db.session() into provider construction. [5]
    services/tools/tools_transform_service.py Uses the global session for provider construction and user lookups. [6] [7]
    services/workflow_run_service.py Passes db.session() into execution-trace assembly. [8]
    models/workflow.py _resolve_workflow_app_tenant_id() queries through the global session. [9]
    tasks/initialize_created_app_rbac_access_task.py Owner lookup and resource batching use db.session(). [10] [11]

    I’d start with the two tool-service files: thread the caller’s session into those helpers, following #35934’s endpoint-owned session and downstream injection pattern. The shared @with_session decorator already provides that controller boundary. [12] [13]

    For the eventual dependency removal, models/engine.py still constructs Flask-SQLAlchemy’s SQLAlchemy object. That is separate from converting individual callers. [14] [15]

    To reply, just mention @dosu.


    Docs are dead. Just use Dosu.

    Leave Feedback Ask Dosu about dify

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions