2020-04-09 22:08:09 -07:00
|
|
|
<?php
|
|
|
|
|
|
2024-03-12 22:39:16 -04:00
|
|
|
namespace App\Services\Backups;
|
2020-04-09 22:08:09 -07:00
|
|
|
|
2025-09-24 13:34:19 +02:00
|
|
|
use App\Exceptions\Service\Backup\BackupLockedException;
|
2026-01-16 21:52:24 +01:00
|
|
|
use App\Extensions\BackupAdapter\BackupAdapterService;
|
2024-03-12 22:39:16 -04:00
|
|
|
use App\Models\Backup;
|
2025-06-07 14:16:01 +02:00
|
|
|
use Exception;
|
2025-09-24 13:34:19 +02:00
|
|
|
use Illuminate\Database\ConnectionInterface;
|
|
|
|
|
use Throwable;
|
2020-04-09 22:08:09 -07:00
|
|
|
|
|
|
|
|
class DeleteBackupService
|
|
|
|
|
{
|
2026-01-16 23:04:18 +01:00
|
|
|
public function __construct(private readonly ConnectionInterface $connection, private readonly BackupAdapterService $backupService) {}
|
2020-04-09 22:08:09 -07:00
|
|
|
|
|
|
|
|
/**
|
2021-06-13 10:26:47 -07:00
|
|
|
* Deletes a backup from the system. If the backup is stored in S3 a request
|
|
|
|
|
* will be made to delete that backup from the disk as well.
|
2020-04-09 22:08:09 -07:00
|
|
|
*
|
2025-09-08 13:12:33 -04:00
|
|
|
* @throws Throwable
|
2020-04-09 22:08:09 -07:00
|
|
|
*/
|
2022-10-14 10:59:20 -06:00
|
|
|
public function handle(Backup $backup): void
|
2020-04-09 22:08:09 -07:00
|
|
|
{
|
2021-06-13 10:26:47 -07:00
|
|
|
// If the backup is marked as failed it can still be deleted, even if locked
|
|
|
|
|
// since the UI doesn't allow you to unlock a failed backup in the first place.
|
|
|
|
|
//
|
|
|
|
|
// I also don't really see any reason you'd have a locked, failed backup to keep
|
|
|
|
|
// around. The logic that updates the backup to the failed state will also remove
|
|
|
|
|
// the lock, so this condition should really never happen.
|
2021-08-03 20:45:25 -06:00
|
|
|
if ($backup->is_locked && ($backup->is_successful && !is_null($backup->completed_at))) {
|
2021-05-03 21:26:09 -07:00
|
|
|
throw new BackupLockedException();
|
|
|
|
|
}
|
|
|
|
|
|
2026-01-20 09:45:35 +01:00
|
|
|
$schema = $this->backupService->get($backup->backupHost->schema);
|
2026-01-16 21:52:24 +01:00
|
|
|
if (!$schema) {
|
2026-01-20 09:45:35 +01:00
|
|
|
throw new Exception('Backup has unknown backup adapter.');
|
2020-05-09 19:43:58 -07:00
|
|
|
}
|
|
|
|
|
|
2026-01-16 21:52:24 +01:00
|
|
|
$this->connection->transaction(function () use ($schema, $backup) {
|
|
|
|
|
$schema->deleteBackup($backup);
|
2020-04-09 22:08:09 -07:00
|
|
|
|
2022-10-14 10:59:20 -06:00
|
|
|
$backup->delete();
|
2020-04-09 22:08:09 -07:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|