[docs]defadd_metadata(key:str,value:str,engine:Engine)->None:"""Add metadata to the database. Args: key: Key of the metadata. value: Value of the metadata. engine: Engine """withSession(engine)assession:ifsession.scalar(select(Metadata).where(Metadata.key==key)):passelse:new_metadata=Metadata(key=key,value=value)session.add(new_metadata)session.commit()
[docs]defcreate_schema(cache_dir:str)->Tuple[Engine,sessionmaker]:"""Create the schema in the sqlite database. Args: cache_dir: Location where the cache directory. Returns: A tuple of sqlalchemy engine and session maker. """engine=create_engine(f"sqlite:///{cache_dir}",connect_args={"check_same_thread":False})Base.metadata.create_all(bind=engine,checkfirst=True)sessionLocal=sessionmaker(autocommit=False,autoflush=False,bind=engine)add_metadata("schema_version",SCHEMA_VERSION,engine)return(engine,sessionLocal)