From 9cc09cf608fbf2d85eb0d50b52d38cbce9ae3f61 Mon Sep 17 00:00:00 2001 From: Kamil Michalak Date: Thu, 21 Dec 2023 20:11:18 +0000 Subject: [PATCH 1/4] fix(PostgreSQL omit dropped columns in getListTableColumnsSQL (pg_attribute table) to avoid ........pg.dropped.x....... Signed-off-by: Kamil Michalak --- src/Platforms/PostgreSQLPlatform.php | 1 + 1 file changed, 1 insertion(+) 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'; } From 57c68c36fc3657dc2dae3dd12fdef7f2ded05675 Mon Sep 17 00:00:00 2001 From: Kamil Michalak Date: Fri, 22 Dec 2023 15:37:02 +0000 Subject: [PATCH 2/4] fix(PostgreSQL omit dropped columns in getListTableColumnsSQL (pg_attribute table) to avoid ........pg.dropped.x....... - test Signed-off-by: Kamil Michalak --- src/Schema/PostgreSQLSchemaManager.php | 1 + tests/Functional/TableDropColumnTest.php | 50 ++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 tests/Functional/TableDropColumnTest.php 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..fea8dca6b1b --- /dev/null +++ b/tests/Functional/TableDropColumnTest.php @@ -0,0 +1,50 @@ +addColumn('id', Types::INTEGER, ['autoincrement' => true]); + $table->addColumn('test_column1', Types::STRING); + $table->addColumn('test_column2', Types::INTEGER); + $table->setPrimaryKey(['id']); + $table->addIndex(['test_column1', 'test_column2']); + + $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'); + } +} From dbf2b628ab7eff4450e67578fa9ad0e173058d81 Mon Sep 17 00:00:00 2001 From: Kamil Michalak Date: Fri, 22 Dec 2023 18:15:11 +0000 Subject: [PATCH 3/4] fix(PostgreSQL omit dropped columns in getListTableColumnsSQL (pg_attribute table) to avoid ........pg.dropped.x....... - test fix Signed-off-by: Kamil Michalak --- tests/Functional/TableDropColumnTest.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/tests/Functional/TableDropColumnTest.php b/tests/Functional/TableDropColumnTest.php index fea8dca6b1b..9947bb33426 100644 --- a/tests/Functional/TableDropColumnTest.php +++ b/tests/Functional/TableDropColumnTest.php @@ -19,11 +19,20 @@ protected function setUp(): void $table->addColumn('test_column1', Types::STRING); $table->addColumn('test_column2', Types::INTEGER); $table->setPrimaryKey(['id']); - $table->addIndex(['test_column1', 'test_column2']); + + $platform = $this->connection->getDatabasePlatform(); + + $table->addIndex(['test_column1', 'test_column2'], 'test'); $this->dropAndCreateTable($table); - $this->connection->executeStatement('ALTER TABLE write_table DROP COLUMN test_column1'); + // some db engine dont allow drop column which belongs to index but on pgsql it leave pg_attribute with attisdropped=true so we can test it + try { + $this->connection->executeStatement('ALTER TABLE write_table DROP COLUMN test_column1'); + } catch (Throwable $e) { + $table->dropIndex('test'); + $this->connection->executeStatement('ALTER TABLE write_table DROP COLUMN test_column1'); + } } public function testPgSqlPgAttributeTable(): void From 32d21c21ff331c5e260c091047946d33cfdc8a49 Mon Sep 17 00:00:00 2001 From: Kamil Michalak Date: Fri, 22 Dec 2023 18:30:08 +0000 Subject: [PATCH 4/4] fix(PostgreSQL omit dropped columns in getListTableColumnsSQL (pg_attribute table) to avoid ........pg.dropped.x....... - test fix Signed-off-by: Kamil Michalak --- tests/Functional/TableDropColumnTest.php | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/tests/Functional/TableDropColumnTest.php b/tests/Functional/TableDropColumnTest.php index 9947bb33426..a0942d2b86b 100644 --- a/tests/Functional/TableDropColumnTest.php +++ b/tests/Functional/TableDropColumnTest.php @@ -22,17 +22,14 @@ protected function setUp(): void $platform = $this->connection->getDatabasePlatform(); - $table->addIndex(['test_column1', 'test_column2'], 'test'); + // 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); - // some db engine dont allow drop column which belongs to index but on pgsql it leave pg_attribute with attisdropped=true so we can test it - try { - $this->connection->executeStatement('ALTER TABLE write_table DROP COLUMN test_column1'); - } catch (Throwable $e) { - $table->dropIndex('test'); - $this->connection->executeStatement('ALTER TABLE write_table DROP COLUMN test_column1'); - } + $this->connection->executeStatement('ALTER TABLE write_table DROP COLUMN test_column1'); } public function testPgSqlPgAttributeTable(): void