Passed
Pull Request — master (#6100)
by Angel Fernando Quiroz
11:43
created

Version20250221113400::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
/* 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\Migrations\AbstractMigrationChamilo;
10
use Doctrine\DBAL\Schema\Schema;
11
12
class Version20250221113400 extends AbstractMigrationChamilo
13
{
14
    public function getDescription(): string
15
    {
16
        return 'Extend the user.auth_source field';
17
    }
18
19
    /**
20
     * @inheritDoc
21
     */
22
    public function up(Schema $schema): void
23
    {
24
        $this->addSql('CREATE TABLE user_auth_source (id INT AUTO_INCREMENT NOT NULL, url_id INT NOT NULL, user_id INT NOT NULL, authentication VARCHAR(255) NOT NULL, INDEX IDX_D632110481CFDAE7 (url_id), INDEX IDX_D6321104A76ED395 (user_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB ROW_FORMAT = DYNAMIC');
25
        $this->addSql('ALTER TABLE user_auth_source ADD CONSTRAINT FK_D632110481CFDAE7 FOREIGN KEY (url_id) REFERENCES access_url (id)');
26
        $this->addSql('ALTER TABLE user_auth_source ADD CONSTRAINT FK_D6321104A76ED395 FOREIGN KEY (user_id) REFERENCES user (id)');
27
28
        $rows = $this->connection
29
            ->executeQuery(
30
                'SELECT u.id user_id, u.auth_source authentication, uru.access_url_id url_id
31
                    FROM user u LEFT JOIN  access_url_rel_user uru ON u.id = uru.user_id'
32
            )
33
            ->fetchAllAssociative();
34
35
        foreach ($rows as $row) {
36
            $row['url_id'] ??= 1;
37
38
            $this->addSql(
39
                'INSERT INTO user_auth_source (user_id, authentication, url_id) VALUES (:user_id, :authentication, :url_id)',
40
                $row
41
            );
42
        }
43
44
        $this->addSql('ALTER TABLE user DROP auth_source');
45
    }
46
}
47