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 for running bin/magento without proper database connection (removes db connection attempts from DI constructors) #37424

Closed
wants to merge 6 commits into from

Conversation

hostep
Copy link
Contributor

@hostep hostep commented Apr 24, 2023

Fix for running bin/magento without proper database connection (removes db connection attempts from DI constructors)

Remove obsolete ResourceModel\Product dependency and reworked DateTime to DateTimeFactory. Both of these caused unnecessary connections to be made to the database when executing bin/magento commands.

Description (*)

Whenever you execute bin/magento (with our without explicit commands), Magento will load all registered Commands and load all their dependencies through DI. Those dependencies will load their own dependencies, and so on and so forth.

I've noticed that through the whole depenencies tree, some dependencies tried to fetch something from the database. This is really not needed when executing bin/magento without providing an explicit command to actually do something with the database.

The removal of the Magento\Catalog\Model\ResourceModel\Product dependency should be safe, as it wasn't being used in the class anyways.
The change of the DateTime to DateTimeFactory causes that no locale info from the database is being requested in the constructor. It should only happen at runtime and was inspired by this code here.

These 2 dependencies were loaded from the commands bin/magento media-gallery:sync & bin/magento media-content:sync

Related Pull Requests

Fixed Issues (if relevant)

  1. Fixes Running setup:config:set can crash in certain situations, seems to be bug introduced in Magento 2.4.1 #37409
  2. Fixes php bin/magento setup:di:compile requires database connection #36845

Manual testing scenarios (*)

  1. Setup clean Magento and install it
  2. Modify the app/etc/env.php file and change the correct database user to an incorrect one
  3. Run bin/magento
  4. Expected is that no error messages show up until you actually want to do something to the database with an explicit command
  5. Change back to a correct database user in the app/etc/env.php file
  6. Make sure both the commands bin/magento media-gallery:sync & bin/magento media-content:sync still work as expected, those should be the only real commands affected by these changes

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)

…e to DateTimeFactory. Both of these caused unnecessary connections to be made to the database when executing bin/magento commands.
@m2-assistant
Copy link

m2-assistant bot commented Apr 24, 2023

Hi @hostep. 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.

@hostep
Copy link
Contributor Author

hostep commented Apr 24, 2023

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@ihor-sviziev ihor-sviziev added the Priority: P2 A defect with this priority could have functionality issues which are not to expectations. label Apr 25, 2023
@ihor-sviziev
Copy link
Contributor

@hostep thank you so much for your improvement!
Your changes look good!
PS: I hope one day someone will finalize this one #29355, it will way more significantly improve performance of CLI

Copy link
Contributor

@ihor-sviziev ihor-sviziev left a comment

Choose a reason for hiding this comment

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

✔ Approved.

Failing tests look not related to changes from this PR.

@m2-community-project m2-community-project bot moved this from Pending Review to Ready for Testing in Pull Requests Dashboard Apr 25, 2023
@hostep hostep changed the title Remove obsolete ResourceModel\Product dependency and reworked DateTim… Fix for running bin/magento without proper database connection (removes db connection attempts from DI constructors) Apr 25, 2023
@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

1 similar comment
@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please re-request them if they don't show in a reasonable amount of time.

@engcom-Lima engcom-Lima moved this from Ready for Testing to Testing in Progress in Pull Requests Dashboard Apr 25, 2023
…es db connection attempts from DI constructors)


Fix typo

Co-authored-by: Dan Wallis <dan@wallis.nz>
@ihor-sviziev
Copy link
Contributor

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@engcom-Echo
Copy link
Contributor

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@hostep
Copy link
Contributor Author

hostep commented Jul 1, 2023

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@engcom-Echo
Copy link
Contributor

@magento run Functional Tests EE, Functional Tests CE, Functional Tests B2B

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@engcom-Echo
Copy link
Contributor

engcom-Echo commented Jul 3, 2023

Functional Tests B2B and Functional Tests EE failures are different on last two run on same commit.
Functional Tests EE
Run1
image

Run2
image

Functional Tests B2B
Run1
image

Run2
image

Hence moving it to Merge In Progress

@engcom-Charlie
Copy link
Contributor

@magento run all tests

@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

1 similar comment
@magento-automated-testing
Copy link

The requested builds are added to the queue. You should be able to see them here within a few minutes. Please message the #magento-devops slack channel if they don't show in a reasonable amount of time and a representative will look into any issues.

@hostep
Copy link
Contributor Author

hostep commented Jul 15, 2023

It looks like this PR got merged in 2.4-develop recently via: d4a9791

But this PR is not marked as merged, which is quite strange? Should I close it myself?

@engcom-Echo engcom-Echo moved this from Merge in Progress to Recently Merged in Community Dashboard Jul 17, 2023
@ihor-sviziev
Copy link
Contributor

This pull request was merged in 4095595.
Unfortunately, it's not marked as merged due to not merged a "merge commit" :(
Since changes are already in 2.4-develop branch, I'm closing this PR.

@dfelton
Copy link
Contributor

dfelton commented Jul 17, 2023

@ihor-sviziev Just want to call out that this certainly appears to be a backwards incompatible change, and for that reason I'm surprised it was merged into 2.4-develop as opposed to 2.5-develop.

  • The revision to app/code/Magento/MediaContentCatalog/Model/ResourceModel/GetEntityContent.php is rather harmless... the removed constructor parameter was the last argument. And therefore, if any existing code (thinking third party code as opposed to Magento native) was extending it and calling parent::__construct() with a (now extra) argument. This will merely be ignored per how PHP treats such things.

  • However, the change to app/code/Magento/MediaGallerySynchronization/Model/SynchronizeFiles.php is definitely backwards breaking. Any existing code that extends this class, that defines its own constructor, and attempts to call parent::__construct() without having DateTime updated to DateTimeFactory will now be broken.

@hostep
Copy link
Contributor Author

hostep commented Jul 17, 2023

@dfelton: I certainly agree that it's backwards compatible breaking in case 3rd party modules extends those classes, but Magento has this rule they only disallow such code changes on classes that are annotated with @api, which isn't the case here.

I don't think a lot of 3rd party modules will have extended these classes and fixing them would be pretty easy (unless they have to support both older Magento versions and upcoming ones at the same time, but even that can be solved, it's only a bit more complex though, here's an example).

@dfelton
Copy link
Contributor

dfelton commented Jul 17, 2023

but Magento has this rule they only disallow such code changes on classes that are annotated with @api

Ah, thank you for this piece of info @hostep. This makes sense.

I felt compelled to mention it because I have had an open pr with Magento since 2019 for a much smaller change, that is taking forever to get merged in due to backwards incompatibility. However, as you point out, classes marked @api get this extra scrutiny.

Cheers!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: Performance Priority: P2 A defect with this priority could have functionality issues which are not to expectations. Project: Community Picked PRs upvoted by the community QA: Added to Regression Scope Scenario was analysed and added to Regression Testing Scope
Projects
None yet
8 participants