Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/Jobs/Maintenance.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

Expand Down Expand Up @@ -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();
}
}
}
8 changes: 8 additions & 0 deletions src/Models/Character/CharacterInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down