Passed
Push — master ( 6c23c3...c1688f )
by Julito
09:45
created

Version20191101132000::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/* For licensing terms, see /license.txt */
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Schema\Schema;
11
12
class Version20191101132000 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'course changes';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $table = $schema->getTable('course');
22
        if (false === $table->hasColumn('resource_node_id')) {
23
            $this->addSql('ALTER TABLE course ADD COLUMN resource_node_id INT DEFAULT NULL;');
24
            $this->addSql(
25
                'ALTER TABLE course ADD CONSTRAINT FK_169E6FB91BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;'
26
            );
27
            $this->addSql('CREATE UNIQUE INDEX UNIQ_169E6FB91BAD783F ON course (resource_node_id);');
28
        }
29
        if ($table->hasForeignKey('FK_169E6FB912469DE2')) {
30
            $this->addSql('ALTER TABLE course DROP FOREIGN KEY FK_169E6FB912469DE2');
31
        }
32
        if ($table->hasForeignKey('IDX_169E6FB912469DE2')) {
33
            $this->addSql('DROP INDEX IDX_169E6FB912469DE2 ON course');
34
        }
35
        if ($table->hasIndex('category_code')) {
36
            $this->addSql('DROP INDEX category_code ON course');
37
        }
38
39
        if ($table->hasIndex('directory')) {
40
            $this->addSql('DROP INDEX directory ON course;');
41
        }
42
43
        $this->addSql('UPDATE course SET course_language = "en" WHERE course_language IS NULL');
44
        $this->addSql('ALTER TABLE course CHANGE course_language course_language VARCHAR(20) NOT NULL');
45
46
        $this->addSql('UPDATE course SET visibility = "0" WHERE visibility IS NULL');
47
        $this->addSql('ALTER TABLE course CHANGE visibility visibility INT NOT NULL');
48
49
        $this->addSql('UPDATE course SET creation_date = NOW() WHERE creation_date IS NULL');
50
        $this->addSql('ALTER TABLE course CHANGE creation_date creation_date DATETIME NOT NULL');
51
52
        $this->addSql('UPDATE course SET subscribe = 0 WHERE subscribe IS NULL');
53
        $this->addSql('ALTER TABLE course CHANGE subscribe subscribe TINYINT(1) NOT NULL');
54
55
        $this->addSql('UPDATE course SET unsubscribe = 0 WHERE unsubscribe IS NULL');
56
        $this->addSql('ALTER TABLE course CHANGE unsubscribe unsubscribe TINYINT(1) NOT NULL');
57
58
        if (false === $schema->hasTable('course_rel_category')) {
59
            $this->addSql('CREATE TABLE course_rel_category (course_id INT NOT NULL, course_category_id INT NOT NULL, INDEX IDX_16B33772591CC992 (course_id), INDEX IDX_16B337726628AD36 (course_category_id), PRIMARY KEY(course_id, course_category_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC;');
60
            $this->addSql('ALTER TABLE course_rel_category ADD CONSTRAINT FK_16B33772591CC992 FOREIGN KEY (course_id) REFERENCES course (id)');
61
            $this->addSql('ALTER TABLE course_rel_category ADD CONSTRAINT FK_16B337726628AD36 FOREIGN KEY (course_category_id) REFERENCES course_category (id);');
62
        }
63
64
        if ($schema->getTable('course')->hasColumn('category_id')) {
65
            //$this->addSql('ALTER TABLE course DROP category_id');
66
        }
67
68
        $table = $schema->getTable('course_rel_user');
69
        if (false === $table->hasIndex('course_rel_user_user_id')) {
70
            $this->addSql('CREATE INDEX course_rel_user_user_id ON course_rel_user (id, user_id)');
71
        }
72
        if (false === $table->hasIndex('course_rel_user_c_id_user_id')) {
73
            $this->addSql('CREATE INDEX course_rel_user_c_id_user_id ON course_rel_user (id, c_id, user_id)');
74
        }
75
        //$this->addSql('ALTER TABLE course DROP category_code');
76
        $connection = $this->getEntityManager()->getConnection();
77
        $sql = 'SELECT * FROM course_category';
78
        $result = $connection->executeQuery($sql);
79
        $all = $result->fetchAllAssociative();
80
81
        $categories = array_column($all, 'parent_id', 'id');
82
        $categoryCodeList = array_column($all, 'id', 'code');
83
84
        foreach ($categories as $categoryId => $parentId) {
85
            if (empty($parentId)) {
86
                continue;
87
            }
88
            $newParentId = $categoryCodeList[$parentId];
89
            if (!empty($newParentId)) {
90
                $this->addSql("UPDATE course_category SET parent_id = {$newParentId} WHERE id = {$categoryId}");
91
            }
92
        }
93
94
        $this->addSql('ALTER TABLE course_category CHANGE parent_id parent_id INT DEFAULT NULL;');
95
96
        $table = $schema->getTable('course_category');
97
        if (false === $table->hasForeignKey('FK_AFF87497727ACA70')) {
98
            $this->addSql(
99
                'ALTER TABLE course_category ADD CONSTRAINT FK_AFF87497727ACA70 FOREIGN KEY (parent_id) REFERENCES course_category (id);'
100
            );
101
        }
102
        if (!$table->hasColumn('image')) {
103
            $this->addSql('ALTER TABLE course_category ADD image VARCHAR(255) DEFAULT NULL');
104
        }
105
        if (!$table->hasColumn('description')) {
106
            $this->addSql('ALTER TABLE course_category ADD description LONGTEXT DEFAULT NULL');
107
        }
108
109
        $this->addSql(
110
            'ALTER TABLE course_category CHANGE auth_course_child auth_course_child VARCHAR(40) DEFAULT NULL'
111
        );
112
    }
113
114
    public function down(Schema $schema): void
115
    {
116
    }
117
}
118