Refactor product relations to use new category_product and channel_product tables
- Updated queries and relationships in various controllers and models to replace `product_has_relations` with `category_product` and `channel_product`. - Adjusted methods in `ProductFilterer`, `FilterParamsController`, and `FilterController` to reflect the new database structure. - Modified seeder to insert data into the new `channel_product` table. - Updated tests to ensure compatibility with the new product relation structure.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
// 1. Create new pivot tables
|
||||
Schema::create('category_product', function (Blueprint $table) {
|
||||
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('category_id')->constrained()->cascadeOnDelete();
|
||||
// No primary key on pivot usually, but maybe composite unique?
|
||||
// product_has_relations didn't have one explicitly shown but usually pivots do.
|
||||
// Let's add an index for performance.
|
||||
$table->unique(['product_id', 'category_id']);
|
||||
});
|
||||
|
||||
Schema::create('collection_product', function (Blueprint $table) {
|
||||
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('collection_id')->constrained()->cascadeOnDelete();
|
||||
$table->unique(['product_id', 'collection_id']);
|
||||
});
|
||||
|
||||
Schema::create('channel_product', function (Blueprint $table) {
|
||||
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('channel_id')->constrained()->cascadeOnDelete();
|
||||
$table->unique(['product_id', 'channel_id']);
|
||||
});
|
||||
|
||||
Schema::create('product_related', function (Blueprint $table) {
|
||||
$table->foreignId('product_id')->constrained()->cascadeOnDelete();
|
||||
$table->foreignId('related_product_id')->constrained('products')->cascadeOnDelete();
|
||||
$table->unique(['product_id', 'related_product_id']);
|
||||
});
|
||||
|
||||
// 2. Migrate data
|
||||
// Using raw SQL for efficiency and simplicity
|
||||
DB::statement("INSERT INTO category_product (product_id, category_id) SELECT product_id, productable_id FROM product_has_relations WHERE productable_type = 'category'");
|
||||
DB::statement("INSERT INTO collection_product (product_id, collection_id) SELECT product_id, productable_id FROM product_has_relations WHERE productable_type = 'collection'");
|
||||
DB::statement("INSERT INTO channel_product (product_id, channel_id) SELECT product_id, productable_id FROM product_has_relations WHERE productable_type = 'channel'");
|
||||
DB::statement("INSERT INTO product_related (product_id, related_product_id) SELECT product_id, productable_id FROM product_has_relations WHERE productable_type = 'product'");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('category_product');
|
||||
Schema::dropIfExists('collection_product');
|
||||
Schema::dropIfExists('channel_product');
|
||||
Schema::dropIfExists('product_related');
|
||||
}
|
||||
};
|
||||
@@ -45,10 +45,9 @@ class ProductsTableSeeder extends Seeder
|
||||
});
|
||||
|
||||
$channel = $user ? $user->channel() : tmpostChannel();
|
||||
DB::table('product_has_relations')->insert([
|
||||
DB::table('channel_product')->insert([
|
||||
'product_id' => $data['id'],
|
||||
'productable_id' => $channel->id,
|
||||
'productable_type' => 'channel',
|
||||
'channel_id' => $channel->id,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user