Passed
Push — master ( d713cb...c7c821 )
by Marcel
13:21
created

Version020400Date20200515185352::changeSchema()   B

Complexity

Conditions 7
Paths 64

Size

Total Lines 230
Code Lines 167

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 7
eloc 167
c 1
b 0
f 0
nc 64
nop 3
dl 0
loc 230
rs 7.0666

How to fix   Long Method   

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
declare(strict_types=1);
4
5
namespace OCA\Analytics\Migration;
6
7
use Closure;
8
use OCP\DB\ISchemaWrapper;
9
use OCP\IDBConnection;
10
use OCP\Migration\IOutput;
11
use OCP\Migration\SimpleMigrationStep;
12
13
/**
14
 * Auto-generated migration step: Please modify to your needs!
15
 */
16
class Version020400Date20200515185352 extends SimpleMigrationStep
17
{
18
19
    /** @var IDBConnection */
20
    private $connection;
21
22
    public function __construct(IDBConnection $connection)
23
    {
24
        $this->connection = $connection;
25
    }
26
27
    /**
28
     * @param IOutput $output
29
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
30
     * @param array $options
31
     */
32
    public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options)
33
    {
34
    }
35
36
    /**
37
     * @param IOutput $output
38
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
39
     * @param array $options
40
     * @return null|ISchemaWrapper
41
     */
42
    public function changeSchema(IOutput $output, Closure $schemaClosure, array $options)
