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
10 changes: 7 additions & 3 deletions src/app/Console/Commands/AlgoliaImportWorldHeritages.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,12 @@ public function handle(): int

WorldHeritage::query()
->with([
'countries',
'images' => function ($query) {
$query->where('is_primary', true)->select(['world_heritage_site_id', 'url']);
},
'countries' => function ($query) {
$query->select(['countries.state_party_code', 'countries.name_en', 'countries.name_jp']);
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The countries relation here is used to build arrays that are sent to Algolia. Without an explicit ordering, the DB may return countries in a non-deterministic order, which can cause unnecessary Algolia updates (array order changes) and make results harder to reason about. Consider adding an orderBy('countries.state_party_code') (or another stable key) in this eager-load constraint.

Suggested change
$query->select(['countries.state_party_code', 'countries.name_en', 'countries.name_jp']);
$query->select(['countries.state_party_code', 'countries.name_en', 'countries.name_jp'])
->orderBy('countries.state_party_code');

Copilot uses AI. Check for mistakes.
},
])
->select([
'world_heritage_sites.id',
Expand All @@ -63,7 +68,6 @@ public function handle(): int
'world_heritage_sites.category',
'world_heritage_sites.year_inscribed',
'world_heritage_sites.is_endangered',
'world_heritage_sites.image_url',
])
->chunkById($chunk, function ($rows) use ($client, $indexName, $dryRun, &$processed) {
$objects = [];
Expand Down Expand Up @@ -130,7 +134,7 @@ public function handle(): int
'category' => (string) $row->category,
'year_inscribed' => $row->year_inscribed !== null ? (int) $row->year_inscribed : null,
'is_endangered' => (bool) $row->is_endangered,
'thumbnail_url' => $row->image_url !== null ? (string) $row->image_url : null,
'thumbnail_url' => $row->images->first()?->url,
'state_party_codes' => $statePartyCodes,
'country_names_jp' => $countryCount > 1 ? $countryNamesJp : [],
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ public function handle(): int
'latitude' => $this->toNullableFloat($row['latitude'] ?? null),
'longitude' => $this->toNullableFloat($row['longitude'] ?? null),
'short_description' => $this->toNullableString($row['short_description'] ?? null),
'image_url' => $this->toNullableString($row['image_url'] ?? null),
'unesco_site_url' => $this->toNullableString($row['unesco_site_url'] ?? null),
'created_at' => $now,
'updated_at' => $now,
Expand Down
4 changes: 3 additions & 1 deletion src/app/Packages/Domains/WorldHeritageReadQueryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ public function findByIdsPreserveOrder(array $ids): Collection
'world_heritage_sites.latitude',
'world_heritage_sites.longitude',
'world_heritage_sites.short_description',
'world_heritage_sites.image_url',
])
->with([
'countries' => function ($q) {
$q->select('countries.state_party_code', 'countries.name_en', 'countries.name_jp', 'countries.region')
->orderBy('countries.state_party_code', 'asc');
},
'images' => function ($imageQuery) {
$imageQuery->where('is_primary', true)->limit(1);
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

limit(1) inside a HasMany eager-load constraint will apply to the entire eager-load query, not per world_heritage_site_id. This can result in only one primary image being loaded across all returned sites, leaving the rest with an empty images relation. Prefer removing the limit(1) (and rely on is_primary uniqueness), or introduce a dedicated hasOne/ofMany relation like primaryImage and eager-load that instead.

Suggested change
$imageQuery->where('is_primary', true)->limit(1);
$imageQuery->where('is_primary', true);

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s no test coverage asserting that findByIdsPreserveOrder() returns the expected primary image per site (especially when multiple site IDs are requested). Given the eager-load constraint on images, adding a test that seeds images for at least 2 sites and verifies each model has its primary image loaded would prevent regressions and catch issues like global eager-load limits.

Suggested change
$imageQuery->where('is_primary', true)->limit(1);
$imageQuery->where('is_primary', true);

Copilot uses AI. Check for mistakes.
},
])
->whereIn('world_heritage_sites.id', $ids)
->get()
Expand Down
Loading