FEATURE [migration] migrate the old profession_id to inserting the corresponding string into new profession column string type

This commit is contained in:
Julie Lenaerts 2023-03-03 15:14:53 +01:00 committed by Julien Fastré
parent 11e7f2179c
commit 9a56a1b115
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB

View File

@ -2,28 +2,64 @@
declare(strict_types=1);
/*
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\Migrations\ThirdParty;
use Chill\ThirdPartyBundle\Entity\ThirdPartyProfession;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Types;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
final class Version20230215175150 extends AbstractMigration
final class Version20230215175150 extends AbstractMigration implements ContainerAwareInterface
{
public ContainerInterface $container;
public function down(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_3party.third_party DROP profession');
$this->addSql('ALTER TABLE chill_3party.third_party ADD profession_id INT DEFAULT NULL');
}
public function getDescription(): string
{
return 'Change profession to a string field and transfer values';
}
/**
* @return mixed
*/
public function setContainer(?ContainerInterface $container = null)
{
$this->container = $container;
}
public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE chill_3party.third_party ADD profession VARCHAR(255) DEFAULT \'\' NOT NULL');
// $this->addSql('ALTER TABLE chill_3party.third_party DROP profession_id');
}
$em = $this->container->get('doctrine.orm.entity_manager');
$professions = $em->getRepository(ThirdPartyProfession::class)->findAll();
public function down(Schema $schema): void
{
// $this->addSql('ALTER TABLE chill_3party.third_party ADD profession_id INT DEFAULT NULL');
$this->addSql('ALTER TABLE chill_3party.third_party DROP profession');
foreach ($professions as $p) {
$id = $p->getId();
$name = $p->getName()['fr'];
$this->addSql(
'UPDATE chill_3party.third_party SET profession = :profession
WHERE chill_3party.third_party.profession_id = :id;',
['profession' => $name, 'id' => $id],
['profession' => Types::STRING, 'id' => Types::INTEGER]
);
}
$this->addSql('ALTER TABLE chill_3party.third_party DROP profession_id');
}
}