Feat: Adding command for backfilling all product skus
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Core\Command;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Lunar\Models\ProductVariant;
|
||||
|
||||
/**
|
||||
* One-off backfill for variants the Shopify import left with a blank SKU —
|
||||
* not an importer bug, the source CSV rows genuinely had no `Variant SKU`
|
||||
* value (see Modules\MigrateImport\Shopify\ShopifyExportImporter) — so
|
||||
* this synthesizes one instead of re-running the import. Format is
|
||||
* "SKU-P{product_id}-V{variant_id}": deterministic and guaranteed unique
|
||||
* without a uniqueness check, since product_id/variant_id already are.
|
||||
* Only variants with a null `sku` are touched.
|
||||
*/
|
||||
class BackfillMissingSkusCommand extends Command
|
||||
{
|
||||
protected $signature = 'boboko:catalog:backfill-skus {--dry-run : List what would change without writing}';
|
||||
|
||||
protected $description = 'Generate a SKU for every product variant that is missing one';
|
||||
|
||||
public function handle(): void
|
||||
{
|
||||
$dryRun = (bool) $this->option('dry-run');
|
||||
|
||||
$query = ProductVariant::query()->whereNull('sku');
|
||||
$total = $query->count();
|
||||
|
||||
if ($total === 0) {
|
||||
$this->info('No variants are missing a SKU.');
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->info(($dryRun ? '[dry-run] ' : '') . "Backfilling SKUs for {$total} variant(s)...");
|
||||
|
||||
$bar = $this->output->createProgressBar($total);
|
||||
$bar->start();
|
||||
|
||||
$query->chunkById(500, function ($variants) use ($dryRun, $bar) {
|
||||
foreach ($variants as $variant) {
|
||||
$sku = "SKU-P{$variant->product_id}-V{$variant->id}";
|
||||
|
||||
if ($dryRun) {
|
||||
$this->newLine();
|
||||
$this->line("Variant {$variant->id}: sku => {$sku}");
|
||||
} else {
|
||||
$variant->update(['sku' => $sku]);
|
||||
}
|
||||
|
||||
$bar->advance();
|
||||
}
|
||||
});
|
||||
|
||||
$bar->finish();
|
||||
$this->newLine();
|
||||
$this->info($dryRun ? 'Dry run complete — no changes were written.' : 'Done.');
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ namespace Modules\Core\Providers;
|
||||
use Illuminate\Support\Facades\Blade;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Modules\Core\Command\AnonymizeCommand;
|
||||
use Modules\Core\Command\BackfillMissingSkusCommand;
|
||||
use Modules\Core\Command\ExportCleanupCommand;
|
||||
use Modules\Core\Command\ExportCommand;
|
||||
use Modules\Core\Command\ImportCommand;
|
||||
@@ -36,7 +37,7 @@ class CoreServiceProvider extends ServiceProvider
|
||||
], 'core-assets');
|
||||
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->commands([AnonymizeCommand::class, ExportCommand::class, ExportCleanupCommand::class, ImportCommand::class, MigrateImportCommand::class, TuneProductSearchCommand::class]);
|
||||
$this->commands([AnonymizeCommand::class, ExportCommand::class, ExportCleanupCommand::class, ImportCommand::class, MigrateImportCommand::class, TuneProductSearchCommand::class, BackfillMissingSkusCommand::class]);
|
||||
|
||||
//Overriding lunar:install
|
||||
$this->app->booted(fn () => $this->commands([InstallLunarCommand::class]));
|
||||
|
||||
Reference in New Issue
Block a user