spatie / laravel-permission

Associate users with roles and permissions

Home Page:https://spatie.be/docs/laravel-permission

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Default string length prerequisite conflict

kkraju4u opened this issue · comments

Related to issue #1689, the Spatie's prerequisites in the official documentation recommends setting the default string length for the whole application as 125 if we're using MySQL 8.0.

But the maximum index length issue does not affect in MySQL 8.0 with Row format Dynamic or Compressed. So the new settings conflicts with the Laravel's default string length and validations, which is 255. So either we have to update the validations to 125 or check for any alternate options such as using MariaDB. That seems unnecessary in the case if we are using dynamic or compressed row format in MySQL.

Also, the limit of 1000 bytes is only applicable for MyISAM. So I think the documentation is added according to that. In the case of Redundant and Compact formats of InnoDB, the index length is further lower, which is 767 bytes. So the string length should be lowered further to 95 characters on something to avoid an error.

It would be better if you could update the documentation accordingly to avoid any further confusion.

Originally posted by @kkraju4u in #1689 (comment)

So the new settings conflicts with the Laravel's default string length and validations, which is 255. So either we have to update the validations to 125 or ...

I suppose you could bypass calling Schema::defaultStringLength(125) if you simply impose it in your migration, as shown in the migration stub comments:

$table->string('name'); // For MySQL 8.0 use string('name', 125);
$table->string('guard_name'); // For MySQL 8.0 use string('guard_name', 125);

You would still have to manually impose your own validation rules (including length limits) on any forms you create to manage those fields, as usual in any application.

But my point is that the migrations works fine with MySQL 8.0 without setting the default string length as 125 or updating the migrations, when using InnoDB with dynamic or compressed row format in MySQL. So what I'm trying to say is as per the prerequisites documentation and the migration stub comments, it's kind of misleading that if I'm using MySQL 8.0, it's mandatory to update the string limit to 125 either globally or just for the permission and roles table.

Right. I'm digging to collect enough information in order to convey the requirements as accurately as possible, without making the documentation extra-long.

I guess perhaps the simplest approach is to explain what environments/configurations require special care to avoid triggering errors caused by limitations within said environments.

Another thought: What's the actual max length "needed" for guard_name? Do people really need to accommodate 255 or 125 character guard names? Most applications I've seen use 3 chars ('web', 'api', etc) or maybe 5 ("admin").
I wonder if we hard-code it to 25 maybe that will give a happy combination?

eg, default in the migration to:

$table->string('guard_name', 25);

Or ... what's the way to query MySQL for the max index length the current db host supports for a given table?
Maybe we can query that in the migration and dynamically set things accordingly?

I couldn't find any query to get the configuration. But as you might already know, the engine and row format can be obtained from the information schema. Maybe that details can be used to set a custom configuration.

SELECT TABLE_NAME, ENGINE, ROW_FORMAT FROM information_schema.TABLES where TABLE_SCHEMA = 'schema_name_from_env' AND TABLE_NAME = 'migrations'

In my opinion, it will be easier to mention these in the documentation so that the user can decide which configuration to use. You just need to include the steps to identify the correct configuration.

So if MyISAM - name and guard_name should be 125.
InnoDB REDUNDANT or COMPACT row format - name and guard_name should be 95 // Not sure if my calculation is correct.
Use the default value for InnoDB DYNAMIC or COMPRESSED row format.

Here are some links for reference if that helps.

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.