
- Modified backup configuration queries in the DatabasesController to filter by team ID, ensuring proper access control. - Enhanced S3 storage retrieval to use the current team context for better data integrity. - Added a relationship method in ScheduledDatabaseBackup model to associate backups with teams.
74 lines
2.0 KiB
PHP
74 lines
2.0 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
|
use Illuminate\Database\Eloquent\Relations\MorphTo;
|
|
|
|
class ScheduledDatabaseBackup extends BaseModel
|
|
{
|
|
protected $guarded = [];
|
|
|
|
public function team()
|
|
{
|
|
return $this->belongsTo(Team::class);
|
|
}
|
|
|
|
public function database(): MorphTo
|
|
{
|
|
return $this->morphTo();
|
|
}
|
|
|
|
public function latest_log(): HasOne
|
|
{
|
|
return $this->hasOne(ScheduledDatabaseBackupExecution::class)->latest();
|
|
}
|
|
|
|
public function executions(): HasMany
|
|
{
|
|
// Last execution first
|
|
return $this->hasMany(ScheduledDatabaseBackupExecution::class)->orderBy('created_at', 'desc');
|
|
}
|
|
|
|
public function s3()
|
|
{
|
|
return $this->belongsTo(S3Storage::class, 's3_storage_id');
|
|
}
|
|
|
|
public function get_last_days_backup_status($days = 7)
|
|
{
|
|
return $this->hasMany(ScheduledDatabaseBackupExecution::class)->where('created_at', '>=', now()->subDays($days))->get();
|
|
}
|
|
|
|
public function executionsPaginated(int $skip = 0, int $take = 10)
|
|
{
|
|
$executions = $this->hasMany(ScheduledDatabaseBackupExecution::class)->orderBy('created_at', 'desc');
|
|
$count = $executions->count();
|
|
$executions = $executions->skip($skip)->take($take)->get();
|
|
|
|
return [
|
|
'count' => $count,
|
|
'executions' => $executions,
|
|
];
|
|
}
|
|
|
|
public function server()
|
|
{
|
|
if ($this->database) {
|
|
if ($this->database instanceof ServiceDatabase) {
|
|
$destination = data_get($this->database->service, 'destination');
|
|
$server = data_get($destination, 'server');
|
|
} else {
|
|
$destination = data_get($this->database, 'destination');
|
|
$server = data_get($destination, 'server');
|
|
}
|
|
if ($server) {
|
|
return $server;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|