Skip to content

Commit

Permalink
Fix Postgres schema restoring after reconnect (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
msmakouz authored Jul 3, 2023
1 parent d5b1be2 commit 993fcf0
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 26 deletions.
9 changes: 2 additions & 7 deletions src/Driver/Postgres/PostgresDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,8 @@ protected function createPDO(): \PDO|PDOInterface
// TODO Should be moved into driver settings.
$pdo->exec("SET NAMES 'UTF-8'");

// TODO May be redundant?
// Search schemas list can not be empty.
if ($this->searchPath !== []) {
$schema = '"' . implode('", "', $this->searchPath) . '"';
$pdo->exec("SET search_path TO {$schema}");
$this->searchPath = [];
}
$schema = '"' . implode('", "', $this->searchPath) . '"';
$pdo->exec("SET search_path TO {$schema}");

return $pdo;
}
Expand Down
15 changes: 5 additions & 10 deletions tests/Database/Functional/Driver/Common/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ public function tearDown(): void
}

/**
* @param array{readonly: bool} $driverConfig
*
* @return DriverInterface
*/
private function getDriver(array $driverConfig = [], array $connectionConfig = []): DriverInterface
Expand Down Expand Up @@ -84,18 +82,15 @@ private function applyConnectionOptions(ConnectionConfig $config, array $options

private function applyDriverOptions(DriverConfig $config, array $options): void
{
// Add readonly options support
if (isset($options['readonly']) && $options['readonly'] === true) {
$config->readonly = true;
}

if (isset($options['withDatetimeMicroseconds']) && $options['withDatetimeMicroseconds'] === true) {
$config->options['withDatetimeMicroseconds'] = true;
foreach ($options as $key => $value) {
if ($key === 'options') {
$value += $config->options;
}
$config->$key = $value;
}
}

/**
* @param array{readonly: bool} $driverConfig
* @param array $connectionConfig
*
* @return Database
Expand Down
13 changes: 10 additions & 3 deletions tests/Database/Functional/Driver/MySQL/Query/InsertQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,25 @@ public function testSimpleInsertEmptyDataset(): void

public function testInsertMicroseconds(): void
{
$schema = $this->schema(table: 'with_microseconds', driverConfig: ['withDatetimeMicroseconds' => true]);
$schema = $this->schema(
table: 'with_microseconds',
driverConfig: ['options' => ['withDatetimeMicroseconds' => true]]
);
$schema->primary('id');
$schema->datetime('datetime', 6);
$schema->save();

$expected = new \DateTimeImmutable();

$id = $this->db(driverConfig: ['withDatetimeMicroseconds' => true])->insert('with_microseconds')->values([
$id = $this->db(
driverConfig: ['options' => ['withDatetimeMicroseconds' => true]]
)->insert('with_microseconds')->values([
'datetime' => $expected,
])->run();

$result = $this->db(driverConfig: ['withDatetimeMicroseconds' => true])->select('datetime')
$result = $this->db(
driverConfig: ['options' => ['withDatetimeMicroseconds' => true]]
)->select('datetime')
->from('with_microseconds')
->where('id', $id)
->run()
Expand Down
15 changes: 15 additions & 0 deletions tests/Database/Functional/Driver/Postgres/Driver/DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,19 @@
class DriverTest extends CommonClass
{
public const DRIVER = 'postgres';

public function testSchemaPathAfterReconnect(): void
{
$db = $this->db(driverConfig: ['schema' => ['custom']]);

$ref = new \ReflectionProperty($db->getDriver(), 'searchPath');
$ref->setAccessible(true);

$this->assertSame(['custom'], $ref->getValue($db->getDriver()));

$db->getDriver()->disconnect();
$db->getDriver()->connect();

$this->assertSame(['custom'], $ref->getValue($db->getDriver()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,25 @@ public function testCustomReturningSupportsOnlySingleColumn(): void

public function testInsertMicroseconds(): void
{
$schema = $this->schema(table: 'with_microseconds', driverConfig: ['withDatetimeMicroseconds' => true]);
$schema = $this->schema(
table: 'with_microseconds',
driverConfig: ['options' => ['withDatetimeMicroseconds' => true]]
);
$schema->primary('id');
$schema->datetime('datetime', 6);
$schema->save();

$expected = new \DateTimeImmutable();

$id = $this->db(driverConfig: ['withDatetimeMicroseconds' => true])->insert('with_microseconds')->values([
$id = $this->db(
driverConfig: ['options' => ['withDatetimeMicroseconds' => true]]
)->insert('with_microseconds')->values([
'datetime' => $expected,
])->run();

$result = $this->db(driverConfig: ['withDatetimeMicroseconds' => true])->select('datetime')
$result = $this->db(
driverConfig: ['options' => ['withDatetimeMicroseconds' => true]]
)->select('datetime')
->from('with_microseconds')
->where('id', $id)
->run()
Expand Down
13 changes: 10 additions & 3 deletions tests/Database/Functional/Driver/SQLite/Query/InsertQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,18 +61,25 @@ public function testSimpleInsertMultipleRows2(): void

public function testInsertMicroseconds(): void
{
$schema = $this->schema(table: 'with_microseconds', driverConfig: ['withDatetimeMicroseconds' => true]);
$schema = $this->schema(
table: 'with_microseconds',
driverConfig: ['options' => ['withDatetimeMicroseconds' => true]]
);
$schema->primary('id');
$schema->datetime('datetime', 6);
$schema->save();

$expected = new \DateTimeImmutable();

$id = $this->db(driverConfig: ['withDatetimeMicroseconds' => true])->insert('with_microseconds')->values([
$id = $this->db(
driverConfig: ['options' => ['withDatetimeMicroseconds' => true]]
)->insert('with_microseconds')->values([
'datetime' => $expected,
])->run();

$result = $this->db(driverConfig: ['withDatetimeMicroseconds' => true])->select('datetime')
$result = $this->db(
driverConfig: ['options' => ['withDatetimeMicroseconds' => true]]
)->select('datetime')
->from('with_microseconds')
->where('id', $id)
->run()
Expand Down

0 comments on commit 993fcf0

Please sign in to comment.