migrations/Version20190404201235.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. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  7. use Symfony\Component\DependencyInjection\ContainerAwareTrait;
  8. /**
  9.  * Auto-generated Migration: Please modify to your needs!
  10.  */
  11. final class Version20190404201235 extends AbstractMigration implements ContainerAwareInterface
  12. {
  13.     use ContainerAwareTrait;
  14.     public function getDescription(): string
  15.     {
  16.         return '';
  17.     }
  18.     public function up(Schema $schema): void
  19.     {
  20.         // this up() migration is auto-generated, please modify it to your needs
  21.         $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql''Migration can only be executed safely on \'mysql\'.');
  22.         $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');
  23.         $this->addSql('ALTER TABLE order_item ADD CONSTRAINT FK_52EA1F098D9F6D38 FOREIGN KEY (order_id) REFERENCES orders (id)');
  24.         $this->addSql('ALTER TABLE order_item ADD CONSTRAINT FK_52EA1F09C3F36F5F FOREIGN KEY (product_id) REFERENCES productsForSale (id)');
  25.         // migrate data
  26.         $sql ='insert into order_item(order_id, created_at, updated_at, name, quantity, price, product_id) '
  27.             'select orders.id, cartItems.createdAt, cartItems.updatedAt, productsForSale.name, cartItems.quantity, cartItems.unitPrice, cartItems.idProduct '
  28.             'from cartItems '
  29.             'JOIN carts ON carts.id = cartItems.idCart '
  30.             'JOIN productsForSale ON productsForSale.id = cartItems.idProduct '
  31.             'JOIN orders ON orders.idCart = carts.id';
  32.         $this->addSql($sql);
  33.     }
  34.     public function postUp(Schema $schema): void
  35.     {
  36.     }
  37.     public function down(Schema $schema): void
  38.     {
  39.         // this down() migration is auto-generated, please modify it to your needs
  40.         $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql''Migration can only be executed safely on \'mysql\'.');
  41.         $this->addSql('DROP TABLE order_item');
  42.     }
  43. }