From 910ce0205d04b562ad640485f975b00e15907926 Mon Sep 17 00:00:00 2001 From: Konstantinos Arvanitakis Date: Tue, 15 Sep 2026 22:13:18 +0300 Subject: [PATCH] Feat: Adding command for backfilling all product skus --- src/Command/BackfillMissingSkusCommand.php | 60 ++++++++++++++++++++++ src/Providers/CoreServiceProvider.php | 3 +- 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/Command/BackfillMissingSkusCommand.php diff --git a/src/Command/BackfillMissingSkusCommand.php b/src/Command/BackfillMissingSkusCommand.php new file mode 100644 index 0000000..6bbbbed --- /dev/null +++ b/src/Command/BackfillMissingSkusCommand.php @@ -0,0 +1,60 @@ +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.'); + } +} diff --git a/src/Providers/CoreServiceProvider.php b/src/Providers/CoreServiceProvider.php index 2756ca1..4c29232 100644 --- a/src/Providers/CoreServiceProvider.php +++ b/src/Providers/CoreServiceProvider.php @@ -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]));