Skip to content

Commit 974a5ab

Browse files
authored
Merge pull request #5800 from allenisalai/psql-schema-manager-error
Ensure the pg_depend relation is for a table object
2 parents e7331bc + b35b52c commit 974a5ab

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

src/Schema/PostgreSQLSchemaManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =
651651
LEFT JOIN pg_depend d
652652
ON d.objid = c.oid
653653
AND d.deptype = 'e'
654+
AND d.classid = (SELECT oid FROM pg_class WHERE relname = 'pg_class')
654655
SQL;
655656

656657
$conditions = array_merge([

tests/Functional/Schema/PostgreSQLSchemaManagerTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Doctrine\DBAL\Tests\Functional\Schema;
44

5+
use Doctrine\DBAL\Driver\ServerInfoAwareConnection;
56
use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException;
67
use Doctrine\DBAL\Platforms\AbstractPlatform;
78
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
@@ -27,7 +28,9 @@
2728
use function array_unshift;
2829
use function assert;
2930
use function count;
31+
use function sprintf;
3032
use function strtolower;
33+
use function version_compare;
3134

3235
class PostgreSQLSchemaManagerTest extends SchemaManagerFunctionalTestCase
3336
{
@@ -561,6 +564,69 @@ public function testAlterTableAutoIncrementIntToBigInt(
561564
self::assertTrue($tableFinal->getColumn('id')->getAutoincrement());
562565
}
563566

567+
public function testListTableColumnsOidConflictWithNonTableObject(): void
568+
{
569+
$wrappedConnection = $this->connection->getWrappedConnection();
570+
assert($wrappedConnection instanceof ServerInfoAwareConnection);
571+
if (version_compare($wrappedConnection->getServerVersion(), '10.0', '<')) {
572+
self::markTestSkipped('Manually setting the Oid is not supported in 9.4');
573+
}
574+
575+
$table = 'test_list_table_columns_oid_conflicts';
576+
$this->connection->executeStatement(sprintf('CREATE TABLE IF NOT EXISTS %s(id INT NOT NULL)', $table));
577+
$beforeColumns = $this->schemaManager->listTableColumns($table);
578+
$this->assertArrayHasKey('id', $beforeColumns);
579+
580+
$this->connection->executeStatement('CREATE EXTENSION IF NOT EXISTS pg_prewarm');
581+
$originalTableOid = $this->connection->fetchOne(
582+
'SELECT oid FROM pg_class WHERE pg_class.relname = ?',
583+
[$table],
584+
);
585+
586+
$getConflictingOidSql = <<<'SQL'
587+
SELECT objid
588+
FROM pg_depend
589+
JOIN pg_extension as ex on ex.oid = pg_depend.refobjid
590+
WHERE ex.extname = 'pg_prewarm'
591+
ORDER BY objid
592+
LIMIT 1
593+
SQL;
594+
$conflictingOid = $this->connection->fetchOne($getConflictingOidSql);
595+
596+
$this->connection->executeStatement(
597+
'UPDATE pg_attribute SET attrelid = ? WHERE attrelid = ?',
598+
[$conflictingOid, $originalTableOid],
599+
);
600+
$this->connection->executeStatement(
601+
'UPDATE pg_description SET objoid = ? WHERE objoid = ?',
602+
[$conflictingOid, $originalTableOid],
603+
);
604+
$this->connection->executeStatement(
605+
'UPDATE pg_class SET oid = ? WHERE oid = ?',
606+
[$conflictingOid, $originalTableOid],
607+
);
608+
609+
$afterColumns = $this->schemaManager->listTableColumns($table);
610+
611+
// revert to the database to original state prior to asserting result
612+
$this->connection->executeStatement(
613+
'UPDATE pg_attribute SET attrelid = ? WHERE attrelid = ?',
614+
[$originalTableOid, $conflictingOid],
615+
);
616+
$this->connection->executeStatement(
617+
'UPDATE pg_description SET objoid = ? WHERE objoid = ?',
618+
[$originalTableOid, $conflictingOid],
619+
);
620+
$this->connection->executeStatement(
621+
'UPDATE pg_class SET oid = ? WHERE oid = ?',
622+
[$originalTableOid, $conflictingOid],
623+
);
624+
$this->connection->executeStatement(sprintf('DROP TABLE IF EXISTS %s', $table));
625+
$this->connection->executeStatement('DROP EXTENSION IF EXISTS pg_prewarm');
626+
627+
$this->assertArrayHasKey('id', $afterColumns);
628+
}
629+
564630
/** @return iterable<mixed[]> */
565631
public static function autoIncrementTypeMigrations(): iterable
566632
{

0 commit comments

Comments
 (0)