Wrapped in a transaction

Signed-off-by: snipe <snipe@snipe.net>
This commit is contained in:
snipe 2024-03-07 15:04:08 +00:00
parent fe08f39900
commit d64ee42ec3

View file

@ -5,6 +5,7 @@ namespace App\Console\Commands;
use App\Models\Asset; use App\Models\Asset;
use App\Models\CustomField; use App\Models\CustomField;
use Illuminate\Console\Command; use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
class ToggleCustomfieldEncryption extends Command 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 // If the field is not encrypted, make it encrypted and encrypt the data in the assets table for the
// corresponding field. // corresponding field.
if ($field->field_encrypted == 0) { DB::transaction(function () use ($field) {
$assets = Asset::whereNotNull($field->db_column)->get();
foreach ($assets as $asset) { if ($field->field_encrypted == 0) {
$asset->{$field->db_column} = encrypt($asset->{$field->db_column}); $assets = Asset::whereNotNull($field->db_column)->get();
$asset->save();
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; // No matching column name found
$field->save();
// This field is already encrypted. Do nothing.
} else {
$this->error('The custom field ' . $fieldname.' is already encrypted. No action was taken.');
}
} else { } else {
$this->error('No matching results for unencrypted custom fields with db_column name: ' . $fieldname.'. Please check the fieldname.'); $this->error('No matching results for unencrypted custom fields with db_column name: ' . $fieldname.'. Please check the fieldname.');
} }