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

Version20170427114227::up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 9.216
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
8
/**
9
 * Auto-generated Migration: Please modify to your needs!
10
 */
11
class Version20170427114227 extends AbstractMigration
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...
12
{
13
    /**
14
     * @param Schema $schema
15
     */
16
    public function up(Schema $schema)
17
    {
18
        // this up() migration is auto-generated, please modify it to your needs
19
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
20
21
        $this->addSql('CREATE SEQUENCE swp_item_renditions_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
22
        $this->addSql('CREATE TABLE swp_item_renditions (id INT NOT NULL, item_id INT DEFAULT NULL, name VARCHAR(255) NOT NULL, href VARCHAR(255) NOT NULL, width INT NOT NULL, height INT NOT NULL, mimetype VARCHAR(255) DEFAULT NULL, media VARCHAR(255) NOT NULL, deleted_at TIMESTAMP(0) WITHOUT TIME ZONE DEFAULT NULL, PRIMARY KEY(id))');
23
        $this->addSql('CREATE INDEX IDX_C89753FD126F525E ON swp_item_renditions (item_id)');
24
        $this->addSql('ALTER TABLE swp_item_renditions ADD CONSTRAINT FK_C89753FD126F525E FOREIGN KEY (item_id) REFERENCES swp_item (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE');
25
        $this->addSql('ALTER TABLE swp_item ADD name VARCHAR(255) DEFAULT NULL');
26
        $this->addSql('UPDATE swp_item SET pub_status = \'usable\' WHERE pub_status IS NULL');
27
        $this->addSql('ALTER TABLE swp_item ALTER pub_status SET NOT NULL');
28
        $this->addSql('ALTER TABLE swp_article ADD package_id INT DEFAULT NULL');
29
        $this->addSql('UPDATE swp_article SET package_id = (SELECT distinct on (p.guid) p.id FROM swp_package AS p WHERE code = p.guid)');
30
        $this->addSql('ALTER TABLE swp_article ALTER keywords SET NOT NULL');
31
        $this->addSql('ALTER TABLE swp_article ALTER tenant_code SET NOT NULL');
32
        $this->addSql('ALTER TABLE swp_article ADD CONSTRAINT FK_FB21E858F44CABFF FOREIGN KEY (package_id) REFERENCES swp_package (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
33
        $this->addSql('CREATE INDEX IDX_FB21E858F44CABFF ON swp_article (package_id)');
34
        $this->addSql('ALTER TABLE swp_article_media DROP tenant_code');
35
        $this->addSql('ALTER TABLE swp_container ALTER created_at SET DEFAULT \'now\'');
36
        $this->addSql('ALTER TABLE swp_container ALTER uuid DROP DEFAULT');
37
        $this->addSql('ALTER TABLE swp_image DROP tenant_code');
38
        $this->addSql('ALTER TABLE swp_package ADD organization_id INT DEFAULT NULL');
39
        $this->addSql('UPDATE swp_package SET organization_id = (SELECT t.id FROM swp_organization AS t LIMIT 1)');
40
        $this->addSql('ALTER TABLE swp_package ALTER organization_id DROP DEFAULT');
41
        $this->addSql('ALTER TABLE swp_package ADD body TEXT DEFAULT NULL');
42
        $this->addSql('ALTER TABLE swp_package ADD status VARCHAR(255) DEFAULT NULL');
43
        $this->addSql('UPDATE swp_package SET status = \'new\' WHERE (SELECT COUNT(a.id) FROM swp_article AS a WHERE a.package_id = id) = 0');
44
        $this->addSql('UPDATE swp_package SET status = \'published\' WHERE (SELECT COUNT(a.id) FROM swp_article AS a WHERE a.package_id = id AND a.status = \'published\') > 0');
45
        $this->addSql('UPDATE swp_package SET status = \'unpublished\' WHERE (SELECT COUNT(a.id) FROM swp_article AS a WHERE a.package_id = id AND a.status = \'unpublished\') > 0');
46
        $this->addSql('ALTER TABLE swp_package ALTER status SET NOT NULL');
47
        $this->addSql('ALTER TABLE swp_package ALTER slugline DROP NOT NULL');
48
        $this->addSql('UPDATE swp_package SET pub_status = \'usable\' WHERE pub_status IS NULL');
49
        $this->addSql('ALTER TABLE swp_package ALTER pub_status SET NOT NULL');
50
        $this->addSql('ALTER TABLE swp_package ADD CONSTRAINT FK_277381AB32C8A3DE FOREIGN KEY (organization_id) REFERENCES swp_organization (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
51
        $this->addSql('CREATE INDEX IDX_277381AB32C8A3DE ON swp_package (organization_id)');
52
        $this->addSql('ALTER TABLE swp_rule ADD COLUMN organization_id INT DEFAULT NULL');
53
        $this->addSql('UPDATE swp_rule SET organization_id = (SELECT t.organization_id FROM swp_tenant AS t WHERE tenant_code = t.code)');
54
        $this->addSql('ALTER TABLE swp_rule ALTER organization_id DROP DEFAULT');
55
        $this->addSql('ALTER TABLE swp_rule ALTER tenant_code DROP NOT NULL');
56
        $this->addSql('ALTER TABLE swp_rule ADD CONSTRAINT FK_B8CF81B432C8A3DE FOREIGN KEY (organization_id) REFERENCES swp_organization (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
57
        $this->addSql('CREATE INDEX IDX_B8CF81B432C8A3DE ON swp_rule (organization_id)');
58
        $this->addSql('ALTER TABLE swp_widget ALTER created_at SET DEFAULT \'now\'');
59
    }
60
61
    /**
62
     * @param Schema $schema
63
     */
64
    public function down(Schema $schema)
65
    {
66
        // this down() migration is auto-generated, please modify it to your needs
67
        $this->abortIf('postgresql' !== $this->connection->getDatabasePlatform()->getName(), 'Migration can only be executed safely on \'postgresql\'.');
68
69
        $this->addSql('DROP SEQUENCE swp_item_renditions_id_seq CASCADE');
70
        $this->addSql('DROP TABLE swp_item_renditions');
71
        $this->addSql('ALTER TABLE swp_rule DROP CONSTRAINT FK_B8CF81B432C8A3DE');
72
        $this->addSql('DROP INDEX IDX_B8CF81B432C8A3DE');
73
        $this->addSql('ALTER TABLE swp_rule DROP organization_id');
74
        $this->addSql('ALTER TABLE swp_rule ALTER tenant_code SET NOT NULL');
75
        $this->addSql('ALTER TABLE swp_widget ALTER created_at SET DEFAULT \'2017-04-27 13:41:56.106694\'');
76
        $this->addSql('ALTER TABLE swp_container ALTER created_at SET DEFAULT \'2017-04-27 13:41:56.106694\'');
77
        $this->addSql('ALTER TABLE swp_container ALTER uuid SET DEFAULT \'substr(md5((random())::text), 0, 12)\'');
78
        $this->addSql('ALTER TABLE swp_image ADD tenant_code VARCHAR(255) NOT NULL');
79
        $this->addSql('ALTER TABLE swp_article DROP CONSTRAINT FK_FB21E858F44CABFF');
80
        $this->addSql('DROP INDEX IDX_FB21E858F44CABFF');
81
        $this->addSql('ALTER TABLE swp_article DROP package_id');
82
        $this->addSql('ALTER TABLE swp_article ALTER keywords DROP NOT NULL');
83
        $this->addSql('ALTER TABLE swp_article ALTER tenant_code DROP NOT NULL');
84
        $this->addSql('ALTER TABLE swp_article_media ADD tenant_code VARCHAR(255) NOT NULL');
85
        $this->addSql('ALTER TABLE swp_item DROP name');
86
        $this->addSql('ALTER TABLE swp_item ALTER pub_status DROP NOT NULL');
87
        $this->addSql('ALTER TABLE swp_package DROP CONSTRAINT FK_277381AB32C8A3DE');
88
        $this->addSql('DROP INDEX IDX_277381AB32C8A3DE');
89
        $this->addSql('ALTER TABLE swp_package DROP organization_id');
90
        $this->addSql('ALTER TABLE swp_package DROP body');
91
        $this->addSql('ALTER TABLE swp_package DROP status');
92
        $this->addSql('ALTER TABLE swp_package ALTER slugline SET NOT NULL');
93
        $this->addSql('ALTER TABLE swp_package ALTER pub_status DROP NOT NULL');
94
    }
95
}
96