-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to refresh database from real files.
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace Arxy\FilesBundle\Command; | ||
|
||
use Arxy\FilesBundle\Manager; | ||
use Arxy\FilesBundle\Model\File; | ||
use Doctrine\Common\Persistence\ManagerRegistry; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Helper\ProgressBar; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class RefreshDatabaseCommand extends Command | ||
{ | ||
protected static $defaultName = 'arxy:files:refresh-database'; | ||
|
||
/** @var Manager */ | ||
private $fileManager; | ||
|
||
/** @var ManagerRegistry */ | ||
private $doctrine; | ||
|
||
public function __construct(Manager $fileManager, ManagerRegistry $registry) | ||
{ | ||
parent::__construct(); | ||
$this->fileManager = $fileManager; | ||
$this->doctrine = $registry; | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$progressBar = new ProgressBar($output); | ||
|
||
$batchSize = 20; | ||
$i = 1; | ||
|
||
$em = $this->doctrine->getManagerForClass($this->fileManager->getClass()); | ||
|
||
$queryBuilder = $em->createQueryBuilder(); | ||
$queryBuilder->select('file')->from($this->fileManager->getClass(), 'file'); | ||
|
||
$iterableResult = $queryBuilder->getQuery()->iterate(); | ||
foreach ($iterableResult as $row) { | ||
/** @var File $file */ | ||
$file = $row[0]; | ||
|
||
$this->fileManager->refresh($file); | ||
|
||
if (($i % $batchSize) === 0) { | ||
$em->flush(); | ||
$em->clear(); | ||
} | ||
++$i; | ||
|
||
$progressBar->advance(); | ||
} | ||
|
||
$em->flush(); | ||
|
||
$progressBar->finish(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters