<?php

declare(strict_types=1);

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;
use Hyperf\DbConnection\Db;

return new class extends Migration
{
    public function up(): void
    {
        Schema::create('aggregate_refresh_queue', function (Blueprint $table) {
            $table->date('refresh_date')->comment('待刷新的聚合日');
            $table->string('aggregate_view', 100)->comment('视图名（orders_daily_by_created / orders_daily_by_paid）');
            $table->timestampTz('created_at')->useCurrent()->comment('入队时间');

            $table->primary(['refresh_date', 'aggregate_view'], 'pk_aggregate_refresh_queue');
            $table->index('created_at', 'idx_aggregate_refresh_queue_created_at');
        });

        // Hyperf PostgresGrammar::compilePrimary 丢弃自定义 index 名（vendor/hyperf/database-pgsql/.../PostgresGrammar.php:230），
        // PG 默认用 {table}_pkey；这里显式重命名以满足计划验收要求（名称 pk_aggregate_refresh_queue）
        Db::statement('ALTER TABLE aggregate_refresh_queue RENAME CONSTRAINT aggregate_refresh_queue_pkey TO pk_aggregate_refresh_queue');
    }

    public function down(): void
    {
        Schema::dropIfExists('aggregate_refresh_queue');
    }
};
