Passed
Pull Request — master (#2138)
by Nico
25:02 queued 13:24
created

Version20250610063612_SpacecraftCondition   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 62
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
A down() 0 44 1
A up() 0 24 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stu\Migrations\Pgsql;
6
7
use Doctrine\DBAL\Schema\Schema;
8
use Doctrine\Migrations\AbstractMigration;
9
10
final class Version20250610063612_SpacecraftCondition extends AbstractMigration
11
{
12
    public function getDescription(): string
13
    {
14
        return 'Extracted spacecraft condition fields from spacecraft entity.';
15
    }
16
17
    public function up(Schema $schema): void
18
    {
19
        $this->addSql(<<<'SQL'
20
            CREATE TABLE stu_spacecraft_condition (spacecraft_id INT NOT NULL, hull INT NOT NULL, shield INT NOT NULL, is_disabled BOOLEAN NOT NULL, state SMALLINT NOT NULL, PRIMARY KEY(spacecraft_id))
21
        SQL);
22
        $this->addSql(<<<'SQL'
23
            INSERT INTO stu_spacecraft_condition 
24
            (spacecraft_id, hull, shield, is_disabled, state)
25
            SELECT sp.id, sp.huelle, sp.schilde, sp.disabled, sp.state
26
            FROM stu_spacecraft sp
27
        SQL);
28
        $this->addSql(<<<'SQL'
29
            ALTER TABLE stu_spacecraft_condition ADD CONSTRAINT FK_5BAFF7731C6AF6FD FOREIGN KEY (spacecraft_id) REFERENCES stu_spacecraft (id) ON DELETE CASCADE NOT DEFERRABLE INITIALLY IMMEDIATE
30
        SQL);
31
        $this->addSql(<<<'SQL'
32
            ALTER TABLE stu_spacecraft DROP huelle
33
        SQL);
34
        $this->addSql(<<<'SQL'
35
            ALTER TABLE stu_spacecraft DROP schilde
36
        SQL);
37
        $this->addSql(<<<'SQL'
38
            ALTER TABLE stu_spacecraft DROP disabled
39
        SQL);
40
        $this->addSql(<<<'SQL'
41
            ALTER TABLE stu_spacecraft DROP state
42
        SQL);
43
    }
44
45
    public function down(Schema $schema): void
46
    {
47
        // create columns
48
        $this->addSql(<<<'SQL'
49
            ALTER TABLE stu_spacecraft ADD huelle INT DEFAULT NULL
50
        SQL);
51
        $this->addSql(<<<'SQL'
52
            ALTER TABLE stu_spacecraft ADD schilde INT DEFAULT NULL
53
        SQL);
54
        $this->addSql(<<<'SQL'
55
            ALTER TABLE stu_spacecraft ADD disabled BOOLEAN DEFAULT NULL
56
        SQL);
57
        $this->addSql(<<<'SQL'
58
            ALTER TABLE stu_spacecraft ADD state SMALLINT DEFAULT NULL
59
        SQL);
60
61
62
        // fill data
63
        $this->addSql(<<<'SQL'
64
            UPDATE stu_spacecraft sp
65
            SET huelle = sc.hull, schilde = sc.shield, disabled = sc.is_disabled, state = sc.state
66
            FROM stu_spacecraft_condition sc
67
            WHERE sc.spacecraft_id = sp.id
68
        SQL);
69
70
        // set not null
71
        $this->addSql(<<<'SQL'
72
            ALTER TABLE stu_spacecraft ALTER huelle SET NOT NULL
73
        SQL);
74
        $this->addSql(<<<'SQL'
75
            ALTER TABLE stu_spacecraft ALTER schilde SET NOT NULL
76
        SQL);
77
        $this->addSql(<<<'SQL'
78
            ALTER TABLE stu_spacecraft ALTER disabled SET NOT NULL
79
        SQL);
80
        $this->addSql(<<<'SQL'
81
            ALTER TABLE stu_spacecraft ALTER state SET NOT NULL
82
        SQL);
83
84
        // drop condition table
85
        $this->addSql(<<<'SQL'
86
            ALTER TABLE stu_spacecraft_condition DROP CONSTRAINT FK_5BAFF7731C6AF6FD
87
        SQL);
88
        $this->addSql(<<<'SQL'
89
            DROP TABLE stu_spacecraft_condition
90
        SQL);
91
    }
92
}
93