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

Fix setting persistent option for PDO connection #2092

Merged
5 changes: 5 additions & 0 deletions src/Phinx/Db/Adapter/MysqlAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,11 @@ public function connect()
$driverOptions[PDO::ATTR_DEFAULT_FETCH_MODE] = constant('\PDO::FETCH_' . strtoupper($options['fetch_mode']));
}

// pass \PDO::ATTR_PERSISTENT to driver options instead of useless setting it after instantiation
if (isset($options['attr_persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = $options['attr_persistent'];
}

// support arbitrary \PDO::MYSQL_ATTR_* driver options and pass them to PDO
// http://php.net/manual/en/ref.pdo-mysql.php#pdo-mysql.constants
foreach ($options as $key => $option) {
Expand Down
5 changes: 5 additions & 0 deletions src/Phinx/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ public function connect()
$driverOptions[PDO::ATTR_DEFAULT_FETCH_MODE] = constant('\PDO::FETCH_' . strtoupper($options['fetch_mode']));
}

// pass \PDO::ATTR_PERSISTENT to driver options instead of useless setting it after instantiation
if (isset($options['attr_persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = $options['attr_persistent'];
}

$db = $this->createPdoConnection($dsn, $options['user'] ?? null, $options['pass'] ?? null, $driverOptions);

try {
Expand Down
5 changes: 5 additions & 0 deletions src/Phinx/Db/Adapter/SQLiteAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public function connect()
$driverOptions[PDO::ATTR_DEFAULT_FETCH_MODE] = constant('\PDO::FETCH_' . strtoupper($options['fetch_mode']));
}

// pass \PDO::ATTR_PERSISTENT to driver options instead of useless setting it after instantiation
if (isset($options['attr_persistent'])) {
$driverOptions[PDO::ATTR_PERSISTENT] = $options['attr_persistent'];
}

$db = $this->createPdoConnection($dsn, null, null, $driverOptions);

$this->setConnection($db);
Expand Down
3 changes: 3 additions & 0 deletions src/Phinx/Db/Adapter/SqlServerAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ public function connect()
if (!empty($options['fetch_mode'])) {
$driverOptions[PDO::ATTR_DEFAULT_FETCH_MODE] = constant('\PDO::FETCH_' . strtoupper($options['fetch_mode']));
}

// Note, the PDO::ATTR_PERSISTENT attribute is not supported for sqlsrv and will throw an error when used
// See https://github.com/Microsoft/msphpsql/issues/65

// support arbitrary \PDO::SQLSRV_ATTR_* driver options and pass them to PDO
// http://php.net/manual/en/ref.pdo-sqlsrv.php#pdo-sqlsrv.constants
Expand Down
12 changes: 12 additions & 0 deletions tests/Phinx/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2214,4 +2214,16 @@ public function testGetPhinxTypeFromSQLDefinition(string $sqlDefinition, array $
$this->assertSame($expectedResponse['name'], $result['name'], "Type mismatch - got '{$result['name']}' when expecting '{$expectedResponse['name']}'");
$this->assertSame($expectedResponse['limit'], $result['limit'], "Field upper boundary mismatch - got '{$result['limit']}' when expecting '{$expectedResponse['limit']}'");
}

public function testPdoPersistentConnection()
{
$adapter = new MysqlAdapter(MYSQL_DB_CONFIG + ['attr_persistent' => true]);
$this->assertTrue($adapter->getConnection()->getAttribute(\PDO::ATTR_PERSISTENT));
}

public function testPdoNotPersistentConnection()
{
$adapter = new MysqlAdapter(MYSQL_DB_CONFIG);
$this->assertFalse($adapter->getConnection()->getAttribute(\PDO::ATTR_PERSISTENT));
}
}
12 changes: 12 additions & 0 deletions tests/Phinx/Db/Adapter/PostgresAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2310,4 +2310,16 @@ public function testInvalidPdoAttribute()
$this->expectExceptionMessage('Invalid PDO attribute: attr_invalid (\PDO::ATTR_INVALID)');
$adapter->connect();
}

public function testPdoPersistentConnection()
{
$adapter = new PostgresAdapter(PGSQL_DB_CONFIG + ['attr_persistent' => true]);
$this->assertTrue($adapter->getConnection()->getAttribute(\PDO::ATTR_PERSISTENT));
}

public function testPdoNotPersistentConnection()
{
$adapter = new PostgresAdapter(PGSQL_DB_CONFIG);
$this->assertFalse($adapter->getConnection()->getAttribute(\PDO::ATTR_PERSISTENT));
}
}
12 changes: 12 additions & 0 deletions tests/Phinx/Db/Adapter/SQLiteAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2306,4 +2306,16 @@ public function testPdoExceptionUpdateNonExistingTable()
$table = new \Phinx\Db\Table('non_existing_table', [], $this->adapter);
$table->addColumn('column', 'string')->update();
}

public function testPdoPersistentConnection()
{
$adapter = new SQLiteAdapter(SQLITE_DB_CONFIG + ['attr_persistent' => true]);
$this->assertTrue($adapter->getConnection()->getAttribute(\PDO::ATTR_PERSISTENT));
}

public function testPdoNotPersistentConnection()
{
$adapter = new SQLiteAdapter(SQLITE_DB_CONFIG);
$this->assertFalse($adapter->getConnection()->getAttribute(\PDO::ATTR_PERSISTENT));
}
}