migrations/Version20180707231353.php line 1

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DoctrineMigrations;
  3. use Symfony\Component\Console\Output\ConsoleOutput;
  4. use Symfony\Component\Console\Formatter\OutputFormatter;
  5. use Doctrine\DBAL\Schema\Schema;
  6. use Doctrine\Migrations\AbstractMigration;
  7. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  8. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  9. use Symfony\Component\DependencyInjection\ContainerInterface;
  10. use App\Customer\Entity\StreetAddress;
  11. /**
  12.  * Auto-generated Migration: Please modify to your needs!
  13.  */
  14. final class Version20180707231353 extends AbstractMigration implements ContainerAwareInterface
  15. {
  16.     use ContainerAwareTrait;
  17.     public function setContainer(ContainerInterface $container null)
  18.     {
  19.         $this->container $container;
  20.     }
  21.     public function up(Schema $schema) : void
  22.     {
  23.         // this up() migration is auto-generated, please modify it to your needs
  24.         $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql''Migration can only be executed safely on \'mysql\'.');
  25.         $this->addSql('ALTER TABLE street_address ADD location POINT COMMENT \'(DC2Type:point)\'');
  26.     }
  27.     public function postUp(Schema $schema) : void
  28.     {
  29.         $output = new ConsoleOutput();
  30.         $output->setFormatter(new OutputFormatter(true));
  31.         $output->writeln("\n  <info>++</info> Migrating data");
  32.         // 1. Move lat/lng into the location field
  33.         // just for reference: x = lon, y = lat
  34.         $qb $this->connection->createQueryBuilder();
  35.         $q $qb->update('street_address''sa')
  36.             ->set('sa.location''point(sa.lng, sa.lat)')
  37.             ->where('sa.lat IS NOT NULL')
  38.             ->andWhere('sa.lng IS NOT NULL');
  39.         $output->writeln("\n     <comment>-></comment> " $q->getSQL());
  40.         $r $q->execute();
  41.         $output->writeln("     <comment>updated " $r " rows</comment>");
  42.         // 2. Disable any addresses without a proper formatted_address (we basically soft-delete any old format address)
  43.         // NOTE: we can't revert this change, not enough information.
  44.         $qb $this->connection->createQueryBuilder();
  45.         $q $qb->update('street_address''sa')
  46.             ->set('sa.isActive''0')
  47.             ->set('sa.isVerified''0')
  48.             ->where('sa.formatted_address is NULL');
  49.         $output->writeln("\n     <comment>-></comment> " $q->getSQL());
  50.         $r $q->execute();
  51.         $output->writeln("     <comment>updated " $r " rows</comment>");
  52.         // 3. Generate formatted_address for entries without one, using the separate pieces of information
  53.         $qb $this->connection->createQueryBuilder();
  54.         $sql "UPDATE street_address LEFT JOIN deliveryZones ON street_address.idDeliveryZone = deliveryZones.id "
  55.               ."SET formatted_address = IF(isGatedCommunity, "
  56.               ."CONCAT(COALESCE(street,''), ' ', COALESCE(number,''), ' P', COALESCE(plot,''), ' H', COALESCE(house,''), ' S', COALESCE(sector,''), ', ', COALESCE(name,'')), "
  57.               ."CONCAT(COALESCE(street,''), ' ', COALESCE(number,''), ' ', COALESCE(floor,''), ' ', COALESCE(apartment,''), ', ', COALESCE(name,''))) "
  58.               ."WHERE formatted_address IS NULL";
  59.         $output->writeln("\n     <comment>-></comment> " $sql);
  60.         $q $this->connection->prepare($sql);
  61.         $r $q->execute();
  62.         $output->writeln("     <comment>updated " $q->rowCount() . " rows</comment>");
  63.     }
  64.     public function preDown(Schema $schema) : void
  65.     {
  66.         $output = new ConsoleOutput();
  67.         $output->setFormatter(new OutputFormatter(true));
  68.         $qb $this->connection->createQueryBuilder();
  69.         $q $qb->update('street_address''sa')
  70.             ->set('sa.lat''Y(sa.location)')
  71.             ->set('sa.lng''X(sa.location)');
  72.         $output->writeln("\n  <info>++</info> Migrating data");
  73.         $output->writeln("\n     <comment>-></comment> " $q->getSQL());
  74.         $r $q->execute();
  75.         $output->writeln("     <comment>updated " $r " rows</comment>");
  76.     }
  77.     public function down(Schema $schema) : void
  78.     {
  79.         // this down() migration is auto-generated, please modify it to your needs
  80.         $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql''Migration can only be executed safely on \'mysql\'.');
  81.         $this->addSql('ALTER TABLE street_address DROP location');
  82.     }
  83. }