43
    {
44
        /** @var ISchemaWrapper $schema */
45
        $schema = $schemaClosure();
46
47
        if (!$schema->hasTable('analytics_dataset')) {
48
            $table = $schema->createTable('analytics_dataset');
49
            $table->addColumn('id', 'integer', [
50
                'autoincrement' => true,
51
                'notnull' => true,
52
            ]);
53
            $table->addColumn('user_id', 'string', [
54
                'notnull' => true,
55
                'length' => 64,
56
            ]);
57
            $table->addColumn('name', 'string', [
58
                'notnull' => true,
59
                'length' => 64,
60
            ]);
61
            $table->addColumn('subheader', 'string', [
62
                'notnull' => false,
63
                'length' => 256,
64
            ]);
65
            $table->addColumn('type', 'integer', [
66
                'notnull' => false,
67
            ]);
68
            $table->addColumn('link', 'string', [
69
                'notnull' => false,
70
                'length' => 256,
71
            ]);
72
            $table->addColumn('visualization', 'string', [
73
                'notnull' => false,
74
                'length' => 10,
75
            ]);
76
            $table->addColumn('chart', 'string', [
77
                'notnull' => false,
78
                'length' => 256,
79
            ]);
80
            $table->addColumn('parent', 'integer', [
81
                'notnull' => false,
82
            ]);
83
            $table->addColumn('dimension1', 'string', [
84
                'notnull' => false,
85
                'length' => 64,
86
            ]);
87
            $table->addColumn('dimension2', 'string', [
88
                'notnull' => false,
89
                'length' => 64,
90
            ]);
91
            $table->addColumn('dimension3', 'string', [
92
                'notnull' => false,
93
                'length' => 64,
94
            ]);
95
            $table->addColumn('chartoptions', 'string', [
96
                'notnull' => false,
97
                'length' => 1000,
98
            ]);
99
            $table->setPrimaryKey(['id']);
100
            $table->addIndex(['user_id'], 'analytics_dataset_user_id_idx');
101
        }
102
103
        if (!$schema->hasTable('analytics_facts')) {
104
            $table = $schema->createTable('analytics_facts');
105
            $table->addColumn('id', 'integer', [
106
                'autoincrement' => true,
107
                'notnull' => true,
108
            ]);
109
            $table->addColumn('user_id', 'string', [
110
                'notnull' => true,
111
                'length' => 64,
112
            ]);
113
            $table->addColumn('dataset', 'integer', [
114
                'notnull' => true,
115
            ]);
116
            $table->addColumn('dimension1', 'string', [
117
                'notnull' => false,
118
                'length' => 64,
119
            ]);
120
            $table->addColumn('dimension2', 'string', [
121
                'notnull' => false,
122
                'length' => 64,
123
            ]);
124
            $table->addColumn('dimension3', 'decimal', [
125
                'notnull' => false,
126
            ]);
127
            $table->setPrimaryKey(['id']);
128
            $table->addIndex(['dataset'], 'analytics_facts_dataset_idx');
129
        }
130
131
        if (!$schema->hasTable('analytics_share')) {
132
            $table = $schema->createTable('analytics_share');
133
            $table->addColumn('id', 'integer', [
134
                'autoincrement' => true,
135
                'notnull' => true,
136
            ]);
137
            $table->addColumn('dataset', 'integer', [
138
                'notnull' => true,
139
            ]);
140
            $table->addColumn('type', 'integer', [
141
                'notnull' => false,
142
            ]);
143
            $table->addColumn('uid_owner', 'string', [
144
                'notnull' => false,
145
                'length' => 64,
146
            ]);
147
            $table->addColumn('uid_initiator', 'string', [
148
                'notnull' => true,
149
                'length' => 64,
150
            ]);
151
            $table->addColumn('permissions', 'integer', [
152
                'notnull' => false,
153
            ]);
154
            $table->addColumn('token', 'string', [
155
                'notnull' => false,
156
                'length' => 32,
157
            ]);
158
            $table->addColumn('password', 'string', [
159
                'notnull' => false,
160
                'length' => 64,
161
            ]);
162
            $table->setPrimaryKey(['id']);
163
            $table->addIndex(['dataset'], 'analytics_share_dataset_idx');
164
            $table->addIndex(['uid_owner'], 'analytics_share_uid_owner_idx');
165
            $table->addIndex(['token'], 'analytics_share_token_idx');
166
        }
167
168
        if (!$schema->hasTable('analytics_threshold')) {
169
            $table = $schema->createTable('analytics_threshold');
170
            $table->addColumn('id', 'integer', [
171
                'autoincrement' => true,
172
                'notnull' => true,
173
            ]);
174
            $table->addColumn('user_id', 'string', [
175
                'notnull' => true,
176
                'length' => 64,
177
            ]);
178
            $table->addColumn('dataset', 'integer', [
179
                'notnull' => true,
180
            ]);
181
            $table->addColumn('name', 'string', [
182
                'notnull' => false,
183
                'length' => 64,
184
            ]);
185
            $table->addColumn('dimension1', 'string', [
186
                'notnull' => false,
187
                'length' => 64,
188
            ]);
189
            $table->addColumn('dimension2', 'string', [
190
                'notnull' => false,
191
                'length' => 64,
192
            ]);
193
            $table->addColumn('dimension3', 'decimal', [
194
                'notnull' => false,
195
            ]);
196
            $table->addColumn('option', 'string', [
197
                'notnull' => false,
198
                'length' => 5,
199
            ]);
200
            $table->addColumn('severity', 'integer', [
201
                'notnull' => true,
202
            ]);
203
            $table->setPrimaryKey(['id']);
204
            $table->addIndex(['dataset'], 'analytics_threshold_dataset_idx');
205
        }
206
207
        if (!$schema->hasTable('analytics_dataload')) {
208
            $table = $schema->createTable('analytics_dataload');
209
            $table->addColumn('id', 'integer', [
210
                'autoincrement' => true,
211
                'notnull' => true,
212
            ]);
213
            $table->addColumn('user_id', 'string', [
214
                'notnull' => true,
215
                'length' => 64,
216
            ]);
217
            $table->addColumn('dataset', 'integer', [
218
                'notnull' => true,
219
            ]);
220
            $table->addColumn('datasource', 'integer', [
221
                'notnull' => true,
222
            ]);
223
            $table->addColumn('name', 'string', [
224
                'notnull' => false,
225
                'length' => 64,
226
            ]);
227
            $table->addColumn('option', 'string', [
228
                'notnull' => false,
229
                'length' => 500,
230
            ]);
231
            $table->addColumn('schedule', 'string', [
232
                'notnull' => false,
233
                'length' => 1,
234
            ]);
235
            $table->setPrimaryKey(['id']);
236
            $table->addIndex(['dataset'], 'analytics_dataload_dataset_idx');
237
        }
238
239
        if (!$schema->hasTable('analytics_whats_new')) {
240
            $table = $schema->createTable('analytics_whats_new');
241
            $table->addColumn('id', 'integer', [
242
                'autoincrement' => true,
243
                'notnull' => true,
244
                'length' => 4,
245
                'unsigned' => true,
246
            ]);
247
            $table->addColumn('version', 'string', [
248
                'notnull' => true,
249
                'length' => 64,
250
                'default' => '11',
251
            ]);
252
            $table->addColumn('etag', 'string', [
253
                'notnull' => true,
254
                'length' => 64,
255
                'default' => '',
256
            ]);
257
            $table->addColumn('last_check', 'integer', [
258
                'notnull' => true,
259
                'length' => 4,
260
                'unsigned' => true,
261
                'default' => 0,
262
            ]);
263
            $table->addColumn('data', 'text', [
264
                'notnull' => true,
265
                'default' => '',
266
            ]);
267
            $table->setPrimaryKey(['id']);
268
            $table->addUniqueIndex(['version'], 'analytics_whats_new_v_idx');
269
            $table->addIndex(['version', 'etag'], 'analytics_whats_new_v_e_idx');
270
        }
271
        return $schema;
272
    }
273
274
    /**
275
     * @param IOutput $output
276
     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
277
     * @param array $options
278
     */
279
    public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options)
280
    {
281
        $query = $this->connection->getQueryBuilder();
282
        $query->insert('analytics_whats_new')
283
            ->values([
284
                'version' => $query->createNamedParameter('2.4.0'),
285
                'data' => $query->createNamedParameter('{"changelogURL":"https:\/\/github.com\/rello\/analytics\/blob\/master\/CHANGELOG.md","whatsNew":{
286
"en":{"regular":["Print and export your report","Filter reports or change the column drilldown (internal reports only)","Advanced chart customization","More chart types"],"admin":["New Features apply to users"]},
287
"de":{"regular":["Drucke und exportiere deinen Bericht","Filtere Breichte oder ändere die Spalten (nur für interne Berichte)","Fortgeschrittene Chart Einstellungen","Weitere Charttypen"],"admin":["Nur User Features"]}
288
}}'),
289
            ])
290
            ->execute();
291
    }
292
}
293