Completed
Pull Request — master (#551)
by Maxence
02:09
created

Version0019Date20200603080001::postSchemaChange()   D

Complexity

Conditions 10
Paths 512

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 39
rs 4.1777
c 0
b 0
f 0
cc 10
nc 512
nop 3

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
 * Circles - Bring cloud-users closer together.
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the COPYING file.
7
 *
8
 * @author Maxence Lange <[email protected]>
9
 * @copyright 2019
10
 * @license GNU AGPL version 3 or any later version
11
 *
12
 * This program is free software: you can redistribute it and/or modify
13
 * it under the terms of the GNU Affero General Public License as
14
 * published by the Free Software Foundation, either version 3 of the
15
 * License, or (at your option) any later version.
16
 *
17
 * This program is distributed in the hope that it will be useful,
18
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
 * GNU Affero General Public License for more details.
21
 *
22
 * You should have received a copy of the GNU Affero General Public License
23
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24
 *
25
 */
26
27
declare(strict_types=1);
28
29
namespace OCA\Circles\Migration;
30
31
use Closure;
32
use Doctrine\DBAL\Schema\SchemaException;
33
use OCP\DB\ISchemaWrapper;
34
use OCP\IDBConnection;
35
use OCP\Migration\IOutput;
36
use OCP\Migration\SimpleMigrationStep;
37
38
/**
39
 * Auto-generated migration step: Please modify to your needs!
40
 */
41
class Version0019Date20200603080001 extends SimpleMigrationStep {
42
43
44
	/** @var IDBConnection */
45
	private $connection;
46
47
48
	/**
49
	 * @param IDBConnection $connection
50
	 */
51
	public function __construct(IDBConnection $connection) {
52
		$this->connection = $connection;
53
	}
54
55
56
	/**
57
	 * @param IOutput $output
58
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
59
	 * @param array $options
60
	 *
61
	 * @return null|ISchemaWrapper
62
	 * @throws SchemaException
63
	 */
64
	public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
65
		/** @var ISchemaWrapper $schema */
66
		$schema = $schemaClosure();
67
68
		if (!$schema->hasTable('circle_circles')) {
69
			$table = $schema->createTable('circle_circles');
70
			$table->addColumn(
71
				'id', 'integer', [
72
						'autoincrement' => true,
73
						'notnull'       => true,
74
						'length'        => 4,
75
						'unsigned'      => true,
76
					]
77
			);
78
			$table->addColumn(
79
				'unique_id', 'string', [
80
							   'notnull' => true,
81
							   'length'  => 15,
82
						   ]
83
			);
84
			$table->addColumn(
85
				'long_id', 'string', [
86
							 'notnull' => true,
87
							 'length'  => 64,
88
						 ]
89
			);
90
			$table->addColumn(
91
				'name', 'string', [
92
						  'notnull' => true,
93
						  'length'  => 127,
94
					  ]
95
			);
96
			$table->addColumn(
97
				'alt_name', 'string', [
98
							  'notnull' => false,
99
							  'length'  => 127,
100
							  'default' => ''
101
						  ]
102
			);
103
			$table->addColumn(
104
				'description', 'text', [
105
								 'notnull' => false
106
							 ]
107
			);
108
			$table->addColumn(
109
				'settings', 'text', [
110
							  'notnull' => false
111
						  ]
112
			);
113
			$table->addColumn(
114
				'type', 'smallint', [
115
						  'notnull' => true,
116
						  'length'  => 2,
117
					  ]
118
			);
119
			$table->addColumn(
120
				'creation', 'datetime', [
121
							  'notnull' => false,
122
						  ]
123
			);
124
			$table->addColumn(
125
				'contact_addressbook', 'integer', [
126
										 'notnull'  => false,
127
										 'unsigned' => true,
128
										 'length'   => 7,
129
									 ]
130
			);
131
			$table->addColumn(
132
				'contact_groupname', 'string', [
133
									   'notnull' => false,
134
									   'length'  => 127,
135
								   ]
136
			);
137
			$table->setPrimaryKey(['id']);
138
			$table->addUniqueIndex(['unique_id']);
139
			$table->addUniqueIndex(['long_id']);
140
			$table->addIndex(['type']);
141
		}
142
143
//
144
//		if (!$schema->hasTable('circle_clouds')) {
145
//			$table = $schema->createTable('circle_clouds');
146
//			$table->addColumn(
147
//				'cloud_id', 'string', [
148
//							  'notnull' => true,
149
//							  'length'  => 64,
150
//						  ]
151
//			);
152
//			$table->addColumn(
153
//				'address', 'string', [
154
//							 'notnull' => true,
155
//							 'length'  => 255,
156
//						 ]
157
//			);
158
//			$table->addColumn(
159
//				'status', 'smallint', [
160
//							'notnull' => true,
161
//							'length'  => 1,
162
//						]
163
//			);
164
//			$table->addColumn(
165
//				'note', 'text', [
166
//						  'notnull' => false
167
//					  ]
168
//			);
169
//			$table->addColumn(
170
//				'created', 'datetime', [
171
//							 'notnull' => false,
172
//						 ]
173
//			);
174
//			$table->setPrimaryKey(['cloud_id']);
175
//		}
176
177
178 View Code Duplication
		if (!$schema->hasTable('circle_groups')) {
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...
179
			$table = $schema->createTable('circle_groups');
180
			$table->addColumn(
181
				'circle_id', 'string', [
182
							   'notnull' => true,
183
							   'length'  => 15,
184
						   ]
185
			);
186
			$table->addColumn(
187
				'group_id', 'string', [
188
							  'notnull' => true,
189
							  'length'  => 64,
190
						  ]
191
			);
192
			$table->addColumn(
193
				'level', 'smallint', [
194
						   'notnull' => true,
195
						   'length'  => 1,
196
					   ]
197
			);
198
			$table->addColumn(
199
				'note', 'text', [
200
						  'notnull' => false
201
					  ]
202
			);
203
			$table->addColumn(
204
				'joined', 'datetime', [
205
							'notnull' => false,
206
						]
207
			);
208
			$table->setPrimaryKey(['circle_id', 'group_id']);
209
		}
210
211
212
		if (!$schema->hasTable('circle_gsevents')) {
213
			$table = $schema->createTable('circle_gsevents');
214
			$table->addColumn(
215
				'token', 'string', [
216
						   'notnull' => false,
217
						   'length'  => 63,
218
					   ]
219
			);
220
			$table->addColumn(
221
				'event', 'text', [
222
						   'notnull' => false
223
					   ]
224
			);
225
			$table->addColumn(
226
				'instance', 'string', [
227
							  'length'  => 255,
228
							  'notnull' => false
229
						  ]
230
			);
231
			$table->addColumn(
232
				'severity', 'integer', [
233
							  'length'  => 3,
234
							  'notnull' => false
235
						  ]
236
			);
237
			$table->addColumn(
238
				'status', 'integer', [
239
							'length'  => 3,
240
							'notnull' => false
241
						]
242
			);
243
			$table->addColumn(
244
				'creation', 'bigint', [
245
							  'length'  => 14,
246
							  'notnull' => false
247
						  ]
248
			);
249
			$table->addUniqueIndex(['token', 'instance']);
250
		}
251
252
253
		if (!$schema->hasTable('circle_gsshares')) {
254
			$table = $schema->createTable('circle_gsshares');
255
			$table->addColumn(
256
				'id', 'integer', [
257
						'notnull'       => false,
258
						'length'        => 11,
259
						'autoincrement' => true,
260
						'unsigned'      => true
261
					]
262
			);
263
			$table->addColumn(
264
				'circle_id', 'string', [
265
							   'length'  => 15,
266
							   'notnull' => false
267
						   ]
268
			);
269
			$table->addColumn(
270
				'owner', 'string', [
271
						   'length'  => 15,
272
						   'notnull' => false
273
					   ]
274
			);
275
			$table->addColumn(
276
				'instance', 'string', [
277
							  'length'  => 255,
278
							  'notnull' => false
279
						  ]
280
			);
281
			$table->addColumn(
282
				'token', 'string', [
283
						   'notnull' => false,
284
						   'length'  => 63
285
					   ]
286
			);
287
			$table->addColumn(
288
				'parent', 'integer', [
289
							'notnull' => false,
290
							'length'  => 11,
291
						]
292
			);
293
			$table->addColumn(
294
				'mountpoint', 'text', [
295
								'notnull' => false
296
							]
297
			);
298
			$table->addColumn(
299
				'mountpoint_hash', 'string', [
300
									 'length'  => 64,
301
									 'notnull' => false
302
								 ]
303
			);
304
			$table->setPrimaryKey(['id']);
305
			$table->addUniqueIndex(['circle_id', 'mountpoint_hash']);
306
		}
307
308
309 View Code Duplication
		if (!$schema->hasTable('circle_gsshares_mp')) {
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...
310
			$table = $schema->createTable('circle_gsshares_mp');
311
			$table->addColumn(
312
				'share_id', 'integer', [
313
							  'length'  => 11,
314
							  'notnull' => false
315
						  ]
316
			);
317
			$table->addColumn(
318
				'user_id', 'string', [
319
							 'length'  => 127,
320
							 'notnull' => false
321
						 ]
322
			);
323
			$table->addColumn(
324
				'mountpoint', 'text', [
325
								'notnull' => false
326
							]
327
			);
328
			$table->addColumn(
329
				'mountpoint_hash', 'string', [
330
									 'length'  => 64,
331
									 'notnull' => false
332
								 ]
333
			);
334
			$table->setPrimaryKey(['share_id', 'user_id']);
335
			$table->addUniqueIndex(['share_id', 'mountpoint_hash']);
336
		}
337
338
339
340
341
		if (!$schema->hasTable('circle_members')) {
342
			$table = $schema->createTable('circle_members');
343
			$table->addColumn(
344
				'circle_id', 'string', [
345
							   'notnull' => true,
346
							   'length'  => 15,
347
						   ]
348
			);
349
			$table->addColumn(
350
				'member_id', 'string', [
351
							   'notnull' => false,
352
							   'length'  => 15,
353
						   ]
354
			);
355
			$table->addColumn(
356
				'contact_id', 'string', [
357
								'notnull' => false,
358
								'length'  => 127,
359
							]
360
			);
361
			$table->addColumn(
362
				'user_type', 'smallint', [
363
							   'notnull' => true,
364
							   'length'  => 1,
365
							   'default' => 1,
366
						   ]
367
			);
368
			$table->addColumn(
369
				'user_id', 'string', [
370
							 'notnull' => true,
371
							 'length'  => 127,
372
						 ]
373
			);
374
			$table->addColumn(
375
				'cached_name', 'string', [
376
								 'notnull' => false,
377
								 'length'  => 255,
378
								 'default' => ''
379
							 ]
380
			);
381
			$table->addColumn(
382
				'cached_update', 'datetime', [
383
								   'notnull' => false,
384
							   ]
385
			);
386
			$table->addColumn(
387
				'instance', 'string', [
388
							  'default' => '',
389
							  'length'  => 255
390
						  ]
391
			);
392
			$table->addColumn(
393
				'level', 'smallint', [
394
						   'notnull' => true,
395
						   'length'  => 1,
396
					   ]
397
			);
398
			$table->addColumn(
399
				'status', 'string', [
400
							'notnull' => false,
401
							'length'  => 15,
402
						]
403
			);
404
			$table->addColumn(
405
				'note', 'text', [
406
						  'notnull' => false
407
					  ]
408
			);
409
			$table->addColumn(
410
				'joined', 'datetime', [
411
							'notnull' => false,
412
						]
413
			);
414
			$table->addColumn(
415
				'contact_meta', 'text', [
416
								  'notnull' => false
417
							  ]
418
			);
419
			$table->addColumn(
420
				'contact_checked', 'integer', [
421
									 'notnull' => false,
422
									 'length'  => 1,
423
								 ]
424
			);
425
426
			$table->setPrimaryKey(['circle_id', 'user_id', 'user_type', 'contact_id', 'instance']);
427
		}
428
429
430
		if (!$schema->hasTable('circle_tokens')) {
431
			$table = $schema->createTable('circle_tokens');
432
			$table->addColumn(
433
				'circle_id', 'string', [
434
							   'notnull' => true,
435
							   'length'  => 15,
436
						   ]
437
			);
438
			$table->addColumn(
439
				'member_id', 'string', [
440
							   'notnull' => false,
441
							   'length'  => 15,
442
						   ]
443
			);
444
			$table->addColumn(
445
				'user_id', 'string', [
446
							 'notnull' => true,
447
							 'length'  => 255,
448
						 ]
449
			);
450
			$table->addColumn(
451
				'share_id', 'bigint', [
452
							  'notnull' => true,
453
							  'length'  => 14,
454
						  ]
455
			);
456
			$table->addColumn(
457
				'token', 'string', [
458
						   'notnull' => true,
459
						   'length'  => 31,
460
					   ]
461
			);
462
			$table->addColumn(
463
				'password', 'string', [
464
							  'notnull' => true,
465
							  'length'  => 127,
466
						  ]
467
			);
468
			$table->addColumn(
469
				'accepted', 'integer', [
470
							  'notnull' => false,
471
							  'length'  => 1,
472
						  ]
473
			);
474
			$table->setPrimaryKey(['circle_id', 'user_id', 'share_id']);
475
		}
476
477
478
		return $schema;
479
	}
480
481
482
	/**
483
	 * @param IOutput $output
484
	 * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
485
	 * @param array $options
486
	 */
487
	public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
488
489
		/** @var ISchemaWrapper $schema */
490
		$schema = $schemaClosure();
491
492
		if ($schema->hasTable('circles_tokens')) {
493
			$this->copyTable('circles_tokens', 'circle_tokens');
494
		}
495
		if ($schema->hasTable('circles_circles')) {
496
			$this->copyTableCircles('circles_circles', 'circle_circles');
497
		}
498
//		if ($schema->hasTable('circles_clouds')) {
499
//			$this->copyTable('circles_clouds', 'circle_clouds');
500
//		}
501
		if ($schema->hasTable('circles_groups')) {
502
			$this->copyTable('circles_groups', 'circle_groups');
503
		}
504
		if ($schema->hasTable('circles_gsevents')) {
505
			$this->copyTable('circles_gsevents', 'circle_gsevents');
506
		}
507
		if ($schema->hasTable('circles_gsshares')) {
508
			$this->copyTable('circles_gsshares', 'circle_gsshares');
509
		}
510
		if ($schema->hasTable('circles_gsshares_mp')) {
511
			$this->copyTable('circles_gsshares_mp', 'circle_gsshares_mp');
512
		}
513
		if ($schema->hasTable('circles_links')) {
514
			$this->copyTable('circles_links', 'circle_links');
515
		}
516
		if ($schema->hasTable('circles_members')) {
517
			$this->copyTable('circles_members', 'circle_members', ['instance' => '', 'contact_id' => '']);
518
		}
519
		if ($schema->hasTable('circles_shares')) {
520
			$this->copyTable('circles_shares', 'circle_shares');
521
		}
522
523
		$this->updateMemberId();
524
		$this->updateTokens();
525
	}
