Passed
Push — master ( 3c693d...ccd5ca )
by Roeland
28:56 queued 14:09
created

Version13000Date20170718121200::postSchemaChange()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 25
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 20
c 1
b 0
f 0
nc 3
nop 3
dl 0
loc 25
rs 9.6
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Joas Schilling <[email protected]>
4
 *
5
 * @author Bjoern Schiessle <[email protected]>
6
 * @author Daniel Kesselberg <[email protected]>
7
 * @author Georg Ehrke <[email protected]>
8
 * @author Joas Schilling <[email protected]>
9
 * @author Mario Danic <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 *
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 OC\Core\Migrations;
31
32
use Doctrine\DBAL\Types\Types;
33
use OCP\DB\ISchemaWrapper;
34
use OCP\IDBConnection;
35
use OCP\Migration\IOutput;
36
use OCP\Migration\SimpleMigrationStep;
37
38
class Version13000Date20170718121200 extends SimpleMigrationStep {
39
40
	/** @var IDBConnection */
41
	private $connection;
42
43
	public function __construct(IDBConnection $connection) {
44
		$this->connection = $connection;
45
	}
46
47
	public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
48
		/** @var ISchemaWrapper $schema */
49
		$schema = $schemaClosure();
50
51
		if (!$schema->hasTable('properties')) {
52
			return;
53
		}
54
		// in case we have a properties table from oc we drop it since we will only migrate
55
		// the dav_properties values in the postSchemaChange step
56
		$table = $schema->getTable('properties');
57
		if ($table->hasColumn('fileid')) {
58
			$qb = $this->connection->getQueryBuilder();
59
			$qb->delete('properties');
60
			$qb->execute();
61
		}
62
	}
63
64
65
	/**
66
	 * @param IOutput $output
67
	 * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
68
	 * @param array $options
69
	 * @return null|ISchemaWrapper
70
	 * @since 13.0.0
71
	 */
72
	public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
73
		/** @var ISchemaWrapper $schema */
74
		$schema = $schemaClosure();
75
76
		if (!$schema->hasTable('appconfig')) {
77
			$table = $schema->createTable('appconfig');
78
			$table->addColumn('appid', 'string', [
79
				'notnull' => true,
80
				'length' => 32,
81
				'default' => '',
82
			]);
83
			$table->addColumn('configkey', 'string', [
84
				'notnull' => true,
85
				'length' => 64,
86
				'default' => '',
87
			]);
88
			$table->addColumn('configvalue', 'text', [
89
				'notnull' => false,
90
			]);
91
			$table->setPrimaryKey(['appid', 'configkey']);
92
			$table->addIndex(['configkey'], 'appconfig_config_key_index');
93
			$table->addIndex(['appid'], 'appconfig_appid_key');
94
		}
95
96
		if (!$schema->hasTable('storages')) {
97
			$table = $schema->createTable('storages');
98
			$table->addColumn('id', 'string', [
99
				'notnull' => false,
100
				'length' => 64,
101
			]);
102
			$table->addColumn('numeric_id', Types::BIGINT, [
103
				'autoincrement' => true,
104
				'notnull' => true,
105
				'length' => 20,
106
			]);
107
			$table->addColumn('available', 'integer', [
108
				'notnull' => true,
109
				'default' => 1,
110
			]);
111
			$table->addColumn('last_checked', 'integer', [
112
				'notnull' => false,
113
			]);
114
			$table->setPrimaryKey(['numeric_id']);
115
			$table->addUniqueIndex(['id'], 'storages_id_index');
116
		}
117
118
		if (!$schema->hasTable('mounts')) {
119
			$table = $schema->createTable('mounts');
120
			$table->addColumn('id', 'integer', [
121
				'autoincrement' => true,
122
				'notnull' => true,
123
				'length' => 4,
124
			]);
125
			$table->addColumn('storage_id', Types::BIGINT, [
126
				'notnull' => true,
127
				'length' => 20,
128
			]);
129
			$table->addColumn('root_id', Types::BIGINT, [
130
				'notnull' => true,
131
				'length' => 20,
132
			]);
133
			$table->addColumn('user_id', 'string', [
134
				'notnull' => true,
135
				'length' => 64,
136
			]);
137
			$table->addColumn('mount_point', 'string', [
138
				'notnull' => true,
139
				'length' => 4000,
140
			]);
141
			$table->addColumn('mount_id', Types::BIGINT, [
142
				'notnull' => false,
143
				'length' => 20,
144
			]);
145
			$table->setPrimaryKey(['id']);
146
			$table->addIndex(['user_id'], 'mounts_user_index');
147
			$table->addIndex(['storage_id'], 'mounts_storage_index');
148
			$table->addIndex(['root_id'], 'mounts_root_index');
149
			$table->addIndex(['mount_id'], 'mounts_mount_id_index');
150
			$table->addUniqueIndex(['user_id', 'root_id'], 'mounts_user_root_index');
151
		} else {
152
			$table = $schema->getTable('mounts');
153
			$table->addColumn('mount_id', Types::BIGINT, [
154
				'notnull' => false,
155
				'length' => 20,
156
			]);
157
			if (!$table->hasIndex('mounts_mount_id_index')) {
158
				$table->addIndex(['mount_id'], 'mounts_mount_id_index');
159
			}
160
		}
