|
| 1 | +import argparse |
| 2 | +from typing import Iterable, Callable |
| 3 | +from services.base.dynamo_service import DynamoDBService |
| 4 | +from utils.audit_logging_setup import LoggingService |
| 5 | +from services.base.s3_service import S3Service |
| 6 | + |
| 7 | +class VersionMigration: |
| 8 | + filesize_key_field_name = "FileSize" |
| 9 | + s3_key_field_name = "S3FileKey" |
| 10 | + s3_version_id_field_name = "S3VersionID" |
| 11 | + |
| 12 | + def __init__(self, environment: str, table_name: str, dry_run: bool = False): |
| 13 | + self.environment = environment |
| 14 | + self.table_name = table_name |
| 15 | + self.dry_run = dry_run |
| 16 | + self.logger = LoggingService("S3Migration") |
| 17 | + self.dynamo_service = DynamoDBService() |
| 18 | + self.s3_service = S3Service() |
| 19 | + |
| 20 | + self.target_table = f"{self.environment}_{self.table_name}" |
| 21 | + |
| 22 | + def main( |
| 23 | + self, entries: Iterable[dict] |
| 24 | + ) -> list[tuple[str, Callable[[dict], dict | None]]]: |
| 25 | + |
| 26 | + """ |
| 27 | + Main entry point. Returns a list of update functions with labels. |
| 28 | + Accepts a list of entries for Lambda-based execution, or scans the table if `entries` is None. |
| 29 | + """ |
| 30 | + self.logger.info("Starting version migration") |
| 31 | + self.logger.info(f"Target table: {self.target_table}") |
| 32 | + self.logger.info(f"Dry run mode: {self.dry_run}") |
| 33 | + |
| 34 | + if entries is None: |
| 35 | + self.logger.error("No entries provided after scanning entire table.") |
| 36 | + raise ValueError("Entries must be provided to main().") |
| 37 | + |
| 38 | + return [ |
| 39 | + ("s3Metadata", self.update_s3_metadata_entry) |
| 40 | + ] |
| 41 | + |
| 42 | + def process_entries( |
| 43 | + self, |
| 44 | + label: str, |
| 45 | + entries: Iterable[dict], |
| 46 | + update_fn: Callable[[dict], dict | None], |
| 47 | + ): |
| 48 | + self.logger.info(f"Running {label} migration") |
| 49 | + |
| 50 | + for index, entry in enumerate(entries, start=1): |
| 51 | + item_id = entry.get("ID") |
| 52 | + |
| 53 | + # Add entry ID validation |
| 54 | + if not item_id: |
| 55 | + self.logger.error(f"[{label}] Item {index} missing ID field, skipping") |
| 56 | + continue |
| 57 | + |
| 58 | + self.logger.info( |
| 59 | + f"[{label}] Processing item {index} (ID: {item_id})" |
| 60 | + ) |
| 61 | + |
| 62 | + updated_fields = update_fn(entry) |
| 63 | + if not updated_fields: |
| 64 | + self.logger.info( |
| 65 | + f"[{label}] Item {item_id} does not require update, skipping." |
| 66 | + ) |
| 67 | + continue |
| 68 | + |
| 69 | + if self.dry_run: |
| 70 | + self.logger.info( |
| 71 | + f"[Dry Run] Would update item {item_id} with {updated_fields}" |
| 72 | + ) |
| 73 | + else: |
| 74 | + self.logger.info(f"Updating item {item_id} with {updated_fields}") |
| 75 | + try: |
| 76 | + self.dynamo_service.update_item( |
| 77 | + table_name=self.target_table, |
| 78 | + key_pair={"ID": item_id}, |
| 79 | + updated_fields=updated_fields, |
| 80 | + ) |
| 81 | + self.logger.info(f"Successfully updated item {item_id}") |
| 82 | + except Exception as e: |
| 83 | + self.logger.error(f"Failed to update item {item_id}: {str(e)}") |
| 84 | + continue |
| 85 | + |
| 86 | + self.logger.info(f"{label} migration completed.") |
| 87 | + |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + def parse_s3_path(s3_path: str) -> tuple[str, str] | None: |
| 91 | + """Parse S3 path into bucket and key components""" |
| 92 | + if not s3_path or not s3_path.startswith("s3://"): |
| 93 | + return None |
| 94 | + |
| 95 | + path = s3_path.removeprefix("s3://") |
| 96 | + parts = path.split("/", 1) |
| 97 | + |
| 98 | + if len(parts) != 2 or not parts[0] or not parts[1]: |
| 99 | + return None |
| 100 | + |
| 101 | + return parts[0], parts[1] |
| 102 | + |
| 103 | + def get_meta_data(self, s3_bucket: str, s3_key: str) -> tuple[int, str] | None: |
| 104 | + try: |
| 105 | + s3_head = self.s3_service.get_head_object(s3_bucket, s3_key) |
| 106 | + if s3_head: |
| 107 | + return s3_head.get('ContentLength'), s3_head.get('VersionId') |
| 108 | + except Exception as e: |
| 109 | + self.logger.error(f"Failed to retrieve S3 metadata for {s3_key}: {str(e)}") |
| 110 | + return None |
| 111 | + |
| 112 | + def update_s3_metadata_entry(self, entry: dict) -> dict | None: |
| 113 | + """Update entry with S3 metadata (FileSize, S3Key, S3VersionID)""" |
| 114 | + |
| 115 | + file_location = entry.get("FileLocation") |
| 116 | + if not file_location: |
| 117 | + self.logger.warning(f"Missing FileLocation for entry: {entry.get('ID')}") |
| 118 | + return None |
| 119 | + |
| 120 | + if not (pathResult := self.parse_s3_path(file_location)) or not all(pathResult): |
| 121 | + self.logger.warning(f"Invalid S3 path: {file_location}") |
| 122 | + return None |
| 123 | + s3_bucket, s3_key = pathResult |
| 124 | + |
| 125 | + if not (metadata := self.get_meta_data(s3_bucket, s3_key)) or not all(metadata): |
| 126 | + self.logger.warning(f"Could not retrieve S3 metadata for item {s3_key}") |
| 127 | + return None |
| 128 | + content_length, version_id = metadata |
| 129 | + |
| 130 | + updated_fields = {} |
| 131 | + |
| 132 | + if self.filesize_key_field_name not in entry: |
| 133 | + if content_length is None: |
| 134 | + self.logger.error(f"FileSize missing in both DynamoDB and S3 for item {s3_key}") |
| 135 | + return None |
| 136 | + updated_fields[self.filesize_key_field_name] = content_length |
| 137 | + |
| 138 | + if self.s3_key_field_name not in entry: |
| 139 | + updated_fields[self.s3_key_field_name] = s3_key |
| 140 | + |
| 141 | + if self.s3_version_id_field_name not in entry: |
| 142 | + if version_id is None: |
| 143 | + self.logger.error(f"S3VersionID missing in both DynamoDB and S3 for item {s3_key}") |
| 144 | + return None |
| 145 | + updated_fields[self.s3_version_id_field_name] = version_id |
| 146 | + |
| 147 | + return updated_fields if updated_fields else None |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + parser = argparse.ArgumentParser( |
| 151 | + prog="dynamodb_migration.py", |
| 152 | + description="Migrate DynamoDB table columns", |
| 153 | + ) |
| 154 | + parser.add_argument("environment", help="Environment prefix for DynamoDB table") |
| 155 | + parser.add_argument("table_name", help="DynamoDB table name to migrate") |
| 156 | + parser.add_argument( |
| 157 | + "--dry-run", |
| 158 | + action="store_true", |
| 159 | + help="Run migration in dry-run mode (no writes)", |
| 160 | + ) |
| 161 | + args = parser.parse_args() |
| 162 | + |
| 163 | + migration = VersionMigration( |
| 164 | + environment=args.environment, |
| 165 | + table_name=args.table_name, |
| 166 | + dry_run=args.dry_run, |
| 167 | + ) |
| 168 | + |
| 169 | + entries_to_process = list( |
| 170 | + migration.dynamo_service.scan_whole_table(migration.target_table) |
| 171 | + ) |
| 172 | + |
| 173 | + update_functions = migration.main(entries=entries_to_process) |
| 174 | + |
| 175 | + for label, fn in update_functions: |
| 176 | + migration.process_entries(label=label, entries=entries_to_process, update_fn=fn) |
0 commit comments