Skip to content

Ajouter un champs uuid à une table déjà existant🔗

On peut avoir le besoin d'ajouter une colonne uuid (v4) à une table. C'est souvent un champ unique et NOT NULL. Le problème que l'on rencontre à ce moment la est de peupler le champ.

La solution se passe en 3 étapes

  • ajouter le champ à la table
  • peuplé le champ
  • ajouter un index pour rendre unique le champ

Ajouter le champs à la table🔗

Utiliser un classique ALTER TABLE

1
    ALTER TABLE [MY_TABLE_NAME] ADD uuid VARCHAR(36) NOT NULL

Peuplé mon champs🔗

Pour peupler le champ nous allons nous servir d'une fonction SQL. Elle permettra de générer un uuid au format voulu. Dans notre exemple, nous allons ajouter une fonction ajouter un uuid v4.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
CREATE DEFINER=CURRENT_USER FUNCTION uuid_v4()
    RETURNS CHAR(36)
    LANGUAGE SQL  DETERMINISTIC  CONTAINS SQL  SQL SECURITY INVOKER
BEGIN
    SET @h1 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
    SET @h2 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
    SET @h3 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
    SET @h6 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
    SET @h7 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');
    SET @h8 = LPAD(HEX(FLOOR(RAND() * 0xffff)), 4, '0');

    SET @h4 = CONCAT('4', LPAD(HEX(FLOOR(RAND() * 0x0fff)), 3, '0'));

    SET @h5 = CONCAT(HEX(FLOOR(RAND() * 4 + 8)),
                LPAD(HEX(FLOOR(RAND() * 0x0fff)), 3, '0'));

    RETURN LOWER(CONCAT(
        @h1, @h2, '-', @h3, '-', @h4, '-', @h5, '-', @h6, @h7, @h8
    ));
END;

Ajouter un index pour rendre unique le champs🔗

On finit par ajouter un index pour rendre notre champ unique.

1
`CREATE UNIQUE INDEX [MY_INDEX_NAME] ON [MY_TABLE_NAME] (uuid)`

L'intégration à une migration🔗

Pour l'utiliser dans une migration passons par un trait.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
trait UuidTrait
{
    private function requireUuidFunction()
    {
        // Add uuid_v4() function
        $this->addSql('DROP FUNCTION IF EXISTS uuid_v4');
        $this->addSql(file_get_contents(__DIR__ . '/sql/uuid_v4.sql'));

        // Check if uuid_v4 function is available
        $this->addSql('SELECT uuid_v4()');
    }
}

Ce qui donne notre migration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php

namespace DoctrineMigrations;

use App\Infra\Bridge\Uuid\UuidTrait;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

final class VersionXXXXXXXXXXXXXXXX extends AbstractMigration
{
    use UuidTrait;

    public function getDescription(): string
    {
        return 'Add uuid on [MY_TABLE_NAME]';
    }

    public function up(Schema $schema): void
    {
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

        $this->requireUuidFunction();
        $this->addSql('ALTER TABLE [MY_TABLE_NAME] ADD uuid VARCHAR(255) NOT NULL');
        $this->addSql('UPDATE [MY_TABLE_NAME] SET uuid = uuid_v4()');
        $this->addSql('CREATE UNIQUE INDEX [MY_INDEX_NAME] ON [MY_TABLE_NAME] (uuid)');
    }

    public function down(Schema $schema): void
    {
        $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

        $this->addSql('DROP INDEX [MY_INDEX_NAME] ON customer_customers');
        $this->addSql('ALTER TABLE [MY_TABLE_NAME] DROP uuid');
    }
}

Source🔗


Last update: December 20, 2024