Skip to content

Commit a2d0a3a

Browse files
committed
fix: update migration scripts to work with the events implementation
1 parent 8d9d9b7 commit a2d0a3a

File tree

3 files changed

+39
-39
lines changed

3 files changed

+39
-39
lines changed

commons/data_access_layer/cosmos_db.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ def delete(self, id: str, event_context: EventContext,
220220
'deleted': generate_uuid4()
221221
}, event_context, peeker=peeker, visible_only=True, mapper=mapper)
222222

223-
def delete_permanently(self, id: str, partition_key_value: str) -> None:
223+
def delete_permanently(self, id: str, event_context: EventContext) -> None:
224+
partition_key_value = self.find_partition_key_value(event_context)
224225
self.container.delete_item(id, partition_key_value)
225226

226227
def find_partition_key_value(self, event_context: EventContext):

commons/data_access_layer/database.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@ def delete(self, id):
3535

3636
class EventContext():
3737
def __init__(self, container_id: str, action: str, description: str = None,
38-
user_id: str = None, tenant_id: str = None, session_id: str = None):
38+
user_id: str = None, tenant_id: str = None, session_id: str = None,
39+
app_id: str = None):
3940
self._container_id = container_id
4041
self._action = action
4142
self._description = description
4243
self._user_id = user_id
4344
self._tenant_id = tenant_id
4445
self._session_id = session_id
46+
self._app_id = app_id
4547

4648
@property
4749
def container_id(self):
@@ -66,3 +68,7 @@ def tenant_id(self):
6668
@property
6769
def session_id(self):
6870
return self._session_id
71+
72+
@property
73+
def app_id(self):
74+
return self._app_id

migrations/__init__.py

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,14 @@
22
from migrate_anything import configure
33
from migrate_anything.storage import Storage
44

5-
from time_tracker_api import create_app
6-
5+
from commons.data_access_layer.database import EventContext
6+
from commons.data_access_layer.cosmos_db import cosmos_helper, init_app, \
7+
CosmosDBRepository
78

8-
class CustomStorage(object):
9-
def __init__(self, file):
10-
self.file = file
11-
12-
def save_migration(self, name, code):
13-
with open(self.file, "a", encoding="utf-8") as file:
14-
file.write("{},{}\n".format(name, code))
15-
16-
def list_migrations(self):
17-
try:
18-
with open(self.file, encoding="utf-8") as file:
19-
return [
20-
line.split(",")
21-
for line in file.readlines()
22-
if line.strip() # Skip empty lines
23-
]
24-
except FileNotFoundError:
25-
return []
26-
27-
def remove_migration(self, name):
28-
migrations = [
29-
migration for migration in self.list_migrations() if migration[0] != name
30-
]
31-
32-
with open(self.file, "w", encoding="utf-8") as file:
33-
for row in migrations:
34-
file.write("{},{}\n".format(*row))
9+
from time_tracker_api import create_app
3510

3611

3712
app = create_app('time_tracker_api.config.CLIConfig')
38-
from commons.data_access_layer.cosmos_db import cosmos_helper, init_app, CosmosDBRepository
3913

4014
if cosmos_helper is None:
4115
init_app(app)
@@ -59,19 +33,38 @@ def __init__(self, collection_id, app_id):
5933
self.repository = CosmosDBRepository.from_definition(migrations_definition)
6034

6135
def save_migration(self, name, code):
62-
self.repository.create({"id": name,
63-
"name": name,
64-
"code": code,
65-
"app_id": self.app_id})
36+
event_ctx = self.create_event_context('create')
37+
self.repository.create(
38+
data={
39+
"id": name,
40+
"name": name,
41+
"code": code,
42+
"app_id": self.app_id
43+
},
44+
event_context=event_ctx,
45+
)
6646

6747
def list_migrations(self):
68-
migrations = self.repository.find_all(self.app_id)
48+
event_ctx = self.create_event_context('read-many')
49+
migrations = self.repository.find_all(event_context=event_ctx)
6950
return [
7051
[item['name'], item['code']] for item in migrations
7152
]
7253

7354
def remove_migration(self, name):
74-
self.repository.delete_permanently(name, self.app_id)
75-
55+
event_ctx = self.create_event_context('delete-permanently')
56+
self.repository.delete_permanently(id=name, event_context=event_ctx)
57+
58+
def create_event_context(
59+
self,
60+
action: str = None,
61+
description: str = None
62+
) -> EventContext:
63+
return EventContext(
64+
container_id=self.collection_id,
65+
action=action,
66+
description=description,
67+
app_id=self.app_id
68+
)
7669

7770
configure(storage=CosmosDBStorage("migration", "time-tracker-api"))

0 commit comments

Comments
 (0)