44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
"""
|
|
Seed data — insert initial profile rows if they don't exist.
|
|
|
|
Called on startup after create_tables().
|
|
"""
|
|
|
|
import json
|
|
import logging
|
|
from pathlib import Path
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
SEED_DIR = Path(__file__).parent / "fixtures"
|
|
|
|
|
|
def seed_profiles():
|
|
"""Insert seed profiles from JSON fixtures if not already present."""
|
|
from .connection import get_session
|
|
from .models import Profile
|
|
|
|
fixtures = list(SEED_DIR.glob("*.json"))
|
|
if not fixtures:
|
|
return
|
|
|
|
with get_session() as session:
|
|
for f in fixtures:
|
|
data = json.loads(f.read_text())
|
|
name = data["name"]
|
|
|
|
existing = session.query(Profile).filter(Profile.name == name).first()
|
|
if existing:
|
|
logger.debug("Profile %s already exists, skipping seed", name)
|
|
continue
|
|
|
|
profile = Profile(
|
|
name=name,
|
|
pipeline=data.get("pipeline", {}),
|
|
configs=data.get("configs", {}),
|
|
)
|
|
session.add(profile)
|
|
logger.info("Seeded profile: %s", name)
|
|
|
|
session.commit()
|