yiisoft / yiisoft/db

A unversal way to work with UUIDs

Open
#1,152 5 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

severity:BC breaking type:feature
Dominant language
PHP
Stars
216
Forks
51
Avg merge
3d 10h
Merged PRs (30d)
2

Description

Proposed new feature or change

Currently you can't work universally with UUIDs:

<?php

declare(strict_types=1);

namespace App\Migration;

use Yiisoft\Db\Migration\MigrationBuilder;
use Yiisoft\Db\Migration\RevertibleMigrationInterface;

final class M251102141707Page implements RevertibleMigrationInterface
{
    public function up(MigrationBuilder $b): void
    {
        $column = $b->columnBuilder();

        $b->createTable('page', [
            'id' => $column::uuidPrimaryKey(),
            'title' => $column::string()->notNull(),
            'slug' => $column::string()->notNull()->unique(),
            'text' => $column::text()->notNull(),
            'created_at' => $column::dateTime(),
            'updated_at' => $column::dateTime(),
        ]);
    }

    public function down(MigrationBuilder $b): void
    {
        $b->dropTable('page');
    }
}

Creates a proper column for PostgreSQL and MySQL but then if you want to insert UUID, there's no universal way to do so:

$this->connection->createCommand()->insert('{{%page}}', [
            'id' => Uuid::uuid7()->getBytes(), // MySQL
            // 'id' => Uuid::uuid7()->toString(), // PostgreSQL
            'title' => 'title',
            'slug' => 'slug',
            'text' => 'text',
            'created_at' => new DateTimeImmutable(),
            'updated_at' => new DateTimeImmutable(),
        ])->execute();

MySQL accepts bytes and errors with strings. PostgreSQL accepts strings and errors with bytes.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The examples use MigrationBuilder::columnBuilder()->uuidPrimaryKey() and connection->createCommand()->insert(); start by tracing those APIs and comparing MySQL and PostgreSQL UUID parameter handling. Done means the supported database drivers accept one UUID representation through the same insertion path, with coverage for both database examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
mysql, php, postgresql
Domain
databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.