Some checks failed
CI / PHP 8.3 / mariadb:10.6 (pull_request) Successful in 7m50s
CI / PHP 8.4 / mariadb:10.6 (pull_request) Successful in 7m34s
CI / PHP 8.3 / mariadb:11.4 (pull_request) Successful in 5m45s
CI / PHP 8.4 / mariadb:11.4 (pull_request) Successful in 5m30s
CI / PHP 8.3 / mysql:8.0 (pull_request) Failing after 7m7s
CI / PHP 8.4 / mysql:8.0 (pull_request) Failing after 7m9s
CI / PHP 8.3 / mysql:8.4 (pull_request) Failing after 6m8s
CI / PHP 8.4 / mysql:8.4 (pull_request) Failing after 6m5s
CI / Bekannte Sicherheitslücken in Paketen (pull_request) Successful in 48s
CI / Build-Artefakte aktuell (pull_request) Successful in 1m5s
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
74 lines
2.4 KiB
PHP
74 lines
2.4 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Middleware;
|
||
|
||
use App\Support\Installation;
|
||
use App\Support\Update\MaintenanceFlag;
|
||
use App\Support\Update\Migrations;
|
||
use Closure;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\Log;
|
||
use Symfony\Component\HttpFoundation\Response;
|
||
use Throwable;
|
||
|
||
/**
|
||
* Führt nach einem Update beim ersten Seitenaufruf ausstehende Migrationen aus –
|
||
* ohne SSH der einzige verlässliche Zeitpunkt.
|
||
*
|
||
* Migriert wird nur, wenn sich der Fingerabdruck der Migrationsdateien geändert
|
||
* hat (sonst liefe der Migrator bei jedem Request), mit einer Sperre gegen
|
||
* Parallellauf. Fehler werden protokolliert, nie geworfen.
|
||
*/
|
||
class RunPendingMigrations
|
||
{
|
||
public function __construct(private readonly Migrations $migrations) {}
|
||
|
||
public function handle(Request $request, Closure $next): Response
|
||
{
|
||
// Nicht während eines laufenden Updates – dann sind die Dateien womöglich erst halb kopiert
|
||
if (app()->runningUnitTests() || ! Installation::isInstalled() || app(MaintenanceFlag::class)->isActive()) {
|
||
return $next($request);
|
||
}
|
||
|
||
$stored = @file_get_contents(storage_path('.migrations-fingerprint'));
|
||
|
||
try {
|
||
$current = $this->migrations->fingerprint();
|
||
|
||
if ($stored !== $current) {
|
||
$this->migrateWithLock($current);
|
||
}
|
||
} catch (Throwable $e) {
|
||
Log::error('Automatische Migration fehlgeschlagen: '.$e->getMessage(), ['exception' => $e]);
|
||
}
|
||
|
||
return $next($request);
|
||
}
|
||
|
||
private function migrateWithLock(string $fingerprint): void
|
||
{
|
||
$lock = @fopen(storage_path('.migrate.lock'), 'c');
|
||
|
||
if ($lock === false || ! flock($lock, LOCK_EX | LOCK_NB)) {
|
||
return; // Läuft gerade in einer anderen Anfrage.
|
||
}
|
||
|
||
try {
|
||
// Nach dem Warten auf die Sperre erneut prüfen – vielleicht war jemand schneller.
|
||
if (@file_get_contents(storage_path('.migrations-fingerprint')) === $fingerprint) {
|
||
return;
|
||
}
|
||
|
||
@set_time_limit(300);
|
||
$count = $this->migrations->run();
|
||
file_put_contents(storage_path('.migrations-fingerprint'), $fingerprint);
|
||
|
||
if ($count > 0) {
|
||
Log::info("Automatische Migration: {$count} Migration(en) ausgeführt.");
|
||
}
|
||
} finally {
|
||
flock($lock, LOCK_UN);
|
||
fclose($lock);
|
||
}
|
||
}
|
||
}
|