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

fix(PostgreSQL omit dropped columns in getListTableColumnsSQL (pg_att… #6249

Open
wants to merge 4 commits 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
1 change: 1 addition & 0 deletions src/Platforms/PostgreSQLPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,7 @@ public function getListTableColumnsSQL($table, $database = null)
AND a.attrelid = c.oid
AND a.atttypid = t.oid
AND n.oid = c.relnamespace
AND a.attisdropped = false
ORDER BY a.attnum';
}

Expand Down
1 change: 1 addition & 0 deletions src/Schema/PostgreSQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,7 @@
'a.attnum > 0',
"c.relkind = 'r'",
'd.refobjid IS NULL',
'a.attisdropped = false',

Check warning on line 660 in src/Schema/PostgreSQLSchemaManager.php

View check run for this annotation

Codecov / codecov/patch

src/Schema/PostgreSQLSchemaManager.php#L660

Added line #L660 was not covered by tests
], $this->buildQueryConditions($tableName));

$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY a.attnum';
Expand Down
56 changes: 56 additions & 0 deletions tests/Functional/TableDropColumnTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Doctrine\DBAL\Tests\Functional;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Types;
use Throwable;

use function count;

class TableDropColumnTest extends FunctionalTestCase
{
protected function setUp(): void
{
$table = new Table('write_table');
$table->addColumn('id', Types::INTEGER, ['autoincrement' => true]);
$table->addColumn('test_column1', Types::STRING);
$table->addColumn('test_column2', Types::INTEGER);
$table->setPrimaryKey(['id']);

$platform = $this->connection->getDatabasePlatform();

// some db engines dont allow drop column which belongs to index but on pgsql it leave pg_attribute with attisdropped=true so we can test it

Check warning on line 25 in tests/Functional/TableDropColumnTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Line exceeds 120 characters; contains 148 characters
if ($platform instanceof PostgreSQLPlatform) {
$table->addIndex(['test_column1', 'test_column2'], 'test');
}

$this->dropAndCreateTable($table);

$this->connection->executeStatement('ALTER TABLE write_table DROP COLUMN test_column1');
}

public function testPgSqlPgAttributeTable(): void
{
$platform = $this->connection->getDatabasePlatform();

if (!$platform instanceof PostgreSQLPlatform) {

Check failure on line 39 in tests/Functional/TableDropColumnTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Expected 1 space after NOT operator; 0 found
self::markTestSkipped('Test does work on PostgreSQL only.');
}

try {
$this->connection->executeQuery('Select attisdropped from pg_attribute Limit 1')->fetchOne();
} catch (Throwable $e) {
self::fail("Column attisdropped not exists in pg_attribute table");

Check failure on line 46 in tests/Functional/TableDropColumnTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

String "Column attisdropped not exists in pg_attribute table" does not require double quotes; use single quotes instead
}
}
Comment on lines +35 to +48
Copy link
Member

Choose a reason for hiding this comment

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

What is the purpose of this test?


public function testColumnNumber(): void
{
$columns = $this->connection->createSchemaManager()->listTableColumns('write_table');

self::assertEquals(2, count($columns), 'listTableColumns() should return the number of exact number of columns');

Check warning on line 54 in tests/Functional/TableDropColumnTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Line exceeds 120 characters; contains 121 characters
}
}
Loading