-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathRemoveFKConstraints.php
More file actions
70 lines (62 loc) · 2.21 KB
/
RemoveFKConstraints.php
File metadata and controls
70 lines (62 loc) · 2.21 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 RemoveFKConstraints extends Command {
protected string $name = parent::NAME_PREFIX . 'index:remove:foreign-key-constraints';
protected string $description = 'Remove all foreign key constraints';
protected array $operationHints = [
'Remove all foreign key constraints.',
'*****************************',
'** Please understand **',
'*****************************',
'This can lead to inconsitent database states, because it affects the database integrity.',
'Therefore this command is highly NOT RECOMMENDED and should only be executed if you know what you are doing.',
'',
'To recreate the Foreign key constraints, run the command \'occ ' . parent::NAME_PREFIX . 'index:create\'',
'Note: NO data migration will be executed, so make sure you have a backup of your database.',
];
public function __construct(
private IndexManager $indexManager,
private IDBConnection $connection,
private Schema $schema,
) {
parent::__construct();
}
protected function runCommands(): int {
// remove constraints and indices
$this->schema = $this->connection->createSchema();
$this->indexManager->setSchema($this->schema);
$this->deleteForeignKeyConstraints();
$this->deleteGenericIndices();
$this->connection->migrateToSchema($this->schema);
return 0;
}
/**
* remove all generic indices (the only generic indices should
* result from the FK Constraints)
*/
private function deleteGenericIndices(): void {
$this->printComment('Remove generic indices');
$messages = $this->indexManager->removeAllGenericIndices();
$this->printInfo($messages, ' - ');
}
/**
* remove on delete fk contraint from all tables referencing the main polls table
*/
private function deleteForeignKeyConstraints(): void {
$this->printComment('Remove foreign key constraints and generic indices');
$messages = $this->indexManager->removeAllForeignKeyConstraints();
$this->printInfo($messages, ' - ');
}
}