161
162
		if (!$schema->hasTable('mimetypes')) {
163
			$table = $schema->createTable('mimetypes');
164
			$table->addColumn('id', Types::BIGINT, [
165
				'autoincrement' => true,
166
				'notnull' => true,
167
				'length' => 20,
168
			]);
169
			$table->addColumn('mimetype', 'string', [
170
				'notnull' => true,
171
				'length' => 255,
172
				'default' => '',
173
			]);
174
			$table->setPrimaryKey(['id']);
175
			$table->addUniqueIndex(['mimetype'], 'mimetype_id_index');
176
		}
177
178
		if (!$schema->hasTable('filecache')) {
179
			$table = $schema->createTable('filecache');
180
			$table->addColumn('fileid', Types::BIGINT, [
181
				'autoincrement' => true,
182
				'notnull' => true,
183
				'length' => 20,
184
			]);
185
			$table->addColumn('storage', Types::BIGINT, [
186
				'notnull' => true,
187
				'length' => 20,
188
				'default' => 0,
189
			]);
190
			$table->addColumn('path', 'string', [
191
				'notnull' => false,
192
				'length' => 4000,
193
			]);
194
			$table->addColumn('path_hash', 'string', [
195
				'notnull' => true,
196
				'length' => 32,
197
				'default' => '',
198
			]);
199
			$table->addColumn('parent', Types::BIGINT, [
200
				'notnull' => true,
201
				'length' => 20,
202
				'default' => 0,
203
			]);
204
			$table->addColumn('name', 'string', [
205
				'notnull' => false,
206
				'length' => 250,
207
			]);
208
			$table->addColumn('mimetype', Types::BIGINT, [
209
				'notnull' => true,
210
				'length' => 20,
211
				'default' => 0,
212
			]);
213
			$table->addColumn('mimepart', Types::BIGINT, [
214
				'notnull' => true,
215
				'length' => 20,
216
				'default' => 0,
217
			]);
218
			$table->addColumn('size', 'bigint', [
219
				'notnull' => true,
220
				'length' => 8,
221
				'default' => 0,
222
			]);
223
			$table->addColumn('mtime', Types::BIGINT, [
224
				'notnull' => true,
225
				'length' => 20,
226
				'default' => 0,
227
			]);
228
			$table->addColumn('storage_mtime', Types::BIGINT, [
229
				'notnull' => true,
230
				'length' => 20,
231
				'default' => 0,
232
			]);
233
			$table->addColumn('encrypted', 'integer', [
234
				'notnull' => true,
235
				'length' => 4,
236
				'default' => 0,
237
			]);
238
			$table->addColumn('unencrypted_size', 'bigint', [
239
				'notnull' => true,
240
				'length' => 8,
241
				'default' => 0,
242
			]);
243
			$table->addColumn('etag', 'string', [
244
				'notnull' => false,
245
				'length' => 40,
246
			]);
247
			$table->addColumn('permissions', 'integer', [
248
				'notnull' => false,
249
				'length' => 4,
250
				'default' => 0,
251
			]);
252
			$table->addColumn('checksum', 'string', [
253
				'notnull' => false,
254
				'length' => 255,
255
			]);
256
			$table->setPrimaryKey(['fileid']);
257
			$table->addUniqueIndex(['storage', 'path_hash'], 'fs_storage_path_hash');
258
			$table->addIndex(['parent', 'name'], 'fs_parent_name_hash');
259
			$table->addIndex(['storage', 'mimetype'], 'fs_storage_mimetype');
260
			$table->addIndex(['storage', 'mimepart'], 'fs_storage_mimepart');
261
			$table->addIndex(['storage', 'size', 'fileid'], 'fs_storage_size');
262
			$table->addIndex(['mtime'], 'fs_mtime');
263
			$table->addIndex(['size'], 'fs_size');
264
		}
