Passed
Push — master ( 181fce...839cdd )
by Christoph
11:22
created

AddMissingIndices::addCoreIndexes()   F

Complexity

Conditions 29
Paths > 20000

Size

Total Lines 149
Code Lines 102

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 29
eloc 102
nc 24786
nop 1
dl 0
loc 149
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
/**
4
 * @copyright Copyright (c) 2017 Bjoern Schiessle <[email protected]>
5
 *
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
24
namespace OC\Core\Command\Db;
25
26
use OC\DB\SchemaWrapper;
27
use OCP\IDBConnection;
28
use Symfony\Component\Console\Command\Command;
29
use Symfony\Component\Console\Input\InputInterface;
30
use Symfony\Component\Console\Output\OutputInterface;
31
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
32
use Symfony\Component\EventDispatcher\GenericEvent;
33
34
/**
35
 * Class AddMissingIndices
36
 *
37
 * if you added any new indices to the database, this is the right place to add
38
 * it your update routine for existing instances
39
 *
40
 * @package OC\Core\Command\Db
41
 */
42
class AddMissingIndices extends Command {
43
44
	/** @var IDBConnection */
45
	private $connection;
46
47
	/** @var EventDispatcherInterface */
48
	private $dispatcher;
49
50
	public function __construct(IDBConnection $connection, EventDispatcherInterface $dispatcher) {
51
		parent::__construct();
52
53
		$this->connection = $connection;
54
		$this->dispatcher = $dispatcher;
55
	}
56
57
	protected function configure() {
58
		$this
59
			->setName('db:add-missing-indices')
60
			->setDescription('Add missing indices to the database tables');
61
	}
62
63
	protected function execute(InputInterface $input, OutputInterface $output) {
64
		$this->addCoreIndexes($output);
65
66
		// Dispatch event so apps can also update indexes if needed
67
		$event = new GenericEvent($output);
68
		$this->dispatcher->dispatch(IDBConnection::ADD_MISSING_INDEXES_EVENT, $event);
69
	}
70
71
	/**
72
	 * add missing indices to the share table
73
	 *
74
	 * @param OutputInterface $output
75
	 * @throws \Doctrine\DBAL\Schema\SchemaException
76
	 */
77
	private function addCoreIndexes(OutputInterface $output) {
78
79
		$output->writeln('<info>Check indices of the share table.</info>');
80
81
		$schema = new SchemaWrapper($this->connection);
82
		$updated = false;
83
84
		if ($schema->hasTable('share')) {
85
			$table = $schema->getTable('share');
86
			if (!$table->hasIndex('share_with_index')) {
87
				$output->writeln('<info>Adding additional share_with index to the share table, this can take some time...</info>');
88
				$table->addIndex(['share_with'], 'share_with_index');
89
				$this->connection->migrateToSchema($schema->getWrappedSchema());
90
				$updated = true;
91
				$output->writeln('<info>Share table updated successfully.</info>');
92
			}
93
94
			if (!$table->hasIndex('parent_index')) {
95
				$output->writeln('<info>Adding additional parent index to the share table, this can take some time...</info>');
96
				$table->addIndex(['parent'], 'parent_index');
97
				$this->connection->migrateToSchema($schema->getWrappedSchema());
98
				$updated = true;
99
				$output->writeln('<info>Share table updated successfully.</info>');
100
			}
101
102
			if (!$table->hasIndex('owner_index')) {
103
				$output->writeln('<info>Adding additional owner index to the share table, this can take some time...</info>');
104
				$table->addIndex(['uid_owner'], 'owner_index');
105
				$this->connection->migrateToSchema($schema->getWrappedSchema());
106
				$updated = true;
107
				$output->writeln('<info>Share table updated successfully.</info>');
108
			}
109
110
			if (!$table->hasIndex('initiator_index')) {
111
				$output->writeln('<info>Adding additional initiator index to the share table, this can take some time...</info>');
112
				$table->addIndex(['uid_initiator'], 'initiator_index');
113
				$this->connection->migrateToSchema($schema->getWrappedSchema());
114
				$updated = true;
115
				$output->writeln('<info>Share table updated successfully.</info>');
116
			}
117
		}
118
119
		$output->writeln('<info>Check indices of the filecache table.</info>');
120
		if ($schema->hasTable('filecache')) {
121
			$table = $schema->getTable('filecache');
122
			if (!$table->hasIndex('fs_mtime')) {
123
				$output->writeln('<info>Adding additional mtime index to the filecache table, this can take some time...</info>');
124
				$table->addIndex(['mtime'], 'fs_mtime');
125
				$this->connection->migrateToSchema($schema->getWrappedSchema());
126
				$updated = true;
127
				$output->writeln('<info>Filecache table updated successfully.</info>');
128
			}
129
		}
130
131
		$output->writeln('<info>Check indices of the twofactor_providers table.</info>');
132
		if ($schema->hasTable('twofactor_providers')) {
133
			$table = $schema->getTable('twofactor_providers');
134
			if (!$table->hasIndex('twofactor_providers_uid')) {
135
				$output->writeln('<info>Adding additional twofactor_providers_uid index to the twofactor_providers table, this can take some time...</info>');
136
				$table->addIndex(['uid'], 'twofactor_providers_uid');
137
				$this->connection->migrateToSchema($schema->getWrappedSchema());
138
				$updated = true;
139
				$output->writeln('<info>Twofactor_providers table updated successfully.</info>');
140
			}
141
		}
142
143
		$output->writeln('<info>Check indices of the login_flow_v2 table.</info>');
144
		if ($schema->hasTable('login_flow_v2')) {
145
			$table = $schema->getTable('login_flow_v2');
146
			if (!$table->hasIndex('poll_token')) {
147
				$output->writeln('<info>Adding additional indeces to the login_flow_v2 table, this can take some time...</info>');
148
149
				foreach ($table->getIndexes() as $index) {
150
					$columns = $index->getColumns();
151
					if ($columns === ['poll_token'] ||
152
						$columns === ['login_token'] ||
153
						$columns === ['timestamp']) {
154
						$table->dropIndex($index->getName());
155
					}
156
				}
157
158
				$table->addUniqueIndex(['poll_token'], 'poll_token');
159
				$table->addUniqueIndex(['login_token'], 'login_token');
160
				$table->addIndex(['timestamp'], 'timestamp');
161
				$this->connection->migrateToSchema($schema->getWrappedSchema());
162
				$updated = true;
163
				$output->writeln('<info>login_flow_v2 table updated successfully.</info>');
164
			}
165
		}
166
167
		$output->writeln('<info>Check indices of the whats_new table.</info>');
168
		if ($schema->hasTable('whats_new')) {
169
			$table = $schema->getTable('whats_new');
170
			if (!$table->hasIndex('version')) {
171
				$output->writeln('<info>Adding version index to the whats_new table, this can take some time...</info>');
172
173
				foreach ($table->getIndexes() as $index) {
174
					if ($index->getColumns() === ['version']) {
175
						$table->dropIndex($index->getName());
176
					}
177
				}
178
179
				$table->addUniqueIndex(['version'], 'version');
180
				$this->connection->migrateToSchema($schema->getWrappedSchema());
181
				$updated = true;
182
				$output->writeln('<info>whats_new table updated successfully.</info>');
183
			}
184
		}
185
186
		$output->writeln('<info>Check indices of the cards table.</info>');
187
		if ($schema->hasTable('cards')) {
188
			$table = $schema->getTable('cards');
189
			if (!$table->hasIndex('cards_abid')) {
190
				$output->writeln('<info>Adding cards_abid index to the cards table, this can take some time...</info>');
191
192
				foreach ($table->getIndexes() as $index) {
193
					if ($index->getColumns() === ['addressbookid']) {
194
						$table->dropIndex($index->getName());
195
					}
196
				}
197
198
				$table->addIndex(['addressbookid'], 'cards_abid');
199
				$this->connection->migrateToSchema($schema->getWrappedSchema());
200
				$updated = true;
201
				$output->writeln('<info>cards table updated successfully.</info>');
202
			}
203
		}
204
205
		$output->writeln('<info>Check indices of the cards_properties table.</info>');
206
		if ($schema->hasTable('cards_properties')) {
207
			$table = $schema->getTable('cards_properties');
208
			if (!$table->hasIndex('cards_prop_abid')) {
209
				$output->writeln('<info>Adding cards_prop_abid index to the cards_properties table, this can take some time...</info>');
210
211
				foreach ($table->getIndexes() as $index) {
212
					if ($index->getColumns() === ['addressbookid']) {
213
						$table->dropIndex($index->getName());
214
					}
215
				}
216
217
				$table->addIndex(['addressbookid'], 'cards_prop_abid');
218
				$this->connection->migrateToSchema($schema->getWrappedSchema());
219
				$updated = true;
220
				$output->writeln('<info>cards_properties table updated successfully.</info>');
221
			}
222
		}
223
224
		if (!$updated) {
225
			$output->writeln('<info>Done.</info>');
226
		}
227
	}
228
}
229