Repository: LAION-AI/Open-Assistant
File: backend/oasst_backend/prompt_repository.py (Python)

Code before:
                    update(Message).filter(Message.parent_id.in_(parent_ids)).values(deleted=True).returning(Message.id)
                )

                parent_ids = self.db.execute(query).scalars().all()

    @managed_tx_method(CommitMode.COMMIT)
    def reintroduce_deleted_message(self, message: Message | UUID, recursive: bool = True):
        """
        Reintroduce deleted messages and all their descendants.
        """

        message_id = None
        if isinstance(message, UUID):
            message_id = message
        elif isinstance(message, Message):
            message_id = message.id
        else:
            raise OasstError("Server error", OasstErrorCode.SERVER_ERROR1, HTTPStatus.INTERNAL_SERVER_ERROR)

        query = update(Message).where(Message.id == message_id).values(deleted=False)
        self.db.execute(query)

        if recursive:
            parent_id = None
            if isinstance(message, UUID):
                parent_id = self.db.query(Message.parent_id).where(Message.id == message).first()[0]
            elif isinstance(message, Message):
                parent_id = message.parent_id

            if parent_id is None:
                return

            # Fetching the entire parent_message so there is no parent_id query executed after
            parent_message: Message = self.db.query(Message).where(Message.id == parent_id).first()

            if parent_message is not None:
                self.reintroduce_deleted_message(parent_message)

    def get_stats(self) -> SystemStats:
        """
        Get data stats such as number of all messages in the system,
        number of deleted and active messages and number of message trees.
        """
        # With columns: lang, deleted, count
        group_count_query = self.db.query(Message.lang, Message.deleted, func.count()).group_by(
            Message.lang, Message.deleted
        )
        # With columns: None, None, count
        msg_tree_query = self.db.query(None, None, func.count(Message.id)).filter(Message.parent_id.is_(None))
        # Union both queries, so that we can fetch the counts in one database query
        query = group_count_query.union_all(msg_tree_query)

Review comment (suggestion):
`Message.id == message` looks a bit strange, should it be `message_id`? Do you want to undelete all descendants? The easiest would be to use the `fetch_message_descendants()` method and use ORM to set deleted = False .. usually message trees have <100 messages.

Code after:
                    update(Message).filter(Message.parent_id.in_(parent_ids)).values(deleted=True).returning(Message.id)
                )

                parent_ids = self.db.execute(query).scalars().all()

    @managed_tx_method(CommitMode.COMMIT)
    def undelete_deleted_message(self, message: Message | UUID):
        """
        Undelete deleted messages and all their parents.
        """
        message_id = None
        if isinstance(message, UUID):
            message_id = message
        elif isinstance(message, Message):
            message_id = message.id
        else:
            raise OasstError("Server error", OasstErrorCode.SERVER_ERROR1, HTTPStatus.INTERNAL_SERVER_ERROR)

        query = update(Message).where(Message.id == message_id).values(deleted=False)
        self.db.execute(query)

        parent_id = None
        if isinstance(message, UUID):
            parent_id = self.db.query(Message.parent_id).where(Message.id == message_id).first()[0]
        elif isinstance(message, Message):
            parent_id = message.parent_id

        if parent_id is None:
            return

        # Fetching the entire parent_message so there is no parent_id query executed after
        parent_message: Message = self.db.query(Message).where(Message.id == parent_id).first()

        if parent_message is not None:
            self.undelete_deleted_message(parent_message)

    def get_stats(self) -> SystemStats:
        """
        Get data stats such as number of all messages in the system,
        number of deleted and active messages and number of message trees.
        """
        # With columns: lang, deleted, count
        group_count_query = self.db.query(Message.lang, Message.deleted, func.count()).group_by(
            Message.lang, Message.deleted
        )
        # With columns: None, None, count
        msg_tree_query = self.db.query(None, None, func.count(Message.id)).filter(Message.parent_id.is_(None))
        # Union both queries, so that we can fetch the counts in one database query
        query = group_count_query.union_all(msg_tree_query)

        nactives = 0