Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added parameter default_dbname to pdo_pgsql driver, used to override the default database #2284

Merged
merged 5 commits into from Jan 15, 2016

Conversation

kimhemsoe
Copy link
Member

No description provided.

// as it is certainly present in every server setup.
$dsn .= 'dbname=postgres' . ' ';
} elseif (isset($params['default_dbname'])) {
$dsn .= 'dbname=' . $params['default_dbname'] . ' ';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should still connect to postgres if the parameter is not set as we will have errors again otherwise.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, postgres is the default if dont provide one. Ive tested it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kimhemsoe that is not true. It defaults to a database that is named equally to the connection user. This is what originally caused trouble. See PHP issue and doctrine/DoctrineBundle#402

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@deeky666 @kimhemsoe Ah yes, I was wrong - you can verify this using:

PDO

php -r "$pdo = new PDO('pgsql:host=localhost port=5432', 'your_username', 'your_password');"

If no db matching your username exists, you get:

PDOException: SQLSTATE[08006] [7] FATAL:  database "your_username" does not exist in Command line code on line 1

Call Stack:
    0.0000     125096   1. {main}() Command line code:0
    0.0000     125520   2. PDO->__construct() Command line code:1

pgsql

php -r "$conn = pg_connect('host=localhost port=5432 user=your_username password=your_password');"

If no db matching your username exists, you get:

Warning: pg_connect(): Unable to connect to PostgreSQL server: FATAL:  database "your_username" does not exist in Command line code on line 1

Call Stack:
    0.0000     124792   1. {main}() Command line code:0
    0.0000     124928   2. pg_connect() Command line code:1

@deeky666 deeky666 self-assigned this Jan 8, 2016
@@ -20,6 +21,26 @@ protected function setUp()
}
}

public function testDefaultDatabaseOption()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please adjust this test case to the following to cover all variants?

/**
 * @dataProvider getDatabaseParameter
 */
public function testDatabaseParameter($databaseName, $defaultDatabaseName, $expectedDatabaseName)
{
    $params = $this->_conn->getParams();
    $params['dbname'] = $databaseName;
    $params['default_dbname'] = $defaultDatabaseName;

    $connection = new Connection(
        $params,
        $this->_conn->getDriver(),
        $this->_conn->getConfiguration(),
        $this->_conn->getEventManager()
    );

    $this->assertSame(
        $expectedDatabaseName,
        $this->driver->getDatabase($connection)
    );
}

public function getDatabaseParameter()
{
    $params = TestUtil::getConnection()->getParams();
    $realDatabaseName = $params['dbname'];
    $dummyDatabaseName = $realDatabaseName . 'a';

    return array(
        // dbname, default_dbname, expected
        array($realDatabaseName, null, $realDatabaseName),
        array($realDatabaseName, $dummyDatabaseName, $realDatabaseName),
        array(null, $realDatabaseName, $realDatabaseName),
        array(null, null, $this->getDatabaseNameForConnectionWithoutDatabaseNameParameter()),
    );
}

@@ -21,6 +23,43 @@ protected function setUp()
}

/**
@dataProvider getDatabaseParameter
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kimhemsoe this looks like borken indentation. Can you fix this? After that I'll merge.

@kimhemsoe
Copy link
Member Author

@deeky666 ping

@deeky666 deeky666 added this to the 2.6 milestone Jan 15, 2016
deeky666 added a commit that referenced this pull request Jan 15, 2016
Added parameter "default_dbname" to pdo_pgsql driver which can be use…
@deeky666 deeky666 merged commit 02e5b61 into doctrine:master Jan 15, 2016
@deeky666
Copy link
Member

@kimhemsoe merged, thanks.

@deeky666 deeky666 changed the title Added parameter "default_dbname" to pdo_pgsql driver which can be use… Added parameter "default_dbname" to pdo_pgsql driver which can be used to override the default database Jan 15, 2016
@kimhemsoe
Copy link
Member Author

@deeky666 Did you merge into 2.5 too?

@deeky666
Copy link
Member

@kimhemsoe no. It is actually an improvement, no? 2.5 only receives bug fixes

@Ocramius Ocramius changed the title Added parameter "default_dbname" to pdo_pgsql driver which can be used to override the default database Added parameter default_dbname to pdo_pgsql driver, used to override the default database Jul 22, 2017
@kimhemsoe kimhemsoe deleted the pg_default_database_option branch July 13, 2018 10:48
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants