Completed
Push — master ( 997265...54aa45 )
by Yannick
01:44 queued 37s
created

Version20250905071600::up()   A

Complexity

Conditions 5
Paths 16

Size

Total Lines 25
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 14
c 1
b 0
f 0
nc 16
nop 1
dl 0
loc 25
rs 9.4888
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 Version20250905071600 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Extend ai_requests with tool_item_id, ai_model, ai_endpoint and add composite index for lookup.';
17
    }
18
19
    public function up(Schema $schema): void
20
    {
21
        $schemaManager = $this->connection->createSchemaManager();
22
        $table = $schemaManager->introspectTable('ai_requests');
23
24
        $hasToolItemId = $table->hasColumn('tool_item_id');
25
        $hasAiModel = $table->hasColumn('ai_model');
26
        $hasAiEndpoint = $table->hasColumn('ai_endpoint');
27
28
        if (!$hasToolItemId) {
29
            $this->addSql('ALTER TABLE ai_requests ADD tool_item_id BIGINT NULL AFTER tool_name');
30
        }
31
32
        if (!$hasAiModel) {
33
            $this->addSql('ALTER TABLE ai_requests ADD ai_model VARCHAR(255) NULL');
34
        }
35
36
        if (!$hasAiEndpoint) {
37
            $this->addSql('ALTER TABLE ai_requests ADD ai_endpoint TEXT NULL');
38
        }
39
40
        // Add composite index for quick lookups when rendering
41
        $indexes = array_map(static fn($i) => $i->getName(), $table->getIndexes());
42
        if (!in_array('idx_ai_requests_lookup', $indexes, true)) {
43
            $this->addSql('CREATE INDEX idx_ai_requests_lookup ON ai_requests (tool_name, tool_item_id)');
44
        }
45
    }
46
47
    public function down(Schema $schema): void
48
    {
49
        $schemaManager = $this->connection->createSchemaManager();
50
        $table = $schemaManager->introspectTable('ai_requests');
51
52
        $indexes = array_map(static fn($i) => $i->getName(), $table->getIndexes());
53
        if (in_array('idx_ai_requests_lookup', $indexes, true)) {
54
            $this->addSql('DROP INDEX idx_ai_requests_lookup ON ai_requests');
55
        }
56
57
        if ($table->hasColumn('ai_endpoint')) {
58
            $this->addSql('ALTER TABLE ai_requests DROP COLUMN ai_endpoint');
59
        }
60
        if ($table->hasColumn('ai_model')) {
61
            $this->addSql('ALTER TABLE ai_requests DROP COLUMN ai_model');
62
        }
63
        if ($table->hasColumn('tool_item_id')) {
64
            $this->addSql('ALTER TABLE ai_requests DROP COLUMN tool_item_id');
65
        }
66
    }
67
}
68