diff --git a/db/migrations/2023_05_21_000001_cleanup_short_api_keys.php b/db/migrations/2023_05_21_000001_cleanup_short_api_keys.php index eee09a32..7f666eef 100644 --- a/db/migrations/2023_05_21_000001_cleanup_short_api_keys.php +++ b/db/migrations/2023_05_21_000001_cleanup_short_api_keys.php @@ -5,6 +5,7 @@ declare(strict_types=1); namespace Engelsystem\Migrations; use Engelsystem\Database\Migration\Migration; +use Illuminate\Support\Str; class CleanupShortApiKeys extends Migration { @@ -14,8 +15,14 @@ class CleanupShortApiKeys extends Migration public function up(): void { $db = $this->schema->getConnection(); - $db->table('users') - ->where($db->raw('LENGTH(api_key)'), '<=', 42) - ->update(['api_key' => '']); + foreach ($db->table('users')->get() as $user) { + if (Str::length($user->api_key) > 42) { + continue; + } + + $db->table('users') + ->where('id', $user->id) + ->update(['api_key' => bin2hex(random_bytes(32))]); + } } }