<?php
declare(strict_types=1);
namespace DoctrineMigrations;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20190404201235 extends AbstractMigration implements ContainerAwareInterface
{
use ContainerAwareTrait;
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 order_item (id INT AUTO_INCREMENT NOT NULL, order_id INT DEFAULT NULL, created_at DATETIME NOT NULL, updated_at DATETIME NOT NULL, name VARCHAR(100) NOT NULL, quantity INT NOT NULL, price DOUBLE PRECISION NOT NULL, product_id INT DEFAULT NULL, INDEX IDX_52EA1F098D9F6D38 (order_id), INDEX IDX_52EA1F09C3F36F5F (product_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci ENGINE = InnoDB');
$this->addSql('ALTER TABLE order_item ADD CONSTRAINT FK_52EA1F098D9F6D38 FOREIGN KEY (order_id) REFERENCES orders (id)');
$this->addSql('ALTER TABLE order_item ADD CONSTRAINT FK_52EA1F09C3F36F5F FOREIGN KEY (product_id) REFERENCES productsForSale (id)');
// migrate data
$sql ='insert into order_item(order_id, created_at, updated_at, name, quantity, price, product_id) '
. 'select orders.id, cartItems.createdAt, cartItems.updatedAt, productsForSale.name, cartItems.quantity, cartItems.unitPrice, cartItems.idProduct '
. 'from cartItems '
. 'JOIN carts ON carts.id = cartItems.idCart '
. 'JOIN productsForSale ON productsForSale.id = cartItems.idProduct '
. 'JOIN orders ON orders.idCart = carts.id';
$this->addSql($sql);
}
public function postUp(Schema $schema): void
{
}
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('DROP TABLE order_item');
}
}