Skip to content

Commit

Permalink
Merge pull request #452 from tomudding/chore/initial-data-fixtures-fo…
Browse files Browse the repository at this point in the history
…r-seeding-the-database

chore: add initial seeder
  • Loading branch information
tomudding authored Nov 20, 2024
2 parents 9c5d938 + 1298cd8 commit 234af59
Show file tree
Hide file tree
Showing 10 changed files with 307 additions and 7 deletions.
4 changes: 3 additions & 1 deletion .idea/php.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
"monolog/monolog": "^1.27.1",
"cweagans/composer-patches": "^1.7.3",
"stripe/stripe-php": "^10.21",
"doctrine/migrations": "^3.8"
"doctrine/migrations": "^3.8",
"doctrine/data-fixtures": "^2.0",
"fakerphp/faker": "^1.24"
},
"require-dev": {
"laminas/laminas-component-installer": "^3.4.0",
Expand Down
148 changes: 147 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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
Loading

0 comments on commit 234af59

Please sign in to comment.