<?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 Version20180819191832 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('
CREATE TABLE customer_goal_time_join_config_service_code (
goal_time_id INT NOT NULL,
service_code_id INT NOT NULL,
INDEX IDX_B1D999852FC1DFC7 (goal_time_id),
INDEX IDX_B1D99985E85EAC8B (service_code_id),
PRIMARY KEY(goal_time_id, service_code_id)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB
');
$stmt = $this->connection->prepare('SELECT count(1) FROM customers_goal_time');
$stmt->execute();
// get total entries
$total = $stmt->fetchColumn();
// insert using pagination
for ($i = 0; $i < ceil($total / 100); ++$i) {
$stmt = $this->connection->prepare(sprintf('SELECT id, service_codes FROM customers_goal_time ORDER BY id ASC LIMIT %d, 100', 100 * $i));
$stmt->execute();
while ($row = $stmt->fetch(\PDO::FETCH_OBJ)) {
$serviceCodes = $row->service_codes ? json_decode($row->service_codes, true) : [];
if ($serviceCodes && \count($serviceCodes)) {
foreach ($serviceCodes as $serviceCode) {
$this->addSql('INSERT INTO customer_goal_time_join_config_service_code (goal_time_id, service_code_id) VALUES (:goal_time_id, :service_code_id)', [
'goal_time_id' => $row->id,
'service_code_id' => $serviceCode,
]);
}
}
}
}
$this->addSql('RENAME TABLE customers_goal_time TO customer_goal_time');
$this->addSql('ALTER TABLE customer_goal_time ENGINE = InnoDB');
$this->addSql('ALTER TABLE customer_goal_time CHARACTER SET = utf8mb4');
$this->addSql('UPDATE customer_goal_time SET status = 0 WHERE status IS NULL');
$this->addSql('
ALTER TABLE customer_goal_time
DROP service_codes,
CHANGE customers_id customer_id INT DEFAULT NULL,
CHANGE administrators_id user_id INT DEFAULT NULL,
CHANGE notes message LONGTEXT DEFAULT NULL,
CHANGE status enabled TINYINT(1) NOT NULL,
CHANGE date_added created_at DATETIME NOT NULL,
CHANGE last_modified modified_at DATETIME NOT NULL
');
$this->addSql('DROP INDEX idx_customers_id ON customer_goal_time');
$this->addSql('DROP INDEX idx_administrators_id ON customer_goal_time');
$this->addSql('CREATE INDEX IDX_DA5925FD9395C3F3 ON customer_goal_time (customer_id)');
$this->addSql('CREATE INDEX IDX_DA5925FDA76ED395 ON customer_goal_time (user_id)');
$this->addSql('DELETE FROM customer_goal_time WHERE customer_id NOT IN (SELECT id FROM customer)');
$this->addSql('UPDATE customer_goal_time SET user_id = NULL WHERE user_id NOT IN (SELECT id FROM user)');
$this->addSql('DELETE FROM customer_goal_time_join_config_service_code WHERE goal_time_id NOT IN (SELECT id FROM customer_goal_time)');
$this->addSql('DELETE FROM customer_goal_time_join_config_service_code WHERE service_code_id NOT IN (SELECT id FROM config_service_code)');
$this->addSql('ALTER TABLE customer_goal_time ADD CONSTRAINT FK_DA5925FD9395C3F3 FOREIGN KEY (customer_id) REFERENCES customer (id) ON DELETE CASCADE');
$this->addSql('ALTER TABLE customer_goal_time ADD CONSTRAINT FK_DA5925FDA76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE SET NULL');
$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');
$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');
}
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_goal_time_join_config_service_code DROP FOREIGN KEY FK_75ED8FF9E85EAC8B');
$this->addSql('ALTER TABLE customer_goal_time_join_config_service_code DROP FOREIGN KEY FK_75ED8FF92FC1DFC7');
$this->addSql('ALTER TABLE customer_goal_time DROP FOREIGN KEY FK_DA5925FDA76ED395');
$this->addSql('ALTER TABLE customer_goal_time DROP FOREIGN KEY FK_DA5925FD9395C3F3');
$this->addSql('RENAME TABLE customer_goal_time TO customers_goal_time');
$this->addSql('ALTER TABLE customers_goal_time ENGINE = InnoDB');
$this->addSql('ALTER TABLE customers_goal_time CHARACTER SET = utf8mb4');
$this->addSql('
ALTER TABLE customers_goal_time
ADD service_codes LONGTEXT DEFAULT NULL COMMENT \'(DC2Type:array)\' AFTER customers_id,
CHANGE customer_id customers_id INT NOT NULL,
CHANGE user_id administrators_id INT NOT NULL,
CHANGE message notes TEXT DEFAULT NULL,
CHANGE enabled status TINYINT(1) DEFAULT \'0\',
CHANGE created_at date_added DATETIME DEFAULT NULL,
CHANGE modified_at last_modified DATETIME DEFAULT NULL
');
$this->addSql('DROP INDEX IDX_DA5925FD9395C3F3 ON customers_goal_time');
$this->addSql('DROP INDEX IDX_DA5925FDA76ED395 ON customers_goal_time');
$this->addSql('CREATE INDEX idx_administrators_id ON customers_goal_time (administrators_id)');
$this->addSql('CREATE INDEX idx_customers_id ON customers_goal_time (customers_id)');
$stmt = $this->connection->prepare('SELECT * FROM customer_goal_time_join_config_service_code');
$stmt->execute();
$goalTimes = [];
while ($row = $stmt->fetch(\PDO::FETCH_OBJ)) {
$goalTimes[$row->goal_time_id][] = $row->service_code_id;
}
foreach ($goalTimes as $key => $value) {
$serviceCodes = $value ? json_encode($value) : null;
$this->addSql('UPDATE customers_goal_time SET service_codes = :service_codes WHERE id = :id', [
'service_codes' => $serviceCodes,
'id' => $key,
]);
}
$this->addSql('DROP TABLE customer_goal_time_join_config_service_code');
}
}