<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
final class Version20241210072621 extends AbstractMigration
{
public function getDescription(): string
{
return 'Remove the postal code relation from address and replace it with string attributes. Migrate existing data to the new structure.';
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE billing_address ADD postal_code VARCHAR(255) DEFAULT NULL, ADD city VARCHAR(255) DEFAULT NULL, ADD country VARCHAR(255) DEFAULT NULL');
$this->addSql('ALTER TABLE delivery_address ADD postal_code VARCHAR(255) DEFAULT NULL, ADD city VARCHAR(255) DEFAULT NULL, ADD country VARCHAR(255) DEFAULT NULL');
$this->addSql('
UPDATE billing_address ba
LEFT JOIN postal_code pc ON ba.postal_code_id = pc.id
LEFT JOIN city c ON pc.city_id = c.id
LEFT JOIN country co ON c.country_id = co.id
SET ba.postal_code = pc.code, ba.city = c.name, ba.country = co.name
');
$this->addSql('
UPDATE delivery_address da
LEFT JOIN postal_code pc ON da.postal_code_id = pc.id
LEFT JOIN city c ON pc.city_id = c.id
LEFT JOIN country co ON c.country_id = co.id
SET da.postal_code = pc.code, da.city = c.name, da.country = co.name
');
$this->addSql('ALTER TABLE billing_address DROP FOREIGN KEY FK_6660E456BDBA6A61');
$this->addSql('DROP INDEX IDX_6660E456BDBA6A61 ON billing_address');
$this->addSql('ALTER TABLE billing_address DROP postal_code_id');
$this->addSql('ALTER TABLE delivery_address DROP FOREIGN KEY FK_750D05FBDBA6A61');
$this->addSql('DROP INDEX IDX_750D05FBDBA6A61 ON delivery_address');
$this->addSql('ALTER TABLE delivery_address DROP postal_code_id');
}
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE billing_address ADD postal_code_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE billing_address ADD CONSTRAINT FK_6660E456BDBA6A61 FOREIGN KEY (postal_code_id) REFERENCES postal_code (id)');
$this->addSql('CREATE INDEX IDX_6660E456BDBA6A61 ON billing_address (postal_code_id)');
$this->addSql('ALTER TABLE delivery_address ADD postal_code_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE delivery_address ADD CONSTRAINT FK_750D05FBDBA6A61 FOREIGN KEY (postal_code_id) REFERENCES postal_code (id)');
$this->addSql('CREATE INDEX IDX_750D05FBDBA6A61 ON delivery_address (postal_code_id)');
$this->addSql('ALTER TABLE billing_address DROP postal_code, DROP city, DROP country');
$this->addSql('ALTER TABLE delivery_address DROP postal_code, DROP city, DROP country');
}
}