<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use App\Entity\Customer\Customer;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20190304025526 extends AbstractMigration implements ContainerAwareInterface
{
/** @var ContainerInterface */
private $container;
/**
* @param ContainerInterface|null $container
*/
public function setContainer(ContainerInterface $container = null)
{
$this->container = $container;
}
public function up(Schema $schema): void
{
/** @var UserPasswordEncoderInterface $encoder */
$encoder = $this->container->get('security.password_encoder');
$stmt = $this->connection->prepare('SELECT count(1) FROM customer WHERE plain_password IS NOT NULL');
$stmt->execute();
// get total entries
$total = $stmt->fetchColumn();
// insert using pagination
for ($i = 0; $i < ceil($total / 500); ++$i) {
$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));
$stmt->execute();
while ($row = $stmt->fetch(\PDO::FETCH_OBJ)) {
$this->addSql('UPDATE customer SET password = :password WHERE id = :id', [
'id' => $row->id,
'password' => $encoder->encodePassword(new Customer(), $row->plain_password),
]);
}
}
}
public function down(Schema $schema): void
{
$this->addSql('UPDATE customer SET password = SHA1(plain_password) WHERE plain_password IS NOT NULL');
}
}