Skip to content

Commit 397460d

Browse files
authored
FlushDatabaseQueryLogListener added (#67)
1 parent 041910f commit 397460d

File tree

5 files changed

+106
-1
lines changed

5 files changed

+106
-1
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ All notable changes to this project will be documented in this file.
44

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

7+
## v5.4.0
8+
9+
### Added
10+
11+
- Listener `FlushDatabaseQueryLogListener` for cleaning up database query log [#67]
12+
13+
[#67]:https://github.com/spiral/roadrunner-laravel/pull/67
14+
715
## v5.3.0
816

917
### Added

config/roadrunner.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
|--------------------------------------------------------------------------
110110
|
111111
| Here you can override the worker class for processing different kinds of
112-
| jobs, that received from the RoadRunner daemon.
112+
| jobs, that received from the RoadRunner daemon. The key is a worker mode.
113113
|
114114
*/
115115

src/Defaults.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public static function afterLoopIteration(): array
9595
Listeners\FlushLogContextListener::class,
9696
Listeners\FlushArrayCacheListener::class,
9797
Listeners\ResetDatabaseRecordModificationStateListener::class,
98+
Listeners\FlushDatabaseQueryLogListener::class,
9899
Listeners\ClearInstancesListener::class,
99100
];
100101
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Listeners;
6+
7+
use Illuminate\Database\Connection;
8+
use Illuminate\Database\DatabaseManager;
9+
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
10+
11+
/**
12+
* @link https://github.com/laravel/octane/blob/1.x/src/Listeners/FlushDatabaseQueryLog.php
13+
*/
14+
class FlushDatabaseQueryLogListener implements ListenerInterface
15+
{
16+
/**
17+
* {@inheritdoc}
18+
*/
19+
public function handle($event): void
20+
{
21+
if ($event instanceof WithApplication) {
22+
$app = $event->application();
23+
24+
if (!$app->resolved($database_abstract = 'db')) {
25+
return;
26+
}
27+
28+
/** @var DatabaseManager $database_manager */
29+
$database_manager = $app->make($database_abstract);
30+
31+
foreach ($database_manager->getConnections() as $connection) {
32+
if ($connection instanceof Connection) {
33+
$connection->flushQueryLog();
34+
}
35+
}
36+
}
37+
}
38+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Spiral\RoadRunnerLaravel\Tests\Unit\Listeners;
6+
7+
use Mockery as m;
8+
use Illuminate\Support\Str;
9+
use Spiral\RoadRunnerLaravel\Events\Contracts\WithApplication;
10+
use Illuminate\Contracts\Config\Repository as ConfigRepository;
11+
use Spiral\RoadRunnerLaravel\Listeners\FlushDatabaseQueryLogListener;
12+
13+
/**
14+
* @covers \Spiral\RoadRunnerLaravel\Listeners\FlushDatabaseQueryLogListener
15+
*/
16+
class FlushDatabaseQueryLogListenerTest extends AbstractListenerTestCase
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
public function testHandle(): void
22+
{
23+
/** @var ConfigRepository $config */
24+
$config = $this->app->make(ConfigRepository::class);
25+
26+
$config->set('database.connections.' . ($connection_name = 'test_' . Str::random()), [
27+
'driver' => 'sqlite',
28+
'database' => ':memory:',
29+
]);
30+
31+
/** @var \Illuminate\Database\Connection $connection */
32+
$connection = $this->app->make('db')->connection($connection_name);
33+
34+
$connection->enableQueryLog();
35+
$connection->logQuery('select $1', ['1' => '1']);
36+
37+
/** @var m\MockInterface|WithApplication $event_mock */
38+
$event_mock = m::mock(WithApplication::class)
39+
->makePartial()
40+
->expects('application')
41+
->andReturn($this->app)
42+
->getMock();
43+
44+
$this->assertNotEmpty($connection->getQueryLog());
45+
46+
$this->listenerFactory()->handle($event_mock);
47+
48+
$this->assertEmpty($connection->getQueryLog());
49+
}
50+
51+
/**
52+
* @return FlushDatabaseQueryLogListener
53+
*/
54+
protected function listenerFactory(): FlushDatabaseQueryLogListener
55+
{
56+
return new FlushDatabaseQueryLogListener();
57+
}
58+
}

0 commit comments

Comments
 (0)