Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Command;
- use App\Entity\Ad;
- use App\Entity\Conversion;
- use App\Entity\CronJob;
- use App\Entity\Project;
- use App\Entity\TrackedUser;
- use App\Entity\TrackingUrl;
- use Doctrine\ORM\Tools\Pagination\Paginator;
- use Symfony\Component\Console\Command\Command;
- use Symfony\Component\Console\Helper\ProgressBar;
- use Symfony\Component\Console\Input\InputArgument;
- use Symfony\Component\Console\Input\InputInterface;
- use Symfony\Component\Console\Output\OutputInterface;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- class FixJsonFieldsCommand extends Command
- {
- private $container;
- public function __construct(ContainerInterface $container)
- {
- $this->container = $container;
- parent::__construct();
- }
- protected function configure()
- {
- $this
- ->setName('app:fix-json-fields')
- ->addArgument('batch-size', InputArgument::OPTIONAL, 'Process N objects at once (default = 10)')
- ->setHelp('Fixes JSONs stored as text');
- }
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- ini_set('memory_limit','256M');
- $batchSize = $input->getArgument('batch-size') ?? 10;
- $progressBar = new ProgressBar($output->section());
- $iterateAndFix = function ($queryString, $objectFixer) use ($output, $progressBar, $batchSize) {
- $em = $this->container->get('doctrine')->resetManager();
- $em->getConnection()->getConfiguration()->setSQLLogger(null);
- $rowsSize = $em->createQuery(
- str_replace("select e", "select count(e.id)", $queryString)
- )
- ->getSingleScalarResult();
- $progressBar->setMaxSteps($rowsSize);
- $loops = ceil($rowsSize/$batchSize);
- for ($iii=0; $iii<$loops; $iii++) {
- gc_enable();
- $em = $this->container->get('doctrine')->resetManager();
- $em->getConnection()->getConfiguration()->setSQLLogger(null);
- $query = $em->createQuery($queryString)
- ->setMaxResults($batchSize)
- ->setFirstResult($iii*$batchSize);
- $paginator = new Paginator($query);
- foreach ($paginator as $object) {
- if ($objectFixer($object)) {
- $em->persist($object);
- }
- }
- $em->flush();
- $em->clear();
- $em = null;
- $paginator = null;
- $query = null;
- gc_collect_cycles();
- $progressBar->advance($batchSize);
- }
- };
- $queryString = 'select e from App\Entity\Project e';
- $iterateAndFix($queryString, function (Project $project) {
- $changed = false;
- if (is_string($project->getJobParameters())) {
- $project->setJobParameters(
- json_decode($project->getJobParameters())
- );
- $changed = true;
- }
- if (is_string($project->getUtmRules())) {
- $project->setUtmRules(
- json_decode($project->getUtmRules())
- );
- $changed = true;
- }
- return $changed;
- });
- $queryString = 'select e from App\Entity\Ad e';
- $iterateAndFix($queryString, function (Ad $ad) {
- $changed = false;
- if (is_string($ad->getCachedNormalizedAd())) {
- $ad->setCachedNormalizedAd(
- json_decode($ad->getCachedNormalizedAd())
- );
- $changed = true;
- }
- if (is_string($ad->getCustomData())) {
- $ad->setCustomData(
- json_decode($ad->getCustomData())
- );
- $changed = true;
- }
- return $changed;
- });
- $queryString = 'select e from App\Entity\Conversion e';
- $iterateAndFix($queryString, function (Conversion $conversion) {
- $changed = false;
- if (is_string($conversion->getCustomFields())) {
- $conversion->setCustomFields(
- json_decode($conversion->getCustomFields())
- );
- $changed = true;
- }
- return $changed;
- });
- $queryString = 'select e from App\Entity\TrackedUser e';
- $iterateAndFix($queryString, function (TrackedUser $trackedUser) {
- $changed = false;
- if (is_string($trackedUser->getCustomFields())) {
- $trackedUser->setCustomFields(
- json_decode($trackedUser->getCustomFields())
- );
- $changed = true;
- }
- return $changed;
- });
- $queryString = 'select e from App\Entity\TrackingUrl e';
- $iterateAndFix($queryString, function (TrackingUrl $trackingUrl) {
- $changed = false;
- if (is_string($trackingUrl->getUtms())) {
- $trackingUrl->setUtms(
- json_decode($trackingUrl->getUtms())
- );
- $changed = true;
- }
- return $changed;
- });
- $queryString = 'select e from App\Entity\CronJob e';
- $iterateAndFix($queryString, function (CronJob $cronJob) {
- $changed = false;
- if (is_string($cronJob->getResponse())) {
- $cronJob->setResponse(
- json_decode($cronJob->getResponse())
- );
- $changed = true;
- }
- if (is_string($cronJob->getParameters())) {
- $cronJob->setParameters(
- json_decode($cronJob->getParameters())
- );
- $changed = true;
- }
- return $changed;
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement