Avoid intermittent integrity error on alarm creation

Fixes bug 1255107

Previously, alarm creation failed intermittently with the
sqlalchemy driver, due to the reliance on session merge to create
the project & user IDs if not already encountered.  This approach
conflicted with the strict foreign key constraints that were
added to the alarms table as an after-thought.

Now, we avoid the potential problem by explicitly creating the
project and user IDs if necessary.

Unfortunately, this cannot be tested via the scenario tests,
as there we use sqlite (the problematic migration does not
apply the foreign key constraints for that engine).

Change-Id: Ieaed676a0a68725242fadbf658646b874d6851ce
This commit is contained in:
Eoghan Glynn
2013-11-26 16:35:36 +00:00
parent ef5351f806
commit b2b500c83c

View File

@@ -695,8 +695,10 @@ class Connection(base.Connection):
"""
session = sqlalchemy_session.get_session()
with session.begin():
session.merge(models.User(id=alarm.user_id))
session.merge(models.Project(id=alarm.project_id))
Connection._create_or_update(session, models.User,
alarm.user_id)
Connection._create_or_update(session, models.Project,
alarm.project_id)
alarm_row = models.Alarm(id=alarm.alarm_id)
alarm_row.update(alarm.as_dict())
session.add(alarm_row)
@@ -803,9 +805,12 @@ class Connection(base.Connection):
"""
session = sqlalchemy_session.get_session()
with session.begin():
session.merge(models.User(id=alarm_change['user_id']))
session.merge(models.Project(id=alarm_change['project_id']))
session.merge(models.Project(id=alarm_change['on_behalf_of']))
Connection._create_or_update(session, models.User,
alarm_change['user_id'])
Connection._create_or_update(session, models.Project,
alarm_change['project_id'])
Connection._create_or_update(session, models.Project,
alarm_change['on_behalf_of'])
alarm_change_row = models.AlarmChange(
event_id=alarm_change['event_id'])
alarm_change_row.update(alarm_change)