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

Version20170301172303   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 66
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setContainer() 0 4 1
A up() 0 25 4
A down() 0 18 3
1
<?php
2
3
namespace SWP\Migrations;
4
5
use Doctrine\DBAL\Migrations\AbstractMigration;
6
use Doctrine\DBAL\Schema\Schema;
7
use SWP\Component\MultiTenancy\Model\TenantInterface;
8
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
11
/**
12
 * Auto-generated Migration: Please modify to your needs!
13
 */
14
class Version20170301172303 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...
15
{
16
    /**
17
     * @var ContainerInterface
18
     */
19
    private $container;
20
21
    /**
22
     * @param ContainerInterface|null $container
23
     */
24
    public function setContainer(ContainerInterface $container = null)
25
    {
26
        $this->container = $container;
27
    }
28
29
    /**
30
     * @param Schema $schema
31
     */
32
    public function up(Schema $schema)
33
    {
34
        // this up() migration is auto-generated, please modify it to your needs
35
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
36
37
        $this->addSql('ALTER TABLE swp_tenant ALTER subdomain DROP NOT NULL');
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
        $domain = $this->container->getParameter('env(SWP_DOMAIN)');
42
        /** @var TenantInterface $tenant */
43
        foreach ($tenants as $tenant) {
44
            $date = new \DateTime();
45
            if (null === $tenant->getDomainName()) {
46
                $this->addSql('UPDATE swp_tenant SET domain_name = ?, updated_at = ? WHERE id = ?', [$domain, $date->format('Y-m-d H:i:s'), $tenant->getId()]);
47
            }
48
            if ('default' === $tenant->getSubdomain()) {
49
                $this->addSql('UPDATE swp_tenant SET subdomain = ?, updated_at = ? WHERE id = ?', [null, $date->format('Y-m-d H:i:s'), $tenant->getId()]);
50
            }
51
        }
52
        $this->container->get('doctrine')->getManager()->flush();
53
54
        $this->addSql('DROP INDEX uniq_ec6095fec1d5962e');
55
        $this->addSql('CREATE UNIQUE INDEX host_idx ON swp_tenant (domain_name, subdomain)');
56
    }
57
58
    /**
59
     * @param Schema $schema
60
     */
61
    public function down(Schema $schema)
62
    {
63
        // this down() migration is auto-generated, please modify it to your needs
64
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
65
66
        $tenants = $this->container->get('swp.repository.tenant')->findAll();
67
        /** @var TenantInterface $tenant */
68
        foreach ($tenants as $tenant) {
69
            if (null === $tenant->getSubdomain()) {
70
                $tenant->setSubdomain('default');
71
            }
72
        }
73
        $this->container->get('doctrine')->getManager()->flush();
74
75
        $this->addSql('ALTER TABLE swp_tenant ALTER subdomain SET NOT NULL');
76
        $this->addSql('CREATE UNIQUE INDEX uniq_ec6095fec1d5962e ON swp_tenant (subdomain)');
77
        $this->addSql('DROP INDEX host_idx');
78
    }
79
}
80