-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathCreateIndices.php
More file actions
70 lines (61 loc) · 1.93 KB
/
CreateIndices.php
File metadata and controls
70 lines (61 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2021 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Polls\Command\Db;
use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Command\Command;
use OCA\Polls\Db\V7\IndexManager;
use OCP\IDBConnection;
/**
* @psalm-api
*/
class CreateIndices extends Command {
protected string $name = parent::NAME_PREFIX . 'index:create';
protected string $description = 'Add unique indices and foreign key constraints';
protected array $operationHints = [
'Adds unique indices and foreign key constraints.',
'To create the optional indices, run the command \'occ db:add-missing-indices\'',
];
public function __construct(
private IndexManager $indexManager,
private IDBConnection $connection,
private Schema $schema,
) {
parent::__construct();
}
protected function runCommands(): int {
// create indices and constraints
// secure, that the schema is updated to the current status
$this->schema = $this->connection->createSchema();
$this->indexManager->setSchema($this->schema);
$this->addForeignKeyConstraints();
$this->addUniqueIndices();
$this->connection->migrateToSchema($this->schema);
$this->listExistingIndices();
return 0;
}
private function listExistingIndices(): void {
$this->printComment('Existing indices:');
$messages = $this->indexManager->listExistingIndices();
$this->printInfo($messages, ' - ');
}
/**
* add an on delete fk contraint to all tables referencing the main polls table
*/
private function addForeignKeyConstraints(): void {
$this->printComment('Add foreign key constraints');
$messages = $this->indexManager->createForeignKeyConstraints();
$this->printInfo($messages, ' - ');
}
/**
* Create index for $table
*/
private function addUniqueIndices(): void {
$this->printComment('Add indices');
$messages = $this->indexManager->createUniqueIndices();
$this->printInfo($messages, ' - ');
}
}