Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/Command/Db/CleanMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Command\Command;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Db/CreateIndices.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Command\Command;
use OCA\Polls\Db\V6\IndexManager;
use OCA\Polls\Db\V7\IndexManager;
use OCP\IDBConnection;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Db/FixDB.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Command\Command;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Db/Purge.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace OCA\Polls\Command\Db;

use OCA\Polls\Command\Command;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;

/**
Expand Down
4 changes: 2 additions & 2 deletions lib/Command/Db/Rebuild.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
namespace OCA\Polls\Command\Db;

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V6\IndexManager;
use OCA\Polls\Db\V7\TableManager;
use OCA\Polls\Db\V7\IndexManager;
use OCA\Polls\Command\Command;
use OCP\IDBConnection;

Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Db/RemoveFKConstraints.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Command\Command;
use OCA\Polls\Db\V6\IndexManager;
use OCA\Polls\Db\V7\IndexManager;
use OCP\IDBConnection;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Db/RemoveOptionalIndices.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Command\Command;
use OCA\Polls\Db\V6\IndexManager;
use OCA\Polls\Db\V7\IndexManager;
use OCP\IDBConnection;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Command/Db/RemoveUniqueIndices.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Command\Command;
use OCA\Polls\Db\V6\IndexManager;
use OCA\Polls\Db\V7\IndexManager;
use OCP\IDBConnection;

/**
Expand Down
6 changes: 3 additions & 3 deletions lib/Command/Db/ResetWatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Command\Command;
use OCA\Polls\Db\V6\IndexManager;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\IndexManager;
use OCA\Polls\Db\V7\TableManager;
use OCA\Polls\Db\Watch;
use OCA\Polls\Migration\V6\TableSchema;
use OCA\Polls\Migration\V7\TableSchema;
use OCP\IDBConnection;

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/Cron/JanitorCron.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use OCA\Polls\Db\OptionMapper;
use OCA\Polls\Db\PollMapper;
use OCA\Polls\Db\ShareMapper;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCA\Polls\Db\VoteMapper;
use OCA\Polls\Model\Settings\AppSettings;
use OCP\AppFramework\Utility\ITimeFactory;
Expand Down
2 changes: 1 addition & 1 deletion lib/Db/V6/DbManager.php → lib/Db/V7/DbManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Polls\Db\V6;
namespace OCA\Polls\Db\V7;

use Doctrine\DBAL\Schema\Schema;
use Exception;
Expand Down
55 changes: 52 additions & 3 deletions lib/Db/V6/IndexManager.php → lib/Db/V7/IndexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Polls\Db\V6;
namespace OCA\Polls\Db\V7;

use Doctrine\DBAL\Schema\Exception\IndexDoesNotExist;
use Exception;
use OCA\Polls\Migration\V6\TableSchema;
use OCA\Polls\Migration\V7\TableSchema;
use OCP\IConfig;
use OCP\IDBConnection;
use Psr\Log\LoggerInterface;
Expand All @@ -27,6 +27,12 @@ public function __construct(
parent::__construct($config, $connection, $logger);
}

private function columnsMatch(array $existing, array $expected): bool {
sort($existing);
sort($expected);
return $existing === $expected;
}

/**
* Create unique indices
* Unique indices are crucial for the correct operation of the polls app.
Expand All @@ -36,10 +42,44 @@ public function __construct(
*/
public function createUniqueIndices(): array {
$messages = [];
$this->needsSchema();

foreach (TableSchema::UNIQUE_INDICES as $tableName => $uniqueIndices) {
$prefixedTable = $this->getTableName($tableName);

if (!$this->schema->hasTable($prefixedTable)) {
continue;
}

$table = $this->schema->getTable($prefixedTable);

foreach ($uniqueIndices as $name => $definition) {
$messages[] = $this->createIndex($tableName, $name, $definition['columns'], true);
$targetColumns = $definition['columns'];
$existsByName = $table->hasIndex($name);
$definitionMatches = $existsByName && $this->columnsMatch($table->getIndex($name)->getColumns(), $targetColumns);

// 1+2: correct name, correct columns → skip
if ($existsByName && $definitionMatches) {
$messages[] = 'Unique index ' . $name . ' already exists in ' . $tableName;
continue;
}

// 1 true, 2 false: correct name, wrong columns → drop and recreate
if ($existsByName) {
$table->dropIndex($name);
$messages[] = 'Dropped unique index ' . $name . ' from ' . $tableName . ' (definition mismatch)';
} else {
// 1 false, 3: different name, same columns → drop and recreate with correct name
foreach ($table->getIndexes() as $index) {
if ($index->isUnique() && $this->columnsMatch($index->getColumns(), $targetColumns)) {
$messages[] = 'Dropped unique index ' . $index->getName() . ' from ' . $tableName . ' (renamed to ' . $name . ')';
$table->dropIndex($index->getName());
break;
}
}
}

$messages[] = $this->createIndex($tableName, $name, $targetColumns, true);
}
}
return $messages;
Expand Down Expand Up @@ -98,6 +138,15 @@ public function createForeignKeyConstraint(string $parentTableName, string $chil
$parentTable = $this->schema->getTable($parentTableName);
$childTable = $this->schema->getTable($childTableName);

foreach ($childTable->getForeignKeys() as $fk) {
if ($fk->getForeignTableName() === $parentTableName
&& $fk->getLocalColumns() === [$constraintColumn]
&& $fk->getForeignColumns() === ['id']
) {
return 'Foreign key ' . $childTableName . '[' . $constraintColumn . '] -> ' . $parentTableName . '[id] already exists';
}
}

$childTable->addForeignKeyConstraint($parentTable, [$constraintColumn], ['id'], ['onDelete' => 'CASCADE']);
return 'Added ' . $parentTableName . '[' . $constraintColumn . '] <- ' . $childTableName . '[id]';
}
Expand Down
9 changes: 7 additions & 2 deletions lib/Db/V6/TableManager.php → lib/Db/V7/TableManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Polls\Db\V6;
namespace OCA\Polls\Db\V7;

use Doctrine\DBAL\Types\Type;
use Exception;
Expand All @@ -20,7 +20,7 @@
use OCA\Polls\Db\Watch;
use OCA\Polls\Exceptions\PreconditionException;
use OCA\Polls\Helper\Hash;
use OCA\Polls\Migration\V6\TableSchema;
use OCA\Polls\Migration\V7\TableSchema;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
Expand Down Expand Up @@ -416,6 +416,11 @@ private function deleteDuplicates(string $table, array $columns):int {
$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 {
Expand Down
2 changes: 1 addition & 1 deletion lib/Listener/AddMissingIndicesListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace OCA\Polls\Listener;

use OCA\Polls\Migration\V6\TableSchema;
use OCA\Polls\Migration\V7\TableSchema;
use OCP\DB\Events\AddMissingIndicesEvent;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\IEventListener;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/FixVotes.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace OCA\Polls\Migration;

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/CleanTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use Doctrine\DBAL\Schema\Schema;
use Exception;
use OCA\Polls\Db\Poll;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/CreateIndices.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Db\Share;
use OCA\Polls\Db\V6\IndexManager;
use OCA\Polls\Db\V7\IndexManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/CreateTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace OCA\Polls\Migration\RepairSteps;

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/DropOrphanedColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace OCA\Polls\Migration\RepairSteps;

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/DropOrphanedTables.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace OCA\Polls\Migration\RepairSteps;

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/FixNullish.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace OCA\Polls\Migration\RepairSteps;

use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
namespace OCA\Polls\Migration\RepairSteps;

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Db\V6\IndexManager;
use OCA\Polls\Db\V7\IndexManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/MigratePublicToOpen.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace OCA\Polls\Migration\RepairSteps;

use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/RemoveIndices.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace OCA\Polls\Migration\RepairSteps;

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Db\V6\IndexManager;
use OCA\Polls\Db\V7\IndexManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/RemoveObsoleteMigrations.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
namespace OCA\Polls\Migration\RepairSteps;

use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/SetLastInteraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace OCA\Polls\Migration\RepairSteps;

use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCA\Polls\Db\WatchMapper;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/UpdateHashes.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace OCA\Polls\Migration\RepairSteps;

use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
2 changes: 1 addition & 1 deletion lib/Migration/RepairSteps/UpdateInteraction.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace OCA\Polls\Migration\RepairSteps;

use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
use OCP\Migration\IRepairStep;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

namespace OCA\Polls\Migration\V6;
namespace OCA\Polls\Migration\V7;

use OCA\Polls\Db\Comment;
use OCA\Polls\Db\Log;
Expand Down
4 changes: 2 additions & 2 deletions lib/Migration/Version080301Date20250822153002.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace OCA\Polls\Migration;

use OCA\Polls\Db\V6\IndexManager;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\IndexManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\DB\ISchemaWrapper;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
Expand Down
4 changes: 2 additions & 2 deletions lib/Migration/Version080307Date20250826231102.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
namespace OCA\Polls\Migration;

use OCA\Polls\Db\Share;
use OCA\Polls\Db\V6\IndexManager;
use OCA\Polls\Db\V6\TableManager;
use OCA\Polls\Db\V7\IndexManager;
use OCA\Polls\Db\V7\TableManager;
use OCP\DB\ISchemaWrapper;
use OCP\IDBConnection;
use OCP\Migration\IOutput;
Expand Down
Loading
Loading