|
| 1 | +import json |
| 2 | +import boto3 |
| 3 | +import logging |
| 4 | +from botocore.exceptions import ClientError |
| 5 | + |
| 6 | +# --- Configuration --- |
| 7 | +SOURCE_TABLE_NAME = "infra-core-api-uin-mapping" |
| 8 | +DESTINATION_TABLE_NAME = "infra-core-api-user-info" |
| 9 | +DESTINATION_ID_SUFFIX = "@illinois.edu" |
| 10 | + |
| 11 | +# --- Logging Setup --- |
| 12 | +logging.basicConfig( |
| 13 | + level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s" |
| 14 | +) |
| 15 | + |
| 16 | + |
| 17 | +def migrate_uin_hashes(): |
| 18 | + """ |
| 19 | + Scans the source table for netId and uinHash mappings and updates |
| 20 | + the corresponding user records in the destination table. |
| 21 | + """ |
| 22 | + try: |
| 23 | + dynamodb = boto3.resource("dynamodb") |
| 24 | + destination_table = dynamodb.Table(DESTINATION_TABLE_NAME) |
| 25 | + |
| 26 | + # A paginator is used to handle scanning tables of any size |
| 27 | + paginator = dynamodb.meta.client.get_paginator("scan") |
| 28 | + page_iterator = paginator.paginate(TableName=SOURCE_TABLE_NAME) |
| 29 | + |
| 30 | + scanned_count = 0 |
| 31 | + updated_count = 0 |
| 32 | + logging.info( |
| 33 | + f"Starting migration from '{SOURCE_TABLE_NAME}' to '{DESTINATION_TABLE_NAME}'" |
| 34 | + ) |
| 35 | + |
| 36 | + for page in page_iterator: |
| 37 | + for item in page.get("Items", []): |
| 38 | + scanned_count += 1 |
| 39 | + net_id = item.get("netId") |
| 40 | + uin_hash = item.get("uinHash") |
| 41 | + |
| 42 | + # Validate that the necessary fields exist |
| 43 | + if not net_id or not uin_hash: |
| 44 | + logging.warning( |
| 45 | + f"Skipping item with missing 'netId' or 'uinHash': {item}" |
| 46 | + ) |
| 47 | + continue |
| 48 | + |
| 49 | + # Construct the primary key and update parameters for the destination table |
| 50 | + destination_pk_id = f"{net_id}{DESTINATION_ID_SUFFIX}" |
| 51 | + update_expression = "SET uinHash = :uh, netId = :ne" |
| 52 | + expression_attribute_values = {":uh": uin_hash, ":ne": net_id} |
| 53 | + |
| 54 | + # Update the item in the destination DynamoDB table |
| 55 | + try: |
| 56 | + destination_table.update_item( |
| 57 | + Key={"id": destination_pk_id}, |
| 58 | + UpdateExpression=update_expression, |
| 59 | + ExpressionAttributeValues=expression_attribute_values, |
| 60 | + ) |
| 61 | + updated_count += 1 |
| 62 | + if updated_count % 100 == 0: |
| 63 | + logging.info( |
| 64 | + f"Scanned {scanned_count} items, updated {updated_count} so far..." |
| 65 | + ) |
| 66 | + except ClientError as e: |
| 67 | + logging.error( |
| 68 | + f"Failed to update item with id '{destination_pk_id}': {e}" |
| 69 | + ) |
| 70 | + |
| 71 | + logging.info("--- Script Finished ---") |
| 72 | + logging.info(f"Total items scanned from source: {scanned_count}") |
| 73 | + logging.info(f"Total items updated in destination: {updated_count}") |
| 74 | + |
| 75 | + except ClientError as e: |
| 76 | + # This will catch errors like table not found, or credential issues |
| 77 | + logging.critical(f"A critical AWS error occurred: {e}") |
| 78 | + except Exception as e: |
| 79 | + logging.critical(f"An unexpected error occurred: {e}") |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + migrate_uin_hashes() |
0 commit comments