Passed
Pull Request — master (#5831)
by
unknown
07:58
created

Version20240928003000::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 1
b 0
f 0
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
use Doctrine\DBAL\Schema\Table;
12
13
final class Version20240928003000 extends AbstractMigrationChamilo
14
{
15
    public function getDescription(): string
16
    {
17
        return 'Add new fields to session and c_lp tables for handling reinscription and session repetition logic';
18
    }
19
20
    public function up(Schema $schema): void
21
    {
22
        $schemaManager = $this->connection->createSchemaManager();
23
24
        // Add fields to the 'session' table
25
        if ($schemaManager->tablesExist('session')) {
26
            $sessionTable = $schemaManager->listTableColumns('session');
27
28
            if (!isset($sessionTable['parent_id'])) {
29
                $this->addSql("ALTER TABLE session ADD parent_id INT DEFAULT NULL");
30
            }
31
            if (!isset($sessionTable['days_to_reinscription'])) {
32
                $this->addSql("ALTER TABLE session ADD days_to_reinscription INT DEFAULT NULL");
33
            }
34
            if (!isset($sessionTable['last_repetition'])) {
35
                $this->addSql("ALTER TABLE session ADD last_repetition TINYINT(1) DEFAULT 0 NOT NULL");
36
            }
37
            if (!isset($sessionTable['days_to_new_repetition'])) {
38
                $this->addSql("ALTER TABLE session ADD days_to_new_repetition INT DEFAULT NULL");
39
            }
40
        }
41
42
        // Add the field to the 'c_lp' (Learnpath) table
43
        if ($schemaManager->tablesExist('c_lp')) {
44
            $clpTable = $schemaManager->listTableColumns('c_lp');
45
46
            if (!isset($clpTable['validity_in_days'])) {
47
                $this->addSql("ALTER TABLE c_lp ADD validity_in_days INT DEFAULT NULL");
48
            }
49
        }
50
    }
51
52
    public function down(Schema $schema): void
53
    {
54
        $schemaManager = $this->connection->createSchemaManager();
55
56
        // Revert changes in the 'session' table
57
        if ($schemaManager->tablesExist('session')) {
58
            $sessionTable = $schemaManager->listTableColumns('session');
59
60
            if (isset($sessionTable['parent_id'])) {
61
                $this->addSql("ALTER TABLE session DROP COLUMN parent_id");
62
            }
63
            if (isset($sessionTable['days_to_reinscription'])) {
64
                $this->addSql("ALTER TABLE session DROP COLUMN days_to_reinscription");
65
            }
66
            if (isset($sessionTable['last_repetition'])) {
67
                $this->addSql("ALTER TABLE session DROP COLUMN last_repetition");
68
            }
69
            if (isset($sessionTable['days_to_new_repetition'])) {
70
                $this->addSql("ALTER TABLE session DROP COLUMN days_to_new_repetition");
71
            }
72
        }
73
74
        // Revert changes in the 'c_lp' table
75
        if ($schemaManager->tablesExist('c_lp')) {
76
            $clpTable = $schemaManager->listTableColumns('c_lp');
77
78
            if (isset($clpTable['validity_in_days'])) {
79
                $this->addSql("ALTER TABLE c_lp DROP COLUMN validity_in_days");
80
            }
81
        }
82
    }
83
}
84