Skip to content

Commit 68efd4b

Browse files
committed
fix: migrations
1 parent e00ec2f commit 68efd4b

File tree

2 files changed

+29
-15
lines changed

2 files changed

+29
-15
lines changed
Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
<?php
22

33
use Illuminate\Database\Migrations\Migration;
4-
use Illuminate\Support\Facades\DB;
54
use Illuminate\Support\Facades\Crypt;
5+
use Illuminate\Support\Facades\DB;
66

77
class EncryptExistingPrivateKeys extends Migration
88
{
99
public function up()
1010
{
11-
DB::table('private_keys')->chunkById(100, function ($keys) {
12-
foreach ($keys as $key) {
13-
DB::table('private_keys')
14-
->where('id', $key->id)
15-
->update(['private_key' => Crypt::encryptString($key->private_key)]);
16-
}
17-
});
11+
try {
12+
DB::table('private_keys')->chunkById(100, function ($keys) {
13+
foreach ($keys as $key) {
14+
DB::table('private_keys')
15+
->where('id', $key->id)
16+
->update(['private_key' => Crypt::encryptString($key->private_key)]);
17+
}
18+
});
19+
} catch (\Exception $e) {
20+
echo 'Encrypting private keys failed.';
21+
echo $e->getMessage();
22+
}
23+
1824
}
1925
}

database/migrations/2024_09_17_111226_add_ssh_key_fingerprint_to_private_keys_table.php

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use App\Models\PrivateKey;
44
use Illuminate\Database\Migrations\Migration;
55
use Illuminate\Database\Schema\Blueprint;
6+
use Illuminate\Support\Facades\DB;
67
use Illuminate\Support\Facades\Schema;
78

89
class AddSshKeyFingerprintToPrivateKeysTable extends Migration
@@ -13,13 +14,20 @@ public function up()
1314
$table->string('fingerprint')->after('private_key')->nullable();
1415
});
1516

16-
PrivateKey::whereNull('fingerprint')->each(function ($key) {
17-
$fingerprint = PrivateKey::generateFingerprint($key->private_key);
18-
if ($fingerprint) {
19-
$key->fingerprint = $fingerprint;
20-
$key->save();
21-
}
22-
});
17+
try {
18+
DB::table('private_keys')->chunkById(100, function ($keys) {
19+
foreach ($keys as $key) {
20+
$fingerprint = PrivateKey::generateFingerprint($key->private_key);
21+
if ($fingerprint) {
22+
$key->fingerprint = $fingerprint;
23+
$key->save();
24+
}
25+
}
26+
});
27+
} catch (\Exception $e) {
28+
echo 'Generating fingerprints failed.';
29+
echo $e->getMessage();
30+
}
2331
}
2432

2533
public function down()

0 commit comments

Comments
 (0)