diff --git a/src/Phinx/Db/Adapter/MysqlAdapter.php b/src/Phinx/Db/Adapter/MysqlAdapter.php index 921933a50..009994fcb 100644 --- a/src/Phinx/Db/Adapter/MysqlAdapter.php +++ b/src/Phinx/Db/Adapter/MysqlAdapter.php @@ -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) { diff --git a/src/Phinx/Db/Adapter/PostgresAdapter.php b/src/Phinx/Db/Adapter/PostgresAdapter.php index f510ba97d..7dafad7a2 100644 --- a/src/Phinx/Db/Adapter/PostgresAdapter.php +++ b/src/Phinx/Db/Adapter/PostgresAdapter.php @@ -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 { diff --git a/src/Phinx/Db/Adapter/SQLiteAdapter.php b/src/Phinx/Db/Adapter/SQLiteAdapter.php index 55d3125b4..2d1e33e4f 100644 --- a/src/Phinx/Db/Adapter/SQLiteAdapter.php +++ b/src/Phinx/Db/Adapter/SQLiteAdapter.php @@ -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); diff --git a/src/Phinx/Db/Adapter/SqlServerAdapter.php b/src/Phinx/Db/Adapter/SqlServerAdapter.php index d9751087e..62b3d1ee9 100644 --- a/src/Phinx/Db/Adapter/SqlServerAdapter.php +++ b/src/Phinx/Db/Adapter/SqlServerAdapter.php @@ -89,6 +89,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) { diff --git a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php index 26d21df9d..4842760c3 100644 --- a/tests/Phinx/Db/Adapter/MysqlAdapterTest.php +++ b/tests/Phinx/Db/Adapter/MysqlAdapterTest.php @@ -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)); + } } diff --git a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php index 47eded3cb..a8baa8c2c 100644 --- a/tests/Phinx/Db/Adapter/PostgresAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PostgresAdapterTest.php @@ -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)); + } } diff --git a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php index 08c94bee3..b92e45878 100644 --- a/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php +++ b/tests/Phinx/Db/Adapter/SQLiteAdapterTest.php @@ -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)); + } }