diff --git a/src/Jobs/Maintenance.php b/src/Jobs/Maintenance.php index 741d0b43..9fc6e134 100644 --- a/src/Jobs/Maintenance.php +++ b/src/Jobs/Maintenance.php @@ -74,8 +74,8 @@ public function handle() $this->cleanup_tables(); if (setting('cleanup_data', true) == 'yes') { - $this->cleanup_characters(); $this->cleanup_corps(); + $this->cleanup_characters(); } } @@ -110,21 +110,31 @@ public function cleanup_tables() private function cleanup_characters() { - CharacterInfo::doesntHave('refresh_token')->delete(); + // Only remove Chars which the token was deleted more than 30 days ago. + CharacterInfo::doesntHave('token_with_trashed') + ->orwhereRelation('token_with_trashed', 'deleted_at', '<', now()->subDay(30)) + ->delete(); } private function cleanup_corps() { // Need to find all corps that dont have a reason to be kept (no chars with tokens and not part of an alliance that has an active member) - Alliance::doesntHave('corporations.characters.refresh_token')->each(function ($alliance) { + Alliance::where(function ($query) { + $query->doesntHave('corporations.characters.token_with_trashed'); + $query->orWhereRelation('corporations.characters.token_with_trashed', 'deleted_at', '<', now()->subDay(30)); + }) + ->each(function ($alliance) { $alliance->corporations()->whereNotBetween('alliance_members.corporation_id', [1000000, 1999999])->delete(); }); // Now delete all the corps that have no alliance and no active tokens CorporationInfo::whereNotBetween('corporation_id', [1000000, 1999999]) ->doesntHave('alliance') - ->doesntHave('characters.refresh_token') + ->where(function ($query) { + $query->doesntHave('characters.token_with_trashed'); + $query->orWhereRelation('characters.token_with_trashed', 'deleted_at', '<', now()->subDay(30)); + }) ->delete(); } -} +} \ No newline at end of file diff --git a/src/Models/Character/CharacterInfo.php b/src/Models/Character/CharacterInfo.php index 7abc9ae2..25120e6f 100644 --- a/src/Models/Character/CharacterInfo.php +++ b/src/Models/Character/CharacterInfo.php @@ -401,6 +401,14 @@ public function refresh_token() return $this->hasOne(RefreshToken::class, 'character_id', 'character_id'); } + /** + * @return \Illuminate\Database\Eloquent\Relations\HasOne + */ + public function token_with_trashed() + { + return $this->hasOne(RefreshToken::class, 'character_id', 'character_id')->withTrashed(); + } + /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */