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

Check if remote storage is enabled before saving local file #2627

Merged
merged 3 commits into from
Oct 17, 2022

Conversation

elidrissidev
Copy link
Member

@elidrissidev elidrissidev commented Sep 30, 2022

Description (*)

This PR will add a simple check if remote DB storage for media is enabled before trying to write the file locally (using get.php). This will prevent unnecessary exceptions from being logged that say core_file_storage doesn't exist, since it's often created at runtime.

Fixed Issues (if relevant)

  1. Fixes Table 'dbName.core_file_storage' doesn't exist #2614

Manual testing scenarios (*)

  1. Have logging enabled and try to access a non-existent media file. e.g: http://<magento_host>/media/catalog/notfound.jpg. You will see an exception logged before this patch, but not after.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All automated tests passed successfully (all builds are green)
  • Add yourself to contributors list

@sreichel
Copy link
Contributor

sreichel commented Oct 2, 2022

Doesnt fix it for me.

Config is set to DB w/o existing table ...

  • there is an error for product image upload
  • errors on frontend

Sample data: https://openmage.ddev.site/men/new-arrivals/linen-blazer.html

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'db.core_file_storage' doesn't exist, query was: SELECT `e`.* FROM `core_file_storage` AS `e` WHERE (filename = 'msj012t_2.jpg') AND (directory = 'catalog/product/m/s')

#0 /var/www/html/lib/Varien/Db/Statement/Pdo/Mysql.php(103): Zend_Db_Statement_Pdo->_execute(Array)
#1 /var/www/html/app/code/core/Zend/Db/Statement.php(289): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#2 /var/www/html/lib/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#3 /var/www/html/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('SELECT `e`.* FR...', Array)
#4 /var/www/html/lib/Varien/Db/Adapter/Pdo/Mysql.php(501): Zend_Db_Adapter_Pdo_Abstract->query('SELECT `e`.* FR...', Array)
#5 /var/www/html/lib/Zend/Db/Adapter/Abstract.php(756): Varien_Db_Adapter_Pdo_Mysql->query('SELECT `e`.* FR...', Array)
#6 /var/www/html/app/code/core/Mage/Core/Model/Resource/File/Storage/Database.php(144): Zend_Db_Adapter_Abstract->fetchRow(Object(Varien_Db_Select))
#7 /var/www/html/app/code/core/Mage/Core/Model/File/Storage/Database.php(118): Mage_Core_Model_Resource_File_Storage_Database->loadByFilename(Object(Mage_Core_Model_File_Storage_Database), 'msj012t_2.jpg', 'catalog/product...')
#8 /var/www/html/app/code/core/Mage/Core/Helper/File/Storage/Database.php(202): Mage_Core_Model_File_Storage_Database->loadByFilename('catalog/product...')
#9 /var/www/html/app/code/core/Mage/Catalog/Model/Product/Image.php(787): Mage_Core_Helper_File_Storage_Database->saveFileToFilesystem('/var/www/html/m...')
#10 /var/www/html/app/code/core/Mage/Catalog/Model/Product/Image.php(346): Mage_Catalog_Model_Product_Image->_fileExists('/var/www/html/m...')
#11 /var/www/html/app/code/core/Mage/Catalog/Helper/Image.php(165): Mage_Catalog_Model_Product_Image->setBaseFile('/m/s/msj012t_2....')
#12 /var/www/html/app/design/frontend/rwd/default/template/catalog/product/view/media.phtml(37): Mage_Catalog_Helper_Image->init(Object(Mage_Catalog_Model_Product), 'image')

@sreichel
Copy link
Contributor

sreichel commented Oct 2, 2022

I even cant switch to "file" from admin config ...

  • try to change config to "file" and save ... validation error "Synchronization is required."
  • try synchronization ... "Base table or view not found"

....

Btw ... "synchronization" to DB means images get deleted ...

When you test this PR/issue, make sure to have a backup of your media dir ... :(

@elidrissidev
Copy link
Member Author

This PR was only meant to fix the error when the config is set to file, but that might be a good thing to include as well.

@elidrissidev
Copy link
Member Author

How did you end up with file storage set to Database without the table being created already??? File System is the default and when I switch to Database, the table is properly created.

@sreichel
Copy link
Contributor

sreichel commented Oct 3, 2022

I've changed the value in core_config_data, but this could also happen with tools that import/export config.

@github-actions github-actions bot added the Component: Core Relates to Mage_Core label Oct 6, 2022
@elidrissidev
Copy link
Member Author

elidrissidev commented Oct 6, 2022

I'm not a fan of the second commit as the storage backend isn't intended to be updated manually from the database, but here it is. It will initialize the database storage every time it's loaded which will create the table if it doesn't exist. The second commit needs to be analyzed to ensure that it won't affect performance though.

@fballiano
Copy link
Contributor

at this point why don't we add an update script that creates the table?

@elidrissidev
Copy link
Member Author

There is already one but it's conditioned by checkDbUsage.

@fballiano
Copy link
Contributor

if we've to check it every "request" I'd prefer to have the table created and that's it

@colinmollenhour
Copy link
Member

Setting config values directly in the database, while it works in most cases, bypasses the before/after save methods of the backend models so I think should be viewed as an "unsupported" action.. So in this case the fix is to change the storage method via the admin UI so the hooks can run as intended. Import/export tools should ideally use the models to update the config rather than manipulating the db directly.

@github-actions github-actions bot removed the Component: Core Relates to Mage_Core label Oct 6, 2022
@elidrissidev
Copy link
Member Author

I reverted the second change to keep this PR in scope of the linked issue, which is what it was supposed to solve in the first place. Anything else can be addressed in a following PR.

@fballiano fballiano merged commit cc6317a into OpenMage:1.9.4.x Oct 17, 2022
@github-actions
Copy link
Contributor

Unit Test Results

1 files  ±0  1 suites  ±0   0s ⏱️ ±0s
0 tests ±0  0 ✔️ ±0  0 💤 ±0  0 ❌ ±0 
7 runs  ±0  5 ✔️ ±0  2 💤 ±0  0 ❌ ±0 

Results for commit cc6317a. ± Comparison against base commit 52f2c75.

@elidrissidev elidrissidev deleted the fix/check-remote-storage branch December 31, 2022 09:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Table 'dbName.core_file_storage' doesn't exist
5 participants