Skip to content

Commit

Permalink
add DisablePrimaryKeyRequirement listener
Browse files Browse the repository at this point in the history
  • Loading branch information
danieletulone committed Jun 13, 2023
1 parent 8d44aae commit a9a745c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,24 @@ composer require danieletulone/laravel-toolkit

## Usage

### Disable Primary Key

Useful for disable the primary key requirement. This is useful for when you want to migrate tables in a db service the Digital Ocean's one.

Register the DisablePrimaryKeyRequirement listener for MigrationStarted event in App\Providers\EventServiceProvider:

1. Add the use:

```php
use App\Listeners\DisablePrimaryKeyRequirement;
use Illuminate\Database\Events\MigrationStarted;
```

2. Add this lines in $listen array:
```php
// Usage description here
MigrationStarted::class => [
DisablePrimaryKeyRequirement::class,
]
```

### Testing
Expand Down
26 changes: 26 additions & 0 deletions src/Listeners/DisablePrimaryKeyRequirement.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace Danieletulone\LaravelToolkit\Listeners;

use Illuminate\Support\Facades\DB;

/**
* This event listener is used to disable the requirement
* for tables in the database to have a primary key defined.
* Required by DigitalOcean to work correctly.
*/
class DisablePrimaryKeyRequirement
{
/**
* Handle the event.
*
* @param object $event
* @return void
*/
public function handle($event)
{
if (app()->environment('production')) {
DB::statement('SET SESSION sql_require_primary_key=0');
}
}
}

0 comments on commit a9a745c

Please sign in to comment.