-
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. --- Conclusion: too many issues with hydration of the enums that are part of our composite keys. I have tried adding a custom mapping type to resolve this issue. Unfortunately, that breaks it outside of seeding the database (so the whole website). --- 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).
- Loading branch information
Showing
20 changed files
with
926 additions
and
24 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
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; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
module/Application/src/Extensions/Doctrine/BackedEnumType.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,72 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Application\Extensions\Doctrine; | ||
|
||
use BackedEnum; | ||
use Doctrine\DBAL\Platforms\AbstractPlatform; | ||
use Doctrine\DBAL\Types\Type; | ||
|
||
use function call_user_func; | ||
|
||
/** | ||
* Custom mapping type for Doctrine DBAL to directly support enums in a database without having to use a native type. | ||
* | ||
* It is necessary to use this custom mapping type due to an apparent bug in the value conversion layer in DBAL when | ||
* using trying to construct our (Sub)Decisions in specific scenarios (e.g. seeding the database). | ||
* | ||
* Due to the `final` marking of the constructor we cannot initialise {@link BackedEnumType::$enumClass} and | ||
* {@link BackedEnumType::$name}. As such, we need to override these when creating specific mapping types. | ||
* | ||
* @template T of BackedEnum | ||
*/ | ||
abstract class BackedEnumType extends Type | ||
{ | ||
/** | ||
* @var class-string<T> $enumClass | ||
* @required | ||
*/ | ||
public string $enumClass; | ||
|
||
/** | ||
* @required | ||
*/ | ||
public const string NAME = ''; | ||
|
||
/** | ||
* {@inheritDoc} | ||
* | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingTraversableTypeHintSpecification | ||
*/ | ||
public function getSQLDeclaration( | ||
array $column, | ||
AbstractPlatform $platform, | ||
): string { | ||
return $platform->getStringTypeDeclarationSQL($column); | ||
} | ||
|
||
/** | ||
* @return T|null | ||
*/ | ||
public function convertToPHPValue( | ||
mixed $value, | ||
AbstractPlatform $platform, | ||
) { | ||
if (empty($value)) { | ||
return null; | ||
} | ||
|
||
return call_user_func([$this->enumClass, 'from'], $value); | ||
} | ||
|
||
/** | ||
* @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint | ||
*/ | ||
public function convertToDatabaseValue( | ||
mixed $value, | ||
AbstractPlatform $platform, | ||
) { | ||
return $value instanceof $this->enumClass ? $value->value : $value; | ||
} | ||
} |
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
23 changes: 23 additions & 0 deletions
23
module/Decision/src/Extensions/Doctrine/MeetingTypesType.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,23 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Decision\Extensions\Doctrine; | ||
|
||
use Application\Extensions\Doctrine\BackedEnumType; | ||
use Decision\Model\Enums\MeetingTypes; | ||
|
||
/** | ||
* @extends BackedEnumType<MeetingTypes> | ||
*/ | ||
class MeetingTypesType extends BackedEnumType | ||
{ | ||
public string $enumClass = MeetingTypes::class; | ||
|
||
public const string NAME = 'meeting_types'; | ||
|
||
public function getName(): string | ||
{ | ||
return self::NAME; | ||
} | ||
} |
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
Oops, something went wrong.