chengwl 1 giorno fa
parent
commit
86c128b39f

+ 128 - 0
packages/Longyi/Core/src/Database/Migrations/2026_02_26_000001_refactor_price_indices_to_polymorphic.php

@@ -0,0 +1,128 @@
+<?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
+{
+    public function up(): void
+    {
+        // ── product_price_indices ──────────────────────────────────
+
+        Schema::table('product_price_indices', function (Blueprint $table) {
+            // Drop foreign keys
+            $table->dropForeign(['product_id']);
+
+            // Drop unique & normal indexes that reference product_id
+            if (Schema::hasIndex('product_price_indices', 'price_indices_product_id_customer_group_id_channel_id_unique')) {
+                $table->dropUnique('price_indices_product_id_customer_group_id_channel_id_unique');
+            }
+
+            if (Schema::hasIndex('product_price_indices', 'ppi_product_id_customer_group_id_idx')) {
+                $table->dropIndex('ppi_product_id_customer_group_id_idx');
+            }
+
+            // Rename column
+            $table->renameColumn('product_id', 'priceable_id');
+        });
+
+        Schema::table('product_price_indices', function (Blueprint $table) {
+            // Add morph type column
+            $table->string('priceable_type')->after('priceable_id')->default('');
+
+            // New indexes
+            $table->unique(
+                ['priceable_id', 'priceable_type', 'customer_group_id', 'channel_id'],
+                'ppi_priceable_group_channel_unique'
+            );
+            $table->index(
+                ['priceable_type', 'priceable_id'],
+                'ppi_priceable_morph_idx'
+            );
+        });
+
+        // Backfill existing rows
+        DB::table('product_price_indices')
+            ->where('priceable_type', '')
+            ->update(['priceable_type' => 'Webkul\\Product\\Models\\Product']);
+
+        // Remove default after backfill
+        Schema::table('product_price_indices', function (Blueprint $table) {
+            $table->string('priceable_type')->default(null)->change();
+        });
+
+        // ── product_customer_group_prices ──────────────────────────
+
+        Schema::table('product_customer_group_prices', function (Blueprint $table) {
+            $table->dropForeign(['product_id']);
+
+            $table->renameColumn('product_id', 'priceable_id');
+        });
+
+        Schema::table('product_customer_group_prices', function (Blueprint $table) {
+            $table->string('priceable_type')->after('priceable_id')->default('');
+
+            $table->index(
+                ['priceable_type', 'priceable_id'],
+                'pcgp_priceable_morph_idx'
+            );
+        });
+
+        // Backfill
+        DB::table('product_customer_group_prices')
+            ->where('priceable_type', '')
+            ->update(['priceable_type' => 'Webkul\\Product\\Models\\Product']);
+
+        Schema::table('product_customer_group_prices', function (Blueprint $table) {
+            $table->string('priceable_type')->default(null)->change();
+        });
+
+        // Re-generate unique_id to include priceable_type
+        DB::table('product_customer_group_prices')->update([
+            'unique_id' => DB::raw("CONCAT_WS('|', qty, priceable_id, priceable_type, customer_group_id)"),
+        ]);
+    }
+
+    public function down(): void
+    {
+        // ── product_price_indices ──────────────────────────────────
+
+        Schema::table('product_price_indices', function (Blueprint $table) {
+            $table->dropUnique('ppi_priceable_group_channel_unique');
+            $table->dropIndex('ppi_priceable_morph_idx');
+            $table->dropColumn('priceable_type');
+            $table->renameColumn('priceable_id', 'product_id');
+        });
+
+        Schema::table('product_price_indices', function (Blueprint $table) {
+            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
+            $table->unique(
+                ['product_id', 'customer_group_id', 'channel_id'],
+                'price_indices_product_id_customer_group_id_channel_id_unique'
+            );
+            $table->index(
+                ['product_id', 'customer_group_id'],
+                'ppi_product_id_customer_group_id_idx'
+            );
+        });
+
+        // ── product_customer_group_prices ──────────────────────────
+
+        Schema::table('product_customer_group_prices', function (Blueprint $table) {
+            $table->dropIndex('pcgp_priceable_morph_idx');
+            $table->dropColumn('priceable_type');
+            $table->renameColumn('priceable_id', 'product_id');
+        });
+
+        Schema::table('product_customer_group_prices', function (Blueprint $table) {
+            $table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
+        });
+
+        // Restore unique_id format
+        DB::table('product_customer_group_prices')->update([
+            'unique_id' => DB::raw("CONCAT_WS('|', qty, product_id, customer_group_id)"),
+        ]);
+    }
+};