From ea0cc3dab065722d227d9c7d00683afd2e476747 Mon Sep 17 00:00:00 2001 From: KKlochko Date: Thu, 9 Jan 2025 21:55:54 +0200 Subject: [PATCH] Add the remove commands. Fixed deleting to remove destinations with a backup plan. --- .../application/commands/__init__.py | 3 + .../remove_all_backup_plans_command.py | 8 ++ .../repository/backup_plan_repository.py | 3 +- .../cli/components/backup_plan/backup_plan.py | 2 + .../backup_plan/remove_backup_plan.py | 74 +++++++++++++++++++ 5 files changed, 89 insertions(+), 1 deletion(-) create mode 100644 tui_rsync/core/components/backup_plan/application/commands/__init__.py create mode 100644 tui_rsync/core/components/backup_plan/application/commands/remove_all_backup_plans_command.py create mode 100644 tui_rsync/user_interface/cli/components/backup_plan/remove_backup_plan.py diff --git a/tui_rsync/core/components/backup_plan/application/commands/__init__.py b/tui_rsync/core/components/backup_plan/application/commands/__init__.py new file mode 100644 index 0000000..7d15909 --- /dev/null +++ b/tui_rsync/core/components/backup_plan/application/commands/__init__.py @@ -0,0 +1,3 @@ +from .remove_all_backup_plans_command import RemoveAllBackupBackupPlansCommand + +__all__ = ['RemoveAllBackupBackupPlansCommand'] diff --git a/tui_rsync/core/components/backup_plan/application/commands/remove_all_backup_plans_command.py b/tui_rsync/core/components/backup_plan/application/commands/remove_all_backup_plans_command.py new file mode 100644 index 0000000..8dca16d --- /dev/null +++ b/tui_rsync/core/components/backup_plan/application/commands/remove_all_backup_plans_command.py @@ -0,0 +1,8 @@ +from tui_rsync.infrastructure.orm.models import BackupPlanModel, DestinationModel + + +class RemoveAllBackupBackupPlansCommand: + def execute(self) -> bool: + rows = BackupPlanModel.delete().execute() + rows = DestinationModel.delete().execute() + rows + return rows != 0 diff --git a/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py b/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py index 9e2c5d0..fc26c8d 100644 --- a/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py +++ b/tui_rsync/core/components/backup_plan/application/repository/backup_plan_repository.py @@ -7,7 +7,7 @@ from .backup_plan_repository_port import BackupPlanRepositoryPort from tui_rsync.core.components.backup_plan.domain import BackupPlan from tui_rsync.core.shared_kernel.components.common import UUID -from tui_rsync.infrastructure.orm.models import BackupPlanModel +from tui_rsync.infrastructure.orm.models import BackupPlanModel, DestinationModel class BackupPlanRepository(BackupPlanRepositoryPort): @@ -34,4 +34,5 @@ class BackupPlanRepository(BackupPlanRepositoryPort): def delete(self, uuid: UUID) -> bool: deleted = BackupPlanModel.delete().where(BackupPlanModel.id == uuid.id).execute() + deleted = DestinationModel.delete().where(DestinationModel.source == uuid.id).execute() | deleted return bool(deleted) diff --git a/tui_rsync/user_interface/cli/components/backup_plan/backup_plan.py b/tui_rsync/user_interface/cli/components/backup_plan/backup_plan.py index 54f2284..efd1e4b 100644 --- a/tui_rsync/user_interface/cli/components/backup_plan/backup_plan.py +++ b/tui_rsync/user_interface/cli/components/backup_plan/backup_plan.py @@ -21,7 +21,9 @@ import typer from .add_backup_plan import add_backup_plan from .show_backup_plan import show_backup_plan +from .remove_backup_plan import remove_backup_plan backup_plan = add_backup_plan backup_plan.add_typer(show_backup_plan, name="show", help="Show backup plans") +backup_plan.add_typer(remove_backup_plan, name="remove", help="Remove backup plans") diff --git a/tui_rsync/user_interface/cli/components/backup_plan/remove_backup_plan.py b/tui_rsync/user_interface/cli/components/backup_plan/remove_backup_plan.py new file mode 100644 index 0000000..4ac9bd3 --- /dev/null +++ b/tui_rsync/user_interface/cli/components/backup_plan/remove_backup_plan.py @@ -0,0 +1,74 @@ +################################################################################ +# Copyright (C) 2023 Kostiantyn Klochko # +# # +# This file is part of tui-rsync. # +# # +# tui-rsync is free software: you can redistribute it and/or modify it under # +# uthe terms of the GNU General Public License as published by the Free # +# Software Foundation, either version 3 of the License, or (at your option) # +# any later version. # +# # +# tui-rsync is distributed in the hope that it will be useful, but WITHOUT ANY # +# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS # +# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more # +# details. # +# # +# You should have received a copy of the GNU General Public License along with # +# tui-rsync. If not, see . # +################################################################################ + +from rich.console import Console +import typer + +from tui_rsync.core.components.backup_plan.application.commands import RemoveAllBackupBackupPlansCommand +from tui_rsync.core.components.backup_plan.application.repository.backup_plan_repository import BackupPlanRepository +from tui_rsync.core.components.backup_plan.application.services.backup_plan_service import BackupPlanService +from tui_rsync.core.shared_kernel.components.common import UUID +from tui_rsync.infrastructure.orm import SqliteDatabaseManager + +console = Console() +remove_backup_plan = typer.Typer() + + +@remove_backup_plan.command() +def one( + id: str = typer.Option( + None, "--id", "-i", + help="[b]The id[/] is an uniq identification of a [b]backup plan[/].", + show_default=False + ) +): + """ + [red b]Remove[/] an [yellow]existing backup plan[/]. + """ + + db = SqliteDatabaseManager() + repository = BackupPlanRepository(db) + service = BackupPlanService(repository) + + if id is None: + console.print("[red b][ERROR][/] Backup plan does not exists!!!") + return + + plan = service.delete(UUID(id)) + + if plan is None: + console.print("[red b][ERROR][/] Backup plan does not exists!!!") + return + + console.print(f"Removed the backup plan with {id}.") + + +@remove_backup_plan.command() +def all(): + """ + [red b]Remove[/] [yellow] all existing backup plans[/]. + """ + + db = SqliteDatabaseManager() + removed = RemoveAllBackupBackupPlansCommand().execute() + + if removed: + console.print(f"Removed all backup plans.") + else: + console.print(f"Nothing to remove. No backup plans.")