Skip to content

Commit

Permalink
chore(seeder): add User seeder
Browse files Browse the repository at this point in the history
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
tomudding committed Nov 20, 2024
1 parent 4ce80f3 commit 1298cd8
Show file tree
Hide file tree
Showing 7 changed files with 154 additions and 4 deletions.
11 changes: 7 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ rundev: builddev
@make replenish
@docker compose exec web rm -rf data/cache/module-config-cache.application.config.cache.php

migrate: replenish
@docker compose exec -it web ./orm migrations:migrate --object-manager doctrine.entitymanager.orm_default
@docker compose exec -it web ./orm migrations:migrate --object-manager doctrine.entitymanager.orm_report

migration-list: replenish
@docker compose exec -T web ./orm migrations:list --object-manager doctrine.entitymanager.orm_default
@docker compose exec -T web ./orm migrations:list --object-manager doctrine.entitymanager.orm_report
Expand All @@ -47,10 +51,6 @@ migration-diff: replenish
@docker compose exec -T web ./orm migrations:diff --object-manager doctrine.entitymanager.orm_report
@docker cp "$(shell docker compose ps -q web)":/code/module/Report/migrations ./module/Report/migrations

migration-migrate: replenish
@docker compose exec -it web ./orm migrations:migrate --object-manager doctrine.entitymanager.orm_default
@docker compose exec -it web ./orm migrations:migrate --object-manager doctrine.entitymanager.orm_report

migration-up: replenish migration-list
@read -p "Enter EM_ALIAS (orm_default or orm_report): " alias; \
read -p "Enter the migration version to execute (e.g., -- note escaping the backslashes is required): " version; \
Expand All @@ -61,6 +61,9 @@ migration-down: replenish migration-list
read -p "Enter the migration version to down (e.g., -- note escaping the backslashes is required): " version; \
docker compose exec -it web ./orm migrations:execute --down $$version --object-manager doctrine.entitymanager.$$alias

seed: replenish
@docker compose exec -T web ./web application:fixtures:load

exec:
docker compose exec -it web $(cmd)

Expand Down
6 changes: 6 additions & 0 deletions module/Application/config/module.config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Application;

use Application\Command\LoadFixturesCommand;
use Application\Controller\IndexController;
use Application\View\Helper\BootstrapElementError;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
Expand Down Expand Up @@ -81,6 +82,11 @@
'bootstrapElementError' => BootstrapElementError::class,
],
],
'laminas-cli' => [
'commands' => [
'application:fixtures:load' => LoadFixturesCommand::class,
],
],
'doctrine' => [
'driver' => [
__NAMESPACE__ . '_driver' => [
Expand Down
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,
);
}
}
80 changes: 80 additions & 0 deletions module/Application/src/Command/LoadFixturesCommand.php
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;
}
}
3 changes: 3 additions & 0 deletions module/Application/src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Application;

use Application\Command\Factory\LoadFixturesCommandFactory;
use Application\Command\LoadFixturesCommand;
use Application\Extensions\Doctrine\Middleware\SetRoleMiddleware;
use Application\Mapper\ConfigItem as ConfigItemMapper;
use Application\Mapper\Factory\ConfigItemFactory as ConfigItemMapperFactory;
Expand Down Expand Up @@ -155,6 +157,7 @@ public function getServiceConfig(): array

return $logger;
},
LoadFixturesCommand::class => LoadFixturesCommandFactory::class,
],
];
}
Expand Down
24 changes: 24 additions & 0 deletions module/User/test/Seeder/UserFixture.php
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();
}
}
2 changes: 2 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
<!-- Type hints that cannot converted to native types due to signature of parent -->
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint">
<!-- Factories -->
<exclude-pattern>module/Application/src/Command/Factory/LoadFixturesCommandFactory.php</exclude-pattern>
<exclude-pattern>module/Application/src/Mapper/Factory/ConfigItemFactory.php</exclude-pattern>
<exclude-pattern>module/Application/src/Service/Factory/ConfigFactory.php</exclude-pattern>
<exclude-pattern>module/Application/src/Service/Factory/EmailFactory.php</exclude-pattern>
Expand Down Expand Up @@ -118,6 +119,7 @@
</rule>
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint">
<!-- CLI commands -->
<exclude-pattern>module/Application/src/Command/LoadFixturesCommand.php</exclude-pattern>
<exclude-pattern>module/Checker/src/Command/CheckAuthenticationKeysCommand.php</exclude-pattern>
<exclude-pattern>module/Checker/src/Command/CheckDatabaseCommand.php</exclude-pattern>
<exclude-pattern>module/Checker/src/Command/CheckDischargesCommand.php</exclude-pattern>
Expand Down

0 comments on commit 1298cd8

Please sign in to comment.