feat(laravel): boot without a database via dumped metadata#8290
Merged
Conversation
d3dee6c to
602b7eb
Compare
602b7eb to
62b74ce
Compare
soyuka
added a commit
to soyuka/core
that referenced
this pull request
Jun 13, 2026
The dumped metadata is a frozen snapshot served outermost (above the cache) when APP_DEBUG is false, so a changed resource or migrated schema is served stale with no signal. Detect both drift axes, warn-only, without breaking the no-database boot the dump provides: - resource drift: hash the ApiResource source files (content, not mtime, so committed/baked dumps survive git clone and docker build) and warn at boot when they differ from the dumped fingerprint; - schema drift: hash the live Eloquent schema and warn on MigrationsEnded when it differs (DB only reachable then). The dump file gains a versioned envelope carrying both fingerprints; bare-map dumps from the previous format still load without a warning. Refs api-platform#8131, api-platform#8290
soyuka
added a commit
to soyuka/core
that referenced
this pull request
Jun 16, 2026
The dumped metadata is a frozen snapshot served outermost (above the cache) when APP_DEBUG is false, so a changed resource or migrated schema is served stale with no signal. Detect both drift axes, warn-only, without breaking the no-database boot the dump provides: - resource drift: hash the ApiResource source files (content, not mtime, so committed/baked dumps survive git clone and docker build) and warn at boot when they differ from the dumped fingerprint; - schema drift: hash the live Eloquent schema and warn on MigrationsEnded when it differs (DB only reachable then). The dump file gains a versioned envelope carrying both fingerprints; bare-map dumps from the previous format still load without a warning. The dump command no longer unwraps the resolved factory at runtime to reach the live source: the source metadata chain is bound under its own container key, the interface everyone injects resolves to the dumped wrapper, and the command receives the source chain via a contextual binding. Removes the DumpedResourceCollectionMetadataFactory::getDecorated() seam and guarantees the command cannot read back its own dump. Refs api-platform#8131, api-platform#8290
5604b09 to
df9fbad
Compare
Add `php artisan api-platform:metadata:dump`, which introspects the Eloquent schema once and writes the per-model attribute and relation metadata to a file. When `api-platform.metadata_dump` points at that file and APP_DEBUG is false, ModelMetadata is seeded from it at boot, so the resource metadata chain builds without a database — e.g. during `docker build`, `composer install` or static analysis in CI. ModelMetadata is the only boot-time component that touches the database, so caching it alone lets the whole chain boot offline while every factory stays live. The dump holds plain attribute and relation arrays (scalars and class-strings), read back with allowed_classes disabled, so it can never carry a Closure and adds no object-injection surface. It is written via a temporary file and renamed into place so a concurrent boot never reads a half-written file.
df9fbad to
873e5d8
Compare
soyuka
added a commit
to soyuka/core
that referenced
this pull request
Jun 17, 2026
The metadata-dump tests added in api-platform#8290 trip larastan: stubResourceClasses is typed list<class-string> yet intentionally receives a non-model class name, readDump returns the untyped result of unserialize(), and the Log::spy()/shouldHaveReceived() spy chain is not statically resolvable. Loosen the stub to list<string>, validate and reshape the dump payload, and assert the stale-fingerprint warning with Log::shouldReceive() as the rest of the suite already does.
soyuka
added a commit
to soyuka/core
that referenced
this pull request
Jun 19, 2026
The metadata-dump tests added in api-platform#8290 trip larastan: stubResourceClasses is typed list<class-string> yet intentionally receives a non-model class name, readDump returns the untyped result of unserialize(), and the Log::spy()/shouldHaveReceived() spy chain is not statically resolvable. Loosen the stub to list<string>, validate and reshape the dump payload, and assert the stale-fingerprint warning with Log::shouldReceive() as the rest of the suite already does.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
To expose an Eloquent model, API Platform reads its metadata (columns, types, relations) from the live database schema (
ModelMetadata::getAttributes()→getColumns()/getIndexes()). That introspection runs while theResourceMetadataCollectionis built, and that build happens at boot:src/Laravel/routes/api.phpiterates every resource to register routes.So the app cannot boot when no migrated database is reachable. This breaks common cacheless boots:
docker build(DB service not running);composer install→@php artisan package:discover;The existing metadata cache only short-circuits the DB read if it was already warmed — and warming needs a boot with the DB up; the configured store may also be redis/memcached (unavailable at build, not committable).
Refs #8131.
Solution
Pre-compute the schema metadata once, with the DB up, dump it to a single file that can be committed to git or baked into a Docker image, and seed it back at boot — bypassing the DB.
api-platform:metadata:dump {--path=}— resolves the path from--pathor theapi-platform.metadata_dumpconfig, iteratesResourceNameCollectionFactoryInterface, and writes the per-model attribute and relation arrays (ModelMetadata::getAttributes()/getRelations()— the step that hits the DB, run it with the DB up). Written via a temporary file + atomicrename().ModelMetadataseeding — its constructor gains optionalattributes/relationsarguments that seed its existing per-class caches. At boot, whenapi-platform.metadata_dumppoints at an existing file andAPP_DEBUGis false, the container buildsModelMetadatapre-seeded from the dump. Skipped whenAPP_DEBUGis true so local dev always introspects fresh.api-platform.metadata_dumpdefaults tonull(feature disabled); the user explicitly chooses a VCS/image-committable path.Why cache
ModelMetadatarather than the resource collectionModelMetadata::getAttributes()is the only boot-time component that touches the database; the whole property/metadata-build chain (EloquentPropertyNameCollectionMetadataFactory,EloquentPropertyMetadataFactory,RelationMetadataLoader,EloquentExtractor,ParameterResourceMetadataCollectionFactory) reaches the DB only through it. Caching that one layer lets the entire resource-metadata chain boot offline while every factory stays live.Because the dump then holds only plain attribute/relation arrays (scalars and class-strings):
unserialize(..., ['allowed_classes' => false])— no object-injection surface;Closure, so a closureprovider/processoron a resource cannot breakserialize();ModelMetadatavia a contextual binding, so it never reads back its own dump.The resource-metadata factory chain is left completely untouched (no container-binding rename, no decorator).
Usage
Re-run the command whenever your models change.
Tests
New tests, runnable without a database:
Tests/Console/DumpMetadataCommandTest.php— dumps the attribute/relation map; no-path failure; non-Model resources skipped.Tests/Metadata/DumpedMetadataBootTest.php— a seededModelMetadataserves dumped data without DB introspection; bypassed whenapp.debug=true.Tests/Unit/Metadata/ModelMetadataTest.php— the seeding contract.