Skip to content

Commit c6bf283

Browse files
committed
change function listTableIndexes
1 parent 3695bc4 commit c6bf283

File tree

3 files changed

+72
-4
lines changed

3 files changed

+72
-4
lines changed

src/ClickHouseSchemaManager.php

+36
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,22 @@
1616

1717
use Doctrine\DBAL\Schema\AbstractSchemaManager;
1818
use Doctrine\DBAL\Schema\Column;
19+
use Doctrine\DBAL\Schema\Index;
1920
use Doctrine\DBAL\Schema\View;
2021
use Doctrine\DBAL\Types\Type;
2122
use const CASE_LOWER;
2223
use function array_change_key_case;
24+
use function array_filter;
25+
use function array_key_exists;
26+
use function array_map;
27+
use function array_reverse;
28+
use function current;
29+
use function explode;
30+
use function is_array;
31+
use function preg_match;
2332
use function preg_replace;
2433
use function stripos;
34+
use function strpos;
2535
use function strtolower;
2636
use function trim;
2737

@@ -53,6 +63,32 @@ protected function _getPortableViewDefinition($view)
5363
*/
5464
public function listTableIndexes($table) : array
5565
{
66+
$tableView = $this->_getPortableViewDefinition(['name' => $table]);
67+
68+
preg_match(
69+
'/MergeTree\(([\w+, \(\)]+)(?= \(((?:[^()]|\((?2)\))+)\),)/mi',
70+
$tableView->getSql(),
71+
$matches
72+
);
73+
74+
if (is_array($matches) && array_key_exists(2, $matches)) {
75+
$indexColumns = array_filter(
76+
array_map('trim', explode(',', $matches[2])),
77+
function (string $column) {
78+
return strpos($column, '(') === false;
79+
}
80+
);
81+
82+
return [
83+
new Index(
84+
current(array_reverse(explode('.', $table))) . '__pk',
85+
$indexColumns,
86+
false,
87+
true
88+
),
89+
];
90+
}
91+
5692
return [];
5793
}
5894

src/ClickHouseStatement.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n
185185
if ($this->assumeFetchMode($fetchMode) === FetchMode::NUMERIC) {
186186
return array_map(
187187
'array_values',
188-
(array) $this->rows
188+
$this->rows
189189
);
190190
}
191191

@@ -194,7 +194,7 @@ public function fetchAll($fetchMode = null, $fetchArgument = null, $ctorArgs = n
194194
function ($row) {
195195
return array_values($row) + $row;
196196
},
197-
(array) $this->rows
197+
$this->rows
198198
);
199199
}
200200

@@ -203,7 +203,7 @@ function ($row) {
203203
function ($row) {
204204
return (object) $row;
205205
},
206-
(array) $this->rows
206+
$this->rows
207207
);
208208
}
209209

@@ -218,7 +218,7 @@ function ($row) {
218218

219219
return [array_shift($row) => array_shift($row)];
220220
},
221-
(array) $this->rows
221+
$this->rows
222222
);
223223
}
224224

tests/CreateSchemaTest.php

+32
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace FOD\DBALClickHouse\Tests;
1313

1414
use Doctrine\DBAL\DBALException;
15+
use Doctrine\DBAL\Schema\Index;
1516
use Doctrine\DBAL\Types\Type;
1617
use FOD\DBALClickHouse\ClickHouseSchemaManager;
1718
use FOD\DBALClickHouse\Connection;
@@ -240,6 +241,37 @@ public function testEventDateProviderColumnBadOption()
240241
$this->connection->exec('DROP TABLE test_table');
241242
}
242243

244+
public function testListTableIndexes()
245+
{
246+
$fromSchema = $this->connection->getSchemaManager()->createSchema();
247+
$toSchema = clone $fromSchema;
248+
249+
$newTable = $toSchema->createTable('test_indexes_table');
250+
251+
$newTable->addColumn('id', 'integer', ['unsigned' => true]);
252+
$newTable->addColumn('payload', 'string');
253+
$newTable->addColumn('event_date', Type::DATE);
254+
$newTable->addOption('eventDateColumn', 'event_date');
255+
$newTable->setPrimaryKey(['id', 'event_date']);
256+
$migrationSQLs = $fromSchema->getMigrateToSql($toSchema, $this->connection->getDatabasePlatform());
257+
foreach ($migrationSQLs as $sql) {
258+
$this->connection->exec($sql);
259+
}
260+
261+
$indexes = $this->connection->getSchemaManager()->listTableIndexes('test_indexes_table');
262+
263+
$this->assertEquals(1, \count($indexes));
264+
265+
if($index = current($indexes)) {
266+
$this->assertInstanceOf(Index::class, $index);
267+
268+
$this->assertEquals(['id', 'event_date'], $index->getColumns());
269+
$this->assertTrue($index->isPrimary());
270+
}
271+
272+
$this->connection->exec('DROP TABLE test_indexes_table');
273+
}
274+
243275
public function testNullableColumns()
244276
{
245277
$fromSchema = $this->connection->getSchemaManager()->createSchema();

0 commit comments

Comments
 (0)