|
2 | 2 |
|
3 | 3 | namespace Doctrine\DBAL\Tests\Functional\Schema; |
4 | 4 |
|
| 5 | +use Doctrine\DBAL\Driver\ServerInfoAwareConnection; |
5 | 6 | use Doctrine\DBAL\Exception\DatabaseObjectNotFoundException; |
6 | 7 | use Doctrine\DBAL\Platforms\AbstractPlatform; |
7 | 8 | use Doctrine\DBAL\Platforms\PostgreSQLPlatform; |
|
27 | 28 | use function array_unshift; |
28 | 29 | use function assert; |
29 | 30 | use function count; |
| 31 | +use function sprintf; |
30 | 32 | use function strtolower; |
| 33 | +use function version_compare; |
31 | 34 |
|
32 | 35 | class PostgreSQLSchemaManagerTest extends SchemaManagerFunctionalTestCase |
33 | 36 | { |
@@ -561,6 +564,69 @@ public function testAlterTableAutoIncrementIntToBigInt( |
561 | 564 | self::assertTrue($tableFinal->getColumn('id')->getAutoincrement()); |
562 | 565 | } |
563 | 566 |
|
| 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 | + |
564 | 630 | /** @return iterable<mixed[]> */ |
565 | 631 | public static function autoIncrementTypeMigrations(): iterable |
566 | 632 | { |
|
0 commit comments