artack / fixtures-guard
Symfony bundle that stops Doctrine fixtures and Foundry from wiping a database whose data must stay
Package info
github.com/artack/fixtures-guard
Type:symfony-bundle
pkg:composer/artack/fixtures-guard
Requires
- php: >=8.5
- symfony/config: ^7.4 || ^8.0
- symfony/console: ^7.4 || ^8.0
- symfony/dependency-injection: ^7.4 || ^8.0
- symfony/event-dispatcher: ^7.4 || ^8.0
- symfony/http-kernel: ^7.4 || ^8.0
Requires (Dev)
- doctrine/doctrine-bundle: ^2.18 || ^3.0
- doctrine/doctrine-fixtures-bundle: ^4.1
- doctrine/orm: ^3.5
- phpunit/phpunit: ^13.2
- symfony/filesystem: ^7.4 || ^8.0
- symfony/framework-bundle: ^7.4 || ^8.0
- zenstruck/foundry: ^2.8
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Symfony bundle that stops Doctrine fixtures and Foundry from wiping a database whose data must stay.
Developed by ARTACK WebLab GmbH in Zurich, Switzerland.
Why
doctrine:fixtures:load and Foundry's foundry:load-fixtures empty the database before they load. As long as the fixture bundles are dev dependencies, production servers don't have these commands. Once a server rebuilds its database from fixtures, for instance a staging server that runs the prod kernel, the bundles move to require. From then on every server has the commands, production included, and one bin/console on the wrong server wipes its data.
This bundle stops these commands before they start, unless the server's stage allows them.
Features
- Stops the commands of the Doctrine fixtures bundle and Foundry wherever the data must stay, and lets them run locally and on the stages you name.
- Guards whole command namespaces (
doctrine:fixtures:*,foundry:*) by name and by alias, so a renamed command stays guarded. - Fails safe: without a stage, the commands are blocked rather than allowed.
- Throws an exception, so a blocked attempt shows up in your error tracking.
- Works without configuration.
- Requires PHP 8.5 or newer and Symfony 7.4 or 8.x.
Installation
Install the bundle via Composer:
$ composer require artack/fixtures-guard
⚠️ Require it in
require, not inrequire-dev: the bundle has to run on the servers it protects. Symfony Flex registers it inconfig/bundles.phpfor all environments. Installed with--dev, Flex registers it fordevandtestonly, and production stays unprotected.
Without Flex, register the bundle yourself:
// config/bundles.php return [ // ... Artack\FixturesGuard\FixturesGuardBundle::class => ['all' => true], ];
Then give every server its stage in the environment variable APP_STAGE, e.g. APP_STAGE=prod on production and APP_STAGE=stag on staging. Locally, APP_STAGE=dev in .env does.
How it decides
A guarded command runs if
- the stage is one of the resettable stages (default:
stag), or - the kernel environment and the stage are both
devortest.
Everywhere else the bundle throws Artack\FixturesGuard\Exception\FixturesLoadBlockedException before the command starts:
| Kernel environment | APP_STAGE |
Fixtures |
|---|---|---|
dev |
dev |
run |
test |
dev or test |
run |
prod |
stag |
run |
prod |
prod |
blocked |
prod |
dev, because the server misses its own |
blocked |
prod |
not set | blocked |
dev |
prod |
blocked |
Rule 2 needs both because .env usually ships APP_STAGE=dev with every release: a server process that misses its own stage reads dev, so the kernel environment has to agree.
artack/fixtures-guard stopped "foundry:load-fixtures": it replaces the database, which may only happen
locally (kernel environment and APP_STAGE both "dev" or "test") or where APP_STAGE is "stag".
Here the kernel environment is "prod" and APP_STAGE is "prod".
Configuration
The configuration is optional:
# config/packages/artack_fixtures_guard.yaml artack_fixtures_guard: stage_env: APP_STAGE # the environment variable that holds the stage resettable_stages: [stag] # stages whose database may be rebuilt; [] allows the fixtures only locally additional_commands: [] # exact names of further commands to guard
resettable_stagesreplaces the default. It must not containdevortest, see How it decides.additional_commandscan only add commands. The built-in namespaces always stay guarded.
Guarded commands
Every command whose name or one of its aliases starts with doctrine:fixtures: or foundry:, which today means:
doctrine:fixtures:loadof the Doctrine fixtures bundle,foundry:load-fixtureswith its aliasesfoundry:load-storiesandfoundry:load-story(Foundry 2.6.1 and newer), andfoundry:load-storiesof Foundry 2.6.0.
Abbreviations such as d:f:l are covered too: Symfony resolves them before the guard sees the command.
Guard your own commands with additional_commands. A project command that loads the fixtures by calling another command's run() needs it: that call skips the console events, so the guard only sees your command. Add a test that runs it with a production stage and expects FixturesLoadBlockedException, because a typo in the name goes unnoticed otherwise.
Limits
The bundle protects against mistakes, not against intent:
- It knows the commands by name. A fixture command outside the two namespaces needs
additional_commands. - It doesn't guard other commands that destroy data, such as
doctrine:schema:dropordoctrine:database:drop. You can add them toadditional_commands, but that is your project's decision and not a promise of this bundle. - It reads the stage from the environment, so
APP_STAGE=stag bin/console doctrine:fixtures:loadpasses on production. - It only guards the console. Code that empties the database outside a console command is not covered.
Development
All checks run in Docker with PHP 8.5 and 8.6:
$ make build # build the images $ make verify # coding standard, the test matrix, PHPStan and the coverage gate $ make help # all targets
make test runs PHPUnit with PHP 8.5 and 8.6, each with the lowest and the highest dependencies. make coverage fails below 100% line and method coverage. GitHub Actions runs the same test matrix plus php-cs-fixer and PHPStan; the coverage gate only runs locally.
License
This bundle is released under the MIT License.