Skip to content

Commit

Permalink
Ensure Update is in session before linking relationships
Browse files Browse the repository at this point in the history
Signed-off-by: Mattia Verga <[email protected]>
  • Loading branch information
mattiaverga committed Jan 17, 2025
1 parent 15b80df commit b83d9d5
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 14 deletions.
16 changes: 10 additions & 6 deletions bodhi-server/bodhi/server/consumers/automatic_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()

Expand All @@ -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]

Expand Down
22 changes: 15 additions & 7 deletions bodhi-server/bodhi/server/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down Expand Up @@ -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(
Expand All @@ -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)

Expand All @@ -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.
Expand All @@ -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()

Expand Down Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion bodhi-server/tests/services/test_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'] = []

Expand Down
1 change: 1 addition & 0 deletions news/PR5840.bug
Original file line number Diff line number Diff line change
@@ -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.

0 comments on commit b83d9d5

Please sign in to comment.