From 9a56a1b1152b73ee5b387e33985180033972133a Mon Sep 17 00:00:00 2001 From: Julie Lenaerts Date: Fri, 3 Mar 2023 15:14:53 +0100 Subject: [PATCH] FEATURE [migration] migrate the old profession_id to inserting the corresponding string into new profession column string type --- .../migrations/Version20230215175150.php | 50 ++++++++++++++++--- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/src/Bundle/ChillThirdPartyBundle/migrations/Version20230215175150.php b/src/Bundle/ChillThirdPartyBundle/migrations/Version20230215175150.php index b87e722b4..38acc8750 100644 --- a/src/Bundle/ChillThirdPartyBundle/migrations/Version20230215175150.php +++ b/src/Bundle/ChillThirdPartyBundle/migrations/Version20230215175150.php @@ -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'); } }