#358 Fix the M:N SQL join in RebuildImagesOnODCSComposeDone.
Merged 3 months ago by jkaluza. Opened 3 months ago by jkaluza.
jkaluza/freshmaker bob-response  into  master

@@ -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 @@ 

          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 = [

@@ -95,6 +95,23 @@ 

                  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'}

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.

Pull-Request has been merged by jkaluza

3 months ago