diff --git a/src/Platforms/PostgreSQLPlatform.php b/src/Platforms/PostgreSQLPlatform.php index 9e6648cbda2..c53dd3c0d8c 100644 --- a/src/Platforms/PostgreSQLPlatform.php +++ b/src/Platforms/PostgreSQLPlatform.php @@ -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'; } diff --git a/src/Schema/PostgreSQLSchemaManager.php b/src/Schema/PostgreSQLSchemaManager.php index 13fde63b2b8..471324b4a13 100644 --- a/src/Schema/PostgreSQLSchemaManager.php +++ b/src/Schema/PostgreSQLSchemaManager.php @@ -657,6 +657,7 @@ protected function selectTableColumns(string $databaseName, ?string $tableName = 'a.attnum > 0', "c.relkind = 'r'", 'd.refobjid IS NULL', + 'a.attisdropped = false', ], $this->buildQueryConditions($tableName)); $sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY a.attnum'; diff --git a/tests/Functional/TableDropColumnTest.php b/tests/Functional/TableDropColumnTest.php new file mode 100644 index 00000000000..a0942d2b86b --- /dev/null +++ b/tests/Functional/TableDropColumnTest.php @@ -0,0 +1,56 @@ +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 + 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) { + 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"); + } + } + + 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'); + } +}