soft delete models

This commit is contained in:
Andras Bacsai 2023-12-13 12:08:12 +01:00
parent 2dc175be63
commit 69343f974a
10 changed files with 133 additions and 30 deletions

View File

@ -115,16 +115,19 @@ private function cleanup_stucked_resources()
$applications = Application::all(); $applications = Application::all();
foreach ($applications as $application) { foreach ($applications as $application) {
if (!data_get($application, 'environment')) { if (!data_get($application, 'environment')) {
echo 'Application without environment: ' . $application->name . ' deleting\n'; echo 'Application without environment: ' . $application->name . ' soft deleting\n';
$application->delete(); $application->delete();
continue;
} }
if (!$application->destination()) { if (!$application->destination()) {
echo 'Application without destination: ' . $application->name . ' deleting\n'; echo 'Application without destination: ' . $application->name . ' soft deleting\n';
$application->delete(); $application->delete();
continue;
} }
if (!data_get($application, 'destination.server')) { if (!data_get($application, 'destination.server')) {
echo 'Application without server: ' . $application->name . ' deleting\n'; echo 'Application without server: ' . $application->name . ' soft deleting\n';
$application->delete(); $application->delete();
continue;
} }
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
@ -134,16 +137,19 @@ private function cleanup_stucked_resources()
$postgresqls = StandalonePostgresql::all(); $postgresqls = StandalonePostgresql::all();
foreach ($postgresqls as $postgresql) { foreach ($postgresqls as $postgresql) {
if (!data_get($postgresql, 'environment')) { if (!data_get($postgresql, 'environment')) {
echo 'Postgresql without environment: ' . $postgresql->name . ' deleting\n'; echo 'Postgresql without environment: ' . $postgresql->name . ' soft deleting\n';
$postgresql->delete(); $postgresql->delete();
continue;
} }
if (!$postgresql->destination()) { if (!$postgresql->destination()) {
echo 'Postgresql without destination: ' . $postgresql->name . ' deleting\n'; echo 'Postgresql without destination: ' . $postgresql->name . ' soft deleting\n';
$postgresql->delete(); $postgresql->delete();
continue;
} }
if (!data_get($postgresql, 'destination.server')) { if (!data_get($postgresql, 'destination.server')) {
echo 'Postgresql without server: ' . $postgresql->name . ' deleting\n'; echo 'Postgresql without server: ' . $postgresql->name . ' soft deleting\n';
$postgresql->delete(); $postgresql->delete();
continue;
} }
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
@ -153,16 +159,19 @@ private function cleanup_stucked_resources()
$redis = StandaloneRedis::all(); $redis = StandaloneRedis::all();
foreach ($redis as $redis) { foreach ($redis as $redis) {
if (!data_get($redis, 'environment')) { if (!data_get($redis, 'environment')) {
echo 'Redis without environment: ' . $redis->name . ' deleting\n'; echo 'Redis without environment: ' . $redis->name . ' soft deleting\n';
$redis->delete(); $redis->delete();
continue;
} }
if (!$redis->destination()) { if (!$redis->destination()) {
echo 'Redis without destination: ' . $redis->name . ' deleting\n'; echo 'Redis without destination: ' . $redis->name . ' soft deleting\n';
$redis->delete(); $redis->delete();
continue;
} }
if (!data_get($redis, 'destination.server')) { if (!data_get($redis, 'destination.server')) {
echo 'Redis without server: ' . $redis->name . ' deleting\n'; echo 'Redis without server: ' . $redis->name . ' soft deleting\n';
$redis->delete(); $redis->delete();
continue;
} }
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
@ -173,16 +182,19 @@ private function cleanup_stucked_resources()
$mongodbs = StandaloneMongodb::all(); $mongodbs = StandaloneMongodb::all();
foreach ($mongodbs as $mongodb) { foreach ($mongodbs as $mongodb) {
if (!data_get($mongodb, 'environment')) { if (!data_get($mongodb, 'environment')) {
echo 'Mongodb without environment: ' . $mongodb->name . ' deleting\n'; echo 'Mongodb without environment: ' . $mongodb->name . ' soft deleting\n';
$mongodb->delete(); $mongodb->delete();
continue;
} }
if (!$mongodb->destination()) { if (!$mongodb->destination()) {
echo 'Mongodb without destination: ' . $mongodb->name . ' deleting\n'; echo 'Mongodb without destination: ' . $mongodb->name . ' soft deleting\n';
$mongodb->delete(); $mongodb->delete();
continue;
} }
if (!data_get($mongodb, 'destination.server')) { if (!data_get($mongodb, 'destination.server')) {
echo 'Mongodb without server: ' . $mongodb->name . ' deleting\n'; echo 'Mongodb without server: ' . $mongodb->name . ' soft deleting\n';
$mongodb->delete(); $mongodb->delete();
continue;
} }
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
@ -193,16 +205,19 @@ private function cleanup_stucked_resources()
$mysqls = StandaloneMysql::all(); $mysqls = StandaloneMysql::all();
foreach ($mysqls as $mysql) { foreach ($mysqls as $mysql) {
if (!data_get($mysql, 'environment')) { if (!data_get($mysql, 'environment')) {
echo 'Mysql without environment: ' . $mysql->name . ' deleting\n'; echo 'Mysql without environment: ' . $mysql->name . ' soft deleting\n';
$mysql->delete(); $mysql->delete();
continue;
} }
if (!$mysql->destination()) { if (!$mysql->destination()) {
echo 'Mysql without destination: ' . $mysql->name . ' deleting\n'; echo 'Mysql without destination: ' . $mysql->name . ' soft deleting\n';
$mysql->delete(); $mysql->delete();
continue;
} }
if (!data_get($mysql, 'destination.server')) { if (!data_get($mysql, 'destination.server')) {
echo 'Mysql without server: ' . $mysql->name . ' deleting\n'; echo 'Mysql without server: ' . $mysql->name . ' soft deleting\n';
$mysql->delete(); $mysql->delete();
continue;
} }
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
@ -213,16 +228,19 @@ private function cleanup_stucked_resources()
$mariadbs = StandaloneMariadb::all(); $mariadbs = StandaloneMariadb::all();
foreach ($mariadbs as $mariadb) { foreach ($mariadbs as $mariadb) {
if (!data_get($mariadb, 'environment')) { if (!data_get($mariadb, 'environment')) {
echo 'Mariadb without environment: ' . $mariadb->name . ' deleting\n'; echo 'Mariadb without environment: ' . $mariadb->name . ' soft deleting\n';
$mariadb->delete(); $mariadb->delete();
continue;
} }
if (!$mariadb->destination()) { if (!$mariadb->destination()) {
echo 'Mariadb without destination: ' . $mariadb->name . ' deleting\n'; echo 'Mariadb without destination: ' . $mariadb->name . ' soft deleting\n';
$mariadb->delete(); $mariadb->delete();
continue;
} }
if (!data_get($mariadb, 'destination.server')) { if (!data_get($mariadb, 'destination.server')) {
echo 'Mariadb without server: ' . $mariadb->name . ' deleting\n'; echo 'Mariadb without server: ' . $mariadb->name . ' soft deleting\n';
$mariadb->delete(); $mariadb->delete();
continue;
} }
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
@ -233,16 +251,19 @@ private function cleanup_stucked_resources()
$services = Service::all(); $services = Service::all();
foreach ($services as $service) { foreach ($services as $service) {
if (!data_get($service, 'environment')) { if (!data_get($service, 'environment')) {
echo 'Service without environment: ' . $service->name . ' deleting\n'; echo 'Service without environment: ' . $service->name . ' soft deleting\n';
$service->delete(); $service->delete();
continue;
} }
if (!$service->destination()) { if (!$service->destination()) {
echo 'Service without destination: ' . $service->name . ' deleting\n'; echo 'Service without destination: ' . $service->name . ' soft deleting\n';
$service->delete(); $service->delete();
continue;
} }
if (!data_get($service, 'server')) { if (!data_get($service, 'server')) {
echo 'Service without server: ' . $service->name . ' deleting\n'; echo 'Service without server: ' . $service->name . ' soft deleting\n';
$service->delete(); $service->delete();
continue;
} }
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
@ -252,8 +273,9 @@ private function cleanup_stucked_resources()
$serviceApplications = ServiceApplication::all(); $serviceApplications = ServiceApplication::all();
foreach ($serviceApplications as $service) { foreach ($serviceApplications as $service) {
if (!data_get($service, 'service')) { if (!data_get($service, 'service')) {
echo 'ServiceApplication without service: ' . $service->name . ' deleting\n'; echo 'ServiceApplication without service: ' . $service->name . ' soft deleting\n';
$service->delete(); $service->delete();
continue;
} }
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
@ -263,8 +285,9 @@ private function cleanup_stucked_resources()
$serviceDatabases = ServiceDatabase::all(); $serviceDatabases = ServiceDatabase::all();
foreach ($serviceDatabases as $service) { foreach ($serviceDatabases as $service) {
if (!data_get($service, 'service')) { if (!data_get($service, 'service')) {
echo 'ServiceDatabase without service: ' . $service->name . ' deleting\n'; echo 'ServiceDatabase without service: ' . $service->name . ' soft deleting\n';
$service->delete(); $service->delete();
continue;
} }
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {

View File

@ -4,6 +4,7 @@
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Spatie\Activitylog\Models\Activity; use Spatie\Activitylog\Models\Activity;
use Illuminate\Support\Str; use Illuminate\Support\Str;
@ -13,6 +14,7 @@
class Application extends BaseModel class Application extends BaseModel
{ {
use SoftDeletes;
protected $guarded = []; protected $guarded = [];
protected static function booted() protected static function booted()

View File

@ -4,11 +4,12 @@
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
class ServiceApplication extends BaseModel class ServiceApplication extends BaseModel
{ {
use HasFactory; use HasFactory, SoftDeletes;
protected $guarded = []; protected $guarded = [];
protected static function booted() protected static function booted()

View File

@ -3,10 +3,11 @@
namespace App\Models; namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\SoftDeletes;
class ServiceDatabase extends BaseModel class ServiceDatabase extends BaseModel
{ {
use HasFactory; use HasFactory, SoftDeletes;
protected $guarded = []; protected $guarded = [];
protected static function booted() protected static function booted()

View File

@ -6,10 +6,11 @@
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class StandaloneMariadb extends BaseModel class StandaloneMariadb extends BaseModel
{ {
use HasFactory; use HasFactory,SoftDeletes;
protected $guarded = []; protected $guarded = [];
protected $casts = [ protected $casts = [

View File

@ -5,10 +5,11 @@
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class StandaloneMongodb extends BaseModel class StandaloneMongodb extends BaseModel
{ {
use HasFactory; use HasFactory, SoftDeletes;
protected $guarded = []; protected $guarded = [];
protected static function booted() protected static function booted()

View File

@ -5,10 +5,11 @@
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class StandaloneMysql extends BaseModel class StandaloneMysql extends BaseModel
{ {
use HasFactory; use HasFactory, SoftDeletes;
protected $guarded = []; protected $guarded = [];
protected $casts = [ protected $casts = [

View File

@ -5,10 +5,11 @@
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class StandalonePostgresql extends BaseModel class StandalonePostgresql extends BaseModel
{ {
use HasFactory; use HasFactory, SoftDeletes;
protected $guarded = []; protected $guarded = [];
protected $casts = [ protected $casts = [

View File

@ -5,10 +5,11 @@
use Illuminate\Database\Eloquent\Casts\Attribute; use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
class StandaloneRedis extends BaseModel class StandaloneRedis extends BaseModel
{ {
use HasFactory; use HasFactory, SoftDeletes;
protected $guarded = []; protected $guarded = [];
protected static function booted() protected static function booted()

View File

@ -0,0 +1,71 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('applications', function (Blueprint $table) {
$table->softDeletes();
});
Schema::table('standalone_postgresqls', function (Blueprint $table) {
$table->softDeletes();
});
Schema::table('standalone_redis', function (Blueprint $table) {
$table->softDeletes();
});
Schema::table('standalone_mongodbs', function (Blueprint $table) {
$table->softDeletes();
});
Schema::table('standalone_mysqls', function (Blueprint $table) {
$table->softDeletes();
});
Schema::table('standalone_mariadbs', function (Blueprint $table) {
$table->softDeletes();
});
Schema::table('service_applications', function (Blueprint $table) {
$table->softDeletes();
});
Schema::table('service_databases', function (Blueprint $table) {
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('applications', function (Blueprint $table) {
$table->dropSoftDeletes();
});
Schema::table('standalone_postgresqls', function (Blueprint $table) {
$table->dropSoftDeletes();
});
Schema::table('standalone_redis', function (Blueprint $table) {
$table->dropSoftDeletes();
});
Schema::table('standalone_mongodbs', function (Blueprint $table) {
$table->dropSoftDeletes();
});
Schema::table('standalone_mysqls', function (Blueprint $table) {
$table->dropSoftDeletes();
});
Schema::table('standalone_mariadbs', function (Blueprint $table) {
$table->dropSoftDeletes();
});
Schema::table('service_applications', function (Blueprint $table) {
$table->dropSoftDeletes();
});
Schema::table('service_databases', function (Blueprint $table) {
$table->dropSoftDeletes();
});
}
};