Skip to content

Commit 0ec1739

Browse files
committed
set application_name for a connection
User can set the application_name for a postgres connection
1 parent f55ed55 commit 0ec1739

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/Illuminate/Database/Connectors/PostgresConnector.php

+8
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,14 @@ public function connect(array $config)
5757
$connection->prepare("set search_path to {$schema}")->execute();
5858
}
5959

60+
// In Postgres the name of the application can be set by the user.
61+
// This is useful for monitoring.
62+
if (isset($config['application_name'])) {
63+
$applicationName = $config['application_name'];
64+
65+
$connection->prepare("set application_name to '$applicationName'")->execute();
66+
}
67+
6068
return $connection;
6169
}
6270

tests/Database/DatabaseConnectorTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,22 @@ public function testPostgresSearchPathArraySupported()
8989
$this->assertSame($result, $connection);
9090
}
9191

92+
public function testPostgresApplicationNameIsSet()
93+
{
94+
$dsn = 'pgsql:host=foo;dbname=bar';
95+
$config = ['host' => 'foo', 'database' => 'bar', 'charset' => 'utf8', 'application_name' => 'Larvel App'];
96+
$connector = $this->getMock('Illuminate\Database\Connectors\PostgresConnector', ['createConnection', 'getOptions']);
97+
$connection = m::mock('stdClass');
98+
$connector->expects($this->once())->method('getOptions')->with($this->equalTo($config))->will($this->returnValue(['options']));
99+
$connector->expects($this->once())->method('createConnection')->with($this->equalTo($dsn), $this->equalTo($config), $this->equalTo(['options']))->will($this->returnValue($connection));
100+
$connection->shouldReceive('prepare')->once()->with('set names \'utf8\'')->andReturn($connection);
101+
$connection->shouldReceive('prepare')->once()->with('set application_name to \'Larvel App\'')->andReturn($connection);
102+
$connection->shouldReceive('execute')->twice();
103+
$result = $connector->connect($config);
104+
105+
$this->assertSame($result, $connection);
106+
}
107+
92108
public function testSQLiteMemoryDatabasesMayBeConnectedTo()
93109
{
94110
$dsn = 'sqlite::memory:';

0 commit comments

Comments
 (0)