mirror of https://github.com/gmbrax/Pilgrim.git
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
from sqlalchemy import create_engine
|
|
from sqlalchemy.ext.declarative import declarative_base
|
|
from sqlalchemy.orm import sessionmaker
|
|
from pathlib import Path
|
|
import os
|
|
import shutil
|
|
|
|
from pilgrim.utils import ConfigManager
|
|
|
|
Base = declarative_base()
|
|
|
|
|
|
|
|
class Database:
|
|
|
|
def __init__(self, config_manager: ConfigManager):
|
|
self.db_path = config_manager.database_url
|
|
|
|
# Garantir que o diretório existe
|
|
db_dir = os.path.dirname(self.db_path)
|
|
if not os.path.exists(db_dir):
|
|
os.makedirs(db_dir, exist_ok=True)
|
|
|
|
self.engine = create_engine(
|
|
f"sqlite:///{self.db_path}",
|
|
echo=False,
|
|
connect_args={"check_same_thread": False},
|
|
)
|
|
self._session_maker = sessionmaker(bind=self.engine, autoflush=False, autocommit=False)
|
|
|
|
def create(self):
|
|
# ADICIONE ESTAS 3 LINHAS AQUI
|
|
print("\n--- [DIAGNÓSTICO DA APLICAÇÃO] ---")
|
|
print("Tabelas que a Base conhece:", sorted(list(Base.metadata.tables.keys())))
|
|
print("----------------------------------")
|
|
Base.metadata.create_all(self.engine)
|
|
|
|
def session(self):
|
|
return self._session_maker()
|
|
|
|
def get_db(self):
|
|
return self._session_maker()
|