Skip to content

Commit 2263e50

Browse files
authored
[12.x] Run ConnectionEstablished on database reconnection (#58136)
This allows application to register custom function etc to PDO such as Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
1 parent 5e90d48 commit 2263e50

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/Illuminate/Database/DatabaseManager.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,9 @@ public function reconnect($name = null)
330330
return $this->connection($name);
331331
}
332332

333-
return $this->refreshPdoConnections($name);
333+
return tap($this->refreshPdoConnections($name), function ($connection) {
334+
$this->dispatchConnectionEstablishedEvent($connection);
335+
});
334336
}
335337

336338
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Integration\Database;
4+
5+
use Illuminate\Database\Events\ConnectionEstablished;
6+
use Illuminate\Foundation\Testing\DatabaseMigrations;
7+
use Illuminate\Support\Facades\Event;
8+
use Orchestra\Testbench\Attributes\WithMigration;
9+
use Orchestra\Testbench\TestCase;
10+
11+
use function Orchestra\Testbench\artisan;
12+
13+
class EventConnectionEstablishedTest extends TestCase
14+
{
15+
use DatabaseMigrations;
16+
17+
#[WithMigration]
18+
public function testItListenToEstablishedConnectionOnReconnect()
19+
{
20+
Event::fake([ConnectionEstablished::class]);
21+
22+
Event::assertNotDispatched(ConnectionEstablished::class);
23+
24+
artisan($this, 'migrate:fresh');
25+
26+
Event::assertDispatched(ConnectionEstablished::class);
27+
}
28+
}

0 commit comments

Comments
 (0)