Skip to content

Commit

Permalink
[PostgreSQL] list partitionned tables
Browse files Browse the repository at this point in the history
  • Loading branch information
nikophil committed Sep 3, 2024
1 parent 7a82524 commit 57620a0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Schema/PostgreSQLSchemaManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,13 @@ protected function selectTableColumns(string $databaseName, ?string $tableName =

$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)
"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)",

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

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

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Line exceeds 120 characters; contains 166 characters
], $this->buildQueryConditions($tableName));

$sql .= ' WHERE ' . implode(' AND ', $conditions) . ' ORDER BY a.attnum';
Expand Down
26 changes: 26 additions & 0 deletions tests/Functional/Schema/PostgreSQLSchemaManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,32 @@ 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 PRIMARY KEY) PARTITION BY LIST (id);'

Check failure on line 606 in tests/Functional/Schema/PostgreSQLSchemaManagerTest.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.3)

Multi-line function calls must have a trailing comma after the last parameter.
);
$this->connection->executeStatement('CREATE TABLE partition PARTITION OF partitioned_table DEFAULT;');

$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'));
}
}

class MoneyType extends Type
Expand Down

0 comments on commit 57620a0

Please sign in to comment.