<?php
/**
 * 执行 dice_play_record 备注字段迁移
 * 用法：在 server 目录执行 php db/run_dice_play_record_add_remark.php
 */
declare(strict_types=1);

define('BASE_PATH', dirname(__DIR__));

require_once BASE_PATH . '/vendor/autoload.php';

if (class_exists(\Dotenv\Dotenv::class) && is_file(BASE_PATH . '/.env')) {
    if (method_exists(\Dotenv\Dotenv::class, 'createUnsafeMutable')) {
        \Dotenv\Dotenv::createUnsafeMutable(BASE_PATH)->load();
    } else {
        \Dotenv\Dotenv::createMutable(BASE_PATH)->load();
    }
}

$host = getenv('DB_HOST') ?: '127.0.0.1';
$port = getenv('DB_PORT') ?: '3306';
$db = getenv('DB_NAME') ?: '';
$user = getenv('DB_USER') ?: '';
$pass = getenv('DB_PASSWORD') ?: '';
$dsn = "mysql:host={$host};port={$port};dbname={$db};charset=utf8mb4";
$pdo = new PDO($dsn, $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);

$sqlFile = __DIR__ . '/dice_play_record_add_remark.sql';
$sql = file_get_contents($sqlFile);
$sql = preg_replace('/--.*$/m', '', $sql);
$parts = array_filter(array_map('trim', explode(';', $sql)));

echo "执行: dice_play_record_add_remark.sql\n";
echo "数据库: {$db} @ {$host}\n\n";

foreach ($parts as $statement) {
    if ($statement === '') {
        continue;
    }
    $pdo->exec($statement);
    echo "OK\n";
}

echo "\n完成。\n";
