Passed
Push — master ( c1368b...bc6a5e )
by Joas
13:04 queued 17s
created

AddMissingColumns::execute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

71
		$this->dispatcher->/** @scrutinizer ignore-call */ 
72
                     dispatch(IDBConnection::ADD_MISSING_COLUMNS_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...
72
	}
73
74
	/**
75
	 * add missing indices to the share table
76
	 *
77
	 * @param OutputInterface $output
78
	 * @throws \Doctrine\DBAL\Schema\SchemaException
79
	 */
80
	private function addCoreColumns(OutputInterface $output) {
81
82
		$output->writeln('<info>Check columns of the comments table.</info>');
83
84
		$schema = new SchemaWrapper($this->connection);
85
		$updated = false;
86
87
		if ($schema->hasTable('comments')) {
88
			$table = $schema->getTable('comments');
89
			if (!$table->hasColumn('reference_id')) {
90
				$output->writeln('<info>Adding additional reference_id column to the comments table, this can take some time...</info>');
91
				$table->addColumn('reference_id', 'string', [
92
					'notnull' => false,
93
					'length' => 64,
94
				]);
95
				$this->connection->migrateToSchema($schema->getWrappedSchema());
96
				$updated = true;
97
				$output->writeln('<info>Comments table updated successfully.</info>');
98
			}
99
		}
100
101
		if (!$updated) {
102
			$output->writeln('<info>Done.</info>');
103
		}
104
	}
105
}
106