From 7dca1e30607c2790a31aa629737e3cd1dc1e8f1e Mon Sep 17 00:00:00 2001 From: Tom Udding Date: Wed, 20 Nov 2024 16:17:02 +0100 Subject: [PATCH] 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 https://github.com/GEWIS/gewisweb/pull/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 https://github.com/GEWIS/orm/commit/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. --- Makefile | 11 ++- module/Application/config/module.config.php | 6 ++ .../Command/Factory/LoadFixturesFactory.php | 32 ++++++++ .../Application/src/Command/LoadFixtures.php | 81 +++++++++++++++++++ module/Application/src/Module.php | 3 + module/User/test/Seeder/UserFixture.php | 24 ++++++ 6 files changed, 153 insertions(+), 4 deletions(-) create mode 100644 module/Application/src/Command/Factory/LoadFixturesFactory.php create mode 100644 module/Application/src/Command/LoadFixtures.php create mode 100644 module/User/test/Seeder/UserFixture.php diff --git a/Makefile b/Makefile index 6c04a3f5..ac21a490 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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; \ @@ -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) diff --git a/module/Application/config/module.config.php b/module/Application/config/module.config.php index a0efd187..1a6f3e07 100644 --- a/module/Application/config/module.config.php +++ b/module/Application/config/module.config.php @@ -4,6 +4,7 @@ namespace Application; +use Application\Command\LoadFixtures; use Application\Controller\IndexController; use Application\View\Helper\BootstrapElementError; use Doctrine\ORM\Mapping\Driver\AttributeDriver; @@ -81,6 +82,11 @@ 'bootstrapElementError' => BootstrapElementError::class, ], ], + 'laminas-cli' => [ + 'commands' => [ + 'application:fixtures:load' => LoadFixtures::class, + ], + ], 'doctrine' => [ 'driver' => [ __NAMESPACE__ . '_driver' => [ diff --git a/module/Application/src/Command/Factory/LoadFixturesFactory.php b/module/Application/src/Command/Factory/LoadFixturesFactory.php new file mode 100644 index 00000000..ffb23350 --- /dev/null +++ b/module/Application/src/Command/Factory/LoadFixturesFactory.php @@ -0,0 +1,32 @@ +get('doctrine.entitymanager.orm_default'); + /** @var EntityManager $entityManager */ + $reportEntityManager = $container->get('doctrine.entitymanager.orm_report'); + + return new LoadFixtures( + $databaseEntityManager, + $reportEntityManager, + ); + } +} diff --git a/module/Application/src/Command/LoadFixtures.php b/module/Application/src/Command/LoadFixtures.php new file mode 100644 index 00000000..8d366eb2 --- /dev/null +++ b/module/Application/src/Command/LoadFixtures.php @@ -0,0 +1,81 @@ +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('Loading fixtures into the database...'); + + $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('Loaded fixtures!'); + + return Command::SUCCESS; + } +} diff --git a/module/Application/src/Module.php b/module/Application/src/Module.php index 49559503..b39f096d 100644 --- a/module/Application/src/Module.php +++ b/module/Application/src/Module.php @@ -4,6 +4,8 @@ namespace Application; +use Application\Command\Factory\LoadFixturesFactory as LoadFixturesCommandFactory; +use Application\Command\LoadFixtures as LoadFixturesCommand; use Application\Extensions\Doctrine\Middleware\SetRoleMiddleware; use Application\Mapper\ConfigItem as ConfigItemMapper; use Application\Mapper\Factory\ConfigItemFactory as ConfigItemMapperFactory; @@ -155,6 +157,7 @@ public function getServiceConfig(): array return $logger; }, + LoadFixturesCommand::class => LoadFixturesCommandFactory::class, ], ]; } diff --git a/module/User/test/Seeder/UserFixture.php b/module/User/test/Seeder/UserFixture.php new file mode 100644 index 00000000..33ba5db5 --- /dev/null +++ b/module/User/test/Seeder/UserFixture.php @@ -0,0 +1,24 @@ +setLogin('admin'); + $user->setPassword('$2y$13$smUYvCkgowlfHOFrogwcPONGDFmcylKHmTOZQAks9cDvs15tPxR2a'); // == gewisdbgewis + + $manager->persist($user); + $this->addReference('user-admin', $user); + + $manager->flush(); + } +}