From 4c8b5c72a2611d4c16fddb0ef8b82fa23215a897 Mon Sep 17 00:00:00 2001 From: Crypta-Eve Date: Mon, 7 Apr 2025 21:57:29 +0930 Subject: [PATCH 1/4] feat: add trashed_token relation to CharInfo --- src/Models/Character/CharacterInfo.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Models/Character/CharacterInfo.php b/src/Models/Character/CharacterInfo.php index 7abc9ae2..512bb794 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 trashed_token() + { + return $this->hasOne(RefreshToken::class, 'character_id', 'character_id')->withTrashed(); + } + /** * @return \Illuminate\Database\Eloquent\Relations\HasMany */ From 53ae0827628700aadffe3199b56b259379e5a427 Mon Sep 17 00:00:00 2001 From: Crypta-Eve Date: Mon, 7 Apr 2025 21:59:14 +0930 Subject: [PATCH 2/4] feat: only cleanup after 30d --- src/Jobs/Maintenance.php | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/Jobs/Maintenance.php b/src/Jobs/Maintenance.php index 741d0b43..b50002d5 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('trashed_token') + ->orwhereRelation('trashed_token', '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.trashed_token'); + $query->orWhereRelation('corporations.characters.trashed_token', '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.trashed_token'); + $query->orWhereRelation('characters.trashed_token', 'deleted_at', '<', now()->subDay(30)); + }) ->delete(); } -} +} \ No newline at end of file From 12be6b91971653c41eb7ffc120a4963852bf81c0 Mon Sep 17 00:00:00 2001 From: Crypta-Eve Date: Mon, 7 Apr 2025 22:02:40 +0930 Subject: [PATCH 3/4] style: styleci --- src/Jobs/Maintenance.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Jobs/Maintenance.php b/src/Jobs/Maintenance.php index b50002d5..c6eed94c 100644 --- a/src/Jobs/Maintenance.php +++ b/src/Jobs/Maintenance.php @@ -120,7 +120,7 @@ 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::where( function ($query) { + Alliance::where(function ($query) { $query->doesntHave('corporations.characters.trashed_token'); $query->orWhereRelation('corporations.characters.trashed_token', 'deleted_at', '<', now()->subDay(30)); }) @@ -131,7 +131,7 @@ private function cleanup_corps() // Now delete all the corps that have no alliance and no active tokens CorporationInfo::whereNotBetween('corporation_id', [1000000, 1999999]) ->doesntHave('alliance') - ->where(function ($query) { + ->where(function ($query) { $query->doesntHave('characters.trashed_token'); $query->orWhereRelation('characters.trashed_token', 'deleted_at', '<', now()->subDay(30)); }) From 63fa843d3a5c08523c7135cb5c7052f8d8d85b51 Mon Sep 17 00:00:00 2001 From: Crypta Eve Date: Tue, 8 Jul 2025 20:16:44 +0930 Subject: [PATCH 4/4] fix: rename trashed_token for clarity --- src/Jobs/Maintenance.php | 12 ++++++------ src/Models/Character/CharacterInfo.php | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Jobs/Maintenance.php b/src/Jobs/Maintenance.php index c6eed94c..9fc6e134 100644 --- a/src/Jobs/Maintenance.php +++ b/src/Jobs/Maintenance.php @@ -111,8 +111,8 @@ public function cleanup_tables() private function cleanup_characters() { // Only remove Chars which the token was deleted more than 30 days ago. - CharacterInfo::doesntHave('trashed_token') - ->orwhereRelation('trashed_token', 'deleted_at', '<', now()->subDay(30)) + CharacterInfo::doesntHave('token_with_trashed') + ->orwhereRelation('token_with_trashed', 'deleted_at', '<', now()->subDay(30)) ->delete(); } @@ -121,8 +121,8 @@ 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::where(function ($query) { - $query->doesntHave('corporations.characters.trashed_token'); - $query->orWhereRelation('corporations.characters.trashed_token', 'deleted_at', '<', now()->subDay(30)); + $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(); @@ -132,8 +132,8 @@ private function cleanup_corps() CorporationInfo::whereNotBetween('corporation_id', [1000000, 1999999]) ->doesntHave('alliance') ->where(function ($query) { - $query->doesntHave('characters.trashed_token'); - $query->orWhereRelation('characters.trashed_token', 'deleted_at', '<', now()->subDay(30)); + $query->doesntHave('characters.token_with_trashed'); + $query->orWhereRelation('characters.token_with_trashed', 'deleted_at', '<', now()->subDay(30)); }) ->delete(); } diff --git a/src/Models/Character/CharacterInfo.php b/src/Models/Character/CharacterInfo.php index 512bb794..25120e6f 100644 --- a/src/Models/Character/CharacterInfo.php +++ b/src/Models/Character/CharacterInfo.php @@ -404,7 +404,7 @@ public function refresh_token() /** * @return \Illuminate\Database\Eloquent\Relations\HasOne */ - public function trashed_token() + public function token_with_trashed() { return $this->hasOne(RefreshToken::class, 'character_id', 'character_id')->withTrashed(); }