Skip to content

Commit

Permalink
Fix ordering logic
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasG0 committed Jan 7, 2025
1 parent 24270c2 commit 936b255
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 21 deletions.
34 changes: 14 additions & 20 deletions backend/infrahub/core/query/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,6 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
self.order_by = []

self.return_labels = ["n.uuid", "rb.branch", f"{db.get_id_function_name()}(rb) as rb_id"]
where_clause_elements = []

branch_filter, branch_params = self.branch.get_query_filter_path(
at=self.at, branch_agnostic=self.branch_agnostic
Expand Down Expand Up @@ -911,18 +910,12 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
""" % {"branch_filter": branch_filter, "node_kind": self.schema.kind}
self.add_to_query(topquery)

use_simple = False
if self.has_filter_by_id and self.filters:
use_simple = True
where_clause_elements.append("n.uuid = $uuid")
self.params["uuid"] = self.filters["id"]
elif not self.has_filters and not self.schema.order_by:
use_simple = True
if self.order is None or self.order.disable is not True:
self.order_by = ["n.uuid"]
if use_simple:
if where_clause_elements:
self.add_to_query(" AND " + " AND ".join(where_clause_elements))
self.add_to_query(" AND n.uuid = $uuid")
return
if not self.has_filters and not self.schema.order_by:
self.order_by = ["n.uuid"]
return

if self.filters and "ids" in self.filters:
Expand All @@ -939,6 +932,16 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
await self._add_node_order_attributes(
db=db, field_attribute_requirements=field_attribute_requirements, branch_filter=branch_filter
)
for far in field_attribute_requirements:
if not far.is_order:
continue
if far.supports_profile:
self.order_by.append(far.final_value_query_variable)
continue
self.order_by.append(far.node_value_query_variable)

# Always order by uuid to guarantee pagination.
self.order_by.append("n.uuid")

if use_profiles:
await self._add_profiles_per_node_query(db=db, branch_filter=branch_filter)
Expand All @@ -948,15 +951,6 @@ async def query_init(self, db: InfrahubDatabase, **kwargs: Any) -> None:
await self._add_profile_rollups(field_attribute_requirements=field_attribute_requirements)

self._add_final_filter(field_attribute_requirements=field_attribute_requirements)
if should_order:
for far in field_attribute_requirements:
if not far.is_order:
continue
if far.supports_profile:
self.order_by.append(far.final_value_query_variable)
continue
self.order_by.append(far.node_value_query_variable)
self.order_by.append("n.uuid")

async def _add_node_filter_attributes(
self,
Expand Down
2 changes: 1 addition & 1 deletion backend/tests/helpers/test_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def bus_simulator(self, db: InfrahubDatabase) -> Generator[BusSimulator, None, N
config.OVERRIDE.message_bus = original

@pytest.fixture(scope="class", autouse=True)
async def workflow_local(self) -> AsyncGenerator[WorkflowLocalExecution, None]:
async def workflow_local(self, prefect) -> AsyncGenerator[WorkflowLocalExecution, None]:
original = config.OVERRIDE.workflow
workflow = WorkflowLocalExecution()
await setup_task_manager()
Expand Down

0 comments on commit 936b255

Please sign in to comment.