-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #452 from tomudding/chore/initial-data-fixtures-fo…
…r-seeding-the-database chore: add initial seeder
- Loading branch information
Showing
10 changed files
with
307 additions
and
7 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
32 changes: 32 additions & 0 deletions
32
module/Application/src/Command/Factory/LoadFixturesCommandFactory.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,32 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Application\Command\Factory; | ||
|
||
use Application\Command\LoadFixturesCommand; | ||
use Doctrine\ORM\EntityManager; | ||
use Laminas\ServiceManager\Factory\FactoryInterface; | ||
use Psr\Container\ContainerInterface; | ||
|
||
class LoadFixturesCommandFactory implements FactoryInterface | ||
{ | ||
/** | ||
* @param string $requestedName | ||
*/ | ||
public function __invoke( | ||
ContainerInterface $container, | ||
$requestedName, | ||
?array $options = null, | ||
): LoadFixturesCommand { | ||
/** @var EntityManager $databaseEntityManager */ | ||
$databaseEntityManager = $container->get('doctrine.entitymanager.orm_default'); | ||
/** @var EntityManager $reportEntityManager */ | ||
$reportEntityManager = $container->get('doctrine.entitymanager.orm_report'); | ||
|
||
return new LoadFixturesCommand( | ||
$databaseEntityManager, | ||
$reportEntityManager, | ||
); | ||
} | ||
} |
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,80 @@ | ||
<?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 LoadFixturesCommand extends Command | ||
{ | ||
private const array DATABASE_FIXTURES = [ | ||
// './module/Database/test/Seeder', | ||
'./module/User/test/Seeder', | ||
]; | ||
|
||
private const array REPORT_FIXTURES = [ | ||
// './module/Report/test/Seeder', | ||
]; | ||
|
||
public function __construct( | ||
private readonly EntityManager $databaseEntityManager, | ||
private readonly EntityManager $reportEntityManager, | ||
) { | ||
parent::__construct(); | ||
} | ||
|
||
protected function execute( | ||
InputInterface $input, | ||
OutputInterface $output, | ||
): int { | ||
$output->setDecorated(true); | ||
|
||
$databaseLoader = new Loader(); | ||
$reportLoader = new Loader(); | ||
$purger = new ORMPurger(); | ||
$purger->setPurgeMode(ORMPurger::PURGE_MODE_TRUNCATE); | ||
$databaseExecutor = new ORMExecutor($this->databaseEntityManager, $purger); | ||
$reportExecutor = new ORMExecutor($this->reportEntityManager, $purger); | ||
|
||
foreach ($this::DATABASE_FIXTURES as $fixture) { | ||
$databaseLoader->loadFromDirectory($fixture); | ||
} | ||
|
||
foreach ($this::REPORT_FIXTURES as $fixture) { | ||
$reportLoader->loadFromDirectory($fixture); | ||
} | ||
|
||
$output->writeln('<info>Loading fixtures into the database...</info>'); | ||
|
||
$databaseConnection = $this->databaseEntityManager->getConnection(); | ||
$reportConnection = $this->reportEntityManager->getConnection(); | ||
try { | ||
// Temporarily disable FK constraint checks. | ||
// The try-catch is necessary to hide some error messages (because the executeStatement). | ||
$databaseConnection->executeStatement('SET session_replication_role = \'replica\''); | ||
$databaseExecutor->execute($databaseLoader->getFixtures()); | ||
$databaseConnection->executeStatement('SET session_replication_role = \'origin\''); | ||
$reportConnection->executeStatement('SET session_replication_role = \'replica\''); | ||
$reportExecutor->execute($reportLoader->getFixtures()); | ||
$reportConnection->executeStatement('SET session_replication_role = \'origin\''); | ||
} 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
Oops, something went wrong.