mirror of
https://github.com/GNS3/gns3-server
synced 2025-01-27 00:11:07 +00:00
Fix issues with latest version of sqlalchemy
This commit is contained in:
parent
4727708c85
commit
956b9056c1
@ -43,6 +43,7 @@ class GUID(TypeDecorator):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
impl = CHAR
|
impl = CHAR
|
||||||
|
cache_ok = True
|
||||||
|
|
||||||
def load_dialect_impl(self, dialect):
|
def load_dialect_impl(self, dialect):
|
||||||
if dialect.name == "postgresql":
|
if dialect.name == "postgresql":
|
||||||
|
@ -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}")
|
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)
|
engine = create_async_engine(db_url, connect_args={"check_same_thread": False}, future=True)
|
||||||
try:
|
try:
|
||||||
async with engine.begin() as conn:
|
async with engine.connect() as conn:
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
log.info(f"Successfully connected to database '{db_url}'")
|
log.info(f"Successfully connected to database '{db_url}'")
|
||||||
app.state._db_engine = engine
|
app.state._db_engine = engine
|
||||||
except SQLAlchemyError as e:
|
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")
|
@event.listens_for(Engine, "connect")
|
||||||
|
@ -78,7 +78,7 @@ async def db_session(db_engine):
|
|||||||
# recreate database tables for each class
|
# recreate database tables for each class
|
||||||
# preferred and faster way would be to rollback the session/transaction
|
# preferred and faster way would be to rollback the session/transaction
|
||||||
# but it doesn't work for some reason
|
# 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
|
# 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
|
# to the database using the "after_create" sqlalchemy event
|
||||||
hashed_password = "$2b$12$jPsNU9IS7.EWEqXahtDfo.26w6VLOLCuFEHKNvDpOjxs5e0WpqJfa"
|
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.drop_all)
|
||||||
await conn.run_sync(Base.metadata.create_all)
|
await conn.run_sync(Base.metadata.create_all)
|
||||||
|
|
||||||
session = AsyncSession(db_engine)
|
session = AsyncSession(db_engine, expire_on_commit=False)
|
||||||
try:
|
try:
|
||||||
yield session
|
yield session
|
||||||
finally:
|
finally:
|
||||||
|
Loading…
Reference in New Issue
Block a user