statamic / eloquent-driver

Provides support for storing your Statamic data in a database, rather than flat files.
https://statamic.dev/tips/storing-content-in-a-database
MIT License
104 stars 74 forks source link

Sync command resets all assets previous data #298

Closed sdussaut closed 3 months ago

sdussaut commented 3 months ago

Bug description

Hi! I am using the eloquent driver with assets enabled. Assets are added outside of Statamic. When new assets come in, the sync command php please eloquent:sync-assets removes all previous data set on all of the already present assets.

How to reproduce

Logs

No response

Environment

Environment
Application Name: Test app
Laravel Version: 11.10.0
PHP Version: 8.3.3
Composer Version: 2.6.6
Environment: local
Debug Mode: ENABLED
URL: test-app.test
Maintenance Mode: OFF

Cache
Config: NOT 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

Simple Commerce
Currencies: EUR, EUR
Gateways: Dummy
Repository: Customer: DuncanMcClean\SimpleCommerce\Customers\EntryCustomerRepository
Repository: Order: DuncanMcClean\SimpleCommerce\Orders\EntryOrderRepository
Repository: Product: DuncanMcClean\SimpleCommerce\Products\EntryProductRepository
Shipping Methods: Free Shipping
Tax Engine: DuncanMcClean\SimpleCommerce\Tax\Standard\TaxEngine

Statamic
Addons: 3
Sites: 2 ([FR] Français, [EN] Anglais)
Stache Watcher: Enabled
Static Caching: Disabled
Version: 5.7.2 PRO

Statamic Addons
aryehraber/statamic-logbook: 3.3.0
duncanmcclean/simple-commerce: 7.3.0
statamic/eloquent-driver: 4.2.0

Statamic Eloquent Driver
Asset Containers: eloquent
Assets: eloquent
Blueprints: file
Collection Trees: eloquent
Collections: eloquent
Entries: eloquent
Forms: file
Global Sets: file
Global Variables: file
Navigation Trees: file
Navigations: file
Revisions: file
Taxonomies: eloquent
Terms: eloquent
Tokens: file

Additional details

I am unsure as if this is the intended behaviour or not. The documentation specifies that it will "add any missing files to the database" (emphasis is mine) not override all of them :-) Am I missing something? A flag to pass to the command to control this behaviour maybe?

Thanks!

ryanmitchell commented 3 months ago

No that definitely shouldnt be happening. If you replace these lines:

https://github.com/statamic/eloquent-driver/blob/d59be053699ca0a2e4e8d7cab54ec906d5dadae6/src/Commands/SyncAssets.php#L66-L69

with:

            if (! Facades\Asset::find($container->handle().'::'.$file)) {
                $asset = Facades\Asset::make()
                    ->container($container->handle())
                    ->path($file)
                    ->saveQuietly();
            }

does that change the behaviour for you?

sdussaut commented 3 months ago

Indeed it does!

Thanks a lot @ryanmitchell!

ryanmitchell commented 3 months ago

Great I’ll get that merged into the next release

sdussaut commented 3 months ago

One question that is maybe (?) related: is it possible to pass a single container to the command to sync (and generate presets) only for the assets of that container?

I try the following php please eloquent:sync-assets --container=myContainer but the command seems to generate every single asset I have in every container. Thanks!

ryanmitchell commented 3 months ago

Yep that’s already supported.