From 817ffa68fc11e025ef3c703ddf340ce1afd0c07b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Munsch?= Date: Wed, 6 Sep 2023 16:05:17 +0200 Subject: [PATCH] Product migration should be ok --- app/Models/Brand.php | 2 +- .../2023_08_14_124752_migrate_oci_tables.php | 109 ++++++++++++++---- 2 files changed, 87 insertions(+), 24 deletions(-) diff --git a/app/Models/Brand.php b/app/Models/Brand.php index b954abf..0b14aa0 100644 --- a/app/Models/Brand.php +++ b/app/Models/Brand.php @@ -7,7 +7,7 @@ use Illuminate\Database\Eloquent\Model; /** * @property integer $id - * @property string $brand + * @property string $name * @property string $filter * @property string $created_at * @property string $updated_at diff --git a/database/migrations/2023_08_14_124752_migrate_oci_tables.php b/database/migrations/2023_08_14_124752_migrate_oci_tables.php index 9be10fc..c20fcda 100644 --- a/database/migrations/2023_08_14_124752_migrate_oci_tables.php +++ b/database/migrations/2023_08_14_124752_migrate_oci_tables.php @@ -9,6 +9,7 @@ use App\Enums\Products\OriginEnum; use App\Enums\Products\RangeEnum; use App\Enums\Products\RayEnum; use App\Enums\Products\SourceEnum; +use App\Lib\Helpers\FileStorageHelper; use App\Models\Brand; use App\Models\Keyword; use App\Models\Product; @@ -22,6 +23,7 @@ use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Facades\Storage; use Illuminate\Support\Str; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Component\Console\Output\ConsoleOutput; @@ -47,9 +49,12 @@ return new class extends Migration 'VEILLE_PROD', 'VEILLE_PROD_COMM', ]; - /** @var \App\Model\User */ + /** @var \App\Models\User */ private $dummyUser; + /** @var \App\Models\Brand */ + private $dummyBrand; + /** * Run the migrations. * @@ -67,6 +72,8 @@ return new class extends Migration DB::table('keywordables')->truncate(); DB::table('keywords')->truncate(); DB::table('products')->truncate(); + // * Remove previously uploaded files. + File::deleteDirectory(storage_path('app/public/modelfiles/products')); Schema::enableForeignKeyConstraints(); if (Artisan::call('db:seed', ['--class' => 'KeywordInitSeeder']) !== 0) { throw new \RuntimeException('Failed to seed keywords'); @@ -77,6 +84,11 @@ return new class extends Migration 'firstname' => 'Dummy', 'lastname' => 'User' ]); + // * Seed a dummy brand. + $this->dummyBrand = Brand::factory()->createOne([ + 'name' => Str::of('Sans Marque')->slug()->studly(), + 'filter' => null + ]); // * Apply DB corrections $this->setUniqueOnBrandName(); @@ -85,7 +97,7 @@ 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(); @@ -93,7 +105,7 @@ return new class extends Migration // ! DEV: Add a die here to develop migration and prevent droping temp tables - // dd('toto'); + dd('toto'); $this->dropTempTables(); $section->clear(); @@ -124,7 +136,7 @@ return new class extends Migration { // phpcs:enable $otherAllegations = Str::of(File::get(base_path('database/migrations/other_allegations.txt'))) - ->explode(PHP_EOL)->map(fn (string $other_allegation) => Str::of($other_allegation)->squish()) + ->explode(PHP_EOL)->map(fn (string $other_allegation) => Str::of($other_allegation)->squish()) ->unique(); $MOTKS = Keyword::all(); @@ -211,15 +223,15 @@ return new class extends Migration // * Uu OMG !!! if ($motk === 'Gain de temps Nomadisme') { return $MOTKS->where('label', 'Gain de temps') - ->where('keyword_cat', KeywordCategoryEnum::productPracticality)->firstOrFail(); + ->where('keyword_cat', KeywordCategoryEnum::productPracticality)->firstOrFail(); } if ($motk === 'Vari??t?? des sens') { return $MOTKS->where('label', 'Variété des sens') - ->where('keyword_cat', KeywordCategoryEnum::productPleasure)->firstOrFail(); + ->where('keyword_cat', KeywordCategoryEnum::productPleasure)->firstOrFail(); } if ($motk === 'V??g??tal') { return $MOTKS->where('label', 'Végétal') - ->where('keyword_cat', KeywordCategoryEnum::productHealth)->firstOrFail(); + ->where('keyword_cat', KeywordCategoryEnum::productHealth)->firstOrFail(); } try { return $MOTKS->firstOrFail( @@ -235,28 +247,32 @@ return new class extends Migration } }; - // FIXME: thread https://lab.s-web.fr/ldc-web/projets/startcoq/-/issues/28#note_9740 . - if (empty($row->MARQUE)) { + // NOTE: thread https://lab.s-web.fr/ldc-web/projets/startcoq/-/issues/28#note_9740 . + if (is_null($row->MARQUE)) { continue; } - // FIXME: thread https://lab.s-web.fr/ldc-web/projets/startcoq/-/issues/28#note_9750 . + if (!strlen(trim($row->MARQUE))) { + $row->MARQUE = $this->dummyBrand->name; + continue; + } + // NOTE: thread https://lab.s-web.fr/ldc-web/projets/startcoq/-/issues/28#note_9750 . if (empty($row->NUM_RESEAU)) { continue; } - // FIXME: thread https://lab.s-web.fr/ldc-web/projets/startcoq/-/issues/28#note_9758 . + // NOTE: thread https://lab.s-web.fr/ldc-web/projets/startcoq/-/issues/28#note_9758 . if (empty($row->NUM_SECTEUR)) { continue; } - // FIXME: thread https://lab.s-web.fr/ldc-web/projets/startcoq/-/issues/28#note_9760 . + // NOTE: thread https://lab.s-web.fr/ldc-web/projets/startcoq/-/issues/28#note_9760 . if (empty($row->NUM_RAYON)) { continue; } - // FIXME: thread https://lab.s-web.fr/ldc-web/projets/startcoq/-/issues/28#note_9767 . + // NOTE: thread https://lab.s-web.fr/ldc-web/projets/startcoq/-/issues/28#note_9767 . if (empty($row->NUM_FAMILLE)) { continue; } - $brandName = Str::of($row->MARQUE)->slug()->studly(); + $brandName = Str::of($row->MARQUE)->trim()->title(); /** name is unique on brand */ $brand = null; try { @@ -316,10 +332,28 @@ return new class extends Migration } } + + /** @var \App\Models\Product $product */ $product = Product::query()->where('id', $row->{$index})->firstOrNew(); $product->id = $row->NUM_VEILLE_PROD; - $fill = [ + + // * DOCS + $disk = $this->getMigrationStorage('Produit/' . strval($row->{$index})); + $files = collect($disk->files())->mapWithKeys(function ($file) use ($disk, $product) { + $filePath = FileStorageHelper::storeFile($product, new \SplFileInfo($disk->path($file))); + return [pathinfo($filePath, PATHINFO_BASENAME) => $filePath]; + }); + + $picture = $files->getOrPut('ref.jpg', FileStorageHelper::storeFile( + $product, + new \SplFileInfo(app_path( + '../database/factories/assets/product/default-picture-1200x900.png' + )) + )); + $files->pull('ref.jpg'); + + $fill = [ 'brand_id' => $brand->id, 'catalogue' => $row->CATALOGUE, 'certification' => $certificationEnum, @@ -341,10 +375,11 @@ return new class extends Migration 'specified_claim' => $claim, 'tasters_number' => $row->NB_DEGUST, 'origins' => $originList, - 'docs' => 'tttt', - 'picture_alt' => 'tttt', - 'picture_title' => 'tttt', - 'picture' => 'tttt', + 'docs' => $files->all(), + 'picture_alt' => "Image de {$row->NOM}", + 'picture_title' => "Image de {$row->NOM}", + 'picture' => $picture, + // NOTE: https://lab.s-web.fr/ldc-web/projets/startcoq/-/issues/28#note_10688 . 'user_id' => $this->dummyUser->id ]; if ($date_saisie) { @@ -376,7 +411,7 @@ return new class extends Migration $query = $this->getQuery("VEILLE_PROD"); /** @var \Illuminate\Support\Collection */ $brands = (clone $query)->pluck('MARQUE')->filter() - ->map(fn (string $brandName) => Str::of($brandName)->slug()->studly())->unique(); + ->map(fn (string $brandName) => Str::of($brandName)->trim()->title())->unique(); $console = new ConsoleOutput(); $console->writeln(''); $progress = new ProgressBar($console, $brands->count()); @@ -445,7 +480,7 @@ return new class extends Migration }); Schema::table('keywordables', function (Blueprint $table) { $table->foreign(['keyword_id'])->references(['id'])->on('keywords') - ->restrictOnUpdate()->cascadeOnDelete(); + ->restrictOnUpdate()->cascadeOnDelete(); }); } @@ -461,13 +496,41 @@ return new class extends Migration }); Schema::table('processes', function (Blueprint $table) { $table->foreign(['user_id'])->references(['id'])->on('users') - ->restrictOnUpdate()->restrictOnDelete(); + ->restrictOnUpdate()->restrictOnDelete(); }); } // -- END DB CORRECTION -- // --- LIB METHODS --- + /** + * Get migration disk + * + * @param string|null $path Get a specific path. + * @return \Illuminate\Filesystem\FilesystemAdapter + * @throws \Error If veille folder is missing. + */ + private function getMigrationStorage(string $path = '') + { + $storagePath = storage_path('app/migration'); + $path = "/Veille/{$path}"; + + if (!File::exists("{$storagePath}{$path}")) { + throw new \Error("Missing storage path {$storagePath}{$path}"); + } + + config()->set('filesystems.disks.migration', [ + 'driver' => 'local', + 'root' => "{$storagePath}{$path}", + 'throw' => true, + ]); + /** @var \Illuminate\Filesystem\FilesystemAdapter */ + $disk = Storage::disk('migration'); + + + return $disk; + } + /** * Get Temp DB query builder * @@ -561,7 +624,7 @@ return new class extends Migration DB::table($this->getTableName($tableName))->insert((array)$row); } $progress->finish(); - }//end foreach + } //end foreach } /**