Refactor code structure for improved readability and maintainability
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\Crypt;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration {
|
||||
public function up(): void
|
||||
{
|
||||
DB::table('module_vmware')
|
||||
->whereNotNull('plain_password')
|
||||
->where('plain_password', '!=', '')
|
||||
->orderBy('id')
|
||||
->chunkById(200, function ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
// Skip rows already encrypted (re-running the migration, or a value
|
||||
// that happens to look like ciphertext) — avoid double-encrypting.
|
||||
try {
|
||||
Crypt::decryptString($row->plain_password);
|
||||
continue;
|
||||
} catch (\Throwable) {
|
||||
// not yet encrypted — fall through and encrypt it
|
||||
}
|
||||
|
||||
DB::table('module_vmware')
|
||||
->where('id', $row->id)
|
||||
->update(['plain_password' => Crypt::encryptString($row->plain_password)]);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
DB::table('module_vmware')
|
||||
->whereNotNull('plain_password')
|
||||
->where('plain_password', '!=', '')
|
||||
->orderBy('id')
|
||||
->chunkById(200, function ($rows) {
|
||||
foreach ($rows as $row) {
|
||||
try {
|
||||
$plain = Crypt::decryptString($row->plain_password);
|
||||
} catch (\Throwable) {
|
||||
continue;
|
||||
}
|
||||
|
||||
DB::table('module_vmware')
|
||||
->where('id', $row->id)
|
||||
->update(['plain_password' => $plain]);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Facade\SignRSA;
|
||||
use App\Models\InfraManagement;
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
|
||||
return new class extends Migration {
|
||||
public function up(): void
|
||||
{
|
||||
InfraManagement::updateOrCreate(
|
||||
['host' => '103.42.58.37'],
|
||||
[
|
||||
'name' => 'Legacy VPS',
|
||||
'username' => 'administrator',
|
||||
'password' => SignRSA::EncryptFunc('TGS@123'),
|
||||
'type' => 'legacy_vps',
|
||||
'status' => true,
|
||||
'meta_data' => ['port' => 22],
|
||||
'description' => 'Legacy direct-SSH-to-guest target (vCenter::loginVPS / change_password_windows fallback)',
|
||||
],
|
||||
);
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
InfraManagement::where('host', '103.42.58.37')->where('type', 'legacy_vps')->delete();
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user