fix(api): sort draft variables in descending order by creation time

Previously, the draft variables are sorted by their ids, which are
randomly generated uuidv4 id. This causes the order of draft
variables unstable.

This fix sorts draft variables by creation time, ensure a consist
order.
pull/21604/head
QuantumGhost 11 months ago
parent 23aab8f82b
commit 9cc07dd76c

@ -154,7 +154,7 @@ class WorkflowDraftVariableService:
variables = ( variables = (
# Do not load the `value` field. # Do not load the `value` field.
query.options(orm.defer(WorkflowDraftVariable.value)) query.options(orm.defer(WorkflowDraftVariable.value))
.order_by(WorkflowDraftVariable.id.desc()) .order_by(WorkflowDraftVariable.created_at.desc())
.limit(limit) .limit(limit)
.offset((page - 1) * limit) .offset((page - 1) * limit)
.all() .all()
@ -168,7 +168,7 @@ class WorkflowDraftVariableService:
WorkflowDraftVariable.node_id == node_id, WorkflowDraftVariable.node_id == node_id,
) )
query = self._session.query(WorkflowDraftVariable).filter(*criteria) query = self._session.query(WorkflowDraftVariable).filter(*criteria)
variables = query.order_by(WorkflowDraftVariable.id.desc()).all() variables = query.order_by(WorkflowDraftVariable.created_at.desc()).all()
return WorkflowDraftVariableList(variables=variables) return WorkflowDraftVariableList(variables=variables)
def list_node_variables(self, app_id: str, node_id: str) -> WorkflowDraftVariableList: def list_node_variables(self, app_id: str, node_id: str) -> WorkflowDraftVariableList:
@ -446,6 +446,9 @@ def _batch_upsert_draft_varaible(
stmt = stmt.on_conflict_do_update( stmt = stmt.on_conflict_do_update(
index_elements=WorkflowDraftVariable.unique_app_id_node_id_name(), index_elements=WorkflowDraftVariable.unique_app_id_node_id_name(),
set_={ set_={
# Refresh creation timestamp to ensure updated variables
# appear first in chronologically sorted result sets.
"created_at": stmt.excluded.created_at,
"updated_at": stmt.excluded.updated_at, "updated_at": stmt.excluded.updated_at,
"last_edited_at": stmt.excluded.last_edited_at, "last_edited_at": stmt.excluded.last_edited_at,
"description": stmt.excluded.description, "description": stmt.excluded.description,

Loading…
Cancel
Save