Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
FlushDatabaseQueryLogListener added (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
tarampampam authored Oct 6, 2021
1 parent 041910f commit 397460d
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog][keepachangelog] and this project adheres to [Semantic Versioning][semver].

## v5.4.0

### Added

- Listener `FlushDatabaseQueryLogListener` for cleaning up database query log [#67]

[#67]:https://github.com/spiral/roadrunner-laravel/pull/67

## v5.3.0

### Added
Expand Down
2 changes: 1 addition & 1 deletion config/roadrunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@
|--------------------------------------------------------------------------
|
| Here you can override the worker class for processing different kinds of
| jobs, that received from the RoadRunner daemon.
| jobs, that received from the RoadRunner daemon. The key is a worker mode.
|
*/

Expand Down
1 change: 1 addition & 0 deletions src/Defaults.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ public static function afterLoopIteration(): array
Listeners\FlushLogContextListener::class,
Listeners\FlushArrayCacheListener::class,
Listeners\ResetDatabaseRecordModificationStateListener::class,
Listeners\FlushDatabaseQueryLogListener::class,
Listeners\ClearInstancesListener::class,
];
}
Expand Down
38 changes: 38 additions & 0 deletions src/Listeners/FlushDatabaseQueryLogListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunnerLaravel\Listeners;

use Illuminate\Database\Connection;
use Illuminate\Database\DatabaseManager;
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;

/**
* @link https://github.com/laravel/octane/blob/1.x/src/Listeners/FlushDatabaseQueryLog.php
*/
class FlushDatabaseQueryLogListener implements ListenerInterface
{
/**
* {@inheritdoc}
*/
public function handle($event): void
{
if ($event instanceof WithApplication) {
$app = $event->application();

if (!$app->resolved($database_abstract = 'db')) {
return;
}

/** @var DatabaseManager $database_manager */
$database_manager = $app->make($database_abstract);

foreach ($database_manager->getConnections() as $connection) {
if ($connection instanceof Connection) {
$connection->flushQueryLog();
}
}
}
}
}
58 changes: 58 additions & 0 deletions tests/Unit/Listeners/FlushDatabaseQueryLogListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners;

use Mockery as m;
use Illuminate\Support\Str;
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
use Illuminate\Contracts\Config\Repository as ConfigRepository;
use Spiral\RoadRunnerLaravel\Listeners\FlushDatabaseQueryLogListener;

/**
* @covers \Spiral\RoadRunnerLaravel\Listeners\FlushDatabaseQueryLogListener
*/
class FlushDatabaseQueryLogListenerTest extends AbstractListenerTestCase
{
/**
* {@inheritdoc}
*/
public function testHandle(): void
{
/** @var ConfigRepository $config */
$config = $this->app->make(ConfigRepository::class);

$config->set('database.connections.' . ($connection_name = 'test_' . Str::random()), [
'driver' => 'sqlite',
'database' => ':memory:',
]);

/** @var \Illuminate\Database\Connection $connection */
$connection = $this->app->make('db')->connection($connection_name);

$connection->enableQueryLog();
$connection->logQuery('select $1', ['1' => '1']);

/** @var m\MockInterface|WithApplication $event_mock */
$event_mock = m::mock(WithApplication::class)
->makePartial()
->expects('application')
->andReturn($this->app)
->getMock();

$this->assertNotEmpty($connection->getQueryLog());

$this->listenerFactory()->handle($event_mock);

$this->assertEmpty($connection->getQueryLog());
}

/**
* @return FlushDatabaseQueryLogListener
*/
protected function listenerFactory(): FlushDatabaseQueryLogListener
{
return new FlushDatabaseQueryLogListener();
}
}

0 comments on commit 397460d

Please sign in to comment.