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

[ENHANCEMENT] Improve Media Creation in Media Repository #140

Open
movchan74 opened this issue Jul 18, 2024 · 0 comments
Open

[ENHANCEMENT] Improve Media Creation in Media Repository #140

movchan74 opened this issue Jul 18, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@movchan74
Copy link
Contributor

Enhancement Description

  • Improve the create method in the /aana/storage/repository/media.py file to handle media creation more efficiently and securely.

Advantages

  • Eliminates race conditions where two processes might try to create the same media simultaneously.
  • Reduces the number of database queries from two to one, potentially improving performance.

Possible Implementation

  1. Replace the current implementation with a try-except block:

    try:
        return super().create(entity)
    except IntegrityError as e:
        self.session.rollback()
        # Check if the IntegrityError is due to a unique constraint violation on the media_id column
        if is_unique_constraint_violation(e, 'media_id'):
            raise MediaIdAlreadyExistsException(self.table_name, entity.id) from e
        raise  # Re-raise the original exception if it's not a unique constraint violation on media_id
  2. Implement a helper function is_unique_constraint_violation(exception, column_name) to check if the IntegrityError is due to a unique constraint violation on the specified column. This function should handle different DBMS-specific error messages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant