Skip to content

Commit

Permalink
SQL server PDO driver not supports persistent connections
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Kachurka committed May 10, 2022
1 parent 7a49fa2 commit 2b86062
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 15 deletions.
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
6 changes: 0 additions & 6 deletions src/Phinx/Db/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,6 @@ protected function createPdoConnection($dsn, $username = null, $password = null,
'attr_errmode' => PDO::ERRMODE_EXCEPTION,
];

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

try {
$db = new PDO($dsn, $username, $password, $options);

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
12 changes: 3 additions & 9 deletions tests/Phinx/Db/Adapter/SqlServerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1139,16 +1139,10 @@ public function testInvalidPdoAttribute($attribute)
$adapter->connect();
}

public function testPdoPersistentConnection()
public function testPdoSqlSrvNotSupportingPersistentConnections()
{
$adapter = new SqlServerAdapter(SQLSRV_DB_CONFIG + ['attr_persistent' => true]);
$this->assertEquals(true, $adapter->getConnection()->getAttribute(\PDO::ATTR_PERSISTENT));
}

public function testPdoNotPersistentConnection()
{
$adapter = new SqlServerAdapter(SQLSRV_DB_CONFIG);
$this->assertEquals(false, $adapter->getConnection()->getAttribute(\PDO::ATTR_PERSISTENT));
$this->expectException(\InvalidArgumentException::class);
$adapter->connect();
}

}

0 comments on commit 2b86062

Please sign in to comment.