Skip to content

Commit

Permalink
Fixed custom_entity_attribute_set_id column migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dmkhr authored and PierreGauthier committed Jul 26, 2023
1 parent 1a7f290 commit 9b5ff3c
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 0 deletions.
75 changes: 75 additions & 0 deletions Setup/Patch/Data/CustomEntityAttributeSetIdMigration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace Smile\CustomEntity\Setup\Patch\Data;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\Patch\DataPatchInterface;

/**
* Migrate custom_entity_attribute_set_id column data from `catalog_eav_attribute` to `eav_attribute` table.
*/
class CustomEntityAttributeSetIdMigration implements DataPatchInterface
{
/**
* Attribute set id column name.
*/
public const ATTRIBUTE_SET_ID_COLUMN = 'custom_entity_attribute_set_id';

/**
* Resource connection.
*/
protected ResourceConnection $resourceConnection;

/**
* Constructor.
*/
public function __construct(
ResourceConnection $resourceConnection
) {
$this->resourceConnection = $resourceConnection;
}

/**
* @inheritdoc
*/
public function apply()
{
$connection = $this->resourceConnection->getConnection();

$catalogEavTable = $connection->getTableName('catalog_eav_attribute');
$eavTable = $connection->getTableName('eav_attribute');

if (
$connection->tableColumnExists($catalogEavTable, self::ATTRIBUTE_SET_ID_COLUMN) &&
$connection->tableColumnExists($eavTable, self::ATTRIBUTE_SET_ID_COLUMN)
) {
$select = $connection->select()
->from(['cea' => 'catalog_eav_attribute'], [self::ATTRIBUTE_SET_ID_COLUMN])
->where('ea.attribute_id = cea.attribute_id');

$connection->query(
$connection->updateFromSelect($select, ['ea' => 'eav_attribute'])
);
}

return $this;
}

/**
* @inheritDoc
*/
public static function getDependencies()
{
return [];
}

/**
* @inheritDoc
*/
public function getAliases()
{
return [];
}
}
71 changes: 71 additions & 0 deletions Setup/Patch/Schema/RemoveCatalogAttributeCustomEntitySetId.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

declare(strict_types=1);

namespace Smile\CustomEntity\Setup\Patch\Schema;

use Magento\Framework\App\ResourceConnection;
use Magento\Framework\Setup\Patch\SchemaPatchInterface;
use Smile\CustomEntity\Setup\Patch\Data\CustomEntityAttributeSetIdMigration;

class RemoveCatalogAttributeCustomEntitySetId implements SchemaPatchInterface
{
/**
* Resource connection.
*/
protected ResourceConnection $resourceConnection;

/**
* Constructor.
*/
public function __construct(
ResourceConnection $resourceConnection
) {
$this->resourceConnection = $resourceConnection;
}

/**
* @inheritdoc
*/
public function apply()
{
$connection = $this->resourceConnection->getConnection();

$catalogEavTable = $connection->getTableName('catalog_eav_attribute');
$eavTable = $connection->getTableName('eav_attribute');

if (
$connection->tableColumnExists(
$catalogEavTable,
CustomEntityAttributeSetIdMigration::ATTRIBUTE_SET_ID_COLUMN
) &&
$connection->tableColumnExists(
$eavTable,
CustomEntityAttributeSetIdMigration::ATTRIBUTE_SET_ID_COLUMN
)
) {
$connection->dropColumn(
$catalogEavTable,
CustomEntityAttributeSetIdMigration::ATTRIBUTE_SET_ID_COLUMN
);
}

return $this;
}

/**
* @inheritDoc
*/
public static function getDependencies()
{
return [CustomEntityAttributeSetIdMigration::class];
}

/**
* @inheritDoc
*/
public function getAliases()
{
return [];
}
}

0 comments on commit 9b5ff3c

Please sign in to comment.