265
266
		if (!$schema->hasTable('group_user')) {
267
			$table = $schema->createTable('group_user');
268
			$table->addColumn('gid', 'string', [
269
				'notnull' => true,
270
				'length' => 64,
271
				'default' => '',
272
			]);
273
			$table->addColumn('uid', 'string', [
274
				'notnull' => true,
275
				'length' => 64,
276
				'default' => '',
277
			]);
278
			$table->setPrimaryKey(['gid', 'uid']);
279
			$table->addIndex(['uid'], 'gu_uid_index');
280
		}
281
282
		if (!$schema->hasTable('group_admin')) {
283
			$table = $schema->createTable('group_admin');
284
			$table->addColumn('gid', 'string', [
285
				'notnull' => true,
286
				'length' => 64,
287
				'default' => '',
288
			]);
289
			$table->addColumn('uid', 'string', [
290
				'notnull' => true,
291
				'length' => 64,
292
				'default' => '',
293
			]);
294
			$table->setPrimaryKey(['gid', 'uid']);
295
			$table->addIndex(['uid'], 'group_admin_uid');
296
		}
297
298
		if (!$schema->hasTable('groups')) {
299
			$table = $schema->createTable('groups');
300
			$table->addColumn('gid', 'string', [
301
				'notnull' => true,
302
				'length' => 64,
303
				'default' => '',
304
			]);
305
			$table->setPrimaryKey(['gid']);
306
		}
307
308
		if (!$schema->hasTable('preferences')) {
309
			$table = $schema->createTable('preferences');
310
			$table->addColumn('userid', 'string', [
311
				'notnull' => true,
312
				'length' => 64,
313
				'default' => '',
314
			]);
315
			$table->addColumn('appid', 'string', [
316
				'notnull' => true,
317
				'length' => 32,
318
				'default' => '',
319
			]);
320
			$table->addColumn('configkey', 'string', [
321
				'notnull' => true,
322
				'length' => 64,
323
				'default' => '',
324
			]);
325
			$table->addColumn('configvalue', 'text', [
326
				'notnull' => false,
327
			]);
328
			$table->setPrimaryKey(['userid', 'appid', 'configkey']);
329
		}
330
331
		if (!$schema->hasTable('properties')) {
332
			$table = $schema->createTable('properties');
333
			$table->addColumn('id', 'integer', [
334
				'autoincrement' => true,
335
				'notnull' => true,
336
				'length' => 4,
337
			]);
338
			$table->addColumn('userid', 'string', [
339
				'notnull' => true,
340
				'length' => 64,
341
				'default' => '',
342
			]);
343
			$table->addColumn('propertypath', 'string', [
344
				'notnull' => true,
345
				'length' => 255,
346
				'default' => '',
347
			]);
348
			$table->addColumn('propertyname', 'string', [
349
				'notnull' => true,
350
				'length' => 255,
351
				'default' => '',
352
			]);
353
			$table->addColumn('propertyvalue', 'text', [
354
				'notnull' => true,
355
			]);
356
			$table->setPrimaryKey(['id']);
357
			$table->addIndex(['userid'], 'property_index');
358
			$table->addIndex(['userid', 'propertypath'], 'properties_path_index');
359
		} else {
360
			$table = $schema->getTable('properties');
361
			if ($table->hasColumn('propertytype')) {
362
				$table->dropColumn('propertytype');
363
			}
364
			if ($table->hasColumn('fileid')) {
365
				$table->dropColumn('fileid');
366
			}
367
			if (!$table->hasColumn('propertypath')) {
368
				$table->addColumn('propertypath', 'string', [
369
					'notnull' => true,
370
					'length' => 255,
371
				]);
372
			}
373
			if (!$table->hasColumn('userid')) {
374
				$table->addColumn('userid', 'string', [
375
					'notnull' => false,
376
					'length' => 64,
377
					'default' => '',
378
				]);
379
			}
380
		}
