mirror of
https://github.com/dat515-2025/Group-8.git
synced 2026-03-22 15:12:08 +01:00
46 lines
1.8 KiB
Python
46 lines
1.8 KiB
Python
import os
|
|
from sqlalchemy.ext.asyncio import create_async_engine, async_sessionmaker
|
|
from sqlalchemy import create_engine
|
|
from sqlalchemy.orm import sessionmaker
|
|
from app.core.base import Base
|
|
|
|
DATABASE_URL = os.getenv("DATABASE_URL")
|
|
if not DATABASE_URL:
|
|
mariadb_host = os.getenv("MARIADB_HOST", "localhost")
|
|
mariadb_port = os.getenv("MARIADB_PORT", "3306")
|
|
mariadb_db = os.getenv("MARIADB_DB", "group_project")
|
|
mariadb_user = os.getenv("MARIADB_USER", "root")
|
|
mariadb_password = os.getenv("MARIADB_PASSWORD", "strongpassword")
|
|
if mariadb_host and mariadb_db and mariadb_user and mariadb_password:
|
|
DATABASE_URL = f"mysql+asyncmy://{mariadb_user}:{mariadb_password}@{mariadb_host}:{mariadb_port}/{mariadb_db}"
|
|
else:
|
|
raise Exception("Only MariaDB is supported. Please set the DATABASE_URL environment variable.")
|
|
|
|
# Load all models to register them
|
|
from app.models.user import User
|
|
from app.models.transaction import Transaction
|
|
from app.models.categories import Category
|
|
|
|
host_env = os.getenv("MARIADB_HOST", "localhost")
|
|
ssl_enabled = host_env not in {"localhost", "127.0.0.1"}
|
|
connect_args = {"ssl": {"ssl": True}} if ssl_enabled else {}
|
|
|
|
# Async engine/session for the async parts of the app
|
|
engine = create_async_engine(
|
|
DATABASE_URL,
|
|
pool_pre_ping=True,
|
|
echo=os.getenv("SQL_ECHO", "0") == "1",
|
|
connect_args=connect_args,
|
|
)
|
|
async_session_maker = async_sessionmaker(engine, expire_on_commit=False)
|
|
|
|
# Synchronous engine/session for sync utilities (e.g., bank_scraper)
|
|
SYNC_DATABASE_URL = DATABASE_URL.replace("+asyncmy", "+pymysql")
|
|
engine_sync = create_engine(
|
|
SYNC_DATABASE_URL,
|
|
pool_pre_ping=True,
|
|
echo=os.getenv("SQL_ECHO", "0") == "1",
|
|
connect_args=connect_args,
|
|
)
|
|
sync_session_maker = sessionmaker(bind=engine_sync, expire_on_commit=False)
|