Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

fix(api): sort installed apps without NULLS LAST for MySQL (#44029)

Xiyuan Chenf43d1ae77e55
diff --git a/api/repositories/installed_app_repository.py b/api/repositories/installed_app_repository.py
index c5dcb086fd2..2fac0c0ac09 100644
--- a/api/repositories/installed_app_repository.py
+++ b/api/repositories/installed_app_repository.py
@@ -60,10 +60,14 @@ def _installed_app_cursor_filter(cursor: InstalledAppCursor) -> ColumnElement[bo
return and_(same_pin_group, later_in_pin_group)
-def _installed_app_order_by() -> tuple[UnaryExpression[bool], UnaryExpression[datetime | None], UnaryExpression[str]]:
+def _installed_app_order_by() -> tuple[
+ UnaryExpression[bool], UnaryExpression[bool], UnaryExpression[datetime | None], UnaryExpression[str]
+]:
+ # Sort null dates last with a boolean key: MySQL has no NULLS LAST.
return (
InstalledApp.is_pinned.desc(),
- InstalledApp.last_used_at.desc().nulls_last(),
+ InstalledApp.last_used_at.is_(None).asc(),
+ InstalledApp.last_used_at.desc(),
InstalledApp.id.asc(),
)
diff --git a/api/tests/unit_tests/repositories/test_installed_app_repository.py b/api/tests/unit_tests/repositories/test_installed_app_repository.py
index 146d088404e..e6d43679807 100644
--- a/api/tests/unit_tests/repositories/test_installed_app_repository.py
+++ b/api/tests/unit_tests/repositories/test_installed_app_repository.py
@@ -4,7 +4,10 @@ from uuid import uuid4
import pytest
from sqlalchemy import event, inspect
+from sqlalchemy.dialects import mysql, postgresql
+from sqlalchemy.engine import Dialect
from sqlalchemy.orm import ORMExecuteState, Session, sessionmaker
+from sqlalchemy.sql import Select
from models.model import App, AppMode, AppModelConfig, IconType, InstalledApp
from models.workflow import Workflow, WorkflowKind, WorkflowType
@@ -386,6 +389,25 @@ def test_candidates_order_and_cursor_cover_pin_groups_ties_and_null_dates(
)
[email protected]("dialect", [mysql.dialect(), postgresql.dialect()], ids=["mysql", "postgresql"])
+def test_candidates_order_compiles_without_nulls_last_for_every_dialect(
+ sqlite_session_factory: sessionmaker[Session], dialect: Dialect
+) -> None:
+ statements: list[Select[tuple[InstalledApp, App]]] = []
+
+ @event.listens_for(sqlite_session_factory, "do_orm_execute")
+ def capture(state: ORMExecuteState) -> None:
+ if isinstance(state.statement, Select):
+ statements.append(state.statement)
+
+ repository = SQLAlchemyInstalledAppRepository(session_factory=sqlite_session_factory)
+ repository.get_candidates(tenant_id=_VIEWER_TENANT_ID, cursor=None, limit=20, app_id=None, name=None)
+
+ assert len(statements) == 1
+ # MySQL rejects NULLS LAST, so null dates must sort last through a portable key.
+ assert "NULLS" not in str(statements[0].compile(dialect=dialect)).upper()
+
+
@pytest.mark.parametrize("search", [" 50%_\\ ", "50%_\\", "literal"])
def test_candidates_filter_literal_case_insensitive_name_and_app_with_workspace_scope(
sqlite_session_factory: sessionmaker[Session], search: str