Skip to content

Commit f8bbada

Browse files
Order builds by id instead of build_id (#2948)
Order builds by id instead of build_id Reviewed-by: gemini-code-assist[bot] Reviewed-by: Nikola Forró Reviewed-by: Marek Blaha <mblaha@redhat.com> Reviewed-by: Maja Massarini Reviewed-by: Laura Barcziová
2 parents 15b93c8 + 9866a4c commit f8bbada

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

packit_service/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2332,7 +2332,8 @@ def get_all_by(
23322332
)
23332333
.filter(CoprBuildTargetModel.project_name == project_name)
23342334
.filter(ProjectEventModel.commit_sha == commit_sha)
2335-
.order_by(CoprBuildTargetModel.build_id.desc())
2335+
# we can't order by `build_id` because it's a string
2336+
.order_by(CoprBuildTargetModel.id.desc())
23362337
)
23372338

23382339
if owner:

tests_openshift/database/test_models.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,58 @@ def test_copr_get_all_by_commit(clean_before_and_after, multiple_copr_builds):
393393
)
394394

395395

396+
def test_copr_get_all_by_ordering_by_id_desc(clean_before_and_after, pr_project_event_model):
397+
"""
398+
Test that get_all_by() orders by id DESC (latest first), not by build_id DESC.
399+
400+
This is important because build_id is a String, so string ordering would be incorrect.
401+
For example, "9931733" > "10020464" as strings, but 10020464 > 9931733 numerically.
402+
Ordering by id DESC ensures we get the latest build (highest id) first.
403+
"""
404+
_, run_model_1 = SRPMBuildModel.create_with_new_run(project_event_model=pr_project_event_model)
405+
group_1 = CoprBuildGroupModel.create(run_model_1)
406+
build_1 = CoprBuildTargetModel.create(
407+
build_id="9931733", # Lower build_id, but created first (lower id)
408+
project_name=SampleValues.project,
409+
owner=SampleValues.owner,
410+
web_url="https://copr.fedorainfracloud.org/coprs/build/9931733/",
411+
target=SampleValues.target,
412+
status=BuildStatus.success,
413+
copr_build_group=group_1,
414+
)
415+
416+
_, run_model_2 = SRPMBuildModel.create_with_new_run(project_event_model=pr_project_event_model)
417+
group_2 = CoprBuildGroupModel.create(run_model_2)
418+
build_2 = CoprBuildTargetModel.create(
419+
build_id="10020464", # Higher build_id, created second (higher id)
420+
project_name=SampleValues.project,
421+
owner=SampleValues.owner,
422+
web_url="https://copr.fedorainfracloud.org/coprs/build/10020464/",
423+
target=SampleValues.target,
424+
status=BuildStatus.success,
425+
copr_build_group=group_2,
426+
)
427+
428+
# Verify build_2 has higher id (was created later)
429+
assert build_2.id > build_1.id
430+
431+
# Verify that string ordering of build_id would be wrong
432+
assert build_1.build_id > build_2.build_id # "9931733" > "10020464" as strings
433+
434+
builds = CoprBuildTargetModel.get_all_by(
435+
commit_sha=SampleValues.commit_sha,
436+
project_name=SampleValues.project,
437+
owner=SampleValues.owner,
438+
target=SampleValues.target,
439+
status=BuildStatus.success,
440+
)
441+
442+
# next(iter(builds)) should return the latest build (build_2 with higher id)
443+
latest_build = next(iter(builds))
444+
assert latest_build.id == build_2.id
445+
assert latest_build.build_id == "10020464"
446+
447+
396448
def test_multiple_pr_models(clean_before_and_after):
397449
pr1 = PullRequestModel.get_or_create(
398450
pr_id=1,

0 commit comments

Comments
 (0)