381
382
		if (!$schema->hasTable('share')) {
383
			$table = $schema->createTable('share');
384
			$table->addColumn('id', 'integer', [
385
				'autoincrement' => true,
386
				'notnull' => true,
387
				'length' => 4,
388
			]);
389
			$table->addColumn('share_type', 'smallint', [
390
				'notnull' => true,
391
				'length' => 1,
392
				'default' => 0,
393
			]);
394
			$table->addColumn('share_with', 'string', [
395
				'notnull' => false,
396
				'length' => 255,
397
			]);
398
			$table->addColumn('password', 'string', [
399
				'notnull' => false,
400
				'length' => 255,
401
			]);
402
			$table->addColumn('uid_owner', 'string', [
403
				'notnull' => true,
404
				'length' => 64,
405
				'default' => '',
406
			]);
407
			$table->addColumn('uid_initiator', 'string', [
408
				'notnull' => false,
409
				'length' => 64,
410
			]);
411
			$table->addColumn('parent', 'integer', [
412
				'notnull' => false,
413
				'length' => 4,
414
			]);
415
			$table->addColumn('item_type', 'string', [
416
				'notnull' => true,
417
				'length' => 64,
418
				'default' => '',
419
			]);
420
			$table->addColumn('item_source', 'string', [
421
				'notnull' => false,
422
				'length' => 255,
423
			]);
424
			$table->addColumn('item_target', 'string', [
425
				'notnull' => false,
426
				'length' => 255,
427
			]);
428
			$table->addColumn('file_source', 'integer', [
429
				'notnull' => false,
430
				'length' => 4,
431
			]);
432
			$table->addColumn('file_target', 'string', [
433
				'notnull' => false,
434
				'length' => 512,
435
			]);
436
			$table->addColumn('permissions', 'smallint', [
437
				'notnull' => true,
438
				'length' => 1,
439
				'default' => 0,
440
			]);
441
			$table->addColumn('stime', 'bigint', [
442
				'notnull' => true,
443
				'length' => 8,
444
				'default' => 0,
445
			]);
446
			$table->addColumn('accepted', 'smallint', [
447
				'notnull' => true,
448
				'length' => 1,
449
				'default' => 0,
450
			]);
451
			$table->addColumn('expiration', 'datetime', [
452
				'notnull' => false,
453
			]);
454
			$table->addColumn('token', 'string', [
455
				'notnull' => false,
456
				'length' => 32,
457
			]);
458
			$table->addColumn('mail_send', 'smallint', [
459
				'notnull' => true,
460
				'length' => 1,
461
				'default' => 0,
462
			]);
463
			$table->addColumn('share_name', 'string', [
464
				'notnull' => false,
465
				'length' => 64,
466
			]);
467
			$table->setPrimaryKey(['id']);
468
			$table->addIndex(['item_type', 'share_type'], 'item_share_type_index');
469
			$table->addIndex(['file_source'], 'file_source_index');
470
			$table->addIndex(['token'], 'token_index');
471
			$table->addIndex(['share_with'], 'share_with_index');
472
			$table->addIndex(['parent'], 'parent_index');
473
			$table->addIndex(['uid_owner'], 'owner_index');
474
			$table->addIndex(['uid_initiator'], 'initiator_index');
475
		} else {
476
			$table = $schema->getTable('share');
477
			if (!$table->hasColumn('password')) {
478
				$table->addColumn('password', 'string', [
479
					'notnull' => false,
480
					'length' => 255,
481
				]);
482
			}
483
		}
484
485
		if (!$schema->hasTable('jobs')) {
486
			$table = $schema->createTable('jobs');
487
			$table->addColumn('id', 'integer', [
488
				'autoincrement' => true,
489
				'notnull' => true,
490
				'length' => 4,
491
				'unsigned' => true,
492
			]);
493
			$table->addColumn('class', 'string', [
494
				'notnull' => true,
495
				'length' => 255,
496
				'default' => '',
497
			]);
498
			$table->addColumn('argument', 'string', [
499
				'notnull' => true,
500
				'length' => 4000,
501
				'default' => '',
502
			]);
503
			$table->addColumn('last_run', 'integer', [
504
				'notnull' => false,
505
				'default' => 0,
506
			]);
507
			$table->addColumn('last_checked', 'integer', [
508
				'notnull' => false,
509
				'default' => 0,
510
			]);
511
			$table->addColumn('reserved_at', 'integer', [
512
				'notnull' => false,
513
				'default' => 0,
514
			]);
515
			$table->addColumn('execution_duration', 'integer', [
516
				'notnull' => true,
517
				'default' => 0,
518
			]);
519
			$table->setPrimaryKey(['id']);
520
			$table->addIndex(['class'], 'job_class_index');
521
		}
