Skip to content

Commit 2c0a3eb

Browse files
committed
feat(data-migration): FTRS-1597 Increase test coverage and pre-review cleanups
1 parent a57d9dd commit 2c0a3eb

File tree

61 files changed

+6037
-2323
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+6037
-2323
lines changed

application/packages/ftrs_aws_local/dynamodb/cli.py

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
from typer import Typer
44

5-
from dynamodb.constants import ALL_ENTITY_TYPES, ClearableEntityType, TargetEnvironment
6-
from dynamodb.init import init_tables
7-
from dynamodb.reset import reset
5+
from dynamodb.init import init_command
6+
from dynamodb.reset import reset_command
87

98
logging.basicConfig(
109
level=logging.INFO,
@@ -19,22 +18,5 @@
1918
help="AWS Local CLI for managing local AWS services",
2019
)
2120

22-
typer_app.command("reset")(reset)
23-
24-
25-
@typer_app.command("init")
26-
def init_tables_command(
27-
endpoint_url: str | None = None,
28-
env: TargetEnvironment = TargetEnvironment.local,
29-
workspace: str | None = None,
30-
entity_type: list[ClearableEntityType] | None = None,
31-
) -> None:
32-
if entity_type is None:
33-
entity_type = list(ALL_ENTITY_TYPES)
34-
35-
init_tables(
36-
endpoint_url=endpoint_url,
37-
env=env,
38-
workspace=workspace,
39-
entity_type=entity_type,
40-
)
21+
typer_app.command("init")(init_command)
22+
typer_app.command("reset")(reset_command)

application/packages/ftrs_aws_local/dynamodb/constants.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,23 @@ class ClearableEntityType(StrEnum):
66
healthcare_service = "healthcare-service"
77
location = "location"
88
triage_code = "triage-code"
9-
state = "data-migration-state"
9+
state = "state-table"
1010

1111

1212
class RepositoryTypes(StrEnum):
1313
document = "document"
1414
field = "field"
1515

1616

17+
class TargetEnvironment(StrEnum):
18+
local = "local"
19+
dev = "dev"
20+
21+
1722
ALL_ENTITY_TYPES = [
1823
ClearableEntityType.organisation,
1924
ClearableEntityType.healthcare_service,
2025
ClearableEntityType.location,
2126
ClearableEntityType.triage_code,
2227
ClearableEntityType.state,
2328
]
24-
25-
26-
class TargetEnvironment(StrEnum):
27-
local = "local"
28-
dev = "dev"

application/packages/ftrs_aws_local/dynamodb/init.py

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
from typing import Annotated
2+
13
from ftrs_common.utils.db_service import format_table_name, get_dynamodb_client
24
from mypy_boto3_dynamodb import DynamoDBClient
5+
from typer import Option
36

4-
from dynamodb.constants import ClearableEntityType, TargetEnvironment
7+
from dynamodb.constants import ALL_ENTITY_TYPES, ClearableEntityType, TargetEnvironment
58
from dynamodb.logger import LOGGER, ResetLogBase
69

710

@@ -30,6 +33,7 @@ def init_tables(
3033
env: TargetEnvironment,
3134
workspace: str | None,
3235
entity_type: list[ClearableEntityType],
36+
delete_existing: bool = False,
3337
) -> None:
3438
LOGGER.log(ResetLogBase.ETL_RESET_001)
3539

@@ -39,9 +43,19 @@ def init_tables(
3943

4044
client = get_dynamodb_client(endpoint_url)
4145
for entity_name in entity_type:
42-
table_name = format_table_name(entity_name, env.value, workspace)
46+
stack_name = "database" if entity_name != "state-table" else "data-migration"
47+
table_name = format_table_name(
48+
entity_name,
49+
env.value,
50+
workspace,
51+
stack_name=stack_name,
52+
)
53+
4354
entity_config = get_entity_config(entity_name)
4455
try:
56+
if delete_existing:
57+
delete_table(client=client, table_name=table_name)
58+
4559
create_table(
4660
client=client,
4761
table_name=table_name,
@@ -166,7 +180,7 @@ def get_entity_config(entity_name: ClearableEntityType) -> dict:
166180
}
167181
],
168182
},
169-
"data-migration-state": {
183+
"state-table": {
170184
"key_schema": [
171185
{"AttributeName": "source_record_id", "KeyType": "HASH"},
172186
],
@@ -188,3 +202,34 @@ def get_entity_config(entity_name: ClearableEntityType) -> dict:
188202
},
189203
}
190204
return table_entity.get(entity_name, table_entity["default"])
205+
206+
207+
def delete_table(
208+
client: DynamoDBClient,
209+
table_name: str,
210+
) -> None:
211+
table_exists = client.list_tables()["TableNames"]
212+
if table_name in table_exists:
213+
client.delete_table(TableName=table_name)
214+
215+
216+
def init_command(
217+
endpoint_url: Annotated[str, Option(help="URL to connect to local DynamoDB")],
218+
workspace: Annotated[str | None, Option(help="Workspace name")] = None,
219+
entity_type: Annotated[
220+
list[ClearableEntityType] | None, Option(help="Entity types to clear")
221+
] = None,
222+
delete_existing: Annotated[
223+
bool, Option(help="Whether to delete existing tables before initialization")
224+
] = False,
225+
) -> None:
226+
if entity_type is None:
227+
entity_type = list(ALL_ENTITY_TYPES)
228+
229+
init_tables(
230+
endpoint_url=endpoint_url,
231+
env=TargetEnvironment.local,
232+
workspace=workspace,
233+
entity_type=entity_type,
234+
delete_existing=delete_existing,
235+
)

application/packages/ftrs_aws_local/dynamodb/reset.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from dynamodb.logger import LOGGER, ResetLogBase
1212

1313

14-
def reset(
14+
def reset_command(
1515
env: Annotated[
1616
TargetEnvironment, Option(help="Environment to clear the data from")
1717
],
@@ -45,11 +45,14 @@ def reset(
4545
session = Session()
4646

4747
for entity_name in entity_type:
48+
stack_name = "database" if entity_name != "state-table" else "data-migration"
4849
table_name = format_table_name(
49-
entity_name, TargetEnvironment(env).value, workspace
50+
entity_name,
51+
env.value,
52+
workspace,
53+
stack_name=stack_name,
5054
)
51-
count = delete_all_table_items(table_name, session, endpoint_url)
52-
55+
count = delete_all_table_items(session, table_name, endpoint_url)
5356
LOGGER.log(ResetLogBase.ETL_RESET_006, count=count, table_name=table_name)
5457

5558

0 commit comments

Comments
 (0)