Repository: Cinnamon/kotaemon
File: libs/ktem/ktem/tags/crud.py (Python)

Code before:
        with Session(self._engine) as session:
            statement = select(Tag).where(Tag.name == tag_name)
            result = session.exec(statement).first()

            if result:
                session.delete(result)
                session.commit()
                return True

        return False

    def update_by_name(
        self,
        name: str,
        prompt: str | None = None,
        config: str | None = None,
        scope: str | None = None,
        type: str | None = None,
        valid_classes: str | None = None,
    ):
        with Session(self._engine) as session:
            statement = select(Tag).where(Tag.name == name)
            result = session.exec(statement).first()

            if result:
                result.prompt = prompt or result.prompt
                result.config = config or result.config
                result.type = type or result.type
                result.scope = scope or result.scope

                if valid_classes is not None:
                    if TagType(result.type) != TagType.classification:
                        result.meta = {}
                    else:
                        result.meta = {"valid_classes": valid_classes}

                session.add(result)
                session.commit()

                return True

        return False


class ChunkTagIndexCRUD:
    def __init__(self, engine):
        self._engine = engine

    @cached_property
    def tag_crud(self) -> TagCRUD:
        return TagCRUD(self._engine)

Review comment (suggestion):
not bad, however I still think `update` looks better
```python
update(Tag).where(...).values(**{key: value for key, value in {'prompt': prompt, ...} if value})...
```

Code after:
        with Session(self._engine) as session:
            statement = select(Tag).where(Tag.name == tag_name)
            result = session.exec(statement).first()

            if result:
                session.delete(result)
                session.commit()
                return True
            else:
                raise Exception(f"Record with name-{tag_name} does not exist!")

        return False

    def update_by_name(
        self,
        name: str,
        prompt: str | None = None,
        config: str | None = None,
        scope: str | None = None,
        type: str | None = None,
        valid_classes: str | None = None,
    ):
        fields_to_update = {
            "prompt": prompt,
            "config": config,
            "type": type,
            "scope": scope,
        }

        # Add `meta` update based on valid_classes
        if valid_classes is not None:
            if type == TagType.classification.value:
                fields_to_update["meta"] = {"valid_classes": valid_classes}
            else:
                fields_to_update["meta"] = {}

        # Ensure that only non-None values are updated
        fields_to_update = {k: v for k, v in fields_to_update.items() if v is not None}

        stmt = update(Tag).where(Tag.name == name).values(**fields_to_update)

        with Session(self._engine) as session:
            session.execute(stmt)
            session.commit()

        return False


class ChunkTagIndexCRUD:
    def __init__(self, engine):
        self._engine = engine