Skip to content

Commit

Permalink
Fix setting persistent option for PDO connection (#2092)
Browse files Browse the repository at this point in the history
  • Loading branch information
paulermo authored Jul 4, 2022
1 parent 2046c44 commit 11ec6ee
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 0 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
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 @@ -97,6 +97,9 @@ public function connect()
$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
foreach ($options as $key => $option) {
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));
}
}

0 comments on commit 11ec6ee

Please sign in to comment.