mirror of
https://github.com/Sylius/Sylius.git
synced 2026-07-14 09:02:12 +00:00
Resolve issues with images positions while upgrading the sylius database to 2.0
This commit is contained in:
parent
538a9f32af
commit
42c6328394
3 changed files with 44 additions and 3 deletions
|
|
@ -20,12 +20,28 @@ final class Version20241020131510 extends AbstractMigration
|
||||||
{
|
{
|
||||||
public function getDescription(): string
|
public function getDescription(): string
|
||||||
{
|
{
|
||||||
return 'Add position field to ProductImage entity';
|
return 'Add position field to ProductImage entity and set positions for existing images.';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function up(Schema $schema): void
|
public function up(Schema $schema): void
|
||||||
{
|
{
|
||||||
$this->addSql('ALTER TABLE sylius_product_image ADD position INT NOT NULL');
|
$this->addSql('ALTER TABLE sylius_product_image ADD position INT NOT NULL');
|
||||||
|
|
||||||
|
$this->addSql('SET @row_number = 0;');
|
||||||
|
|
||||||
|
$this->addSql('CREATE TEMPORARY TABLE IF NOT EXISTS image_count_per_product AS
|
||||||
|
SELECT sylius_product_image.owner_id, sylius_product_image.id AS image_id
|
||||||
|
FROM sylius_product_image
|
||||||
|
ORDER BY sylius_product_image.owner_id, sylius_product_image.id');
|
||||||
|
|
||||||
|
$this->addSql('UPDATE sylius_product_image
|
||||||
|
JOIN (
|
||||||
|
SELECT owner_id, image_id, (@row_number := IF(@current_product = owner_id, @row_number + 1, 0)) AS position, @current_product := owner_id
|
||||||
|
FROM image_count_per_product
|
||||||
|
) AS ranked_images ON ranked_images.image_id = sylius_product_image.id
|
||||||
|
SET sylius_product_image.position = ranked_images.position');
|
||||||
|
|
||||||
|
$this->addSql('DROP TEMPORARY TABLE image_count_per_product;');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function down(Schema $schema): void
|
public function down(Schema $schema): void
|
||||||
|
|
|
||||||
|
|
@ -20,12 +20,36 @@ final class Version20241020131533 extends AbstractPostgreSQLMigration
|
||||||
{
|
{
|
||||||
public function getDescription(): string
|
public function getDescription(): string
|
||||||
{
|
{
|
||||||
return 'Add position field to ProductImage entity';
|
return 'Add position field to ProductImage entity and set positions for existing images.';
|
||||||
}
|
}
|
||||||
|
|
||||||
public function up(Schema $schema): void
|
public function up(Schema $schema): void
|
||||||
{
|
{
|
||||||
$this->addSql('ALTER TABLE sylius_product_image ADD position INT NOT NULL');
|
$this->addSql('ALTER TABLE sylius_product_image ADD position INT DEFAULT 0');
|
||||||
|
|
||||||
|
$this->addSql('CREATE TEMPORARY TABLE image_variants_count AS
|
||||||
|
SELECT sylius_product_image.id AS image_id, sylius_product_variant.product_id, COUNT(sylius_product_variant.id) AS variant_count
|
||||||
|
FROM sylius_product_image
|
||||||
|
JOIN sylius_product_image_product_variants AS piv ON sylius_product_image.id = piv.image_id
|
||||||
|
JOIN sylius_product_variant ON piv.variant_id = sylius_product_variant.id
|
||||||
|
GROUP BY sylius_product_image.id, sylius_product_variant.product_id'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->addSql('WITH ranked_images AS (
|
||||||
|
SELECT sylius_product_image.id AS image_id,
|
||||||
|
sylius_product_image.owner_id,
|
||||||
|
ROW_NUMBER() OVER (PARTITION BY sylius_product_image.owner_id ORDER BY sylius_product_image.id) - 1 AS position_rank
|
||||||
|
FROM sylius_product_image
|
||||||
|
)
|
||||||
|
UPDATE sylius_product_image
|
||||||
|
SET position = ranked_images.position_rank
|
||||||
|
FROM ranked_images
|
||||||
|
WHERE sylius_product_image.id = ranked_images.image_id'
|
||||||
|
);
|
||||||
|
|
||||||
|
$this->addSql('ALTER TABLE sylius_product_image ALTER COLUMN position SET NOT NULL');
|
||||||
|
|
||||||
|
$this->addSql('DROP TABLE image_variants_count');
|
||||||
}
|
}
|
||||||
|
|
||||||
public function down(Schema $schema): void
|
public function down(Schema $schema): void
|
||||||
|
|
|
||||||
|
|
@ -19,6 +19,7 @@
|
||||||
<mapped-superclass name="Sylius\Component\Core\Model\ProductImage" table="sylius_product_image">
|
<mapped-superclass name="Sylius\Component\Core\Model\ProductImage" table="sylius_product_image">
|
||||||
<many-to-one field="owner" target-entity="Sylius\Component\Product\Model\ProductInterface" inversed-by="images">
|
<many-to-one field="owner" target-entity="Sylius\Component\Product\Model\ProductInterface" inversed-by="images">
|
||||||
<join-column name="owner_id" referenced-column-name="id" nullable="false" on-delete="CASCADE"/>
|
<join-column name="owner_id" referenced-column-name="id" nullable="false" on-delete="CASCADE"/>
|
||||||
|
<gedmo:sortable-group />
|
||||||
</many-to-one>
|
</many-to-one>
|
||||||
<many-to-many field="productVariants" target-entity="Sylius\Component\Product\Model\ProductVariantInterface" inversed-by="images">
|
<many-to-many field="productVariants" target-entity="Sylius\Component\Product\Model\ProductVariantInterface" inversed-by="images">
|
||||||
<order-by>
|
<order-by>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue