Skip to content

Commit

Permalink
feat(NodeType): Roadiz now generates a new Doctrine Migration for eac…
Browse files Browse the repository at this point in the history
…h updates on node-types and node-types fields.

BREAKING CHANGE: `bin/console themes:migrate` command do not execute Doctrine migrations, generate NS entities and schema update. **You must version your NS\*\*\*\*.php files and migrations** to sync your app in different environments.
  • Loading branch information
ambroisemaupate committed Jul 26, 2023
1 parent 569f3f9 commit 3e1b8bb
Show file tree
Hide file tree
Showing 8 changed files with 191 additions and 144 deletions.
8 changes: 0 additions & 8 deletions lib/RoadizCompatBundle/src/Console/ThemeInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,14 +176,6 @@ protected function importThemeData(?ThemeInfo $themeInfo, string $themeConfigPat
$this->importFile($themeInfo, $filename, $this->attributeImporter);
}
}
if ($this->io->isVeryVerbose()) {
$this->io->note(
'You should do a `bin/console generate:nsentities`' .
' to regenerate your node-types source classes, ' .
'and a `bin/console doctrine:schema:update --dump-sql --force` ' .
'to apply your changes into database.'
);
}
} else {
$this->io->warning('Config file "' . $themeConfigPath . '" has no data to import.');
}
Expand Down
94 changes: 55 additions & 39 deletions lib/RoadizCompatBundle/src/Console/ThemeMigrateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __construct(string $projectDir)
protected function configure(): void
{
$this->setName('themes:migrate')
->setDescription('Update your site against theme import files, regenerate NSEntities, update database schema and clear caches.')
->setDescription('Update your app node-types, settings, roles against theme import files')
->addArgument(
'classname',
InputArgument::REQUIRED,
Expand All @@ -40,6 +40,18 @@ protected function configure(): void
'd',
InputOption::VALUE_NONE,
'Do nothing, only print information.'
)
->addOption(
'doctrine-migrations',
null,
InputOption::VALUE_NONE,
'Run Doctrine migrations before importing theme resources.'
)
->addOption(
'ns-entities',
null,
InputOption::VALUE_NONE,
'Regenerate NS entities classes (NS classes should be versioned).'
);
}

