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

Version20181101141057::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 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 Version20181101141057 extends AbstractMigration
14
{
15
    private $imdbCountries = [
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 imdb_countries_id_seq INCREMENT BY 1 MINVALUE 1 START 1');
31
        $this->addSql('CREATE TABLE imdb_countries (id INT NOT NULL, country_id INT NOT NULL, name VARCHAR(50) NOT NULL, alt_names VARCHAR(255) NOT NULL, PRIMARY KEY(id))');
32
        $this->addSql('CREATE UNIQUE INDEX UNIQ_6A73E9C65E237E06 ON imdb_countries (name)');
33
        $this->addSql('CREATE UNIQUE INDEX UNIQ_6A73E9C6F92F3E70 ON imdb_countries (country_id)');
34
        $this->addSql('ALTER TABLE imdb_countries ADD CONSTRAINT FK_6A73E9C6F92F3E70 FOREIGN KEY (country_id) REFERENCES countries (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
35
36
        foreach ($this->imdbCountries as $code => $imdbName) {
37
            $this->addSql("INSERT INTO imdb_countries (id, country_id, name, alt_names) VALUES (NEXTVAL('imdb_countries_id_seq'), (SELECT c.id FROM countries c WHERE c.code = '{$code}'), '{$imdbName}', '');");
38
        }
39
    }
40
41
    public function down(Schema $schema): void
42
    {
43
        // this down() migration is auto-generated, please modify it to your needs
44
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'postgresql', 'Migration can only be executed safely on \'postgresql\'.');
45
46
        $this->addSql('CREATE SCHEMA public');
47
        $this->addSql('DROP SEQUENCE imdb_countries_id_seq CASCADE');
48
        $this->addSql('DROP TABLE imdb_countries');
49
    }
50
}
51