526
527
528
	/**
529
	 * @param $orig
530
	 * @param $dest
531
	 * @param array $default
532
	 */
533
	protected function copyTable($orig, $dest, array $default = []) {
534
		$qb = $this->connection->getQueryBuilder();
535
536
		$qb->select('*')
537
		   ->from($orig);
538
539
		$result = $qb->execute();
540
		while ($row = $result->fetch()) {
541
			$copy = $this->connection->getQueryBuilder();
542
			$copy->insert($dest);
543
			$ak = array_keys($row);
544
			foreach ($ak as $k) {
545
				if ($row[$k] !== null) {
546
					$copy->setValue($k, $copy->createNamedParameter($row[$k]));
547
				} elseif (array_key_exists($k, $default)) {
548
					$copy->setValue($k, $copy->createNamedParameter($default[$k]));
549
				}
550
			}
551
552
			$ak = array_keys($default);
553
			foreach ($ak as $k) {
554
				if (!array_key_exists($k, $row)) {
555
					$copy->setValue($k, $copy->createNamedParameter($default[$k]));
556
				}
557
			}
558
559
			$copy->execute();
560
		}
561
	}
562
563
564
	/**
565
	 * @param $orig
566
	 * @param $dest
567
	 */
568
	protected function copyTableCircles($orig, $dest) {
569
		$qb = $this->connection->getQueryBuilder();
570
571
		$qb->select('*')
572
		   ->from($orig);
573
574
		$result = $qb->execute();
575
		while ($row = $result->fetch()) {
576
			$copy = $this->connection->getQueryBuilder();
577
			$copy->insert($dest);
578
			$ak = array_keys($row);
579
			foreach ($ak as $k) {
580
				$v = $row[$k];
581
				if ($k === 'unique_id') {
582
					$copy->setValue('unique_id', $copy->createNamedParameter(substr($v, 0, 14)));
583
					$copy->setValue('long_id', $copy->createNamedParameter($v));
584
				} else if ($v !== null) {
585
					$copy->setValue($k, $copy->createNamedParameter($v));
586
				}
587
			}
588
589
			$copy->execute();
590
		}
591
	}
