migrations/Version20180819191832.php line 1

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace DoctrineMigrations;
  4. use Doctrine\DBAL\Schema\Schema;
  5. use Doctrine\Migrations\AbstractMigration;
  6. /**
  7.  * Auto-generated Migration: Please modify to your needs!
  8.  */
  9. final class Version20180819191832 extends AbstractMigration
  10. {
  11.     public function up(Schema $schema): void
  12.     {
  13.         // this up() migration is auto-generated, please modify it to your needs
  14.         $this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
  15.         $this->addSql('
  16.             CREATE TABLE customer_goal_time_join_config_service_code (
  17.                 goal_time_id INT NOT NULL,
  18.                 service_code_id INT NOT NULL,
  19.                 INDEX IDX_B1D999852FC1DFC7 (goal_time_id),
  20.                 INDEX IDX_B1D99985E85EAC8B (service_code_id),
  21.                 PRIMARY KEY(goal_time_id, service_code_id)
  22.             ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB
  23.         ');
  24.         $stmt $this->connection->prepare('SELECT count(1) FROM customers_goal_time');
  25.         $stmt->execute();
  26.         // get total entries
  27.         $total $stmt->fetchColumn();
  28.         // insert using pagination
  29.         for ($i 0$i ceil($total 100); ++$i) {
  30.             $stmt $this->connection->prepare(sprintf('SELECT id, service_codes FROM customers_goal_time ORDER BY id ASC LIMIT %d, 100'100 $i));
  31.             $stmt->execute();
  32.             while ($row $stmt->fetch(\PDO::FETCH_OBJ)) {
  33.                 $serviceCodes $row->service_codes json_decode($row->service_codestrue) : [];
  34.                 if ($serviceCodes && \count($serviceCodes)) {
  35.                     foreach ($serviceCodes as $serviceCode) {
  36.                         $this->addSql('INSERT INTO customer_goal_time_join_config_service_code (goal_time_id, service_code_id) VALUES (:goal_time_id, :service_code_id)', [
  37.                             'goal_time_id' => $row->id,
  38.                             'service_code_id' => $serviceCode,
  39.                         ]);
  40.                     }
  41.                 }
  42.             }
  43.         }
  44.         $this->addSql('RENAME TABLE customers_goal_time TO customer_goal_time');
  45.         $this->addSql('ALTER TABLE customer_goal_time ENGINE = InnoDB');
  46.         $this->addSql('ALTER TABLE customer_goal_time CHARACTER SET = utf8mb4');
  47.         $this->addSql('UPDATE customer_goal_time SET status = 0 WHERE status IS NULL');
  48.         $this->addSql('
  49.             ALTER TABLE customer_goal_time
  50.                 DROP service_codes,
  51.                 CHANGE customers_id customer_id INT DEFAULT NULL,
  52.                 CHANGE administrators_id user_id INT DEFAULT NULL,
  53.                 CHANGE notes message LONGTEXT DEFAULT NULL,
  54.                 CHANGE status enabled TINYINT(1) NOT NULL,
  55.                 CHANGE date_added created_at DATETIME NOT NULL,
  56.                 CHANGE last_modified modified_at DATETIME NOT NULL
  57.         ');
  58.         $this->addSql('DROP INDEX idx_customers_id ON customer_goal_time');
  59.         $this->addSql('DROP INDEX idx_administrators_id ON customer_goal_time');
  60.         $this->addSql('CREATE INDEX IDX_DA5925FD9395C3F3 ON customer_goal_time (customer_id)');
  61.         $this->addSql('CREATE INDEX IDX_DA5925FDA76ED395 ON customer_goal_time (user_id)');
  62.         $this->addSql('DELETE FROM customer_goal_time WHERE customer_id NOT IN (SELECT id FROM customer)');
  63.         $this->addSql('UPDATE customer_goal_time SET user_id = NULL WHERE user_id NOT IN (SELECT id FROM user)');
  64.         $this->addSql('DELETE FROM customer_goal_time_join_config_service_code WHERE goal_time_id NOT IN (SELECT id FROM customer_goal_time)');
  65.         $this->addSql('DELETE FROM customer_goal_time_join_config_service_code WHERE service_code_id NOT IN (SELECT id FROM config_service_code)');
  66.         $this->addSql('ALTER TABLE customer_goal_time ADD CONSTRAINT FK_DA5925FD9395C3F3 FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE');
  67.         $this->addSql('ALTER TABLE customer_goal_time ADD CONSTRAINT FK_DA5925FDA76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE SET NULL');
  68.         $this->addSql('ALTER TABLE customer_goal_time_join_config_service_code ADD CONSTRAINT FK_75ED8FF92FC1DFC7 FOREIGN KEY (goal_time_id) REFERENCES customer_goal_time (id) ON DELETE CASCADE');
  69.         $this->addSql('ALTER TABLE customer_goal_time_join_config_service_code ADD CONSTRAINT FK_75ED8FF9E85EAC8B FOREIGN KEY (service_code_id) REFERENCES config_service_code (id) ON DELETE CASCADE');
  70.     }
  71.     public function down(Schema $schema): void
  72.     {
  73.         // this down() migration is auto-generated, please modify it to your needs
  74.         $this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
  75.         $this->addSql('ALTER TABLE customer_goal_time_join_config_service_code DROP FOREIGN KEY FK_75ED8FF9E85EAC8B');
  76.         $this->addSql('ALTER TABLE customer_goal_time_join_config_service_code DROP FOREIGN KEY FK_75ED8FF92FC1DFC7');
  77.         $this->addSql('ALTER TABLE customer_goal_time DROP FOREIGN KEY FK_DA5925FDA76ED395');
  78.         $this->addSql('ALTER TABLE customer_goal_time DROP FOREIGN KEY FK_DA5925FD9395C3F3');
  79.         $this->addSql('RENAME TABLE customer_goal_time TO customers_goal_time');
  80.         $this->addSql('ALTER TABLE customers_goal_time ENGINE = InnoDB');
  81.         $this->addSql('ALTER TABLE customers_goal_time CHARACTER SET = utf8mb4');
  82.         $this->addSql('
  83.             ALTER TABLE customers_goal_time
  84.                 ADD service_codes LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:array)\' AFTER customers_id,
  85.                 CHANGE customer_id customers_id INT NOT NULL,
  86.                 CHANGE user_id administrators_id INT NOT NULL,
  87.                 CHANGE message notes TEXT DEFAULT NULL,
  88.                 CHANGE enabled status TINYINT(1) DEFAULT \'0\',
  89.                 CHANGE created_at date_added DATETIME DEFAULT NULL,
  90.                 CHANGE modified_at last_modified DATETIME DEFAULT NULL
  91.         ');
  92.         $this->addSql('DROP INDEX IDX_DA5925FD9395C3F3 ON customers_goal_time');
  93.         $this->addSql('DROP INDEX IDX_DA5925FDA76ED395 ON customers_goal_time');
  94.         $this->addSql('CREATE INDEX idx_administrators_id ON customers_goal_time (administrators_id)');
  95.         $this->addSql('CREATE INDEX idx_customers_id ON customers_goal_time (customers_id)');
  96.         $stmt $this->connection->prepare('SELECT * FROM customer_goal_time_join_config_service_code');
  97.         $stmt->execute();
  98.         $goalTimes = [];
  99.         while ($row $stmt->fetch(\PDO::FETCH_OBJ)) {
  100.             $goalTimes[$row->goal_time_id][] = $row->service_code_id;
  101.         }
  102.         foreach ($goalTimes as $key => $value) {
  103.             $serviceCodes $value json_encode($value) : null;
  104.             $this->addSql('UPDATE customers_goal_time SET service_codes = :service_codes WHERE id = :id', [
  105.                 'service_codes' => $serviceCodes,
  106.                 'id' => $key,
  107.             ]);
  108.         }
  109.         $this->addSql('DROP TABLE customer_goal_time_join_config_service_code');
  110.     }
  111. }