File size: 831 Bytes
c7dfe8b |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
from Mikobot import DB_URI
from Mikobot import LOGGER as log
if DB_URI and DB_URI.startswith("postgres://"):
DB_URI = DB_URI.replace("postgres://", "postgresql://", 1)
def start() -> scoped_session:
engine = create_engine(DB_URI, client_encoding="utf8")
log.info("[PostgreSQL] Connecting to database......")
BASE.metadata.bind = engine
BASE.metadata.create_all(engine)
return scoped_session(sessionmaker(bind=engine, autoflush=False))
BASE = declarative_base()
try:
SESSION = start()
except Exception as e:
log.exception(f"[PostgreSQL] Failed to connect due to {e}")
exit()
log.info("[PostgreSQL] Connection successful, session started.")
|