522
523
		if (!$schema->hasTable('users')) {
524
			$table = $schema->createTable('users');
525
			$table->addColumn('uid', 'string', [
526
				'notnull' => true,
527
				'length' => 64,
528
				'default' => '',
529
			]);
530
			$table->addColumn('displayname', 'string', [
531
				'notnull' => false,
532
				'length' => 64,
533
			]);
534
			$table->addColumn('password', 'string', [
535
				'notnull' => true,
536
				'length' => 255,
537
				'default' => '',
538
			]);
539
			$table->setPrimaryKey(['uid']);
540
		}
541
542
		if (!$schema->hasTable('authtoken')) {
543
			$table = $schema->createTable('authtoken');
544
			$table->addColumn('id', 'integer', [
545
				'autoincrement' => true,
546
				'notnull' => true,
547
				'length' => 4,
548
				'unsigned' => true,
549
			]);
550
			$table->addColumn('uid', 'string', [
551
				'notnull' => true,
552
				'length' => 64,
553
				'default' => '',
554
			]);
555
			$table->addColumn('login_name', 'string', [
556
				'notnull' => true,
557
				'length' => 64,
558
				'default' => '',
559
			]);
560
			$table->addColumn('password', 'text', [
561
				'notnull' => false,
562
			]);
563
			$table->addColumn('name', 'text', [
564
				'notnull' => true,
565
				'default' => '',
566
			]);
567
			$table->addColumn('token', 'string', [
568
				'notnull' => true,
569
				'length' => 200,
570
				'default' => '',
571
			]);
572
			$table->addColumn('type', 'smallint', [
573
				'notnull' => false,
574
				'length' => 2,
575
				'default' => 0,
576
				'unsigned' => true,
577
			]);
578
			$table->addColumn('remember', 'smallint', [
579
				'notnull' => false,
580
				'length' => 1,
581
				'default' => 0,
582
				'unsigned' => true,
583
			]);
584
			$table->addColumn('last_activity', 'integer', [
585
				'notnull' => false,
586
				'length' => 4,
587
				'default' => 0,
588
				'unsigned' => true,
589
			]);
590
			$table->addColumn('last_check', 'integer', [
591
				'notnull' => false,
592
				'length' => 4,
593
				'default' => 0,
594
				'unsigned' => true,
595
			]);
596
			$table->addColumn('scope', 'text', [
597
				'notnull' => false,
598
			]);
599
			$table->setPrimaryKey(['id']);
600
			$table->addUniqueIndex(['token'], 'authtoken_token_index');
601
			$table->addIndex(['last_activity'], 'authtoken_last_activity_idx');
602
		} else {
603
			$table = $schema->getTable('authtoken');
604
			$table->addColumn('scope', 'text', [
605
				'notnull' => false,
606
			]);
607
		}
608
609
		if (!$schema->hasTable('bruteforce_attempts')) {
610
			$table = $schema->createTable('bruteforce_attempts');
611
			$table->addColumn('id', 'integer', [
612
				'autoincrement' => true,
613
				'notnull' => true,
614
				'length' => 4,
615
				'unsigned' => true,
616
			]);
617
			$table->addColumn('action', 'string', [
618
				'notnull' => true,
619
				'length' => 64,
620
				'default' => '',
621
			]);
622
			$table->addColumn('occurred', 'integer', [
623
				'notnull' => true,
624
				'length' => 4,
625
				'default' => 0,
626
				'unsigned' => true,
627
			]);
628
			$table->addColumn('ip', 'string', [
629
				'notnull' => true,
630
				'length' => 255,
631
				'default' => '',
632
			]);
633
			$table->addColumn('subnet', 'string', [
634
				'notnull' => true,
635
				'length' => 255,
636
				'default' => '',
637
			]);
638
			$table->addColumn('metadata', 'string', [
639
				'notnull' => true,
640
				'length' => 255,
641
				'default' => '',
642
			]);
643
			$table->setPrimaryKey(['id']);
644
			$table->addIndex(['ip'], 'bruteforce_attempts_ip');
645
			$table->addIndex(['subnet'], 'bruteforce_attempts_subnet');
646
		}
647
648
		if (!$schema->hasTable('vcategory')) {
649
			$table = $schema->createTable('vcategory');
650
			$table->addColumn('id', 'integer', [
651
				'autoincrement' => true,
652
				'notnull' => true,
653
				'length' => 4,
654
				'unsigned' => true,
655
			]);
656
			$table->addColumn('uid', 'string', [
657
				'notnull' => true,
658
				'length' => 64,
659
				'default' => '',
660
			]);
661
			$table->addColumn('type', 'string', [
662
				'notnull' => true,
663
				'length' => 64,
664
				'default' => '',
665
			]);
666
			$table->addColumn('category', 'string', [
667
				'notnull' => true,
668
				'length' => 255,
669
				'default' => '',
670
			]);
671
			$table->setPrimaryKey(['id']);
672
			$table->addIndex(['uid'], 'uid_index');
673
			$table->addIndex(['type'], 'type_index');
674
			$table->addIndex(['category'], 'category_index');
675
		}
