cloudflare / Cloudflare-Magento

A Cloudflare plugin for Magento2.
https://www.cloudflare.com/integrations/magento/
BSD 3-Clause "New" or "Revised" License
50 stars 17 forks source link

Error when installing via `setup:upgrade` on an existing environment #58

Closed navarr closed 7 years ago

navarr commented 7 years ago

Forewarning: I don't have perfect recreation steps, but wanted to go ahead and let you know what's going on.

My colleague got one of these when he was running setup:upgrade after pulling down my code with the module installed:

  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magento.cloudfla
  re_data' doesn't exist, query was: SELECT `cloudflare_data`.* FROM `cloudfl
  are_data` WHERE (`cloudflare_data`.`key`='zoneId:local-dev.sdbullion.com/ma
  gento')

  [PDOException]
  SQLSTATE[42S02]: Base table or view not found: 1146 Table 'magento.cloudfla
  re_data' doesn't exist

Attached a debugger to Zend_Exception to find the stack trace, and it's being triggered because part of the setup:upgrade process is re-organizing app/etc/config.php. Our problem then is that (like good developers) we keep app/etc/config.php in version control - so CloudFlare was already enabled and when it came time to clear the cache it failed.

I realize this might be an edge-case. We easily worked around it by running the individual schema and data upgrade scripts.

Either way, I wanted to at least document it here.

jwineman commented 7 years ago

Is it possible your database was out of sync with your config.php file? If the plugin was enabled previously shouldn't the tables already exist in the database?

navarr commented 7 years ago

In our workflow we have multiple developers working on a single site. A developer would add a module in a local environment (adding it to config.php). Our deployment process would roll that out to other sites, and other developers would pull in the branch, composer install and setup:upgrade - we think that last step (along with it already being enabled in config.php) is what leads up to this issue.

It's likely going to occur in any caching module, and may just be a result of our environment and process.

jwineman commented 7 years ago

I assume you don't have this error with other Magento2 plugins because they don't have a database component?

Honestly outside of developing this plugin I don't have much experience with Magento. setup:upgrade updates the database schema so each developer would only need to run it once after they pull the branch for the first time right? Are they running it every time they composer install or just the first time?

I don't think I see a work around here. If config.php in VCS shows the module enabled then you shouldn't need to run setup:upgrade.

navarr commented 7 years ago

This issue only occurs with Cloudflare because Cloudflare takes actions when the cache is cleaned out, which Magento does during setup:upgrade before it runs the actual upgrade scripts.

So it's enabled in the config.php through VCS, then they need to upgrade their database structure (which most people use setup:upgrade for).

An example future situation where this might occur would be - say you modify the database structure for the CloudFlare plugin. This new structure is used by the mechanism triggered from the cache clearing event. When a user runs setup:upgrade to upgrade your schema, a database error would occur.

A possible fix might be to fail gracefully from database errors during the cache flushing process - catching and logging Zend DB exceptions where as right now they pass through and end the setup process.

jwineman commented 7 years ago

Ah, that makes sense! Thank you for the explanation. We can definitely handle cache purge DB errors more gracefully.

jwineman commented 7 years ago

Hi @navarr,

This should be fixed in 1.1.6.

Thanks, John