migrations/Version20180822163302.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 Version20180822163302 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('RENAME TABLE customers_stages_items TO customer_stage');
  16.         $this->addSql('ALTER TABLE customer_stage ENGINE = InnoDB');
  17.         $this->addSql('ALTER TABLE customer_stage CHARACTER SET = utf8mb4');
  18.         $this->addSql('UPDATE customer_stage SET status = 0 WHERE status IS NULL');
  19.         $this->addSql('ALTER TABLE customer_stage ADD modified_at DATETIME DEFAULT NULL');
  20.         $this->addSql('UPDATE customer_stage SET modified_at = date_added');
  21.         $this->addSql('
  22.             ALTER TABLE customer_stage
  23.                 ADD completed TINYINT(1) NOT NULL,
  24.                 CHANGE customers_id customer_id INT DEFAULT NULL,
  25.                 CHANGE administrators_id user_id INT DEFAULT NULL,
  26.                 CHANGE stages_items_id stage_item_id INT DEFAULT NULL,
  27.                 CHANGE status enabled TINYINT(1) NOT NULL,
  28.                 CHANGE date_emailed sent_at DATETIME DEFAULT NULL,
  29.                 CHANGE date_added created_at DATETIME NOT NULL,
  30.                 CHANGE modified_at modified_at DATETIME NOT NULL
  31.         ');
  32.         $this->addSql('UPDATE customer_stage SET completed = 1 WHERE enabled = 1');
  33.         $this->addSql('DROP INDEX idx_customers_id ON customer_stage');
  34.         $this->addSql('DROP INDEX idx_administrators_id ON customer_stage');
  35.         $this->addSql('DROP INDEX idx_stages_items_id ON customer_stage');
  36.         $this->addSql('CREATE INDEX IDX_C49E6529395C3F3 ON customer_stage (customer_id)');
  37.         $this->addSql('CREATE INDEX IDX_C49E652A76ED395 ON customer_stage (user_id)');
  38.         $this->addSql('CREATE INDEX IDX_C49E65298961213 ON customer_stage (stage_item_id)');
  39.         $this->addSql('DELETE FROM customer_stage WHERE customer_id NOT IN (SELECT id FROM customer)');
  40.         $this->addSql('UPDATE customer_stage SET user_id = NULL WHERE user_id NOT IN (SELECT id FROM user)');
  41.         $this->addSql('DELETE FROM customer_stage WHERE stage_item_id NOT IN (SELECT id FROM config_stage_item)');
  42.         $this->addSql('ALTER TABLE customer_stage ADD CONSTRAINT FK_C49E6529395C3F3 FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE');
  43.         $this->addSql('ALTER TABLE customer_stage ADD CONSTRAINT FK_C49E652A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE SET NULL');
  44.         $this->addSql('ALTER TABLE customer_stage ADD CONSTRAINT FK_C49E65298961213 FOREIGN KEY (stage_item_id) REFERENCES config_stage_item (id) ON DELETE CASCADE');
  45.     }
  46.     public function down(Schema $schema): void
  47.     {
  48.         // this down() migration is auto-generated, please modify it to your needs
  49.         $this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
  50.         $this->addSql('ALTER TABLE customer_stage DROP FOREIGN KEY FK_C49E65298961213');
  51.         $this->addSql('ALTER TABLE customer_stage DROP FOREIGN KEY FK_C49E652A76ED395');
  52.         $this->addSql('ALTER TABLE customer_stage DROP FOREIGN KEY FK_C49E6529395C3F3');
  53.         $this->addSql('RENAME TABLE customer_stage TO customers_stages_items');
  54.         $this->addSql('ALTER TABLE customers_stages_items ENGINE = InnoDB');
  55.         $this->addSql('ALTER TABLE customers_stages_items CHARACTER SET = utf8mb4');
  56.         $this->addSql('DELETE customers_stages_items SET status = 1 AND completed = 0');
  57.         $this->addSql('
  58.             ALTER TABLE customers_stages_items
  59.                 DROP completed,
  60.                 DROP modified_at,
  61.                 CHANGE customer_id customers_id INT DEFAULT NULL,
  62.                 CHANGE user_id administrators_id INT NOT NULL,
  63.                 CHANGE stage_item_id stages_items_id INT DEFAULT NULL,
  64.                 CHANGE enabled status TINYINT(1) DEFAULT \'1\',
  65.                 CHANGE sent_at date_emailed DATETIME DEFAULT NULL,
  66.                 CHANGE created_at date_added DATETIME DEFAULT NULL
  67.         ');
  68.         $this->addSql('DROP INDEX IDX_C49E6529395C3F3 ON customers_stages_items');
  69.         $this->addSql('DROP INDEX IDX_C49E652A76ED395 ON customers_stages_items');
  70.         $this->addSql('DROP INDEX IDX_C49E65298961213 ON customers_stages_items');
  71.         $this->addSql('CREATE INDEX idx_customers_id ON customers_stages_items (customers_id)');
  72.         $this->addSql('CREATE INDEX idx_stages_items_id ON customers_stages_items (stages_items_id)');
  73.         $this->addSql('CREATE INDEX idx_administrators_id ON customers_stages_items (administrators_id)');
  74.     }
  75. }