Completed
Push — master ( 19732d...076741 )
by Paweł
11:47
created

Version20180118194100::setContainer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace SWP\Migrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration;
6
use Doctrine\DBAL\Migrations\IrreversibleMigrationException;
7
use Doctrine\DBAL\Schema\Schema;
8
use SWP\Bundle\CoreBundle\Model\Article;
9
use SWP\Bundle\CoreBundle\Model\ArticleInterface;
10
use SWP\Bundle\MultiTenancyBundle\MultiTenancyEvents;
11
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
12
use Symfony\Component\DependencyInjection\ContainerInterface;
13
14
/**
15
 * Add article statistics to articles.
16
 */
17
class Version20180118194100 extends AbstractMigration implements ContainerAwareInterface
0 ignored issues
show
Deprecated Code introduced by
The class Doctrine\DBAL\Migrations\AbstractMigration has been deprecated with message: Please use Doctrine\Migrations\AbstractMigration

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
18
{
19
    /**
20
     * @var ContainerInterface
21
     */
22
    private $container;
23
24
    /**
25
     * @param ContainerInterface|null $container
26
     */
27
    public function setContainer(ContainerInterface $container = null)
28
    {
29
        $this->container = $container;
30
    }
31
32
    /**
33
     * @param Schema $schema
34
     */
35
    public function up(Schema $schema)
36
    {
37
        // this up() migration is auto-generated, please modify it to your needs
38
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
39
40
        $entityManager = $this->container->get('doctrine.orm.default_entity_manager');
41
        $this->container->get('event_dispatcher')->dispatch(MultiTenancyEvents::TENANTABLE_DISABLE);
42
43
        $articles = $entityManager
44
            ->createQuery('SELECT partial a.{id,tenantCode},  partial es.{id} FROM SWP\Bundle\CoreBundle\Model\Article a LEFT JOIN a.articleStatistics es')
45
            ->getArrayResult();
46
47
        if (empty($articles)) {
48
            return;
49
        }
50
51
        $dbConnection = $entityManager->getConnection();
52
        $nextvalQuery = $dbConnection->getDatabasePlatform()->getSequenceNextValSQL('swp_article_statistics_id_seq');
53
        $newId = (int) $dbConnection->fetchColumn($nextvalQuery);
54
55
        /* @var ArticleInterface $article */
56
        foreach ($articles as $article) {
57
            if (null !== $article['articleStatistics']) {
58
                continue;
59
            }
60
61
            $this->addSql('INSERT INTO swp_article_statistics (id, article_id, tenant_code, created_at) VALUES (:id, :article, :tenantCode, :createdAt)', [
62
                'id' => $newId,
63
                'article' => $article['id'],
64
                'tenantCode' => $article['tenantCode'],
65
                'createdAt' => (new \DateTime('now'))->format('Y-m-d h:i:sT'),
66
            ]);
67
68
            ++$newId;
69
        }
70
    }
71
72
    /**
73
     * @param Schema $schema
74
     *
75
     * @throws IrreversibleMigrationException
76
     * @throws \Doctrine\DBAL\Migrations\AbortMigrationException
77
     */
78
    public function down(Schema $schema)
79
    {
80
        // this down() migration is auto-generated, please modify it to your needs
81
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
82
83
        // No way to rollback this
84
        throw new IrreversibleMigrationException();
85
    }
86
}
87