Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions core/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,12 @@
@asynccontextmanager
async def lifespan(app: FastAPI) -> AsyncGenerator[None, None]:
"""
Application lifespan event handler to initialize the database and lexicon.
Application lifespan event handler to seed data in development mode.
"""
if settings.get_enum("MODE") == "development":
erase_and_rebuild_db()
from transfers.seed import seed_all

seed_all(10)
seed_all(10, skip_if_exists=True)

register_routes(app)
yield
Expand Down
27 changes: 26 additions & 1 deletion transfers/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,31 @@ def get_terms_by_category(s, category_name: str) -> list[LexiconTerm]:
)


def seed_all(n: int = 5):
def ensure_seed_prereqs() -> None:
"""Ensure that lexicon and parameter data exist before seeding."""
from core.initializers import init_lexicon, init_parameter

with session_ctx() as s:
has_lexicon = s.scalar(select(LexiconTerm.id).limit(1)) is not None
has_parameter = s.scalar(select(Parameter.id).limit(1)) is not None

if not has_lexicon:
init_lexicon()
if not has_parameter:
init_parameter()


def contact_data_exists() -> bool:
with session_ctx() as s:
return s.scalar(select(Contact.id).limit(1)) is not None


def seed_all(n: int = 5, skip_if_exists: bool = False):
"""Seed roughly `n` of each main entity and connect them."""
if skip_if_exists and contact_data_exists():
print("Contact data exists; skipping seeding.")
return
ensure_seed_prereqs()
new_mexico_bounds = [
(36.9, -106.6), # Taos area
(35.1, -106.6), # Albuquerque
Expand Down Expand Up @@ -433,6 +456,8 @@ def seed_all(n: int = 5):
print(f"Error committing seed data: {e}")
raise

print("Seeding data finished.")


if __name__ == "__main__":
seed_all(5)