"""Idempotent seed data helpers.""" from __future__ import annotations import json from dataclasses import dataclass, field from typing import Any from database.db_helper import Database @dataclass(frozen=True) class SeedCategory: category_id: str name: str parent_id: str | None = None category_type: str = "material" sort_order: int = 0 metadata: dict[str, Any] = field(default_factory=dict) DEFAULT_MATERIAL_CATEGORIES: tuple[SeedCategory, ...] = ( SeedCategory("cat_default_top", "Top", sort_order=10, metadata={"seed": True, "wardrobe_category": "top"}), SeedCategory("cat_default_bottom", "Bottom", sort_order=20, metadata={"seed": True, "wardrobe_category": "bottom"}), SeedCategory("cat_default_dress", "Dress", sort_order=30, metadata={"seed": True, "wardrobe_category": "dress"}), SeedCategory("cat_default_shoes", "Shoes", sort_order=40, metadata={"seed": True, "wardrobe_category": "shoes"}), SeedCategory( "cat_default_accessory", "Accessory", sort_order=50, metadata={"seed": True, "wardrobe_category": "accessory"}, ), SeedCategory( "cat_imagestudio_构图", "构图", sort_order=110, metadata={"seed": True, "source": "imagestudio_backend", "frontend_group": "构图"}, ), SeedCategory( "cat_imagestudio_服装", "服装", sort_order=120, metadata={"seed": True, "source": "imagestudio_backend", "frontend_group": "服装"}, ), SeedCategory( "cat_imagestudio_风格", "风格", sort_order=130, metadata={"seed": True, "source": "imagestudio_backend", "frontend_group": "风格"}, ), SeedCategory( "cat_imagestudio_配饰", "配饰", sort_order=140, metadata={"seed": True, "source": "imagestudio_backend", "frontend_group": "配饰"}, ), SeedCategory( "cat_imagestudio_鞋袜", "鞋袜", sort_order=150, metadata={"seed": True, "source": "imagestudio_backend", "frontend_group": "鞋袜"}, ), SeedCategory( "cat_imagestudio_accessory_眼部", "眼部", parent_id="cat_imagestudio_配饰", category_type="accessory_group", sort_order=210, metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, ), SeedCategory( "cat_imagestudio_accessory_头部", "头部", parent_id="cat_imagestudio_配饰", category_type="accessory_group", sort_order=220, metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, ), SeedCategory( "cat_imagestudio_accessory_手部", "手部", parent_id="cat_imagestudio_配饰", category_type="accessory_group", sort_order=230, metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, ), SeedCategory( "cat_imagestudio_accessory_胸前", "胸前", parent_id="cat_imagestudio_配饰", category_type="accessory_group", sort_order=240, metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, ), SeedCategory( "cat_imagestudio_accessory_颈部", "颈部", parent_id="cat_imagestudio_配饰", category_type="accessory_group", sort_order=250, metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, ), SeedCategory( "cat_imagestudio_accessory_腰部", "腰部", parent_id="cat_imagestudio_配饰", category_type="accessory_group", sort_order=260, metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, ), SeedCategory( "cat_imagestudio_accessory_脸部", "脸部", parent_id="cat_imagestudio_配饰", category_type="accessory_group", sort_order=270, metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, ), SeedCategory( "cat_imagestudio_accessory_臂部", "臂部", parent_id="cat_imagestudio_配饰", category_type="accessory_group", sort_order=280, metadata={"seed": True, "source": "imagestudio_backend", "parent_category": "配饰"}, ), ) def seed_default_material_categories(database: Database) -> dict[str, Any]: placeholder = database._placeholder inserted: list[str] = [] skipped: list[str] = [] conn = database.connect() try: for category in DEFAULT_MATERIAL_CATEGORIES: exists = conn.execute( f"SELECT category_id FROM material_categories WHERE category_id = {placeholder}", (category.category_id,), ).fetchone() if exists: skipped.append(category.category_id) continue conn.execute( f""" INSERT INTO material_categories ( category_id, name, parent_id, category_type, sort_order, metadata_json ) VALUES ( {placeholder}, {placeholder}, {placeholder}, {placeholder}, {placeholder}, {placeholder} ) """, ( category.category_id, category.name, category.parent_id, category.category_type, category.sort_order, json.dumps(category.metadata, ensure_ascii=False, sort_keys=True), ), ) inserted.append(category.category_id) conn.commit() finally: conn.close() return {"inserted": inserted, "skipped": skipped, "total": len(inserted)}