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
37 changes: 35 additions & 2 deletions lib/Db/V8/IndexManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,39 @@ public function createOptionalIndices(): array {
return $messages;
}

/**
* Ensure all tables have their primary key on 'id'.
* All PKs in this app are autoincrement 'id' columns — if one is missing
* (e.g. after a failed migration on a non-transactional DB engine), restore it.
*
* @return string[] logged messages
*/
public function repairPrimaryKeys(): array {
$this->needsSchema();
$messages = [];

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

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

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

if ($table->getPrimaryKey() === null) {
$table->setPrimaryKey(['id']);
$messages[] = 'Restored missing primary key for ' . $tableName;
}
}

if (empty($messages)) {
$messages[] = 'All primary keys intact';
}

return $messages;
}

/**
* add 'on delete' fk contraints to all tables referencing the main polls table
* Foreign key constraints are crucial for the correct operation of the polls app.
Expand Down Expand Up @@ -261,7 +294,7 @@ public function removeUniqueIndicesFromTable(string $tableName): array {
$table = $this->schema->getTable($tableName);

foreach ($table->getIndexes() as $index) {
if (strpos($index->getName(), 'UNIQ_') === 0) {
if (stripos($index->getName(), 'UNIQ_') === 0) {
$table->dropIndex($index->getName());
$messages[] = 'Removed ' . $index->getName() . ' from ' . $tableName;
}
Expand Down Expand Up @@ -435,7 +468,7 @@ public function createUniqueIndices(): array {
$table = $this->schema->getTable($prefixedTable);

foreach ($table->getIndexes() as $index) {
if ($index->isUnique()) {
if ($index->isUnique() && !$index->isPrimary()) {
$table->dropIndex($index->getName());
$messages[] = 'Dropped unique index ' . $index->getName() . ' from ' . $tableName;
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Migration/RepairSteps/CreateUniqueIndices.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace OCA\Polls\Migration\RepairSteps;

use Doctrine\DBAL\Exception as DBALException;
use Doctrine\DBAL\Schema\Schema;
use OCA\Polls\Db\V8\IndexManager;
use OCP\IDBConnection;
Expand Down Expand Up @@ -38,7 +37,7 @@ public function run(IOutput $output): void {

try {
$this->connection->migrateToSchema($this->schema);
} catch (DBALException $e) {
} catch (\Exception $e) {
// Hard fallback!
// Recreating indices can affect system performance on some db engines with large datasets.
// But the app relies on these indices to function properly, so we have to ensure they are created.
Expand All @@ -48,6 +47,7 @@ public function run(IOutput $output): void {
$output->warning('Polls - Index conflict detected, rebuilding unique indices: ' . $e->getMessage());
$this->schema = $this->connection->createSchema();
$this->indexManager->setSchema($this->schema);
$messages = array_merge($messages, $this->indexManager->repairPrimaryKeys());
$messages = array_merge($messages, $this->indexManager->removeAllUniqueIndices());
$this->connection->migrateToSchema($this->schema);

Expand Down
Loading