Completed
Push — master ( 4751f1...b03366 )
by Morris
27:52 queued 12:19
created

AddMissingIndices   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 77
Duplicated Lines 27.27 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 21
loc 77
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A configure() 0 5 1
A execute() 0 7 1
B addShareTableIndicies() 21 41 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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->addShareTableIndicies($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 addShareTableIndicies(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 View Code Duplication
			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 View Code Duplication
			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
103
		if ($schema->hasTable('filecache')) {
104
			$table = $schema->getTable('filecache');
105 View Code Duplication
			if (!$table->hasIndex('fs_mtime')) {
106
				$output->writeln('<info>Adding additional mtime index to the filecache table, this can take some time...</info>');
107
				$table->addIndex(['mtime'], 'fs_mtime');
108
				$this->connection->migrateToSchema($schema->getWrappedSchema());
109
				$updated = true;
110
				$output->writeln('<info>Filecache table updated successfully.</info>');
111
			}
112
		}
113
114
		if (!$updated) {
115
			$output->writeln('<info>Done.</info>');
116
		}
117
	}
118
}
119