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

Passive support for partitioned tables on Postgres #6516

Merged
merged 1 commit into from
Sep 4, 2024
Merged
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
15 changes: 14 additions & 1 deletion src/Schema/PostgreSQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,21 @@

$conditions = array_merge([
'a.attnum > 0',
"c.relkind = 'r'",
'd.refobjid IS NULL',

// 'r' for regular tables - 'p' for partitioned tables
"c.relkind IN('r', 'p')",

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/PostgreSQLSchemaManager.php#L459

Added line #L459 was not covered by tests

// exclude partitions (tables that inherit from partitioned tables)
<<<'SQL'

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/PostgreSQLSchemaManager.php#L462

Added line #L462 was not covered by tests
NOT EXISTS (
SELECT 1
FROM pg_inherits
INNER JOIN pg_class parent on pg_inherits.inhparent = parent.oid
AND parent.relkind = 'p'
WHERE inhrelid = c.oid
)
SQL,

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

View check run for this annotation

Codecov / codecov/patch

src/Schema/PostgreSQLSchemaManager.php#L470

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

$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY a.attnum';
Expand Down
51 changes: 51 additions & 0 deletions tests/Functional/Schema/PostgreSQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\PostgreSQL120Platform;
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Exception\TableDoesNotExist;
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Schema\Table;
Expand Down Expand Up @@ -598,6 +599,56 @@ public static function autoIncrementTypeMigrations(): iterable
'bigint->int' => ['bigint', 'integer', 'INT'],
];
}

public function testPartitionTable(): void
{
$this->connection->executeStatement('DROP TABLE IF EXISTS partitioned_table');
$this->connection->executeStatement(
'CREATE TABLE partitioned_table (id INT) PARTITION BY LIST (id);',
);
$this->connection->executeStatement('CREATE TABLE partition PARTITION OF partitioned_table FOR VALUES IN (1);');
try {
$this->schemaManager->introspectTable('partition');
} catch (TableDoesNotExist $e) {
}

self::assertNotNull($e ?? null, 'Partition table should not be introspected');

$tableFrom = $this->schemaManager->introspectTable('partitioned_table');

$tableTo = $this->schemaManager->introspectTable('partitioned_table');
$tableTo->addColumn('foo', Types::INTEGER);

$platform = $this->connection->getDatabasePlatform();
$diff = $this->schemaManager->createComparator()->compareTables($tableFrom, $tableTo);

$sql = $platform->getAlterTableSQL($diff);
self::assertSame(['ALTER TABLE partitioned_table ADD foo INT NOT NULL'], $sql);

$this->schemaManager->alterTable($diff);

$tableFinal = $this->schemaManager->introspectTable('partitioned_table');
self::assertTrue($tableFinal->hasColumn('id'));
self::assertTrue($tableFinal->hasColumn('foo'));

$partitionedTableCount = (int) ($this->connection->fetchOne(
"select count(*) as count from pg_class where relname = 'partitioned_table' and relkind = 'p'",
));
self::assertSame(1, $partitionedTableCount);

$partitionsCount = (int) ($this->connection->fetchOne(
<<<'SQL'
select count(*) as count
from pg_class parent
inner join pg_inherits on pg_inherits.inhparent = parent.oid
inner join pg_class child on pg_inherits.inhrelid = child.oid
and child.relkind = 'r'
and child.relname = 'partition'
where parent.relname = 'partitioned_table' and parent.relkind = 'p';
SQL,
));
self::assertSame(1, $partitionsCount);
}
}

class MoneyType extends Type
Expand Down
Loading