Skip to content

Commit 970f281

Browse files
authored
backups: default is_successful to false (pterodactyl#3522)
* backups: default is_successful to false * backups: properly query backups
1 parent b19a164 commit 970f281

File tree

8 files changed

+54
-10
lines changed

8 files changed

+54
-10
lines changed

app/Http/Controllers/Api/Client/Servers/BackupController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ public function restore(Request $request, Server $server, Backup $backup): JsonR
225225
throw new BadRequestHttpException('This server is not currently in a state that allows for a backup to be restored.');
226226
}
227227

228-
if (!$backup->is_successful && !$backup->completed_at) {
228+
if (!$backup->is_successful && is_null($backup->completed_at)) {
229229
throw new BadRequestHttpException('This backup cannot be restored at this time: not completed or failed.');
230230
}
231231

app/Http/Requests/Api/Remote/ReportBackupCompleteRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ class ReportBackupCompleteRequest extends FormRequest
1212
public function rules()
1313
{
1414
return [
15-
'successful' => 'present|boolean',
15+
'successful' => 'required|boolean',
1616
'checksum' => 'nullable|string|required_if:successful,true',
1717
'checksum_type' => 'nullable|string|required_if:successful,true',
1818
'size' => 'nullable|numeric|required_if:successful,true',

app/Models/Backup.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ class Backup extends Model
6464
* @var array
6565
*/
6666
protected $attributes = [
67-
'is_successful' => true,
67+
'is_successful' => false,
6868
'is_locked' => false,
6969
'checksum' => null,
7070
'bytes' => 0,

app/Repositories/Eloquent/BackupRepository.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ public function getBackupsGeneratedDuringTimespan(int $server, int $seconds = 60
2525
return $this->getBuilder()
2626
->withTrashed()
2727
->where('server_id', $server)
28-
->where('is_successful', true)
28+
->where(function ($query) {
29+
$query->whereNull('completed_at')
30+
->orWhere('is_successful', '=', true);
31+
})
2932
->where('created_at', '>=', Carbon::now()->subSeconds($seconds)->toDateTimeString())
3033
->get()
3134
->toBase();

app/Services/Backups/DeleteBackupService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function handle(Backup $backup)
6363
// I also don't really see any reason you'd have a locked, failed backup to keep
6464
// around. The logic that updates the backup to the failed state will also remove
6565
// the lock, so this condition should really never happen.
66-
if ($backup->is_locked && ($backup->completed_at && $backup->is_successful)) {
66+
if ($backup->is_locked && ($backup->is_successful && !is_null($backup->completed_at))) {
6767
throw new BackupLockedException();
6868
}
6969

app/Services/Backups/InitiateBackupService.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,12 @@ public function handle(Server $server, string $name = null, bool $override = fal
132132
}
133133
}
134134

135-
// Check if the server has reached or exceeded it's backup limit.
136-
$successful = $server->backups()->where('is_successful', true);
135+
// Check if the server has reached or exceeded its backup limit.
136+
// completed_at == null will cover any ongoing backups, while is_successful == true will cover any completed backups.
137+
$successful = $server->backups()->where(function ($query) {
138+
$query->whereNull('completed_at')
139+
->orWhere('is_successful', true);
140+
});
137141
if (!$server->backup_limit || $successful->count() >= $server->backup_limit) {
138142
// Do not allow the user to continue if this server is already at its limit and can't override.
139143
if (!$override || $server->backup_limit <= 0) {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
use Illuminate\Support\Facades\Schema;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Database\Migrations\Migration;
6+
7+
class ChangeSuccessfulFieldToDefaultToFalseOnBackupsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::table('backups', function (Blueprint $table) {
17+
$table->boolean('is_successful')->after('uuid')->default(false)->change();
18+
});
19+
20+
// Convert currently processing backups to the new format so things don't break.
21+
DB::table('backups')->select('id')->where('is_successful', 1)->whereNull('completed_at')->update([
22+
'is_successful' => 0,
23+
]);
24+
}
25+
26+
/**
27+
* Reverse the migrations.
28+
*
29+
* @return void
30+
*/
31+
public function down()
32+
{
33+
Schema::table('backups', function (Blueprint $table) {
34+
$table->boolean('is_successful')->after('uuid')->default(true)->change();
35+
});
36+
}
37+
}

resources/scripts/components/server/backups/BackupRow.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export default ({ backup, className }: Props) => {
4444
<GreyRowBox css={tw`flex-wrap md:flex-nowrap items-center`} className={className}>
4545
<div css={tw`flex items-center truncate w-full md:flex-1`}>
4646
<div css={tw`mr-4`}>
47-
{backup.completedAt ?
47+
{backup.completedAt !== null ?
4848
backup.isLocked ?
4949
<FontAwesomeIcon icon={faLock} css={tw`text-yellow-500`}/>
5050
:
@@ -55,15 +55,15 @@ export default ({ backup, className }: Props) => {
5555
</div>
5656
<div css={tw`flex flex-col truncate`}>
5757
<div css={tw`flex items-center text-sm mb-1`}>
58-
{!backup.isSuccessful &&
58+
{backup.completedAt !== null && !backup.isSuccessful &&
5959
<span css={tw`bg-red-500 py-px px-2 rounded-full text-white text-xs uppercase border border-red-600 mr-2`}>
6060
Failed
6161
</span>
6262
}
6363
<p css={tw`break-words truncate`}>
6464
{backup.name}
6565
</p>
66-
{(backup.completedAt && backup.isSuccessful) &&
66+
{(backup.completedAt !== null && backup.isSuccessful) &&
6767
<span css={tw`ml-3 text-neutral-300 text-xs font-extralight hidden sm:inline`}>{bytesToHuman(backup.bytes)}</span>
6868
}
6969
</div>

0 commit comments

Comments
 (0)