Fix bulk insert issue

This commit is contained in:
DariusIII
2026-05-14 11:39:21 +02:00
parent c2b00752d7
commit bd9aa517d3
3 changed files with 70 additions and 1 deletions
@@ -366,7 +366,7 @@ class ManticoreSearchDriver implements SearchDriverInterface
$documents[] = array_merge(
['id' => $release['id']],
ReleaseSearchIndexDocument::normalize($release)
ReleaseSearchIndexDocument::normalizeForBulk($release)
);
}
@@ -9,6 +9,26 @@ namespace App\Support;
*/
final class ReleaseSearchIndexDocument
{
/**
* Normalize a release row for bulk indexing. Rows already produced by {@see normalize()}
* (e.g. from release search populate) lack `postdate` / `adddate` keys;
* calling {@see normalize()} again would zero `postdate_ts` / `adddate_ts` unless those sources are restored.
*
* @param array<string, mixed> $row
* @return array<string, mixed>
*/
public static function normalizeForBulk(array $row): array
{
if (array_key_exists('postdate_ts', $row) && ! array_key_exists('postdate', $row)) {
$row = array_merge($row, [
'postdate' => $row['postdate_ts'] ?? null,
'adddate' => $row['adddate_ts'] ?? null,
]);
}
return self::normalize($row);
}
/**
* @param array<string, mixed> $row Keys from DB or insert parameters
* @return array<string, mixed>
@@ -0,0 +1,49 @@
<?php
declare(strict_types=1);
namespace Tests\Unit;
use App\Support\ReleaseSearchIndexDocument;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
final class ReleaseSearchIndexDocumentTest extends TestCase
{
#[Test]
public function normalize_for_bulk_preserves_timestamps_when_row_is_already_normalized(): void
{
$first = ReleaseSearchIndexDocument::normalize([
'id' => 42,
'name' => 'n',
'searchname' => 's',
'fromname' => 'f',
'categories_id' => 1,
'filename' => '',
'imdbid' => '',
'tmdbid' => 0,
'traktid' => 0,
'tvdb' => 0,
'tvmaze' => 0,
'tvrage' => 0,
'videos_id' => 0,
'movieinfo_id' => 0,
'size' => 100,
'postdate' => '2025-01-15 12:00:00',
'adddate' => '2025-01-16 08:30:00',
'totalpart' => 0,
'grabs' => 0,
'passwordstatus' => -1,
'groups_id' => 1,
'nzbstatus' => 1,
'haspreview' => 0,
]);
$second = ReleaseSearchIndexDocument::normalizeForBulk($first);
self::assertSame($first['postdate_ts'], $second['postdate_ts']);
self::assertSame($first['adddate_ts'], $second['adddate_ts']);
self::assertGreaterThan(0, $second['postdate_ts']);
self::assertGreaterThan(0, $second['adddate_ts']);
}
}