Passed
Push — master ( cdfad9...e39d65 )
by Joas
12:09 queued 10s
created

AddMissingPrimaryKeys::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * @copyright Copyright (c) 2017 Bjoern Schiessle <[email protected]>
7
 *
8
 * @author Bjoern Schiessle <[email protected]>
9
 * @author Joas Schilling <[email protected]>
10
 * @author Mario Danic <[email protected]>
11
 * @author Morris Jobke <[email protected]>
12
 * @author Robin Appelman <[email protected]>
13
 * @author Roeland Jago Douma <[email protected]>
14
 * @author Thomas Citharel <[email protected]>
15
 *
16
 * @license GNU AGPL version 3 or any later version
17
 *
18
 * This program is free software: you can redistribute it and/or modify
19
 * it under the terms of the GNU Affero General Public License as
20
 * published by the Free Software Foundation, either version 3 of the
21
 * License, or (at your option) any later version.
22
 *
23
 * This program is distributed in the hope that it will be useful,
24
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
26
 * GNU Affero General Public License for more details.
27
 *
28
 * You should have received a copy of the GNU Affero General Public License
29
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30
 *
31
 */
32
33
namespace OC\Core\Command\Db;
34
35
use OC\DB\SchemaWrapper;
36
use OCP\IDBConnection;
37
use Symfony\Component\Console\Command\Command;
38
use Symfony\Component\Console\Input\InputInterface;
39
use Symfony\Component\Console\Output\OutputInterface;
40
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
41
use Symfony\Component\EventDispatcher\GenericEvent;
42
43
/**
44
 * Class AddMissingPrimaryKeys
45
 *
46
 * if you added primary keys to the database, this is the right place to add
47
 * your update routine for existing instances
48
 *
49
 * @package OC\Core\Command\Db
50
 */
