From 0b00f218fe3e78560fde5fe94bd50cc1ecea6fad Mon Sep 17 00:00:00 2001 From: Szabolcs Hajdu Date: Mon, 3 Mar 2025 08:15:45 +0100 Subject: [PATCH 1/2] fix: properly quote table names with schema definition (#84) --- .github/workflows/main.yml | 2 +- composer.json | 1 + docker-compose.yml | 2 +- src/Codeception/Lib/Driver/PostgreSql.php | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 72852229..4ab8485e 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -41,7 +41,7 @@ jobs: --health-timeout 5s --health-retries 5 postgres: - image: postgres + image: postgres:16.4 env: POSTGRES_PASSWORD: postgres POSTGRES_DB: codeception_test diff --git a/composer.json b/composer.json index 77eefd0e..f21a5165 100644 --- a/composer.json +++ b/composer.json @@ -22,6 +22,7 @@ "codeception/codeception": "*@dev" }, "require-dev": { + "behat/gherkin": "~4.10.0", "squizlabs/php_codesniffer": "*" }, "conflict": { diff --git a/docker-compose.yml b/docker-compose.yml index 0a234913..2de9e92c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -30,7 +30,7 @@ services: - "3306:3306" postgres: - image: postgres + image: postgres:16.4 environment: POSTGRES_PASSWORD: codeception POSTGRES_DB: codeception diff --git a/src/Codeception/Lib/Driver/PostgreSql.php b/src/Codeception/Lib/Driver/PostgreSql.php index 3d0818eb..f5a5b761 100644 --- a/src/Codeception/Lib/Driver/PostgreSql.php +++ b/src/Codeception/Lib/Driver/PostgreSql.php @@ -164,7 +164,7 @@ public function getPrimaryKey(string $tableName): array FROM pg_index i JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey) - WHERE i.indrelid = '\"{$tableName}\"'::regclass + WHERE i.indrelid = '" . $this->getQuotedName($tableName) . "'::regclass AND i.indisprimary"; $stmt = $this->executeQuery($query, []); $columns = $stmt->fetchAll(PDO::FETCH_ASSOC); From 41827036681517df56b5122a88ddbe6e6362400d Mon Sep 17 00:00:00 2001 From: Szabolcs Hajdu Date: Mon, 3 Mar 2025 09:07:57 +0100 Subject: [PATCH 2/2] fix: cover fix with test --- .../Module/Db/PostgreSqlDbTest.php | 26 ++++++++++++------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/tests/unit/Codeception/Module/Db/PostgreSqlDbTest.php b/tests/unit/Codeception/Module/Db/PostgreSqlDbTest.php index ee2f8396..d93e3294 100644 --- a/tests/unit/Codeception/Module/Db/PostgreSqlDbTest.php +++ b/tests/unit/Codeception/Module/Db/PostgreSqlDbTest.php @@ -17,20 +17,20 @@ public function getConfig(): array $this->markTestSkipped(); } - $host = getenv('PG_HOST') ?: 'localhost'; - $user = getenv('PG_USER') ?: 'postgres'; + $host = getenv('PG_HOST') ?: 'localhost'; + $user = getenv('PG_USER') ?: 'postgres'; $password = getenv('PG_PASSWORD') ?: null; $database = getenv('PG_DB') ?: 'codeception_test'; - $dsn = getenv('PG_DSN') ?: 'pgsql:host=' . $host . ';dbname=' . $database; + $dsn = getenv('PG_DSN') ?: 'pgsql:host=' . $host . ';dbname=' . $database; return [ - 'dsn' => $dsn, - 'user' => $user, - 'password' => $password, - 'dump' => 'tests/data/dumps/postgres.sql', + 'dsn' => $dsn, + 'user' => $user, + 'password' => $password, + 'dump' => 'tests/data/dumps/postgres.sql', 'reconnect' => true, - 'cleanup' => true, - 'populate' => true + 'cleanup' => true, + 'populate' => true, ]; } @@ -39,4 +39,12 @@ public function testHaveInDatabaseWithUppercaseTableName() $testData = ['Status' => 'test']; $this->module->haveInDatabase('NoPk', $testData); } + + public function testHaveInDatabaseWithAnotherSchema() + { + $userId = $this->module->haveInDatabase('anotherschema.users', ['name' => 'anotherschema', 'email' => 'anotherschema@test.com']); + $this->assertIsInt($userId); + + $this->module->seeInDatabase('anotherschema.users', ['name' => 'anotherschema', 'email' => 'anotherschema@test.com']); + } }