Passed
Push — master ( ef7ec7...a59030 )
by
unknown
16:03 queued 06:59
created

Version20251219143200::down()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
1
<?php
2
3
/* For licensing terms, see /license.txt */
4
5
declare(strict_types=1);
6
7
namespace Chamilo\CoreBundle\Migrations\Schema\V200;
8
9
use Chamilo\CoreBundle\DataFixtures\SettingsValueTemplateFixtures;
10
use Chamilo\CoreBundle\Migrations\AbstractMigrationChamilo;
11
use Doctrine\DBAL\Schema\Schema;
12
13
use const JSON_UNESCAPED_SLASHES;
14
use const JSON_UNESCAPED_UNICODE;
15
16
final class Version20251219143200 extends AbstractMigrationChamilo
17
{
18
    public function getDescription(): string
19
    {
20
        return 'Update settings_value_template JSON example for a single template and link it to settings rows (no deletes).';
21
    }
22
23
    public function up(Schema $schema): void
24
    {
25
        $targetVariable = 'score_grade_model';
26
27
        // Find the template definition from fixtures (single variable only).
28
        $templatesGrouped = SettingsValueTemplateFixtures::getTemplatesGrouped();
29
30
        $template = null;
31
        foreach ($templatesGrouped as $category => $list) {
32
            foreach ($list as $tpl) {
33
                if (!is_array($tpl)) {
34
                    continue;
35
                }
36
37
                if (($tpl['variable'] ?? null) === $targetVariable) {
38
                    $template = $tpl;
39
                    break 2;
40
                }
41
            }
42
        }
43
44
        if (null === $template) {
45
            $this->write("Template not found in SettingsValueTemplateFixtures for variable '{$targetVariable}'. No changes applied.");
46
47
            return;
48
        }
49
50
        $jsonExampleRaw = $template['json_example'] ?? null;
51
        if (!is_array($jsonExampleRaw) && !is_string($jsonExampleRaw)) {
52
            $this->write("Template '{$targetVariable}' has no usable json_example payload. No changes applied.");
53
54
            return;
55
        }
56
57
        // Store JSON example as a string in DB.
58
        $jsonExample = is_string($jsonExampleRaw)
59
            ? $jsonExampleRaw
60
            : (string) json_encode($jsonExampleRaw, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
61
62
        // Upsert into settings_value_template (no deletes).
63
        $templateId = $this->connection->fetchOne(
64
            'SELECT id FROM settings_value_template WHERE variable = ?',
65
            [$targetVariable]
66
        );
67
68
        if ($templateId) {
69
            $updated = $this->connection->executeStatement(
70
                'UPDATE settings_value_template
71
                 SET json_example = ?, updated_at = CURRENT_TIMESTAMP
72
                 WHERE id = ?',
73
                [$jsonExample, (int) $templateId]
74
            );
75
76
            $this->write("Updated settings_value_template for '{$targetVariable}' (rows: {$updated}).");
77
        } else {
78
            $inserted = $this->connection->executeStatement(
79
                'INSERT INTO settings_value_template (variable, json_example, created_at, updated_at)
80
                 VALUES (?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)',
81
                [$targetVariable, $jsonExample]
82
            );
83
84
            $templateId = $this->connection->fetchOne(
85
                'SELECT id FROM settings_value_template WHERE variable = ?',
86
                [$targetVariable]
87
            );
88
89
            $this->write("Inserted settings_value_template for '{$targetVariable}' (rows: {$inserted}, id: ".((string) $templateId).').');
90
        }
91
92
        if (!$templateId) {
93
            $this->write("Unable to resolve template id for '{$targetVariable}'. Settings will not be linked.");
94
95
            return;
96
        }
97
98
        // Link settings rows to this template (no value changes, no deletes).
99
        // Apply to all access URLs to avoid inconsistencies in multi-url setups.
100
        $linkedRows = $this->connection->executeStatement(
101
            'UPDATE settings
102
             SET value_template_id = ?
103
             WHERE variable = ?
104
               AND (subkey IS NULL OR subkey = \'\')',
105
            [(int) $templateId, $targetVariable]
106
        );
107
108
        $this->write("Linked settings.value_template_id for '{$targetVariable}' (rows: {$linkedRows}).");
109
    }
110
111
    public function down(Schema $schema): void
112
    {
113
        // Intentionally no-op to avoid deleting or reverting data.
114
        $this->write('No-op down(): this migration only updates/link templates and does not remove data.');
115
    }
116
}
117