Completed
Push — master ( a470d0...96cae4 )
by Julius
08:32 queued 11s
created

ConvertToBigInt   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 13
lcom 1
cbo 1
dl 0
loc 79
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A configure() 0 5 1
A getColumnsByTable() 0 7 1
B execute() 0 51 10
1
<?php
2
/**
3
 * @copyright Copyright (c) 2019 Julius Härtl <[email protected]>
4
 *
5
 * @author Julius Härtl <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
25
namespace OCA\RichDocuments\Command;
26
27
use Doctrine\DBAL\Platforms\SqlitePlatform;
28
use Doctrine\DBAL\Types\Type;
29
use Doctrine\DBAL\Types\Types;
30
use OC\DB\SchemaWrapper;
31
use OCP\IDBConnection;
32
use Symfony\Component\Console\Command\Command;
33
use Symfony\Component\Console\Input\InputInterface;
34
use Symfony\Component\Console\Output\OutputInterface;
35
use Symfony\Component\Console\Question\ConfirmationQuestion;
36
37
class ConvertToBigInt extends Command {
38
39
	/** @var IDBConnection */
40
	private $connection;
41
42
	/**
43
	 * @param IDBConnection $connection
44
	 */
45
	public function __construct(IDBConnection $connection) {
46
		$this->connection = $connection;
47
		parent::__construct();
48
	}
49
50
	protected function configure() {
51
		$this
52
			->setName('richdocuments:convert-bigint')
53
			->setDescription('Convert the ID columns of the richdocuments tables to BigInt');
54
	}
55
56
	protected function getColumnsByTable() {
57
		return [
58
			'richdocuments_wopi' => ['id', 'fileid', 'version', 'template_id', 'template_destination', 'expiry'],
59
			'richdocuments_direct' => ['id', 'fileid', 'template_id', 'template_destination', 'timestamp'],
60
			'richdocuments_assets' => ['id', 'fileid', 'timestamp'],
61
		];
62
	}
63
64
	protected function execute(InputInterface $input, OutputInterface $output): int {
65
		$schema = new SchemaWrapper($this->connection);
66
		$isSqlite = $this->connection->getDatabasePlatform() instanceof SqlitePlatform;
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Platforms\SqlitePlatform does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
67
		$updates = [];
68
69
		$tables = $this->getColumnsByTable();
70
		foreach ($tables as $tableName => $columns) {
71
			if (!$schema->hasTable($tableName)) {
72
				continue;
73
			}
74
75
			$table = $schema->getTable($tableName);
76
77
			foreach ($columns as $columnName) {
78
				$column = $table->getColumn($columnName);
79
				$isAutoIncrement = $column->getAutoincrement();
80
				$isAutoIncrementOnSqlite = $isSqlite && $isAutoIncrement;
81
				if ($column->getType()->getName() !== Types::BIGINT && !$isAutoIncrementOnSqlite) {
82
					$column->setType(Type::getType(Types::BIGINT));
83
					$column->setOptions(['length' => 20]);
84
					$column->setUnsigned(true);
85
86
					$updates[] = '* ' . $tableName . '.' . $columnName;
87
				}
88
			}
89
		}
90
91
		if (empty($updates)) {
92
			$output->writeln('<info>All tables already up to date!</info>');
93
			return 0;
94
		}
95
96
		$output->writeln('<comment>Following columns will be updated:</comment>');
97
		$output->writeln('');
98
		$output->writeln($updates);
99
		$output->writeln('');
100
		$output->writeln('<comment>This can take up to hours, depending on the number of Collabora WOPI tokens in your instance!</comment>');
101
102
		if ($input->isInteractive()) {
103
			$helper = $this->getHelper('question');
104
			$question = new ConfirmationQuestion('Continue with the conversion (y/n)? [n] ', false);
105
106
			if (!$helper->ask($input, $output, $question)) {
107
				return 1;
108
			}
109
		}
110
111
		$this->connection->migrateToSchema($schema->getWrappedSchema());
112
113
		return 0;
114
	}
115
}
116