Expand All @@ -65,13 +77,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->isQuiet(),
);
} else {
$this->runCommand(
'doctrine:migrations:migrate',
'--allow-no-migration',
null,
false,
$output->isQuiet()
) === 0 ? $io->success('doctrine:migrations:migrate') : $io->error('doctrine:migrations:migrate');
if ($input->getOption('doctrine-migrations')) {
$this->runCommand(
'doctrine:migrations:migrate',
'--allow-no-migration',
null,
false,
$output->isQuiet()
) === 0 ? $io->success('doctrine:migrations:migrate') : $io->error('doctrine:migrations:migrate');
}

$this->runCommand(
'themes:install',
Expand All @@ -81,37 +95,39 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$output->isQuiet()
) === 0 ? $io->success('themes:install') : $io->error('themes:install');

$this->runCommand(
'generate:nsentities',
'',
null,
$input->isInteractive(),
$output->isQuiet()
) === 0 ? $io->success('generate:nsentities') : $io->error('generate:nsentities');

$this->runCommand(
'doctrine:cache:clear-metadata',
'',
null,
$input->isInteractive(),
$output->isQuiet()
) === 0 ? $io->success('doctrine:cache:clear-metadata') : $io->error('doctrine:cache:clear-metadata');

$this->runCommand(
'doctrine:schema:update',
'--dump-sql --force',
null,
$input->isInteractive(),
$output->isQuiet()
) === 0 ? $io->success('doctrine:schema:update') : $io->error('doctrine:schema:update');

$this->runCommand(
'cache:clear',
'',
null,
$input->isInteractive(),
$output->isQuiet()
) === 0 ? $io->success('cache:clear') : $io->error('cache:clear');
if ($input->getOption('ns-entities')) {
$this->runCommand(
'generate:nsentities',
'',
null,
$input->isInteractive(),
$output->isQuiet()
) === 0 ? $io->success('generate:nsentities') : $io->error('generate:nsentities');

$this->runCommand(
'doctrine:cache:clear-metadata',
'',
null,
$input->isInteractive(),
$output->isQuiet()
) === 0 ? $io->success('doctrine:cache:clear-metadata') : $io->error('doctrine:cache:clear-metadata');

$this->runCommand(
'cache:clear',
'',
null,
$input->isInteractive(),
$output->isQuiet()
) === 0 ? $io->success('cache:clear') : $io->error('cache:clear');

$this->runCommand(
'cache:pool:clear',
'cache.global_clearer',
null,
$input->isInteractive(),
$output->isQuiet()
) === 0 ? $io->success('cache:pool:clear') : $io->error('cache:pool:clear');
}
}
return 0;
}
Expand Down
18 changes: 5 additions & 13 deletions lib/RoadizCoreBundle/src/Doctrine/SchemaUpdater.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,23 +98,15 @@ public function updateSchema(): void
*/
public function updateNodeTypesSchema(): void
{
/*
* Execute pending application migrations
*/
$this->updateSchema();

/*
* Update schema with new node-types
* without creating any migration
*/
$this->clearMetadata();
$process = $this->runCommand(
'doctrine:schema:update',
'--dump-sql --force',
'doctrine:migrations:diff',
'--namespace=DoctrineMigrations --quiet --allow-empty-diff',
);
$process->run();

if ($process->wait() === 0) {
$this->logger->info('DB schema has been updated.');
$this->logger->info('New migration has been generated.');
$this->updateSchema();
} else {
throw new \RuntimeException('DB schema update failed. ' . $process->getErrorOutput());
}
Expand Down
15 changes: 14 additions & 1 deletion lib/RoadizCoreBundle/src/EntityHandler/NodeTypeHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
use Doctrine\Persistence\ObjectManager;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\SerializerInterface;
use Psr\Log\LoggerInterface;
use RZ\Roadiz\Core\Handlers\AbstractHandler;
use RZ\Roadiz\CoreBundle\Doctrine\SchemaUpdater;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\NodeType;
use RZ\Roadiz\CoreBundle\Entity\NodeTypeField;
Expand All @@ -35,6 +35,7 @@ class NodeTypeHandler extends AbstractHandler
private string $serializedNodeTypesDir;
private string $importFilesConfigPath;
private string $kernelProjectDir;
private LoggerInterface $logger;

/**
* @return NodeType
Expand Down Expand Up @@ -63,6 +64,7 @@ public function __construct(
HandlerFactory $handlerFactory,
SerializerInterface $serializer,
ApiResourceGenerator $apiResourceGenerator,
LoggerInterface $logger,
string $generatedEntitiesDir,
string $serializedNodeTypesDir,
string $importFilesConfigPath,
Expand All @@ -77,6 +79,7 @@ public function __construct(
$this->importFilesConfigPath = $importFilesConfigPath;
$this->kernelProjectDir = $kernelProjectDir;
$this->apiResourceGenerator = $apiResourceGenerator;
$this->logger = $logger;
}

public function getGeneratedEntitiesFolder(): string
Expand Down Expand Up @@ -106,6 +109,11 @@ public function removeSourceEntityClass(): bool
if ($fileSystem->exists($repositoryFile) && is_file($repositoryFile)) {
$fileSystem->remove($repositoryFile);
}
$this->logger->info('Entity class file and repository have been removed.', [
'nodeType' => $this->nodeType->getName(),
'file' => $file,
'repositoryFile' => $repositoryFile,
]);
return true;
}

Expand Down Expand Up @@ -254,6 +262,11 @@ public function generateSourceEntityClass(): bool

\clearstatcache(true, $file);
\clearstatcache(true, $repositoryFile);
$this->logger->info('Entity class file and repository have been generated.', [
'nodeType' => $this->nodeType->getName(),
'file' => $file,
'repositoryFile' => $repositoryFile,
]);

return true;
}
Expand Down
13 changes: 12 additions & 1 deletion lib/RoadizCoreBundle/src/NodeType/ApiResourceGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Doctrine\Inflector\InflectorFactory;
use LogicException;
use Psr\Log\LoggerInterface;
use RZ\Roadiz\Contracts\NodeType\NodeTypeInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\String\UnicodeString;
Expand All @@ -14,10 +15,12 @@
final class ApiResourceGenerator
{
private string $apiResourcesDir;
private LoggerInterface $logger;

public function __construct(string $apiResourcesDir)
public function __construct(string $apiResourcesDir, LoggerInterface $logger)
{
$this->apiResourcesDir = $apiResourcesDir;
$this->logger = $logger;
}

/**
Expand All @@ -39,6 +42,10 @@ public function generate(NodeTypeInterface $nodeType): ?string
$resourcePath,
Yaml::dump($this->getApiResourceDefinition($nodeType), 6)
);
$this->logger->info('API resource config file has been generated.', [
'nodeType' => $nodeType->getName(),
'file' => $resourcePath,
]);
\clearstatcache(true, $resourcePath);
return $resourcePath;
} else {
Expand All @@ -58,6 +65,10 @@ public function remove(NodeTypeInterface $nodeType): void

if ($filesystem->exists($resourcePath)) {
$filesystem->remove($resourcePath);
$this->logger->info('API resource config file has been removed.', [
'nodeType' => $nodeType->getName(),
'file' => $resourcePath,
]);
@\clearstatcache(true, $resourcePath);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public function __construct(
$this->inheritanceType = $inheritanceType;
}


public function getDefaultValuesAmongAllFields(NodeTypeFieldInterface $field): array
{
/*
Expand Down
Loading

0 comments on commit 3e1b8bb

Please sign in to comment.