statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
4.07k stars 531 forks source link

Importing users into Eloquent fails due to wrong column type checked #8907

Closed mynetx closed 1 year ago

mynetx commented 1 year ago

Bug description

In Storing Users in a Database, there's these instructions:

  1. ... If you have existing file based users, edit the migration to change the id column to a uuid.

    $table->uuid('id')->change();
  2. Run the migrations:

    php artisan migrate
  3. If you have existing file based users, import them:

    php please eloquent:import-users

While trying to run the second command, I’m seeing this error:

Your users table must use UUIDs for ids in order for this migration to run

How to reproduce

Follow the same steps as above.

This is my User model:

<?php

namespace App\Models;

// use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, HasUuids, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
        'preferences' => 'json',
    ];
}

Logs

No response

Environment

Environment
Application Name: Statamic
Laravel Version: 10.30.1
PHP Version: 8.2.11
Composer Version: 2.5.5
MySQL Version: 8.0.33
Environment: local
Debug Mode: ENABLED
URL: statamic.test
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: NOT CACHED

Drivers
Broadcasting: log
Cache: statamic
Database: mysql
Logs: stack / single
Mail: smtp
Queue: sync
Session: file

Statamic
Addons: 0
Antlers: runtime
Stache Watcher: Enabled
Static Caching: Disabled
Version: 4.31.0 Solo

Installation

Starter Kit using via CLI

Antlers Parser

None

Additional details

After running the migrations, the column type of users.id is CHAR(36). The code in ImportUsers.php expects a GUID or a STRING, however.

        if (! in_array(Schema::getColumnType('users', 'id'), ['guid', 'string'])) {
            $this->error('Your users table must use UUIDs for ids in order for this migration to run');

            return;
        }
Bildschirmfoto 2023-11-01 um 17 45 16
ryanmitchell commented 1 year ago

Interesting, what mysql version are you running?

ignore me, it was there. looks like a legitimate bug.

mynetx commented 1 year ago

Well, patching it like this is definitely possible, but is it supposed to be like this?

        if (! in_array(Schema::getColumnType('users', $model->getKey()), ['char', 'guid', 'string'])) {
            $this->error('Your users table must use UUIDs for ids in order for this migration to run');

            return;
        }

Or should there even be an additional check if the char length is 36?