Completed
Push — master ( c78f02...3beff8 )
by Rafał
25:32 queued 15:53
created

Version20210112135542::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.9
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SWP\Migrations;
6
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\Migrations\AbstractMigration;
9
use SWP\Bundle\ContentBundle\Model\Article;
10
use SWP\Bundle\ContentBundle\Model\ArticleExtraEmbedField;
11
use SWP\Bundle\ContentBundle\Model\ArticleExtraTextField;
12
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
13
use Symfony\Component\DependencyInjection\ContainerInterface;
14
15
/**
16
 * Auto-generated Migration: Please modify to your needs!
17
 */
18
final class Version20210112135542 extends AbstractMigration implements ContainerAwareInterface
19
{
20
    /**
21
     * @var ContainerInterface
22
     */
23
    private $container;
24
25
    public function setContainer(ContainerInterface $container = null)
26
    {
27
        $this->container = $container;
28
    }
29
30
    public function up(Schema $schema): void
31
    {
32
        // this up() migration is auto-generated, please modify it to your needs
33
        $this->abortIf(
34
            'postgresql' !== $this->connection->getDatabasePlatform()->getName(),
35
            'Migration can only be executed safely on \'postgresql\'.'
36
        );
37
38
        $this->addSql('CREATE SEQUENCE swp_article_extra_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
39
        $this->addSql(
40
            'CREATE TABLE swp_article_extra (id INT NOT NULL, article_id INT DEFAULT NULL, field_name VARCHAR(255) NOT NULL, discr VARCHAR(255) NOT NULL, value VARCHAR(255) DEFAULT NULL, embed VARCHAR(255) DEFAULT NULL, description VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))'
41
        );
42
        $this->addSql('CREATE INDEX IDX_9E61B3177294869C ON swp_article_extra (article_id)');
43
        $this->addSql(
44
            'ALTER TABLE swp_article_extra ADD CONSTRAINT FK_9E61B3177294869C FOREIGN KEY (article_id) REFERENCES swp_article (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE'
45
        );
46
    }
47
48
    public function down(Schema $schema): void
49
    {
50
        // this down() migration is auto-generated, please modify it to your needs
51
        $this->abortIf(
52
            'postgresql' !== $this->connection->getDatabasePlatform()->getName(),
53
            'Migration can only be executed safely on \'postgresql\'.'
54
        );
55
56
        $this->addSql('DROP SEQUENCE swp_article_extra_id_seq CASCADE');
57
        $this->addSql('DROP TABLE swp_article_extra');
58
    }
59
60
    public function postUp(Schema $schema): void
61
    {
62
        $entityManager = $this->container->get('doctrine.orm.default_entity_manager');
63
        $entityManager->getConnection()->getConfiguration()->setSQLLogger(null);
64
65
        $batchSize = 500;
66
        $numberOfRecordsPerPage = 2000;
67
68
        $totalArticles = $entityManager
69
            ->createQuery('SELECT count(a) FROM SWP\Bundle\CoreBundle\Model\Article a')
70
            ->getSingleScalarResult();
71
72
        $totalArticlesProcessed = 0;
73
        $isProcessing = true;
74
75
        while ($isProcessing) {
76
            $sql = "SELECT id, extra FROM swp_article LIMIT $numberOfRecordsPerPage OFFSET $totalArticlesProcessed";
77
            $query = $entityManager->getConnection()->prepare($sql);
78
            $query->execute();
79
            $results = $query->fetchAll();
80
81
            echo 'fetching '.$numberOfRecordsPerPage.' starting from '.$totalArticlesProcessed.PHP_EOL;
82
83
            foreach ($results as $result) {
84
                $legacyExtra = unserialize($result['extra']);
85
                if (empty($legacyExtra)) {
86
                    ++$totalArticlesProcessed;
87
                    continue;
88
                }
89
90
                $article = $entityManager->find(
91
                    Article::class,
92
                    $result['id']
93
                );
94
95
                foreach ($legacyExtra as $key => $extraItem) {
96
                    if (is_array($extraItem)) {
97
                        $extra = ArticleExtraEmbedField::newFromValue($key, $extraItem);
98
                    } else {
99
                        $extra = ArticleExtraTextField::newFromValue($key, $extraItem);
100
                    }
101
                    $extra->setArticle($article);
102
                }
103
104
                $entityManager->persist($extra);
0 ignored issues
show
Bug introduced by
The variable $extra does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
105
106
                if (0 === ($totalArticlesProcessed % $batchSize)) {
107
                    $entityManager->flush();
108
                    $entityManager->clear();
109
                }
110
                ++$totalArticlesProcessed;
111
            }
112
113
            if ($totalArticlesProcessed === $totalArticles) {
114
                break;
115
            }
116
117
            $entityManager->flush();
118
        }
119
    }
120
}
121