Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add mass pruneable blocks #137

Merged
merged 6 commits into from
Feb 27, 2024
Merged
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
11 changes: 11 additions & 0 deletions config/enjin-platform.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,15 @@
'api_url' => env('GITHUB_API_URL', 'https://api.github.com/'),
'token' => env('GITHUB_TOKEN'),
],

/*
|--------------------------------------------------------------------------
| Prune blocks
|--------------------------------------------------------------------------
|
| Here, you can specify the number of days to retain blocks data before pruning.
| If set to null or zero, blocks will not be pruned.
|
*/
'prune_blocks' => env('PRUNE_BLOCKS', 7),
];
2 changes: 1 addition & 1 deletion database/factories/BlockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class BlockFactory extends Factory
public function definition()
{
return [
'number' => random_int(1, 1000),
'number' => $this->faker->numberBetween(1, 1000000),
'hash' => $this->faker->unique()->public_key(),
];
}
Expand Down
17 changes: 17 additions & 0 deletions src/Models/Laravel/Block.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
use Enjin\Platform\Database\Factories\BlockFactory;
use Enjin\Platform\Models\BaseModel;
use Enjin\Platform\Models\Laravel\Traits\EagerLoadSelectFields;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\MassPrunable;

class Block extends BaseModel
{
use HasFactory;
use EagerLoadSelectFields;
use MassPrunable;


/**
Expand Down Expand Up @@ -38,6 +41,20 @@ class Block extends BaseModel
'updated_at',
];

/**
* Get the prunable model query.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function prunable(): Builder
{
if (!empty($days = config('enjin-platform.prune_blocks'))) {
return static::where('created_at', '<=', now()->subDays($days));
}

return static::where('id', 0);
}

/**
* Create a new factory instance for the model.
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/Feature/Commands/PruneTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Enjin\Platform\Tests\Feature\Commands;

use Enjin\Platform\Models\Block;
use Enjin\Platform\Models\PendingEvent;
use Enjin\Platform\Tests\Feature\GraphQL\TestCaseGraphQL;

Expand Down Expand Up @@ -34,4 +35,29 @@ public function test_it_cannot_prune_expired_events(): void
$this->artisan('model:prune', ['--model' => PendingEvent::resolveClassFqn()]);
$this->assertNotEmpty(PendingEvent::count());
}

public function test_it_can_prune_expired_blocks(): void
{
Block::truncate();
Block::factory(fake()->numberBetween(1, 100))->create([
'created_at' => now()->subDays(config('enjin-platform.prune_blocks') + 1)->toDateTimeString(),
]);
$this->artisan('model:prune', ['--model' => Block::resolveClassFqn()]);
$this->assertDatabaseCount('blocks', 0);
}

public function test_it_cannot_prune_expired_blocks(): void
{
config(['enjin-platform.prune_blocks' => null]);
Block::truncate();
Block::factory(fake()->numberBetween(1, 100))->create([
'created_at' => now()->subDays(config('enjin-platform.prune_blocks') + 1)->toDateTimeString(),
]);
$this->artisan('model:prune', ['--model' => Block::resolveClassFqn()]);
$this->assertNotEmpty(Block::count());

config(['enjin-platform.prune_blocks' => 0]);
$this->artisan('model:prune', ['--model' => Block::resolveClassFqn()]);
$this->assertNotEmpty(Block::count());
}
}
Loading