From b83d9d594e57003a300f8d07f8f3089a945b1e82 Mon Sep 17 00:00:00 2001 From: Mattia Verga Date: Sat, 11 Jan 2025 18:01:04 +0100 Subject: [PATCH] Ensure Update is in session before linking relationships Signed-off-by: Mattia Verga --- .../server/consumers/automatic_updates.py | 16 +++++++++----- bodhi-server/bodhi/server/models.py | 22 +++++++++++++------ bodhi-server/tests/services/test_updates.py | 3 ++- news/PR5840.bug | 1 + 4 files changed, 28 insertions(+), 14 deletions(-) create mode 100644 news/PR5840.bug diff --git a/bodhi-server/bodhi/server/consumers/automatic_updates.py b/bodhi-server/bodhi/server/consumers/automatic_updates.py index 1a5d250091..1ea456608e 100644 --- a/bodhi-server/bodhi/server/consumers/automatic_updates.py +++ b/bodhi-server/bodhi/server/consumers/automatic_updates.py @@ -191,21 +191,27 @@ def __call__(self, message: fedora_messaging.api.Message) -> None: except ValueError: critpath_groups = None critpath = Update.contains_critpath_component([build], rel.branch) + # Avoid sqlalchemy warnings about adding relationships before the Update is in session update = Update( release=rel, - builds=[build], - bugs=closing_bugs, notes=notes, type=utype, stable_karma=3, unstable_karma=-3, autokarma=False, - user=user, status=UpdateStatus.pending, critpath_groups=critpath_groups, critpath=critpath, ) + log.debug("Adding new update to the database.") + dbsession.add(update) + + # Now add relationships + update.user = user + update.bugs = closing_bugs + update.builds = [build] + # Comment on the update that it was automatically created. update.comment( dbsession, @@ -215,9 +221,6 @@ def __call__(self, message: fedora_messaging.api.Message) -> None: update.add_tag(update.release.pending_signing_tag) - log.debug("Adding new update to the database.") - dbsession.add(update) - log.debug("Flushing changes to the database.") dbsession.flush() @@ -227,6 +230,7 @@ def __call__(self, message: fedora_messaging.api.Message) -> None: except Exception as e: log.error(f'Problem obsoleting older updates: {e}') + Update._ready_for_testing(update, None) alias = update.alias buglist = [b.bug_id for b in update.bugs] diff --git a/bodhi-server/bodhi/server/models.py b/bodhi-server/bodhi/server/models.py index 2572e04103..a513295be6 100644 --- a/bodhi-server/bodhi/server/models.py +++ b/bodhi-server/bodhi/server/models.py @@ -2085,8 +2085,6 @@ def __init__(self, *args, **kwargs): log.debug('Set alias for %s to %s' % (self.get_title(), alias)) - self._ready_for_testing(self, None) - @property def version_hash(self): """ @@ -2547,7 +2545,6 @@ def new(cls, request, data): """ db = request.db user = User.get(request.identity.name) - data['user'] = user caveats = [] try: data['critpath_groups'] = cls.get_critpath_groups( @@ -2574,9 +2571,12 @@ def new(cls, request, data): db.add(bug) db.flush() bugs.append(bug) - data['bugs'] = bugs del data['edited'] + # Avoid sqlalchemy warnings about adding relationships before the Update is in session + data.pop("bugs", None) + builds = data.pop("builds", None) + data.pop("user", None) req = data.pop("request", UpdateRequest.testing) @@ -2591,7 +2591,15 @@ def new(cls, request, data): data['stable_days'] = 0 log.debug("Overriding autotime settings for rawhide update.") + # Add the update to session up = Update(**data, release=release) + log.debug(f"Adding new update to the db {up.alias}.") + db.add(up) + + # Now add relationships + up.user = user + up.bugs = bugs + up.builds = builds # We want to make sure that the value of stable_days # will not be lower than the mandatory_days_in_testing. @@ -2613,8 +2621,8 @@ def new(cls, request, data): f"release value of {up.min_karma}" }) - log.debug(f"Adding new update to the db {up.alias}.") - db.add(up) + cls._ready_for_testing(up, None) + log.debug(f"Triggering db commit for new update {up.alias}.") db.commit() @@ -4251,7 +4259,7 @@ def _ready_for_testing(target, old): """ if old == NEVER_SET: # This is the object initialization phase. This instance is not ready, don't create - # the message now. This method will be called again at the end of __init__ + # the message now. This method will be called again after the update is created return if target.content_type != ContentType.rpm: return diff --git a/bodhi-server/tests/services/test_updates.py b/bodhi-server/tests/services/test_updates.py index e18fcfae9b..a84a71a769 100644 --- a/bodhi-server/tests/services/test_updates.py +++ b/bodhi-server/tests/services/test_updates.py @@ -5369,12 +5369,13 @@ def test_submit_older_build_to_stable(self, *args): request=UpdateRequest.testing, notes='second update', user=update.user, release=update.release, stable_karma=3, unstable_karma=-3) + self.db.add(update) + Update._ready_for_testing(update, None) update.comment(self.db, "foo1", 1, 'foo1') update.comment(self.db, "foo2", 1, 'foo2') # this is triggering a karma-autopush to get the newer update pushed stable with fml_testing.mock_sends(api.Message, api.Message, api.Message, api.Message): update.comment(self.db, "foo3", 1, 'foo3') - self.db.add(update) # Let's clear any messages that might get sent self.db.info['messages'] = [] diff --git a/news/PR5840.bug b/news/PR5840.bug new file mode 100644 index 0000000000..90e4c71d0d --- /dev/null +++ b/news/PR5840.bug @@ -0,0 +1 @@ +When creating a new Update, do not add relationships before the Update object is in session. Fixes some SQLAlchemy 2.x warnings.