fix(deletion): fix DB deletion

- delete file mounts, volume mounts, envs, ssl crts, backups and detach tags correctly when deleting
This commit is contained in:
peaklabs-dev
2025-02-04 15:32:56 +01:00
parent c3a440a64e
commit 6de76ca3f8
4 changed files with 42 additions and 23 deletions

View File

@@ -66,12 +66,9 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueue
} }
if ($this->deleteVolumes && $this->resource->type() !== 'service') { if ($this->deleteVolumes && $this->resource->type() !== 'service') {
$this->resource?->delete_volumes($persistentStorages); $this->resource->delete_volumes($persistentStorages);
$this->resource->persistentStorages()->delete();
} }
if ($this->deleteConfigurations) {
$this->resource?->delete_configurations();
}
$isDatabase = $this->resource instanceof StandalonePostgresql $isDatabase = $this->resource instanceof StandalonePostgresql
|| $this->resource instanceof StandaloneRedis || $this->resource instanceof StandaloneRedis
|| $this->resource instanceof StandaloneMongodb || $this->resource instanceof StandaloneMongodb
@@ -80,6 +77,18 @@ class DeleteResourceJob implements ShouldBeEncrypted, ShouldQueue
|| $this->resource instanceof StandaloneKeydb || $this->resource instanceof StandaloneKeydb
|| $this->resource instanceof StandaloneDragonfly || $this->resource instanceof StandaloneDragonfly
|| $this->resource instanceof StandaloneClickhouse; || $this->resource instanceof StandaloneClickhouse;
if ($this->deleteConfigurations) {
$this->resource->delete_configurations(); // rename to FileStorages
$this->resource->fileStorages()->delete();
}
if ($isDatabase) {
$this->resource->sslCertificates()->delete();
$this->resource->scheduledBackups()->delete();
$this->resource->environment_variables()->delete();
$this->resource->tags()->detach();
}
$server = data_get($this->resource, 'server') ?? data_get($this->resource, 'destination.server'); $server = data_get($this->resource, 'server') ?? data_get($this->resource, 'destination.server');
if (($this->dockerCleanup || $isDatabase) && $server) { if (($this->dockerCleanup || $isDatabase) && $server) {
CleanupDocker::dispatch($server, true); CleanupDocker::dispatch($server, true);

View File

@@ -24,11 +24,6 @@ class LocalPersistentVolume extends Model
return $this->morphTo('resource'); return $this->morphTo('resource');
} }
public function standalone_postgresql()
{
return $this->morphTo('resource');
}
protected function name(): Attribute protected function name(): Attribute
{ {
return Attribute::make( return Attribute::make(

View File

@@ -25,9 +25,19 @@ class SslCertificate extends Model
'valid_until' => 'datetime', 'valid_until' => 'datetime',
]; ];
public function resource() public function application()
{ {
return $this->morphTo(); return $this->morphTo('resource');
}
public function service()
{
return $this->morphTo('resource');
}
public function database()
{
return $this->morphTo('resource');
} }
public function server() public function server()

View File

@@ -259,11 +259,21 @@ class StandalonePostgresql extends BaseModel
return $this->belongsTo(Environment::class); return $this->belongsTo(Environment::class);
} }
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');
}
public function fileStorages() public function fileStorages()
{ {
return $this->morphMany(LocalFileVolume::class, 'resource'); return $this->morphMany(LocalFileVolume::class, 'resource');
} }
public function sslCertificates()
{
return $this->morphMany(SslCertificate::class, 'resource');
}
public function destination() public function destination()
{ {
return $this->morphTo(); return $this->morphTo();
@@ -274,16 +284,17 @@ class StandalonePostgresql extends BaseModel
return $this->morphMany(EnvironmentVariable::class, 'resourceable'); return $this->morphMany(EnvironmentVariable::class, 'resourceable');
} }
public function persistentStorages()
{
return $this->morphMany(LocalPersistentVolume::class, 'resource');
}
public function scheduledBackups() public function scheduledBackups()
{ {
return $this->morphMany(ScheduledDatabaseBackup::class, 'database'); return $this->morphMany(ScheduledDatabaseBackup::class, 'database');
} }
public function environment_variables()
{
return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->orderBy('key', 'asc');
}
public function isBackupSolutionAvailable() public function isBackupSolutionAvailable()
{ {
return true; return true;
@@ -332,10 +343,4 @@ class StandalonePostgresql extends BaseModel
return $parsedCollection->toArray(); return $parsedCollection->toArray();
} }
public function environment_variables()
{
return $this->morphMany(EnvironmentVariable::class, 'resourceable')
->orderBy('key', 'asc');
}
} }