Set ON DELETE SET NULL for savedExport foreign key in ExportGeneration entity and related migration

This commit is contained in:
Julien Fastré 2025-06-17 16:20:14 +02:00
parent ea4cbfe3b9
commit 49607e431f
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
2 changed files with 38 additions and 2 deletions

View File

@ -58,11 +58,11 @@ class ExportGeneration implements TrackCreationInterface
/** /**
* The related saved export. * The related saved export.
* *
* Note that, in some case, the options of this ExportGenration are not equals to the options of the saved export. * Note that, in some case, the options of this ExportGeneration are not equals to the options of the saved export.
* This happens when the options of the saved export are updated. * This happens when the options of the saved export are updated.
*/ */
#[ORM\ManyToOne(targetEntity: SavedExport::class)] #[ORM\ManyToOne(targetEntity: SavedExport::class)]
#[ORM\JoinColumn(nullable: true)] #[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?SavedExport $savedExport = null, private ?SavedExport $savedExport = null,
) { ) {
$this->id = Uuid::uuid4(); $this->id = Uuid::uuid4();

View File

@ -0,0 +1,36 @@
<?php
declare(strict_types=1);
namespace Chill\Migrations\Main;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20250617141354 extends AbstractMigration
{
public function getDescription(): string
{
return 'When a row in chill_main_saved_export is deleted, set the references to NULL in chill_main_export_generation table';
}
public function up(Schema $schema): void
{
$this->addSql(<<<'SQL'
ALTER TABLE chill_main_export_generation DROP CONSTRAINT FK_E644B77DA61D6F69
SQL);
$this->addSql(<<<'SQL'
ALTER TABLE chill_main_export_generation ADD CONSTRAINT FK_E644B77DA61D6F69 FOREIGN KEY (savedExport_id) REFERENCES chill_main_saved_export (id) ON DELETE SET NULL NOT DEFERRABLE INITIALLY IMMEDIATE
SQL);
}
public function down(Schema $schema): void
{
$this->addSql(<<<'SQL'
ALTER TABLE chill_main_export_generation DROP CONSTRAINT fk_e644b77da61d6f69
SQL);
$this->addSql(<<<'SQL'
ALTER TABLE chill_main_export_generation ADD CONSTRAINT fk_e644b77da61d6f69 FOREIGN KEY (savedexport_id) REFERENCES chill_main_saved_export (id) NOT DEFERRABLE INITIALLY IMMEDIATE
SQL);
}
}