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 Installer Exception Handling - TPROD-194 #9934

Merged

Conversation

mabumusa1
Copy link
Member

@mabumusa1 mabumusa1 commented Apr 25, 2021

Q A
Branch? "features"
Bug fix? yes
New feature? no
Deprecations? no
BC breaks? no
Automated tests included? yes
Related user documentation PR URL
Related developer documentation PR URL
Issue(s) addressed Fixes #9473

Description:

In 4.0alpha, going through the first step of the installer to configure and install the schema fails if using a db table prefix. It works fine without one but one throws an exception like An error occurred while attempting to add default data: Table db_leads does not exist!

To reproduce:

  1. Go through the UI installer (delete or rename app/config/local.php)
  2. Enter the database credentials with a db table prefix and proceed to the next step
  3. Note the error
  4. Repeat without a prefix and you'll be able to complete the installation
    Update
    @alanhartless wrote some patches that fix the issues on the UI.
    The bug exists on the CLI part of the installer, To reproduce the issue
  5. Make sure the cache folder is empty.
  6. Make sure local.php does not exist.
  7. Make sure the database is empty.
  8. Run the command to install Mautic php bin/console mautic:install --db_port 3306 --db_host db --db_name db --db_user db --db_password db --db_table_prefix ma_ https://mautic.ddev.site/ --env=dev
  9. Notice that the prefix is not honored

Run this script to reproduce the issue and for quick testing

