feat(workflow_records): do not use default_factory for workflow id

Using default_factory to autogenerate UUIDs doesn't make sense here, and results awkward typescript types.

Remove the default factory and instead manually create a UUID for workflow id. There are only two places where this needs to happen so it's not a big change.
This commit is contained in:
psychedelicious 2023-12-06 23:33:01 +11:00
parent d75d3885c3
commit 7436aa8e3a
2 changed files with 9 additions and 6 deletions

View File

@ -3,10 +3,9 @@ from enum import Enum
from typing import Any, Union from typing import Any, Union
import semver import semver
from pydantic import BaseModel, Field, JsonValue, TypeAdapter, field_validator from pydantic import BaseModel, ConfigDict, Field, JsonValue, TypeAdapter, field_validator
from invokeai.app.util.metaenum import MetaEnum from invokeai.app.util.metaenum import MetaEnum
from invokeai.app.util.misc import uuid_string
__workflow_meta_version__ = semver.Version.parse("1.0.0") __workflow_meta_version__ = semver.Version.parse("1.0.0")
@ -66,12 +65,14 @@ class WorkflowWithoutID(BaseModel):
nodes: list[dict[str, JsonValue]] = Field(description="The nodes of the workflow.") nodes: list[dict[str, JsonValue]] = Field(description="The nodes of the workflow.")
edges: list[dict[str, JsonValue]] = Field(description="The edges of the workflow.") edges: list[dict[str, JsonValue]] = Field(description="The edges of the workflow.")
model_config = ConfigDict(extra="forbid")
WorkflowWithoutIDValidator = TypeAdapter(WorkflowWithoutID) WorkflowWithoutIDValidator = TypeAdapter(WorkflowWithoutID)
class Workflow(WorkflowWithoutID): class Workflow(WorkflowWithoutID):
id: str = Field(default_factory=uuid_string, description="The id of the workflow.") id: str = Field(description="The id of the workflow.")
WorkflowValidator = TypeAdapter(Workflow) WorkflowValidator = TypeAdapter(Workflow)

View File

@ -14,9 +14,10 @@ from invokeai.app.services.workflow_records.workflow_records_common import (
WorkflowRecordListItemDTO, WorkflowRecordListItemDTO,
WorkflowRecordListItemDTOValidator, WorkflowRecordListItemDTOValidator,
WorkflowRecordOrderBy, WorkflowRecordOrderBy,
WorkflowValidator,
WorkflowWithoutID, WorkflowWithoutID,
WorkflowWithoutIDValidator,
) )
from invokeai.app.util.misc import uuid_string
class SqliteWorkflowRecordsStorage(WorkflowRecordsStorageBase): class SqliteWorkflowRecordsStorage(WorkflowRecordsStorageBase):
@ -66,7 +67,7 @@ class SqliteWorkflowRecordsStorage(WorkflowRecordsStorageBase):
try: try:
# Only user workflows may be created by this method # Only user workflows may be created by this method
assert workflow.meta.category is WorkflowCategory.User assert workflow.meta.category is WorkflowCategory.User
workflow_with_id = WorkflowValidator.validate_python(workflow.model_dump()) workflow_with_id = Workflow(**workflow.model_dump(), id=uuid_string())
self._lock.acquire() self._lock.acquire()
self._cursor.execute( self._cursor.execute(
"""--sql """--sql
@ -204,7 +205,8 @@ class SqliteWorkflowRecordsStorage(WorkflowRecordsStorageBase):
workflow_paths = workflows_dir.glob("*.json") workflow_paths = workflows_dir.glob("*.json")
for path in workflow_paths: for path in workflow_paths:
bytes_ = path.read_bytes() bytes_ = path.read_bytes()
workflow = WorkflowValidator.validate_json(bytes_) workflow_without_id = WorkflowWithoutIDValidator.validate_json(bytes_)
workflow = Workflow(**workflow_without_id.model_dump(), id=uuid_string())
workflows.append(workflow) workflows.append(workflow)
# Only default workflows may be managed by this method # Only default workflows may be managed by this method
assert all(w.meta.category is WorkflowCategory.Default for w in workflows) assert all(w.meta.category is WorkflowCategory.Default for w in workflows)