<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20180822163302 extends AbstractMigration
{
public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('RENAME TABLE customers_stages_items TO customer_stage');
$this->addSql('ALTER TABLE customer_stage ENGINE = InnoDB');
$this->addSql('ALTER TABLE customer_stage CHARACTER SET = utf8mb4');
$this->addSql('UPDATE customer_stage SET status = 0 WHERE status IS NULL');
$this->addSql('ALTER TABLE customer_stage ADD modified_at DATETIME DEFAULT NULL');
$this->addSql('UPDATE customer_stage SET modified_at = date_added');
$this->addSql('
ALTER TABLE customer_stage
ADD completed TINYINT(1) NOT NULL,
CHANGE customers_id customer_id INT DEFAULT NULL,
CHANGE administrators_id user_id INT DEFAULT NULL,
CHANGE stages_items_id stage_item_id INT DEFAULT NULL,
CHANGE status enabled TINYINT(1) NOT NULL,
CHANGE date_emailed sent_at DATETIME DEFAULT NULL,
CHANGE date_added created_at DATETIME NOT NULL,
CHANGE modified_at modified_at DATETIME NOT NULL
');
$this->addSql('UPDATE customer_stage SET completed = 1 WHERE enabled = 1');
$this->addSql('DROP INDEX idx_customers_id ON customer_stage');
$this->addSql('DROP INDEX idx_administrators_id ON customer_stage');
$this->addSql('DROP INDEX idx_stages_items_id ON customer_stage');
$this->addSql('CREATE INDEX IDX_C49E6529395C3F3 ON customer_stage (customer_id)');
$this->addSql('CREATE INDEX IDX_C49E652A76ED395 ON customer_stage (user_id)');
$this->addSql('CREATE INDEX IDX_C49E65298961213 ON customer_stage (stage_item_id)');
$this->addSql('DELETE FROM customer_stage WHERE customer_id NOT IN (SELECT id FROM customer)');
$this->addSql('UPDATE customer_stage SET user_id = NULL WHERE user_id NOT IN (SELECT id FROM user)');
$this->addSql('DELETE FROM customer_stage WHERE stage_item_id NOT IN (SELECT id FROM config_stage_item)');
$this->addSql('ALTER TABLE customer_stage ADD CONSTRAINT FK_C49E6529395C3F3 FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE customer_stage ADD CONSTRAINT FK_C49E652A76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE customer_stage ADD CONSTRAINT FK_C49E65298961213 FOREIGN KEY (stage_item_id) REFERENCES config_stage_item (id) ON DELETE CASCADE');
}
public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf('mysql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE customer_stage DROP FOREIGN KEY FK_C49E65298961213');
$this->addSql('ALTER TABLE customer_stage DROP FOREIGN KEY FK_C49E652A76ED395');
$this->addSql('ALTER TABLE customer_stage DROP FOREIGN KEY FK_C49E6529395C3F3');
$this->addSql('RENAME TABLE customer_stage TO customers_stages_items');
$this->addSql('ALTER TABLE customers_stages_items ENGINE = InnoDB');
$this->addSql('ALTER TABLE customers_stages_items CHARACTER SET = utf8mb4');
$this->addSql('DELETE customers_stages_items SET status = 1 AND completed = 0');
$this->addSql('
ALTER TABLE customers_stages_items
DROP completed,
DROP modified_at,
CHANGE customer_id customers_id INT DEFAULT NULL,
CHANGE user_id administrators_id INT NOT NULL,
CHANGE stage_item_id stages_items_id INT DEFAULT NULL,
CHANGE enabled status TINYINT(1) DEFAULT \'1\',
CHANGE sent_at date_emailed DATETIME DEFAULT NULL,
CHANGE created_at date_added DATETIME DEFAULT NULL
');
$this->addSql('DROP INDEX IDX_C49E6529395C3F3 ON customers_stages_items');
$this->addSql('DROP INDEX IDX_C49E652A76ED395 ON customers_stages_items');
$this->addSql('DROP INDEX IDX_C49E65298961213 ON customers_stages_items');
$this->addSql('CREATE INDEX idx_customers_id ON customers_stages_items (customers_id)');
$this->addSql('CREATE INDEX idx_stages_items_id ON customers_stages_items (stages_items_id)');
$this->addSql('CREATE INDEX idx_administrators_id ON customers_stages_items (administrators_id)');
}
}