Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flush request to database #566

Merged
merged 1 commit into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Commit request to database to prevent UniqueViolation due to parallel…
… requests processing.

[CLOUDDST-20061]
  • Loading branch information
lipoja committed Oct 3, 2023
commit e186fa8fcd70b9b950d0cb3d81df4c5b35344e2f
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