33 lines
1.1 KiB
Python
33 lines
1.1 KiB
Python
|
from sqlalchemy import create_engine, event, Column, Integer, String, DateTime, UniqueConstraint
|
||
|
from sqlalchemy.orm import sessionmaker, declarative_base
|
||
|
|
||
|
Base = declarative_base()
|
||
|
|
||
|
class ModificadosEv(Base):
|
||
|
__tablename__ = "modificados_ev"
|
||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||
|
evento_uuid = Column(String, nullable=False)
|
||
|
publicado_fecha = Column(DateTime, nullable=False)
|
||
|
__table_args__ = (
|
||
|
UniqueConstraint('evento_uuid', 'publicado_fecha', name='ev_pub_fecha'),
|
||
|
)
|
||
|
|
||
|
def get_engine(db_path: str):
|
||
|
# Uso de connect_args={"check_same_thread": False} para múltiples hilos
|
||
|
return create_engine(
|
||
|
f"sqlite:///{db_path}",
|
||
|
echo=False,
|
||
|
connect_args={"check_same_thread": False}
|
||
|
)
|
||
|
|
||
|
def set_sqlite_pragma(dbapi_connection, connection_record):
|
||
|
cursor = dbapi_connection.cursor()
|
||
|
cursor.execute("PRAGMA journal_mode=WAL;")
|
||
|
cursor.close()
|
||
|
|
||
|
def create_tables(engine):
|
||
|
Base.metadata.create_all(bind=engine)
|
||
|
|
||
|
def get_session(engine):
|
||
|
return sessionmaker(bind=engine, autoflush=False, autocommit=False)()
|