-
Notifications
You must be signed in to change notification settings - Fork 322
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
378 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
lib/shared/layers/python-sdk/python/genai_core/aurora/delete.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import os | ||
import boto3 | ||
import genai_core.utils.delete_files_with_prefix | ||
from psycopg2 import sql | ||
from genai_core.aurora.connection import AuroraConnection | ||
|
||
PROCESSING_BUCKET_NAME = os.environ["PROCESSING_BUCKET_NAME"] | ||
UPLOAD_BUCKET_NAME = os.environ["UPLOAD_BUCKET_NAME"] | ||
WORKSPACES_TABLE_NAME = os.environ["WORKSPACES_TABLE_NAME"] | ||
DOCUMENTS_TABLE_NAME = os.environ.get("DOCUMENTS_TABLE_NAME") | ||
|
||
WORKSPACE_OBJECT_TYPE = "workspace" | ||
|
||
dynamodb = boto3.resource("dynamodb") | ||
|
||
|
||
def delete_aurora_workspace(workspace: dict): | ||
workspace_id = workspace["workspace_id"] | ||
genai_core.utils.delete_files_with_prefix.delete_files_with_prefix( | ||
UPLOAD_BUCKET_NAME, workspace_id | ||
) | ||
genai_core.utils.delete_files_with_prefix.delete_files_with_prefix( | ||
PROCESSING_BUCKET_NAME, workspace_id | ||
) | ||
|
||
table_name = sql.Identifier(workspace_id.replace("-", "")) | ||
with AuroraConnection(autocommit=False) as cursor: | ||
cursor.execute( | ||
sql.SQL("DROP TABLE IF EXISTS {table};").format(table=table_name) | ||
) | ||
|
||
workspaces_table = dynamodb.Table(WORKSPACES_TABLE_NAME) | ||
documents_table = dynamodb.Table(DOCUMENTS_TABLE_NAME) | ||
|
||
items_to_delete = [] | ||
last_evaluated_key = None | ||
while True: | ||
query_args = { | ||
"KeyConditionExpression": boto3.dynamodb.conditions.Key("workspace_id").eq( | ||
workspace_id | ||
) | ||
} | ||
|
||
if last_evaluated_key: | ||
query_args["ExclusiveStartKey"] = last_evaluated_key | ||
|
||
response = documents_table.query(**query_args) | ||
items_to_delete.extend(response["Items"]) | ||
|
||
last_evaluated_key = response.get("LastEvaluatedKey") | ||
if not last_evaluated_key: | ||
break | ||
|
||
# Batch delete in groups of 25 | ||
for i in range(0, len(items_to_delete), 25): | ||
with documents_table.batch_writer() as batch: | ||
for item in items_to_delete[i : i + 25]: | ||
batch.delete_item( | ||
Key={ | ||
"workspace_id": item["workspace_id"], | ||
"document_id": item["document_id"], | ||
} | ||
) | ||
|
||
print(f"Deleted {len(items_to_delete)} items.") | ||
|
||
response = workspaces_table.delete_item( | ||
Key={"workspace_id": workspace_id, "object_type": WORKSPACE_OBJECT_TYPE}, | ||
) | ||
|
||
print(f"Delete Item succeeded: {response}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.