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

Standardize database error-handling in logic.py, storage.py, downloads.py, crypto.py #2222

Open
rocodes opened this issue Sep 12, 2024 · 3 comments
Labels
database SQLite, SQLAlchemy, and data model

Comments

@rocodes
Copy link
Contributor

rocodes commented Sep 12, 2024

Description

logic.py, storage.py, downloads.py, and crypto.py all interact with the database, querying the current session for records. Currently, they are all 'aware' of sqlalchemy-specific errors, but don't implement consistent error-handling, meaning that a SqlAlchemy exception under certain circumstances (particularly a race between an operation on a File, Message, or Reply object and the deletion of that object) can crash the app (see #2217).

IMO:

  • SQLAlchemy errors should not be exposed outside of storage.py, so that the rest of the stack can remain database-agnostic. The most basic version of this would be to handle all SqlAlchemyErrors in storage.py, wrapping in our own exception class to report the relevant information (ie, preserving the distinction between an error such as NoResultFound, which may not be an error, and a more general SqlAlchemy error, would does represent a problem)
  • We can refactor some of the classes that store a reference to the session (and operate on it) in order to avoid all of the elements having to be aware of the db session, and outsource those operations to the controller
  • Consolidating all the database/session operations into one place will make it easier to maintain consistent logic and to reason about database operations, and will also pave the way for performance improvements (offloading to another thread etc).

Needs a bit more detail to sketch out exactly what the implementation changes should be, but filing just to provide context for anyone looking into this more.

@cfm cfm added the database SQLite, SQLAlchemy, and data model label Sep 12, 2024
@cfm
Copy link
Member

cfm commented Sep 12, 2024

Everything labeled "database" is relevant evidence in support of this refactoring, but see especially #1440.

@rocodes
Copy link
Contributor Author

rocodes commented Sep 17, 2024

see especially #1440.

Oh, hello, past me, it's so nice to see you 🤦

Thanks. I initially was going to close that as a dupe, but there's a lot of comment history there. I'll leave it open for now.

@rocodes
Copy link
Contributor Author

rocodes commented Sep 17, 2024

Here's what I propose:

  • Investigate more robust error-handling for database transactions in storage.py #1440 includes things around transaction handling, savepoints, and better transactions, let's leave it open
  • Keep this issue scoped to areas where we don't catch SQLAlchemy errors at all, and/or specifically the issue of constraining SQLAlchemy exceptions to storage.py (and db.py by extension), and leave any modifications to transactions, commits, etc out of scope of this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
database SQLite, SQLAlchemy, and data model
Projects
None yet
Development

No branches or pull requests

2 participants