676
677
		if (!$schema->hasTable('vcategory_to_object')) {
678
			$table = $schema->createTable('vcategory_to_object');
679
			$table->addColumn('objid', 'integer', [
680
				'notnull' => true,
681
				'length' => 4,
682
				'default' => 0,
683
				'unsigned' => true,
684
			]);
685
			$table->addColumn('categoryid', 'integer', [
686
				'notnull' => true,
687
				'length' => 4,
688
				'default' => 0,
689
				'unsigned' => true,
690
			]);
691
			$table->addColumn('type', 'string', [
692
				'notnull' => true,
693
				'length' => 64,
694
				'default' => '',
695
			]);
696
			$table->setPrimaryKey(['categoryid', 'objid', 'type']);
697
			$table->addIndex(['objid', 'type'], 'vcategory_objectd_index');
698
		}
699
700
		if (!$schema->hasTable('systemtag')) {
701
			$table = $schema->createTable('systemtag');
702
			$table->addColumn('id', 'integer', [
703
				'autoincrement' => true,
704
				'notnull' => true,
705
				'length' => 4,
706
				'unsigned' => true,
707
			]);
708
			$table->addColumn('name', 'string', [
709
				'notnull' => true,
710
				'length' => 64,
711
				'default' => '',
712
			]);
713
			$table->addColumn('visibility', 'smallint', [
714
				'notnull' => true,
715
				'length' => 1,
716
				'default' => 1,
717
			]);
718
			$table->addColumn('editable', 'smallint', [
719
				'notnull' => true,
720
				'length' => 1,
721
				'default' => 1,
722
			]);
723
			$table->setPrimaryKey(['id']);
724
			$table->addUniqueIndex(['name', 'visibility', 'editable'], 'tag_ident');
725
		}
726
727
		if (!$schema->hasTable('systemtag_object_mapping')) {
728
			$table = $schema->createTable('systemtag_object_mapping');
729
			$table->addColumn('objectid', 'string', [
730
				'notnull' => true,
731
				'length' => 64,
732
				'default' => '',
733
			]);
734
			$table->addColumn('objecttype', 'string', [
735
				'notnull' => true,
736
				'length' => 64,
737
				'default' => '',
738
			]);
739
			$table->addColumn('systemtagid', 'integer', [
740
				'notnull' => true,
741
				'length' => 4,
742
				'default' => 0,
743
				'unsigned' => true,
744
			]);
745
			$table->setPrimaryKey(['objecttype', 'objectid', 'systemtagid'], 'som_pk');
746
//			$table->addUniqueIndex(['objecttype', 'objectid', 'systemtagid'], 'mapping');
747
		}
748
749
		if (!$schema->hasTable('systemtag_group')) {
750
			$table = $schema->createTable('systemtag_group');
751
			$table->addColumn('systemtagid', 'integer', [
752
				'notnull' => true,
753
				'length' => 4,
754
				'default' => 0,
755
				'unsigned' => true,
756
			]);
757
			$table->addColumn('gid', 'string', [
758
				'notnull' => true,
759
			]);
760
			$table->setPrimaryKey(['gid', 'systemtagid']);
761
		}
762
763
		if (!$schema->hasTable('file_locks')) {
764
			$table = $schema->createTable('file_locks');
765
			$table->addColumn('id', 'integer', [
766
				'autoincrement' => true,
767
				'notnull' => true,
768
				'length' => 4,
769
				'unsigned' => true,
770
			]);
771
			$table->addColumn('lock', 'integer', [
772
				'notnull' => true,
773
				'length' => 4,
774
				'default' => 0,
775
			]);
776
			$table->addColumn('key', 'string', [
777
				'notnull' => true,
778
				'length' => 64,
779
			]);
780
			$table->addColumn('ttl', 'integer', [
781
				'notnull' => true,
782
				'length' => 4,
783
				'default' => -1,
784
			]);
785
			$table->setPrimaryKey(['id']);
786
			$table->addUniqueIndex(['key'], 'lock_key_index');
787
			$table->addIndex(['ttl'], 'lock_ttl_index');
788
		}
789
790
		if (!$schema->hasTable('comments')) {
791
			$table = $schema->createTable('comments');
792
			$table->addColumn('id', 'integer', [
793
				'autoincrement' => true,
794
				'notnull' => true,
795
				'length' => 4,
796
				'unsigned' => true,
797
			]);
798
			$table->addColumn('parent_id', 'integer', [
799
				'notnull' => true,
800
				'length' => 4,
801
				'default' => 0,
802
				'unsigned' => true,
803
			]);
804
			$table->addColumn('topmost_parent_id', 'integer', [
805
				'notnull' => true,
806
				'length' => 4,
807
				'default' => 0,
808
				'unsigned' => true,
809
			]);
810
			$table->addColumn('children_count', 'integer', [
811
				'notnull' => true,
812
				'length' => 4,
813
				'default' => 0,
814
				'unsigned' => true,
815
			]);
816
			$table->addColumn('actor_type', 'string', [
817
				'notnull' => true,
818
				'length' => 64,
819
				'default' => '',
820
			]);
821
			$table->addColumn('actor_id', 'string', [
822
				'notnull' => true,
823
				'length' => 64,
824
				'default' => '',
825
			]);
826
			$table->addColumn('message', 'text', [
827
				'notnull' => false,
828
			]);
829
			$table->addColumn('verb', 'string', [
830
				'notnull' => false,
831
				'length' => 64,
832
			]);
833
			$table->addColumn('creation_timestamp', 'datetime', [
834
				'notnull' => false,
835
			]);
836
			$table->addColumn('latest_child_timestamp', 'datetime', [
837
				'notnull' => false,
838
			]);
839
			$table->addColumn('object_type', 'string', [
840
				'notnull' => true,
841
				'length' => 64,
842
				'default' => '',
843
			]);
844
			$table->addColumn('object_id', 'string', [
845
				'notnull' => true,
846
				'length' => 64,
847
				'default' => '',
848
			]);
849
			$table->addColumn('reference_id', 'string', [
850
				'notnull' => false,
851
				'length' => 64,
852
			]);
853
			$table->setPrimaryKey(['id']);
854
			$table->addIndex(['parent_id'], 'comments_parent_id_index');
855
			$table->addIndex(['topmost_parent_id'], 'comments_topmost_parent_id_idx');
856
			$table->addIndex(['object_type', 'object_id', 'creation_timestamp'], 'comments_object_index');
857
			$table->addIndex(['actor_type', 'actor_id'], 'comments_actor_index');
858
		}
859
860
		if (!$schema->hasTable('comments_read_markers')) {
861
			$table = $schema->createTable('comments_read_markers');
862
			$table->addColumn('user_id', 'string', [
863
				'notnull' => true,
864
				'length' => 64,
865
				'default' => '',
866
			]);
867
			$table->addColumn('marker_datetime', 'datetime', [
868
				'notnull' => false,
869
			]);
870
			$table->addColumn('object_type', 'string', [
871
				'notnull' => true,
872
				'length' => 64,
873
				'default' => '',
874
			]);
875
			$table->addColumn('object_id', 'string', [
876
				'notnull' => true,
877
				'length' => 64,
878
				'default' => '',
879
			]);
880
			$table->addIndex(['object_type', 'object_id'], 'comments_marker_object_index');
881
			$table->setPrimaryKey(['user_id', 'object_type', 'object_id'], 'crm_pk');
882
//			$table->addUniqueIndex(['user_id', 'object_type', 'object_id'], 'comments_marker_index');
883
		}
884
885
//		if (!$schema->hasTable('credentials')) {
886
//			$table = $schema->createTable('credentials');
887
//			$table->addColumn('user', 'string', [
888
//				'notnull' => false,
889
//				'length' => 64,
890
//			]);
891
//			$table->addColumn('identifier', 'string', [
892
//				'notnull' => true,
893
//				'length' => 64,
894
//			]);
895
//			$table->addColumn('credentials', 'text', [
896
//				'notnull' => false,
897
//			]);
898
//			$table->setPrimaryKey(['user', 'identifier']);
899
//			$table->addIndex(['user'], 'credentials_user');
900
//		}
901
902
		if (!$schema->hasTable('admin_sections')) {
903
			$table = $schema->createTable('admin_sections');
904
			$table->addColumn('id', 'string', [
905
				'notnull' => true,
906
				'length' => 64,
907
			]);
908
			$table->addColumn('class', 'string', [
909
				'notnull' => true,
910
				'length' => 255,
911
				'default' => '',
912
			]);
913
			$table->addColumn('priority', 'smallint', [
914
				'notnull' => true,
915
				'length' => 1,
916
				'default' => 0,
917
			]);
918
			$table->setPrimaryKey(['id']);
919
			$table->addUniqueIndex(['class'], 'admin_sections_class');
920
		}
