Skip to content

Commit

Permalink
Merge pull request #216 from eusonlito/main
Browse files Browse the repository at this point in the history
Added Different Database Connection Support
  • Loading branch information
freekmurze authored Feb 3, 2024
2 parents bcbd081 + ee37fee commit b313ffe
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config/health.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

return [

/*
* A result store is responsible for saving the results of the checks. The
* `EloquentHealthResultStore` will save results in the database. You
* can use multiple stores at the same time.
*/
'result_stores' => [
Spatie\Health\ResultStores\EloquentHealthResultStore::class => [
'connection' => env('HEALTH_DB_CONNECTION', env('DB_CONNECTION')),
'model' => Spatie\Health\Models\HealthCheckResultHistoryItem::class,
'keep_history_for_days' => 5,
],
Expand Down
6 changes: 4 additions & 2 deletions database/migrations/create_health_tables.php.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
use Spatie\Health\Models\HealthCheckResultHistoryItem;
use Spatie\Health\ResultStores\EloquentHealthResultStore;

return new class extends Migration
{
public function up()
{
$connection = (new HealthCheckResultHistoryItem())->getConnectionName();
$tableName = EloquentHealthResultStore::getHistoryItemInstance()->getTable();

Schema::create($tableName, function (Blueprint $table) {
Schema::connection($connection)->create($tableName, function (Blueprint $table) {
$table->id();

$table->string('check_name');
Expand All @@ -26,7 +28,7 @@ return new class extends Migration
$table->timestamps();
});

Schema::table($tableName, function(Blueprint $table) {
Schema::connection($connection)->table($tableName, function(Blueprint $table) {
$table->index('created_at');
$table->index('batch');
});
Expand Down
6 changes: 6 additions & 0 deletions src/Models/HealthCheckResultHistoryItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ class HealthCheckResultHistoryItem extends Model
'started_failing_at' => 'timestamp',
];

public function getConnectionName(): string
{
return 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;
Expand Down

0 comments on commit b313ffe

Please sign in to comment.