Passed
Push — master ( 5bda23...cfd449 )
by Yannick
09:11
created

Version20250926142500   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 43
rs 10
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A down() 0 4 1
A up() 0 25 1
A getDescription() 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\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Schema\Schema;
11
12
final class Version20250926142500 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return "Fix mis-labeled 'Português do Brasil': change isocode from pt_PT to pt_BR only when pt_PT appears twice and pt_BR doesn't exist.";
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        // Convert the Brazilian row from pt_PT -> pt_BR
22
        // Only do it when there are at least 2 rows with isocode 'pt_PT' and there's no existing 'pt_BR'
23
        $this->addSql("
24
            UPDATE language l
25
            JOIN (
26
                SELECT id
27
                FROM language
28
                WHERE isocode = 'pt_PT'
29
                  AND (
30
                        original_name = 'Português do Brasil'
31
                        OR english_name LIKE '%Brazil%'
32
                        OR english_name LIKE '%brazilian%'
33
                      )
34
                LIMIT 1
35
            ) b ON b.id = l.id
36
            SET l.isocode = 'pt_BR'
37
            WHERE
38
                (SELECT COUNT(*) FROM language WHERE isocode = 'pt_PT') > 1
39
                AND (SELECT COUNT(*) FROM language WHERE isocode = 'pt_BR') = 0
40
        ");
41
42
        // Normalize english_name for Brazilian row (optional, harmless if already set)
43
        $this->addSql("
44
            UPDATE language
45
            SET english_name = 'brazilian portuguese'
46
            WHERE isocode = 'pt_BR'
47
              AND (english_name IS NULL OR english_name = '' OR english_name LIKE '%brazil%')
48
        ");
49
    }
50
51
    public function down(Schema $schema): void
52
    {
53
        // Revert only if it looks like we performed the change (avoid clobbering a valid dataset).
54
        $this->addSql("
55
            UPDATE language l
56
            JOIN (
57
                SELECT id
58
                FROM language
59
                WHERE isocode = 'pt_BR'
60
                  AND (
61
                        original_name = 'Português do Brasil'
62
                        OR english_name LIKE '%brazil%'
63
                      )
64
                LIMIT 1
65
            ) b ON b.id = l.id
66
            SET l.isocode = 'pt_PT'
67
            WHERE (SELECT COUNT(*) FROM language WHERE isocode = 'pt_PT') = 1
68
        ");
69
    }
70
}
71