Skip to content

Commit d03371d

Browse files
Deleted directory items on S3 container still in Statamic Assets DB after running sync-assets (#511)
* first stab at fixing this * actually run delete * fix bad merge * PINT! * Implement @ryanmitchell's suggestions with minor fixes. * PINT! * one more change to ensure that sub directories are being deleted * PINT! * remove dump and add additional folder check * remove map in favor of better comparison on filesystem * use blessed Str facade * recache --------- Co-authored-by: Ryan Mitchell <[email protected]>
1 parent 01eb011 commit d03371d

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

src/Commands/SyncAssets.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Statamic\Eloquent\Commands;
44

55
use Illuminate\Console\Command;
6+
use Statamic\Assets\AssetContainerContents;
67
use Statamic\Console\RunsInPlease;
78
use Statamic\Contracts\Assets\AssetContainer;
89
use Statamic\Eloquent\Assets\AssetModel;
@@ -44,6 +45,12 @@ private function processContainer(AssetContainer $container)
4445
$this->info("Container: {$container->handle()}");
4546

4647
$this->processFolder($container);
48+
49+
$contents = app(AssetContainerContents::class);
50+
51+
$contents->cacheStore()->forget('asset-folder-contents-'.$container->handle());
52+
53+
$contents->container($container)->directories();
4754
}
4855

4956
private function processFolder(AssetContainer $container, $folder = '/')
@@ -87,6 +94,45 @@ private function processFolder(AssetContainer $container, $folder = '/')
8794
});
8895
});
8996

97+
// delete any sub-folders we have a db entry for that no longer exist
98+
$filesystemFolders = $contents
99+
->reject(fn ($item) => $item['type'] != 'dir')
100+
->pluck('path');
101+
102+
// The folder variable is passed with a leading slash. This must be removed
103+
// in order to match against the folder column in the database.
104+
$folderNoLeadingSlash = Str::chopStart($folder, '/');
105+
106+
AssetModel::query()
107+
->where('container', $container->handle())
108+
->when(
109+
$folder == '/',
110+
fn ($query) => $query->where('folder', 'not like', '%/'),
111+
fn ($query) => $query->where('folder', 'like', $folderNoLeadingSlash.'/%')
112+
)
113+
->select('folder')
114+
->distinct()
115+
->pluck('folder')
116+
->unique()
117+
->each(function ($folder) use ($filesystemFolders, $container) {
118+
if ($filesystemFolders->contains(fn ($fsFolder) => Str::startsWith($folder, $fsFolder.'/'))) {
119+
return;
120+
}
121+
122+
$this->error("Deleting assets in {$folder}");
123+
AssetModel::query()
124+
->where('container', $container->handle())
125+
->where('folder', 'like', $folder)
126+
->orWhere('folder', 'like', $folder.'/%')
127+
->chunk(100, function ($assets) {
128+
$assets->each(function ($asset) {
129+
$this->error("Deleting {$asset->path}");
130+
131+
$asset->delete();
132+
});
133+
});
134+
});
135+
90136
// process any sub-folders of this folder
91137
$contents
92138
->reject(fn ($item) => $item['type'] != 'dir')

0 commit comments

Comments
 (0)