From e0cfa4fbd832a67b543d3606b1527be10a3f61b4 Mon Sep 17 00:00:00 2001 From: Jan Kaluza Date: Jan 31 2019 13:06:33 +0000 Subject: Fix the M:N SQL join in RebuildImagesOnODCSComposeDone. The old code did not join with ArtifactBuildCompose but only with Compose. The ArtifactBuild to Compose mapping was therefore wrong and the SQL query returned all the PLANNED ArtifactBuilds no matter what the compose ID was. In this commit, the ArtifactBuildCompose join is added and only ArtifactBuilds with matching compose ID are returned as expected. --- diff --git a/freshmaker/handlers/koji/rebuild_images_on_odcs_compose_done.py b/freshmaker/handlers/koji/rebuild_images_on_odcs_compose_done.py index 12b98ea..04e9c4e 100644 --- a/freshmaker/handlers/koji/rebuild_images_on_odcs_compose_done.py +++ b/freshmaker/handlers/koji/rebuild_images_on_odcs_compose_done.py @@ -24,7 +24,8 @@ import six from freshmaker import db -from freshmaker.models import ArtifactBuild, ArtifactBuildState, Compose +from freshmaker.models import ( + ArtifactBuild, ArtifactBuildState, Compose, ArtifactBuildCompose) from freshmaker.handlers import ( ContainerBuildHandler, fail_event_on_handler_exception) from freshmaker.events import ODCSComposeStateChangeEvent @@ -47,11 +48,13 @@ class RebuildImagesOnODCSComposeDone(ContainerBuildHandler): if event.dry_run: self.force_dry_run() - query = db.session.query(ArtifactBuild).join('composes') + builds_ready_to_rebuild = db.session.query(ArtifactBuild).join( + ArtifactBuildCompose).join(Compose) # Get all the builds waiting for this compose in PLANNED state ... - builds_ready_to_rebuild = query.filter( + builds_ready_to_rebuild = builds_ready_to_rebuild.filter( ArtifactBuild.state == ArtifactBuildState.PLANNED.value, - Compose.odcs_compose_id == event.compose['id']) + Compose.odcs_compose_id == event.compose['id'], + ArtifactBuildCompose.compose_id == Compose.id) # ... and depending on DONE parent image or parent image which is # not planned to be built in this Event (dep_on == None). builds_ready_to_rebuild = [ diff --git a/tests/handlers/koji/test_rebuild_images_on_odcs_compose_done.py b/tests/handlers/koji/test_rebuild_images_on_odcs_compose_done.py index 2c41f48..6fe57cd 100644 --- a/tests/handlers/koji/test_rebuild_images_on_odcs_compose_done.py +++ b/tests/handlers/koji/test_rebuild_images_on_odcs_compose_done.py @@ -95,6 +95,23 @@ class TestRebuildImagesOnODCSComposeDone(helpers.ModelsTestCase): build_id=build.id, compose_id=compose.id)) db.session.commit() + # Create another DB event, build and compose just to have more data + # in database. + another_db_event = Event.create( + db.session, 'msg-2', 'search-key-2', + EVENT_TYPES[ErrataAdvisoryRPMsSignedEvent], + state=EventState.INITIALIZED, + released=False) + another_build_1 = ArtifactBuild.create( + db.session, another_db_event, 'another-build-1', ArtifactType.IMAGE, + state=ArtifactBuildState.PLANNED) + another_compose_1 = Compose(odcs_compose_id=2) + db.session.add(another_compose_1) + db.session.commit() + db.session.add(ArtifactBuildCompose( + build_id=another_build_1.id, compose_id=another_compose_1.id)) + db.session.commit() + def test_cannot_handle_if_compose_is_not_done(self): event = ODCSComposeStateChangeEvent( 'msg-id', {'id': 1, 'state': 'generating'}