diff --git a/saas-app/public/admin.php b/saas-app/public/admin.php index 6db4c3b..9ee1a0f 100644 --- a/saas-app/public/admin.php +++ b/saas-app/public/admin.php @@ -34,6 +34,10 @@ function admin_update_summary(array $items): array foreach ($items as $item) { $status = (string) ($item['status'] ?? 'pending'); + if ($status === 'baselined') { + $status = 'success'; + } + if (array_key_exists($status, $summary)) { $summary[$status]++; } @@ -79,6 +83,7 @@ $page = match ($path) { '/admin/login' => 'login', '/admin/tenants' => 'tenants', '/admin/updates' => 'updates', + '/admin/migration' => 'migration', '/admin/logout' => 'logout', default => 'login', }; @@ -94,6 +99,8 @@ $summaryMetrics = []; $licensePlans = []; $updateItems = []; $updateSummary = ['pending' => 0, 'success' => 0, 'failed' => 0, 'running' => 0]; +$migrationItems = []; +$migrationSummary = ['pending' => 0, 'success' => 0, 'failed' => 0, 'running' => 0]; $appVersion = is_file(dirname(__DIR__) . '/version.php') ? (string) require dirname(__DIR__) . '/version.php' : 'unbekannt'; if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST' && $page === 'logout') { @@ -116,7 +123,7 @@ if ($page === 'login' && $pdo instanceof PDO) { $admin = app_admin_user(); } -if (in_array($page, ['overview', 'tenants', 'updates'], true)) { +if (in_array($page, ['overview', 'tenants', 'updates', 'migration'], true)) { $admin = app_require_platform_admin(); if (!$pdo instanceof PDO) { @@ -124,7 +131,7 @@ if (in_array($page, ['overview', 'tenants', 'updates'], true)) { } } -if (in_array($page, ['overview', 'tenants', 'updates'], true) && $pdo instanceof PDO) { +if (in_array($page, ['overview', 'tenants', 'updates', 'migration'], true) && $pdo instanceof PDO) { if (($_SERVER['REQUEST_METHOD'] ?? 'GET') === 'POST' && !hash_equals($_SESSION['admin_csrf'], (string) ($_POST['csrf'] ?? ''))) { app_flash('Die Sitzung ist abgelaufen. Bitte lade die Seite neu.', 'error'); app_redirect('/admin/' . ($page === 'overview' ? '' : $page . '/')); @@ -155,10 +162,33 @@ if (in_array($page, ['overview', 'tenants', 'updates'], true) && $pdo instanceof app_redirect('/admin/updates/'); } + if ($page === 'migration' && (string) ($_POST['action'] ?? '') === 'run-migrations') { + try { + $results = scripts_run_pending_migrations( + scripts_update_config_from_env(app_env()), + 'admin:' . (string) ($admin['email'] ?? 'platform') + ); + + $successful = count(array_filter($results, static fn(array $result): bool => in_array(($result['status'] ?? ''), ['success', 'baselined'], true))); + $skipped = count(array_filter($results, static fn(array $result): bool => ($result['status'] ?? '') === 'skipped')); + + app_flash( + $successful . ' Migration(en) verarbeitet, ' . $skipped . ' bereits erledigt.', + 'success' + ); + } catch (Throwable $exception) { + app_flash('Die Migrationen konnten nicht abgeschlossen werden: ' . $exception->getMessage(), 'error'); + } + + app_redirect('/admin/migration/'); + } + $tenants = app_admin_tenant_list($pdo); $licensePlans = app_license_plan_options($pdo); $updateItems = scripts_list_updates_status(scripts_update_config_from_env(app_env())); $updateSummary = admin_update_summary($updateItems); + $migrationItems = scripts_list_migrations_status(scripts_update_config_from_env(app_env())); + $migrationSummary = admin_update_summary($migrationItems); $summaryMetrics = admin_summary_metrics($tenants, $updateItems); if ($page === 'tenants' && isset($_GET['edit']) && $_GET['edit'] !== '') { @@ -211,6 +241,7 @@ SQL, Übersicht Mandanten Updates + Migrationen