-
Notifications
You must be signed in to change notification settings - Fork 170
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed variants propagating to incorrect sites
- Loading branch information
1 parent
8710f1c
commit 05f65bf
Showing
4 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
src/migrations/m240923_132625_remove_orphaned_variants_sites.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
|
||
namespace craft\commerce\migrations; | ||
|
||
use craft\db\Migration; | ||
use craft\db\Query; | ||
|
||
/** | ||
* m240923_132625_remove_orphaned_variants_sites migration. | ||
*/ | ||
class m240923_132625_remove_orphaned_variants_sites extends Migration | ||
{ | ||
/** | ||
* @inheritdoc | ||
*/ | ||
public function safeUp(): bool | ||
{ | ||
// Find all existing combinations of product and site IDs | ||
$allProductsSites = (new Query()) | ||
->select(['elementId', 'siteId']) | ||
->from('{{%elements_sites}}' . ' es') | ||
->innerJoin('{{%commerce_products}}' . ' p', '[[es.elementId]] = [[p.id]]') | ||
->collect(); | ||
|
||
// Group them by product ID | ||
$siteIdsByProductId = $allProductsSites->groupBy('elementId')->map(function($row) { | ||
return collect($row)->pluck('siteId')->toArray(); | ||
} | ||
); | ||
|
||
// Find all existing combinations of variant and site IDs | ||
$allVariantsSites = (new Query) | ||
->select(['es.id', 'elementId', 'siteId', 'primaryOwnerId']) | ||
->from('{{%elements_sites}}' . ' es') | ||
->innerJoin('{{%commerce_variants}}' . ' v', '[[es.elementId]] = [[v.id]]') | ||
->collect(); | ||
|
||
// Find all variants that are not associated with any of their product's sites | ||
$orphanedVariantsSites = array_values($allVariantsSites->filter(function($row) use ($siteIdsByProductId) { | ||
return !in_array($row['siteId'], $siteIdsByProductId[$row['primaryOwnerId']]); | ||
})->map(fn($row) => $row['id'])->toArray()); | ||
|
||
if (empty($orphanedVariantsSites)) { | ||
return true; | ||
} | ||
|
||
// Bulk delete the orphaned variants' site rows (if any) 1000 at a time | ||
foreach (array_chunk($orphanedVariantsSites, 1000) as $chunk) { | ||
$this->delete('{{%elements_sites}}', ['id' => $chunk]); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function safeDown(): bool | ||
{ | ||
echo "m240923_132625_remove_orphaned_variants_sites cannot be reverted.\n"; | ||
return false; | ||
} | ||
} |