Skip to content

Commit

Permalink
[Doctrine] Fix migration to allow running on postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
NoResponseMate committed Jun 24, 2024
1 parent b7f81e5 commit 7021024
Showing 1 changed file with 34 additions and 3 deletions.
37 changes: 34 additions & 3 deletions migrations/Version20230630102308.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,57 @@

namespace CommerceWeaversSyliusAlsoBoughtMigrations;

use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;
use Sylius\Bundle\CoreBundle\Doctrine\Migrations\AbstractMigration;

final class Version20230630102308 extends AbstractMigration
{
public function preUp(Schema $schema): void
{
// Intentionally left empty to override the parent method
}

public function preDown(Schema $schema): void
{
// Intentionally left empty to override the parent method
}

public function getDescription(): string
{
return 'Setup the database for the plugin';
}

public function up(Schema $schema): void
{
$this->addSql('CREATE TABLE sylius_also_bought_product_synchronization (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', start_date DATETIME NOT NULL, end_date DATETIME DEFAULT NULL, number_of_orders INT NOT NULL, affected_products JSON NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB');
$this->abortIf(
!$this->isMySql() && !$this->isPostgreSQL(),
'This migration can only be executed on \'MySQL\' or \'PostgreSQL\'.',
);

if ($this->isMySql()) {
$this->addSql('CREATE TABLE sylius_also_bought_product_synchronization (id BINARY(16) NOT NULL COMMENT \'(DC2Type:uuid)\', start_date DATETIME NOT NULL, end_date DATETIME DEFAULT NULL, number_of_orders INT NOT NULL, affected_products JSON NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB');
$this->addSql('ALTER TABLE sylius_product ADD bought_together_products JSON NOT NULL');
$this->addSql('UPDATE sylius_product SET bought_together_products = JSON_ARRAY()');

return;
}

$this->addSql('CREATE TABLE sylius_also_bought_product_synchronization (id UUID NOT NULL, start_date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, end_date TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, number_of_orders INT NOT NULL, affected_products JSON NOT NULL, PRIMARY KEY(id))');
$this->addSql('COMMENT ON COLUMN sylius_also_bought_product_synchronization.id IS \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE sylius_product ADD bought_together_products JSON NOT NULL');
$this->addSql('UPDATE sylius_product SET bought_together_products = JSON_ARRAY()');
}

public function down(Schema $schema): void
{
$this->addSql('DROP TABLE sylius_also_bought_product_synchronization');
$this->addSql('ALTER TABLE sylius_product DROP bought_together_products');
}

protected function isPostgreSQL(): bool
{
return
class_exists(PostgreSQLPlatform::class) &&
is_a($this->connection->getDatabasePlatform(), PostgreSQLPlatform::class, true);
}
}

0 comments on commit 7021024

Please sign in to comment.