<?php declare(strict_types=1);
namespace DoctrineMigrations;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\DependencyInjection\ContainerInterface;
use App\Customer\Entity\StreetAddress;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20180707231353 extends AbstractMigration implements ContainerAwareInterface
{
use ContainerAwareTrait;
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE street_address ADD location POINT COMMENT \'(DC2Type:point)\'');
}
public function postUp(Schema $schema) : void
{
$output = new ConsoleOutput();
$output->setFormatter(new OutputFormatter(true));
$output->writeln("\n <info>++</info> Migrating data");
// 1. Move lat/lng into the location field
// just for reference: x = lon, y = lat
$qb = $this->connection->createQueryBuilder();
$q = $qb->update('street_address', 'sa')
->set('sa.location', 'point(sa.lng, sa.lat)')
->where('sa.lat IS NOT NULL')
->andWhere('sa.lng IS NOT NULL');
$output->writeln("\n <comment>-></comment> " . $q->getSQL());
$r = $q->execute();
$output->writeln(" <comment>updated " . $r . " rows</comment>");
// 2. Disable any addresses without a proper formatted_address (we basically soft-delete any old format address)
// NOTE: we can't revert this change, not enough information.
$qb = $this->connection->createQueryBuilder();
$q = $qb->update('street_address', 'sa')
->set('sa.isActive', '0')
->set('sa.isVerified', '0')
->where('sa.formatted_address is NULL');
$output->writeln("\n <comment>-></comment> " . $q->getSQL());
$r = $q->execute();
$output->writeln(" <comment>updated " . $r . " rows</comment>");
// 3. Generate formatted_address for entries without one, using the separate pieces of information
$qb = $this->connection->createQueryBuilder();
$sql = "UPDATE street_address LEFT JOIN deliveryZones ON street_address.idDeliveryZone = deliveryZones.id "
."SET formatted_address = IF(isGatedCommunity, "
."CONCAT(COALESCE(street,''), ' ', COALESCE(number,''), ' P', COALESCE(plot,''), ' H', COALESCE(house,''), ' S', COALESCE(sector,''), ', ', COALESCE(name,'')), "
."CONCAT(COALESCE(street,''), ' ', COALESCE(number,''), ' ', COALESCE(floor,''), ' ', COALESCE(apartment,''), ', ', COALESCE(name,''))) "
."WHERE formatted_address IS NULL";
$output->writeln("\n <comment>-></comment> " . $sql);
$q = $this->connection->prepare($sql);
$r = $q->execute();
$output->writeln(" <comment>updated " . $q->rowCount() . " rows</comment>");
}
public function preDown(Schema $schema) : void
{
$output = new ConsoleOutput();
$output->setFormatter(new OutputFormatter(true));
$qb = $this->connection->createQueryBuilder();
$q = $qb->update('street_address', 'sa')
->set('sa.lat', 'Y(sa.location)')
->set('sa.lng', 'X(sa.location)');
$output->writeln("\n <info>++</info> Migrating data");
$output->writeln("\n <comment>-></comment> " . $q->getSQL());
$r = $q->execute();
$output->writeln(" <comment>updated " . $r . " rows</comment>");
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE street_address DROP location');
}
}