Oci migration Products comments

This commit is contained in:
Jérémy Munsch 2023-09-11 11:05:17 +02:00
parent 923c3e0f6a
commit 0681bf8f0a
No known key found for this signature in database
GPG key ID: 18361AF3BFEC0FEC

View file

@ -13,9 +13,12 @@ use App\Lib\Helpers\FileStorageHelper;
use App\Models\Brand;
use App\Models\Keyword;
use App\Models\Product;
use App\Models\ProductComment;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Console\OutputStyle;
use Illuminate\Contracts\Database\Query\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\QueryException;
use Illuminate\Database\Schema\Blueprint;
@ -27,6 +30,7 @@ use Illuminate\Support\Facades\Storage;
use Illuminate\Support\ItemNotFoundException;
use Illuminate\Support\Str;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\ConsoleOutput;
return new class extends Migration
@ -50,6 +54,12 @@ return new class extends Migration
'VEILLE_PROD', 'VEILLE_PROD_COMM',
];
/**
* Console tabulation (margin left)
* @var integer
*/
private $consoleTabulation = 28;
/** @var \App\Models\User */
private $dummyUser;
@ -64,10 +74,14 @@ return new class extends Migration
*/
public function up(): void
{
// ! DEV: Uncomment this to skip the migration
// return;
$console = new ConsoleOutput();
$section = $console->section();
$ouput = new ConsoleOutput();
$input = new ArgvInput();
$style = new OutputStyle($input, $ouput);
$section = $ouput->section();
if (!$style->confirm('Do this migration or skip ?', false)) {
return;
}
// * SEED KEYWORDS As they are needed on runtime.
Schema::disableForeignKeyConstraints();
@ -75,6 +89,7 @@ return new class extends Migration
DB::table('keywordables')->truncate();
DB::table('keywords')->truncate();
DB::table('products')->truncate();
DB::table('product_comments')->truncate();
// * Remove previously uploaded files.
File::deleteDirectory(storage_path('app/public/modelfiles/products'));
Schema::enableForeignKeyConstraints();
@ -100,15 +115,16 @@ return new class extends Migration
$this->fixForeignKeyActionOnProcesses();
// ! DEV: Comment this after running once, to develop a migration without remigrating every temp table.
// $this->createAndFillTempTables();
$this->createAndFillTempTables();
// * Migrate PRODUCTS, comment all except the one you want to develop here.
$this->migrateBrands();
$this->migrateProducts();
// ! DEV: Add a die here to develop migration and prevent droping temp tables
// dd('');
$this->migrateBrands($style);
$this->migrateProducts($style);
$this->migrateProductsComments($style);
if (!$style->confirm('Continue ?')) {
throw new \RuntimeException('Migration stoped');
}
$this->dropTempTables();
$section->clear();
}
@ -127,14 +143,84 @@ return new class extends Migration
// - MIGRATION METHODS -
/**
* Migrate products
*
* Migrate products comments
*
* @param \Illuminate\Console\OutputStyle $style
* @return void
* @throws \Error In case of failure.
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found
*/
private function migrateProducts(): void
private function migrateProductsComments(OutputStyle $style): void
{
// phpcs:enable
$index = "NUM_COMM";
$query = $this->getQuery("VEILLE_PROD_COMM")->orderBy($index);
$nbRowsToMigrate = (clone $query)->count();
$totalRowsToMigrate = $nbRowsToMigrate;
$console = new ConsoleOutput();
$console->writeln('');
$progress = new ProgressBar($console, $nbRowsToMigrate);
$progress->setMessage(str_pad('Migrate Products Comments', $this->consoleTabulation, ' '));
$progress->setFormat(' - %message% [%bar%] %percent:3s%% %current%/%max% %elapsed:6s%/%estimated:-6s%');
$skipped = collect();
while ($nbRowsToMigrate > 0) {
$progress->display();
$progress->advance(1);
$row = (clone $query)->skip($totalRowsToMigrate - $nbRowsToMigrate--)->limit(1)->first();
try {
/** @var \App\Models\ProductComment $comment */
$comment = ProductComment::query()->where('id', $row->{$index})->firstOrNew();
$comment->id = $row->NUM_COMM;
/** @var \App\Models\Product $product */
$product = Product::query()->where('id', $row->NUM_PROD)->firstOrFail();
$product->id = $row->NUM_PROD;
$date_saisie = $row->DATE_SAISIE ? $this->tryToParseCarbon($row->DATE_SAISIE, true) : null;
$fill = [
'product_id' => $product->id,
'evaluation' => $row->APPRECIATION ?? '',
'aspect' => $row->ASPECT,
'taste' => $row->GOUT,
'odor' => $row->ODEUR,
'texture' => $row->TEXTURE,
];
if ($date_saisie) {
// ? FIXME: IS this pertinent ?
$fill['created_at'] = $date_saisie;
$fill['updated_at'] = $date_saisie;
}
$comment->fill($fill);
if (!DB::transaction(fn () => $comment->saveQuietly())) {
throw new \Error("Failed to save comment {$row->NUM_COMM}");
}
} catch (ModelNotFoundException $e) {
$model = $e->getModel();
$skipped->push("No {$model} found for '{$row->NUM_PROD}', skip comment '{$row->NUM_COMM}'");
} //end try
} //end while
$progress->finish();
if ($skipped->count() and !$console->writeln('') and $style->confirm('Show errors ?', false)) {
$console->writeln('');
$skipped->each(fn (string $error) => $console->writeln(str_repeat(' ', 8) . $error));
}
}
/**
* Migrate products
*
*
* @param \Illuminate\Console\OutputStyle $style
* @return void
* @throws \Error In case of failure.
* @phpcs:disable Generic.Metrics.CyclomaticComplexity.MaxExceeded
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found
*/
private function migrateProducts(OutputStyle $style): void
{
// phpcs:enable
$otherAllegations = Str::of(File::get(base_path('database/migrations/other_allegations.txt')))
@ -170,7 +256,7 @@ return new class extends Migration
$console = new ConsoleOutput();
$console->writeln('');
$progress = new ProgressBar($console, $nbRowsToMigrate);
$progress->setMessage(str_pad('Migrate Products', 24, ' '));
$progress->setMessage(str_pad('Migrate Products', $this->consoleTabulation, ' '));
$progress->setFormat(' - %message% [%bar%] %percent:3s%% %current%/%max% %elapsed:6s%/%estimated:-6s%');
while ($nbRowsToMigrate > 0) {
@ -407,10 +493,13 @@ return new class extends Migration
/**
* Create unique brands using veille products
*
* @param \Illuminate\Console\OutputStyle $style
* @return void
* @phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.Found
*/
private function migrateBrands(): void
private function migrateBrands(OutputStyle $style): void
{
// phpcs:enable
$query = $this->getQuery("VEILLE_PROD");
/** @var \Illuminate\Support\Collection<string,string> */
$brands = (clone $query)->pluck('MARQUE')->filter()
@ -418,7 +507,7 @@ return new class extends Migration
$console = new ConsoleOutput();
$console->writeln('');
$progress = new ProgressBar($console, $brands->count());
$progress->setMessage(str_pad('Migrate Brands', 24, ' '));
$progress->setMessage(str_pad('Migrate Brands', $this->consoleTabulation, ' '));
$progress->setFormat(' - %message% [%bar%] %percent:3s%% %current%/%max% %elapsed:6s%/%estimated:-6s%');
// FIXME: Insert brand->filter and find from where it should be fetched.
$brands->each(function ($brandName) use ($progress) {
@ -616,7 +705,7 @@ return new class extends Migration
$this->getTableName($tableName),
$header
);
$message = str_pad("{$this->tablePrefix} {$tableName}", 24, ' ');
$message = str_pad("{$this->tablePrefix} {$tableName}", $this->consoleTabulation, ' ');
$section->write(" - {$message} -> Fetching OCI DATA..");
$content = $this->fetchOciTableContent($tableName, $header);
$section->clear();