Skip to content

Commit

Permalink
Show a progress bar during the update script.
Browse files Browse the repository at this point in the history
  • Loading branch information
duncanmcclean committed Feb 27, 2025
1 parent a814250 commit 653c2a8
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/UpdateScripts/ConvertDatesToUtc.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
use Statamic\Listeners\Concerns\GetsItemsContainingData;
use Statamic\Support\Arr;

use function Laravel\Prompts\progress;

class ConvertDatesToUtc extends UpdateScript
{
use GetsItemsContainingData;
Expand All @@ -21,22 +23,36 @@ public function shouldUpdate($newVersion, $oldVersion)

public function update()
{
$this
->getItemsContainingData()
->each(function ($item) {
/** @var Fields $fields */
$fields = $item->blueprint()->fields();
$items = $this->getItemsContainingData();

$this->recursivelyUpdateFields($item, $fields);
$progress = progress(
label: 'Converting dates to UTC',
steps: $items->count(),
hint: 'This may take a while depending on the amount of content you have.'
);

if (method_exists($item, 'isDirty')) {
$item->isDirty() ? $item->saveQuietly() : null;
$progress->start();

return;
}
$items->each(function ($item) use ($progress) {
$progress->advance();

$item->saveQuietly();
});
/** @var Fields $fields */
$fields = $item->blueprint()->fields();

$this->recursivelyUpdateFields($item, $fields);

if (method_exists($item, 'isDirty')) {
$item->isDirty() ? $item->saveQuietly() : null;
$progress->advance();

return;
}

$item->saveQuietly();
$progress->advance();
});

$progress->finish();
}

private function recursivelyUpdateFields($item, Fields $fields, ?string $dottedPrefix = null): void
Expand Down

0 comments on commit 653c2a8

Please sign in to comment.