vendor/bluue/crm-bundle/migrations/Version20231121155915.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace CrmBundleMigrations;
  4. use Bluue\CrmBundle\Entity\Opportunity;
  5. use Doctrine\DBAL\Schema\Schema;
  6. use Symfony\Component\Uid\UuidV6;
  7. use Doctrine\Migrations\AbstractMigration;
  8. final class Version20231121155915 extends AbstractMigration
  9. {
  10.     public function up(Schema $schema): void
  11.     {
  12.         $this->addSql('CREATE TABLE crm_bundle__opportunity_priority (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', created_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', updated_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', deleted_by_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', name VARCHAR(128) NOT NULL, color VARCHAR(7) NOT NULL, text_color VARCHAR(7) NOT NULL, position INT NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, deleted_at DATETIME DEFAULT NULL, is_active TINYINT(1) DEFAULT 1 NOT NULL, is_default TINYINT(1) NOT NULL, INDEX IDX_F3D2BD24B03A8386 (created_by_id), INDEX IDX_F3D2BD24896DBBDE (updated_by_id), INDEX IDX_F3D2BD24C76F1F52 (deleted_by_id), INDEX color (color), INDEX text_color (text_color), INDEX name (name), INDEX position (position), INDEX is_active (is_active), INDEX is_default (is_default), INDEX deleted_at (deleted_at), INDEX created_at (created_at), INDEX updated_at (updated_at), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  13.         $this->addSql('CREATE TABLE crm_bundle__opportunity_priority_translations (id INT AUTO_INCREMENT NOT NULL, object_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\', locale VARCHAR(8) NOT NULL, field VARCHAR(32) NOT NULL, content LONGTEXT DEFAULT NULL, INDEX IDX_A922A7A7232D562B (object_id), FULLTEXT INDEX content (content), UNIQUE INDEX lookup_unique_idx (locale, object_id, field), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
  14.         $this->addSql('ALTER TABLE crm_bundle__opportunity_priority ADD CONSTRAINT FK_F3D2BD24B03A8386 FOREIGN KEY (created_by_id) REFERENCES user (id)');
  15.         $this->addSql('ALTER TABLE crm_bundle__opportunity_priority ADD CONSTRAINT FK_F3D2BD24896DBBDE FOREIGN KEY (updated_by_id) REFERENCES user (id)');
  16.         $this->addSql('ALTER TABLE crm_bundle__opportunity_priority ADD CONSTRAINT FK_F3D2BD24C76F1F52 FOREIGN KEY (deleted_by_id) REFERENCES user (id)');
  17.         $this->addSql('ALTER TABLE crm_bundle__opportunity_priority_translations ADD CONSTRAINT FK_A922A7A7232D562B FOREIGN KEY (object_id) REFERENCES crm_bundle__opportunity_priority (id) ON DELETE CASCADE');
  18.         $this->addSql('ALTER TABLE crm_bundle__opportunity ADD opportunity_priority_id BINARY(16) DEFAULT NULL COMMENT \'(DC2Type:uuid)\'');
  19.         $this->addSql('ALTER TABLE crm_bundle__opportunity ADD CONSTRAINT FK_CEF0F8C742612354 FOREIGN KEY (opportunity_priority_id) REFERENCES crm_bundle__opportunity_priority (id)');
  20.         $this->addSql('CREATE INDEX IDX_CEF0F8C742612354 ON crm_bundle__opportunity (opportunity_priority_id)');
  21.     }
  22.     public function postUp(Schema $schema): void
  23.     {
  24.         parent::postUp($schema);
  25.         $now = (new \DateTime())->format('Y-m-d H:i:s');
  26.         $languages $this->connection->executeQuery('SELECT * FROM language WHERE deleted_at IS NULL')
  27.             ->fetchAllAssociative();
  28.         $rootLocale 'en';
  29.         $defaultLocale $this->connection
  30.             ->executeQuery('SELECT locale FROM language WHERE deleted_at IS NULL AND is_default = 1')
  31.             ->fetchOne() ?: 'en';
  32.         $opportunityPriorities = [
  33.             [
  34.                 'names' => ['fr' => 'Faible''en' => 'Low'],
  35.                 'color' => '#79affd',
  36.                 'text_color' => '#000000',
  37.                 'active' => 1,
  38.                 'default' => 0,
  39.                 'position' => 1
  40.             ],
  41.             [
  42.                 'names' => ['fr' => 'Normal''en' => 'Normal'],
  43.                 'color' => '#ffa500',
  44.                 'text_color' => '#ffffff',
  45.                 'active' => 1,
  46.                 'default' => 1,
  47.                 'position' => 2
  48.             ],
  49.             [
  50.                 'names' => ['fr' => 'Important''en' => 'Important'],
  51.                 'color' => '#febd63',
  52.                 'text_color' => '#ffffff',
  53.                 'active' => 1,
  54.                 'default' => 0,
  55.                 'position' => 3
  56.             ],
  57.             [
  58.                 'names' => ['fr' => 'Critique''en' => 'Critical'],
  59.                 'color' => '#c85c75',
  60.                 'text_color' => '#ffffff',
  61.                 'active' => 1,
  62.                 'default' => 0,
  63.                 'position' => 4
  64.             ]
  65.         ];
  66.         foreach ($opportunityPriorities as $opportunityPriority) {
  67.             $opportunityPriorityId = (new UuidV6())->toBinary();
  68.             $this->connection->insert('crm_bundle__opportunity_priority', [
  69.                 'id' => $opportunityPriorityId,
  70.                 'name' => !empty($opportunityPriority['names'][$defaultLocale]) ?
  71.                     $opportunityPriority['names'][$defaultLocale] : $opportunityPriority['names'][$rootLocale],
  72.                 'color' => $opportunityPriority['color'],
  73.                 'text_color' => $opportunityPriority['text_color'],
  74.                 'is_active' => $opportunityPriority['active'],
  75.                 'is_default' => $opportunityPriority['default'],
  76.                 'position' => $opportunityPriority['position'],
  77.                 'created_at' => $now,
  78.                 'updated_at' => $now
  79.             ]);
  80.             foreach ($languages as $language) {
  81.                 $localeTo $rootLocale;
  82.                 if (!empty($opportunityPriority['names'][$language['locale']])) {
  83.                     $localeTo $language['locale'];
  84.                 } elseif (!empty($opportunityPriority['names'][$defaultLocale])) {
  85.                     $localeTo $defaultLocale;
  86.                 }
  87.                 $this->connection->insert('crm_bundle__opportunity_priority_translations', [
  88.                     'object_id' => $opportunityPriorityId,
  89.                     'locale' => $language['locale'],
  90.                     'field' => 'name',
  91.                     'content' => $opportunityPriority['names'][$localeTo]
  92.                 ]);
  93.             }
  94.         }
  95.         $opportunities $this->connection->executeQuery('SELECT o.id FROM crm_bundle__opportunity o')->fetchAllAssociative();
  96.         $priorityDefaultId $this->connection->executeQuery('SELECT id FROM crm_bundle__opportunity_priority op WHERE op.is_default = 1')->fetchOne();
  97.         $priorityDefaultId UuidV6::fromBinary($priorityDefaultId)->toRfc4122();
  98.         $priorityDefaultId '0x' str_replace('-'''$priorityDefaultId);
  99.         foreach ($opportunities as $opportunity) {
  100.             $id UuidV6::fromBinary($opportunity['id'])->toRfc4122();
  101.             $this->connection->executeStatement("UPDATE crm_bundle__opportunity SET opportunity_priority_id = " $priorityDefaultId " WHERE id = UNHEX(CONCAT('', REPLACE('" $id "', '-', '')))");
  102.         }
  103.     }
  104.     public function down(Schema $schema): void
  105.     {
  106.         $this->addSql('ALTER TABLE crm_bundle__opportunity_priority DROP FOREIGN KEY FK_F3D2BD24B03A8386');
  107.         $this->addSql('ALTER TABLE crm_bundle__opportunity_priority DROP FOREIGN KEY FK_F3D2BD24896DBBDE');
  108.         $this->addSql('ALTER TABLE crm_bundle__opportunity_priority DROP FOREIGN KEY FK_F3D2BD24C76F1F52');
  109.         $this->addSql('ALTER TABLE crm_bundle__opportunity_priority_translations DROP FOREIGN KEY FK_A922A7A7232D562B');
  110.         $this->addSql('DROP TABLE crm_bundle__opportunity_priority');
  111.         $this->addSql('DROP TABLE crm_bundle__opportunity_priority_translations');
  112.         $this->addSql('ALTER TABLE crm_bundle__opportunity DROP FOREIGN KEY FK_CEF0F8C742612354');
  113.         $this->addSql('DROP INDEX IDX_CEF0F8C742612354 ON crm_bundle__opportunity');
  114.         $this->addSql('ALTER TABLE crm_bundle__opportunity DROP opportunity_priority_id');
  115.     }
  116. }