-
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.
chore(seeder): add Member and User seeders
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. This also fixes some inconsistencies in the (sub)decision model with GEWISDB, somehow the possibility for these to be `null` got lost somewhere (and fixes for initialisation of `Collection`s). --- 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 #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
13 changed files
with
540 additions
and
8 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
27 changes: 27 additions & 0 deletions
27
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,27 @@ | ||
<?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 | ||
{ | ||
/** | ||
* @param string $requestedName | ||
*/ | ||
public function __invoke( | ||
ContainerInterface $container, | ||
$requestedName, | ||
?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
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
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,44 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace DecisionTest\Seeder; | ||
|
||
use DateTime; | ||
use Decision\Model\AssociationYear; | ||
use Decision\Model\Enums\MeetingTypes; | ||
use Decision\Model\Meeting; | ||
use Doctrine\Common\DataFixtures\AbstractFixture; | ||
use Doctrine\Persistence\ObjectManager; | ||
|
||
use function range; | ||
|
||
class MeetingFixture extends AbstractFixture | ||
{ | ||
public function load(ObjectManager $manager): void | ||
{ | ||
$today = new DateTime(); | ||
|
||
foreach (MeetingTypes::cases() as $meetingType) { | ||
foreach (range(0, 3) as $meetingNumber) { | ||
$meeting = new Meeting(); | ||
$meeting->setType($meetingType); | ||
$meeting->setNumber($meetingNumber); | ||
|
||
// 2 meetings in the past, 1 today, and 1 in the future. | ||
if (3 > $meetingNumber) { | ||
$meetingDate = (clone $today)->modify('-' . (2 - $meetingNumber) . ' days'); | ||
} else { | ||
$meetingDate = AssociationYear::fromDate($today)->getEndDate(); | ||
} | ||
|
||
$meeting->setDate($meetingDate); | ||
|
||
$manager->persist($meeting); | ||
$this->addReference('meeting-' . $meetingType->value . '-' . $meetingNumber, $meeting); | ||
} | ||
|
||
$manager->flush(); | ||
} | ||
} | ||
} |
Oops, something went wrong.