Migration: Trim api key length before down migration

This commit is contained in:
Igor Scheller 2023-11-13 21:08:50 +01:00 committed by msquare
parent 39dbfabea7
commit f3347ba140
1 changed files with 14 additions and 0 deletions

View File

@ -6,6 +6,7 @@ namespace Engelsystem\Migrations;
use Engelsystem\Database\Migration\Migration; use Engelsystem\Database\Migration\Migration;
use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Str;
class ChangeApiKeyLength extends Migration class ChangeApiKeyLength extends Migration
{ {
@ -24,6 +25,19 @@ class ChangeApiKeyLength extends Migration
*/ */
public function down(): void public function down(): void
{ {
$connection = $this->schema->getConnection();
$data = $connection->table('users')->get(['id', 'api_key']);
foreach ($data as $user) {
if (Str::length($user->api_key) <= 32) {
continue;
}
$key = Str::substr($user->api_key, 0, 32);
$connection->table('users')
->where('id', $user->id)
->update(['api_key' => $key]);
}
$this->schema->table('users', function (Blueprint $table): void { $this->schema->table('users', function (Blueprint $table): void {
$table->string('api_key', 32)->change(); $table->string('api_key', 32)->change();
}); });