fix: migrations

This commit is contained in:
Andras Bacsai
2024-09-23 19:50:26 +02:00
parent e00ec2f75b
commit 68efd4b553
2 changed files with 29 additions and 15 deletions

View File

@@ -1,19 +1,25 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\DB;
class EncryptExistingPrivateKeys extends Migration
{
public function up()
{
DB::table('private_keys')->chunkById(100, function ($keys) {
foreach ($keys as $key) {
DB::table('private_keys')
->where('id', $key->id)
->update(['private_key' => Crypt::encryptString($key->private_key)]);
}
});
try {
DB::table('private_keys')->chunkById(100, function ($keys) {
foreach ($keys as $key) {
DB::table('private_keys')
->where('id', $key->id)
->update(['private_key' => Crypt::encryptString($key->private_key)]);
}
});
} catch (\Exception $e) {
echo 'Encrypting private keys failed.';
echo $e->getMessage();
}
}
}

View File

@@ -3,6 +3,7 @@
use App\Models\PrivateKey;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
class AddSshKeyFingerprintToPrivateKeysTable extends Migration
@@ -13,13 +14,20 @@ class AddSshKeyFingerprintToPrivateKeysTable extends Migration
$table->string('fingerprint')->after('private_key')->nullable();
});
PrivateKey::whereNull('fingerprint')->each(function ($key) {
$fingerprint = PrivateKey::generateFingerprint($key->private_key);
if ($fingerprint) {
$key->fingerprint = $fingerprint;
$key->save();
}
});
try {
DB::table('private_keys')->chunkById(100, function ($keys) {
foreach ($keys as $key) {
$fingerprint = PrivateKey::generateFingerprint($key->private_key);
if ($fingerprint) {
$key->fingerprint = $fingerprint;
$key->save();
}
}
});
} catch (\Exception $e) {
echo 'Generating fingerprints failed.';
echo $e->getMessage();
}
}
public function down()