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

decouple database connection #74

Merged
merged 21 commits into from
Jan 26, 2021
Merged

Conversation

geospatial-jeff
Copy link
Collaborator

@geospatial-jeff geospatial-jeff commented Jan 21, 2021

Moving the management of database resources to the client, instead of the API. Ref #57 #71

  • Adds the stac_api.clients.postgres.session.Session object which uses fastapi-utils to manage database sessions and commits.
    • Previously, database sessions were managed by a FastAPI middleware and some context vars to distribute the session throughout the app. Commits were handled through PostgresClient which has been removed.
    • PaginationTokenClient is now a mixin to align with the spec (pagination is now part of core) and to re-use the database session of the parent class.
    • Note how the app factory (stac_api.api.app) no longer references any postgres related code.

Dockerfile Outdated Show resolved Hide resolved
@geospatial-jeff geospatial-jeff changed the title [WIP] decouple database connection decouple database connection Jan 25, 2021
elif isinstance(e.orig, psycopg2.errors.ForeignKeyViolation):
raise errors.ForeignKeyError("collection does not exist") from e
logger.error(e, exc_info=True)
raise errors.DatabaseError("unhandled database error")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a catch-all exception handler here?

Suggested change
raise errors.DatabaseError("unhandled database error")
raise errors.DatabaseError("unhandled database error")
except Exception as e:
logger.error(e, exc_info=True)
raise errors.DatabaseError("unhandled database error")

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes good catch, we need to handle exceptions from get_db which are not of IntegrityError.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

catch-all exception handler doesn't work here because there are other exceptions that might get thrown within the context of a database session (like NotFoundError). These exceptions end up getting lost in the catch-all exception handler which then impacts response codes.

Instead I've updated the except to use a lower-level sqlalchemy exception. It's the lowest level that includes the wrapped psycopg2 error (e.orig) that we need to determine what error to throw. It should cover any error which occurs during the execution of a SQL statement, which I think covers our requirements here.

@geospatial-jeff geospatial-jeff merged commit 3838242 into master Jan 26, 2021
@geospatial-jeff geospatial-jeff deleted the update-database-connection branch January 26, 2021 23:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants