-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathTableManager.php
More file actions
837 lines (706 loc) · 24.6 KB
/
TableManager.php
File metadata and controls
837 lines (706 loc) · 24.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace OCA\Polls\Db\V7;
use Doctrine\DBAL\Types\Type;
use Exception;
use OCA\Polls\AppInfo\Application;
use OCA\Polls\Db\OptionMapper;
use OCA\Polls\Db\Poll;
use OCA\Polls\Db\PollGroup;
use OCA\Polls\Db\PollMapper;
use OCA\Polls\Db\Share;
use OCA\Polls\Db\VoteMapper;
use OCA\Polls\Db\Watch;
use OCA\Polls\Exceptions\PreconditionException;
use OCA\Polls\Helper\Hash;
use OCA\Polls\Migration\V7\TableSchema;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use PDO;
use Psr\Log\LoggerInterface;
class TableManager extends DbManager {
/** @psalm-suppress PossiblyUnusedMethod */
public function __construct(
protected IConfig $config,
protected IDBConnection $connection,
protected LoggerInterface $logger,
private OptionMapper $optionMapper,
private VoteMapper $voteMapper,
) {
parent::__construct($config, $connection, $logger);
}
/**
* Purge all tables and all data
*
* @return string[] Messages as array
*/
public function purgeTables(): array {
$messages = [];
$droppedTables = [];
// First drop all tables that have foreign key constraints
foreach (TableSchema::FK_INDICES as $parent => $child) {
// drop all child tables referencing the parent table
foreach (array_keys($child) as $table) {
if ($this->connection->tableExists($table)) {
$this->connection->dropTable($table);
$droppedTables[] = $this->dbPrefix . $table;
$messages[] = 'Dropped ' . $this->dbPrefix . $table;
}
}
// drop the parent table
if ($this->connection->tableExists($parent)) {
$this->connection->dropTable($parent);
$droppedTables[] = $this->dbPrefix . $parent;
$messages[] = 'Dropped ' . $this->dbPrefix . $parent;
}
}
// Then if there are any tables left, drop them
foreach (array_keys(TableSchema::TABLES) as $tableName) {
if ($this->connection->tableExists($tableName)) {
$this->connection->dropTable($tableName);
$droppedTables[] = $this->dbPrefix . $tableName;
$messages[] = 'Dropped ' . $this->dbPrefix . $tableName;
}
}
if ($droppedTables) {
$this->logger->info('Dropped tables', $droppedTables);
}
// delete all migration records
// ATTENTION: This is more or less an illegal access
// to the migrations table which belong to the core
$query = $this->connection->getQueryBuilder();
$query->delete('migrations')
->where('app = :appName')
->setParameter('appName', Application::APP_ID)
->executeStatement();
$this->logger->info('Removed all migration records from {dbPrefix}migrations', ['dbPrefix' => $this->dbPrefix]);
$messages[] = 'Removed all migration records from ' . $this->dbPrefix . 'migrations';
// delete all app configs
// ATTENTION: This is more or less an illegal access
// to the migrations table which belong to the core
$query->delete('appconfig')
->where('appid = :appid')
->setParameter('appid', Application::APP_ID)
->executeStatement();
$this->logger->info('Removed all app config records from {dbPrefix}appconfig', ['dbPrefix' => $this->dbPrefix]);
$messages[] = 'Removed all app config records from ' . $this->dbPrefix . 'appconfig';
$messages[] = 'Done.';
$messages[] = '';
$messages[] = 'Please call \'occ app:remove polls\' now!';
return $messages;
}
/**
* Remove the watch table if it exists
* Used as shorthand to reset the primary key autoincrement value
*
* @return string[] Messages as array
*/
public function removeWatch(): array {
$messages = [];
$tableName = $this->dbPrefix . Watch::TABLE;
if ($this->connection->tableExists(Watch::TABLE)) {
$this->connection->dropTable(Watch::TABLE);
$messages[] = 'Dropped ' . $tableName;
}
return $messages;
}
/**
* Create or update a table defined in TableSchema::TABLES
*
* @return string[] Messages as array
*/
public function createTable(string $tableName): array {
$this->needsSchema();
$messages = [];
$columns = TableSchema::TABLES[$tableName];
// Ensure the table name is prefixed correctly
$tableName = $this->getTableName($tableName);
if ($this->schema->hasTable($tableName)) {
$table = $this->schema->getTable($tableName);
$messages[] = 'Validating table ' . $table->getName();
$tableCreated = false;
} else {
$table = $this->schema->createTable($tableName);
$tableCreated = true;
$messages[] = 'Creating table ' . $table->getName();
}
foreach ($columns as $columnName => $columnDefinition) {
if ($table->hasColumn($columnName)) {
$column = $table->getColumn($columnName);
if (Type::lookupName($column->getType()) !== $columnDefinition['type']) {
$messages[] = 'Migrated type of ' . $table->getName() . '[\'' . $columnName . '\'] from ' . Type::lookupName($column->getType()) . ' to ' . $columnDefinition['type'];
$column->setType(Type::getType($columnDefinition['type']));
}
$column->setOptions($columnDefinition['options']);
// force change to current options definition
$table->modifyColumn($columnName, $columnDefinition['options']);
} else {
$table->addColumn($columnName, $columnDefinition['type'], $columnDefinition['options']);
$messages[] = "Added {$table->getName()}, {$columnName} ({$columnDefinition['type']})";
}
}
if ($tableCreated) {
$table->setPrimaryKey(['id']);
}
return $messages;
}
/**
* Create all tables defined in TableSchema::TABLES
*
* @return string[] Messages as array
*/
public function createTables(): array {
$this->needsSchema();
$messages = [];
foreach (array_keys(TableSchema::TABLES) as $tableName) {
$messages = array_merge($messages, $this->createTable($tableName));
}
return $messages;
}
/**
* Remove obsolete tables if they still exist
*
* @return string[] Messages as array
*/
public function removeObsoleteTables(): array {
$dropped = false;
$messages = [];
foreach (TableSchema::GONE_TABLES as $tableName) {
if ($this->connection->tableExists($tableName)) {
$dropped = true;
$this->connection->dropTable($tableName);
$messages[] = 'Dropped ' . $this->dbPrefix . $tableName;
}
}
if (!$dropped) {
$messages[] = 'No orphaned tables found';
}
return $messages;
}
/**
* Remove obsolete columns if they still exist
*
* @return string[] Messages as array
*/
public function removeObsoleteColumns(): array {
$messages = [];
$dropped = false;
foreach (TableSchema::GONE_COLUMNS as $tableName => $columns) {
$prefixedTableName = $this->dbPrefix . $tableName;
if (!$this->schema->hasTable($prefixedTableName)) {
continue;
}
$table = $this->schema->getTable($prefixedTableName);
foreach ($columns as $columnName) {
if ($table->hasColumn($columnName)) {
$dropped = true;
$table->dropColumn($columnName);
$messages[] = 'Dropped ' . $columnName . ' from ' . $prefixedTableName;
}
}
}
if (!$dropped) {
$messages[] = 'No orphaned columns found';
}
return $messages;
}
/**
* delete all orphaned entries by selecting all rows
* those poll_ids are not present in the polls table
*
* Because we allowed nullish poll_ids between version 8.0.0 and 8.1.0,
* we also delete all entries with a nullish poll_id.
*
* This method is used to clean up orphaned entries in the database and
* is used by the occ command `occ polls:db:rebuild and while updating
*
* @return string[] Messages as array
*/
public function removeOrphaned(): array {
$orphanedCount = [];
// collects all pollIds
$subqueryPolls = $this->connection->getQueryBuilder();
$subqueryPolls->selectDistinct('id')->from(Poll::TABLE);
// collects all groupIds
$subqueryGroups = $this->connection->getQueryBuilder();
$subqueryGroups->selectDistinct('id')->from(PollGroup::TABLE);
// delete all orphaned entries without a corresponding poll (poll_id is NULL or not in the polls table)
foreach (TableSchema::FK_INDICES as $children) {
foreach (array_keys($children) as $tableName) {
$query = $this->connection->getQueryBuilder();
$query->delete($tableName)
->where(
$query->expr()->orX(
$query->expr()->notIn('poll_id', $query->createFunction($subqueryPolls->getSQL()), IQueryBuilder::PARAM_INT_ARRAY),
$query->expr()->isNull('poll_id')
)
);
$executed = $query->executeStatement();
if (isset($orphanedCount[$tableName])) {
$orphanedCount[$tableName] += $executed;
} else {
$orphanedCount[$tableName] = $executed;
}
}
}
// delete all orphaned shares without corresponding poll group and poll (group_id and poll_id are NULL or not in the polls or poll groups table)
$query = $this->connection->getQueryBuilder();
$query->delete(Share::TABLE)
->where(
$query->expr()->orX(
$query->expr()->notIn('poll_id', $query->createFunction($subqueryPolls->getSQL()), IQueryBuilder::PARAM_INT_ARRAY),
$query->expr()->isNull('poll_id')
)
);
$query->andWhere(
$query->expr()->orX(
$query->expr()->notIn('group_id', $query->createFunction($subqueryGroups->getSQL()), IQueryBuilder::PARAM_INT_ARRAY),
$query->expr()->isNull('group_id')
)
);
$orphanedCount[Share::TABLE] = $query->executeStatement();
// delete all orphaned entries from the poll-group-relation (group_id or poll_id are NULL or not in the polls or poll groups table)
$query = $this->connection->getQueryBuilder();
$query->delete(PollGroup::RELATION_TABLE)
->where(
$query->expr()->orX(
$query->expr()->notIn('poll_id', $query->createFunction($subqueryPolls->getSQL()), IQueryBuilder::PARAM_INT_ARRAY),
$query->expr()->isNull('poll_id')
)
);
$query->orWhere(
$query->expr()->orX(
$query->expr()->notIn('group_id', $query->createFunction($subqueryGroups->getSQL()), IQueryBuilder::PARAM_INT_ARRAY),
$query->expr()->isNull('group_id')
)
);
$orphanedCount[PollGroup::RELATION_TABLE] = $query->executeStatement();
// finally delete all polls with id === null
$query = $this->connection->getQueryBuilder();
$query->delete(Poll::TABLE)
->where($query->expr()->isNull('id'));
$orphanedCount[Poll::TABLE] = $query->executeStatement();
$messages = [];
foreach ($orphanedCount as $tableName => $count) {
if ($count > 0) {
$this->logger->info(
'Removed {count} orphaned record(s) from {tableName}',
['count' => $count, 'tableName' => $this->dbPrefix . $tableName]
);
$messages[] = 'Removed ' . $count . ' orphaned record(s) from ' . $this->dbPrefix . $tableName;
} else {
$messages[] = 'No orphaned records found in ' . $this->dbPrefix . $tableName;
}
}
return $messages;
}
/**
* Delete all duplicate entries in all tables based on the unique indices defined in TableSchema::UNIQUE_INDICES
*
* @return string[] Messages as array
*/
public function deleteAllDuplicates(?IOutput $output = null): array {
$messages = [];
foreach (TableSchema::UNIQUE_INDICES as $tableName => $uniqueIndices) {
foreach ($uniqueIndices as $definition) {
// delete all duplicates based on the unique index definition
$count = $this->deleteDuplicates($tableName, $definition['columns']);
if ($count) {
$messages[] = 'Removed ' . $count . ' duplicate records from ' . $this->dbPrefix . $tableName;
$this->logger->info(end($messages));
}
if ($output && $count) {
$output->info(end($messages));
}
}
}
return $messages;
}
/**
* Delete entries per timestamp
*
* @return string Message
*/
public function tidyWatchTable(int $offset): string {
$query = $this->connection->getQueryBuilder();
$query->delete(Watch::TABLE)
->where(
$query->expr()->lt('updated', $query->createNamedParameter($offset))
);
$count = $query->executeStatement();
if ($count > 0) {
$this->logger->info('Removed {number} old watch records', ['number' => $count, 'db' => $this->dbPrefix . Watch::TABLE]);
return 'Removed ' . $count . ' old watch records';
}
$this->logger->info('Watch table is clean');
return 'Watch table is clean';
}
/**
* Delete duplicate entries in $table based on $columns
* Keep the entry with the lowest id
*
* @param string $table
* @param array $columns
* @return int number of deleted entries
*/
private function deleteDuplicates(string $table, array $columns):int {
$this->needsSchema();
if (!$this->schema->hasTable($this->dbPrefix . $table)) {
return 0;
}
$qb = $this->connection->getQueryBuilder();
// identify duplicates
$selection = $qb->selectDistinct('t1.id')
->from($table, 't1')
->innerJoin('t1', $table, 't2', $qb->expr()->lt('t1.id', 't2.id'));
$i = 0;
foreach ($columns as $column) {
if (!$this->schema->getTable($this->dbPrefix . $table)->hasColumn($column)) {
$this->logger->warning('Column {column} does not exist in table {table} - cannot check for duplicates based on this column', ['column' => $column, 'table' => $this->dbPrefix . $table]);
return 0;
}
if ($i > 0) {
$selection->andWhere($qb->expr()->eq('t1.' . $column, 't2.' . $column));
} else {
$selection->where($qb->expr()->eq('t1.' . $column, 't2.' . $column));
}
$i++;
}
$duplicates = $qb->executeQuery()->fetchAll(PDO::FETCH_COLUMN);
$this->connection->getQueryBuilder()
->delete($table)
->where('id in (:ids)')
->setParameter('ids', $duplicates, IQueryBuilder::PARAM_INT_ARRAY)
->executeStatement();
return count($duplicates);
}
/**
* Tidy migrations table and remove obsolete migration entries.
*
* @return string[] Messages as array
*/
public function removeObsoleteMigrations(): array {
$messages = [];
$query = $this->connection->getQueryBuilder();
$messages[] = 'tidy migration entries';
foreach (TableSchema::GONE_MIGRATIONS as $version) {
$query->delete('migrations')
->where('app = :appName')
->andWhere('version = :version')
->setParameter('appName', Application::APP_ID)
->setParameter('version', $version)
->executeStatement();
}
return $messages;
}
/**
* Fix all votes with option text not matching the option text in the options table
* Precondition: The options table has to have a duration column
* This method is used to fix votes which were cast while the option text was changing
* because of a duration change.
*
* This method is used by the occ command `occ polls:db:rebuild` and while updating
*/
public function fixVotes(): void {
if (!$this->schema->hasTable($this->dbPrefix . OptionMapper::TABLE)) {
return;
}
$table = $this->schema->getTable($this->dbPrefix . OptionMapper::TABLE);
if (!$table->hasColumn('duration')) {
return;
}
$foundOptions = $this->optionMapper->findOptionsWithDuration();
foreach ($foundOptions as $option) {
$this->voteMapper->fixVoteOptionText(
$option->getPollId(),
$option->getId(),
$option->getPollOptionTextStart(),
$option->getPollOptionText(),
);
}
}
/**
* Fix all shares with nullish group_id or poll_id
* Precondition have to be checked before
*
* @return string[] Messages as array
*/
public function fixNullishShares(): array {
$messages = [];
try {
$tableName = Share::TABLE;
$affectedColumns = ['group_id', 'poll_id'];
$this->checkPrecondition($tableName, $affectedColumns);
// set all nullish group_id and poll_id to 0
foreach ($affectedColumns as $affectedColumn) {
$count = $this->migrateNullishColumnToZero($tableName, $affectedColumn);
if ($count > 0) {
$messages[] = 'Updated ' . $count . ' shares with nullish ' . $affectedColumn . ' to 0';
}
}
} catch (PreconditionException $e) {
$messages[] = $e->getMessage() . ' - aborted fix nullish shares';
return $messages;
}
if (empty($messages)) {
$messages[] = 'All shares are valid';
}
return $messages;
}
/**
* Fix all poll group relations with nullish group_id or poll_id
* Precondition have to be checked before
*
* @return string[] Messages as array
*/
public function fixNullishPollGroupRelations(): array {
$messages = [];
try {
$tableName = PollGroup::RELATION_TABLE;
$affectedColumns = ['group_id', 'poll_id'];
$this->checkPrecondition($tableName, $affectedColumns);
$countAll = 0;
// set all nullish group_id and poll_id to 0
foreach ($affectedColumns as $affectedColumn) {
$updateCount = $this->migrateNullishColumnToZero($tableName, $affectedColumn);
if ($updateCount > 0) {
$countAll += $updateCount;
$messages[] = 'Updated ' . $updateCount . ' pollgroup relations and set ' . $affectedColumn . ' to 0 for nullish values';
}
}
} catch (PreconditionException $e) {
$messages[] = $e->getMessage() . ' - aborted fix nullish poll group relations';
return $messages;
}
if ($countAll === 0) {
$messages[] = 'All poll group relations are valid';
}
return $messages;
}
/**
* Migrate all nullish values in $columnName of $tableName to 0
*
* @param string $tableName Unprefixed tablename
* @param string $columnName Column name to update
*
* @return int number of updated entries
*/
private function migrateNullishColumnToZero(string $tableName, string $columnName): int {
$query = $this->connection->getQueryBuilder();
$query->update($tableName)
->set($columnName, $query->createNamedParameter(0, IQueryBuilder::PARAM_INT))
->where($query->expr()->isNull($columnName));
$count = $query->executeStatement();
return $count;
}
/**
* Set last interaction to current timestamp for all polls
* where last interaction is 0
*
* @param int|null $timestamp
* @return string
*/
public function setLastInteraction(?int $timestamp = null): string {
$timestamp = $timestamp ?? time();
$query = $this->connection->getQueryBuilder();
$query->update(Poll::TABLE)
->set('last_interaction', $query->createNamedParameter($timestamp))
->where($query->expr()->eq('last_interaction', $query->expr()->literal(0, IQueryBuilder::PARAM_INT)));
$count = $query->executeStatement();
if ($count > 0) {
$this->logger->info('Updated {number} polls in {db} and set last_interaction to current timestamp {timestamp}', ['number' => $count, 'db' => $this->dbPrefix . PollMapper::TABLE, 'timestamp' => $timestamp]);
return 'Updated last interaction in ' . $count . ' polls';
}
$this->logger->info('No polls needed to get updated with last interaction info');
return 'Last interaction all set';
}
/**
* Update all option and vote hashes
* Ensures the preconditions are met
*
* @return string[] Messages as array
*/
public function updateHashes(): array {
// Do not catch any exceptions but let any operation break to ensure hash updates can be performed
// Otherwise data loss of votes can occur
$this->checkPrecondition(OptionMapper::TABLE, ['poll_id', 'poll_option_text', 'poll_option_hash']);
$this->checkPrecondition(VoteMapper::TABLE, ['poll_id', 'vote_option_text', 'vote_option_hash']);
$messages = $this->updateOptionHashes();
$messages = array_merge($messages, $this->updateVoteHashes());
return $messages;
}
/**
* Update all vote hashes
* Precondition have to be checked before
*
* @return string[] Messages as array
*/
private function updateVoteHashes(): array {
$messages = [];
$tableName = VoteMapper::TABLE;
$prefixedTableName = $this->dbPrefix . $tableName;
$count = 0;
$updated = 0;
foreach ($this->voteMapper->getAll(includeNull: true) as $vote) {
try {
// if the hash of the vote differs from calculated hash update the vote hash
if ($vote->getVoteOptionHashInDB() !== Hash::getOptionHash($vote->getPollId(), $vote->getVoteOptionText())) {
$messages[] = 'update voteId ' . $vote->getId()
. ' hash: ' . $vote->getVoteOptionHashInDB()
. ':' . Hash::getOptionHash($vote->getPollId(), $vote->getVoteOptionText())
. '\'' . $vote->getVoteOptionText() . '\'';
$vote->setVoteOptionHash(Hash::getOptionHash($vote->getPollId(), $vote->getVoteOptionText()));
$vote = $this->voteMapper->update($vote);
$updated++;
}
$count++;
} catch (Exception $e) {
$messages[] = 'Skip hash update - Error updating option hash for voteId ' . $vote->getId();
$this->logger->error('Error updating option hash for voteId {id}', [
'id' => $vote->getId(),
'message' => $e->getMessage()
]);
}
}
if ($updated === 0) {
$this->logger->info('Verified {count} vote hashes in {db}', [
'count' => $count,
'db' => $prefixedTableName
]);
$messages[] = 'No vote hashes to update';
} else {
$this->logger->info('Updated {updated} hashes of {count} votes in {db}', [
'updated' => $updated,
'count' => $count,
'db' => $prefixedTableName
]);
$messages[] = 'Updated ' . $updated . ' vote hashes';
}
return $messages;
}
/**
* Update all option hashes
* Precondition have to be checked before
*
* @return string[] Messages as array
*/
private function updateOptionHashes(): array {
$messages = [];
$tableName = OptionMapper::TABLE;
$prefixedTableName = $this->dbPrefix . $tableName;
$count = 0;
$updated = 0;
foreach ($this->optionMapper->getAll(includeNull: true) as $option) {
try {
// if the option's hash differs from $actualHash update the option
if ($option->getPollOptionHashInDB() !== Hash::getOptionHash($option->getPollId(), $option->getPollOptionText())) {
$messages[] = 'update optionId ' . $option->getId()
. ' hash: ' . $option->getPollOptionHashInDB()
. ':' . Hash::getOptionHash($option->getPollId(), $option->getPollOptionText())
. '\'' . $option->getPollOptionText() . '\'';
$option->setText($option->getPollOptionText());
$option = $this->optionMapper->update($option);
$updated++;
}
$count++;
} catch (Exception $e) {
$messages[] = 'Skip hash update - Error updating option hash for optionId ' . $option->getId();
$this->logger->error('Error updating option hash for optionId {id}', ['id' => $option->getId(), 'message' => $e->getMessage()]);
}
}
if ($updated === 0) {
$this->logger->info('Verified {count} option hashes in {db}', [
'count' => $count,
'db' => $prefixedTableName
]);
$messages[] = 'No option hashes to update';
} else {
$this->logger->info('Updated {updated} hashes of {count} options in {db}', [
'updated' => $updated,
'count' => $count,
'db' => $prefixedTableName
]);
$messages[] = 'Updated ' . $updated . ' option hashes';
}
return $messages;
}
/**
* Migrate all share labels to display_name
*
* @return string[] Messages as array
*
*/
public function migrateShareLabels(): array {
$messages = [];
$tableName = Share::TABLE;
$affectedColumn = 'label';
try {
$this->checkPrecondition($tableName, $affectedColumn);
} catch (PreconditionException $e) {
$messages[] = $e->getMessage() . ' - aborted migrating labels';
return $messages;
}
$prefixedTableName = $this->dbPrefix . $tableName;
$qb = $this->connection->getQueryBuilder();
$qb->update($tableName)
->set('display_name', $affectedColumn)
->andWhere($qb->expr()->isNotNull($prefixedTableName . '.' . $affectedColumn))
->andWhere($qb->expr()->eq($prefixedTableName . '.' . $affectedColumn, $qb->expr()->literal('')));
$updated = $qb->executeStatement();
if ($updated === 0) {
$this->logger->info('Verified all share labels in {db}', [
'db' => $prefixedTableName
]);
$messages[] = 'No share labels to update';
} else {
$this->logger->info('Updated {updated} share labels in {db}', [
'updated' => $updated,
'db' => $prefixedTableName
]);
$messages[] = 'Updated ' . $updated . ' labels';
}
return $messages;
}
/**
* Migrate all polls with access 'public' to access 'open'
*
* @return string[] Messages as array
*
*/
public function migratePublicToOpen(): array {
$messages = [];
$tableName = Poll::TABLE;
$affectedColumn = 'access';
$prefixedTableName = $this->dbPrefix . $tableName;
try {
$this->checkPrecondition($tableName, $affectedColumn);
} catch (PreconditionException $e) {
$messages[] = $e->getMessage() . ' - aborted migrating public to open';
return $messages;
}
$qb = $this->connection->getQueryBuilder();
$qb->update($tableName)
->set('access', $qb->expr()->literal(Poll::ACCESS_OPEN))
->where($qb->expr()->eq($prefixedTableName . '.' . $affectedColumn, $qb->expr()->literal('public')));
$updated = $qb->executeStatement();
if ($updated === 0) {
$this->logger->info('Verified poll access to be \'open\' instead of \'public\' in {db}', [
'db' => $prefixedTableName
]);
$messages[] = 'No poll access values to update';
} else {
$this->logger->info('Updated {updated} access values in {db}', [
'updated' => $updated,
'db' => $prefixedTableName
]);
$messages[] = 'Updated ' . $updated . ' poll access value';
}
return $messages;
}
}