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.'); }