Completed
Push — master ( e1f264...8e4abf )
by Rafał
09:21 queued 11s
created

Version20200622125414::postUp()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 52

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 52
rs 8.425
c 0
b 0
f 0
cc 6
nc 9
nop 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 Symfony\Component\DependencyInjection\ContainerAwareInterface;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
12
/**
13
 * Auto-generated Migration: Please modify to your needs!
14
 */
15
final class Version20200622125414 extends AbstractMigration implements ContainerAwareInterface
16
{
17
    /**
18
     * @var ContainerInterface
19
     */
20
    private $container;
21
22
    public function setContainer(ContainerInterface $container = null)
23
    {
24
        $this->container = $container;
25
    }
26
27
    public function up(Schema $schema): void
28
    {
29
        // this up() migration is auto-generated, please modify it to your needs
30
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
31
32
        $this->addSql('CREATE SEQUENCE swp_article_metadata_place_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
33
        $this->addSql('CREATE SEQUENCE swp_article_metadata_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
34
        $this->addSql('CREATE SEQUENCE swp_article_metadata_service_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
35
        $this->addSql('CREATE SEQUENCE swp_article_metadata_subject_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
36
        $this->addSql('CREATE TABLE swp_article_metadata_place (id INT NOT NULL, metadata_id INT DEFAULT NULL, country VARCHAR(255) DEFAULT NULL, world_region VARCHAR(255) DEFAULT NULL, state VARCHAR(255) DEFAULT NULL, name VARCHAR(255) DEFAULT NULL, qgroup VARCHAR(255) DEFAULT NULL, qcode VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))');
37
        $this->addSql('CREATE INDEX IDX_6C173802DC9EE959 ON swp_article_metadata_place (metadata_id)');
38
        $this->addSql('CREATE TABLE swp_article_metadata (id INT NOT NULL, article_id INT DEFAULT NULL, profile VARCHAR(255) DEFAULT NULL, priority INT DEFAULT NULL, urgency INT DEFAULT NULL, ed_note VARCHAR(255) DEFAULT NULL, language VARCHAR(255) DEFAULT NULL, genre VARCHAR(255) DEFAULT NULL, guid VARCHAR(255) DEFAULT NULL, located VARCHAR(255) DEFAULT NULL, byline VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))');
39
        $this->addSql('CREATE UNIQUE INDEX UNIQ_EEF4773C7294869C ON swp_article_metadata (article_id)');
40
        $this->addSql('CREATE TABLE swp_article_metadata_service (id INT NOT NULL, metadata_id INT DEFAULT NULL, code VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
41
        $this->addSql('CREATE INDEX IDX_779FF189DC9EE959 ON swp_article_metadata_service (metadata_id)');
42
        $this->addSql('CREATE TABLE swp_article_metadata_subject (id INT NOT NULL, metadata_id INT DEFAULT NULL, code VARCHAR(255) NOT NULL, scheme VARCHAR(255) DEFAULT NULL, PRIMARY KEY(id))');
43
        $this->addSql('CREATE INDEX IDX_6DCC5521DC9EE959 ON swp_article_metadata_subject (metadata_id)');
44
        $this->addSql('ALTER TABLE swp_article_metadata_place ADD CONSTRAINT FK_6C173802DC9EE959 FOREIGN KEY (metadata_id) REFERENCES swp_article_metadata (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
45
        $this->addSql('ALTER TABLE swp_article_metadata ADD CONSTRAINT FK_EEF4773C7294869C FOREIGN KEY (article_id) REFERENCES swp_article (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
46
        $this->addSql('ALTER TABLE swp_article_metadata_service ADD CONSTRAINT FK_779FF189DC9EE959 FOREIGN KEY (metadata_id) REFERENCES swp_article_metadata (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
47
        $this->addSql('ALTER TABLE swp_article_metadata_subject ADD CONSTRAINT FK_6DCC5521DC9EE959 FOREIGN KEY (metadata_id) REFERENCES swp_article_metadata (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
48
        $this->addSql('ALTER TABLE swp_item ADD profile VARCHAR(255) DEFAULT NULL');
49
        $this->addSql('ALTER TABLE swp_route ADD description VARCHAR(255) DEFAULT NULL');
50
    }
51
52
    public function postUp(Schema $schema): void
53
    {
54
        $metadataFactory = $this->container->get('swp.factory.metadata');
55
        $entityManager = $this->container->get('doctrine.orm.default_entity_manager');
56
        $entityManager->getConnection()->getConfiguration()->setSQLLogger(null);
57
58
        $batchSize = 500;
59
        $numberOfRecordsPerPage = 2000;
60
61
        $totalArticles = $entityManager
62
            ->createQuery('SELECT count(a) FROM SWP\Bundle\CoreBundle\Model\Article a')
63
            ->getSingleScalarResult();
64
65
        $totalArticlesProcessed = 0;
66
        $isProcessing = true;
67
68
        while ($isProcessing) {
69
            $query = $entityManager->createQuery('SELECT a FROM SWP\Bundle\CoreBundle\Model\Article a')
70
                ->setMaxResults($numberOfRecordsPerPage)
71
                ->setFirstResult($totalArticlesProcessed);
72
73
            echo 'fetching '.$numberOfRecordsPerPage.' starting from '.$totalArticlesProcessed;
74
75
            $iterableResult = $query->iterate();
76
77
            foreach ($iterableResult as $row) {
78
                $article = $row[0];
79
                $legacyMetadata = $article->getMetadata();
80
                if (empty($legacyMetadata)) {
81
                    continue;
82
                }
83
84
                $metadata = $metadataFactory->createFrom($legacyMetadata);
85
86
                $entityManager->persist($metadata);
87
88
                $article->setData($metadata);
89
90
                if (0 === ($totalArticlesProcessed % $batchSize)) {
91
                    $entityManager->flush();
92
                    $entityManager->clear();
93
                }
94
                ++$totalArticlesProcessed;
95
            }
96
97
            if ($totalArticlesProcessed === $totalArticles) {
98
                break;
99
            }
100
        }
101
102
        $entityManager->flush();
103
    }
104
105
    public function down(Schema $schema): void
106
    {
107
        // this down() migration is auto-generated, please modify it to your needs
108
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
109
110
        $this->addSql('ALTER TABLE swp_article_metadata_place DROP CONSTRAINT FK_6C173802DC9EE959');
111
        $this->addSql('ALTER TABLE swp_article_metadata_service DROP CONSTRAINT FK_779FF189DC9EE959');
112
        $this->addSql('ALTER TABLE swp_article_metadata_subject DROP CONSTRAINT FK_6DCC5521DC9EE959');
113
        $this->addSql('DROP SEQUENCE swp_article_metadata_place_id_seq CASCADE');
114
        $this->addSql('DROP SEQUENCE swp_article_metadata_id_seq CASCADE');
115
        $this->addSql('DROP SEQUENCE swp_article_metadata_service_id_seq CASCADE');
116
        $this->addSql('DROP SEQUENCE swp_article_metadata_subject_id_seq CASCADE');
117
        $this->addSql('DROP TABLE swp_article_metadata_place');
118
        $this->addSql('DROP TABLE swp_article_metadata');
119
        $this->addSql('DROP TABLE swp_article_metadata_service');
120
        $this->addSql('DROP TABLE swp_article_metadata_subject');
121
        $this->addSql('ALTER TABLE swp_item DROP profile');
122
        $this->addSql('ALTER TABLE swp_route DROP description');
123
    }
124
}
125