51
class AddMissingPrimaryKeys extends Command {
52
53
	/** @var IDBConnection */
54
	private $connection;
55
56
	/** @var EventDispatcherInterface */
57
	private $dispatcher;
58
59
	public function __construct(IDBConnection $connection, EventDispatcherInterface $dispatcher) {
60
		parent::__construct();
61
62
		$this->connection = $connection;
63
		$this->dispatcher = $dispatcher;
64
	}
65
66
	protected function configure() {
67
		$this
68
			->setName('db:add-missing-primary-keys')
69
			->setDescription('Add missing primary keys to the database tables');
70
	}
71
72
	protected function execute(InputInterface $input, OutputInterface $output): int {
73
		$this->addCorePrimaryKeys($output);
74
75
		// Dispatch event so apps can also update indexes if needed
76
		$event = new GenericEvent($output);
77
		$this->dispatcher->dispatch(IDBConnection::ADD_MISSING_PRIMARY_KEYS_EVENT, $event);
0 ignored issues
show
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
		$this->dispatcher->/** @scrutinizer ignore-call */ 
78
                     dispatch(IDBConnection::ADD_MISSING_PRIMARY_KEYS_EVENT, $event);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
78
		return 0;
79
	}
80
81
	/**
82
	 * add missing indices to the share table
83
	 *
84
	 * @param OutputInterface $output
85
	 * @throws \Doctrine\DBAL\Schema\SchemaException
86
	 */
87
	private function addCorePrimaryKeys(OutputInterface $output) {
88
		$output->writeln('<info>Check primary keys.</info>');
89
90
		$schema = new SchemaWrapper($this->connection);
91
		$updated = false;
92
93
		if ($schema->hasTable('federated_reshares')) {
94
			$table = $schema->getTable('federated_reshares');
95
			if (!$table->hasPrimaryKey()) {
96
				$output->writeln('<info>Adding primary key to the federated_reshares table, this can take some time...</info>');
97
				$table->setPrimaryKey(['share_id'], 'federated_res_pk');
98
				if ($table->hasIndex('share_id_index')) {
99
					$table->dropIndex('share_id_index');
100
				}
101
				$this->connection->migrateToSchema($schema->getWrappedSchema());
102
				$updated = true;
103
				$output->writeln('<info>federated_reshares table updated successfully.</info>');
104
			}
105
		}
106
107
		if ($schema->hasTable('systemtag_object_mapping')) {
108
			$table = $schema->getTable('systemtag_object_mapping');
109
			if (!$table->hasPrimaryKey()) {
110
				$output->writeln('<info>Adding primary key to the systemtag_object_mapping table, this can take some time...</info>');
111
				$table->setPrimaryKey(['objecttype', 'objectid', 'systemtagid'], 'som_pk');
112
				if ($table->hasIndex('mapping')) {
113
					$table->dropIndex('mapping');
114
				}
115
				$this->connection->migrateToSchema($schema->getWrappedSchema());
116
				$updated = true;
117
				$output->writeln('<info>systemtag_object_mapping table updated successfully.</info>');
118
			}
119
		}
120
121
		if ($schema->hasTable('comments_read_markers')) {
122
			$table = $schema->getTable('comments_read_markers');
123
			if (!$table->hasPrimaryKey()) {
124
				$output->writeln('<info>Adding primary key to the comments_read_markers table, this can take some time...</info>');
125
				$table->setPrimaryKey(['user_id', 'object_type', 'object_id'], 'crm_pk');
126
				if ($table->hasIndex('comments_marker_index')) {
127
					$table->dropIndex('comments_marker_index');
128
				}
129
				$this->connection->migrateToSchema($schema->getWrappedSchema());
130
				$updated = true;
131
				$output->writeln('<info>comments_read_markers table updated successfully.</info>');
132
			}
133
		}
134
135
		if ($schema->hasTable('collres_resources')) {
136
			$table = $schema->getTable('collres_resources');
137
			if (!$table->hasPrimaryKey()) {
138
				$output->writeln('<info>Adding primary key to the collres_resources table, this can take some time...</info>');
139
				$table->setPrimaryKey(['collection_id', 'resource_type', 'resource_id'], 'crr_pk');
140
				if ($table->hasIndex('collres_unique_res')) {
141
					$table->dropIndex('collres_unique_res');
142
				}
143
				$this->connection->migrateToSchema($schema->getWrappedSchema());
144
				$updated = true;
145
				$output->writeln('<info>collres_resources table updated successfully.</info>');
146
			}
147
		}
148
149
		if ($schema->hasTable('collres_accesscache')) {
150
			$table = $schema->getTable('collres_accesscache');
151
			if (!$table->hasPrimaryKey()) {
152
				$output->writeln('<info>Adding primary key to the collres_accesscache table, this can take some time...</info>');
153
				$table->setPrimaryKey(['user_id', 'collection_id', 'resource_type', 'resource_id'], 'cra_pk');
154
				if ($table->hasIndex('collres_unique_user')) {
155
					$table->dropIndex('collres_unique_user');
156
				}
157
				$this->connection->migrateToSchema($schema->getWrappedSchema());
158
				$updated = true;
159
				$output->writeln('<info>collres_accesscache table updated successfully.</info>');
160
			}
161
		}
162
163
		if ($schema->hasTable('filecache_extended')) {
164
			$table = $schema->getTable('filecache_extended');
165
			if (!$table->hasPrimaryKey()) {
166
				$output->writeln('<info>Adding primary key to the filecache_extended table, this can take some time...</info>');
167
				$table->setPrimaryKey(['fileid'], 'fce_pk');
168
				if ($table->hasIndex('fce_fileid_idx')) {
169
					$table->dropIndex('fce_fileid_idx');
170
				}
171
				$this->connection->migrateToSchema($schema->getWrappedSchema());
172
				$updated = true;
173
				$output->writeln('<info>filecache_extended table updated successfully.</info>');
174
			}
175
		}
176
177
		if (!$updated) {
178
			$output->writeln('<info>Done.</info>');
179
		}
180
	}
181
}
182