"""All database connection information is defined here"""
from sqlmodel import SQLModel, create_engine
from sqlalchemy.engine import Engine
from sqlalchemy import event


DB_FILE = "devices.db"
sqlite_url = f"sqlite:///{DB_FILE}"

connect_args = {"check_same_thread": False}
engine = create_engine(
    sqlite_url, connect_args=connect_args, echo=True
)  # set echo=True to view output


# From SQLAlchemy docs to allow foreign Key support
# https://docs.sqlalchemy.org/en/14/dialects/sqlite.html#foreign-key-support
@event.listens_for(Engine, "connect")
def set_sqlite_pragma(dbapi_connection, connection_record):
    """Used to enable foreign keys in sqlite"""
    cursor = dbapi_connection.cursor()
    cursor.execute("PRAGMA foreign_keys=ON")
    cursor.close()


def create_db_and_tables():
    """Used to create and initialize DB"""
    SQLModel.metadata.create_all(engine)

sqlalchemy.event.listens_for sqlmodel.SQLModel.metadata.create_all sqlmodel.create_engine
[((284, 347), 'sqlmodel.create_engine', 'create_engine', (['sqlite_url'], {'connect_args': 'connect_args', 'echo': '(True)'}), '(sqlite_url, connect_args=connect_args, echo=True)\n', (297, 347), False, 'from sqlmodel import SQLModel, create_engine\n'), ((518, 554), 'sqlalchemy.event.listens_for', 'event.listens_for', (['Engine', '"""connect"""'], {}), "(Engine, 'connect')\n", (535, 554), False, 'from sqlalchemy import event\n'), ((843, 879), 'sqlmodel.SQLModel.metadata.create_all', 'SQLModel.metadata.create_all', (['engine'], {}), '(engine)\n', (871, 879), False, 'from sqlmodel import SQLModel, create_engine\n')]