fix: ignore unrequested row/document types in status counters

getStatusCounters() seeds a status bucket only for requested resource
types and guards the non-row branch with isset(). The row/document branch
had no such guard: a row/document count aggregated into the cache for a
type that was not requested would auto-vivify $status[$type], read an
unseeded 'pending' key (emitting an "Undefined array key" warning) and
leave a phantom, non-empty entry that survives the empty-bucket prune.

Downstream this surfaced as a flaky assertEmpty($statusCounters) after an
otherwise clean migration. Add the same isset() guard the non-row branch
already uses so unrequested row/document counts are ignored.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Jake BarnbyandClaude Opus 4.8 committed 2026-06-04 16:03:01 +12:00
1 parent 0395af6192
commit 9b5cf6d873
2 files changed
+36

No files matched your search

+9
View File
@@ -251,6 +251,15 @@ class Transfer
foreach ($this->cache->getAll() as $resourceType => $resources) {
foreach ($resources as $k => $resource) {
if (($resourceType === Resource::TYPE_ROW || $resourceType === Resource::TYPE_DOCUMENT) && is_string($resource)) {
// Only report status for resource types that were requested,
// mirroring the isset() guard below. Row/document counts can be
// aggregated into the cache for an unrequested type, which would
// otherwise read an unseeded 'pending' key and leave a phantom,
// non-empty counter.
if (!isset($status[$resourceType])) {
continue;
}
$resource = intval($resource);
$status[$resourceType][$k] = $resource;
@@ -5,6 +5,8 @@ namespace Utopia\Tests\Unit\General;
use PHPUnit\Framework\TestCase;
use Utopia\Migration\Resource;
use Utopia\Migration\Resources\Database\Database;
use Utopia\Migration\Resources\Database\Row;
use Utopia\Migration\Resources\Database\Table;
use Utopia\Migration\Transfer;
use Utopia\Tests\Unit\Adapters\MockDestination;
use Utopia\Tests\Unit\Adapters\MockSource;
@@ -64,4 +66,29 @@ class TransferTest extends TestCase
$this->assertSame('test', $database->getDatabaseName());
$this->assertSame('test', $database->getId());
}
/**
* Row and document counts are aggregated into the cache by status. When such
* a count exists for a resource type that was not part of the migration
* request, getStatusCounters() must ignore it, exactly as it already does for
* non-row resources via the isset() guard. Otherwise it reads an unseeded
* 'pending' key (triggering an "Undefined array key" warning) and reports a
* phantom, non-empty counter for a type the caller never asked to migrate.
*/
public function testStatusCountersIgnoreUnrequestedRowCounts(): void
{
// No resource types were requested, so 'row'/'document' are unrequested.
// A row count still leaks into the cache: the destination tallies row and
// document counts by status as it imports them.
$table = new Table(new Database('db', 'db'), 'table', 'table');
$row = new Row('row-1', $table);
$row->setStatus(Resource::STATUS_SUCCESS);
$this->transfer->getCache()->add($row);
$counters = $this->transfer->getStatusCounters();
$this->assertArrayNotHasKey(Resource::TYPE_ROW, $counters);
$this->assertSame([], $counters);
}
}