Skip to content

Commit

Permalink
feat(Log): Added entityClass and entityId to remove hard relationship…
Browse files Browse the repository at this point in the history
… with NodesSources

BREAKING CHANGE: Log entity requires a Migration that may be destructive on `log` table.
  • Loading branch information
ambroisemaupate committed Jun 28, 2023
1 parent 02bc78f commit 7300cc3
Show file tree
Hide file tree
Showing 43 changed files with 433 additions and 283 deletions.
2 changes: 1 addition & 1 deletion lib/Documents/src/DownscaleImageManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public function processDocumentFromExistingRaw(?DocumentInterface $document = nu
null !== $this->createDocumentFromImage($document, $processImage, true)
&& null !== $this->logger
) {
$this->logger->info('Document has been downscaled.', ['path' => $documentPath]);
$this->logger->info('Document has been downscaled.', ['path' => $documentPath, 'entity' => $document]);
}
}
}
Expand Down
19 changes: 11 additions & 8 deletions lib/RoadizCompatBundle/src/Controller/AppController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ReflectionClass;
use ReflectionException;
use RZ\Roadiz\CompatBundle\Theme\ThemeResolverInterface;
use RZ\Roadiz\Core\AbstractEntities\PersistableInterface;
use RZ\Roadiz\Core\AbstractEntities\TranslationInterface;
use RZ\Roadiz\CoreBundle\Entity\Node;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;
Expand Down Expand Up @@ -390,9 +391,9 @@ public function getTheme(): ?Theme
*
* @param Request $request
* @param string $msg
* @param NodesSources|null $source
* @param object|null $source
*/
public function publishConfirmMessage(Request $request, string $msg, ?NodesSources $source = null): void
public function publishConfirmMessage(Request $request, string $msg, ?object $source = null): void
{
$this->publishMessage($request, $msg, 'confirm', $source);
}
Expand All @@ -404,13 +405,13 @@ public function publishConfirmMessage(Request $request, string $msg, ?NodesSourc
* @param Request $request
* @param string $msg
* @param string $level
* @param NodesSources|null $source
* @param object|null $source
*/
protected function publishMessage(
Request $request,
string $msg,
string $level = "confirm",
?NodesSources $source = null
?object $source = null
): void {
$session = $this->getSession();
if ($session instanceof Session) {
Expand All @@ -419,10 +420,12 @@ protected function publishMessage(

switch ($level) {
case 'error':
$this->getLogger()->error($msg, ['source' => $source]);
case 'danger':
case 'fail':
$this->getLogger()->error($msg, ['entity' => $source]);
break;
default:
$this->getLogger()->info($msg, ['source' => $source]);
$this->getLogger()->info($msg, ['entity' => $source]);
break;
}
}
Expand All @@ -444,10 +447,10 @@ public function getSession(): ?SessionInterface
*
* @param Request $request
* @param string $msg
* @param NodesSources|null $source
* @param object|null $source
* @return void
*/
public function publishErrorMessage(Request $request, string $msg, NodesSources $source = null): void
public function publishErrorMessage(Request $request, string $msg, ?object $source = null): void
{
$this->publishMessage($request, $msg, 'error', $source);
}
Expand Down
75 changes: 75 additions & 0 deletions lib/RoadizCoreBundle/migrations/Version20230628143106.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace RZ\Roadiz\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use RZ\Roadiz\CoreBundle\Entity\NodesSources;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20230628143106 extends AbstractMigration
{
public function getDescription(): string
{
return 'Refactored log table to store entity class and id instead of node source id.';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE log ADD entity_class VARCHAR(255) DEFAULT NULL, ADD entity_id VARCHAR(36) DEFAULT NULL');
$this->addSql('CREATE INDEX IDX_8F3F68C541BF2C66 ON log (entity_class)');
$this->addSql('CREATE INDEX IDX_8F3F68C541BF2C6681257D5D ON log (entity_class, entity_id)');
$this->addSql('CREATE INDEX log_entity_class_datetime ON log (entity_class, datetime)');
$this->addSql('CREATE INDEX log_entity_class_id_datetime ON log (entity_class, entity_id, datetime)');

// Move node_source_id to entity_class and entity_id
$nodeSourceClass = NodesSources::class;
$this->addSql(<<<EOF
UPDATE log
SET log.entity_class = '${nodeSourceClass}',
log.entity_id = log.node_source_id
WHERE log.node_source_id IS NOT NULL
EOF
);

// Drop old indexes and foreign key on node_source_id
$this->addSql('ALTER TABLE log DROP FOREIGN KEY FK_8F3F68C58E831402');
$this->addSql('ALTER TABLE log DROP node_source_id');
$this->addSql('DROP INDEX log_ns_datetime ON log');
$this->addSql('DROP INDEX IDX_8F3F68C58E831402 ON log');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE log ADD node_source_id INT DEFAULT NULL');

// Move entity_class and entity_id to node_source_id
$nodeSourceClass = NodesSources::class;
$this->addSql(<<<EOF
UPDATE log
SET log.node_source_id = log.entity_id
WHERE log.entity_class = '${nodeSourceClass}'
EOF
);

// this down() migration is auto-generated, please modify it to your needs
$this->addSql('DROP INDEX IDX_8F3F68C541BF2C66 ON log');
$this->addSql('DROP INDEX IDX_8F3F68C541BF2C6681257D5D ON log');
$this->addSql('DROP INDEX log_entity_class_datetime ON log');
$this->addSql('DROP INDEX log_entity_class_id_datetime ON log');
$this->addSql('ALTER TABLE log DROP entity_class, DROP entity_id');
$this->addSql('ALTER TABLE log ADD CONSTRAINT FK_8F3F68C58E831402 FOREIGN KEY (node_source_id) REFERENCES nodes_sources (id) ON UPDATE NO ACTION ON DELETE SET NULL');
$this->addSql('CREATE INDEX log_ns_datetime ON log (node_source_id, datetime)');
$this->addSql('CREATE INDEX IDX_8F3F68C58E831402 ON log (node_source_id)');
}

public function isTransactional(): bool
{
return false;
}
}
4 changes: 3 additions & 1 deletion lib/RoadizCoreBundle/src/Controller/CustomFormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,9 @@ public function sendAnswer(
}
}
} catch (FilesystemException $exception) {
$this->logger->error($exception->getMessage());
$this->logger->error($exception->getMessage(), [
'entity' => $answer
]);
}

if (empty($receiver)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ public function postLoad(LifecycleEventArgs $event): void
[
'exception_message' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
'entity' => $setting
]
);
} catch (InvalidMessage $exception) {
Expand All @@ -107,6 +108,7 @@ public function postLoad(LifecycleEventArgs $event): void
[
'exception_message' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
'entity' => $setting
]
);
}
Expand Down
90 changes: 71 additions & 19 deletions lib/RoadizCoreBundle/src/Entity/Log.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
ORM\Entity(repositoryClass: LogRepository::class),
ORM\Table(name: "log"),
ORM\Index(columns: ["datetime"]),
ORM\Index(columns: ["node_source_id", "datetime"], name: "log_ns_datetime"),
ORM\Index(columns: ["entity_class"]),
ORM\Index(columns: ["entity_class", "entity_id"]),
ORM\Index(columns: ["entity_class", "datetime"], name: "log_entity_class_datetime"),
ORM\Index(columns: ["entity_class", "entity_id", "datetime"], name: "log_entity_class_id_datetime"),
ORM\Index(columns: ["username", "datetime"], name: "log_username_datetime"),
ORM\Index(columns: ["user_id", "datetime"], name: "log_user_datetime"),
ORM\Index(columns: ["level", "datetime"], name: "log_level_datetime"),
Expand Down Expand Up @@ -65,12 +68,6 @@ class Log extends AbstractEntity
#[Serializer\Groups(['log'])]
protected \DateTime $datetime;

