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

Version20170221091231::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\Schema\Schema;
7
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
10
/**
11
 * Auto-generated Migration: Please modify to your needs!
12
 */
13
class Version20170221091231 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...
14
{
15
    /**
16
     * @var ContainerInterface
17
     */
18
    private $container;
19
20
    /**
21
     * @param ContainerInterface|null $container
22
     */
23
    public function setContainer(ContainerInterface $container = null)
24
    {
25
        $this->container = $container;
26
    }
27
28
    /**
29
     * @param Schema $schema
30
     */
31
    public function up(Schema $schema)
32
    {
33
        // this up() migration is auto-generated, please modify it to your needs
34
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
35
36
        $revisionRepository = $this->container->get('swp.repository.revision');
37
        $revisionFactory = $this->container->get('swp.factory.revision');
38
        $query = $this->container->get('doctrine.orm.default_entity_manager')
39
            ->createQuery('SELECT t FROM SWP\Bundle\CoreBundle\Model\Tenant t');
40
        $tenants = $query->getResult();
41
        $revisionManager = $this->container->get('swp.manager.revision');
42
        foreach ($tenants as $tenant) {
43
            $existingRevision = $this->container->get('swp.repository.revision')
44
                ->getWorkingRevision()
45
                ->andWhere('r.tenantCode = :tenantCode')
46
                ->setParameter('tenantCode', $tenant->getCode())
47
                ->getQuery()
48
                ->getOneOrNullResult();
49
50
            if (null !== $existingRevision) {
51
                continue;
52
            }
53
54
            $publishedRevision = $revisionFactory->create();
55
            $publishedRevision->setTenantCode($tenant->getCode());
56
            $revisionRepository->add($publishedRevision);
57
58
            $workingRevision = $revisionFactory->create();
59
            $workingRevision->setTenantCode($tenant->getCode());
60
            $revisionRepository->add($workingRevision);
61
            $workingRevision->setPrevious($publishedRevision);
62
            $revisionManager->publish($publishedRevision, $workingRevision);
63
        }
64
    }
65
66
    /**
67
     * @param Schema $schema
68
     */
69
    public function down(Schema $schema)
70
    {
71
        // this down() migration is auto-generated, please modify it to your needs
72
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
73
74
        $this->addSql('ALTER TABLE swp_container DROP CONSTRAINT FK_CF0E49301DFA7C8F');
75
        $this->addSql('ALTER TABLE swp_revision_log DROP CONSTRAINT FK_A1F96AFD9AC03385');
76
        $this->addSql('ALTER TABLE swp_revision_log DROP CONSTRAINT FK_A1F96AFD21852C2F');
77
        $this->addSql('TRUNCATE TABLE swp_revision');
78
        $this->addSql('ALTER TABLE swp_container ADD CONSTRAINT FK_CF0E49301DFA7C8F FOREIGN KEY (revision_id) REFERENCES swp_revision (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
79
        $this->addSql('ALTER TABLE swp_revision_log ADD CONSTRAINT FK_A1F96AFD9AC03385 FOREIGN KEY (target_revision_id) REFERENCES swp_revision (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
80
        $this->addSql('ALTER TABLE swp_revision_log ADD CONSTRAINT FK_A1F96AFD21852C2F FOREIGN KEY (source_revision_id) REFERENCES swp_revision (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
81
    }
82
}
83