diff --git a/db/migrations/2022_12_06_000000_change_api_key_length.php b/db/migrations/2022_12_06_000000_change_api_key_length.php index d5e811de..f51bcb74 100644 --- a/db/migrations/2022_12_06_000000_change_api_key_length.php +++ b/db/migrations/2022_12_06_000000_change_api_key_length.php @@ -6,6 +6,7 @@ namespace Engelsystem\Migrations; use Engelsystem\Database\Migration\Migration; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Support\Str; class ChangeApiKeyLength extends Migration { @@ -24,6 +25,19 @@ class ChangeApiKeyLength extends Migration */ 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 { $table->string('api_key', 32)->change(); });