Skip to content

Commit 8e0c1ee

Browse files
otuhacekdg
authored andcommitted
PgSqlDriver: support for partitioned tables (#286)
1 parent f281af2 commit 8e0c1ee

2 files changed

Lines changed: 37 additions & 3 deletions

File tree

src/Database/Drivers/PgSqlDriver.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public function getTables(): array
100100
pg_catalog.pg_class AS c
101101
JOIN pg_catalog.pg_namespace AS n ON n.oid = c.relnamespace
102102
WHERE
103-
c.relkind IN ('r', 'v', 'm')
103+
c.relkind IN ('r', 'v', 'm', 'p')
104104
AND n.nspname = ANY (pg_catalog.current_schemas(FALSE))
105105
ORDER BY
106106
c.relname
@@ -131,7 +131,7 @@ public function getColumns(string $table): array
131131
LEFT JOIN pg_catalog.pg_attrdef AS ad ON ad.adrelid = c.oid AND ad.adnum = a.attnum
132132
LEFT JOIN pg_catalog.pg_constraint AS co ON co.connamespace = c.relnamespace AND contype = 'p' AND co.conrelid = c.oid AND a.attnum = ANY(co.conkey)
133133
WHERE
134-
c.relkind IN ('r', 'v', 'm')
134+
c.relkind IN ('r', 'v', 'm', 'p')
135135
AND c.oid = {$this->pdo->quote($this->delimiteFQN($table))}::regclass
136136
AND a.attnum > 0
137137
AND NOT a.attisdropped
@@ -163,7 +163,7 @@ public function getIndexes(string $table): array
163163
JOIN pg_catalog.pg_class AS c2 ON i.indexrelid = c2.oid
164164
LEFT JOIN pg_catalog.pg_attribute AS a ON c1.oid = a.attrelid AND a.attnum = ANY(i.indkey)
165165
WHERE
166-
c1.relkind = 'r'
166+
c1.relkind IN ('r', 'p')
167167
AND c1.oid = {$this->pdo->quote($this->delimiteFQN($table))}::regclass
168168
") as $row) {
169169
$indexes[$row['name']]['name'] = $row['name'];

tests/Database/Reflection.postgre.10.phpt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,37 @@ test('Materialized view columns', function () use ($connection) {
124124
array_column($driver->getColumns('source_mt'), 'name'),
125125
);
126126
});
127+
128+
129+
test('Partitioned table', function () use ($connection) {
130+
Nette\Database\Helpers::loadFromFile($connection, Tester\FileMock::create('
131+
DROP SCHEMA IF EXISTS "reflection_10" CASCADE;
132+
CREATE SCHEMA "reflection_10";
133+
134+
CREATE TABLE "reflection_10"."parted" (
135+
"id" INTEGER PRIMARY KEY,
136+
"value" INTEGER
137+
) PARTITION BY RANGE (id);
138+
139+
CREATE TABLE "reflection_10"."part_1" PARTITION OF "reflection_10"."parted" FOR VALUES FROM (1) TO (10);
140+
'));
141+
142+
$driver = $connection->getDriver();
143+
144+
$connection->query('SET search_path TO reflection_10');
145+
146+
Assert::same([
147+
['name' => 'part_1', 'view' => false, 'fullName' => 'reflection_10.part_1'],
148+
['name' => 'parted', 'view' => false, 'fullName' => 'reflection_10.parted'],
149+
], $driver->getTables());
150+
151+
Assert::same(['id', 'value'], array_column($driver->getColumns('parted'), 'name'));
152+
Assert::same(['id', 'value'], array_column($driver->getColumns('part_1'), 'name'));
153+
154+
Assert::same([[
155+
'name' => 'parted_pkey',
156+
'unique' => true,
157+
'primary' => true,
158+
'columns' => ['id'],
159+
]], $driver->getIndexes('parted'));
160+
});

0 commit comments

Comments
 (0)