592
593
594
	/**
595
	 *
596
	 */
597
	private function updateMemberId() {
598
		$qb = $this->connection->getQueryBuilder();
599
		$expr = $qb->expr();
600
601
		$orX = $expr->orX();
602
		$orX->add($expr->eq('member_id', $qb->createNamedParameter('')));
603
		$orX->add($expr->isNull('member_id'));
604
605
		$qb->select('circle_id', 'user_id', 'user_type', 'instance')
606
		   ->from('circle_members')
607
		   ->where($orX);
608
609
		$result = $qb->execute();
610
		while ($row = $result->fetch()) {
611
			$uniqueId = substr(bin2hex(openssl_random_pseudo_bytes(24)), 0, 15);
612
613
			$update = $this->connection->getQueryBuilder();
614
			$expru = $update->expr();
615
			$update->update('circle_members')
616
				   ->set('member_id', $update->createNamedParameter($uniqueId))
617
				   ->where($expru->eq('circle_id', $update->createNamedParameter($row['circle_id'])))
618
				   ->andWhere($expru->eq('user_id', $update->createNamedParameter($row['user_id'])))
619
				   ->andWhere($expru->eq('user_type', $update->createNamedParameter($row['user_type'])))
620
				   ->andWhere($expru->eq('instance', $update->createNamedParameter($row['instance'])));
621
622
			$update->execute();
623
		}
624
	}
