Skip to content
Open
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 src/config/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
'id' => 'CraftCMS',
'name' => 'Craft CMS',
'version' => '5.8.21',
'schemaVersion' => '5.9.0.2',
'schemaVersion' => '5.9.0.3',
'minVersionRequired' => '4.5.0',
'basePath' => dirname(__DIR__), // Defines the @app alias
'runtimePath' => '@storage/runtime', // Defines the @runtime alias
Expand Down
1 change: 0 additions & 1 deletion src/migrations/Install.php
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,6 @@ public function addForeignKeys(): void
$this->addForeignKey(null, Table::RELATIONS, ['sourceSiteId'], Table::SITES, ['id'], 'CASCADE', 'CASCADE');
$this->addForeignKey(null, Table::REVISIONS, ['creatorId'], Table::USERS, ['id'], 'SET NULL', null);
$this->addForeignKey(null, Table::REVISIONS, ['canonicalId'], Table::ELEMENTS, ['id'], 'CASCADE', null);
$this->addForeignKey(null, Table::SEARCHINDEXQUEUE, 'elementId', Table::ELEMENTS, 'id', 'CASCADE', null);
$this->addForeignKey(null, Table::SEARCHINDEXQUEUE_FIELDS, 'jobId', Table::SEARCHINDEXQUEUE, 'id', 'CASCADE', null);
$this->addForeignKey(null, Table::SECTIONS, ['structureId'], Table::STRUCTURES, ['id'], 'SET NULL', null);
$this->addForeignKey(null, Table::SECTIONS_ENTRYTYPES, ['sectionId'], Table::SECTIONS, ['id'], 'CASCADE', null);
Expand Down
30 changes: 30 additions & 0 deletions src/migrations/m251205_190131_drop_searchindexqueue_fk.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace craft\migrations;

use craft\db\Migration;
use craft\db\Table;

/**
* m251205_190131_drop_searchindexqueue_fk migration.
*/
class m251205_190131_drop_searchindexqueue_fk extends Migration
{
/**
* @inheritdoc
*/
public function safeUp(): bool
{
$this->dropForeignKeyIfExists(Table::SEARCHINDEXQUEUE, 'elementId');
return true;
}

/**
* @inheritdoc
*/
public function safeDown(): bool
{
$this->addForeignKey(null, Table::SEARCHINDEXQUEUE, 'elementId', Table::ELEMENTS, 'id', 'CASCADE', null);
return true;
}
}
8 changes: 8 additions & 0 deletions src/services/Gc.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public function run(bool $force = false): void

$this->_deleteOrphanedDraftsAndRevisions();
$this->_deleteOrphanedSearchIndexes();
$this->_deleteOrphanedSearchIndexJobs();
$this->_deleteOrphanedRelations();
$this->_deleteOrphanedStructureElements();
$this->_deleteOrphanedFkRows();
Expand Down Expand Up @@ -580,6 +581,13 @@ private function _deleteOrphanedSearchIndexes(): void
$this->_stdout("done\n", Console::FG_GREEN);
}

private function _deleteOrphanedSearchIndexJobs(): void
{
$this->_stdout(' > deleting orphaned search index jobs ... ');
Craft::$app->getSearch()->deleteOrphanedIndexJobs();
$this->_stdout("done\n", Console::FG_GREEN);
}

private function _deleteOrphanedRelations(): void
{
$this->_stdout(' > deleting orphaned relations ... ');
Expand Down
48 changes: 32 additions & 16 deletions src/services/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -308,22 +308,9 @@ public function indexElementIfQueued(int $elementId, int $siteId, ?string $eleme
}

try {
for ($try = 0; $try < 3; $try++) {
try {
if (!Db::update(Table::SEARCHINDEXQUEUE, ['reserved' => true], ['id' => $jobId])) {
// another process must be handling the same job
return;
}
break;
} catch (DbException $e) {
if (str_contains($e->getPrevious()?->getMessage(), 'deadlock')) {
// A gap lock was probably hit. Try again in one second
// https://github.com/craftcms/cms/issues/17318
sleep(1);
} else {
throw $e;
}
}
if (!Db::update(Table::SEARCHINDEXQUEUE, ['reserved' => true], ['id' => $jobId])) {
// another process must be handling the same job
return;
}
} finally {
$mutex->release($lockName);
Expand Down Expand Up @@ -607,6 +594,35 @@ public function deleteOrphanedIndexes(): void
$db->createCommand($sql)->execute();
}

/**
* Deletes any search indexes that belong to elements that don’t exist anymore.
*
* @since 5.9.0
*/
public function deleteOrphanedIndexJobs(): void
{
$db = Craft::$app->getDb();
$searchIndexQueueTable = Table::SEARCHINDEXQUEUE;
$elementsTable = Table::ELEMENTS;

if ($db->getIsMysql()) {
$sql = <<<SQL
DELETE q.* FROM $searchIndexQueueTable q
LEFT JOIN $elementsTable e ON e.id = q.elementId
WHERE e.id IS NULL
SQL;
} else {
$sql = <<<SQL
DELETE FROM $searchIndexQueueTable q
WHERE NOT EXISTS (
SELECT * FROM $elementsTable
WHERE id = q."elementId"
)
SQL;
}
$db->createCommand($sql)->execute();
}

/**
* Indexes keywords for a specific element attribute/field.
*
Expand Down
Loading