Skip to content
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

Fix "Syntax error or access violation: Duplicate key name" If we app… #38143

Open
wants to merge 2 commits into
base: 2.4-develop
Choose a base branch
from

Conversation

majiddev10
Copy link

@majiddev10 majiddev10 commented Nov 2, 2023

…ly fulltext index and unique constraint on same column.

Description (*)

I have fixed the issue where applying a full-text index and a unique constraint on the same column was causing an error in Magento. The problem has been resolved, and this pull request includes the necessary changes to make this combination work seamlessly.

Error Detail:

Schema File:
image

Error:
image

Manual testing scenarios (*)

  1. Apply a full-text index and a unique constraint to the same column in a Magento database.
  2. Verify that the operation completes successfully without errors.
  3. Insert duplicate and non-duplicate values into the column.
  4. Ensure that duplicate values trigger a unique constraint violation error, while non-duplicate values are allowed.
  5. Confirm that the full-text indexing functionality still works as expected for the column.

Questions or comments

Contribution checklist (*)

  • [*] Pull request has a meaningful description of its purpose
  • [*] All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • All automated tests passed successfully (all builds are green)

Copy link

m2-assistant bot commented Nov 2, 2023

Hi @Abdul-Majid10. Thank you for your contribution!
Here are some useful tips on how you can test your changes using Magento test environment.

Add the comment under your pull request to deploy test or vanilla Magento instance:
  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names.

Allowed build names are:
  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests
  13. Semantic Version Checker

You can find more information about the builds here
ℹ️ Run only required test builds during development. Run all test builds before sending your pull request for review.


For more details, review the Code Contributions documentation.
Join Magento Community Engineering Slack and ask your questions in #github channel.

…y fulltext index and unique constraint on same column.
@engcom-Bravo engcom-Bravo added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Nov 3, 2023
@swnsma swnsma self-requested a review November 13, 2023 22:31
Copy link
Contributor

@swnsma swnsma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @Abdul-Majid10,
Thank you for your contribution!

I think it's a really good Pull Request, however there are some points that I am worry about.
Imagine that PR will be merged to Magento codebase and released in new versions.
Anyone who will update to that Magento version and receive these changes would face next steps (during setup:upgrade call):

  1. All MySQL indexes will be dropped and created again with new name (that may cause issues for merchants with giant DB or those who has some data inconsistency in the DB).
  2. New index name won't be whitelisted in etc/db_schema_whitelist.json files.

So I may suggest 2 options:

  1. Design and create some logic that would generate index name in a backward compatibility way (e.g. won't generate new name for already existing indexes or generate new name for an index if same name already occupied.
  2. Update all available etc/db_schema_whitelist.json and add new index name (that would require changes in other repositories). Also, it may require additional approve procedure as changes would introduce BC breaks.

@majiddev10
Copy link
Author

Hi @swnsma,
Happy to see your response! As you suggest solutions for it, I also want to discuss another solution that came to my mind.

The solution involves adding a 4th argument (bool $includePrefix = false) to the getIndexName function. When its value is true, we can concatenate the prefix, similar to this.
Screenshot 2023-11-16 at 12 04 51 PM

Note: We can only pass includePrefix true when Key already created for other index.
Screenshot 2023-11-16 at 12 16 56 PM

This is just draft to discuss the solution.

@swnsma, Looking forward to your response.

@majiddev10 majiddev10 requested a review from swnsma November 16, 2023 07:46
@swnsma
Copy link
Contributor

swnsma commented Nov 17, 2023

Hi @Abdul-Majid10,
I like flight of your thoughts.

I would only ask to consider:

  1. Maybe it better to make it as a separated method and call it in the places where we need
  2. How to deal in cases in dynamic (e.g. we had index "column1_column2", after added "column1_column2_unq", later we deleted "column1_column2" - how to avoid that our 2nd unique index will be renamed back to "column1_column2"?).

Think that I don't like is that right now we generate index name based on the current DB state.
What if we do things vice-verse and:

  1. By default, we generate name like "column1_column2_unq" with prefix
  2. If name not found - we fallback if index with "old" name conversion exists - "column1_column2" (here it may need to make a smart check that existing index is exactly what we need and not just index with the same name, maybe to check if index type in schema and index type in db are matching).
  3. If we found index with old name conversion and correct type - do nothing. Otherwise - create a new index with a new name conversion.

What do you think?

@swnsma
Copy link
Contributor

swnsma commented Nov 17, 2023

@Abdul-Majid10,

Actually, I have even better point to discuss:
Why don't use "referenceId" from db_schema.xml as index name instead of generating index name based on columns?
Right now it's kind weird that it ignores 'referenceId' in db_schema.xml, meanwhile it looks into db_schema_whitelist.json and search for exact name of the index as it in the DB.
If there are no underwater rocks, using referenceId should make behaviour consistent (maybe it would require to think about additional index name validation - length, pattern etc).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Progress: needs update
Projects
Status: Changes Requested
Development

Successfully merging this pull request may close these issues.

3 participants