This is an automated email from the ASF dual-hosted git repository.

pierrejeambrun pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/airflow.git


The following commit(s) were added to refs/heads/main by this push:
     new ec37818a04 Ensure stable secondary ordering (#43085)
ec37818a04 is described below

commit ec37818a04e63ed13bc91c14bef81bfd0cb19d0a
Author: Pierre Jeambrun <[email protected]>
AuthorDate: Thu Oct 17 18:40:06 2024 +0800

    Ensure stable secondary ordering (#43085)
---
 airflow/api_fastapi/common/parameters.py | 30 ++++++++++++------------------
 1 file changed, 12 insertions(+), 18 deletions(-)

diff --git a/airflow/api_fastapi/common/parameters.py 
b/airflow/api_fastapi/common/parameters.py
index 07d8a76c79..4aa8335905 100644
--- a/airflow/api_fastapi/common/parameters.py
+++ b/airflow/api_fastapi/common/parameters.py
@@ -17,7 +17,6 @@
 
 from __future__ import annotations
 
-import importlib
 from abc import ABC, abstractmethod
 from datetime import datetime
 from typing import TYPE_CHECKING, Any, Callable, Generic, List, TypeVar
@@ -195,32 +194,27 @@ class SortParam(BaseParam[str]):
         # Reset default sorting
         select = select.order_by(None)
 
+        primary_key_column = self.get_primary_key_column()
+
         if self.value[0] == "-":
-            return select.order_by(nullscheck, column.desc(), column.desc())
+            return select.order_by(nullscheck, column.desc(), 
primary_key_column)
         else:
-            return select.order_by(nullscheck, column.asc(), column.asc())
-
-    def get_primary_key(self) -> str:
-        """Get the primary key of the model of SortParam object."""
-        return inspect(self.model).primary_key[0].name
+            return select.order_by(nullscheck, column.asc(), 
primary_key_column)
 
-    @staticmethod
-    def get_primary_key_of_given_model_string(model_string: str) -> str:
-        """
-        Get the primary key of given 'airflow.models' class as a string. The 
class should have driven be from 'airflow.models.base'.
+    def get_primary_key_column(self) -> Column:
+        """Get the primary key column of the model of SortParam object."""
+        return inspect(self.model).primary_key[0]
 
-        :param model_string: The string representation of the model class.
-        :return: The primary key of the model class.
-        """
-        dynamic_return_model = 
getattr(importlib.import_module("airflow.models"), model_string)
-        return inspect(dynamic_return_model).primary_key[0].name
+    def get_primary_key_string(self) -> str:
+        """Get the primary key string of the model of SortParam object."""
+        return self.get_primary_key_column().name
 
     def depends(self, *args: Any, **kwargs: Any) -> Self:
         raise NotImplementedError("Use dynamic_depends, depends not 
implemented.")
 
     def dynamic_depends(self) -> Callable:
-        def inner(order_by: str = self.get_primary_key()) -> SortParam:
-            return self.set_value(self.get_primary_key() if order_by == "" 
else order_by)
+        def inner(order_by: str = self.get_primary_key_string()) -> SortParam:
+            return self.set_value(self.get_primary_key_string() if order_by == 
"" else order_by)
 
         return inner
 

Reply via email to