Version3005Date20210508180000::changeSchema()   F
last analyzed

Complexity

Conditions 14
Paths 2040

Size

Total Lines 310
Code Lines 226

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 226
nc 2040
nop 3
dl 0
loc 310
rs 1.68
c 0
b 0
f 0

How to fix   Long Method    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 /var/www/nextcloud/occ migrations:execute analytics 3005Date20210508180000
22
 */
23
class Version3005Date20210508180000 extends SimpleMigrationStep
24
{
25
26
    /** @var IDBConnection */
27
    private $connection;
28
29
    public function __construct(IDBConnection $connection)
30
    {
31
        $this->connection = $connection;
32
    }
33
34
    /**
35
     * @param IOutput $output
36
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
37
     * @param array $options
38
     */
39
    public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options)
40
    {
41
    }
42
43
    /**
44
     * @param IOutput $output
45
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
46
     * @param array $options
47
     * @return null|ISchemaWrapper
48
     */
49
    public function changeSchema(IOutput $output, Closure $schemaClosure, array $options)
50
    {
51
        /** @var Schema $schema */
52
        $schema = $schemaClosure();
53
54
        if (!$schema->hasTable('analytics_dataset')) {
55
            $table = $schema->createTable('analytics_dataset');
56
            $table->addColumn('id', 'integer', [
57
                'autoincrement' => true,
58
                'notnull' => true,
59
            ]);
60
            $table->addColumn('user_id', 'string', [
61
                'notnull' => true,
62
                'length' => 64,
63
            ]);
64
            $table->addColumn('name', 'string', [
65
                'notnull' => true,
66
                'length' => 64,
67
            ]);
68
            $table->addColumn('subheader', 'string', [
69
                'notnull' => false,
70
                'length' => 256,
71
            ]);
72
            $table->addColumn('type', 'integer', [
73
                'notnull' => false,
74
            ]);
75
            $table->addColumn('link', 'string', [
76
                'notnull' => false,
77
                'length' => 500,
78
            ]);
79
            $table->addColumn('visualization', 'string', [
80
                'notnull' => false,
81
                'length' => 10,
82
            ]);
83
            $table->addColumn('chart', 'string', [
84
                'notnull' => false,
85
                'length' => 256,
86
            ]);
87
            $table->addColumn('parent', 'integer', [
88
                'notnull' => false,
89
            ]);
90
            $table->addColumn('dimension1', 'string', [
91
                'notnull' => false,
92
                'length' => 64,
93
            ]);
94
            $table->addColumn('dimension2', 'string', [
95
                'notnull' => false,
96
                'length' => 64,
97
            ]);
98
            $table->addColumn('dimension3', 'string', [
99
                'notnull' => false,
100
                'length' => 64,
101
            ]);
102
            $table->addColumn('value', 'string', [
103
                'notnull' => false,
104
                'length' => 64,
105
            ]);
106
            $table->addColumn('chartoptions', 'string', [
107
                'notnull' => false,
108
                'length' => 1000,
109
            ]);
110
            $table->addColumn('dataoptions', 'string', [
111
                'notnull' => false,
112
                'length' => 1000,
113
            ]);
114
            $table->addColumn('filteroptions', 'string', [
115
                'notnull' => false,
116
                'length' => 1000,
117
            ]);
118
            $table->setPrimaryKey(['id']);
119
            $table->addIndex(['user_id'], 'analytics_dataset_user_id_idx');
120
        } else {
121
            $table = $schema->getTable('analytics_dataset');
122
            if (!$table->hasColumn('value')) {
123
                $table->addColumn('value', 'string', [
124
                    'notnull' => false,
125
                    'length' => 64,
126
                ]);
127
            }
128
            $column = $table->getColumn('link');
129
            if ($column->getLength() !== 500) {
130
                $column->setLength(500);
131
            }
132
133
            if (!$table->hasColumn('dataoptions')) {
134
                $table->addColumn('dataoptions', 'string', [
135
                    'notnull' => false,
136
                    'length' => 1000,
137
                ]);
138
            }
139
            if (!$table->hasColumn('filteroptions')) {
140
                $table->addColumn('filteroptions', 'string', [
141
                    'notnull' => false,
142
                    'length' => 1000,
143
                ]);
144
            }
145
        }
146
147
        if (!$schema->hasTable('analytics_facts')) {
148
            $table = $schema->createTable('analytics_facts');
149
            $table->addColumn('id', 'integer', [
150
                'autoincrement' => true,
151
                'notnull' => true,
152
            ]);
153
            $table->addColumn('user_id', 'string', [
154
                'notnull' => true,
155
                'length' => 64,
156
            ]);
157
            $table->addColumn('dataset', 'integer', [
158
                'notnull' => true,
159
            ]);
160
            $table->addColumn('dimension1', 'string', [
161
                'notnull' => false,
162
                'length' => 64,
163
            ]);
164
            $table->addColumn('dimension2', 'string', [
165
                'notnull' => false,
166
                'length' => 64,
167
            ]);
168
            $table->addColumn('dimension3', 'decimal', [
169
                'notnull' => false,
170
            ]);
171
            $table->addColumn('value', 'decimal', [
172
                'notnull' => false,
173
                'precision' => 15,
174
                'scale' => 2,
175
                'default' => 0,
176
            ]);
177
            $table->addColumn('timestamp', 'integer', [
178
                'notnull' => false,
179
                'default' => 0,
180
            ]);
181
            $table->setPrimaryKey(['id']);
182
            $table->addIndex(['dataset'], 'analytics_facts_dataset_idx');
183
        } else {
184
            $table = $schema->getTable('analytics_facts');
185
            if (!$table->hasColumn('value')) {
186
                $table->addColumn('value', 'decimal', [
187
                    'notnull' => false,
188
                    'precision' => 15,
189
                    'scale' => 2,
190
                    'default' => 0,
191
                ]);
192
            }
193
            if (!$table->hasColumn('timestamp')) {
194
                $table->addColumn('timestamp', 'integer', [
195
                    'notnull' => false,
196
                    'default' => 0,
197
                ]);
198
            }
199
        }
200
201
        if (!$schema->hasTable('analytics_share')) {
202
            $table = $schema->createTable('analytics_share');
203
            $table->addColumn('id', 'integer', [
204
                'autoincrement' => true,
205
                'notnull' => true,
206
            ]);
207
            $table->addColumn('dataset', 'integer', [
208
                'notnull' => true,
209
            ]);
210
            $table->addColumn('type', 'integer', [
211
                'notnull' => false,
212
            ]);
213
            $table->addColumn('uid_owner', 'string', [
214
                'notnull' => false,
215
                'length' => 64,
216
            ]);
217
            $table->addColumn('uid_initiator', 'string', [
218
                'notnull' => true,
219
                'length' => 64,
220
            ]);
221
            $table->addColumn('permissions', 'integer', [
222
                'notnull' => false,
223
            ]);
224
            $table->addColumn('token', 'string', [
225
                'notnull' => false,
226
                'length' => 32,
227
            ]);
228
            $table->addColumn('password', 'string', [
229
                'notnull' => false,
230
                'length' => 64,
231
            ]);
232
            $table->setPrimaryKey(['id']);
233
            $table->addIndex(['dataset'], 'analytics_share_dataset_idx');
234
            $table->addIndex(['uid_owner'], 'analytics_share_uid_owner_idx');
235
            $table->addIndex(['token'], 'analytics_share_token_idx');
236
        }
237
238
        if (!$schema->hasTable('analytics_threshold')) {
239
            $table = $schema->createTable('analytics_threshold');
240
            $table->addColumn('id', 'integer', [
241
                'autoincrement' => true,
242
                'notnull' => true,
243
            ]);
244
            $table->addColumn('user_id', 'string', [
245
                'notnull' => true,
246
                'length' => 64,
247
            ]);
248
            $table->addColumn('dataset', 'integer', [
249
                'notnull' => true,
250
            ]);
251
            $table->addColumn('name', 'string', [
252
                'notnull' => false,
253
                'length' => 64,
254
            ]);
255
            $table->addColumn('dimension1', 'string', [
256
                'notnull' => false,
257
                'length' => 64,
258
            ]);
259
            $table->addColumn('dimension2', 'string', [
260
                'notnull' => false,
261
                'length' => 64,
262
            ]);
263
            $table->addColumn('dimension3', 'decimal', [
264
                'notnull' => false,
265
            ]);
266
            $table->addColumn('value', 'decimal', [
267
                'notnull' => false,
268
                'precision' => 15,
269
                'scale' => 2,
270
                'default' => 0,
271
            ]);
272
            $table->addColumn('option', 'string', [
273
                'notnull' => false,
274
                'length' => 5,
275
            ]);
276
            $table->addColumn('severity', 'integer', [
277
                'notnull' => true,
278
            ]);
279
            $table->setPrimaryKey(['id']);
280
            $table->addIndex(['dataset'], 'analytics_threshold_dset_idx');
281
        } else {
282
            $table = $schema->getTable('analytics_threshold');
283
            if (!$table->hasColumn('value')) {
284
                $table->addColumn('value', 'decimal', [
285
                    'notnull' => false,
286
                    'precision' => 15,
287
                    'scale' => 2,
288
                    'default' => 0,
289
                ]);
290
            }
291
        }
292
293
        if (!$schema->hasTable('analytics_dataload')) {
294
            $table = $schema->createTable('analytics_dataload');
295
            $table->addColumn('id', 'integer', [
296
                'autoincrement' => true,
297
                'notnull' => true,
298
            ]);
299
            $table->addColumn('user_id', 'string', [
300
                'notnull' => true,
301
                'length' => 64,
302
            ]);
303
            $table->addColumn('dataset', 'integer', [
304
                'notnull' => true,
305
            ]);
306
            $table->addColumn('datasource', 'integer', [
307
                'notnull' => true,
308
            ]);
309
            $table->addColumn('name', 'string', [
310
                'notnull' => false,
311
                'length' => 64,
312
            ]);
313
            $table->addColumn('option', 'string', [
314
                'notnull' => false,
315
                'length' => 500,
316
            ]);
317
            $table->addColumn('schedule', 'string', [
318
                'notnull' => false,
319
                'length' => 1,
320
            ]);
321
            $table->setPrimaryKey(['id']);
322
            $table->addIndex(['dataset'], 'analytics_dataload_dataset_idx');
323
        }
324
325
        if (!$schema->hasTable('analytics_whats_new')) {
326
            $table = $schema->createTable('analytics_whats_new');
327
            $table->addColumn('id', 'integer', [
328
                'autoincrement' => true,
329
                'notnull' => true,
330
                'length' => 4,
331
                'unsigned' => true,
332
            ]);
333
            $table->addColumn('version', 'string', [
334
                'notnull' => true,
335
                'length' => 64,
336
                'default' => '11',
337
            ]);
338
            $table->addColumn('etag', 'string', [
339
                'notnull' => true,
340
                'length' => 64,
341
                'default' => '',
342
            ]);
343
            $table->addColumn('last_check', 'integer', [
344
                'notnull' => true,
345
                'length' => 4,
346
                'unsigned' => true,
347
                'default' => 0,
348
            ]);
349
            $table->addColumn('data', 'text', [
350
                'notnull' => true,
351
                'default' => '',
352
            ]);
353
            $table->setPrimaryKey(['id']);
354
            $table->addUniqueIndex(['version'], 'analytics_whats_new_v_idx');
355
            $table->addIndex(['version', 'etag'], 'analytics_whats_new_v_e_idx');
356
        }
357
358
        return $schema;
359
    }
360
361
    /**
362
     * @param IOutput $output
363
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
364
     * @param array $options
365
     */
366
    public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options)
367
    {
368
    }
369
}
370