From a5a9186b44113202e7c351ea571c334df3b4e508 Mon Sep 17 00:00:00 2001 From: YokiiroBW Date: Sun, 5 Jul 2026 16:02:29 +0800 Subject: [PATCH] feat: add database metadata and seed helpers --- database/models.py | 123 +++++++++++++++++++++++++++++++- database/seed.py | 70 +++++++++++++++++- tests/test_s0_infrastructure.py | 30 ++++++++ 3 files changed, 221 insertions(+), 2 deletions(-) diff --git a/database/models.py b/database/models.py index f845b96..2316dd1 100644 --- a/database/models.py +++ b/database/models.py @@ -1 +1,122 @@ -"""SQL model definitions placeholder.""" +"""Lightweight SQL table metadata for ImageStudio.""" + +from __future__ import annotations + +from dataclasses import dataclass, field + + +@dataclass(frozen=True) +class ColumnSpec: + name: str + nullable: bool = True + sensitive: bool = False + + +@dataclass(frozen=True) +class TableSpec: + name: str + primary_key: str + columns: tuple[ColumnSpec, ...] = field(default_factory=tuple) + + def column_names(self) -> list[str]: + return [column.name for column in self.columns] + + def sensitive_columns(self) -> list[str]: + return [column.name for column in self.columns if column.sensitive] + + +TABLES: tuple[TableSpec, ...] = ( + TableSpec( + "character_profiles", + "profile_id", + ( + ColumnSpec("profile_id", nullable=False), + ColumnSpec("owner_user_id"), + ColumnSpec("name", nullable=False), + ColumnSpec("status", nullable=False), + ColumnSpec("metadata_json"), + ), + ), + TableSpec( + "profile_workflows", + "workflow_id", + ( + ColumnSpec("workflow_id", nullable=False), + ColumnSpec("profile_id", nullable=False), + ColumnSpec("workflow_type", nullable=False), + ColumnSpec("status", nullable=False), + ColumnSpec("current_node_id"), + ), + ), + TableSpec( + "workflow_nodes", + "node_id", + ( + ColumnSpec("node_id", nullable=False), + ColumnSpec("workflow_id", nullable=False), + ColumnSpec("node_key", nullable=False), + ColumnSpec("status", nullable=False), + ColumnSpec("metadata_json"), + ), + ), + TableSpec( + "generation_tasks", + "task_id", + ( + ColumnSpec("task_id", nullable=False), + ColumnSpec("workflow_id"), + ColumnSpec("node_id"), + ColumnSpec("status", nullable=False), + ColumnSpec("request_json"), + ColumnSpec("result_json"), + ), + ), + TableSpec( + "material_categories", + "category_id", + ( + ColumnSpec("category_id", nullable=False), + ColumnSpec("name", nullable=False), + ColumnSpec("parent_id"), + ColumnSpec("category_type", nullable=False), + ColumnSpec("sort_order", nullable=False), + ), + ), + TableSpec( + "materials", + "material_id", + ( + ColumnSpec("material_id", nullable=False), + ColumnSpec("category_id"), + ColumnSpec("display_name", nullable=False), + ColumnSpec("image_uri"), + ColumnSpec("recognition_status", nullable=False), + ), + ), + TableSpec( + "audit_logs", + "audit_id", + ( + ColumnSpec("audit_id", nullable=False), + ColumnSpec("actor_id"), + ColumnSpec("action", nullable=False), + ColumnSpec("target_type", nullable=False), + ColumnSpec("target_id"), + ), + ), + TableSpec( + "config_snapshots", + "snapshot_id", + ( + ColumnSpec("snapshot_id", nullable=False), + ColumnSpec("reason", nullable=False), + ColumnSpec("actor_id"), + ColumnSpec("config_json", nullable=False, sensitive=True), + ), + ), +) + + +def table_spec(table_name: str) -> TableSpec | None: + normalized = str(table_name or "").strip() + return next((table for table in TABLES if table.name == normalized), None) diff --git a/database/seed.py b/database/seed.py index db29f95..e78f177 100644 --- a/database/seed.py +++ b/database/seed.py @@ -1 +1,69 @@ -"""Seed data helpers placeholder.""" +"""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 + 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"}, + ), +) + + +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}, NULL, {placeholder}, {placeholder}, {placeholder} + ) + """, + ( + category.category_id, + category.name, + 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)} diff --git a/tests/test_s0_infrastructure.py b/tests/test_s0_infrastructure.py index 3fdb49f..a4b43fd 100644 --- a/tests/test_s0_infrastructure.py +++ b/tests/test_s0_infrastructure.py @@ -8,6 +8,8 @@ from backend.dto.common import ErrorCode, error_response, success_response from core.audit_service import AuditService from core.identity import RequestContext from database.db_helper import Database, DatabaseSettings, PostgresSettings, SqliteSettings +from database.models import table_spec +from database.seed import DEFAULT_MATERIAL_CATEGORIES, seed_default_material_categories class CommonDtoTests(unittest.TestCase): @@ -45,6 +47,34 @@ class DatabaseSettingsTests(unittest.TestCase): self.assertEqual(settings.selected_backend, "sqlite") + +class DatabaseMetadataTests(unittest.TestCase): + def test_table_spec_exposes_known_tables(self): + workflow_nodes = table_spec("workflow_nodes") + + self.assertIsNotNone(workflow_nodes) + self.assertEqual(workflow_nodes.primary_key, "node_id") + self.assertIn("status", workflow_nodes.column_names()) + self.assertEqual(table_spec("config_snapshots").sensitive_columns(), ["config_json"]) + + def test_seed_default_material_categories_is_idempotent(self): + with tempfile.TemporaryDirectory() as tmp: + db_path = Path(tmp) / "imagestudio.db" + database = Database(DatabaseSettings(sqlite=SqliteSettings(path=str(db_path)))) + database.initialize() + + first = seed_default_material_categories(database) + second = seed_default_material_categories(database) + + self.assertEqual(first["total"], len(DEFAULT_MATERIAL_CATEGORIES)) + self.assertEqual(second["total"], 0) + conn = database.connect() + try: + count = conn.execute("SELECT COUNT(*) AS total FROM material_categories").fetchone()["total"] + finally: + conn.close() + self.assertEqual(count, len(DEFAULT_MATERIAL_CATEGORIES)) + def test_postgres_is_selected_when_configured(self): settings = DatabaseSettings( backend="auto",