Remove nzb_guid column

This commit is contained in:
DariusIII
2026-05-08 18:52:14 +02:00
parent d30efc4070
commit a6a095586f
6 changed files with 101 additions and 49 deletions
-2
View File
@@ -29,7 +29,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @property string $shareid
* @property string $siteid
* @property int|null $sourceid
* @property mixed $nzb_guid
* @property-read Release $release
* @property-read User $user
*
@@ -40,7 +39,6 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereIssynced($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereIsvisible($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereNzbGuid($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereReleasesId($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereShared($value)
* @method static \Illuminate\Database\Eloquent\Builder|\App\Models\ReleaseComment whereShareid($value)
-31
View File
@@ -57,11 +57,6 @@ class NzbImportService
public NzbService $nzb;
/**
* The MD5 hash of the first segment Message-ID of the NZB
*/
protected string $nzbGuid;
/**
* @param array<string, mixed> $options
*/
@@ -123,8 +118,6 @@ class NzbImportService
// Loop over the NZB file names only.
foreach ($nzbFiles as $nzbFilePath) {
$this->nzbGuid = '';
// Check if the file is really there.
if (File::isFile($nzbFilePath)) {
// Get the contents of the NZB file as a string.
@@ -187,7 +180,6 @@ class NzbImportService
}
$nzbsSkipped++;
} else {
$this->updateNzbGuid();
if ($delete) {
// Remove the nzb file.
File::delete($nzbFilePath);
@@ -339,17 +331,6 @@ class NzbImportService
// After scanning all files, persist any matched whitelist/blacklist usage
$this->blacklistService->updateBlacklistUsage($this->blacklistService->getAndClearIdsToUpdate()); // @phpstan-ignore argument.type
// Sort values alphabetically but keep the keys intact
if (\count($binary_names) > 0) {
asort($binary_names);
foreach ($nzbXML->file as $file) {
if ($file['subject'] === $binary_names[0]) {
$this->nzbGuid = md5((string) $file->segments->segment);
break;
}
}
}
// Try to insert the NZB details into the DB.
return $this->insertNZB(
[
@@ -469,16 +450,4 @@ class NzbImportService
cli()->notice($message);
}
}
/**
* The function updates the NZB guid after there is no chance of deletion.
*/
protected function updateNzbGuid(): void
{
try {
Release::query()->where('guid', $this->relGuid)->update(['nzb_guid' => sodium_hex2bin($this->nzbGuid)]);
} catch (\SodiumException $e) {
$this->echoOut('ERROR: Problem updating nzb_guid for: '.$this->relGuid);
}
}
}
-8
View File
@@ -124,8 +124,6 @@ class NzbService
$XMLWriter->setIndent(true);
$XMLWriter->setIndentString(' ');
$nzb_guid = '';
$XMLWriter->startDocument('1.0', 'UTF-8');
$XMLWriter->startDtd(self::NZB_DTD_NAME, self::NZB_DTD_PUBLIC, self::NZB_DTD_EXTERNAL);
$XMLWriter->endDtd();
@@ -181,9 +179,6 @@ class NzbService
$XMLWriter->startElement('segments');
foreach ($parts as $part) {
$messageId = $this->normalizeSegmentMessageId($part->messageid);
if ($nzb_guid === '') {
$nzb_guid = $messageId;
}
$XMLWriter->startElement('segment');
$XMLWriter->writeAttribute('bytes', (string) $part->size);
$XMLWriter->writeAttribute('number', (string) $part->partnumber);
@@ -212,9 +207,6 @@ class NzbService
}
// Mark release as having NZB.
$release->update(['nzbstatus' => self::NZB_ADDED]);
if (! empty($nzb_guid)) {
$release->update(['nzb_guid' => DB::raw('UNHEX( '.escapeString(md5((string) $nzb_guid)).' )')]);
}
// Delete CBP (Collections, Binaries, Parts) for release that has its NZB created.
// Use a transaction to ensure cascading deletes complete properly.
@@ -0,0 +1,100 @@
<?php
declare(strict_types=1);
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* Drop the unused `nzb_guid` columns.
*
* `releases.nzb_guid` was only ever written (md5 of the first NZB segment
* Message-ID) by NzbService and NzbImportService and never read by any code
* path. `release_comments.nzb_guid` was fully dead — never written, never
* read. Dropping both columns and the dedicated `ix_releases_nzb_guid` index
* removes write overhead with no consumer-facing impact.
*
* Rollback caveat: the original `releases.nzb_guid` was `BLOB NOT NULL` with
* no default. Recreating it with a zero-byte default keeps existing rows from
* being blocked on `migrate:rollback`.
*/
return new class extends Migration
{
public function up(): void
{
if (Schema::hasTable('releases') && $this->indexExists('releases', 'ix_releases_nzb_guid')) {
Schema::table('releases', function (Blueprint $table) {
$table->dropIndex('ix_releases_nzb_guid');
});
}
if (Schema::hasTable('releases') && Schema::hasColumn('releases', 'nzb_guid')) {
Schema::table('releases', function (Blueprint $table) {
$table->dropColumn('nzb_guid');
});
}
if (Schema::hasTable('release_comments') && Schema::hasColumn('release_comments', 'nzb_guid')) {
Schema::table('release_comments', function (Blueprint $table) {
$table->dropColumn('nzb_guid');
});
}
}
public function down(): void
{
$driver = DB::getDriverName();
if (Schema::hasTable('releases') && ! Schema::hasColumn('releases', 'nzb_guid')) {
if ($driver === 'sqlite') {
Schema::table('releases', function (Blueprint $table) {
$table->binary('nzb_guid')->nullable();
});
} else {
DB::statement(
"ALTER TABLE `releases` ADD COLUMN `nzb_guid` BLOB NOT NULL DEFAULT ''"
);
}
}
if (Schema::hasTable('releases') && ! $this->indexExists('releases', 'ix_releases_nzb_guid')) {
if ($driver === 'sqlite') {
Schema::table('releases', function (Blueprint $table) {
$table->index('nzb_guid', 'ix_releases_nzb_guid');
});
} else {
DB::statement('CREATE INDEX `ix_releases_nzb_guid` ON `releases` (`nzb_guid`(3072))');
}
}
if (Schema::hasTable('release_comments') && ! Schema::hasColumn('release_comments', 'nzb_guid')) {
if ($driver === 'sqlite') {
Schema::table('release_comments', function (Blueprint $table) {
$table->binary('nzb_guid')->nullable();
});
} else {
DB::statement(
"ALTER TABLE `release_comments` ADD COLUMN `nzb_guid` BINARY(16) NOT NULL DEFAULT '\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0\\0'"
);
}
}
}
private function indexExists(string $table, string $indexName): bool
{
if (DB::getDriverName() === 'sqlite') {
$rows = DB::select(
"SELECT name FROM sqlite_master WHERE type = 'index' AND tbl_name = ? AND name = ?",
[$table, $indexName]
);
return $rows !== [];
}
$rows = DB::select("SHOW INDEX FROM `{$table}` WHERE Key_name = ?", [$indexName]);
return $rows !== [];
}
};
+1 -4
View File
@@ -89,7 +89,6 @@ class CbpCleanupServiceTest extends TestCase
'iscategorized' => 1,
'predb_id' => 0,
'source' => null,
'nzb_guid' => null,
]);
DB::table('collections')->insert([
@@ -158,7 +157,6 @@ class CbpCleanupServiceTest extends TestCase
'iscategorized' => 1,
'predb_id' => 0,
'source' => null,
'nzb_guid' => null,
]);
DB::table('collections')->insert([
@@ -244,8 +242,7 @@ class CbpCleanupServiceTest extends TestCase
isrenamed INTEGER,
iscategorized INTEGER,
predb_id INTEGER,
source VARCHAR(255) NULL,
nzb_guid BLOB NULL
source VARCHAR(255) NULL
)');
DB::statement('CREATE TABLE collections (
id INTEGER PRIMARY KEY,
@@ -106,7 +106,6 @@ class ReleaseNameFixedRecategorizationTest extends TestCase
'isrenamed' => 0,
'guid' => str_repeat('a', 40),
'leftguid' => 'a',
'nzb_guid' => 'test',
'size' => 1,
'postdate' => now(),
'adddate' => now(),
@@ -154,7 +153,6 @@ class ReleaseNameFixedRecategorizationTest extends TestCase
'isrenamed' => 0,
'guid' => str_repeat('b', 40),
'leftguid' => 'b',
'nzb_guid' => 'test-olympics',
'size' => 1,
'postdate' => now(),
'adddate' => now(),
@@ -200,7 +198,6 @@ class ReleaseNameFixedRecategorizationTest extends TestCase
'isrenamed' => 1,
'guid' => str_repeat('c', 40),
'leftguid' => 'c',
'nzb_guid' => 'test-southern-charm',
'size' => 1,
'postdate' => now(),
'adddate' => now(),
@@ -298,7 +295,6 @@ class ReleaseNameFixedRecategorizationTest extends TestCase
$table->tinyInteger('proc_crc32')->default(0);
$table->tinyInteger('passwordstatus')->default(0);
$table->tinyInteger('nzbstatus')->default(0);
$table->binary('nzb_guid')->nullable();
});
}
}