625
626
627
	/**
628
	 *
629
	 */
630
	private function updateTokens() {
631
		$qb = $this->connection->getQueryBuilder();
632
		$expr = $qb->expr();
633
		$orX = $expr->orX();
634
		$orX->add($expr->eq('member_id', $qb->createNamedParameter('')));
635
		$orX->add($expr->isNull('member_id'));
636
		$qb->select('user_id', 'circle_id')
637
		   ->from('circle_tokens')
638
		   ->where($orX);
639
640
		$result = $qb->execute();
641
		while ($row = $result->fetch()) {
642
			$qbm = $this->connection->getQueryBuilder();
643
			$exprm = $qbm->expr();
644
645
			$qbm->select('member_id')
646
				->from('circle_members')
647
				->where($exprm->eq('circle_id', $qbm->createNamedParameter($row['circle_id'])))
648
				->andWhere($exprm->eq('user_id', $qbm->createNamedParameter($row['user_id'])))
649
				->andWhere($exprm->neq('user_type', $qbm->createNamedParameter('1')));
650
651
			$resultm = $qbm->execute();
652
			$member = $resultm->fetch();
653
654
			$update = $this->connection->getQueryBuilder();
655
			$expru = $update->expr();
656
			$update->update('circle_tokens')
657
				   ->set('member_id', $update->createNamedParameter($member['member_id']))
658
				   ->where($expru->eq('circle_id', $update->createNamedParameter($row['circle_id'])))
659
				   ->andWhere($expru->eq('user_id', $update->createNamedParameter($row['user_id'])));
660
661
			$update->execute();
662
		}
663
	}
664
665
666
}
667