Passed
Pull Request — master (#7328)
by
unknown
10:08
created

Version20251001221600   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 57
c 0
b 0
f 0
dl 0
loc 88
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
B up() 0 71 7
A down() 0 2 1
A getDescription() 0 3 1
A quoteIdentifier() 0 3 1
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\Command\DoctrineMigrationsMigrateCommandDecorator;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Doctrine\DBAL\Platforms\AbstractPlatform;
12
use Doctrine\DBAL\Platforms\MySQLPlatform;
13
use Doctrine\DBAL\Schema\Schema;
14
15
final class Version20251001221600 extends AbstractMigrationChamilo
16
{
17
    public function getDescription(): string
18
    {
19
        return 'Migration to drop unused tables and ensure schema consistency.';
20
    }
21
22
    public function up(Schema $schema): void
23
    {
24
        $skipAttendances = (bool) getenv(DoctrineMigrationsMigrateCommandDecorator::SKIP_ATTENDANCES_FLAG);
25
        $platform = $this->connection->getDatabasePlatform();
26
27
        // MySQL-only FK checks toggle
28
        if ($platform instanceof MySQLPlatform) {
29
            $this->addSql('SET FOREIGN_KEY_CHECKS = 0;');
30
        }
31
32
        $tablesToDrop = [
33
            'page__snapshot',
34
            'class_item',
35
            'classification__collection',
36
            'c_userinfo_def',
37
            'class_user',
38
            'faq_question',
39
            'timeline__component',
40
            'page__page',
41
            'c_online_connected',
42
            'c_permission_task',
43
            'page__bloc',
44
            'c_online_link',
45
            'c_permission_user',
46
            'c_role_user',
47
            'c_role',
48
            'page__site',
49
            'shared_survey',
50
            'media__gallery',
51
            'faq_category',
52
            'classification__context',
53
            'timeline__timeline',
54
            'classification__category',
55
            'faq_question_translation',
56
            'c_userinfo_content',
57
            'contact_category',
58
            'classification__tag',
59
            'faq_category_translation',
60
            'timeline__action_component',
61
            'media__media',
62
            'c_role_permissions',
63
            'shared_survey_question_option',
64
            'shared_survey_question',
65
            'timeline__action',
66
            'contact_category_translation',
67
            'media__gallery_media',
68
            // 'c_item_property' handled separately below
69
            'c_survey_group',
70
            'c_permission_group',
71
            'c_role_group',
72
            'track_e_open',
73
        ];
74
75
        foreach ($tablesToDrop as $table) {
76
            if ($schema->hasTable($table)) {
77
                $this->addSql('DROP TABLE '.$this->quoteIdentifier($platform, $table).';');
78
            }
79
        }
80
81
        // If skip-attendances is enabled, we keep c_item_property because the post-migration command
82
        // "chamilo:migration:migrate-attendances-fast" needs it to map attendances to courses and metadata.
83
        if ($schema->hasTable('c_item_property')) {
84
            if ($skipAttendances) {
85
                $this->write('skip-attendances enabled: keeping c_item_property for post-migration attendance fast migration.');
86
            } else {
87
                $this->addSql('DROP TABLE '.$this->quoteIdentifier($platform, 'c_item_property').';');
88
            }
89
        }
90
91
        if ($platform instanceof MySQLPlatform) {
92
            $this->addSql('SET FOREIGN_KEY_CHECKS = 1;');
93
        }
94
    }
95
96
    public function down(Schema $schema): void
97
    {
98
    }
99
100
    private function quoteIdentifier(AbstractPlatform $platform, string $name): string
101
    {
102
        return $platform->quoteIdentifier($name);
103
    }
104
}
105