<?php

declare(strict_types=1);

require_once __DIR__ . '/support.php';

$options = scripts_parse_options($argv);
$env = scripts_read_env_file(scripts_env_path());
$connection = scripts_normalize_db_connection($options['connection'] ?? $env['DB_CONNECTION'] ?? 'mysql');

$config = [
    'connection' => $connection,
    'server' => $options['server'] ?? $env['DB_HOST'] ?? null,
    'database' => $options['database'] ?? $env['DB_DATABASE'] ?? null,
    'port' => $options['port'] ?? $env['DB_PORT'] ?? scripts_default_db_port($connection),
    'username' => $options['username'] ?? $env['DB_USERNAME'] ?? null,
    'password' => $options['password'] ?? $env['DB_PASSWORD'] ?? null,
];

try {
    scripts_run_sql_migrations($config);
    scripts_stdout('Migrationen fuer ' . scripts_connection_label($connection) . ' wurden erfolgreich ausgefuehrt.');
} catch (Throwable $exception) {
    scripts_stderr('Migration fehlgeschlagen: ' . $exception->getMessage());
}
