Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion libs/agno/agno/db/firestore/firestore.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def __init__(
self,
db_client: Optional[Client] = None,
project_id: Optional[str] = None,
database_id: Optional[str] = None,
session_collection: Optional[str] = None,
memory_collection: Optional[str] = None,
metrics_collection: Optional[str] = None,
Expand All @@ -53,6 +54,7 @@ def __init__(
Args:
db_client (Optional[Client]): The Firestore client to use.
project_id (Optional[str]): The GCP project ID for Firestore.
database_id (Optional[str]): The Firestore database ID (defaults to "(default)").
session_collection (Optional[str]): Name of the collection to store sessions.
memory_collection (Optional[str]): Name of the collection to store memories.
metrics_collection (Optional[str]): Name of the collection to store metrics.
Expand Down Expand Up @@ -86,6 +88,7 @@ def __init__(

self.project_id: Optional[str] = project_id
self.db_client: Client = _client
self.database_id: str = database_id or "(default)"

# -- DB methods --

Expand Down Expand Up @@ -186,7 +189,7 @@ def _get_or_create_collection(
if not hasattr(self, f"_{collection_name}_initialized"):
if not create_collection_if_not_found:
return None
create_collection_indexes(self.db_client, collection_name, collection_type)
create_collection_indexes(self.db_client, collection_name, collection_type, database_id=self.database_id)
setattr(self, f"_{collection_name}_initialized", True)

return collection_ref
Expand Down
10 changes: 6 additions & 4 deletions libs/agno/agno/db/firestore/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
# -- DB util methods --


def create_collection_indexes(client: Client, collection_name: str, collection_type: str) -> None:
def create_collection_indexes(client: Client, collection_name: str, collection_type: str, database_id: Optional[str] = None) -> None:
"""Create all required indexes for a collection including composite indexes.

This function automatically creates both single-field and composite indexes.
Expand All @@ -38,21 +38,23 @@ def create_collection_indexes(client: Client, collection_name: str, collection_t

# Create composite indexes programmatically
if composite_indexes:
_create_composite_indexes(client, collection_name, composite_indexes)
_create_composite_indexes(client, collection_name, composite_indexes, database_id=database_id)
log_debug(f"Collection '{collection_name}' initialized")

except Exception as e:
log_warning(f"Error processing indexes for {collection_type} collection: {e}")


def _create_composite_indexes(client: Client, collection_name: str, composite_indexes: List[Dict[str, Any]]) -> None:
def _create_composite_indexes(client: Client, collection_name: str, composite_indexes: List[Dict[str, Any]], database_id: Optional[str] = None) -> None:
"""Create composite indexes using Firestore Admin API."""
try:
project_id = client.project
if not project_id:
log_warning("Cannot create composite indexes: project_id not available from client")
return

db_id = database_id or "(default)"

admin_client = FirestoreAdminClient()

created_count = 0
Expand Down Expand Up @@ -83,7 +85,7 @@ def _create_composite_indexes(client: Client, collection_name: str, composite_in
)

# Create the index
parent_path = f"projects/{project_id}/databases/(default)/collectionGroups/{collection_name}"
parent_path = f"projects/{project_id}/databases/{db_id}/collectionGroups/{collection_name}"
admin_client.create_index(parent=parent_path, index=index)
created_count += 1

Expand Down