2
2
from migrate_anything import configure
3
3
from migrate_anything .storage import Storage
4
4
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
7
8
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
35
10
36
11
37
12
app = create_app ('time_tracker_api.config.CLIConfig' )
38
- from commons .data_access_layer .cosmos_db import cosmos_helper , init_app , CosmosDBRepository
39
13
40
14
if cosmos_helper is None :
41
15
init_app (app )
@@ -59,19 +33,38 @@ def __init__(self, collection_id, app_id):
59
33
self .repository = CosmosDBRepository .from_definition (migrations_definition )
60
34
61
35
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
+ )
66
46
67
47
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 )
69
50
return [
70
51
[item ['name' ], item ['code' ]] for item in migrations
71
52
]
72
53
73
54
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
+ )
76
69
77
70
configure (storage = CosmosDBStorage ("migration" , "time-tracker-api" ))
0 commit comments