Exception when running migrations if used with Statamic eloquent driver
mcrepaldi opened this issue · comments
Bug description
Issue:
In a fresh Statamic install with the Statamic-Peak starter kit and the eloquent driver installed, I encountered an error from this addon. The error originates from the web.php routes file on the following lines:
$useSitemap = (bool)GlobalSet::findByHandle('seo')?->inDefaultSite()?->get('use_sitemap');
$useSocialImageGeneration = (bool)GlobalSet::findByHandle('seo')?->inDefaultSite()?->get('use_social_image_generation');
Somehow, when running php artisan migrate, it makes a database call from these lines. Since no tables exist yet, this throws an error before the migration process even starts.
Possible Fix:
$useSitemap = false;
$useSocialImageGeneration = false;
try {
$useSitemap = (bool)GlobalSet::findByHandle('seo')?->inDefaultSite()?->get('use_sitemap');
$useSocialImageGeneration = (bool)GlobalSet::findByHandle('seo')?->inDefaultSite()?->get('use_social_image_generation');
} catch (\Exception $e) {
// no action
}
This fix ensures that the variables are initialized with default values before attempting to fetch data from the database. Additionally, a try-catch block is added to handle any exceptions that might occur during the database call, preventing the migration process from failing.
How to reproduce
- Install Statamic
- Install Statamic-Peak
- Install Eloquent-Driver
- php artisan migrate
Logs
No response
Environment
Environment
Application Name: Statamic Peak
Laravel Version: 10.46.0
PHP Version: 8.2.13
Composer Version: 2.6.5
Environment: local
Debug Mode: ENABLED
URL: statamic-template.test
Maintenance Mode: OFF
Cache
Config: CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED
Drivers
Broadcasting: log
Cache: statamic
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file
Statamic
Addons: 5
Antlers: runtime
Sites: 1
Stache Watcher: Enabled
Static Caching: Disabled
Version: 4.51.0 PRO
Statamic Addons
statamic/eloquent-driver: 3.1.2
studio1902/statamic-peak-browser-appearance: 3.3.4
studio1902/statamic-peak-commands: 6.1.0
studio1902/statamic-peak-seo: 8.2.0
studio1902/statamic-peak-tools: 4.4.2
Statamic Eloquent Driver
Asset Containers: file
Assets: file
Blueprints: file
Collection Trees: eloquent
Collections: file
Entries: eloquent
Forms: file
Global Sets: eloquent
Global Variables: eloquent
Navigation Trees: eloquent
Navigations: eloquent
Revisions: eloquent
Taxonomies: eloquent
Terms: eloquent
Installation
Fresh statamic/statamic site via CLI
Antlers Parser
None
Additional details
No response
Thanks, great fix: b35d91c