Skip to content

fix: allow uppercase table names using haveInDatabase with postgreSql #82

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/Codeception/Lib/Driver/PostgreSql.php
Original file line number Diff line number Diff line change
Expand Up @@ -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->postgreQuotedName($tableName) . "::regclass
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could explicitly quote every table name without the extra overhead of a regex. I believe it's fully backward-compatible. What do you think?

Suggested change
WHERE i.indrelid = " . $this->postgreQuotedName($tableName) . "::regclass
WHERE i.indrelid = '\"{$tableName}\"'::regclass

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I created the postgreQuotedName method because I was not sure it would be backwards compatible, but if it works, your suggestion it's ok for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All tests pass. Changed.

AND i.indisprimary";
$stmt = $this->executeQuery($query, []);
$columns = $stmt->fetchAll(PDO::FETCH_ASSOC);
Expand All @@ -177,4 +177,13 @@ public function getPrimaryKey(string $tableName): array

return $this->primaryKeys[$tableName];
}

private function postgreQuotedName(string $tableName): string
{
if (preg_match("/[A-Z\-]/", $tableName)) {
$tableName = sprintf('"%s"', $tableName);
}

return "'" . $tableName . "'";
}
}
4 changes: 4 additions & 0 deletions tests/data/dumps/postgres.sql
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,10 @@ CREATE TABLE "no_pk" (
"status" VARCHAR NOT NULL
);

CREATE TABLE "NoPk" (
"Status" VARCHAR NOT NULL
);

CREATE TABLE "order" (
"id" INTEGER NOT NULL PRIMARY KEY,
"name" VARCHAR NOT NULL,
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/Codeception/Module/Db/PostgreSqlDbTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,10 @@ public function getConfig(): array
'populate' => true
];
}

public function testHaveInDatabaseWithIllegalTableName()
{
$testData = ['Status' => 'test'];
$this->module->haveInDatabase('NoPk', $testData);
}
}