#[ORM\ManyToOne(targetEntity: NodesSources::class, inversedBy: 'logs')]
#[ORM\JoinColumn(name: 'node_source_id', referencedColumnName: 'id', onDelete: 'SET NULL')]
#[SymfonySerializer\Groups(['log_sources'])]
#[Serializer\Groups(['log_sources'])]
protected ?NodesSources $nodeSource = null;

#[ORM\Column(name: 'client_ip', type: 'string', length: 46, unique: false, nullable: true)]
#[SymfonySerializer\Groups(['log'])]
#[Serializer\Groups(['log'])]
Expand All @@ -83,6 +80,26 @@ class Log extends AbstractEntity
#[Assert\Length(max: 64)]
protected ?string $channel = null;

/**
* @var class-string|null
*/
#[ORM\Column(name: 'entity_class', type: 'string', length: 255, unique: false, nullable: true)]
#[SymfonySerializer\Groups(['log'])]
#[Serializer\Groups(['log'])]
#[Assert\Length(max: 255)]
// @phpstan-ignore-next-line
protected ?string $entityClass = null;

/**
* @var string|int|null
*/
#[ORM\Column(name: 'entity_id', type: 'string', length: 36, unique: false, nullable: true)]
#[SymfonySerializer\Groups(['log'])]
#[Serializer\Groups(['log'])]
#[Assert\Length(max: 36)]
// @phpstan-ignore-next-line
protected string|int|null $entityId = null;

#[ORM\Column(name: 'additional_data', type: 'json', unique: false, nullable: true)]
#[SymfonySerializer\Groups(['log'])]
#[Serializer\Groups(['log'])]
Expand All @@ -101,6 +118,10 @@ public function __construct(int $level, string $message)
$this->datetime = new \DateTime("now");
}

/**
* @return User|null
* @deprecated Use additionalData or username instead
*/
public function getUser(): ?User
{
return $this->user;
Expand Down Expand Up @@ -163,35 +184,30 @@ public function getDatetime(): \DateTime
}

/**
* Get log related node-source.
* BC setter.
*
* @return NodesSources|null
*/
public function getNodeSource(): ?NodesSources
{
return $this->nodeSource;
}

/**
* @param NodesSources|null $nodeSource
* @return $this
*/
public function setNodeSource(?NodesSources $nodeSource): Log
{
$this->nodeSource = $nodeSource;
if (null !== $nodeSource) {
$this->entityClass = NodesSources::class;
$this->entityId = $nodeSource->getId();
}
return $this;
}

/**
* @return string
* @return string|null
*/
public function getClientIp(): ?string
{
return $this->clientIp;
}

/**
* @param string $clientIp
* @param string|null $clientIp
* @return Log
*/
public function setClientIp(?string $clientIp): Log
Expand Down Expand Up @@ -240,6 +256,42 @@ public function setChannel(?string $channel): Log
return $this;
}

/**
* @return class-string|null
*/
public function getEntityClass(): ?string
{
return $this->entityClass;
}

/**
* @param class-string|null $entityClass
* @return Log
*/
public function setEntityClass(?string $entityClass): Log
{
$this->entityClass = $entityClass;
return $this;
}

/**
* @return int|string|null
*/
public function getEntityId(): int|string|null
{
return $this->entityId;
}

/**
* @param int|string|null $entityId
* @return Log
*/
public function setEntityId(int|string|null $entityId): Log
{
$this->entityId = $entityId;
return $this;
}

#[ORM\PrePersist]
public function prePersist(): void
{
Expand Down
Loading

0 comments on commit 7300cc3

Please sign in to comment.