Skip to content

Commit 7a2f8cf

Browse files
[PRME-173] Update Bulk Order Upload Process to clear LG stitch table (#752)
* [PRME-173] Update Bulk Order Upload Process to clear LG stitch table * changes to make ruff.toml update work * Refactor removing_previous_uploads to truncate multiple tables in a loop --------- Co-authored-by: Adam Whiting <[email protected]>
1 parent 8b38bcc commit 7a2f8cf

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed

lambdas/models/access_audit.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def type(self) -> str:
3838

3939
@field_validator("request_type", mode="before")
4040
@classmethod
41-
def request_type(cls, value):
41+
def validate_request_type(cls, value):
4242
if value not in AccessAuditRequestType.list():
4343
raise ValueError("Invalid request type")
4444
return AccessAuditRequestType(value)
@@ -85,9 +85,6 @@ def sanitise_custom_reason(cls, value):
8585
@model_validator(mode="before")
8686
@classmethod
8787
def check_custom_reason(cls, data: Any) -> Any:
88-
if (
89-
DeceasedAccessReason.OTHER.value in data["reason_codes"]
90-
and not data["custom_reason"]
91-
):
88+
if DeceasedAccessReason.OTHER.value in data["reason_codes"] and not data["custom_reason"]:
9289
raise ValueError("Missing custom reason")
9390
return data

lambdas/requirements/requirements_test.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ pytest-mock==3.11.1
88
pytest-unordered==0.6.0
99
pytest==7.4.3
1010
requests_mock==1.11.0
11-
ruff==0.0.284
11+
ruff==0.12.8
1212
syrupy==4.9.1

lambdas/ruff.toml

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
# Enable the pycodestyle (`E`) and Pyflakes (`F`) rules by default.
2-
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
3-
# McCabe complexity (`C901`) by default.
4-
select = ["E", "F"]
5-
ignore = ["E402"]
6-
7-
# Allow autofix for all enabled rules (when `--fix`) is provided.
8-
fixable = ["ALL"]
9-
unfixable = []
10-
111
# Exclude a variety of commonly ignored directories.
122
exclude = [
133
".bzr",
@@ -32,13 +22,24 @@ exclude = [
3222
"node_modules",
3323
"venv",
3424
]
35-
per-file-ignores = {}
3625

3726
# Same as Black.
3827
line-length = 130
3928

29+
30+
[lint]
31+
32+
# Enable the pycodestyle (`E`) and Pyflakes (`F`) rules by default.
33+
# Unlike Flake8, Ruff doesn't enable pycodestyle warnings (`W`) or
34+
# McCabe complexity (`C901`) by default.
35+
select = ["E", "F"]
36+
ignore = []
37+
38+
# Allow autofix for all enabled rules (when `--fix`) is provided.
39+
fixable = ["ALL"]
40+
unfixable = []
41+
42+
per-file-ignores = {}
43+
4044
# Allow unused variables when underscore-prefixed.
4145
dummy-variable-rgx = "^(_+|(_+[a-zA-Z0-9_]*[a-zA-Z0-9]+?))$"
42-
43-
# Assume Python 3.8
44-
target-version = "py38"

tests/bulk-upload/scripts/setup_bulk_upload.py

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -415,11 +415,11 @@ def upload_lg_files_to_staging():
415415
def removing_previous_uploads():
416416
dynamodb = boto3.resource("dynamodb")
417417

418-
bulk_table = dynamodb.Table(BULK_UPLOAD_TABLE_NAME)
419-
scan_and_remove_items(bulk_table)
418+
tables_to_truncate = [BULK_UPLOAD_TABLE_NAME, LG_TABLE_NAME, LG_STITCH_TABLE_NAME]
420419

421-
bulk_table = dynamodb.Table(LG_TABLE_NAME)
422-
scan_and_remove_items(bulk_table)
420+
for table_name in tables_to_truncate:
421+
table = dynamodb.Table(table_name)
422+
scan_and_remove_items(table)
423423

424424

425425
def create_previous_uploads():
@@ -448,16 +448,29 @@ def create_previous_uploads():
448448

449449

450450
def scan_and_remove_items(table):
451-
# Scan the table to get all items
452-
response = table.scan()
453-
items = response["Items"]
451+
"""
452+
Scans all items in the provided DynamoDB table and removes them.
453+
This function performs a full table scan and deletes each item using a batch writer.
454+
It handles pagination to ensure all items are deleted, even if the scan result is paginated.
455+
Args:
456+
table: A boto3 DynamoDB Table resource instance.
457+
Returns:
458+
None
459+
Side Effects:
460+
Deletes all items from the specified DynamoDB table.
461+
Prints a confirmation message upon completion.
462+
"""
463+
464+
response: Dict[str, Any] = table.scan()
465+
items: list[Dict[str, Any]] = response["Items"]
454466

455467
with table.batch_writer() as batch:
456468
# Loop through the items and delete each one
457469
for item in items:
458470
batch.delete_item(Key={"ID": item["ID"]})
459471

460-
# Handle pagination if there are more items
472+
# No LastEvaluatedKey means no more items to retrieve
473+
# (no more pages, we just need to delete last page)
461474
while "LastEvaluatedKey" in response:
462475
response = table.scan(ExclusiveStartKey=response["LastEvaluatedKey"])
463476
items = response["Items"]
@@ -472,7 +485,7 @@ def create_items(table, items):
472485
for item in items:
473486
batch.put_item(Item=item)
474487
except ClientError as e:
475-
print(f"Error writing items: {e.response['Error']['Message']}")
488+
print(f"Error writing items: {e.response['Error']['Message']}") # type: ignore
476489
exit(1)
477490

478491

@@ -560,6 +573,7 @@ def get_user_input():
560573
LLOYD_GEORGE_BUCKET = f"{ENVIRONMENT}-lloyd-george-store"
561574
BULK_UPLOAD_TABLE_NAME = f"{ENVIRONMENT}_BulkUploadReport"
562575
LG_TABLE_NAME = f"{ENVIRONMENT}_LloydGeorgeReferenceMetadata"
576+
LG_STITCH_TABLE_NAME = f"{ENVIRONMENT}_LloydGeorgeStitchJobMetadata"
563577

564578
if not args.environment:
565579
env_confirmation = input(

0 commit comments

Comments
 (0)