migrations/Version20190304025526.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use App\Entity\Customer\Customer;
  5. use Doctrine\DBAL\Schema\Schema;
  6. use Doctrine\Migrations\AbstractMigration;
  7. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  8. use Symfony\Component\DependencyInjection\ContainerInterface;
  9. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  10. /**
  11.  * Auto-generated Migration: Please modify to your needs!
  12.  */
  13. final class Version20190304025526 extends AbstractMigration implements ContainerAwareInterface
  14. {
  15.     /** @var ContainerInterface */
  16.     private $container;
  17.     /**
  18.      * @param ContainerInterface|null $container
  19.      */
  20.     public function setContainer(ContainerInterface $container null)
  21.     {
  22.         $this->container $container;
  23.     }
  24.     public function up(Schema $schema): void
  25.     {
  26.         /** @var UserPasswordEncoderInterface $encoder */
  27.         $encoder $this->container->get('security.password_encoder');
  28.         $stmt $this->connection->prepare('SELECT count(1) FROM customer WHERE plain_password IS NOT NULL');
  29.         $stmt->execute();
  30.         // get total entries
  31.         $total $stmt->fetchColumn();
  32.         // insert using pagination
  33.         for ($i 0$i ceil($total 500); ++$i) {
  34.             $stmt $this->connection->prepare(sprintf('SELECT id, plain_password FROM customer WHERE plain_password IS NOT NULL ORDER BY id ASC LIMIT %d, 100'100 $i));
  35.             $stmt->execute();
  36.             while ($row $stmt->fetch(\PDO::FETCH_OBJ)) {
  37.                 $this->addSql('UPDATE customer SET password = :password WHERE id = :id', [
  38.                     'id' => $row->id,
  39.                     'password' => $encoder->encodePassword(new Customer(), $row->plain_password),
  40.                 ]);
  41.             }
  42.         }
  43.     }
  44.     public function down(Schema $schema): void
  45.     {
  46.         $this->addSql('UPDATE customer SET password = SHA1(plain_password) WHERE plain_password IS NOT NULL');
  47.     }
  48. }