Skip to content
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

🐛 Oracle: fix comment retrieval on table with reserved keyword name #6765

Open
wants to merge 1 commit into
base: 3.9.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion docs/en/reference/testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ then run the following command:

.. code-block:: console

$ phpunit -c ci/github/pdo_mysql.xml
$ phpunit -c ci/github/phpunit/pdo_mysql.xml

We do not currently have specific instructions on how to run a Database
server, but we do recommend Docker as a convenient way to do so.
Expand Down
9 changes: 5 additions & 4 deletions src/Schema/AbstractSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,12 @@ protected function doListTableColumns($table, $database = null): array
);
}

$normalizedTable = $this->normalizeName($table);

return $this->_getPortableTableColumnList(
$table,
$normalizedTable,
$database,
$this->selectTableColumns($database, $this->normalizeName($table))
->fetchAllAssociative(),
$this->selectTableColumns($database, $normalizedTable)->fetchAllAssociative(),
);
}

Expand Down Expand Up @@ -486,7 +487,7 @@ protected function doListTableDetails($name): Table
$this->listTableIndexes($name),
[],
$foreignKeys,
$tableOptionsByTable[$normalizedName] ?? [],
$tableOptionsByTable[$name] ?? [],
Copy link
Author

Choose a reason for hiding this comment

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

@morozov This change works for oracle but breaks for SQLite... I'm not sure of what approach should I take, but it looks like the mix between normalized / quoted names is the main issue here (and the various databases may not handle them the same).

Copy link
Member

@morozov morozov Feb 6, 2025

Choose a reason for hiding this comment

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

If you want, you can scope your pull request only to fixing the table comment, which is necessary only for Oracle (in my understanding).

If you want to fix it for SQLite / column comments as well, you can try referring to the 4.3.x branch. This code has been slightly cleaned up there, and I believe the normalized/quoted names are no longer mixed there. If that's the case, you can try to reconstruct the logic from 4.3.x here.

Copy link
Member

@morozov morozov Feb 6, 2025

Choose a reason for hiding this comment

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

This change works for oracle

I think options (and all other table objects) should be indexed by the normalized name. The quoted names are just form or user input, while the normalized ones are the actual values. E.g. on Oracle, the inputs 'users' and '"USERS"' will both be normalized to 'USERS'.

Copy link
Author

Choose a reason for hiding this comment

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

I believe the normalized/quoted names are no longer mixed there.

I did not check whether there were mixed or no, but I confirm it works for both Oracle and SQLite there.

);
}

Expand Down
10 changes: 5 additions & 5 deletions src/Schema/OracleSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,16 +512,16 @@

$sql .= ' FROM ALL_TAB_COMMENTS WHERE ' . implode(' AND ', $conditions);

/** @var array<string,array<string,mixed>> $metadata */
$metadata = $this->_conn->executeQuery($sql, $params)
->fetchAllAssociativeIndexed();
/** @var array<array{TABLE_NAME: string, COMMENTS: string|null}> $metadata */
$metadata = $this->_conn->executeQuery($sql, $params)->fetchAllAssociative();

$tableOptions = [];
foreach ($metadata as $table => $data) {
$data = array_change_key_case($data, CASE_LOWER);
foreach ($metadata as $data) {
$data = array_change_key_case($data, CASE_LOWER);
$table = $this->_getPortableTableDefinition($data);

$tableOptions[$table] = [
'comment' => $data['comments'],

Check failure on line 524 in src/Schema/OracleSchemaManager.php

View workflow job for this annotation

GitHub Actions / Static Analysis / PHPStan (PHP: 8.4)

Offset 'comments' does not exist on array<'COMMENTS'|'TABLE_NAME', string|null>.
];
}

Expand Down
28 changes: 17 additions & 11 deletions tests/Functional/Schema/SchemaManagerFunctionalTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1676,10 +1676,12 @@ public function testIntrospectReservedKeywordTableViaListTableDetails(): void
{
$this->createReservedKeywordTables();

$user = $this->schemaManager->introspectTable('"user"');
self::assertCount(2, $user->getColumns());
self::assertCount(2, $user->getIndexes());
self::assertCount(1, $user->getForeignKeys());
$select = $this->schemaManager->introspectTable('"select"');
self::assertCount(3, $select->getColumns());
self::assertCount(2, $select->getIndexes());
self::assertCount(1, $select->getForeignKeys());
self::assertSame('table comment', $select->getComment());
self::assertSame('column comment', $select->getColumn('select')->getComment());
}

public function testIntrospectReservedKeywordTableViaListTables(): void
Expand All @@ -1688,24 +1690,28 @@ public function testIntrospectReservedKeywordTableViaListTables(): void

$tables = $this->schemaManager->listTables();

$user = $this->findTableByName($tables, 'user');
self::assertNotNull($user);
self::assertCount(2, $user->getColumns());
self::assertCount(2, $user->getIndexes());
self::assertCount(1, $user->getForeignKeys());
$select = $this->findTableByName($tables, 'select');
self::assertNotNull($select);
self::assertCount(3, $select->getColumns());
self::assertCount(2, $select->getIndexes());
self::assertCount(1, $select->getForeignKeys());
self::assertSame('table comment', $select->getComment());
self::assertSame('column comment', $select->getColumn('select')->getComment());
}

private function createReservedKeywordTables(): void
{
$platform = $this->connection->getDatabasePlatform();

$this->dropTableIfExists($platform->quoteIdentifier('user'));
$this->dropTableIfExists($platform->quoteIdentifier('select'));
$this->dropTableIfExists($platform->quoteIdentifier('group'));

$schema = new Schema();

$user = $schema->createTable('user');
$user = $schema->createTable('select');
$user->setComment('table comment');
$user->addColumn('id', Types::INTEGER);
$user->addColumn('select', Types::INTEGER)->setComment('column comment');
$user->addColumn('group_id', Types::INTEGER);
$user->setPrimaryKey(['id']);
$user->addForeignKeyConstraint('group', ['group_id'], ['id']);
Expand Down
Loading