<?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 Version20191126041220 extends AbstractMigration
{
public function getDescription() : string
{
return '';
}
public function up(Schema $schema) : void
{
// this up() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('CREATE TABLE checking_account_movement (id INT AUTO_INCREMENT NOT NULL, checking_account_id INT DEFAULT NULL, movement_type VARCHAR(20) NOT NULL, created_by_id INT DEFAULT NULL, updated_by_id INT DEFAULT NULL, amount NUMERIC(10, 2) NOT NULL, notes TEXT(500) DEFAULT NULL, balance NUMERIC(10, 2) NOT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, INDEX IDX_E22B6B12F0B3EF62 (checking_account_id), INDEX IDX_E22B6B12B03A8386 (created_by_id), INDEX IDX_E22B6B12896DBBDE (updated_by_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE checking_account_movement ADD CONSTRAINT FK_E22B6B12F0B3EF62 FOREIGN KEY (checking_account_id) REFERENCES checking_account (id)');
$this->addSql('ALTER TABLE checking_account_movement ADD CONSTRAINT FK_E22B6B12B03A8386 FOREIGN KEY (created_by_id) REFERENCES users (id)');
$this->addSql('ALTER TABLE checking_account_movement ADD CONSTRAINT FK_E22B6B12896DBBDE FOREIGN KEY (updated_by_id) REFERENCES users (id)');
$this->addSql('ALTER TABLE checking_account DROP FOREIGN KEY FK_23EB7C35D3564642');
$this->addSql('ALTER TABLE checking_account DROP FOREIGN KEY FK_23EB7C35E8DE7170');
$this->addSql('DROP INDEX IDX_23EB7C35E8DE7170 ON checking_account');
$this->addSql('DROP INDEX IDX_23EB7C35D3564642 ON checking_account');
$this->addSql('ALTER TABLE checking_account CHANGE updatedby updated_by_id INT DEFAULT NULL, CHANGE updatedat updated_at DATETIME NOT NULL');
$this->addSql('ALTER TABLE checking_account CHANGE createdby created_by_id INT DEFAULT NULL, CHANGE createdat created_at DATETIME NOT NULL');
$this->addSql('ALTER TABLE checking_account ADD CONSTRAINT FK_23EB7C35B03A8386 FOREIGN KEY (created_by_id) REFERENCES users (id)');
$this->addSql('ALTER TABLE checking_account ADD CONSTRAINT FK_23EB7C35896DBBDE FOREIGN KEY (updated_by_id) REFERENCES users (id)');
$this->addSql('CREATE INDEX IDX_23EB7C35B03A8386 ON checking_account (created_by_id)');
$this->addSql('CREATE INDEX IDX_23EB7C35896DBBDE ON checking_account (updated_by_id)');
$this->addSql('ALTER TABLE checking_account ADD balance NUMERIC(10, 2) NOT NULL');
$this->addSql('ALTER TABLE checking_account_movement RENAME INDEX idx_e22b6b12f0b3ef62 TO IDX_C5DB54A0F0B3EF62');
$this->addSql('ALTER TABLE checking_account_movement RENAME INDEX idx_e22b6b12b03a8386 TO IDX_C5DB54A0B03A8386');
$this->addSql('ALTER TABLE checking_account_movement RENAME INDEX idx_e22b6b12896dbbde TO IDX_C5DB54A0896DBBDE');
$this->addSql('INSERT INTO checking_account_movement(checking_account_id, movement_type, created_by_id, created_at, updated_by_id, updated_at, amount, notes, balance) '
. 'SELECT checking_account.id, IF(accounting_entry.entryType = \'Compra\', \'debit\', \'credit\'), IFNULL(accounting_entry.createdBy, 1), '
. 'IFNULL(accounting_entry.createdAt, NOW()), IFNULL(accounting_entry.updatedBy, 1), IFNULL(accounting_entry.updatedAt, NOW()), '
. 'IF(accounting_entry.entryType = "Compra", -accounting_entry.total, accounting_entry.total), accounting_entry.comment, accounting_entry.currentTotal '
. 'FROM checking_account INNER JOIN accounting_entry ON checking_account.idClient=accounting_entry.idClient');
$this->addSql('UPDATE checking_account SET checking_account.balance=(SELECT COALESCE(SUM(checking_account_movement.amount), 0) FROM checking_account_movement WHERE checking_account_movement.checking_account_id=checking_account.id)');
}
public function down(Schema $schema) : void
{
// this down() migration is auto-generated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');
$this->addSql('ALTER TABLE checking_account DROP balance');
$this->addSql('ALTER TABLE checking_account_movement RENAME INDEX idx_c5db54a0b03a8386 TO IDX_E22B6B12B03A8386');
$this->addSql('ALTER TABLE checking_account_movement RENAME INDEX idx_c5db54a0f0b3ef62 TO IDX_E22B6B12F0B3EF62');
$this->addSql('ALTER TABLE checking_account_movement RENAME INDEX idx_c5db54a0896dbbde TO IDX_E22B6B12896DBBDE');
$this->addSql('DROP TABLE checking_account_movement');
$this->addSql('ALTER TABLE checking_account DROP FOREIGN KEY FK_23EB7C35B03A8386');
$this->addSql('ALTER TABLE checking_account DROP FOREIGN KEY FK_23EB7C35896DBBDE');
$this->addSql('DROP INDEX IDX_23EB7C35B03A8386 ON checking_account');
$this->addSql('DROP INDEX IDX_23EB7C35896DBBDE ON checking_account');
$this->addSql('ALTER TABLE checking_account CHANGE updated_by_id updatedby INT DEFAULT NULL, CHANGE updated_at updatedat DATETIME NOT NULL');
$this->addSql('ALTER TABLE checking_account CHANGE created_by_id createdby INT DEFAULT NULL, CHANGE created_at createdat DATETIME NOT NULL');
$this->addSql('ALTER TABLE checking_account ADD CONSTRAINT FK_23EB7C35D3564642 FOREIGN KEY (createdBy) REFERENCES users (id)');
$this->addSql('ALTER TABLE checking_account ADD CONSTRAINT FK_23EB7C35E8DE7170 FOREIGN KEY (updatedBy) REFERENCES users (id)');
$this->addSql('CREATE INDEX IDX_23EB7C35E8DE7170 ON checking_account (updatedBy)');
$this->addSql('CREATE INDEX IDX_23EB7C35D3564642 ON checking_account (createdBy)');
}
}