fix: sqlalchemy sessions per request

Best practice for web-apps is to have a session per web request.

Instead of having a per worker-thread, we add a middleware that
generates a unique ID per request, utilizing scoped_sessions
scopefunc (custom function for defining a session scope)
in combination with a ContextVar.
With this we create a new session per request.

Signed-off-by: moson <moson@archlinux.org>
This commit is contained in:
moson 2023-11-30 14:44:00 +01:00
parent d7ecede2eb
commit 3558c6ae5c
No known key found for this signature in database
GPG key ID: 4A4760AB4EE15296
8 changed files with 94 additions and 19 deletions

View file

@ -1531,6 +1531,7 @@ def test_packages_post_disown_as_maintainer(
errors = get_errors(resp.text)
expected = "You did not select any packages to disown."
assert errors[0].text.strip() == expected
db.refresh(package)
assert package.PackageBase.Maintainer is not None
# Try to disown `package` without giving the confirm argument.
@ -1555,6 +1556,7 @@ def test_packages_post_disown_as_maintainer(
data={"action": "disown", "IDs": [package.ID], "confirm": True},
)
assert resp.status_code == int(HTTPStatus.BAD_REQUEST)
db.refresh(package)
assert package.PackageBase.Maintainer is not None
errors = get_errors(resp.text)
expected = "You are not allowed to disown one of the packages you selected."
@ -1568,6 +1570,7 @@ def test_packages_post_disown_as_maintainer(
data={"action": "disown", "IDs": [package.ID], "confirm": True},
)
db.get_session().expire_all()
assert package.PackageBase.Maintainer is None
successes = get_successes(resp.text)
expected = "The selected packages have been disowned."
@ -1652,6 +1655,7 @@ def test_packages_post_delete(
# Whoo. Now, let's finally make a valid request as `pm_user`
# to delete `package`.
pkgname = package.PackageBase.Name
with client as request:
request.cookies = pm_cookies
resp = request.post(
@ -1664,7 +1668,7 @@ def test_packages_post_delete(
assert successes[0].text.strip() == expected
# Expect that the package deletion was logged.
pkgbases = [package.PackageBase.Name]
pkgbases = [pkgname]
expected = (
f"Privileged user '{pm_user.Username}' deleted the "
f"following package bases: {str(pkgbases)}."