From 51d3213ba6b9c0ecb166927dfc53e77ee22e1859 Mon Sep 17 00:00:00 2001 From: Denis Alustau Date: Fri, 4 Jul 2025 16:57:28 +0200 Subject: [PATCH] Set timezone on session --- src/RedshiftConnector.php | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/RedshiftConnector.php b/src/RedshiftConnector.php index b3bf078..42bc392 100644 --- a/src/RedshiftConnector.php +++ b/src/RedshiftConnector.php @@ -36,7 +36,7 @@ public function createConnection($dsn, array $config, array $options) // is useful for automation tests that bypass the connection process. if (! empty($config['password'])) { - return parent::createConnection($dsn, $config, $options); + return $this->customConnection($dsn, $config, $options); } $execute = function (int $attempt) use ($dsn, $config, $options) { @@ -52,11 +52,26 @@ public function createConnection($dsn, array $config, array $options) $config['password'] = $this->password->resolve($config['redshift']['secret'], $freshSecret); - return parent::createConnection($dsn, $config, $options); + return $this->customConnection($dsn, $config, $options); }; $condition = fn (Exception $e) => $e instanceof PDOException && str_contains($e->getMessage(), 'Access denied for user'); return retry(when: $condition, callback: $execute, times: 3); } + + private function customConnection($dsn, array $config, array $options): \PDO + { + $connection = parent::createConnection($dsn, $config, $options); + + $timezone = $config['timezone'] ?? null; + + // Redshift does not support connect on timezone using DSN, + // only through SET timezone {timezone} + if ($timezone) { + $connection->exec("SET timezone TO $timezone"); + } + + return $connection; + } }