-
Notifications
You must be signed in to change notification settings - Fork 29
Fix: duplicate attribute slugs #381
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ahuininga-orisha
wants to merge
8
commits into
develop
Choose a base branch
from
fix/duplicate-attribute-slugs
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4577fe7
Fix: duplicate attribute slug
70447be
Refactor
392063f
Merge with develop
ahuininga-orisha dda1088
Add tests
ahuininga-orisha a699cab
Fix tests
ahuininga-orisha 30f9aa2
Update FilterSlugManagerTest.php
ahuininga-orisha 42ba574
Fix tests
ahuininga-orisha 5875c70
Update FilterSlugManager.php
ahuininga-orisha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tweakwise\Magento2Tweakwise\Api\Data; | ||
|
|
||
| use Magento\Framework\ObjectManagerInterface; | ||
|
|
||
| class AttributeSlugInterfaceFactory | ||
| { | ||
| /** | ||
| * @param ObjectManagerInterface $objectManager | ||
| * @param string $instanceName | ||
| */ | ||
| public function __construct( | ||
| private readonly ObjectManagerInterface $objectManager, | ||
| private readonly string $instanceName = AttributeSlugInterface::class | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @param array $data | ||
| * @return AttributeSlugInterface | ||
| */ | ||
| public function create(array $data = []): AttributeSlugInterface | ||
| { | ||
| return $this->objectManager->create($this->instanceName, $data); | ||
| } | ||
| } |
29 changes: 29 additions & 0 deletions
29
src/Api/Data/AttributeSlugSearchResultsInterfaceFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tweakwise\Magento2Tweakwise\Api\Data; | ||
|
|
||
| use Magento\Framework\ObjectManagerInterface; | ||
|
|
||
| class AttributeSlugSearchResultsInterfaceFactory | ||
| { | ||
| /** | ||
| * @param ObjectManagerInterface $objectManager | ||
| * @param string $instanceName | ||
| */ | ||
| public function __construct( | ||
| private readonly ObjectManagerInterface $objectManager, | ||
| private readonly string $instanceName = AttributeSlugSearchResultsInterface::class | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @param array $data | ||
| * @return AttributeSlugSearchResultsInterface | ||
| */ | ||
| public function create(array $data = []): AttributeSlugSearchResultsInterface | ||
| { | ||
| return $this->objectManager->create($this->instanceName, $data); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/Model/ResourceModel/AttributeSlug/CollectionFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tweakwise\Magento2Tweakwise\Model\ResourceModel\AttributeSlug; | ||
|
|
||
| use Magento\Framework\ObjectManagerInterface; | ||
|
|
||
| class CollectionFactory | ||
| { | ||
| /** | ||
| * @param ObjectManagerInterface $objectManager | ||
| * @param string $instanceName | ||
| */ | ||
| public function __construct( | ||
| private readonly ObjectManagerInterface $objectManager, | ||
| private readonly string $instanceName = Collection::class | ||
| ) { | ||
| } | ||
|
|
||
| /** | ||
| * @param array $data | ||
| * @return Collection | ||
| */ | ||
| public function create(array $data = []): Collection | ||
| { | ||
| return $this->objectManager->create($this->instanceName, $data); | ||
| } | ||
| } |
102 changes: 102 additions & 0 deletions
102
src/Setup/Patch/Schema/FixSlugUniqueIndexOnTweakwiseAttributeSlugTable.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Tweakwise\Magento2Tweakwise\Setup\Patch\Schema; | ||
|
|
||
| use Magento\Framework\Setup\Patch\SchemaPatchInterface; | ||
| use Magento\Framework\Setup\SchemaSetupInterface; | ||
|
|
||
| class FixSlugUniqueIndexOnTweakwiseAttributeSlugTable implements SchemaPatchInterface | ||
| { | ||
| /** | ||
| * @param SchemaSetupInterface $schemaSetup | ||
| */ | ||
| public function __construct(private readonly SchemaSetupInterface $schemaSetup) | ||
| { | ||
| } | ||
|
|
||
| /** | ||
| * @return $this | ||
| */ | ||
| public function apply() | ||
| { | ||
| $setup = $this->schemaSetup; | ||
| $setup->startSetup(); | ||
| $connection = $setup->getConnection(); | ||
| $tableName = $setup->getTable('tweakwise_attribute_slug'); | ||
|
|
||
| if ($connection->isTableExists($tableName)) { | ||
| $this->dropGlobalSlugIndex($tableName); | ||
| $this->addCompositeSlugStoreIndex($tableName); | ||
| } | ||
|
|
||
| $setup->endSetup(); | ||
|
|
||
| return $this; | ||
| } | ||
|
|
||
| /** | ||
| * Drops the global unique index on `slug` that was created by InstallSchema. | ||
| * | ||
| * @param string $tableName | ||
| * @return void | ||
| */ | ||
| private function dropGlobalSlugIndex(string $tableName): void | ||
| { | ||
| $connection = $this->schemaSetup->getConnection(); | ||
|
|
||
| foreach ($connection->getIndexList($tableName) as $indexName => $indexData) { | ||
| $columns = array_map('strtolower', $indexData['COLUMNS_LIST'] ?? []); | ||
| if ($columns === ['slug'] && strtoupper($indexData['INDEX_TYPE'] ?? '') === 'UNIQUE') { | ||
| $connection->dropIndex($tableName, $indexName); | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Adds a composite unique index on `(slug, store_id)` if it does not exist yet. | ||
| * | ||
| * @param string $tableName | ||
| * @return void | ||
| */ | ||
| private function addCompositeSlugStoreIndex(string $tableName): void | ||
| { | ||
| $setup = $this->schemaSetup; | ||
| $connection = $setup->getConnection(); | ||
|
|
||
| foreach ($connection->getIndexList($tableName) as $indexData) { | ||
| $columns = array_map('strtolower', $indexData['COLUMNS_LIST'] ?? []); | ||
| sort($columns); | ||
| if ($columns === ['slug', 'store_id'] && strtoupper($indexData['INDEX_TYPE'] ?? '') === 'UNIQUE') { | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| $connection->addIndex( | ||
| $tableName, | ||
| $setup->getIdxName($tableName, ['slug', 'store_id'], 'unique'), | ||
| ['slug', 'store_id'], | ||
| 'unique' | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return class-string[] | ||
| */ | ||
| public static function getDependencies() | ||
| { | ||
| return [ | ||
| ChangePrimaryKeyTweakwiseAttributeSlugTable::class, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @return string[] | ||
| */ | ||
| public function getAliases() | ||
| { | ||
| return []; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$this->lookupTable = null;or$this->lookupTable = [];?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getLookupTable() has a if ($this->lookupTable === null) guard (line 267) that triggers a DB reload
Setting it to [] would skip that check and return an empty array on the next call, meaning the in-memory cache would never be reloaded after a truncate