Commit 41e718ea authored by Steve Müller's avatar Steve Müller

Merge pull request #844 from kiwidg/pgsql

template1 as default database for PostgreSQL
parents 18507520 f229b8a7
...@@ -88,6 +88,11 @@ class Driver extends AbstractPostgreSQLDriver ...@@ -88,6 +88,11 @@ class Driver extends AbstractPostgreSQLDriver
if (isset($params['dbname'])) { if (isset($params['dbname'])) {
$dsn .= 'dbname=' . $params['dbname'] . ' '; $dsn .= 'dbname=' . $params['dbname'] . ' ';
} else {
// Used for temporary connections to allow operations like dropping the database currently connected to.
// Connecting without an explicit database does not work, therefore "template1" database is used
// as it is certainly present in every server setup.
$dsn .= 'dbname=template1' . ' ';
} }
if (isset($params['sslmode'])) { if (isset($params['sslmode'])) {
......
<?php
namespace Doctrine\Tests\DBAL\Functional\Driver;
use Doctrine\DBAL\Connection;
use Doctrine\Tests\DbalFunctionalTestCase;
abstract class AbstractDriverTest extends DbalFunctionalTestCase
{
/**
* The driver instance under test.
*
* @var \Doctrine\DBAL\Driver
*/
private $driver;
protected function setUp()
{
parent::setUp();
$this->driver = $this->createDriver();
}
/**
* @group DBAL-1215
*/
public function testConnectsWithoutDatabaseNameParameter()
{
$params = $this->_conn->getParams();
unset($params['dbname']);
$user = isset($params['user']) ? $params['user'] : null;
$password = isset($params['password']) ? $params['password'] : null;
$connection = $this->driver->connect($params, $user, $password);
$this->assertInstanceOf('Doctrine\DBAL\Driver\Connection', $connection);
}
/**
* @group DBAL-1215
*/
public function testReturnsDatabaseNameWithoutDatabaseNameParameter()
{
$params = $this->_conn->getParams();
unset($params['dbname']);
$connection = new Connection(
$params,
$this->_conn->getDriver(),
$this->_conn->getConfiguration(),
$this->_conn->getEventManager()
);
$this->assertSame(
$this->getDatabaseNameForConnectionWithoutDatabaseNameParameter(),
$this->driver->getDatabase($connection)
);
}
/**
* @return \Doctrine\DBAL\Driver
*/
abstract protected function createDriver();
/**
* @return string|null
*/
protected function getDatabaseNameForConnectionWithoutDatabaseNameParameter()
{
return null;
}
}
<?php
namespace Doctrine\Tests\DBAL\Functional\Driver\PDOPgSql;
use Doctrine\DBAL\Driver\PDOPgSql\Driver;
use Doctrine\Tests\DBAL\Functional\Driver\AbstractDriverTest;
class DriverTest extends AbstractDriverTest
{
protected function setUp()
{
if (! extension_loaded('pdo_pgsql')) {
$this->markTestSkipped('pdo_pgsql is not installed.');
}
parent::setUp();
if (! $this->_conn->getDriver() instanceof Driver) {
$this->markTestSkipped('pdo_pgsql only test.');
}
}
/**
* {@inheritdoc}
*/
protected function createDriver()
{
return new Driver();
}
/**
* {@inheritdoc}
*/
protected function getDatabaseNameForConnectionWithoutDatabaseNameParameter()
{
return 'template1';
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment