-
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.
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. --- Unfortunately, adding the data fixtures for (sub)decisions has proved to be quite difficult. As such, these have been removed. The WIP can be found in GEWIS/gewisweb#1913. There is an issue with the "hydration" of the entities when they are added to the database. I have not seen this issue in GEWISDB, but the cause appears to be the usage of `BackedEnum`s as part of a composite key (which forms the foundation for our (sub)decision entities and relations). Either the enum cannot be cast to string while being saved to the database. Or when using custom mapping types (see the PR mentioned) above the value cannot be properly restored from the database. The latter can then also be fixed with another patch for ORM (see GEWIS/orm@8031547), however, this may break other things. This patch can probably also be applied in reverse, such that we do not need the custom mapping types. However, this has not (yet) been tested. As such, this has to be investigated more and potentially a bug report must be submitted to Doctrine ORM to get this fixed.
- Loading branch information
Showing
7 changed files
with
154 additions
and
4 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
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
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 UserTest\Seeder; | ||
|
||
use Doctrine\Common\DataFixtures\AbstractFixture; | ||
use Doctrine\Persistence\ObjectManager; | ||
use User\Model\User; | ||
|
||
class UserFixture extends AbstractFixture | ||
{ | ||
public function load(ObjectManager $manager): void | ||
{ | ||
$user = new User(); | ||
$user->setLogin('admin'); | ||
$user->setPassword('$2y$13$smUYvCkgowlfHOFrogwcPONGDFmcylKHmTOZQAks9cDvs15tPxR2a'); // == gewisdbgewis | ||
|
||
$manager->persist($user); | ||
$this->addReference('user-admin', $user); | ||
|
||
$manager->flush(); | ||
} | ||
} |
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