Skip to content

Commit

Permalink
Merge pull request #219 from wizzymore/fix-model-connection
Browse files Browse the repository at this point in the history
Fix model connection
  • Loading branch information
freekmurze authored Feb 12, 2024
2 parents 75dc464 + 4716ce4 commit 8f6a193
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/Models/HealthCheckResultHistoryItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ class HealthCheckResultHistoryItem extends Model

public function getConnectionName(): string
{
return config('health.result_stores.'.EloquentHealthResultStore::class.'.connection')
?: config('database.default');
return $this->connection ?:
config('health.result_stores.' . EloquentHealthResultStore::class . '.connection') ?:
config('database.default');
}

public function prunable(): Builder
{
$days = config('health.result_stores.'.EloquentHealthResultStore::class.'.keep_history_for_days') ?? 5;
$days = config('health.result_stores.' . EloquentHealthResultStore::class . '.keep_history_for_days') ?? 5;

return static::where('created_at', '<=', now()->subDays($days));
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Support/DbConnectionInfoTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
<?php

use Illuminate\Database\ConnectionResolverInterface;
use Spatie\Health\Models\HealthCheckResultHistoryItem;
use Spatie\Health\ResultStores\EloquentHealthResultStore;
use Spatie\Health\Support\DbConnectionInfo;
use Spatie\Health\Tests\TestClasses\CrashingHealthCheckResultHistoryItem;

it('can determine the table size in mb', function () {
$connection = app(ConnectionResolverInterface::class)->connection('mysql');
Expand All @@ -22,3 +25,21 @@

expect($size)->toBeGreaterThan(0);
});

it('correctly determines the connection of the model', function () {
$model = new CrashingHealthCheckResultHistoryItem();

expect($model->getConnectionName())->toBe('custom');

$model = new HealthCheckResultHistoryItem();

expect($model->getConnectionName())->toBe(config('database.default'));

config()->set('health.result_stores', [
EloquentHealthResultStore::class => [
'connection' => 'custom_in_config'
],
]);

expect($model->getConnectionName())->toBe(config('health.result_stores.' . EloquentHealthResultStore::class . '.connection'));
});
10 changes: 10 additions & 0 deletions tests/TestClasses/CrashingHealthCheckResultHistoryItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Spatie\Health\Tests\TestClasses;

use Spatie\Health\Models\HealthCheckResultHistoryItem;

class CrashingHealthCheckResultHistoryItem extends HealthCheckResultHistoryItem
{
protected $connection = 'custom';
}

0 comments on commit 8f6a193

Please sign in to comment.