921
922
		if (!$schema->hasTable('admin_settings')) {
923
			$table = $schema->createTable('admin_settings');
924
			$table->addColumn('id', 'integer', [
925
				'autoincrement' => true,
926
				'notnull' => true,
927
				'length' => 4,
928
			]);
929
			$table->addColumn('class', 'string', [
930
				'notnull' => true,
931
				'length' => 255,
932
				'default' => '',
933
			]);
934
			$table->addColumn('section', 'string', [
935
				'notnull' => false,
936
				'length' => 64,
937
			]);
938
			$table->addColumn('priority', 'smallint', [
939
				'notnull' => true,
940
				'length' => 1,
941
				'default' => 0,
942
			]);
943
			$table->setPrimaryKey(['id']);
944
			$table->addUniqueIndex(['class'], 'admin_settings_class');
945
			$table->addIndex(['section'], 'admin_settings_section');
946
		}
947
948
		if (!$schema->hasTable('personal_sections')) {
949
			$table = $schema->createTable('personal_sections');
950
			$table->addColumn('id', 'string', [
951
				'notnull' => true,
952
				'length' => 64,
953
			]);
954
			$table->addColumn('class', 'string', [
955
				'notnull' => true,
956
				'length' => 255,
957
				'default' => '',
958
			]);
959
			$table->addColumn('priority', 'smallint', [
960
				'notnull' => true,
961
				'length' => 1,
962
				'default' => 0,
963
			]);
964
			$table->setPrimaryKey(['id']);
965
			$table->addUniqueIndex(['class'], 'personal_sections_class');
966
		}
967
968
		if (!$schema->hasTable('personal_settings')) {
969
			$table = $schema->createTable('personal_settings');
970
			$table->addColumn('id', 'integer', [
971
				'autoincrement' => true,
972
				'notnull' => true,
973
				'length' => 4,
974
			]);
975
			$table->addColumn('class', 'string', [
976
				'notnull' => true,
977
				'length' => 255,
978
				'default' => '',
979
			]);
980
			$table->addColumn('section', 'string', [
981
				'notnull' => false,
982
				'length' => 64,
983
			]);
984
			$table->addColumn('priority', 'smallint', [
985
				'notnull' => true,
986
				'length' => 1,
987
				'default' => 0,
988
			]);
989
			$table->setPrimaryKey(['id']);
990
			$table->addUniqueIndex(['class'], 'personal_settings_class');
991
			$table->addIndex(['section'], 'personal_settings_section');
992
		}
993
994
		if (!$schema->hasTable('accounts')) {
995
			$table = $schema->createTable('accounts');
996
			$table->addColumn('uid', 'string', [
997
				'notnull' => true,
998
				'length' => 64,
999
				'default' => '',
1000
			]);
1001
			$table->addColumn('data', 'text', [
1002
				'notnull' => true,
1003
				'default' => '',
1004
			]);
1005
			$table->setPrimaryKey(['uid']);
1006
		}
1007
		return $schema;
1008
	}
1009
1010
	public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
1011
		/** @var ISchemaWrapper $schema */
1012
		$schema = $schemaClosure();
1013
		if (!$schema->hasTable('dav_properties')) {
1014
			return;
1015
		}
1016
		$query = $this->connection->getQueryBuilder();
1017
		$query->select('*')
1018
			->from('dav_properties');
1019
1020
		$insert = $this->connection->getQueryBuilder();
1021
		$insert->insert('properties')
1022
			->setValue('propertypath', $insert->createParameter('propertypath'))
1023
			->setValue('propertyname', $insert->createParameter('propertyname'))
1024
			->setValue('propertyvalue', $insert->createParameter('propertyvalue'))
1025
			->setValue('userid', $insert->createParameter('userid'));
1026
1027
		$result = $query->execute();
1028
		while ($row = $result->fetch()) {
1029
			preg_match('/(calendar)\/([A-z0-9-@_]+)\//', $row['propertypath'], $match);
1030
			$insert->setParameter('propertypath', (string) $row['propertypath'])
1031
				->setParameter('propertyname', (string) $row['propertyname'])
1032
				->setParameter('propertyvalue', (string) $row['propertyvalue'])
1033
				->setParameter('userid', (string) ($match[2] ?? ''));
1034
			$insert->execute();
1035
		}
1036
	}
1037
}
1038