fix(deps): upgrade to sqlalchemy version 2

Bump sqlalchemy to version 2.0.22

There are quite some changes that happened with v2.
We are currently relying on the "auto-commit" feature which was removed.
For the moment we can use a wrapper class to mimic the auto-commit
behavior allowing us to move to v2.

Ultimately, the (db) session management needs some overhaul though.

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

View file

@ -44,7 +44,7 @@ from multiprocessing import Lock
import py
import pytest
from prometheus_client import values
from sqlalchemy import create_engine
from sqlalchemy import create_engine, text
from sqlalchemy.engine import URL
from sqlalchemy.engine.base import Engine
from sqlalchemy.exc import ProgrammingError
@ -113,15 +113,16 @@ def _create_database(engine: Engine, dbname: str) -> None:
:param dbname: Database name to create
"""
conn = engine.connect()
try:
conn.execute(f"CREATE DATABASE {dbname}")
except ProgrammingError: # pragma: no cover
# The database most likely already existed if we hit
# a ProgrammingError. Just drop the database and try
# again. If at that point things still fail, any
# exception will be propogated up to the caller.
conn.execute(f"DROP DATABASE {dbname} WITH (FORCE)")
conn.execute(f"CREATE DATABASE {dbname}")
with conn.begin():
try:
conn.execute(text(f"CREATE DATABASE {dbname}"))
except ProgrammingError: # pragma: no cover
# The database most likely already existed if we hit
# a ProgrammingError. Just drop the database and try
# again. If at that point things still fail, any
# exception will be propogated up to the caller.
conn.execute(text(f"DROP DATABASE {dbname} WITH (FORCE)"))
conn.execute(text(f"CREATE DATABASE {dbname}"))
conn.close()
initdb.run(AlembicArgs)
@ -133,8 +134,9 @@ def _drop_database(engine: Engine, dbname: str) -> None:
:param engine: Engine returned by test_engine()
:param dbname: Database name to drop
"""
aurweb.schema.metadata.drop_all(bind=engine)
conn = engine.connect()
conn.execute(f"DROP DATABASE {dbname} WITH (FORCE)")
conn.execute(text(f"DROP DATABASE {dbname}"))
conn.close()

View file

@ -24,5 +24,6 @@ def test_run():
aurweb.initdb.run(Args())
# Check that constant table rows got added via initdb.
record = aurweb.db.query(AccountType, AccountType.AccountType == "User").first()
with aurweb.db.begin():
record = aurweb.db.query(AccountType, AccountType.AccountType == "User").first()
assert record is not None

View file

@ -1,5 +1,5 @@
import pytest
from sqlalchemy.exc import IntegrityError
from sqlalchemy.exc import IntegrityError, SAWarning
from aurweb import db, time
from aurweb.db import create, rollback
@ -109,7 +109,7 @@ def test_voteinfo_null_submitter_raises(user: User):
def test_voteinfo_null_agenda_raises(user: User):
with pytest.raises(IntegrityError):
with pytest.raises(IntegrityError), pytest.warns(SAWarning):
with db.begin():
create(
VoteInfo,
@ -123,7 +123,7 @@ def test_voteinfo_null_agenda_raises(user: User):
def test_voteinfo_null_user_raises(user: User):
with pytest.raises(IntegrityError):
with pytest.raises(IntegrityError), pytest.warns(SAWarning):
with db.begin():
create(
VoteInfo,
@ -137,7 +137,7 @@ def test_voteinfo_null_user_raises(user: User):
def test_voteinfo_null_submitted_raises(user: User):
with pytest.raises(IntegrityError):
with pytest.raises(IntegrityError), pytest.warns(SAWarning):
with db.begin():
create(
VoteInfo,
@ -151,7 +151,7 @@ def test_voteinfo_null_submitted_raises(user: User):
def test_voteinfo_null_end_raises(user: User):
with pytest.raises(IntegrityError):
with pytest.raises(IntegrityError), pytest.warns(SAWarning):
with db.begin():
create(
VoteInfo,