[Feature] Add support to use any sql database as the metadata storage for embedchain apps (#1273)

This commit is contained in:
Deshraj Yadav
2024-02-19 13:04:18 -08:00
committed by GitHub
parent 6c12bc9044
commit 5e2e7fb639
20 changed files with 601 additions and 202 deletions

View File

@@ -1,19 +1,31 @@
import os
import pytest
from sqlalchemy import MetaData, create_engine
from sqlalchemy.orm import sessionmaker
@pytest.fixture(autouse=True)
def clean_db():
db_path = os.path.expanduser("~/.embedchain/embedchain.db")
if os.path.exists(db_path):
os.remove(db_path)
db_url = f"sqlite:///{db_path}"
engine = create_engine(db_url)
metadata = MetaData()
metadata.reflect(bind=engine) # Reflect schema from the engine
Session = sessionmaker(bind=engine)
session = Session()
@pytest.fixture
def setup():
clean_db()
yield
clean_db()
try:
# Iterate over all tables in reversed order to respect foreign keys
for table in reversed(metadata.sorted_tables):
if table.name != "alembic_version": # Skip the Alembic version table
session.execute(table.delete())
session.commit()
except Exception as e:
session.rollback()
print(f"Error cleaning database: {e}")
finally:
session.close()
@pytest.fixture(autouse=True)