Creates a CSV file from the PostgreSQL data and returns the path to the temp file.
(
self,
parent_id: UUID,
store_type: StoreType,
columns: Optional[list[str]] = None,
filters: Optional[dict] = None,
include_header: bool = True,
)
| 561 | ) |
| 562 | |
| 563 | async def export_to_csv( |
| 564 | self, |
| 565 | parent_id: UUID, |
| 566 | store_type: StoreType, |
| 567 | columns: Optional[list[str]] = None, |
| 568 | filters: Optional[dict] = None, |
| 569 | include_header: bool = True, |
| 570 | ) -> tuple[str, IO]: |
| 571 | """Creates a CSV file from the PostgreSQL data and returns the path to |
| 572 | the temp file.""" |
| 573 | valid_columns = { |
| 574 | "id", |
| 575 | "name", |
| 576 | "category", |
| 577 | "description", |
| 578 | "parent_id", |
| 579 | "chunk_ids", |
| 580 | "metadata", |
| 581 | "created_at", |
| 582 | "updated_at", |
| 583 | } |
| 584 | |
| 585 | if not columns: |
| 586 | columns = list(valid_columns) |
| 587 | elif invalid_cols := set(columns) - valid_columns: |
| 588 | raise ValueError(f"Invalid columns: {invalid_cols}") |
| 589 | |
| 590 | select_stmt = f""" |
| 591 | SELECT |
| 592 | id::text, |
| 593 | name, |
| 594 | category, |
| 595 | description, |
| 596 | parent_id::text, |
| 597 | chunk_ids::text, |
| 598 | metadata::text, |
| 599 | to_char(created_at, 'YYYY-MM-DD HH24:MI:SS') AS created_at, |
| 600 | to_char(updated_at, 'YYYY-MM-DD HH24:MI:SS') AS updated_at |
| 601 | FROM {self._get_table_name(self._get_entity_table_for_store(store_type))} |
| 602 | """ |
| 603 | |
| 604 | conditions = ["parent_id = $1"] |
| 605 | params: list[Any] = [parent_id] |
| 606 | param_index = 2 |
| 607 | |
| 608 | if filters: |
| 609 | for field, value in filters.items(): |
| 610 | if field not in valid_columns: |
| 611 | continue |
| 612 | |
| 613 | if isinstance(value, dict): |
| 614 | for op, val in value.items(): |
| 615 | if op == "$eq": |
| 616 | conditions.append(f"{field} = ${param_index}") |
| 617 | params.append(val) |
| 618 | param_index += 1 |
| 619 | elif op == "$gt": |
| 620 | conditions.append(f"{field} > ${param_index}") |
nothing calls this directly
no test coverage detected