Passed
Push — master ( 1330f4...591d77 )
by Marcel
05:54 queued 15s
created

Version4015Date20240714200000::changeSchema()   F

Complexity

Conditions 11
Paths 1024

Size

Total Lines 46
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 11
eloc 28
nc 1024
nop 3
dl 0
loc 46
rs 3.15
c 1
b 0
f 0

How to fix   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
/**
3
 * Analytics
4
 *
5
 * SPDX-FileCopyrightText: 2019-2022 Marcel Scherello
6
 * SPDX-License-Identifier: AGPL-3.0-or-later
7
 */
8
9
declare(strict_types=1);
10
11
namespace OCA\Analytics\Migration;
12
13
use Closure;
14
use OCP\DB\ISchemaWrapper;
0 ignored issues
show
Bug introduced by
The type OCP\DB\ISchemaWrapper 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...
15
use OCP\IDBConnection;
0 ignored issues
show
Bug introduced by
The type OCP\IDBConnection 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...
16
use OCP\Migration\IOutput;
0 ignored issues
show
Bug introduced by
The type OCP\Migration\IOutput 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...
17
use OCP\Migration\SimpleMigrationStep;
0 ignored issues
show
Bug introduced by
The type OCP\Migration\SimpleMigrationStep 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...
18
19
/**
20
 * Auto-generated migration step: Please modify to your needs!
21
 * sudo -u www-data php occ migrations:execute analytics 4015Date20240714200000
22
 */
23
class Version4015Date20240714200000 extends SimpleMigrationStep
24
{
25
	public function __construct(
26
		private IDBConnection $connection,
27
	) {
28
	}
29
30
    /**
31
     * @param IOutput $output
32
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
33
     * @param array $options
34
     * @return null|ISchemaWrapper
35
     */
36
    public function changeSchema(IOutput $output, Closure $schemaClosure, array $options)
37
    {
38
        /** @var ISchemaWrapper $schema */
39
        $schema = $schemaClosure();
40
41
        $table = $schema->getTable('analytics_dataset');
42
        if (!$table->hasColumn('ai_index')) {
43
            $table->addColumn('ai_index', 'integer', [
44
                'notnull' => false
45
            ]);
46
        }
47
		if ($table->hasColumn('chartoptions')) {
48
			$table->dropColumn('chartoptions');
49
		}
50
		if ($table->hasColumn('dataoptions')) {
51
			$table->dropColumn('dataoptions');
52
		}
53
		if ($table->hasColumn('filteroptions')) {
54
			$table->dropColumn('filteroptions');
55
		}
56
		if ($table->hasColumn('refresh')) {
57
			$table->dropColumn('refresh');
58
		}
59
		if ($table->hasColumn('link')) {
60
			$table->dropColumn('link');
61
		}
62
		if ($table->hasColumn('visualization')) {
63
			$table->dropColumn('visualization');
64
		}
65
		if ($table->hasColumn('chart')) {
66
			$table->dropColumn('chart');
67
		}
68
69
		$table = $schema->getTable('analytics_share');
70
		if (!$table->hasColumn('item_type')) {
71
			$table->addColumn('item_type', 'string', [
72
				'notnull' => true,
73
				'length' => 64,
74
			]);
75
		}
76
		if (!$table->hasColumn('item_source')) {
77
			$table->addColumn('item_source', 'integer', [
78
				'notnull' => false,
79
			]);
80
		}
81
		return $schema;
82
    }
83
84
	/**
85
	 * @param IOutput $output
86
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
87
	 * @param array $options
88
	 */
89
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options)
90
	{
91
92
		$update = $this->connection->getQueryBuilder();
93
		$update->update('analytics_share')
94
			   ->set('item_type', $update->createParameter('item_type'))
95
			   ->set('item_source', $update->createParameter('item_source'))
96
			   ->where($update->expr()->eq('id', $update->createParameter('id')));
97
98
		$query = $this->connection->getQueryBuilder();
99
		$query->select(['id', 'report'])
100
			  ->from('analytics_share');
101
		$result = $query->executeQuery() ;
102
103
		while ($row = $result->fetch()) {
104
			$update
105
				->setParameter('item_type', 'report')
106
				->setParameter('item_source', $row['report'])
107
				->setParameter('id', $row['id'])
108
			;
109
			$update->executeStatement();
110
		}
111
	}
112
}