Skip to content

Clean up DB transactions and switch to using APSW for SQLite interfacing

hcsch requested to merge hcsch/mukas:master into master

Open one SQLite connection per request to avoid concurrency issues. With write-ahead-logging (APSW best practices enable this) writing as well as reading accesses are cheap enough concurrently. This way transactions won't accidentally be mixed between transactions.

Wrap each request in a DB transaction and explicitly pass in the db connection for requests that require it with the help of a decorator. If a request exits exceptionally the request is rolled back, otherwise it is committed. All previous non-exceptional failing returns from a request handler using the DB have been converted to raise custom exceptions that are later converted to what would have been returned in an error handler for the specific exception. This also includes redirects after a failing request.

Along the way, add python type hints where code was touched.

DB migration is now automatic using the user_version pragma from SQLite. The first migration from the current database with the current schema will have to be done manually. This is because the already filled DB will still have user_version 0 which is only to be carried by still empty DBs in this new migration scheme. This can be manually resolved by checking the DB against the schema and the setting the user_version to 1. Future migrations will then be automatically applicable again.

Merge request reports