Skip to content

Commit ba9749f

Browse files
authored
Don't call Schema::defaultStringLength()
Add attention in README
1 parent 11fe6aa commit ba9749f

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

README.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,3 +295,67 @@ class DatabaseServiceProvider extends ServiceProvider
295295
|:---:|:---:|:---:|:---:|
296296
| `AlwaysModifiedInitializer`<br>**(Default)**| Master | **Slave** | Master |
297297
| `AlwaysFreshInitializer` | Slave | Slave | **Master** |
298+
299+
## Attention
300+
301+
### Don't call `Schema::defaultStringLength()` in `ServiceProvider::boot()`
302+
303+
#### Problem
304+
305+
Assume that you have the following `ServiceProvider`.
306+
307+
```php
308+
<?php
309+
310+
namespace App\Providers;
311+
312+
use Illuminate\Support\Facades\Schema;
313+
use Illuminate\Support\ServiceProvider;
314+
315+
class AppServiceProvider extends ServiceProvider
316+
{
317+
/**
318+
* Bootstrap any application services.
319+
*
320+
* @return void
321+
*/
322+
public function boot()
323+
{
324+
Schema::defaultStringLength(191);
325+
}
326+
}
327+
```
328+
329+
If you run `composer install` or directly call `php artisan pacakge:discover`, it will unexpectedly use caches. It will trigger errors when we execute the command in the environment unreachable to the cache repository.
330+
331+
```
332+
RedisException : Operation timed out
333+
```
334+
335+
#### Solution
336+
337+
Directly use **`Illuminate\Database\Schema\Builder`**. Don't call via `Illuminate\Support\Facades\Schema` Facade.
338+
339+
```diff
340+
<?php
341+
342+
namespace App\Providers;
343+
344+
-use Illuminate\Support\Facades\Schema;
345+
+use Illuminate\Database\Schema\Builder as SchemaBuilder;
346+
use Illuminate\Support\ServiceProvider;
347+
348+
class AppServiceProvider extends ServiceProvider
349+
{
350+
/**
351+
* Bootstrap any application services.
352+
*
353+
* @return void
354+
*/
355+
public function boot()
356+
{
357+
- Schema::defaultStringLength(191);
358+
+ SchemaBuilder::defaultStringLength(191);
359+
}
360+
}
361+
```

0 commit comments

Comments
 (0)