Passed
Push — master ( bcd783...e118c4 )
by Angel Fernando Quiroz
09:27 queued 14s
created

Version20211005153900   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 39
c 1
b 0
f 0
dl 0
loc 75
rs 10
wmc 13

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getDescription() 0 3 1
F up() 0 68 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
6
7
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
8
use Doctrine\DBAL\Schema\Schema;
9
10
class Version20211005153900 extends AbstractMigrationChamilo
11
{
12
    public function getDescription(): string
13
    {
14
        return 'ticket changes';
15
    }
16
17
    public function up(Schema $schema): void
18
    {
19
        // Assigned users
20
        $table = $schema->getTable('ticket_ticket');
21
        $this->addSql(
22
            'UPDATE ticket_ticket SET assigned_last_user = NULL WHERE assigned_last_user NOT IN (SELECT id FROM user)'
23
        );
24
25
        $this->addSql('UPDATE ticket_ticket SET session_id = NULL WHERE session_id = 0');
26
        $this->addSql('UPDATE ticket_ticket SET course_id = NULL WHERE course_id = 0');
27
28
        $this->addSql('DELETE FROM ticket_ticket WHERE session_id IS NOT NULL AND session_id NOT IN (SELECT id FROM session)');
29
        $this->addSql('DELETE FROM ticket_ticket WHERE course_id IS NOT NULL AND course_id NOT IN (SELECT id FROM course)');
30
31
        if (!$table->hasForeignKey('FK_EDE2C7686219A7B7')) {
32
            $this->addSql(
33
                'ALTER TABLE ticket_ticket ADD CONSTRAINT FK_EDE2C7686219A7B7 FOREIGN KEY (assigned_last_user) REFERENCES user (id) ON DELETE CASCADE'
34
            );
35
        }
36
37
        if (!$table->hasForeignKey('FK_EDE2C768591CC992')) {
38
            $this->addSql('ALTER TABLE ticket_ticket ADD CONSTRAINT FK_EDE2C768591CC992 FOREIGN KEY (course_id) REFERENCES course (id) ON DELETE CASCADE;');
39
        }
40
41
        if (!$table->hasForeignKey('FK_EDE2C768613FECDF')) {
42
            $this->addSql('ALTER TABLE ticket_ticket ADD CONSTRAINT FK_EDE2C768613FECDF FOREIGN KEY (session_id) REFERENCES session (id) ON DELETE CASCADE;');
43
        }
44
45
        if (false === $table->hasIndex('IDX_EDE2C7686219A7B7')) {
46
            $this->addSql('CREATE INDEX IDX_EDE2C7686219A7B7 ON ticket_ticket (assigned_last_user)');
47
        }
48
49
        if (!$table->hasColumn('exercise_id')) {
50
            $this->addSql("ALTER TABLE ticket_ticket ADD exercise_id INT DEFAULT NULL");
51
        }
52
53
        if (!$table->hasColumn('lp_id')) {
54
            $this->addSql("ALTER TABLE ticket_ticket ADD lp_id INT DEFAULT NULL");
55
        }
56
57
        $table = $schema->getTable('ticket_assigned_log');
58
        if (!$table->hasForeignKey('FK_54B65868700047D2')) {
59
            $this->addSql('ALTER TABLE ticket_assigned_log ADD CONSTRAINT FK_54B65868700047D2 FOREIGN KEY (ticket_id) REFERENCES ticket_ticket (id) ON DELETE CASCADE;');
60
        }
61
62
        $table = $schema->getTable('ticket_message');
63
        if (!$table->hasForeignKey('FK_BA71692D700047D2')) {
64
            $this->addSql('ALTER TABLE ticket_message ADD CONSTRAINT FK_BA71692D700047D2 FOREIGN KEY (ticket_id) REFERENCES ticket_ticket (id) ON DELETE CASCADE;');
65
        }
66
67
        // Attachments
68
        $table = $schema->getTable('ticket_message_attachments');
69
70
        if (!$table->hasForeignKey('FK_70BF9E26537A1329')) {
71
            $this->addSql('ALTER TABLE ticket_message_attachments ADD CONSTRAINT FK_70BF9E26537A1329 FOREIGN KEY (message_id) REFERENCES ticket_message (id) ON DELETE CASCADE;');
72
        }
73
74
        if (!$table->hasForeignKey('FK_70BF9E26700047D2')) {
75
            $this->addSql('ALTER TABLE ticket_message_attachments ADD CONSTRAINT FK_70BF9E26700047D2 FOREIGN KEY (ticket_id) REFERENCES ticket_ticket (id) ON DELETE CASCADE;');
76
        }
77
78
        if (!$table->hasColumn('resource_node_id')) {
79
            $this->addSql('ALTER TABLE ticket_message_attachments ADD resource_node_id INT DEFAULT NULL;');
80
            $this->addSql(
81
                'ALTER TABLE ticket_message_attachments ADD CONSTRAINT FK_70BF9E261BAD783F FOREIGN KEY (resource_node_id) REFERENCES resource_node (id) ON DELETE CASCADE;'
82
            );
83
            $this->addSql(
84
                'CREATE UNIQUE INDEX UNIQ_70BF9E261BAD783F ON ticket_message_attachments (resource_node_id);'
85
            );
86
        }
87
    }
88
}
89