Skip to content

Commit

Permalink
support PDO persistent connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Pavel Kachurka committed May 10, 2022
1 parent 48d8e1f commit 7a49fa2
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/Phinx/Db/Adapter/PdoAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ 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
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->assertEquals(true, $adapter->getConnection()->getAttribute(\PDO::ATTR_PERSISTENT));
}

public function testPdoNotPersistentConnection()
{
$adapter = new MysqlAdapter(MYSQL_DB_CONFIG);
$this->assertEquals(false, $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->assertEquals(true, $adapter->getConnection()->getAttribute(\PDO::ATTR_PERSISTENT));
}

public function testPdoNotPersistentConnection()
{
$adapter = new PostgresAdapter(PGSQL_DB_CONFIG);
$this->assertEquals(false, $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->assertEquals(true, $adapter->getConnection()->getAttribute(\PDO::ATTR_PERSISTENT));
}

public function testPdoNotPersistentConnection()
{
$adapter = new SQLiteAdapter(SQLITE_DB_CONFIG);
$this->assertEquals(false, $adapter->getConnection()->getAttribute(\PDO::ATTR_PERSISTENT));
}
}
13 changes: 13 additions & 0 deletions tests/Phinx/Db/Adapter/SqlServerAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1138,4 +1138,17 @@ public function testInvalidPdoAttribute($attribute)
$this->expectExceptionMessage('Invalid PDO attribute: ' . $attribute . ' (\PDO::' . strtoupper($attribute) . ')');
$adapter->connect();
}

public function testPdoPersistentConnection()
{
$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));
}

}

0 comments on commit 7a49fa2

Please sign in to comment.