Skip to content

Commit

Permalink
[Framework] Fix parameters groups migrations (#3557)
Browse files Browse the repository at this point in the history
Co-authored-by: Michal Vanek <[email protected]>
Co-authored-by: Martin Grossmann <[email protected]>
  • Loading branch information
3 people authored Nov 1, 2024
1 parent 644a4d0 commit a3cec58
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 29 deletions.
27 changes: 0 additions & 27 deletions src/Migrations/Version20241007064158.php

This file was deleted.

67 changes: 67 additions & 0 deletions src/Migrations/Version20241101073720.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration;

class Version20241101073720 extends AbstractMigration
{
/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function up(Schema $schema): void
{
if ($this->isAppMigrationNotInstalledRemoveIfExists('Version20200220124729')) {
$this->sql('
CREATE TABLE parameter_groups (
id SERIAL NOT NULL,
akeneo_code VARCHAR(100) NOT NULL,
PRIMARY KEY(id)
)');
$this->sql('CREATE UNIQUE INDEX UNIQ_14133381CC7118A2 ON parameter_groups (akeneo_code)');
$this->sql('ALTER TABLE parameters ADD group_id INT DEFAULT NULL');
$this->sql('ALTER TABLE parameters ADD akeneo_code VARCHAR(100) DEFAULT NULL');
$this->sql('ALTER TABLE parameters ADD akeneo_type VARCHAR(100) DEFAULT NULL');

$this->sql('
ALTER TABLE
parameters
ADD
CONSTRAINT FK_69348FEFE54D947 FOREIGN KEY (group_id) REFERENCES parameter_groups (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->sql('CREATE UNIQUE INDEX UNIQ_69348FECC7118A2 ON parameters (akeneo_code)');
$this->sql('CREATE INDEX IDX_69348FEFE54D947 ON parameters (group_id)');

$this->sql('
CREATE TABLE parameter_groups_translations (
id SERIAL NOT NULL,
translatable_id INT NOT NULL,
name VARCHAR(100) NOT NULL,
locale VARCHAR(255) NOT NULL,
PRIMARY KEY(id)
)');
$this->sql('CREATE INDEX IDX_9BF623102C2AC5D3 ON parameter_groups_translations (translatable_id)');
$this->sql('
CREATE UNIQUE INDEX parameter_groups_translations_uniq_trans ON parameter_groups_translations (translatable_id, locale)');
$this->sql('
ALTER TABLE
parameter_groups_translations
ADD
CONSTRAINT FK_9BF623102C2AC5D3 FOREIGN KEY (translatable_id) REFERENCES parameter_groups (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->sql('ALTER TABLE parameter_groups ADD ordering_priority INT NOT NULL');
}

$this->sql('ALTER TABLE parameter_groups DROP COLUMN IF EXISTS akeneo_code');
$this->sql('ALTER TABLE parameters DROP COLUMN IF EXISTS akeneo_code');
$this->sql('ALTER TABLE parameters DROP COLUMN IF EXISTS akeneo_type');
}

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function down(Schema $schema): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use Doctrine\DBAL\Schema\Schema;
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration;

class Version20241018094304 extends AbstractMigration
class Version20241101081004 extends AbstractMigration
{
/**
* @param \Doctrine\DBAL\Schema\Schema $schema
Expand Down
37 changes: 37 additions & 0 deletions src/Migrations/Version20241101083720.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Shopsys\MigrationBundle\Component\Doctrine\Migrations\AbstractMigration;

final class Version20241101083720 extends AbstractMigration
{
/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function up(Schema $schema): void
{
$this->sql('ALTER TABLE parameter_groups ADD position INT NOT NULL DEFAULT 0');
$this->sql('WITH ordered_groups AS (
SELECT id,
ROW_NUMBER() OVER (ORDER BY ordering_priority DESC, id ASC) - 1 AS new_position
FROM parameter_groups
)
UPDATE parameter_groups
SET position = ordered_groups.new_position
FROM ordered_groups
WHERE parameter_groups.id = ordered_groups.id');
$this->sql('ALTER TABLE parameter_groups ALTER position DROP DEFAULT');
$this->sql('ALTER TABLE parameter_groups DROP ordering_priority');
}

/**
* @param \Doctrine\DBAL\Schema\Schema $schema
*/
public function down(Schema $schema): void
{
}
}
2 changes: 1 addition & 1 deletion src/Model/Product/Parameter/ParameterRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ protected function getProductParameterValuesByProductSortedByOrderingPriorityAnd
'product_id' => $product->getId(),
'locale' => $locale,
])
->orderBy('p.orderingPriority', 'DESC')
->orderBy('p.position', 'ASC')
->addOrderBy('pt.name');
}

Expand Down

0 comments on commit a3cec58

Please sign in to comment.