-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6eff39c
commit 521fe46
Showing
6 changed files
with
117 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace SaeedVaziry\Monitoring\Commands; | ||
|
||
use Exception; | ||
use Illuminate\Console\Command; | ||
use SaeedVaziry\Monitoring\Models\MonitoringRecord; | ||
|
||
class PurgeCommand extends Command | ||
{ | ||
/** | ||
* The name and signature of the console command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'monitoring:purge'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Purge old data'; | ||
|
||
/** | ||
* Create a new command instance. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return void | ||
* @throws Exception | ||
* | ||
*/ | ||
public function handle() | ||
{ | ||
MonitoringRecord::query() | ||
->where( | ||
'created_at', | ||
'<', | ||
date('Y-m-d', strtotime(config('monitoring.purge_before', '-1 day'))) | ||
) | ||
->delete(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace SaeedVaziry\Monitoring\Tests\Commands; | ||
|
||
use Illuminate\Foundation\Testing\RefreshDatabase; | ||
use SaeedVaziry\Monitoring\Database\Factories\MonitoringRecordFactory; | ||
use SaeedVaziry\Monitoring\Tests\TestCase; | ||
|
||
class PurgeCommandTest extends TestCase | ||
{ | ||
use RefreshDatabase; | ||
|
||
public function testPurge() | ||
{ | ||
config()->set('monitoring.purge_before', '-1 day'); | ||
|
||
MonitoringRecordFactory::times(10)->create([ | ||
'created_at' => now()->subDays(10) | ||
]); | ||
|
||
$this->artisan('monitoring:purge'); | ||
|
||
$this->assertDatabaseCount('monitoring_records', 0); | ||
} | ||
|
||
public function testPurgeLastHour() | ||
{ | ||
config()->set('monitoring.purge_before', '-1 hour'); | ||
|
||
MonitoringRecordFactory::times(10)->create([ | ||
'created_at' => now()->subMinutes(10) | ||
]); | ||
|
||
MonitoringRecordFactory::times(10)->create([ | ||
'created_at' => now()->subDays(10) | ||
]); | ||
|
||
$this->artisan('monitoring:purge'); | ||
|
||
$this->assertDatabaseCount('monitoring_records', 10); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters