Completed
Push — master ( c57b95...1d3e11 )
by Maxence
30s queued 14s
created

Version0021Date20210105123456::changeSchema()   F

Complexity

Conditions 15
Paths > 20000

Size

Total Lines 353

Duplication

Lines 85
Ratio 24.08 %

Importance

Changes 0
Metric Value
dl 85
loc 353
rs 1.3999
c 0
b 0
f 0
cc 15
nc 44352
nop 3

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
declare(strict_types=1);
4
5
/**
6
 * Circles - Bring cloud-users closer together.
7
 *
8
 * This file is licensed under the Affero General Public License version 3 or
9
 * later. See the COPYING file.
10
 *
11
 * @author Maxence Lange <[email protected]>
12
 * @copyright 2021
13
 * @license GNU AGPL version 3 or any later version
14
 *
15
 * This program is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License as
17
 * published by the Free Software Foundation, either version 3 of the
18
 * License, or (at your option) any later version.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27
 *
28
 */
29
30
namespace OCA\Circles\Migration;
31
32
use Closure;
33
use Doctrine\DBAL\Schema\SchemaException;
34
use OCP\DB\ISchemaWrapper;
35
use OCP\IDBConnection;
36
use OCP\Migration\IOutput;
37
use OCP\Migration\SimpleMigrationStep;
38
39
40
/**
41
 * Class Version0021Date20210105123456
42
 *
43
 * @package OCA\Circles\Migration
44
 */
45
class Version0021Date20210105123456 extends SimpleMigrationStep {
46
47
48
	/** @var IDBConnection */
49
	private $connection;
50
51
52
	/**
53
	 * @param IDBConnection $connection
54
	 */
55
	public function __construct(IDBConnection $connection) {
56
		$this->connection = $connection;
57
	}
58
59
60
	/**
61
	 * @param IOutput $output
62
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
63
	 * @param array $options
64
	 *
65
	 * @return null|ISchemaWrapper
66
	 */
67
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
68
		/** @var ISchemaWrapper $schema */
69
		$schema = $schemaClosure();
70
71
		try {
72
			$circles = $schema->getTable('circle_circles');
73 View Code Duplication
			if (!$circles->hasColumn('config')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
74
				$circles->addColumn(
75
					'config', 'integer', [
76
								'notnull'  => false,
77
								'length'   => 11,
78
								'unsigned' => true
79
							]
80
				);
81
			}
82 View Code Duplication
			if (!$circles->hasColumn('source')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
				$circles->addColumn(
84
					'source', 'integer', [
85
								'notnull'  => false,
86
								'length'   => 5,
87
								'unsigned' => true
88
							]
89
				);
90
			}
91
			if (!$circles->hasColumn('instance')) {
92
				$circles->addColumn(
93
					'instance', 'string', [
94
								  'notnull' => false,
95
								  'default' => '',
96
								  'length'  => 255
97
							  ]
98
				);
99
			}
100
			if (!$circles->hasColumn('display_name')) {
101
				$circles->addColumn(
102
					'display_name', 'string', [
103
									  'notnull' => false,
104
									  'default' => '',
105
									  'length'  => 127
106
								  ]
107
				);
108
			}
109
			$circles->addIndex(['config']);
110
		} catch (SchemaException $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
Bug introduced by
The class Doctrine\DBAL\Schema\SchemaException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
111
		}
112
113
114
		try {
115
			$circles = $schema->getTable('circle_members');
116 View Code Duplication
			if (!$circles->hasColumn('single_id')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
				$circles->addColumn(
118
					'single_id', 'string', [
119
								   'notnull' => false,
120
								   'length'  => 15
121
							   ]
122
				);
123
			}
124 View Code Duplication
			if (!$circles->hasColumn('circle_source')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
				$circles->addColumn(
126
					'circle_source', 'string', [
127
									   'notnull' => false,
128
									   'length'  => 63
129
								   ]
130
				);
131
			}
132
		} catch (SchemaException $e) {
0 ignored issues
show
Bug introduced by
The class Doctrine\DBAL\Schema\SchemaException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
133
		}
134
135
136
		if (!$schema->hasTable('circle_remotes')) {
137
			$table = $schema->createTable('circle_remotes');
138
			$table->addColumn(
139
				'id', 'integer', [
140
						'autoincrement' => true,
141
						'notnull'       => true,
142
						'length'        => 4,
143
						'unsigned'      => true,
144
					]
145
			);
146
			$table->addColumn(
147
				'type', 'string', [
148
						  'notnull' => true,
149
						  'length'  => 15,
150
						  'default' => 'Unknown'
151
					  ]
152
			);
153
			$table->addColumn(
154
				'uid', 'string', [
155
						 'notnull' => false,
156
						 'length'  => 20,
157
					 ]
158
			);
159
			$table->addColumn(
160
				'instance', 'string', [
161
							  'notnull' => false,
162
							  'length'  => 127,
163
						  ]
164
			);
165
			$table->addColumn(
166
				'href', 'string', [
167
						  'notnull' => false,
168
						  'length'  => 254,
169
					  ]
170
			);
171
			$table->addColumn(
172
				'item', 'text', [
173
						  'notnull' => false,
174
					  ]
175
			);
176
			$table->addColumn(
177
				'creation', 'datetime', [
178
							  'notnull' => false,
179
						  ]
180
			);
181
182
			$table->setPrimaryKey(['id']);
183
			$table->addUniqueIndex(['instance']);
184
			$table->addIndex(['uid']);
185
			$table->addIndex(['href']);
186
		}
187
188
189
		if (!$schema->hasTable('circle_events')) {
190
			$table = $schema->createTable('circle_events');
191
			$table->addColumn(
192
				'token', 'string', [
193
						   'notnull' => false,
194
						   'length'  => 63,
195
					   ]
196
			);
197
			$table->addColumn(
198
				'event', 'text', [
199
						   'notnull' => false
200
					   ]
201
			);
202
			$table->addColumn(
203
				'result', 'text', [
204
							'notnull' => false
205
						]
206
			);
207
			$table->addColumn(
208
				'instance', 'string', [
209
							  'length'  => 255,
210
							  'notnull' => false
211
						  ]
212
			);
213
			$table->addColumn(
214
				'severity', 'integer', [
215
							  'length'  => 3,
216
							  'notnull' => false
217
						  ]
218
			);
219
			$table->addColumn(
220
				'status', 'integer', [
221
							'length'  => 3,
222
							'notnull' => false
223
						]
224
			);
225
			$table->addColumn(
226
				'creation', 'bigint', [
227
							  'length'  => 14,
228
							  'notnull' => false
229
						  ]
230
			);
231
232
			$table->addUniqueIndex(['token', 'instance']);
233
		}
234
235
236 View Code Duplication
		if (!$schema->hasTable('circle_membership')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
			$table = $schema->createTable('circle_membership');
238
239
			$table->addColumn(
240
				'circle_id', 'string', [
241
							   'notnull' => true,
242
							   'length'  => 15,
243
						   ]
244
			);
245
			$table->addColumn(
246
				'single_id', 'string', [
247
							   'notnull' => true,
248
							   'length'  => 15,
249
						   ]
250
			);
251
			$table->addColumn(
252
				'level', 'integer', [
253
						   'notnull'  => true,
254
						   'length'   => 1,
255
						   'unsigned' => true
256
					   ]
257
			);
258
			$table->addColumn(
259
				'inheritance_first', 'string', [
260
									   'notnull' => true,
261
									   'length'  => 15,
262
								   ]
263
			);
264
			$table->addColumn(
265
				'inheritance_last', 'string', [
266
									  'notnull' => true,
267
									  'length'  => 15,
268
								  ]
269
			);
270
			$table->addColumn(
271
				'inheritance_depth', 'integer', [
272
									   'notnull'  => true,
273
									   'length'   => 2,
274
									   'unsigned' => true
275
								   ]
276
			);
277
			$table->addColumn(
278
				'inheritance_path', 'text', [
279
									  'notnull' => true
280
								  ]
281
			);
282
283
			$table->addIndex(['single_id']);
284
			$table->addUniqueIndex(['single_id', 'circle_id']);
285
			$table->addIndex(['inheritance_first', 'inheritance_last', 'circle_id'], 'ifilci');
286
		}
287
288
289
		if (!$schema->hasTable('circle_mount')) {
290
			$table = $schema->createTable('circle_mount');
291
			$table->addColumn(
292
				'id', 'integer', [
293
						'autoincrement' => true,
294
						'notnull'       => true,
295
						'length'        => 11,
296
						'unsigned'      => true,
297
					]
298
			);
299
			$table->addColumn(
300
				'mount_id', 'string', [
301
							  'notnull' => false,
302
							  'length'  => 15
303
						  ]
304
			);
305
			$table->addColumn(
306
				'circle_id', 'string', [
307
							   'notnull' => false,
308
							   'length'  => 15
309
						   ]
310
			);
311
			$table->addColumn(
312
				'single_id', 'string', [
313
						   'notnull' => false,
314
						   'length'  => 15
315
					   ]
316
			);
317
			$table->addColumn(
318
				'token', 'string', [
319
						   'notnull' => false,
320
						   'length'  => 63
321
					   ]
322
			);
323
			$table->addColumn(
324
				'parent', 'integer', [
325
							'notnull' => false,
326
							'length'  => 11
327
						]
328
			);
329
			$table->addColumn(
330
				'mountpoint', 'text', [
331
								'notnull' => false
332
							]
333
			);
334
			$table->addColumn(
335
				'mountpoint_hash', 'string', [
336
									 'notnull' => false,
337
									 'length'  => 64
338
								 ]
339
			);
340
341
			$table->setPrimaryKey(['id']);
342
			$table->addIndex(['circle_id', 'mount_id', 'parent', 'token'], 'cmpt');
343
		}
344
345
346
		if (!$schema->hasTable('circle_mountpoint')) {
347
			$table = $schema->createTable('circle_mountpoint');
348
			$table->addColumn(
349
				'id', 'integer', [
350
						'autoincrement' => true,
351
						'notnull'       => true,
352
						'length'        => 11,
353
						'unsigned'      => true,
354
					]
355
			);
356
			$table->addColumn(
357
				'mount_id', 'string', [
358
							  'notnull' => false,
359
							  'length'  => 15
360
						  ]
361
			);
362
			$table->addColumn(
363
				'single_id', 'string', [
364
							   'notnull' => false,
365
							   'length'  => 15
366
						   ]
367
			);
368
			$table->addColumn(
369
				'mountpoint', 'text', [
370
								'notnull' => false
371
							]
372
			);
373
			$table->addColumn(
374
				'mountpoint_hash', 'string', [
375
									 'notnull' => false,
376
									 'length'  => 64
377
								 ]
378
			);
379
380
			$table->setPrimaryKey(['id']);
381
			$table->addIndex(['mount_id', 'single_id'], 'ms');
382
		}
383
384
385
		if (!$schema->hasTable('circle_share_locks')) {
386
			$table = $schema->createTable('circle_share_locks');
387
			$table->addColumn(
388
				'id', 'integer', [
389
						'autoincrement' => true,
390
						'notnull'       => true,
391
						'length'        => 4,
392
						'unsigned'      => true,
393
					]
394
			);
395
			$table->addColumn(
396
				'item_id', 'string', [
397
							 'notnull' => true,
398
							 'length'  => 15
399
						 ]
400
			);
401
			$table->addColumn(
402
				'circle_id', 'string', [
403
							   'notnull' => true,
404
							   'length'  => 15
405
						   ]
406
			);
407
			$table->addColumn(
408
				'instance', 'string', [
409
							  'notnull' => true,
410
							  'length'  => 127,
411
						  ]
412
			);
413
414
			$table->setPrimaryKey(['id']);
415
			$table->addUniqueIndex(['item_id', 'circle_id']);
416
		}
417
418
		return $schema;
419
	}
420
421
422
}
423