From 956b9056c119582296163d9c1be2551373eb2872 Mon Sep 17 00:00:00 2001 From: grossmj Date: Sat, 8 May 2021 15:37:51 +0930 Subject: [PATCH] Fix issues with latest version of sqlalchemy --- gns3server/db/models/base.py | 1 + gns3server/db/tasks.py | 4 ++-- tests/conftest.py | 4 ++-- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/gns3server/db/models/base.py b/gns3server/db/models/base.py index 17c68f76..86017f06 100644 --- a/gns3server/db/models/base.py +++ b/gns3server/db/models/base.py @@ -43,6 +43,7 @@ class GUID(TypeDecorator): """ impl = CHAR + cache_ok = True def load_dialect_impl(self, dialect): if dialect.name == "postgresql": diff --git a/gns3server/db/tasks.py b/gns3server/db/tasks.py index 567ce272..c3c5ec0c 100644 --- a/gns3server/db/tasks.py +++ b/gns3server/db/tasks.py @@ -43,12 +43,12 @@ async def connect_to_db(app: FastAPI) -> None: db_url = os.environ.get("GNS3_DATABASE_URI", f"sqlite+aiosqlite:///{db_path}") engine = create_async_engine(db_url, connect_args={"check_same_thread": False}, future=True) try: - async with engine.begin() as conn: + async with engine.connect() as conn: await conn.run_sync(Base.metadata.create_all) log.info(f"Successfully connected to database '{db_url}'") app.state._db_engine = engine except SQLAlchemyError as e: - log.error(f"Error while connecting to database '{db_url}: {e}") + log.fatal(f"Error while connecting to database '{db_url}: {e}") @event.listens_for(Engine, "connect") diff --git a/tests/conftest.py b/tests/conftest.py index 9645c85f..669f777d 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -78,7 +78,7 @@ async def db_session(db_engine): # recreate database tables for each class # preferred and faster way would be to rollback the session/transaction # but it doesn't work for some reason - async with db_engine.begin() as conn: + async with db_engine.connect() as conn: # Speed up tests by avoiding to hash the 'admin' password everytime the default super admin is added # to the database using the "after_create" sqlalchemy event hashed_password = "$2b$12$jPsNU9IS7.EWEqXahtDfo.26w6VLOLCuFEHKNvDpOjxs5e0WpqJfa" @@ -86,7 +86,7 @@ async def db_session(db_engine): await conn.run_sync(Base.metadata.drop_all) await conn.run_sync(Base.metadata.create_all) - session = AsyncSession(db_engine) + session = AsyncSession(db_engine, expire_on_commit=False) try: yield session finally: