Skip to content

Commit 1298cd8

Browse files
committed
chore(seeder): add User seeder
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.
1 parent 4ce80f3 commit 1298cd8

File tree

7 files changed

+154
-4
lines changed

7 files changed

+154
-4
lines changed

Makefile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ rundev: builddev
3737
@make replenish
3838
@docker compose exec web rm -rf data/cache/module-config-cache.application.config.cache.php
3939

40+
migrate: replenish
41+
@docker compose exec -it web ./orm migrations:migrate --object-manager doctrine.entitymanager.orm_default
42+
@docker compose exec -it web ./orm migrations:migrate --object-manager doctrine.entitymanager.orm_report
43+
4044
migration-list: replenish
4145
@docker compose exec -T web ./orm migrations:list --object-manager doctrine.entitymanager.orm_default
4246
@docker compose exec -T web ./orm migrations:list --object-manager doctrine.entitymanager.orm_report
@@ -47,10 +51,6 @@ migration-diff: replenish
4751
@docker compose exec -T web ./orm migrations:diff --object-manager doctrine.entitymanager.orm_report
4852
@docker cp "$(shell docker compose ps -q web)":/code/module/Report/migrations ./module/Report/migrations
4953

50-
migration-migrate: replenish
51-
@docker compose exec -it web ./orm migrations:migrate --object-manager doctrine.entitymanager.orm_default
52-
@docker compose exec -it web ./orm migrations:migrate --object-manager doctrine.entitymanager.orm_report
53-
5454
migration-up: replenish migration-list
5555
@read -p "Enter EM_ALIAS (orm_default or orm_report): " alias; \
5656
read -p "Enter the migration version to execute (e.g., -- note escaping the backslashes is required): " version; \
@@ -61,6 +61,9 @@ migration-down: replenish migration-list
6161
read -p "Enter the migration version to down (e.g., -- note escaping the backslashes is required): " version; \
6262
docker compose exec -it web ./orm migrations:execute --down $$version --object-manager doctrine.entitymanager.$$alias
6363

64+
seed: replenish
65+
@docker compose exec -T web ./web application:fixtures:load
66+
6467
exec:
6568
docker compose exec -it web $(cmd)
6669

module/Application/config/module.config.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Application;
66

7+
use Application\Command\LoadFixturesCommand;
78
use Application\Controller\IndexController;
89
use Application\View\Helper\BootstrapElementError;
910
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
@@ -81,6 +82,11 @@
8182
'bootstrapElementError' => BootstrapElementError::class,
8283
],
8384
],
85+
'laminas-cli' => [
86+
'commands' => [
87+
'application:fixtures:load' => LoadFixturesCommand::class,
88+
],
89+
],
8490
'doctrine' => [
8591
'driver' => [
8692
__NAMESPACE__ . '_driver' => [
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Application\Command\Factory;
6+
7+
use Application\Command\LoadFixturesCommand;
8+
use Doctrine\ORM\EntityManager;
9+
use Laminas\ServiceManager\Factory\FactoryInterface;
10+
use Psr\Container\ContainerInterface;
11+
12+
class LoadFixturesCommandFactory implements FactoryInterface
13+
{
14+
/**
15+
* @param string $requestedName
16+
*/
17+
public function __invoke(
18+
ContainerInterface $container,
19+
$requestedName,
20+
?array $options = null,
21+
): LoadFixturesCommand {
22+
/** @var EntityManager $databaseEntityManager */
23+
$databaseEntityManager = $container->get('doctrine.entitymanager.orm_default');
24+
/** @var EntityManager $reportEntityManager */
25+
$reportEntityManager = $container->get('doctrine.entitymanager.orm_report');
26+
27+
return new LoadFixturesCommand(
28+
$databaseEntityManager,
29+
$reportEntityManager,
30+
);
31+
}
32+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Application\Command;
6+
7+
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
8+
use Doctrine\Common\DataFixtures\Loader;
9+
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
10+
use Doctrine\ORM\EntityManager;
11+
use Symfony\Component\Console\Attribute\AsCommand;
12+
use Symfony\Component\Console\Command\Command;
13+
use Symfony\Component\Console\Input\InputInterface;
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
use Throwable;
16+
17+
#[AsCommand(
18+
name: 'application:fixtures:load',
19+
description: 'Seed the database with data fixtures.',
20+
)]
21+
class LoadFixturesCommand extends Command
22+
{
23+
private const array DATABASE_FIXTURES = [
24+
// './module/Database/test/Seeder',
25+
'./module/User/test/Seeder',
26+
];
27+
28+
private const array REPORT_FIXTURES = [
29+
// './module/Report/test/Seeder',
30+
];
31+
32+
public function __construct(
33+
private readonly EntityManager $databaseEntityManager,
34+
private readonly EntityManager $reportEntityManager,
35+
) {
36+
parent::__construct();
37+
}
38+
39+
protected function execute(
40+
InputInterface $input,
41+
OutputInterface $output,
42+
): int {
43+
$output->setDecorated(true);
44+
45+
$databaseLoader = new Loader();
46+
$reportLoader = new Loader();
47+
$purger = new ORMPurger();
48+
$purger->setPurgeMode(ORMPurger::PURGE_MODE_TRUNCATE);
49+
$databaseExecutor = new ORMExecutor($this->databaseEntityManager, $purger);
50+
$reportExecutor = new ORMExecutor($this->reportEntityManager, $purger);
51+
52+
foreach ($this::DATABASE_FIXTURES as $fixture) {
53+
$databaseLoader->loadFromDirectory($fixture);
54+
}
55+
56+
foreach ($this::REPORT_FIXTURES as $fixture) {
57+
$reportLoader->loadFromDirectory($fixture);
58+
}
59+
60+
$output->writeln('<info>Loading fixtures into the database...</info>');
61+
62+
$databaseConnection = $this->databaseEntityManager->getConnection();
63+
$reportConnection = $this->reportEntityManager->getConnection();
64+
try {
65+
// Temporarily disable FK constraint checks.
66+
// The try-catch is necessary to hide some error messages (because the executeStatement).
67+
$databaseConnection->executeStatement('SET session_replication_role = \'replica\'');
68+
$databaseExecutor->execute($databaseLoader->getFixtures());
69+
$databaseConnection->executeStatement('SET session_replication_role = \'origin\'');
70+
$reportConnection->executeStatement('SET session_replication_role = \'replica\'');
71+
$reportExecutor->execute($reportLoader->getFixtures());
72+
$reportConnection->executeStatement('SET session_replication_role = \'origin\'');
73+
} catch (Throwable) {
74+
}
75+
76+
$output->writeln('<info>Loaded fixtures!</info>');
77+
78+
return Command::SUCCESS;
79+
}
80+
}

module/Application/src/Module.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
namespace Application;
66

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

156158
return $logger;
157159
},
160+
LoadFixturesCommand::class => LoadFixturesCommandFactory::class,
158161
],
159162
];
160163
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace UserTest\Seeder;
6+
7+
use Doctrine\Common\DataFixtures\AbstractFixture;
8+
use Doctrine\Persistence\ObjectManager;
9+
use User\Model\User;
10+
11+
class UserFixture extends AbstractFixture
12+
{
13+
public function load(ObjectManager $manager): void
14+
{
15+
$user = new User();
16+
$user->setLogin('admin');
17+
$user->setPassword('$2y$13$smUYvCkgowlfHOFrogwcPONGDFmcylKHmTOZQAks9cDvs15tPxR2a'); // == gewisdbgewis
18+
19+
$manager->persist($user);
20+
$this->addReference('user-admin', $user);
21+
22+
$manager->flush();
23+
}
24+
}

phpcs.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<!-- Type hints that cannot converted to native types due to signature of parent -->
3131
<rule ref="SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint">
3232
<!-- Factories -->
33+
<exclude-pattern>module/Application/src/Command/Factory/LoadFixturesCommandFactory.php</exclude-pattern>
3334
<exclude-pattern>module/Application/src/Mapper/Factory/ConfigItemFactory.php</exclude-pattern>
3435
<exclude-pattern>module/Application/src/Service/Factory/ConfigFactory.php</exclude-pattern>
3536
<exclude-pattern>module/Application/src/Service/Factory/EmailFactory.php</exclude-pattern>
@@ -118,6 +119,7 @@
118119
</rule>
119120
<rule ref="SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint">
120121
<!-- CLI commands -->
122+
<exclude-pattern>module/Application/src/Command/LoadFixturesCommand.php</exclude-pattern>
121123
<exclude-pattern>module/Checker/src/Command/CheckAuthenticationKeysCommand.php</exclude-pattern>
122124
<exclude-pattern>module/Checker/src/Command/CheckDatabaseCommand.php</exclude-pattern>
123125
<exclude-pattern>module/Checker/src/Command/CheckDischargesCommand.php</exclude-pattern>

0 commit comments

Comments
 (0)