22from migrate_anything import configure
33from 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
3712app = create_app ('time_tracker_api.config.CLIConfig' )
38- from commons .data_access_layer .cosmos_db import cosmos_helper , init_app , CosmosDBRepository
3913
4014if 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
7770configure (storage = CosmosDBStorage ("migration" , "time-tracker-api" ))
0 commit comments