generated from softwareone-platform/swo-extension-playground
-
Notifications
You must be signed in to change notification settings - Fork 0
MPT-18132: add command to run a single migration #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| from mpt_tool.enums import MigrationStatusEnum, MigrationTypeEnum | ||
| from mpt_tool.managers import StateManager | ||
| from mpt_tool.managers.errors import StateNotFoundError | ||
| from mpt_tool.models import Migration | ||
|
|
||
|
|
||
| class MigrationStateService: | ||
| """Shared migration state operations for migration use cases.""" | ||
|
|
||
| def __init__(self, state_manager: StateManager) -> None: | ||
| self._state_manager = state_manager | ||
|
|
||
| def get_or_create_state( | ||
| self, migration_id: str, migration_type: MigrationTypeEnum, order_id: int | ||
| ) -> Migration: | ||
| """Return existing migration state, creating it if needed.""" | ||
| try: | ||
| state = self._state_manager.get_by_id(migration_id) | ||
| except StateNotFoundError: | ||
| state = self._state_manager.new(migration_id, migration_type, order_id) | ||
|
|
||
| return state | ||
|
|
||
| def save_state(self, state: Migration, status: MigrationStatusEnum) -> None: | ||
| """Apply status transition and persist migration state.""" | ||
| match status: | ||
| case MigrationStatusEnum.APPLIED: | ||
| state.applied() | ||
| case MigrationStatusEnum.FAILED: | ||
| state.failed() | ||
| case MigrationStatusEnum.MANUAL_APPLIED: | ||
| state.manual() | ||
| case MigrationStatusEnum.RUNNING: | ||
| state.start() | ||
|
|
||
| self._state_manager.save_state(state) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| import logging | ||
|
|
||
| from mpt_tool.enums import MigrationStatusEnum, MigrationTypeEnum | ||
| from mpt_tool.managers import FileMigrationManager, StateManager, StateManagerFactory | ||
| from mpt_tool.managers.errors import LoadMigrationError, MigrationFolderError | ||
| from mpt_tool.migration.base import BaseMigration | ||
| from mpt_tool.models import MigrationFile | ||
| from mpt_tool.services.migration_state import MigrationStateService | ||
| from mpt_tool.use_cases.errors import RunMigrationError | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class RunSingleMigrationUseCase: | ||
| """Use case for running a single migration.""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| file_migration_manager: FileMigrationManager | None = None, | ||
| state_manager: StateManager | None = None, | ||
| state_service: MigrationStateService | None = None, | ||
| ): | ||
| self.file_migration_manager = file_migration_manager or FileMigrationManager() | ||
| self.state_manager = state_manager or StateManagerFactory.get_instance() | ||
| self.state_service = state_service or MigrationStateService(self.state_manager) | ||
|
|
||
| def execute(self, migration_id: str, migration_type: MigrationTypeEnum) -> None: | ||
| """Run one migration by id and type. | ||
|
|
||
| Args: | ||
| migration_id: The migration id to run. | ||
| migration_type: The expected migration type. | ||
|
|
||
| Raises: | ||
| RunMigrationError: If an error occurs during migration execution. | ||
| """ | ||
| migration_file = self._get_migration_file(migration_id) | ||
| migration_instance = self._get_migration_instance_by_type(migration_file, migration_type) | ||
| state = self.state_service.get_or_create_state( | ||
| migration_file.migration_id, migration_type, migration_file.order_id | ||
| ) | ||
| if state.applied_at is not None: | ||
| raise RunMigrationError(f"Migration {migration_id} already applied") | ||
|
|
||
| logger.info("Running migration: %s", migration_file.migration_id) | ||
| self.state_service.save_state(state, status=MigrationStatusEnum.RUNNING) | ||
| try: | ||
| migration_instance.run() | ||
| # We catch all exceptions here to ensure the state is updated | ||
| # and the flow is not interrupted abruptly | ||
| except Exception as error: | ||
| self.state_service.save_state(state, status=MigrationStatusEnum.FAILED) | ||
| raise RunMigrationError( | ||
| f"Migration {migration_file.migration_id} failed: {error!s}" | ||
| ) from error | ||
|
|
||
| self.state_service.save_state(state, status=MigrationStatusEnum.APPLIED) | ||
|
|
||
| def _get_migration_file(self, migration_id: str) -> MigrationFile: | ||
| try: | ||
| migration_files = self.file_migration_manager.validate() | ||
| except MigrationFolderError as error: | ||
| raise RunMigrationError(str(error)) from error | ||
|
|
||
| migration_file = next( | ||
| (migration for migration in migration_files if migration.migration_id == migration_id), | ||
| None, | ||
| ) | ||
| if migration_file is None: | ||
| raise RunMigrationError(f"Migration {migration_id} not found") | ||
|
|
||
| return migration_file | ||
|
|
||
| def _get_migration_instance_by_type( | ||
| self, migration_file: MigrationFile, migration_type: MigrationTypeEnum | ||
| ) -> BaseMigration: | ||
| try: | ||
| migration_instance = self.file_migration_manager.load_migration(migration_file) | ||
| except LoadMigrationError as error: | ||
| raise RunMigrationError(str(error)) from error | ||
|
|
||
| if migration_instance.type != migration_type: | ||
| raise RunMigrationError( | ||
| f"Migration {migration_file.migration_id} is not a {migration_type} migration" | ||
| ) | ||
|
|
||
| return migration_instance |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.