Issues (496)

lib/Migration/Version3007Date20211003180000.php (5 issues)

Labels
Severity
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
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
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\DB\QueryBuilder\IQueryBuilder;
0 ignored issues
show
The type OCP\DB\QueryBuilder\IQueryBuilder 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\IOutput;
0 ignored issues
show
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...
18
use OCP\Migration\SimpleMigrationStep;
0 ignored issues
show
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...
19
20
/**
21
 * Auto-generated migration step: Please modify to your needs!
22
 * sudo -u www-data php /var/www/nextcloud/occ migrations:execute analytics 3007Date20211003180000
23
 *
24
 * Deletions
25
 * dataset:
26
 * subheader, link, visual, chart*, *options, parent, type
27
 */
28
class Version3007Date20211003180000 extends SimpleMigrationStep
29
{
30
31
    /** @var IDBConnection */
32
    private $connection;
33
34
    public function __construct(IDBConnection $connection)
35
    {
36
        $this->connection = $connection;
37
    }
38
39
    /**
40
     * @param IOutput $output
41
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
42
     * @param array $options
43
     */
44
    public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options)
45
    {
46
    }
47
48
    /**
49
     * @param IOutput $output
50
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
51
     * @param array $options
52
     * @return null|ISchemaWrapper
53
     */
54
    public function changeSchema(IOutput $output, Closure $schemaClosure, array $options)
55
    {
56
        /** @var ISchemaWrapper $schema */
57
        $schema = $schemaClosure();
58
59
        if (!$schema->hasTable('analytics_report')) {
60
            $table = $schema->createTable('analytics_report');
61
            $table->addColumn('id', 'integer', [
62
                'autoincrement' => true,
63
                'notnull' => true,
64
            ]);
65
            $table->addColumn('user_id', 'string', [
66
                'notnull' => true,
67
                'length' => 64,
68
            ]);
69
            $table->addColumn('dataset', 'integer', [
70
                'notnull' => true,
71
            ]);
72
            $table->addColumn('name', 'string', [
73
                'notnull' => true,
74
                'length' => 64,
75
            ]);
76
            $table->addColumn('subheader', 'string', [
77
                'notnull' => false,
78
                'length' => 256,
79
            ]);
80
            $table->addColumn('type', 'integer', [
81
                'notnull' => false,
82
            ]);
83
            $table->addColumn('link', 'string', [
84
                'notnull' => false,
85
                'length' => 500,
86
            ]);
87
            $table->addColumn('visualization', 'string', [
88
                'notnull' => false,
89
                'length' => 10,
90
            ]);
91
            $table->addColumn('chart', 'string', [
92
                'notnull' => false,
93
                'length' => 256,
94
            ]);
95
            $table->addColumn('parent', 'integer', [
96
                'notnull' => false,
97
            ]);
98
            $table->addColumn('dimension1', 'string', [
99
                'notnull' => false,
100
                'length' => 64,
101
            ]);
102
            $table->addColumn('dimension2', 'string', [
103
                'notnull' => false,
104
                'length' => 64,
105
            ]);
106
            $table->addColumn('dimension3', 'string', [
107
                'notnull' => false,
108
                'length' => 64,
109
            ]);
110
            $table->addColumn('value', 'string', [
111
                'notnull' => false,
112
                'length' => 64,
113
            ]);
114
            $table->addColumn('chartoptions', 'string', [
115
                'notnull' => false,
116
                'length' => 1000,
117
            ]);
118
            $table->addColumn('dataoptions', 'string', [
119
                'notnull' => false,
120
                'length' => 1000,
121
            ]);
122
            $table->addColumn('filteroptions', 'string', [
123
                'notnull' => false,
124
                'length' => 1000,
125
            ]);
126
            $table->addColumn('refresh', 'integer', [
127
                'notnull' => false,
128
            ]);
129
            $table->setPrimaryKey(['id']);
130
            $table->addIndex(['user_id'], 'analytics_report_user_id_idx');
131
        }
132
133
        $table = $schema->getTable('analytics_share');
134
        if (!$table->hasColumn('report')) {
135
            $table->addColumn('report', 'integer', [
136
                'notnull' => false,
137
            ]);
138
        }
139
140
        $table = $schema->getTable('analytics_threshold');
141
        if (!$table->hasColumn('report')) {
142
            $table->addColumn('report', 'integer', [
143
                'notnull' => false,
144
            ]);
145
        }
146
147
        return $schema;
148
    }
149
150
    /**
151
     * @param IOutput $output
152
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
153
     * @param array $options
154
     */
155
    public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options)
156
    {
157
        $reportIdMap = $this->copyReport();
158
        $this->fixShares($reportIdMap);
159
        $this->fixThreshold($reportIdMap);
160
        $this->fixParents($reportIdMap);
161
    }
162
163
    /**
164
     * @return int[]
165
     */
