22
33from mpt_tool .enums import MigrationStatusEnum , MigrationTypeEnum
44from mpt_tool .managers import FileMigrationManager , StateManager , StateManagerFactory
5- from mpt_tool .managers .errors import LoadMigrationError , MigrationFolderError , StateNotFoundError
5+ from mpt_tool .managers .errors import LoadMigrationError , MigrationFolderError
66from mpt_tool .migration .base import BaseMigration
7- from mpt_tool .models import Migration , MigrationFile
7+ from mpt_tool .models import MigrationFile
8+ from mpt_tool .services .migration_state import MigrationStateService
89from mpt_tool .use_cases .errors import RunMigrationError
910
1011logger = logging .getLogger (__name__ )
@@ -17,9 +18,11 @@ def __init__(
1718 self ,
1819 file_migration_manager : FileMigrationManager | None = None ,
1920 state_manager : StateManager | None = None ,
21+ state_service : MigrationStateService | None = None ,
2022 ):
2123 self .file_migration_manager = file_migration_manager or FileMigrationManager ()
2224 self .state_manager = state_manager or StateManagerFactory .get_instance ()
25+ self .state_service = state_service or MigrationStateService (self .state_manager )
2326
2427 def execute (self , migration_type : MigrationTypeEnum ) -> None : # noqa: WPS231
2528 """Run all migrations of a given type.
@@ -42,26 +45,26 @@ def execute(self, migration_type: MigrationTypeEnum) -> None: # noqa: WPS231
4245 if migration_instance is None :
4346 continue
4447
45- state = self ._get_or_create_state (
48+ state = self .state_service . get_or_create_state (
4649 migration_file .migration_id , migration_type , migration_file .order_id
4750 )
4851 if state .applied_at is not None :
4952 logger .debug ("Skipping applied migration: %s" , migration_file .migration_id )
5053 continue
5154
5255 logger .info ("Running migration: %s" , migration_file .migration_id )
53- self ._save_state (state , status = MigrationStatusEnum .RUNNING )
56+ self .state_service . save_state (state , status = MigrationStatusEnum .RUNNING )
5457 try :
5558 migration_instance .run ()
5659 # We catch all exceptions here to ensure the state is updated
5760 # and the flow is not interrupted abruptly
5861 except Exception as error :
59- self ._save_state (state , status = MigrationStatusEnum .FAILED )
62+ self .state_service . save_state (state , status = MigrationStatusEnum .FAILED )
6063 raise RunMigrationError (
6164 f"Migration { migration_file .migration_id } failed: { error !s} "
6265 ) from error
6366
64- self ._save_state (state , status = MigrationStatusEnum .APPLIED )
67+ self .state_service . save_state (state , status = MigrationStatusEnum .APPLIED )
6568
6669 def _get_migration_instance_by_type (
6770 self , migration_file : MigrationFile , migration_type : MigrationTypeEnum
@@ -75,24 +78,3 @@ def _get_migration_instance_by_type(
7578 return None
7679
7780 return migration_instance
78-
79- def _get_or_create_state (
80- self , migration_id : str , migration_type : MigrationTypeEnum , order_id : int
81- ) -> Migration :
82- try :
83- state = self .state_manager .get_by_id (migration_id )
84- except StateNotFoundError :
85- state = self .state_manager .new (migration_id , migration_type , order_id )
86-
87- return state
88-
89- def _save_state (self , state : Migration , status : MigrationStatusEnum ) -> None :
90- match status :
91- case MigrationStatusEnum .APPLIED :
92- state .applied ()
93- case MigrationStatusEnum .FAILED :
94- state .failed ()
95- case MigrationStatusEnum .RUNNING :
96- state .start ()
97-
98- self .state_manager .save_state (state )
0 commit comments