From 3ab03e2c7a9e60f07d4c3c409e9d98338a32e4e7 Mon Sep 17 00:00:00 2001 From: Pontoporeia Date: Thu, 10 Sep 2026 18:11:41 +0200 Subject: [PATCH] fix(migrations): load composer autoloader so run.php works on a fresh DB run.php never loaded vendor/autoload.php. SQL migrations were fine, but PHP migrations run as isolated subprocesses that require src/ files directly, and those files reference sibling classes (e.g. Database -> DatabaseMigrations) resolvable only via composer's classmap. On a fresh schema.sql database the run died at 013_fix_remarks_keywords.php with: Class "DatabaseMigrations" not found in app/src/Database.php:89 - run.php now resolves and requires the autoloader (vendor/ is a sibling dir in dev, same-dir in prod, matching app/bootstrap.php). - Subprocess PHP migrations get it injected via -d auto_prepend_file so each migration's own contract is untouched ($argv[1] stays the DB path). Verified: fresh schema.sql DB applies 47 migrations, exit 0; second run is a no-op (0 applied). --- TODO.md | 3 +-- app/migrations/run.php | 40 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 4 deletions(-) diff --git a/TODO.md b/TODO.md index fc67332..0177002 100644 --- a/TODO.md +++ b/TODO.md @@ -65,6 +65,7 @@ - [x] #extend-smoke-test-for Extend smoke test for rotation and 4h idle boundary - [x] #fix-14-biome-check Fix 14 biome check errors: unsorted imports in scripts/css-*.mjs + unformatted CSS/JS files - [x] #fix-peertube-resumable-chunked-upload [!high] fix(peertube): resumable chunked upload to stop 504 on large A/V — Admin MP4 upload fails at 100%: PHP relays whole file to PeerTube via one blocking multipart POST; PeerTube edge nginx returns 504 Gateway Time-out (see xamxam-error log filepond_peertube). Migrate PeerTubeService::upload() to resumable upload protocol with chunked PATCH so no single request exceeds the proxy timeout. +- [x] #fix-migrations-run-php-not-loading Fix migrations/run.php not loading composer autoloader (fresh-DB failure) — run.php loaded no autoloader; PHP subprocess migrations require src/ files whose sibling classes (Database->DatabaseMigrations) only resolve via composer classmap. Loaded autoloader in run.php and injected into subprocesses via -d auto_prepend_file, preserving $argv[1]=db contract. Verified: fresh schema.sql DB applies 47 migrations, exit 0, idempotent on 2nd run. ## Deferred / Blocked - [ ] #just-setup-backs-a [!medium] just setup backs a stale setup-dev.sh (clones php-live-reload, legacy admin/data/ dirs) — needs rewrite or removal @@ -75,5 +76,3 @@ - [ ] #wire-templates-to-load [!medium] Wire templates to load their page-type bundle instead of base.min.css — Update each template's tags to point at the appropriate per-page bundle. Ensure no page loads CSS it doesn't need and that shared pages still get full styling. - [ ] #measure-before-after-css-bytes [!low] Measure before/after CSS bytes shipped per page type — Moot: part of split stream which is NO-GO (task 14). Skip unless split is revived. - [ ] #add-lightningcss-unusedsymbols-report-only [!high] Add lightningcss unusedSymbols report-only pass for base.min.css — SEQUENCING: do this (unusedSymbols report-only) BEFORE the per-page split (u). Reasoning: high priority, zero-risk diagnostic-only, produces reclaimable-byte go/no-go (14) against ~216KB baseline; if pruning is not worthwhile the split may not be either. Reuses inventory from v (docs/css-split-analysis.md). Start: 11 collect-content-sources. - -- [x] #trim-deploy-code-comments Remove oversized comment blocks from deploy-code/deploy-permissions recipes in justfile. diff --git a/app/migrations/run.php b/app/migrations/run.php index 321c4d9..6cfdf13 100644 --- a/app/migrations/run.php +++ b/app/migrations/run.php @@ -14,6 +14,33 @@ $root = dirname(__DIR__); $dbPath = $argv[1] ?? ($root . '/storage/xamxam.db'); +// Load the composer autoloader (classmap over src/). PHP migrations require +// src/ files directly, and those files reference sibling classes +// (e.g. Database -> DatabaseMigrations) that are only resolvable via the +// autoloader. Without this, run.php dies on a fresh database. +// vendor/ is a sibling dir in dev (repo root) and same-dir in prod. +$autoloadCandidates = [ + dirname($root) . '/vendor/autoload.php', + $root . '/vendor/autoload.php', +]; +$autoloadPath = null; +foreach ($autoloadCandidates as $candidate) { + if (file_exists($candidate)) { + $autoloadPath = $candidate; + require_once $candidate; + break; + } +} + +// PHP migrations run as isolated subprocesses, so they do not inherit the +// autoloader above. Export the resolved path so run.php (and any code it +// invokes) can inject it into those subprocesses. +if ($autoloadPath !== null) { + putenv('XAMXAM_AUTOLOAD=' . $autoloadPath); + $_ENV['XAMXAM_AUTOLOAD'] = $autoloadPath; + $_SERVER['XAMXAM_AUTOLOAD'] = $autoloadPath; +} + if (!file_exists($dbPath)) { die("Database not found: $dbPath\n"); } @@ -68,9 +95,18 @@ foreach ($files as $name => $file) { try { if ($isPhp) { - // PHP migrations: execute in a subprocess for isolation + // PHP migrations: execute in a subprocess for isolation. + // The subprocess only requires the migration file, so composer's + // autoloader has to be loaded there too. -d auto_prepend_file + // injects it without altering the migration's own $argv contract + // ($argv[1] must remain the DB path). + $phpArgs = ''; + if ($autoloadPath !== null) { + $phpArgs = '-d auto_prepend_file=' . escapeshellarg($autoloadPath) . ' '; + } $cmd = sprintf( - 'php %s %s 2>&1', + 'php %s%s %s 2>&1', + $phpArgs, escapeshellarg($file), escapeshellarg($dbPath) );