didier/database/engine.py

33 lines
1014 B
Python
Raw Normal View History

from urllib.parse import quote_plus
from sqlalchemy.engine import URL
from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import sessionmaker
import settings
2022-06-17 00:43:55 +02:00
# Run local tests against SQLite instead of Postgres
2022-06-19 00:23:25 +02:00
if settings.TESTING and settings.DB_TEST_SQLITE:
2022-06-17 00:43:55 +02:00
engine = create_async_engine(
URL.create(
drivername="sqlite+aiosqlite",
database="tests.db",
),
connect_args={"check_same_thread": False},
)
else:
encoded_password = quote_plus(settings.DB_PASSWORD)
engine = create_async_engine(
URL.create(
drivername="postgresql+asyncpg",
username=settings.DB_USERNAME,
password=encoded_password,
host=settings.DB_HOST,
port=settings.DB_PORT,
database=settings.DB_NAME,
),
pool_pre_ping=True,
)
DBSession = sessionmaker(autocommit=False, autoflush=False, bind=engine, class_=AsyncSession, expire_on_commit=False)