-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The data fixtures can be loaded into the database using the `application:fixtures:load` command. All existing records are `TRUNCATE`d from the database to ensure a clean start.
- Loading branch information
Showing
9 changed files
with
477 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
24 changes: 24 additions & 0 deletions
24
module/Application/src/Command/Factory/LoadFixturesFactory.php
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Application\Command\Factory; | ||
|
||
use Application\Command\LoadFixtures; | ||
use Doctrine\ORM\EntityManager; | ||
use Laminas\ServiceManager\Factory\FactoryInterface; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class LoadFixturesFactory implements FactoryInterface | ||
{ | ||
public function __invoke( | ||
Check failure on line 14 in module/Application/src/Command/Factory/LoadFixturesFactory.php GitHub Actions / PHPStan
|
||
ContainerInterface $container, | ||
string $requestedName, | ||
Check failure on line 16 in module/Application/src/Command/Factory/LoadFixturesFactory.php GitHub Actions / PsalmMethodSignatureMismatch
|
||
?array $options = null, | ||
): LoadFixtures { | ||
/** @var EntityManager $entityManager */ | ||
$entityManager = $container->get('doctrine.entitymanager.orm_default'); | ||
|
||
return new LoadFixtures($entityManager); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Application\Command; | ||
|
||
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; | ||
use Doctrine\Common\DataFixtures\Loader; | ||
use Doctrine\Common\DataFixtures\Purger\ORMPurger; | ||
use Doctrine\ORM\EntityManager; | ||
use Symfony\Component\Console\Attribute\AsCommand; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Throwable; | ||
|
||
#[AsCommand( | ||
name: 'application:fixtures:load', | ||
description: 'Seed the database with data fixtures.', | ||
)] | ||
class LoadFixtures extends Command | ||
{ | ||
private const array FIXTURES = [ | ||
// './module/Activity/test/Seeder', | ||
// './module/Company/test/Seeder', | ||
'./module/Decision/test/Seeder', | ||
// './module/Education/test/Seeder', | ||
// './module/Frontpage/test/Seeder', | ||
// './module/Photo/test/Seeder', | ||
'./module/User/test/Seeder', | ||
]; | ||
|
||
public function __construct(private readonly EntityManager $entityManager) | ||
{ | ||
parent::__construct(); | ||
} | ||
|
||
protected function execute( | ||
InputInterface $input, | ||
OutputInterface $output, | ||
): int { | ||
$loader = new Loader(); | ||
$purger = new ORMPurger(); | ||
$purger->setPurgeMode(ORMPurger::PURGE_MODE_TRUNCATE); | ||
$executor = new ORMExecutor($this->entityManager, $purger); | ||
|
||
foreach ($this::FIXTURES as $fixture) { | ||
$loader->loadFromDirectory($fixture); | ||
} | ||
|
||
$output->writeln('<info>Loading fixtures into the database...</info>'); | ||
|
||
$connection = $this->entityManager->getConnection(); | ||
try { | ||
// Temporarily disable FK constraint checks. This is necessary because large parts of our database do not have | ||
// explicit CASCADEs set to prevent data loss when syncing with ReportDB (GEWISDB). | ||
// The try-catch is necessary to hide some error messages (because the executeStatement). | ||
$connection->executeStatement('SET FOREIGN_KEY_CHECKS = 0'); | ||
$executor->execute($loader->getFixtures()); | ||
$connection->executeStatement('SET FOREIGN_KEY_CHECKS = 1'); | ||
} catch (Throwable) { | ||
} | ||
|
||
$output->writeln('<info>Loaded fixtures!</info>'); | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains 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
This file contains 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
Oops, something went wrong.