Version20191101132000::down()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 1
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 1
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\Entity\Asset;
10
use Chamilo\CoreBundle\Entity\CourseCategory;
11
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
12
use Doctrine\DBAL\Schema\Schema;
13
use Symfony\Component\HttpFoundation\File\UploadedFile;
14
15
class Version20191101132000 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Course changes';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        $table = $schema->getTable('course');
25
26
        if (!$table->hasColumn('introduction')) {
27
            $this->addSql('ALTER TABLE course ADD introduction LONGTEXT DEFAULT NULL');
28
        }
29
30
        if (!$table->hasColumn('video_url')) {
31
            $this->addSql('ALTER TABLE course ADD video_url VARCHAR(255) NOT NULL');
32
            $this->addSql('UPDATE course SET video_url = ""');
33
        }
34
35
        if (!$table->hasColumn('sticky')) {
36
            $this->addSql('ALTER TABLE course ADD sticky TINYINT(1) NOT NULL');
37
        }
38
39
        if (!$table->hasIndex('idx_course_sticky')) {
40
            $this->addSql('CREATE INDEX idx_course_sticky ON course (sticky)');
41
        }
42
43
        if (!$table->hasColumn('resource_node_id')) {
44
            $this->addSql('ALTER TABLE course ADD COLUMN resource_node_id INT DEFAULT NULL;');
45
            $this->addSql(
46
                'ALTER TABLE course ADD CONSTRAINT FK_169E6FB91BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;'
47
            );
48
            $this->addSql('CREATE UNIQUE INDEX UNIQ_169E6FB91BAD783F ON course (resource_node_id);');
49
        }
50
        if ($table->hasForeignKey('FK_169E6FB912469DE2')) {
51
            $this->addSql('ALTER TABLE course DROP FOREIGN KEY FK_169E6FB912469DE2');
52
        }
53
        if ($table->hasForeignKey('IDX_169E6FB912469DE2')) {
54
            $this->addSql('DROP INDEX IDX_169E6FB912469DE2 ON course');
55
        }
56
        if ($table->hasIndex('category_code')) {
57
            $this->addSql('DROP INDEX category_code ON course');
58
        }
59
60
        if ($table->hasIndex('directory')) {
61
            $this->addSql('DROP INDEX directory ON course;');
62
        }
63
64
        $this->addSql('UPDATE course SET course_language = "en" WHERE course_language IS NULL');
65
        $this->addSql('ALTER TABLE course CHANGE course_language course_language VARCHAR(20) NOT NULL');
66
67
        $this->addSql('UPDATE course SET visibility = "0" WHERE visibility IS NULL');
68
        $this->addSql('ALTER TABLE course CHANGE visibility visibility INT NOT NULL');
69
70
        $this->addSql('UPDATE course SET creation_date = NOW() WHERE creation_date IS NULL');
71
        $this->addSql('ALTER TABLE course CHANGE creation_date creation_date DATETIME NOT NULL');
72
73
        $this->addSql('UPDATE course SET subscribe = 0 WHERE subscribe IS NULL');
74
        $this->addSql('ALTER TABLE course CHANGE subscribe subscribe TINYINT(1) NOT NULL');
75
76
        $this->addSql('UPDATE course SET unsubscribe = 0 WHERE unsubscribe IS NULL');
77
        $this->addSql('ALTER TABLE course CHANGE unsubscribe unsubscribe TINYINT(1) NOT NULL');
78
79
        if (false === $schema->hasTable('course_rel_category')) {
80
            $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;');
81
            $this->addSql('ALTER TABLE course_rel_category ADD CONSTRAINT FK_16B33772591CC992 FOREIGN KEY (course_id) REFERENCES course (id)');
82
            $this->addSql('ALTER TABLE course_rel_category ADD CONSTRAINT FK_16B337726628AD36 FOREIGN KEY (course_category_id) REFERENCES course_category (id);');
83
        }
84
85
        if ($schema->getTable('course')->hasColumn('category_id')) {
86
            // $this->addSql('ALTER TABLE course DROP category_id');
87
        }
88
89
        $table = $schema->getTable('course_rel_user');
90
91
        if (!$table->hasColumn('progress')) {
92
            $this->addSql('ALTER TABLE course_rel_user ADD progress INT NOT NULL;');
93
        }
94
95
        if (!$table->hasIndex('course_rel_user_user_id')) {
96
            $this->addSql('CREATE INDEX course_rel_user_user_id ON course_rel_user (id, user_id)');
97
        }
98
        if (!$table->hasIndex('course_rel_user_c_id_user_id')) {
99
            $this->addSql('CREATE INDEX course_rel_user_c_id_user_id ON course_rel_user (id, c_id, user_id)');
100
        }
101
102
        $table = $schema->getTable('course_category');
103
104
        // $this->addSql('ALTER TABLE course DROP category_code');
105
        $em = $this->entityManager;
106
        $sql = 'SELECT * FROM course_category';
107
        $result = $this->connection->executeQuery($sql);
108
        $all = $result->fetchAllAssociative();
109
110
        $categories = array_column($all, 'parent_id', 'id');
111
        $categoryCodeList = array_column($all, 'id', 'code');
112
113
        foreach ($categories as $categoryId => $parentId) {
114
            if (empty($parentId)) {
115
                continue;
116
            }
117
            $newParentId = $categoryCodeList[$parentId];
118
            if (!empty($newParentId)) {
119
                $this->addSql("UPDATE course_category SET parent_id = {$newParentId} WHERE id = {$categoryId}");
120
            }
121
        }
122
123
        $this->addSql('ALTER TABLE course_category CHANGE parent_id parent_id INT DEFAULT NULL;');
124
125
        if (!$table->hasForeignKey('FK_AFF87497727ACA70')) {
126
            $this->addSql(
127
                'ALTER TABLE course_category ADD CONSTRAINT FK_AFF87497727ACA70 FOREIGN KEY (parent_id) REFERENCES course_category (id) ON DELETE CASCADE'
128
            );
129
        }
130
131
        if (!$table->hasColumn('asset_id')) {
132
            $this->addSql("ALTER TABLE course_category ADD asset_id BINARY(16) DEFAULT NULL COMMENT '(DC2Type:uuid)'");
133
            $this->addSql('ALTER TABLE course_category ADD CONSTRAINT FK_AFF874975DA1941 FOREIGN KEY (asset_id) REFERENCES asset (id) ON DELETE SET NULL');
134
            $this->addSql('CREATE INDEX IDX_AFF874975DA1941 ON course_category (asset_id);');
135
        }
136
137
        $kernel = $this->container->get('kernel');
0 ignored issues
show
Bug introduced by
The method get() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

137
        /** @scrutinizer ignore-call */ 
138
        $kernel = $this->container->get('kernel');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
138
        $rootPath = $kernel->getProjectDir();
139
140
        $repo = $this->entityManager->getRepository(CourseCategory::class);
0 ignored issues
show
Bug introduced by
The method getRepository() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

140
        /** @scrutinizer ignore-call */ 
141
        $repo = $this->entityManager->getRepository(CourseCategory::class);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
141
142
        if ($table->hasColumn('image')) {
143
            foreach ($all as $category) {
144
                if (!empty($category['image'])) {
145
                    /** @var CourseCategory $categoryEntity */
146
                    $categoryEntity = $repo->find($category['id']);
147
148
                    if ($categoryEntity->hasAsset()) {
149
                        continue;
150
                    }
151
152
                    $filePath = $this->getUpdateRootPath().'/app/upload/course_category/'.$category['image'];
153
                    error_log('MIGRATIONS :: $filePath -- '.$filePath.' ...');
154
                    if ($this->fileExists($filePath)) {
155
                        $fileName = basename($filePath);
156
                        $mimeType = mime_content_type($filePath);
157
                        $file = new UploadedFile($filePath, $fileName, $mimeType, null, true);
158
                        $asset = (new Asset())
159
                            ->setCategory(Asset::COURSE_CATEGORY)
160
                            ->setTitle($fileName)
161
                            ->setFile($file)
162
                        ;
163
                        $em->persist($asset);
0 ignored issues
show
Bug introduced by
The method persist() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

163
                        $em->/** @scrutinizer ignore-call */ 
164
                             persist($asset);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
164
                        $em->flush();
165
                        $categoryEntity->setAsset($asset);
166
167
                        $em->persist($categoryEntity);
168
                        $em->flush();
169
                    }
170
                }
171
            }
172
        }
173
174
        if (!$table->hasColumn('description')) {
175
            $this->addSql('ALTER TABLE course_category ADD description LONGTEXT DEFAULT NULL');
176
        }
177
178
        $this->addSql(
179
            'ALTER TABLE course_category CHANGE auth_course_child auth_course_child VARCHAR(40) DEFAULT NULL'
180
        );
181
182
        $table = $schema->getTable('c_tool');
183
184
        if ($table->hasIndex('course')) {
185
            // $this->addSql('DROP INDEX course ON c_tool');
186
        }
187
188
        $table = $schema->getTable('c_tool_intro');
189
190
        $this->addSql("DELETE FROM c_tool_intro WHERE id = '0' ");
191
192
        // Migrate only course intro.
193
        $this->addSql("DELETE FROM c_tool_intro WHERE id <> 'course_homepage' ");
194
195
        $this->addSql('ALTER TABLE c_tool_intro CHANGE id id VARCHAR(255) DEFAULT NULL');
196
197
        if (!$table->hasColumn('resource_node_id')) {
198
            $this->addSql('ALTER TABLE c_tool_intro ADD resource_node_id INT DEFAULT NULL');
199
            $this->addSql('ALTER TABLE c_tool_intro ADD CONSTRAINT FK_D705267B1BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;');
200
            $this->addSql('CREATE UNIQUE INDEX UNIQ_D705267B1BAD783F ON c_tool_intro (resource_node_id);');
201
        }
202
203
        if (!$table->hasColumn('c_tool_id')) {
204
            $this->addSql('ALTER TABLE c_tool_intro ADD c_tool_id INT NOT NULL');
205
        }
206
    }
207
208
    public function down(Schema $schema): void {}
209
}
210