Skip to content

Commit 1873264

Browse files
committed
Merge branch '4.2.x' into 5.0.x
* 4.2.x: Passive support for partitioned tables on Postgres (#6516) fix typo
2 parents 8592ef6 + 0e81fa2 commit 1873264

File tree

3 files changed

+66
-2
lines changed

3 files changed

+66
-2
lines changed

docs/en/reference/configuration.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ Connect to your Postgres server and run the ``SHOW server_version`` query::
409409
(1 row)
410410

411411
In the example above, ``15.2 (Debian 15.2-1.pgdg110+1)`` is the correct value for
412-
``server Version``.
412+
``serverVersion``.
413413

414414
Other Platforms
415415
^^^^^^^^^^^^^^^

src/Schema/PostgreSQLSchemaManager.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,21 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =
460460

461461
$conditions = array_merge([
462462
'a.attnum > 0',
463-
"c.relkind = 'r'",
464463
'd.refobjid IS NULL',
464+
465+
// 'r' for regular tables - 'p' for partitioned tables
466+
"c.relkind IN('r', 'p')",
467+
468+
// exclude partitions (tables that inherit from partitioned tables)
469+
<<<'SQL'
470+
NOT EXISTS (
471+
SELECT 1
472+
FROM pg_inherits
473+
INNER JOIN pg_class parent on pg_inherits.inhparent = parent.oid
474+
AND parent.relkind = 'p'
475+
WHERE inhrelid = c.oid
476+
)
477+
SQL,
465478
], $this->buildQueryConditions($tableName));
466479

467480
$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY a.attnum';

tests/Functional/Schema/PostgreSQLSchemaManagerTest.php

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

77
use Doctrine\DBAL\Platforms\AbstractPlatform;
88
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
9+
use Doctrine\DBAL\Schema\Exception\TableDoesNotExist;
910
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
1011
use Doctrine\DBAL\Schema\Schema;
1112
use Doctrine\DBAL\Schema\Table;
@@ -593,6 +594,56 @@ public static function autoIncrementTypeMigrations(): iterable
593594
'bigint->int' => ['bigint', 'integer', 'INT'],
594595
];
595596
}
597+
598+
public function testPartitionTable(): void
599+
{
600+
$this->connection->executeStatement('DROP TABLE IF EXISTS partitioned_table');
601+
$this->connection->executeStatement(
602+
'CREATE TABLE partitioned_table (id INT) PARTITION BY LIST (id);',
603+
);
604+
$this->connection->executeStatement('CREATE TABLE partition PARTITION OF partitioned_table FOR VALUES IN (1);');
605+
try {
606+
$this->schemaManager->introspectTable('partition');
607+
} catch (TableDoesNotExist $e) {
608+
}
609+
610+
self::assertNotNull($e ?? null, 'Partition table should not be introspected');
611+
612+
$tableFrom = $this->schemaManager->introspectTable('partitioned_table');
613+
614+
$tableTo = $this->schemaManager->introspectTable('partitioned_table');
615+
$tableTo->addColumn('foo', Types::INTEGER);
616+
617+
$platform = $this->connection->getDatabasePlatform();
618+
$diff = $this->schemaManager->createComparator()->compareTables($tableFrom, $tableTo);
619+
620+
$sql = $platform->getAlterTableSQL($diff);
621+
self::assertSame(['ALTER TABLE partitioned_table ADD foo INT NOT NULL'], $sql);
622+
623+
$this->schemaManager->alterTable($diff);
624+
625+
$tableFinal = $this->schemaManager->introspectTable('partitioned_table');
626+
self::assertTrue($tableFinal->hasColumn('id'));
627+
self::assertTrue($tableFinal->hasColumn('foo'));
628+
629+
$partitionedTableCount = (int) ($this->connection->fetchOne(
630+
"select count(*) as count from pg_class where relname = 'partitioned_table' and relkind = 'p'",
631+
));
632+
self::assertSame(1, $partitionedTableCount);
633+
634+
$partitionsCount = (int) ($this->connection->fetchOne(
635+
<<<'SQL'
636+
select count(*) as count
637+
from pg_class parent
638+
inner join pg_inherits on pg_inherits.inhparent = parent.oid
639+
inner join pg_class child on pg_inherits.inhrelid = child.oid
640+
and child.relkind = 'r'
641+
and child.relname = 'partition'
642+
where parent.relname = 'partitioned_table' and parent.relkind = 'p';
643+
SQL,
644+
));
645+
self::assertSame(1, $partitionsCount);
646+
}
596647
}
597648

598649
class MoneyType extends Type

0 commit comments

Comments
 (0)