Completed
Push — master ( c5110b...a167e6 )
by Valentyn
13:46
created

Version20181028155717   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A up() 0 25 2
A down() 0 14 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace DoctrineMigrations;
6
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\Migrations\AbstractMigration;
9
10
/**
11
 * Auto-generated Migration: Please modify to your needs!
12
 */
13
final class Version20181028155717 extends AbstractMigration
14
{
15
    private $countries = [
16
        'UKR' => 'Ukraine',
17
        'POL' => 'Poland',
18
        'BLR' => 'Belarus',
19
        'RUS' => 'Russia',
20
        'ESP' => 'Spain',
21
        'CAN' => 'Canada',
22
        'USA' => 'USA',
23
    ];
24
25
    public function up(Schema $schema): void
26
    {
27
        // this up() migration is auto-generated, please modify it to your needs
28
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
29
30
        $this->addSql('CREATE SEQUENCE movies_release_dates_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
31
        $this->addSql('CREATE SEQUENCE release_date_queue_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
32
        $this->addSql('CREATE SEQUENCE countries_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
33
        $this->addSql('CREATE TABLE movies_release_dates (id INT NOT NULL, movie_id INT NOT NULL, country_code VARCHAR(3) NOT NULL, date DATE DEFAULT NULL, PRIMARY KEY(id))');
34
        $this->addSql('CREATE INDEX IDX_69ECF8158F93B6FC ON movies_release_dates (movie_id)');
35
        $this->addSql('CREATE INDEX IDX_69ECF815F026BB7C ON movies_release_dates (country_code)');
36
        $this->addSql('CREATE UNIQUE INDEX Movie_id_Country_code ON movies_release_dates (movie_id, country_code)');
37
        $this->addSql('CREATE TABLE release_date_queue (id INT NOT NULL, movie_id INT NOT NULL, added_at DATE DEFAULT NULL, is_active INT DEFAULT 1 NOT NULL, PRIMARY KEY(id))');
38
        $this->addSql('CREATE UNIQUE INDEX UNIQ_A8EA9D558F93B6FC ON release_date_queue (movie_id)');
39
        $this->addSql('CREATE TABLE countries (id INT NOT NULL, code VARCHAR(3) NOT NULL, name VARCHAR(50) NOT NULL, PRIMARY KEY(id))');
40
        $this->addSql('CREATE UNIQUE INDEX UNIQ_5D66EBAD77153098 ON countries (code)');
41
        $this->addSql('CREATE UNIQUE INDEX UNIQ_5D66EBAD5E237E06 ON countries (name)');
42
        $this->addSql('ALTER TABLE movies_release_dates ADD CONSTRAINT FK_69ECF8158F93B6FC FOREIGN KEY (movie_id) REFERENCES movies (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
43
        $this->addSql('ALTER TABLE movies_release_dates ADD CONSTRAINT FK_69ECF815F026BB7C FOREIGN KEY (country_code) REFERENCES countries (code) NOT DEFERRABLE INITIALLY IMMEDIATE');
44
        $this->addSql('ALTER TABLE release_date_queue ADD CONSTRAINT FK_A8EA9D558F93B6FC FOREIGN KEY (movie_id) REFERENCES movies (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
45
46
        foreach ($this->countries as $code => $name) {
47
            $this->addSql("INSERT INTO countries (id, code, name) VALUES (NEXTVAL('countries_id_seq'), '{$code}', '{$name}');");
48
        }
49
    }
50
51
    public function down(Schema $schema): void
52
    {
53
        // this down() migration is auto-generated, please modify it to your needs
54
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
55
56
        $this->addSql('CREATE SCHEMA public');
57
        $this->addSql('ALTER TABLE movies_release_dates DROP CONSTRAINT FK_69ECF815F026BB7C');
58
        $this->addSql('DROP SEQUENCE movies_release_dates_id_seq CASCADE');
59
        $this->addSql('DROP SEQUENCE release_date_queue_id_seq CASCADE');
60
        $this->addSql('DROP SEQUENCE countries_id_seq CASCADE');
61
        $this->addSql('DROP TABLE movies_release_dates');
62
        $this->addSql('DROP TABLE release_date_queue');
63
        $this->addSql('DROP TABLE countries');
64
    }
65
}
66