Issues (27)

lib/Migration/CreateIndices.php (2 issues)

Labels
Severity
1
<?php
2
/**
3
 * @copyright Copyright (c) 2021 RenĂ© Gieling <[email protected]>
4
 *
5
 * @author RenĂ© Gieling <[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\Polls\Migration;
26
27
use OC\DB\Connection;
0 ignored issues
show
The type OC\DB\Connection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use OC\DB\SchemaWrapper;
0 ignored issues
show
The type OC\DB\SchemaWrapper was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use OCP\Migration\IRepairStep;
30
use OCP\Migration\IOutput;
31
32
class CreateIndices implements IRepairStep {
33
34
	/** @var Connection */
35
	private $connection;
36
37
	public function __construct(Connection $connection) {
38
		$this->connection = $connection;
39
	}
40
41
	public function getName() {
42
		return 'Create polls table indices';
43
	}
44
45
	/**
46
	 * @return void
47
	 */
48
	public function run(IOutput $output) {
49
		$this->createIndex('polls_options', 'UNIQ_options', ['poll_id', 'poll_option_text', 'timestamp'], true);
50
		$this->createIndex('polls_log', 'UNIQ_unprocessed', ['processed', 'poll_id', 'user_id', 'message_id'], true);
51
		$this->createIndex('polls_notif', 'UNIQ_subscription', ['poll_id', 'user_id'], true);
52
		$this->createIndex('polls_share', 'UNIQ_shares', ['poll_id', 'user_id'], true);
53
		$this->createIndex('polls_votes', 'UNIQ_votes', ['poll_id', 'user_id', 'vote_option_text'], true);
54
		$this->createIndex('polls_preferences', 'UNIQ_preferences', ['user_id'], true);
55
		$this->createIndex('polls_watch', 'UNIQ_watch', ['poll_id', 'table'], true);
56
	}
57
58
	/**
59
	 * Create index for $table
60
	 *
61
	 * @return void
62
	 */
63
	private function createIndex(string $tableName, string $indexName, array $columns, bool $unique = false): void {
64
		$schema = new SchemaWrapper($this->connection);
65
		if ($schema->hasTable($tableName)) {
66
			$table = $schema->getTable($tableName);
67
			if (!$table->hasIndex($indexName)) {
68
				if ($unique) {
69
					$table->addUniqueIndex($columns, $indexName);
70
				} else {
71
					$table->addIndex($columns, $indexName);
72
				}
73
				$this->connection->migrateToSchema($schema->getWrappedSchema());
74
			}
75
		}
76
	}
77
}
78