166
    protected function copyReport(): array {
167
        $reportIdMap = [];
168
169
        $insert = $this->connection->getQueryBuilder();
170
        $insert->insert('analytics_report')
171
            ->values([
172
                'user_id' => $insert->createParameter('user_id'),
173
                'dataset' => $insert->createParameter('dataset'),
174
                'name' => $insert->createParameter('name'),
175
                'subheader' => $insert->createParameter('subheader'),
176
                'type' => $insert->createParameter('type'),
177
                'link' => $insert->createParameter('link'),
178
                'visualization' => $insert->createParameter('visualization'),
179
                'chart' => $insert->createParameter('chart'),
180
                'parent' => $insert->createParameter('parent'),
181
                'dimension1' => $insert->createParameter('dimension1'),
182
                'dimension2' => $insert->createParameter('dimension2'),
183
                'dimension3' => $insert->createParameter('dimension3'),
184
                'value' => $insert->createParameter('value'),
185
                'chartoptions' => $insert->createParameter('chartoptions'),
186
                'dataoptions' => $insert->createParameter('dataoptions'),
187
                'filteroptions' => $insert->createParameter('filteroptions'),
188
                'refresh' => $insert->createParameter('refresh'),
189
            ]);
190
191
        $query = $this->connection->getQueryBuilder();
192
        $query->select('*')
193
            ->from('analytics_dataset');
194
195
        $result = $query->execute();
196
        while ($row = $result->fetch()) {
197
            $insert
198
                ->setParameter('user_id', $row['user_id'])
199
                ->setParameter('dataset', (int) $row['id'], IQueryBuilder::PARAM_INT)
200
                ->setParameter('name', $row['name'])
201
                ->setParameter('subheader', $row['subheader'])
202
                ->setParameter('type', (int) $row['type'], IQueryBuilder::PARAM_INT)
203
                ->setParameter('link', $row['link'])
204
                ->setParameter('visualization', $row['visualization'])
205
                ->setParameter('chart', $row['chart'])
206
                ->setParameter('parent', (int) $row['parent'], IQueryBuilder::PARAM_INT)
207
                ->setParameter('dimension1', $row['dimension1'])
208
                ->setParameter('dimension2', $row['dimension2'])
209
                ->setParameter('dimension3', $row['dimension3'])
210
                ->setParameter('value', $row['value'])
211
                ->setParameter('chartoptions', $row['chartoptions'])
212
                ->setParameter('dataoptions', $row['dataoptions'])
213
                ->setParameter('filteroptions', $row['filteroptions'])
214
                ->setParameter('refresh', (int) $row['refresh'], IQueryBuilder::PARAM_INT);
215
            $insert->execute();
216
217
            $reportIdMap[(int)$row['id']] = $insert->getLastInsertId();
218
        }
219
        $result->closeCursor();
220
221
        return $reportIdMap;
222
    }
223
224
    /**
225
     * @param int[] $reportIdMap
226
     */
227
    protected function fixShares(array $reportIdMap): void {
228
229
        $update = $this->connection->getQueryBuilder();
230
        $update->update('analytics_share')
231
            ->set('report', $update->createParameter('report'))
232
            ->where($update->expr()->eq('dataset', $update->createParameter('dataset')));
233
234
        $query = $this->connection->getQueryBuilder();
235
        $query->select(['id'])
236
            ->from('analytics_dataset');
237
        $result = $query->execute();
238
239
        while ($row = $result->fetch()) {
240
            $update
241
                ->setParameter('report', $reportIdMap[(int) $row['id']])
242
                ->setParameter('dataset', (int) $row['id'])
243
            ;
244
            $update->execute();
245
        }
246
    }
247
248
    /**
249
     * @param int[] $reportIdMap
250
     */
251
    protected function fixThreshold(array $reportIdMap): void {
252
253
        $update = $this->connection->getQueryBuilder();
254
        $update->update('analytics_threshold')
255
            ->set('report', $update->createParameter('report'))
256
            ->where($update->expr()->eq('dataset', $update->createParameter('dataset')));
257
258
        $query = $this->connection->getQueryBuilder();
259
        $query->select(['id'])
260
            ->from('analytics_dataset');
261
        $result = $query->execute();
262
263
        while ($row = $result->fetch()) {
264
            $update
265
                ->setParameter('report', $reportIdMap[(int) $row['id']])
266
                ->setParameter('dataset', (int) $row['id'])
267
            ;
268
            $update->execute();
269
        }
270
    }
271
272
    /**
273
     * @param int[] $reportIdMap
274
     */
275
    protected function fixParents(array $reportIdMap): void {
276
277
        $update = $this->connection->getQueryBuilder();
278
        $update->update('analytics_report')
279
            ->set('parent', $update->createParameter('parent_to'))
280
            ->where($update->expr()->eq('id', $update->createParameter('id')));
281
282
        $query = $this->connection->getQueryBuilder();
283
        $query->select(['id', 'parent'])
284
            ->from('analytics_report')
285
            ->where($query->expr()->neq('parent', $query->createNamedParameter('0')));
286
        $result = $query->execute();
287
288
        while ($row = $result->fetch()) {
289
            $update
290
                ->setParameter('parent_to', $reportIdMap[(int) $row['parent']])
291
                ->setParameter('id', (int) $row['id'])
292
            ;
293
            $update->execute();
294
        }
295
    }
296
297
}