<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Migrations\Migration;

return new class extends Migration
{
    /**
     * Run the migrations.
     *
     * 修复 products 表唯一索引：添加 NULLS NOT DISTINCT
     * 解决 platform_model_id 为 NULL 时唯一约束不生效的问题（PostgreSQL 15+）
     */
    public function up(): void
    {
        Schema::getConnection()->statement('ALTER TABLE products DROP CONSTRAINT products_store_item_model_unique');
        Schema::getConnection()->statement('CREATE UNIQUE INDEX products_store_item_model_unique ON products (store_id, platform_item_id, platform_model_id) NULLS NOT DISTINCT');
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::getConnection()->statement('DROP INDEX IF EXISTS products_store_item_model_unique');
        Schema::getConnection()->statement('ALTER TABLE products ADD CONSTRAINT products_store_item_model_unique UNIQUE (store_id, platform_item_id, platform_model_id)');
    }
};