#!/bin/bash         
mysql -udb -pdb -e "drop database db; create database db"
rm -rf /var/www/html/var/cache/*
rm /var/www/html/app/config/local.php
php bin/console mautic:install --db_port 3306 --db_host db --db_name db --db_user db --db_password db --db_table_prefix ma_ https://mautic.ddev.site/ --env=dev --admin_firstname=admin --admin_lastname=admin --admin_username=user --admin_email=email@domainc.com --admin_password=password

Steps to test this PR:

  1. Load up this PR
  2. Try to install with wrong DB settings, no exception should show
  3. Add a prefix, database tables should have a prefix

@mabumusa1 mabumusa1 added T1 Low difficulty to fix (issue) or test (PR) bug Issues or PR's relating to bugs regression A bug that broke something in the last release installation Anything related to the installation of Mautic labels Apr 25, 2021
@mabumusa1 mabumusa1 added this to the 4.0-beta milestone Apr 25, 2021
@mabumusa1 mabumusa1 self-assigned this Apr 25, 2021
@cla-bot cla-bot bot added the cla-signed The PR contributors have signed the contributors agreement label Apr 25, 2021
@codecov
Copy link

codecov bot commented Apr 25, 2021

Codecov Report

Merging #9934 (569a0be) into features (a30175e) will increase coverage by 0.02%.
The diff coverage is 45.05%.

Impacted file tree graph

@@              Coverage Diff               @@
##             features    #9934      +/-   ##
==============================================
+ Coverage       41.88%   41.91%   +0.02%     
+ Complexity      34518    34495      -23     
==============================================
  Files            2056     2059       +3     
  Lines          111361   111390      +29     
==============================================
+ Hits            46644    46685      +41     
+ Misses          64717    64705      -12     
Impacted Files Coverage Δ
...reBundle/Doctrine/Connection/ConnectionWrapper.php 0.00% <0.00%> (ø)
...les/InstallBundle/Controller/InstallController.php 4.44% <4.16%> (+0.06%) ⬆️
...ependencyInjection/Compiler/InstallCommandPass.php 15.38% <15.38%> (ø)
...p/bundles/InstallBundle/Install/InstallService.php 32.87% <40.27%> (+5.56%) ⬆️
...p/bundles/InstallBundle/Command/InstallCommand.php 83.23% <95.55%> (+0.56%) ⬆️
...nstallBundle/InstallFixtures/ORM/LeadFieldData.php 76.66% <100.00%> (ø)
app/bundles/InstallBundle/MauticInstallBundle.php 100.00% <100.00%> (ø)
... and 1 more

Copy link
Contributor

@alanhartless alanhartless left a comment

Choose a reason for hiding this comment

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

I'm still getting the same error when I try installing using a database table prefix with this PR.

An error occurred while attempting to add default data: Table mautic_leads does not exist!

@@ -308,19 +308,20 @@ public function createDatabaseStep($step, $dbParams)
{
$translator = $this->translator;

$messages = $this->validateDatabaseParams($dbParams);
$status = $this->validateDatabaseParams($dbParams);
Copy link
Contributor

Choose a reason for hiding this comment

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

validateDatabaseParams() is returning an array of validation errors as well which would be ignored with this change.

@alanhartless
Copy link
Contributor

Hmmm, this is odd. It seems to work when I don't have xdebug on but does not when I have it off. Maybe a timing issue where the slightest delay in processing allows it to work in my Docker environment. Still doing some digging.

@alanhartless
Copy link
Contributor

alanhartless commented Apr 25, 2021

Ok, it's something to do with Symfony's cache and Doctrine's EntityManager (the reason xdebug affected it above is that I have two containers, one with xdebug and one without, so each had their own compiled cache and thus not related directly to the issue). Deleting app/config/local.php and nuking Symfony's cache will replicate the issue and I've been able to track it down to

$metadatas = $this->em->getMetadataFactory()->getAllMetadata();
does not include the table prefix in the table names for the metadata if going through a clean install. If app/config/local.php exists with the prefix defined, deleting the cache, and going through the installer works.

@alanhartless
Copy link
Contributor

alanhartless commented Apr 25, 2021

Doctrine is caching the table names in /prod/doctrine/orm/default_metadata.php and the file is only created when the cache is warmed up (DoctrineMetadataCacheWarmer) so we can't just delete the one file or lose it's benefits. I'd advocate that we remove support for naming a prefix through the UI installer but Mautic and the mautic:install command can continue to support one.

@mabumusa1
Copy link
Member Author

mabumusa1 commented May 5, 2021

@alanhartless How can I support pushing this PR forward?

@RCheesley
Copy link
Sponsor Member

@mabumusa1 we had a discussion on this topic in Slack: https://mautic.slack.com/archives/C01KB7GQFCL/p1619382546118000

@alanhartless
Copy link
Contributor

@mabumusa1 would you like help implementing as discussed in the slack channel?

@npracht npracht modified the milestones: 4.0-beta, 4.0-rc May 14, 2021
@mabumusa1 mabumusa1 force-pushed the fix_installaer_db_config_TPROD-194 branch from e9196ca to df8000e Compare May 26, 2021 18:22
@RCheesley
Copy link
Sponsor Member

@mabumusa1 @luguenth we seem to have a big load of tests failing on this PR now - can you please take a look?

@RCheesley RCheesley closed this Aug 12, 2021
@RCheesley RCheesley reopened this Aug 12, 2021
@dennisameling
Copy link
Member

OK I was able to reproduce the issue locally where tests fail if Mautic is not installed. It tries to redirect to /index.php/installer which is an indication that Mautic thinks it's not installed yet. After installing Mautic locally, the tests pass. The CI pipeline has an "Install Mautic" step so something must be going wrong there, because when running the tests, which comes right after that, Mautic still thinks that it's not installed yet. One possible cause I could think of is that this PR introduced a bug where it doesn't respect the db_name/db_user/etc. that we set in the CI pipeline, and installs into a different database instead (or something similar to this).

I'll have a closer look tomorrow by adding tmate to the CI pipeline, so I can log into the database and see in which database Mautic was actually installed

@dennisameling
Copy link
Member

Created #10350 as a successor to this PR - it's up to date with the latest changes from the features branch and the tests are passing 🎉 therefore closing this one

disha1611 and others added 3 commits August 23, 2021 10:34
…rs (mautic#10303)

* button lable in all Translation language folders

* de folder change translation for button

* Update plugins/GrapesJsBuilderBundle/Translations/cs/javascript.ini

Correct translation for form button label

Co-authored-by: John Linhart <jan@linhart.email>

Co-authored-by: John Linhart <jan@linhart.email>
* docs: update README.md [skip ci]

* docs: update .all-contributorsrc [skip ci]

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
* docs: update README.md [skip ci]

* docs: update .all-contributorsrc [skip ci]

Co-authored-by: allcontributors[bot] <46447321+allcontributors[bot]@users.noreply.github.com>
@dennisameling dennisameling force-pushed the fix_installaer_db_config_TPROD-194 branch from 48ba6d4 to fbb6647 Compare August 23, 2021 08:35
There were some cases (e.g. when the user doesn't provide a table prefix when installing Mautic) where the installer would throw a warning and simply stop. This commit provides some default parameters for the database, enables PHP strict types and improves some parts of the logic.
@dennisameling
Copy link
Member

dennisameling commented Aug 23, 2021

For info - the tests in this PR were failing because of define('MAUTIC_INSTALLER', 1) - the thing here is that the constant is defined once during a PHPUnit test run, but then remains active in the rest of the tests as well. That's causing other, unrelated tests to fail as the logic thinks Mautic still has to be installed. ed82bfe prevents that by only defining MAUTIC_INSTALLER when we're not running tests (IS_PHPUNIT). CC @luguenth

Reopened this PR in favor of #10350

@dennisameling
Copy link
Member

This PR can now be tested again. Please make sure to test this in the following 4 scenarios:

  • CLI installation with a DB prefix
  • CLI installation without a DB prefix
  • UI installation with a DB prefix
  • UI installation without a DB prefix

To clean things up after testing each scenario, you can run ddev delete --omit-snapshot && rm app/config/local.php && rm -rf var/cache && ddev start to start with a clean state.

For CLI installation, simply run the DDEV command below:

ddev exec bin/console mautic:install --mailer_from_name="DDEV" --mailer_from_email="mautic@ddev.local" --mailer_transport="smtp" --mailer_host="localhost" --mailer_port="1025" --db_driver="pdo_mysql" --db_host="db" --db_port="3306" --db_name="db" --db_user="db" --db_password="db" --db_table_prefix="mau_" --db_backup_tables="false" --admin_email="admin@mautic.local" --admin_password="mautic" https://mautic.ddev.site --env=dev

For CLI installation without a prefix, simply leave out --db_table_prefix, so that it looks as follows:

ddev exec bin/console mautic:install --mailer_from_name="DDEV" --mailer_from_email="mautic@ddev.local" --mailer_transport="smtp" --mailer_host="localhost" --mailer_port="1025" --db_driver="pdo_mysql" --db_host="db" --db_port="3306" --db_name="db" --db_user="db" --db_password="db" --db_backup_tables="false" --admin_email="admin@mautic.local" --admin_password="mautic" https://mautic.ddev.site --env=dev

@dennisameling dennisameling added ready-to-test PR's that are ready to test and removed pending-feedback PR's and issues that are awaiting feedback from the author labels Aug 23, 2021
@mabumusa1
Copy link
Member Author

I just tested the new fixes, it works as expected. Thanks @dennisameling

Copy link
Sponsor Member

@RCheesley RCheesley left a comment

Choose a reason for hiding this comment

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

All four scenarios are now working flawlessly, a huuuuge thank you to everybody who has worked on getting this PR over the line!

Let's go! 🚀

@RCheesley RCheesley merged commit c6fff63 into mautic:features Aug 23, 2021
@mabumusa1 mabumusa1 deleted the fix_installaer_db_config_TPROD-194 branch February 13, 2022 12:50
Comment on lines +27 to +35
$definition->addOption(
new InputOption(
'--env',
'-e',
InputOption::VALUE_REQUIRED,
'The Environment name.',
$container->getParameter('kernel.environment')
)
);
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

@mabumusa1 what was the reason the add the options here and not in the InstallCommand itself?

Copy link
Member Author

Choose a reason for hiding this comment

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

@escopecz I think we needed this to be injected before the cache warmup, otherwise, it will not be collected if inserted in the InstallCommand. I am not 100% sure. but I think this is the reason

Copy link
Sponsor Member

Choose a reason for hiding this comment

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

Ok. That's one of the reasons why we need to cover the code with tests. A test would show us the need for it.

As the InstallCommand must have dependencies for Symfony 5 I had to simplify the code. I tested it by clearing the cache and then running the install command with a table prefix param and it is present in the compiler pass. See 3cabc82

Copy link
Member Author

Choose a reason for hiding this comment

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

I totally agree, this PR was not fully covered. Once I am done with the email PR I will work on increasing the coverage for this command

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issues or PR's relating to bugs cla-signed The PR contributors have signed the contributors agreement installation Anything related to the installation of Mautic ready-to-test PR's that are ready to test regression A bug that broke something in the last release T1 Low difficulty to fix (issue) or test (PR)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Installation throwing 500 error if credentials are incorrect