From d64ee42ec380f02cb38c1a5229eee417e7145da9 Mon Sep 17 00:00:00 2001 From: snipe Date: Thu, 7 Mar 2024 15:04:08 +0000 Subject: [PATCH] Wrapped in a transaction Signed-off-by: snipe --- .../Commands/ToggleCustomfieldEncryption.php | 31 +++++++++++-------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/app/Console/Commands/ToggleCustomfieldEncryption.php b/app/Console/Commands/ToggleCustomfieldEncryption.php index 4a6498048c..2ba07f7bd1 100644 --- a/app/Console/Commands/ToggleCustomfieldEncryption.php +++ b/app/Console/Commands/ToggleCustomfieldEncryption.php @@ -5,6 +5,7 @@ namespace App\Console\Commands; use App\Models\Asset; use App\Models\CustomField; use Illuminate\Console\Command; +use Illuminate\Support\Facades\DB; class ToggleCustomfieldEncryption extends Command { @@ -47,22 +48,26 @@ class ToggleCustomfieldEncryption extends Command // If the field is not encrypted, make it encrypted and encrypt the data in the assets table for the // corresponding field. - if ($field->field_encrypted == 0) { - $assets = Asset::whereNotNull($field->db_column)->get(); + DB::transaction(function () use ($field) { - foreach ($assets as $asset) { - $asset->{$field->db_column} = encrypt($asset->{$field->db_column}); - $asset->save(); + if ($field->field_encrypted == 0) { + $assets = Asset::whereNotNull($field->db_column)->get(); + + foreach ($assets as $asset) { + $asset->{$field->db_column} = encrypt($asset->{$field->db_column}); + $asset->save(); + } + + $field->field_encrypted = 1; + $field->save(); + + // This field is already encrypted. Do nothing. + } else { + $this->error('The custom field ' . $field->db_column.' is already encrypted. No action was taken.'); } + }); - $field->field_encrypted = 1; - $field->save(); - - // This field is already encrypted. Do nothing. - } else { - $this->error('The custom field ' . $fieldname.' is already encrypted. No action was taken.'); - } - + // No matching column name found } else { $this->error('No matching results for unencrypted custom fields with db_column name: ' . $fieldname.'. Please check the fieldname.'); }