<?php

declare(strict_types=1);

/**
 * Allow cancelling a running job after current chunk.
 *
 * @return callable(PDO, string): void
 */
return static function (\PDO $pdo, string $prefix): void {
    if (!preg_match('/^[a-zA-Z0-9_]+$/', $prefix)) {
        throw new InvalidArgumentException('Invalid table prefix');
    }

    $t = '`' . $prefix . 'sync_jobs`';
    try { $pdo->exec("ALTER TABLE {$t} ADD COLUMN `cancel_requested` tinyint(1) NOT NULL DEFAULT 0 AFTER `next_run_at`"); } catch (\Throwable) {}
    try { $pdo->exec("ALTER TABLE {$t} ADD KEY `idx_user_cancel` (`user_id`, `cancel_requested`)"); } catch (\Throwable) {}
};

