31 lines
786 B
Python
31 lines
786 B
Python
|
"""
|
||
|
Initialization routines for the database
|
||
|
"""
|
||
|
from typing import Optional, TYPE_CHECKING
|
||
|
|
||
|
# This should also import all the public models
|
||
|
# so the engine is ready for them
|
||
|
# https://sqlmodel.tiangolo.com/tutorial/create-db-and-table/
|
||
|
from diyalgo import models, Config
|
||
|
|
||
|
from sqlmodel import SQLModel, create_engine
|
||
|
|
||
|
if TYPE_CHECKING:
|
||
|
from sqlalchemy.future.engine import Engine
|
||
|
|
||
|
def init_db_engine(config:Optional[Config] = None) -> 'Engine':
|
||
|
if config is None:
|
||
|
config = Config()
|
||
|
|
||
|
engine = create_engine(config.sqlite_path)
|
||
|
return engine
|
||
|
|
||
|
|
||
|
def create_tables(
|
||
|
engine: Optional['Engine'] = None,
|
||
|
config:Optional[Config] = None
|
||
|
):
|
||
|
if engine is None:
|
||
|
engine = init_db_engine(config=config)
|
||
|
|
||
|
SQLModel.metadata.create_all(engine)
|