Skip to content

Commit

Permalink
On shipment deletion cascade slot deletion & remove orphan slots.
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquesbh committed Sep 15, 2021
1 parent fee8523 commit 5356484
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
38 changes: 38 additions & 0 deletions src/Migrations/Version20210915210206.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of Monsieur Biz' Shipping Slot plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace MonsieurBiz\SyliusShippingSlotPlugin\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class Version20210915210206 extends AbstractMigration
{
public function getDescription(): string
{
return 'Cascade shipment deletion to slot deletion. Also remove slots without shipment.';
}

public function up(Schema $schema): void
{
$this->addSql('DELETE FROM monsieurbiz_shipping_slot_slot WHERE shipment_id IS NULL');
$this->addSql('ALTER TABLE monsieurbiz_shipping_slot_slot DROP FOREIGN KEY FK_3BD6F1F67BE036FC');
$this->addSql('ALTER TABLE monsieurbiz_shipping_slot_slot ADD CONSTRAINT FK_3BD6F1F67BE036FC FOREIGN KEY (shipment_id) REFERENCES sylius_shipment (id) ON DELETE CASCADE');
}

public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE monsieurbiz_shipping_slot_slot DROP FOREIGN KEY FK_3BD6F1F67BE036FC');
$this->addSql('ALTER TABLE monsieurbiz_shipping_slot_slot ADD CONSTRAINT FK_3BD6F1F67BE036FC FOREIGN KEY (shipment_id) REFERENCES sylius_shipment (id) ON DELETE SET NULL');
}
}
2 changes: 1 addition & 1 deletion src/Resources/config/doctrine/Slot.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<gedmo:timestampable on="update"/>
</field>
<one-to-one field="shipment" target-entity="Sylius\Component\Shipping\Model\ShipmentInterface" inversed-by="slot">
<join-column name="shipment_id" referenced-column-name="id" nullable="true" on-delete="SET NULL" />
<join-column name="shipment_id" referenced-column-name="id" nullable="true" on-delete="CASCADE" />
</one-to-one>
</entity>
</doctrine-mapping>

0 comments on commit 5356484

Please sign in to comment.