Skip to content

Commit

Permalink
Commit request to database to prevent UniqueViolation due to parallel…
Browse files Browse the repository at this point in the history
… requests processing.

[CLOUDDST-20061]
  • Loading branch information
lipoja committed Oct 3, 2023
1 parent 4af1d2d commit e186fa8
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
17 changes: 17 additions & 0 deletions iib/web/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,13 @@ def get_or_create(cls, pull_specification: str) -> Image:
if not image:
image = Image(pull_specification=pull_specification)
db.session.add(image)
try:
db.session.commit()
except sqlalchemy.exc.IntegrityError:
current_app.logger.info(
'Image pull specification is already in database. "%s"', pull_specification
)
image = cls.query.filter_by(pull_specification=pull_specification).first()

return image

Expand Down Expand Up @@ -284,6 +291,11 @@ def get_or_create(cls, name: str) -> Operator:
if not operator:
operator = Operator(name=name)
db.session.add(operator)
try:
db.session.commit()
except sqlalchemy.exc.IntegrityError:
current_app.logger.info('Operators is already in database. "%s"', name)
operator = cls.query.filter_by(name=name).first()

return operator

Expand Down Expand Up @@ -1696,6 +1708,11 @@ def get_or_create(cls, username: str) -> User:
if not user:
user = User(username=username)
db.session.add(user)
try:
db.session.commit()
except sqlalchemy.exc.IntegrityError:
current_app.logger.info('User is already in database. "%s"', username)
user = cls.query.filter_by(username=username).first()

return user

Expand Down
3 changes: 2 additions & 1 deletion tests/test_web/test_api_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,8 @@ def test_patch_request_add_success(
}

for bundle in bundles:
minimal_request_add.bundles.append(Image.get_or_create(bundle))
img = Image.get_or_create(bundle)
minimal_request_add.bundles.append(img)
minimal_request_add.add_state('in_progress', 'Starting things up')
db.session.commit()

Expand Down

0 comments on commit e186fa8

Please sign in to comment.